xref: /csrg-svn/sys/kern/kern_proc.c (revision 1399)
1*1399Sbill /*	kern_proc.c	3.25	10/11/80	*/
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;
4536Sbill 	struct inode *ip;
4636Sbill 	swblk_t bno;
4736Sbill 
4836Sbill 	if ((ip = namei(uchar, 0)) == NULL)
4936Sbill 		return;
5036Sbill 	bno = 0;
5136Sbill 	bp = 0;
5236Sbill 	if(access(ip, IEXEC))
5336Sbill 		goto bad;
5436Sbill 	if((ip->i_mode & IFMT) != IFREG ||
5536Sbill 	   (ip->i_mode & (IEXEC|(IEXEC>>3)|(IEXEC>>6))) == 0) {
5636Sbill 		u.u_error = EACCES;
5736Sbill 		goto bad;
5836Sbill 	}
5936Sbill 	/*
6036Sbill 	 * Collect arguments on "file" in swap space.
6136Sbill 	 */
6236Sbill 	na = 0;
6336Sbill 	ne = 0;
6436Sbill 	nc = 0;
6536Sbill 	uap = (struct execa *)u.u_ap;
66307Sbill 	if ((bno = malloc(argmap, ctod(clrnd((int) btoc(NCARGS))))) == 0) {
6736Sbill 		swkill(u.u_procp, "exece");
6836Sbill 		goto bad;
6936Sbill 	}
7036Sbill 	if (bno % CLSIZE)
7136Sbill 		panic("execa malloc");
7236Sbill 	if (uap->argp) for (;;) {
7336Sbill 		ap = NULL;
7436Sbill 		if (uap->argp) {
7536Sbill 			ap = fuword((caddr_t)uap->argp);
7636Sbill 			uap->argp++;
7736Sbill 		}
7836Sbill 		if (ap==NULL && uap->envp) {
7936Sbill 			uap->argp = NULL;
8036Sbill 			if ((ap = fuword((caddr_t)uap->envp)) == NULL)
8136Sbill 				break;
8236Sbill 			uap->envp++;
8336Sbill 			ne++;
8436Sbill 		}
8536Sbill 		if (ap==NULL)
8636Sbill 			break;
8736Sbill 		na++;
8836Sbill 		if(ap == -1)
8936Sbill 			u.u_error = EFAULT;
9036Sbill 		do {
9136Sbill 			if (nc >= NCARGS-1)
9236Sbill 				u.u_error = E2BIG;
9336Sbill 			if ((c = fubyte((caddr_t)ap++)) < 0)
9436Sbill 				u.u_error = EFAULT;
9583Sbill 			if (u.u_error) {
9683Sbill 				if (bp)
9783Sbill 					brelse(bp);
9883Sbill 				bp = 0;
9936Sbill 				goto badarg;
10083Sbill 			}
10136Sbill 			if ((nc&BMASK) == 0) {
10236Sbill 				if (bp)
10336Sbill 					bdwrite(bp);
104307Sbill 				bp = getblk(argdev,
105307Sbill 				    (daddr_t)(dbtofsb(bno)+(nc>>BSHIFT)));
10636Sbill 				cp = bp->b_un.b_addr;
10736Sbill 			}
10836Sbill 			nc++;
10936Sbill 			*cp++ = c;
11036Sbill 		} while (c>0);
11136Sbill 	}
11236Sbill 	if (bp)
11336Sbill 		bdwrite(bp);
11436Sbill 	bp = 0;
11536Sbill 	nc = (nc + NBPW-1) & ~(NBPW-1);
1161182Sbill 	getxfile(ip, nc + (na+4)*NBPW);
117912Sbill 	if (u.u_error) {
11836Sbill badarg:
11936Sbill 		for (c = 0; c < nc; c += BSIZE)
120307Sbill 			if (bp = baddr(argdev, dbtofsb(bno)+(c>>BSHIFT))) {
12136Sbill 				bp->b_flags |= B_AGE;		/* throw away */
12236Sbill 				bp->b_flags &= ~B_DELWRI;	/* cancel io */
12336Sbill 				brelse(bp);
12436Sbill 				bp = 0;
12536Sbill 			}
12636Sbill 		goto bad;
12736Sbill 	}
12836Sbill 
12936Sbill 	/*
13036Sbill 	 * copy back arglist
13136Sbill 	 */
13236Sbill 
13336Sbill 	ucp = USRSTACK - nc - NBPW;
13436Sbill 	ap = ucp - na*NBPW - 3*NBPW;
13536Sbill 	u.u_ar0[SP] = ap;
136132Sbill 	(void) suword((caddr_t)ap, na-ne);
13736Sbill 	nc = 0;
13836Sbill 	for (;;) {
13936Sbill 		ap += NBPW;
14036Sbill 		if (na==ne) {
141132Sbill 			(void) suword((caddr_t)ap, 0);
14236Sbill 			ap += NBPW;
14336Sbill 		}
14436Sbill 		if (--na < 0)
14536Sbill 			break;
146132Sbill 		(void) suword((caddr_t)ap, ucp);
14736Sbill 		do {
14836Sbill 			if ((nc&BMASK) == 0) {
14936Sbill 				if (bp)
15036Sbill 					brelse(bp);
151307Sbill 				bp = bread(argdev,
152307Sbill 				    (daddr_t)(dbtofsb(bno)+(nc>>BSHIFT)));
15336Sbill 				bp->b_flags |= B_AGE;		/* throw away */
15436Sbill 				bp->b_flags &= ~B_DELWRI;	/* cancel io */
15536Sbill 				cp = bp->b_un.b_addr;
15636Sbill 			}
157132Sbill 			(void) subyte((caddr_t)ucp++, (c = *cp++));
15836Sbill 			nc++;
15936Sbill 		} while(c&0377);
16036Sbill 	}
161132Sbill 	(void) suword((caddr_t)ap, 0);
162132Sbill 	(void) suword((caddr_t)ucp, 0);
16336Sbill 	setregs();
16436Sbill bad:
16536Sbill 	if (bp)
16636Sbill 		brelse(bp);
16736Sbill 	if (bno)
168307Sbill 		mfree(argmap, ctod(clrnd((int) btoc(NCARGS))), bno);
16936Sbill 	iput(ip);
17036Sbill }
17136Sbill 
17236Sbill /*
17336Sbill  * Read in and set up memory for executed file.
17436Sbill  */
17536Sbill getxfile(ip, nargc)
17636Sbill register struct inode *ip;
17736Sbill {
17836Sbill 	register size_t ts, ds, ss;
17936Sbill 	int pagi = 0;
18036Sbill 
18136Sbill 	/*
18236Sbill 	 * read in first few bytes
18336Sbill 	 * of file for segment
18436Sbill 	 * sizes:
185912Sbill 	 * ux_mag = 407/410/413
18636Sbill 	 *  407 is plain executable
18736Sbill 	 *  410 is RO text
18836Sbill 	 *  413 is demand paged RO text
18936Sbill 	 */
19036Sbill 
19136Sbill 	u.u_base = (caddr_t)&u.u_exdata;
19236Sbill 	u.u_count = sizeof(u.u_exdata);
19336Sbill 	u.u_offset = 0;
19436Sbill 	u.u_segflg = 1;
19536Sbill 	readi(ip);
19636Sbill 	u.u_segflg = 0;
19736Sbill 	if(u.u_error)
19836Sbill 		goto bad;
19936Sbill 	if (u.u_count!=0) {
20036Sbill 		u.u_error = ENOEXEC;
20136Sbill 		goto bad;
20236Sbill 	}
20336Sbill 	switch (u.u_exdata.ux_mag) {
20436Sbill 
20536Sbill 	case 0407:
20636Sbill 		u.u_exdata.ux_dsize += u.u_exdata.ux_tsize;
20736Sbill 		u.u_exdata.ux_tsize = 0;
20836Sbill 		break;
20936Sbill 
21036Sbill 	case 0413:
21136Sbill 		pagi = SPAGI;
21236Sbill 		/* fall into ... */
21336Sbill 
21436Sbill 	case 0410:
21536Sbill 		if (u.u_exdata.ux_tsize == 0) {
21636Sbill 			u.u_error = ENOEXEC;
21736Sbill 			goto bad;
21836Sbill 		}
21936Sbill 		break;
22036Sbill 
22136Sbill 	default:
22236Sbill 		u.u_error = ENOEXEC;
22336Sbill 		goto bad;
22436Sbill 	}
22536Sbill 	if(u.u_exdata.ux_tsize!=0 && (ip->i_flag&ITEXT)==0 && ip->i_count!=1) {
226890Sbill 		register struct file *fp;
227890Sbill 
228890Sbill 		for (fp = file; fp < &file[NFILE]; fp++)
229890Sbill 			if (fp->f_inode == ip && (fp->f_flag&FWRITE)) {
230890Sbill 				u.u_error = ETXTBSY;
231890Sbill 				goto bad;
232890Sbill 			}
23336Sbill 	}
23436Sbill 
23536Sbill 	/*
23636Sbill 	 * find text and data sizes
23736Sbill 	 * try them out for possible
23836Sbill 	 * exceed of max sizes
23936Sbill 	 */
24036Sbill 
24136Sbill 	ts = clrnd(btoc(u.u_exdata.ux_tsize));
24236Sbill 	ds = clrnd(btoc((u.u_exdata.ux_dsize+u.u_exdata.ux_bsize)));
24336Sbill 	ss = clrnd(SSIZE + btoc(nargc));
244912Sbill 	if (chksize(ts, ds, ss))
245912Sbill 		goto bad;
246912Sbill 	u.u_cdmap = zdmap;
247912Sbill 	u.u_csmap = zdmap;
248912Sbill 	if (swpexpand(ds, ss, &u.u_cdmap, &u.u_csmap) == NULL)
249912Sbill 		goto bad;
25036Sbill 
251912Sbill 	/*
252912Sbill 	 * At this point, committed to the new image!
253912Sbill 	 * Release virtual memory resources of old process, and
254912Sbill 	 * initialize the virtual memory of the new process.
255912Sbill 	 * If we resulted from vfork(), instead wakeup our
256912Sbill 	 * parent who will set SVFDONE when he has taken back
257912Sbill 	 * our resources.
258912Sbill 	 */
259912Sbill 	u.u_prof.pr_scale = 0;
260912Sbill 	if ((u.u_procp->p_flag & SVFORK) == 0)
261912Sbill 		vrelvm();
262912Sbill 	else {
263912Sbill 		u.u_procp->p_flag &= ~SVFORK;
264912Sbill 		u.u_procp->p_flag |= SKEEP;
265912Sbill 		wakeup((caddr_t)u.u_procp);
266912Sbill 		while ((u.u_procp->p_flag & SVFDONE) == 0)
267912Sbill 			sleep((caddr_t)u.u_procp, PZERO - 1);
268912Sbill 		u.u_procp->p_flag &= ~(SVFDONE|SKEEP);
269912Sbill 	}
270912Sbill 	u.u_procp->p_flag &= ~(SPAGI|SANOM|SUANOM|SNUSIG);
271912Sbill 	u.u_procp->p_flag |= pagi;
272912Sbill 	u.u_dmap = u.u_cdmap;
273912Sbill 	u.u_smap = u.u_csmap;
274912Sbill 	vgetvm(ts, ds, ss);
275912Sbill 
276912Sbill 	if (pagi == 0) {
27736Sbill 		/*
278912Sbill 		 * Read in data segment.
27936Sbill 		 */
280912Sbill 		u.u_base = (char *)ctob(ts);
281912Sbill 		u.u_offset = sizeof(u.u_exdata)+u.u_exdata.ux_tsize;
282912Sbill 		u.u_count = u.u_exdata.ux_dsize;
283912Sbill 		readi(ip);
284912Sbill 	}
285912Sbill 	xalloc(ip, pagi);
286912Sbill 	if (pagi && u.u_procp->p_textp)
287912Sbill 		vinifod((struct fpte *)dptopte(u.u_procp, 0),
288912Sbill 		    PG_FTEXT, u.u_procp->p_textp->x_iptr,
289912Sbill 		    1 + ts/CLSIZE, (int)btoc(u.u_exdata.ux_dsize));
29036Sbill 
291912Sbill 	/* THIS SHOULD BE DONE AT A LOWER LEVEL, IF AT ALL */
292912Sbill 	mtpr(TBIA, 0);
29336Sbill 
294912Sbill 	/*
295912Sbill 	 * set SUID/SGID protections, if no tracing
296912Sbill 	 */
297912Sbill 	if ((u.u_procp->p_flag&STRC)==0) {
298912Sbill 		if(ip->i_mode&ISUID)
299912Sbill 			if(u.u_uid != 0) {
300912Sbill 				u.u_uid = ip->i_uid;
301912Sbill 				u.u_procp->p_uid = ip->i_uid;
302912Sbill 			}
303912Sbill 		if(ip->i_mode&ISGID)
304912Sbill 			u.u_gid = ip->i_gid;
305912Sbill 	} else
306912Sbill 		psignal(u.u_procp, SIGTRAP);
30736Sbill 	u.u_tsize = ts;
30836Sbill 	u.u_dsize = ds;
30936Sbill 	u.u_ssize = ss;
31036Sbill bad:
311912Sbill 	return;
31236Sbill }
31336Sbill 
31436Sbill /*
31536Sbill  * Clear registers on exec
31636Sbill  */
31736Sbill setregs()
31836Sbill {
319173Sbill 	register int (**rp)();
32036Sbill 	register i;
321188Sbill 	long sigmask;
32236Sbill 
323188Sbill 	for(rp = &u.u_signal[0], sigmask = 1L; rp < &u.u_signal[NSIG];
324188Sbill 	    sigmask <<= 1, rp++) {
325188Sbill 		switch (*rp) {
326188Sbill 
327188Sbill 		case SIG_IGN:
328188Sbill 		case SIG_DFL:
329188Sbill 		case SIG_HOLD:
330188Sbill 			continue;
331188Sbill 
332188Sbill 		default:
333188Sbill 			/*
334206Sbill 			 * Normal or deferring catch; revert to default.
335188Sbill 			 */
336206Sbill 			(void) spl6();
337206Sbill 			*rp = SIG_DFL;
338188Sbill 			if ((int)*rp & 1)
339188Sbill 				u.u_procp->p_siga0 |= sigmask;
340188Sbill 			else
341188Sbill 				u.u_procp->p_siga1 &= ~sigmask;
342188Sbill 			if ((int)*rp & 2)
343188Sbill 				u.u_procp->p_siga1 |= sigmask;
344188Sbill 			else
345188Sbill 				u.u_procp->p_siga1 &= ~sigmask;
346206Sbill 			(void) spl0();
347188Sbill 			continue;
348188Sbill 		}
349188Sbill 	}
35036Sbill /*
35136Sbill 	for(rp = &u.u_ar0[0]; rp < &u.u_ar0[16];)
35236Sbill 		*rp++ = 0;
35336Sbill */
35436Sbill 	u.u_ar0[PC] = u.u_exdata.ux_entloc + 2; /* skip over entry mask */
35536Sbill 	for(i=0; i<NOFILE; i++) {
35636Sbill 		if (u.u_pofile[i]&EXCLOSE) {
35736Sbill 			closef(u.u_ofile[i]);
35836Sbill 			u.u_ofile[i] = NULL;
359188Sbill 			u.u_pofile[i] &= ~EXCLOSE;
36036Sbill 		}
36136Sbill 	}
36236Sbill 	/*
36336Sbill 	 * Remember file name for accounting.
36436Sbill 	 */
36536Sbill 	u.u_acflag &= ~AFORK;
36636Sbill 	bcopy((caddr_t)u.u_dbuf, (caddr_t)u.u_comm, DIRSIZ);
36736Sbill }
36836Sbill 
36936Sbill /*
37036Sbill  * exit system call:
37136Sbill  * pass back caller's arg
37236Sbill  */
37336Sbill rexit()
37436Sbill {
37536Sbill 	register struct a {
37636Sbill 		int	rval;
37736Sbill 	} *uap;
37836Sbill 
37936Sbill 	uap = (struct a *)u.u_ap;
38036Sbill 	exit((uap->rval & 0377) << 8);
38136Sbill }
38236Sbill 
38336Sbill /*
38436Sbill  * Release resources.
38536Sbill  * Save u. area for parent to look at.
38636Sbill  * Enter zombie state.
38736Sbill  * Wake up parent and init processes,
38836Sbill  * and dispose of children.
38936Sbill  */
39036Sbill exit(rv)
39136Sbill {
39236Sbill 	register int i;
39336Sbill 	register struct proc *p, *q;
39436Sbill 	register struct file *f;
39536Sbill 	register int x;
39636Sbill 
39736Sbill #ifdef PGINPROF
39836Sbill 	vmsizmon();
39936Sbill #endif
40036Sbill 	p = u.u_procp;
40136Sbill 	p->p_flag &= ~(STRC|SULOCK);
40236Sbill 	p->p_flag |= SWEXIT;
40336Sbill 	p->p_clktim = 0;
404188Sbill 	(void) spl6();
405188Sbill 	if ((int)SIG_IGN & 1)
406188Sbill 		p->p_siga0 = ~0;
407188Sbill 	else
408188Sbill 		p->p_siga0 = 0;
409188Sbill 	if ((int)SIG_IGN & 2)
410188Sbill 		p->p_siga1 = ~0;
411188Sbill 	else
412206Sbill 		p->p_siga1 = 0;
413188Sbill 	(void) spl0();
414*1399Sbill 	p->p_cpticks = 0;
415*1399Sbill 	p->p_pctcpu = 0;
41636Sbill 	for(i=0; i<NSIG; i++)
417173Sbill 		u.u_signal[i] = SIG_IGN;
41836Sbill 	/*
41936Sbill 	 * Release virtual memory.  If we resulted from
42036Sbill 	 * a vfork(), instead give the resources back to
42136Sbill 	 * the parent.
42236Sbill 	 */
42336Sbill 	if ((p->p_flag & SVFORK) == 0)
42436Sbill 		vrelvm();
42536Sbill 	else {
42636Sbill 		p->p_flag &= ~SVFORK;
42736Sbill 		wakeup((caddr_t)p);
42836Sbill 		while ((p->p_flag & SVFDONE) == 0)
42936Sbill 			sleep((caddr_t)p, PZERO - 1);
43036Sbill 		p->p_flag &= ~SVFDONE;
43136Sbill 	}
43236Sbill 	for(i=0; i<NOFILE; i++) {
43336Sbill 		f = u.u_ofile[i];
43436Sbill 		u.u_ofile[i] = NULL;
43536Sbill 		closef(f);
43636Sbill 	}
43736Sbill 	plock(u.u_cdir);
43836Sbill 	iput(u.u_cdir);
43936Sbill 	if (u.u_rdir) {
44036Sbill 		plock(u.u_rdir);
44136Sbill 		iput(u.u_rdir);
44236Sbill 	}
443363Sbill 	u.u_limit[LIM_FSIZE] = INFINITY;
44436Sbill 	acct();
44536Sbill 	vrelpt(u.u_procp);
44636Sbill 	vrelu(u.u_procp, 0);
44736Sbill 	multprog--;
448307Sbill /*	spl7();			/* clock will get mad because of overlaying */
449931Sbill 	p->p_stat = SZOMB;
450307Sbill 	noproc = 1;
45136Sbill 	i = PIDHASH(p->p_pid);
45236Sbill 	x = p - proc;
45336Sbill 	if (pidhash[i] == x)
45436Sbill 		pidhash[i] = p->p_idhash;
45536Sbill 	else {
45636Sbill 		for (i = pidhash[i]; i != 0; i = proc[i].p_idhash)
45736Sbill 			if (proc[i].p_idhash == x) {
45836Sbill 				proc[i].p_idhash = p->p_idhash;
45936Sbill 				goto done;
46036Sbill 			}
46136Sbill 		panic("exit");
46236Sbill 	}
46336Sbill done:
46436Sbill 	((struct xproc *)p)->xp_xstat = rv;		/* overlay */
46536Sbill 	((struct xproc *)p)->xp_vm = u.u_vm;		/* overlay */
46636Sbill 	vmsadd(&((struct xproc *)p)->xp_vm, &u.u_cvm);
46736Sbill 	for(q = &proc[0]; q < &proc[NPROC]; q++)
468188Sbill 		if(q->p_pptr == p) {
469188Sbill 			q->p_pptr = &proc[1];
470188Sbill 			q->p_ppid = 1;
47136Sbill 			wakeup((caddr_t)&proc[1]);
472188Sbill 			/*
473212Sbill 			 * Traced processes are killed
474188Sbill 			 * since their existence means someone is screwing up.
475354Sbill 			 * Stopped processes are sent a hangup and a continue.
476212Sbill 			 * This is designed to be ``safe'' for setuid
477212Sbill 			 * processes since they must be willing to tolerate
478212Sbill 			 * hangups anyways.
479188Sbill 			 */
480212Sbill 			if (q->p_flag&STRC) {
481188Sbill 				q->p_flag &= ~STRC;
482188Sbill 				psignal(q, SIGKILL);
483212Sbill 			} else if (q->p_stat == SSTOP) {
484212Sbill 				psignal(q, SIGHUP);
485212Sbill 				psignal(q, SIGCONT);
486188Sbill 			}
487215Sbill 			/*
488215Sbill 			 * Protect this process from future
489354Sbill 			 * tty signals, clear TSTP/TTIN/TTOU if pending,
490354Sbill 			 * and set SDETACH bit on procs.
491215Sbill 			 */
492307Sbill 			spgrp(q, -1);
49336Sbill 		}
494188Sbill 	wakeup((caddr_t)p->p_pptr);
495188Sbill 	psignal(p->p_pptr, SIGCHLD);
49636Sbill 	swtch();
49736Sbill }
49836Sbill 
49936Sbill wait()
50036Sbill {
501188Sbill 	struct vtimes vm;
502188Sbill 	struct vtimes *vp;
50336Sbill 
504188Sbill 	if ((u.u_ar0[PS] & PSL_ALLCC) != PSL_ALLCC) {
505188Sbill 		wait1(0, (struct vtimes *)0);
506188Sbill 		return;
507188Sbill 	}
508188Sbill 	vp = (struct vtimes *)u.u_ar0[R1];
509188Sbill 	wait1(u.u_ar0[R0], &vm);
510188Sbill 	if (u.u_error)
511188Sbill 		return;
512188Sbill 	(void) copyout((caddr_t)&vm, (caddr_t)vp, sizeof (struct vtimes));
51336Sbill }
51436Sbill 
51536Sbill /*
51636Sbill  * Wait system call.
51736Sbill  * Search for a terminated (zombie) child,
51836Sbill  * finally lay it to rest, and collect its status.
51936Sbill  * Look also for stopped (traced) children,
52036Sbill  * and pass back status from them.
52136Sbill  */
522188Sbill wait1(options, vp)
523188Sbill 	register options;
52436Sbill 	struct vtimes *vp;
52536Sbill {
52636Sbill 	register f;
52736Sbill 	register struct proc *p;
52836Sbill 
52936Sbill 	f = 0;
53036Sbill loop:
53136Sbill 	for(p = &proc[0]; p < &proc[NPROC]; p++)
532188Sbill 	if(p->p_pptr == u.u_procp) {
53336Sbill 		f++;
53436Sbill 		if(p->p_stat == SZOMB) {
53536Sbill 			u.u_r.r_val1 = p->p_pid;
53636Sbill 			u.u_r.r_val2 = ((struct xproc *)p)->xp_xstat;
53736Sbill 			((struct xproc *)p)->xp_xstat = 0;
53836Sbill 			if (vp)
53936Sbill 				*vp = ((struct xproc *)p)->xp_vm;
54036Sbill 			vmsadd(&u.u_cvm, &((struct xproc *)p)->xp_vm);
54136Sbill 			((struct xproc *)p)->xp_vm = zvms;
54236Sbill 			p->p_stat = NULL;
54336Sbill 			p->p_pid = 0;
54436Sbill 			p->p_ppid = 0;
545188Sbill 			p->p_pptr = 0;
54636Sbill 			p->p_sig = 0;
547188Sbill 			p->p_siga0 = 0;
548188Sbill 			p->p_siga1 = 0;
54936Sbill 			p->p_pgrp = 0;
55036Sbill 			p->p_flag = 0;
55136Sbill 			p->p_wchan = 0;
552188Sbill 			p->p_cursig = 0;
55336Sbill 			return;
55436Sbill 		}
555188Sbill 		if (p->p_stat == SSTOP && (p->p_flag&SWTED)==0 &&
556188Sbill 		    (p->p_flag&STRC || options&WUNTRACED)) {
557188Sbill 			p->p_flag |= SWTED;
558188Sbill 			u.u_r.r_val1 = p->p_pid;
559188Sbill 			u.u_r.r_val2 = (p->p_cursig<<8) | WSTOPPED;
560188Sbill 			return;
56136Sbill 		}
56236Sbill 	}
563188Sbill 	if (f==0) {
564188Sbill 		u.u_error = ECHILD;
565188Sbill 		return;
56636Sbill 	}
567188Sbill 	if (options&WNOHANG) {
568188Sbill 		u.u_r.r_val1 = 0;
569188Sbill 		return;
570188Sbill 	}
571912Sbill 	if ((u.u_procp->p_flag&SNUSIG) && setjmp(u.u_qsav)) {
572188Sbill 		u.u_eosys = RESTARTSYS;
573188Sbill 		return;
574188Sbill 	}
575188Sbill 	sleep((caddr_t)u.u_procp, PWAIT);
576188Sbill 	goto loop;
57736Sbill }
57836Sbill 
57936Sbill /*
58036Sbill  * fork system call.
58136Sbill  */
58236Sbill fork()
58336Sbill {
58436Sbill 
58536Sbill 	u.u_cdmap = zdmap;
58636Sbill 	u.u_csmap = zdmap;
58736Sbill 	if (swpexpand(u.u_dsize, u.u_ssize, &u.u_cdmap, &u.u_csmap) == 0) {
58836Sbill 		u.u_r.r_val2 = 0;
58936Sbill 		return;
59036Sbill 	}
59136Sbill 	fork1(0);
59236Sbill }
59336Sbill 
59436Sbill fork1(isvfork)
59536Sbill {
59636Sbill 	register struct proc *p1, *p2;
59736Sbill 	register a;
59836Sbill 
59936Sbill 	a = 0;
60036Sbill 	p2 = NULL;
60136Sbill 	for(p1 = &proc[0]; p1 < &proc[NPROC]; p1++) {
60236Sbill 		if (p1->p_stat==NULL && p2==NULL)
60336Sbill 			p2 = p1;
60436Sbill 		else {
60536Sbill 			if (p1->p_uid==u.u_uid && p1->p_stat!=NULL)
60636Sbill 				a++;
60736Sbill 		}
60836Sbill 	}
60936Sbill 	/*
61036Sbill 	 * Disallow if
61136Sbill 	 *  No processes at all;
61236Sbill 	 *  not su and too many procs owned; or
61336Sbill 	 *  not su and would take last slot.
61436Sbill 	 */
61536Sbill 	if (p2==NULL || (u.u_uid!=0 && (p2==&proc[NPROC-1] || a>MAXUPRC))) {
61636Sbill 		u.u_error = EAGAIN;
61736Sbill 		if (!isvfork) {
618132Sbill 			(void) vsexpand(0, &u.u_cdmap, 1);
619132Sbill 			(void) vsexpand(0, &u.u_csmap, 1);
62036Sbill 		}
62136Sbill 		goto out;
62236Sbill 	}
62336Sbill 	p1 = u.u_procp;
62436Sbill 	if(newproc(isvfork)) {
62536Sbill 		u.u_r.r_val1 = p1->p_pid;
62636Sbill 		u.u_r.r_val2 = 1;  /* child */
62736Sbill 		u.u_start = time;
62836Sbill 		u.u_acflag = AFORK;
62936Sbill 		return;
63036Sbill 	}
63136Sbill 	u.u_r.r_val1 = p2->p_pid;
63236Sbill 
63336Sbill out:
63436Sbill 	u.u_r.r_val2 = 0;
63536Sbill }
63636Sbill 
63736Sbill /*
63836Sbill  * break system call.
63936Sbill  *  -- bad planning: "break" is a dirty word in C.
64036Sbill  */
64136Sbill sbreak()
64236Sbill {
64336Sbill 	struct a {
64436Sbill 		char	*nsiz;
64536Sbill 	};
64636Sbill 	register int n, d;
64736Sbill 
64836Sbill 	/*
64936Sbill 	 * set n to new data size
65036Sbill 	 * set d to new-old
65136Sbill 	 */
65236Sbill 
65336Sbill 	n = btoc(((struct a *)u.u_ap)->nsiz);
65436Sbill 	if (!u.u_sep)
65536Sbill 		n -= ctos(u.u_tsize) * stoc(1);
65636Sbill 	if (n < 0)
65736Sbill 		n = 0;
65836Sbill 	d = clrnd(n - u.u_dsize);
659368Sbill 	if (ctob(u.u_dsize+d) > u.u_limit[LIM_DATA]) {
660363Sbill 		u.u_error = ENOMEM;
661363Sbill 		return;
662363Sbill 	}
66336Sbill 	if (chksize(u.u_tsize, u.u_dsize+d, u.u_ssize))
66436Sbill 		return;
66536Sbill 	if (swpexpand(u.u_dsize+d, u.u_ssize, &u.u_dmap, &u.u_smap)==0)
66636Sbill 		return;
66736Sbill 	expand(d, P0BR);
66836Sbill }
669