kern_exit.c revision 1.98 1 /* $NetBSD: kern_exit.c,v 1.98 2002/08/07 11:13:41 briggs Exp $ */
2
3 /*-
4 * Copyright (c) 1998, 1999 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 <sys/cdefs.h>
81 __KERNEL_RCSID(0, "$NetBSD: kern_exit.c,v 1.98 2002/08/07 11:13:41 briggs Exp $");
82
83 #include "opt_ktrace.h"
84 #include "opt_perfctrs.h"
85 #include "opt_systrace.h"
86 #include "opt_sysv.h"
87
88 #include <sys/param.h>
89 #include <sys/systm.h>
90 #include <sys/map.h>
91 #include <sys/ioctl.h>
92 #include <sys/proc.h>
93 #include <sys/tty.h>
94 #include <sys/time.h>
95 #include <sys/resource.h>
96 #include <sys/kernel.h>
97 #include <sys/ktrace.h>
98 #include <sys/proc.h>
99 #include <sys/buf.h>
100 #include <sys/wait.h>
101 #include <sys/file.h>
102 #include <sys/vnode.h>
103 #include <sys/syslog.h>
104 #include <sys/malloc.h>
105 #include <sys/pool.h>
106 #include <sys/resourcevar.h>
107 #if defined(PERFCTRS)
108 #include <sys/pmc.h>
109 #endif
110 #include <sys/ptrace.h>
111 #include <sys/acct.h>
112 #include <sys/filedesc.h>
113 #include <sys/signalvar.h>
114 #include <sys/sched.h>
115 #include <sys/mount.h>
116 #include <sys/syscallargs.h>
117 #include <sys/systrace.h>
118
119 #include <machine/cpu.h>
120
121 #include <uvm/uvm_extern.h>
122
123
124 /*
125 * exit --
126 * Death of process.
127 */
128 int
129 sys_exit(struct proc *p, void *v, register_t *retval)
130 {
131 struct sys_exit_args /* {
132 syscallarg(int) rval;
133 } */ *uap = v;
134
135 exit1(p, W_EXITCODE(SCARG(uap, rval), 0));
136 /* NOTREACHED */
137 return (0);
138 }
139
140 /*
141 * Exit: deallocate address space and other resources, change proc state
142 * to zombie, and unlink proc from allproc and parent's lists. Save exit
143 * status and rusage for wait(). Check for child processes and orphan them.
144 */
145 void
146 exit1(struct proc *p, int rv)
147 {
148 struct proc *q, *nq;
149 int s;
150
151 if (__predict_false(p == initproc))
152 panic("init died (signal %d, exit %d)",
153 WTERMSIG(rv), WEXITSTATUS(rv));
154
155 #ifdef PGINPROF
156 vmsizmon();
157 #endif
158 if (p->p_flag & P_PROFIL)
159 stopprofclock(p);
160 p->p_ru = pool_get(&rusage_pool, PR_WAITOK);
161 /*
162 * If parent is waiting for us to exit or exec, P_PPWAIT is set; we
163 * wake up the parent early to avoid deadlock.
164 */
165 p->p_flag |= P_WEXIT;
166 if (p->p_flag & P_PPWAIT) {
167 p->p_flag &= ~P_PPWAIT;
168 wakeup((caddr_t)p->p_pptr);
169 }
170 sigfillset(&p->p_sigctx.ps_sigignore);
171 sigemptyset(&p->p_sigctx.ps_siglist);
172 p->p_sigctx.ps_sigcheck = 0;
173 callout_stop(&p->p_realit_ch);
174
175 /*
176 * Close open files and release open-file table.
177 * This may block!
178 */
179 fdfree(p);
180 cwdfree(p);
181
182 doexithooks(p);
183
184 if (SESS_LEADER(p)) {
185 struct session *sp = p->p_session;
186
187 if (sp->s_ttyvp) {
188 /*
189 * Controlling process.
190 * Signal foreground pgrp,
191 * drain controlling terminal
192 * and revoke access to controlling terminal.
193 */
194 if (sp->s_ttyp->t_session == sp) {
195 if (sp->s_ttyp->t_pgrp)
196 pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1);
197 (void) ttywait(sp->s_ttyp);
198 /*
199 * The tty could have been revoked
200 * if we blocked.
201 */
202 if (sp->s_ttyvp)
203 VOP_REVOKE(sp->s_ttyvp, REVOKEALL);
204 }
205 if (sp->s_ttyvp)
206 vrele(sp->s_ttyvp);
207 sp->s_ttyvp = NULL;
208 /*
209 * s_ttyp is not zero'd; we use this to indicate
210 * that the session once had a controlling terminal.
211 * (for logging and informational purposes)
212 */
213 }
214 sp->s_leader = NULL;
215 }
216 fixjobc(p, p->p_pgrp, 0);
217 (void)acct_process(p);
218 #ifdef KTRACE
219 /*
220 * release trace file
221 */
222 ktrderef(p);
223 #endif
224 #ifdef SYSTRACE
225 systrace_sys_exit(p);
226 #endif
227 /*
228 * NOTE: WE ARE NO LONGER ALLOWED TO SLEEP!
229 */
230 p->p_stat = SDEAD;
231
232 /*
233 * Remove proc from pidhash chain so looking it up won't
234 * work. Move it from allproc to zombproc, but do not yet
235 * wake up the reaper. We will put the proc on the
236 * deadproc list later (using the p_hash member), and
237 * wake up the reaper when we do.
238 */
239 s = proclist_lock_write();
240 LIST_REMOVE(p, p_hash);
241 LIST_REMOVE(p, p_list);
242 LIST_INSERT_HEAD(&zombproc, p, p_list);
243 proclist_unlock_write(s);
244
245 /*
246 * Give orphaned children to init(8).
247 */
248 q = p->p_children.lh_first;
249 if (q) /* only need this if any child is S_ZOMB */
250 wakeup((caddr_t)initproc);
251 for (; q != 0; q = nq) {
252 nq = q->p_sibling.le_next;
253 proc_reparent(q, initproc);
254 /*
255 * Traced processes are killed
256 * since their existence means someone is screwing up.
257 */
258 if (q->p_flag & P_TRACED) {
259 q->p_flag &= ~(P_TRACED|P_WAITED|P_FSTRACE);
260 psignal(q, SIGKILL);
261 }
262 }
263
264 /*
265 * Reset p_opptr pointer of all former children which got
266 * traced by another process and were reparented. We reset
267 * it to NULL here; the trace detach code then reparents
268 * the child to initproc. We only check allproc list, since
269 * eventual former children on zombproc list won't reference
270 * p_opptr anymore.
271 */
272 if (p->p_flag & P_CHTRACED) {
273 struct proc *t;
274
275 proclist_lock_read();
276
277 LIST_FOREACH(t, &allproc, p_list) {
278 if (t->p_opptr == p)
279 t->p_opptr = NULL;
280 }
281
282 proclist_unlock_read();
283 }
284
285 /*
286 * Save exit status and final rusage info, adding in child rusage
287 * info and self times.
288 */
289 p->p_xstat = rv;
290 *p->p_ru = p->p_stats->p_ru;
291 calcru(p, &p->p_ru->ru_utime, &p->p_ru->ru_stime, NULL);
292 ruadd(p->p_ru, &p->p_stats->p_cru);
293
294 #if PERFCTRS
295 /*
296 * Save final PMC information in parent process & clean up.
297 */
298 if (PMC_ENABLED(p)) {
299 pmc_save_context(p);
300 pmc_accumulate(p->p_pptr, p);
301 pmc_process_exit(p);
302 }
303 #endif
304
305 /*
306 * Notify parent that we're gone. If parent has the P_NOCLDWAIT
307 * flag set, notify init instead (and hope it will handle
308 * this situation).
309 */
310 if (p->p_pptr->p_flag & P_NOCLDWAIT) {
311 struct proc *pp = p->p_pptr;
312 proc_reparent(p, initproc);
313 /*
314 * If this was the last child of our parent, notify
315 * parent, so in case he was wait(2)ing, he will
316 * continue.
317 */
318 if (pp->p_children.lh_first == NULL)
319 wakeup((caddr_t)pp);
320 }
321
322 /*
323 * Release the process's signal state.
324 */
325 sigactsfree(p);
326
327 /*
328 * If emulation has process exit hook, call it now.
329 */
330 if (p->p_emul->e_proc_exit)
331 (*p->p_emul->e_proc_exit)(p);
332
333 /*
334 * Clear curproc after we've done all operations
335 * that could block, and before tearing down the rest
336 * of the process state that might be used from clock, etc.
337 * Also, can't clear curproc while we're still runnable,
338 * as we're not on a run queue (we are current, just not
339 * a proper proc any longer!).
340 *
341 * Other substructures are freed from wait().
342 */
343 curproc = NULL;
344 limfree(p->p_limit);
345 p->p_limit = NULL;
346
347 /* This process no longer needs to hold the kernel lock. */
348 KERNEL_PROC_UNLOCK(p);
349
350 /*
351 * Finally, call machine-dependent code to switch to a new
352 * context (possibly the idle context). Once we are no longer
353 * using the dead process's vmspace and stack, exit2() will be
354 * called to schedule those resources to be released by the
355 * reaper thread.
356 *
357 * Note that cpu_exit() will end with a call equivalent to
358 * cpu_switch(), finishing our execution (pun intended).
359 */
360 cpu_exit(p);
361 }
362
363 /*
364 * We are called from cpu_exit() once it is safe to schedule the
365 * dead process's resources to be freed (i.e., once we've switched to
366 * the idle PCB for the current CPU).
367 *
368 * NOTE: One must be careful with locking in this routine. It's
369 * called from a critical section in machine-dependent code, so
370 * we should refrain from changing any interrupt state.
371 *
372 * We lock the deadproc list (a spin lock), place the proc on that
373 * list (using the p_hash member), and wake up the reaper.
374 */
375 void
376 exit2(struct proc *p)
377 {
378
379 simple_lock(&deadproc_slock);
380 LIST_INSERT_HEAD(&deadproc, p, p_hash);
381 simple_unlock(&deadproc_slock);
382
383 wakeup(&deadproc);
384 }
385
386 /*
387 * Process reaper. This is run by a kernel thread to free the resources
388 * of a dead process. Once the resources are free, the process becomes
389 * a zombie, and the parent is allowed to read the undead's status.
390 */
391 void
392 reaper(void *arg)
393 {
394 struct proc *p;
395
396 KERNEL_PROC_UNLOCK(curproc);
397
398 for (;;) {
399 simple_lock(&deadproc_slock);
400 p = LIST_FIRST(&deadproc);
401 if (p == NULL) {
402 /* No work for us; go to sleep until someone exits. */
403 (void) ltsleep(&deadproc, PVM|PNORELOCK,
404 "reaper", 0, &deadproc_slock);
405 continue;
406 }
407
408 /* Remove us from the deadproc list. */
409 LIST_REMOVE(p, p_hash);
410 simple_unlock(&deadproc_slock);
411 KERNEL_PROC_LOCK(curproc);
412
413 /*
414 * Give machine-dependent code a chance to free any
415 * resources it couldn't free while still running on
416 * that process's context. This must be done before
417 * uvm_exit(), in case these resources are in the PCB.
418 */
419 cpu_wait(p);
420
421 /*
422 * Free the VM resources we're still holding on to.
423 * We must do this from a valid thread because doing
424 * so may block.
425 */
426 uvm_exit(p);
427
428 /* Process is now a true zombie. */
429 p->p_stat = SZOMB;
430
431 /* Wake up the parent so it can get exit status. */
432 if ((p->p_flag & P_FSTRACE) == 0 && p->p_exitsig != 0)
433 psignal(p->p_pptr, P_EXITSIG(p));
434 KERNEL_PROC_UNLOCK(curproc);
435 wakeup((caddr_t)p->p_pptr);
436 }
437 }
438
439 int
440 sys_wait4(struct proc *q, void *v, register_t *retval)
441 {
442 struct sys_wait4_args /* {
443 syscallarg(int) pid;
444 syscallarg(int *) status;
445 syscallarg(int) options;
446 syscallarg(struct rusage *) rusage;
447 } */ *uap = v;
448 struct proc *p, *t;
449 int nfound, status, error, s;
450
451 if (SCARG(uap, pid) == 0)
452 SCARG(uap, pid) = -q->p_pgid;
453 if (SCARG(uap, options) &~ (WUNTRACED|WNOHANG|WALTSIG))
454 return (EINVAL);
455
456 loop:
457 nfound = 0;
458 for (p = q->p_children.lh_first; p != 0; p = p->p_sibling.le_next) {
459 if (SCARG(uap, pid) != WAIT_ANY &&
460 p->p_pid != SCARG(uap, pid) &&
461 p->p_pgid != -SCARG(uap, pid))
462 continue;
463 /*
464 * Wait for processes with p_exitsig != SIGCHLD processes only
465 * if WALTSIG is set; wait for processes with p_exitsig ==
466 * SIGCHLD only if WALTSIG is clear.
467 */
468 if (((SCARG(uap, options) & WALLSIG) == 0) &&
469 ((SCARG(uap, options) & WALTSIG) ?
470 (p->p_exitsig == SIGCHLD) : (P_EXITSIG(p) != SIGCHLD)))
471 continue;
472
473 nfound++;
474 if (p->p_stat == SZOMB) {
475 retval[0] = p->p_pid;
476
477 if (SCARG(uap, status)) {
478 status = p->p_xstat; /* convert to int */
479 error = copyout((caddr_t)&status,
480 (caddr_t)SCARG(uap, status),
481 sizeof(status));
482 if (error)
483 return (error);
484 }
485 if (SCARG(uap, rusage) &&
486 (error = copyout((caddr_t)p->p_ru,
487 (caddr_t)SCARG(uap, rusage),
488 sizeof(struct rusage))))
489 return (error);
490 /*
491 * If we got the child via ptrace(2) or procfs, and
492 * the parent is different (meaning the process was
493 * attached, rather than run as a child), then we need
494 * to give it back to the old parent, and send the
495 * parent the exit signal. The rest of the cleanup
496 * will be done when the old parent waits on the child.
497 */
498 if ((p->p_flag & P_TRACED) && p->p_opptr != p->p_pptr){
499 t = p->p_opptr;
500 proc_reparent(p, t ? t : initproc);
501 p->p_opptr = NULL;
502 p->p_flag &= ~(P_TRACED|P_WAITED|P_FSTRACE);
503 if (p->p_exitsig != 0)
504 psignal(p->p_pptr, P_EXITSIG(p));
505 wakeup((caddr_t)p->p_pptr);
506 return (0);
507 }
508 scheduler_wait_hook(q, p);
509 p->p_xstat = 0;
510 ruadd(&q->p_stats->p_cru, p->p_ru);
511 pool_put(&rusage_pool, p->p_ru);
512
513 /*
514 * Finally finished with old proc entry.
515 * Unlink it from its process group and free it.
516 */
517 leavepgrp(p);
518
519 s = proclist_lock_write();
520 LIST_REMOVE(p, p_list); /* off zombproc */
521 proclist_unlock_write(s);
522
523 LIST_REMOVE(p, p_sibling);
524
525 /*
526 * Decrement the count of procs running with this uid.
527 */
528 (void)chgproccnt(p->p_cred->p_ruid, -1);
529
530 /*
531 * Free up credentials.
532 */
533 if (--p->p_cred->p_refcnt == 0) {
534 crfree(p->p_cred->pc_ucred);
535 pool_put(&pcred_pool, p->p_cred);
536 }
537
538 /*
539 * Release reference to text vnode
540 */
541 if (p->p_textvp)
542 vrele(p->p_textvp);
543
544 pool_put(&proc_pool, p);
545 nprocs--;
546 return (0);
547 }
548 if (p->p_stat == SSTOP && (p->p_flag & P_WAITED) == 0 &&
549 (p->p_flag & P_TRACED || SCARG(uap, options) & WUNTRACED)) {
550 p->p_flag |= P_WAITED;
551 retval[0] = p->p_pid;
552
553 if (SCARG(uap, status)) {
554 status = W_STOPCODE(p->p_xstat);
555 error = copyout((caddr_t)&status,
556 (caddr_t)SCARG(uap, status),
557 sizeof(status));
558 } else
559 error = 0;
560 return (error);
561 }
562 }
563 if (nfound == 0)
564 return (ECHILD);
565 if (SCARG(uap, options) & WNOHANG) {
566 retval[0] = 0;
567 return (0);
568 }
569 if ((error = tsleep((caddr_t)q, PWAIT | PCATCH, "wait", 0)) != 0)
570 return (error);
571 goto loop;
572 }
573
574 /*
575 * make process 'parent' the new parent of process 'child'.
576 */
577 void
578 proc_reparent(struct proc *child, struct proc *parent)
579 {
580
581 if (child->p_pptr == parent)
582 return;
583
584 if (parent == initproc)
585 child->p_exitsig = SIGCHLD;
586
587 LIST_REMOVE(child, p_sibling);
588 LIST_INSERT_HEAD(&parent->p_children, child, p_sibling);
589 child->p_pptr = parent;
590 }
591