-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexec.c
More file actions
650 lines (596 loc) · 12.6 KB
/
exec.c
File metadata and controls
650 lines (596 loc) · 12.6 KB
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
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include "exec.h"
#include <sys/types.h>
#include <signal.h>
#include <sys/wait.h>
extern pid_t shell_pgid;
extern struct termios shell_tmodes;
extern int shell_terminal;
extern int shell_is_interactive;
extern char* buildin[];
extern job* first_job;
struct passwd *pwd;
void print_process (process* p)
{
int i;
for (i = 0; p->argv[i]; ++i)
{
printf("argv%d: %s\n", i, p->argv[i]);
}
}
process* create_process (void)
{
process* p = malloc(sizeof(process));
if (!p)
{
perror ("malloc");
return NULL;
}
p->next = NULL;
p->argv = NULL;
p->completed = 0;
p->stopped = 0;
return p;
}
job* create_job ()
{
job* j = malloc(sizeof(job));
if (!j)
{
perror ("malloc");
return NULL;
}
j->first_process = NULL;
j->valid = 1;
j->id = 0;
j->infile = NULL;
j->outfile = NULL;
j->stdin = STDIN_FILENO;
j->stdout = STDOUT_FILENO;
j->stderr = STDERR_FILENO;
j->foreground = 1;
j->notified = 0;
j->pgid = 0;
return j;
}
void free_process (process* p)
{
if(!p->argv) return;
for (int i = 0; p->argv[i] && i < ARG_MAX; ++i)
{
free(p->argv[i]);
}
free(p->argv);
}
void free_job (job *j)
{
if(!j) return;
process* p = j->first_process;
while(p)
{
process* tmp = p->next;
free_process(p);
p = tmp;
}
free(j->infile);
free(j->outfile);
}
/* Find the active job with the indicated pgid. */
job *find_job (pid_t pgid)
{
job *j;
for (j = first_job; j; j = j->next)
if (j->pgid == pgid)
return j;
return NULL;
}
job *find_job_id (int id)
{
if(id < 1) return NULL;
job *j;
for (j = first_job; j; j = j->next)
if (j->id == id)
return j;
return NULL;
}
/* Return true if all processes in the job have stopped or completed. */
int job_is_stopped (job *j)
{
process *p;
for (p = j->first_process; p; p = p->next)
if (!p->completed && !p->stopped)
return 0;
return 1;
}
/* Return true if all processes in the job have completed. */
int job_is_completed (job *j)
{
process *p;
for (p = j->first_process; p; p = p->next)
if (!p->completed)
return 0;
return 1;
}
void print_job (job* j)
{
if(!j->id)
{
fprintf(stderr, "parsing failed\n");
return;
}
process *p;
int i = 0;
if(j->infile)
printf("infile: %s\n", j->infile);
if(j->outfile)
printf("outfile: %s\n", j->outfile);
for (p = j->first_process; p; p = p->next)
{
printf("pro%d\n", i);
print_process(p);
i = i + 1;
}
}
int is_buildin (process *p)
{
int i;
for (i = 0; buildin[i]; ++i)
if(strcmp(buildin[i], p->argv[0]) == 0)
return 1;
return 0;
}
/* Store the status of the process pid that was returned by waitpid.
Return 0 if all went well, nonzero otherwise. */
int mark_process_status (pid_t pid, int status)
{
job *j;
process *p;
if (pid > 0)
{
/* Update the record for the process. */
for (j = first_job; j; j = j->next)
for (p = j->first_process; p; p = p->next)
if (p->pid == pid)
{
p->status = status;
if (WIFSTOPPED (status))
p->stopped = 1;
else
{
p->completed = 1;
if (WIFSIGNALED (status))
fprintf (stderr, "%d: Terminated by signal %d.\n",
(int) pid, WTERMSIG (p->status));
}
return 0;
}
fprintf (stderr, "No child process %d.\n", pid);
return -1;
}
else if (pid == 0 || errno == ECHILD)
/* No processes ready to report. */
return -1;
else
{
/* Other weird errors. */
perror ("waitpid");
return -1;
}
}
/* Check for processes that have status information available,
without blocking. */
void update_status (void)
{
int status;
pid_t pid;
do
pid = waitpid (WAIT_ANY, &status, WUNTRACED|WNOHANG);
while (!mark_process_status (pid, status));
}
void launch_process (process *p, pid_t pgid,
int infile, int outfile, int errfile,
int foreground)
{
pid_t pid;
if (shell_is_interactive)
{
/* Put the process into the process group and give the process group
the terminal, if appropriate.
This has to be done both by the shell and in the individual
child processes because of potential race conditions. */
pid = getpid ();
if (pgid == 0) pgid = pid;
setpgid (pid, pgid);
if (foreground)
tcsetpgrp (shell_terminal, pgid);
/* Set the handling for job control signals back to the 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);
}
/* Set the standard input/output channels of the new process. */
if (infile != STDIN_FILENO)
{
dup2 (infile, STDIN_FILENO);
close (infile);
}
if (outfile != STDOUT_FILENO)
{
dup2 (outfile, STDOUT_FILENO);
close (outfile);
}
if (errfile != STDERR_FILENO)
{
dup2 (errfile, STDERR_FILENO);
close (errfile);
}
/* Exec the new process. Make sure we exit. */
execvp (p->argv[0], p->argv);
perror (p->argv[0]);
exit (1);
}
/* Check for processes that have status information available,
blocking until all processes in the given job have reported. */
void wait_for_job (job *j)
{
int status;
pid_t pid;
do
pid = waitpid (-j->pgid, &status, WUNTRACED);
while (!mark_process_status (pid, status)
&& !job_is_stopped (j)
&& !job_is_completed (j));
}
/* Format information about job status for the user to look at. */
void format_job_info (job *j, const char *status)
{
fprintf (stderr, "[%d] %ld %s\n", j->id, (long)j->pgid, status);
}
/* Notify the user about stopped or terminated jobs.
Delete terminated jobs from the active job list. */
void do_job_notification (void)
{
job *j, *jlast, *jnext;
/* Update status information for child processes. */
update_status ();
jlast = NULL;
for (j = first_job; j; j = jnext)
{
jnext = j->next;
/* If all processes have completed, tell the user the job has
completed and delete it from the list of active jobs. */
if (job_is_completed (j)) {
if (!j->foreground)
format_job_info (j, "Done");
if (jlast)
jlast->next = jnext;
else
first_job = jnext;
free_job (j);
}
/* Notify the user about stopped jobs,
marking them so that we won’t do this more than once. */
else if (job_is_stopped (j) && !j->notified)
{
format_job_info (j, "Stopped");
j->notified = 1;
jlast = j;
}
/* Don’t say anything about jobs that are still running. */
else
jlast = j;
}
}
/* Put job j in the foreground. If cont is nonzero,
restore the saved terminal modes and send the process group a
SIGCONT signal to wake it up before we block. */
void put_job_in_foreground (job *j, int cont)
{
j->foreground = 1;
/* Put the job into the foreground. */
tcsetpgrp (shell_terminal, j->pgid);
/* Send the job a continue signal, if necessary. */
if (cont)
{
tcsetattr (shell_terminal, TCSADRAIN, &j->tmodes);
if (kill (- j->pgid, SIGCONT) < 0)
perror ("kill (SIGCONT)");
}
/* Wait for it to report. */
wait_for_job (j);
/* Put the shell back in the foreground. */
tcsetpgrp (shell_terminal, shell_pgid);
/* Restore the shell’s terminal modes. */
tcgetattr (shell_terminal, &j->tmodes);
tcsetattr (shell_terminal, TCSADRAIN, &shell_tmodes);
}
/* Put a job in the background. If the cont argument is true, send
the process group a SIGCONT signal to wake it up. */
void put_job_in_background (job *j, int cont)
{
/* Send the job a continue signal, if necessary. */
j->foreground = 0;
if (cont)
if (kill (-j->pgid, SIGCONT) < 0)
perror ("kill (SIGCONT)");
}
/* Mark a stopped job J as being running again. */
void mark_job_as_running (job *j)
{
process *p;
for (p = j->first_process; p; p = p->next)
p->stopped = 0;
j->notified = 0;
}
/* Continue the job J. */
void continue_job (job *j, int foreground)
{
mark_job_as_running (j);
if (foreground)
put_job_in_foreground (j, 1);
else
put_job_in_background (j, 1);
}
void lauch_buildin (process *p, int infile, int outfile, int errfile)
{
/* Set the standard input/output channels of the new process. */
if (strcmp (p->argv[0], "cd") == 0)
{
char *cd_path = NULL;
if(!p->argv[1])
cd_path = strdup(getenv("HOME"));
else if (p->argv[1][0] == '~')
{
cd_path = malloc(strlen(getenv("HOME")) + strlen (p->argv[1]));
strcpy (cd_path, getenv("HOME"));
strncpy (cd_path + strlen(getenv("HOME")), p->argv[1] + 1, strlen(p->argv[1]));
}
else
cd_path = strdup(p->argv[1]);
if (chdir(cd_path) < 0)
perror("cd:");
free(cd_path);
}
else if (strcmp(p->argv[0], "exit") == 0)
{
update_status();
exit(0);
}
else if (strcmp(p->argv[0], "quit") == 0)
{
update_status();
exit(0);
}
else if (strcmp(p->argv[0], "jobs") == 0)
{
if (p->argv[1])
{
int i;
int id;
job* j;
for (i = 1; p->argv[i]; ++i)
{
id = atoi(p->argv[i]);
j = find_job_id(id);
if (j)
{
if(!job_is_completed(j))
{
if(job_is_stopped(j))
{
dprintf(outfile, "[%d] %ld Stopped\n", j->id, (long)j->pgid);
}
else
{
dprintf(outfile, "[%d] %ld Running\n", j->id, (long)j->pgid);
}
}
else
dprintf(errfile, "jobs: %s : no such job\n", p->argv[i]);
}
else
dprintf(errfile, "jobs: %s : no such job\n", p->argv[i]);
}
return;
}
job *j;
/* Update status information for child processes. */
update_status ();
for (j = first_job; j; j = j->next)
{
if(!job_is_completed(j) && j->id)
{
if(job_is_stopped(j))
{
dprintf(outfile, "[%d] %ld Stopped\n", j->id, (long)j->pgid);
}
else
{
dprintf(outfile, "[%d] %ld Running\n", j->id, (long)j->pgid);
}
}
}
}
else if (strcmp(p->argv[0], "fg") == 0)
{
if (p->argv[1])
{
int i;
int id;
job* j;
for (i = 1; p->argv[i]; ++i)
{
id = atoi(p->argv[i]);
j = find_job_id(id);
if (j)
{
if(!job_is_completed(j) && job_is_stopped(j))
continue_job(j, 1);
else
dprintf(errfile, "fg: %s : no such job\n", p->argv[i]);
}
else
dprintf(errfile, "fg: %s : no such job\n", p->argv[i]);
}
return;
}
job *j;
job *jlast = NULL;
/* Update status information for child processes. */
update_status ();
for (j = first_job; j; j = j->next)
{
if(!job_is_completed(j) && j->id)
{
if(job_is_stopped(j))
{
jlast = j;
}
}
}
if(jlast)
continue_job(jlast, 1);
else
dprintf(errfile, "fg: current: no such job\n");
}
else if (strcmp(p->argv[0], "bg") == 0)
{
if (p->argv[1])
{
int i;
int id;
job* j;
for (i = 1; p->argv[i]; ++i)
{
id = atoi(p->argv[i]);
j = find_job_id(id);
if (j)
{
if(!job_is_completed(j) && job_is_stopped(j))
continue_job(j, 0);
else
dprintf(errfile, "bg: %s : no such job\n", p->argv[i]);
}
else
dprintf(errfile, "bg: %s : no such job\n", p->argv[i]);
}
return;
}
job *j;
job *jlast = NULL;
/* Update status information for child processes. */
update_status ();
for (j = first_job; j; j = j->next)
{
if(!job_is_completed(j) && j->id)
{
if(job_is_stopped(j))
{
jlast = j;
}
}
}
if(jlast)
continue_job(jlast, 0);
else
dprintf(errfile, "bg: current: no such job\n");
}
}
void launch_job (job *j, int foreground, int *id)
{
process *p;
pid_t pid;
int mypipe[2], infile, outfile;
if (j->infile)
{
j->stdin = open(j->infile, O_RDONLY);
if (j->stdin < 0)
{
perror(j->infile);
exit(1);
}
}
if (j->outfile)
{
j->stdout = open(j->outfile, O_WRONLY|O_CREAT|O_TRUNC, 0666);
if (j->stdout < 0)
{
perror(j->outfile);
exit(1);
}
}
infile = j->stdin;
for (p = j->first_process; p; p = p->next)
{
/* Set up pipes, if necessary. */
if (p->next)
{
if (pipe (mypipe) < 0)
{
perror ("pipe");
exit (1);
}
outfile = mypipe[1];
}
else
outfile = j->stdout;
if (is_buildin(p))
{
lauch_buildin(p, infile, outfile, j->stderr);
p->completed = 1;
}
else
{
/* Fork the child processes. */
pid = fork ();
if (pid == 0)
/* This is the child process. */
launch_process (p, j->pgid, infile,
outfile, j->stderr, foreground);
else if (pid < 0)
{
/* The fork failed. */
perror ("fork");
exit (1);
}
else
{
/* This is the parent process. */
p->pid = pid;
if (shell_is_interactive)
{
if (!j->pgid)
{
j->pgid = pid;
j->id = *id;
*id = *id + 1;
}
setpgid (pid, j->pgid);
}
}
}
/* Clean up after pipes. */
if (infile != j->stdin)
close (infile);
if (outfile != j->stdout)
close (outfile);
infile = mypipe[0];
}
if (!shell_is_interactive)
wait_for_job (j);
else if (foreground)
put_job_in_foreground (j, 0);
else
{
put_job_in_background (j, 0);
format_job_info (j, "backgroud");
}
}