-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshell-behavior.c
362 lines (307 loc) · 10.8 KB
/
shell-behavior.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
#include "shell-behavior.h"
void setRunningMode(int argc, char **argv) {
char asyncFlag[] = "--async";
if (ftell(stdin) == 0) {
runningMode = NON_INTERACTIVE;
} else {
if (argc == 2 && strcmp(argv[1], asyncFlag) == 0) {
runningMode = INTERACTIVE_ASYNC;
} else {
runningMode = INTERACTIVE_SYNC;
}
}
}
void writePrompt() {
if (write(STDERR_FILENO, PROMPT, strlen(PROMPT)) == -1) {
perror("Failed to write the prompt.");
exit(EXIT_FAILURE);
}
}
void setSignalHandler() {
if (signal(SIGINT, SIGINTHandler) == SIG_ERR) {
perror("Failed to set SIGINT handler.");
exit(EXIT_FAILURE);
}
if (signal(SIGTSTP, SIGTSTPHandler) == SIG_ERR) {
perror("Failed to set SIGTSTP handler.");
exit(EXIT_FAILURE);
}
/* cater to valgrind */
if (signal(SIGTTIN, SIGTTINHandler) == SIG_ERR) {
perror("Failed to set SIGTTIN handler.");
exit(EXIT_FAILURE);
}
if (signal(SIGCONT, SIGCONTHandler) == SIG_ERR) {
perror("Failed to set SIGCONT handler.");
exit(EXIT_FAILURE);
}
if (runningMode == INTERACTIVE_ASYNC) {
if (signal(SIGCHLD, SIGCHLDHandler) == SIG_ERR) {
perror("Failed to set SIGCHLD handler.");
exit(EXIT_FAILURE);
}
}
}
/* When Ctrl+C is pressed, it will send SIGINT to the running foreground job process */
void SIGINTHandler(int sig) {
if (runningMode != NON_INTERACTIVE) {
if (fgPid > 0) { // shell process
} else if (fgPid == 0) { // foreground job process
killpg(fgPid, SIGKILL);
} else { // shell process with no foreground job process
writeNewline();
writePrompt();
}
}
}
/* When Ctrl+Z is pressed, it will send SIGTSTP to the running foreground job process */
void SIGTSTPHandler(int sig) {
if (runningMode != NON_INTERACTIVE) {
if (fgPid > 0) { // shell process
} else if (fgPid == 0) { // foreground job process
#ifdef DEBUG_INFO
pid_t pid = getpid();
printf("Pid: %d received SIGTSTP.\n", pid);
#endif
killpg(fgPid, SIGSTOP);
} else { // shell process with no foreground job process
writeNewline();
writePrompt();
}
}
}
void SIGTTINHandler(int sig) {
pid_t pid = getpid();
#ifdef DEBUG_INFO
printf("Pid: %d received SIGTTIN.\n", pid);
printf("Pid: %d is stopped because of TC.\n", pid);
#endif
killpg(pid, SIGSTOP);
}
void SIGCONTHandler(int sig) {
#ifdef DEBUG_INFO
pid_t pid = getpid();
printf("Pid: %d received SIGCONT.\n", pid);
#endif
}
void SIGCHLDHandler(int sig) {
if (runningMode == INTERACTIVE_ASYNC) {
int wstatus;
pid_t pid = waitpid(-1, &wstatus, WNOHANG | WUNTRACED);
if (pid > 0) {
if (WIFEXITED(wstatus)) {
Job *finishedJob = popJobList(&_jobList, pid);
finishedJob->state = FINISHED;
appendJobList(&_finishedJobList, finishedJob);
}
}
#ifdef DEBUG_INFO
if (WIFEXITED(wstatus)) {
printf("Current pid: %d waitpid: %d EXIT\n", getpid(), pid);
} else if (WIFSIGNALED(wstatus)) {
printf("Current pid: %d waitpid: %d TERMINATED\n", getpid(), pid);
} else if (WIFSTOPPED(wstatus)) {
printf("Current pid: %d waitpid: %d STOPPED\n", getpid(), pid);
} else {
printf("Current pid: %d waitpid: %d WTF\n", getpid(), pid);
}
#endif
}
}
void readUserInput(char **line) {
char inputBuffer[MAX_LINE_LENGTH];
int numBytes = read(STDIN_FILENO, inputBuffer, MAX_LINE_LENGTH);
if (numBytes == -1) {
perror("Failed to read the user input.");
exit(EXIT_FAILURE);
}
if (numBytes == 0) { // read nothing but EOF (Ctrl + D at the beginning of the input line)
*line = NULL;
} else {
char *userInput = NULL;
if (inputBuffer[numBytes - 1] != '\n') { // input ended with EOF (Ctrl + D)
userInput = malloc(1);
userInput[0] = '\0';
*line = userInput;
} else {
if (numBytes == 1) { // read nothing but '/n'
userInput = malloc(2);
userInput[0] = '\n';
userInput[1] = '\0';
*line = userInput;
}
else {
userInput = malloc(numBytes);
for (int i = 0; i < numBytes; i++) {
if (inputBuffer[i] == '\n') {
userInput[i] = '\0';
}
else {
userInput[i] = inputBuffer[i];
}
}
*line = userInput;
}
}
}
}
LineType parseUserInput(char *line) {
LineType lineType = EXECUTE_COMMAND;
if (line == NULL) {
lineType = EXIT_SHELL;
return lineType;
}
if (line[0] == '\0') {
lineType = EMPTY_LINE;
}
return lineType;
}
LineType readAndParseUserInput(char **line) {
readUserInput(line);
return parseUserInput(*line);
}
LineType readAndParseFileInput(char **line, size_t *len) {
LineType lineType;
ssize_t numBytes = getline(line, len, stdin);
if (numBytes <= 0) {
lineType = EXIT_SHELL;
} else {
lineType = EXECUTE_COMMAND;
}
return lineType;
}
/*
In project 1, we assume that input redirection needs to go into first command
and output redirection needs to come out of last command. (According to ed#355)
As long as the command line can not be recognized by the parser, it will not be executed.
*/
int parseLine(char *line, struct parsed_command **cmd) {
int res = parse_command(line, cmd);
if (res < 0) {
perror("parse_command");
}
else if (res > 0) {
printf("syntax error: %d\n", res);
}
return res;
}
void executeLine(struct parsed_command *cmd) {
pid_t pid = fork(); // job process
fgPid = pid;
if (pid == 0) { // child process
handlePipeline(cmd);
if (runningMode != NON_INTERACTIVE) {
exit(EXIT_SUCCESS);
} else {
/* Working around solution for the bug in the non-interactive mode */
abort(); // abort() will terminate the process by raising a SIGABRT signal
}
/* The child process should end here to avoid fork spawning. */
}
if (pid > 0) { // parent process
setpgid(pid, pid);
if (cmd->is_background == false || runningMode == NON_INTERACTIVE) { // In non-interactive mode, & will be ignored
if (runningMode != NON_INTERACTIVE) {
tcsetpgrp(STDIN_FILENO, pid); // delegate the terminal control
}
int wstatus = 0;
do {
waitpid(pid, &wstatus, WUNTRACED | WCONTINUED);
} while (!WIFEXITED(wstatus) && !WIFSIGNALED(wstatus) && !WIFSTOPPED(wstatus));
if (runningMode != NON_INTERACTIVE) {
signal(SIGTTOU, SIG_IGN); // ignore the signal from UNIX when the main process come back from the background to get the terminal control
tcsetpgrp(STDIN_FILENO, getpid()); // give back the terminal control to the main process
}
if (WIFEXITED(wstatus)) {
free(cmd);
}
else if (WIFSIGNALED(wstatus)) {
if (runningMode != NON_INTERACTIVE) {
writeNewline();
}
free(cmd);
} else if (WIFSTOPPED(wstatus)) {
Job *newBackgroundJob = createJob(cmd, pid, STOPPED);
appendJobList(&_jobList, newBackgroundJob);
writeNewline();
writeJobState(newBackgroundJob);
if (runningMode == INTERACTIVE_ASYNC) {
kill(pid, SIGCONT);
}
}
} else { // cmd->is_background == true
Job *newBackgroundJob = createJob(cmd, pid, RUNNING);
appendJobList(&_jobList, newBackgroundJob);
writeJobState(newBackgroundJob);
}
fgPid = -1; // reset the foreground process
}
}
void handlePipeline(struct parsed_command *cmd) {
if (cmd->num_commands > 0) {
int pids[cmd->num_commands];
/* Set stdin/stdout redirection */
if (cmd->stdin_file != NULL) {
int inputFd = open(cmd->stdin_file, O_RDONLY);
dup2(inputFd, STDIN_FILENO);
}
if (cmd->stdout_file != NULL) {
int createMode = O_RDWR | O_CREAT;
if (cmd->is_file_append) {
createMode |= O_APPEND;
} else {
createMode |= O_TRUNC;
}
int outputFd = open(cmd->stdout_file, createMode, 0644);
dup2(outputFd, STDOUT_FILENO);
}
/* create n-1 pipes for n commands in the pipeline */
int pfds[cmd->num_commands - 1][2]; // pipe file descriptors
for (int i = 0; i < cmd->num_commands - 1; i++) {
pipe(pfds[i]);
}
/*
cmd1 ------- write -----
pfds[0] |
--> [ | ]<--
|
----- read ----- cmd2 ----- write ------
pfds[1] |
--> [ | ]<--
|
----- read ----- cmd3
...
*/
for (int idx = 0; idx < cmd->num_commands; idx++) {
pid_t pid = fork();
if (pid == 0) { // child process
if (idx != 0) {
dup2(pfds[idx -1][0], STDIN_FILENO);
}
if (idx != cmd->num_commands - 1) {
dup2(pfds[idx][1], STDOUT_FILENO);
}
for (int i = 0; i < cmd->num_commands - 1; i++) {
close(pfds[i][0]);
close(pfds[i][1]);
}
execvp(cmd->commands[idx][0], cmd->commands[idx]);
/* if the command is executed successfully , the child process will end here.*/
perror(cmd->commands[idx][0]);
exit(EXIT_FAILURE);
}
pids[idx] = pid;
}
// close all pipe ports
for (int i = 0; i < cmd->num_commands - 1; i++) {
close(pfds[i][0]);
close(pfds[i][1]);
}
int wstatus;
for (int i = 0; i < cmd->num_commands; i++) {
do {
waitpid(pids[i], &wstatus, WUNTRACED | WCONTINUED);
} while (!WIFEXITED(wstatus) && !WIFSIGNALED(wstatus));
}
}
}