-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmall-shell.c
More file actions
399 lines (361 loc) · 12.6 KB
/
Copy pathsmall-shell.c
File metadata and controls
399 lines (361 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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <signal.h>
#define N_ARGS 512 + 1
#define LEN_INPUT 2048 + 1
#define DEV_NULL "/dev/null"
/*
A global variable to control `foreground-only mode`,
which needs to be accessable in `handle_sigtstp`.
*/
int background = 1;
void handle_sigtstp(int signo) {
// toggle `foreground-only mode`
background = !background;
if (background) { write(STDOUT_FILENO, "\nExiting foreground-only mode\n", 30); }
else { write(STDOUT_FILENO, "\nEntering foreground-only mode (& is now ignored)\n", 50); }
return;
}
/*
Stores a parsed command.
*/
struct command {
char* args[N_ARGS];
char* input_file;
char* output_file;
int background;
};
/*
Creates and returns an empty `command`.
*/
struct command* create_command() {
struct command* command = malloc(sizeof(struct command));
for (int i = 0; i < N_ARGS; i++) {
command->args[i] = NULL;
}
command->input_file = NULL;
command->output_file = NULL;
command->background = 0;
return command;
}
/*
Free's a `command`'s memory.
*/
void free_command(struct command* command) {
for (int i = 0; i < N_ARGS; i++) {
free(command->args[i]);
}
free(command->input_file);
free(command->output_file);
free(command);
return;
}
/*
A linked list to store PIDs.
*/
struct background_pids {
int pid;
struct background_pids* next;
};
/*
Insert a new PID into the given `background_pids`.
*/
struct background_pids* insert_pid(struct background_pids* background_pids, int pid) {
struct background_pids* head = malloc(sizeof(struct background_pids));
head->pid = pid;
head->next = background_pids;
return head;
}
/*
Recursively traverse the linked list `background_pids`,
killing each process associated with the PID and freeing each node.
*/
void free_background_pids(struct background_pids* background_pids) {
if (background_pids) {
if (background_pids->next) { free_background_pids(background_pids->next); }
kill(background_pids->pid, SIGKILL);
free(background_pids);
}
return;
}
/*
Return a `command` parsed from the given character array.
*/
struct command* parse(char* input) {
// return an empty `command` if the input is a comment
if (!strncmp(input, "#", 1)) { return create_command(); }
// pid expansion
char buffer[21];
char* expanded_input = calloc(LEN_INPUT, sizeof(char));
char* cache = expanded_input;
// check each character for `$$`
for (int i = 0; i < strlen(input); i++) {
/*
If current and next character is `$`,
print the PID into `expanded_input` and skip the next character.
*/
if (input[i] == '$' && input[i + 1] == '$') {
sprintf(buffer, "%ld", getpid());
strcat(expanded_input, buffer);
i++;
}
// print the current character into `expanded_input`
else {
sprintf(buffer, "%c", input[i]);
strcat(expanded_input, buffer);
}
}
struct command* command = create_command();
char* token;
int arg_n = 0;
int mode = 0;
int i = strlen(expanded_input) - 2;
/*
If the second to last character is `&`,
the command should run in the background.
This character is also set to ` ` so that it is ignored during tokenizing.
*/
if (i >= 0 && expanded_input[i] == '&') {
command->background = background;
expanded_input[i] = ' ';
}
/*
There are 3 parsing modes.
If the token if `<` or `>`, it will set `mode = 1` or `mode = 2`, respectively.
`mode = 0` is the default and will insert `token` into `command->args`.
`mode = 1` and `mode = 2` replace the value of
`command->input_file` and `command->output_file`, respectively, with `token`.
*/
while (token = strtok_r(NULL, " \n", &expanded_input)) {
// change modes
if (!strcmp(token, "<")) { mode = 1; }
else if (!strcmp(token, ">")) { mode = 2; }
// token is an argument
else if (mode == 0) {
command->args[arg_n] = calloc(strlen(token) + 1, sizeof(char));
strcpy(command->args[arg_n], token);
// increment a counter so that the next arg goes into the next index
arg_n++;
}
// token is an input file
else if (mode == 1) {
command->input_file = calloc(strlen(token) + 1, sizeof(char));
strcpy(command->input_file, token);
}
// token is an output file
else if (mode == 2) {
command->output_file = calloc(strlen(token) + 1, sizeof(char));
strcpy(command->output_file, token);
}
else {
printf("Error parsing input\n");
fflush(stdout);
}
}
// `cache` is a pointer to the beginning of `expanded_input`
free(cache);
return command;
}
/*
Redirects the given `file_name`.
`flag` determines what flags to open the file with,
which will either be `O_RDONLY` or `O_WRONLY | O_CREAT`.
If a new file is created, it is given full permissions.
If `flag == (O_WRONLY | O_CREAT)`, then the file is redirected to `stdout`.
Otherwise, the file is redirected to `stdin`.
Returns `0` on success and `1` on failure.
*/
int redirect(char* file_name, int flag, char* error, int* status) {
int file_descriptor = open(file_name, flag, 0777);
// `open` failed
if (file_descriptor == -1) {
printf("bash: %s: No such file or directory\n", file_name);
fflush(stdout);
*status = 1;
return 1;
}
// `open` succeeded
else {
// redirect file to either `stdin` or `stdout`
if (dup2(file_descriptor, flag == (O_WRONLY | O_CREAT)) == -1) {
printf("Error redirecting %s\n", error);
fflush(stdout);
return 1;
}
}
return 0;
}
/*
Uses macros to determine whether the status indicates a normal exit or exit by signal,
then prints the status with a message.
*/
void print_status(int status) {
if (WIFEXITED(status)) { printf("exit value %d\n", WEXITSTATUS(status)); }
else { printf("terminated by signal %d\n", WTERMSIG(status)); } // if (WIFSIGNALED(status))
fflush(stdout);
}
/*
Registers the given signal and signal handler.
*/
void register_signal_handler(int signal, void (*handler)(int)) {
struct sigaction signal_handler = {0};
signal_handler.sa_handler = handler;
sigaction(signal, &signal_handler, NULL);
return;
}
/*
Executes the given `command`.
May modify `status` to include the most recent exit value or terminating signal.
May insert a new PID into `background_pids`.
Returns `0` to exit shell and `1` to continue.
*/
int run(struct command* command, int* status, struct background_pids** background_pids) {
// empty command, do nothing
if (!command->args[0]) {}
// stop the `main` loop
else if (!strcmp(command->args[0], "exit")) { return 0; }
// change directories
else if (!strcmp(command->args[0], "cd")) {
if (command->args[1]) { chdir(command->args[1]); }
// if no argument is given, default to the `HOME` directory
else { chdir(getenv("HOME")); }
}
// print the most recent status
else if (!strcmp(command->args[0], "status")) { print_status(*status); }
// use `fork` and `exec` to run an unknown command in a child process
else {
pid_t child_pid = fork();
switch (child_pid) {
case -1: // error
printf("Error forking process\n");
fflush(stdout);
break;
case 0: // child
// ignore sigtstp
register_signal_handler(SIGTSTP, SIG_IGN);
// default input/output file for background processes is `DEV_NULL`
if (command->background) {
if (!command->input_file) { command->input_file = DEV_NULL; }
if (!command->output_file) { command->output_file = DEV_NULL; }
}
// foreground `SIGINT` should be handled normally
else { register_signal_handler(SIGINT, SIG_DFL); }
// redirect `stdin` as read-only
if (command->input_file &&
redirect(command->input_file, O_RDONLY, "input", status)
) { exit(1); };
// redirect `stdout` as write-only and create if non-existant
if (command->output_file &&
redirect(command->output_file, O_WRONLY | O_CREAT, "output", status)
) { exit(1); };
execvp(command->args[0], command->args);
// `execvp` failed
printf("bash: %s: Command not found\n", command->args[0]);
fflush(stdout);
exit(1);
break;
default: // parent
// background
if (command->background) {
// cache the new PID to `waitpid` later
*background_pids = insert_pid(*background_pids, child_pid);
printf("background pid is %d\n", child_pid);
fflush(stdout);
}
// foreground
else {
// block `SIGTSTP` until child has finished
sigset_t sigtstp;
sigaddset(&sigtstp, SIGTSTP);
sigprocmask(SIG_BLOCK, &sigtstp, NULL);
// wait until child has finished
waitpid(child_pid, status, 0);
if (WIFSIGNALED(*status)) { print_status(*status); }
// unblock `SIGTSTP`
sigprocmask(SIG_UNBLOCK, &sigtstp, NULL);
}
}
}
return 1;
}
/*
Check to see if each background PID has exited or terminated.
If so, print a message and its status and remove it from the list of PIDs.
*/
struct background_pids* check_background(struct background_pids* background_pids) {
struct background_pids* head = background_pids;
struct background_pids* previous = NULL;
int status;
// continue until the end of the linked list
while (background_pids) {
// check the current PID
pid_t result = waitpid(background_pids->pid, &status, WNOHANG);
if (result) {
// print message
printf("background pid %d is done: ", background_pids->pid);
print_status(status);
// remove current PID from linked list
if (previous) {
previous->next = background_pids->next;
free(background_pids);
background_pids = previous;
}
else {
head = background_pids->next;
free(background_pids);
background_pids = head;
}
}
// iterate to the next node in the linked list
previous = background_pids;
if (background_pids) { background_pids = background_pids->next; }
}
return head;
}
/*
Run a small shell in a loop until the `exit` command is given.
First, check if any background processes have completed.
If so, print their PIDs and exit values and remove them from the list of background processes.
Next, prompt the user and read their input.
Finally, Parse the input to a command and run the command.
Upon exit, kill all remaining background processes.
*/
int main(int argc, char* argv[]) {
// ignore `SIGINT`
register_signal_handler(SIGINT, SIG_IGN);
// register custom handler for `SIGTSTP`
register_signal_handler(SIGTSTP, handle_sigtstp);
struct command* command;
struct background_pids* background_pids = NULL;
char input[LEN_INPUT];
int status = 0;
int running = 1;
while (running) {
// print message when background processes have completed
// update `background_pids` to remove completed PIDs
background_pids = check_background(background_pids);
// shell prompt
printf(": ");
fflush(stdout);
// read command
memset(input, '\0', sizeof(input));
!fgets(input, sizeof(input), stdin);
command = parse(input);
/*
Set `running` to `0` when the command `exit` is given,
which stops the main loop.
The previous `status` must be available for the next call,
so it is updated and cached here.
`background_pids` may be updated to include a new PID.
*/
running = run(command, &status, &background_pids);
free_command(command);
}
// also kills background processes
free_background_pids(background_pids);
return 0;
}