xref: /csrg-svn/sys/kern/kern_proc.c (revision 879)
1*879Sbill /*	kern_proc.c	3.20	09/16/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"
20*879Sbill #include "../h/vlimit.h"
2136Sbill 
2236Sbill /*
2336Sbill  * exec system call, with and without environments.
2436Sbill  */
2536Sbill struct execa {
2636Sbill 	char	*fname;
2736Sbill 	char	**argp;
2836Sbill 	char	**envp;
2936Sbill };
3036Sbill 
3136Sbill exec()
3236Sbill {
3336Sbill 	((struct execa *)u.u_ap)->envp = NULL;
3436Sbill 	exece();
3536Sbill }
3636Sbill 
3736Sbill exece()
3836Sbill {
3936Sbill 	register nc;
4036Sbill 	register char *cp;
4136Sbill 	register struct buf *bp;
4236Sbill 	register struct execa *uap;
4336Sbill 	int na, ne, ucp, ap, c;
4436Sbill 	struct inode *ip;
4536Sbill 	swblk_t bno;
4636Sbill 
4736Sbill 	if ((ip = namei(uchar, 0)) == NULL)
4836Sbill 		return;
4936Sbill 	bno = 0;
5036Sbill 	bp = 0;
5136Sbill 	if(access(ip, IEXEC))
5236Sbill 		goto bad;
5336Sbill 	if((ip->i_mode & IFMT) != IFREG ||
5436Sbill 	   (ip->i_mode & (IEXEC|(IEXEC>>3)|(IEXEC>>6))) == 0) {
5536Sbill 		u.u_error = EACCES;
5636Sbill 		goto bad;
5736Sbill 	}
5836Sbill 	/*
5936Sbill 	 * Collect arguments on "file" in swap space.
6036Sbill 	 */
6136Sbill 	na = 0;
6236Sbill 	ne = 0;
6336Sbill 	nc = 0;
6436Sbill 	uap = (struct execa *)u.u_ap;
65307Sbill 	if ((bno = malloc(argmap, ctod(clrnd((int) btoc(NCARGS))))) == 0) {
6636Sbill 		swkill(u.u_procp, "exece");
6736Sbill 		goto bad;
6836Sbill 	}
6936Sbill 	if (bno % CLSIZE)
7036Sbill 		panic("execa malloc");
7136Sbill 	if (uap->argp) for (;;) {
7236Sbill 		ap = NULL;
7336Sbill 		if (uap->argp) {
7436Sbill 			ap = fuword((caddr_t)uap->argp);
7536Sbill 			uap->argp++;
7636Sbill 		}
7736Sbill 		if (ap==NULL && uap->envp) {
7836Sbill 			uap->argp = NULL;
7936Sbill 			if ((ap = fuword((caddr_t)uap->envp)) == NULL)
8036Sbill 				break;
8136Sbill 			uap->envp++;
8236Sbill 			ne++;
8336Sbill 		}
8436Sbill 		if (ap==NULL)
8536Sbill 			break;
8636Sbill 		na++;
8736Sbill 		if(ap == -1)
8836Sbill 			u.u_error = EFAULT;
8936Sbill 		do {
9036Sbill 			if (nc >= NCARGS-1)
9136Sbill 				u.u_error = E2BIG;
9236Sbill 			if ((c = fubyte((caddr_t)ap++)) < 0)
9336Sbill 				u.u_error = EFAULT;
9483Sbill 			if (u.u_error) {
9583Sbill 				if (bp)
9683Sbill 					brelse(bp);
9783Sbill 				bp = 0;
9836Sbill 				goto badarg;
9983Sbill 			}
10036Sbill 			if ((nc&BMASK) == 0) {
10136Sbill 				if (bp)
10236Sbill 					bdwrite(bp);
103307Sbill 				bp = getblk(argdev,
104307Sbill 				    (daddr_t)(dbtofsb(bno)+(nc>>BSHIFT)));
10536Sbill 				cp = bp->b_un.b_addr;
10636Sbill 			}
10736Sbill 			nc++;
10836Sbill 			*cp++ = c;
10936Sbill 		} while (c>0);
11036Sbill 	}
11136Sbill 	if (bp)
11236Sbill 		bdwrite(bp);
11336Sbill 	bp = 0;
11436Sbill 	nc = (nc + NBPW-1) & ~(NBPW-1);
11536Sbill 	if (getxfile(ip, nc) || u.u_error) {
11636Sbill badarg:
11736Sbill 		for (c = 0; c < nc; c += BSIZE)
118307Sbill 			if (bp = baddr(argdev, dbtofsb(bno)+(c>>BSHIFT))) {
11936Sbill 				bp->b_flags |= B_AGE;		/* throw away */
12036Sbill 				bp->b_flags &= ~B_DELWRI;	/* cancel io */
12136Sbill 				brelse(bp);
12236Sbill 				bp = 0;
12336Sbill 			}
12436Sbill 		goto bad;
12536Sbill 	}
12636Sbill 
12736Sbill 	/*
12836Sbill 	 * copy back arglist
12936Sbill 	 */
13036Sbill 
13136Sbill 	ucp = USRSTACK - nc - NBPW;
13236Sbill 	ap = ucp - na*NBPW - 3*NBPW;
13336Sbill 	u.u_ar0[SP] = ap;
134132Sbill 	(void) suword((caddr_t)ap, na-ne);
13536Sbill 	nc = 0;
13636Sbill 	for (;;) {
13736Sbill 		ap += NBPW;
13836Sbill 		if (na==ne) {
139132Sbill 			(void) suword((caddr_t)ap, 0);
14036Sbill 			ap += NBPW;
14136Sbill 		}
14236Sbill 		if (--na < 0)
14336Sbill 			break;
144132Sbill 		(void) suword((caddr_t)ap, ucp);
14536Sbill 		do {
14636Sbill 			if ((nc&BMASK) == 0) {
14736Sbill 				if (bp)
14836Sbill 					brelse(bp);
149307Sbill 				bp = bread(argdev,
150307Sbill 				    (daddr_t)(dbtofsb(bno)+(nc>>BSHIFT)));
15136Sbill 				bp->b_flags |= B_AGE;		/* throw away */
15236Sbill 				bp->b_flags &= ~B_DELWRI;	/* cancel io */
15336Sbill 				cp = bp->b_un.b_addr;
15436Sbill 			}
155132Sbill 			(void) subyte((caddr_t)ucp++, (c = *cp++));
15636Sbill 			nc++;
15736Sbill 		} while(c&0377);
15836Sbill 	}
159132Sbill 	(void) suword((caddr_t)ap, 0);
160132Sbill 	(void) suword((caddr_t)ucp, 0);
16136Sbill 	setregs();
16236Sbill bad:
16336Sbill 	if (bp)
16436Sbill 		brelse(bp);
16536Sbill 	if (bno)
166307Sbill 		mfree(argmap, ctod(clrnd((int) btoc(NCARGS))), bno);
16736Sbill 	iput(ip);
16836Sbill }
16936Sbill 
17036Sbill /*
17136Sbill  * Read in and set up memory for executed file.
17236Sbill  * Zero return is normal;
17336Sbill  * non-zero means only the text is being replaced
17436Sbill  */
17536Sbill getxfile(ip, nargc)
17636Sbill register struct inode *ip;
17736Sbill {
17836Sbill 	register sep;
17936Sbill 	register size_t ts, ds, ss;
18036Sbill 	register int overlay;
18136Sbill 	int pagi = 0;
18236Sbill 
18336Sbill 	/*
18436Sbill 	 * read in first few bytes
18536Sbill 	 * of file for segment
18636Sbill 	 * sizes:
18736Sbill 	 * ux_mag = 407/410/411/405
18836Sbill 	 *  407 is plain executable
18936Sbill 	 *  410 is RO text
19036Sbill 	 *  411 is separated ID
19136Sbill 	 *  405 is overlaid text
19236Sbill 	 *  412 is demand paged plain executable (NOT IMPLEMENTED)
19336Sbill 	 *  413 is demand paged RO text
19436Sbill 	 */
19536Sbill 
19636Sbill 	u.u_base = (caddr_t)&u.u_exdata;
19736Sbill 	u.u_count = sizeof(u.u_exdata);
19836Sbill 	u.u_offset = 0;
19936Sbill 	u.u_segflg = 1;
20036Sbill 	readi(ip);
20136Sbill 	u.u_segflg = 0;
20236Sbill 	if(u.u_error)
20336Sbill 		goto bad;
20436Sbill 	if (u.u_count!=0) {
20536Sbill 		u.u_error = ENOEXEC;
20636Sbill 		goto bad;
20736Sbill 	}
20836Sbill 	sep = 0;
20936Sbill 	overlay = 0;
21036Sbill 	switch (u.u_exdata.ux_mag) {
21136Sbill 
21236Sbill 	case 0405:
21336Sbill 		overlay++;
21436Sbill 		break;
21536Sbill 
21636Sbill 	case 0412:
21736Sbill 		u.u_error = ENOEXEC;
21836Sbill 		goto bad;
21936Sbill 
22036Sbill 	case 0407:
22136Sbill 		u.u_exdata.ux_dsize += u.u_exdata.ux_tsize;
22236Sbill 		u.u_exdata.ux_tsize = 0;
22336Sbill 		break;
22436Sbill 
22536Sbill 	case 0413:
22636Sbill 		pagi = SPAGI;
22736Sbill 		/* fall into ... */
22836Sbill 
22936Sbill 	case 0410:
23036Sbill 		if (u.u_exdata.ux_tsize == 0) {
23136Sbill 			u.u_error = ENOEXEC;
23236Sbill 			goto bad;
23336Sbill 		}
23436Sbill 		break;
23536Sbill 
23636Sbill 	case 0411:
23736Sbill 		u.u_error = ENOEXEC;
23836Sbill 		goto bad;
23936Sbill 
24036Sbill 	default:
24136Sbill 		u.u_error = ENOEXEC;
24236Sbill 		goto bad;
24336Sbill 	}
24436Sbill 	if(u.u_exdata.ux_tsize!=0 && (ip->i_flag&ITEXT)==0 && ip->i_count!=1) {
24536Sbill 		u.u_error = ETXTBSY;
24636Sbill 		goto bad;
24736Sbill 	}
24836Sbill 
24936Sbill 	/*
25036Sbill 	 * find text and data sizes
25136Sbill 	 * try them out for possible
25236Sbill 	 * exceed of max sizes
25336Sbill 	 */
25436Sbill 
25536Sbill 	ts = clrnd(btoc(u.u_exdata.ux_tsize));
25636Sbill 	ds = clrnd(btoc((u.u_exdata.ux_dsize+u.u_exdata.ux_bsize)));
25736Sbill 	ss = clrnd(SSIZE + btoc(nargc));
25836Sbill 	if (overlay) {
259215Sbill 		if ((u.u_procp->p_flag & SPAGI) ||
260215Sbill 		    u.u_sep==0 && ctos(ts) != ctos(u.u_tsize) || nargc) {
26136Sbill 			u.u_error = ENOMEM;
26236Sbill 			goto bad;
26336Sbill 		}
26436Sbill 		ds = u.u_dsize;
26536Sbill 		ss = u.u_ssize;
26636Sbill 		sep = u.u_sep;
26736Sbill 		xfree();
26836Sbill 		xalloc(ip, pagi);
26936Sbill 		u.u_ar0[PC] = u.u_exdata.ux_entloc + 2; /* skip over entry mask */
27036Sbill 	} else {
27136Sbill 		if (chksize(ts, ds, ss))
27236Sbill 			goto bad;
27336Sbill 		u.u_cdmap = zdmap;
27436Sbill 		u.u_csmap = zdmap;
27536Sbill 		if (swpexpand(ds, ss, &u.u_cdmap, &u.u_csmap) == NULL)
27636Sbill 			goto bad;
27736Sbill 
27836Sbill 		/*
27936Sbill 		 * At this point, committed to the new image!
28036Sbill 		 * Release virtual memory resources of old process, and
28136Sbill 		 * initialize the virtual memory of the new process.
28236Sbill 		 * If we resulted from vfork(), instead wakeup our
28336Sbill 		 * parent who will set SVFDONE when he has taken back
28436Sbill 		 * our resources.
28536Sbill 		 */
28636Sbill 		u.u_prof.pr_scale = 0;
28736Sbill 		if ((u.u_procp->p_flag & SVFORK) == 0)
28836Sbill 			vrelvm();
28936Sbill 		else {
29036Sbill 			u.u_procp->p_flag &= ~SVFORK;
29136Sbill 			u.u_procp->p_flag |= SKEEP;
29236Sbill 			wakeup((caddr_t)u.u_procp);
29336Sbill 			while ((u.u_procp->p_flag & SVFDONE) == 0)
29436Sbill 				sleep((caddr_t)u.u_procp, PZERO - 1);
29536Sbill 			u.u_procp->p_flag &= ~(SVFDONE|SKEEP);
29636Sbill 		}
29736Sbill 		u.u_procp->p_flag &= ~(SPAGI|SANOM|SUANOM);
29836Sbill 		u.u_procp->p_flag |= pagi;
29936Sbill 		u.u_dmap = u.u_cdmap;
30036Sbill 		u.u_smap = u.u_csmap;
30136Sbill 		vgetvm(ts, ds, ss);
30236Sbill 
30336Sbill 		if (pagi == 0) {
30436Sbill 			/*
30536Sbill 			 * Read in data segment.
30636Sbill 			 */
30736Sbill 			u.u_base = (char *)ctob(ts);
30836Sbill 			u.u_offset = sizeof(u.u_exdata)+u.u_exdata.ux_tsize;
30936Sbill 			u.u_count = u.u_exdata.ux_dsize;
31036Sbill 			readi(ip);
31136Sbill 		}
31236Sbill 		xalloc(ip, pagi);
31336Sbill 		if (pagi && u.u_procp->p_textp)
31436Sbill 			vinifod((struct fpte *)dptopte(u.u_procp, 0),
31536Sbill 			    PG_FTEXT, u.u_procp->p_textp->x_iptr,
31636Sbill 			    1 + ts/CLSIZE, (int)btoc(u.u_exdata.ux_dsize));
31736Sbill 
31836Sbill 		/* THIS SHOULD BE DONE AT A LOWER LEVEL, IF AT ALL */
319331Sbill 		mtpr(TBIA, 0);
32036Sbill 
32136Sbill 		/*
32236Sbill 		 * set SUID/SGID protections, if no tracing
32336Sbill 		 */
32436Sbill 		if ((u.u_procp->p_flag&STRC)==0) {
32536Sbill 			if(ip->i_mode&ISUID)
32636Sbill 				if(u.u_uid != 0) {
32736Sbill 					u.u_uid = ip->i_uid;
32836Sbill 					u.u_procp->p_uid = ip->i_uid;
32936Sbill 				}
33036Sbill 			if(ip->i_mode&ISGID)
33136Sbill 				u.u_gid = ip->i_gid;
33236Sbill 		} else
333173Sbill 			psignal(u.u_procp, SIGTRAP);
33436Sbill 	}
33536Sbill 	u.u_tsize = ts;
33636Sbill 	u.u_dsize = ds;
33736Sbill 	u.u_ssize = ss;
33836Sbill 	u.u_sep = sep;
33936Sbill bad:
34036Sbill 	return(overlay);
34136Sbill }
34236Sbill 
34336Sbill /*
34436Sbill  * Clear registers on exec
34536Sbill  */
34636Sbill setregs()
34736Sbill {
348173Sbill 	register int (**rp)();
34936Sbill 	register i;
350188Sbill 	long sigmask;
35136Sbill 
352188Sbill 	for(rp = &u.u_signal[0], sigmask = 1L; rp < &u.u_signal[NSIG];
353188Sbill 	    sigmask <<= 1, rp++) {
354188Sbill 		switch (*rp) {
355188Sbill 
356188Sbill 		case SIG_IGN:
357188Sbill 		case SIG_DFL:
358188Sbill 		case SIG_HOLD:
359188Sbill 			continue;
360188Sbill 
361188Sbill 		default:
362188Sbill 			/*
363206Sbill 			 * Normal or deferring catch; revert to default.
364188Sbill 			 */
365206Sbill 			(void) spl6();
366206Sbill 			*rp = SIG_DFL;
367188Sbill 			if ((int)*rp & 1)
368188Sbill 				u.u_procp->p_siga0 |= sigmask;
369188Sbill 			else
370188Sbill 				u.u_procp->p_siga1 &= ~sigmask;
371188Sbill 			if ((int)*rp & 2)
372188Sbill 				u.u_procp->p_siga1 |= sigmask;
373188Sbill 			else
374188Sbill 				u.u_procp->p_siga1 &= ~sigmask;
375206Sbill 			(void) spl0();
376188Sbill 			continue;
377188Sbill 		}
378188Sbill 	}
37936Sbill /*
38036Sbill 	for(rp = &u.u_ar0[0]; rp < &u.u_ar0[16];)
38136Sbill 		*rp++ = 0;
38236Sbill */
38336Sbill 	u.u_ar0[PC] = u.u_exdata.ux_entloc + 2; /* skip over entry mask */
38436Sbill 	for(i=0; i<NOFILE; i++) {
38536Sbill 		if (u.u_pofile[i]&EXCLOSE) {
38636Sbill 			closef(u.u_ofile[i]);
38736Sbill 			u.u_ofile[i] = NULL;
388188Sbill 			u.u_pofile[i] &= ~EXCLOSE;
38936Sbill 		}
39036Sbill 	}
39136Sbill 	/*
39236Sbill 	 * Remember file name for accounting.
39336Sbill 	 */
39436Sbill 	u.u_acflag &= ~AFORK;
39536Sbill 	bcopy((caddr_t)u.u_dbuf, (caddr_t)u.u_comm, DIRSIZ);
39636Sbill }
39736Sbill 
39836Sbill /*
39936Sbill  * exit system call:
40036Sbill  * pass back caller's arg
40136Sbill  */
40236Sbill rexit()
40336Sbill {
40436Sbill 	register struct a {
40536Sbill 		int	rval;
40636Sbill 	} *uap;
40736Sbill 
40836Sbill 	uap = (struct a *)u.u_ap;
40936Sbill 	exit((uap->rval & 0377) << 8);
41036Sbill }
41136Sbill 
41236Sbill /*
41336Sbill  * Release resources.
41436Sbill  * Save u. area for parent to look at.
41536Sbill  * Enter zombie state.
41636Sbill  * Wake up parent and init processes,
41736Sbill  * and dispose of children.
41836Sbill  */
41936Sbill exit(rv)
42036Sbill {
42136Sbill 	register int i;
42236Sbill 	register struct proc *p, *q;
42336Sbill 	register struct file *f;
42436Sbill 	register int x;
42536Sbill 
42636Sbill #ifdef PGINPROF
42736Sbill 	vmsizmon();
42836Sbill #endif
42936Sbill 	p = u.u_procp;
43036Sbill 	p->p_flag &= ~(STRC|SULOCK);
43136Sbill 	p->p_flag |= SWEXIT;
43236Sbill 	p->p_clktim = 0;
433188Sbill 	(void) spl6();
434188Sbill 	if ((int)SIG_IGN & 1)
435188Sbill 		p->p_siga0 = ~0;
436188Sbill 	else
437188Sbill 		p->p_siga0 = 0;
438188Sbill 	if ((int)SIG_IGN & 2)
439188Sbill 		p->p_siga1 = ~0;
440188Sbill 	else
441206Sbill 		p->p_siga1 = 0;
442188Sbill 	(void) spl0();
44336Sbill 	p->p_aveflt = 0;
44436Sbill 	for(i=0; i<NSIG; i++)
445173Sbill 		u.u_signal[i] = SIG_IGN;
44636Sbill 	/*
44736Sbill 	 * Release virtual memory.  If we resulted from
44836Sbill 	 * a vfork(), instead give the resources back to
44936Sbill 	 * the parent.
45036Sbill 	 */
45136Sbill 	if ((p->p_flag & SVFORK) == 0)
45236Sbill 		vrelvm();
45336Sbill 	else {
45436Sbill 		p->p_flag &= ~SVFORK;
45536Sbill 		wakeup((caddr_t)p);
45636Sbill 		while ((p->p_flag & SVFDONE) == 0)
45736Sbill 			sleep((caddr_t)p, PZERO - 1);
45836Sbill 		p->p_flag &= ~SVFDONE;
45936Sbill 	}
46036Sbill 	for(i=0; i<NOFILE; i++) {
46136Sbill 		f = u.u_ofile[i];
46236Sbill 		u.u_ofile[i] = NULL;
46336Sbill 		closef(f);
46436Sbill 	}
46536Sbill 	plock(u.u_cdir);
46636Sbill 	iput(u.u_cdir);
46736Sbill 	if (u.u_rdir) {
46836Sbill 		plock(u.u_rdir);
46936Sbill 		iput(u.u_rdir);
47036Sbill 	}
471363Sbill 	u.u_limit[LIM_FSIZE] = INFINITY;
47236Sbill 	acct();
47336Sbill 	vrelpt(u.u_procp);
47436Sbill 	vrelu(u.u_procp, 0);
47536Sbill 	multprog--;
476307Sbill /*	spl7();			/* clock will get mad because of overlaying */
477307Sbill 	noproc = 1;
47836Sbill 	p->p_stat = SZOMB;
47936Sbill 	i = PIDHASH(p->p_pid);
48036Sbill 	x = p - proc;
48136Sbill 	if (pidhash[i] == x)
48236Sbill 		pidhash[i] = p->p_idhash;
48336Sbill 	else {
48436Sbill 		for (i = pidhash[i]; i != 0; i = proc[i].p_idhash)
48536Sbill 			if (proc[i].p_idhash == x) {
48636Sbill 				proc[i].p_idhash = p->p_idhash;
48736Sbill 				goto done;
48836Sbill 			}
48936Sbill 		panic("exit");
49036Sbill 	}
49136Sbill done:
49236Sbill 	((struct xproc *)p)->xp_xstat = rv;		/* overlay */
49336Sbill 	((struct xproc *)p)->xp_vm = u.u_vm;		/* overlay */
49436Sbill 	vmsadd(&((struct xproc *)p)->xp_vm, &u.u_cvm);
49536Sbill 	for(q = &proc[0]; q < &proc[NPROC]; q++)
496188Sbill 		if(q->p_pptr == p) {
497188Sbill 			q->p_pptr = &proc[1];
498188Sbill 			q->p_ppid = 1;
49936Sbill 			wakeup((caddr_t)&proc[1]);
500188Sbill 			/*
501212Sbill 			 * Traced processes are killed
502188Sbill 			 * since their existence means someone is screwing up.
503354Sbill 			 * Stopped processes are sent a hangup and a continue.
504212Sbill 			 * This is designed to be ``safe'' for setuid
505212Sbill 			 * processes since they must be willing to tolerate
506212Sbill 			 * hangups anyways.
507188Sbill 			 */
508212Sbill 			if (q->p_flag&STRC) {
509188Sbill 				q->p_flag &= ~STRC;
510188Sbill 				psignal(q, SIGKILL);
511212Sbill 			} else if (q->p_stat == SSTOP) {
512212Sbill 				psignal(q, SIGHUP);
513212Sbill 				psignal(q, SIGCONT);
514188Sbill 			}
515215Sbill 			/*
516215Sbill 			 * Protect this process from future
517354Sbill 			 * tty signals, clear TSTP/TTIN/TTOU if pending,
518354Sbill 			 * and set SDETACH bit on procs.
519215Sbill 			 */
520307Sbill 			spgrp(q, -1);
52136Sbill 		}
522188Sbill 	wakeup((caddr_t)p->p_pptr);
523188Sbill 	psignal(p->p_pptr, SIGCHLD);
52436Sbill 	swtch();
52536Sbill }
52636Sbill 
52736Sbill wait()
52836Sbill {
529188Sbill 	struct vtimes vm;
530188Sbill 	struct vtimes *vp;
53136Sbill 
532188Sbill 	if ((u.u_ar0[PS] & PSL_ALLCC) != PSL_ALLCC) {
533188Sbill 		wait1(0, (struct vtimes *)0);
534188Sbill 		return;
535188Sbill 	}
536188Sbill 	vp = (struct vtimes *)u.u_ar0[R1];
537188Sbill 	wait1(u.u_ar0[R0], &vm);
538188Sbill 	if (u.u_error)
539188Sbill 		return;
540188Sbill 	(void) copyout((caddr_t)&vm, (caddr_t)vp, sizeof (struct vtimes));
54136Sbill }
54236Sbill 
54336Sbill /*
54436Sbill  * Wait system call.
54536Sbill  * Search for a terminated (zombie) child,
54636Sbill  * finally lay it to rest, and collect its status.
54736Sbill  * Look also for stopped (traced) children,
54836Sbill  * and pass back status from them.
54936Sbill  */
550188Sbill wait1(options, vp)
551188Sbill 	register options;
55236Sbill 	struct vtimes *vp;
55336Sbill {
55436Sbill 	register f;
55536Sbill 	register struct proc *p;
55636Sbill 
55736Sbill 	f = 0;
55836Sbill loop:
55936Sbill 	for(p = &proc[0]; p < &proc[NPROC]; p++)
560188Sbill 	if(p->p_pptr == u.u_procp) {
56136Sbill 		f++;
56236Sbill 		if(p->p_stat == SZOMB) {
56336Sbill 			u.u_r.r_val1 = p->p_pid;
56436Sbill 			u.u_r.r_val2 = ((struct xproc *)p)->xp_xstat;
56536Sbill 			((struct xproc *)p)->xp_xstat = 0;
56636Sbill 			if (vp)
56736Sbill 				*vp = ((struct xproc *)p)->xp_vm;
56836Sbill 			vmsadd(&u.u_cvm, &((struct xproc *)p)->xp_vm);
56936Sbill 			((struct xproc *)p)->xp_vm = zvms;
57036Sbill 			p->p_stat = NULL;
57136Sbill 			p->p_pid = 0;
57236Sbill 			p->p_ppid = 0;
573188Sbill 			p->p_pptr = 0;
57436Sbill 			p->p_sig = 0;
575188Sbill 			p->p_siga0 = 0;
576188Sbill 			p->p_siga1 = 0;
57736Sbill 			p->p_pgrp = 0;
57836Sbill 			p->p_flag = 0;
57936Sbill 			p->p_wchan = 0;
580188Sbill 			p->p_cursig = 0;
58136Sbill 			return;
58236Sbill 		}
583188Sbill 		if (p->p_stat == SSTOP && (p->p_flag&SWTED)==0 &&
584188Sbill 		    (p->p_flag&STRC || options&WUNTRACED)) {
585188Sbill 			p->p_flag |= SWTED;
586188Sbill 			u.u_r.r_val1 = p->p_pid;
587188Sbill 			u.u_r.r_val2 = (p->p_cursig<<8) | WSTOPPED;
588188Sbill 			return;
58936Sbill 		}
59036Sbill 	}
591188Sbill 	if (f==0) {
592188Sbill 		u.u_error = ECHILD;
593188Sbill 		return;
59436Sbill 	}
595188Sbill 	if (options&WNOHANG) {
596188Sbill 		u.u_r.r_val1 = 0;
597188Sbill 		return;
598188Sbill 	}
599206Sbill /*
600188Sbill 	if (setjmp(u.u_qsav)) {
601188Sbill 		u.u_eosys = RESTARTSYS;
602188Sbill 		return;
603188Sbill 	}
604206Sbill */
605188Sbill 	sleep((caddr_t)u.u_procp, PWAIT);
606188Sbill 	goto loop;
60736Sbill }
60836Sbill 
60936Sbill /*
61036Sbill  * fork system call.
61136Sbill  */
61236Sbill fork()
61336Sbill {
61436Sbill 
61536Sbill 	u.u_cdmap = zdmap;
61636Sbill 	u.u_csmap = zdmap;
61736Sbill 	if (swpexpand(u.u_dsize, u.u_ssize, &u.u_cdmap, &u.u_csmap) == 0) {
61836Sbill 		u.u_r.r_val2 = 0;
61936Sbill 		return;
62036Sbill 	}
62136Sbill 	fork1(0);
62236Sbill }
62336Sbill 
62436Sbill fork1(isvfork)
62536Sbill {
62636Sbill 	register struct proc *p1, *p2;
62736Sbill 	register a;
62836Sbill 
62936Sbill 	a = 0;
63036Sbill 	p2 = NULL;
63136Sbill 	for(p1 = &proc[0]; p1 < &proc[NPROC]; p1++) {
63236Sbill 		if (p1->p_stat==NULL && p2==NULL)
63336Sbill 			p2 = p1;
63436Sbill 		else {
63536Sbill 			if (p1->p_uid==u.u_uid && p1->p_stat!=NULL)
63636Sbill 				a++;
63736Sbill 		}
63836Sbill 	}
63936Sbill 	/*
64036Sbill 	 * Disallow if
64136Sbill 	 *  No processes at all;
64236Sbill 	 *  not su and too many procs owned; or
64336Sbill 	 *  not su and would take last slot.
64436Sbill 	 */
64536Sbill 	if (p2==NULL || (u.u_uid!=0 && (p2==&proc[NPROC-1] || a>MAXUPRC))) {
64636Sbill 		u.u_error = EAGAIN;
64736Sbill 		if (!isvfork) {
648132Sbill 			(void) vsexpand(0, &u.u_cdmap, 1);
649132Sbill 			(void) vsexpand(0, &u.u_csmap, 1);
65036Sbill 		}
65136Sbill 		goto out;
65236Sbill 	}
65336Sbill 	p1 = u.u_procp;
65436Sbill 	if(newproc(isvfork)) {
65536Sbill 		u.u_r.r_val1 = p1->p_pid;
65636Sbill 		u.u_r.r_val2 = 1;  /* child */
65736Sbill 		u.u_start = time;
65836Sbill 		u.u_acflag = AFORK;
65936Sbill 		return;
66036Sbill 	}
66136Sbill 	u.u_r.r_val1 = p2->p_pid;
66236Sbill 
66336Sbill out:
66436Sbill 	u.u_r.r_val2 = 0;
66536Sbill }
66636Sbill 
66736Sbill /*
66836Sbill  * break system call.
66936Sbill  *  -- bad planning: "break" is a dirty word in C.
67036Sbill  */
67136Sbill sbreak()
67236Sbill {
67336Sbill 	struct a {
67436Sbill 		char	*nsiz;
67536Sbill 	};
67636Sbill 	register int n, d;
67736Sbill 
67836Sbill 	/*
67936Sbill 	 * set n to new data size
68036Sbill 	 * set d to new-old
68136Sbill 	 */
68236Sbill 
68336Sbill 	n = btoc(((struct a *)u.u_ap)->nsiz);
68436Sbill 	if (!u.u_sep)
68536Sbill 		n -= ctos(u.u_tsize) * stoc(1);
68636Sbill 	if (n < 0)
68736Sbill 		n = 0;
68836Sbill 	d = clrnd(n - u.u_dsize);
689368Sbill 	if (ctob(u.u_dsize+d) > u.u_limit[LIM_DATA]) {
690363Sbill 		u.u_error = ENOMEM;
691363Sbill 		return;
692363Sbill 	}
69336Sbill 	if (chksize(u.u_tsize, u.u_dsize+d, u.u_ssize))
69436Sbill 		return;
69536Sbill 	if (swpexpand(u.u_dsize+d, u.u_ssize, &u.u_dmap, &u.u_smap)==0)
69636Sbill 		return;
69736Sbill 	expand(d, P0BR);
69836Sbill }
699