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