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