-
Notifications
You must be signed in to change notification settings - Fork 9
/
unix.c
627 lines (565 loc) · 9.9 KB
/
unix.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
/*
* Unix versions of system-specific functions
* By convention, exported routines herein have names beginning with an
* upper case letter.
*/
#include "rc.h"
#include "fns.h"
#include "io.h"
#include "exec.h"
#include "getflags.h"
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <dirent.h>
#include <assert.h>
#include <limits.h>
char *Rcmain = PREFIX "/lib/rcmain";
char *Fdprefix = "/dev/fd/";
void execfinit(void);
struct builtin Builtin[] = {
"cd", execcd,
"whatis", execwhatis,
"eval", execeval,
"exec", execexec, /* but with popword first */
"exit", execexit,
"shift", execshift,
"wait", execwait,
"umask", execumask,
".", execdot,
"finit", execfinit,
"flag", execflag,
"history", exechistory,
0
};
#define SEP '\1'
char **environp;
struct word*
enval(register char *s)
{
char *t, c;
struct word *v;
for(t = s;*t && *t!=SEP;t++);
c=*t;
*t='\0';
v = newword(s, c=='\0'?(struct word *)0:enval(t+1));
*t = c;
return v;
}
void
Vinit(void)
{
extern char **environ;
char *s;
char **env = environ;
environp = env;
for(;*env;env++){
for(s=*env;*s && *s!='(' && *s!='=';s++);
switch(*s){
case '\0':
pfmt(err, "environment %q?\n", *env);
break;
case '=':
*s='\0';
setvar(*env, enval(s+1));
*s='=';
break;
case '(': /* ignore functions for now */
break;
}
}
}
char **envp;
void
Xrdfn(void)
{
char *s;
int len;
for(;*envp;envp++){
for(s=*envp;*s && *s!='(' && *s!='=';s++);
switch(*s){
case '\0':
pfmt(err, "environment %q?\n", *envp);
break;
case '=': /* ignore variables */
break;
case '(': /* Bourne again */
s=*envp+3;
envp++;
len = strlen(s);
s[len]='\n';
execcmds(opencore(s, len+1));
s[len]='\0';
return;
}
}
Xreturn();
}
union code rdfns[4];
void
execfinit(void)
{
static int first = 1;
if(first){
rdfns[0].i = 1;
rdfns[1].f = Xrdfn;
rdfns[2].f = Xjump;
rdfns[3].i = 1;
first = 0;
}
Xpopm();
envp = environp;
start(rdfns, 1, runq->local);
}
int
cmpenv(const void *aa, const void *ab)
{
char **a = (char**)aa, **b = (char**)ab;
return strcmp(*a, *b);
}
char **
mkenv(void)
{
char **env, **ep, *p, *q;
struct var **h, *v;
struct word *a;
int nvar = 0, nchr = 0, sep;
/*
* Slightly kludgy loops look at locals then globals.
* locals no longer exist - geoff
*/
for(h = gvar-1; h != &gvar[NVAR]; h++)
for(v = h >= gvar? *h: runq->local; v ;v = v->next){
if((v==vlook(v->name)) && v->val){
nvar++;
nchr+=strlen(v->name)+1;
for(a = v->val;a;a = a->next)
nchr+=strlen(a->word)+1;
}
if(v->fn){
nvar++;
nchr+=strlen(v->name)+strlen(v->fn[v->pc-1].s)+8;
}
}
env = (char **)emalloc((nvar+1)*sizeof(char *)+nchr);
ep = env;
p = (char *)&env[nvar+1];
for(h = gvar-1; h != &gvar[NVAR]; h++)
for(v = h >= gvar? *h: runq->local;v;v = v->next){
if((v==vlook(v->name)) && v->val){
*ep++=p;
q = v->name;
while(*q) *p++=*q++;
sep='=';
for(a = v->val;a;a = a->next){
*p++=sep;
sep = SEP;
q = a->word;
while(*q) *p++=*q++;
}
*p++='\0';
}
if(v->fn){
*ep++=p;
*p++='#'; *p++='('; *p++=')'; /* to fool Bourne */
*p++='f'; *p++='n'; *p++=' ';
q = v->name;
while(*q) *p++=*q++;
*p++=' ';
q = v->fn[v->pc-1].s;
while(*q) *p++=*q++;
*p++='\0';
}
}
*ep = 0;
qsort((void *)env, nvar, sizeof ep[0], cmpenv);
return env;
}
char *sigmsg[] = {
/* 0 normal */ 0,
/* 1 SIGHUP */ "Hangup",
/* 2 SIGINT */ 0,
/* 3 SIGQUIT */ "Quit",
/* 4 SIGILL */ "Illegal instruction",
/* 5 SIGTRAP */ "Trace/BPT trap",
/* 6 SIGIOT */ "abort",
/* 7 SIGEMT */ "EMT trap",
/* 8 SIGFPE */ "Floating exception",
/* 9 SIGKILL */ "Killed",
/* 10 SIGBUS */ "Bus error",
/* 11 SIGSEGV */ "Memory fault",
/* 12 SIGSYS */ "Bad system call",
/* 13 SIGPIPE */ 0,
/* 14 SIGALRM */ "Alarm call",
/* 15 SIGTERM */ "Terminated",
/* 16 unused */ "signal 16",
/* 17 SIGSTOP */ "Process stopped",
/* 18 unused */ "signal 18",
/* 19 SIGCONT */ "Process continued",
/* 20 SIGCHLD */ "Child death",
};
int
Waitfor(int pid, int persist)
{
int wpid, sig;
struct thread *p;
int wstat;
char wstatstr[12];
for(;;){
errno = 0;
wpid = wait(&wstat);
if(errno==EINTR && persist)
continue;
if(wpid==-1)
break;
sig = wstat&0177;
if(sig==0177){
pfmt(err, "trace: ");
sig = (wstat>>8)&0177;
}
if(sig>(sizeof sigmsg/sizeof sigmsg[0]) || sigmsg[sig]){
if(pid!=wpid)
pfmt(err, "%d: ", wpid);
if(sig<=(sizeof sigmsg/sizeof sigmsg[0]))
pfmt(err, "%s", sigmsg[sig]);
else if(sig==0177) pfmt(err, "stopped by ptrace");
else pfmt(err, "signal %d", sig);
if(wstat&0200)pfmt(err, " -- core dumped");
pfmt(err, "\n");
}
wstat = sig?sig+1000:(wstat>>8)&0xFF;
if(wpid==pid){
inttoascii(wstatstr, wstat);
setstatus(wstatstr);
break;
}
else{
for(p = runq->ret;p;p = p->ret)
if(p->pid==wpid){
p->pid=-1;
inttoascii(p->status, wstat);
break;
}
}
}
return 0;
}
char **
mkargv(register struct word *a)
{
char **argv = (char **)emalloc((count(a)+2)*sizeof(char *));
char **argp = argv+1; /* leave one at front for runcoms */
for(;a;a = a->next)
*argp++=a->word;
*argp = 0;
return argv;
}
void
Updenv(void)
{
}
void
Execute(struct word *args, struct word *path)
{
char *msg="not found";
int txtbusy = 0;
char **env = mkenv();
char **argv = mkargv(args);
char file[512];
for(;path;path = path->next){
strcpy(file, path->word);
if(file[0])
strcat(file, "/");
strcat(file, argv[1]);
ReExec:
execve(file, argv+1, env);
switch(errno){
case ENOEXEC:
pfmt(err, "%s: Bourne again\n", argv[1]);
argv[0]="sh";
argv[1] = strdup(file);
execve("/bin/sh", argv, env);
goto Bad;
case ETXTBSY:
if(++txtbusy!=5){
sleep(txtbusy);
goto ReExec;
}
msg="text busy"; goto Bad;
case EACCES:
msg="no access";
break;
case ENOMEM:
msg="not enough memory"; goto Bad;
case E2BIG:
msg="too big"; goto Bad;
}
}
Bad:
pfmt(err, "%s: %s\n", argv[1], msg);
efree((char *)env);
efree((char *)argv);
}
#define NDIR NAME_MAX
int
Globsize(register char *p)
{
int isglob = 0, globlen = NDIR+1;
for(;*p;p++){
if(*p==GLOB){
p++;
if(*p!=GLOB)
isglob++;
globlen+=*p=='*'?NDIR:1;
}
else
globlen++;
}
return isglob?globlen:0;
}
#define NDIRLIST 50
DIR *dirlist[NDIRLIST];
int
Opendir(char *name)
{
DIR **dp;
for(dp = dirlist;dp!=&dirlist[NDIRLIST];dp++)
if(*dp==0){
*dp = opendir(name);
return *dp?dp-dirlist:-1;
}
return -1;
}
int
Readdir(int f, char *p, int onlydirs)
{
struct dirent *dp;
if(f<0 || f>=NDIRLIST)
return 0;
dp = readdir(dirlist[f]);
if(dp==0)
return 0;
strcpy(p, dp->d_name);
return 1;
}
void
Closedir(int f)
{
if(f<0 || f>=NDIRLIST)
return;
closedir(dirlist[f]);
dirlist[f] = 0;
}
char *Signame[] = {
"sigexit", "sighup", "sigint", "sigquit",
"sigill", "sigtrap", "sigiot", "sigemt",
"sigfpe", "sigkill", "sigbus", "sigsegv",
"sigsys", "sigpipe", "sigalrm", "sigterm",
"sig16", "sigstop", "sigtstp", "sigcont",
"sigchld", "sigttin", "sigttou", "sigtint",
"sigxcpu", "sigxfsz", "sig26", "sig27",
"sig28", "sig29", "sig30", "sig31",
0,
};
void
gettrap(int sig)
{
signal(sig, gettrap);
trap[sig]++;
ntrap++;
if(ntrap>=NSIG){
pfmt(err, "rc: Too many traps (trap %d), dumping core\n", sig);
signal(SIGABRT, (void (*)())0);
kill(getpid(), SIGABRT);
}
}
void
Trapinit(void)
{
int i;
void (*sig)();
if(1 || flag['d']){ /* wrong!!! */
sig = signal(SIGINT, gettrap);
if(sig==SIG_IGN)
signal(SIGINT, SIG_IGN);
}
else{
for(i = 1;i<=NSIG;i++) if(i!=SIGCHLD){
sig = signal(i, gettrap);
if(sig==SIG_IGN)
signal(i, SIG_IGN);
}
}
}
void
Unlink(char *name)
{
unlink(name);
}
long
Write(int fd, char *buf, long cnt)
{
return write(fd, buf, cnt);
}
long
Read(int fd, char *buf, long cnt)
{
return read(fd, buf, cnt);
}
long
Seek(int fd, long cnt, long whence)
{
return lseek(fd, cnt, whence);
}
int
Executable(char *file)
{
return(access(file, 01)==0);
}
int
Creat(char *file)
{
return creat(file, 0666);
}
int
Dup(int a, int b){
return dup2(a, b);
}
int
Dup1(int a){
return dup(a);
}
/*
* Wrong: should go through components of a|b|c and return the maximum.
*/
void
Exit(char *stat)
{
int n = 0;
while(*stat){
if(*stat!='|'){
if(*stat<'0' || '9'<*stat)
exit(1);
else n = n*10+*stat-'0';
}
stat++;
}
exit(n);
}
int
Eintr(void){
return errno==EINTR;
}
void
Noerror(void)
{
errno = 0;
}
int
Isatty(int fd){
return isatty(fd);
}
void
Abort(void)
{
abort();
}
void
execumask(void) /* wrong -- should fork before writing */
{
int m;
struct io out[1];
switch(count(runq->argv->words)){
default:
pfmt(err, "Usage: umask [umask]\n");
setstatus("umask usage");
poplist();
return;
case 2:
umask(octal(runq->argv->words->next->word));
break;
case 1:
umask(m = umask(0));
out->fd = mapfd(1);
out->bufp = out->buf;
out->ebuf=&out->buf[NBUF];
out->strp = 0;
pfmt(out, "%o\n", m);
break;
}
setstatus("");
poplist();
}
void
Memcpy(char *a, char *b, long n)
{
memmove(a, b, n);
}
void*
Malloc(unsigned long n)
{
return (void *)malloc(n);
}
void
errstr(char *buf, int len)
{
strncpy(buf, strerror(errno), len);
}
int
needsrcquote(int c)
{
if(c <= ' ')
return 1;
if(strchr("`^#*[]=|\\?${}()'<>&;", c))
return 1;
return 0;
}
int
rfork(int bits)
{
return fork();
}
int *waitpids;
int nwaitpids;
void
addwaitpid(int pid)
{
waitpids = realloc(waitpids, (nwaitpids+1)*sizeof waitpids[0]);
if(waitpids == 0)
panic("Can't realloc %d waitpids", nwaitpids+1);
waitpids[nwaitpids++] = pid;
}
void
delwaitpid(int pid)
{
int r, w;
for(r=w=0; r<nwaitpids; r++)
if(waitpids[r] != pid)
waitpids[w++] = waitpids[r];
nwaitpids = w;
}
void
clearwaitpids(void)
{
nwaitpids = 0;
}
int
havewaitpid(int pid)
{
int i;
for(i=0; i<nwaitpids; i++)
if(waitpids[i] == pid)
return 1;
return 0;
}
int
chartorune(wchar_t *rune, const char *str)
{
int r = mbtowc(rune, str, strlen(str));
if(r < 0){
*rune = 0xFFFD;
return 1;
}
return r;
}