xref: /csrg-svn/sys/kern/kern_proc.c (revision 3621)
1*3621Sroot /*	kern_proc.c	4.12	81/04/28	*/
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;
180*3621Sroot 		if (indir && (na == 1 || na == 2 && sharg))
1812301Skre 			ap = (int)uap->fname;
1822301Skre 		else if (uap->argp) {
18336Sbill 			ap = fuword((caddr_t)uap->argp);
18436Sbill 			uap->argp++;
18536Sbill 		}
18636Sbill 		if (ap==NULL && uap->envp) {
18736Sbill 			uap->argp = NULL;
18836Sbill 			if ((ap = fuword((caddr_t)uap->envp)) == NULL)
18936Sbill 				break;
19036Sbill 			uap->envp++;
19136Sbill 			ne++;
19236Sbill 		}
19336Sbill 		if (ap==NULL)
19436Sbill 			break;
19536Sbill 		na++;
19636Sbill 		if(ap == -1)
19736Sbill 			u.u_error = EFAULT;
19836Sbill 		do {
19936Sbill 			if (nc >= NCARGS-1)
20036Sbill 				u.u_error = E2BIG;
2012301Skre 			if (indir && na == 2 && sharg != NULL)
2022301Skre 				c = *sharg++ & 0377;
2032301Skre 			else if ((c = fubyte((caddr_t)ap++)) < 0)
20436Sbill 				u.u_error = EFAULT;
20583Sbill 			if (u.u_error) {
20683Sbill 				if (bp)
20783Sbill 					brelse(bp);
20883Sbill 				bp = 0;
20936Sbill 				goto badarg;
21083Sbill 			}
21136Sbill 			if ((nc&BMASK) == 0) {
21236Sbill 				if (bp)
21336Sbill 					bdwrite(bp);
214307Sbill 				bp = getblk(argdev,
215307Sbill 				    (daddr_t)(dbtofsb(bno)+(nc>>BSHIFT)));
21636Sbill 				cp = bp->b_un.b_addr;
21736Sbill 			}
21836Sbill 			nc++;
21936Sbill 			*cp++ = c;
22036Sbill 		} while (c>0);
22136Sbill 	}
22236Sbill 	if (bp)
22336Sbill 		bdwrite(bp);
22436Sbill 	bp = 0;
22536Sbill 	nc = (nc + NBPW-1) & ~(NBPW-1);
2262301Skre 	if (indir)
2272301Skre 		bcopy((caddr_t)cfname, (caddr_t)u.u_dbuf, DIRSIZ);
2282301Skre 	getxfile(ip, nc + (na+4)*NBPW, uid, gid);
229912Sbill 	if (u.u_error) {
23036Sbill badarg:
23136Sbill 		for (c = 0; c < nc; c += BSIZE)
232307Sbill 			if (bp = baddr(argdev, dbtofsb(bno)+(c>>BSHIFT))) {
23336Sbill 				bp->b_flags |= B_AGE;		/* throw away */
23436Sbill 				bp->b_flags &= ~B_DELWRI;	/* cancel io */
23536Sbill 				brelse(bp);
23636Sbill 				bp = 0;
23736Sbill 			}
23836Sbill 		goto bad;
23936Sbill 	}
24036Sbill 
24136Sbill 	/*
24236Sbill 	 * copy back arglist
24336Sbill 	 */
24436Sbill 
24536Sbill 	ucp = USRSTACK - nc - NBPW;
24636Sbill 	ap = ucp - na*NBPW - 3*NBPW;
24736Sbill 	u.u_ar0[SP] = ap;
248132Sbill 	(void) suword((caddr_t)ap, na-ne);
24936Sbill 	nc = 0;
25036Sbill 	for (;;) {
25136Sbill 		ap += NBPW;
25236Sbill 		if (na==ne) {
253132Sbill 			(void) suword((caddr_t)ap, 0);
25436Sbill 			ap += NBPW;
25536Sbill 		}
25636Sbill 		if (--na < 0)
25736Sbill 			break;
258132Sbill 		(void) suword((caddr_t)ap, ucp);
25936Sbill 		do {
26036Sbill 			if ((nc&BMASK) == 0) {
26136Sbill 				if (bp)
26236Sbill 					brelse(bp);
263307Sbill 				bp = bread(argdev,
264307Sbill 				    (daddr_t)(dbtofsb(bno)+(nc>>BSHIFT)));
26536Sbill 				bp->b_flags |= B_AGE;		/* throw away */
26636Sbill 				bp->b_flags &= ~B_DELWRI;	/* cancel io */
26736Sbill 				cp = bp->b_un.b_addr;
26836Sbill 			}
269132Sbill 			(void) subyte((caddr_t)ucp++, (c = *cp++));
27036Sbill 			nc++;
27136Sbill 		} while(c&0377);
27236Sbill 	}
273132Sbill 	(void) suword((caddr_t)ap, 0);
274132Sbill 	(void) suword((caddr_t)ucp, 0);
27536Sbill 	setregs();
27636Sbill bad:
27736Sbill 	if (bp)
27836Sbill 		brelse(bp);
27936Sbill 	if (bno)
2802780Swnj 		rmfree(argmap, ctod(clrnd((int) btoc(NCARGS))), bno);
28136Sbill 	iput(ip);
28236Sbill }
28336Sbill 
28436Sbill /*
28536Sbill  * Read in and set up memory for executed file.
28636Sbill  */
2872301Skre getxfile(ip, nargc, uid, gid)
28836Sbill register struct inode *ip;
28936Sbill {
29036Sbill 	register size_t ts, ds, ss;
2912301Skre 	int pagi;
29236Sbill 
2932301Skre 	if (u.u_exdata.ux_mag == 0413)
29436Sbill 		pagi = SPAGI;
2952301Skre 	else
2962301Skre 		pagi = 0;
29736Sbill 
29836Sbill 	if(u.u_exdata.ux_tsize!=0 && (ip->i_flag&ITEXT)==0 && ip->i_count!=1) {
299890Sbill 		register struct file *fp;
300890Sbill 
3012738Swnj 		for (fp = file; fp < fileNFILE; fp++)
302890Sbill 			if (fp->f_inode == ip && (fp->f_flag&FWRITE)) {
303890Sbill 				u.u_error = ETXTBSY;
304890Sbill 				goto bad;
305890Sbill 			}
30636Sbill 	}
30736Sbill 
30836Sbill 	/*
30936Sbill 	 * find text and data sizes
31036Sbill 	 * try them out for possible
31136Sbill 	 * exceed of max sizes
31236Sbill 	 */
31336Sbill 
31436Sbill 	ts = clrnd(btoc(u.u_exdata.ux_tsize));
31536Sbill 	ds = clrnd(btoc((u.u_exdata.ux_dsize+u.u_exdata.ux_bsize)));
31636Sbill 	ss = clrnd(SSIZE + btoc(nargc));
317912Sbill 	if (chksize(ts, ds, ss))
318912Sbill 		goto bad;
319912Sbill 	u.u_cdmap = zdmap;
320912Sbill 	u.u_csmap = zdmap;
321912Sbill 	if (swpexpand(ds, ss, &u.u_cdmap, &u.u_csmap) == NULL)
322912Sbill 		goto bad;
32336Sbill 
324912Sbill 	/*
325912Sbill 	 * At this point, committed to the new image!
326912Sbill 	 * Release virtual memory resources of old process, and
327912Sbill 	 * initialize the virtual memory of the new process.
328912Sbill 	 * If we resulted from vfork(), instead wakeup our
329912Sbill 	 * parent who will set SVFDONE when he has taken back
330912Sbill 	 * our resources.
331912Sbill 	 */
332912Sbill 	u.u_prof.pr_scale = 0;
333912Sbill 	if ((u.u_procp->p_flag & SVFORK) == 0)
334912Sbill 		vrelvm();
335912Sbill 	else {
336912Sbill 		u.u_procp->p_flag &= ~SVFORK;
337912Sbill 		u.u_procp->p_flag |= SKEEP;
338912Sbill 		wakeup((caddr_t)u.u_procp);
339912Sbill 		while ((u.u_procp->p_flag & SVFDONE) == 0)
340912Sbill 			sleep((caddr_t)u.u_procp, PZERO - 1);
341912Sbill 		u.u_procp->p_flag &= ~(SVFDONE|SKEEP);
342912Sbill 	}
3433592Swnj 	u.u_procp->p_flag &= ~(SPAGI|SSEQL|SUANOM|SNUSIG);
344912Sbill 	u.u_procp->p_flag |= pagi;
345912Sbill 	u.u_dmap = u.u_cdmap;
346912Sbill 	u.u_smap = u.u_csmap;
347912Sbill 	vgetvm(ts, ds, ss);
348912Sbill 
349912Sbill 	if (pagi == 0) {
35036Sbill 		/*
351912Sbill 		 * Read in data segment.
35236Sbill 		 */
353912Sbill 		u.u_base = (char *)ctob(ts);
354912Sbill 		u.u_offset = sizeof(u.u_exdata)+u.u_exdata.ux_tsize;
355912Sbill 		u.u_count = u.u_exdata.ux_dsize;
356912Sbill 		readi(ip);
357912Sbill 	}
358912Sbill 	xalloc(ip, pagi);
359912Sbill 	if (pagi && u.u_procp->p_textp)
360912Sbill 		vinifod((struct fpte *)dptopte(u.u_procp, 0),
361912Sbill 		    PG_FTEXT, u.u_procp->p_textp->x_iptr,
362912Sbill 		    1 + ts/CLSIZE, (int)btoc(u.u_exdata.ux_dsize));
36336Sbill 
364912Sbill 	/* THIS SHOULD BE DONE AT A LOWER LEVEL, IF AT ALL */
365912Sbill 	mtpr(TBIA, 0);
36636Sbill 
367912Sbill 	/*
368912Sbill 	 * set SUID/SGID protections, if no tracing
369912Sbill 	 */
370912Sbill 	if ((u.u_procp->p_flag&STRC)==0) {
3712301Skre #ifndef	MELB
3722301Skre 		if(u.u_uid != 0)
3732301Skre #endif
3742301Skre 		{
3752301Skre 			u.u_uid = uid;
3762301Skre 			u.u_procp->p_uid = uid;
3772301Skre 		}
3782301Skre 		u.u_gid = gid;
379912Sbill 	} else
380912Sbill 		psignal(u.u_procp, SIGTRAP);
38136Sbill 	u.u_tsize = ts;
38236Sbill 	u.u_dsize = ds;
38336Sbill 	u.u_ssize = ss;
38436Sbill bad:
385912Sbill 	return;
38636Sbill }
38736Sbill 
38836Sbill /*
38936Sbill  * Clear registers on exec
39036Sbill  */
39136Sbill setregs()
39236Sbill {
393173Sbill 	register int (**rp)();
39436Sbill 	register i;
395188Sbill 	long sigmask;
39636Sbill 
397188Sbill 	for(rp = &u.u_signal[0], sigmask = 1L; rp < &u.u_signal[NSIG];
398188Sbill 	    sigmask <<= 1, rp++) {
399188Sbill 		switch (*rp) {
400188Sbill 
401188Sbill 		case SIG_IGN:
402188Sbill 		case SIG_DFL:
403188Sbill 		case SIG_HOLD:
404188Sbill 			continue;
405188Sbill 
406188Sbill 		default:
407188Sbill 			/*
408206Sbill 			 * Normal or deferring catch; revert to default.
409188Sbill 			 */
410206Sbill 			(void) spl6();
411206Sbill 			*rp = SIG_DFL;
412188Sbill 			if ((int)*rp & 1)
413188Sbill 				u.u_procp->p_siga0 |= sigmask;
414188Sbill 			else
415188Sbill 				u.u_procp->p_siga1 &= ~sigmask;
416188Sbill 			if ((int)*rp & 2)
417188Sbill 				u.u_procp->p_siga1 |= sigmask;
418188Sbill 			else
419188Sbill 				u.u_procp->p_siga1 &= ~sigmask;
420206Sbill 			(void) spl0();
421188Sbill 			continue;
422188Sbill 		}
423188Sbill 	}
42436Sbill /*
42536Sbill 	for(rp = &u.u_ar0[0]; rp < &u.u_ar0[16];)
42636Sbill 		*rp++ = 0;
42736Sbill */
42836Sbill 	u.u_ar0[PC] = u.u_exdata.ux_entloc + 2; /* skip over entry mask */
42936Sbill 	for(i=0; i<NOFILE; i++) {
43036Sbill 		if (u.u_pofile[i]&EXCLOSE) {
43136Sbill 			closef(u.u_ofile[i]);
43236Sbill 			u.u_ofile[i] = NULL;
433188Sbill 			u.u_pofile[i] &= ~EXCLOSE;
43436Sbill 		}
43536Sbill 	}
43636Sbill 	/*
43736Sbill 	 * Remember file name for accounting.
43836Sbill 	 */
43936Sbill 	u.u_acflag &= ~AFORK;
44036Sbill 	bcopy((caddr_t)u.u_dbuf, (caddr_t)u.u_comm, DIRSIZ);
44136Sbill }
44236Sbill 
44336Sbill /*
44436Sbill  * exit system call:
44536Sbill  * 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 struct file *f;
46936Sbill 	register int x;
47036Sbill 
47136Sbill #ifdef PGINPROF
47236Sbill 	vmsizmon();
47336Sbill #endif
47436Sbill 	p = u.u_procp;
47536Sbill 	p->p_flag &= ~(STRC|SULOCK);
47636Sbill 	p->p_flag |= SWEXIT;
47736Sbill 	p->p_clktim = 0;
478188Sbill 	(void) spl6();
479188Sbill 	if ((int)SIG_IGN & 1)
480188Sbill 		p->p_siga0 = ~0;
481188Sbill 	else
482188Sbill 		p->p_siga0 = 0;
483188Sbill 	if ((int)SIG_IGN & 2)
484188Sbill 		p->p_siga1 = ~0;
485188Sbill 	else
486206Sbill 		p->p_siga1 = 0;
487188Sbill 	(void) spl0();
4881399Sbill 	p->p_cpticks = 0;
4891399Sbill 	p->p_pctcpu = 0;
49036Sbill 	for(i=0; i<NSIG; i++)
491173Sbill 		u.u_signal[i] = SIG_IGN;
49236Sbill 	/*
49336Sbill 	 * Release virtual memory.  If we resulted from
49436Sbill 	 * a vfork(), instead give the resources back to
49536Sbill 	 * the parent.
49636Sbill 	 */
49736Sbill 	if ((p->p_flag & SVFORK) == 0)
49836Sbill 		vrelvm();
49936Sbill 	else {
50036Sbill 		p->p_flag &= ~SVFORK;
50136Sbill 		wakeup((caddr_t)p);
50236Sbill 		while ((p->p_flag & SVFDONE) == 0)
50336Sbill 			sleep((caddr_t)p, PZERO - 1);
50436Sbill 		p->p_flag &= ~SVFDONE;
50536Sbill 	}
50636Sbill 	for(i=0; i<NOFILE; i++) {
50736Sbill 		f = u.u_ofile[i];
50836Sbill 		u.u_ofile[i] = NULL;
50936Sbill 		closef(f);
51036Sbill 	}
51136Sbill 	plock(u.u_cdir);
51236Sbill 	iput(u.u_cdir);
51336Sbill 	if (u.u_rdir) {
51436Sbill 		plock(u.u_rdir);
51536Sbill 		iput(u.u_rdir);
51636Sbill 	}
517363Sbill 	u.u_limit[LIM_FSIZE] = INFINITY;
51836Sbill 	acct();
51936Sbill 	vrelpt(u.u_procp);
52036Sbill 	vrelu(u.u_procp, 0);
52136Sbill 	multprog--;
522307Sbill /*	spl7();			/* clock will get mad because of overlaying */
523931Sbill 	p->p_stat = SZOMB;
524307Sbill 	noproc = 1;
52536Sbill 	i = PIDHASH(p->p_pid);
52636Sbill 	x = p - proc;
52736Sbill 	if (pidhash[i] == x)
52836Sbill 		pidhash[i] = p->p_idhash;
52936Sbill 	else {
53036Sbill 		for (i = pidhash[i]; i != 0; i = proc[i].p_idhash)
53136Sbill 			if (proc[i].p_idhash == x) {
53236Sbill 				proc[i].p_idhash = p->p_idhash;
53336Sbill 				goto done;
53436Sbill 			}
53536Sbill 		panic("exit");
53636Sbill 	}
5371409Sbill 	if (p->p_pid == 1)
5381409Sbill 		panic("init died");
53936Sbill done:
54036Sbill 	((struct xproc *)p)->xp_xstat = rv;		/* overlay */
54136Sbill 	((struct xproc *)p)->xp_vm = u.u_vm;		/* overlay */
54236Sbill 	vmsadd(&((struct xproc *)p)->xp_vm, &u.u_cvm);
5432741Swnj 	for(q = proc; q < procNPROC; q++)
544188Sbill 		if(q->p_pptr == p) {
545188Sbill 			q->p_pptr = &proc[1];
546188Sbill 			q->p_ppid = 1;
54736Sbill 			wakeup((caddr_t)&proc[1]);
548188Sbill 			/*
549212Sbill 			 * Traced processes are killed
550188Sbill 			 * since their existence means someone is screwing up.
551354Sbill 			 * Stopped processes are sent a hangup and a continue.
552212Sbill 			 * This is designed to be ``safe'' for setuid
553212Sbill 			 * processes since they must be willing to tolerate
554212Sbill 			 * hangups anyways.
555188Sbill 			 */
556212Sbill 			if (q->p_flag&STRC) {
557188Sbill 				q->p_flag &= ~STRC;
558188Sbill 				psignal(q, SIGKILL);
559212Sbill 			} else if (q->p_stat == SSTOP) {
560212Sbill 				psignal(q, SIGHUP);
561212Sbill 				psignal(q, SIGCONT);
562188Sbill 			}
563215Sbill 			/*
564215Sbill 			 * Protect this process from future
565354Sbill 			 * tty signals, clear TSTP/TTIN/TTOU if pending,
566354Sbill 			 * and set SDETACH bit on procs.
567215Sbill 			 */
5681788Sbill 			(void) spgrp(q, -1);
56936Sbill 		}
570188Sbill 	wakeup((caddr_t)p->p_pptr);
571188Sbill 	psignal(p->p_pptr, SIGCHLD);
57236Sbill 	swtch();
57336Sbill }
57436Sbill 
57536Sbill wait()
57636Sbill {
577188Sbill 	struct vtimes vm;
578188Sbill 	struct vtimes *vp;
57936Sbill 
580188Sbill 	if ((u.u_ar0[PS] & PSL_ALLCC) != PSL_ALLCC) {
581188Sbill 		wait1(0, (struct vtimes *)0);
582188Sbill 		return;
583188Sbill 	}
584188Sbill 	vp = (struct vtimes *)u.u_ar0[R1];
585188Sbill 	wait1(u.u_ar0[R0], &vm);
586188Sbill 	if (u.u_error)
587188Sbill 		return;
588188Sbill 	(void) copyout((caddr_t)&vm, (caddr_t)vp, sizeof (struct vtimes));
58936Sbill }
59036Sbill 
59136Sbill /*
59236Sbill  * Wait system call.
59336Sbill  * Search for a terminated (zombie) child,
59436Sbill  * finally lay it to rest, and collect its status.
59536Sbill  * Look also for stopped (traced) children,
59636Sbill  * and pass back status from them.
59736Sbill  */
598188Sbill wait1(options, vp)
599188Sbill 	register options;
60036Sbill 	struct vtimes *vp;
60136Sbill {
60236Sbill 	register f;
60336Sbill 	register struct proc *p;
60436Sbill 
60536Sbill 	f = 0;
60636Sbill loop:
6072741Swnj 	for(p = proc; p < procNPROC; p++)
608188Sbill 	if(p->p_pptr == u.u_procp) {
60936Sbill 		f++;
61036Sbill 		if(p->p_stat == SZOMB) {
61136Sbill 			u.u_r.r_val1 = p->p_pid;
61236Sbill 			u.u_r.r_val2 = ((struct xproc *)p)->xp_xstat;
61336Sbill 			((struct xproc *)p)->xp_xstat = 0;
61436Sbill 			if (vp)
61536Sbill 				*vp = ((struct xproc *)p)->xp_vm;
61636Sbill 			vmsadd(&u.u_cvm, &((struct xproc *)p)->xp_vm);
61736Sbill 			((struct xproc *)p)->xp_vm = zvms;
61836Sbill 			p->p_stat = NULL;
61936Sbill 			p->p_pid = 0;
62036Sbill 			p->p_ppid = 0;
621188Sbill 			p->p_pptr = 0;
62236Sbill 			p->p_sig = 0;
623188Sbill 			p->p_siga0 = 0;
624188Sbill 			p->p_siga1 = 0;
62536Sbill 			p->p_pgrp = 0;
62636Sbill 			p->p_flag = 0;
62736Sbill 			p->p_wchan = 0;
628188Sbill 			p->p_cursig = 0;
62936Sbill 			return;
63036Sbill 		}
631188Sbill 		if (p->p_stat == SSTOP && (p->p_flag&SWTED)==0 &&
632188Sbill 		    (p->p_flag&STRC || options&WUNTRACED)) {
633188Sbill 			p->p_flag |= SWTED;
634188Sbill 			u.u_r.r_val1 = p->p_pid;
635188Sbill 			u.u_r.r_val2 = (p->p_cursig<<8) | WSTOPPED;
636188Sbill 			return;
63736Sbill 		}
63836Sbill 	}
639188Sbill 	if (f==0) {
640188Sbill 		u.u_error = ECHILD;
641188Sbill 		return;
64236Sbill 	}
643188Sbill 	if (options&WNOHANG) {
644188Sbill 		u.u_r.r_val1 = 0;
645188Sbill 		return;
646188Sbill 	}
647912Sbill 	if ((u.u_procp->p_flag&SNUSIG) && setjmp(u.u_qsav)) {
648188Sbill 		u.u_eosys = RESTARTSYS;
649188Sbill 		return;
650188Sbill 	}
651188Sbill 	sleep((caddr_t)u.u_procp, PWAIT);
652188Sbill 	goto loop;
65336Sbill }
65436Sbill 
65536Sbill /*
65636Sbill  * fork system call.
65736Sbill  */
65836Sbill fork()
65936Sbill {
66036Sbill 
66136Sbill 	u.u_cdmap = zdmap;
66236Sbill 	u.u_csmap = zdmap;
66336Sbill 	if (swpexpand(u.u_dsize, u.u_ssize, &u.u_cdmap, &u.u_csmap) == 0) {
66436Sbill 		u.u_r.r_val2 = 0;
66536Sbill 		return;
66636Sbill 	}
66736Sbill 	fork1(0);
66836Sbill }
66936Sbill 
67036Sbill fork1(isvfork)
67136Sbill {
67236Sbill 	register struct proc *p1, *p2;
67336Sbill 	register a;
67436Sbill 
67536Sbill 	a = 0;
67636Sbill 	p2 = NULL;
6772741Swnj 	for(p1 = proc; p1 < procNPROC; p1++) {
67836Sbill 		if (p1->p_stat==NULL && p2==NULL)
67936Sbill 			p2 = p1;
68036Sbill 		else {
68136Sbill 			if (p1->p_uid==u.u_uid && p1->p_stat!=NULL)
68236Sbill 				a++;
68336Sbill 		}
68436Sbill 	}
68536Sbill 	/*
68636Sbill 	 * Disallow if
68736Sbill 	 *  No processes at all;
68836Sbill 	 *  not su and too many procs owned; or
68936Sbill 	 *  not su and would take last slot.
69036Sbill 	 */
6912938Swnj 	if (p2==NULL)
6922938Swnj 		tablefull("proc");
6932741Swnj 	if (p2==NULL || (u.u_uid!=0 && (p2==procNPROC-1 || a>MAXUPRC))) {
69436Sbill 		u.u_error = EAGAIN;
69536Sbill 		if (!isvfork) {
696132Sbill 			(void) vsexpand(0, &u.u_cdmap, 1);
697132Sbill 			(void) vsexpand(0, &u.u_csmap, 1);
69836Sbill 		}
69936Sbill 		goto out;
70036Sbill 	}
70136Sbill 	p1 = u.u_procp;
70236Sbill 	if(newproc(isvfork)) {
70336Sbill 		u.u_r.r_val1 = p1->p_pid;
70436Sbill 		u.u_r.r_val2 = 1;  /* child */
70536Sbill 		u.u_start = time;
70636Sbill 		u.u_acflag = AFORK;
70736Sbill 		return;
70836Sbill 	}
70936Sbill 	u.u_r.r_val1 = p2->p_pid;
71036Sbill 
71136Sbill out:
71236Sbill 	u.u_r.r_val2 = 0;
71336Sbill }
71436Sbill 
71536Sbill /*
71636Sbill  * break system call.
71736Sbill  *  -- bad planning: "break" is a dirty word in C.
71836Sbill  */
71936Sbill sbreak()
72036Sbill {
72136Sbill 	struct a {
72236Sbill 		char	*nsiz;
72336Sbill 	};
72436Sbill 	register int n, d;
72536Sbill 
72636Sbill 	/*
72736Sbill 	 * set n to new data size
72836Sbill 	 * set d to new-old
72936Sbill 	 */
73036Sbill 
73136Sbill 	n = btoc(((struct a *)u.u_ap)->nsiz);
73236Sbill 	if (!u.u_sep)
73336Sbill 		n -= ctos(u.u_tsize) * stoc(1);
73436Sbill 	if (n < 0)
73536Sbill 		n = 0;
73636Sbill 	d = clrnd(n - u.u_dsize);
737368Sbill 	if (ctob(u.u_dsize+d) > u.u_limit[LIM_DATA]) {
738363Sbill 		u.u_error = ENOMEM;
739363Sbill 		return;
740363Sbill 	}
74136Sbill 	if (chksize(u.u_tsize, u.u_dsize+d, u.u_ssize))
74236Sbill 		return;
74336Sbill 	if (swpexpand(u.u_dsize+d, u.u_ssize, &u.u_dmap, &u.u_smap)==0)
74436Sbill 		return;
74536Sbill 	expand(d, P0BR);
74636Sbill }
747