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