from shannon
[unix-history] / usr / src / sys / kern / kern_proc.c
CommitLineData
d10e4853 1/* kern_proc.c 4.59 83/02/16 */
961945a8
SL
2
3#include "../machine/reg.h"
4#include "../machine/pte.h"
5#include "../machine/psl.h"
29dd101b
BJ
6
7#include "../h/param.h"
8#include "../h/systm.h"
9#include "../h/map.h"
29dd101b
BJ
10#include "../h/dir.h"
11#include "../h/user.h"
a1bce776 12#include "../h/kernel.h"
29dd101b
BJ
13#include "../h/proc.h"
14#include "../h/buf.h"
29dd101b
BJ
15#include "../h/inode.h"
16#include "../h/seg.h"
17#include "../h/acct.h"
cf1233e6 18#include "../h/wait.h"
29dd101b
BJ
19#include "../h/vm.h"
20#include "../h/text.h"
3ca1542b 21#include "../h/file.h"
feab6b5e 22#include "../h/quota.h"
4147b3f6 23#include "../h/descrip.h"
6fd40cea 24#include "../h/uio.h"
a1bce776 25#include "../h/mbuf.h"
4f083fd7 26#include "../h/nami.h"
a1bce776
BJ
27
28gethostid()
29{
30
1edb1cf8 31 u.u_r.r_val1 = hostid;
a1bce776
BJ
32}
33
34sethostid()
35{
1edb1cf8
BJ
36 struct a {
37 int hostid;
38 } *uap = (struct a *)u.u_ap;
a1bce776 39
1edb1cf8
BJ
40 if (suser())
41 hostid = uap->hostid;
42}
43
44gethostname()
45{
46 register struct a {
47 char *hostname;
48 int len;
49 } *uap = (struct a *)u.u_ap;
50 register u_int len;
51
52 len = uap->len;
53 if (len > hostnamelen)
54 len = hostnamelen;
127f7d76 55 u.u_error = copyout((caddr_t)hostname, (caddr_t)uap->hostname, len);
1edb1cf8
BJ
56}
57
58sethostname()
59{
60 register struct a {
61 char *hostname;
62 u_int len;
63 } *uap = (struct a *)u.u_ap;
64
65 if (!suser())
66 return;
67 if (uap->len > sizeof (hostname) - 1) {
68 u.u_error = EINVAL;
69 return;
70 }
71 hostnamelen = uap->len;
127f7d76 72 u.u_error = copyin((caddr_t)uap->hostname, hostname, uap->len);
1edb1cf8 73 hostname[hostnamelen] = 0;
a1bce776 74}
29dd101b
BJ
75
76/*
77 * exec system call, with and without environments.
78 */
79struct execa {
80 char *fname;
81 char **argp;
82 char **envp;
83};
84
a1bce776 85execv()
29dd101b
BJ
86{
87 ((struct execa *)u.u_ap)->envp = NULL;
a1bce776 88 execve();
29dd101b
BJ
89}
90
a1bce776 91execve()
29dd101b
BJ
92{
93 register nc;
94 register char *cp;
95 register struct buf *bp;
96 register struct execa *uap;
97 int na, ne, ucp, ap, c;
7eeaac77
RE
98 int indir, uid, gid;
99 char *sharg;
29dd101b
BJ
100 struct inode *ip;
101 swblk_t bno;
6459ebe0 102 char cfname[MAXNAMLEN + 1];
7eeaac77 103 char cfarg[SHSIZE];
a6b6f679 104 int resid;
29dd101b 105
4f083fd7 106 if ((ip = namei(uchar, LOOKUP, 1)) == NULL)
29dd101b
BJ
107 return;
108 bno = 0;
109 bp = 0;
7eeaac77
RE
110 indir = 0;
111 uid = u.u_uid;
112 gid = u.u_gid;
7eeaac77
RE
113 if (ip->i_mode & ISUID)
114 uid = ip->i_uid;
115 if (ip->i_mode & ISGID)
116 gid = ip->i_gid;
117
118 again:
e92a04af 119 if (access(ip, IEXEC))
29dd101b 120 goto bad;
e92a04af 121 if ((u.u_procp->p_flag&STRC) && access(ip, IREAD))
eb83bf86 122 goto bad;
e92a04af 123 if ((ip->i_mode & IFMT) != IFREG ||
29dd101b
BJ
124 (ip->i_mode & (IEXEC|(IEXEC>>3)|(IEXEC>>6))) == 0) {
125 u.u_error = EACCES;
126 goto bad;
127 }
7eeaac77
RE
128
129 /*
130 * Read in first few bytes of file for segment sizes, ux_mag:
131 * 407 = plain executable
132 * 410 = RO text
133 * 413 = demand paged RO text
134 * Also an ASCII line beginning with #! is
135 * the file name of a ``shell'' and arguments may be prepended
136 * to the argument list if given here.
137 *
138 * SHELL NAMES ARE LIMITED IN LENGTH.
139 *
140 * ONLY ONE ARGUMENT MAY BE PASSED TO THE SHELL FROM
141 * THE ASCII LINE.
142 */
961945a8 143 u.u_exdata.ux_shell[0] = 0; /* for zero length files */
a2a2a0d6 144 u.u_error = rdwri(UIO_READ, ip, (caddr_t)&u.u_exdata, sizeof (u.u_exdata),
a6b6f679 145 0, 1, &resid);
e92a04af 146 if (u.u_error)
7eeaac77 147 goto bad;
a6b6f679 148 u.u_count = resid;
a2a2a0d6 149#ifndef lint
348d7c13
BJ
150 if (u.u_count > sizeof(u.u_exdata) - sizeof(u.u_exdata.Ux_A) &&
151 u.u_exdata.ux_shell[0] != '#') {
7eeaac77
RE
152 u.u_error = ENOEXEC;
153 goto bad;
154 }
a2a2a0d6 155#endif
7eeaac77
RE
156 switch (u.u_exdata.ux_mag) {
157
158 case 0407:
159 u.u_exdata.ux_dsize += u.u_exdata.ux_tsize;
160 u.u_exdata.ux_tsize = 0;
161 break;
162
163 case 0413:
164 case 0410:
165 if (u.u_exdata.ux_tsize == 0) {
166 u.u_error = ENOEXEC;
167 goto bad;
168 }
169 break;
170
171 default:
172 if (u.u_exdata.ux_shell[0] != '#' ||
173 u.u_exdata.ux_shell[1] != '!' ||
174 indir) {
175 u.u_error = ENOEXEC;
176 goto bad;
177 }
178 cp = &u.u_exdata.ux_shell[2]; /* skip "#!" */
179 while (cp < &u.u_exdata.ux_shell[SHSIZE]) {
180 if (*cp == '\t')
181 *cp = ' ';
182 else if (*cp == '\n') {
183 *cp = '\0';
184 break;
185 }
186 cp++;
187 }
188 if (*cp != '\0') {
189 u.u_error = ENOEXEC;
190 goto bad;
191 }
192 cp = &u.u_exdata.ux_shell[2];
193 while (*cp == ' ')
194 cp++;
195 u.u_dirp = cp;
196 while (*cp && *cp != ' ')
197 cp++;
198 sharg = NULL;
199 if (*cp) {
200 *cp++ = '\0';
201 while (*cp == ' ')
202 cp++;
203 if (*cp) {
204 bcopy((caddr_t)cp, (caddr_t)cfarg, SHSIZE);
205 sharg = cfarg;
206 }
207 }
6459ebe0 208 bcopy((caddr_t)u.u_dent.d_name, (caddr_t)cfname,
a2a2a0d6 209 (unsigned)(u.u_dent.d_namlen + 1));
7eeaac77
RE
210 indir = 1;
211 iput(ip);
4f083fd7 212 ip = namei(schar, LOOKUP, 1);
7eeaac77
RE
213 if (ip == NULL)
214 return;
215 goto again;
216 }
217
29dd101b
BJ
218 /*
219 * Collect arguments on "file" in swap space.
220 */
221 na = 0;
222 ne = 0;
223 nc = 0;
224 uap = (struct execa *)u.u_ap;
39d536e6 225 if ((bno = rmalloc(argmap, (long)ctod(clrnd((int)btoc(NCARGS))))) == 0) {
29dd101b
BJ
226 swkill(u.u_procp, "exece");
227 goto bad;
228 }
229 if (bno % CLSIZE)
b725a0ca 230 panic("execa rmalloc");
29dd101b
BJ
231 if (uap->argp) for (;;) {
232 ap = NULL;
40d07ebb 233 if (indir && (na == 1 || na == 2 && sharg))
7eeaac77
RE
234 ap = (int)uap->fname;
235 else if (uap->argp) {
29dd101b
BJ
236 ap = fuword((caddr_t)uap->argp);
237 uap->argp++;
238 }
239 if (ap==NULL && uap->envp) {
240 uap->argp = NULL;
241 if ((ap = fuword((caddr_t)uap->envp)) == NULL)
242 break;
243 uap->envp++;
244 ne++;
245 }
6459ebe0 246 if (ap == NULL)
29dd101b
BJ
247 break;
248 na++;
e92a04af 249 if (ap == -1)
29dd101b
BJ
250 u.u_error = EFAULT;
251 do {
252 if (nc >= NCARGS-1)
253 u.u_error = E2BIG;
7eeaac77
RE
254 if (indir && na == 2 && sharg != NULL)
255 c = *sharg++ & 0377;
256 else if ((c = fubyte((caddr_t)ap++)) < 0)
29dd101b 257 u.u_error = EFAULT;
64d6118b
BJ
258 if (u.u_error) {
259 if (bp)
260 brelse(bp);
261 bp = 0;
29dd101b 262 goto badarg;
64d6118b 263 }
6459ebe0 264 if (nc % (CLSIZE*NBPG) == 0) {
29dd101b
BJ
265 if (bp)
266 bdwrite(bp);
e0fc9a90 267 bp = getblk(argdev, bno + ctod(nc / NBPG),
6459ebe0 268 CLSIZE*NBPG);
29dd101b
BJ
269 cp = bp->b_un.b_addr;
270 }
271 nc++;
272 *cp++ = c;
6459ebe0 273 } while (c > 0);
29dd101b
BJ
274 }
275 if (bp)
276 bdwrite(bp);
277 bp = 0;
278 nc = (nc + NBPW-1) & ~(NBPW-1);
6459ebe0
KM
279 if (indir) {
280 u.u_dent.d_namlen = strlen(cfname);
281 bcopy((caddr_t)cfname, (caddr_t)u.u_dent.d_name,
a2a2a0d6 282 (unsigned)(u.u_dent.d_namlen + 1));
6459ebe0 283 }
7eeaac77 284 getxfile(ip, nc + (na+4)*NBPW, uid, gid);
29a06346 285 if (u.u_error) {
29dd101b 286badarg:
0981aef0
BJ
287 for (c = 0; c < nc; c += CLSIZE*NBPG) {
288 bp = baddr(argdev, bno + ctod(c / NBPG), CLSIZE*NBPG);
e0fc9a90 289 if (bp) {
29dd101b
BJ
290 bp->b_flags |= B_AGE; /* throw away */
291 bp->b_flags &= ~B_DELWRI; /* cancel io */
292 brelse(bp);
293 bp = 0;
294 }
0981aef0 295 }
29dd101b
BJ
296 goto bad;
297 }
298
299 /*
300 * copy back arglist
301 */
29dd101b
BJ
302 ucp = USRSTACK - nc - NBPW;
303 ap = ucp - na*NBPW - 3*NBPW;
304 u.u_ar0[SP] = ap;
81263dba 305 (void) suword((caddr_t)ap, na-ne);
29dd101b
BJ
306 nc = 0;
307 for (;;) {
308 ap += NBPW;
309 if (na==ne) {
81263dba 310 (void) suword((caddr_t)ap, 0);
29dd101b
BJ
311 ap += NBPW;
312 }
313 if (--na < 0)
314 break;
81263dba 315 (void) suword((caddr_t)ap, ucp);
29dd101b 316 do {
6459ebe0 317 if (nc % (CLSIZE*NBPG) == 0) {
29dd101b
BJ
318 if (bp)
319 brelse(bp);
62cd6584 320 bp = bread(argdev, bno + ctod(nc / NBPG),
6459ebe0 321 CLSIZE*NBPG);
29dd101b
BJ
322 bp->b_flags |= B_AGE; /* throw away */
323 bp->b_flags &= ~B_DELWRI; /* cancel io */
324 cp = bp->b_un.b_addr;
325 }
81263dba 326 (void) subyte((caddr_t)ucp++, (c = *cp++));
29dd101b
BJ
327 nc++;
328 } while(c&0377);
329 }
81263dba 330 (void) suword((caddr_t)ap, 0);
29dd101b
BJ
331 setregs();
332bad:
333 if (bp)
334 brelse(bp);
335 if (bno)
30c36259 336 rmfree(argmap, (long)ctod(clrnd((int) btoc(NCARGS))), bno);
29dd101b
BJ
337 iput(ip);
338}
339
340/*
341 * Read in and set up memory for executed file.
29dd101b 342 */
7eeaac77 343getxfile(ip, nargc, uid, gid)
29dd101b
BJ
344register struct inode *ip;
345{
29dd101b 346 register size_t ts, ds, ss;
7eeaac77 347 int pagi;
29dd101b 348
7eeaac77 349 if (u.u_exdata.ux_mag == 0413)
29dd101b 350 pagi = SPAGI;
7eeaac77
RE
351 else
352 pagi = 0;
e92a04af
BJ
353 if (u.u_exdata.ux_tsize!=0 && (ip->i_flag&ITEXT)==0 &&
354 ip->i_count!=1) {
3ca1542b
BJ
355 register struct file *fp;
356
e92a04af 357 for (fp = file; fp < fileNFILE; fp++) {
4147b3f6 358 if (fp->f_type == DTYPE_FILE &&
ea726938 359 fp->f_count > 0 &&
4147b3f6 360 fp->f_inode == ip && (fp->f_flag&FWRITE)) {
3ca1542b
BJ
361 u.u_error = ETXTBSY;
362 goto bad;
363 }
e92a04af 364 }
29dd101b
BJ
365 }
366
367 /*
e92a04af 368 * Compute text and data sizes and make sure not too large.
29dd101b 369 */
29dd101b
BJ
370 ts = clrnd(btoc(u.u_exdata.ux_tsize));
371 ds = clrnd(btoc((u.u_exdata.ux_dsize+u.u_exdata.ux_bsize)));
372 ss = clrnd(SSIZE + btoc(nargc));
29a06346
BJ
373 if (chksize(ts, ds, ss))
374 goto bad;
e92a04af
BJ
375
376 /*
377 * Make sure enough space to start process.
378 */
29a06346
BJ
379 u.u_cdmap = zdmap;
380 u.u_csmap = zdmap;
381 if (swpexpand(ds, ss, &u.u_cdmap, &u.u_csmap) == NULL)
382 goto bad;
29dd101b 383
29a06346
BJ
384 /*
385 * At this point, committed to the new image!
386 * Release virtual memory resources of old process, and
387 * initialize the virtual memory of the new process.
388 * If we resulted from vfork(), instead wakeup our
389 * parent who will set SVFDONE when he has taken back
390 * our resources.
391 */
29a06346
BJ
392 if ((u.u_procp->p_flag & SVFORK) == 0)
393 vrelvm();
394 else {
395 u.u_procp->p_flag &= ~SVFORK;
396 u.u_procp->p_flag |= SKEEP;
397 wakeup((caddr_t)u.u_procp);
398 while ((u.u_procp->p_flag & SVFDONE) == 0)
399 sleep((caddr_t)u.u_procp, PZERO - 1);
400 u.u_procp->p_flag &= ~(SVFDONE|SKEEP);
401 }
2ce421b6 402 u.u_procp->p_flag &= ~(SPAGI|SSEQL|SUANOM|SNUSIG);
29a06346
BJ
403 u.u_procp->p_flag |= pagi;
404 u.u_dmap = u.u_cdmap;
405 u.u_smap = u.u_csmap;
406 vgetvm(ts, ds, ss);
29dd101b 407
a6b6f679 408 if (pagi == 0)
a2a2a0d6
BJ
409 u.u_error =
410 rdwri(UIO_READ, ip,
e0fc9a90
BJ
411 (char *)ctob(dptov(u.u_procp, 0)),
412 (int)u.u_exdata.ux_dsize,
a2a2a0d6
BJ
413 (int)(sizeof(u.u_exdata)+u.u_exdata.ux_tsize),
414 0, (int *)0);
29a06346
BJ
415 xalloc(ip, pagi);
416 if (pagi && u.u_procp->p_textp)
417 vinifod((struct fpte *)dptopte(u.u_procp, 0),
418 PG_FTEXT, u.u_procp->p_textp->x_iptr,
39d536e6 419 (long)(1 + ts/CLSIZE), (int)btoc(u.u_exdata.ux_dsize));
29a06346 420
e0fc9a90 421#ifdef vax
29a06346 422 /* THIS SHOULD BE DONE AT A LOWER LEVEL, IF AT ALL */
206ecc72 423#include "../vax/mtpr.h" /* XXX */
29a06346 424 mtpr(TBIA, 0);
e0fc9a90 425#endif
29a06346 426
5b98abb9
BJ
427 if (u.u_error)
428 swkill(u.u_procp, "i/o error mapping pages");
29a06346
BJ
429 /*
430 * set SUID/SGID protections, if no tracing
431 */
432 if ((u.u_procp->p_flag&STRC)==0) {
e92a04af
BJ
433 u.u_uid = uid;
434 u.u_procp->p_uid = uid;
7eeaac77 435 u.u_gid = gid;
29a06346
BJ
436 } else
437 psignal(u.u_procp, SIGTRAP);
29dd101b
BJ
438 u.u_tsize = ts;
439 u.u_dsize = ds;
440 u.u_ssize = ss;
29dd101b 441bad:
29a06346 442 return;
29dd101b
BJ
443}
444
445/*
446 * Clear registers on exec
447 */
448setregs()
449{
594ebedd 450 register int (**rp)();
29dd101b 451 register i;
bdfe5b0f 452 long sigmask;
29dd101b 453
4e6e3887 454 for (rp = &u.u_signal[1], sigmask = 1L; rp < &u.u_signal[NSIG];
bdfe5b0f
BJ
455 sigmask <<= 1, rp++) {
456 switch (*rp) {
457
458 case SIG_IGN:
459 case SIG_DFL:
460 case SIG_HOLD:
461 continue;
462
463 default:
464 /*
99fa88a2 465 * Normal or deferring catch; revert to default.
bdfe5b0f 466 */
99fa88a2
BJ
467 (void) spl6();
468 *rp = SIG_DFL;
bdfe5b0f
BJ
469 if ((int)*rp & 1)
470 u.u_procp->p_siga0 |= sigmask;
471 else
a4aaef65 472 u.u_procp->p_siga0 &= ~sigmask;
bdfe5b0f
BJ
473 if ((int)*rp & 2)
474 u.u_procp->p_siga1 |= sigmask;
475 else
476 u.u_procp->p_siga1 &= ~sigmask;
99fa88a2 477 (void) spl0();
bdfe5b0f
BJ
478 continue;
479 }
480 }
e0fc9a90 481#ifdef vax
29dd101b 482/*
e92a04af 483 for (rp = &u.u_ar0[0]; rp < &u.u_ar0[16];)
29dd101b
BJ
484 *rp++ = 0;
485*/
e0fc9a90
BJ
486 u.u_ar0[PC] = u.u_exdata.ux_entloc+2;
487#endif
488#ifdef sun
489 { register struct regs *r = (struct regs *)u.u_ar0;
490 for (i = 0; i < 8; i++) {
491 r->r_dreg[i] = 0;
492 if (&r->r_areg[i] != &r->r_sp)
493 r->r_areg[i] = 0;
494 }
495 r->r_sr = PSL_USERSET;
496 r->r_pc = u.u_exdata.ux_entloc;
497 }
498#endif
e92a04af 499 for (i=0; i<NOFILE; i++) {
3650c37d 500 if (u.u_pofile[i]&UF_EXCLOSE) {
a81e9a81 501 closef(u.u_ofile[i], 1, u.u_pofile[i]);
29dd101b 502 u.u_ofile[i] = NULL;
a81e9a81 503 u.u_pofile[i] = 0;
29dd101b 504 }
e0fc9a90 505 u.u_pofile[i] &= ~UF_MAPPED;
29dd101b 506 }
e92a04af 507
29dd101b
BJ
508 /*
509 * Remember file name for accounting.
510 */
511 u.u_acflag &= ~AFORK;
6459ebe0 512 bcopy((caddr_t)u.u_dent.d_name, (caddr_t)u.u_comm,
a2a2a0d6 513 (unsigned)(u.u_dent.d_namlen + 1));
e0fc9a90
BJ
514#ifdef sun
515 u.u_eosys = REALLYRETURN;
516#endif
29dd101b
BJ
517}
518
519/*
e92a04af 520 * Exit system call: pass back caller's arg
29dd101b
BJ
521 */
522rexit()
523{
524 register struct a {
525 int rval;
526 } *uap;
527
528 uap = (struct a *)u.u_ap;
529 exit((uap->rval & 0377) << 8);
530}
531
532/*
533 * Release resources.
534 * Save u. area for parent to look at.
535 * Enter zombie state.
536 * Wake up parent and init processes,
537 * and dispose of children.
538 */
539exit(rv)
540{
541 register int i;
542 register struct proc *p, *q;
29dd101b 543 register int x;
cce93e4b 544 struct mbuf *m = m_getclr(M_WAIT, MT_ZOMBIE);
29dd101b
BJ
545
546#ifdef PGINPROF
547 vmsizmon();
548#endif
549 p = u.u_procp;
550 p->p_flag &= ~(STRC|SULOCK);
551 p->p_flag |= SWEXIT;
bdfe5b0f
BJ
552 (void) spl6();
553 if ((int)SIG_IGN & 1)
554 p->p_siga0 = ~0;
555 else
556 p->p_siga0 = 0;
557 if ((int)SIG_IGN & 2)
558 p->p_siga1 = ~0;
559 else
99fa88a2 560 p->p_siga1 = 0;
bdfe5b0f 561 (void) spl0();
dd808ba3
BJ
562 p->p_cpticks = 0;
563 p->p_pctcpu = 0;
e92a04af 564 for (i=0; i<NSIG; i++)
594ebedd 565 u.u_signal[i] = SIG_IGN;
b32450f4 566 untimeout(realitexpire, (caddr_t)p);
29dd101b
BJ
567 /*
568 * Release virtual memory. If we resulted from
569 * a vfork(), instead give the resources back to
570 * the parent.
571 */
572 if ((p->p_flag & SVFORK) == 0)
573 vrelvm();
574 else {
575 p->p_flag &= ~SVFORK;
576 wakeup((caddr_t)p);
577 while ((p->p_flag & SVFDONE) == 0)
578 sleep((caddr_t)p, PZERO - 1);
579 p->p_flag &= ~SVFDONE;
580 }
a81e9a81 581 for (i = 0; i < NOFILE; i++) {
c5c236f8
SL
582 struct file *f;
583 int p;
584
29dd101b
BJ
585 f = u.u_ofile[i];
586 u.u_ofile[i] = NULL;
c5c236f8 587 p = u.u_pofile[i];
a81e9a81 588 u.u_pofile[i] = 0;
c5c236f8 589 closef(f, 1, p);
29dd101b 590 }
e92a04af 591 ilock(u.u_cdir);
29dd101b
BJ
592 iput(u.u_cdir);
593 if (u.u_rdir) {
e92a04af 594 ilock(u.u_rdir);
29dd101b
BJ
595 iput(u.u_rdir);
596 }
a1bce776 597 u.u_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
29dd101b 598 acct();
feab6b5e
RE
599#ifdef QUOTA
600 qclean();
e0fc9a90
BJ
601#endif
602#ifdef sun
603 ctxfree(&u);
feab6b5e 604#endif
29dd101b
BJ
605 vrelpt(u.u_procp);
606 vrelu(u.u_procp, 0);
f12a8410 607 (void) spl5(); /* hack for mem alloc race XXX */
29dd101b 608 multprog--;
29dd101b 609 p->p_stat = SZOMB;
42343c0f 610 noproc = 1;
29dd101b
BJ
611 i = PIDHASH(p->p_pid);
612 x = p - proc;
613 if (pidhash[i] == x)
614 pidhash[i] = p->p_idhash;
615 else {
616 for (i = pidhash[i]; i != 0; i = proc[i].p_idhash)
617 if (proc[i].p_idhash == x) {
618 proc[i].p_idhash = p->p_idhash;
619 goto done;
620 }
621 panic("exit");
622 }
9c82b9fd
BJ
623 if (p->p_pid == 1)
624 panic("init died");
29dd101b 625done:
a1bce776 626 p->p_xstat = rv;
c5c236f8
SL
627if (m == 0)
628panic("exit: m_getclr");
8c2b0bcd 629 p->p_ru = mtod(m, struct rusage *);
a1bce776
BJ
630 *p->p_ru = u.u_ru;
631 ruadd(p->p_ru, &u.u_cru);
e92a04af
BJ
632 for (q = proc; q < procNPROC; q++)
633 if (q->p_pptr == p) {
feab6b5e
RE
634 if (q->p_osptr)
635 q->p_osptr->p_ysptr = q->p_ysptr;
636 if (q->p_ysptr)
637 q->p_ysptr->p_osptr = q->p_osptr;
638 if (proc[1].p_cptr)
639 proc[1].p_cptr->p_ysptr = q;
640 q->p_osptr = proc[1].p_cptr;
641 q->p_ysptr = NULL;
642 proc[1].p_cptr = q;
643
bdfe5b0f 644 q->p_pptr = &proc[1];
29dd101b 645 q->p_ppid = 1;
bdfe5b0f
BJ
646 wakeup((caddr_t)&proc[1]);
647 /*
0dde1c43 648 * Traced processes are killed
bdfe5b0f 649 * since their existence means someone is screwing up.
62bac59f 650 * Stopped processes are sent a hangup and a continue.
0dde1c43
BJ
651 * This is designed to be ``safe'' for setuid
652 * processes since they must be willing to tolerate
653 * hangups anyways.
bdfe5b0f 654 */
0dde1c43 655 if (q->p_flag&STRC) {
bdfe5b0f
BJ
656 q->p_flag &= ~STRC;
657 psignal(q, SIGKILL);
0dde1c43
BJ
658 } else if (q->p_stat == SSTOP) {
659 psignal(q, SIGHUP);
660 psignal(q, SIGCONT);
bdfe5b0f 661 }
8643403f
BJ
662 /*
663 * Protect this process from future
7ac93404 664 * tty signals, clear TSTP/TTIN/TTOU if pending.
8643403f 665 */
934e4ecf 666 (void) spgrp(q, -1);
29dd101b 667 }
bdfe5b0f 668 psignal(p->p_pptr, SIGCHLD);
4e6e3887 669 wakeup((caddr_t)p->p_pptr);
29dd101b
BJ
670 swtch();
671}
672
8ec6ce7a
SL
673wait()
674{
675 struct rusage ru, *rup;
676
677 if ((u.u_ar0[PS] & PSL_ALLCC) != PSL_ALLCC) {
678 u.u_error = wait1(0, (struct rusage *)0);
679 return;
680 }
681 rup = (struct rusage *)u.u_ar0[R1];
682 u.u_error = wait1(u.u_ar0[R0], &ru);
683 if (u.u_error)
684 return;
685 (void) copyout((caddr_t)&ru, (caddr_t)rup, sizeof (struct rusage));
686}
687
688#ifndef NOCOMPAT
961945a8 689#include "../h/vtimes.h"
3269c554 690
1edb1cf8 691owait()
29dd101b 692{
3269c554
BJ
693 struct rusage ru;
694 struct vtimes *vtp, avt;
29dd101b 695
bdfe5b0f 696 if ((u.u_ar0[PS] & PSL_ALLCC) != PSL_ALLCC) {
8ec6ce7a 697 u.u_error = wait1(0, (struct rusage *)0);
bdfe5b0f
BJ
698 return;
699 }
3269c554 700 vtp = (struct vtimes *)u.u_ar0[R1];
8ec6ce7a 701 u.u_error = wait1(u.u_ar0[R0], &ru);
bdfe5b0f
BJ
702 if (u.u_error)
703 return;
3269c554
BJ
704 getvtimes(&ru, &avt);
705 (void) copyout((caddr_t)&avt, (caddr_t)vtp, sizeof (struct vtimes));
29dd101b 706}
8ec6ce7a 707#endif
29dd101b
BJ
708
709/*
710 * Wait system call.
711 * Search for a terminated (zombie) child,
712 * finally lay it to rest, and collect its status.
713 * Look also for stopped (traced) children,
714 * and pass back status from them.
715 */
a1bce776
BJ
716wait1(options, ru)
717 register int options;
718 struct rusage *ru;
29dd101b
BJ
719{
720 register f;
feab6b5e 721 register struct proc *p, *q;
29dd101b
BJ
722
723 f = 0;
29dd101b 724loop:
e92a04af
BJ
725 for (p = proc; p < procNPROC; p++)
726 if (p->p_pptr == u.u_procp) {
29dd101b 727 f++;
e92a04af 728 if (p->p_stat == SZOMB) {
29dd101b 729 u.u_r.r_val1 = p->p_pid;
a1bce776
BJ
730 u.u_r.r_val2 = p->p_xstat;
731 p->p_xstat = 0;
732 if (ru)
733 *ru = *p->p_ru;
734 ruadd(&u.u_cru, p->p_ru);
39d536e6 735 (void) m_free(dtom(p->p_ru));
a1bce776 736 p->p_ru = 0;
29dd101b
BJ
737 p->p_stat = NULL;
738 p->p_pid = 0;
739 p->p_ppid = 0;
feab6b5e
RE
740 if (q = p->p_ysptr)
741 q->p_osptr = p->p_osptr;
742 if (q = p->p_osptr)
743 q->p_ysptr = p->p_ysptr;
744 if ((q = p->p_pptr)->p_cptr == p)
745 q->p_cptr = p->p_osptr;
bdfe5b0f 746 p->p_pptr = 0;
feab6b5e
RE
747 p->p_ysptr = 0;
748 p->p_osptr = 0;
749 p->p_cptr = 0;
29dd101b 750 p->p_sig = 0;
bdfe5b0f
BJ
751 p->p_siga0 = 0;
752 p->p_siga1 = 0;
29dd101b
BJ
753 p->p_pgrp = 0;
754 p->p_flag = 0;
755 p->p_wchan = 0;
bdfe5b0f 756 p->p_cursig = 0;
8ec6ce7a 757 return (0);
29dd101b 758 }
bdfe5b0f
BJ
759 if (p->p_stat == SSTOP && (p->p_flag&SWTED)==0 &&
760 (p->p_flag&STRC || options&WUNTRACED)) {
761 p->p_flag |= SWTED;
762 u.u_r.r_val1 = p->p_pid;
763 u.u_r.r_val2 = (p->p_cursig<<8) | WSTOPPED;
8ec6ce7a 764 return (0);
29dd101b
BJ
765 }
766 }
8ec6ce7a
SL
767 if (f == 0) {
768 return (ECHILD);
bdfe5b0f
BJ
769 }
770 if (options&WNOHANG) {
771 u.u_r.r_val1 = 0;
8ec6ce7a 772 return (0);
bdfe5b0f 773 }
f9c5db19 774 if ((u.u_procp->p_flag&SNUSIG) && setjmp(&u.u_qsave)) {
bdfe5b0f 775 u.u_eosys = RESTARTSYS;
8ec6ce7a 776 return (0);
29dd101b 777 }
bdfe5b0f
BJ
778 sleep((caddr_t)u.u_procp, PWAIT);
779 goto loop;
29dd101b
BJ
780}
781
782/*
783 * fork system call.
784 */
785fork()
786{
787
788 u.u_cdmap = zdmap;
789 u.u_csmap = zdmap;
790 if (swpexpand(u.u_dsize, u.u_ssize, &u.u_cdmap, &u.u_csmap) == 0) {
791 u.u_r.r_val2 = 0;
792 return;
793 }
794 fork1(0);
795}
796
797fork1(isvfork)
798{
799 register struct proc *p1, *p2;
feab6b5e 800#ifndef QUOTA
29dd101b
BJ
801 register a;
802
803 a = 0;
feab6b5e
RE
804#else
805 if (u.u_quota != NOQUOT && u.u_quota->q_plim &&
806 u.u_quota->q_cnt >= u.u_quota->q_plim) {
807 u.u_error = EPROCLIM;
808 return;
809 }
810#endif
29dd101b 811 p2 = NULL;
e92a04af 812 for (p1 = proc; p1 < procNPROC; p1++) {
feab6b5e
RE
813#ifdef QUOTA
814 if (p1->p_stat == NULL) {
815 p2 = p1;
816 break;
817 }
818#else
29dd101b
BJ
819 if (p1->p_stat==NULL && p2==NULL)
820 p2 = p1;
821 else {
822 if (p1->p_uid==u.u_uid && p1->p_stat!=NULL)
823 a++;
824 }
feab6b5e 825#endif
29dd101b
BJ
826 }
827 /*
828 * Disallow if
829 * No processes at all;
830 * not su and too many procs owned; or
831 * not su and would take last slot.
832 */
62901f34
BJ
833 if (p2==NULL)
834 tablefull("proc");
feab6b5e
RE
835#ifdef QUOTA
836 if (p2==NULL || (u.u_uid!=0 && p2==procNPROC-1)) {
837#else
86fd527f 838 if (p2==NULL || (u.u_uid!=0 && (p2==procNPROC-1 || a>MAXUPRC))) {
feab6b5e 839#endif
29dd101b
BJ
840 u.u_error = EAGAIN;
841 if (!isvfork) {
81263dba
BJ
842 (void) vsexpand(0, &u.u_cdmap, 1);
843 (void) vsexpand(0, &u.u_csmap, 1);
29dd101b
BJ
844 }
845 goto out;
846 }
847 p1 = u.u_procp;
e92a04af 848 if (newproc(isvfork)) {
29dd101b
BJ
849 u.u_r.r_val1 = p1->p_pid;
850 u.u_r.r_val2 = 1; /* child */
a1bce776 851 u.u_start = time.tv_sec;
29dd101b 852 u.u_acflag = AFORK;
feab6b5e
RE
853#ifdef QUOTA
854 u.u_qflags &= ~QUF_LOGIN;
855#endif
29dd101b
BJ
856 return;
857 }
858 u.u_r.r_val1 = p2->p_pid;
859
860out:
861 u.u_r.r_val2 = 0;
862}
863
4147b3f6
BJ
864spgrp(top, npgrp)
865register struct proc *top;
866{
867 register struct proc *pp, *p;
868 int f = 0;
869
870 for (p = top; npgrp == -1 || u.u_uid == p->p_uid ||
871 !u.u_uid || inferior(p); p = pp) {
872 if (npgrp == -1) {
873#define bit(a) (1<<(a-1))
874 p->p_sig &= ~(bit(SIGTSTP)|bit(SIGTTIN)|bit(SIGTTOU));
875 } else
876 p->p_pgrp = npgrp;
877 f++;
878 /*
879 * Search for children.
880 */
881 for (pp = proc; pp < procNPROC; pp++)
882 if (pp->p_pptr == p)
883 goto cont;
884 /*
885 * Search for siblings.
886 */
887 for (; p != top; p = p->p_pptr)
888 for (pp = p + 1; pp < procNPROC; pp++)
889 if (pp->p_pptr == p->p_pptr)
890 goto cont;
891 break;
892 cont:
893 ;
894 }
895 return (f);
896}
897
29dd101b 898/*
4147b3f6 899 * Is p an inferior of the current process?
29dd101b 900 */
4147b3f6 901inferior(p)
a2a2a0d6 902 register struct proc *p;
29dd101b 903{
29dd101b 904
4147b3f6
BJ
905 for (; p != u.u_procp; p = p->p_pptr)
906 if (p->p_ppid == 0)
907 return (0);
908 return (1);
29dd101b 909}
a2a2a0d6
BJ
910
911struct proc *
912pfind(pid)
913 int pid;
914{
915 register struct proc *p;
916
917 for (p = &proc[pidhash[PIDHASH(pid)]]; p != &proc[0]; p = &proc[p->p_idhash])
918 if (p->p_pid == pid)
919 return (p);
920 return ((struct proc *)0);
921}
1edb1cf8
BJ
922
923/*
924 * Create a new process-- the internal version of
925 * sys fork.
926 * It returns 1 in the new process, 0 in the old.
927 */
928newproc(isvfork)
929 int isvfork;
930{
931 register struct proc *p;
932 register struct proc *rpp, *rip;
933 register int n;
934 register struct file *fp;
935
936 p = NULL;
937 /*
938 * First, just locate a slot for a process
939 * and copy the useful info from this process into it.
940 * The panic "cannot happen" because fork has already
941 * checked for the existence of a slot.
942 */
943retry:
944 mpid++;
945 if (mpid >= 30000) {
946 mpid = 0;
947 goto retry;
948 }
949 for (rpp = proc; rpp < procNPROC; rpp++) {
950 if (rpp->p_stat == NULL && p==NULL)
951 p = rpp;
952 if (rpp->p_pid==mpid || rpp->p_pgrp==mpid)
953 goto retry;
954 }
955 if ((rpp = p) == NULL)
956 panic("no procs");
957
958 /*
959 * Make a proc table entry for the new process.
960 */
961 rip = u.u_procp;
962#ifdef QUOTA
963 (rpp->p_quota = rip->p_quota)->q_cnt++;
964#endif
965 rpp->p_stat = SIDL;
966 timerclear(&rpp->p_realtimer.it_value);
967 rpp->p_flag = SLOAD | (rip->p_flag & (SPAGI|SNUSIG));
968 if (isvfork) {
969 rpp->p_flag |= SVFORK;
970 rpp->p_ndx = rip->p_ndx;
971 } else
972 rpp->p_ndx = rpp - proc;
973 rpp->p_uid = rip->p_uid;
974 rpp->p_pgrp = rip->p_pgrp;
975 rpp->p_nice = rip->p_nice;
976 rpp->p_textp = isvfork ? 0 : rip->p_textp;
977 rpp->p_pid = mpid;
978 rpp->p_ppid = rip->p_pid;
979 rpp->p_pptr = rip;
980 rpp->p_osptr = rip->p_cptr;
981 if (rip->p_cptr)
982 rip->p_cptr->p_ysptr = rpp;
983 rpp->p_ysptr = NULL;
984 rpp->p_cptr = NULL;
985 rip->p_cptr = rpp;
986 rpp->p_time = 0;
987 rpp->p_cpu = 0;
988 rpp->p_siga0 = rip->p_siga0;
989 rpp->p_siga1 = rip->p_siga1;
990 /* take along any pending signals, like stops? */
991 if (isvfork) {
992 rpp->p_tsize = rpp->p_dsize = rpp->p_ssize = 0;
993 rpp->p_szpt = clrnd(ctopt(UPAGES));
994 forkstat.cntvfork++;
995 forkstat.sizvfork += rip->p_dsize + rip->p_ssize;
996 } else {
997 rpp->p_tsize = rip->p_tsize;
998 rpp->p_dsize = rip->p_dsize;
999 rpp->p_ssize = rip->p_ssize;
1000 rpp->p_szpt = rip->p_szpt;
1001 forkstat.cntfork++;
1002 forkstat.sizfork += rip->p_dsize + rip->p_ssize;
1003 }
1004 rpp->p_rssize = 0;
1005 rpp->p_maxrss = rip->p_maxrss;
1006 rpp->p_wchan = 0;
1007 rpp->p_slptime = 0;
1008 rpp->p_pctcpu = 0;
1009 rpp->p_cpticks = 0;
1010 n = PIDHASH(rpp->p_pid);
1011 p->p_idhash = pidhash[n];
1012 pidhash[n] = rpp - proc;
1013 multprog++;
1014
1015 /*
1016 * Increase reference counts on shared objects.
1017 */
1018 for (n = 0; n < NOFILE; n++) {
1019 fp = u.u_ofile[n];
1020 if (fp == NULL)
1021 continue;
1022 fp->f_count++;
3650c37d 1023 if (u.u_pofile[n]&UF_SHLOCK)
4f083fd7 1024 fp->f_inode->i_shlockc++;
3650c37d 1025 if (u.u_pofile[n]&UF_EXLOCK)
4f083fd7 1026 fp->f_inode->i_exlockc++;
1edb1cf8
BJ
1027 }
1028 u.u_cdir->i_count++;
1029 if (u.u_rdir)
1030 u.u_rdir->i_count++;
1031
1032 /*
1033 * Partially simulate the environment
1034 * of the new process so that when it is actually
1035 * created (by copying) it will look right.
1036 * This begins the section where we must prevent the parent
1037 * from being swapped.
1038 */
1039 rip->p_flag |= SKEEP;
1040 if (procdup(rpp, isvfork))
1041 return (1);
1042
1043 /*
1044 * Make child runnable and add to run queue.
1045 */
1046 (void) spl6();
1047 rpp->p_stat = SRUN;
1048 setrq(rpp);
1049 (void) spl0();
1050
1051 /*
1052 * Cause child to take a non-local goto as soon as it runs.
1053 * On older systems this was done with SSWAP bit in proc
1054 * table; on VAX we use u.u_pcb.pcb_sswap so don't need
1055 * to do rpp->p_flag |= SSWAP. Actually do nothing here.
1056 */
1057 /* rpp->p_flag |= SSWAP; */
1058
1059 /*
1060 * Now can be swapped.
1061 */
1062 rip->p_flag &= ~SKEEP;
1063
1064 /*
1065 * If vfork make chain from parent process to child
1066 * (where virtal memory is temporarily). Wait for
1067 * child to finish, steal virtual memory back,
1068 * and wakeup child to let it die.
1069 */
1070 if (isvfork) {
1071 u.u_procp->p_xlink = rpp;
1072 u.u_procp->p_flag |= SNOVM;
1073 while (rpp->p_flag & SVFORK)
1074 sleep((caddr_t)rpp, PZERO - 1);
1075 if ((rpp->p_flag & SLOAD) == 0)
1076 panic("newproc vfork");
1077 uaccess(rpp, Vfmap, &vfutl);
1078 u.u_procp->p_xlink = 0;
1079 vpassvm(rpp, u.u_procp, &vfutl, &u, Vfmap);
1080 u.u_procp->p_flag &= ~SNOVM;
1081 rpp->p_ndx = rpp - proc;
1082 rpp->p_flag |= SVFDONE;
1083 wakeup((caddr_t)rpp);
1084 }
1085
1086 /*
1087 * 0 return means parent.
1088 */
1089 return (0);
1090}