-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
725 lines (640 loc) · 26.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
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
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
//main.c
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <errno.h>
#include <ctype.h>
#include <signal.h>
#include <unistd.h>
#include <ucontext.h>
#include <sys/time.h>
#include <sys/resource.h>
//defines
#define TRUE 1
#define FALSE 0
#define cmd_size 100
/*Global Variables*/
/*flags*/
static int verbose_flag;
static int wait_flag;
static int profile_flag;
/*temporary storage*/
static int pid_child[1000];
static int fd[1000];
static int fd_pipe[1000];
static int pipe_fd[2];
static int close_fd;
static int catch_sig;
static int ignore_sig;
static int default_sig;
static int cmd_fd[3];
char *cmd[cmd_size];
static int return_val;
const mode_t mode = 0644;
struct wait_info{
pid_t wait_pid;
char *wait_cmd[cmd_size];
};
struct wait_info wait_infos[100];
/*getrusage*/
struct rusage usage;
double prev_usertime;
double prev_systemtime;
/*Helper Functions*/
int isAnOption(char *cstring){
if((cstring[0] != '-') || (cstring[1] != '-'))
return FALSE;
else
return TRUE;
}
void flushCmd(){
for(int i = 0; i < cmd_size; i++)
cmd[i] = NULL;
}
int isNumber(char *string){
int i;
for(i = 0; string != NULL && *(string + i) != '\0'; i++) {
if (!isdigit(*(string+i)))
return FALSE;
return TRUE;
}
}
void print_usage(int who)
{
if (getrusage(who, &usage) < 0) {
fprintf(stderr, "error: cannot get usage information\n");
exit(errno);
}
else {
double usertime = ((double)usage.ru_utime.tv_sec + (double)usage.ru_utime.tv_usec/1000000) - prev_usertime;
double systemtime = ((double)usage.ru_stime.tv_sec + (double)usage.ru_stime.tv_usec/1000000) - prev_systemtime;
printf("\tLast Option Usage Statistics:\n");
printf("\t\tUser time:\t\t\t%f seconds\n", usertime);
printf("\t\tSystem time:\t\t\t%f seconds\n", systemtime);
/*
if(profile_flag){
//do a round of getrusage
if(getrusage(RUSAGE_SELF, &usage) == -1){
//then we have an error. Do something about it.
}
else{
//set the values of ru_utime_prev and ru_stime_prev
double usertime = ((double)usage.ru_utime.tv_sec + (double)usage.ru_utime.tv_usec/1000000) - prev_usertime;
double systemtime = ((double)usage.ru_stime.tv_sec + (double)usage.ru_stime.tv_usec/1000000) - prev_systemtime;
printf("Command User Run Time: %f s \nCommand System Run Time: %f s \n", usertime, systemtime);
}
}
*/
printf("\tTotal Usage Statistics:\n");
printf("\t\tUser time:\t\t\t%lld seconds\n", (long long) usage.ru_utime.tv_sec);
printf("\t\t\t\t\t\t%lld microseconds\n", (long long) usage.ru_utime.tv_usec);
printf("\t\tSystem time:\t\t\t%lld seconds\n", (long long) usage.ru_stime.tv_sec);
printf("\t\t\t\t\t\t%lld microseconds\n", (long long) usage.ru_stime.tv_usec);
printf("\t\tMax resident set size:\t\t%ld\n", usage.ru_maxrss);
printf("\t\tSoft page faults:\t\t%ld\n", usage.ru_minflt);
printf("\t\tHard page faults:\t\t%ld\n", usage.ru_majflt);
printf("\t\tVoluntary context switches:\t%ld\n", usage.ru_nvcsw);
printf("\t\tInvoluntary context switches:\t%ld\n", usage.ru_nivcsw);
printf("\n");
}
}
// Signal Handlers
void catch_handler(int sig){
fprintf(stderr, "error: signal %d caught\n", sig);
exit(sig);
}
void ignore_handler(int sig, siginfo_t *si, void *arg){
signal(sig, SIG_IGN);
ucontext_t *context = (ucontext_t*)arg;
context->uc_mcontext.gregs[REG_RIP]++;
}
void pause_handler(int sig, siginfo_t *si, void *arg){
fprintf(stderr, "error: fail to pause\n");
exit(EXIT_FAILURE);
}
//main function
int main(int argc, char **argv){
static struct option long_opts[] =
{
/*file flags options*/
{"append", no_argument, 0, 'a'},
{"cloexec", no_argument, 0, 'l'},
{"creat", no_argument, 0, 'C'},
{"directory", no_argument, 0, 'd'},
{"dsync", no_argument, 0, 'D'},
{"excl", no_argument, 0, 'e'},
{"nofollow", no_argument, 0, 'n'},
{"nonblock", no_argument, 0, 'N'},
{"rsync", no_argument, 0, 's'},
{"sync", no_argument, 0, 'S'},
{"trunc", no_argument, 0, 't'},
/*file opening options*/
{"rdonly", required_argument, 0, 'r'},
{"wronly", required_argument, 0, 'w'},
{"rdwr", required_argument, 0, 'R'},
{"pipe", no_argument, 0, 'p'},
/*subcommand options*/
{"command", required_argument, 0, 'c'},
{"wait", no_argument, 0, 'W'},
/*miscellaneous options*/
{"close", required_argument, 0, 'L'},
{"verbose", no_argument, &verbose_flag, 1},
{"profile", no_argument, &profile_flag, 1},
{"abort", no_argument, 0, 'A'},
{"catch", required_argument, 0, 'T'},
{"ignore", required_argument, 0, 'i'},
{"default", required_argument, 0, 'f'},
{"pause", no_argument, 0, 'P'},
{0, 0, 0, 0}
};
int long_opts_ind;
int curr_opt;
int curr_optind;
int next_optind;
int oflags = 0;
char *option;
int pid_child_ind = 0;
int fd_ind = 0;
int cmd_ind = 0;
int wait_info_ind = 0;
verbose_flag = 0;
wait_flag = 0;
profile_flag = 0;
ignore_sig = 0;
default_sig = 0;
return_val = 0;
pid_t pid;
pid_t c_pid;
//signal handling
struct sigaction sa;
curr_optind = optind;
curr_opt = getopt_long(argc, argv, "", long_opts, &long_opts_ind);
next_optind = optind;
if(curr_opt == -1){
fprintf(stderr, "error: no options found\n");
}
else {
if(!isAnOption(argv[curr_optind])){
fprintf(stderr, "error: argument found before options and was ignored\n");
}
do {
//debug message
//printf("curr_optind = %d | curr_opt = %c | next_optind = %d\n", curr_optind, curr_opt, next_optind);
//printf("argv[%d] = %s\n", next_optind, argv[next_optind]);
//printf("optopt = %d\n", optopt);
if(profile_flag){
if(getrusage(RUSAGE_SELF, &usage) == -1){
fprintf(stderr, "error: cannot get the usage informations\n");
}
else{
//set the values of ru_utime_prev and ru_stime_prev
prev_usertime = (double)usage.ru_utime.tv_sec + (double)usage.ru_utime.tv_usec/1000000;
prev_systemtime = (double)usage.ru_stime.tv_sec + (double)usage.ru_stime.tv_usec/1000000;
}
}
switch(curr_opt){
// append
case 'a':{
if((next_optind != argc) && !isAnOption(argv[next_optind])){
fprintf(stderr, "error: append can not accept any arguments, all arguments to append were ignored\n");
}
oflags |= O_APPEND;
if(verbose_flag){
printf("--append\n");
}
break;
}
// cloexec
case 'l':{
if((next_optind != argc) && !isAnOption(argv[next_optind])){
fprintf(stderr, "error: cloexec can not accept any arguments, all arguments to cloexec were ignored\n");
}
oflags |= O_CLOEXEC;
if(verbose_flag){
printf("--cloexec\n");
}
break;
}
// creat
case 'C':{
if((next_optind != argc) && !isAnOption(argv[next_optind])){
fprintf(stderr, "error: creat can not accept any arguments, all arguments to creat were ignored\n");
exit(EXIT_FAILURE);
}
oflags |= O_CREAT;
if(verbose_flag){
printf("--creat\n");
}
break;
}
// directory
case 'd':{
if((next_optind != argc) && !isAnOption(argv[next_optind])){
fprintf(stderr, "error: directory can not accept any arguments, all arguments to directory were ignored\n");
}
oflags |= O_DIRECTORY;
if(verbose_flag){
printf("--directory\n");
}
break;
}
// dsync
case 'D':{
if((next_optind != argc) && !isAnOption(argv[next_optind])){
fprintf(stderr, "error: dsync can not accept any arguments, all arguments to dsync were ignored\n");
}
oflags |= O_DSYNC;
if(verbose_flag){
printf("--dsync\n");
}
break;
}
// excl
case 'e':{
if((next_optind != argc) && !isAnOption(argv[next_optind])){
fprintf(stderr, "error: excl can not accept any arguments, all arguments to excl were ignored\n");
}
oflags |= O_EXCL;
if(verbose_flag){
printf("--excl\n");
}
break;
}
// nofollow
case 'n':{
if((next_optind != argc) && !isAnOption(argv[next_optind])){
fprintf(stderr, "error: nofollow can not accept any arguments, all arguments to nofollow were ignored\n");
}
oflags |= O_NOFOLLOW;
if(verbose_flag){
printf("--nofllow\n");
}
break;
}
// nonblock
case 'N':{
if((next_optind != argc) && !isAnOption(argv[next_optind])){
fprintf(stderr, "error: nonblock can not accept any arguments, all arguments to nonblock were ignored\n");
}
oflags |= O_NONBLOCK;
if(verbose_flag){
printf("--nonblock\n");
}
break;
}
// rsync
case 's':{
if((next_optind != argc) && !isAnOption(argv[next_optind])){
fprintf(stderr, "error: rsync can not accept any arguments, all arguments to rsync were ignored\n");
}
oflags |= O_RSYNC;
if(verbose_flag){
printf("--rsync\n");
}
break;
}
// sync
case 'S':{
if((next_optind != argc) && !isAnOption(argv[next_optind])){
fprintf(stderr, "error: sync can not accept any arguments, all arguments to sync were ignored\n");
}
oflags |= O_SYNC;
if(verbose_flag){
printf("--sync\n");
}
break;
}
// trunc
case 't':{
if((next_optind != argc) && !isAnOption(argv[next_optind])){
fprintf(stderr, "error: trunc can not accept any arguments, all arguments to trunc were ignored\n");
}
oflags |= O_TRUNC;
if(verbose_flag){
printf("--trunc\n");
}
break;
}
// rdonly, rdwr, wronly
case 'r':
case 'R':
case 'w':
if (curr_opt == 'r'){
oflags |= O_RDONLY;
option = "rdonly";
}
else if (curr_opt == 'R'){
oflags |= O_RDWR;
option = "rdwr";
}
else if (curr_opt == 'w'){
oflags |= O_WRONLY;
option = "wronly";
}
if((next_optind != argc) && !isAnOption(argv[next_optind])){
fprintf(stderr, "error: %s can only accept one argument, further arguments to %s were ignored\n", option, option);
}
if(verbose_flag){
printf("--%s %s\n", option, optarg);
}
int temp_fd = open(optarg, oflags, mode);
if (errno == EEXIST){
fprintf(stderr, "error: file \"%s\" already exists\n", optarg);
exit(errno);
}
if (errno == ENOTDIR){
fprintf(stderr, "error: file \"%s\" is a non-direcotry file\n", optarg);
exit(errno);
}
if (temp_fd == -1){
fprintf(stderr, "error: could not open file \"%s\"\n", optarg);
exit(errno);
}
fd[fd_ind++] = temp_fd;
fd_pipe[fd_ind] = 0;
/*clean oflags*/
oflags = 0;
if (profile_flag){
print_usage(RUSAGE_SELF);
}
break;
// pipe
case 'p':{
if((next_optind != argc) && !isAnOption(argv[next_optind])){
fprintf(stderr, "error: pipe can not accept any arguments, all arguments to pipe were ignored\n");
}
if(verbose_flag){
printf("--pipe\n");
}
if(pipe(pipe_fd) == -1){
fprintf(stderr, "error: failure to create a pipe\n");
}
/*identify pipe file descriptors to the pipe array*/
fd[fd_ind++] = pipe_fd[0];
fd_pipe[fd_ind] = 1;
fd[fd_ind++] = pipe_fd[1];
fd_pipe[fd_ind] = 1;
if (profile_flag){
print_usage(RUSAGE_SELF);
}
break;
}
//command
case 'c':{
while((optind != argc) && !isAnOption(argv[optind])){
optind++;
}
if(optind < (curr_optind + 5))
fprintf(stderr, "error: missing arguments to --command, your command will not be run\n");
else{
cmd_fd[0] = atoi(optarg);
cmd_fd[1] = atoi(argv[next_optind++]);
cmd_fd[2] = atoi(argv[next_optind++]);
cmd_ind = 0;
//for(int i = 0; i < 3; i++)
// printf("cmd_fd[%d] = %d\n", i, cmd_fd[i]);
flushCmd();
while((next_optind != argc) && !isAnOption(argv[next_optind])){
char *char_ptr = malloc(sizeof(argv[next_optind]));
char *wait_cmd_ptr = malloc(sizeof(argv[next_optind]));
//printf("%d ", (int)sizeof(argv[next_optind]));
cmd[cmd_ind] = char_ptr;
wait_infos[wait_info_ind].wait_cmd[cmd_ind] = wait_cmd_ptr;
strcpy(cmd[cmd_ind], argv[next_optind]);
strcpy(wait_infos[wait_info_ind].wait_cmd[cmd_ind], argv[next_optind]);
cmd_ind++;
next_optind++;
}
if(verbose_flag){
printf("--command");
for(int i = 0; i < 3; i++){
printf(" %d", cmd_fd[i]);
}
for(int i = 0; i < cmd_size; i++){
if(cmd[i] != NULL){
printf(" %s", cmd[i]);
}
}
printf("\n");
}
c_pid = fork();
if(c_pid == 0){
/*close unused side of the pipe*/
if (fd_pipe[cmd_fd[0]])
close(fd[cmd_fd[0]+1]);
if (fd_pipe[cmd_fd[1]])
close(fd[cmd_fd[1]-1]);
if (fd_pipe[cmd_fd[2]])
close(fd[cmd_fd[2]-1]);
/*direct file descriptors*/
dup2(fd[cmd_fd[0]], STDIN_FILENO);
dup2(fd[cmd_fd[1]], STDOUT_FILENO);
dup2(fd[cmd_fd[2]], STDERR_FILENO);
/*execute command*/
if(execvp(cmd[0], cmd) == -1){
fprintf(stderr, "error: command failed\n");
}
}
else if(c_pid > 0){
pid_child[pid_child_ind] = c_pid;
pid_child_ind++;
wait_infos[wait_info_ind].wait_pid = c_pid;
/*close unused side of the pipe*/
if (fd_pipe[cmd_fd[0]]){
close(fd[cmd_fd[0]]);
fd[cmd_fd[0]] = -1;
}
if (fd_pipe[cmd_fd[1]]){
close(fd[cmd_fd[1]]);
fd[cmd_fd[1]] = -1;
}
if (fd_pipe[cmd_fd[2]]){
close(fd[cmd_fd[2]]);
fd[cmd_fd[2]] = -1;
}
}
else { //couldn't create child process
fprintf(stderr, "error: could not create child process\n");
}
for(int i = 0; i < cmd_size; i++)
free(cmd[i]);
}
wait_info_ind++;
if (profile_flag){
print_usage(RUSAGE_SELF);
}
break;
}
//wait
case 'W':{
if (verbose_flag)
printf("--wait\n");
int status;
int exit_status;
for (int i = 0; i < wait_info_ind; i++) {
exit_status = waitpid(-1, &status, 0);
printf("%d ", status);
int j;
for (j = 0; j < wait_info_ind; j++)
if (exit_status == wait_infos[j].wait_pid)
break;
int k = 0;
for (; wait_infos[j].wait_cmd[k]!= NULL; k++)
printf("%s ", wait_infos[j].wait_cmd[k]);
printf("\n");
}
if (profile_flag) {
printf("Parent Usage:\n");
print_usage(RUSAGE_SELF);
printf("Children Usage:\n");
print_usage(RUSAGE_CHILDREN);
}
break;
}
// close
case 'L':{
if (verbose_flag)
printf("--close %s\n", optarg);
if (!isNumber(optarg)){
fprintf(stderr, "error: close requires an integer argument\n");
exit(EXIT_FAILURE);
}
close_fd = atoi(optarg);
if (close_fd > fd_ind){
fprintf(stderr, "error: the entered file descriptor number is invalid\n");
exit(EXIT_FAILURE);
}
close(fd[close_fd]);
fd[close_fd] = -1;
if (profile_flag){
print_usage(RUSAGE_SELF);
}
break;
}
// abort
case 'A':{
if (verbose_flag)
printf("--abort\n");
raise(SIGSEGV);
break;
}
// catch
case 'T':{
if (verbose_flag) {
printf("--catch %s\n", optarg);
}
catch_sig = atoi(optarg);
if (!isNumber(optarg) || catch_sig < 0){
fprintf(stderr, "error: catch requires a valid integer argument\n");
continue;
}
sa.sa_handler = catch_handler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_SIGINFO;
if (sigaction(catch_sig, &sa, NULL) < 0){
/*handle error*/
fprintf(stderr, "error: fail to handle signal catch %d.\n",catch_sig);
exit(EXIT_FAILURE);
}
if (profile_flag){
print_usage(RUSAGE_SELF);
}
break;
}
// ignore
case 'i':{
ignore_sig = atoi(optarg);
if (verbose_flag) {
printf("--ignore %s\n", optarg);
}
if (!isNumber(optarg) || ignore_sig < 0){
fprintf(stderr, "error: ignore requires a valid integer argument\n");
continue;
}
sa.sa_sigaction = &ignore_handler;
sa.sa_flags = SA_SIGINFO;
if (sigaction(ignore_sig, &sa, NULL) < 0){
/*handle error*/
fprintf(stderr, "error: fail to ignore signal %d.\n",ignore_sig);
exit(EXIT_FAILURE);
}
if (profile_flag){
print_usage(RUSAGE_SELF);
}
break;
}
// default
case 'f':{
default_sig = atoi(optarg);
if (verbose_flag) {
printf("--default %s\n", optarg);
}
if (!isNumber(optarg) || default_sig < 0){
fprintf(stderr, "error: default requires a valid integer argument\n");
continue;
}
if (signal(default_sig, SIG_DFL) < 0){
/* Handle error */
fprintf(stderr, "error: fail to handle signal %d with default\n", default_sig);
exit(EXIT_FAILURE);
break;
}
if (profile_flag){
print_usage(RUSAGE_SELF);
}
break;
}
// pause
case 'P':{
if (verbose_flag) {
printf("--pause %s\n", optarg);
}
pause();
if (profile_flag){
print_usage(RUSAGE_SELF);
}
break;
}
case '?':{
fprintf(stderr, "error: option not recognized or no option argument found\n");
break;
}
}
//move to next option
while((optind != argc) && !isAnOption(argv[optind]))
optind++;
curr_optind = optind;
curr_opt = getopt_long(argc, argv, "", long_opts, &long_opts_ind);
next_optind = optind;
} while(curr_opt != -1);
}
/*close all file descriptors that were opened*/
for(int i = 0; i < fd_ind; i++){
if (fd[i] != -1){
if(close(fd[i]) != 0){
fprintf(stderr, "error: could not close file at logical index %d\n", i);
exit(EXIT_FAILURE);
}
}
}
int status;
for (int i = 0; i < pid_child_ind; i++){
waitpid(pid_child[i], &status, 0);
int exit_status = WEXITSTATUS(status);
/*calculate the largest exit status*/
if(exit_status > return_val)
return_val = exit_status;
}
/*free the memory allocated for --wait commnands and arguments*/
for (int i = 0; i < wait_info_ind; i++){
for (int j = 0; j < cmd_size; j++)
free(wait_infos[i].wait_cmd[j]);
}
return return_val;
}