date and time created 82/04/02 10:29:14 by wnj
[unix-history] / usr / src / sys / kern / kern_proc.c
CommitLineData
a4aaef65 1/* kern_proc.c 4.24 82/03/27 */
29dd101b
BJ
2
3#include "../h/param.h"
4#include "../h/systm.h"
5#include "../h/map.h"
6#include "../h/mtpr.h"
7#include "../h/dir.h"
8#include "../h/user.h"
9#include "../h/proc.h"
10#include "../h/buf.h"
11#include "../h/reg.h"
12#include "../h/inode.h"
13#include "../h/seg.h"
14#include "../h/acct.h"
8643403f 15#include "/usr/include/wait.h"
29dd101b
BJ
16#include "../h/pte.h"
17#include "../h/vm.h"
18#include "../h/text.h"
bdfe5b0f 19#include "../h/psl.h"
b07c4d64 20#include "../h/vlimit.h"
3ca1542b 21#include "../h/file.h"
29dd101b
BJ
22
23/*
24 * exec system call, with and without environments.
25 */
26struct execa {
27 char *fname;
28 char **argp;
29 char **envp;
30};
31
32exec()
33{
34 ((struct execa *)u.u_ap)->envp = NULL;
35 exece();
36}
37
38exece()
39{
40 register nc;
41 register char *cp;
42 register struct buf *bp;
43 register struct execa *uap;
44 int na, ne, ucp, ap, c;
7eeaac77
RE
45 int indir, uid, gid;
46 char *sharg;
29dd101b
BJ
47 struct inode *ip;
48 swblk_t bno;
7eeaac77
RE
49 char cfname[DIRSIZ];
50 char cfarg[SHSIZE];
29dd101b 51
5485e062 52 if ((ip = namei(uchar, 0, 1)) == NULL)
29dd101b
BJ
53 return;
54 bno = 0;
55 bp = 0;
7eeaac77
RE
56 indir = 0;
57 uid = u.u_uid;
58 gid = u.u_gid;
7eeaac77
RE
59 if (ip->i_mode & ISUID)
60 uid = ip->i_uid;
61 if (ip->i_mode & ISGID)
62 gid = ip->i_gid;
63
64 again:
e92a04af 65 if (access(ip, IEXEC))
29dd101b 66 goto bad;
e92a04af 67 if ((u.u_procp->p_flag&STRC) && access(ip, IREAD))
eb83bf86 68 goto bad;
e92a04af 69 if ((ip->i_mode & IFMT) != IFREG ||
29dd101b
BJ
70 (ip->i_mode & (IEXEC|(IEXEC>>3)|(IEXEC>>6))) == 0) {
71 u.u_error = EACCES;
72 goto bad;
73 }
7eeaac77
RE
74
75 /*
76 * Read in first few bytes of file for segment sizes, ux_mag:
77 * 407 = plain executable
78 * 410 = RO text
79 * 413 = demand paged RO text
80 * Also an ASCII line beginning with #! is
81 * the file name of a ``shell'' and arguments may be prepended
82 * to the argument list if given here.
83 *
84 * SHELL NAMES ARE LIMITED IN LENGTH.
85 *
86 * ONLY ONE ARGUMENT MAY BE PASSED TO THE SHELL FROM
87 * THE ASCII LINE.
88 */
89 u.u_base = (caddr_t)&u.u_exdata;
90 u.u_count = sizeof(u.u_exdata);
91 u.u_offset = 0;
92 u.u_segflg = 1;
93 readi(ip);
94 u.u_segflg = 0;
e92a04af 95 if (u.u_error)
7eeaac77 96 goto bad;
348d7c13
BJ
97 if (u.u_count > sizeof(u.u_exdata) - sizeof(u.u_exdata.Ux_A) &&
98 u.u_exdata.ux_shell[0] != '#') {
7eeaac77
RE
99 u.u_error = ENOEXEC;
100 goto bad;
101 }
102 switch (u.u_exdata.ux_mag) {
103
104 case 0407:
105 u.u_exdata.ux_dsize += u.u_exdata.ux_tsize;
106 u.u_exdata.ux_tsize = 0;
107 break;
108
109 case 0413:
110 case 0410:
111 if (u.u_exdata.ux_tsize == 0) {
112 u.u_error = ENOEXEC;
113 goto bad;
114 }
115 break;
116
117 default:
118 if (u.u_exdata.ux_shell[0] != '#' ||
119 u.u_exdata.ux_shell[1] != '!' ||
120 indir) {
121 u.u_error = ENOEXEC;
122 goto bad;
123 }
124 cp = &u.u_exdata.ux_shell[2]; /* skip "#!" */
125 while (cp < &u.u_exdata.ux_shell[SHSIZE]) {
126 if (*cp == '\t')
127 *cp = ' ';
128 else if (*cp == '\n') {
129 *cp = '\0';
130 break;
131 }
132 cp++;
133 }
134 if (*cp != '\0') {
135 u.u_error = ENOEXEC;
136 goto bad;
137 }
138 cp = &u.u_exdata.ux_shell[2];
139 while (*cp == ' ')
140 cp++;
141 u.u_dirp = cp;
142 while (*cp && *cp != ' ')
143 cp++;
144 sharg = NULL;
145 if (*cp) {
146 *cp++ = '\0';
147 while (*cp == ' ')
148 cp++;
149 if (*cp) {
150 bcopy((caddr_t)cp, (caddr_t)cfarg, SHSIZE);
151 sharg = cfarg;
152 }
153 }
154 bcopy((caddr_t)u.u_dbuf, (caddr_t)cfname, DIRSIZ);
155 indir = 1;
156 iput(ip);
5485e062 157 ip = namei(schar, 0, 1);
7eeaac77
RE
158 if (ip == NULL)
159 return;
160 goto again;
161 }
162
29dd101b
BJ
163 /*
164 * Collect arguments on "file" in swap space.
165 */
166 na = 0;
167 ne = 0;
168 nc = 0;
169 uap = (struct execa *)u.u_ap;
b725a0ca 170 if ((bno = rmalloc(argmap, ctod(clrnd((int) btoc(NCARGS))))) == 0) {
29dd101b
BJ
171 swkill(u.u_procp, "exece");
172 goto bad;
173 }
174 if (bno % CLSIZE)
b725a0ca 175 panic("execa rmalloc");
29dd101b
BJ
176 if (uap->argp) for (;;) {
177 ap = NULL;
40d07ebb 178 if (indir && (na == 1 || na == 2 && sharg))
7eeaac77
RE
179 ap = (int)uap->fname;
180 else if (uap->argp) {
29dd101b
BJ
181 ap = fuword((caddr_t)uap->argp);
182 uap->argp++;
183 }
184 if (ap==NULL && uap->envp) {
185 uap->argp = NULL;
186 if ((ap = fuword((caddr_t)uap->envp)) == NULL)
187 break;
188 uap->envp++;
189 ne++;
190 }
191 if (ap==NULL)
192 break;
193 na++;
e92a04af 194 if (ap == -1)
29dd101b
BJ
195 u.u_error = EFAULT;
196 do {
197 if (nc >= NCARGS-1)
198 u.u_error = E2BIG;
7eeaac77
RE
199 if (indir && na == 2 && sharg != NULL)
200 c = *sharg++ & 0377;
201 else if ((c = fubyte((caddr_t)ap++)) < 0)
29dd101b 202 u.u_error = EFAULT;
64d6118b
BJ
203 if (u.u_error) {
204 if (bp)
205 brelse(bp);
206 bp = 0;
29dd101b 207 goto badarg;
64d6118b 208 }
29dd101b
BJ
209 if ((nc&BMASK) == 0) {
210 if (bp)
211 bdwrite(bp);
41888f16
BJ
212 bp = getblk(argdev,
213 (daddr_t)(dbtofsb(bno)+(nc>>BSHIFT)));
29dd101b
BJ
214 cp = bp->b_un.b_addr;
215 }
216 nc++;
217 *cp++ = c;
218 } while (c>0);
219 }
220 if (bp)
221 bdwrite(bp);
222 bp = 0;
223 nc = (nc + NBPW-1) & ~(NBPW-1);
7eeaac77
RE
224 if (indir)
225 bcopy((caddr_t)cfname, (caddr_t)u.u_dbuf, DIRSIZ);
226 getxfile(ip, nc + (na+4)*NBPW, uid, gid);
29a06346 227 if (u.u_error) {
29dd101b
BJ
228badarg:
229 for (c = 0; c < nc; c += BSIZE)
41888f16 230 if (bp = baddr(argdev, dbtofsb(bno)+(c>>BSHIFT))) {
29dd101b
BJ
231 bp->b_flags |= B_AGE; /* throw away */
232 bp->b_flags &= ~B_DELWRI; /* cancel io */
233 brelse(bp);
234 bp = 0;
235 }
236 goto bad;
237 }
238
239 /*
240 * copy back arglist
241 */
29dd101b
BJ
242 ucp = USRSTACK - nc - NBPW;
243 ap = ucp - na*NBPW - 3*NBPW;
244 u.u_ar0[SP] = ap;
81263dba 245 (void) suword((caddr_t)ap, na-ne);
29dd101b
BJ
246 nc = 0;
247 for (;;) {
248 ap += NBPW;
249 if (na==ne) {
81263dba 250 (void) suword((caddr_t)ap, 0);
29dd101b
BJ
251 ap += NBPW;
252 }
253 if (--na < 0)
254 break;
81263dba 255 (void) suword((caddr_t)ap, ucp);
29dd101b
BJ
256 do {
257 if ((nc&BMASK) == 0) {
258 if (bp)
259 brelse(bp);
41888f16
BJ
260 bp = bread(argdev,
261 (daddr_t)(dbtofsb(bno)+(nc>>BSHIFT)));
29dd101b
BJ
262 bp->b_flags |= B_AGE; /* throw away */
263 bp->b_flags &= ~B_DELWRI; /* cancel io */
264 cp = bp->b_un.b_addr;
265 }
81263dba 266 (void) subyte((caddr_t)ucp++, (c = *cp++));
29dd101b
BJ
267 nc++;
268 } while(c&0377);
269 }
81263dba
BJ
270 (void) suword((caddr_t)ap, 0);
271 (void) suword((caddr_t)ucp, 0);
29dd101b
BJ
272 setregs();
273bad:
274 if (bp)
275 brelse(bp);
276 if (bno)
b725a0ca 277 rmfree(argmap, ctod(clrnd((int) btoc(NCARGS))), bno);
29dd101b
BJ
278 iput(ip);
279}
280
281/*
282 * Read in and set up memory for executed file.
29dd101b 283 */
7eeaac77 284getxfile(ip, nargc, uid, gid)
29dd101b
BJ
285register struct inode *ip;
286{
29dd101b 287 register size_t ts, ds, ss;
7eeaac77 288 int pagi;
29dd101b 289
7eeaac77 290 if (u.u_exdata.ux_mag == 0413)
29dd101b 291 pagi = SPAGI;
7eeaac77
RE
292 else
293 pagi = 0;
e92a04af
BJ
294 if (u.u_exdata.ux_tsize!=0 && (ip->i_flag&ITEXT)==0 &&
295 ip->i_count!=1) {
3ca1542b
BJ
296 register struct file *fp;
297
e92a04af
BJ
298 for (fp = file; fp < fileNFILE; fp++) {
299 if (fp->f_flag & FSOCKET)
300 continue;
3ca1542b
BJ
301 if (fp->f_inode == ip && (fp->f_flag&FWRITE)) {
302 u.u_error = ETXTBSY;
303 goto bad;
304 }
e92a04af 305 }
29dd101b
BJ
306 }
307
308 /*
e92a04af 309 * Compute text and data sizes and make sure not too large.
29dd101b 310 */
29dd101b
BJ
311 ts = clrnd(btoc(u.u_exdata.ux_tsize));
312 ds = clrnd(btoc((u.u_exdata.ux_dsize+u.u_exdata.ux_bsize)));
313 ss = clrnd(SSIZE + btoc(nargc));
29a06346
BJ
314 if (chksize(ts, ds, ss))
315 goto bad;
e92a04af
BJ
316
317 /*
318 * Make sure enough space to start process.
319 */
29a06346
BJ
320 u.u_cdmap = zdmap;
321 u.u_csmap = zdmap;
322 if (swpexpand(ds, ss, &u.u_cdmap, &u.u_csmap) == NULL)
323 goto bad;
29dd101b 324
29a06346
BJ
325 /*
326 * At this point, committed to the new image!
327 * Release virtual memory resources of old process, and
328 * initialize the virtual memory of the new process.
329 * If we resulted from vfork(), instead wakeup our
330 * parent who will set SVFDONE when he has taken back
331 * our resources.
332 */
333 u.u_prof.pr_scale = 0;
334 if ((u.u_procp->p_flag & SVFORK) == 0)
335 vrelvm();
336 else {
337 u.u_procp->p_flag &= ~SVFORK;
338 u.u_procp->p_flag |= SKEEP;
339 wakeup((caddr_t)u.u_procp);
340 while ((u.u_procp->p_flag & SVFDONE) == 0)
341 sleep((caddr_t)u.u_procp, PZERO - 1);
342 u.u_procp->p_flag &= ~(SVFDONE|SKEEP);
343 }
2ce421b6 344 u.u_procp->p_flag &= ~(SPAGI|SSEQL|SUANOM|SNUSIG);
29a06346
BJ
345 u.u_procp->p_flag |= pagi;
346 u.u_dmap = u.u_cdmap;
347 u.u_smap = u.u_csmap;
348 vgetvm(ts, ds, ss);
29dd101b 349
29a06346 350 if (pagi == 0) {
29dd101b 351 /*
29a06346 352 * Read in data segment.
29dd101b 353 */
29a06346
BJ
354 u.u_base = (char *)ctob(ts);
355 u.u_offset = sizeof(u.u_exdata)+u.u_exdata.ux_tsize;
356 u.u_count = u.u_exdata.ux_dsize;
357 readi(ip);
29dd101b 358 }
29a06346
BJ
359 xalloc(ip, pagi);
360 if (pagi && u.u_procp->p_textp)
361 vinifod((struct fpte *)dptopte(u.u_procp, 0),
362 PG_FTEXT, u.u_procp->p_textp->x_iptr,
363 1 + ts/CLSIZE, (int)btoc(u.u_exdata.ux_dsize));
364
365 /* THIS SHOULD BE DONE AT A LOWER LEVEL, IF AT ALL */
366 mtpr(TBIA, 0);
367
368 /*
369 * set SUID/SGID protections, if no tracing
370 */
371 if ((u.u_procp->p_flag&STRC)==0) {
e92a04af
BJ
372 u.u_uid = uid;
373 u.u_procp->p_uid = uid;
7eeaac77 374 u.u_gid = gid;
4f4caf05 375 u.u_grps[gid/(sizeof(int)*8)] |= 1 << (gid%(sizeof(int)*8));
29a06346
BJ
376 } else
377 psignal(u.u_procp, SIGTRAP);
29dd101b
BJ
378 u.u_tsize = ts;
379 u.u_dsize = ds;
380 u.u_ssize = ss;
29dd101b 381bad:
29a06346 382 return;
29dd101b
BJ
383}
384
385/*
386 * Clear registers on exec
387 */
388setregs()
389{
594ebedd 390 register int (**rp)();
29dd101b 391 register i;
bdfe5b0f 392 long sigmask;
29dd101b 393
e92a04af 394 for (rp = &u.u_signal[0], sigmask = 1L; rp < &u.u_signal[NSIG];
bdfe5b0f
BJ
395 sigmask <<= 1, rp++) {
396 switch (*rp) {
397
398 case SIG_IGN:
399 case SIG_DFL:
400 case SIG_HOLD:
401 continue;
402
403 default:
404 /*
99fa88a2 405 * Normal or deferring catch; revert to default.
bdfe5b0f 406 */
99fa88a2
BJ
407 (void) spl6();
408 *rp = SIG_DFL;
bdfe5b0f
BJ
409 if ((int)*rp & 1)
410 u.u_procp->p_siga0 |= sigmask;
411 else
a4aaef65 412 u.u_procp->p_siga0 &= ~sigmask;
bdfe5b0f
BJ
413 if ((int)*rp & 2)
414 u.u_procp->p_siga1 |= sigmask;
415 else
416 u.u_procp->p_siga1 &= ~sigmask;
99fa88a2 417 (void) spl0();
bdfe5b0f
BJ
418 continue;
419 }
420 }
29dd101b 421/*
e92a04af 422 for (rp = &u.u_ar0[0]; rp < &u.u_ar0[16];)
29dd101b
BJ
423 *rp++ = 0;
424*/
425 u.u_ar0[PC] = u.u_exdata.ux_entloc + 2; /* skip over entry mask */
e92a04af 426 for (i=0; i<NOFILE; i++) {
29dd101b 427 if (u.u_pofile[i]&EXCLOSE) {
f3156a73 428 closef(u.u_ofile[i], 1);
29dd101b 429 u.u_ofile[i] = NULL;
bdfe5b0f 430 u.u_pofile[i] &= ~EXCLOSE;
29dd101b 431 }
29dd101b 432 }
e92a04af 433
29dd101b
BJ
434 /*
435 * Remember file name for accounting.
436 */
437 u.u_acflag &= ~AFORK;
438 bcopy((caddr_t)u.u_dbuf, (caddr_t)u.u_comm, DIRSIZ);
439}
440
441/*
e92a04af 442 * Exit system call: pass back caller's arg
29dd101b
BJ
443 */
444rexit()
445{
446 register struct a {
447 int rval;
448 } *uap;
449
450 uap = (struct a *)u.u_ap;
451 exit((uap->rval & 0377) << 8);
452}
453
454/*
455 * Release resources.
456 * Save u. area for parent to look at.
457 * Enter zombie state.
458 * Wake up parent and init processes,
459 * and dispose of children.
460 */
461exit(rv)
462{
463 register int i;
464 register struct proc *p, *q;
465 register struct file *f;
466 register int x;
467
468#ifdef PGINPROF
469 vmsizmon();
470#endif
471 p = u.u_procp;
472 p->p_flag &= ~(STRC|SULOCK);
473 p->p_flag |= SWEXIT;
474 p->p_clktim = 0;
bdfe5b0f
BJ
475 (void) spl6();
476 if ((int)SIG_IGN & 1)
477 p->p_siga0 = ~0;
478 else
479 p->p_siga0 = 0;
480 if ((int)SIG_IGN & 2)
481 p->p_siga1 = ~0;
482 else
99fa88a2 483 p->p_siga1 = 0;
bdfe5b0f 484 (void) spl0();
dd808ba3
BJ
485 p->p_cpticks = 0;
486 p->p_pctcpu = 0;
e92a04af 487 for (i=0; i<NSIG; i++)
594ebedd 488 u.u_signal[i] = SIG_IGN;
29dd101b
BJ
489 /*
490 * Release virtual memory. If we resulted from
491 * a vfork(), instead give the resources back to
492 * the parent.
493 */
494 if ((p->p_flag & SVFORK) == 0)
495 vrelvm();
496 else {
497 p->p_flag &= ~SVFORK;
498 wakeup((caddr_t)p);
499 while ((p->p_flag & SVFDONE) == 0)
500 sleep((caddr_t)p, PZERO - 1);
501 p->p_flag &= ~SVFDONE;
502 }
e92a04af 503 for (i=0; i<NOFILE; i++) {
29dd101b
BJ
504 f = u.u_ofile[i];
505 u.u_ofile[i] = NULL;
f3156a73 506 closef(f, 1);
29dd101b 507 }
e92a04af 508 ilock(u.u_cdir);
29dd101b
BJ
509 iput(u.u_cdir);
510 if (u.u_rdir) {
e92a04af 511 ilock(u.u_rdir);
29dd101b
BJ
512 iput(u.u_rdir);
513 }
054016e1 514 u.u_limit[LIM_FSIZE] = INFINITY;
29dd101b
BJ
515 acct();
516 vrelpt(u.u_procp);
517 vrelu(u.u_procp, 0);
f12a8410 518 (void) spl5(); /* hack for mem alloc race XXX */
29dd101b 519 multprog--;
29dd101b 520 p->p_stat = SZOMB;
42343c0f 521 noproc = 1;
29dd101b
BJ
522 i = PIDHASH(p->p_pid);
523 x = p - proc;
524 if (pidhash[i] == x)
525 pidhash[i] = p->p_idhash;
526 else {
527 for (i = pidhash[i]; i != 0; i = proc[i].p_idhash)
528 if (proc[i].p_idhash == x) {
529 proc[i].p_idhash = p->p_idhash;
530 goto done;
531 }
532 panic("exit");
533 }
9c82b9fd
BJ
534 if (p->p_pid == 1)
535 panic("init died");
29dd101b
BJ
536done:
537 ((struct xproc *)p)->xp_xstat = rv; /* overlay */
538 ((struct xproc *)p)->xp_vm = u.u_vm; /* overlay */
539 vmsadd(&((struct xproc *)p)->xp_vm, &u.u_cvm);
e92a04af
BJ
540 for (q = proc; q < procNPROC; q++)
541 if (q->p_pptr == p) {
bdfe5b0f 542 q->p_pptr = &proc[1];
29dd101b 543 q->p_ppid = 1;
bdfe5b0f
BJ
544 wakeup((caddr_t)&proc[1]);
545 /*
0dde1c43 546 * Traced processes are killed
bdfe5b0f 547 * since their existence means someone is screwing up.
62bac59f 548 * Stopped processes are sent a hangup and a continue.
0dde1c43
BJ
549 * This is designed to be ``safe'' for setuid
550 * processes since they must be willing to tolerate
551 * hangups anyways.
bdfe5b0f 552 */
0dde1c43 553 if (q->p_flag&STRC) {
bdfe5b0f
BJ
554 q->p_flag &= ~STRC;
555 psignal(q, SIGKILL);
0dde1c43
BJ
556 } else if (q->p_stat == SSTOP) {
557 psignal(q, SIGHUP);
558 psignal(q, SIGCONT);
bdfe5b0f 559 }
8643403f
BJ
560 /*
561 * Protect this process from future
7ac93404 562 * tty signals, clear TSTP/TTIN/TTOU if pending.
8643403f 563 */
934e4ecf 564 (void) spgrp(q, -1);
29dd101b 565 }
bdfe5b0f
BJ
566 wakeup((caddr_t)p->p_pptr);
567 psignal(p->p_pptr, SIGCHLD);
29dd101b
BJ
568 swtch();
569}
570
571wait()
572{
bdfe5b0f
BJ
573 struct vtimes vm;
574 struct vtimes *vp;
29dd101b 575
bdfe5b0f
BJ
576 if ((u.u_ar0[PS] & PSL_ALLCC) != PSL_ALLCC) {
577 wait1(0, (struct vtimes *)0);
578 return;
579 }
580 vp = (struct vtimes *)u.u_ar0[R1];
581 wait1(u.u_ar0[R0], &vm);
582 if (u.u_error)
583 return;
584 (void) copyout((caddr_t)&vm, (caddr_t)vp, sizeof (struct vtimes));
29dd101b
BJ
585}
586
587/*
588 * Wait system call.
589 * Search for a terminated (zombie) child,
590 * finally lay it to rest, and collect its status.
591 * Look also for stopped (traced) children,
592 * and pass back status from them.
593 */
bdfe5b0f
BJ
594wait1(options, vp)
595 register options;
29dd101b
BJ
596 struct vtimes *vp;
597{
598 register f;
599 register struct proc *p;
600
601 f = 0;
29dd101b 602loop:
e92a04af
BJ
603 for (p = proc; p < procNPROC; p++)
604 if (p->p_pptr == u.u_procp) {
29dd101b 605 f++;
e92a04af 606 if (p->p_stat == SZOMB) {
29dd101b
BJ
607 u.u_r.r_val1 = p->p_pid;
608 u.u_r.r_val2 = ((struct xproc *)p)->xp_xstat;
609 ((struct xproc *)p)->xp_xstat = 0;
610 if (vp)
611 *vp = ((struct xproc *)p)->xp_vm;
612 vmsadd(&u.u_cvm, &((struct xproc *)p)->xp_vm);
613 ((struct xproc *)p)->xp_vm = zvms;
614 p->p_stat = NULL;
615 p->p_pid = 0;
616 p->p_ppid = 0;
bdfe5b0f 617 p->p_pptr = 0;
29dd101b 618 p->p_sig = 0;
bdfe5b0f
BJ
619 p->p_siga0 = 0;
620 p->p_siga1 = 0;
29dd101b
BJ
621 p->p_pgrp = 0;
622 p->p_flag = 0;
623 p->p_wchan = 0;
bdfe5b0f 624 p->p_cursig = 0;
29dd101b
BJ
625 return;
626 }
bdfe5b0f
BJ
627 if (p->p_stat == SSTOP && (p->p_flag&SWTED)==0 &&
628 (p->p_flag&STRC || options&WUNTRACED)) {
629 p->p_flag |= SWTED;
630 u.u_r.r_val1 = p->p_pid;
631 u.u_r.r_val2 = (p->p_cursig<<8) | WSTOPPED;
632 return;
29dd101b
BJ
633 }
634 }
bdfe5b0f
BJ
635 if (f==0) {
636 u.u_error = ECHILD;
637 return;
638 }
639 if (options&WNOHANG) {
640 u.u_r.r_val1 = 0;
641 return;
642 }
29a06346 643 if ((u.u_procp->p_flag&SNUSIG) && setjmp(u.u_qsav)) {
bdfe5b0f
BJ
644 u.u_eosys = RESTARTSYS;
645 return;
29dd101b 646 }
bdfe5b0f
BJ
647 sleep((caddr_t)u.u_procp, PWAIT);
648 goto loop;
29dd101b
BJ
649}
650
651/*
652 * fork system call.
653 */
654fork()
655{
656
657 u.u_cdmap = zdmap;
658 u.u_csmap = zdmap;
659 if (swpexpand(u.u_dsize, u.u_ssize, &u.u_cdmap, &u.u_csmap) == 0) {
660 u.u_r.r_val2 = 0;
661 return;
662 }
663 fork1(0);
664}
665
666fork1(isvfork)
667{
668 register struct proc *p1, *p2;
669 register a;
670
671 a = 0;
672 p2 = NULL;
e92a04af 673 for (p1 = proc; p1 < procNPROC; p1++) {
29dd101b
BJ
674 if (p1->p_stat==NULL && p2==NULL)
675 p2 = p1;
676 else {
677 if (p1->p_uid==u.u_uid && p1->p_stat!=NULL)
678 a++;
679 }
680 }
681 /*
682 * Disallow if
683 * No processes at all;
684 * not su and too many procs owned; or
685 * not su and would take last slot.
686 */
62901f34
BJ
687 if (p2==NULL)
688 tablefull("proc");
86fd527f 689 if (p2==NULL || (u.u_uid!=0 && (p2==procNPROC-1 || a>MAXUPRC))) {
29dd101b
BJ
690 u.u_error = EAGAIN;
691 if (!isvfork) {
81263dba
BJ
692 (void) vsexpand(0, &u.u_cdmap, 1);
693 (void) vsexpand(0, &u.u_csmap, 1);
29dd101b
BJ
694 }
695 goto out;
696 }
697 p1 = u.u_procp;
e92a04af 698 if (newproc(isvfork)) {
29dd101b
BJ
699 u.u_r.r_val1 = p1->p_pid;
700 u.u_r.r_val2 = 1; /* child */
701 u.u_start = time;
702 u.u_acflag = AFORK;
703 return;
704 }
705 u.u_r.r_val1 = p2->p_pid;
706
707out:
708 u.u_r.r_val2 = 0;
709}
710
711/*
712 * break system call.
713 * -- bad planning: "break" is a dirty word in C.
714 */
715sbreak()
716{
717 struct a {
718 char *nsiz;
719 };
720 register int n, d;
721
722 /*
723 * set n to new data size
724 * set d to new-old
725 */
726
727 n = btoc(((struct a *)u.u_ap)->nsiz);
728 if (!u.u_sep)
729 n -= ctos(u.u_tsize) * stoc(1);
730 if (n < 0)
731 n = 0;
732 d = clrnd(n - u.u_dsize);
5290ec92 733 if (ctob(u.u_dsize+d) > u.u_limit[LIM_DATA]) {
054016e1
BJ
734 u.u_error = ENOMEM;
735 return;
736 }
29dd101b
BJ
737 if (chksize(u.u_tsize, u.u_dsize+d, u.u_ssize))
738 return;
739 if (swpexpand(u.u_dsize+d, u.u_ssize, &u.u_dmap, &u.u_smap)==0)
740 return;
741 expand(d, P0BR);
742}