kern_exit.c revision 1.66 1 /* $NetBSD: kern_exit.c,v 1.66 1999/04/30 20:54:04 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 /*
41 * Copyright (c) 1982, 1986, 1989, 1991, 1993
42 * The Regents of the University of California. All rights reserved.
43 * (c) UNIX System Laboratories, Inc.
44 * All or some portions of this file are derived from material licensed
45 * to the University of California by American Telephone and Telegraph
46 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
47 * the permission of UNIX System Laboratories, Inc.
48 *
49 * Redistribution and use in source and binary forms, with or without
50 * modification, are permitted provided that the following conditions
51 * are met:
52 * 1. Redistributions of source code must retain the above copyright
53 * notice, this list of conditions and the following disclaimer.
54 * 2. Redistributions in binary form must reproduce the above copyright
55 * notice, this list of conditions and the following disclaimer in the
56 * documentation and/or other materials provided with the distribution.
57 * 3. All advertising materials mentioning features or use of this software
58 * must display the following acknowledgement:
59 * This product includes software developed by the University of
60 * California, Berkeley and its contributors.
61 * 4. Neither the name of the University nor the names of its contributors
62 * may be used to endorse or promote products derived from this software
63 * without specific prior written permission.
64 *
65 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
66 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
67 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
68 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
69 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
70 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
71 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
72 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
73 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
74 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
75 * SUCH DAMAGE.
76 *
77 * @(#)kern_exit.c 8.10 (Berkeley) 2/23/95
78 */
79
80 #include "opt_ktrace.h"
81 #include "opt_sysv.h"
82
83 #include <sys/param.h>
84 #include <sys/systm.h>
85 #include <sys/map.h>
86 #include <sys/ioctl.h>
87 #include <sys/proc.h>
88 #include <sys/tty.h>
89 #include <sys/time.h>
90 #include <sys/resource.h>
91 #include <sys/kernel.h>
92 #include <sys/ktrace.h>
93 #include <sys/proc.h>
94 #include <sys/buf.h>
95 #include <sys/wait.h>
96 #include <sys/file.h>
97 #include <sys/vnode.h>
98 #include <sys/syslog.h>
99 #include <sys/malloc.h>
100 #include <sys/pool.h>
101 #include <sys/resourcevar.h>
102 #include <sys/ptrace.h>
103 #include <sys/acct.h>
104 #include <sys/filedesc.h>
105 #include <sys/signalvar.h>
106 #include <sys/sched.h>
107 #ifdef SYSVSHM
108 #include <sys/shm.h>
109 #endif
110 #ifdef SYSVSEM
111 #include <sys/sem.h>
112 #endif
113
114 #include <sys/mount.h>
115 #include <sys/syscallargs.h>
116
117 #include <machine/cpu.h>
118
119 #include <vm/vm.h>
120 #include <vm/vm_kern.h>
121
122 #include <uvm/uvm_extern.h>
123
124 /*
125 * exit --
126 * Death of process.
127 */
128 int
129 sys_exit(p, v, retval)
130 struct proc *p;
131 void *v;
132 register_t *retval;
133 {
134 struct sys_exit_args /* {
135 syscallarg(int) rval;
136 } */ *uap = v;
137
138 exit1(p, W_EXITCODE(SCARG(uap, rval), 0));
139 /* NOTREACHED */
140 return (0);
141 }
142
143 /*
144 * Exit: deallocate address space and other resources, change proc state
145 * to zombie, and unlink proc from allproc and parent's lists. Save exit
146 * status and rusage for wait(). Check for child processes and orphan them.
147 */
148 void
149 exit1(p, rv)
150 register struct proc *p;
151 int rv;
152 {
153 register struct proc *q, *nq;
154 register struct vmspace *vm;
155
156 if (p->p_pid == 1)
157 panic("init died (signal %d, exit %d)",
158 WTERMSIG(rv), WEXITSTATUS(rv));
159 #ifdef PGINPROF
160 vmsizmon();
161 #endif
162 if (p->p_flag & P_PROFIL)
163 stopprofclock(p);
164 p->p_ru = pool_get(&rusage_pool, PR_WAITOK);
165 /*
166 * If parent is waiting for us to exit or exec, P_PPWAIT is set; we
167 * wake up the parent early to avoid deadlock.
168 */
169 p->p_flag |= P_WEXIT;
170 if (p->p_flag & P_PPWAIT) {
171 p->p_flag &= ~P_PPWAIT;
172 wakeup((caddr_t)p->p_pptr);
173 }
174 sigfillset(&p->p_sigignore);
175 sigemptyset(&p->p_siglist);
176 p->p_sigcheck = 0;
177 untimeout(realitexpire, (caddr_t)p);
178
179 /*
180 * Close open files and release open-file table.
181 * This may block!
182 */
183 fdfree(p);
184 cwdfree(p);
185
186 /* The next three chunks should probably be moved to vmspace_exit. */
187 vm = p->p_vmspace;
188 #ifdef SYSVSHM
189 if (vm->vm_shm && vm->vm_refcnt == 1)
190 shmexit(vm);
191 #endif
192 #ifdef SYSVSEM
193 semexit(p);
194 #endif
195 /*
196 * Release user portion of address space.
197 * This releases references to vnodes,
198 * which could cause I/O if the file has been unlinked.
199 * Need to do this early enough that we can still sleep.
200 * Can't free the entire vmspace as the kernel stack
201 * may be mapped within that space also.
202 */
203 if (vm->vm_refcnt == 1)
204 (void) uvm_deallocate(&vm->vm_map, VM_MIN_ADDRESS,
205 VM_MAXUSER_ADDRESS - VM_MIN_ADDRESS);
206
207 if (SESS_LEADER(p)) {
208 register struct session *sp = p->p_session;
209
210 if (sp->s_ttyvp) {
211 /*
212 * Controlling process.
213 * Signal foreground pgrp,
214 * drain controlling terminal
215 * and revoke access to controlling terminal.
216 */
217 if (sp->s_ttyp->t_session == sp) {
218 if (sp->s_ttyp->t_pgrp)
219 pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1);
220 (void) ttywait(sp->s_ttyp);
221 /*
222 * The tty could have been revoked
223 * if we blocked.
224 */
225 if (sp->s_ttyvp)
226 VOP_REVOKE(sp->s_ttyvp, REVOKEALL);
227 }
228 if (sp->s_ttyvp)
229 vrele(sp->s_ttyvp);
230 sp->s_ttyvp = NULL;
231 /*
232 * s_ttyp is not zero'd; we use this to indicate
233 * that the session once had a controlling terminal.
234 * (for logging and informational purposes)
235 */
236 }
237 sp->s_leader = NULL;
238 }
239 fixjobc(p, p->p_pgrp, 0);
240 p->p_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
241 (void)acct_process(p);
242 #ifdef KTRACE
243 /*
244 * release trace file
245 */
246 ktrderef(p);
247 #endif
248 /*
249 * Remove proc from allproc queue and pidhash chain.
250 * Unlink from parent's child list.
251 */
252 LIST_REMOVE(p, p_list);
253 p->p_stat = SZOMB;
254
255 /*
256 * NOTE: WE ARE NO LONGER ALLOWED TO SLEEP!
257 */
258
259 LIST_REMOVE(p, p_hash);
260
261 q = p->p_children.lh_first;
262 if (q) /* only need this if any child is S_ZOMB */
263 wakeup((caddr_t)initproc);
264 for (; q != 0; q = nq) {
265 nq = q->p_sibling.le_next;
266 proc_reparent(q, initproc);
267 /*
268 * Traced processes are killed
269 * since their existence means someone is screwing up.
270 */
271 if (q->p_flag & P_TRACED) {
272 q->p_flag &= ~(P_TRACED|P_WAITED|P_FSTRACE);
273 psignal(q, SIGKILL);
274 }
275 }
276
277 /*
278 * Save exit status and final rusage info, adding in child rusage
279 * info and self times.
280 */
281 p->p_xstat = rv;
282 *p->p_ru = p->p_stats->p_ru;
283 calcru(p, &p->p_ru->ru_utime, &p->p_ru->ru_stime, NULL);
284 ruadd(p->p_ru, &p->p_stats->p_cru);
285
286 /*
287 * Notify parent that we're gone. If parent has the P_NOCLDWAIT
288 * flag set, notify init instead (and hope it will handle
289 * this situation).
290 */
291 if (p->p_pptr->p_flag & P_NOCLDWAIT) {
292 struct proc *pp = p->p_pptr;
293 proc_reparent(p, initproc);
294 /*
295 * If this was the last child of our parent, notify
296 * parent, so in case he was wait(2)ing, he will
297 * continue.
298 */
299 if (pp->p_children.lh_first == NULL)
300 wakeup((caddr_t)pp);
301 }
302
303 /*
304 * Clear curproc after we've done all operations
305 * that could block, and before tearing down the rest
306 * of the process state that might be used from clock, etc.
307 * Also, can't clear curproc while we're still runnable,
308 * as we're not on a run queue (we are current, just not
309 * a proper proc any longer!).
310 *
311 * Other substructures are freed from wait().
312 */
313 curproc = NULL;
314 if (--p->p_limit->p_refcnt == 0)
315 pool_put(&plimit_pool, p->p_limit);
316
317 /*
318 * Finally, call machine-dependent code to switch to a new
319 * context (possibly the idle context). Once we are no longer
320 * using the dead process's vmspace and stack, exit2() will be
321 * called to schedule those resources to be released by the
322 * reaper thread.
323 *
324 * Note that cpu_exit() will end with a call equivalent to
325 * cpu_switch(), finishing our execution (pun intended).
326 */
327 cpu_exit(p);
328 }
329
330 /*
331 * We are called from cpu_exit() once it is safe to schedule the
332 * dead process's resources to be freed.
333 */
334 void
335 exit2(p)
336 struct proc *p;
337 {
338
339 LIST_INSERT_HEAD(&deadproc, p, p_list);
340 wakeup(&deadproc);
341 }
342
343 /*
344 * Process reaper. This is run by a kernel thread to free the resources
345 * of a dead process. Once the resources are free, the process becomes
346 * a zombie, and the parent is allowed to read the undead's status.
347 */
348 void
349 reaper()
350 {
351 struct proc *p;
352
353 for (;;) {
354 p = LIST_FIRST(&deadproc);
355 if (p == NULL) {
356 /* No work for us; go to sleep until someone exits. */
357 (void) tsleep(&deadproc, PVM, "reaper", 0);
358 continue;
359 }
360
361 /* Remove us from the deadproc list. */
362 LIST_REMOVE(p, p_list);
363
364 /*
365 * Free the VM resources we're still holding on to.
366 * We must do this from a valid thread because doing
367 * so may block.
368 */
369 uvm_exit(p);
370
371 /* Process is now a true zombie. */
372 LIST_INSERT_HEAD(&zombproc, p, p_list);
373
374 /* Wake up the parent so it can get exit satus. */
375 if ((p->p_flag & P_FSTRACE) == 0)
376 psignal(p->p_pptr, SIGCHLD);
377 wakeup((caddr_t)p->p_pptr);
378 }
379 }
380
381 int
382 sys_wait4(q, v, retval)
383 register struct proc *q;
384 void *v;
385 register_t *retval;
386 {
387 register struct sys_wait4_args /* {
388 syscallarg(int) pid;
389 syscallarg(int *) status;
390 syscallarg(int) options;
391 syscallarg(struct rusage *) rusage;
392 } */ *uap = v;
393 register int nfound;
394 register struct proc *p, *t;
395 int status, error;
396
397 if (SCARG(uap, pid) == 0)
398 SCARG(uap, pid) = -q->p_pgid;
399 if (SCARG(uap, options) &~ (WUNTRACED|WNOHANG))
400 return (EINVAL);
401
402 loop:
403 nfound = 0;
404 for (p = q->p_children.lh_first; p != 0; p = p->p_sibling.le_next) {
405 if (SCARG(uap, pid) != WAIT_ANY &&
406 p->p_pid != SCARG(uap, pid) &&
407 p->p_pgid != -SCARG(uap, pid))
408 continue;
409 nfound++;
410 if (p->p_stat == SZOMB) {
411 retval[0] = p->p_pid;
412
413 if (SCARG(uap, status)) {
414 status = p->p_xstat; /* convert to int */
415 error = copyout((caddr_t)&status,
416 (caddr_t)SCARG(uap, status),
417 sizeof(status));
418 if (error)
419 return (error);
420 }
421 if (SCARG(uap, rusage) &&
422 (error = copyout((caddr_t)p->p_ru,
423 (caddr_t)SCARG(uap, rusage),
424 sizeof(struct rusage))))
425 return (error);
426 /*
427 * If we got the child via ptrace(2) or procfs, and
428 * the parent is different (meaning the process was
429 * attached, rather than run as a child), then we need
430 * to give it back to the old parent, and send the
431 * parent a SIGCHLD. The rest of the cleanup will be
432 * done when the old parent waits on the child.
433 */
434 if ((p->p_flag & P_TRACED) &&
435 p->p_oppid != p->p_pptr->p_pid) {
436 t = pfind(p->p_oppid);
437 proc_reparent(p, t ? t : initproc);
438 p->p_oppid = 0;
439 p->p_flag &= ~(P_TRACED|P_WAITED|P_FSTRACE);
440 psignal(p->p_pptr, SIGCHLD);
441 wakeup((caddr_t)p->p_pptr);
442 return (0);
443 }
444 scheduler_wait_hook(curproc, p);
445 p->p_xstat = 0;
446 ruadd(&q->p_stats->p_cru, p->p_ru);
447 pool_put(&rusage_pool, p->p_ru);
448
449 /*
450 * Finally finished with old proc entry.
451 * Unlink it from its process group and free it.
452 */
453 leavepgrp(p);
454
455 LIST_REMOVE(p, p_list); /* off zombproc */
456
457 LIST_REMOVE(p, p_sibling);
458
459 /*
460 * Decrement the count of procs running with this uid.
461 */
462 (void)chgproccnt(p->p_cred->p_ruid, -1);
463
464 /*
465 * Free up credentials.
466 */
467 if (--p->p_cred->p_refcnt == 0) {
468 crfree(p->p_cred->pc_ucred);
469 pool_put(&pcred_pool, p->p_cred);
470 }
471
472 /*
473 * Release reference to text vnode
474 */
475 if (p->p_textvp)
476 vrele(p->p_textvp);
477
478 /*
479 * Give machine-dependent layer a chance
480 * to free anything that cpu_exit couldn't
481 * release while still running in process context.
482 */
483 cpu_wait(p);
484 pool_put(&proc_pool, p);
485 nprocs--;
486 return (0);
487 }
488 if (p->p_stat == SSTOP && (p->p_flag & P_WAITED) == 0 &&
489 (p->p_flag & P_TRACED || SCARG(uap, options) & WUNTRACED)) {
490 p->p_flag |= P_WAITED;
491 retval[0] = p->p_pid;
492
493 if (SCARG(uap, status)) {
494 status = W_STOPCODE(p->p_xstat);
495 error = copyout((caddr_t)&status,
496 (caddr_t)SCARG(uap, status),
497 sizeof(status));
498 } else
499 error = 0;
500 return (error);
501 }
502 }
503 if (nfound == 0)
504 return (ECHILD);
505 if (SCARG(uap, options) & WNOHANG) {
506 retval[0] = 0;
507 return (0);
508 }
509 if ((error = tsleep((caddr_t)q, PWAIT | PCATCH, "wait", 0)) != 0)
510 return (error);
511 goto loop;
512 }
513
514 /*
515 * make process 'parent' the new parent of process 'child'.
516 */
517 void
518 proc_reparent(child, parent)
519 register struct proc *child;
520 register struct proc *parent;
521 {
522
523 if (child->p_pptr == parent)
524 return;
525
526 LIST_REMOVE(child, p_sibling);
527 LIST_INSERT_HEAD(&parent->p_children, child, p_sibling);
528 child->p_pptr = parent;
529 }
530