Commit | Line | Data |
---|---|---|
a81e9a81 | 1 | /* kern_proc.c 4.30 82/08/10 */ |
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" |
feab6b5e | 22 | #include "../h/quota.h" |
4147b3f6 | 23 | #include "../h/descrip.h" |
29dd101b BJ |
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; | |
7eeaac77 RE |
47 | int indir, uid, gid; |
48 | char *sharg; | |
29dd101b BJ |
49 | struct inode *ip; |
50 | swblk_t bno; | |
6459ebe0 | 51 | char cfname[MAXNAMLEN + 1]; |
7eeaac77 | 52 | char cfarg[SHSIZE]; |
29dd101b | 53 | |
5485e062 | 54 | if ((ip = namei(uchar, 0, 1)) == NULL) |
29dd101b BJ |
55 | return; |
56 | bno = 0; | |
57 | bp = 0; | |
7eeaac77 RE |
58 | indir = 0; |
59 | uid = u.u_uid; | |
60 | gid = u.u_gid; | |
7eeaac77 RE |
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: | |
e92a04af | 67 | if (access(ip, IEXEC)) |
29dd101b | 68 | goto bad; |
e92a04af | 69 | if ((u.u_procp->p_flag&STRC) && access(ip, IREAD)) |
eb83bf86 | 70 | goto bad; |
e92a04af | 71 | if ((ip->i_mode & IFMT) != IFREG || |
29dd101b BJ |
72 | (ip->i_mode & (IEXEC|(IEXEC>>3)|(IEXEC>>6))) == 0) { |
73 | u.u_error = EACCES; | |
74 | goto bad; | |
75 | } | |
7eeaac77 RE |
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; | |
e92a04af | 97 | if (u.u_error) |
7eeaac77 | 98 | goto bad; |
348d7c13 BJ |
99 | if (u.u_count > sizeof(u.u_exdata) - sizeof(u.u_exdata.Ux_A) && |
100 | u.u_exdata.ux_shell[0] != '#') { | |
7eeaac77 RE |
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 | } | |
6459ebe0 KM |
156 | bcopy((caddr_t)u.u_dent.d_name, (caddr_t)cfname, |
157 | u.u_dent.d_namlen + 1); | |
7eeaac77 RE |
158 | indir = 1; |
159 | iput(ip); | |
5485e062 | 160 | ip = namei(schar, 0, 1); |
7eeaac77 RE |
161 | if (ip == NULL) |
162 | return; | |
163 | goto again; | |
164 | } | |
165 | ||
29dd101b BJ |
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; | |
b725a0ca | 173 | if ((bno = rmalloc(argmap, ctod(clrnd((int) btoc(NCARGS))))) == 0) { |
29dd101b BJ |
174 | swkill(u.u_procp, "exece"); |
175 | goto bad; | |
176 | } | |
177 | if (bno % CLSIZE) | |
b725a0ca | 178 | panic("execa rmalloc"); |
29dd101b BJ |
179 | if (uap->argp) for (;;) { |
180 | ap = NULL; | |
40d07ebb | 181 | if (indir && (na == 1 || na == 2 && sharg)) |
7eeaac77 RE |
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 | } | |
6459ebe0 | 194 | if (ap == NULL) |
29dd101b BJ |
195 | break; |
196 | na++; | |
e92a04af | 197 | if (ap == -1) |
29dd101b BJ |
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 | } |
6459ebe0 | 212 | if (nc % (CLSIZE*NBPG) == 0) { |
29dd101b BJ |
213 | if (bp) |
214 | bdwrite(bp); | |
6459ebe0 KM |
215 | bp = getblk(argdev, bno + nc / NBPG, |
216 | CLSIZE*NBPG); | |
29dd101b BJ |
217 | cp = bp->b_un.b_addr; |
218 | } | |
219 | nc++; | |
220 | *cp++ = c; | |
6459ebe0 | 221 | } while (c > 0); |
29dd101b BJ |
222 | } |
223 | if (bp) | |
224 | bdwrite(bp); | |
225 | bp = 0; | |
226 | nc = (nc + NBPW-1) & ~(NBPW-1); | |
6459ebe0 KM |
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 | } | |
7eeaac77 | 232 | getxfile(ip, nc + (na+4)*NBPW, uid, gid); |
29a06346 | 233 | if (u.u_error) { |
29dd101b | 234 | badarg: |
6459ebe0 KM |
235 | for (c = 0; c < nc; c += CLSIZE*NBPG) |
236 | if (bp = baddr(argdev, bno + c / NBPG, CLSIZE*NBPG)) { | |
29dd101b BJ |
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 | */ | |
29dd101b BJ |
248 | ucp = USRSTACK - nc - NBPW; |
249 | ap = ucp - na*NBPW - 3*NBPW; | |
250 | u.u_ar0[SP] = ap; | |
81263dba | 251 | (void) suword((caddr_t)ap, na-ne); |
29dd101b BJ |
252 | nc = 0; |
253 | for (;;) { | |
254 | ap += NBPW; | |
255 | if (na==ne) { | |
81263dba | 256 | (void) suword((caddr_t)ap, 0); |
29dd101b BJ |
257 | ap += NBPW; |
258 | } | |
259 | if (--na < 0) | |
260 | break; | |
81263dba | 261 | (void) suword((caddr_t)ap, ucp); |
29dd101b | 262 | do { |
6459ebe0 | 263 | if (nc % (CLSIZE*NBPG) == 0) { |
29dd101b BJ |
264 | if (bp) |
265 | brelse(bp); | |
6459ebe0 KM |
266 | bp = bread(argdev, bno + nc / NBPG, |
267 | CLSIZE*NBPG); | |
29dd101b BJ |
268 | bp->b_flags |= B_AGE; /* throw away */ |
269 | bp->b_flags &= ~B_DELWRI; /* cancel io */ | |
270 | cp = bp->b_un.b_addr; | |
271 | } | |
81263dba | 272 | (void) subyte((caddr_t)ucp++, (c = *cp++)); |
29dd101b BJ |
273 | nc++; |
274 | } while(c&0377); | |
275 | } | |
81263dba BJ |
276 | (void) suword((caddr_t)ap, 0); |
277 | (void) suword((caddr_t)ucp, 0); | |
29dd101b BJ |
278 | setregs(); |
279 | bad: | |
280 | if (bp) | |
281 | brelse(bp); | |
282 | if (bno) | |
b725a0ca | 283 | rmfree(argmap, ctod(clrnd((int) btoc(NCARGS))), bno); |
29dd101b BJ |
284 | iput(ip); |
285 | } | |
286 | ||
287 | /* | |
288 | * Read in and set up memory for executed file. | |
29dd101b | 289 | */ |
7eeaac77 | 290 | getxfile(ip, nargc, uid, gid) |
29dd101b BJ |
291 | register struct inode *ip; |
292 | { | |
29dd101b | 293 | register size_t ts, ds, ss; |
7eeaac77 | 294 | int pagi; |
29dd101b | 295 | |
7eeaac77 | 296 | if (u.u_exdata.ux_mag == 0413) |
29dd101b | 297 | pagi = SPAGI; |
7eeaac77 RE |
298 | else |
299 | pagi = 0; | |
e92a04af BJ |
300 | if (u.u_exdata.ux_tsize!=0 && (ip->i_flag&ITEXT)==0 && |
301 | ip->i_count!=1) { | |
3ca1542b BJ |
302 | register struct file *fp; |
303 | ||
e92a04af | 304 | for (fp = file; fp < fileNFILE; fp++) { |
4147b3f6 BJ |
305 | if (fp->f_type == DTYPE_FILE && |
306 | fp->f_inode == ip && (fp->f_flag&FWRITE)) { | |
3ca1542b BJ |
307 | u.u_error = ETXTBSY; |
308 | goto bad; | |
309 | } | |
e92a04af | 310 | } |
29dd101b BJ |
311 | } |
312 | ||
313 | /* | |
e92a04af | 314 | * Compute text and data sizes and make sure not too large. |
29dd101b | 315 | */ |
29dd101b BJ |
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)); | |
29a06346 BJ |
319 | if (chksize(ts, ds, ss)) |
320 | goto bad; | |
e92a04af BJ |
321 | |
322 | /* | |
323 | * Make sure enough space to start process. | |
324 | */ | |
29a06346 BJ |
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; | |
29dd101b | 329 | |
29a06346 BJ |
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 | } | |
2ce421b6 | 349 | u.u_procp->p_flag &= ~(SPAGI|SSEQL|SUANOM|SNUSIG); |
29a06346 BJ |
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); | |
29dd101b | 354 | |
29a06346 | 355 | if (pagi == 0) { |
29dd101b | 356 | /* |
29a06346 | 357 | * Read in data segment. |
29dd101b | 358 | */ |
29a06346 BJ |
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); | |
29dd101b | 363 | } |
29a06346 BJ |
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 | ||
5b98abb9 BJ |
373 | if (u.u_error) |
374 | swkill(u.u_procp, "i/o error mapping pages"); | |
29a06346 BJ |
375 | /* |
376 | * set SUID/SGID protections, if no tracing | |
377 | */ | |
378 | if ((u.u_procp->p_flag&STRC)==0) { | |
e92a04af BJ |
379 | u.u_uid = uid; |
380 | u.u_procp->p_uid = uid; | |
7eeaac77 | 381 | u.u_gid = gid; |
4f4caf05 | 382 | u.u_grps[gid/(sizeof(int)*8)] |= 1 << (gid%(sizeof(int)*8)); |
29a06346 BJ |
383 | } else |
384 | psignal(u.u_procp, SIGTRAP); | |
29dd101b BJ |
385 | u.u_tsize = ts; |
386 | u.u_dsize = ds; | |
387 | u.u_ssize = ss; | |
29dd101b | 388 | bad: |
29a06346 | 389 | return; |
29dd101b BJ |
390 | } |
391 | ||
392 | /* | |
393 | * Clear registers on exec | |
394 | */ | |
395 | setregs() | |
396 | { | |
594ebedd | 397 | register int (**rp)(); |
29dd101b | 398 | register i; |
bdfe5b0f | 399 | long sigmask; |
29dd101b | 400 | |
4e6e3887 | 401 | for (rp = &u.u_signal[1], sigmask = 1L; rp < &u.u_signal[NSIG]; |
bdfe5b0f BJ |
402 | sigmask <<= 1, rp++) { |
403 | switch (*rp) { | |
404 | ||
405 | case SIG_IGN: | |
406 | case SIG_DFL: | |
407 | case SIG_HOLD: | |
408 | continue; | |
409 | ||
410 | default: | |
411 | /* | |
99fa88a2 | 412 | * Normal or deferring catch; revert to default. |
bdfe5b0f | 413 | */ |
99fa88a2 BJ |
414 | (void) spl6(); |
415 | *rp = SIG_DFL; | |
bdfe5b0f BJ |
416 | if ((int)*rp & 1) |
417 | u.u_procp->p_siga0 |= sigmask; | |
418 | else | |
a4aaef65 | 419 | u.u_procp->p_siga0 &= ~sigmask; |
bdfe5b0f BJ |
420 | if ((int)*rp & 2) |
421 | u.u_procp->p_siga1 |= sigmask; | |
422 | else | |
423 | u.u_procp->p_siga1 &= ~sigmask; | |
99fa88a2 | 424 | (void) spl0(); |
bdfe5b0f BJ |
425 | continue; |
426 | } | |
427 | } | |
29dd101b | 428 | /* |
e92a04af | 429 | for (rp = &u.u_ar0[0]; rp < &u.u_ar0[16];) |
29dd101b BJ |
430 | *rp++ = 0; |
431 | */ | |
432 | u.u_ar0[PC] = u.u_exdata.ux_entloc + 2; /* skip over entry mask */ | |
e92a04af | 433 | for (i=0; i<NOFILE; i++) { |
29dd101b | 434 | if (u.u_pofile[i]&EXCLOSE) { |
a81e9a81 | 435 | closef(u.u_ofile[i], 1, u.u_pofile[i]); |
29dd101b | 436 | u.u_ofile[i] = NULL; |
a81e9a81 | 437 | u.u_pofile[i] = 0; |
29dd101b | 438 | } |
29dd101b | 439 | } |
e92a04af | 440 | |
29dd101b BJ |
441 | /* |
442 | * Remember file name for accounting. | |
443 | */ | |
444 | u.u_acflag &= ~AFORK; | |
6459ebe0 KM |
445 | bcopy((caddr_t)u.u_dent.d_name, (caddr_t)u.u_comm, |
446 | u.u_dent.d_namlen + 1); | |
29dd101b BJ |
447 | } |
448 | ||
449 | /* | |
e92a04af | 450 | * Exit system call: pass back caller's arg |
29dd101b BJ |
451 | */ |
452 | rexit() | |
453 | { | |
454 | register struct a { | |
455 | int rval; | |
456 | } *uap; | |
457 | ||
458 | uap = (struct a *)u.u_ap; | |
459 | exit((uap->rval & 0377) << 8); | |
460 | } | |
461 | ||
462 | /* | |
463 | * Release resources. | |
464 | * Save u. area for parent to look at. | |
465 | * Enter zombie state. | |
466 | * Wake up parent and init processes, | |
467 | * and dispose of children. | |
468 | */ | |
469 | exit(rv) | |
470 | { | |
471 | register int i; | |
472 | register struct proc *p, *q; | |
29dd101b BJ |
473 | register int x; |
474 | ||
475 | #ifdef PGINPROF | |
476 | vmsizmon(); | |
477 | #endif | |
478 | p = u.u_procp; | |
479 | p->p_flag &= ~(STRC|SULOCK); | |
480 | p->p_flag |= SWEXIT; | |
481 | p->p_clktim = 0; | |
bdfe5b0f BJ |
482 | (void) spl6(); |
483 | if ((int)SIG_IGN & 1) | |
484 | p->p_siga0 = ~0; | |
485 | else | |
486 | p->p_siga0 = 0; | |
487 | if ((int)SIG_IGN & 2) | |
488 | p->p_siga1 = ~0; | |
489 | else | |
99fa88a2 | 490 | p->p_siga1 = 0; |
bdfe5b0f | 491 | (void) spl0(); |
dd808ba3 BJ |
492 | p->p_cpticks = 0; |
493 | p->p_pctcpu = 0; | |
e92a04af | 494 | for (i=0; i<NSIG; i++) |
594ebedd | 495 | u.u_signal[i] = SIG_IGN; |
29dd101b BJ |
496 | /* |
497 | * Release virtual memory. If we resulted from | |
498 | * a vfork(), instead give the resources back to | |
499 | * the parent. | |
500 | */ | |
501 | if ((p->p_flag & SVFORK) == 0) | |
502 | vrelvm(); | |
503 | else { | |
504 | p->p_flag &= ~SVFORK; | |
505 | wakeup((caddr_t)p); | |
506 | while ((p->p_flag & SVFDONE) == 0) | |
507 | sleep((caddr_t)p, PZERO - 1); | |
508 | p->p_flag &= ~SVFDONE; | |
509 | } | |
a81e9a81 SL |
510 | for (i = 0; i < NOFILE; i++) { |
511 | #ifdef notdef | |
512 | /* why was this like this? */ | |
29dd101b BJ |
513 | f = u.u_ofile[i]; |
514 | u.u_ofile[i] = NULL; | |
f3156a73 | 515 | closef(f, 1); |
a81e9a81 SL |
516 | #else |
517 | closef(u.u_ofile[i], 1, u.u_pofile[i]); | |
518 | u.u_ofile[i] = NULL; | |
519 | u.u_pofile[i] = 0; | |
520 | #endif | |
29dd101b | 521 | } |
e92a04af | 522 | ilock(u.u_cdir); |
29dd101b BJ |
523 | iput(u.u_cdir); |
524 | if (u.u_rdir) { | |
e92a04af | 525 | ilock(u.u_rdir); |
29dd101b BJ |
526 | iput(u.u_rdir); |
527 | } | |
054016e1 | 528 | u.u_limit[LIM_FSIZE] = INFINITY; |
29dd101b | 529 | acct(); |
feab6b5e RE |
530 | #ifdef QUOTA |
531 | qclean(); | |
532 | #endif | |
29dd101b BJ |
533 | vrelpt(u.u_procp); |
534 | vrelu(u.u_procp, 0); | |
f12a8410 | 535 | (void) spl5(); /* hack for mem alloc race XXX */ |
29dd101b | 536 | multprog--; |
29dd101b | 537 | p->p_stat = SZOMB; |
42343c0f | 538 | noproc = 1; |
29dd101b BJ |
539 | i = PIDHASH(p->p_pid); |
540 | x = p - proc; | |
541 | if (pidhash[i] == x) | |
542 | pidhash[i] = p->p_idhash; | |
543 | else { | |
544 | for (i = pidhash[i]; i != 0; i = proc[i].p_idhash) | |
545 | if (proc[i].p_idhash == x) { | |
546 | proc[i].p_idhash = p->p_idhash; | |
547 | goto done; | |
548 | } | |
549 | panic("exit"); | |
550 | } | |
9c82b9fd BJ |
551 | if (p->p_pid == 1) |
552 | panic("init died"); | |
29dd101b BJ |
553 | done: |
554 | ((struct xproc *)p)->xp_xstat = rv; /* overlay */ | |
555 | ((struct xproc *)p)->xp_vm = u.u_vm; /* overlay */ | |
556 | vmsadd(&((struct xproc *)p)->xp_vm, &u.u_cvm); | |
e92a04af BJ |
557 | for (q = proc; q < procNPROC; q++) |
558 | if (q->p_pptr == p) { | |
feab6b5e RE |
559 | if (q->p_osptr) |
560 | q->p_osptr->p_ysptr = q->p_ysptr; | |
561 | if (q->p_ysptr) | |
562 | q->p_ysptr->p_osptr = q->p_osptr; | |
563 | if (proc[1].p_cptr) | |
564 | proc[1].p_cptr->p_ysptr = q; | |
565 | q->p_osptr = proc[1].p_cptr; | |
566 | q->p_ysptr = NULL; | |
567 | proc[1].p_cptr = q; | |
568 | ||
bdfe5b0f | 569 | q->p_pptr = &proc[1]; |
29dd101b | 570 | q->p_ppid = 1; |
bdfe5b0f BJ |
571 | wakeup((caddr_t)&proc[1]); |
572 | /* | |
0dde1c43 | 573 | * Traced processes are killed |
bdfe5b0f | 574 | * since their existence means someone is screwing up. |
62bac59f | 575 | * Stopped processes are sent a hangup and a continue. |
0dde1c43 BJ |
576 | * This is designed to be ``safe'' for setuid |
577 | * processes since they must be willing to tolerate | |
578 | * hangups anyways. | |
bdfe5b0f | 579 | */ |
0dde1c43 | 580 | if (q->p_flag&STRC) { |
bdfe5b0f BJ |
581 | q->p_flag &= ~STRC; |
582 | psignal(q, SIGKILL); | |
0dde1c43 BJ |
583 | } else if (q->p_stat == SSTOP) { |
584 | psignal(q, SIGHUP); | |
585 | psignal(q, SIGCONT); | |
bdfe5b0f | 586 | } |
8643403f BJ |
587 | /* |
588 | * Protect this process from future | |
7ac93404 | 589 | * tty signals, clear TSTP/TTIN/TTOU if pending. |
8643403f | 590 | */ |
934e4ecf | 591 | (void) spgrp(q, -1); |
29dd101b | 592 | } |
bdfe5b0f | 593 | psignal(p->p_pptr, SIGCHLD); |
4e6e3887 | 594 | wakeup((caddr_t)p->p_pptr); |
29dd101b BJ |
595 | swtch(); |
596 | } | |
597 | ||
598 | wait() | |
599 | { | |
bdfe5b0f BJ |
600 | struct vtimes vm; |
601 | struct vtimes *vp; | |
29dd101b | 602 | |
bdfe5b0f BJ |
603 | if ((u.u_ar0[PS] & PSL_ALLCC) != PSL_ALLCC) { |
604 | wait1(0, (struct vtimes *)0); | |
605 | return; | |
606 | } | |
607 | vp = (struct vtimes *)u.u_ar0[R1]; | |
608 | wait1(u.u_ar0[R0], &vm); | |
609 | if (u.u_error) | |
610 | return; | |
611 | (void) copyout((caddr_t)&vm, (caddr_t)vp, sizeof (struct vtimes)); | |
29dd101b BJ |
612 | } |
613 | ||
614 | /* | |
615 | * Wait system call. | |
616 | * Search for a terminated (zombie) child, | |
617 | * finally lay it to rest, and collect its status. | |
618 | * Look also for stopped (traced) children, | |
619 | * and pass back status from them. | |
620 | */ | |
bdfe5b0f BJ |
621 | wait1(options, vp) |
622 | register options; | |
29dd101b BJ |
623 | struct vtimes *vp; |
624 | { | |
625 | register f; | |
feab6b5e | 626 | register struct proc *p, *q; |
29dd101b BJ |
627 | |
628 | f = 0; | |
29dd101b | 629 | loop: |
e92a04af BJ |
630 | for (p = proc; p < procNPROC; p++) |
631 | if (p->p_pptr == u.u_procp) { | |
29dd101b | 632 | f++; |
e92a04af | 633 | if (p->p_stat == SZOMB) { |
29dd101b BJ |
634 | u.u_r.r_val1 = p->p_pid; |
635 | u.u_r.r_val2 = ((struct xproc *)p)->xp_xstat; | |
636 | ((struct xproc *)p)->xp_xstat = 0; | |
637 | if (vp) | |
638 | *vp = ((struct xproc *)p)->xp_vm; | |
639 | vmsadd(&u.u_cvm, &((struct xproc *)p)->xp_vm); | |
640 | ((struct xproc *)p)->xp_vm = zvms; | |
641 | p->p_stat = NULL; | |
642 | p->p_pid = 0; | |
643 | p->p_ppid = 0; | |
feab6b5e RE |
644 | if (q = p->p_ysptr) |
645 | q->p_osptr = p->p_osptr; | |
646 | if (q = p->p_osptr) | |
647 | q->p_ysptr = p->p_ysptr; | |
648 | if ((q = p->p_pptr)->p_cptr == p) | |
649 | q->p_cptr = p->p_osptr; | |
bdfe5b0f | 650 | p->p_pptr = 0; |
feab6b5e RE |
651 | p->p_ysptr = 0; |
652 | p->p_osptr = 0; | |
653 | p->p_cptr = 0; | |
29dd101b | 654 | p->p_sig = 0; |
bdfe5b0f BJ |
655 | p->p_siga0 = 0; |
656 | p->p_siga1 = 0; | |
29dd101b BJ |
657 | p->p_pgrp = 0; |
658 | p->p_flag = 0; | |
659 | p->p_wchan = 0; | |
bdfe5b0f | 660 | p->p_cursig = 0; |
29dd101b BJ |
661 | return; |
662 | } | |
bdfe5b0f BJ |
663 | if (p->p_stat == SSTOP && (p->p_flag&SWTED)==0 && |
664 | (p->p_flag&STRC || options&WUNTRACED)) { | |
665 | p->p_flag |= SWTED; | |
666 | u.u_r.r_val1 = p->p_pid; | |
667 | u.u_r.r_val2 = (p->p_cursig<<8) | WSTOPPED; | |
668 | return; | |
29dd101b BJ |
669 | } |
670 | } | |
bdfe5b0f BJ |
671 | if (f==0) { |
672 | u.u_error = ECHILD; | |
673 | return; | |
674 | } | |
675 | if (options&WNOHANG) { | |
676 | u.u_r.r_val1 = 0; | |
677 | return; | |
678 | } | |
29a06346 | 679 | if ((u.u_procp->p_flag&SNUSIG) && setjmp(u.u_qsav)) { |
bdfe5b0f BJ |
680 | u.u_eosys = RESTARTSYS; |
681 | return; | |
29dd101b | 682 | } |
bdfe5b0f BJ |
683 | sleep((caddr_t)u.u_procp, PWAIT); |
684 | goto loop; | |
29dd101b BJ |
685 | } |
686 | ||
687 | /* | |
688 | * fork system call. | |
689 | */ | |
690 | fork() | |
691 | { | |
692 | ||
693 | u.u_cdmap = zdmap; | |
694 | u.u_csmap = zdmap; | |
695 | if (swpexpand(u.u_dsize, u.u_ssize, &u.u_cdmap, &u.u_csmap) == 0) { | |
696 | u.u_r.r_val2 = 0; | |
697 | return; | |
698 | } | |
699 | fork1(0); | |
700 | } | |
701 | ||
702 | fork1(isvfork) | |
703 | { | |
704 | register struct proc *p1, *p2; | |
feab6b5e | 705 | #ifndef QUOTA |
29dd101b BJ |
706 | register a; |
707 | ||
708 | a = 0; | |
feab6b5e RE |
709 | #else |
710 | if (u.u_quota != NOQUOT && u.u_quota->q_plim && | |
711 | u.u_quota->q_cnt >= u.u_quota->q_plim) { | |
712 | u.u_error = EPROCLIM; | |
713 | return; | |
714 | } | |
715 | #endif | |
29dd101b | 716 | p2 = NULL; |
e92a04af | 717 | for (p1 = proc; p1 < procNPROC; p1++) { |
feab6b5e RE |
718 | #ifdef QUOTA |
719 | if (p1->p_stat == NULL) { | |
720 | p2 = p1; | |
721 | break; | |
722 | } | |
723 | #else | |
29dd101b BJ |
724 | if (p1->p_stat==NULL && p2==NULL) |
725 | p2 = p1; | |
726 | else { | |
727 | if (p1->p_uid==u.u_uid && p1->p_stat!=NULL) | |
728 | a++; | |
729 | } | |
feab6b5e | 730 | #endif |
29dd101b BJ |
731 | } |
732 | /* | |
733 | * Disallow if | |
734 | * No processes at all; | |
735 | * not su and too many procs owned; or | |
736 | * not su and would take last slot. | |
737 | */ | |
62901f34 BJ |
738 | if (p2==NULL) |
739 | tablefull("proc"); | |
feab6b5e RE |
740 | #ifdef QUOTA |
741 | if (p2==NULL || (u.u_uid!=0 && p2==procNPROC-1)) { | |
742 | #else | |
86fd527f | 743 | if (p2==NULL || (u.u_uid!=0 && (p2==procNPROC-1 || a>MAXUPRC))) { |
feab6b5e | 744 | #endif |
29dd101b BJ |
745 | u.u_error = EAGAIN; |
746 | if (!isvfork) { | |
81263dba BJ |
747 | (void) vsexpand(0, &u.u_cdmap, 1); |
748 | (void) vsexpand(0, &u.u_csmap, 1); | |
29dd101b BJ |
749 | } |
750 | goto out; | |
751 | } | |
752 | p1 = u.u_procp; | |
e92a04af | 753 | if (newproc(isvfork)) { |
29dd101b BJ |
754 | u.u_r.r_val1 = p1->p_pid; |
755 | u.u_r.r_val2 = 1; /* child */ | |
756 | u.u_start = time; | |
757 | u.u_acflag = AFORK; | |
feab6b5e RE |
758 | #ifdef QUOTA |
759 | u.u_qflags &= ~QUF_LOGIN; | |
760 | #endif | |
29dd101b BJ |
761 | return; |
762 | } | |
763 | u.u_r.r_val1 = p2->p_pid; | |
764 | ||
765 | out: | |
766 | u.u_r.r_val2 = 0; | |
767 | } | |
768 | ||
4147b3f6 BJ |
769 | spgrp(top, npgrp) |
770 | register struct proc *top; | |
771 | { | |
772 | register struct proc *pp, *p; | |
773 | int f = 0; | |
774 | ||
775 | for (p = top; npgrp == -1 || u.u_uid == p->p_uid || | |
776 | !u.u_uid || inferior(p); p = pp) { | |
777 | if (npgrp == -1) { | |
778 | #define bit(a) (1<<(a-1)) | |
779 | p->p_sig &= ~(bit(SIGTSTP)|bit(SIGTTIN)|bit(SIGTTOU)); | |
780 | } else | |
781 | p->p_pgrp = npgrp; | |
782 | f++; | |
783 | /* | |
784 | * Search for children. | |
785 | */ | |
786 | for (pp = proc; pp < procNPROC; pp++) | |
787 | if (pp->p_pptr == p) | |
788 | goto cont; | |
789 | /* | |
790 | * Search for siblings. | |
791 | */ | |
792 | for (; p != top; p = p->p_pptr) | |
793 | for (pp = p + 1; pp < procNPROC; pp++) | |
794 | if (pp->p_pptr == p->p_pptr) | |
795 | goto cont; | |
796 | break; | |
797 | cont: | |
798 | ; | |
799 | } | |
800 | return (f); | |
801 | } | |
802 | ||
29dd101b | 803 | /* |
4147b3f6 | 804 | * Is p an inferior of the current process? |
29dd101b | 805 | */ |
4147b3f6 BJ |
806 | inferior(p) |
807 | register struct proc *p; | |
29dd101b | 808 | { |
29dd101b | 809 | |
4147b3f6 BJ |
810 | for (; p != u.u_procp; p = p->p_pptr) |
811 | if (p->p_ppid == 0) | |
812 | return (0); | |
813 | return (1); | |
29dd101b | 814 | } |