kern_exit.c revision 1.163 1 /* $NetBSD: kern_exit.c,v 1.163 2006/12/06 10:02:22 yamt 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.163 2006/12/06 10:02:22 yamt 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/tty.h>
88 #include <sys/time.h>
89 #include <sys/resource.h>
90 #include <sys/kernel.h>
91 #include <sys/ktrace.h>
92 #include <sys/proc.h>
93 #include <sys/buf.h>
94 #include <sys/wait.h>
95 #include <sys/file.h>
96 #include <sys/vnode.h>
97 #include <sys/syslog.h>
98 #include <sys/malloc.h>
99 #include <sys/pool.h>
100 #include <sys/resourcevar.h>
101 #if defined(PERFCTRS)
102 #include <sys/pmc.h>
103 #endif
104 #include <sys/ptrace.h>
105 #include <sys/acct.h>
106 #include <sys/filedesc.h>
107 #include <sys/ras.h>
108 #include <sys/signalvar.h>
109 #include <sys/sched.h>
110 #include <sys/sa.h>
111 #include <sys/savar.h>
112 #include <sys/mount.h>
113 #include <sys/syscallargs.h>
114 #include <sys/systrace.h>
115 #include <sys/kauth.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
132 /*
133 * Fill in the appropriate signal information, and signal the parent.
134 */
135 static void
136 exit_psignal(struct proc *p, struct proc *pp, ksiginfo_t *ksi)
137 {
138
139 KSI_INIT(ksi);
140 if ((ksi->ksi_signo = P_EXITSIG(p)) == SIGCHLD) {
141 if (WIFSIGNALED(p->p_xstat)) {
142 if (WCOREDUMP(p->p_xstat))
143 ksi->ksi_code = CLD_DUMPED;
144 else
145 ksi->ksi_code = CLD_KILLED;
146 } else {
147 ksi->ksi_code = CLD_EXITED;
148 }
149 }
150 /*
151 * we fill those in, even for non-SIGCHLD.
152 */
153 ksi->ksi_pid = p->p_pid;
154 ksi->ksi_uid = kauth_cred_geteuid(p->p_cred);
155 ksi->ksi_status = p->p_xstat;
156 /* XXX: is this still valid? */
157 ksi->ksi_utime = p->p_ru->ru_utime.tv_sec;
158 ksi->ksi_stime = p->p_ru->ru_stime.tv_sec;
159 }
160
161 /*
162 * exit --
163 * Death of process.
164 */
165 int
166 sys_exit(struct lwp *l, void *v, register_t *retval)
167 {
168 struct sys_exit_args /* {
169 syscallarg(int) rval;
170 } */ *uap = v;
171
172 /* Don't call exit1() multiple times in the same process.*/
173 if (l->l_proc->p_flag & P_WEXIT)
174 lwp_exit(l);
175
176 exit1(l, W_EXITCODE(SCARG(uap, rval), 0));
177 /* NOTREACHED */
178 return (0);
179 }
180
181 /*
182 * Exit: deallocate address space and other resources, change proc state
183 * to zombie, and unlink proc from allproc and parent's lists. Save exit
184 * status and rusage for wait(). Check for child processes and orphan them.
185 */
186 void
187 exit1(struct lwp *l, int rv)
188 {
189 struct proc *p, *q, *nq;
190 int s, sa;
191 struct plimit *plim;
192 struct pstats *pstats;
193 struct sigacts *ps;
194 ksiginfo_t ksi;
195 int do_psignal = 0;
196
197 p = l->l_proc;
198
199 if (__predict_false(p == initproc))
200 panic("init died (signal %d, exit %d)",
201 WTERMSIG(rv), WEXITSTATUS(rv));
202
203 p->p_flag |= P_WEXIT;
204 if (p->p_flag & P_STOPEXIT) {
205 sigminusset(&contsigmask, &p->p_sigctx.ps_siglist);
206 SCHED_LOCK(s);
207 p->p_stat = SSTOP;
208 l->l_stat = LSSTOP;
209 p->p_nrlwps--;
210 mi_switch(l, NULL);
211 SCHED_ASSERT_UNLOCKED();
212 splx(s);
213 }
214
215 DPRINTF(("exit1: %d.%d exiting.\n", p->p_pid, l->l_lid));
216 /*
217 * Disable scheduler activation upcalls.
218 * We're trying to get out of here.
219 */
220 sa = 0;
221 if (p->p_sa != NULL) {
222 l->l_flag &= ~L_SA;
223 #if 0
224 p->p_flag &= ~P_SA;
225 #endif
226 sa = 1;
227 }
228
229 #ifdef PGINPROF
230 vmsizmon();
231 #endif
232 if (p->p_flag & P_PROFIL)
233 stopprofclock(p);
234 p->p_ru = pool_get(&rusage_pool, PR_WAITOK);
235 /*
236 * If parent is waiting for us to exit or exec, P_PPWAIT is set; we
237 * wake up the parent early to avoid deadlock.
238 */
239 if (p->p_flag & P_PPWAIT) {
240 p->p_flag &= ~P_PPWAIT;
241 wakeup(p->p_pptr);
242 }
243 sigfillset(&p->p_sigctx.ps_sigignore);
244 sigemptyset(&p->p_sigctx.ps_siglist);
245 p->p_sigctx.ps_sigcheck = 0;
246 timers_free(p, TIMERS_ALL);
247
248 if (sa || (p->p_nlwps > 1)) {
249 exit_lwps(l);
250
251 /*
252 * Collect thread u-areas.
253 */
254 uvm_uarea_drain(FALSE);
255 }
256
257 #if defined(__HAVE_RAS)
258 ras_purgeall(p);
259 #endif
260
261 /*
262 * Close open files and release open-file table.
263 * This may block!
264 */
265 fdfree(l);
266 cwdfree(p->p_cwdi);
267 p->p_cwdi = 0;
268
269 doexithooks(p);
270
271 if (SESS_LEADER(p)) {
272 struct session *sp = p->p_session;
273 struct tty *tp;
274
275 if (sp->s_ttyvp) {
276 /*
277 * Controlling process.
278 * Signal foreground pgrp,
279 * drain controlling terminal
280 * and revoke access to controlling terminal.
281 */
282 tp = sp->s_ttyp;
283 s = spltty();
284 TTY_LOCK(tp);
285 if (tp->t_session == sp) {
286 if (tp->t_pgrp)
287 pgsignal(tp->t_pgrp, SIGHUP, 1);
288 /* we can't guarantee the revoke will do this */
289 tp->t_pgrp = NULL;
290 tp->t_session = NULL;
291 TTY_UNLOCK(tp);
292 splx(s);
293 SESSRELE(sp);
294 (void) ttywait(tp);
295 /*
296 * The tty could have been revoked
297 * if we blocked.
298 */
299 if (sp->s_ttyvp)
300 VOP_REVOKE(sp->s_ttyvp, REVOKEALL);
301 } else {
302 TTY_UNLOCK(tp);
303 splx(s);
304 }
305 if (sp->s_ttyvp)
306 vrele(sp->s_ttyvp);
307 sp->s_ttyvp = NULL;
308 /*
309 * s_ttyp is not zero'd; we use this to indicate
310 * that the session once had a controlling terminal.
311 * (for logging and informational purposes)
312 */
313 }
314 sp->s_leader = NULL;
315 }
316 fixjobc(p, p->p_pgrp, 0);
317
318 /*
319 * Collect accounting flags from the last remaining LWP (this one),
320 * and write out accounting data.
321 */
322 p->p_acflag |= l->l_acflag;
323 (void)acct_process(l);
324
325 #ifdef KTRACE
326 /*
327 * Release trace file.
328 */
329 ktrderef(p);
330 #endif
331 #ifdef SYSTRACE
332 systrace_sys_exit(p);
333 #endif
334
335 /*
336 * If emulation has process exit hook, call it now.
337 * Set the exit status now so that the exit hook has
338 * an opportunity to tweak it (COMPAT_LINUX requires
339 * this for thread group emulation)
340 */
341 p->p_xstat = rv;
342 if (p->p_emul->e_proc_exit)
343 (*p->p_emul->e_proc_exit)(p);
344
345 /*
346 * Finalize the last LWP's specificdata, as well as the
347 * specificdata for the proc itself.
348 */
349 lwp_finispecific(l);
350 proc_finispecific(p);
351
352 /*
353 * Free the VM resources we're still holding on to.
354 * We must do this from a valid thread because doing
355 * so may block. This frees vmspace, which we don't
356 * need anymore. The only remaining lwp is the one
357 * we run at this moment, nothing runs in userland
358 * anymore.
359 */
360 uvm_proc_exit(p);
361
362 /*
363 * Give machine-dependent code a chance to free any
364 * MD LWP resources while we can still block. This must be done
365 * before uvm_lwp_exit(), in case these resources are in the
366 * PCB.
367 * THIS IS LAST BLOCKING OPERATION.
368 */
369 #ifndef __NO_CPU_LWP_FREE
370 cpu_lwp_free(l, 1);
371 #endif
372
373 pmap_deactivate(l);
374
375 /*
376 * NOTE: WE ARE NO LONGER ALLOWED TO SLEEP!
377 */
378
379 /*
380 * Save final rusage info, adding in child rusage info and self times.
381 * In order to pick up the time for the current execution, we must
382 * do this before unlinking the lwp from l_list.
383 */
384 *p->p_ru = p->p_stats->p_ru;
385 calcru(p, &p->p_ru->ru_utime, &p->p_ru->ru_stime, NULL);
386 ruadd(p->p_ru, &p->p_stats->p_cru);
387
388 /*
389 * Notify interested parties of our demise.
390 */
391 KNOTE(&p->p_klist, NOTE_EXIT);
392
393 #if PERFCTRS
394 /*
395 * Save final PMC information in parent process & clean up.
396 */
397 if (PMC_ENABLED(p)) {
398 pmc_save_context(p);
399 pmc_accumulate(p->p_pptr, p);
400 pmc_process_exit(p);
401 }
402 #endif
403
404 s = proclist_lock_write();
405 /*
406 * Reset p_opptr pointer of all former children which got
407 * traced by another process and were reparented. We reset
408 * it to NULL here; the trace detach code then reparents
409 * the child to initproc. We only check allproc list, since
410 * eventual former children on zombproc list won't reference
411 * p_opptr anymore.
412 */
413 if (p->p_flag & P_CHTRACED) {
414 PROCLIST_FOREACH(q, &allproc) {
415 if (q->p_opptr == p)
416 q->p_opptr = NULL;
417 }
418 }
419
420 /*
421 * Give orphaned children to init(8).
422 */
423 q = LIST_FIRST(&p->p_children);
424 if (q) /* only need this if any child is SZOMB */
425 wakeup(initproc);
426 for (; q != NULL; q = nq) {
427 nq = LIST_NEXT(q, p_sibling);
428
429 /*
430 * Traced processes are killed since their existence
431 * means someone is screwing up. Since we reset the
432 * trace flags, the logic in sys_wait4() would not be
433 * triggered to reparent the process to its
434 * original parent, so we must do this here.
435 */
436 if (q->p_flag & P_TRACED) {
437 if (q->p_opptr != q->p_pptr) {
438 struct proc *t = q->p_opptr;
439 proc_reparent(q, t ? t : initproc);
440 q->p_opptr = NULL;
441 } else
442 proc_reparent(q, initproc);
443 q->p_flag &= ~(P_TRACED|P_WAITED|P_FSTRACE|P_SYSCALL);
444 killproc(q, "orphaned traced process");
445 } else {
446 proc_reparent(q, initproc);
447 }
448 }
449
450 /*
451 * Move proc from allproc to zombproc, it's now ready
452 * to be collected by parent. Remaining lwp resources
453 * will be freed in lwp_exit2() once we've switch to idle
454 * context.
455 * Changing the state to SZOMB stops it being found by pfind().
456 */
457 LIST_REMOVE(p, p_list);
458 LIST_INSERT_HEAD(&zombproc, p, p_list);
459 p->p_stat = SZOMB;
460
461 LIST_REMOVE(l, l_list);
462 LIST_REMOVE(l, l_sibling);
463 l->l_flag |= L_DETACHED; /* detached from proc too */
464 l->l_stat = LSDEAD;
465
466 KASSERT(p->p_nrlwps == 1);
467 KASSERT(p->p_nlwps == 1);
468 p->p_nrlwps--;
469 p->p_nlwps--;
470
471 /* Put in front of parent's sibling list for parent to collect it */
472 q = p->p_pptr;
473 q->p_nstopchild++;
474 if (LIST_FIRST(&q->p_children) != p) {
475 /* Put child where it can be found quickly */
476 LIST_REMOVE(p, p_sibling);
477 LIST_INSERT_HEAD(&q->p_children, p, p_sibling);
478 }
479
480 /*
481 * Notify parent that we're gone. If parent has the P_NOCLDWAIT
482 * flag set, notify init instead (and hope it will handle
483 * this situation).
484 */
485 if (q->p_flag & (P_NOCLDWAIT|P_CLDSIGIGN)) {
486 proc_reparent(p, initproc);
487
488 /*
489 * If this was the last child of our parent, notify
490 * parent, so in case he was wait(2)ing, he will
491 * continue.
492 */
493 if (LIST_FIRST(&q->p_children) == NULL)
494 wakeup(q);
495 }
496
497 /*
498 * Clear curlwp after we've done all operations
499 * that could block, and before tearing down the rest
500 * of the process state that might be used from clock, etc.
501 * Also, can't clear curlwp while we're still runnable,
502 * as we're not on a run queue (we are current, just not
503 * a proper proc any longer!).
504 *
505 * Other substructures are freed from wait().
506 */
507 curlwp = NULL;
508
509 /* Delay release until after dropping the proclist lock */
510 plim = p->p_limit;
511 pstats = p->p_stats;
512 ps = p->p_sigacts;
513
514 p->p_limit = NULL;
515 p->p_stats = NULL;
516 p->p_sigacts = NULL;
517
518 /* Reload parent pointer, since p may have been reparented above */
519 q = p->p_pptr;
520
521 if ((p->p_flag & P_FSTRACE) == 0 && p->p_exitsig != 0) {
522 exit_psignal(p, q, &ksi);
523 do_psignal = 1;
524 }
525
526 /*
527 * Once we release the proclist lock, we shouldn't touch the
528 * process structure anymore, since it's now on the zombie
529 * list and available for collection by the parent.
530 */
531 proclist_unlock_write(s);
532
533 if (do_psignal)
534 kpsignal(q, &ksi, NULL);
535
536 /* Wake up the parent so it can get exit status. */
537 wakeup(q);
538
539 /* Release substructures */
540 sigactsfree(ps);
541 limfree(plim);
542 pstatsfree(pstats);
543
544 /* Release cached credentials. */
545 kauth_cred_free(l->l_cred);
546
547 #ifdef DEBUG
548 /* Nothing should use the process link anymore */
549 l->l_proc = NULL;
550 #endif
551
552 /* This process no longer needs to hold the kernel lock. */
553 KERNEL_PROC_UNLOCK(l);
554
555 /*
556 * Finally, call machine-dependent code to switch to a new
557 * context (possibly the idle context). Once we are no longer
558 * using the dead lwp's stack, lwp_exit2() will be called
559 * to arrange for the resources to be released.
560 *
561 * Note that cpu_exit() will end with a call equivalent to
562 * cpu_switch(), finishing our execution (pun intended).
563 */
564
565 uvmexp.swtch++;
566 cpu_exit(l);
567 }
568
569 void
570 exit_lwps(struct lwp *l)
571 {
572 struct proc *p;
573 struct lwp *l2;
574 struct sadata_vp *vp;
575 int s, error;
576 lwpid_t waited;
577
578 p = l->l_proc;
579
580 /* XXX SMP
581 * This would be the right place to IPI any LWPs running on
582 * other processors so that they can notice the userret exit hook.
583 */
584 p->p_userret = lwp_exit_hook;
585 p->p_userret_arg = NULL;
586
587 if (p->p_sa) {
588 SLIST_FOREACH(vp, &p->p_sa->sa_vps, savp_next) {
589 /*
590 * Make SA-cached LWPs normal process runnable
591 * LWPs so that they'll also self-destruct.
592 */
593 DPRINTF(("exit_lwps: Making cached LWPs of %d on VP %d runnable: ",
594 p->p_pid, vp->savp_id));
595 SCHED_LOCK(s);
596 while ((l2 = sa_getcachelwp(vp)) != 0) {
597 l2->l_priority = l2->l_usrpri;
598 setrunnable(l2);
599 DPRINTF(("%d ", l2->l_lid));
600 }
601 SCHED_UNLOCK(s);
602 DPRINTF(("\n"));
603
604 /*
605 * Clear wokenq, the LWPs on the queue will
606 * run below.
607 */
608 vp->savp_wokenq_head = NULL;
609 }
610 }
611
612 retry:
613 /*
614 * Interrupt LWPs in interruptable sleep, unsuspend suspended
615 * LWPs, make detached LWPs undetached (so we can wait for
616 * them) and then wait for everyone else to finish.
617 */
618 LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
619 l2->l_flag &= ~(L_DETACHED|L_SA);
620
621 SCHED_LOCK(s);
622 if ((l2->l_stat == LSSLEEP && (l2->l_flag & L_SINTR)) ||
623 l2->l_stat == LSSUSPENDED || l2->l_stat == LSSTOP) {
624 setrunnable(l2);
625 DPRINTF(("exit_lwps: Made %d.%d runnable\n",
626 p->p_pid, l2->l_lid));
627 }
628 SCHED_UNLOCK(s);
629 }
630
631
632 while (p->p_nlwps > 1) {
633 DPRINTF(("exit_lwps: waiting for %d LWPs (%d runnable, %d zombies)\n",
634 p->p_nlwps, p->p_nrlwps, p->p_nzlwps));
635 error = lwp_wait1(l, 0, &waited, LWPWAIT_EXITCONTROL);
636 if (error == EDEADLK) {
637 /*
638 * LWPs can get suspended/slept behind us.
639 * (eg. sa_setwoken)
640 * kick them again and retry.
641 */
642 goto retry;
643 }
644 if (error)
645 panic("exit_lwps: lwp_wait1 failed with error %d",
646 error);
647 DPRINTF(("exit_lwps: Got LWP %d from lwp_wait1()\n", waited));
648 }
649
650 p->p_userret = NULL;
651 }
652
653 /* Wrapper function for use in p_userret */
654 static void
655 lwp_exit_hook(struct lwp *l, void *arg)
656 {
657
658 KERNEL_PROC_LOCK(l);
659 lwp_exit(l);
660 }
661
662 int
663 sys_wait4(struct lwp *l, void *v, register_t *retval)
664 {
665 struct sys_wait4_args /* {
666 syscallarg(int) pid;
667 syscallarg(int *) status;
668 syscallarg(int) options;
669 syscallarg(struct rusage *) rusage;
670 } */ *uap = v;
671 struct proc *child, *parent;
672 int status, error;
673
674 parent = l->l_proc;
675
676 if (SCARG(uap, pid) == 0)
677 SCARG(uap, pid) = -parent->p_pgid;
678 if (SCARG(uap, options) & ~(WUNTRACED|WNOHANG|WALTSIG|WALLSIG))
679 return (EINVAL);
680
681 error = find_stopped_child(parent, SCARG(uap,pid), SCARG(uap,options),
682 &child);
683 if (error != 0)
684 return error;
685 if (child == NULL) {
686 *retval = 0;
687 return 0;
688 }
689
690 /*
691 * Collect child u-areas.
692 */
693 uvm_uarea_drain(FALSE);
694
695 retval[0] = child->p_pid;
696
697 if (P_ZOMBIE(child)) {
698 if (SCARG(uap, status)) {
699 status = child->p_xstat; /* convert to int */
700 error = copyout(&status, SCARG(uap, status),
701 sizeof(status));
702 if (error)
703 return (error);
704 }
705 if (SCARG(uap, rusage)) {
706 error = copyout(child->p_ru, SCARG(uap, rusage),
707 sizeof(struct rusage));
708 if (error)
709 return (error);
710 }
711
712 proc_free(child);
713 return 0;
714 }
715
716 /* child state must be SSTOP */
717 if (SCARG(uap, status)) {
718 status = W_STOPCODE(child->p_xstat);
719 return copyout(&status, SCARG(uap, status), sizeof(status));
720 }
721 return 0;
722 }
723
724 /*
725 * Scan list of child processes for a child process that has stopped or
726 * exited. Used by sys_wait4 and 'compat' equivalents.
727 */
728 int
729 find_stopped_child(struct proc *parent, pid_t pid, int options,
730 struct proc **child_p)
731 {
732 struct proc *child;
733 int error;
734
735 for (;;) {
736 proclist_lock_read();
737 error = ECHILD;
738 LIST_FOREACH(child, &parent->p_children, p_sibling) {
739 if (pid >= 0) {
740 if (child->p_pid != pid) {
741 child = p_find(pid, PFIND_ZOMBIE |
742 PFIND_LOCKED);
743 if (child == NULL
744 || child->p_pptr != parent) {
745 child = NULL;
746 break;
747 }
748 }
749 } else
750 if (pid != WAIT_ANY && child->p_pgid != -pid)
751 /* child not in correct pgrp */
752 continue;
753 /*
754 * Wait for processes with p_exitsig != SIGCHLD
755 * processes only if WALTSIG is set; wait for
756 * processes with p_exitsig == SIGCHLD only
757 * if WALTSIG is clear.
758 */
759 if (((options & WALLSIG) == 0) &&
760 (options & WALTSIG ? child->p_exitsig == SIGCHLD
761 : P_EXITSIG(child) != SIGCHLD)){
762 if (child->p_pid == pid) {
763 child = NULL;
764 break;
765 }
766 continue;
767 }
768
769 error = 0;
770 if (child->p_stat == SZOMB && !(options & WNOZOMBIE))
771 break;
772
773 if (child->p_stat == SSTOP &&
774 (child->p_flag & P_WAITED) == 0 &&
775 (child->p_flag & P_TRACED || options & WUNTRACED)) {
776 if ((options & WNOWAIT) == 0) {
777 child->p_flag |= P_WAITED;
778 parent->p_nstopchild--;
779 }
780 break;
781 }
782 if (parent->p_nstopchild == 0 || child->p_pid == pid) {
783 child = NULL;
784 break;
785 }
786 }
787 proclist_unlock_read();
788 if (child != NULL || error != 0 || options & WNOHANG) {
789 *child_p = child;
790 return error;
791 }
792 error = tsleep(parent, PWAIT | PCATCH, "wait", 0);
793 if (error != 0)
794 return error;
795 }
796 }
797
798 /*
799 * Free a process after parent has taken all the state info.
800 */
801 void
802 proc_free(struct proc *p)
803 {
804 struct proc *parent = p->p_pptr;
805 ksiginfo_t ksi;
806 int s;
807
808 KASSERT(p->p_nlwps == 0);
809 KASSERT(p->p_nzlwps == 0);
810 KASSERT(p->p_nrlwps == 0);
811 KASSERT(LIST_EMPTY(&p->p_lwps));
812
813 /*
814 * If we got the child via ptrace(2) or procfs, and
815 * the parent is different (meaning the process was
816 * attached, rather than run as a child), then we need
817 * to give it back to the old parent, and send the
818 * parent the exit signal. The rest of the cleanup
819 * will be done when the old parent waits on the child.
820 */
821 if ((p->p_flag & P_TRACED) && p->p_opptr != parent){
822 parent = p->p_opptr;
823 if (parent == NULL)
824 parent = initproc;
825 proc_reparent(p, parent);
826 p->p_opptr = NULL;
827 p->p_flag &= ~(P_TRACED|P_WAITED|P_FSTRACE|P_SYSCALL);
828 if (p->p_exitsig != 0) {
829 exit_psignal(p, parent, &ksi);
830 kpsignal(parent, &ksi, NULL);
831 }
832 wakeup(parent);
833 return;
834 }
835
836 scheduler_wait_hook(parent, p);
837 p->p_xstat = 0;
838
839 ruadd(&parent->p_stats->p_cru, p->p_ru);
840
841 /*
842 * At this point we are going to start freeing the final resources.
843 * If anyone tries to access the proc structure after here they
844 * will get a shock - bits are missing.
845 * Attempt to make it hard!
846 */
847
848 p->p_stat = SIDL; /* not even a zombie any more */
849
850 pool_put(&rusage_pool, p->p_ru);
851
852 /*
853 * Finally finished with old proc entry.
854 * Unlink it from its process group and free it.
855 */
856 leavepgrp(p);
857
858 s = proclist_lock_write();
859 LIST_REMOVE(p, p_list); /* off zombproc */
860 p->p_pptr->p_nstopchild--;
861 LIST_REMOVE(p, p_sibling);
862 proclist_unlock_write(s);
863
864 /*
865 * Decrement the count of procs running with this uid.
866 */
867 (void)chgproccnt(kauth_cred_getuid(p->p_cred), -1);
868
869 /*
870 * Free up credentials.
871 */
872 kauth_cred_free(p->p_cred);
873
874 /*
875 * Release reference to text vnode
876 */
877 if (p->p_textvp)
878 vrele(p->p_textvp);
879
880 /* Release any SA state. */
881 if (p->p_sa)
882 sa_release(p);
883
884 /* Free proc structure and let pid be reallocated */
885 proc_free_mem(p);
886 }
887
888 /*
889 * make process 'parent' the new parent of process 'child'.
890 *
891 * Must be called with proclist_lock_write() held.
892 */
893 void
894 proc_reparent(struct proc *child, struct proc *parent)
895 {
896
897 if (child->p_pptr == parent)
898 return;
899
900 if (child->p_stat == SZOMB
901 || (child->p_stat == SSTOP && !(child->p_flag & P_WAITED))) {
902 child->p_pptr->p_nstopchild--;
903 parent->p_nstopchild++;
904 }
905 if (parent == initproc)
906 child->p_exitsig = SIGCHLD;
907
908 LIST_REMOVE(child, p_sibling);
909 LIST_INSERT_HEAD(&parent->p_children, child, p_sibling);
910 child->p_pptr = parent;
911 }
912