-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtry.c
677 lines (649 loc) · 13.9 KB
/
try.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
#include<unistd.h>
#include<termios.h>
#include<signal.h>
#include<errno.h>
#include<sys/types.h>
#include<fcntl.h>
#include<stdio.h>
#include <sys/prctl.h>
#include<stdlib.h>
#include<string.h>
#include<sys/wait.h>
char *start,*infile,*outfile; //stores the starting path from where shell is executed
char *args[100]; //the arguments list
char buf[BUFSIZ];
pid_t shell_pgid; //shell's process group id
int interactive,shell_terminal,back,redirection;
pid_t child_pid; //pid of the foreground running process (0 if shell is in foreground)
typedef struct bp{ //queue storing the details of the processes
char pname[200];
pid_t pid,pgid;
struct bp *next;
}bp;
typedef struct linkd{
char *argv[100];
char *infile,*outfile;
int argc;
struct linkd *next;
}linkd;
linkd *commands;
bp *background_process;
/*Initialisation of the shell, performs a check if the shell is interactive or not.
sets the global variables*/
void initialize_shell()
{
shell_terminal=STDERR_FILENO;back=0;
infile=outfile=NULL;
interactive=isatty(shell_terminal);
if(interactive)
{
while(tcgetpgrp(shell_terminal) != (shell_pgid=getpgrp()))
kill(- shell_pgid,SIGTTIN);
}
signal (SIGINT, SIG_IGN);
signal (SIGTSTP, SIG_IGN);
signal (SIGQUIT, SIG_IGN);
signal (SIGTTIN, SIG_IGN);
signal (SIGTTOU, SIG_IGN);
shell_pgid=getpid();
if(setpgid(shell_pgid,shell_pgid)<0)
{
perror("Can't make shell a member of it's own process group");
_exit(1);
}
tcsetpgrp(shell_terminal,shell_pgid); //gives terminal access to the shell only
}
void insert_command(int argc)
{
int i;
linkd *new=(linkd *)malloc(sizeof(linkd));
for(i=0;i<argc;i++)
{
new->argv[i]=(char *)malloc(100);
strcpy(new->argv[i],args[i]);
}
new->argv[i]=NULL;
if(infile!=NULL)
{
new->infile=(char *)malloc(100);
strcpy(new->infile,infile);
}
else
new->infile=NULL;
if(outfile!=NULL)
{
new->outfile=(char *)malloc(100);
strcpy(new->outfile,outfile);
}
else
new->outfile=NULL;
new->argc=argc;
new->next=NULL;
if(commands==NULL)
commands=new;
else
{
linkd *temp=commands;
while(temp->next!=NULL)
temp=temp->next;
temp->next=new;
}
}
/*insert a new process into the jobs table*/
void insert_process(char name[200],pid_t pid,pid_t pgid)
{
bp *new=(bp *)malloc(sizeof(bp));
strcpy(new->pname,name);
new->pid=pid;
new->pgid=pgid;
new->next=NULL;
if(background_process==NULL)
background_process=new;
else
{
bp *temp=background_process;
while(temp->next!=NULL)
temp=temp->next;
temp->next=new;
}
}
/*print the jobs in order of there starting time*/
void jobs()
{
int i=1;
bp *temp=background_process;
while(temp!=NULL)
{
fprintf(stdout,"[%d]%s[%d]\n",i++,temp->pname,temp->pid);
temp=temp->next;
}
return;
}
bp *getname(pid_t pid)
{
bp *temp=background_process;
while(temp!=NULL&&temp->pid!=pid)
temp=temp->next;
return temp;
}
pid_t get_pid(int n)
{
n--;
bp *temp=background_process;
while(temp!=NULL&&n--)
temp=temp->next;
if(temp!=NULL)
return temp->pid;
else
return -1;
}
/*remove a process from jobs table on termination*/
void remove_process(pid_t pid)
{
if(background_process!=NULL)
{
bp *temp=background_process;
if(background_process->pid==pid)
{
background_process=background_process->next;
free(temp);
}
else
{
bp *r;
while(temp!=NULL&&temp->pid!=pid)
{
r=temp;
temp=temp->next;
}
if(temp!=NULL)
{
r->next=temp->next;
free(temp);
}
else
;
}
}
}
/*prints the prompt onto the stderr*/
void getprompt()
{
char a[200],*user;
user=getenv("LOGNAME");
gethostname(a,200);
char *c,fi[100];
c=getcwd(NULL,0);
if(strstr(c,start))
{
strcpy(fi,c+strlen(start));
fprintf(stderr,"<%s@%s:~%s>",user,a,fi);
}
else
{
fprintf(stderr,"<%s@%s:%s>",user,a,c);
}
return;
}
/*handle the signals*/
void sig_handler(int signo)
{
if(signo==SIGINT)
{
fprintf(stderr,"\n");
getprompt();
}
else if(signo==SIGCHLD)
{
int status;
pid_t pid;
while((pid=waitpid(-1,&status,WNOHANG))>0) //return if no child has changed state
{
if(pid!=-1&&pid!=0)
{
if(WIFEXITED(status))
{
bp* temp=getname(pid);
if(temp!=NULL)
{
fprintf(stdout,"%s with pid %d exited normally\n",temp->pname,pid);
remove_process(pid);
}
}
else if(WIFSIGNALED(status))
{
bp* temp=getname(pid);
if(temp!=NULL)
{
fprintf(stdout,"%s with pid %d signalled to exit\n",temp->pname,pid);
remove_process(pid);
}
}
}
}
}
}
/*implementing th cd command, and getting the correct prompt path*/
int change_dir(int argc)
{
if(argc<2) //if only cd given return to start
{
if((chdir(start))<0)
{
perror("Error going to home");
return -1;
}
}
else
{
if(args[1][0]=='~') //if path starts with ~, refers to relative path from start
{
if(strlen(args[1])==1)
{
if((chdir(start))<0)
{
perror("Error going to home");
return -1;
}
}
else
{
char path[100];
strcpy(path,start);
strcat(path,args[1]+1);
if((chdir(path))<0)
{
perror("No such path exists");
return -1;
}
}
}
else if(args[1][0]!='/') //absolute path given
{
char path[100];
getcwd(path,100);
strcat(path,"/");
strcat(path,args[1]);
if((chdir(path))<0)
{
perror("No such path exists");
return -1;
}
}
else
{
if((chdir(args[1]))<0)
{
perror("No such path exists");
return -1;
}
}
}
return 0;
}
int parser(char input[10000])
{
int i1=1,ret;char *token,*str;
char *saveptr1, *saveptr2;
for(str=input,ret=0;;ret++,str=NULL)
{
token=strtok_r(str,"|",&saveptr1);
if(token==NULL)
break;
char *temp=token;
char *subtoken,*str1;
int i;
for(str1=temp,i=0;;str1=NULL,i++)
{
args[i]=strtok_r(str1," \t",&saveptr2);
if(args[i]==NULL)
break;
if(strcmp(args[i],"<")==0)
{
if((infile=strtok_r(NULL," \t",&saveptr2))==NULL)
{
perror("No input file gievn!");
return;
}
args[i--]=NULL;
}
else if(strcmp(args[i],">")==0)
{
if((outfile=strtok_r(NULL," \t",&saveptr2))==NULL)
{
perror("No output file gievn!");
return;
}
args[i--]=NULL;
}
}
if(strcmp(args[i-1],"")==0)
{
args[i-1]=NULL;
i--;
}
insert_command(i);
}
return ret;
}
/*the execution function, contains execution of binaries and user defined processes*/
int execute(int argc)
{
int p=0;
if(strcmp("quit",args[0])==0) //quit shell
_exit(0);
else if(strcmp("fg",args[0])==0) //make the job args[1] the foreground process
{
if(argc==2)
{
int status;
pid_t pgid,pid;
if((pid=get_pid(atoi(args[1])))>=0) //get the pgid of the job
{
fprintf(stderr,"%s\n",getname(pid)->pname);
pgid=getpgid(pid);
tcsetpgrp(shell_terminal,pgid); //give control of the terminal to the process
child_pid=pgid;
if(killpg(pgid,SIGCONT)<0) //send a SIGCONT signal
perror("can't continue!");
waitpid(pid,&status,WUNTRACED); //wait for it to stop or terminate
if(!WIFSTOPPED(status))
{
remove_process(pid); //if terminated remove from jobs table
child_pid=0;
}
else
fprintf(stderr,"\n[%d]+ stopped %s\n",child_pid,getname(pid)->pname); //if stopped print message
tcsetpgrp(shell_terminal,shell_pgid); //return control of terminal to the shell
}
else
fprintf(stderr,"no such job!!\n");
}
else
fprintf(stderr,"Takes Exactly one argument!!\n");
}
else if(strcmp("jobs",args[0])==0)
{
jobs();
fflush(stdout);
}
else if(strcmp("cd",args[0])==0)
{
p=change_dir(argc);
}
else if(strcmp("kjob",args[0])==0)
{
int pid;
if(argc==3) //must have three arguments
{
if((pid=get_pid(atoi(args[1])))>=0)
{
if(killpg(getpgid(pid),atoi(args[2]))<0)
perror("Signal not sent!!");
}
else
fprintf(stderr,"No such job number\n");
}
else
fprintf(stderr,"Takes Two Arguments!!\n");
}
else if(strcmp("overkill",args[0])==0) //kill all processses
{
bp *temp;
while(background_process!=NULL)
{
if(killpg(getpgid(background_process->pid),9)<0)
perror("Error killing process");
}
}
else if(strcmp("pinfo",args[0])==0)
{
FILE *fp;
char buf1[256],buf2[256];
int pid;
char name[256],state;
long unsigned int size;
if(argc==1)
{
sprintf(buf1,"/proc/%d/stat",getpid()); //open the stat and exe files
sprintf(buf2,"/proc/%d/exe",getpid());
}
else
{
sprintf(buf1,"/proc/%s/stat",(args[1]));
sprintf(buf2,"/proc/%s/exe",(args[1]));
}
if((fp=fopen(buf1,"r"))!=NULL)
{
/*scan the stat file*/
fscanf(fp,"%d %*s %c %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %lu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d",&pid,&state,&size);
fclose(fp);
printf("pid -- %d\nProcess Status -- %c\nmemory -- %lu\n",pid,state,size);
readlink(buf2,name,256);
if(strstr(name,start))
{
char fi[3]="~";
strcat(fi,name+strlen(start));
printf("Executable Path -- %s\n",fi); //print print the stuff
}
else
printf("Executable Path -- %s\n",name);
}
else
perror("No such process");
}
else
{
pid_t pid;
pid=fork(); //fork the child
if(pid<0)
{
perror("Child process not created");
p=-1;
}
else if(pid==0)
{
/*inside child*/
int run,in,out;
prctl(PR_SET_PDEATHSIG, SIGHUP); //kill the child when parent dies.(prevents formation of zombie process)
setpgid(getpid(),getpid()); //put the child into it's own process group
if(infile!=NULL)
{
in=open(infile,O_RDONLY);
dup2(in,0);
close(in);
}
if(outfile!=NULL)
{
out=open(outfile,O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IRGRP | S_IWGRP | S_IWUSR);
dup2(out,1);
close(out);
}
if(back==0)
tcsetpgrp(shell_terminal,getpid());
/*make the signals for the child as default*/
signal (SIGINT, SIG_DFL);
signal (SIGQUIT, SIG_DFL);
signal (SIGTSTP, SIG_DFL);
signal (SIGTTIN, SIG_DFL);
signal (SIGTTOU, SIG_DFL);
signal (SIGCHLD, SIG_DFL);
run=execvp(args[0],args);
if(run<0)
{
perror("Error exectuing command");
_exit(-1);
}
_exit(0);
}
if(back==0)
{
tcsetpgrp(shell_terminal,pid); //to avoid racing conditions
insert_process(args[0],pid,pid);
int status;
child_pid=pid;
waitpid(pid,&status,WUNTRACED); //wait for child till it terminates or stops
if(!WIFSTOPPED(status))
remove_process(pid);
else
fprintf(stderr,"\n[%d]+ stopped %s\n",child_pid,args[0]);
tcsetpgrp(shell_terminal,shell_pgid); //return control of terminal to the shell
}
else
insert_process(args[0],pid,pid);
}
fflush(stdout);
return p;
}
int piped_execute(int pnum,char input[10000])
{
int p=0,i,j=pnum-1,pgid,pipes[2*(pnum-1)],comc=0; //declare pipes
for(i=0;j--;i+=2)
{
if((pipe(pipes+i))<0) //open the pipes
{
perror("pipe error");
return -1;
}
}
linkd *temp=commands;
while(temp!=NULL)
{
int pid=fork(),in,out; //fork the child
if(temp->next==NULL)
insert_process(input,pid,pgid);
if(comc==0&&pid!=0)
pgid=pid;
if(pid!=0)
setpgid(pid,pgid); //set the process group id
if(pid==0)
{
/*set the signal of child to default*/
signal (SIGINT, SIG_DFL);
signal (SIGQUIT, SIG_DFL);
signal (SIGTSTP, SIG_DFL);
signal (SIGTTIN, SIG_DFL);
signal (SIGTTOU, SIG_DFL);
signal (SIGCHLD, SIG_DFL);
if(temp->outfile!=NULL) //if there is a outfile
{
out=open(temp->outfile,O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IRGRP | S_IWGRP | S_IWUSR);
dup2(out,1);
close(out);
}
else if(temp->next!=NULL) //if not the last command
{
if((dup2(pipes[2*comc+1],1))<0)
{
perror("dup2 error");
}
}
if(temp->infile!=NULL) //if there is an infile
{
in=open(temp->infile,O_RDONLY);
dup2(in,0);
close(in);
}
else if(comc!=0) //if not the first command
{
if((dup2(pipes[2*(comc-1)],0))<0)
{
perror("dup2 error");
}
}
for(i=0;i<2*(pnum-1);i++) //close all pipes in the children
close(pipes[i]);
if((execvp(temp->argv[0],temp->argv))<0)
{
perror("Cannot ececute");
_exit(-1);
}
}
else if(pid<0)
{
perror("Could not fork child");
return -1;
}
temp=temp->next; //go to next command
if(temp!=NULL)
// fprintf(stderr,"Up nxt%s\n",temp->argv[0]);
comc++;
}
int status;
for(i=0;i<2*(pnum-1);i++)
{
close(pipes[i]); //close all pipes in parent
}
if(back==0)
{
tcsetpgrp(shell_terminal,pgid); //set the terminal control to child
for(i=0;i<pnum;i++)
{
int ppp;
ppp=waitpid(-pgid,&status,WUNTRACED);
if(!WIFSTOPPED(status))
remove_process(ppp);
//else
// killpg(pgid,SIGSTOP);
}
tcsetpgrp(shell_terminal,shell_pgid); //return control to parent
}
return p;
}
int main()
{
int p,c,argc,pnum;
char input[10000],copy[10000];
memset(buf,'\0',sizeof(buf));
int a=setvbuf(stdout,buf,_IOFBF,BUFSIZ);
if(a==0)
{
fprintf(stdout,"Welcome\n");
fflush(stdout);
}
start=getcwd(NULL,0);
initialize_shell();commands=NULL;
while(1)
{
if(signal(SIGINT,sig_handler)==SIG_ERR)
perror("Signal not caught!!");
if(signal(SIGCHLD,sig_handler)==SIG_ERR)
perror("signal not caught!!");
getprompt();
c=getc(stdin);
while(isspace(c))
{
if(c=='\n')
break;
c=getc(stdin);
}
if(c=='\n')
{
fflush(stdout);
continue;
}
ungetc(c,stdin);
fgets(input,10000,stdin);
while(isspace(input[strlen(input)-1]))
input[stderr,strlen(input)-1]='\0';
if(input[strlen(input)-1]=='&') //check if background
{
back=1;
input[strlen(input)-1]='\0';
}
while(isspace(input[strlen(input)-1]))
input[stderr,strlen(input)-1]='\0';
strcpy(copy,input);
pnum=parser(input);
if(pnum==1)
p=execute(commands->argc);
else
{
p=piped_execute(pnum,copy);
}
back=0;
infile=outfile=NULL;
commands=NULL;
}
return 0;
}