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