xref: /csrg-svn/sys/kern/kern_proc.c (revision 2938)
1*2938Swnj /*	kern_proc.c	4.9	03/06/81	*/
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"
2236Sbill 
2336Sbill /*
2436Sbill  * exec system call, with and without environments.
2536Sbill  */
2636Sbill struct execa {
2736Sbill 	char	*fname;
2836Sbill 	char	**argp;
2936Sbill 	char	**envp;
3036Sbill };
3136Sbill 
3236Sbill exec()
3336Sbill {
3436Sbill 	((struct execa *)u.u_ap)->envp = NULL;
3536Sbill 	exece();
3636Sbill }
3736Sbill 
3836Sbill exece()
3936Sbill {
4036Sbill 	register nc;
4136Sbill 	register char *cp;
4236Sbill 	register struct buf *bp;
4336Sbill 	register struct execa *uap;
4436Sbill 	int na, ne, ucp, ap, c;
452301Skre 	int indir, uid, gid;
462301Skre 	char *sharg;
4736Sbill 	struct inode *ip;
4836Sbill 	swblk_t bno;
492301Skre 	char cfname[DIRSIZ];
502301Skre 	char cfarg[SHSIZE];
5136Sbill 
5236Sbill 	if ((ip = namei(uchar, 0)) == NULL)
5336Sbill 		return;
542301Skre 
5536Sbill 	bno = 0;
5636Sbill 	bp = 0;
572301Skre 	indir = 0;
582301Skre 	uid = u.u_uid;
592301Skre 	gid = u.u_gid;
602301Skre 
612301Skre 	if (ip->i_mode & ISUID)
622301Skre 		uid = ip->i_uid;
632301Skre 	if (ip->i_mode & ISGID)
642301Skre 		gid = ip->i_gid;
652301Skre 
662301Skre   again:
6736Sbill 	if(access(ip, IEXEC))
6836Sbill 		goto bad;
692330Swnj 	if((u.u_procp->p_flag&STRC) && access(ip, IREAD))
702330Swnj 		goto bad;
7136Sbill 	if((ip->i_mode & IFMT) != IFREG ||
7236Sbill 	   (ip->i_mode & (IEXEC|(IEXEC>>3)|(IEXEC>>6))) == 0) {
7336Sbill 		u.u_error = EACCES;
7436Sbill 		goto bad;
7536Sbill 	}
762301Skre 
7736Sbill 	/*
782301Skre 	 * Read in first few bytes of file for segment sizes, ux_mag:
792301Skre 	 *	407 = plain executable
802301Skre 	 *	410 = RO text
812301Skre 	 *	413 = demand paged RO text
822301Skre 	 * Also an ASCII line beginning with #! is
832301Skre 	 * the file name of a ``shell'' and arguments may be prepended
842301Skre 	 * to the argument list if given here.
852301Skre 	 *
862301Skre 	 * SHELL NAMES ARE LIMITED IN LENGTH.
872301Skre 	 *
882301Skre 	 * ONLY ONE ARGUMENT MAY BE PASSED TO THE SHELL FROM
892301Skre 	 * THE ASCII LINE.
902301Skre 	 */
912301Skre 	u.u_base = (caddr_t)&u.u_exdata;
922301Skre 	u.u_count = sizeof(u.u_exdata);
932301Skre 	u.u_offset = 0;
942301Skre 	u.u_segflg = 1;
952301Skre 	readi(ip);
962301Skre 	u.u_segflg = 0;
972301Skre 	if(u.u_error)
982301Skre 		goto bad;
992301Skre 	if (u.u_count > sizeof(u.u_exdata) - sizeof(u.u_exdata.Ux_A)
1002301Skre 		&& u.u_exdata.ux_shell[0] != '#') {
1012301Skre 		u.u_error = ENOEXEC;
1022301Skre 		goto bad;
1032301Skre 	}
1042301Skre 	switch (u.u_exdata.ux_mag) {
1052301Skre 
1062301Skre 	case 0407:
1072301Skre 		u.u_exdata.ux_dsize += u.u_exdata.ux_tsize;
1082301Skre 		u.u_exdata.ux_tsize = 0;
1092301Skre 		break;
1102301Skre 
1112301Skre 	case 0413:
1122301Skre 	case 0410:
1132301Skre 		if (u.u_exdata.ux_tsize == 0) {
1142301Skre 			u.u_error = ENOEXEC;
1152301Skre 			goto bad;
1162301Skre 		}
1172301Skre 		break;
1182301Skre 
1192301Skre 	default:
1202301Skre 		if (u.u_exdata.ux_shell[0] != '#' ||
1212301Skre 		    u.u_exdata.ux_shell[1] != '!' ||
1222301Skre 		    indir) {
1232301Skre 			u.u_error = ENOEXEC;
1242301Skre 			goto bad;
1252301Skre 		}
1262301Skre 		cp = &u.u_exdata.ux_shell[2];		/* skip "#!" */
1272301Skre 		while (cp < &u.u_exdata.ux_shell[SHSIZE]) {
1282301Skre 			if (*cp == '\t')
1292301Skre 				*cp = ' ';
1302301Skre 			else if (*cp == '\n') {
1312301Skre 				*cp = '\0';
1322301Skre 				break;
1332301Skre 			}
1342301Skre 			cp++;
1352301Skre 		}
1362301Skre 		if (*cp != '\0') {
1372301Skre 			u.u_error = ENOEXEC;
1382301Skre 			goto bad;
1392301Skre 		}
1402301Skre 		cp = &u.u_exdata.ux_shell[2];
1412301Skre 		while (*cp == ' ')
1422301Skre 			cp++;
1432301Skre 		u.u_dirp = cp;
1442301Skre 		while (*cp && *cp != ' ')
1452301Skre 			cp++;
1462301Skre 		sharg = NULL;
1472301Skre 		if (*cp) {
1482301Skre 			*cp++ = '\0';
1492301Skre 			while (*cp == ' ')
1502301Skre 				cp++;
1512301Skre 			if (*cp) {
1522301Skre 				bcopy((caddr_t)cp, (caddr_t)cfarg, SHSIZE);
1532301Skre 				sharg = cfarg;
1542301Skre 			}
1552301Skre 		}
1562301Skre 		bcopy((caddr_t)u.u_dbuf, (caddr_t)cfname, DIRSIZ);
1572301Skre 		indir = 1;
1582301Skre 		iput(ip);
1592301Skre 		ip = namei(schar, 0);
1602301Skre 		if (ip == NULL)
1612301Skre 			return;
1622301Skre 		goto again;
1632301Skre 	}
1642301Skre 
1652301Skre 	/*
16636Sbill 	 * Collect arguments on "file" in swap space.
16736Sbill 	 */
16836Sbill 	na = 0;
16936Sbill 	ne = 0;
17036Sbill 	nc = 0;
17136Sbill 	uap = (struct execa *)u.u_ap;
1722780Swnj 	if ((bno = rmalloc(argmap, ctod(clrnd((int) btoc(NCARGS))))) == 0) {
17336Sbill 		swkill(u.u_procp, "exece");
17436Sbill 		goto bad;
17536Sbill 	}
17636Sbill 	if (bno % CLSIZE)
1772780Swnj 		panic("execa rmalloc");
17836Sbill 	if (uap->argp) for (;;) {
17936Sbill 		ap = NULL;
1802301Skre 		if (na == 1 && indir) {
1812301Skre 			if (sharg == NULL)
1822301Skre 				ap = (int)uap->fname;
1832301Skre 		} else if (na == 2 && indir && sharg != NULL)
1842301Skre 			ap = (int)uap->fname;
1852301Skre 		else if (uap->argp) {
18636Sbill 			ap = fuword((caddr_t)uap->argp);
18736Sbill 			uap->argp++;
18836Sbill 		}
18936Sbill 		if (ap==NULL && uap->envp) {
19036Sbill 			uap->argp = NULL;
19136Sbill 			if ((ap = fuword((caddr_t)uap->envp)) == NULL)
19236Sbill 				break;
19336Sbill 			uap->envp++;
19436Sbill 			ne++;
19536Sbill 		}
19636Sbill 		if (ap==NULL)
19736Sbill 			break;
19836Sbill 		na++;
19936Sbill 		if(ap == -1)
20036Sbill 			u.u_error = EFAULT;
20136Sbill 		do {
20236Sbill 			if (nc >= NCARGS-1)
20336Sbill 				u.u_error = E2BIG;
2042301Skre 			if (indir && na == 2 && sharg != NULL)
2052301Skre 				c = *sharg++ & 0377;
2062301Skre 			else if ((c = fubyte((caddr_t)ap++)) < 0)
20736Sbill 				u.u_error = EFAULT;
20883Sbill 			if (u.u_error) {
20983Sbill 				if (bp)
21083Sbill 					brelse(bp);
21183Sbill 				bp = 0;
21236Sbill 				goto badarg;
21383Sbill 			}
21436Sbill 			if ((nc&BMASK) == 0) {
21536Sbill 				if (bp)
21636Sbill 					bdwrite(bp);
217307Sbill 				bp = getblk(argdev,
218307Sbill 				    (daddr_t)(dbtofsb(bno)+(nc>>BSHIFT)));
21936Sbill 				cp = bp->b_un.b_addr;
22036Sbill 			}
22136Sbill 			nc++;
22236Sbill 			*cp++ = c;
22336Sbill 		} while (c>0);
22436Sbill 	}
22536Sbill 	if (bp)
22636Sbill 		bdwrite(bp);
22736Sbill 	bp = 0;
22836Sbill 	nc = (nc + NBPW-1) & ~(NBPW-1);
2292301Skre 	if (indir)
2302301Skre 		bcopy((caddr_t)cfname, (caddr_t)u.u_dbuf, DIRSIZ);
2312301Skre 	getxfile(ip, nc + (na+4)*NBPW, uid, gid);
232912Sbill 	if (u.u_error) {
23336Sbill badarg:
23436Sbill 		for (c = 0; c < nc; c += BSIZE)
235307Sbill 			if (bp = baddr(argdev, dbtofsb(bno)+(c>>BSHIFT))) {
23636Sbill 				bp->b_flags |= B_AGE;		/* throw away */
23736Sbill 				bp->b_flags &= ~B_DELWRI;	/* cancel io */
23836Sbill 				brelse(bp);
23936Sbill 				bp = 0;
24036Sbill 			}
24136Sbill 		goto bad;
24236Sbill 	}
24336Sbill 
24436Sbill 	/*
24536Sbill 	 * copy back arglist
24636Sbill 	 */
24736Sbill 
24836Sbill 	ucp = USRSTACK - nc - NBPW;
24936Sbill 	ap = ucp - na*NBPW - 3*NBPW;
25036Sbill 	u.u_ar0[SP] = ap;
251132Sbill 	(void) suword((caddr_t)ap, na-ne);
25236Sbill 	nc = 0;
25336Sbill 	for (;;) {
25436Sbill 		ap += NBPW;
25536Sbill 		if (na==ne) {
256132Sbill 			(void) suword((caddr_t)ap, 0);
25736Sbill 			ap += NBPW;
25836Sbill 		}
25936Sbill 		if (--na < 0)
26036Sbill 			break;
261132Sbill 		(void) suword((caddr_t)ap, ucp);
26236Sbill 		do {
26336Sbill 			if ((nc&BMASK) == 0) {
26436Sbill 				if (bp)
26536Sbill 					brelse(bp);
266307Sbill 				bp = bread(argdev,
267307Sbill 				    (daddr_t)(dbtofsb(bno)+(nc>>BSHIFT)));
26836Sbill 				bp->b_flags |= B_AGE;		/* throw away */
26936Sbill 				bp->b_flags &= ~B_DELWRI;	/* cancel io */
27036Sbill 				cp = bp->b_un.b_addr;
27136Sbill 			}
272132Sbill 			(void) subyte((caddr_t)ucp++, (c = *cp++));
27336Sbill 			nc++;
27436Sbill 		} while(c&0377);
27536Sbill 	}
276132Sbill 	(void) suword((caddr_t)ap, 0);
277132Sbill 	(void) suword((caddr_t)ucp, 0);
27836Sbill 	setregs();
27936Sbill bad:
28036Sbill 	if (bp)
28136Sbill 		brelse(bp);
28236Sbill 	if (bno)
2832780Swnj 		rmfree(argmap, ctod(clrnd((int) btoc(NCARGS))), bno);
28436Sbill 	iput(ip);
28536Sbill }
28636Sbill 
28736Sbill /*
28836Sbill  * Read in and set up memory for executed file.
28936Sbill  */
2902301Skre getxfile(ip, nargc, uid, gid)
29136Sbill register struct inode *ip;
29236Sbill {
29336Sbill 	register size_t ts, ds, ss;
2942301Skre 	int pagi;
29536Sbill 
2962301Skre 	if (u.u_exdata.ux_mag == 0413)
29736Sbill 		pagi = SPAGI;
2982301Skre 	else
2992301Skre 		pagi = 0;
30036Sbill 
30136Sbill 	if(u.u_exdata.ux_tsize!=0 && (ip->i_flag&ITEXT)==0 && ip->i_count!=1) {
302890Sbill 		register struct file *fp;
303890Sbill 
3042738Swnj 		for (fp = file; fp < fileNFILE; fp++)
305890Sbill 			if (fp->f_inode == ip && (fp->f_flag&FWRITE)) {
306890Sbill 				u.u_error = ETXTBSY;
307890Sbill 				goto bad;
308890Sbill 			}
30936Sbill 	}
31036Sbill 
31136Sbill 	/*
31236Sbill 	 * find text and data sizes
31336Sbill 	 * try them out for possible
31436Sbill 	 * exceed of max sizes
31536Sbill 	 */
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;
322912Sbill 	u.u_cdmap = zdmap;
323912Sbill 	u.u_csmap = zdmap;
324912Sbill 	if (swpexpand(ds, ss, &u.u_cdmap, &u.u_csmap) == NULL)
325912Sbill 		goto bad;
32636Sbill 
327912Sbill 	/*
328912Sbill 	 * At this point, committed to the new image!
329912Sbill 	 * Release virtual memory resources of old process, and
330912Sbill 	 * initialize the virtual memory of the new process.
331912Sbill 	 * If we resulted from vfork(), instead wakeup our
332912Sbill 	 * parent who will set SVFDONE when he has taken back
333912Sbill 	 * our resources.
334912Sbill 	 */
335912Sbill 	u.u_prof.pr_scale = 0;
336912Sbill 	if ((u.u_procp->p_flag & SVFORK) == 0)
337912Sbill 		vrelvm();
338912Sbill 	else {
339912Sbill 		u.u_procp->p_flag &= ~SVFORK;
340912Sbill 		u.u_procp->p_flag |= SKEEP;
341912Sbill 		wakeup((caddr_t)u.u_procp);
342912Sbill 		while ((u.u_procp->p_flag & SVFDONE) == 0)
343912Sbill 			sleep((caddr_t)u.u_procp, PZERO - 1);
344912Sbill 		u.u_procp->p_flag &= ~(SVFDONE|SKEEP);
345912Sbill 	}
346912Sbill 	u.u_procp->p_flag &= ~(SPAGI|SANOM|SUANOM|SNUSIG);
347912Sbill 	u.u_procp->p_flag |= pagi;
348912Sbill 	u.u_dmap = u.u_cdmap;
349912Sbill 	u.u_smap = u.u_csmap;
350912Sbill 	vgetvm(ts, ds, ss);
351912Sbill 
352912Sbill 	if (pagi == 0) {
35336Sbill 		/*
354912Sbill 		 * Read in data segment.
35536Sbill 		 */
356912Sbill 		u.u_base = (char *)ctob(ts);
357912Sbill 		u.u_offset = sizeof(u.u_exdata)+u.u_exdata.ux_tsize;
358912Sbill 		u.u_count = u.u_exdata.ux_dsize;
359912Sbill 		readi(ip);
360912Sbill 	}
361912Sbill 	xalloc(ip, pagi);
362912Sbill 	if (pagi && u.u_procp->p_textp)
363912Sbill 		vinifod((struct fpte *)dptopte(u.u_procp, 0),
364912Sbill 		    PG_FTEXT, u.u_procp->p_textp->x_iptr,
365912Sbill 		    1 + ts/CLSIZE, (int)btoc(u.u_exdata.ux_dsize));
36636Sbill 
367912Sbill 	/* THIS SHOULD BE DONE AT A LOWER LEVEL, IF AT ALL */
368912Sbill 	mtpr(TBIA, 0);
36936Sbill 
370912Sbill 	/*
371912Sbill 	 * set SUID/SGID protections, if no tracing
372912Sbill 	 */
373912Sbill 	if ((u.u_procp->p_flag&STRC)==0) {
3742301Skre #ifndef	MELB
3752301Skre 		if(u.u_uid != 0)
3762301Skre #endif
3772301Skre 		{
3782301Skre 			u.u_uid = uid;
3792301Skre 			u.u_procp->p_uid = uid;
3802301Skre 		}
3812301Skre 		u.u_gid = gid;
382912Sbill 	} else
383912Sbill 		psignal(u.u_procp, SIGTRAP);
38436Sbill 	u.u_tsize = ts;
38536Sbill 	u.u_dsize = ds;
38636Sbill 	u.u_ssize = ss;
38736Sbill bad:
388912Sbill 	return;
38936Sbill }
39036Sbill 
39136Sbill /*
39236Sbill  * Clear registers on exec
39336Sbill  */
39436Sbill setregs()
39536Sbill {
396173Sbill 	register int (**rp)();
39736Sbill 	register i;
398188Sbill 	long sigmask;
39936Sbill 
400188Sbill 	for(rp = &u.u_signal[0], sigmask = 1L; rp < &u.u_signal[NSIG];
401188Sbill 	    sigmask <<= 1, rp++) {
402188Sbill 		switch (*rp) {
403188Sbill 
404188Sbill 		case SIG_IGN:
405188Sbill 		case SIG_DFL:
406188Sbill 		case SIG_HOLD:
407188Sbill 			continue;
408188Sbill 
409188Sbill 		default:
410188Sbill 			/*
411206Sbill 			 * Normal or deferring catch; revert to default.
412188Sbill 			 */
413206Sbill 			(void) spl6();
414206Sbill 			*rp = SIG_DFL;
415188Sbill 			if ((int)*rp & 1)
416188Sbill 				u.u_procp->p_siga0 |= sigmask;
417188Sbill 			else
418188Sbill 				u.u_procp->p_siga1 &= ~sigmask;
419188Sbill 			if ((int)*rp & 2)
420188Sbill 				u.u_procp->p_siga1 |= sigmask;
421188Sbill 			else
422188Sbill 				u.u_procp->p_siga1 &= ~sigmask;
423206Sbill 			(void) spl0();
424188Sbill 			continue;
425188Sbill 		}
426188Sbill 	}
42736Sbill /*
42836Sbill 	for(rp = &u.u_ar0[0]; rp < &u.u_ar0[16];)
42936Sbill 		*rp++ = 0;
43036Sbill */
43136Sbill 	u.u_ar0[PC] = u.u_exdata.ux_entloc + 2; /* skip over entry mask */
43236Sbill 	for(i=0; i<NOFILE; i++) {
43336Sbill 		if (u.u_pofile[i]&EXCLOSE) {
43436Sbill 			closef(u.u_ofile[i]);
43536Sbill 			u.u_ofile[i] = NULL;
436188Sbill 			u.u_pofile[i] &= ~EXCLOSE;
43736Sbill 		}
43836Sbill 	}
43936Sbill 	/*
44036Sbill 	 * Remember file name for accounting.
44136Sbill 	 */
44236Sbill 	u.u_acflag &= ~AFORK;
44336Sbill 	bcopy((caddr_t)u.u_dbuf, (caddr_t)u.u_comm, DIRSIZ);
44436Sbill }
44536Sbill 
44636Sbill /*
44736Sbill  * exit system call:
44836Sbill  * pass back caller's arg
44936Sbill  */
45036Sbill rexit()
45136Sbill {
45236Sbill 	register struct a {
45336Sbill 		int	rval;
45436Sbill 	} *uap;
45536Sbill 
45636Sbill 	uap = (struct a *)u.u_ap;
45736Sbill 	exit((uap->rval & 0377) << 8);
45836Sbill }
45936Sbill 
46036Sbill /*
46136Sbill  * Release resources.
46236Sbill  * Save u. area for parent to look at.
46336Sbill  * Enter zombie state.
46436Sbill  * Wake up parent and init processes,
46536Sbill  * and dispose of children.
46636Sbill  */
46736Sbill exit(rv)
46836Sbill {
46936Sbill 	register int i;
47036Sbill 	register struct proc *p, *q;
47136Sbill 	register struct file *f;
47236Sbill 	register int x;
47336Sbill 
47436Sbill #ifdef PGINPROF
47536Sbill 	vmsizmon();
47636Sbill #endif
47736Sbill 	p = u.u_procp;
47836Sbill 	p->p_flag &= ~(STRC|SULOCK);
47936Sbill 	p->p_flag |= SWEXIT;
48036Sbill 	p->p_clktim = 0;
481188Sbill 	(void) spl6();
482188Sbill 	if ((int)SIG_IGN & 1)
483188Sbill 		p->p_siga0 = ~0;
484188Sbill 	else
485188Sbill 		p->p_siga0 = 0;
486188Sbill 	if ((int)SIG_IGN & 2)
487188Sbill 		p->p_siga1 = ~0;
488188Sbill 	else
489206Sbill 		p->p_siga1 = 0;
490188Sbill 	(void) spl0();
4911399Sbill 	p->p_cpticks = 0;
4921399Sbill 	p->p_pctcpu = 0;
49336Sbill 	for(i=0; i<NSIG; i++)
494173Sbill 		u.u_signal[i] = SIG_IGN;
49536Sbill 	/*
49636Sbill 	 * Release virtual memory.  If we resulted from
49736Sbill 	 * a vfork(), instead give the resources back to
49836Sbill 	 * the parent.
49936Sbill 	 */
50036Sbill 	if ((p->p_flag & SVFORK) == 0)
50136Sbill 		vrelvm();
50236Sbill 	else {
50336Sbill 		p->p_flag &= ~SVFORK;
50436Sbill 		wakeup((caddr_t)p);
50536Sbill 		while ((p->p_flag & SVFDONE) == 0)
50636Sbill 			sleep((caddr_t)p, PZERO - 1);
50736Sbill 		p->p_flag &= ~SVFDONE;
50836Sbill 	}
50936Sbill 	for(i=0; i<NOFILE; i++) {
51036Sbill 		f = u.u_ofile[i];
51136Sbill 		u.u_ofile[i] = NULL;
51236Sbill 		closef(f);
51336Sbill 	}
51436Sbill 	plock(u.u_cdir);
51536Sbill 	iput(u.u_cdir);
51636Sbill 	if (u.u_rdir) {
51736Sbill 		plock(u.u_rdir);
51836Sbill 		iput(u.u_rdir);
51936Sbill 	}
520363Sbill 	u.u_limit[LIM_FSIZE] = INFINITY;
52136Sbill 	acct();
52236Sbill 	vrelpt(u.u_procp);
52336Sbill 	vrelu(u.u_procp, 0);
52436Sbill 	multprog--;
525307Sbill /*	spl7();			/* clock will get mad because of overlaying */
526931Sbill 	p->p_stat = SZOMB;
527307Sbill 	noproc = 1;
52836Sbill 	i = PIDHASH(p->p_pid);
52936Sbill 	x = p - proc;
53036Sbill 	if (pidhash[i] == x)
53136Sbill 		pidhash[i] = p->p_idhash;
53236Sbill 	else {
53336Sbill 		for (i = pidhash[i]; i != 0; i = proc[i].p_idhash)
53436Sbill 			if (proc[i].p_idhash == x) {
53536Sbill 				proc[i].p_idhash = p->p_idhash;
53636Sbill 				goto done;
53736Sbill 			}
53836Sbill 		panic("exit");
53936Sbill 	}
5401409Sbill 	if (p->p_pid == 1)
5411409Sbill 		panic("init died");
54236Sbill done:
54336Sbill 	((struct xproc *)p)->xp_xstat = rv;		/* overlay */
54436Sbill 	((struct xproc *)p)->xp_vm = u.u_vm;		/* overlay */
54536Sbill 	vmsadd(&((struct xproc *)p)->xp_vm, &u.u_cvm);
5462741Swnj 	for(q = proc; q < procNPROC; q++)
547188Sbill 		if(q->p_pptr == p) {
548188Sbill 			q->p_pptr = &proc[1];
549188Sbill 			q->p_ppid = 1;
55036Sbill 			wakeup((caddr_t)&proc[1]);
551188Sbill 			/*
552212Sbill 			 * Traced processes are killed
553188Sbill 			 * since their existence means someone is screwing up.
554354Sbill 			 * Stopped processes are sent a hangup and a continue.
555212Sbill 			 * This is designed to be ``safe'' for setuid
556212Sbill 			 * processes since they must be willing to tolerate
557212Sbill 			 * hangups anyways.
558188Sbill 			 */
559212Sbill 			if (q->p_flag&STRC) {
560188Sbill 				q->p_flag &= ~STRC;
561188Sbill 				psignal(q, SIGKILL);
562212Sbill 			} else if (q->p_stat == SSTOP) {
563212Sbill 				psignal(q, SIGHUP);
564212Sbill 				psignal(q, SIGCONT);
565188Sbill 			}
566215Sbill 			/*
567215Sbill 			 * Protect this process from future
568354Sbill 			 * tty signals, clear TSTP/TTIN/TTOU if pending,
569354Sbill 			 * and set SDETACH bit on procs.
570215Sbill 			 */
5711788Sbill 			(void) spgrp(q, -1);
57236Sbill 		}
573188Sbill 	wakeup((caddr_t)p->p_pptr);
574188Sbill 	psignal(p->p_pptr, SIGCHLD);
57536Sbill 	swtch();
57636Sbill }
57736Sbill 
57836Sbill wait()
57936Sbill {
580188Sbill 	struct vtimes vm;
581188Sbill 	struct vtimes *vp;
58236Sbill 
583188Sbill 	if ((u.u_ar0[PS] & PSL_ALLCC) != PSL_ALLCC) {
584188Sbill 		wait1(0, (struct vtimes *)0);
585188Sbill 		return;
586188Sbill 	}
587188Sbill 	vp = (struct vtimes *)u.u_ar0[R1];
588188Sbill 	wait1(u.u_ar0[R0], &vm);
589188Sbill 	if (u.u_error)
590188Sbill 		return;
591188Sbill 	(void) copyout((caddr_t)&vm, (caddr_t)vp, sizeof (struct vtimes));
59236Sbill }
59336Sbill 
59436Sbill /*
59536Sbill  * Wait system call.
59636Sbill  * Search for a terminated (zombie) child,
59736Sbill  * finally lay it to rest, and collect its status.
59836Sbill  * Look also for stopped (traced) children,
59936Sbill  * and pass back status from them.
60036Sbill  */
601188Sbill wait1(options, vp)
602188Sbill 	register options;
60336Sbill 	struct vtimes *vp;
60436Sbill {
60536Sbill 	register f;
60636Sbill 	register struct proc *p;
60736Sbill 
60836Sbill 	f = 0;
60936Sbill loop:
6102741Swnj 	for(p = proc; p < procNPROC; p++)
611188Sbill 	if(p->p_pptr == u.u_procp) {
61236Sbill 		f++;
61336Sbill 		if(p->p_stat == SZOMB) {
61436Sbill 			u.u_r.r_val1 = p->p_pid;
61536Sbill 			u.u_r.r_val2 = ((struct xproc *)p)->xp_xstat;
61636Sbill 			((struct xproc *)p)->xp_xstat = 0;
61736Sbill 			if (vp)
61836Sbill 				*vp = ((struct xproc *)p)->xp_vm;
61936Sbill 			vmsadd(&u.u_cvm, &((struct xproc *)p)->xp_vm);
62036Sbill 			((struct xproc *)p)->xp_vm = zvms;
62136Sbill 			p->p_stat = NULL;
62236Sbill 			p->p_pid = 0;
62336Sbill 			p->p_ppid = 0;
624188Sbill 			p->p_pptr = 0;
62536Sbill 			p->p_sig = 0;
626188Sbill 			p->p_siga0 = 0;
627188Sbill 			p->p_siga1 = 0;
62836Sbill 			p->p_pgrp = 0;
62936Sbill 			p->p_flag = 0;
63036Sbill 			p->p_wchan = 0;
631188Sbill 			p->p_cursig = 0;
63236Sbill 			return;
63336Sbill 		}
634188Sbill 		if (p->p_stat == SSTOP && (p->p_flag&SWTED)==0 &&
635188Sbill 		    (p->p_flag&STRC || options&WUNTRACED)) {
636188Sbill 			p->p_flag |= SWTED;
637188Sbill 			u.u_r.r_val1 = p->p_pid;
638188Sbill 			u.u_r.r_val2 = (p->p_cursig<<8) | WSTOPPED;
639188Sbill 			return;
64036Sbill 		}
64136Sbill 	}
642188Sbill 	if (f==0) {
643188Sbill 		u.u_error = ECHILD;
644188Sbill 		return;
64536Sbill 	}
646188Sbill 	if (options&WNOHANG) {
647188Sbill 		u.u_r.r_val1 = 0;
648188Sbill 		return;
649188Sbill 	}
650912Sbill 	if ((u.u_procp->p_flag&SNUSIG) && setjmp(u.u_qsav)) {
651188Sbill 		u.u_eosys = RESTARTSYS;
652188Sbill 		return;
653188Sbill 	}
654188Sbill 	sleep((caddr_t)u.u_procp, PWAIT);
655188Sbill 	goto loop;
65636Sbill }
65736Sbill 
65836Sbill /*
65936Sbill  * fork system call.
66036Sbill  */
66136Sbill fork()
66236Sbill {
66336Sbill 
66436Sbill 	u.u_cdmap = zdmap;
66536Sbill 	u.u_csmap = zdmap;
66636Sbill 	if (swpexpand(u.u_dsize, u.u_ssize, &u.u_cdmap, &u.u_csmap) == 0) {
66736Sbill 		u.u_r.r_val2 = 0;
66836Sbill 		return;
66936Sbill 	}
67036Sbill 	fork1(0);
67136Sbill }
67236Sbill 
67336Sbill fork1(isvfork)
67436Sbill {
67536Sbill 	register struct proc *p1, *p2;
67636Sbill 	register a;
67736Sbill 
67836Sbill 	a = 0;
67936Sbill 	p2 = NULL;
6802741Swnj 	for(p1 = proc; p1 < procNPROC; p1++) {
68136Sbill 		if (p1->p_stat==NULL && p2==NULL)
68236Sbill 			p2 = p1;
68336Sbill 		else {
68436Sbill 			if (p1->p_uid==u.u_uid && p1->p_stat!=NULL)
68536Sbill 				a++;
68636Sbill 		}
68736Sbill 	}
68836Sbill 	/*
68936Sbill 	 * Disallow if
69036Sbill 	 *  No processes at all;
69136Sbill 	 *  not su and too many procs owned; or
69236Sbill 	 *  not su and would take last slot.
69336Sbill 	 */
694*2938Swnj 	if (p2==NULL)
695*2938Swnj 		tablefull("proc");
6962741Swnj 	if (p2==NULL || (u.u_uid!=0 && (p2==procNPROC-1 || a>MAXUPRC))) {
69736Sbill 		u.u_error = EAGAIN;
69836Sbill 		if (!isvfork) {
699132Sbill 			(void) vsexpand(0, &u.u_cdmap, 1);
700132Sbill 			(void) vsexpand(0, &u.u_csmap, 1);
70136Sbill 		}
70236Sbill 		goto out;
70336Sbill 	}
70436Sbill 	p1 = u.u_procp;
70536Sbill 	if(newproc(isvfork)) {
70636Sbill 		u.u_r.r_val1 = p1->p_pid;
70736Sbill 		u.u_r.r_val2 = 1;  /* child */
70836Sbill 		u.u_start = time;
70936Sbill 		u.u_acflag = AFORK;
71036Sbill 		return;
71136Sbill 	}
71236Sbill 	u.u_r.r_val1 = p2->p_pid;
71336Sbill 
71436Sbill out:
71536Sbill 	u.u_r.r_val2 = 0;
71636Sbill }
71736Sbill 
71836Sbill /*
71936Sbill  * break system call.
72036Sbill  *  -- bad planning: "break" is a dirty word in C.
72136Sbill  */
72236Sbill sbreak()
72336Sbill {
72436Sbill 	struct a {
72536Sbill 		char	*nsiz;
72636Sbill 	};
72736Sbill 	register int n, d;
72836Sbill 
72936Sbill 	/*
73036Sbill 	 * set n to new data size
73136Sbill 	 * set d to new-old
73236Sbill 	 */
73336Sbill 
73436Sbill 	n = btoc(((struct a *)u.u_ap)->nsiz);
73536Sbill 	if (!u.u_sep)
73636Sbill 		n -= ctos(u.u_tsize) * stoc(1);
73736Sbill 	if (n < 0)
73836Sbill 		n = 0;
73936Sbill 	d = clrnd(n - u.u_dsize);
740368Sbill 	if (ctob(u.u_dsize+d) > u.u_limit[LIM_DATA]) {
741363Sbill 		u.u_error = ENOMEM;
742363Sbill 		return;
743363Sbill 	}
74436Sbill 	if (chksize(u.u_tsize, u.u_dsize+d, u.u_ssize))
74536Sbill 		return;
74636Sbill 	if (swpexpand(u.u_dsize+d, u.u_ssize, &u.u_dmap, &u.u_smap)==0)
74736Sbill 		return;
74836Sbill 	expand(d, P0BR);
74936Sbill }
750