-
Notifications
You must be signed in to change notification settings - Fork 0
/
hw2.c
655 lines (581 loc) · 21.4 KB
/
hw2.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
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
651
652
653
654
655
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <ctype.h>
#include <signal.h>
#include <fcntl.h>
#define bufferSize 80
#define whitespace " \t\n"
#define quit "quit"
int debug = 0;
int writeToFile = 0;
int readFromFile = 0;
int appendToFile = 0;
char* inputFile;
char* outputFile;
struct Job{
// each child process owns a struct
int jobId; // assigned by shell
int processId; // returned to parent
// 1 = fg
// 0 = bg
int foreground;
// Running
// Stopped
char* status;
char** args;
int numargs;
};
struct Job* jobList[5] = {NULL};
int latestJobId = 1;
int nextJobIndex = 0;
int fg_pid = 0;
int intByKill = 0;
void cleanJobList(int endedPid){
// removes the job within joblist of the job that just ended
// shifts nextJobIndex back by 1
// free each argument, then free the argument array, then free the struct
int removeIndex;
for(int i = 0; i < nextJobIndex; i++){
if (jobList[i] -> processId == endedPid){
removeIndex = i;
}
}
for(int a = 0; a < jobList[removeIndex] -> numargs; a++){
free(jobList[removeIndex] -> args[a]);
}
free(jobList[removeIndex] -> args);
free(jobList[removeIndex]);
jobList[removeIndex] = NULL;
for(int i = 0; i < 4; i++){
// shift everything in jobList back by 1 after removing a job;
if(jobList[i] == NULL && jobList[i+1] != NULL){
jobList[i] = jobList[i+1];
jobList[i+1] = NULL;
}
}
// next available index in jobList goes back by 1
nextJobIndex -= 1;
}
void jobStatusStopped(int stoppedPid){
// updates the job with stoppedPid's status to stopped
for(int i = 0; i < nextJobIndex; i++){
if(jobList[i] -> processId == stoppedPid){
jobList[i] -> status = "Stopped";
jobList[i] -> foreground = 0;
break;
}
}
}
void resumeJobStatus(int restartPid, int fgStatus){
// updates the job's information when it's brought from stopped to fg
for(int i = 0; i < nextJobIndex; i++){
if(jobList[i] -> processId == restartPid){
jobList[i] -> status = "Running";
jobList[i] -> foreground = fgStatus;
break;
}
}
}
void INTHandler(int sig){
if (debug) printf("Handled SIGINT from %d\n", getpid());
if(getpid() == fg_pid){
kill(getpid(), SIGKILL);
}
}
void TSTPHandler(int sig){
if (debug) printf("Handled TSTP from %d\n", getpid());
if(getpid() == fg_pid){
kill(getpid(), SIGSTOP);
}
}
void shellINTHandler(int sig){
if (debug) printf("Handling INT from shell\n");
if (fg_pid > 0){
if (debug) printf("Terminating foreground process pid: %d, pgid: %d\n", fg_pid, getpgid(fg_pid));
kill(fg_pid, SIGINT);
fg_pid = 0;
}
}
void shellTSTPHandler(int sig){
if (debug) printf("Handling TSTP from shell\n");
if (fg_pid > 0){
if (debug) printf("Stopping foreground process pid: %d, pgid: %d\n", fg_pid, getpgid(fg_pid));
kill(fg_pid, SIGTSTP);
fg_pid = 0;
}
}
void SIGCHLDHandler(int sig){
if (debug) printf("SIGCHLD handler called\n");
int child_status;
int waitpid_status = waitpid(-1, &child_status, WNOHANG);
if (debug) printf("waitpid called\n");
if (debug){
if(waitpid_status < 0){
perror("waitpid");
} else if(waitpid_status > 0){
printf("Child pid: %d stopped or terminated\n", waitpid_status);
} else{
printf("No children to be reaped\n");
}
}
if(waitpid_status > 0){
if (WIFEXITED(child_status)){
if (debug) printf("Child finished and reaped\n");
// remove from job list
cleanJobList(waitpid_status);
} else if (WIFSIGNALED(child_status)){
if (debug) printf("Child was terminated and reaped\n");
// remove from job list
cleanJobList(waitpid_status);
} else if (WIFSTOPPED(child_status)){
if (debug) printf("Child was stopped\n");
}
}
}
void nonResponseCHLDHandler(int sig){
if (debug) printf("PID: %d encountered a SIGCHLD signal and ignored it\n", getpid());
}
void promptInput(char* inputBuffPtr){
printf("%s", "prompt > ");
fgets(inputBuffPtr, bufferSize, stdin);
}
// implementation for pwd shell command
void pwd(){
char* dir = getcwd(NULL, 0);
printf("%s\n", dir);
free(dir);
}
// implementation for cd shell command
void cd(char ** args, int numargs){
// cd args can be:
// {NULL, "~", f"{someDirectory}"}
if(numargs < 1 || (numargs == 1 && strcmp(args[1],"~") == 0)){
// if no arguments or argument = "~"
char *homeDirectory = getenv("HOME");
chdir(homeDirectory);
} else if(numargs > 2){
// if too many arguments (>1)
printf("Error: Too many arguments");
} else{
// fix
// if 1 argument
if(chdir(args[1]) != 0){
// if changing directory throws an error
perror("Error");
}
// otherwise changing directory was successful
}
}
void executeTaskFg(char* programName, char** args, int numargs){
pid_t pid = fork();
if(pid == 0){
signal(SIGINT, INTHandler); // fg task must be able to be affected by Ctrl+C
signal(SIGTSTP, TSTPHandler); // fg task must be able to be affected by Ctrl+Z
if(setpgid(getpid(),getpid()) == -1){
// error setting pgid to its own pid
perror("setpgid");
}
if (debug) printf("Forked child and running fg process: %s pid: %d, pgid: %d\n", programName, getpid(), getpgid(getpid()));
// child
if(execv(programName, args)){
if(execvp(programName, args)){
// if there's a return value at all
printf("%s: Program not found.\n", programName);
exit(0);
}
}
} else{
// parent, pid = process id of child
int child_status;
if (debug) printf("shell waiting for foreground task to complete\n");
fg_pid = pid;
struct Job* newForeroundJob = malloc(sizeof(struct Job)); // will need to free later in SIGCHLD handler or smth
newForeroundJob -> jobId = latestJobId;
newForeroundJob -> processId = pid;
newForeroundJob -> foreground = 1;
newForeroundJob -> status = "Running";
newForeroundJob -> numargs = numargs;
if (debug) printf("Duplicating the following arguments: ");
char** argsCopy = malloc((numargs + 2) * sizeof(char*));
for (int i = 0; i < numargs + 1; i++){
if (debug) printf("%s ", args[i]);
argsCopy[i] = strdup(args[i]);
}
argsCopy[numargs + 1] = NULL;
newForeroundJob -> args = argsCopy; // will need to free the array and each string later
jobList[nextJobIndex] = newForeroundJob;
if (debug) printf("\nCreating a new job and insert into jobList at index %d\n", nextJobIndex);
nextJobIndex += 1;
latestJobId += 1;
int waitpid_status = waitpid(pid, &child_status, WUNTRACED);
if (debug) printf("waitpid of foreground process called\n");
if (WIFEXITED(child_status)){
if (debug) printf("Foreground child finished and reaped\n");
cleanJobList(waitpid_status);
fg_pid = 0;
} else if (WIFSIGNALED(child_status)){
if (debug) printf("Foreground child was terminated and reaped\n");
cleanJobList(waitpid_status);
fg_pid = 0;
} else if (WIFSTOPPED(child_status)){
if (debug) printf("Foreground child was stopped\n");
jobStatusStopped(waitpid_status);
fg_pid = 0;
}
}
}
void bringToForeground(char** args, int numargs){
int restartPid;
if(args[1][0] == '%'){
// by jobid
int length = 0;
while(args[1][length+1] != '\0'){
length += 1;
}
char jobidstr[length + 1];
strncpy(jobidstr, args[1] + 1, length);
jobidstr[length] = '\0';
int jobid = atoi(jobidstr);
if (debug) printf("Bring to foreground by jobid via jobid %d\n", jobid);
for(int i = 0; i < nextJobIndex; i++){
if(jobList[i] -> jobId == jobid){
if (debug) printf("process id: %d\n", jobList[i] -> processId);
restartPid = jobList[i] -> processId;
break;
}
}
} else{
restartPid = atoi(args[1]);
if (debug) printf("Bring to foreground by processid via processid %d\n", restartPid);
// by pid
}
fg_pid = restartPid;
if (kill(restartPid, SIGCONT) == -1){
perror("SIGCONT signal error");
} else{
resumeJobStatus(restartPid, 1);
fg_pid = restartPid;
if (debug) printf("Setting process id: %d to process group id: %d\n", restartPid, getpid());
if (debug) printf("Process with PID: %d now running in PG with PGID: %d\n", restartPid, getpgid(getpid()));
int child_status;
int waitpid_status = waitpid(restartPid, &child_status, WUNTRACED);
if (debug) printf("waitpid of foreground process called\n");
if (WIFEXITED(child_status)){
if (debug) printf("Foreground child finished and reaped\n");
cleanJobList(waitpid_status);
fg_pid = 0;
} else if (WIFSIGNALED(child_status)){
if (debug) printf("Foreground child was terminated and reaped\n");
cleanJobList(waitpid_status);
fg_pid = 0;
} else if (WIFSTOPPED(child_status)){
if (debug) printf("Foreground child was stopped\n");
jobStatusStopped(waitpid_status);
fg_pid = 0;
}
}
}
void executeTaskBg(char* programName, char** args, int numargs){
if (debug) printf("called execute task in background with program: %s\n", programName);
pid_t pid = fork();
if(pid == 0){
// child
if(setpgid(getpid(),getpid()) == -1){
// error setting pgid to its own pid
perror("setpgid");
}
signal(SIGINT, INTHandler); // should not respond to any SIGINT
signal(SIGTSTP, TSTPHandler); // should not respond to any SIGTSTP
// signal(SIGCHLD, nonResponseCHLDHandler); // should not respond to any SIGCHLD
if (debug) printf("Forked child and running bg process: %s, pid: %d, pgid: %d\n", programName, getpid(), getpgid(getpid()));
if(execv(programName, args)){
if(execvp(programName, args)){
perror("execv");
exit(EXIT_FAILURE);
}
}
} else{
struct Job* newBackgroundJob = malloc(sizeof(struct Job)); // will need to free later in SIGCHLD handler or smth
newBackgroundJob -> jobId = latestJobId;
newBackgroundJob -> processId = pid;
newBackgroundJob -> foreground = 0;
newBackgroundJob -> status = "Running";
// newBackgroundJob -> args = args;
newBackgroundJob -> numargs = numargs;
if (debug) printf("Duplicating the following arguments: ");
char** argsCopy = malloc((numargs + 2) * sizeof(char*));
for (int i = 0; i < numargs + 1; i++){
if (debug) printf("%s ", args[i]);
argsCopy[i] = strdup(args[i]);
}
argsCopy[numargs + 1] = NULL;
newBackgroundJob -> args = argsCopy; // will need to free the array and each string later
jobList[nextJobIndex] = newBackgroundJob;
if (debug) printf("\nCreating a new job and insert into jobList at index %d\n", nextJobIndex);
nextJobIndex += 1;
latestJobId += 1;
}
}
void bringToBackground(char** args, int numargs){
int restartPid;
if(args[1][0] == '%'){
// by jobid
int length = 0;
while(args[1][length+1] != '\0'){
length += 1;
}
char jobidstr[length + 1];
strncpy(jobidstr, args[1] + 1, length);
jobidstr[length] = '\0';
int jobid = atoi(jobidstr);
if (debug) printf("Bring to background by jobid via jobid %d\n", jobid);
for(int i = 0; i < nextJobIndex; i++){
if(jobList[i] -> jobId == jobid){
if (debug) printf("process id: %d\n", jobList[i] -> processId);
restartPid = jobList[i] -> processId;
break;
}
}
} else{
restartPid = atoi(args[1]);
if (debug) printf("Bring to background by processid via processid %d\n", restartPid);
// by pid
}
if (kill(restartPid, SIGCONT) == -1){
perror("SIGCONT signal error");
} else{
resumeJobStatus(restartPid, 0);
if (debug) printf("After resuming, restartPid is %d\n", restartPid);
if (debug) printf("Child process now has pid: %d, pgid: %d\n", restartPid, getpgid(restartPid));
if (debug) printf("Resumed in background\n");
}
}
void killJob(char** args, int numargs){
int killPid;
if (args[1][0] == '%'){
int length = 0;
while(args[1][length+1] != '\0'){
length += 1;
}
char jobidstr[length + 1];
strncpy(jobidstr, args[1] + 1, length);
jobidstr[length] = '\0';
int jobid = atoi(jobidstr);
if (debug) printf("Kill job via jobid %d\n", jobid);
for(int i = 0; i < nextJobIndex; i++){
if(jobList[i] -> jobId == jobid){
if (debug) printf("process id: %d\n", jobList[i] -> processId);
killPid = jobList[i] -> processId;
break;
}
}
} else{
killPid = atoi(args[1]);
printf("Killing job with processid: %s\n", args[1]);
}
kill(killPid, SIGKILL);
}
void printJob(struct Job* jobPtr){
struct Job job = *jobPtr;
printf("[%d] ", job.jobId);
printf("(%d) ", job.processId);
printf("%s ", job.status);
printf("%s", job.args[0]);
for(int i = 1; i < job.numargs + 1; i++){
printf(" %s", job.args[i]);
}
printf("\n");
}
void executeCommand(char* command, char** args, int numargs){
if (debug){
printf("numargs: %d\nargs: ", numargs);
for(int i = 1; i < numargs + 1; i++){
printf("%s ", args[i]);
}
printf("\n");
}
if(strcmp(command, "pwd") == 0){
pwd();
} else if(strcmp(command, "cd") == 0){
cd(args, numargs);
} else if(strcmp(command, "fg") == 0){
// change job that is in stopped or background/running to fg/running
// job is identified via %+job_id or pid
if(numargs == 0){
printf("Error: no jobid or processid provided");
} else{
bringToForeground(args, numargs);
}
} else if(strcmp(command, "bg") == 0){
// change job that is in stopped to background/running
// same identifiers as fg
if(numargs == 0){
printf("Error: no jobid or processid provided");
} else{
bringToBackground(args, numargs);
}
} else if(strcmp(command, "kill") == 0){
// kill job and reap | use kill() syscall with -9 signal to kill process
// same identifiers
if(numargs == 0){
printf("Error: no jobid or processid provided");
} else {
killJob(args, numargs);
}
} else if(strcmp(command, "jobs") == 0){
// print all jobs from jobList
int jobIndex = 0;
while(jobList[jobIndex] != NULL){
printJob(jobList[jobIndex]);
jobIndex += 1;
}
} else if(strcmp(command, "println") == 0){
printf("-------------------------------------------------\n");
} else{
if (debug) printf("Execute task");
// start executing a program in fore/background
if(numargs > 0 && strcmp(args[numargs], "&") == 0){
// check if last arg is an "&"
if (debug) printf(" in background\n");
executeTaskBg(command, args, numargs);
} else{
if (debug) printf(" in foreground\n");
executeTaskFg(command, args, numargs);
}
}
}
void commitGenocideOnChildren(){
for(int i = 0; i < nextJobIndex; i++){
pid_t toKill = jobList[i] -> processId;
kill(toKill, SIGKILL);
if (debug) printf("Killed %d\n", toKill);
}
}
int main(){
if (debug) printf("shell started in gpid: %d\n", getpgid(getpid()));
signal(SIGINT, shellINTHandler);
signal(SIGTSTP, shellTSTPHandler);
signal(SIGCHLD, SIGCHLDHandler);
char* command = "";
int stdin_fileno_original = dup(STDIN_FILENO); // dupe stdin_fileno
int stdout_fileno_original = dup(STDOUT_FILENO); // dupe stdout_fileno
while(1){
char inputBuffer[bufferSize];
promptInput(inputBuffer);
// empty line, do nothing
if(strcmp(inputBuffer, "\n") == 0) continue;
command = strtok(inputBuffer, whitespace);
if(strcmp(command, quit) == 0){
commitGenocideOnChildren();
break;
} else{
// defines an array of 80 charpointers [command, arg1, arg2, ...]
char* args[80] = {NULL};
// store command into first element
args[0] = command;
// read first argument if exists
char* arg = strtok(NULL, whitespace); // arg is a pointer to the first cell in a char array
int argpos = 1;
while(arg != NULL){
args[argpos] = strdup(arg); // pointer to duplicated argument since arg will be overwritten by next strtok call
argpos += 1;
// continue reading arguments if exists
arg = strtok(NULL, whitespace);
}
int numargs = argpos - 1;
if(numargs > 0){
if(strcmp(args[1], ">") == 0){
// direct output
writeToFile = 1;
outputFile = args[2];
} else if(strcmp(args[1], ">>") == 0){
appendToFile = 1;
outputFile = args[2];
} else if(strcmp(args[1],"<") == 0){
readFromFile = 1;
inputFile = args[2];
if(numargs > 2 && strcmp(args[3], ">") == 0){
writeToFile = 1;
outputFile = args[4];
} else if(numargs > 2 && strcmp(args[3], ">>") == 0){
appendToFile = 1;
outputFile = args[4];
}
}
}
int skipExecution = 0;
int ifd;
int ofd;
if (readFromFile || writeToFile || appendToFile){
mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
if(readFromFile){
if (debug) printf("Read from file %s\n",inputFile);
if ((ifd = open(inputFile, O_RDONLY, mode)) < 0){
skipExecution = 1;
perror("open");
if (debug) printf("input file open error\n");
} else{
dup2(ifd, STDIN_FILENO);
}
}
if(writeToFile){
if (debug) printf("Write to file %s\n", outputFile);
if ((ofd = open(outputFile, O_CREAT|O_WRONLY|O_TRUNC, mode)) < 0){
skipExecution = 1;
perror("open");
if (debug) printf("output file open error\n");
} else{
dup2(ofd, STDOUT_FILENO);
}
} else if(appendToFile){
if (debug) printf("Append to file %s\n", outputFile);
if ((ofd = open(outputFile, O_CREAT|O_APPEND|O_WRONLY, mode)) < 0){
skipExecution = 1;
perror("open");
if (debug) printf("output append file open error\n");
} else{
dup2(ofd, STDOUT_FILENO);
}
}
for (int i = 1; i < numargs + 1; i++){
args[i] = NULL; // clear args since only args were filenames
}
numargs = 0;
}
// argpos - 1 = actual number of arguments (excludes command) aka numargs in function
// example:
// prompt > count 5 &
// command = "count"
// args = ["count", "5", "&"]
// argpos = 2
if (skipExecution){
printf("Erroneous Input\n");
} else{
executeCommand(command, args, numargs);
}
if (readFromFile){
if (close(ifd) < 0){
perror("close");
}
}
if (writeToFile || appendToFile){
if(close(ofd) < 0){
perror("close");
}
}
dup2(stdin_fileno_original, STDIN_FILENO); // reset input to stdin
dup2(stdout_fileno_original, STDOUT_FILENO); // reset output to stdout
readFromFile = 0;
writeToFile = 0;
appendToFile = 0;
skipExecution = 0;
}
}
printf("%s\n","exit");
return 0;
}