-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
388 lines (323 loc) · 14.5 KB
/
main.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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <err.h>
#include <errno.h>
#include <sys/wait.h>
#include <fcntl.h>
volatile sig_atomic_t foregroundOnlyMode = 0;
bool childProcessRun = false;
int lastForegroundExitStatus = 0;
struct Command {
// Structure to store info about a user's entered command
char* commandName;
int totalArguments;
char* arguments[513];
bool input_redirect;
char* input_redirect_name;
bool output_redirect;
char* output_redirect_name;
bool sendToBackground;
};
void disableForegroundOnlyMode(int signo);
void enableForegroundOnlyMode(int signo) {
char* message = "\nEntering foreground-only mode (& is now ignored)\n: ";
write(STDOUT_FILENO, message, 52);
foregroundOnlyMode = 1;
signal(SIGTSTP, disableForegroundOnlyMode);
}
void disableForegroundOnlyMode(int signo) {
char* message = "\nExiting foreground-only mode\n: ";
write(STDOUT_FILENO, message, 32);
foregroundOnlyMode = 0;
signal(SIGTSTP, enableForegroundOnlyMode);
}
void readLine(char* inputBuffer, const char* pidString, int pidStringLength) {
// Reads in a line of input from stdin to inputBuffer that is a maximum of 2048 characters
int index = 0;
int currentChar;
currentChar = fgetc(stdin);
while (currentChar != EOF) {
if (currentChar == '\n') return;
// If we run into a $, check to see if next character is also $
if (currentChar == '$') {
// Break out if the current char is the last possible (2047th index)
if (index + 1 == 2048) return;
// Otherwise, check the next character to see if it is also '$'
int nextChar = fgetc(stdin);
if (nextChar == '\n') return;
if (nextChar == EOF) break;
if (nextChar == '$') {
// variable expansion (add pidString to inputBuffer)
for (int i = 0; i < pidStringLength; i++) {
inputBuffer[index] = pidString[i];
index++;
}
} else {
// no variable expansion (add currentChar and nextChar to inputBuffer)
inputBuffer[index] = currentChar;
inputBuffer[index + 1] = nextChar;
index += 2;
}
} else {
inputBuffer[index] = currentChar;
index++;
}
currentChar = fgetc(stdin);
}
// If EOF actually found, exit with an error. If user did Ctrl-C, clearerr and this function will return.
if (feof(stdin)) {
fprintf(stderr, "\nfgetc() found EOF; stdin must have been closed; exiting\n");
exit(1);
} else if (ferror(stdin)) {
clearerr(stdin);
}
}
int main(int argc, char* argv[]) {
// Ensure that the user doesn't provide smallsh any arguments to start the shell
if (argc != 1) {
fprintf(stderr, "Usage: %s\n", argv[0]);
exit(1);
}
struct sigaction sigtstpAction = {0};
sigset_t blockSIGTSTP = {0};
sigaddset(&blockSIGTSTP, SIGTSTP);
char inputBuffer[2048];
pid_t childPids[500];
memset(childPids, 0, sizeof(childPids));
int childPidsCount = 0;
pid_t backgroundPid;
int backgroundProcessExitStatus = 0;
// Setup ignoreMask to ignore SIGINT (Ctrl-C) for the shell and background processes
sigset_t ignoreMask = {0};
sigaddset(&ignoreMask, SIGINT);
sigprocmask(SIG_SETMASK, &ignoreMask, NULL);
// Setup sigtstpAction to toggle between enabling and disabling foreground-only mode
sigtstpAction.sa_handler = enableForegroundOnlyMode;
sigfillset(&sigtstpAction.sa_mask);
sigtstpAction.sa_flags = SA_RESTART;
sigaction(SIGTSTP, &sigtstpAction, NULL);
// Get the PID for the smallsh instance in string form for variable expansion
pid_t shellPid = getpid();
int pidStringLength = snprintf(NULL, 0, "%d", shellPid);
char pidString[pidStringLength];
sprintf(pidString, "%d", shellPid);
while (1) {
// reap any child processes before prompting the user
if (childPidsCount > 0) {
for (int i = 0; i < 500; i++) {
if (childPids[i] > 0) {
backgroundPid = waitpid(childPids[i], &backgroundProcessExitStatus, WNOHANG);
if (backgroundPid == 0) {
// this child process is not complete, continue to next
continue;
} else if (backgroundPid == -1) {
printf("error has occurred; attempted to waitpid() on pid: %d\n", childPids[i]);
fflush(stdout);
err(errno, "waitpid()");
} else {
if (WIFEXITED(backgroundProcessExitStatus)) {
printf("background pid %d is done: exit value %d\n",
backgroundPid, WEXITSTATUS(backgroundProcessExitStatus));
} else {
printf("background pid %d is done: terminated by signal %d\n",
backgroundPid, WTERMSIG(backgroundProcessExitStatus));
}
fflush(stdout);
childPids[i] = 0;
childPidsCount--;
}
}
}
}
// Prompt user for a command
fflush(stdout);
printf(": ");
fflush(stdout);
// Read in a line of user input
memset(inputBuffer, '\0', sizeof(inputBuffer));
readLine(inputBuffer, pidString, pidStringLength);
if (inputBuffer[0] == '#' || inputBuffer[0] == '\0' || inputBuffer[0] == '\n') continue;
// Initialize a struct to store the user's command and parse via strtok
struct Command userCommand;
userCommand.totalArguments = 0;
userCommand.input_redirect = false;
userCommand.output_redirect = false;
userCommand.sendToBackground = false;
char* token;
int argumentIndex = 0;
token = strtok(inputBuffer, " ");
if (token == NULL) {
continue;
}
userCommand.commandName = token;
userCommand.arguments[argumentIndex] = token;
userCommand.totalArguments++;
argumentIndex++;
// strtok until <, >, and/or &; break when NULL is found
while (1) {
token = strtok(NULL, " ");
if (token == NULL) {
break;
} else if (strcmp(token, "<") == 0) {
token = strtok(NULL, " ");
userCommand.input_redirect = true;
userCommand.input_redirect_name = token;
} else if (strcmp(token, ">") == 0) {
token = strtok(NULL, " ");
userCommand.output_redirect = true;
userCommand.output_redirect_name = token;
} else {
userCommand.arguments[argumentIndex] = token;
userCommand.totalArguments++;
argumentIndex++;
}
}
// If last argument was '&', determine if process should run in background
if (userCommand.totalArguments - 1 >= 0 && strcmp(userCommand.arguments[userCommand.totalArguments - 1], "&") == 0) {
// Send to background if command is not exit, cd, or status or if foregroundOnlyMode is disabled
if ( strcmp(userCommand.arguments[0], "exit") != 0 &&
strcmp(userCommand.arguments[0], "cd") != 0 &&
strcmp(userCommand.arguments[0], "status") != 0 &&
foregroundOnlyMode == 0 ) {
userCommand.sendToBackground = true;
}
// Replace the last argument (&) with NULL
userCommand.arguments[userCommand.totalArguments - 1] = NULL;
} else {
// Otherwise, simply ensure the last argument is NULL so exec will work
userCommand.arguments[argumentIndex] = NULL;
userCommand.totalArguments++;
}
// Handle if user entered a built-in command or other command
if (strcmp(userCommand.commandName, "cd") == 0) {
// Change directory: If user provided no arguments, chdir to $HOME, otherwise chdir to path provided
if (userCommand.arguments[1] == NULL) {
if(chdir(getenv("HOME")) != 0) err(errno, "chdir()");
} else {
if(chdir(userCommand.arguments[1]) != 0) err(errno, "chdir()");
}
} else if (strcmp(userCommand.commandName, "exit") == 0) {
// Exit smallsh: Kill any other processes or jobs that the shell started before terminating smallsh
for (int i = 0; i < 500; i++ ) {
if (childPids[i] > 0) {
kill(childPids[i], SIGTERM);
int zombieStatus;
waitpid(childPids[i], &zombieStatus, 0);
}
}
break;
} else if (strcmp(userCommand.commandName, "status") == 0) {
// Status: Prints exit value or signal of last terminated foreground signal (0 if none)
// Built-in shell commands cd, exit, and status are excluded.
if (childProcessRun) {
if (WIFEXITED(lastForegroundExitStatus)) {
printf("exit value %d\n", WEXITSTATUS(lastForegroundExitStatus));
fflush(stdout);
} else {
printf("terminated by signal %d\n", WTERMSIG(lastForegroundExitStatus));
fflush(stdout);
}
} else {
printf("exit value 0\n");
fflush(stdout);
}
} else {
// User is running a non-builtin command; fork a child processes and exec the command.
int childStatus;
pid_t spawnpid = fork();
switch (spawnpid) {
case -1: ;
perror("fork() failed!");
exit(1);
case 0: ;
sigset_t childMask = {0};
// Setup bg processes to ignore SIGINT and both bg and fg to ignore SIGTSTP
if (userCommand.sendToBackground) {
sigaddset(&childMask, SIGINT);
}
sigaddset(&childMask, SIGTSTP);
sigprocmask(SIG_SETMASK, &childMask, NULL);
// Handle input and output file redirections; /dev/null for any that are not specified
if (userCommand.input_redirect || userCommand.sendToBackground) {
int sourceFD;
if (userCommand.input_redirect) {
sourceFD = open(userCommand.input_redirect_name, O_RDONLY);
} else {
sourceFD = open("/dev/null", O_RDONLY);
}
if (sourceFD == -1) {
fprintf(stderr, "cannot open %s for input\n", userCommand.input_redirect_name);
fflush(stderr);
exit(1);
}
int result = dup2(sourceFD, 0);
if (result == -1) {
perror("source dup2()");
exit(2);
}
}
if (userCommand.output_redirect || userCommand.sendToBackground) {
int targetFD;
if (userCommand.output_redirect) {
targetFD = open(userCommand.output_redirect_name, O_WRONLY | O_CREAT | O_TRUNC, 0644);
} else {
targetFD = open("/dev/null", O_WRONLY);
}
if (targetFD == -1) {
fprintf(stderr, "cannot open %s for output\n", userCommand.output_redirect_name);
exit(1);
}
int result = dup2(targetFD, 1);
if (result == -1) {
perror("dup2");
exit(2);
}
}
// Finally, execute command and handle errors
execvp(userCommand.commandName, userCommand.arguments);
fprintf(stderr, "%s: ", userCommand.commandName);
fflush(stderr);
perror("");
exit(EXIT_FAILURE);
default: ;
// Parent: wait on fg processes and allow execution to continue for bg processes
if (userCommand.sendToBackground) {
printf("background pid is %d\n", spawnpid);
fflush(stdout);
// Add child's PID to array to later be waited on before re-prompting user
if (childPidsCount >= 500) err(errno=EAGAIN, "child processes full");
int index = 0;
while (childPids[index] != 0) {
index++;
}
if (index < 500 && childPids[index] == 0) {
childPids[index] = spawnpid;
childPidsCount++;
} else {
err(errno=EAGAIN, "child processes full");
}
spawnpid = waitpid(spawnpid, &childStatus, WNOHANG);
} else {
childProcessRun = true;
// Wait on fg process; hold foregroundOnlyMode message til after execution by blocking SIGTSTP
sigprocmask(SIG_BLOCK, &blockSIGTSTP, NULL);
spawnpid = waitpid(spawnpid, &lastForegroundExitStatus, 0);
sigprocmask(SIG_UNBLOCK, &blockSIGTSTP, NULL);
// If fg child was signaled with SIGINT, notify immediately
if (WIFSIGNALED(lastForegroundExitStatus)) {
printf("terminated by signal %d\n", WTERMSIG(lastForegroundExitStatus));
}
fflush(stdout);
}
break;
}
}
// If user entered "exit", break out to terminate smallsh
if (strcmp(userCommand.commandName, "exit") == 0) break;
}
return EXIT_SUCCESS;
}