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