xref: /csrg-svn/sys/kern/kern_proc.c (revision 7497)
1 /*	kern_proc.c	4.28	82/07/24	*/
2 
3 #include "../h/param.h"
4 #include "../h/systm.h"
5 #include "../h/map.h"
6 #include "../h/mtpr.h"
7 #include "../h/dir.h"
8 #include "../h/user.h"
9 #include "../h/proc.h"
10 #include "../h/buf.h"
11 #include "../h/reg.h"
12 #include "../h/inode.h"
13 #include "../h/seg.h"
14 #include "../h/acct.h"
15 #include "/usr/include/wait.h"
16 #include "../h/pte.h"
17 #include "../h/vm.h"
18 #include "../h/text.h"
19 #include "../h/psl.h"
20 #include "../h/vlimit.h"
21 #include "../h/file.h"
22 #include "../h/quota.h"
23 #include "../h/descrip.h"
24 
25 /*
26  * exec system call, with and without environments.
27  */
28 struct execa {
29 	char	*fname;
30 	char	**argp;
31 	char	**envp;
32 };
33 
34 exec()
35 {
36 	((struct execa *)u.u_ap)->envp = NULL;
37 	exece();
38 }
39 
40 exece()
41 {
42 	register nc;
43 	register char *cp;
44 	register struct buf *bp;
45 	register struct execa *uap;
46 	int na, ne, ucp, ap, c;
47 	int indir, uid, gid;
48 	char *sharg;
49 	struct inode *ip;
50 	swblk_t bno;
51 	char cfname[MAXNAMLEN + 1];
52 	char cfarg[SHSIZE];
53 
54 	if ((ip = namei(uchar, 0, 1)) == NULL)
55 		return;
56 	bno = 0;
57 	bp = 0;
58 	indir = 0;
59 	uid = u.u_uid;
60 	gid = u.u_gid;
61 	if (ip->i_mode & ISUID)
62 		uid = ip->i_uid;
63 	if (ip->i_mode & ISGID)
64 		gid = ip->i_gid;
65 
66   again:
67 	if (access(ip, IEXEC))
68 		goto bad;
69 	if ((u.u_procp->p_flag&STRC) && access(ip, IREAD))
70 		goto bad;
71 	if ((ip->i_mode & IFMT) != IFREG ||
72 	   (ip->i_mode & (IEXEC|(IEXEC>>3)|(IEXEC>>6))) == 0) {
73 		u.u_error = EACCES;
74 		goto bad;
75 	}
76 
77 	/*
78 	 * Read in first few bytes of file for segment sizes, ux_mag:
79 	 *	407 = plain executable
80 	 *	410 = RO text
81 	 *	413 = demand paged RO text
82 	 * Also an ASCII line beginning with #! is
83 	 * the file name of a ``shell'' and arguments may be prepended
84 	 * to the argument list if given here.
85 	 *
86 	 * SHELL NAMES ARE LIMITED IN LENGTH.
87 	 *
88 	 * ONLY ONE ARGUMENT MAY BE PASSED TO THE SHELL FROM
89 	 * THE ASCII LINE.
90 	 */
91 	u.u_base = (caddr_t)&u.u_exdata;
92 	u.u_count = sizeof(u.u_exdata);
93 	u.u_offset = 0;
94 	u.u_segflg = 1;
95 	readi(ip);
96 	u.u_segflg = 0;
97 	if (u.u_error)
98 		goto bad;
99 	if (u.u_count > sizeof(u.u_exdata) - sizeof(u.u_exdata.Ux_A) &&
100 	    u.u_exdata.ux_shell[0] != '#') {
101 		u.u_error = ENOEXEC;
102 		goto bad;
103 	}
104 	switch (u.u_exdata.ux_mag) {
105 
106 	case 0407:
107 		u.u_exdata.ux_dsize += u.u_exdata.ux_tsize;
108 		u.u_exdata.ux_tsize = 0;
109 		break;
110 
111 	case 0413:
112 	case 0410:
113 		if (u.u_exdata.ux_tsize == 0) {
114 			u.u_error = ENOEXEC;
115 			goto bad;
116 		}
117 		break;
118 
119 	default:
120 		if (u.u_exdata.ux_shell[0] != '#' ||
121 		    u.u_exdata.ux_shell[1] != '!' ||
122 		    indir) {
123 			u.u_error = ENOEXEC;
124 			goto bad;
125 		}
126 		cp = &u.u_exdata.ux_shell[2];		/* skip "#!" */
127 		while (cp < &u.u_exdata.ux_shell[SHSIZE]) {
128 			if (*cp == '\t')
129 				*cp = ' ';
130 			else if (*cp == '\n') {
131 				*cp = '\0';
132 				break;
133 			}
134 			cp++;
135 		}
136 		if (*cp != '\0') {
137 			u.u_error = ENOEXEC;
138 			goto bad;
139 		}
140 		cp = &u.u_exdata.ux_shell[2];
141 		while (*cp == ' ')
142 			cp++;
143 		u.u_dirp = cp;
144 		while (*cp && *cp != ' ')
145 			cp++;
146 		sharg = NULL;
147 		if (*cp) {
148 			*cp++ = '\0';
149 			while (*cp == ' ')
150 				cp++;
151 			if (*cp) {
152 				bcopy((caddr_t)cp, (caddr_t)cfarg, SHSIZE);
153 				sharg = cfarg;
154 			}
155 		}
156 		bcopy((caddr_t)u.u_dent.d_name, (caddr_t)cfname,
157 		    u.u_dent.d_namlen + 1);
158 		indir = 1;
159 		iput(ip);
160 		ip = namei(schar, 0, 1);
161 		if (ip == NULL)
162 			return;
163 		goto again;
164 	}
165 
166 	/*
167 	 * Collect arguments on "file" in swap space.
168 	 */
169 	na = 0;
170 	ne = 0;
171 	nc = 0;
172 	uap = (struct execa *)u.u_ap;
173 	if ((bno = rmalloc(argmap, ctod(clrnd((int) btoc(NCARGS))))) == 0) {
174 		swkill(u.u_procp, "exece");
175 		goto bad;
176 	}
177 	if (bno % CLSIZE)
178 		panic("execa rmalloc");
179 	if (uap->argp) for (;;) {
180 		ap = NULL;
181 		if (indir && (na == 1 || na == 2 && sharg))
182 			ap = (int)uap->fname;
183 		else if (uap->argp) {
184 			ap = fuword((caddr_t)uap->argp);
185 			uap->argp++;
186 		}
187 		if (ap==NULL && uap->envp) {
188 			uap->argp = NULL;
189 			if ((ap = fuword((caddr_t)uap->envp)) == NULL)
190 				break;
191 			uap->envp++;
192 			ne++;
193 		}
194 		if (ap == NULL)
195 			break;
196 		na++;
197 		if (ap == -1)
198 			u.u_error = EFAULT;
199 		do {
200 			if (nc >= NCARGS-1)
201 				u.u_error = E2BIG;
202 			if (indir && na == 2 && sharg != NULL)
203 				c = *sharg++ & 0377;
204 			else if ((c = fubyte((caddr_t)ap++)) < 0)
205 				u.u_error = EFAULT;
206 			if (u.u_error) {
207 				if (bp)
208 					brelse(bp);
209 				bp = 0;
210 				goto badarg;
211 			}
212 			if (nc % (CLSIZE*NBPG) == 0) {
213 				if (bp)
214 					bdwrite(bp);
215 				bp = getblk(argdev, bno + nc / NBPG,
216 				    CLSIZE*NBPG);
217 				cp = bp->b_un.b_addr;
218 			}
219 			nc++;
220 			*cp++ = c;
221 		} while (c > 0);
222 	}
223 	if (bp)
224 		bdwrite(bp);
225 	bp = 0;
226 	nc = (nc + NBPW-1) & ~(NBPW-1);
227 	if (indir) {
228 		u.u_dent.d_namlen = strlen(cfname);
229 		bcopy((caddr_t)cfname, (caddr_t)u.u_dent.d_name,
230 		    u.u_dent.d_namlen + 1);
231 	}
232 	getxfile(ip, nc + (na+4)*NBPW, uid, gid);
233 	if (u.u_error) {
234 badarg:
235 		for (c = 0; c < nc; c += CLSIZE*NBPG)
236 			if (bp = baddr(argdev, bno + c / NBPG, CLSIZE*NBPG)) {
237 				bp->b_flags |= B_AGE;		/* throw away */
238 				bp->b_flags &= ~B_DELWRI;	/* cancel io */
239 				brelse(bp);
240 				bp = 0;
241 			}
242 		goto bad;
243 	}
244 
245 	/*
246 	 * copy back arglist
247 	 */
248 	ucp = USRSTACK - nc - NBPW;
249 	ap = ucp - na*NBPW - 3*NBPW;
250 	u.u_ar0[SP] = ap;
251 	(void) suword((caddr_t)ap, na-ne);
252 	nc = 0;
253 	for (;;) {
254 		ap += NBPW;
255 		if (na==ne) {
256 			(void) suword((caddr_t)ap, 0);
257 			ap += NBPW;
258 		}
259 		if (--na < 0)
260 			break;
261 		(void) suword((caddr_t)ap, ucp);
262 		do {
263 			if (nc % (CLSIZE*NBPG) == 0) {
264 				if (bp)
265 					brelse(bp);
266 				bp = bread(argdev, bno + nc / NBPG,
267 				    CLSIZE*NBPG);
268 				bp->b_flags |= B_AGE;		/* throw away */
269 				bp->b_flags &= ~B_DELWRI;	/* cancel io */
270 				cp = bp->b_un.b_addr;
271 			}
272 			(void) subyte((caddr_t)ucp++, (c = *cp++));
273 			nc++;
274 		} while(c&0377);
275 	}
276 	(void) suword((caddr_t)ap, 0);
277 	(void) suword((caddr_t)ucp, 0);
278 	setregs();
279 bad:
280 	if (bp)
281 		brelse(bp);
282 	if (bno)
283 		rmfree(argmap, ctod(clrnd((int) btoc(NCARGS))), bno);
284 	iput(ip);
285 }
286 
287 /*
288  * Read in and set up memory for executed file.
289  */
290 getxfile(ip, nargc, uid, gid)
291 register struct inode *ip;
292 {
293 	register size_t ts, ds, ss;
294 	int pagi;
295 
296 	if (u.u_exdata.ux_mag == 0413)
297 		pagi = SPAGI;
298 	else
299 		pagi = 0;
300 	if (u.u_exdata.ux_tsize!=0 && (ip->i_flag&ITEXT)==0 &&
301 	    ip->i_count!=1) {
302 		register struct file *fp;
303 
304 		for (fp = file; fp < fileNFILE; fp++) {
305 			if (fp->f_type == DTYPE_FILE &&
306 			    fp->f_inode == ip && (fp->f_flag&FWRITE)) {
307 				u.u_error = ETXTBSY;
308 				goto bad;
309 			}
310 		}
311 	}
312 
313 	/*
314 	 * Compute text and data sizes and make sure not too large.
315 	 */
316 	ts = clrnd(btoc(u.u_exdata.ux_tsize));
317 	ds = clrnd(btoc((u.u_exdata.ux_dsize+u.u_exdata.ux_bsize)));
318 	ss = clrnd(SSIZE + btoc(nargc));
319 	if (chksize(ts, ds, ss))
320 		goto bad;
321 
322 	/*
323 	 * Make sure enough space to start process.
324 	 */
325 	u.u_cdmap = zdmap;
326 	u.u_csmap = zdmap;
327 	if (swpexpand(ds, ss, &u.u_cdmap, &u.u_csmap) == NULL)
328 		goto bad;
329 
330 	/*
331 	 * At this point, committed to the new image!
332 	 * Release virtual memory resources of old process, and
333 	 * initialize the virtual memory of the new process.
334 	 * If we resulted from vfork(), instead wakeup our
335 	 * parent who will set SVFDONE when he has taken back
336 	 * our resources.
337 	 */
338 	u.u_prof.pr_scale = 0;
339 	if ((u.u_procp->p_flag & SVFORK) == 0)
340 		vrelvm();
341 	else {
342 		u.u_procp->p_flag &= ~SVFORK;
343 		u.u_procp->p_flag |= SKEEP;
344 		wakeup((caddr_t)u.u_procp);
345 		while ((u.u_procp->p_flag & SVFDONE) == 0)
346 			sleep((caddr_t)u.u_procp, PZERO - 1);
347 		u.u_procp->p_flag &= ~(SVFDONE|SKEEP);
348 	}
349 	u.u_procp->p_flag &= ~(SPAGI|SSEQL|SUANOM|SNUSIG);
350 	u.u_procp->p_flag |= pagi;
351 	u.u_dmap = u.u_cdmap;
352 	u.u_smap = u.u_csmap;
353 	vgetvm(ts, ds, ss);
354 
355 	if (pagi == 0) {
356 		/*
357 		 * Read in data segment.
358 		 */
359 		u.u_base = (char *)ctob(ts);
360 		u.u_offset = sizeof(u.u_exdata)+u.u_exdata.ux_tsize;
361 		u.u_count = u.u_exdata.ux_dsize;
362 		readi(ip);
363 	}
364 	xalloc(ip, pagi);
365 	if (pagi && u.u_procp->p_textp)
366 		vinifod((struct fpte *)dptopte(u.u_procp, 0),
367 		    PG_FTEXT, u.u_procp->p_textp->x_iptr,
368 		    1 + ts/CLSIZE, (int)btoc(u.u_exdata.ux_dsize));
369 
370 	/* THIS SHOULD BE DONE AT A LOWER LEVEL, IF AT ALL */
371 	mtpr(TBIA, 0);
372 
373 	/*
374 	 * set SUID/SGID protections, if no tracing
375 	 */
376 	if ((u.u_procp->p_flag&STRC)==0) {
377 		u.u_uid = uid;
378 		u.u_procp->p_uid = uid;
379 		u.u_gid = gid;
380 		u.u_grps[gid/(sizeof(int)*8)] |= 1 << (gid%(sizeof(int)*8));
381 	} else
382 		psignal(u.u_procp, SIGTRAP);
383 	u.u_tsize = ts;
384 	u.u_dsize = ds;
385 	u.u_ssize = ss;
386 bad:
387 	return;
388 }
389 
390 /*
391  * Clear registers on exec
392  */
393 setregs()
394 {
395 	register int (**rp)();
396 	register i;
397 	long sigmask;
398 
399 	for (rp = &u.u_signal[1], sigmask = 1L; rp < &u.u_signal[NSIG];
400 	    sigmask <<= 1, rp++) {
401 		switch (*rp) {
402 
403 		case SIG_IGN:
404 		case SIG_DFL:
405 		case SIG_HOLD:
406 			continue;
407 
408 		default:
409 			/*
410 			 * Normal or deferring catch; revert to default.
411 			 */
412 			(void) spl6();
413 			*rp = SIG_DFL;
414 			if ((int)*rp & 1)
415 				u.u_procp->p_siga0 |= sigmask;
416 			else
417 				u.u_procp->p_siga0 &= ~sigmask;
418 			if ((int)*rp & 2)
419 				u.u_procp->p_siga1 |= sigmask;
420 			else
421 				u.u_procp->p_siga1 &= ~sigmask;
422 			(void) spl0();
423 			continue;
424 		}
425 	}
426 /*
427 	for (rp = &u.u_ar0[0]; rp < &u.u_ar0[16];)
428 		*rp++ = 0;
429 */
430 	u.u_ar0[PC] = u.u_exdata.ux_entloc + 2; /* skip over entry mask */
431 	for (i=0; i<NOFILE; i++) {
432 		if (u.u_pofile[i]&EXCLOSE) {
433 			closef(u.u_ofile[i], 1);
434 			u.u_ofile[i] = NULL;
435 			u.u_pofile[i] &= ~EXCLOSE;
436 		}
437 	}
438 
439 	/*
440 	 * Remember file name for accounting.
441 	 */
442 	u.u_acflag &= ~AFORK;
443 	bcopy((caddr_t)u.u_dent.d_name, (caddr_t)u.u_comm,
444 	    u.u_dent.d_namlen + 1);
445 }
446 
447 /*
448  * Exit system call: pass back caller's arg
449  */
450 rexit()
451 {
452 	register struct a {
453 		int	rval;
454 	} *uap;
455 
456 	uap = (struct a *)u.u_ap;
457 	exit((uap->rval & 0377) << 8);
458 }
459 
460 /*
461  * Release resources.
462  * Save u. area for parent to look at.
463  * Enter zombie state.
464  * Wake up parent and init processes,
465  * and dispose of children.
466  */
467 exit(rv)
468 {
469 	register int i;
470 	register struct proc *p, *q;
471 	register struct file *f;
472 	register int x;
473 
474 #ifdef PGINPROF
475 	vmsizmon();
476 #endif
477 	p = u.u_procp;
478 	p->p_flag &= ~(STRC|SULOCK);
479 	p->p_flag |= SWEXIT;
480 	p->p_clktim = 0;
481 	(void) spl6();
482 	if ((int)SIG_IGN & 1)
483 		p->p_siga0 = ~0;
484 	else
485 		p->p_siga0 = 0;
486 	if ((int)SIG_IGN & 2)
487 		p->p_siga1 = ~0;
488 	else
489 		p->p_siga1 = 0;
490 	(void) spl0();
491 	p->p_cpticks = 0;
492 	p->p_pctcpu = 0;
493 	for (i=0; i<NSIG; i++)
494 		u.u_signal[i] = SIG_IGN;
495 	/*
496 	 * Release virtual memory.  If we resulted from
497 	 * a vfork(), instead give the resources back to
498 	 * the parent.
499 	 */
500 	if ((p->p_flag & SVFORK) == 0)
501 		vrelvm();
502 	else {
503 		p->p_flag &= ~SVFORK;
504 		wakeup((caddr_t)p);
505 		while ((p->p_flag & SVFDONE) == 0)
506 			sleep((caddr_t)p, PZERO - 1);
507 		p->p_flag &= ~SVFDONE;
508 	}
509 	for (i=0; i<NOFILE; i++) {
510 		f = u.u_ofile[i];
511 		u.u_ofile[i] = NULL;
512 		closef(f, 1);
513 	}
514 	ilock(u.u_cdir);
515 	iput(u.u_cdir);
516 	if (u.u_rdir) {
517 		ilock(u.u_rdir);
518 		iput(u.u_rdir);
519 	}
520 	u.u_limit[LIM_FSIZE] = INFINITY;
521 	acct();
522 #ifdef QUOTA
523 	qclean();
524 #endif
525 	vrelpt(u.u_procp);
526 	vrelu(u.u_procp, 0);
527 	(void) spl5();		/* hack for mem alloc race XXX */
528 	multprog--;
529 	p->p_stat = SZOMB;
530 	noproc = 1;
531 	i = PIDHASH(p->p_pid);
532 	x = p - proc;
533 	if (pidhash[i] == x)
534 		pidhash[i] = p->p_idhash;
535 	else {
536 		for (i = pidhash[i]; i != 0; i = proc[i].p_idhash)
537 			if (proc[i].p_idhash == x) {
538 				proc[i].p_idhash = p->p_idhash;
539 				goto done;
540 			}
541 		panic("exit");
542 	}
543 	if (p->p_pid == 1)
544 		panic("init died");
545 done:
546 	((struct xproc *)p)->xp_xstat = rv;		/* overlay */
547 	((struct xproc *)p)->xp_vm = u.u_vm;		/* overlay */
548 	vmsadd(&((struct xproc *)p)->xp_vm, &u.u_cvm);
549 	for (q = proc; q < procNPROC; q++)
550 		if (q->p_pptr == p) {
551 			if (q->p_osptr)
552 				q->p_osptr->p_ysptr = q->p_ysptr;
553 			if (q->p_ysptr)
554 				q->p_ysptr->p_osptr = q->p_osptr;
555 			if (proc[1].p_cptr)
556 				proc[1].p_cptr->p_ysptr = q;
557 			q->p_osptr = proc[1].p_cptr;
558 			q->p_ysptr = NULL;
559 			proc[1].p_cptr = q;
560 
561 			q->p_pptr = &proc[1];
562 			q->p_ppid = 1;
563 			wakeup((caddr_t)&proc[1]);
564 			/*
565 			 * Traced processes are killed
566 			 * since their existence means someone is screwing up.
567 			 * Stopped processes are sent a hangup and a continue.
568 			 * This is designed to be ``safe'' for setuid
569 			 * processes since they must be willing to tolerate
570 			 * hangups anyways.
571 			 */
572 			if (q->p_flag&STRC) {
573 				q->p_flag &= ~STRC;
574 				psignal(q, SIGKILL);
575 			} else if (q->p_stat == SSTOP) {
576 				psignal(q, SIGHUP);
577 				psignal(q, SIGCONT);
578 			}
579 			/*
580 			 * Protect this process from future
581 			 * tty signals, clear TSTP/TTIN/TTOU if pending.
582 			 */
583 			(void) spgrp(q, -1);
584 		}
585 	psignal(p->p_pptr, SIGCHLD);
586 	wakeup((caddr_t)p->p_pptr);
587 	swtch();
588 }
589 
590 wait()
591 {
592 	struct vtimes vm;
593 	struct vtimes *vp;
594 
595 	if ((u.u_ar0[PS] & PSL_ALLCC) != PSL_ALLCC) {
596 		wait1(0, (struct vtimes *)0);
597 		return;
598 	}
599 	vp = (struct vtimes *)u.u_ar0[R1];
600 	wait1(u.u_ar0[R0], &vm);
601 	if (u.u_error)
602 		return;
603 	(void) copyout((caddr_t)&vm, (caddr_t)vp, sizeof (struct vtimes));
604 }
605 
606 /*
607  * Wait system call.
608  * Search for a terminated (zombie) child,
609  * finally lay it to rest, and collect its status.
610  * Look also for stopped (traced) children,
611  * and pass back status from them.
612  */
613 wait1(options, vp)
614 	register options;
615 	struct vtimes *vp;
616 {
617 	register f;
618 	register struct proc *p, *q;
619 
620 	f = 0;
621 loop:
622 	for (p = proc; p < procNPROC; p++)
623 	if (p->p_pptr == u.u_procp) {
624 		f++;
625 		if (p->p_stat == SZOMB) {
626 			u.u_r.r_val1 = p->p_pid;
627 			u.u_r.r_val2 = ((struct xproc *)p)->xp_xstat;
628 			((struct xproc *)p)->xp_xstat = 0;
629 			if (vp)
630 				*vp = ((struct xproc *)p)->xp_vm;
631 			vmsadd(&u.u_cvm, &((struct xproc *)p)->xp_vm);
632 			((struct xproc *)p)->xp_vm = zvms;
633 			p->p_stat = NULL;
634 			p->p_pid = 0;
635 			p->p_ppid = 0;
636 			if (q = p->p_ysptr)
637 				q->p_osptr = p->p_osptr;
638 			if (q = p->p_osptr)
639 				q->p_ysptr = p->p_ysptr;
640 			if ((q = p->p_pptr)->p_cptr == p)
641 				q->p_cptr = p->p_osptr;
642 			p->p_pptr = 0;
643 			p->p_ysptr = 0;
644 			p->p_osptr = 0;
645 			p->p_cptr = 0;
646 			p->p_sig = 0;
647 			p->p_siga0 = 0;
648 			p->p_siga1 = 0;
649 			p->p_pgrp = 0;
650 			p->p_flag = 0;
651 			p->p_wchan = 0;
652 			p->p_cursig = 0;
653 			return;
654 		}
655 		if (p->p_stat == SSTOP && (p->p_flag&SWTED)==0 &&
656 		    (p->p_flag&STRC || options&WUNTRACED)) {
657 			p->p_flag |= SWTED;
658 			u.u_r.r_val1 = p->p_pid;
659 			u.u_r.r_val2 = (p->p_cursig<<8) | WSTOPPED;
660 			return;
661 		}
662 	}
663 	if (f==0) {
664 		u.u_error = ECHILD;
665 		return;
666 	}
667 	if (options&WNOHANG) {
668 		u.u_r.r_val1 = 0;
669 		return;
670 	}
671 	if ((u.u_procp->p_flag&SNUSIG) && setjmp(u.u_qsav)) {
672 		u.u_eosys = RESTARTSYS;
673 		return;
674 	}
675 	sleep((caddr_t)u.u_procp, PWAIT);
676 	goto loop;
677 }
678 
679 /*
680  * fork system call.
681  */
682 fork()
683 {
684 
685 	u.u_cdmap = zdmap;
686 	u.u_csmap = zdmap;
687 	if (swpexpand(u.u_dsize, u.u_ssize, &u.u_cdmap, &u.u_csmap) == 0) {
688 		u.u_r.r_val2 = 0;
689 		return;
690 	}
691 	fork1(0);
692 }
693 
694 fork1(isvfork)
695 {
696 	register struct proc *p1, *p2;
697 #ifndef	QUOTA
698 	register a;
699 
700 	a = 0;
701 #else
702 	if (u.u_quota != NOQUOT && u.u_quota->q_plim &&
703 	    u.u_quota->q_cnt >= u.u_quota->q_plim) {
704 		u.u_error = EPROCLIM;
705 		return;
706 	}
707 #endif
708 	p2 = NULL;
709 	for (p1 = proc; p1 < procNPROC; p1++) {
710 #ifdef QUOTA
711 		if (p1->p_stat == NULL) {
712 			p2 = p1;
713 			break;
714 		}
715 #else
716 		if (p1->p_stat==NULL && p2==NULL)
717 			p2 = p1;
718 		else {
719 			if (p1->p_uid==u.u_uid && p1->p_stat!=NULL)
720 				a++;
721 		}
722 #endif
723 	}
724 	/*
725 	 * Disallow if
726 	 *  No processes at all;
727 	 *  not su and too many procs owned; or
728 	 *  not su and would take last slot.
729 	 */
730 	if (p2==NULL)
731 		tablefull("proc");
732 #ifdef QUOTA
733 	if (p2==NULL || (u.u_uid!=0 && p2==procNPROC-1)) {
734 #else
735 	if (p2==NULL || (u.u_uid!=0 && (p2==procNPROC-1 || a>MAXUPRC))) {
736 #endif
737 		u.u_error = EAGAIN;
738 		if (!isvfork) {
739 			(void) vsexpand(0, &u.u_cdmap, 1);
740 			(void) vsexpand(0, &u.u_csmap, 1);
741 		}
742 		goto out;
743 	}
744 	p1 = u.u_procp;
745 	if (newproc(isvfork)) {
746 		u.u_r.r_val1 = p1->p_pid;
747 		u.u_r.r_val2 = 1;  /* child */
748 		u.u_start = time;
749 		u.u_acflag = AFORK;
750 #ifdef QUOTA
751 		u.u_qflags &= ~QUF_LOGIN;
752 #endif
753 		return;
754 	}
755 	u.u_r.r_val1 = p2->p_pid;
756 
757 out:
758 	u.u_r.r_val2 = 0;
759 }
760 
761 spgrp(top, npgrp)
762 register struct proc *top;
763 {
764 	register struct proc *pp, *p;
765 	int f = 0;
766 
767 	for (p = top; npgrp == -1 || u.u_uid == p->p_uid ||
768 	    !u.u_uid || inferior(p); p = pp) {
769 		if (npgrp == -1) {
770 #define	bit(a)	(1<<(a-1))
771 			p->p_sig &= ~(bit(SIGTSTP)|bit(SIGTTIN)|bit(SIGTTOU));
772 		} else
773 			p->p_pgrp = npgrp;
774 		f++;
775 		/*
776 		 * Search for children.
777 		 */
778 		for (pp = proc; pp < procNPROC; pp++)
779 			if (pp->p_pptr == p)
780 				goto cont;
781 		/*
782 		 * Search for siblings.
783 		 */
784 		for (; p != top; p = p->p_pptr)
785 			for (pp = p + 1; pp < procNPROC; pp++)
786 				if (pp->p_pptr == p->p_pptr)
787 					goto cont;
788 		break;
789 	cont:
790 		;
791 	}
792 	return (f);
793 }
794 
795 /*
796  * Is p an inferior of the current process?
797  */
798 inferior(p)
799 register struct proc *p;
800 {
801 
802 	for (; p != u.u_procp; p = p->p_pptr)
803 		if (p->p_ppid == 0)
804 			return (0);
805 	return (1);
806 }
807