-
Notifications
You must be signed in to change notification settings - Fork 46
/
wsh.c
456 lines (384 loc) · 10.4 KB
/
wsh.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
/**
*
* Simple shell for WINIX.
*
* @author Bruce Tan
* @email [email protected]
*
* @author Paul Monigatti
* @email [email protected]
*
* @create date 2016-0919
*
*/
#include "wsh.h"
int exec_cmd(char *line);
static char history_file[] = ".history";
static char PREFIX[] = "WINIX> ";
static pid_t pgid;
static pid_t last_pgid;
static pid_t last_stopped_pgid;
struct termios termios;
bool termios_inited = false;
// Prototypes
CMD_PROTOTYPE(slab);
CMD_PROTOTYPE(cmd_kill);
CMD_PROTOTYPE(print_pid);
CMD_PROTOTYPE(mem_info);
CMD_PROTOTYPE(do_trace_syscall);
CMD_PROTOTYPE(do_untrace_syscall);
CMD_PROTOTYPE(help);
CMD_PROTOTYPE(cmd_exit);
CMD_PROTOTYPE(printenv);
CMD_PROTOTYPE(do_cd);
CMD_PROTOTYPE(do_cls);
CMD_PROTOTYPE(do_fg);
CMD_PROTOTYPE(do_stest);
// Command handling
struct cmd_internal builtin_commands[] = {
{ printenv, "env" },
{ do_trace_syscall, "trace"},
{ do_untrace_syscall, "untrace"},
{ slab, "slab"},
{ cmd_kill, "kill"},
{ print_pid, "pid"},
{ mem_info, "free"},
{ cmd_exit, "exit"},
{ help, "help"},
{ help, "?"},
{ do_cd, "cd"},
{ do_cls, "clear"},
{ do_fg, "fg"},
{ do_stest, "stest"},
{ 0, NULL}
};
void init_pgid(){
pgid = getpgid(0);
}
void init_shell(){
int ret = setpgid(0, 0);
init_pgid();
ret = tcsetpgrp(STDIN_FILENO, pgid);
assert(ret == 0);
tcgetattr(STDIN_FILENO, &termios);
termios_inited = true;
}
void init_shell_sysenv(){
#ifdef __wramp__
signal(SIGINT, SIG_IGN);
signal(SIGTSTP, SIG_IGN);
#endif
}
void restore_shell_sysenv(){
#ifdef __wramp__
signal(SIGINT, SIG_DFL);
signal(SIGTSTP, SIG_DFL);
#endif
}
int append_line_to_history_file (char *filename, char *line){
int ret;
int fd = open(filename, O_WRONLY | O_CREAT | O_APPEND, 0666);
if (fd < 0)
return -errno;
ret = write(fd, line, strlen(line));
close(fd);
return ret;
}
int main(int argc, char *argv[]) {
init_pgid();
if(argc > 2 && strcmp(argv[1], "-c") == 0){
int i;
char buf[MAX_LINE];
buf[0] = '\0';
for(i = 2; i < argc; i++){
strlcat(buf, argv[i], MAX_LINE);
if (i < argc - 1)
strlcat(buf, " ", MAX_LINE);
}
return exec_cmd(buf);
}
init_shell();
init_shell_sysenv();
while(1) {
int linelen;
char *line = readline(PREFIX);
if (line == NULL)
break;
linelen = strlen(line);
if (linelen == 0 || line[0] == '#' || line[0] == '\n')
continue;
if (*line){
add_history(line);
append_line_to_history_file(history_file, line);
}
if (line[linelen - 1] == '\n')
line[linelen - 1] = '\0';
exec_cmd(line);
free(line);
}
if (termios_inited)
tcsetattr(STDIN_FILENO, TCSANOW, &termios);
return 0;
}
int search_path(char* path, int len, char* name){
int ret;
struct stat statbuf;
strlcpy(path, "/bin/", len);
strlcat(path, name, len);
ret = stat(path, &statbuf);
if(ret)
return ret;
return statbuf.st_mode & S_IFREG ? 0 : -1;
}
#define PIPE_READ (0)
#define PIPE_WRITE (1)
#define BUFFER_LEN (30)
pid_t run_cmd(struct cmdLine *cmd, int i, int *pipe_ptr, int *prev_pipe_ptr){
int sout;
pid_t pid;
int cmd_start = cmd->cmdStart[i];
char buffer[BUFFER_LEN];
if(search_path(buffer, BUFFER_LEN, cmd->argv[cmd_start])){
fprintf(stderr, "Unknown command '%s'\n", cmd->argv[cmd_start]);
return -1;
}
pid = tfork();
if (pid == -1){
perror("fork");
return -1;
}
if(pid == 0){ // child, actual command
// if redirecting input and first command
if (i == 0 && cmd->infile) {
int sin = open(cmd->infile, O_RDONLY);
dup2(sin,STDIN_FILENO);
close(sin);
}
// if redirecting output and last command
if(i == cmd->numCommands - 1 && cmd->outfile){
int mode = O_WRONLY | O_CREAT;
if(cmd->append) //if append
mode |= O_APPEND;
else //else replace the original document
mode |= O_TRUNC;
sout = open(cmd->outfile, mode, 0664);
dup2(sout,STDOUT_FILENO);
close(sout);
}
restore_shell_sysenv();
if (termios_inited)
tcsetattr(STDIN_FILENO, TCSANOW, &termios);
if (last_pgid){
setpgid(0, last_pgid);
}else{
setpgid(0, 0);
last_pgid = getpgid(0);
tcsetpgrp(STDIN_FILENO, last_pgid);
}
if(cmd->numCommands > 1){
if((i+1) < cmd->numCommands ){ // not the last command
dup2(pipe_ptr[PIPE_WRITE], STDOUT_FILENO);
close(pipe_ptr[PIPE_WRITE]);
close(pipe_ptr[PIPE_READ]);
// printf("cmd %d %s pipe %d %d\n", i, buffer, pipe_ptr[PIPE_READ], pipe_ptr[PIPE_WRITE]);
}
// printf("cmd %d %s com %d, res %d\n", i, buffer, cmd->numCommands, (i+1) < cmd->numCommands);
if(i > 0){ // not the first command, read previous pipe
dup2(prev_pipe_ptr[PIPE_READ], STDIN_FILENO);
close(prev_pipe_ptr[PIPE_READ]);
close(prev_pipe_ptr[PIPE_WRITE]);
// printf("cmd %d %s dup read %d\n", i, buffer, prev_pipe_ptr[PIPE_READ]);
}
}
execv(buffer, &cmd->argv[cmd_start]);
perror("execv");
exit(1);
}
return pid;
}
/**
* Handles any unknown command.
**/
int _exec_cmd(struct cmdLine *cmd) {
int status;
int i, ret;
int pipe_fds[10];
int *pipe_ptr, *prev_pipe_ptr = NULL;
pid_t jobid;
if(cmd->argc == 0)
return 1;
last_pgid = 0;
for(i = 0; i < cmd->numCommands; i++){
pipe_ptr = &pipe_fds[(i * 2)];
if (i < cmd->numCommands - 1){ // not the last command, create new pipe
ret = pipe(pipe_ptr);
if(ret){
perror("pipe");
return -1;
}
}
jobid = run_cmd(cmd, i, pipe_ptr, prev_pipe_ptr);
cmd->job_pid[i] = jobid;
if (prev_pipe_ptr){
close(prev_pipe_ptr[PIPE_READ]);
close(prev_pipe_ptr[PIPE_WRITE]);
}
prev_pipe_ptr = pipe_ptr;
}
for(i = 0; i < cmd->numCommands; i++){
if (cmd->job_pid[i] == -1)
continue;
ret = waitpid(cmd->job_pid[i], &status, WUNTRACED);
if(WIFSTOPPED(status)){
// printf("Stopped pg %d\n", last_pgid);
last_stopped_pgid = last_pgid;
}
}
tcsetpgrp(STDIN_FILENO, pgid);
return WEXITSTATUS(status);
}
int exec_cmd(char *line){
struct cmdLine cmd;
char* buffer;
struct cmd_internal *handler = NULL;
(void)parse(line,&cmd);
if(cmd.env && cmd.env_val){ // if a new environment variable is set
buffer = malloc(MAX_LINE);
parse_quotes(cmd.env_val, buffer, MAX_LINE);
if(*buffer){
printf("setenv %s=%s\n", cmd.env, buffer);
setenv(cmd.env, buffer, 1);
}
free(buffer);
return 0;
}
// Decode command
handler = builtin_commands;
while(handler && handler->name && strcmp(cmd.argv[0], handler->name)) {
handler++;
}
// Run it
if(handler->name){
return handler->handle(cmd.argc, cmd.argv);
}
return _exec_cmd(&cmd);
}
int printenv(int argc, char **argv){
const char **v;
const char *p;
v = environ;
while((p = *v++)){
printf("%s\n",p);
}
return 0;
}
int cmd_kill(int argc, char **argv){
pid_t pid;
int signum = SIGTERM;
char *endptr;
int pidposition = 1;
if(argc < 2){
printf("kill <-signal> [pid]\n");
return 1;
}
if(*argv[1] == '-'){
signum = strtol(argv[1] + 1, &endptr, 10);
if(*endptr){
fprintf(stderr, "Invalid number '%s'\n", argv[1] + 1);
return 1;
}
pidposition = 2;
}
pid = strtol(argv[pidposition], &endptr, 10);
if(*endptr){
fprintf(stderr, "Invalid number '%s'\n", argv[2]);
return 1;
}
if(kill(pid,signum))
perror("kill ");
return 0;
}
int do_cd(int argc, char** argv){
int ret;
if(argc < 2)
return 1;
ret = chdir(argv[1]);
if(ret)
perror("");
return ret;
}
int help(int argc, char** argv){
struct cmd_internal* handler;
handler = builtin_commands;
printf("Available Commands\n");
while(handler && handler->name != NULL) {
printf(" * %s\n",handler->name);
handler++;
}
return 0;
}
int do_stest(int argc, char** argv){
static char test_str[] = "ls -lah /bin | grep snake | wc | cat";
// static char test_str[] = "test deadlock";
int i, ret;
for(i = 0; i < 10; i++){
if((ret = exec_cmd(test_str))){
break;
}
}
return 0;
}
// byte stream for \e[0;0H\e[2J
char cls[] = {0x1b, 0x5b, 0x30, 0x3b, 0x30, 0x48, 0x1b, 0x5b, 0x32, 0x4a, 0};
int do_cls(int argc, char** argv){
printf("%s", cls);
return 0;
}
int do_trace_syscall(int argc, char** argv){
printf("Printing syscall tracing in Serial Port 2\n");
return enable_syscall_tracing();
}
int do_untrace_syscall(int argc, char** argv){
printf("Syscall tracking disabled\n");
return disable_syscall_tracing();
}
// Print the system wise memory info
int mem_info(int argc, char** argv){
(void)wramp_syscall(WINFO, WINFO_MEM);
return 0;
}
// current pid
int print_pid(int argc, char **argv){
printf("%d\n",getpid());
return 0;
}
// list all the processes in the system
int slab(int argc, char **argv){
return wramp_syscall(WINFO, WINFO_SLAB);
}
int cmd_exit(int argc, char **argv){
int status = 0;
char *endptr;
if(argc > 1){
status = strtol(argv[1], &endptr, 10);
if(*endptr){
fprintf(stderr, "Invalid number '%s'\n", argv[1]);
return 1;
}
}
printf("Bye!\n");
// printf("Child %d [parent %d] exits\n",getpid(),getppid());
exit(status);
return 1;
}
int do_fg(int argc, char **argv){
int status;
int ret;
tcsetpgrp(STDIN_FILENO, last_stopped_pgid);
ret = kill(-last_stopped_pgid, SIGCONT);
if(ret == 0){
ret = waitpid(-last_stopped_pgid, &status, WUNTRACED);
}
tcsetpgrp(STDIN_FILENO, pgid);
return ret;
}