xref: /csrg-svn/sys/kern/kern_proc.c (revision 7748)
1*7748Sroot /*	kern_proc.c	4.33	82/08/14	*/
236Sbill 
336Sbill #include "../h/param.h"
436Sbill #include "../h/systm.h"
536Sbill #include "../h/map.h"
636Sbill #include "../h/mtpr.h"
736Sbill #include "../h/dir.h"
836Sbill #include "../h/user.h"
936Sbill #include "../h/proc.h"
1036Sbill #include "../h/buf.h"
1136Sbill #include "../h/reg.h"
1236Sbill #include "../h/inode.h"
1336Sbill #include "../h/seg.h"
1436Sbill #include "../h/acct.h"
15215Sbill #include "/usr/include/wait.h"
1636Sbill #include "../h/pte.h"
1736Sbill #include "../h/vm.h"
1836Sbill #include "../h/text.h"
19188Sbill #include "../h/psl.h"
20879Sbill #include "../h/vlimit.h"
21890Sbill #include "../h/file.h"
227488Skre #include "../h/quota.h"
237497Sroot #include "../h/descrip.h"
247713Sroot #include "../h/uio.h"
2536Sbill 
2636Sbill /*
2736Sbill  * exec system call, with and without environments.
2836Sbill  */
2936Sbill struct execa {
3036Sbill 	char	*fname;
3136Sbill 	char	**argp;
3236Sbill 	char	**envp;
3336Sbill };
3436Sbill 
3536Sbill exec()
3636Sbill {
3736Sbill 	((struct execa *)u.u_ap)->envp = NULL;
3836Sbill 	exece();
3936Sbill }
4036Sbill 
4136Sbill exece()
4236Sbill {
4336Sbill 	register nc;
4436Sbill 	register char *cp;
4536Sbill 	register struct buf *bp;
4636Sbill 	register struct execa *uap;
4736Sbill 	int na, ne, ucp, ap, c;
482301Skre 	int indir, uid, gid;
492301Skre 	char *sharg;
5036Sbill 	struct inode *ip;
5136Sbill 	swblk_t bno;
526568Smckusic 	char cfname[MAXNAMLEN + 1];
532301Skre 	char cfarg[SHSIZE];
547713Sroot 	struct uio uio;
557713Sroot 	struct iovec iovec;
56*7748Sroot 	int resid;
5736Sbill 
585991Swnj 	if ((ip = namei(uchar, 0, 1)) == NULL)
5936Sbill 		return;
6036Sbill 	bno = 0;
6136Sbill 	bp = 0;
622301Skre 	indir = 0;
632301Skre 	uid = u.u_uid;
642301Skre 	gid = u.u_gid;
652301Skre 	if (ip->i_mode & ISUID)
662301Skre 		uid = ip->i_uid;
672301Skre 	if (ip->i_mode & ISGID)
682301Skre 		gid = ip->i_gid;
692301Skre 
702301Skre   again:
714827Swnj 	if (access(ip, IEXEC))
7236Sbill 		goto bad;
734827Swnj 	if ((u.u_procp->p_flag&STRC) && access(ip, IREAD))
742330Swnj 		goto bad;
754827Swnj 	if ((ip->i_mode & IFMT) != IFREG ||
7636Sbill 	   (ip->i_mode & (IEXEC|(IEXEC>>3)|(IEXEC>>6))) == 0) {
7736Sbill 		u.u_error = EACCES;
7836Sbill 		goto bad;
7936Sbill 	}
802301Skre 
8136Sbill 	/*
822301Skre 	 * Read in first few bytes of file for segment sizes, ux_mag:
832301Skre 	 *	407 = plain executable
842301Skre 	 *	410 = RO text
852301Skre 	 *	413 = demand paged RO text
862301Skre 	 * Also an ASCII line beginning with #! is
872301Skre 	 * the file name of a ``shell'' and arguments may be prepended
882301Skre 	 * to the argument list if given here.
892301Skre 	 *
902301Skre 	 * SHELL NAMES ARE LIMITED IN LENGTH.
912301Skre 	 *
922301Skre 	 * ONLY ONE ARGUMENT MAY BE PASSED TO THE SHELL FROM
932301Skre 	 * THE ASCII LINE.
942301Skre 	 */
95*7748Sroot 	u.u_error = readip1(ip, (caddr_t)&u.u_exdata, sizeof (u.u_exdata),
96*7748Sroot 	    0, 1, &resid);
974827Swnj 	if (u.u_error)
982301Skre 		goto bad;
99*7748Sroot 	u.u_count = resid;
1004982Swnj 	if (u.u_count > sizeof(u.u_exdata) - sizeof(u.u_exdata.Ux_A) &&
1014982Swnj 	    u.u_exdata.ux_shell[0] != '#') {
1022301Skre 		u.u_error = ENOEXEC;
1032301Skre 		goto bad;
1042301Skre 	}
1052301Skre 	switch (u.u_exdata.ux_mag) {
1062301Skre 
1072301Skre 	case 0407:
1082301Skre 		u.u_exdata.ux_dsize += u.u_exdata.ux_tsize;
1092301Skre 		u.u_exdata.ux_tsize = 0;
1102301Skre 		break;
1112301Skre 
1122301Skre 	case 0413:
1132301Skre 	case 0410:
1142301Skre 		if (u.u_exdata.ux_tsize == 0) {
1152301Skre 			u.u_error = ENOEXEC;
1162301Skre 			goto bad;
1172301Skre 		}
1182301Skre 		break;
1192301Skre 
1202301Skre 	default:
1212301Skre 		if (u.u_exdata.ux_shell[0] != '#' ||
1222301Skre 		    u.u_exdata.ux_shell[1] != '!' ||
1232301Skre 		    indir) {
1242301Skre 			u.u_error = ENOEXEC;
1252301Skre 			goto bad;
1262301Skre 		}
1272301Skre 		cp = &u.u_exdata.ux_shell[2];		/* skip "#!" */
1282301Skre 		while (cp < &u.u_exdata.ux_shell[SHSIZE]) {
1292301Skre 			if (*cp == '\t')
1302301Skre 				*cp = ' ';
1312301Skre 			else if (*cp == '\n') {
1322301Skre 				*cp = '\0';
1332301Skre 				break;
1342301Skre 			}
1352301Skre 			cp++;
1362301Skre 		}
1372301Skre 		if (*cp != '\0') {
1382301Skre 			u.u_error = ENOEXEC;
1392301Skre 			goto bad;
1402301Skre 		}
1412301Skre 		cp = &u.u_exdata.ux_shell[2];
1422301Skre 		while (*cp == ' ')
1432301Skre 			cp++;
1442301Skre 		u.u_dirp = cp;
1452301Skre 		while (*cp && *cp != ' ')
1462301Skre 			cp++;
1472301Skre 		sharg = NULL;
1482301Skre 		if (*cp) {
1492301Skre 			*cp++ = '\0';
1502301Skre 			while (*cp == ' ')
1512301Skre 				cp++;
1522301Skre 			if (*cp) {
1532301Skre 				bcopy((caddr_t)cp, (caddr_t)cfarg, SHSIZE);
1542301Skre 				sharg = cfarg;
1552301Skre 			}
1562301Skre 		}
1576568Smckusic 		bcopy((caddr_t)u.u_dent.d_name, (caddr_t)cfname,
1586568Smckusic 		    u.u_dent.d_namlen + 1);
1592301Skre 		indir = 1;
1602301Skre 		iput(ip);
1615991Swnj 		ip = namei(schar, 0, 1);
1622301Skre 		if (ip == NULL)
1632301Skre 			return;
1642301Skre 		goto again;
1652301Skre 	}
1662301Skre 
1672301Skre 	/*
16836Sbill 	 * Collect arguments on "file" in swap space.
16936Sbill 	 */
17036Sbill 	na = 0;
17136Sbill 	ne = 0;
17236Sbill 	nc = 0;
17336Sbill 	uap = (struct execa *)u.u_ap;
1742780Swnj 	if ((bno = rmalloc(argmap, ctod(clrnd((int) btoc(NCARGS))))) == 0) {
17536Sbill 		swkill(u.u_procp, "exece");
17636Sbill 		goto bad;
17736Sbill 	}
17836Sbill 	if (bno % CLSIZE)
1792780Swnj 		panic("execa rmalloc");
18036Sbill 	if (uap->argp) for (;;) {
18136Sbill 		ap = NULL;
1823621Sroot 		if (indir && (na == 1 || na == 2 && sharg))
1832301Skre 			ap = (int)uap->fname;
1842301Skre 		else if (uap->argp) {
18536Sbill 			ap = fuword((caddr_t)uap->argp);
18636Sbill 			uap->argp++;
18736Sbill 		}
18836Sbill 		if (ap==NULL && uap->envp) {
18936Sbill 			uap->argp = NULL;
19036Sbill 			if ((ap = fuword((caddr_t)uap->envp)) == NULL)
19136Sbill 				break;
19236Sbill 			uap->envp++;
19336Sbill 			ne++;
19436Sbill 		}
1956568Smckusic 		if (ap == NULL)
19636Sbill 			break;
19736Sbill 		na++;
1984827Swnj 		if (ap == -1)
19936Sbill 			u.u_error = EFAULT;
20036Sbill 		do {
20136Sbill 			if (nc >= NCARGS-1)
20236Sbill 				u.u_error = E2BIG;
2032301Skre 			if (indir && na == 2 && sharg != NULL)
2042301Skre 				c = *sharg++ & 0377;
2052301Skre 			else if ((c = fubyte((caddr_t)ap++)) < 0)
20636Sbill 				u.u_error = EFAULT;
20783Sbill 			if (u.u_error) {
20883Sbill 				if (bp)
20983Sbill 					brelse(bp);
21083Sbill 				bp = 0;
21136Sbill 				goto badarg;
21283Sbill 			}
2136568Smckusic 			if (nc % (CLSIZE*NBPG) == 0) {
21436Sbill 				if (bp)
21536Sbill 					bdwrite(bp);
2166568Smckusic 				bp = getblk(argdev, bno + nc / NBPG,
2176568Smckusic 				    CLSIZE*NBPG);
21836Sbill 				cp = bp->b_un.b_addr;
21936Sbill 			}
22036Sbill 			nc++;
22136Sbill 			*cp++ = c;
2226568Smckusic 		} while (c > 0);
22336Sbill 	}
22436Sbill 	if (bp)
22536Sbill 		bdwrite(bp);
22636Sbill 	bp = 0;
22736Sbill 	nc = (nc + NBPW-1) & ~(NBPW-1);
2286568Smckusic 	if (indir) {
2296568Smckusic 		u.u_dent.d_namlen = strlen(cfname);
2306568Smckusic 		bcopy((caddr_t)cfname, (caddr_t)u.u_dent.d_name,
2316568Smckusic 		    u.u_dent.d_namlen + 1);
2326568Smckusic 	}
2332301Skre 	getxfile(ip, nc + (na+4)*NBPW, uid, gid);
234912Sbill 	if (u.u_error) {
23536Sbill badarg:
2366568Smckusic 		for (c = 0; c < nc; c += CLSIZE*NBPG)
2376568Smckusic 			if (bp = baddr(argdev, bno + c / NBPG, CLSIZE*NBPG)) {
23836Sbill 				bp->b_flags |= B_AGE;		/* throw away */
23936Sbill 				bp->b_flags &= ~B_DELWRI;	/* cancel io */
24036Sbill 				brelse(bp);
24136Sbill 				bp = 0;
24236Sbill 			}
24336Sbill 		goto bad;
24436Sbill 	}
24536Sbill 
24636Sbill 	/*
24736Sbill 	 * copy back arglist
24836Sbill 	 */
24936Sbill 	ucp = USRSTACK - nc - NBPW;
25036Sbill 	ap = ucp - na*NBPW - 3*NBPW;
25136Sbill 	u.u_ar0[SP] = ap;
252132Sbill 	(void) suword((caddr_t)ap, na-ne);
25336Sbill 	nc = 0;
25436Sbill 	for (;;) {
25536Sbill 		ap += NBPW;
25636Sbill 		if (na==ne) {
257132Sbill 			(void) suword((caddr_t)ap, 0);
25836Sbill 			ap += NBPW;
25936Sbill 		}
26036Sbill 		if (--na < 0)
26136Sbill 			break;
262132Sbill 		(void) suword((caddr_t)ap, ucp);
26336Sbill 		do {
2646568Smckusic 			if (nc % (CLSIZE*NBPG) == 0) {
26536Sbill 				if (bp)
26636Sbill 					brelse(bp);
2676568Smckusic 				bp = bread(argdev, bno + nc / NBPG,
2686568Smckusic 				    CLSIZE*NBPG);
26936Sbill 				bp->b_flags |= B_AGE;		/* throw away */
27036Sbill 				bp->b_flags &= ~B_DELWRI;	/* cancel io */
27136Sbill 				cp = bp->b_un.b_addr;
27236Sbill 			}
273132Sbill 			(void) subyte((caddr_t)ucp++, (c = *cp++));
27436Sbill 			nc++;
27536Sbill 		} while(c&0377);
27636Sbill 	}
277132Sbill 	(void) suword((caddr_t)ap, 0);
278132Sbill 	(void) suword((caddr_t)ucp, 0);
27936Sbill 	setregs();
28036Sbill bad:
28136Sbill 	if (bp)
28236Sbill 		brelse(bp);
28336Sbill 	if (bno)
2842780Swnj 		rmfree(argmap, ctod(clrnd((int) btoc(NCARGS))), bno);
28536Sbill 	iput(ip);
28636Sbill }
28736Sbill 
28836Sbill /*
28936Sbill  * Read in and set up memory for executed file.
29036Sbill  */
2912301Skre getxfile(ip, nargc, uid, gid)
29236Sbill register struct inode *ip;
29336Sbill {
29436Sbill 	register size_t ts, ds, ss;
2952301Skre 	int pagi;
29636Sbill 
2972301Skre 	if (u.u_exdata.ux_mag == 0413)
29836Sbill 		pagi = SPAGI;
2992301Skre 	else
3002301Skre 		pagi = 0;
3014827Swnj 	if (u.u_exdata.ux_tsize!=0 && (ip->i_flag&ITEXT)==0 &&
3024827Swnj 	    ip->i_count!=1) {
303890Sbill 		register struct file *fp;
304890Sbill 
3054827Swnj 		for (fp = file; fp < fileNFILE; fp++) {
3067497Sroot 			if (fp->f_type == DTYPE_FILE &&
3077497Sroot 			    fp->f_inode == ip && (fp->f_flag&FWRITE)) {
308890Sbill 				u.u_error = ETXTBSY;
309890Sbill 				goto bad;
310890Sbill 			}
3114827Swnj 		}
31236Sbill 	}
31336Sbill 
31436Sbill 	/*
3154827Swnj 	 * Compute text and data sizes and make sure not too large.
31636Sbill 	 */
31736Sbill 	ts = clrnd(btoc(u.u_exdata.ux_tsize));
31836Sbill 	ds = clrnd(btoc((u.u_exdata.ux_dsize+u.u_exdata.ux_bsize)));
31936Sbill 	ss = clrnd(SSIZE + btoc(nargc));
320912Sbill 	if (chksize(ts, ds, ss))
321912Sbill 		goto bad;
3224827Swnj 
3234827Swnj 	/*
3244827Swnj 	 * Make sure enough space to start process.
3254827Swnj 	 */
326912Sbill 	u.u_cdmap = zdmap;
327912Sbill 	u.u_csmap = zdmap;
328912Sbill 	if (swpexpand(ds, ss, &u.u_cdmap, &u.u_csmap) == NULL)
329912Sbill 		goto bad;
33036Sbill 
331912Sbill 	/*
332912Sbill 	 * At this point, committed to the new image!
333912Sbill 	 * Release virtual memory resources of old process, and
334912Sbill 	 * initialize the virtual memory of the new process.
335912Sbill 	 * If we resulted from vfork(), instead wakeup our
336912Sbill 	 * parent who will set SVFDONE when he has taken back
337912Sbill 	 * our resources.
338912Sbill 	 */
339912Sbill 	u.u_prof.pr_scale = 0;
340912Sbill 	if ((u.u_procp->p_flag & SVFORK) == 0)
341912Sbill 		vrelvm();
342912Sbill 	else {
343912Sbill 		u.u_procp->p_flag &= ~SVFORK;
344912Sbill 		u.u_procp->p_flag |= SKEEP;
345912Sbill 		wakeup((caddr_t)u.u_procp);
346912Sbill 		while ((u.u_procp->p_flag & SVFDONE) == 0)
347912Sbill 			sleep((caddr_t)u.u_procp, PZERO - 1);
348912Sbill 		u.u_procp->p_flag &= ~(SVFDONE|SKEEP);
349912Sbill 	}
3503592Swnj 	u.u_procp->p_flag &= ~(SPAGI|SSEQL|SUANOM|SNUSIG);
351912Sbill 	u.u_procp->p_flag |= pagi;
352912Sbill 	u.u_dmap = u.u_cdmap;
353912Sbill 	u.u_smap = u.u_csmap;
354912Sbill 	vgetvm(ts, ds, ss);
355912Sbill 
356*7748Sroot 	if (pagi == 0)
357*7748Sroot 		u.u_error = readip1(ip, (char*)ctob(ts), u.u_exdata.ux_dsize,
358*7748Sroot 		    sizeof(u.u_exdata)+u.u_exdata.ux_tsize, 0, 0);
359912Sbill 	xalloc(ip, pagi);
360912Sbill 	if (pagi && u.u_procp->p_textp)
361912Sbill 		vinifod((struct fpte *)dptopte(u.u_procp, 0),
362912Sbill 		    PG_FTEXT, u.u_procp->p_textp->x_iptr,
363912Sbill 		    1 + ts/CLSIZE, (int)btoc(u.u_exdata.ux_dsize));
36436Sbill 
365912Sbill 	/* THIS SHOULD BE DONE AT A LOWER LEVEL, IF AT ALL */
366912Sbill 	mtpr(TBIA, 0);
36736Sbill 
3687530Sroot 	if (u.u_error)
3697530Sroot 		swkill(u.u_procp, "i/o error mapping pages");
370912Sbill 	/*
371912Sbill 	 * set SUID/SGID protections, if no tracing
372912Sbill 	 */
373912Sbill 	if ((u.u_procp->p_flag&STRC)==0) {
3744827Swnj 		u.u_uid = uid;
3754827Swnj 		u.u_procp->p_uid = uid;
3762301Skre 		u.u_gid = gid;
3775856Swnj 		u.u_grps[gid/(sizeof(int)*8)] |= 1 << (gid%(sizeof(int)*8));
378912Sbill 	} else
379912Sbill 		psignal(u.u_procp, SIGTRAP);
38036Sbill 	u.u_tsize = ts;
38136Sbill 	u.u_dsize = ds;
38236Sbill 	u.u_ssize = ss;
38336Sbill bad:
384912Sbill 	return;
38536Sbill }
38636Sbill 
38736Sbill /*
38836Sbill  * Clear registers on exec
38936Sbill  */
39036Sbill setregs()
39136Sbill {
392173Sbill 	register int (**rp)();
39336Sbill 	register i;
394188Sbill 	long sigmask;
39536Sbill 
3966464Swnj 	for (rp = &u.u_signal[1], sigmask = 1L; rp < &u.u_signal[NSIG];
397188Sbill 	    sigmask <<= 1, rp++) {
398188Sbill 		switch (*rp) {
399188Sbill 
400188Sbill 		case SIG_IGN:
401188Sbill 		case SIG_DFL:
402188Sbill 		case SIG_HOLD:
403188Sbill 			continue;
404188Sbill 
405188Sbill 		default:
406188Sbill 			/*
407206Sbill 			 * Normal or deferring catch; revert to default.
408188Sbill 			 */
409206Sbill 			(void) spl6();
410206Sbill 			*rp = SIG_DFL;
411188Sbill 			if ((int)*rp & 1)
412188Sbill 				u.u_procp->p_siga0 |= sigmask;
413188Sbill 			else
4146327Swnj 				u.u_procp->p_siga0 &= ~sigmask;
415188Sbill 			if ((int)*rp & 2)
416188Sbill 				u.u_procp->p_siga1 |= sigmask;
417188Sbill 			else
418188Sbill 				u.u_procp->p_siga1 &= ~sigmask;
419206Sbill 			(void) spl0();
420188Sbill 			continue;
421188Sbill 		}
422188Sbill 	}
42336Sbill /*
4244827Swnj 	for (rp = &u.u_ar0[0]; rp < &u.u_ar0[16];)
42536Sbill 		*rp++ = 0;
42636Sbill */
42736Sbill 	u.u_ar0[PC] = u.u_exdata.ux_entloc + 2; /* skip over entry mask */
4284827Swnj 	for (i=0; i<NOFILE; i++) {
42936Sbill 		if (u.u_pofile[i]&EXCLOSE) {
4307697Ssam 			closef(u.u_ofile[i], 1, u.u_pofile[i]);
43136Sbill 			u.u_ofile[i] = NULL;
4327697Ssam 			u.u_pofile[i] = 0;
43336Sbill 		}
43436Sbill 	}
4354827Swnj 
43636Sbill 	/*
43736Sbill 	 * Remember file name for accounting.
43836Sbill 	 */
43936Sbill 	u.u_acflag &= ~AFORK;
4406568Smckusic 	bcopy((caddr_t)u.u_dent.d_name, (caddr_t)u.u_comm,
4416568Smckusic 	    u.u_dent.d_namlen + 1);
44236Sbill }
44336Sbill 
44436Sbill /*
4454827Swnj  * Exit system call: pass back caller's arg
44636Sbill  */
44736Sbill rexit()
44836Sbill {
44936Sbill 	register struct a {
45036Sbill 		int	rval;
45136Sbill 	} *uap;
45236Sbill 
45336Sbill 	uap = (struct a *)u.u_ap;
45436Sbill 	exit((uap->rval & 0377) << 8);
45536Sbill }
45636Sbill 
45736Sbill /*
45836Sbill  * Release resources.
45936Sbill  * Save u. area for parent to look at.
46036Sbill  * Enter zombie state.
46136Sbill  * Wake up parent and init processes,
46236Sbill  * and dispose of children.
46336Sbill  */
46436Sbill exit(rv)
46536Sbill {
46636Sbill 	register int i;
46736Sbill 	register struct proc *p, *q;
46836Sbill 	register int x;
46936Sbill 
47036Sbill #ifdef PGINPROF
47136Sbill 	vmsizmon();
47236Sbill #endif
47336Sbill 	p = u.u_procp;
47436Sbill 	p->p_flag &= ~(STRC|SULOCK);
47536Sbill 	p->p_flag |= SWEXIT;
47636Sbill 	p->p_clktim = 0;
477188Sbill 	(void) spl6();
478188Sbill 	if ((int)SIG_IGN & 1)
479188Sbill 		p->p_siga0 = ~0;
480188Sbill 	else
481188Sbill 		p->p_siga0 = 0;
482188Sbill 	if ((int)SIG_IGN & 2)
483188Sbill 		p->p_siga1 = ~0;
484188Sbill 	else
485206Sbill 		p->p_siga1 = 0;
486188Sbill 	(void) spl0();
4871399Sbill 	p->p_cpticks = 0;
4881399Sbill 	p->p_pctcpu = 0;
4894827Swnj 	for (i=0; i<NSIG; i++)
490173Sbill 		u.u_signal[i] = SIG_IGN;
49136Sbill 	/*
49236Sbill 	 * Release virtual memory.  If we resulted from
49336Sbill 	 * a vfork(), instead give the resources back to
49436Sbill 	 * the parent.
49536Sbill 	 */
49636Sbill 	if ((p->p_flag & SVFORK) == 0)
49736Sbill 		vrelvm();
49836Sbill 	else {
49936Sbill 		p->p_flag &= ~SVFORK;
50036Sbill 		wakeup((caddr_t)p);
50136Sbill 		while ((p->p_flag & SVFDONE) == 0)
50236Sbill 			sleep((caddr_t)p, PZERO - 1);
50336Sbill 		p->p_flag &= ~SVFDONE;
50436Sbill 	}
5057697Ssam 	for (i = 0; i < NOFILE; i++) {
5067697Ssam #ifdef notdef
5077697Ssam 		/* why was this like this? */
50836Sbill 		f = u.u_ofile[i];
50936Sbill 		u.u_ofile[i] = NULL;
5105581Swnj 		closef(f, 1);
5117697Ssam #else
5127697Ssam 		closef(u.u_ofile[i], 1, u.u_pofile[i]);
5137697Ssam 		u.u_ofile[i] = NULL;
5147697Ssam 		u.u_pofile[i] = 0;
5157697Ssam #endif
51636Sbill 	}
5174827Swnj 	ilock(u.u_cdir);
51836Sbill 	iput(u.u_cdir);
51936Sbill 	if (u.u_rdir) {
5204827Swnj 		ilock(u.u_rdir);
52136Sbill 		iput(u.u_rdir);
52236Sbill 	}
523363Sbill 	u.u_limit[LIM_FSIZE] = INFINITY;
52436Sbill 	acct();
5257488Skre #ifdef QUOTA
5267488Skre 	qclean();
5277488Skre #endif
52836Sbill 	vrelpt(u.u_procp);
52936Sbill 	vrelu(u.u_procp, 0);
5305630Swnj 	(void) spl5();		/* hack for mem alloc race XXX */
53136Sbill 	multprog--;
532931Sbill 	p->p_stat = SZOMB;
533307Sbill 	noproc = 1;
53436Sbill 	i = PIDHASH(p->p_pid);
53536Sbill 	x = p - proc;
53636Sbill 	if (pidhash[i] == x)
53736Sbill 		pidhash[i] = p->p_idhash;
53836Sbill 	else {
53936Sbill 		for (i = pidhash[i]; i != 0; i = proc[i].p_idhash)
54036Sbill 			if (proc[i].p_idhash == x) {
54136Sbill 				proc[i].p_idhash = p->p_idhash;
54236Sbill 				goto done;
54336Sbill 			}
54436Sbill 		panic("exit");
54536Sbill 	}
5461409Sbill 	if (p->p_pid == 1)
5471409Sbill 		panic("init died");
54836Sbill done:
54936Sbill 	((struct xproc *)p)->xp_xstat = rv;		/* overlay */
55036Sbill 	((struct xproc *)p)->xp_vm = u.u_vm;		/* overlay */
55136Sbill 	vmsadd(&((struct xproc *)p)->xp_vm, &u.u_cvm);
5524827Swnj 	for (q = proc; q < procNPROC; q++)
5534827Swnj 		if (q->p_pptr == p) {
5547488Skre 			if (q->p_osptr)
5557488Skre 				q->p_osptr->p_ysptr = q->p_ysptr;
5567488Skre 			if (q->p_ysptr)
5577488Skre 				q->p_ysptr->p_osptr = q->p_osptr;
5587488Skre 			if (proc[1].p_cptr)
5597488Skre 				proc[1].p_cptr->p_ysptr = q;
5607488Skre 			q->p_osptr = proc[1].p_cptr;
5617488Skre 			q->p_ysptr = NULL;
5627488Skre 			proc[1].p_cptr = q;
5637488Skre 
564188Sbill 			q->p_pptr = &proc[1];
565188Sbill 			q->p_ppid = 1;
56636Sbill 			wakeup((caddr_t)&proc[1]);
567188Sbill 			/*
568212Sbill 			 * Traced processes are killed
569188Sbill 			 * since their existence means someone is screwing up.
570354Sbill 			 * Stopped processes are sent a hangup and a continue.
571212Sbill 			 * This is designed to be ``safe'' for setuid
572212Sbill 			 * processes since they must be willing to tolerate
573212Sbill 			 * hangups anyways.
574188Sbill 			 */
575212Sbill 			if (q->p_flag&STRC) {
576188Sbill 				q->p_flag &= ~STRC;
577188Sbill 				psignal(q, SIGKILL);
578212Sbill 			} else if (q->p_stat == SSTOP) {
579212Sbill 				psignal(q, SIGHUP);
580212Sbill 				psignal(q, SIGCONT);
581188Sbill 			}
582215Sbill 			/*
583215Sbill 			 * Protect this process from future
5845619Swnj 			 * tty signals, clear TSTP/TTIN/TTOU if pending.
585215Sbill 			 */
5861788Sbill 			(void) spgrp(q, -1);
58736Sbill 		}
5886464Swnj 	psignal(p->p_pptr, SIGCHLD);
589188Sbill 	wakeup((caddr_t)p->p_pptr);
59036Sbill 	swtch();
59136Sbill }
59236Sbill 
59336Sbill wait()
59436Sbill {
595188Sbill 	struct vtimes vm;
596188Sbill 	struct vtimes *vp;
59736Sbill 
598188Sbill 	if ((u.u_ar0[PS] & PSL_ALLCC) != PSL_ALLCC) {
599188Sbill 		wait1(0, (struct vtimes *)0);
600188Sbill 		return;
601188Sbill 	}
602188Sbill 	vp = (struct vtimes *)u.u_ar0[R1];
603188Sbill 	wait1(u.u_ar0[R0], &vm);
604188Sbill 	if (u.u_error)
605188Sbill 		return;
606188Sbill 	(void) copyout((caddr_t)&vm, (caddr_t)vp, sizeof (struct vtimes));
60736Sbill }
60836Sbill 
60936Sbill /*
61036Sbill  * Wait system call.
61136Sbill  * Search for a terminated (zombie) child,
61236Sbill  * finally lay it to rest, and collect its status.
61336Sbill  * Look also for stopped (traced) children,
61436Sbill  * and pass back status from them.
61536Sbill  */
616188Sbill wait1(options, vp)
617188Sbill 	register options;
61836Sbill 	struct vtimes *vp;
61936Sbill {
62036Sbill 	register f;
6217488Skre 	register struct proc *p, *q;
62236Sbill 
62336Sbill 	f = 0;
62436Sbill loop:
6254827Swnj 	for (p = proc; p < procNPROC; p++)
6264827Swnj 	if (p->p_pptr == u.u_procp) {
62736Sbill 		f++;
6284827Swnj 		if (p->p_stat == SZOMB) {
62936Sbill 			u.u_r.r_val1 = p->p_pid;
63036Sbill 			u.u_r.r_val2 = ((struct xproc *)p)->xp_xstat;
63136Sbill 			((struct xproc *)p)->xp_xstat = 0;
63236Sbill 			if (vp)
63336Sbill 				*vp = ((struct xproc *)p)->xp_vm;
63436Sbill 			vmsadd(&u.u_cvm, &((struct xproc *)p)->xp_vm);
63536Sbill 			((struct xproc *)p)->xp_vm = zvms;
63636Sbill 			p->p_stat = NULL;
63736Sbill 			p->p_pid = 0;
63836Sbill 			p->p_ppid = 0;
6397488Skre 			if (q = p->p_ysptr)
6407488Skre 				q->p_osptr = p->p_osptr;
6417488Skre 			if (q = p->p_osptr)
6427488Skre 				q->p_ysptr = p->p_ysptr;
6437488Skre 			if ((q = p->p_pptr)->p_cptr == p)
6447488Skre 				q->p_cptr = p->p_osptr;
645188Sbill 			p->p_pptr = 0;
6467488Skre 			p->p_ysptr = 0;
6477488Skre 			p->p_osptr = 0;
6487488Skre 			p->p_cptr = 0;
64936Sbill 			p->p_sig = 0;
650188Sbill 			p->p_siga0 = 0;
651188Sbill 			p->p_siga1 = 0;
65236Sbill 			p->p_pgrp = 0;
65336Sbill 			p->p_flag = 0;
65436Sbill 			p->p_wchan = 0;
655188Sbill 			p->p_cursig = 0;
65636Sbill 			return;
65736Sbill 		}
658188Sbill 		if (p->p_stat == SSTOP && (p->p_flag&SWTED)==0 &&
659188Sbill 		    (p->p_flag&STRC || options&WUNTRACED)) {
660188Sbill 			p->p_flag |= SWTED;
661188Sbill 			u.u_r.r_val1 = p->p_pid;
662188Sbill 			u.u_r.r_val2 = (p->p_cursig<<8) | WSTOPPED;
663188Sbill 			return;
66436Sbill 		}
66536Sbill 	}
666188Sbill 	if (f==0) {
667188Sbill 		u.u_error = ECHILD;
668188Sbill 		return;
66936Sbill 	}
670188Sbill 	if (options&WNOHANG) {
671188Sbill 		u.u_r.r_val1 = 0;
672188Sbill 		return;
673188Sbill 	}
674912Sbill 	if ((u.u_procp->p_flag&SNUSIG) && setjmp(u.u_qsav)) {
675188Sbill 		u.u_eosys = RESTARTSYS;
676188Sbill 		return;
677188Sbill 	}
678188Sbill 	sleep((caddr_t)u.u_procp, PWAIT);
679188Sbill 	goto loop;
68036Sbill }
68136Sbill 
68236Sbill /*
68336Sbill  * fork system call.
68436Sbill  */
68536Sbill fork()
68636Sbill {
68736Sbill 
68836Sbill 	u.u_cdmap = zdmap;
68936Sbill 	u.u_csmap = zdmap;
69036Sbill 	if (swpexpand(u.u_dsize, u.u_ssize, &u.u_cdmap, &u.u_csmap) == 0) {
69136Sbill 		u.u_r.r_val2 = 0;
69236Sbill 		return;
69336Sbill 	}
69436Sbill 	fork1(0);
69536Sbill }
69636Sbill 
69736Sbill fork1(isvfork)
69836Sbill {
69936Sbill 	register struct proc *p1, *p2;
7007488Skre #ifndef	QUOTA
70136Sbill 	register a;
70236Sbill 
70336Sbill 	a = 0;
7047488Skre #else
7057488Skre 	if (u.u_quota != NOQUOT && u.u_quota->q_plim &&
7067488Skre 	    u.u_quota->q_cnt >= u.u_quota->q_plim) {
7077488Skre 		u.u_error = EPROCLIM;
7087488Skre 		return;
7097488Skre 	}
7107488Skre #endif
71136Sbill 	p2 = NULL;
7124827Swnj 	for (p1 = proc; p1 < procNPROC; p1++) {
7137488Skre #ifdef QUOTA
7147488Skre 		if (p1->p_stat == NULL) {
7157488Skre 			p2 = p1;
7167488Skre 			break;
7177488Skre 		}
7187488Skre #else
71936Sbill 		if (p1->p_stat==NULL && p2==NULL)
72036Sbill 			p2 = p1;
72136Sbill 		else {
72236Sbill 			if (p1->p_uid==u.u_uid && p1->p_stat!=NULL)
72336Sbill 				a++;
72436Sbill 		}
7257488Skre #endif
72636Sbill 	}
72736Sbill 	/*
72836Sbill 	 * Disallow if
72936Sbill 	 *  No processes at all;
73036Sbill 	 *  not su and too many procs owned; or
73136Sbill 	 *  not su and would take last slot.
73236Sbill 	 */
7332938Swnj 	if (p2==NULL)
7342938Swnj 		tablefull("proc");
7357488Skre #ifdef QUOTA
7367488Skre 	if (p2==NULL || (u.u_uid!=0 && p2==procNPROC-1)) {
7377488Skre #else
7382741Swnj 	if (p2==NULL || (u.u_uid!=0 && (p2==procNPROC-1 || a>MAXUPRC))) {
7397488Skre #endif
74036Sbill 		u.u_error = EAGAIN;
74136Sbill 		if (!isvfork) {
742132Sbill 			(void) vsexpand(0, &u.u_cdmap, 1);
743132Sbill 			(void) vsexpand(0, &u.u_csmap, 1);
74436Sbill 		}
74536Sbill 		goto out;
74636Sbill 	}
74736Sbill 	p1 = u.u_procp;
7484827Swnj 	if (newproc(isvfork)) {
74936Sbill 		u.u_r.r_val1 = p1->p_pid;
75036Sbill 		u.u_r.r_val2 = 1;  /* child */
75136Sbill 		u.u_start = time;
75236Sbill 		u.u_acflag = AFORK;
7537488Skre #ifdef QUOTA
7547488Skre 		u.u_qflags &= ~QUF_LOGIN;
7557488Skre #endif
75636Sbill 		return;
75736Sbill 	}
75836Sbill 	u.u_r.r_val1 = p2->p_pid;
75936Sbill 
76036Sbill out:
76136Sbill 	u.u_r.r_val2 = 0;
76236Sbill }
76336Sbill 
7647497Sroot spgrp(top, npgrp)
7657497Sroot register struct proc *top;
7667497Sroot {
7677497Sroot 	register struct proc *pp, *p;
7687497Sroot 	int f = 0;
7697497Sroot 
7707497Sroot 	for (p = top; npgrp == -1 || u.u_uid == p->p_uid ||
7717497Sroot 	    !u.u_uid || inferior(p); p = pp) {
7727497Sroot 		if (npgrp == -1) {
7737497Sroot #define	bit(a)	(1<<(a-1))
7747497Sroot 			p->p_sig &= ~(bit(SIGTSTP)|bit(SIGTTIN)|bit(SIGTTOU));
7757497Sroot 		} else
7767497Sroot 			p->p_pgrp = npgrp;
7777497Sroot 		f++;
7787497Sroot 		/*
7797497Sroot 		 * Search for children.
7807497Sroot 		 */
7817497Sroot 		for (pp = proc; pp < procNPROC; pp++)
7827497Sroot 			if (pp->p_pptr == p)
7837497Sroot 				goto cont;
7847497Sroot 		/*
7857497Sroot 		 * Search for siblings.
7867497Sroot 		 */
7877497Sroot 		for (; p != top; p = p->p_pptr)
7887497Sroot 			for (pp = p + 1; pp < procNPROC; pp++)
7897497Sroot 				if (pp->p_pptr == p->p_pptr)
7907497Sroot 					goto cont;
7917497Sroot 		break;
7927497Sroot 	cont:
7937497Sroot 		;
7947497Sroot 	}
7957497Sroot 	return (f);
7967497Sroot }
7977497Sroot 
79836Sbill /*
7997497Sroot  * Is p an inferior of the current process?
80036Sbill  */
8017497Sroot inferior(p)
8027497Sroot register struct proc *p;
80336Sbill {
80436Sbill 
8057497Sroot 	for (; p != u.u_procp; p = p->p_pptr)
8067497Sroot 		if (p->p_ppid == 0)
8077497Sroot 			return (0);
8087497Sroot 	return (1);
80936Sbill }
810