xref: /csrg-svn/sys/kern/kern_proc.c (revision 890)
1*890Sbill /*	kern_proc.c	3.21	09/19/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"
21*890Sbill #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);
11636Sbill 	if (getxfile(ip, nc) || u.u_error) {
11736Sbill badarg:
11836Sbill 		for (c = 0; c < nc; c += BSIZE)
119307Sbill 			if (bp = baddr(argdev, dbtofsb(bno)+(c>>BSHIFT))) {
12036Sbill 				bp->b_flags |= B_AGE;		/* throw away */
12136Sbill 				bp->b_flags &= ~B_DELWRI;	/* cancel io */
12236Sbill 				brelse(bp);
12336Sbill 				bp = 0;
12436Sbill 			}
12536Sbill 		goto bad;
12636Sbill 	}
12736Sbill 
12836Sbill 	/*
12936Sbill 	 * copy back arglist
13036Sbill 	 */
13136Sbill 
13236Sbill 	ucp = USRSTACK - nc - NBPW;
13336Sbill 	ap = ucp - na*NBPW - 3*NBPW;
13436Sbill 	u.u_ar0[SP] = ap;
135132Sbill 	(void) suword((caddr_t)ap, na-ne);
13636Sbill 	nc = 0;
13736Sbill 	for (;;) {
13836Sbill 		ap += NBPW;
13936Sbill 		if (na==ne) {
140132Sbill 			(void) suword((caddr_t)ap, 0);
14136Sbill 			ap += NBPW;
14236Sbill 		}
14336Sbill 		if (--na < 0)
14436Sbill 			break;
145132Sbill 		(void) suword((caddr_t)ap, ucp);
14636Sbill 		do {
14736Sbill 			if ((nc&BMASK) == 0) {
14836Sbill 				if (bp)
14936Sbill 					brelse(bp);
150307Sbill 				bp = bread(argdev,
151307Sbill 				    (daddr_t)(dbtofsb(bno)+(nc>>BSHIFT)));
15236Sbill 				bp->b_flags |= B_AGE;		/* throw away */
15336Sbill 				bp->b_flags &= ~B_DELWRI;	/* cancel io */
15436Sbill 				cp = bp->b_un.b_addr;
15536Sbill 			}
156132Sbill 			(void) subyte((caddr_t)ucp++, (c = *cp++));
15736Sbill 			nc++;
15836Sbill 		} while(c&0377);
15936Sbill 	}
160132Sbill 	(void) suword((caddr_t)ap, 0);
161132Sbill 	(void) suword((caddr_t)ucp, 0);
16236Sbill 	setregs();
16336Sbill bad:
16436Sbill 	if (bp)
16536Sbill 		brelse(bp);
16636Sbill 	if (bno)
167307Sbill 		mfree(argmap, ctod(clrnd((int) btoc(NCARGS))), bno);
16836Sbill 	iput(ip);
16936Sbill }
17036Sbill 
17136Sbill /*
17236Sbill  * Read in and set up memory for executed file.
17336Sbill  * Zero return is normal;
17436Sbill  * non-zero means only the text is being replaced
17536Sbill  */
17636Sbill getxfile(ip, nargc)
17736Sbill register struct inode *ip;
17836Sbill {
17936Sbill 	register sep;
18036Sbill 	register size_t ts, ds, ss;
181*890Sbill 	int overlay;
18236Sbill 	int pagi = 0;
18336Sbill 
18436Sbill 	/*
18536Sbill 	 * read in first few bytes
18636Sbill 	 * of file for segment
18736Sbill 	 * sizes:
18836Sbill 	 * ux_mag = 407/410/411/405
18936Sbill 	 *  407 is plain executable
19036Sbill 	 *  410 is RO text
19136Sbill 	 *  411 is separated ID
19236Sbill 	 *  405 is overlaid text
19336Sbill 	 *  412 is demand paged plain executable (NOT IMPLEMENTED)
19436Sbill 	 *  413 is demand paged RO text
19536Sbill 	 */
19636Sbill 
19736Sbill 	u.u_base = (caddr_t)&u.u_exdata;
19836Sbill 	u.u_count = sizeof(u.u_exdata);
19936Sbill 	u.u_offset = 0;
20036Sbill 	u.u_segflg = 1;
20136Sbill 	readi(ip);
20236Sbill 	u.u_segflg = 0;
20336Sbill 	if(u.u_error)
20436Sbill 		goto bad;
20536Sbill 	if (u.u_count!=0) {
20636Sbill 		u.u_error = ENOEXEC;
20736Sbill 		goto bad;
20836Sbill 	}
20936Sbill 	sep = 0;
21036Sbill 	overlay = 0;
21136Sbill 	switch (u.u_exdata.ux_mag) {
21236Sbill 
21336Sbill 	case 0405:
21436Sbill 		overlay++;
21536Sbill 		break;
21636Sbill 
21736Sbill 	case 0412:
21836Sbill 		u.u_error = ENOEXEC;
21936Sbill 		goto bad;
22036Sbill 
22136Sbill 	case 0407:
22236Sbill 		u.u_exdata.ux_dsize += u.u_exdata.ux_tsize;
22336Sbill 		u.u_exdata.ux_tsize = 0;
22436Sbill 		break;
22536Sbill 
22636Sbill 	case 0413:
22736Sbill 		pagi = SPAGI;
22836Sbill 		/* fall into ... */
22936Sbill 
23036Sbill 	case 0410:
23136Sbill 		if (u.u_exdata.ux_tsize == 0) {
23236Sbill 			u.u_error = ENOEXEC;
23336Sbill 			goto bad;
23436Sbill 		}
23536Sbill 		break;
23636Sbill 
23736Sbill 	case 0411:
23836Sbill 		u.u_error = ENOEXEC;
23936Sbill 		goto bad;
24036Sbill 
24136Sbill 	default:
24236Sbill 		u.u_error = ENOEXEC;
24336Sbill 		goto bad;
24436Sbill 	}
24536Sbill 	if(u.u_exdata.ux_tsize!=0 && (ip->i_flag&ITEXT)==0 && ip->i_count!=1) {
246*890Sbill 		register struct file *fp;
247*890Sbill 
248*890Sbill 		for (fp = file; fp < &file[NFILE]; fp++)
249*890Sbill 			if (fp->f_inode == ip && (fp->f_flag&FWRITE)) {
250*890Sbill 				u.u_error = ETXTBSY;
251*890Sbill 				goto bad;
252*890Sbill 			}
25336Sbill 	}
25436Sbill 
25536Sbill 	/*
25636Sbill 	 * find text and data sizes
25736Sbill 	 * try them out for possible
25836Sbill 	 * exceed of max sizes
25936Sbill 	 */
26036Sbill 
26136Sbill 	ts = clrnd(btoc(u.u_exdata.ux_tsize));
26236Sbill 	ds = clrnd(btoc((u.u_exdata.ux_dsize+u.u_exdata.ux_bsize)));
26336Sbill 	ss = clrnd(SSIZE + btoc(nargc));
26436Sbill 	if (overlay) {
265215Sbill 		if ((u.u_procp->p_flag & SPAGI) ||
266215Sbill 		    u.u_sep==0 && ctos(ts) != ctos(u.u_tsize) || nargc) {
26736Sbill 			u.u_error = ENOMEM;
26836Sbill 			goto bad;
26936Sbill 		}
27036Sbill 		ds = u.u_dsize;
27136Sbill 		ss = u.u_ssize;
27236Sbill 		sep = u.u_sep;
27336Sbill 		xfree();
27436Sbill 		xalloc(ip, pagi);
27536Sbill 		u.u_ar0[PC] = u.u_exdata.ux_entloc + 2; /* skip over entry mask */
27636Sbill 	} else {
27736Sbill 		if (chksize(ts, ds, ss))
27836Sbill 			goto bad;
27936Sbill 		u.u_cdmap = zdmap;
28036Sbill 		u.u_csmap = zdmap;
28136Sbill 		if (swpexpand(ds, ss, &u.u_cdmap, &u.u_csmap) == NULL)
28236Sbill 			goto bad;
28336Sbill 
28436Sbill 		/*
28536Sbill 		 * At this point, committed to the new image!
28636Sbill 		 * Release virtual memory resources of old process, and
28736Sbill 		 * initialize the virtual memory of the new process.
28836Sbill 		 * If we resulted from vfork(), instead wakeup our
28936Sbill 		 * parent who will set SVFDONE when he has taken back
29036Sbill 		 * our resources.
29136Sbill 		 */
29236Sbill 		u.u_prof.pr_scale = 0;
29336Sbill 		if ((u.u_procp->p_flag & SVFORK) == 0)
29436Sbill 			vrelvm();
29536Sbill 		else {
29636Sbill 			u.u_procp->p_flag &= ~SVFORK;
29736Sbill 			u.u_procp->p_flag |= SKEEP;
29836Sbill 			wakeup((caddr_t)u.u_procp);
29936Sbill 			while ((u.u_procp->p_flag & SVFDONE) == 0)
30036Sbill 				sleep((caddr_t)u.u_procp, PZERO - 1);
30136Sbill 			u.u_procp->p_flag &= ~(SVFDONE|SKEEP);
30236Sbill 		}
30336Sbill 		u.u_procp->p_flag &= ~(SPAGI|SANOM|SUANOM);
30436Sbill 		u.u_procp->p_flag |= pagi;
30536Sbill 		u.u_dmap = u.u_cdmap;
30636Sbill 		u.u_smap = u.u_csmap;
30736Sbill 		vgetvm(ts, ds, ss);
30836Sbill 
30936Sbill 		if (pagi == 0) {
31036Sbill 			/*
31136Sbill 			 * Read in data segment.
31236Sbill 			 */
31336Sbill 			u.u_base = (char *)ctob(ts);
31436Sbill 			u.u_offset = sizeof(u.u_exdata)+u.u_exdata.ux_tsize;
31536Sbill 			u.u_count = u.u_exdata.ux_dsize;
31636Sbill 			readi(ip);
31736Sbill 		}
31836Sbill 		xalloc(ip, pagi);
31936Sbill 		if (pagi && u.u_procp->p_textp)
32036Sbill 			vinifod((struct fpte *)dptopte(u.u_procp, 0),
32136Sbill 			    PG_FTEXT, u.u_procp->p_textp->x_iptr,
32236Sbill 			    1 + ts/CLSIZE, (int)btoc(u.u_exdata.ux_dsize));
32336Sbill 
32436Sbill 		/* THIS SHOULD BE DONE AT A LOWER LEVEL, IF AT ALL */
325331Sbill 		mtpr(TBIA, 0);
32636Sbill 
32736Sbill 		/*
32836Sbill 		 * set SUID/SGID protections, if no tracing
32936Sbill 		 */
33036Sbill 		if ((u.u_procp->p_flag&STRC)==0) {
33136Sbill 			if(ip->i_mode&ISUID)
33236Sbill 				if(u.u_uid != 0) {
33336Sbill 					u.u_uid = ip->i_uid;
33436Sbill 					u.u_procp->p_uid = ip->i_uid;
33536Sbill 				}
33636Sbill 			if(ip->i_mode&ISGID)
33736Sbill 				u.u_gid = ip->i_gid;
33836Sbill 		} else
339173Sbill 			psignal(u.u_procp, SIGTRAP);
34036Sbill 	}
34136Sbill 	u.u_tsize = ts;
34236Sbill 	u.u_dsize = ds;
34336Sbill 	u.u_ssize = ss;
34436Sbill 	u.u_sep = sep;
34536Sbill bad:
34636Sbill 	return(overlay);
34736Sbill }
34836Sbill 
34936Sbill /*
35036Sbill  * Clear registers on exec
35136Sbill  */
35236Sbill setregs()
35336Sbill {
354173Sbill 	register int (**rp)();
35536Sbill 	register i;
356188Sbill 	long sigmask;
35736Sbill 
358188Sbill 	for(rp = &u.u_signal[0], sigmask = 1L; rp < &u.u_signal[NSIG];
359188Sbill 	    sigmask <<= 1, rp++) {
360188Sbill 		switch (*rp) {
361188Sbill 
362188Sbill 		case SIG_IGN:
363188Sbill 		case SIG_DFL:
364188Sbill 		case SIG_HOLD:
365188Sbill 			continue;
366188Sbill 
367188Sbill 		default:
368188Sbill 			/*
369206Sbill 			 * Normal or deferring catch; revert to default.
370188Sbill 			 */
371206Sbill 			(void) spl6();
372206Sbill 			*rp = SIG_DFL;
373188Sbill 			if ((int)*rp & 1)
374188Sbill 				u.u_procp->p_siga0 |= sigmask;
375188Sbill 			else
376188Sbill 				u.u_procp->p_siga1 &= ~sigmask;
377188Sbill 			if ((int)*rp & 2)
378188Sbill 				u.u_procp->p_siga1 |= sigmask;
379188Sbill 			else
380188Sbill 				u.u_procp->p_siga1 &= ~sigmask;
381206Sbill 			(void) spl0();
382188Sbill 			continue;
383188Sbill 		}
384188Sbill 	}
38536Sbill /*
38636Sbill 	for(rp = &u.u_ar0[0]; rp < &u.u_ar0[16];)
38736Sbill 		*rp++ = 0;
38836Sbill */
38936Sbill 	u.u_ar0[PC] = u.u_exdata.ux_entloc + 2; /* skip over entry mask */
39036Sbill 	for(i=0; i<NOFILE; i++) {
39136Sbill 		if (u.u_pofile[i]&EXCLOSE) {
39236Sbill 			closef(u.u_ofile[i]);
39336Sbill 			u.u_ofile[i] = NULL;
394188Sbill 			u.u_pofile[i] &= ~EXCLOSE;
39536Sbill 		}
39636Sbill 	}
39736Sbill 	/*
39836Sbill 	 * Remember file name for accounting.
39936Sbill 	 */
40036Sbill 	u.u_acflag &= ~AFORK;
40136Sbill 	bcopy((caddr_t)u.u_dbuf, (caddr_t)u.u_comm, DIRSIZ);
40236Sbill }
40336Sbill 
40436Sbill /*
40536Sbill  * exit system call:
40636Sbill  * pass back caller's arg
40736Sbill  */
40836Sbill rexit()
40936Sbill {
41036Sbill 	register struct a {
41136Sbill 		int	rval;
41236Sbill 	} *uap;
41336Sbill 
41436Sbill 	uap = (struct a *)u.u_ap;
41536Sbill 	exit((uap->rval & 0377) << 8);
41636Sbill }
41736Sbill 
41836Sbill /*
41936Sbill  * Release resources.
42036Sbill  * Save u. area for parent to look at.
42136Sbill  * Enter zombie state.
42236Sbill  * Wake up parent and init processes,
42336Sbill  * and dispose of children.
42436Sbill  */
42536Sbill exit(rv)
42636Sbill {
42736Sbill 	register int i;
42836Sbill 	register struct proc *p, *q;
42936Sbill 	register struct file *f;
43036Sbill 	register int x;
43136Sbill 
43236Sbill #ifdef PGINPROF
43336Sbill 	vmsizmon();
43436Sbill #endif
43536Sbill 	p = u.u_procp;
43636Sbill 	p->p_flag &= ~(STRC|SULOCK);
43736Sbill 	p->p_flag |= SWEXIT;
43836Sbill 	p->p_clktim = 0;
439188Sbill 	(void) spl6();
440188Sbill 	if ((int)SIG_IGN & 1)
441188Sbill 		p->p_siga0 = ~0;
442188Sbill 	else
443188Sbill 		p->p_siga0 = 0;
444188Sbill 	if ((int)SIG_IGN & 2)
445188Sbill 		p->p_siga1 = ~0;
446188Sbill 	else
447206Sbill 		p->p_siga1 = 0;
448188Sbill 	(void) spl0();
44936Sbill 	p->p_aveflt = 0;
45036Sbill 	for(i=0; i<NSIG; i++)
451173Sbill 		u.u_signal[i] = SIG_IGN;
45236Sbill 	/*
45336Sbill 	 * Release virtual memory.  If we resulted from
45436Sbill 	 * a vfork(), instead give the resources back to
45536Sbill 	 * the parent.
45636Sbill 	 */
45736Sbill 	if ((p->p_flag & SVFORK) == 0)
45836Sbill 		vrelvm();
45936Sbill 	else {
46036Sbill 		p->p_flag &= ~SVFORK;
46136Sbill 		wakeup((caddr_t)p);
46236Sbill 		while ((p->p_flag & SVFDONE) == 0)
46336Sbill 			sleep((caddr_t)p, PZERO - 1);
46436Sbill 		p->p_flag &= ~SVFDONE;
46536Sbill 	}
46636Sbill 	for(i=0; i<NOFILE; i++) {
46736Sbill 		f = u.u_ofile[i];
46836Sbill 		u.u_ofile[i] = NULL;
46936Sbill 		closef(f);
47036Sbill 	}
47136Sbill 	plock(u.u_cdir);
47236Sbill 	iput(u.u_cdir);
47336Sbill 	if (u.u_rdir) {
47436Sbill 		plock(u.u_rdir);
47536Sbill 		iput(u.u_rdir);
47636Sbill 	}
477363Sbill 	u.u_limit[LIM_FSIZE] = INFINITY;
47836Sbill 	acct();
47936Sbill 	vrelpt(u.u_procp);
48036Sbill 	vrelu(u.u_procp, 0);
48136Sbill 	multprog--;
482307Sbill /*	spl7();			/* clock will get mad because of overlaying */
483307Sbill 	noproc = 1;
48436Sbill 	p->p_stat = SZOMB;
48536Sbill 	i = PIDHASH(p->p_pid);
48636Sbill 	x = p - proc;
48736Sbill 	if (pidhash[i] == x)
48836Sbill 		pidhash[i] = p->p_idhash;
48936Sbill 	else {
49036Sbill 		for (i = pidhash[i]; i != 0; i = proc[i].p_idhash)
49136Sbill 			if (proc[i].p_idhash == x) {
49236Sbill 				proc[i].p_idhash = p->p_idhash;
49336Sbill 				goto done;
49436Sbill 			}
49536Sbill 		panic("exit");
49636Sbill 	}
49736Sbill done:
49836Sbill 	((struct xproc *)p)->xp_xstat = rv;		/* overlay */
49936Sbill 	((struct xproc *)p)->xp_vm = u.u_vm;		/* overlay */
50036Sbill 	vmsadd(&((struct xproc *)p)->xp_vm, &u.u_cvm);
50136Sbill 	for(q = &proc[0]; q < &proc[NPROC]; q++)
502188Sbill 		if(q->p_pptr == p) {
503188Sbill 			q->p_pptr = &proc[1];
504188Sbill 			q->p_ppid = 1;
50536Sbill 			wakeup((caddr_t)&proc[1]);
506188Sbill 			/*
507212Sbill 			 * Traced processes are killed
508188Sbill 			 * since their existence means someone is screwing up.
509354Sbill 			 * Stopped processes are sent a hangup and a continue.
510212Sbill 			 * This is designed to be ``safe'' for setuid
511212Sbill 			 * processes since they must be willing to tolerate
512212Sbill 			 * hangups anyways.
513188Sbill 			 */
514212Sbill 			if (q->p_flag&STRC) {
515188Sbill 				q->p_flag &= ~STRC;
516188Sbill 				psignal(q, SIGKILL);
517212Sbill 			} else if (q->p_stat == SSTOP) {
518212Sbill 				psignal(q, SIGHUP);
519212Sbill 				psignal(q, SIGCONT);
520188Sbill 			}
521215Sbill 			/*
522215Sbill 			 * Protect this process from future
523354Sbill 			 * tty signals, clear TSTP/TTIN/TTOU if pending,
524354Sbill 			 * and set SDETACH bit on procs.
525215Sbill 			 */
526307Sbill 			spgrp(q, -1);
52736Sbill 		}
528188Sbill 	wakeup((caddr_t)p->p_pptr);
529188Sbill 	psignal(p->p_pptr, SIGCHLD);
53036Sbill 	swtch();
53136Sbill }
53236Sbill 
53336Sbill wait()
53436Sbill {
535188Sbill 	struct vtimes vm;
536188Sbill 	struct vtimes *vp;
53736Sbill 
538188Sbill 	if ((u.u_ar0[PS] & PSL_ALLCC) != PSL_ALLCC) {
539188Sbill 		wait1(0, (struct vtimes *)0);
540188Sbill 		return;
541188Sbill 	}
542188Sbill 	vp = (struct vtimes *)u.u_ar0[R1];
543188Sbill 	wait1(u.u_ar0[R0], &vm);
544188Sbill 	if (u.u_error)
545188Sbill 		return;
546188Sbill 	(void) copyout((caddr_t)&vm, (caddr_t)vp, sizeof (struct vtimes));
54736Sbill }
54836Sbill 
54936Sbill /*
55036Sbill  * Wait system call.
55136Sbill  * Search for a terminated (zombie) child,
55236Sbill  * finally lay it to rest, and collect its status.
55336Sbill  * Look also for stopped (traced) children,
55436Sbill  * and pass back status from them.
55536Sbill  */
556188Sbill wait1(options, vp)
557188Sbill 	register options;
55836Sbill 	struct vtimes *vp;
55936Sbill {
56036Sbill 	register f;
56136Sbill 	register struct proc *p;
56236Sbill 
56336Sbill 	f = 0;
56436Sbill loop:
56536Sbill 	for(p = &proc[0]; p < &proc[NPROC]; p++)
566188Sbill 	if(p->p_pptr == u.u_procp) {
56736Sbill 		f++;
56836Sbill 		if(p->p_stat == SZOMB) {
56936Sbill 			u.u_r.r_val1 = p->p_pid;
57036Sbill 			u.u_r.r_val2 = ((struct xproc *)p)->xp_xstat;
57136Sbill 			((struct xproc *)p)->xp_xstat = 0;
57236Sbill 			if (vp)
57336Sbill 				*vp = ((struct xproc *)p)->xp_vm;
57436Sbill 			vmsadd(&u.u_cvm, &((struct xproc *)p)->xp_vm);
57536Sbill 			((struct xproc *)p)->xp_vm = zvms;
57636Sbill 			p->p_stat = NULL;
57736Sbill 			p->p_pid = 0;
57836Sbill 			p->p_ppid = 0;
579188Sbill 			p->p_pptr = 0;
58036Sbill 			p->p_sig = 0;
581188Sbill 			p->p_siga0 = 0;
582188Sbill 			p->p_siga1 = 0;
58336Sbill 			p->p_pgrp = 0;
58436Sbill 			p->p_flag = 0;
58536Sbill 			p->p_wchan = 0;
586188Sbill 			p->p_cursig = 0;
58736Sbill 			return;
58836Sbill 		}
589188Sbill 		if (p->p_stat == SSTOP && (p->p_flag&SWTED)==0 &&
590188Sbill 		    (p->p_flag&STRC || options&WUNTRACED)) {
591188Sbill 			p->p_flag |= SWTED;
592188Sbill 			u.u_r.r_val1 = p->p_pid;
593188Sbill 			u.u_r.r_val2 = (p->p_cursig<<8) | WSTOPPED;
594188Sbill 			return;
59536Sbill 		}
59636Sbill 	}
597188Sbill 	if (f==0) {
598188Sbill 		u.u_error = ECHILD;
599188Sbill 		return;
60036Sbill 	}
601188Sbill 	if (options&WNOHANG) {
602188Sbill 		u.u_r.r_val1 = 0;
603188Sbill 		return;
604188Sbill 	}
605206Sbill /*
606188Sbill 	if (setjmp(u.u_qsav)) {
607188Sbill 		u.u_eosys = RESTARTSYS;
608188Sbill 		return;
609188Sbill 	}
610206Sbill */
611188Sbill 	sleep((caddr_t)u.u_procp, PWAIT);
612188Sbill 	goto loop;
61336Sbill }
61436Sbill 
61536Sbill /*
61636Sbill  * fork system call.
61736Sbill  */
61836Sbill fork()
61936Sbill {
62036Sbill 
62136Sbill 	u.u_cdmap = zdmap;
62236Sbill 	u.u_csmap = zdmap;
62336Sbill 	if (swpexpand(u.u_dsize, u.u_ssize, &u.u_cdmap, &u.u_csmap) == 0) {
62436Sbill 		u.u_r.r_val2 = 0;
62536Sbill 		return;
62636Sbill 	}
62736Sbill 	fork1(0);
62836Sbill }
62936Sbill 
63036Sbill fork1(isvfork)
63136Sbill {
63236Sbill 	register struct proc *p1, *p2;
63336Sbill 	register a;
63436Sbill 
63536Sbill 	a = 0;
63636Sbill 	p2 = NULL;
63736Sbill 	for(p1 = &proc[0]; p1 < &proc[NPROC]; p1++) {
63836Sbill 		if (p1->p_stat==NULL && p2==NULL)
63936Sbill 			p2 = p1;
64036Sbill 		else {
64136Sbill 			if (p1->p_uid==u.u_uid && p1->p_stat!=NULL)
64236Sbill 				a++;
64336Sbill 		}
64436Sbill 	}
64536Sbill 	/*
64636Sbill 	 * Disallow if
64736Sbill 	 *  No processes at all;
64836Sbill 	 *  not su and too many procs owned; or
64936Sbill 	 *  not su and would take last slot.
65036Sbill 	 */
65136Sbill 	if (p2==NULL || (u.u_uid!=0 && (p2==&proc[NPROC-1] || a>MAXUPRC))) {
65236Sbill 		u.u_error = EAGAIN;
65336Sbill 		if (!isvfork) {
654132Sbill 			(void) vsexpand(0, &u.u_cdmap, 1);
655132Sbill 			(void) vsexpand(0, &u.u_csmap, 1);
65636Sbill 		}
65736Sbill 		goto out;
65836Sbill 	}
65936Sbill 	p1 = u.u_procp;
66036Sbill 	if(newproc(isvfork)) {
66136Sbill 		u.u_r.r_val1 = p1->p_pid;
66236Sbill 		u.u_r.r_val2 = 1;  /* child */
66336Sbill 		u.u_start = time;
66436Sbill 		u.u_acflag = AFORK;
66536Sbill 		return;
66636Sbill 	}
66736Sbill 	u.u_r.r_val1 = p2->p_pid;
66836Sbill 
66936Sbill out:
67036Sbill 	u.u_r.r_val2 = 0;
67136Sbill }
67236Sbill 
67336Sbill /*
67436Sbill  * break system call.
67536Sbill  *  -- bad planning: "break" is a dirty word in C.
67636Sbill  */
67736Sbill sbreak()
67836Sbill {
67936Sbill 	struct a {
68036Sbill 		char	*nsiz;
68136Sbill 	};
68236Sbill 	register int n, d;
68336Sbill 
68436Sbill 	/*
68536Sbill 	 * set n to new data size
68636Sbill 	 * set d to new-old
68736Sbill 	 */
68836Sbill 
68936Sbill 	n = btoc(((struct a *)u.u_ap)->nsiz);
69036Sbill 	if (!u.u_sep)
69136Sbill 		n -= ctos(u.u_tsize) * stoc(1);
69236Sbill 	if (n < 0)
69336Sbill 		n = 0;
69436Sbill 	d = clrnd(n - u.u_dsize);
695368Sbill 	if (ctob(u.u_dsize+d) > u.u_limit[LIM_DATA]) {
696363Sbill 		u.u_error = ENOMEM;
697363Sbill 		return;
698363Sbill 	}
69936Sbill 	if (chksize(u.u_tsize, u.u_dsize+d, u.u_ssize))
70036Sbill 		return;
70136Sbill 	if (swpexpand(u.u_dsize+d, u.u_ssize, &u.u_dmap, &u.u_smap)==0)
70236Sbill 		return;
70336Sbill 	expand(d, P0BR);
70436Sbill }
705