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