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