Commit | Line | Data |
---|---|---|
197da11b | 1 | /* kern_proc.c 4.35 82/08/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" |
6fd40cea | 24 | #include "../h/uio.h" |
29dd101b BJ |
25 | |
26 | /* | |
27 | * exec system call, with and without environments. | |
28 | */ | |
29 | struct execa { | |
30 | char *fname; | |
31 | char **argp; | |
32 | char **envp; | |
33 | }; | |
34 | ||
35 | exec() | |
36 | { | |
37 | ((struct execa *)u.u_ap)->envp = NULL; | |
38 | exece(); | |
39 | } | |
40 | ||
41 | exece() | |
42 | { | |
43 | register nc; | |
44 | register char *cp; | |
45 | register struct buf *bp; | |
46 | register struct execa *uap; | |
47 | int na, ne, ucp, ap, c; | |
7eeaac77 RE |
48 | int indir, uid, gid; |
49 | char *sharg; | |
29dd101b BJ |
50 | struct inode *ip; |
51 | swblk_t bno; | |
6459ebe0 | 52 | char cfname[MAXNAMLEN + 1]; |
7eeaac77 | 53 | char cfarg[SHSIZE]; |
a6b6f679 | 54 | int resid; |
29dd101b | 55 | |
5485e062 | 56 | if ((ip = namei(uchar, 0, 1)) == NULL) |
29dd101b BJ |
57 | return; |
58 | bno = 0; | |
59 | bp = 0; | |
7eeaac77 RE |
60 | indir = 0; |
61 | uid = u.u_uid; | |
62 | gid = u.u_gid; | |
7eeaac77 RE |
63 | if (ip->i_mode & ISUID) |
64 | uid = ip->i_uid; | |
65 | if (ip->i_mode & ISGID) | |
66 | gid = ip->i_gid; | |
67 | ||
68 | again: | |
e92a04af | 69 | if (access(ip, IEXEC)) |
29dd101b | 70 | goto bad; |
e92a04af | 71 | if ((u.u_procp->p_flag&STRC) && access(ip, IREAD)) |
eb83bf86 | 72 | goto bad; |
e92a04af | 73 | if ((ip->i_mode & IFMT) != IFREG || |
29dd101b BJ |
74 | (ip->i_mode & (IEXEC|(IEXEC>>3)|(IEXEC>>6))) == 0) { |
75 | u.u_error = EACCES; | |
76 | goto bad; | |
77 | } | |
7eeaac77 RE |
78 | |
79 | /* | |
80 | * Read in first few bytes of file for segment sizes, ux_mag: | |
81 | * 407 = plain executable | |
82 | * 410 = RO text | |
83 | * 413 = demand paged RO text | |
84 | * Also an ASCII line beginning with #! is | |
85 | * the file name of a ``shell'' and arguments may be prepended | |
86 | * to the argument list if given here. | |
87 | * | |
88 | * SHELL NAMES ARE LIMITED IN LENGTH. | |
89 | * | |
90 | * ONLY ONE ARGUMENT MAY BE PASSED TO THE SHELL FROM | |
91 | * THE ASCII LINE. | |
92 | */ | |
a2a2a0d6 | 93 | u.u_error = rdwri(UIO_READ, ip, (caddr_t)&u.u_exdata, sizeof (u.u_exdata), |
a6b6f679 | 94 | 0, 1, &resid); |
e92a04af | 95 | if (u.u_error) |
7eeaac77 | 96 | goto bad; |
a6b6f679 | 97 | u.u_count = resid; |
a2a2a0d6 | 98 | #ifndef lint |
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 | } | |
a2a2a0d6 | 104 | #endif |
7eeaac77 RE |
105 | switch (u.u_exdata.ux_mag) { |
106 | ||
107 | case 0407: | |
108 | u.u_exdata.ux_dsize += u.u_exdata.ux_tsize; | |
109 | u.u_exdata.ux_tsize = 0; | |
110 | break; | |
111 | ||
112 | case 0413: | |
113 | case 0410: | |
114 | if (u.u_exdata.ux_tsize == 0) { | |
115 | u.u_error = ENOEXEC; | |
116 | goto bad; | |
117 | } | |
118 | break; | |
119 | ||
120 | default: | |
121 | if (u.u_exdata.ux_shell[0] != '#' || | |
122 | u.u_exdata.ux_shell[1] != '!' || | |
123 | indir) { | |
124 | u.u_error = ENOEXEC; | |
125 | goto bad; | |
126 | } | |
127 | cp = &u.u_exdata.ux_shell[2]; /* skip "#!" */ | |
128 | while (cp < &u.u_exdata.ux_shell[SHSIZE]) { | |
129 | if (*cp == '\t') | |
130 | *cp = ' '; | |
131 | else if (*cp == '\n') { | |
132 | *cp = '\0'; | |
133 | break; | |
134 | } | |
135 | cp++; | |
136 | } | |
137 | if (*cp != '\0') { | |
138 | u.u_error = ENOEXEC; | |
139 | goto bad; | |
140 | } | |
141 | cp = &u.u_exdata.ux_shell[2]; | |
142 | while (*cp == ' ') | |
143 | cp++; | |
144 | u.u_dirp = cp; | |
145 | while (*cp && *cp != ' ') | |
146 | cp++; | |
147 | sharg = NULL; | |
148 | if (*cp) { | |
149 | *cp++ = '\0'; | |
150 | while (*cp == ' ') | |
151 | cp++; | |
152 | if (*cp) { | |
153 | bcopy((caddr_t)cp, (caddr_t)cfarg, SHSIZE); | |
154 | sharg = cfarg; | |
155 | } | |
156 | } | |
6459ebe0 | 157 | bcopy((caddr_t)u.u_dent.d_name, (caddr_t)cfname, |
a2a2a0d6 | 158 | (unsigned)(u.u_dent.d_namlen + 1)); |
7eeaac77 RE |
159 | indir = 1; |
160 | iput(ip); | |
5485e062 | 161 | ip = namei(schar, 0, 1); |
7eeaac77 RE |
162 | if (ip == NULL) |
163 | return; | |
164 | goto again; | |
165 | } | |
166 | ||
29dd101b BJ |
167 | /* |
168 | * Collect arguments on "file" in swap space. | |
169 | */ | |
170 | na = 0; | |
171 | ne = 0; | |
172 | nc = 0; | |
173 | uap = (struct execa *)u.u_ap; | |
b725a0ca | 174 | if ((bno = rmalloc(argmap, ctod(clrnd((int) btoc(NCARGS))))) == 0) { |
29dd101b BJ |
175 | swkill(u.u_procp, "exece"); |
176 | goto bad; | |
177 | } | |
178 | if (bno % CLSIZE) | |
b725a0ca | 179 | panic("execa rmalloc"); |
29dd101b BJ |
180 | if (uap->argp) for (;;) { |
181 | ap = NULL; | |
40d07ebb | 182 | if (indir && (na == 1 || na == 2 && sharg)) |
7eeaac77 RE |
183 | ap = (int)uap->fname; |
184 | else if (uap->argp) { | |
29dd101b BJ |
185 | ap = fuword((caddr_t)uap->argp); |
186 | uap->argp++; | |
187 | } | |
188 | if (ap==NULL && uap->envp) { | |
189 | uap->argp = NULL; | |
190 | if ((ap = fuword((caddr_t)uap->envp)) == NULL) | |
191 | break; | |
192 | uap->envp++; | |
193 | ne++; | |
194 | } | |
6459ebe0 | 195 | if (ap == NULL) |
29dd101b BJ |
196 | break; |
197 | na++; | |
e92a04af | 198 | if (ap == -1) |
29dd101b BJ |
199 | u.u_error = EFAULT; |
200 | do { | |
201 | if (nc >= NCARGS-1) | |
202 | u.u_error = E2BIG; | |
7eeaac77 RE |
203 | if (indir && na == 2 && sharg != NULL) |
204 | c = *sharg++ & 0377; | |
205 | else if ((c = fubyte((caddr_t)ap++)) < 0) | |
29dd101b | 206 | u.u_error = EFAULT; |
64d6118b BJ |
207 | if (u.u_error) { |
208 | if (bp) | |
209 | brelse(bp); | |
210 | bp = 0; | |
29dd101b | 211 | goto badarg; |
64d6118b | 212 | } |
6459ebe0 | 213 | if (nc % (CLSIZE*NBPG) == 0) { |
29dd101b BJ |
214 | if (bp) |
215 | bdwrite(bp); | |
6459ebe0 KM |
216 | bp = getblk(argdev, bno + nc / NBPG, |
217 | CLSIZE*NBPG); | |
29dd101b BJ |
218 | cp = bp->b_un.b_addr; |
219 | } | |
220 | nc++; | |
221 | *cp++ = c; | |
6459ebe0 | 222 | } while (c > 0); |
29dd101b BJ |
223 | } |
224 | if (bp) | |
225 | bdwrite(bp); | |
226 | bp = 0; | |
227 | nc = (nc + NBPW-1) & ~(NBPW-1); | |
6459ebe0 KM |
228 | if (indir) { |
229 | u.u_dent.d_namlen = strlen(cfname); | |
230 | bcopy((caddr_t)cfname, (caddr_t)u.u_dent.d_name, | |
a2a2a0d6 | 231 | (unsigned)(u.u_dent.d_namlen + 1)); |
6459ebe0 | 232 | } |
7eeaac77 | 233 | getxfile(ip, nc + (na+4)*NBPW, uid, gid); |
29a06346 | 234 | if (u.u_error) { |
29dd101b | 235 | badarg: |
6459ebe0 KM |
236 | for (c = 0; c < nc; c += CLSIZE*NBPG) |
237 | if (bp = baddr(argdev, bno + c / NBPG, CLSIZE*NBPG)) { | |
29dd101b BJ |
238 | bp->b_flags |= B_AGE; /* throw away */ |
239 | bp->b_flags &= ~B_DELWRI; /* cancel io */ | |
240 | brelse(bp); | |
241 | bp = 0; | |
242 | } | |
243 | goto bad; | |
244 | } | |
245 | ||
246 | /* | |
247 | * copy back arglist | |
248 | */ | |
29dd101b BJ |
249 | ucp = USRSTACK - nc - NBPW; |
250 | ap = ucp - na*NBPW - 3*NBPW; | |
251 | u.u_ar0[SP] = ap; | |
81263dba | 252 | (void) suword((caddr_t)ap, na-ne); |
29dd101b BJ |
253 | nc = 0; |
254 | for (;;) { | |
255 | ap += NBPW; | |
256 | if (na==ne) { | |
81263dba | 257 | (void) suword((caddr_t)ap, 0); |
29dd101b BJ |
258 | ap += NBPW; |
259 | } | |
260 | if (--na < 0) | |
261 | break; | |
81263dba | 262 | (void) suword((caddr_t)ap, ucp); |
29dd101b | 263 | do { |
6459ebe0 | 264 | if (nc % (CLSIZE*NBPG) == 0) { |
29dd101b BJ |
265 | if (bp) |
266 | brelse(bp); | |
6459ebe0 KM |
267 | bp = bread(argdev, bno + nc / NBPG, |
268 | CLSIZE*NBPG); | |
29dd101b BJ |
269 | bp->b_flags |= B_AGE; /* throw away */ |
270 | bp->b_flags &= ~B_DELWRI; /* cancel io */ | |
271 | cp = bp->b_un.b_addr; | |
272 | } | |
81263dba | 273 | (void) subyte((caddr_t)ucp++, (c = *cp++)); |
29dd101b BJ |
274 | nc++; |
275 | } while(c&0377); | |
276 | } | |
81263dba BJ |
277 | (void) suword((caddr_t)ap, 0); |
278 | (void) suword((caddr_t)ucp, 0); | |
29dd101b BJ |
279 | setregs(); |
280 | bad: | |
281 | if (bp) | |
282 | brelse(bp); | |
283 | if (bno) | |
b725a0ca | 284 | rmfree(argmap, ctod(clrnd((int) btoc(NCARGS))), bno); |
29dd101b BJ |
285 | iput(ip); |
286 | } | |
287 | ||
288 | /* | |
289 | * Read in and set up memory for executed file. | |
29dd101b | 290 | */ |
7eeaac77 | 291 | getxfile(ip, nargc, uid, gid) |
29dd101b BJ |
292 | register struct inode *ip; |
293 | { | |
29dd101b | 294 | register size_t ts, ds, ss; |
7eeaac77 | 295 | int pagi; |
29dd101b | 296 | |
7eeaac77 | 297 | if (u.u_exdata.ux_mag == 0413) |
29dd101b | 298 | pagi = SPAGI; |
7eeaac77 RE |
299 | else |
300 | pagi = 0; | |
e92a04af BJ |
301 | if (u.u_exdata.ux_tsize!=0 && (ip->i_flag&ITEXT)==0 && |
302 | ip->i_count!=1) { | |
3ca1542b BJ |
303 | register struct file *fp; |
304 | ||
e92a04af | 305 | for (fp = file; fp < fileNFILE; fp++) { |
4147b3f6 BJ |
306 | if (fp->f_type == DTYPE_FILE && |
307 | fp->f_inode == ip && (fp->f_flag&FWRITE)) { | |
3ca1542b BJ |
308 | u.u_error = ETXTBSY; |
309 | goto bad; | |
310 | } | |
e92a04af | 311 | } |
29dd101b BJ |
312 | } |
313 | ||
314 | /* | |
e92a04af | 315 | * Compute text and data sizes and make sure not too large. |
29dd101b | 316 | */ |
29dd101b BJ |
317 | ts = clrnd(btoc(u.u_exdata.ux_tsize)); |
318 | ds = clrnd(btoc((u.u_exdata.ux_dsize+u.u_exdata.ux_bsize))); | |
319 | ss = clrnd(SSIZE + btoc(nargc)); | |
29a06346 BJ |
320 | if (chksize(ts, ds, ss)) |
321 | goto bad; | |
e92a04af BJ |
322 | |
323 | /* | |
324 | * Make sure enough space to start process. | |
325 | */ | |
29a06346 BJ |
326 | u.u_cdmap = zdmap; |
327 | u.u_csmap = zdmap; | |
328 | if (swpexpand(ds, ss, &u.u_cdmap, &u.u_csmap) == NULL) | |
329 | goto bad; | |
29dd101b | 330 | |
29a06346 BJ |
331 | /* |
332 | * At this point, committed to the new image! | |
333 | * Release virtual memory resources of old process, and | |
334 | * initialize the virtual memory of the new process. | |
335 | * If we resulted from vfork(), instead wakeup our | |
336 | * parent who will set SVFDONE when he has taken back | |
337 | * our resources. | |
338 | */ | |
339 | u.u_prof.pr_scale = 0; | |
340 | if ((u.u_procp->p_flag & SVFORK) == 0) | |
341 | vrelvm(); | |
342 | else { | |
343 | u.u_procp->p_flag &= ~SVFORK; | |
344 | u.u_procp->p_flag |= SKEEP; | |
345 | wakeup((caddr_t)u.u_procp); | |
346 | while ((u.u_procp->p_flag & SVFDONE) == 0) | |
347 | sleep((caddr_t)u.u_procp, PZERO - 1); | |
348 | u.u_procp->p_flag &= ~(SVFDONE|SKEEP); | |
349 | } | |
2ce421b6 | 350 | u.u_procp->p_flag &= ~(SPAGI|SSEQL|SUANOM|SNUSIG); |
29a06346 BJ |
351 | u.u_procp->p_flag |= pagi; |
352 | u.u_dmap = u.u_cdmap; | |
353 | u.u_smap = u.u_csmap; | |
354 | vgetvm(ts, ds, ss); | |
29dd101b | 355 | |
a6b6f679 | 356 | if (pagi == 0) |
a2a2a0d6 BJ |
357 | u.u_error = |
358 | rdwri(UIO_READ, ip, | |
359 | (char*)ctob(ts), (int)u.u_exdata.ux_dsize, | |
360 | (int)(sizeof(u.u_exdata)+u.u_exdata.ux_tsize), | |
361 | 0, (int *)0); | |
29a06346 BJ |
362 | xalloc(ip, pagi); |
363 | if (pagi && u.u_procp->p_textp) | |
364 | vinifod((struct fpte *)dptopte(u.u_procp, 0), | |
365 | PG_FTEXT, u.u_procp->p_textp->x_iptr, | |
366 | 1 + ts/CLSIZE, (int)btoc(u.u_exdata.ux_dsize)); | |
367 | ||
368 | /* THIS SHOULD BE DONE AT A LOWER LEVEL, IF AT ALL */ | |
369 | mtpr(TBIA, 0); | |
370 | ||
5b98abb9 BJ |
371 | if (u.u_error) |
372 | swkill(u.u_procp, "i/o error mapping pages"); | |
29a06346 BJ |
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; |
197da11b | 380 | entergroup(gid); |
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) { |
a81e9a81 | 433 | closef(u.u_ofile[i], 1, u.u_pofile[i]); |
29dd101b | 434 | u.u_ofile[i] = NULL; |
a81e9a81 | 435 | u.u_pofile[i] = 0; |
29dd101b | 436 | } |
29dd101b | 437 | } |
e92a04af | 438 | |
29dd101b BJ |
439 | /* |
440 | * Remember file name for accounting. | |
441 | */ | |
442 | u.u_acflag &= ~AFORK; | |
6459ebe0 | 443 | bcopy((caddr_t)u.u_dent.d_name, (caddr_t)u.u_comm, |
a2a2a0d6 | 444 | (unsigned)(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; | |
29dd101b BJ |
471 | register int x; |
472 | ||
473 | #ifdef PGINPROF | |
474 | vmsizmon(); | |
475 | #endif | |
476 | p = u.u_procp; | |
477 | p->p_flag &= ~(STRC|SULOCK); | |
478 | p->p_flag |= SWEXIT; | |
479 | p->p_clktim = 0; | |
bdfe5b0f BJ |
480 | (void) spl6(); |
481 | if ((int)SIG_IGN & 1) | |
482 | p->p_siga0 = ~0; | |
483 | else | |
484 | p->p_siga0 = 0; | |
485 | if ((int)SIG_IGN & 2) | |
486 | p->p_siga1 = ~0; | |
487 | else | |
99fa88a2 | 488 | p->p_siga1 = 0; |
bdfe5b0f | 489 | (void) spl0(); |
dd808ba3 BJ |
490 | p->p_cpticks = 0; |
491 | p->p_pctcpu = 0; | |
e92a04af | 492 | for (i=0; i<NSIG; i++) |
594ebedd | 493 | u.u_signal[i] = SIG_IGN; |
29dd101b BJ |
494 | /* |
495 | * Release virtual memory. If we resulted from | |
496 | * a vfork(), instead give the resources back to | |
497 | * the parent. | |
498 | */ | |
499 | if ((p->p_flag & SVFORK) == 0) | |
500 | vrelvm(); | |
501 | else { | |
502 | p->p_flag &= ~SVFORK; | |
503 | wakeup((caddr_t)p); | |
504 | while ((p->p_flag & SVFDONE) == 0) | |
505 | sleep((caddr_t)p, PZERO - 1); | |
506 | p->p_flag &= ~SVFDONE; | |
507 | } | |
a81e9a81 SL |
508 | for (i = 0; i < NOFILE; i++) { |
509 | #ifdef notdef | |
510 | /* why was this like this? */ | |
29dd101b BJ |
511 | f = u.u_ofile[i]; |
512 | u.u_ofile[i] = NULL; | |
f3156a73 | 513 | closef(f, 1); |
a81e9a81 SL |
514 | #else |
515 | closef(u.u_ofile[i], 1, u.u_pofile[i]); | |
516 | u.u_ofile[i] = NULL; | |
517 | u.u_pofile[i] = 0; | |
518 | #endif | |
29dd101b | 519 | } |
e92a04af | 520 | ilock(u.u_cdir); |
29dd101b BJ |
521 | iput(u.u_cdir); |
522 | if (u.u_rdir) { | |
e92a04af | 523 | ilock(u.u_rdir); |
29dd101b BJ |
524 | iput(u.u_rdir); |
525 | } | |
054016e1 | 526 | u.u_limit[LIM_FSIZE] = INFINITY; |
29dd101b | 527 | acct(); |
feab6b5e RE |
528 | #ifdef QUOTA |
529 | qclean(); | |
530 | #endif | |
29dd101b BJ |
531 | vrelpt(u.u_procp); |
532 | vrelu(u.u_procp, 0); | |
f12a8410 | 533 | (void) spl5(); /* hack for mem alloc race XXX */ |
29dd101b | 534 | multprog--; |
29dd101b | 535 | p->p_stat = SZOMB; |
42343c0f | 536 | noproc = 1; |
29dd101b BJ |
537 | i = PIDHASH(p->p_pid); |
538 | x = p - proc; | |
539 | if (pidhash[i] == x) | |
540 | pidhash[i] = p->p_idhash; | |
541 | else { | |
542 | for (i = pidhash[i]; i != 0; i = proc[i].p_idhash) | |
543 | if (proc[i].p_idhash == x) { | |
544 | proc[i].p_idhash = p->p_idhash; | |
545 | goto done; | |
546 | } | |
547 | panic("exit"); | |
548 | } | |
9c82b9fd BJ |
549 | if (p->p_pid == 1) |
550 | panic("init died"); | |
29dd101b BJ |
551 | done: |
552 | ((struct xproc *)p)->xp_xstat = rv; /* overlay */ | |
553 | ((struct xproc *)p)->xp_vm = u.u_vm; /* overlay */ | |
554 | vmsadd(&((struct xproc *)p)->xp_vm, &u.u_cvm); | |
e92a04af BJ |
555 | for (q = proc; q < procNPROC; q++) |
556 | if (q->p_pptr == p) { | |
feab6b5e RE |
557 | if (q->p_osptr) |
558 | q->p_osptr->p_ysptr = q->p_ysptr; | |
559 | if (q->p_ysptr) | |
560 | q->p_ysptr->p_osptr = q->p_osptr; | |
561 | if (proc[1].p_cptr) | |
562 | proc[1].p_cptr->p_ysptr = q; | |
563 | q->p_osptr = proc[1].p_cptr; | |
564 | q->p_ysptr = NULL; | |
565 | proc[1].p_cptr = q; | |
566 | ||
bdfe5b0f | 567 | q->p_pptr = &proc[1]; |
29dd101b | 568 | q->p_ppid = 1; |
bdfe5b0f BJ |
569 | wakeup((caddr_t)&proc[1]); |
570 | /* | |
0dde1c43 | 571 | * Traced processes are killed |
bdfe5b0f | 572 | * since their existence means someone is screwing up. |
62bac59f | 573 | * Stopped processes are sent a hangup and a continue. |
0dde1c43 BJ |
574 | * This is designed to be ``safe'' for setuid |
575 | * processes since they must be willing to tolerate | |
576 | * hangups anyways. | |
bdfe5b0f | 577 | */ |
0dde1c43 | 578 | if (q->p_flag&STRC) { |
bdfe5b0f BJ |
579 | q->p_flag &= ~STRC; |
580 | psignal(q, SIGKILL); | |
0dde1c43 BJ |
581 | } else if (q->p_stat == SSTOP) { |
582 | psignal(q, SIGHUP); | |
583 | psignal(q, SIGCONT); | |
bdfe5b0f | 584 | } |
8643403f BJ |
585 | /* |
586 | * Protect this process from future | |
7ac93404 | 587 | * tty signals, clear TSTP/TTIN/TTOU if pending. |
8643403f | 588 | */ |
934e4ecf | 589 | (void) spgrp(q, -1); |
29dd101b | 590 | } |
bdfe5b0f | 591 | psignal(p->p_pptr, SIGCHLD); |
4e6e3887 | 592 | wakeup((caddr_t)p->p_pptr); |
29dd101b BJ |
593 | swtch(); |
594 | } | |
595 | ||
596 | wait() | |
597 | { | |
bdfe5b0f BJ |
598 | struct vtimes vm; |
599 | struct vtimes *vp; | |
29dd101b | 600 | |
bdfe5b0f BJ |
601 | if ((u.u_ar0[PS] & PSL_ALLCC) != PSL_ALLCC) { |
602 | wait1(0, (struct vtimes *)0); | |
603 | return; | |
604 | } | |
605 | vp = (struct vtimes *)u.u_ar0[R1]; | |
606 | wait1(u.u_ar0[R0], &vm); | |
607 | if (u.u_error) | |
608 | return; | |
609 | (void) copyout((caddr_t)&vm, (caddr_t)vp, sizeof (struct vtimes)); | |
29dd101b BJ |
610 | } |
611 | ||
612 | /* | |
613 | * Wait system call. | |
614 | * Search for a terminated (zombie) child, | |
615 | * finally lay it to rest, and collect its status. | |
616 | * Look also for stopped (traced) children, | |
617 | * and pass back status from them. | |
618 | */ | |
bdfe5b0f BJ |
619 | wait1(options, vp) |
620 | register options; | |
29dd101b BJ |
621 | struct vtimes *vp; |
622 | { | |
623 | register f; | |
feab6b5e | 624 | register struct proc *p, *q; |
29dd101b BJ |
625 | |
626 | f = 0; | |
29dd101b | 627 | loop: |
e92a04af BJ |
628 | for (p = proc; p < procNPROC; p++) |
629 | if (p->p_pptr == u.u_procp) { | |
29dd101b | 630 | f++; |
e92a04af | 631 | if (p->p_stat == SZOMB) { |
29dd101b BJ |
632 | u.u_r.r_val1 = p->p_pid; |
633 | u.u_r.r_val2 = ((struct xproc *)p)->xp_xstat; | |
634 | ((struct xproc *)p)->xp_xstat = 0; | |
635 | if (vp) | |
636 | *vp = ((struct xproc *)p)->xp_vm; | |
637 | vmsadd(&u.u_cvm, &((struct xproc *)p)->xp_vm); | |
638 | ((struct xproc *)p)->xp_vm = zvms; | |
639 | p->p_stat = NULL; | |
640 | p->p_pid = 0; | |
641 | p->p_ppid = 0; | |
feab6b5e RE |
642 | if (q = p->p_ysptr) |
643 | q->p_osptr = p->p_osptr; | |
644 | if (q = p->p_osptr) | |
645 | q->p_ysptr = p->p_ysptr; | |
646 | if ((q = p->p_pptr)->p_cptr == p) | |
647 | q->p_cptr = p->p_osptr; | |
bdfe5b0f | 648 | p->p_pptr = 0; |
feab6b5e RE |
649 | p->p_ysptr = 0; |
650 | p->p_osptr = 0; | |
651 | p->p_cptr = 0; | |
29dd101b | 652 | p->p_sig = 0; |
bdfe5b0f BJ |
653 | p->p_siga0 = 0; |
654 | p->p_siga1 = 0; | |
29dd101b BJ |
655 | p->p_pgrp = 0; |
656 | p->p_flag = 0; | |
657 | p->p_wchan = 0; | |
bdfe5b0f | 658 | p->p_cursig = 0; |
29dd101b BJ |
659 | return; |
660 | } | |
bdfe5b0f BJ |
661 | if (p->p_stat == SSTOP && (p->p_flag&SWTED)==0 && |
662 | (p->p_flag&STRC || options&WUNTRACED)) { | |
663 | p->p_flag |= SWTED; | |
664 | u.u_r.r_val1 = p->p_pid; | |
665 | u.u_r.r_val2 = (p->p_cursig<<8) | WSTOPPED; | |
666 | return; | |
29dd101b BJ |
667 | } |
668 | } | |
bdfe5b0f BJ |
669 | if (f==0) { |
670 | u.u_error = ECHILD; | |
671 | return; | |
672 | } | |
673 | if (options&WNOHANG) { | |
674 | u.u_r.r_val1 = 0; | |
675 | return; | |
676 | } | |
29a06346 | 677 | if ((u.u_procp->p_flag&SNUSIG) && setjmp(u.u_qsav)) { |
bdfe5b0f BJ |
678 | u.u_eosys = RESTARTSYS; |
679 | return; | |
29dd101b | 680 | } |
bdfe5b0f BJ |
681 | sleep((caddr_t)u.u_procp, PWAIT); |
682 | goto loop; | |
29dd101b BJ |
683 | } |
684 | ||
685 | /* | |
686 | * fork system call. | |
687 | */ | |
688 | fork() | |
689 | { | |
690 | ||
691 | u.u_cdmap = zdmap; | |
692 | u.u_csmap = zdmap; | |
693 | if (swpexpand(u.u_dsize, u.u_ssize, &u.u_cdmap, &u.u_csmap) == 0) { | |
694 | u.u_r.r_val2 = 0; | |
695 | return; | |
696 | } | |
697 | fork1(0); | |
698 | } | |
699 | ||
700 | fork1(isvfork) | |
701 | { | |
702 | register struct proc *p1, *p2; | |
feab6b5e | 703 | #ifndef QUOTA |
29dd101b BJ |
704 | register a; |
705 | ||
706 | a = 0; | |
feab6b5e RE |
707 | #else |
708 | if (u.u_quota != NOQUOT && u.u_quota->q_plim && | |
709 | u.u_quota->q_cnt >= u.u_quota->q_plim) { | |
710 | u.u_error = EPROCLIM; | |
711 | return; | |
712 | } | |
713 | #endif | |
29dd101b | 714 | p2 = NULL; |
e92a04af | 715 | for (p1 = proc; p1 < procNPROC; p1++) { |
feab6b5e RE |
716 | #ifdef QUOTA |
717 | if (p1->p_stat == NULL) { | |
718 | p2 = p1; | |
719 | break; | |
720 | } | |
721 | #else | |
29dd101b BJ |
722 | if (p1->p_stat==NULL && p2==NULL) |
723 | p2 = p1; | |
724 | else { | |
725 | if (p1->p_uid==u.u_uid && p1->p_stat!=NULL) | |
726 | a++; | |
727 | } | |
feab6b5e | 728 | #endif |
29dd101b BJ |
729 | } |
730 | /* | |
731 | * Disallow if | |
732 | * No processes at all; | |
733 | * not su and too many procs owned; or | |
734 | * not su and would take last slot. | |
735 | */ | |
62901f34 BJ |
736 | if (p2==NULL) |
737 | tablefull("proc"); | |
feab6b5e RE |
738 | #ifdef QUOTA |
739 | if (p2==NULL || (u.u_uid!=0 && p2==procNPROC-1)) { | |
740 | #else | |
86fd527f | 741 | if (p2==NULL || (u.u_uid!=0 && (p2==procNPROC-1 || a>MAXUPRC))) { |
feab6b5e | 742 | #endif |
29dd101b BJ |
743 | u.u_error = EAGAIN; |
744 | if (!isvfork) { | |
81263dba BJ |
745 | (void) vsexpand(0, &u.u_cdmap, 1); |
746 | (void) vsexpand(0, &u.u_csmap, 1); | |
29dd101b BJ |
747 | } |
748 | goto out; | |
749 | } | |
750 | p1 = u.u_procp; | |
e92a04af | 751 | if (newproc(isvfork)) { |
29dd101b BJ |
752 | u.u_r.r_val1 = p1->p_pid; |
753 | u.u_r.r_val2 = 1; /* child */ | |
754 | u.u_start = time; | |
755 | u.u_acflag = AFORK; | |
feab6b5e RE |
756 | #ifdef QUOTA |
757 | u.u_qflags &= ~QUF_LOGIN; | |
758 | #endif | |
29dd101b BJ |
759 | return; |
760 | } | |
761 | u.u_r.r_val1 = p2->p_pid; | |
762 | ||
763 | out: | |
764 | u.u_r.r_val2 = 0; | |
765 | } | |
766 | ||
4147b3f6 BJ |
767 | spgrp(top, npgrp) |
768 | register struct proc *top; | |
769 | { | |
770 | register struct proc *pp, *p; | |
771 | int f = 0; | |
772 | ||
773 | for (p = top; npgrp == -1 || u.u_uid == p->p_uid || | |
774 | !u.u_uid || inferior(p); p = pp) { | |
775 | if (npgrp == -1) { | |
776 | #define bit(a) (1<<(a-1)) | |
777 | p->p_sig &= ~(bit(SIGTSTP)|bit(SIGTTIN)|bit(SIGTTOU)); | |
778 | } else | |
779 | p->p_pgrp = npgrp; | |
780 | f++; | |
781 | /* | |
782 | * Search for children. | |
783 | */ | |
784 | for (pp = proc; pp < procNPROC; pp++) | |
785 | if (pp->p_pptr == p) | |
786 | goto cont; | |
787 | /* | |
788 | * Search for siblings. | |
789 | */ | |
790 | for (; p != top; p = p->p_pptr) | |
791 | for (pp = p + 1; pp < procNPROC; pp++) | |
792 | if (pp->p_pptr == p->p_pptr) | |
793 | goto cont; | |
794 | break; | |
795 | cont: | |
796 | ; | |
797 | } | |
798 | return (f); | |
799 | } | |
800 | ||
29dd101b | 801 | /* |
4147b3f6 | 802 | * Is p an inferior of the current process? |
29dd101b | 803 | */ |
4147b3f6 | 804 | inferior(p) |
a2a2a0d6 | 805 | register struct proc *p; |
29dd101b | 806 | { |
29dd101b | 807 | |
4147b3f6 BJ |
808 | for (; p != u.u_procp; p = p->p_pptr) |
809 | if (p->p_ppid == 0) | |
810 | return (0); | |
811 | return (1); | |
29dd101b | 812 | } |
a2a2a0d6 BJ |
813 | |
814 | struct proc * | |
815 | pfind(pid) | |
816 | int pid; | |
817 | { | |
818 | register struct proc *p; | |
819 | ||
820 | for (p = &proc[pidhash[PIDHASH(pid)]]; p != &proc[0]; p = &proc[p->p_idhash]) | |
821 | if (p->p_pid == pid) | |
822 | return (p); | |
823 | return ((struct proc *)0); | |
824 | } |