xref: /csrg-svn/sys/kern/kern_proc.c (revision 2301)
1*2301Skre /*	kern_proc.c	4.3	01/28/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;
45*2301Skre 	int indir, uid, gid;
46*2301Skre 	char *sharg;
4736Sbill 	struct inode *ip;
4836Sbill 	swblk_t bno;
49*2301Skre 	char cfname[DIRSIZ];
50*2301Skre 	char cfarg[SHSIZE];
5136Sbill 
5236Sbill 	if ((ip = namei(uchar, 0)) == NULL)
5336Sbill 		return;
54*2301Skre 
5536Sbill 	bno = 0;
5636Sbill 	bp = 0;
57*2301Skre 	indir = 0;
58*2301Skre 	uid = u.u_uid;
59*2301Skre 	gid = u.u_gid;
60*2301Skre 
61*2301Skre 	if (ip->i_mode & ISUID)
62*2301Skre 		uid = ip->i_uid;
63*2301Skre 	if (ip->i_mode & ISGID)
64*2301Skre 		gid = ip->i_gid;
65*2301Skre 
66*2301Skre   again:
6736Sbill 	if(access(ip, IEXEC))
6836Sbill 		goto bad;
6936Sbill 	if((ip->i_mode & IFMT) != IFREG ||
7036Sbill 	   (ip->i_mode & (IEXEC|(IEXEC>>3)|(IEXEC>>6))) == 0) {
7136Sbill 		u.u_error = EACCES;
7236Sbill 		goto bad;
7336Sbill 	}
74*2301Skre 
7536Sbill 	/*
76*2301Skre 	 * Read in first few bytes of file for segment sizes, ux_mag:
77*2301Skre 	 *	407 = plain executable
78*2301Skre 	 *	410 = RO text
79*2301Skre 	 *	413 = demand paged RO text
80*2301Skre 	 * Also an ASCII line beginning with #! is
81*2301Skre 	 * the file name of a ``shell'' and arguments may be prepended
82*2301Skre 	 * to the argument list if given here.
83*2301Skre 	 *
84*2301Skre 	 * SHELL NAMES ARE LIMITED IN LENGTH.
85*2301Skre 	 *
86*2301Skre 	 * ONLY ONE ARGUMENT MAY BE PASSED TO THE SHELL FROM
87*2301Skre 	 * THE ASCII LINE.
88*2301Skre 	 */
89*2301Skre 	u.u_base = (caddr_t)&u.u_exdata;
90*2301Skre 	u.u_count = sizeof(u.u_exdata);
91*2301Skre 	u.u_offset = 0;
92*2301Skre 	u.u_segflg = 1;
93*2301Skre 	readi(ip);
94*2301Skre 	u.u_segflg = 0;
95*2301Skre 	if(u.u_error)
96*2301Skre 		goto bad;
97*2301Skre 	if (u.u_count > sizeof(u.u_exdata) - sizeof(u.u_exdata.Ux_A)
98*2301Skre 		&& u.u_exdata.ux_shell[0] != '#') {
99*2301Skre 		u.u_error = ENOEXEC;
100*2301Skre 		goto bad;
101*2301Skre 	}
102*2301Skre 	switch (u.u_exdata.ux_mag) {
103*2301Skre 
104*2301Skre 	case 0407:
105*2301Skre 		u.u_exdata.ux_dsize += u.u_exdata.ux_tsize;
106*2301Skre 		u.u_exdata.ux_tsize = 0;
107*2301Skre 		break;
108*2301Skre 
109*2301Skre 	case 0413:
110*2301Skre 	case 0410:
111*2301Skre 		if (u.u_exdata.ux_tsize == 0) {
112*2301Skre 			u.u_error = ENOEXEC;
113*2301Skre 			goto bad;
114*2301Skre 		}
115*2301Skre 		break;
116*2301Skre 
117*2301Skre 	default:
118*2301Skre 		if (u.u_exdata.ux_shell[0] != '#' ||
119*2301Skre 		    u.u_exdata.ux_shell[1] != '!' ||
120*2301Skre 		    indir) {
121*2301Skre 			u.u_error = ENOEXEC;
122*2301Skre 			goto bad;
123*2301Skre 		}
124*2301Skre 		cp = &u.u_exdata.ux_shell[2];		/* skip "#!" */
125*2301Skre 		while (cp < &u.u_exdata.ux_shell[SHSIZE]) {
126*2301Skre 			if (*cp == '\t')
127*2301Skre 				*cp = ' ';
128*2301Skre 			else if (*cp == '\n') {
129*2301Skre 				*cp = '\0';
130*2301Skre 				break;
131*2301Skre 			}
132*2301Skre 			cp++;
133*2301Skre 		}
134*2301Skre 		if (*cp != '\0') {
135*2301Skre 			u.u_error = ENOEXEC;
136*2301Skre 			goto bad;
137*2301Skre 		}
138*2301Skre 		cp = &u.u_exdata.ux_shell[2];
139*2301Skre 		while (*cp == ' ')
140*2301Skre 			cp++;
141*2301Skre 		u.u_dirp = cp;
142*2301Skre 		while (*cp && *cp != ' ')
143*2301Skre 			cp++;
144*2301Skre 		sharg = NULL;
145*2301Skre 		if (*cp) {
146*2301Skre 			*cp++ = '\0';
147*2301Skre 			while (*cp == ' ')
148*2301Skre 				cp++;
149*2301Skre 			if (*cp) {
150*2301Skre 				bcopy((caddr_t)cp, (caddr_t)cfarg, SHSIZE);
151*2301Skre 				sharg = cfarg;
152*2301Skre 			}
153*2301Skre 		}
154*2301Skre 		bcopy((caddr_t)u.u_dbuf, (caddr_t)cfname, DIRSIZ);
155*2301Skre 		indir = 1;
156*2301Skre 		iput(ip);
157*2301Skre 		ip = namei(schar, 0);
158*2301Skre 		if (ip == NULL)
159*2301Skre 			return;
160*2301Skre 		goto again;
161*2301Skre 	}
162*2301Skre 
163*2301Skre 	/*
16436Sbill 	 * Collect arguments on "file" in swap space.
16536Sbill 	 */
16636Sbill 	na = 0;
16736Sbill 	ne = 0;
16836Sbill 	nc = 0;
16936Sbill 	uap = (struct execa *)u.u_ap;
170307Sbill 	if ((bno = malloc(argmap, ctod(clrnd((int) btoc(NCARGS))))) == 0) {
17136Sbill 		swkill(u.u_procp, "exece");
17236Sbill 		goto bad;
17336Sbill 	}
17436Sbill 	if (bno % CLSIZE)
17536Sbill 		panic("execa malloc");
17636Sbill 	if (uap->argp) for (;;) {
17736Sbill 		ap = NULL;
178*2301Skre 		if (na == 1 && indir) {
179*2301Skre 			if (sharg == NULL)
180*2301Skre 				ap = (int)uap->fname;
181*2301Skre 		} else if (na == 2 && indir && sharg != NULL)
182*2301Skre 			ap = (int)uap->fname;
183*2301Skre 		else if (uap->argp) {
18436Sbill 			ap = fuword((caddr_t)uap->argp);
18536Sbill 			uap->argp++;
18636Sbill 		}
18736Sbill 		if (ap==NULL && uap->envp) {
18836Sbill 			uap->argp = NULL;
18936Sbill 			if ((ap = fuword((caddr_t)uap->envp)) == NULL)
19036Sbill 				break;
19136Sbill 			uap->envp++;
19236Sbill 			ne++;
19336Sbill 		}
19436Sbill 		if (ap==NULL)
19536Sbill 			break;
19636Sbill 		na++;
19736Sbill 		if(ap == -1)
19836Sbill 			u.u_error = EFAULT;
19936Sbill 		do {
20036Sbill 			if (nc >= NCARGS-1)
20136Sbill 				u.u_error = E2BIG;
202*2301Skre 			if (indir && na == 2 && sharg != NULL)
203*2301Skre 				c = *sharg++ & 0377;
204*2301Skre 			else if ((c = fubyte((caddr_t)ap++)) < 0)
20536Sbill 				u.u_error = EFAULT;
20683Sbill 			if (u.u_error) {
20783Sbill 				if (bp)
20883Sbill 					brelse(bp);
20983Sbill 				bp = 0;
21036Sbill 				goto badarg;
21183Sbill 			}
21236Sbill 			if ((nc&BMASK) == 0) {
21336Sbill 				if (bp)
21436Sbill 					bdwrite(bp);
215307Sbill 				bp = getblk(argdev,
216307Sbill 				    (daddr_t)(dbtofsb(bno)+(nc>>BSHIFT)));
21736Sbill 				cp = bp->b_un.b_addr;
21836Sbill 			}
21936Sbill 			nc++;
22036Sbill 			*cp++ = c;
22136Sbill 		} while (c>0);
22236Sbill 	}
22336Sbill 	if (bp)
22436Sbill 		bdwrite(bp);
22536Sbill 	bp = 0;
22636Sbill 	nc = (nc + NBPW-1) & ~(NBPW-1);
227*2301Skre 	if (indir)
228*2301Skre 		bcopy((caddr_t)cfname, (caddr_t)u.u_dbuf, DIRSIZ);
229*2301Skre 	getxfile(ip, nc + (na+4)*NBPW, uid, gid);
230912Sbill 	if (u.u_error) {
23136Sbill badarg:
23236Sbill 		for (c = 0; c < nc; c += BSIZE)
233307Sbill 			if (bp = baddr(argdev, dbtofsb(bno)+(c>>BSHIFT))) {
23436Sbill 				bp->b_flags |= B_AGE;		/* throw away */
23536Sbill 				bp->b_flags &= ~B_DELWRI;	/* cancel io */
23636Sbill 				brelse(bp);
23736Sbill 				bp = 0;
23836Sbill 			}
23936Sbill 		goto bad;
24036Sbill 	}
24136Sbill 
24236Sbill 	/*
24336Sbill 	 * copy back arglist
24436Sbill 	 */
24536Sbill 
24636Sbill 	ucp = USRSTACK - nc - NBPW;
24736Sbill 	ap = ucp - na*NBPW - 3*NBPW;
24836Sbill 	u.u_ar0[SP] = ap;
249132Sbill 	(void) suword((caddr_t)ap, na-ne);
25036Sbill 	nc = 0;
25136Sbill 	for (;;) {
25236Sbill 		ap += NBPW;
25336Sbill 		if (na==ne) {
254132Sbill 			(void) suword((caddr_t)ap, 0);
25536Sbill 			ap += NBPW;
25636Sbill 		}
25736Sbill 		if (--na < 0)
25836Sbill 			break;
259132Sbill 		(void) suword((caddr_t)ap, ucp);
26036Sbill 		do {
26136Sbill 			if ((nc&BMASK) == 0) {
26236Sbill 				if (bp)
26336Sbill 					brelse(bp);
264307Sbill 				bp = bread(argdev,
265307Sbill 				    (daddr_t)(dbtofsb(bno)+(nc>>BSHIFT)));
26636Sbill 				bp->b_flags |= B_AGE;		/* throw away */
26736Sbill 				bp->b_flags &= ~B_DELWRI;	/* cancel io */
26836Sbill 				cp = bp->b_un.b_addr;
26936Sbill 			}
270132Sbill 			(void) subyte((caddr_t)ucp++, (c = *cp++));
27136Sbill 			nc++;
27236Sbill 		} while(c&0377);
27336Sbill 	}
274132Sbill 	(void) suword((caddr_t)ap, 0);
275132Sbill 	(void) suword((caddr_t)ucp, 0);
27636Sbill 	setregs();
27736Sbill bad:
27836Sbill 	if (bp)
27936Sbill 		brelse(bp);
28036Sbill 	if (bno)
281307Sbill 		mfree(argmap, ctod(clrnd((int) btoc(NCARGS))), bno);
28236Sbill 	iput(ip);
28336Sbill }
28436Sbill 
28536Sbill /*
28636Sbill  * Read in and set up memory for executed file.
28736Sbill  */
288*2301Skre getxfile(ip, nargc, uid, gid)
28936Sbill register struct inode *ip;
29036Sbill {
29136Sbill 	register size_t ts, ds, ss;
292*2301Skre 	int pagi;
29336Sbill 
294*2301Skre 	if (u.u_exdata.ux_mag == 0413)
29536Sbill 		pagi = SPAGI;
296*2301Skre 	else
297*2301Skre 		pagi = 0;
29836Sbill 
29936Sbill 	if(u.u_exdata.ux_tsize!=0 && (ip->i_flag&ITEXT)==0 && ip->i_count!=1) {
300890Sbill 		register struct file *fp;
301890Sbill 
302890Sbill 		for (fp = file; fp < &file[NFILE]; fp++)
303890Sbill 			if (fp->f_inode == ip && (fp->f_flag&FWRITE)) {
304890Sbill 				u.u_error = ETXTBSY;
305890Sbill 				goto bad;
306890Sbill 			}
30736Sbill 	}
30836Sbill 
30936Sbill 	/*
31036Sbill 	 * find text and data sizes
31136Sbill 	 * try them out for possible
31236Sbill 	 * exceed of max sizes
31336Sbill 	 */
31436Sbill 
31536Sbill 	ts = clrnd(btoc(u.u_exdata.ux_tsize));
31636Sbill 	ds = clrnd(btoc((u.u_exdata.ux_dsize+u.u_exdata.ux_bsize)));
31736Sbill 	ss = clrnd(SSIZE + btoc(nargc));
318912Sbill 	if (chksize(ts, ds, ss))
319912Sbill 		goto bad;
320912Sbill 	u.u_cdmap = zdmap;
321912Sbill 	u.u_csmap = zdmap;
322912Sbill 	if (swpexpand(ds, ss, &u.u_cdmap, &u.u_csmap) == NULL)
323912Sbill 		goto bad;
32436Sbill 
325912Sbill 	/*
326912Sbill 	 * At this point, committed to the new image!
327912Sbill 	 * Release virtual memory resources of old process, and
328912Sbill 	 * initialize the virtual memory of the new process.
329912Sbill 	 * If we resulted from vfork(), instead wakeup our
330912Sbill 	 * parent who will set SVFDONE when he has taken back
331912Sbill 	 * our resources.
332912Sbill 	 */
333912Sbill 	u.u_prof.pr_scale = 0;
334912Sbill 	if ((u.u_procp->p_flag & SVFORK) == 0)
335912Sbill 		vrelvm();
336912Sbill 	else {
337912Sbill 		u.u_procp->p_flag &= ~SVFORK;
338912Sbill 		u.u_procp->p_flag |= SKEEP;
339912Sbill 		wakeup((caddr_t)u.u_procp);
340912Sbill 		while ((u.u_procp->p_flag & SVFDONE) == 0)
341912Sbill 			sleep((caddr_t)u.u_procp, PZERO - 1);
342912Sbill 		u.u_procp->p_flag &= ~(SVFDONE|SKEEP);
343912Sbill 	}
344912Sbill 	u.u_procp->p_flag &= ~(SPAGI|SANOM|SUANOM|SNUSIG);
345912Sbill 	u.u_procp->p_flag |= pagi;
346912Sbill 	u.u_dmap = u.u_cdmap;
347912Sbill 	u.u_smap = u.u_csmap;
348912Sbill 	vgetvm(ts, ds, ss);
349912Sbill 
350912Sbill 	if (pagi == 0) {
35136Sbill 		/*
352912Sbill 		 * Read in data segment.
35336Sbill 		 */
354912Sbill 		u.u_base = (char *)ctob(ts);
355912Sbill 		u.u_offset = sizeof(u.u_exdata)+u.u_exdata.ux_tsize;
356912Sbill 		u.u_count = u.u_exdata.ux_dsize;
357912Sbill 		readi(ip);
358912Sbill 	}
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 
368912Sbill 	/*
369912Sbill 	 * set SUID/SGID protections, if no tracing
370912Sbill 	 */
371912Sbill 	if ((u.u_procp->p_flag&STRC)==0) {
372*2301Skre #ifndef	MELB
373*2301Skre 		if(u.u_uid != 0)
374*2301Skre #endif
375*2301Skre 		{
376*2301Skre 			u.u_uid = uid;
377*2301Skre 			u.u_procp->p_uid = uid;
378*2301Skre 		}
379*2301Skre 		u.u_gid = gid;
380912Sbill 	} else
381912Sbill 		psignal(u.u_procp, SIGTRAP);
38236Sbill 	u.u_tsize = ts;
38336Sbill 	u.u_dsize = ds;
38436Sbill 	u.u_ssize = ss;
38536Sbill bad:
386912Sbill 	return;
38736Sbill }
38836Sbill 
38936Sbill /*
39036Sbill  * Clear registers on exec
39136Sbill  */
39236Sbill setregs()
39336Sbill {
394173Sbill 	register int (**rp)();
39536Sbill 	register i;
396188Sbill 	long sigmask;
39736Sbill 
398188Sbill 	for(rp = &u.u_signal[0], sigmask = 1L; rp < &u.u_signal[NSIG];
399188Sbill 	    sigmask <<= 1, rp++) {
400188Sbill 		switch (*rp) {
401188Sbill 
402188Sbill 		case SIG_IGN:
403188Sbill 		case SIG_DFL:
404188Sbill 		case SIG_HOLD:
405188Sbill 			continue;
406188Sbill 
407188Sbill 		default:
408188Sbill 			/*
409206Sbill 			 * Normal or deferring catch; revert to default.
410188Sbill 			 */
411206Sbill 			(void) spl6();
412206Sbill 			*rp = SIG_DFL;
413188Sbill 			if ((int)*rp & 1)
414188Sbill 				u.u_procp->p_siga0 |= sigmask;
415188Sbill 			else
416188Sbill 				u.u_procp->p_siga1 &= ~sigmask;
417188Sbill 			if ((int)*rp & 2)
418188Sbill 				u.u_procp->p_siga1 |= sigmask;
419188Sbill 			else
420188Sbill 				u.u_procp->p_siga1 &= ~sigmask;
421206Sbill 			(void) spl0();
422188Sbill 			continue;
423188Sbill 		}
424188Sbill 	}
42536Sbill /*
42636Sbill 	for(rp = &u.u_ar0[0]; rp < &u.u_ar0[16];)
42736Sbill 		*rp++ = 0;
42836Sbill */
42936Sbill 	u.u_ar0[PC] = u.u_exdata.ux_entloc + 2; /* skip over entry mask */
43036Sbill 	for(i=0; i<NOFILE; i++) {
43136Sbill 		if (u.u_pofile[i]&EXCLOSE) {
43236Sbill 			closef(u.u_ofile[i]);
43336Sbill 			u.u_ofile[i] = NULL;
434188Sbill 			u.u_pofile[i] &= ~EXCLOSE;
43536Sbill 		}
43636Sbill 	}
43736Sbill 	/*
43836Sbill 	 * Remember file name for accounting.
43936Sbill 	 */
44036Sbill 	u.u_acflag &= ~AFORK;
44136Sbill 	bcopy((caddr_t)u.u_dbuf, (caddr_t)u.u_comm, DIRSIZ);
44236Sbill }
44336Sbill 
44436Sbill /*
44536Sbill  * exit system call:
44636Sbill  * pass back caller's arg
44736Sbill  */
44836Sbill rexit()
44936Sbill {
45036Sbill 	register struct a {
45136Sbill 		int	rval;
45236Sbill 	} *uap;
45336Sbill 
45436Sbill 	uap = (struct a *)u.u_ap;
45536Sbill 	exit((uap->rval & 0377) << 8);
45636Sbill }
45736Sbill 
45836Sbill /*
45936Sbill  * Release resources.
46036Sbill  * Save u. area for parent to look at.
46136Sbill  * Enter zombie state.
46236Sbill  * Wake up parent and init processes,
46336Sbill  * and dispose of children.
46436Sbill  */
46536Sbill exit(rv)
46636Sbill {
46736Sbill 	register int i;
46836Sbill 	register struct proc *p, *q;
46936Sbill 	register struct file *f;
47036Sbill 	register int x;
47136Sbill 
47236Sbill #ifdef PGINPROF
47336Sbill 	vmsizmon();
47436Sbill #endif
47536Sbill 	p = u.u_procp;
47636Sbill 	p->p_flag &= ~(STRC|SULOCK);
47736Sbill 	p->p_flag |= SWEXIT;
47836Sbill 	p->p_clktim = 0;
479188Sbill 	(void) spl6();
480188Sbill 	if ((int)SIG_IGN & 1)
481188Sbill 		p->p_siga0 = ~0;
482188Sbill 	else
483188Sbill 		p->p_siga0 = 0;
484188Sbill 	if ((int)SIG_IGN & 2)
485188Sbill 		p->p_siga1 = ~0;
486188Sbill 	else
487206Sbill 		p->p_siga1 = 0;
488188Sbill 	(void) spl0();
4891399Sbill 	p->p_cpticks = 0;
4901399Sbill 	p->p_pctcpu = 0;
49136Sbill 	for(i=0; i<NSIG; i++)
492173Sbill 		u.u_signal[i] = SIG_IGN;
49336Sbill 	/*
49436Sbill 	 * Release virtual memory.  If we resulted from
49536Sbill 	 * a vfork(), instead give the resources back to
49636Sbill 	 * the parent.
49736Sbill 	 */
49836Sbill 	if ((p->p_flag & SVFORK) == 0)
49936Sbill 		vrelvm();
50036Sbill 	else {
50136Sbill 		p->p_flag &= ~SVFORK;
50236Sbill 		wakeup((caddr_t)p);
50336Sbill 		while ((p->p_flag & SVFDONE) == 0)
50436Sbill 			sleep((caddr_t)p, PZERO - 1);
50536Sbill 		p->p_flag &= ~SVFDONE;
50636Sbill 	}
50736Sbill 	for(i=0; i<NOFILE; i++) {
50836Sbill 		f = u.u_ofile[i];
50936Sbill 		u.u_ofile[i] = NULL;
51036Sbill 		closef(f);
51136Sbill 	}
51236Sbill 	plock(u.u_cdir);
51336Sbill 	iput(u.u_cdir);
51436Sbill 	if (u.u_rdir) {
51536Sbill 		plock(u.u_rdir);
51636Sbill 		iput(u.u_rdir);
51736Sbill 	}
518363Sbill 	u.u_limit[LIM_FSIZE] = INFINITY;
51936Sbill 	acct();
52036Sbill 	vrelpt(u.u_procp);
52136Sbill 	vrelu(u.u_procp, 0);
52236Sbill 	multprog--;
523307Sbill /*	spl7();			/* clock will get mad because of overlaying */
524931Sbill 	p->p_stat = SZOMB;
525307Sbill 	noproc = 1;
52636Sbill 	i = PIDHASH(p->p_pid);
52736Sbill 	x = p - proc;
52836Sbill 	if (pidhash[i] == x)
52936Sbill 		pidhash[i] = p->p_idhash;
53036Sbill 	else {
53136Sbill 		for (i = pidhash[i]; i != 0; i = proc[i].p_idhash)
53236Sbill 			if (proc[i].p_idhash == x) {
53336Sbill 				proc[i].p_idhash = p->p_idhash;
53436Sbill 				goto done;
53536Sbill 			}
53636Sbill 		panic("exit");
53736Sbill 	}
5381409Sbill 	if (p->p_pid == 1)
5391409Sbill 		panic("init died");
54036Sbill done:
54136Sbill 	((struct xproc *)p)->xp_xstat = rv;		/* overlay */
54236Sbill 	((struct xproc *)p)->xp_vm = u.u_vm;		/* overlay */
54336Sbill 	vmsadd(&((struct xproc *)p)->xp_vm, &u.u_cvm);
54436Sbill 	for(q = &proc[0]; q < &proc[NPROC]; q++)
545188Sbill 		if(q->p_pptr == p) {
546188Sbill 			q->p_pptr = &proc[1];
547188Sbill 			q->p_ppid = 1;
54836Sbill 			wakeup((caddr_t)&proc[1]);
549188Sbill 			/*
550212Sbill 			 * Traced processes are killed
551188Sbill 			 * since their existence means someone is screwing up.
552354Sbill 			 * Stopped processes are sent a hangup and a continue.
553212Sbill 			 * This is designed to be ``safe'' for setuid
554212Sbill 			 * processes since they must be willing to tolerate
555212Sbill 			 * hangups anyways.
556188Sbill 			 */
557212Sbill 			if (q->p_flag&STRC) {
558188Sbill 				q->p_flag &= ~STRC;
559188Sbill 				psignal(q, SIGKILL);
560212Sbill 			} else if (q->p_stat == SSTOP) {
561212Sbill 				psignal(q, SIGHUP);
562212Sbill 				psignal(q, SIGCONT);
563188Sbill 			}
564215Sbill 			/*
565215Sbill 			 * Protect this process from future
566354Sbill 			 * tty signals, clear TSTP/TTIN/TTOU if pending,
567354Sbill 			 * and set SDETACH bit on procs.
568215Sbill 			 */
5691788Sbill 			(void) spgrp(q, -1);
57036Sbill 		}
571188Sbill 	wakeup((caddr_t)p->p_pptr);
572188Sbill 	psignal(p->p_pptr, SIGCHLD);
57336Sbill 	swtch();
57436Sbill }
57536Sbill 
57636Sbill wait()
57736Sbill {
578188Sbill 	struct vtimes vm;
579188Sbill 	struct vtimes *vp;
58036Sbill 
581188Sbill 	if ((u.u_ar0[PS] & PSL_ALLCC) != PSL_ALLCC) {
582188Sbill 		wait1(0, (struct vtimes *)0);
583188Sbill 		return;
584188Sbill 	}
585188Sbill 	vp = (struct vtimes *)u.u_ar0[R1];
586188Sbill 	wait1(u.u_ar0[R0], &vm);
587188Sbill 	if (u.u_error)
588188Sbill 		return;
589188Sbill 	(void) copyout((caddr_t)&vm, (caddr_t)vp, sizeof (struct vtimes));
59036Sbill }
59136Sbill 
59236Sbill /*
59336Sbill  * Wait system call.
59436Sbill  * Search for a terminated (zombie) child,
59536Sbill  * finally lay it to rest, and collect its status.
59636Sbill  * Look also for stopped (traced) children,
59736Sbill  * and pass back status from them.
59836Sbill  */
599188Sbill wait1(options, vp)
600188Sbill 	register options;
60136Sbill 	struct vtimes *vp;
60236Sbill {
60336Sbill 	register f;
60436Sbill 	register struct proc *p;
60536Sbill 
60636Sbill 	f = 0;
60736Sbill loop:
60836Sbill 	for(p = &proc[0]; p < &proc[NPROC]; p++)
609188Sbill 	if(p->p_pptr == u.u_procp) {
61036Sbill 		f++;
61136Sbill 		if(p->p_stat == SZOMB) {
61236Sbill 			u.u_r.r_val1 = p->p_pid;
61336Sbill 			u.u_r.r_val2 = ((struct xproc *)p)->xp_xstat;
61436Sbill 			((struct xproc *)p)->xp_xstat = 0;
61536Sbill 			if (vp)
61636Sbill 				*vp = ((struct xproc *)p)->xp_vm;
61736Sbill 			vmsadd(&u.u_cvm, &((struct xproc *)p)->xp_vm);
61836Sbill 			((struct xproc *)p)->xp_vm = zvms;
61936Sbill 			p->p_stat = NULL;
62036Sbill 			p->p_pid = 0;
62136Sbill 			p->p_ppid = 0;
622188Sbill 			p->p_pptr = 0;
62336Sbill 			p->p_sig = 0;
624188Sbill 			p->p_siga0 = 0;
625188Sbill 			p->p_siga1 = 0;
62636Sbill 			p->p_pgrp = 0;
62736Sbill 			p->p_flag = 0;
62836Sbill 			p->p_wchan = 0;
629188Sbill 			p->p_cursig = 0;
63036Sbill 			return;
63136Sbill 		}
632188Sbill 		if (p->p_stat == SSTOP && (p->p_flag&SWTED)==0 &&
633188Sbill 		    (p->p_flag&STRC || options&WUNTRACED)) {
634188Sbill 			p->p_flag |= SWTED;
635188Sbill 			u.u_r.r_val1 = p->p_pid;
636188Sbill 			u.u_r.r_val2 = (p->p_cursig<<8) | WSTOPPED;
637188Sbill 			return;
63836Sbill 		}
63936Sbill 	}
640188Sbill 	if (f==0) {
641188Sbill 		u.u_error = ECHILD;
642188Sbill 		return;
64336Sbill 	}
644188Sbill 	if (options&WNOHANG) {
645188Sbill 		u.u_r.r_val1 = 0;
646188Sbill 		return;
647188Sbill 	}
648912Sbill 	if ((u.u_procp->p_flag&SNUSIG) && setjmp(u.u_qsav)) {
649188Sbill 		u.u_eosys = RESTARTSYS;
650188Sbill 		return;
651188Sbill 	}
652188Sbill 	sleep((caddr_t)u.u_procp, PWAIT);
653188Sbill 	goto loop;
65436Sbill }
65536Sbill 
65636Sbill /*
65736Sbill  * fork system call.
65836Sbill  */
65936Sbill fork()
66036Sbill {
66136Sbill 
66236Sbill 	u.u_cdmap = zdmap;
66336Sbill 	u.u_csmap = zdmap;
66436Sbill 	if (swpexpand(u.u_dsize, u.u_ssize, &u.u_cdmap, &u.u_csmap) == 0) {
66536Sbill 		u.u_r.r_val2 = 0;
66636Sbill 		return;
66736Sbill 	}
66836Sbill 	fork1(0);
66936Sbill }
67036Sbill 
67136Sbill fork1(isvfork)
67236Sbill {
67336Sbill 	register struct proc *p1, *p2;
67436Sbill 	register a;
67536Sbill 
67636Sbill 	a = 0;
67736Sbill 	p2 = NULL;
67836Sbill 	for(p1 = &proc[0]; p1 < &proc[NPROC]; p1++) {
67936Sbill 		if (p1->p_stat==NULL && p2==NULL)
68036Sbill 			p2 = p1;
68136Sbill 		else {
68236Sbill 			if (p1->p_uid==u.u_uid && p1->p_stat!=NULL)
68336Sbill 				a++;
68436Sbill 		}
68536Sbill 	}
686*2301Skre 
68736Sbill 	/*
68836Sbill 	 * Disallow if
68936Sbill 	 *  No processes at all;
69036Sbill 	 *  not su and too many procs owned; or
69136Sbill 	 *  not su and would take last slot.
69236Sbill 	 */
693*2301Skre 	if (p2==NULL || (u.u_uid!=0 && (p2>=&proc[NPROC-5] || 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