kern_exit.c revision 1.112 1 /* $NetBSD: kern_exit.c,v 1.112 2003/02/22 01:00:14 nathanw 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.112 2003/02/22 01:00:14 nathanw 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/ioctl.h>
91 #include <sys/proc.h>
92 #include <sys/tty.h>
93 #include <sys/time.h>
94 #include <sys/resource.h>
95 #include <sys/kernel.h>
96 #include <sys/ktrace.h>
97 #include <sys/proc.h>
98 #include <sys/buf.h>
99 #include <sys/wait.h>
100 #include <sys/file.h>
101 #include <sys/vnode.h>
102 #include <sys/syslog.h>
103 #include <sys/malloc.h>
104 #include <sys/pool.h>
105 #include <sys/resourcevar.h>
106 #if defined(PERFCTRS)
107 #include <sys/pmc.h>
108 #endif
109 #include <sys/ptrace.h>
110 #include <sys/acct.h>
111 #include <sys/filedesc.h>
112 #include <sys/ras.h>
113 #include <sys/signalvar.h>
114 #include <sys/sched.h>
115 #include <sys/sa.h>
116 #include <sys/savar.h>
117 #include <sys/mount.h>
118 #include <sys/syscallargs.h>
119 #include <sys/systrace.h>
120
121 #include <machine/cpu.h>
122
123 #include <uvm/uvm_extern.h>
124
125 #define DEBUG_EXIT
126
127 #ifdef DEBUG_EXIT
128 int debug_exit = 0;
129 #define DPRINTF(x) if (debug_exit) printf x
130 #else
131 #define DPRINTF(x)
132 #endif
133
134 static void lwp_exit_hook(struct lwp *, void *);
135
136 /*
137 * exit --
138 * Death of process.
139 */
140 int
141 sys_exit(struct lwp *l, void *v, register_t *retval)
142 {
143 struct sys_exit_args /* {
144 syscallarg(int) rval;
145 } */ *uap = v;
146
147 /* Don't call exit1() multiple times in the same process.*/
148 if (l->l_proc->p_flag & P_WEXIT)
149 lwp_exit(l);
150
151 exit1(l, W_EXITCODE(SCARG(uap, rval), 0));
152 /* NOTREACHED */
153 return (0);
154 }
155
156 /*
157 * Exit: deallocate address space and other resources, change proc state
158 * to zombie, and unlink proc from allproc and parent's lists. Save exit
159 * status and rusage for wait(). Check for child processes and orphan them.
160 */
161 void
162 exit1(struct lwp *l, int rv)
163 {
164 struct proc *p, *q, *nq;
165 int s, sa;
166
167 p = l->l_proc;
168
169 if (__predict_false(p == initproc))
170 panic("init died (signal %d, exit %d)",
171 WTERMSIG(rv), WEXITSTATUS(rv));
172
173 p->p_flag |= P_WEXIT;
174
175 DPRINTF(("exit1: %d.%d exiting.\n", p->p_pid, l->l_lid));
176 /*
177 * Disable scheduler activation upcalls.
178 * We're trying to get out of here.
179 */
180 sa = 0;
181 if (p->p_sa != NULL) {
182 l->l_flag &= ~L_SA;
183 p->p_flag &= ~P_SA;
184 sa = 1;
185 }
186
187 #ifdef PGINPROF
188 vmsizmon();
189 #endif
190 if (p->p_flag & P_PROFIL)
191 stopprofclock(p);
192 p->p_ru = pool_get(&rusage_pool, PR_WAITOK);
193 /*
194 * If parent is waiting for us to exit or exec, P_PPWAIT is set; we
195 * wake up the parent early to avoid deadlock.
196 */
197 if (p->p_flag & P_PPWAIT) {
198 p->p_flag &= ~P_PPWAIT;
199 wakeup((caddr_t)p->p_pptr);
200 }
201 sigfillset(&p->p_sigctx.ps_sigignore);
202 sigemptyset(&p->p_sigctx.ps_siglist);
203 p->p_sigctx.ps_sigcheck = 0;
204 timers_free(p, TIMERS_ALL);
205
206 if (sa || (p->p_nlwps > 1))
207 exit_lwps(l);
208
209 #if defined(__HAVE_RAS)
210 ras_purgeall(p);
211 #endif
212
213 /*
214 * Close open files and release open-file table.
215 * This may block!
216 */
217 fdfree(p);
218 cwdfree(p);
219
220 doexithooks(p);
221
222 if (SESS_LEADER(p)) {
223 struct session *sp = p->p_session;
224
225 if (sp->s_ttyvp) {
226 /*
227 * Controlling process.
228 * Signal foreground pgrp,
229 * drain controlling terminal
230 * and revoke access to controlling terminal.
231 */
232 if (sp->s_ttyp->t_session == sp) {
233 if (sp->s_ttyp->t_pgrp)
234 pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1);
235 (void) ttywait(sp->s_ttyp);
236 /*
237 * The tty could have been revoked
238 * if we blocked.
239 */
240 if (sp->s_ttyvp)
241 VOP_REVOKE(sp->s_ttyvp, REVOKEALL);
242 }
243 if (sp->s_ttyvp)
244 vrele(sp->s_ttyvp);
245 sp->s_ttyvp = NULL;
246 /*
247 * s_ttyp is not zero'd; we use this to indicate
248 * that the session once had a controlling terminal.
249 * (for logging and informational purposes)
250 */
251 }
252 sp->s_leader = NULL;
253 }
254 fixjobc(p, p->p_pgrp, 0);
255 (void)acct_process(p);
256 #ifdef KTRACE
257 /*
258 * release trace file
259 */
260 ktrderef(p);
261 #endif
262 #ifdef SYSTRACE
263 systrace_sys_exit(p);
264 #endif
265 /*
266 * If emulation has process exit hook, call it now.
267 */
268 if (p->p_emul->e_proc_exit)
269 (*p->p_emul->e_proc_exit)(p);
270
271 /*
272 * NOTE: WE ARE NO LONGER ALLOWED TO SLEEP!
273 */
274 p->p_stat = SDEAD;
275 p->p_nrlwps--;
276 l->l_stat = SDEAD;
277
278 /*
279 * Remove proc from pidhash chain so looking it up won't
280 * work. Move it from allproc to zombproc, but do not yet
281 * wake up the reaper. We will put the proc on the
282 * deadproc list later (using the p_hash member), and
283 * wake up the reaper when we do.
284 */
285 s = proclist_lock_write();
286 LIST_REMOVE(p, p_hash);
287 LIST_REMOVE(p, p_list);
288 LIST_INSERT_HEAD(&zombproc, p, p_list);
289 LIST_REMOVE(l, l_list);
290 l->l_flag |= L_DETACHED;
291 proclist_unlock_write(s);
292
293 /*
294 * Give orphaned children to init(8).
295 */
296 q = LIST_FIRST(&p->p_children);
297 if (q) /* only need this if any child is S_ZOMB */
298 wakeup((caddr_t)initproc);
299 for (; q != 0; q = nq) {
300 nq = LIST_NEXT(q, p_sibling);
301
302 /*
303 * Traced processes are killed since their existence
304 * means someone is screwing up. Since we reset the
305 * trace flags, the logic in sys_wait4() would not be
306 * triggered to reparent the process to its
307 * original parent, so we must do this here.
308 */
309 if (q->p_flag & P_TRACED) {
310 if (q->p_opptr != q->p_pptr) {
311 struct proc *t = q->p_opptr;
312 proc_reparent(q, t ? t : initproc);
313 q->p_opptr = NULL;
314 } else
315 proc_reparent(q, initproc);
316 q->p_flag &= ~(P_TRACED|P_WAITED|P_FSTRACE);
317 psignal(q, SIGKILL);
318 } else {
319 proc_reparent(q, initproc);
320 }
321 }
322
323 /*
324 * Reset p_opptr pointer of all former children which got
325 * traced by another process and were reparented. We reset
326 * it to NULL here; the trace detach code then reparents
327 * the child to initproc. We only check allproc list, since
328 * eventual former children on zombproc list won't reference
329 * p_opptr anymore.
330 */
331 if (p->p_flag & P_CHTRACED) {
332 struct proc *t;
333
334 proclist_lock_read();
335
336 LIST_FOREACH(t, &allproc, p_list) {
337 if (t->p_opptr == p)
338 t->p_opptr = NULL;
339 }
340
341 proclist_unlock_read();
342 }
343
344 /*
345 * Save exit status and final rusage info, adding in child rusage
346 * info and self times.
347 */
348 p->p_xstat = rv;
349 *p->p_ru = p->p_stats->p_ru;
350 calcru(p, &p->p_ru->ru_utime, &p->p_ru->ru_stime, NULL);
351 ruadd(p->p_ru, &p->p_stats->p_cru);
352
353 /*
354 * Notify interested parties of our demise.
355 */
356 KNOTE(&p->p_klist, NOTE_EXIT);
357
358 #if PERFCTRS
359 /*
360 * Save final PMC information in parent process & clean up.
361 */
362 if (PMC_ENABLED(p)) {
363 pmc_save_context(p);
364 pmc_accumulate(p->p_pptr, p);
365 pmc_process_exit(p);
366 }
367 #endif
368
369 /*
370 * Notify parent that we're gone. If parent has the P_NOCLDWAIT
371 * flag set, notify init instead (and hope it will handle
372 * this situation).
373 */
374 if (p->p_pptr->p_flag & P_NOCLDWAIT) {
375 struct proc *pp = p->p_pptr;
376 proc_reparent(p, initproc);
377 /*
378 * If this was the last child of our parent, notify
379 * parent, so in case he was wait(2)ing, he will
380 * continue.
381 */
382 if (LIST_FIRST(&pp->p_children) == NULL)
383 wakeup((caddr_t)pp);
384 }
385
386 /*
387 * Release the process's signal state.
388 */
389 sigactsfree(p);
390
391 /*
392 * Clear curlwp after we've done all operations
393 * that could block, and before tearing down the rest
394 * of the process state that might be used from clock, etc.
395 * Also, can't clear curlwp while we're still runnable,
396 * as we're not on a run queue (we are current, just not
397 * a proper proc any longer!).
398 *
399 * Other substructures are freed from wait().
400 */
401 curlwp = NULL;
402 limfree(p->p_limit);
403 pstatsfree(p->p_stats);
404 p->p_limit = NULL;
405
406 /* This process no longer needs to hold the kernel lock. */
407 KERNEL_PROC_UNLOCK(l);
408
409 /*
410 * Finally, call machine-dependent code to switch to a new
411 * context (possibly the idle context). Once we are no longer
412 * using the dead process's vmspace and stack, exit2() will be
413 * called to schedule those resources to be released by the
414 * reaper thread.
415 *
416 * Note that cpu_exit() will end with a call equivalent to
417 * cpu_switch(), finishing our execution (pun intended).
418 */
419
420 cpu_exit(l, 1);
421 }
422
423 void
424 exit_lwps(struct lwp *l)
425 {
426 struct proc *p;
427 struct lwp *l2;
428 int s, error;
429 lwpid_t waited;
430
431 p = l->l_proc;
432
433 /* XXX SMP
434 * This would be the right place to IPI any LWPs running on
435 * other processors so that they can notice the userret exit hook.
436 */
437 p->p_userret = lwp_exit_hook;
438 p->p_userret_arg = NULL;
439
440 /*
441 * Make SA-cached LWPs normal process runnable LWPs so that
442 * they'll also self-destruct.
443 */
444 if (p->p_sa && p->p_sa->sa_ncached > 0) {
445 DPRINTF(("exit_lwps: Making cached LWPs of %d runnable: ",
446 p->p_pid));
447 SCHED_LOCK(s);
448 while ((l2 = sa_getcachelwp(p)) != 0) {
449 l2->l_priority = l2->l_usrpri;
450 setrunnable(l2);
451 DPRINTF(("%d ", l2->l_lid));
452 }
453 DPRINTF(("\n"));
454 SCHED_UNLOCK(s);
455 }
456
457 /*
458 * Interrupt LWPs in interruptable sleep, unsuspend suspended
459 * LWPs, make detached LWPs undetached (so we can wait for
460 * them) and then wait for everyone else to finish.
461 */
462 LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
463 l2->l_flag &= ~(L_DETACHED|L_SA);
464 if ((l2->l_stat == LSSLEEP && (l2->l_flag & L_SINTR)) ||
465 l2->l_stat == LSSUSPENDED) {
466 SCHED_LOCK(s);
467 setrunnable(l2);
468 SCHED_UNLOCK(s);
469 DPRINTF(("exit_lwps: Made %d.%d runnable\n",
470 p->p_pid, l2->l_lid));
471 }
472 }
473
474
475 while (p->p_nlwps > 1) {
476 DPRINTF(("exit_lwps: waiting for %d LWPs (%d runnable, %d zombies)\n",
477 p->p_nlwps, p->p_nrlwps, p->p_nzlwps));
478 error = lwp_wait1(l, 0, &waited, LWPWAIT_EXITCONTROL);
479 if (error)
480 panic("exit_lwps: lwp_wait1 failed with error %d\n",
481 error);
482 DPRINTF(("exit_lwps: Got LWP %d from lwp_wait1()\n", waited));
483 }
484
485 p->p_userret = NULL;
486 }
487
488 /* Wrapper function for use in p_userret */
489 static void
490 lwp_exit_hook(struct lwp *l, void *arg)
491 {
492 KERNEL_PROC_LOCK(l);
493 lwp_exit(l);
494 }
495
496 /*
497 * We are called from cpu_exit() once it is safe to schedule the
498 * dead process's resources to be freed (i.e., once we've switched to
499 * the idle PCB for the current CPU).
500 *
501 * NOTE: One must be careful with locking in this routine. It's
502 * called from a critical section in machine-dependent code, so
503 * we should refrain from changing any interrupt state.
504 *
505 * We lock the deadproc list (a spin lock), place the proc on that
506 * list (using the p_hash member), and wake up the reaper.
507 */
508 void
509 exit2(struct lwp *l)
510 {
511 struct proc *p = l->l_proc;
512
513 simple_lock(&deadproc_slock);
514 LIST_INSERT_HEAD(&deadproc, p, p_hash);
515 simple_unlock(&deadproc_slock);
516
517 /* lwp_exit2() will wake up deadproc for us. */
518 lwp_exit2(l);
519 }
520
521 /*
522 * Process reaper. This is run by a kernel thread to free the resources
523 * of a dead process. Once the resources are free, the process becomes
524 * a zombie, and the parent is allowed to read the undead's status.
525 */
526 void
527 reaper(void *arg)
528 {
529 struct proc *p;
530 struct lwp *l;
531
532 KERNEL_PROC_UNLOCK(curlwp);
533
534 for (;;) {
535 simple_lock(&deadproc_slock);
536 p = LIST_FIRST(&deadproc);
537 l = LIST_FIRST(&deadlwp);
538 if (p == NULL && l == NULL) {
539 /* No work for us; go to sleep until someone exits. */
540 (void) ltsleep(&deadproc, PVM|PNORELOCK,
541 "reaper", 0, &deadproc_slock);
542 continue;
543 }
544
545 if (l != NULL ) {
546 p = l->l_proc;
547
548 /* Remove us from the deadlwp list. */
549 LIST_REMOVE(l, l_list);
550 simple_unlock(&deadproc_slock);
551 KERNEL_PROC_LOCK(curlwp);
552
553 /*
554 * Give machine-dependent code a chance to free any
555 * resources it couldn't free while still running on
556 * that process's context. This must be done before
557 * uvm_lwp_exit(), in case these resources are in the
558 * PCB.
559 */
560 cpu_wait(l);
561
562 /*
563 * Free the VM resources we're still holding on to.
564 */
565 uvm_lwp_exit(l);
566
567 l->l_stat = LSZOMB;
568 if (l->l_flag & L_DETACHED) {
569 /* Nobody waits for detached LWPs. */
570 LIST_REMOVE(l, l_sibling);
571 p->p_nlwps--;
572 pool_put(&lwp_pool, l);
573 } else {
574 p->p_nzlwps++;
575 wakeup((caddr_t)&p->p_nlwps);
576 }
577 /* XXXNJW where should this be with respect to
578 * the wakeup() above? */
579 KERNEL_PROC_UNLOCK(curlwp);
580 } else {
581 /* Remove us from the deadproc list. */
582 LIST_REMOVE(p, p_hash);
583 simple_unlock(&deadproc_slock);
584 KERNEL_PROC_LOCK(curlwp);
585
586 /*
587 * Free the VM resources we're still holding on to.
588 * We must do this from a valid thread because doing
589 * so may block.
590 */
591 uvm_proc_exit(p);
592
593 /* Process is now a true zombie. */
594 p->p_stat = SZOMB;
595
596 /* Wake up the parent so it can get exit status. */
597 if ((p->p_flag & P_FSTRACE) == 0 && p->p_exitsig != 0)
598 psignal(p->p_pptr, P_EXITSIG(p));
599 KERNEL_PROC_UNLOCK(curlwp);
600 wakeup((caddr_t)p->p_pptr);
601 }
602 }
603 }
604
605 int
606 sys_wait4(struct lwp *l, void *v, register_t *retval)
607 {
608 struct sys_wait4_args /* {
609 syscallarg(int) pid;
610 syscallarg(int *) status;
611 syscallarg(int) options;
612 syscallarg(struct rusage *) rusage;
613 } */ *uap = v;
614 struct proc *child, *parent;
615 int status, error;
616
617 parent = l->l_proc;
618
619 if (SCARG(uap, pid) == 0)
620 SCARG(uap, pid) = -parent->p_pgid;
621 if (SCARG(uap, options) & ~(WUNTRACED|WNOHANG|WALTSIG|WALLSIG))
622 return (EINVAL);
623
624 error = find_stopped_child(parent, SCARG(uap,pid), SCARG(uap,options),
625 &child);
626 if (error != 0)
627 return error;
628 if (child == NULL) {
629 *retval = 0;
630 return 0;
631 }
632
633 retval[0] = child->p_pid;
634
635 if (child->p_stat == SZOMB) {
636 if (SCARG(uap, status)) {
637 status = child->p_xstat; /* convert to int */
638 error = copyout((caddr_t)&status,
639 (caddr_t)SCARG(uap, status),
640 sizeof(status));
641 if (error)
642 return (error);
643 }
644 if (SCARG(uap, rusage)) {
645 error = copyout((caddr_t)child->p_ru,
646 (caddr_t)SCARG(uap, rusage),
647 sizeof(struct rusage));
648 if (error)
649 return (error);
650 }
651
652 proc_free(child);
653 return 0;
654 }
655
656 /* child state must be SSTOP */
657 if (SCARG(uap, status)) {
658 status = W_STOPCODE(child->p_xstat);
659 return copyout((caddr_t)&status,
660 (caddr_t)SCARG(uap, status),
661 sizeof(status));
662 }
663 return 0;
664 }
665
666 /*
667 * Scan list of child processes for a child process that has stopped or
668 * exited. Used by sys_wait4 and 'compat' equivalents.
669 */
670 int
671 find_stopped_child(struct proc *parent, pid_t pid, int options,
672 struct proc **child_p)
673 {
674 struct proc *child;
675 int c_found, error;
676
677 for (;;) {
678 c_found = 0;
679 LIST_FOREACH(child, &parent->p_children, p_sibling) {
680 if (pid != WAIT_ANY &&
681 child->p_pid != pid &&
682 child->p_pgid != -pid)
683 continue;
684 /*
685 * Wait for processes with p_exitsig != SIGCHLD
686 * processes only if WALTSIG is set; wait for
687 * processes with p_exitsig == SIGCHLD only
688 * if WALTSIG is clear.
689 */
690 if (((options & WALLSIG) == 0) &&
691 (options & WALTSIG ? child->p_exitsig == SIGCHLD
692 : P_EXITSIG(child) != SIGCHLD))
693 continue;
694
695 c_found = 1;
696 if (child->p_stat == SZOMB &&
697 (options & WNOZOMBIE) == 0) {
698 *child_p = child;
699 return 0;
700 }
701
702 if (child->p_stat == SSTOP &&
703 (child->p_flag & P_WAITED) == 0 &&
704 (child->p_flag & P_TRACED || options & WUNTRACED)) {
705 if ((options & WNOWAIT) == 0)
706 child->p_flag |= P_WAITED;
707 *child_p = child;
708 return 0;
709 }
710 }
711 if (c_found == 0)
712 return ECHILD;
713 if (options & WNOHANG) {
714 *child_p = NULL;
715 return 0;
716 }
717 error = tsleep((caddr_t)parent, PWAIT | PCATCH, "wait", 0);
718 if (error != 0)
719 return error;
720 }
721 }
722
723 /*
724 * Free a process after parent has taken all the state info.
725 */
726 void
727 proc_free(struct proc *p)
728 {
729 struct proc *parent = p->p_pptr;
730 int s;
731
732 /*
733 * If we got the child via ptrace(2) or procfs, and
734 * the parent is different (meaning the process was
735 * attached, rather than run as a child), then we need
736 * to give it back to the old parent, and send the
737 * parent the exit signal. The rest of the cleanup
738 * will be done when the old parent waits on the child.
739 */
740 if ((p->p_flag & P_TRACED) && p->p_opptr != parent){
741 parent = p->p_opptr;
742 if (parent == NULL)
743 parent = initproc;
744 proc_reparent(p, parent);
745 p->p_opptr = NULL;
746 p->p_flag &= ~(P_TRACED|P_WAITED|P_FSTRACE);
747 if (p->p_exitsig != 0)
748 psignal(parent, P_EXITSIG(p));
749 wakeup((caddr_t)parent);
750 return;
751 }
752
753 scheduler_wait_hook(parent, p);
754 p->p_xstat = 0;
755 ruadd(&parent->p_stats->p_cru, p->p_ru);
756 pool_put(&rusage_pool, p->p_ru);
757
758 /*
759 * Finally finished with old proc entry.
760 * Unlink it from its process group and free it.
761 */
762 leavepgrp(p);
763
764 s = proclist_lock_write();
765 LIST_REMOVE(p, p_list); /* off zombproc */
766 proclist_unlock_write(s);
767
768 LIST_REMOVE(p, p_sibling);
769
770 /*
771 * Decrement the count of procs running with this uid.
772 */
773 (void)chgproccnt(p->p_cred->p_ruid, -1);
774
775 /*
776 * Free up credentials.
777 */
778 if (--p->p_cred->p_refcnt == 0) {
779 crfree(p->p_cred->pc_ucred);
780 pool_put(&pcred_pool, p->p_cred);
781 }
782
783 /*
784 * Release reference to text vnode
785 */
786 if (p->p_textvp)
787 vrele(p->p_textvp);
788
789 /*
790 * Release any SA state
791 */
792 if (p->p_sa) {
793 free(p->p_sa->sa_stacks, M_SA);
794 pool_put(&sadata_pool, p->p_sa);
795 }
796
797 pool_put(&proc_pool, p);
798 nprocs--;
799 return;
800 }
801
802 /*
803 * make process 'parent' the new parent of process 'child'.
804 */
805 void
806 proc_reparent(struct proc *child, struct proc *parent)
807 {
808
809 if (child->p_pptr == parent)
810 return;
811
812 if (parent == initproc)
813 child->p_exitsig = SIGCHLD;
814
815 LIST_REMOVE(child, p_sibling);
816 LIST_INSERT_HEAD(&parent->p_children, child, p_sibling);
817 child->p_pptr = parent;
818 }
819