-
Notifications
You must be signed in to change notification settings - Fork 0
/
pr7.4.c
399 lines (331 loc) · 10.8 KB
/
pr7.4.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
/* CMPSC 311, Spring 2012, Project 7
*
* Authors: Erich Stoekl and Andrew Moyer
* Email: ems5311@psu.edu and abm5149@psu.edu
*
* pr7.4.c: Main entry point for pr7 command shell
*
* -- forks child processes for command execution
* -- can background child processes and run multiple processes simultaneously
* -- numerous built-in functions, run "help" in shell for info
* -- dynamic process table, keeps track of indefinite number of forked processes
*
* IMPORTANT: Runs perfectly in Linux, not-so-perfectly in solaris.
*
*/
/*----------------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/types.h>
#include "cmpsc311.h"
#include "pr7_signal.h"
#include "builtin.h"
#include "pr7_table.h"
/* evaluate a command line */
int eval_line(char *cmdline, int e_flags);
/* build the _argv array */
int parse(char *buf, char *_argv[]);
int cleanup_terminated_children(void);
extern char **environ;
// variable to keep track of process table:
table_t *process_table;
// Usage function, details how program should be used.
static void usage(int status)
{
if (status == EXIT_SUCCESS)
{
printf("Usage: pr7 [-h] [-v] [-d] [-i] [-e] [-s f] [file]\n");
printf("-h help\n");
printf("-v verbose mode\n");
printf("-d developer debug mode\n");
printf("-i interactive mode\n");
printf("-e echo commands before execution\n");
printf("-s f use startup file f, default pr7.init\n");
}
else
{
fprintf(stderr, ": Try '-h' for usage information.\n");
}
exit(status);
}
// Need to install with install_signal_handler function
static void SIGINT_handler(int sig)
{
if(sig == SIGINT)
{
printf("\n");
}
else
printf("SERIOUS ERROR: SIGINT_handler received signal %d\n", sig);
}
// Handler for SIGCHLD
static void SIGCHLD_handler(int sig)
{
if(sig == SIGCHLD)
{
cleanup_terminated_children();
}
else
printf("SERIOUS ERROR: SIGCHLD_handler received signal %d\n", sig);
}
/*----------------------------------------------------------------------------*/
int main(int argc, char *argv[])
{
prog = argv[0];
int ret = EXIT_SUCCESS;
char cmdline[MAXLINE]; /* command line */
// File pointer:
extern FILE *f_p;
extern char *filename;
extern char *startfile;
extern char *prompt;
extern int pr7_debug;
pr7_debug = 0;
// Getopt declarations:
extern int optind;
extern char *optarg;
int i_flag = 0;
int e_flag = 0;
int s_flag = 0;
extern int verbose; // This was initialized to 0 in cmpsc311.c
int ch;
// Getopt switch statement to locate command line options:
optind = 1;
while((ch = (getopt(argc, argv, ":hvdies:"))) != -1)
{
switch(ch)
{
case 'h':
usage(ret);
break;
case 'v':
verbose++;
break;
case 'd':
pr7_debug++;
break;
case 'i':
i_flag++;
break;
case 'e':
e_flag++;
break;
case 's':
s_flag++;
startfile = Strdup(optarg);
break;
default:
usage(EXIT_FAILURE);
break;
}
}
// start the signal handlers:
install_signal_handler(SIGINT, SIGINT_handler);
install_signal_handler(SIGCHLD, SIGCHLD_handler);
// create the process table:
process_table = allocate_table();
if(s_flag > 0)
{
f_p = fopen(startfile, "r");
if(f_p == NULL)
{
fprintf(stderr, "%s: Cannot open file %s: %s\n", prog, filename, strerror(errno));
exit(EXIT_FAILURE);
}
}
else // open the default init file, if it exists
{
f_p = fopen("pr7.init", "r");
if(f_p == NULL)
{
fprintf(stderr, "%s: Cannot open file %s: %s\n", prog, filename, strerror(errno));
exit(EXIT_FAILURE);
}
}
// STARTFILE LOOP:
while(fgets(cmdline, MAXLINE, f_p) != NULL)
{
/* issue prompt and read command line */
if (feof(stdin)) /* end of file */
{ break; }
if((ret = eval_line(cmdline, e_flag)) == EXIT_FAILURE)
fprintf(stderr, "%s: failure in eval_line, on line %s. %s\n", prog, cmdline, strerror(errno));
}
fclose(f_p);
// Now read from either a file or stdin
// If a file is given, open it here
if(argv[optind] != NULL)
{
// there is a filename given, so read from that only.
f_p = fopen(argv[optind], "r");
if(f_p == NULL)
{
fprintf(stderr, "%s: Cannot open file %s: %s\n", prog, filename, strerror(errno));
exit(EXIT_FAILURE);
}
filename = argv[optind];
}
// Else, no file was given, so just read from stdin.
else
{
f_p = stdin;
filename = "[stdin]";
}
// MAIN PROGRAM LOOP:
prompt = argv[0];
if(f_p == stdin && i_flag > 0)
printf("%s%% ", prompt);
while(fgets(cmdline, MAXLINE, f_p) != NULL)
{
/* issue prompt and read command line */
if (feof(stdin)) /* end of file */
{ break; }
if((ret = eval_line(cmdline, e_flag)) == EXIT_FAILURE)
fprintf(stderr, "%s: failure in eval_line, on line %s. %s\n", prog, cmdline, strerror(errno));
if(f_p == stdin && i_flag > 0)
printf("%s%% ", prompt);
}
free(startfile);
return ret;
}
/*----------------------------------------------------------------------------*/
/* evaluate a command line
*
* Compare to eval() in CS:APP Fig. 8.23.
*/
int eval_line(char *cmdline, int e_flag)
{
char *_argv[MAXARGS]; /* _argv for execve(), denoted by underscore to distinguish from main argv array*/
char buf[MAXLINE]; /* holds modified command line */
int background; /* should the job run in background or foreground? */
pid_t pid; /* process id */
int ret = EXIT_SUCCESS;
strcpy(buf, cmdline); /* buf[] will be modified by parse() */
/* build the argv array */
background = parse(buf, _argv);
if(e_flag > 0)
Echo(_argv);
if (_argv[0] == NULL) /* ignore empty lines */
{ return ret; }
if (builtin(_argv, process_table) == 1) /* the work is done */
{ return ret; }
if(background)
{
block_signal(SIGINT, 1);
}
/* child runs user job */
if ((pid = fork()) == 0)
{
if (execvp(_argv[0], _argv/*, environ*/) == -1)
{
printf("%s: execve failed: %s\n", _argv[0], strerror(errno));
_exit(EXIT_FAILURE);
}
}
if (background && pid != 0) /* parent waits for foreground job to terminate */
{
printf("background process %d: %s", (int) pid, cmdline);
// Add to process table:
int err;
if( (err = insert_process_table(process_table, pid)) == -1)
{
fprintf(stderr, "insert_process_table failed: line %d: %s\n", __LINE__, strerror(errno));
}
// Passing 0 unblocks the signal.
block_signal(SIGINT, 0);
}
else
{
if (waitpid(pid, &ret, 0) == -1)
{
printf("%s: waitpid failed: %s\n", _argv[0], strerror(errno));
exit(EXIT_FAILURE);
}
// If in verbose mode, print out a detailed message:
if(verbose)
{
printf("process %d, completed normally, status %d\n", pid, ret);
}
}
return ret;
}
/*----------------------------------------------------------------------------*/
/* parse the command line and build the _argv array
*
* Compare to parseline() in CS:APP Fig. 8.24.
*/
int parse(char *buf, char *_argv[])
{
char *delim; /* points to first whitespace delimiter */
char *comment;
int argc = 0; /* number of args */
int bg; /* background job? */
char whsp[] = " \t\n\v\f\r"; /* whitespace characters */
// Look for comments:
comment = strchr(buf, '#');
if(comment)
{
buf[comment-buf] = '\n';
buf[comment-buf+1] = '\0';
}
/* Note - the trailing '\n' in buf is whitespace, and we need it as a delimiter. */
while (1) /* build the _argv list */
{
buf += strspn(buf, whsp); /* skip leading whitespace */
/* next whitespace char or NULL */
delim = strpbrk(buf, whsp);
if (delim == NULL) /* end of line */
{ break; }
_argv[argc++] = buf; /* start _argv[i] */
*delim = '\0'; /* terminate _argv[i] */
buf = delim + 1; /* start _argv[i+1]? */
}
_argv[argc] = NULL;
if (argc == 0) /* blank line */
{ return 0; }
/* should the job run in the background? */
if ((bg = (strcmp(_argv[argc-1], "&") == 0)))
{ _argv[--argc] = NULL; }
return bg;
}
// Function to check for any terminated child processes, and remove
// them from the process table
int cleanup_terminated_children()
{
pid_t pid;
int status;
int count = 0;
while (1)
{
pid = waitpid(-1, &status, WNOHANG);
if (pid == 0) /* returns 0 if no child process to wait for */
{ break; }
if (pid == -1) /* returns -1 if there was an error */
{
/* errno will have been set by waitpid() */
if (errno == ECHILD) /* no children */
{ break; }
if (errno == EINTR) /* waitpid() was interrupted by a signal */
{ /* try again */
continue;
}
else
{
printf("unexpected error in cleanup_terminated_children(): %s\n",
strerror(errno));
break;
}
}
if(pr7_debug)
printf("pid is %d, status is %d\n", (int)pid, status);
update_process_table(process_table, pid, status);
remove_process_table(process_table, pid);
count++;
}
return count;
}