major cleanups for socket code
[unix-history] / usr / src / sys / kern / kern_proc.c
CommitLineData
e92a04af 1/* kern_proc.c 4.13 81/11/08 */
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;
54 bno = 0;
55 bp = 0;
7eeaac77
RE
56 indir = 0;
57 uid = u.u_uid;
58 gid = u.u_gid;
7eeaac77
RE
59 if (ip->i_mode & ISUID)
60 uid = ip->i_uid;
61 if (ip->i_mode & ISGID)
62 gid = ip->i_gid;
63
64 again:
e92a04af 65 if (access(ip, IEXEC))
29dd101b 66 goto bad;
e92a04af 67 if ((u.u_procp->p_flag&STRC) && access(ip, IREAD))
eb83bf86 68 goto bad;
e92a04af 69 if ((ip->i_mode & IFMT) != IFREG ||
29dd101b
BJ
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;
e92a04af 95 if (u.u_error)
7eeaac77
RE
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;
b725a0ca 170 if ((bno = rmalloc(argmap, ctod(clrnd((int) btoc(NCARGS))))) == 0) {
29dd101b
BJ
171 swkill(u.u_procp, "exece");
172 goto bad;
173 }
174 if (bno % CLSIZE)
b725a0ca 175 panic("execa rmalloc");
29dd101b
BJ
176 if (uap->argp) for (;;) {
177 ap = NULL;
40d07ebb 178 if (indir && (na == 1 || na == 2 && sharg))
7eeaac77
RE
179 ap = (int)uap->fname;
180 else if (uap->argp) {
29dd101b
BJ
181 ap = fuword((caddr_t)uap->argp);
182 uap->argp++;
183 }
184 if (ap==NULL && uap->envp) {
185 uap->argp = NULL;
186 if ((ap = fuword((caddr_t)uap->envp)) == NULL)
187 break;
188 uap->envp++;
189 ne++;
190 }
191 if (ap==NULL)
192 break;
193 na++;
e92a04af 194 if (ap == -1)
29dd101b
BJ
195 u.u_error = EFAULT;
196 do {
197 if (nc >= NCARGS-1)
198 u.u_error = E2BIG;
7eeaac77
RE
199 if (indir && na == 2 && sharg != NULL)
200 c = *sharg++ & 0377;
201 else if ((c = fubyte((caddr_t)ap++)) < 0)
29dd101b 202 u.u_error = EFAULT;
64d6118b
BJ
203 if (u.u_error) {
204 if (bp)
205 brelse(bp);
206 bp = 0;
29dd101b 207 goto badarg;
64d6118b 208 }
29dd101b
BJ
209 if ((nc&BMASK) == 0) {
210 if (bp)
211 bdwrite(bp);
41888f16
BJ
212 bp = getblk(argdev,
213 (daddr_t)(dbtofsb(bno)+(nc>>BSHIFT)));
29dd101b
BJ
214 cp = bp->b_un.b_addr;
215 }
216 nc++;
217 *cp++ = c;
218 } while (c>0);
219 }
220 if (bp)
221 bdwrite(bp);
222 bp = 0;
223 nc = (nc + NBPW-1) & ~(NBPW-1);
7eeaac77
RE
224 if (indir)
225 bcopy((caddr_t)cfname, (caddr_t)u.u_dbuf, DIRSIZ);
226 getxfile(ip, nc + (na+4)*NBPW, uid, gid);
29a06346 227 if (u.u_error) {
29dd101b
BJ
228badarg:
229 for (c = 0; c < nc; c += BSIZE)
41888f16 230 if (bp = baddr(argdev, dbtofsb(bno)+(c>>BSHIFT))) {
29dd101b
BJ
231 bp->b_flags |= B_AGE; /* throw away */
232 bp->b_flags &= ~B_DELWRI; /* cancel io */
233 brelse(bp);
234 bp = 0;
235 }
236 goto bad;
237 }
238
239 /*
240 * copy back arglist
241 */
29dd101b
BJ
242 ucp = USRSTACK - nc - NBPW;
243 ap = ucp - na*NBPW - 3*NBPW;
244 u.u_ar0[SP] = ap;
81263dba 245 (void) suword((caddr_t)ap, na-ne);
29dd101b
BJ
246 nc = 0;
247 for (;;) {
248 ap += NBPW;
249 if (na==ne) {
81263dba 250 (void) suword((caddr_t)ap, 0);
29dd101b
BJ
251 ap += NBPW;
252 }
253 if (--na < 0)
254 break;
81263dba 255 (void) suword((caddr_t)ap, ucp);
29dd101b
BJ
256 do {
257 if ((nc&BMASK) == 0) {
258 if (bp)
259 brelse(bp);
41888f16
BJ
260 bp = bread(argdev,
261 (daddr_t)(dbtofsb(bno)+(nc>>BSHIFT)));
29dd101b
BJ
262 bp->b_flags |= B_AGE; /* throw away */
263 bp->b_flags &= ~B_DELWRI; /* cancel io */
264 cp = bp->b_un.b_addr;
265 }
81263dba 266 (void) subyte((caddr_t)ucp++, (c = *cp++));
29dd101b
BJ
267 nc++;
268 } while(c&0377);
269 }
81263dba
BJ
270 (void) suword((caddr_t)ap, 0);
271 (void) suword((caddr_t)ucp, 0);
29dd101b
BJ
272 setregs();
273bad:
274 if (bp)
275 brelse(bp);
276 if (bno)
b725a0ca 277 rmfree(argmap, ctod(clrnd((int) btoc(NCARGS))), bno);
29dd101b
BJ
278 iput(ip);
279}
280
281/*
282 * Read in and set up memory for executed file.
29dd101b 283 */
7eeaac77 284getxfile(ip, nargc, uid, gid)
29dd101b
BJ
285register struct inode *ip;
286{
29dd101b 287 register size_t ts, ds, ss;
7eeaac77 288 int pagi;
29dd101b 289
7eeaac77 290 if (u.u_exdata.ux_mag == 0413)
29dd101b 291 pagi = SPAGI;
7eeaac77
RE
292 else
293 pagi = 0;
e92a04af
BJ
294 if (u.u_exdata.ux_tsize!=0 && (ip->i_flag&ITEXT)==0 &&
295 ip->i_count!=1) {
3ca1542b
BJ
296 register struct file *fp;
297
e92a04af
BJ
298 for (fp = file; fp < fileNFILE; fp++) {
299 if (fp->f_flag & FSOCKET)
300 continue;
3ca1542b
BJ
301 if (fp->f_inode == ip && (fp->f_flag&FWRITE)) {
302 u.u_error = ETXTBSY;
303 goto bad;
304 }
e92a04af 305 }
29dd101b
BJ
306 }
307
308 /*
e92a04af 309 * Compute text and data sizes and make sure not too large.
29dd101b 310 */
29dd101b
BJ
311 ts = clrnd(btoc(u.u_exdata.ux_tsize));
312 ds = clrnd(btoc((u.u_exdata.ux_dsize+u.u_exdata.ux_bsize)));
313 ss = clrnd(SSIZE + btoc(nargc));
29a06346
BJ
314 if (chksize(ts, ds, ss))
315 goto bad;
e92a04af
BJ
316
317 /*
318 * Make sure enough space to start process.
319 */
29a06346
BJ
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 }
2ce421b6 344 u.u_procp->p_flag &= ~(SPAGI|SSEQL|SUANOM|SNUSIG);
29a06346
BJ
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) {
e92a04af
BJ
372 u.u_uid = uid;
373 u.u_procp->p_uid = uid;
7eeaac77 374 u.u_gid = gid;
29a06346
BJ
375 } else
376 psignal(u.u_procp, SIGTRAP);
29dd101b
BJ
377 u.u_tsize = ts;
378 u.u_dsize = ds;
379 u.u_ssize = ss;
29dd101b 380bad:
29a06346 381 return;
29dd101b
BJ
382}
383
384/*
385 * Clear registers on exec
386 */
387setregs()
388{
594ebedd 389 register int (**rp)();
29dd101b 390 register i;
bdfe5b0f 391 long sigmask;
29dd101b 392
e92a04af 393 for (rp = &u.u_signal[0], sigmask = 1L; rp < &u.u_signal[NSIG];
bdfe5b0f
BJ
394 sigmask <<= 1, rp++) {
395 switch (*rp) {
396
397 case SIG_IGN:
398 case SIG_DFL:
399 case SIG_HOLD:
400 continue;
401
402 default:
403 /*
99fa88a2 404 * Normal or deferring catch; revert to default.
bdfe5b0f 405 */
99fa88a2
BJ
406 (void) spl6();
407 *rp = SIG_DFL;
bdfe5b0f
BJ
408 if ((int)*rp & 1)
409 u.u_procp->p_siga0 |= sigmask;
410 else
411 u.u_procp->p_siga1 &= ~sigmask;
412 if ((int)*rp & 2)
413 u.u_procp->p_siga1 |= sigmask;
414 else
415 u.u_procp->p_siga1 &= ~sigmask;
99fa88a2 416 (void) spl0();
bdfe5b0f
BJ
417 continue;
418 }
419 }
29dd101b 420/*
e92a04af 421 for (rp = &u.u_ar0[0]; rp < &u.u_ar0[16];)
29dd101b
BJ
422 *rp++ = 0;
423*/
424 u.u_ar0[PC] = u.u_exdata.ux_entloc + 2; /* skip over entry mask */
e92a04af 425 for (i=0; i<NOFILE; i++) {
29dd101b
BJ
426 if (u.u_pofile[i]&EXCLOSE) {
427 closef(u.u_ofile[i]);
428 u.u_ofile[i] = NULL;
bdfe5b0f 429 u.u_pofile[i] &= ~EXCLOSE;
29dd101b 430 }
29dd101b 431 }
e92a04af 432
29dd101b
BJ
433 /*
434 * Remember file name for accounting.
435 */
436 u.u_acflag &= ~AFORK;
437 bcopy((caddr_t)u.u_dbuf, (caddr_t)u.u_comm, DIRSIZ);
438}
439
440/*
e92a04af 441 * Exit system call: pass back caller's arg
29dd101b
BJ
442 */
443rexit()
444{
445 register struct a {
446 int rval;
447 } *uap;
448
449 uap = (struct a *)u.u_ap;
450 exit((uap->rval & 0377) << 8);
451}
452
453/*
454 * Release resources.
455 * Save u. area for parent to look at.
456 * Enter zombie state.
457 * Wake up parent and init processes,
458 * and dispose of children.
459 */
460exit(rv)
461{
462 register int i;
463 register struct proc *p, *q;
464 register struct file *f;
465 register int x;
466
467#ifdef PGINPROF
468 vmsizmon();
469#endif
470 p = u.u_procp;
471 p->p_flag &= ~(STRC|SULOCK);
472 p->p_flag |= SWEXIT;
473 p->p_clktim = 0;
bdfe5b0f
BJ
474 (void) spl6();
475 if ((int)SIG_IGN & 1)
476 p->p_siga0 = ~0;
477 else
478 p->p_siga0 = 0;
479 if ((int)SIG_IGN & 2)
480 p->p_siga1 = ~0;
481 else
99fa88a2 482 p->p_siga1 = 0;
bdfe5b0f 483 (void) spl0();
dd808ba3
BJ
484 p->p_cpticks = 0;
485 p->p_pctcpu = 0;
e92a04af 486 for (i=0; i<NSIG; i++)
594ebedd 487 u.u_signal[i] = SIG_IGN;
29dd101b
BJ
488 /*
489 * Release virtual memory. If we resulted from
490 * a vfork(), instead give the resources back to
491 * the parent.
492 */
493 if ((p->p_flag & SVFORK) == 0)
494 vrelvm();
495 else {
496 p->p_flag &= ~SVFORK;
497 wakeup((caddr_t)p);
498 while ((p->p_flag & SVFDONE) == 0)
499 sleep((caddr_t)p, PZERO - 1);
500 p->p_flag &= ~SVFDONE;
501 }
e92a04af 502 for (i=0; i<NOFILE; i++) {
29dd101b
BJ
503 f = u.u_ofile[i];
504 u.u_ofile[i] = NULL;
505 closef(f);
506 }
e92a04af 507 ilock(u.u_cdir);
29dd101b
BJ
508 iput(u.u_cdir);
509 if (u.u_rdir) {
e92a04af 510 ilock(u.u_rdir);
29dd101b
BJ
511 iput(u.u_rdir);
512 }
054016e1 513 u.u_limit[LIM_FSIZE] = INFINITY;
29dd101b
BJ
514 acct();
515 vrelpt(u.u_procp);
516 vrelu(u.u_procp, 0);
517 multprog--;
29dd101b 518 p->p_stat = SZOMB;
42343c0f 519 noproc = 1;
29dd101b
BJ
520 i = PIDHASH(p->p_pid);
521 x = p - proc;
522 if (pidhash[i] == x)
523 pidhash[i] = p->p_idhash;
524 else {
525 for (i = pidhash[i]; i != 0; i = proc[i].p_idhash)
526 if (proc[i].p_idhash == x) {
527 proc[i].p_idhash = p->p_idhash;
528 goto done;
529 }
530 panic("exit");
531 }
9c82b9fd
BJ
532 if (p->p_pid == 1)
533 panic("init died");
29dd101b
BJ
534done:
535 ((struct xproc *)p)->xp_xstat = rv; /* overlay */
536 ((struct xproc *)p)->xp_vm = u.u_vm; /* overlay */
537 vmsadd(&((struct xproc *)p)->xp_vm, &u.u_cvm);
e92a04af
BJ
538 for (q = proc; q < procNPROC; q++)
539 if (q->p_pptr == p) {
bdfe5b0f 540 q->p_pptr = &proc[1];
29dd101b 541 q->p_ppid = 1;
bdfe5b0f
BJ
542 wakeup((caddr_t)&proc[1]);
543 /*
0dde1c43 544 * Traced processes are killed
bdfe5b0f 545 * since their existence means someone is screwing up.
62bac59f 546 * Stopped processes are sent a hangup and a continue.
0dde1c43
BJ
547 * This is designed to be ``safe'' for setuid
548 * processes since they must be willing to tolerate
549 * hangups anyways.
bdfe5b0f 550 */
0dde1c43 551 if (q->p_flag&STRC) {
bdfe5b0f
BJ
552 q->p_flag &= ~STRC;
553 psignal(q, SIGKILL);
0dde1c43
BJ
554 } else if (q->p_stat == SSTOP) {
555 psignal(q, SIGHUP);
556 psignal(q, SIGCONT);
bdfe5b0f 557 }
8643403f
BJ
558 /*
559 * Protect this process from future
62bac59f
BJ
560 * tty signals, clear TSTP/TTIN/TTOU if pending,
561 * and set SDETACH bit on procs.
8643403f 562 */
934e4ecf 563 (void) spgrp(q, -1);
29dd101b 564 }
bdfe5b0f
BJ
565 wakeup((caddr_t)p->p_pptr);
566 psignal(p->p_pptr, SIGCHLD);
29dd101b
BJ
567 swtch();
568}
569
570wait()
571{
bdfe5b0f
BJ
572 struct vtimes vm;
573 struct vtimes *vp;
29dd101b 574
bdfe5b0f
BJ
575 if ((u.u_ar0[PS] & PSL_ALLCC) != PSL_ALLCC) {
576 wait1(0, (struct vtimes *)0);
577 return;
578 }
579 vp = (struct vtimes *)u.u_ar0[R1];
580 wait1(u.u_ar0[R0], &vm);
581 if (u.u_error)
582 return;
583 (void) copyout((caddr_t)&vm, (caddr_t)vp, sizeof (struct vtimes));
29dd101b
BJ
584}
585
586/*
587 * Wait system call.
588 * Search for a terminated (zombie) child,
589 * finally lay it to rest, and collect its status.
590 * Look also for stopped (traced) children,
591 * and pass back status from them.
592 */
bdfe5b0f
BJ
593wait1(options, vp)
594 register options;
29dd101b
BJ
595 struct vtimes *vp;
596{
597 register f;
598 register struct proc *p;
599
600 f = 0;
29dd101b 601loop:
e92a04af
BJ
602 for (p = proc; p < procNPROC; p++)
603 if (p->p_pptr == u.u_procp) {
29dd101b 604 f++;
e92a04af 605 if (p->p_stat == SZOMB) {
29dd101b
BJ
606 u.u_r.r_val1 = p->p_pid;
607 u.u_r.r_val2 = ((struct xproc *)p)->xp_xstat;
608 ((struct xproc *)p)->xp_xstat = 0;
609 if (vp)
610 *vp = ((struct xproc *)p)->xp_vm;
611 vmsadd(&u.u_cvm, &((struct xproc *)p)->xp_vm);
612 ((struct xproc *)p)->xp_vm = zvms;
613 p->p_stat = NULL;
614 p->p_pid = 0;
615 p->p_ppid = 0;
bdfe5b0f 616 p->p_pptr = 0;
29dd101b 617 p->p_sig = 0;
bdfe5b0f
BJ
618 p->p_siga0 = 0;
619 p->p_siga1 = 0;
29dd101b
BJ
620 p->p_pgrp = 0;
621 p->p_flag = 0;
622 p->p_wchan = 0;
bdfe5b0f 623 p->p_cursig = 0;
29dd101b
BJ
624 return;
625 }
bdfe5b0f
BJ
626 if (p->p_stat == SSTOP && (p->p_flag&SWTED)==0 &&
627 (p->p_flag&STRC || options&WUNTRACED)) {
628 p->p_flag |= SWTED;
629 u.u_r.r_val1 = p->p_pid;
630 u.u_r.r_val2 = (p->p_cursig<<8) | WSTOPPED;
631 return;
29dd101b
BJ
632 }
633 }
bdfe5b0f
BJ
634 if (f==0) {
635 u.u_error = ECHILD;
636 return;
637 }
638 if (options&WNOHANG) {
639 u.u_r.r_val1 = 0;
640 return;
641 }
29a06346 642 if ((u.u_procp->p_flag&SNUSIG) && setjmp(u.u_qsav)) {
bdfe5b0f
BJ
643 u.u_eosys = RESTARTSYS;
644 return;
29dd101b 645 }
bdfe5b0f
BJ
646 sleep((caddr_t)u.u_procp, PWAIT);
647 goto loop;
29dd101b
BJ
648}
649
650/*
651 * fork system call.
652 */
653fork()
654{
655
656 u.u_cdmap = zdmap;
657 u.u_csmap = zdmap;
658 if (swpexpand(u.u_dsize, u.u_ssize, &u.u_cdmap, &u.u_csmap) == 0) {
659 u.u_r.r_val2 = 0;
660 return;
661 }
662 fork1(0);
663}
664
665fork1(isvfork)
666{
667 register struct proc *p1, *p2;
668 register a;
669
670 a = 0;
671 p2 = NULL;
e92a04af 672 for (p1 = proc; p1 < procNPROC; p1++) {
29dd101b
BJ
673 if (p1->p_stat==NULL && p2==NULL)
674 p2 = p1;
675 else {
676 if (p1->p_uid==u.u_uid && p1->p_stat!=NULL)
677 a++;
678 }
679 }
680 /*
681 * Disallow if
682 * No processes at all;
683 * not su and too many procs owned; or
684 * not su and would take last slot.
685 */
62901f34
BJ
686 if (p2==NULL)
687 tablefull("proc");
86fd527f 688 if (p2==NULL || (u.u_uid!=0 && (p2==procNPROC-1 || a>MAXUPRC))) {
29dd101b
BJ
689 u.u_error = EAGAIN;
690 if (!isvfork) {
81263dba
BJ
691 (void) vsexpand(0, &u.u_cdmap, 1);
692 (void) vsexpand(0, &u.u_csmap, 1);
29dd101b
BJ
693 }
694 goto out;
695 }
696 p1 = u.u_procp;
e92a04af 697 if (newproc(isvfork)) {
29dd101b
BJ
698 u.u_r.r_val1 = p1->p_pid;
699 u.u_r.r_val2 = 1; /* child */
700 u.u_start = time;
701 u.u_acflag = AFORK;
702 return;
703 }
704 u.u_r.r_val1 = p2->p_pid;
705
706out:
707 u.u_r.r_val2 = 0;
708}
709
710/*
711 * break system call.
712 * -- bad planning: "break" is a dirty word in C.
713 */
714sbreak()
715{
716 struct a {
717 char *nsiz;
718 };
719 register int n, d;
720
721 /*
722 * set n to new data size
723 * set d to new-old
724 */
725
726 n = btoc(((struct a *)u.u_ap)->nsiz);
727 if (!u.u_sep)
728 n -= ctos(u.u_tsize) * stoc(1);
729 if (n < 0)
730 n = 0;
731 d = clrnd(n - u.u_dsize);
5290ec92 732 if (ctob(u.u_dsize+d) > u.u_limit[LIM_DATA]) {
054016e1
BJ
733 u.u_error = ENOMEM;
734 return;
735 }
29dd101b
BJ
736 if (chksize(u.u_tsize, u.u_dsize+d, u.u_ssize))
737 return;
738 if (swpexpand(u.u_dsize+d, u.u_ssize, &u.u_dmap, &u.u_smap)==0)
739 return;
740 expand(d, P0BR);
741}