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