-
Notifications
You must be signed in to change notification settings - Fork 2
/
scall2.c
665 lines (503 loc) · 11.6 KB
/
scall2.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
/**************************************************
UZI (Unix Z80 Implementation) Kernel: scall2.c
***************************************************/
/*LINTLIBRARY*/
#include "unix.h"
#include "extern.h"
/* Getpid() */
_getpid()
{
return(udata.u_ptab->p_pid);
}
/* Getppid() */
_getppid()
{
return(udata.u_ptab->p_pptr->p_pid);
}
/* Getuid() */
_getuid()
{
return(udata.u_ptab->p_uid);
}
_getgid()
{
return(udata.u_gid);
}
/*********************************
setuid(uid)
int uid;
***********************************/
#define uid (int)udata.u_argn
_setuid()
{
if (super() || udata.u_ptab->p_uid == uid)
{
udata.u_ptab->p_uid = uid;
udata.u_euid = uid;
return(0);
}
udata.u_error = EPERM;
return(-1);
}
#undef uid
/*****************************************
setgid(gid)
int gid;
****************************************/
#define gid (int16)udata.u_argn
_setgid()
{
if (super() || udata.u_gid == gid)
{
udata.u_gid = gid;
udata.u_egid = gid;
return(0);
}
udata.u_error = EPERM;
return(-1);
}
#undef gid;
/***********************************
time(tvec)
int tvec[];
**************************************/
#define tvec (int *)udata.u_argn
_time()
{
rdtime(tvec); /* In machdep.c */
return(0);
}
#undef tvec
/**************************************
stime(tvec)
int tvec[];
**********************************/
#define tvec (int *)udata.u_argn
_stime()
{
/*
ifnot (super())
{
udata.u_error = EPERM;
return(-1);
}
sttime(tvec);
return(0);
*/
udata.u_error = EPERM;
return(-1);
}
#undef tvec
/********************************************
times(buf)
char *buf;
**********************************************/
#define buf (char *)udata.u_argn
_times()
{
ifnot (valadr(buf,6*sizeof(time_t)))
return(-1);
di();
bcopy(&udata.u_utime, buf, 4*sizeof(time_t));
bcopy(&ticks, buf + 4*sizeof(time_t), sizeof(time_t));
ei();
return(0);
}
#undef buf
/* User's execve() call. All other flavors are library routines. */
/*****************************************
execve(name, argv, envp)
char *name;
char *argv[];
char *envp[];
*****************************************/
#define name (char *)udata.u_argn2
#define argv (char **)udata.u_argn1
#define envp (char **)udata.u_argn
_execve()
{
register inoptr ino;
register char *buf;
inoptr n_open();
char *bread();
blkno_t bmap();
ifnot (ino = n_open(name,NULLINOPTR))
return(-1);
if (ino->c_node.i_size.o_blkno >= ((uint16)(&udata)/512))
{
udata.u_error = ENOMEM;
goto nogood;
}
ifnot ( (getperm(ino) & OTH_EX) &&
(ino->c_node.i_mode & F_REG) &&
(ino->c_node.i_mode & (OWN_EX | OTH_EX | GRP_EX)) )
{
udata.u_error = EACCES;
goto nogood;
}
setftime(ino, A_TIME);
/* Gather the arguments, and put them on the root device */
/* Put environment on another block */
if (wargs(argv, 0) || wargs(envp, 1))
goto nogood;
/* Read in the first block of the new program */
buf = bread( ino->c_dev, bmap(ino, 0, 1), 0);
if ((*buf & 0xff) != EMAGIC)
{
udata.u_error = ENOEXEC;
goto nogood2;
}
/* Here, check the setuid stuff. No other changes need be made in
the user data */
if (ino->c_node.i_mode & SET_UID)
udata.u_euid = ino->c_node.i_uid;
if (ino->c_node.i_mode & SET_GID)
udata.u_egid = ino->c_node.i_gid;
bcopy(buf,PROGBASE,512);
bfree(buf, 0);
/* At this point, we are committed to reading in and executing
the program. We switch to a local stack, and pass to it
the necessary parameter: ino */
udata.u_ino = ino; /* Termorarily stash these here */
tempstk();
exec2(); /* Never returns */
nogood2:
bfree(buf, 0);
nogood:
i_deref(ino);
return(-1);
}
#undef name
#undef argv
#undef envp
exec2()
{
register blkno_t blk;
register char **argv;
register char **envp;
register int (**sp)();
int argc;
char *rargs();
register char *progptr;
char *buf;
blkno_t pblk;
blkno_t bmap();
char *bread();
/* Read in the rest of the program */
progptr = PROGBASE+512;
for (blk = 1; blk <= udata.u_ino->c_node.i_size.o_blkno; ++blk)
{
pblk = bmap(udata.u_ino, blk, 1);
if (pblk != -1)
{
buf = bread( udata.u_ino->c_dev, pblk, 0);
bcopy(buf, progptr, 512);
bfree(buf, 0);
}
progptr += 512;
}
i_deref(udata.u_ino);
/* Zero out the free memory */
bzero(progptr,(uint16)((char *)&udata - progptr));
udata.u_break = progptr;
/* Read back the arguments and the environment */
argv = (char **)rargs((char *)&udata, 0, &argc);
envp = (char **)rargs((char *)argv, 1, NULL);
/* Fill in udata.u_name */
bcopy(*argv,udata.u_name,8);
/* Turn off caught signals */
for (sp= udata.u_sigvec; sp < (udata.u_sigvec+NSIGS); ++sp)
if (*sp != SIG_IGN)
*sp = SIG_DFL;
/* Shove argc and the address of argv just below envp */
*(envp - 1) = (char *)argc;
*(envp - 2) = (char *)argv;
/* Go jump into the program, first setting the stack */
doexec((int16 *)(udata.u_isp = envp - 2));
}
wargs(argv,blk)
char **argv;
int blk;
{
register char *ptr; /* Address of base of arg strings in user space */
register int n;
struct s_argblk *argbuf;
register char *bufp;
register int j;
char *zerobuf();
char *bread();
/* Gather the arguments, and put them on the swap device */
argbuf = (struct s_argblk *)bread(SWAPDEV, udata.u_ptab->p_swap+blk, 2);
bufp = argbuf->a_buf;
for (j=0; argv[j] != NULL; ++j)
{
ptr = argv[j];
do
{
*bufp++ = *ptr;
if (bufp >= argbuf->a_buf+500)
{
udata.u_error = E2BIG;
bfree((char *)argbuf, 1);
return (1);
}
}
while (*ptr++ != '\0');
}
argbuf->a_argc = j; /* Store argc in argbuf. */
argbuf->a_arglen = bufp - argbuf->a_buf; /*Store total string size. */
/* Swap out the arguments into the given swap block */
bfree((char *)argbuf, 1);
return (0);
}
char *
rargs(ptr,blk,cnt)
register char *ptr;
int blk;
int *cnt;
{
struct s_argblk *argbuf;
register char **argv; /* Address of users argv[], just below ptr */
register int n;
char *bread();
/* Read back the arguments */
argbuf = (struct s_argblk *)bread(SWAPDEV,udata.u_ptab->p_swap+blk, 0);
/* Move them into the users address space, at the very top */
ptr -= argbuf->a_arglen;
if (argbuf->a_arglen)
bcopy(argbuf->a_buf, ptr, argbuf->a_arglen);
/* Set argv to point below the argument strings */
argv = (char **)ptr - (argbuf->a_argc + 1);
/* Set each element of argv[] to point to its argument string */
argv[0] = ptr;
for (n=1; n < argbuf->a_argc; ++n)
argv[n] = argv[n-1] + strlen(argv[n-1]) + 1;
argv[argbuf->a_argc] = NULL;
if (cnt)
*cnt = argbuf->a_argc;
bfree((char *)argbuf, 0);
return (argv);
}
/**********************************
brk(addr)
char *addr;
************************************/
#define addr (char *)udata.u_argn
_brk()
{
char dummy; /* A thing to take address of */
/* A hack to get approx val of stack ptr. */
if (addr < PROGBASE || (addr+64) >= (char *)&dummy)
{
udata.u_error = ENOMEM;
return(-1);
}
udata.u_break = addr;
return(0);
}
#undef addr
/************************************
sbrk(incr)
uint16 incr;
***************************************/
#define incr (uint16)udata.u_argn
_sbrk()
{
register char *oldbrk;
udata.u_argn += (oldbrk = udata.u_break);
if (_brk())
return(-1);
return((int)oldbrk);
}
#undef incr
/**************************************
wait(statloc)
int *statloc;
****************************************/
#define statloc (int *)udata.u_argn
_wait()
{
register ptptr p;
register int retval;
if (statloc > (int *)(&udata))
{
udata.u_error = EFAULT;
return(-1);
}
di();
/* See if we have any children. */
for (p=ptab;p < ptab+PTABSIZE; ++p)
{
if (p->p_status && p->p_pptr == udata.u_ptab && p != udata.u_ptab)
goto ok;
}
udata.u_error = ECHILD;
ei();
return (-1);
ok:
/* Search for an exited child; */
for (;;)
{
chksigs();
if (udata.u_cursig)
{
udata.u_error = EINTR;
return(-1);
}
di();
for(p=ptab;p < ptab+PTABSIZE; ++p)
{
if (p->p_status == P_ZOMBIE && p->p_pptr == udata.u_ptab)
{
if (statloc)
*statloc = p->p_exitval;
p->p_status = P_EMPTY;
retval = p->p_pid;
/* Add in child's time info */
/* It was stored on top of p_wait in the childs process
table entry */
addtick(&udata.u_cutime, &(p->p_wait));
addtick(&udata.u_cstime, (char *)(&(p->p_wait)) +
sizeof(time_t));
ei();
return(retval);
}
}
/* Nothing yet, so wait */
psleep(udata.u_ptab);
}
}
#undef statloc
/**************************************
_exit(val)
int16 val;
**************************************/
#define val (int16)udata.u_argn
__exit()
{
doexit(val,0);
}
#undef val
doexit(val,val2)
int16 val;
int16 val2;
{
register int16 j;
register ptptr p;
for (j=0; j < UFTSIZE; ++j)
{
ifnot (udata.u_files[j] & 0x80) /* Portable equivalent of == -1 */
doclose(j);
}
/* _sync(); /* Not necessary, but a good idea. */
di();
udata.u_ptab->p_exitval = (val<<8) | (val2 & 0xff);
/* Set child's parents to init */
for(p=ptab;p < ptab+PTABSIZE; ++p)
{
if (p->p_status && p->p_pptr == udata.u_ptab)
p->p_pptr = initproc;
}
i_deref(udata.u_cwd);
/* Stash away child's execution tick counts in process table,
overlaying some no longer necessary stuff. */
addtick(&udata.u_utime,&udata.u_cutime);
addtick(&udata.u_stime,&udata.u_cstime);
bcopy(&udata.u_utime, &(udata.u_ptab->p_wait), 2 * sizeof(time_t));
/* Wake up a waiting parent, if any. */
if (udata.u_ptab != initproc)
wakeup((char *)udata.u_ptab->p_pptr);
udata.u_ptab->p_status = P_ZOMBIE;
ei();
swapin(getproc());
panic("doexit:won't exit");
}
_fork()
{
return (dofork());
}
_pause()
{
psleep(0);
udata.u_error = EINTR;
return(-1);
}
/*************************************
signal(sig, func)
int16 sig;
int16 (*func)();
***************************************/
#define sig (int16)udata.u_argn1
#define func (int (*)())udata.u_argn
_signal()
{
int retval;
di();
if (sig < 1 || sig == SIGKILL || sig >= NSIGS)
{
udata.u_error = EINVAL;
goto nogood;
}
if (func == SIG_IGN)
udata.u_ptab->p_ignored |= sigmask(sig);
else
{
if (func != SIG_DFL && ((char *)func < PROGBASE ||
(struct u_data *)func >= &udata))
{
udata.u_error = EFAULT;
goto nogood;
}
udata.u_ptab->p_ignored &= ~sigmask(sig);
}
retval = udata.u_sigvec[sig];
udata.u_sigvec[sig] = func;
ei();
return(retval);
nogood:
ei();
return(-1);
}
#undef sig
#undef func
/**************************************
kill(pid, sig)
int16 pid;
int16 sig;
*****************************************/
#define pid (int16)udata.u_argn1
#define sig (int16)udata.u_argn
_kill()
{
ptptr p;
if (sig <= 0 || sig > 15)
goto nogood;
for (p=ptab; p < ptab+PTABSIZE; ++p)
{
if (p->p_pid == pid)
{
sendsig(p,sig);
return(0);
}
}
nogood:
udata.u_error = EINVAL;
return(-1);
}
#undef pid
#undef sig
/********************************
alarm(secs)
uint16 secs;
*********************************/
#define secs (int16)udata.u_argn
_alarm()
{
int retval;
di();
retval = udata.u_ptab->p_alarm;
udata.u_ptab->p_alarm = secs;
ei();
return(retval);
}
#undef secs