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