xref: /csrg-svn/sys/kern/kern_proc.c (revision 2330)
1*2330Swnj /*	kern_proc.c	4.5	02/03/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;
69*2330Swnj 	if((u.u_procp->p_flag&STRC) && access(ip, IREAD))
70*2330Swnj 		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;
172307Sbill 	if ((bno = malloc(argmap, ctod(clrnd((int) btoc(NCARGS))))) == 0) {
17336Sbill 		swkill(u.u_procp, "exece");
17436Sbill 		goto bad;
17536Sbill 	}
17636Sbill 	if (bno % CLSIZE)
17736Sbill 		panic("execa malloc");
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)
283307Sbill 		mfree(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 
304890Sbill 		for (fp = file; fp < &file[NFILE]; 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;
3982329Swnj #ifdef UCBIPC
3992329Swnj 	register struct port *pt;
4002329Swnj #endif UCBIPC
401188Sbill 	long sigmask;
40236Sbill 
403188Sbill 	for(rp = &u.u_signal[0], sigmask = 1L; rp < &u.u_signal[NSIG];
404188Sbill 	    sigmask <<= 1, rp++) {
405188Sbill 		switch (*rp) {
406188Sbill 
407188Sbill 		case SIG_IGN:
408188Sbill 		case SIG_DFL:
409188Sbill 		case SIG_HOLD:
410188Sbill 			continue;
411188Sbill 
412188Sbill 		default:
413188Sbill 			/*
414206Sbill 			 * Normal or deferring catch; revert to default.
415188Sbill 			 */
416206Sbill 			(void) spl6();
417206Sbill 			*rp = SIG_DFL;
418188Sbill 			if ((int)*rp & 1)
419188Sbill 				u.u_procp->p_siga0 |= sigmask;
420188Sbill 			else
421188Sbill 				u.u_procp->p_siga1 &= ~sigmask;
422188Sbill 			if ((int)*rp & 2)
423188Sbill 				u.u_procp->p_siga1 |= sigmask;
424188Sbill 			else
425188Sbill 				u.u_procp->p_siga1 &= ~sigmask;
426206Sbill 			(void) spl0();
427188Sbill 			continue;
428188Sbill 		}
429188Sbill 	}
43036Sbill /*
43136Sbill 	for(rp = &u.u_ar0[0]; rp < &u.u_ar0[16];)
43236Sbill 		*rp++ = 0;
43336Sbill */
43436Sbill 	u.u_ar0[PC] = u.u_exdata.ux_entloc + 2; /* skip over entry mask */
43536Sbill 	for(i=0; i<NOFILE; i++) {
43636Sbill 		if (u.u_pofile[i]&EXCLOSE) {
4372329Swnj #ifndef UCBIPC
43836Sbill 			closef(u.u_ofile[i]);
43936Sbill 			u.u_ofile[i] = NULL;
4402329Swnj #else UCBIPC
4412329Swnj 			if (u.u_pofile[i]&ISPORT) {
4422329Swnj 				pt = u.u_oport[i];
4432329Swnj 				if (--pt->pt_count == 0)
4442329Swnj 					ptclose(pt);
4452329Swnj 				u.u_pofile[i] &= ~ISPORT;
4462329Swnj 				u.u_oport[i] = NULL;
4472329Swnj 			} else {
4482329Swnj 				closef(u.u_ofile[i]);
4492329Swnj 				u.u_ofile[i] = NULL;
4502329Swnj 			}
4512329Swnj #endif UCBIPC
452188Sbill 			u.u_pofile[i] &= ~EXCLOSE;
45336Sbill 		}
45436Sbill 	}
45536Sbill 	/*
45636Sbill 	 * Remember file name for accounting.
45736Sbill 	 */
45836Sbill 	u.u_acflag &= ~AFORK;
45936Sbill 	bcopy((caddr_t)u.u_dbuf, (caddr_t)u.u_comm, DIRSIZ);
46036Sbill }
46136Sbill 
46236Sbill /*
46336Sbill  * exit system call:
46436Sbill  * pass back caller's arg
46536Sbill  */
46636Sbill rexit()
46736Sbill {
46836Sbill 	register struct a {
46936Sbill 		int	rval;
47036Sbill 	} *uap;
47136Sbill 
47236Sbill 	uap = (struct a *)u.u_ap;
47336Sbill 	exit((uap->rval & 0377) << 8);
47436Sbill }
47536Sbill 
47636Sbill /*
47736Sbill  * Release resources.
47836Sbill  * Save u. area for parent to look at.
47936Sbill  * Enter zombie state.
48036Sbill  * Wake up parent and init processes,
48136Sbill  * and dispose of children.
48236Sbill  */
48336Sbill exit(rv)
48436Sbill {
48536Sbill 	register int i;
48636Sbill 	register struct proc *p, *q;
48736Sbill 	register struct file *f;
4882329Swnj #ifdef UCBIPC
4892329Swnj 	register struct port *pt;
4902329Swnj #endif UCBIPC
49136Sbill 	register int x;
49236Sbill 
49336Sbill #ifdef PGINPROF
49436Sbill 	vmsizmon();
49536Sbill #endif
49636Sbill 	p = u.u_procp;
49736Sbill 	p->p_flag &= ~(STRC|SULOCK);
49836Sbill 	p->p_flag |= SWEXIT;
49936Sbill 	p->p_clktim = 0;
500188Sbill 	(void) spl6();
501188Sbill 	if ((int)SIG_IGN & 1)
502188Sbill 		p->p_siga0 = ~0;
503188Sbill 	else
504188Sbill 		p->p_siga0 = 0;
505188Sbill 	if ((int)SIG_IGN & 2)
506188Sbill 		p->p_siga1 = ~0;
507188Sbill 	else
508206Sbill 		p->p_siga1 = 0;
509188Sbill 	(void) spl0();
5101399Sbill 	p->p_cpticks = 0;
5111399Sbill 	p->p_pctcpu = 0;
51236Sbill 	for(i=0; i<NSIG; i++)
513173Sbill 		u.u_signal[i] = SIG_IGN;
51436Sbill 	/*
51536Sbill 	 * Release virtual memory.  If we resulted from
51636Sbill 	 * a vfork(), instead give the resources back to
51736Sbill 	 * the parent.
51836Sbill 	 */
51936Sbill 	if ((p->p_flag & SVFORK) == 0)
52036Sbill 		vrelvm();
52136Sbill 	else {
52236Sbill 		p->p_flag &= ~SVFORK;
52336Sbill 		wakeup((caddr_t)p);
52436Sbill 		while ((p->p_flag & SVFDONE) == 0)
52536Sbill 			sleep((caddr_t)p, PZERO - 1);
52636Sbill 		p->p_flag &= ~SVFDONE;
52736Sbill 	}
52836Sbill 	for(i=0; i<NOFILE; i++) {
5292329Swnj #ifndef UCBIPC
53036Sbill 		f = u.u_ofile[i];
53136Sbill 		u.u_ofile[i] = NULL;
53236Sbill 		closef(f);
5332329Swnj #else UCBIPC
5342329Swnj 		if (u.u_pofile[i]&ISPORT) {
5352329Swnj 			pt = u.u_oport[i];
5362329Swnj 			if (--pt->pt_count == 0)
5372329Swnj 				ptclose(pt);
5382329Swnj 			u.u_oport[i] = NULL;
5392329Swnj 		} else {
5402329Swnj 			f = u.u_ofile[i];
5412329Swnj 			u.u_ofile[i] = NULL;
5422329Swnj 			closef(f);
5432329Swnj 		}
5442329Swnj #endif UCBIPC
54536Sbill 	}
54636Sbill 	plock(u.u_cdir);
54736Sbill 	iput(u.u_cdir);
54836Sbill 	if (u.u_rdir) {
54936Sbill 		plock(u.u_rdir);
55036Sbill 		iput(u.u_rdir);
55136Sbill 	}
552363Sbill 	u.u_limit[LIM_FSIZE] = INFINITY;
55336Sbill 	acct();
55436Sbill 	vrelpt(u.u_procp);
55536Sbill 	vrelu(u.u_procp, 0);
55636Sbill 	multprog--;
557307Sbill /*	spl7();			/* clock will get mad because of overlaying */
558931Sbill 	p->p_stat = SZOMB;
559307Sbill 	noproc = 1;
56036Sbill 	i = PIDHASH(p->p_pid);
56136Sbill 	x = p - proc;
56236Sbill 	if (pidhash[i] == x)
56336Sbill 		pidhash[i] = p->p_idhash;
56436Sbill 	else {
56536Sbill 		for (i = pidhash[i]; i != 0; i = proc[i].p_idhash)
56636Sbill 			if (proc[i].p_idhash == x) {
56736Sbill 				proc[i].p_idhash = p->p_idhash;
56836Sbill 				goto done;
56936Sbill 			}
57036Sbill 		panic("exit");
57136Sbill 	}
5721409Sbill 	if (p->p_pid == 1)
5731409Sbill 		panic("init died");
57436Sbill done:
57536Sbill 	((struct xproc *)p)->xp_xstat = rv;		/* overlay */
57636Sbill 	((struct xproc *)p)->xp_vm = u.u_vm;		/* overlay */
57736Sbill 	vmsadd(&((struct xproc *)p)->xp_vm, &u.u_cvm);
57836Sbill 	for(q = &proc[0]; q < &proc[NPROC]; q++)
579188Sbill 		if(q->p_pptr == p) {
580188Sbill 			q->p_pptr = &proc[1];
581188Sbill 			q->p_ppid = 1;
58236Sbill 			wakeup((caddr_t)&proc[1]);
583188Sbill 			/*
584212Sbill 			 * Traced processes are killed
585188Sbill 			 * since their existence means someone is screwing up.
586354Sbill 			 * Stopped processes are sent a hangup and a continue.
587212Sbill 			 * This is designed to be ``safe'' for setuid
588212Sbill 			 * processes since they must be willing to tolerate
589212Sbill 			 * hangups anyways.
590188Sbill 			 */
591212Sbill 			if (q->p_flag&STRC) {
592188Sbill 				q->p_flag &= ~STRC;
593188Sbill 				psignal(q, SIGKILL);
594212Sbill 			} else if (q->p_stat == SSTOP) {
595212Sbill 				psignal(q, SIGHUP);
596212Sbill 				psignal(q, SIGCONT);
597188Sbill 			}
598215Sbill 			/*
599215Sbill 			 * Protect this process from future
600354Sbill 			 * tty signals, clear TSTP/TTIN/TTOU if pending,
601354Sbill 			 * and set SDETACH bit on procs.
602215Sbill 			 */
6031788Sbill 			(void) spgrp(q, -1);
60436Sbill 		}
605188Sbill 	wakeup((caddr_t)p->p_pptr);
606188Sbill 	psignal(p->p_pptr, SIGCHLD);
60736Sbill 	swtch();
60836Sbill }
60936Sbill 
61036Sbill wait()
61136Sbill {
612188Sbill 	struct vtimes vm;
613188Sbill 	struct vtimes *vp;
61436Sbill 
615188Sbill 	if ((u.u_ar0[PS] & PSL_ALLCC) != PSL_ALLCC) {
616188Sbill 		wait1(0, (struct vtimes *)0);
617188Sbill 		return;
618188Sbill 	}
619188Sbill 	vp = (struct vtimes *)u.u_ar0[R1];
620188Sbill 	wait1(u.u_ar0[R0], &vm);
621188Sbill 	if (u.u_error)
622188Sbill 		return;
623188Sbill 	(void) copyout((caddr_t)&vm, (caddr_t)vp, sizeof (struct vtimes));
62436Sbill }
62536Sbill 
62636Sbill /*
62736Sbill  * Wait system call.
62836Sbill  * Search for a terminated (zombie) child,
62936Sbill  * finally lay it to rest, and collect its status.
63036Sbill  * Look also for stopped (traced) children,
63136Sbill  * and pass back status from them.
63236Sbill  */
633188Sbill wait1(options, vp)
634188Sbill 	register options;
63536Sbill 	struct vtimes *vp;
63636Sbill {
63736Sbill 	register f;
63836Sbill 	register struct proc *p;
63936Sbill 
64036Sbill 	f = 0;
64136Sbill loop:
64236Sbill 	for(p = &proc[0]; p < &proc[NPROC]; p++)
643188Sbill 	if(p->p_pptr == u.u_procp) {
64436Sbill 		f++;
64536Sbill 		if(p->p_stat == SZOMB) {
64636Sbill 			u.u_r.r_val1 = p->p_pid;
64736Sbill 			u.u_r.r_val2 = ((struct xproc *)p)->xp_xstat;
64836Sbill 			((struct xproc *)p)->xp_xstat = 0;
64936Sbill 			if (vp)
65036Sbill 				*vp = ((struct xproc *)p)->xp_vm;
65136Sbill 			vmsadd(&u.u_cvm, &((struct xproc *)p)->xp_vm);
65236Sbill 			((struct xproc *)p)->xp_vm = zvms;
65336Sbill 			p->p_stat = NULL;
65436Sbill 			p->p_pid = 0;
65536Sbill 			p->p_ppid = 0;
656188Sbill 			p->p_pptr = 0;
65736Sbill 			p->p_sig = 0;
658188Sbill 			p->p_siga0 = 0;
659188Sbill 			p->p_siga1 = 0;
66036Sbill 			p->p_pgrp = 0;
66136Sbill 			p->p_flag = 0;
66236Sbill 			p->p_wchan = 0;
663188Sbill 			p->p_cursig = 0;
66436Sbill 			return;
66536Sbill 		}
666188Sbill 		if (p->p_stat == SSTOP && (p->p_flag&SWTED)==0 &&
667188Sbill 		    (p->p_flag&STRC || options&WUNTRACED)) {
668188Sbill 			p->p_flag |= SWTED;
669188Sbill 			u.u_r.r_val1 = p->p_pid;
670188Sbill 			u.u_r.r_val2 = (p->p_cursig<<8) | WSTOPPED;
671188Sbill 			return;
67236Sbill 		}
67336Sbill 	}
674188Sbill 	if (f==0) {
675188Sbill 		u.u_error = ECHILD;
676188Sbill 		return;
67736Sbill 	}
678188Sbill 	if (options&WNOHANG) {
679188Sbill 		u.u_r.r_val1 = 0;
680188Sbill 		return;
681188Sbill 	}
682912Sbill 	if ((u.u_procp->p_flag&SNUSIG) && setjmp(u.u_qsav)) {
683188Sbill 		u.u_eosys = RESTARTSYS;
684188Sbill 		return;
685188Sbill 	}
686188Sbill 	sleep((caddr_t)u.u_procp, PWAIT);
687188Sbill 	goto loop;
68836Sbill }
68936Sbill 
69036Sbill /*
69136Sbill  * fork system call.
69236Sbill  */
69336Sbill fork()
69436Sbill {
69536Sbill 
69636Sbill 	u.u_cdmap = zdmap;
69736Sbill 	u.u_csmap = zdmap;
69836Sbill 	if (swpexpand(u.u_dsize, u.u_ssize, &u.u_cdmap, &u.u_csmap) == 0) {
69936Sbill 		u.u_r.r_val2 = 0;
70036Sbill 		return;
70136Sbill 	}
70236Sbill 	fork1(0);
70336Sbill }
70436Sbill 
70536Sbill fork1(isvfork)
70636Sbill {
70736Sbill 	register struct proc *p1, *p2;
70836Sbill 	register a;
70936Sbill 
71036Sbill 	a = 0;
71136Sbill 	p2 = NULL;
71236Sbill 	for(p1 = &proc[0]; p1 < &proc[NPROC]; p1++) {
71336Sbill 		if (p1->p_stat==NULL && p2==NULL)
71436Sbill 			p2 = p1;
71536Sbill 		else {
71636Sbill 			if (p1->p_uid==u.u_uid && p1->p_stat!=NULL)
71736Sbill 				a++;
71836Sbill 		}
71936Sbill 	}
72036Sbill 	/*
72136Sbill 	 * Disallow if
72236Sbill 	 *  No processes at all;
72336Sbill 	 *  not su and too many procs owned; or
72436Sbill 	 *  not su and would take last slot.
72536Sbill 	 */
7262329Swnj 	if (p2==NULL || (u.u_uid!=0 && (p2==&proc[NPROC-1] || a>MAXUPRC))) {
72736Sbill 		u.u_error = EAGAIN;
72836Sbill 		if (!isvfork) {
729132Sbill 			(void) vsexpand(0, &u.u_cdmap, 1);
730132Sbill 			(void) vsexpand(0, &u.u_csmap, 1);
73136Sbill 		}
73236Sbill 		goto out;
73336Sbill 	}
73436Sbill 	p1 = u.u_procp;
73536Sbill 	if(newproc(isvfork)) {
73636Sbill 		u.u_r.r_val1 = p1->p_pid;
73736Sbill 		u.u_r.r_val2 = 1;  /* child */
73836Sbill 		u.u_start = time;
73936Sbill 		u.u_acflag = AFORK;
74036Sbill 		return;
74136Sbill 	}
74236Sbill 	u.u_r.r_val1 = p2->p_pid;
74336Sbill 
74436Sbill out:
74536Sbill 	u.u_r.r_val2 = 0;
74636Sbill }
74736Sbill 
74836Sbill /*
74936Sbill  * break system call.
75036Sbill  *  -- bad planning: "break" is a dirty word in C.
75136Sbill  */
75236Sbill sbreak()
75336Sbill {
75436Sbill 	struct a {
75536Sbill 		char	*nsiz;
75636Sbill 	};
75736Sbill 	register int n, d;
75836Sbill 
75936Sbill 	/*
76036Sbill 	 * set n to new data size
76136Sbill 	 * set d to new-old
76236Sbill 	 */
76336Sbill 
76436Sbill 	n = btoc(((struct a *)u.u_ap)->nsiz);
76536Sbill 	if (!u.u_sep)
76636Sbill 		n -= ctos(u.u_tsize) * stoc(1);
76736Sbill 	if (n < 0)
76836Sbill 		n = 0;
76936Sbill 	d = clrnd(n - u.u_dsize);
770368Sbill 	if (ctob(u.u_dsize+d) > u.u_limit[LIM_DATA]) {
771363Sbill 		u.u_error = ENOMEM;
772363Sbill 		return;
773363Sbill 	}
77436Sbill 	if (chksize(u.u_tsize, u.u_dsize+d, u.u_ssize))
77536Sbill 		return;
77636Sbill 	if (swpexpand(u.u_dsize+d, u.u_ssize, &u.u_dmap, &u.u_smap)==0)
77736Sbill 		return;
77836Sbill 	expand(d, P0BR);
77936Sbill }
780