-
Notifications
You must be signed in to change notification settings - Fork 0
/
shell.c
703 lines (631 loc) · 14.5 KB
/
shell.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
/* shell.c - Mini POSIX Shell
*
* Author: Matus Marhefka
* Date: 2015-04-23
*
*
* IMPLEMENTATION:
* Shell creates 3 threads:
* 1) Input handling thread
* 2) Execution handling thread
* 3) Signal handling thread
* Input handling thread is processing the user input which is then
* used in execution handling thread (monitor is used for synchronization)
* which forks the new process and then executes the user input. Input
* and execution threads have all signals blocked so only signal handling
* thread can receive signals delivered to the main shell process.
*
*
* Mini POSIX Shell features:
* -- file redirection using >FILE or <FILE
* -- run process in background by specifying '&' character
* at the end of the command line
*
* Mini POSIX Shell built-in commands:
* -- jobs - prints all background jobs
* -- cd - change working directory
* -- exit - exits the shell
*
*/
#define _POSIX_C_SOURCE 200809L
#ifndef _REENTRANT
# define _REENTRANT
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <pthread.h>
#include "shell.h"
int is_space(char c)
{
if (c == ' ' || c == '\t')
return 1;
return 0;
}
/* Sets exit_flag to the value specified as the argument. */
void set_exit_flag(int flag)
{
pthread_mutex_lock(&mtx_exit);
exit_flag = flag;
pthread_mutex_unlock(&mtx_exit);
}
/* Returns 1 if exec thread should exit, otherwise 0. */
int is_exit_flag(void)
{
int flag;
pthread_mutex_lock(&mtx_exit);
flag = exit_flag;
pthread_mutex_unlock(&mtx_exit);
return flag;
}
/* Input thread monitor: allows to execute content in args. */
void monitor_args_execute(void)
{
pthread_mutex_lock(&mtx);
exec_args = 1;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mtx);
}
/* Input thread monitor: waits on exec thread to finish execution. */
void monitor_args_wait_finished(void)
{
pthread_mutex_lock(&mtx);
while (exec_args != 0)
pthread_cond_wait(&cond, &mtx);
pthread_mutex_unlock(&mtx);
}
/* Exec thread monitor: waits on input thread to fill in the args. */
void monitor_args_wait_executable(void)
{
pthread_mutex_lock(&mtx);
while (exec_args != 1)
pthread_cond_wait(&cond, &mtx);
pthread_mutex_unlock(&mtx);
}
/* Exec thread monitor: allows input thread to work with args. */
void monitor_args_executed(void)
{
pthread_mutex_lock(&mtx);
exec_args = 0;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mtx);
}
/* Processes shell input and fills the global variable args in format suitable
* for execvp() function. Returns:
* 0 - input processed and filled args
* 1 - input processing error
* -1 - memory allocation error
*/
int create_args(char *buf)
{
int i, j;
char *ptr, *bptr;
run_bg = 0;
/* skip beginning whitespaces */
bptr = ptr = buf;
while (is_space(*ptr)) {
ptr++;
bptr = ptr;
}
/* preprocessing: count the number of arguments including
* filename (argsc) */
argsc = 1;
i = 0;
while (*ptr != '\0') {
if (is_space(*ptr)) {
/* argument too long */
if (i >= MAXARG) {
fprintf(stderr, "Argument too long!\n");
return 1;
}
/* IO redirection */
if (*bptr == '>' || *bptr == '<') {
bptr++;
if (is_space(*bptr)) {
fprintf(stderr, "No whitespaces after "
"'%c' operator!\n", *(--bptr));
return 1;
}
} else if (*bptr == '&') {
run_bg = 1;
bptr--;
while (is_space(*bptr))
bptr--;
bptr++;
*bptr = '\0';
argsc--;
} else {
argsc++;
}
i = -1;
bptr = ptr + 1;
/* skip whitespaces */
while (is_space(*ptr)) {
ptr++;
bptr = ptr;
}
ptr--;
}
ptr++;
i++;
}
/* cases where we have only one argument and it is too long */
if (i >= MAXARG) {
fprintf(stderr, "Argument too long!\n");
return 1;
}
/* cases when the last argument has redirection operator */
if ((argsc > 1) && (*bptr == '>' || *bptr == '<'))
argsc--;
/* background job, line ends with '&' so we remove it */
if (argsc > 1 && *bptr == '&') {
run_bg = 1;
bptr--;
while (is_space(*bptr))
bptr--;
bptr++;
*bptr = '\0';
argsc--;
} else { /* removes trailing whitespace */
ptr--;
while (is_space(*ptr))
ptr--;
ptr++;
*ptr = '\0';
}
/* args must be NULL terminated, we must count also NULL member */
argsc++;
/* allocate memory for arguments */
args = calloc(argsc, sizeof(char *));
if (args == NULL) {
fprintf(stderr, "Not enough memory!\n");
return -1;
}
for (i = 0; i < argsc-1; i++) {
args[i] = calloc(MAXARG, sizeof(char));
if (args[i] == NULL) {
fprintf(stderr, "Not enough memory!\n");
return -1;
}
}
/* fills in the args variable with shell input */
bptr = ptr = buf;
while (is_space(*ptr)) {
ptr++;
bptr = ptr;
}
i = j = 0;
while (*ptr != '\0') {
if (is_space(*ptr)) {
if (*bptr == '>') {
bptr++;
memcpy(redir_t, bptr, j);
redir_t[j-1] = '\0';
} else if (*bptr == '<') {
bptr++;
memcpy(redir_f, bptr, j);
redir_f[j-1] = '\0';
} else {
memcpy(args[i], bptr, j);
i++;
}
j = -1;
bptr = ptr + 1;
/* skip whitespaces */
while (is_space(*ptr)) {
ptr++;
bptr = ptr;
}
ptr--;
}
ptr++;
j++;
}
/* last argument or only one argument */
if (*bptr == '>') {
bptr++;
memcpy(redir_t, bptr, j);
redir_t[j-1] = '\0';
args[i] = NULL;
} else if (*bptr == '<') {
bptr++;
memcpy(redir_f, bptr, j);
redir_f[j-1] = '\0';
args[i] = NULL;
} else {
memcpy(args[i], bptr, j);
args[i+1] = NULL;
}
return 0;
}
/* Frees the memory occupied by args. */
void clear_args(void)
{
int i;
for (i = 0; i < argsc; i++)
free(args[i]);
free(args);
redir_t[0] = '\0';
redir_f[0] = '\0';
}
/* Flushes the rest of an input on stdin if maximum length of input
* is reached. */
void flush_input(void)
{
int rv, n;
char c;
while ((n = read(STDIN_FILENO, &c, 1)) != 0) {
if (n < 0) {
if (errno == EINTR)
continue;
else {
perror("read");
fflush(stderr);
/* signal exec thread to exit */
set_exit_flag(1);
monitor_args_execute();
rv = 1;
pthread_exit(&rv);
}
}
if (c == '\n')
break;
}
}
/* Input thread */
void *input_start(void *arg)
{
int rv;
ssize_t n;
char cmd_buf[MAXLEN];
printf("$ ");
fflush(stdout);
/* read from stdin */
while ((n = read(STDIN_FILENO, &cmd_buf, MAXLEN)) != 0) {
if (n < 0) {
perror("read");
fflush(stderr);
/* signal exec thread to exit */
set_exit_flag(1);
monitor_args_execute();
return (void *)1;
}
if (n == MAXLEN) {
if (cmd_buf[n-1] == '\n') {
fprintf(stderr, "Argument too long!\n");
printf("$ ");
} else {
flush_input();
fprintf(stderr, "Argument too long!\n");
printf("$ ");
}
fflush(stdout);
fflush(stderr);
continue;
}
cmd_buf[n-1] = '\0';
/* constructs args variable for execvp */
rv = create_args(cmd_buf);
if (rv == -1) {
fflush(stderr);
break;
}
if (rv == 1) {
fflush(stderr);
printf("$ ");
fflush(stdout);
continue;
}
if (strcmp(args[0], "exit") == 0) {
clear_args();
/* signal exec thread to exit */
set_exit_flag(1);
monitor_args_execute();
return 0;
}
if (strcmp(args[0], "jobs") == 0) {
clear_args();
jobs_print(&jobs);
printf("$ ");
fflush(stdout);
continue;
}
if (strcmp(args[0], "cd") == 0) {
clear_args();
if (change_cwd() == -1) {
fflush(stderr);
/* signal exec thread to exit */
set_exit_flag(1);
monitor_args_execute();
return 0;
}
printf("$ ");
fflush(stdout);
fflush(stderr);
continue;
}
if (strlen(args[0]) == 0) {
clear_args();
printf("\r$ ");
fflush(stdout);
continue;
}
/* signal the exec thread to execute the args content */
monitor_args_execute();
/* wait until execution is finished */
monitor_args_wait_finished();
clear_args();
if (is_exit_flag())
return 0;
printf("$ ");
fflush(stdout);
}
printf("\n");
fflush(stdout);
/* signal exec thread to exit */
set_exit_flag(1);
monitor_args_execute();
return 0;
}
/* If target argument is STDOUT_FILENO (STDIN_FILENO) the stdout (stdin)
* descriptor is redirected into the file specified in global variable
* redir_t (redir_f). On success, the file descriptor of redirection file
* is returned, otherwise -1 indicating error. */
int redir_file(int target)
{
int fd;
if (target == STDOUT_FILENO) {
fd = open(redir_t, O_CREAT|O_TRUNC|O_WRONLY, S_IRUSR|S_IWUSR);
if (fd == -1) {
perror("open");
return -1;
}
/* make stdout go to fd */
if (dup2(fd, STDOUT_FILENO) == -1) {
perror("dup2");
return -1;
}
} else {
fd = open(redir_f, O_CREAT|O_RDONLY, S_IRUSR|S_IWUSR);
if (fd == -1) {
perror("open");
return -1;
}
/* make stdin go to fd */
if (dup2(fd, STDIN_FILENO) == -1) {
perror("dup2");
return -1;
}
}
return fd;
}
/* Executes the file in args[0] and also handles file redirection
* and backgrounding of processes. Returns 0 on success or -1 on error. */
int execute_file(void)
{
pid_t cpid, w;
sigset_t signal_set;
int status, fd;
cpid = fork();
if (cpid == -1) {
perror("fork");
return -1;
}
if (cpid == 0) { /* child */
/* unblock all signals for new process except SIGTSTP */
sigfillset(&signal_set);
sigdelset(&signal_set, SIGTSTP);
if (run_bg)
sigdelset(&signal_set, SIGINT);
pthread_sigmask(SIG_UNBLOCK, &signal_set, NULL);
/* IO redirection */
if (redir_t[0] != '\0') {
fd = redir_file(STDOUT_FILENO);
if (fd == -1)
return -1;
}
if (redir_f[0] != '\0') {
fd = redir_file(STDIN_FILENO);
if (fd == -1)
return -1;
}
if (run_bg) {
/* child make itself the process group leader (of its
* own group - different from shell group) - this
* will lead in SIGTTIN signal when trying to read
* from stdin which causes stopping of child */
if (setpgid(0, 0) == -1) {
perror("setpgid");
exit(1);
}
}
/* PATH variable is searched automatically */
execvp(args[0], args);
if (errno == ENOENT)
fprintf(stderr, "%s: command not found...\n", args[0]);
else
perror("");
exit(1);
} else { /* parent */
/* restore all signals blocking */
sigfillset(&signal_set);
pthread_sigmask(SIG_BLOCK, &signal_set, NULL);
if (run_bg) {
if (jobs_insert(&jobs, args[0], cpid) == -1)
return -1;
printf("[%d] %s\n", cpid, args[0]);
fflush(stdout);
} else {
w = waitpid(cpid, &status, 0);
if (w == -1 && errno != ECHILD) {
perror("waitpid");
return -1;
} else if (w > 0) {
if (WIFSIGNALED(status))
printf("\n");
}
}
}
return 0;
}
/* Exec thread */
void *cmd_exec_start(void *arg)
{
for(;;) {
/* wait until input thread fills in the args */
monitor_args_wait_executable();
if (is_exit_flag())
break;
if (execute_file() == -1) {
set_exit_flag(1);
fflush(stderr);
monitor_args_executed();
return (void *)1;
}
/* allow input thread to work with the args */
monitor_args_executed();
}
return 0;
}
/* Prints the exit status of a background process. */
void print_status(int w, int status)
{
if (WIFEXITED(status)) {
if (WEXITSTATUS(status) != 0)
printf("\n[%d]+ Exit %d\n", w, WEXITSTATUS(status));
else
printf("\n[%d]+ Done\n", w);
} else if (WIFSIGNALED(status)) {
printf("\n[%d]+ Killed\n", w);
} else if (WIFSTOPPED(status)) {
printf("\n[%d]+ Stopped\n", w);
} else {
printf("\n[%d]+ Terminated\n", w);
}
}
/* Signal handling thread */
void *sig_handler(void *arg)
{
sigset_t signal_set;
pid_t w;
int sig, status;
for (;;) {
/* wait for any signal */
sigfillset(&signal_set);
sigwait(&signal_set, &sig);
/* signal caught */
switch (sig) {
case SIGINT: /* ctrl+c */
pthread_mutex_lock(&mtx);
if (exec_args)
printf("\n");
else
printf("\n$ ");
pthread_mutex_unlock(&mtx);
fflush(stdout);
break;
case SIGTSTP: /* ctrl+z */
pthread_mutex_lock(&mtx);
if (exec_args)
printf("\n");
else
printf("\n$ ");
pthread_mutex_unlock(&mtx);
fflush(stdout);
break;
case SIGCHLD: /* child exit */
w = waitpid(-1, &status, WNOHANG);
if (w > 0) {
if (jobs_find_remove(&jobs, w)) {
print_status(w, status);
pthread_mutex_lock(&mtx);
if (!exec_args)
printf("$ ");
pthread_mutex_unlock(&mtx);
fflush(stdout);
}
}
break;
case SIGUSR1:
if (is_exit_flag())
return 0;
break;
default:
break;
}
}
return 0;
}
int main(int argc, char *argv[])
{
int stat, i;
pthread_t threads[3];
pthread_attr_t attr;
sigset_t signal_set;
stat = pthread_attr_init(&attr);
if (stat != 0)
handle_error_en(stat, "pthread_attr_init");
stat = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
if (stat != 0)
handle_error_en(stat, "pthread_attr_setdetachstate");
/* initilize our jobs (struct job_list) variable - a linked
* list for storing backgrounded jobs */
stat = jobs_init(&jobs);
if (stat != 0)
handle_error_en(stat, "jobs_init: pthread_mutex_init");
/* make shell process group leader */
if (setpgid(getpid(), getpid()) == -1) {
perror("setpgid");
exit(1);
}
/* set the terminal prcess group to the shell process group - causes
* that only processes in shell group can read stdin, if process
* of other process group tries to read from stdin, it will be
* sent the SIGTTIN signal which stops that process */
if (tcsetpgrp(STDIN_FILENO, getpgid(0)) == -1) {
perror("tcsetpgrp");
exit(1);
}
/* block all signals */
sigfillset(&signal_set);
stat = pthread_sigmask(SIG_BLOCK, &signal_set, NULL);
if (stat != 0)
handle_error_en(stat, "pthread_sigmask");
/* create the signal handling thread */
stat = pthread_create(&threads[0], &attr, sig_handler, NULL);
if (stat != 0)
handle_error_en(stat, "pthread_create");
/* input thread */
stat = pthread_create(&threads[1], &attr, input_start, NULL);
if (stat != 0)
handle_error_en(stat, "pthread_create");
/* commands executing thread */
stat = pthread_create(&threads[2], &attr, cmd_exec_start, NULL);
if (stat != 0)
handle_error_en(stat, "pthread_create");
/* Free attribute and wait for the input and exec threads */
stat = pthread_attr_destroy(&attr);
if (stat != 0)
handle_error_en(stat, "pthread_attr_destroy");
for (i = 1; i < 3; i++) {
stat = pthread_join(threads[i], NULL);
if (stat != 0)
handle_error_en(stat, "pthread_join");
}
/* finally kill and join our signal handling thread */
stat = pthread_kill(threads[0], SIGUSR1);
if (stat != 0)
handle_error_en(stat, "pthread_kill");
stat = pthread_join(threads[0], NULL);
if (stat != 0)
handle_error_en(stat, "pthread_join");
jobs_free(&jobs);
exit(0);
}