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