kern_exit.c revision 1.127 1 /* $NetBSD: kern_exit.c,v 1.127 2003/11/06 09:30:13 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.127 2003/11/06 09:30:13 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 * Give orphaned children to init(8).
312 */
313 q = LIST_FIRST(&p->p_children);
314 if (q) /* only need this if any child is S_ZOMB */
315 wakeup(initproc);
316 for (; q != 0; q = nq) {
317 nq = LIST_NEXT(q, p_sibling);
318
319 /*
320 * Traced processes are killed since their existence
321 * means someone is screwing up. Since we reset the
322 * trace flags, the logic in sys_wait4() would not be
323 * triggered to reparent the process to its
324 * original parent, so we must do this here.
325 */
326 if (q->p_flag & P_TRACED) {
327 if (q->p_opptr != q->p_pptr) {
328 struct proc *t = q->p_opptr;
329 proc_reparent(q, t ? t : initproc);
330 q->p_opptr = NULL;
331 } else
332 proc_reparent(q, initproc);
333 q->p_flag &= ~(P_TRACED|P_WAITED|P_FSTRACE);
334 psignal(q, SIGKILL);
335 } else {
336 proc_reparent(q, initproc);
337 }
338 }
339
340 /*
341 * Reset p_opptr pointer of all former children which got
342 * traced by another process and were reparented. We reset
343 * it to NULL here; the trace detach code then reparents
344 * the child to initproc. We only check allproc list, since
345 * eventual former children on zombproc list won't reference
346 * p_opptr anymore.
347 */
348 if (p->p_flag & P_CHTRACED) {
349 struct proc *t;
350
351 proclist_lock_read();
352
353 LIST_FOREACH(t, &allproc, p_list) {
354 if (t->p_opptr == p)
355 t->p_opptr = NULL;
356 }
357
358 proclist_unlock_read();
359 }
360
361 /*
362 * Save exit status and final rusage info, adding in child rusage
363 * info and self times.
364 * In order to pick up the time for the current execution, we must
365 * do this before unlinking the lwp from l_list.
366 */
367 p->p_xstat = rv;
368 *p->p_ru = p->p_stats->p_ru;
369 calcru(p, &p->p_ru->ru_utime, &p->p_ru->ru_stime, NULL);
370 ruadd(p->p_ru, &p->p_stats->p_cru);
371
372 /*
373 * NOTE: WE ARE NO LONGER ALLOWED TO SLEEP!
374 */
375
376 /*
377 * Move proc from allproc to zombproc, but do not yet
378 * wake up the reaper. We will put the proc on the
379 * deadproc list later (using the p_dead member), and
380 * wake up the reaper when we do.
381 * Changing the state to SDEAD stops it being found by pfind().
382 */
383 s = proclist_lock_write();
384 p->p_stat = SDEAD;
385 p->p_nrlwps--;
386 l->l_stat = LSDEAD;
387 LIST_REMOVE(p, p_list);
388 LIST_INSERT_HEAD(&zombproc, p, p_list);
389 LIST_REMOVE(l, l_list);
390 l->l_flag |= L_DETACHED;
391 proclist_unlock_write(s);
392
393 /*
394 * Notify interested parties of our demise.
395 */
396 KNOTE(&p->p_klist, NOTE_EXIT);
397
398 #if PERFCTRS
399 /*
400 * Save final PMC information in parent process & clean up.
401 */
402 if (PMC_ENABLED(p)) {
403 pmc_save_context(p);
404 pmc_accumulate(p->p_pptr, p);
405 pmc_process_exit(p);
406 }
407 #endif
408
409 /*
410 * Notify parent that we're gone. If parent has the P_NOCLDWAIT
411 * flag set, notify init instead (and hope it will handle
412 * this situation).
413 */
414 if (p->p_pptr->p_flag & P_NOCLDWAIT) {
415 struct proc *pp = p->p_pptr;
416 proc_reparent(p, initproc);
417 /*
418 * If this was the last child of our parent, notify
419 * parent, so in case he was wait(2)ing, he will
420 * continue.
421 */
422 if (LIST_FIRST(&pp->p_children) == NULL)
423 wakeup(pp);
424 }
425
426 /*
427 * Release the process's signal state.
428 */
429 sigactsfree(p);
430
431 /*
432 * Clear curlwp after we've done all operations
433 * that could block, and before tearing down the rest
434 * of the process state that might be used from clock, etc.
435 * Also, can't clear curlwp while we're still runnable,
436 * as we're not on a run queue (we are current, just not
437 * a proper proc any longer!).
438 *
439 * Other substructures are freed from wait().
440 */
441 curlwp = NULL;
442 limfree(p->p_limit);
443 pstatsfree(p->p_stats);
444 p->p_limit = NULL;
445
446 /* This process no longer needs to hold the kernel lock. */
447 KERNEL_PROC_UNLOCK(l);
448
449 /*
450 * Finally, call machine-dependent code to switch to a new
451 * context (possibly the idle context). Once we are no longer
452 * using the dead process's vmspace and stack, exit2() will be
453 * called to schedule those resources to be released by the
454 * reaper thread.
455 *
456 * Note that cpu_exit() will end with a call equivalent to
457 * cpu_switch(), finishing our execution (pun intended).
458 */
459
460 cpu_exit(l, 1);
461 }
462
463 void
464 exit_lwps(struct lwp *l)
465 {
466 struct proc *p;
467 struct lwp *l2;
468 int s, error;
469 lwpid_t waited;
470
471 p = l->l_proc;
472
473 /* XXX SMP
474 * This would be the right place to IPI any LWPs running on
475 * other processors so that they can notice the userret exit hook.
476 */
477 p->p_userret = lwp_exit_hook;
478 p->p_userret_arg = NULL;
479
480 /*
481 * Make SA-cached LWPs normal process runnable LWPs so that
482 * they'll also self-destruct.
483 */
484 if (p->p_sa && p->p_sa->sa_ncached > 0) {
485 DPRINTF(("exit_lwps: Making cached LWPs of %d runnable: ",
486 p->p_pid));
487 SCHED_LOCK(s);
488 while ((l2 = sa_getcachelwp(p)) != 0) {
489 l2->l_priority = l2->l_usrpri;
490 setrunnable(l2);
491 DPRINTF(("%d ", l2->l_lid));
492 }
493 DPRINTF(("\n"));
494 SCHED_UNLOCK(s);
495 }
496
497 /*
498 * Interrupt LWPs in interruptable sleep, unsuspend suspended
499 * LWPs, make detached LWPs undetached (so we can wait for
500 * them) and then wait for everyone else to finish.
501 */
502 LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
503 l2->l_flag &= ~(L_DETACHED|L_SA);
504
505 if (l2->l_wchan == &l2->l_upcallstack)
506 wakeup(&l2->l_upcallstack);
507
508 if ((l2->l_stat == LSSLEEP && (l2->l_flag & L_SINTR)) ||
509 l2->l_stat == LSSUSPENDED || l2->l_stat == LSSTOP) {
510 SCHED_LOCK(s);
511 setrunnable(l2);
512 SCHED_UNLOCK(s);
513 DPRINTF(("exit_lwps: Made %d.%d runnable\n",
514 p->p_pid, l2->l_lid));
515 }
516 }
517
518
519 while (p->p_nlwps > 1) {
520 DPRINTF(("exit_lwps: waiting for %d LWPs (%d runnable, %d zombies)\n",
521 p->p_nlwps, p->p_nrlwps, p->p_nzlwps));
522 error = lwp_wait1(l, 0, &waited, LWPWAIT_EXITCONTROL);
523 if (error)
524 panic("exit_lwps: lwp_wait1 failed with error %d\n",
525 error);
526 DPRINTF(("exit_lwps: Got LWP %d from lwp_wait1()\n", waited));
527 }
528
529 p->p_userret = NULL;
530 }
531
532 /* Wrapper function for use in p_userret */
533 static void
534 lwp_exit_hook(struct lwp *l, void *arg)
535 {
536 KERNEL_PROC_LOCK(l);
537 lwp_exit(l);
538 }
539
540 /*
541 * We are called from cpu_exit() once it is safe to schedule the
542 * dead process's resources to be freed (i.e., once we've switched to
543 * the idle PCB for the current CPU).
544 *
545 * NOTE: One must be careful with locking in this routine. It's
546 * called from a critical section in machine-dependent code, so
547 * we should refrain from changing any interrupt state.
548 *
549 * We lock the deadproc list (a spin lock), place the proc on that
550 * list (using the p_dead member), and wake up the reaper.
551 */
552 void
553 exit2(struct lwp *l)
554 {
555 struct proc *p = l->l_proc;
556
557 simple_lock(&deadproc_slock);
558 SLIST_INSERT_HEAD(&deadprocs, p, p_dead);
559 simple_unlock(&deadproc_slock);
560
561 /* lwp_exit2() will wake up deadproc for us. */
562 lwp_exit2(l);
563 }
564
565 /*
566 * Process reaper. This is run by a kernel thread to free the resources
567 * of a dead process. Once the resources are free, the process becomes
568 * a zombie, and the parent is allowed to read the undead's status.
569 */
570 void
571 reaper(void *arg)
572 {
573 struct proc *p;
574 struct lwp *l;
575
576 KERNEL_PROC_UNLOCK(curlwp);
577
578 for (;;) {
579 simple_lock(&deadproc_slock);
580 p = SLIST_FIRST(&deadprocs);
581 l = LIST_FIRST(&deadlwp);
582 if (p == NULL && l == NULL) {
583 /* No work for us; go to sleep until someone exits. */
584 (void) ltsleep(&deadprocs, PVM|PNORELOCK,
585 "reaper", 0, &deadproc_slock);
586 continue;
587 }
588
589 if (l != NULL ) {
590 p = l->l_proc;
591
592 /* Remove lwp from the deadlwp list. */
593 LIST_REMOVE(l, l_list);
594 simple_unlock(&deadproc_slock);
595 KERNEL_PROC_LOCK(curlwp);
596
597 /*
598 * Give machine-dependent code a chance to free any
599 * resources it couldn't free while still running on
600 * that process's context. This must be done before
601 * uvm_lwp_exit(), in case these resources are in the
602 * PCB.
603 */
604 cpu_wait(l);
605
606 /*
607 * Free the VM resources we're still holding on to.
608 */
609 uvm_lwp_exit(l);
610
611 l->l_stat = LSZOMB;
612 if (l->l_flag & L_DETACHED) {
613 /* Nobody waits for detached LWPs. */
614 LIST_REMOVE(l, l_sibling);
615 p->p_nlwps--;
616 pool_put(&lwp_pool, l);
617 } else {
618 p->p_nzlwps++;
619 wakeup(&p->p_nlwps);
620 }
621 /* XXXNJW where should this be with respect to
622 * the wakeup() above? */
623 KERNEL_PROC_UNLOCK(curlwp);
624 } else {
625 /* Remove proc from the deadproc list. */
626 SLIST_REMOVE_HEAD(&deadprocs, p_dead);
627 simple_unlock(&deadproc_slock);
628 KERNEL_PROC_LOCK(curlwp);
629
630 /*
631 * Free the VM resources we're still holding on to.
632 * We must do this from a valid thread because doing
633 * so may block.
634 */
635 uvm_proc_exit(p);
636
637 /* Process is now a true zombie. */
638 p->p_stat = SZOMB;
639
640 /* Wake up the parent so it can get exit status. */
641 if ((p->p_flag & P_FSTRACE) == 0 && p->p_exitsig != 0)
642 exit_psignal(p, p->p_pptr);
643 KERNEL_PROC_UNLOCK(curlwp);
644 wakeup(p->p_pptr);
645 }
646 }
647 }
648
649 int
650 sys_wait4(struct lwp *l, void *v, register_t *retval)
651 {
652 struct sys_wait4_args /* {
653 syscallarg(int) pid;
654 syscallarg(int *) status;
655 syscallarg(int) options;
656 syscallarg(struct rusage *) rusage;
657 } */ *uap = v;
658 struct proc *child, *parent;
659 int status, error;
660
661 parent = l->l_proc;
662
663 if (SCARG(uap, pid) == 0)
664 SCARG(uap, pid) = -parent->p_pgid;
665 if (SCARG(uap, options) & ~(WUNTRACED|WNOHANG|WALTSIG|WALLSIG))
666 return (EINVAL);
667
668 error = find_stopped_child(parent, SCARG(uap,pid), SCARG(uap,options),
669 &child);
670 if (error != 0)
671 return error;
672 if (child == NULL) {
673 *retval = 0;
674 return 0;
675 }
676
677 retval[0] = child->p_pid;
678
679 if (child->p_stat == SZOMB) {
680 if (SCARG(uap, status)) {
681 status = child->p_xstat; /* convert to int */
682 error = copyout(&status, SCARG(uap, status),
683 sizeof(status));
684 if (error)
685 return (error);
686 }
687 if (SCARG(uap, rusage)) {
688 error = copyout(child->p_ru, SCARG(uap, rusage),
689 sizeof(struct rusage));
690 if (error)
691 return (error);
692 }
693
694 proc_free(child);
695 return 0;
696 }
697
698 /* child state must be SSTOP */
699 if (SCARG(uap, status)) {
700 status = W_STOPCODE(child->p_xstat);
701 return copyout(&status, SCARG(uap, status), sizeof (status));
702 }
703 return 0;
704 }
705
706 /*
707 * Scan list of child processes for a child process that has stopped or
708 * exited. Used by sys_wait4 and 'compat' equivalents.
709 */
710 int
711 find_stopped_child(struct proc *parent, pid_t pid, int options,
712 struct proc **child_p)
713 {
714 struct proc *child;
715 int c_found, error;
716
717 for (;;) {
718 c_found = 0;
719 LIST_FOREACH(child, &parent->p_children, p_sibling) {
720 if (pid != WAIT_ANY &&
721 child->p_pid != pid &&
722 child->p_pgid != -pid)
723 continue;
724 /*
725 * Wait for processes with p_exitsig != SIGCHLD
726 * processes only if WALTSIG is set; wait for
727 * processes with p_exitsig == SIGCHLD only
728 * if WALTSIG is clear.
729 */
730 if (((options & WALLSIG) == 0) &&
731 (options & WALTSIG ? child->p_exitsig == SIGCHLD
732 : P_EXITSIG(child) != SIGCHLD))
733 continue;
734
735 c_found = 1;
736 if (child->p_stat == SZOMB &&
737 (options & WNOZOMBIE) == 0) {
738 *child_p = child;
739 return 0;
740 }
741
742 if (child->p_stat == SSTOP &&
743 (child->p_flag & P_WAITED) == 0 &&
744 (child->p_flag & P_TRACED || options & WUNTRACED)) {
745 if ((options & WNOWAIT) == 0)
746 child->p_flag |= P_WAITED;
747 *child_p = child;
748 return 0;
749 }
750 }
751 if (c_found == 0)
752 return ECHILD;
753 if (options & WNOHANG) {
754 *child_p = NULL;
755 return 0;
756 }
757 error = tsleep(parent, PWAIT | PCATCH, "wait", 0);
758 if (error != 0)
759 return error;
760 }
761 }
762
763 /*
764 * Free a process after parent has taken all the state info.
765 */
766 void
767 proc_free(struct proc *p)
768 {
769 struct proc *parent = p->p_pptr;
770 int s;
771
772 /*
773 * If we got the child via ptrace(2) or procfs, and
774 * the parent is different (meaning the process was
775 * attached, rather than run as a child), then we need
776 * to give it back to the old parent, and send the
777 * parent the exit signal. The rest of the cleanup
778 * will be done when the old parent waits on the child.
779 */
780 if ((p->p_flag & P_TRACED) && p->p_opptr != parent){
781 parent = p->p_opptr;
782 if (parent == NULL)
783 parent = initproc;
784 proc_reparent(p, parent);
785 p->p_opptr = NULL;
786 p->p_flag &= ~(P_TRACED|P_WAITED|P_FSTRACE);
787 if (p->p_exitsig != 0)
788 exit_psignal(p, parent);
789 wakeup(parent);
790 return;
791 }
792
793 scheduler_wait_hook(parent, p);
794 p->p_xstat = 0;
795
796 ruadd(&parent->p_stats->p_cru, p->p_ru);
797
798 /*
799 * At this point we are going to start freeing the final resources.
800 * If anyone tries to access the proc structure after here they
801 * will get a shock - bits are missing.
802 * Attempt to make it hard!
803 */
804
805 p->p_stat = SIDL; /* not even a zombie any more */
806
807 pool_put(&rusage_pool, p->p_ru);
808
809 /*
810 * Finally finished with old proc entry.
811 * Unlink it from its process group and free it.
812 */
813 leavepgrp(p);
814
815 s = proclist_lock_write();
816 LIST_REMOVE(p, p_list); /* off zombproc */
817 LIST_REMOVE(p, p_sibling);
818 proclist_unlock_write(s);
819
820 /*
821 * Decrement the count of procs running with this uid.
822 */
823 (void)chgproccnt(p->p_cred->p_ruid, -1);
824
825 /*
826 * Free up credentials.
827 */
828 if (--p->p_cred->p_refcnt == 0) {
829 crfree(p->p_cred->pc_ucred);
830 pool_put(&pcred_pool, p->p_cred);
831 }
832
833 /*
834 * Release reference to text vnode
835 */
836 if (p->p_textvp)
837 vrele(p->p_textvp);
838
839 /*
840 * Release any SA state
841 */
842 if (p->p_sa) {
843 free(p->p_sa->sa_stacks, M_SA);
844 pool_put(&sadata_pool, p->p_sa);
845 }
846
847 /* Free proc structure and let pid be reallocated */
848 proc_free_mem(p);
849 }
850
851 /*
852 * make process 'parent' the new parent of process 'child'.
853 */
854 void
855 proc_reparent(struct proc *child, struct proc *parent)
856 {
857
858 if (child->p_pptr == parent)
859 return;
860
861 if (parent == initproc)
862 child->p_exitsig = SIGCHLD;
863
864 LIST_REMOVE(child, p_sibling);
865 LIST_INSERT_HEAD(&parent->p_children, child, p_sibling);
866 child->p_pptr = parent;
867 }
868