Commit | Line | Data |
---|---|---|
4147b3f6 | 1 | /* kern_proc.c 4.28 82/07/24 */ |
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 | ||
373 | /* | |
374 | * set SUID/SGID protections, if no tracing | |
375 | */ | |
376 | if ((u.u_procp->p_flag&STRC)==0) { | |
e92a04af BJ |
377 | u.u_uid = uid; |
378 | u.u_procp->p_uid = uid; | |
7eeaac77 | 379 | u.u_gid = gid; |
4f4caf05 | 380 | u.u_grps[gid/(sizeof(int)*8)] |= 1 << (gid%(sizeof(int)*8)); |
29a06346 BJ |
381 | } else |
382 | psignal(u.u_procp, SIGTRAP); | |
29dd101b BJ |
383 | u.u_tsize = ts; |
384 | u.u_dsize = ds; | |
385 | u.u_ssize = ss; | |
29dd101b | 386 | bad: |
29a06346 | 387 | return; |
29dd101b BJ |
388 | } |
389 | ||
390 | /* | |
391 | * Clear registers on exec | |
392 | */ | |
393 | setregs() | |
394 | { | |
594ebedd | 395 | register int (**rp)(); |
29dd101b | 396 | register i; |
bdfe5b0f | 397 | long sigmask; |
29dd101b | 398 | |
4e6e3887 | 399 | for (rp = &u.u_signal[1], sigmask = 1L; rp < &u.u_signal[NSIG]; |
bdfe5b0f BJ |
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 | /* | |
99fa88a2 | 410 | * Normal or deferring catch; revert to default. |
bdfe5b0f | 411 | */ |
99fa88a2 BJ |
412 | (void) spl6(); |
413 | *rp = SIG_DFL; | |
bdfe5b0f BJ |
414 | if ((int)*rp & 1) |
415 | u.u_procp->p_siga0 |= sigmask; | |
416 | else | |
a4aaef65 | 417 | u.u_procp->p_siga0 &= ~sigmask; |
bdfe5b0f BJ |
418 | if ((int)*rp & 2) |
419 | u.u_procp->p_siga1 |= sigmask; | |
420 | else | |
421 | u.u_procp->p_siga1 &= ~sigmask; | |
99fa88a2 | 422 | (void) spl0(); |
bdfe5b0f BJ |
423 | continue; |
424 | } | |
425 | } | |
29dd101b | 426 | /* |
e92a04af | 427 | for (rp = &u.u_ar0[0]; rp < &u.u_ar0[16];) |
29dd101b BJ |
428 | *rp++ = 0; |
429 | */ | |
430 | u.u_ar0[PC] = u.u_exdata.ux_entloc + 2; /* skip over entry mask */ | |
e92a04af | 431 | for (i=0; i<NOFILE; i++) { |
29dd101b | 432 | if (u.u_pofile[i]&EXCLOSE) { |
f3156a73 | 433 | closef(u.u_ofile[i], 1); |
29dd101b | 434 | u.u_ofile[i] = NULL; |
bdfe5b0f | 435 | u.u_pofile[i] &= ~EXCLOSE; |
29dd101b | 436 | } |
29dd101b | 437 | } |
e92a04af | 438 | |
29dd101b BJ |
439 | /* |
440 | * Remember file name for accounting. | |
441 | */ | |
442 | u.u_acflag &= ~AFORK; | |
6459ebe0 KM |
443 | bcopy((caddr_t)u.u_dent.d_name, (caddr_t)u.u_comm, |
444 | u.u_dent.d_namlen + 1); | |
29dd101b BJ |
445 | } |
446 | ||
447 | /* | |
e92a04af | 448 | * Exit system call: pass back caller's arg |
29dd101b BJ |
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; | |
bdfe5b0f BJ |
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 | |
99fa88a2 | 489 | p->p_siga1 = 0; |
bdfe5b0f | 490 | (void) spl0(); |
dd808ba3 BJ |
491 | p->p_cpticks = 0; |
492 | p->p_pctcpu = 0; | |
e92a04af | 493 | for (i=0; i<NSIG; i++) |
594ebedd | 494 | u.u_signal[i] = SIG_IGN; |
29dd101b BJ |
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 | } | |
e92a04af | 509 | for (i=0; i<NOFILE; i++) { |
29dd101b BJ |
510 | f = u.u_ofile[i]; |
511 | u.u_ofile[i] = NULL; | |
f3156a73 | 512 | closef(f, 1); |
29dd101b | 513 | } |
e92a04af | 514 | ilock(u.u_cdir); |
29dd101b BJ |
515 | iput(u.u_cdir); |
516 | if (u.u_rdir) { | |
e92a04af | 517 | ilock(u.u_rdir); |
29dd101b BJ |
518 | iput(u.u_rdir); |
519 | } | |
054016e1 | 520 | u.u_limit[LIM_FSIZE] = INFINITY; |
29dd101b | 521 | acct(); |
feab6b5e RE |
522 | #ifdef QUOTA |
523 | qclean(); | |
524 | #endif | |
29dd101b BJ |
525 | vrelpt(u.u_procp); |
526 | vrelu(u.u_procp, 0); | |
f12a8410 | 527 | (void) spl5(); /* hack for mem alloc race XXX */ |
29dd101b | 528 | multprog--; |
29dd101b | 529 | p->p_stat = SZOMB; |
42343c0f | 530 | noproc = 1; |
29dd101b BJ |
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 | } | |
9c82b9fd BJ |
543 | if (p->p_pid == 1) |
544 | panic("init died"); | |
29dd101b BJ |
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); | |
e92a04af BJ |
549 | for (q = proc; q < procNPROC; q++) |
550 | if (q->p_pptr == p) { | |
feab6b5e RE |
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 | ||
bdfe5b0f | 561 | q->p_pptr = &proc[1]; |
29dd101b | 562 | q->p_ppid = 1; |
bdfe5b0f BJ |
563 | wakeup((caddr_t)&proc[1]); |
564 | /* | |
0dde1c43 | 565 | * Traced processes are killed |
bdfe5b0f | 566 | * since their existence means someone is screwing up. |
62bac59f | 567 | * Stopped processes are sent a hangup and a continue. |
0dde1c43 BJ |
568 | * This is designed to be ``safe'' for setuid |
569 | * processes since they must be willing to tolerate | |
570 | * hangups anyways. | |
bdfe5b0f | 571 | */ |
0dde1c43 | 572 | if (q->p_flag&STRC) { |
bdfe5b0f BJ |
573 | q->p_flag &= ~STRC; |
574 | psignal(q, SIGKILL); | |
0dde1c43 BJ |
575 | } else if (q->p_stat == SSTOP) { |
576 | psignal(q, SIGHUP); | |
577 | psignal(q, SIGCONT); | |
bdfe5b0f | 578 | } |
8643403f BJ |
579 | /* |
580 | * Protect this process from future | |
7ac93404 | 581 | * tty signals, clear TSTP/TTIN/TTOU if pending. |
8643403f | 582 | */ |
934e4ecf | 583 | (void) spgrp(q, -1); |
29dd101b | 584 | } |
bdfe5b0f | 585 | psignal(p->p_pptr, SIGCHLD); |
4e6e3887 | 586 | wakeup((caddr_t)p->p_pptr); |
29dd101b BJ |
587 | swtch(); |
588 | } | |
589 | ||
590 | wait() | |
591 | { | |
bdfe5b0f BJ |
592 | struct vtimes vm; |
593 | struct vtimes *vp; | |
29dd101b | 594 | |
bdfe5b0f BJ |
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)); | |
29dd101b BJ |
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 | */ | |
bdfe5b0f BJ |
613 | wait1(options, vp) |
614 | register options; | |
29dd101b BJ |
615 | struct vtimes *vp; |
616 | { | |
617 | register f; | |
feab6b5e | 618 | register struct proc *p, *q; |
29dd101b BJ |
619 | |
620 | f = 0; | |
29dd101b | 621 | loop: |
e92a04af BJ |
622 | for (p = proc; p < procNPROC; p++) |
623 | if (p->p_pptr == u.u_procp) { | |
29dd101b | 624 | f++; |
e92a04af | 625 | if (p->p_stat == SZOMB) { |
29dd101b BJ |
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; | |
feab6b5e RE |
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; | |
bdfe5b0f | 642 | p->p_pptr = 0; |
feab6b5e RE |
643 | p->p_ysptr = 0; |
644 | p->p_osptr = 0; | |
645 | p->p_cptr = 0; | |
29dd101b | 646 | p->p_sig = 0; |
bdfe5b0f BJ |
647 | p->p_siga0 = 0; |
648 | p->p_siga1 = 0; | |
29dd101b BJ |
649 | p->p_pgrp = 0; |
650 | p->p_flag = 0; | |
651 | p->p_wchan = 0; | |
bdfe5b0f | 652 | p->p_cursig = 0; |
29dd101b BJ |
653 | return; |
654 | } | |
bdfe5b0f BJ |
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; | |
29dd101b BJ |
661 | } |
662 | } | |
bdfe5b0f BJ |
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 | } | |
29a06346 | 671 | if ((u.u_procp->p_flag&SNUSIG) && setjmp(u.u_qsav)) { |
bdfe5b0f BJ |
672 | u.u_eosys = RESTARTSYS; |
673 | return; | |
29dd101b | 674 | } |
bdfe5b0f BJ |
675 | sleep((caddr_t)u.u_procp, PWAIT); |
676 | goto loop; | |
29dd101b BJ |
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; | |
feab6b5e | 697 | #ifndef QUOTA |
29dd101b BJ |
698 | register a; |
699 | ||
700 | a = 0; | |
feab6b5e RE |
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 | |
29dd101b | 708 | p2 = NULL; |
e92a04af | 709 | for (p1 = proc; p1 < procNPROC; p1++) { |
feab6b5e RE |
710 | #ifdef QUOTA |
711 | if (p1->p_stat == NULL) { | |
712 | p2 = p1; | |
713 | break; | |
714 | } | |
715 | #else | |
29dd101b BJ |
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 | } | |
feab6b5e | 722 | #endif |
29dd101b BJ |
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 | */ | |
62901f34 BJ |
730 | if (p2==NULL) |
731 | tablefull("proc"); | |
feab6b5e RE |
732 | #ifdef QUOTA |
733 | if (p2==NULL || (u.u_uid!=0 && p2==procNPROC-1)) { | |
734 | #else | |
86fd527f | 735 | if (p2==NULL || (u.u_uid!=0 && (p2==procNPROC-1 || a>MAXUPRC))) { |
feab6b5e | 736 | #endif |
29dd101b BJ |
737 | u.u_error = EAGAIN; |
738 | if (!isvfork) { | |
81263dba BJ |
739 | (void) vsexpand(0, &u.u_cdmap, 1); |
740 | (void) vsexpand(0, &u.u_csmap, 1); | |
29dd101b BJ |
741 | } |
742 | goto out; | |
743 | } | |
744 | p1 = u.u_procp; | |
e92a04af | 745 | if (newproc(isvfork)) { |
29dd101b BJ |
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; | |
feab6b5e RE |
750 | #ifdef QUOTA |
751 | u.u_qflags &= ~QUF_LOGIN; | |
752 | #endif | |
29dd101b BJ |
753 | return; |
754 | } | |
755 | u.u_r.r_val1 = p2->p_pid; | |
756 | ||
757 | out: | |
758 | u.u_r.r_val2 = 0; | |
759 | } | |
760 | ||
4147b3f6 BJ |
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 | ||
29dd101b | 795 | /* |
4147b3f6 | 796 | * Is p an inferior of the current process? |
29dd101b | 797 | */ |
4147b3f6 BJ |
798 | inferior(p) |
799 | register struct proc *p; | |
29dd101b | 800 | { |
29dd101b | 801 | |
4147b3f6 BJ |
802 | for (; p != u.u_procp; p = p->p_pptr) |
803 | if (p->p_ppid == 0) | |
804 | return (0); | |
805 | return (1); | |
29dd101b | 806 | } |