kern_exit.c revision 1.132 1 /* $NetBSD: kern_exit.c,v 1.132 2004/01/03 19:43:55 jdolecek 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.132 2004/01/03 19:43:55 jdolecek 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 signal 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 SZOMB */
343 wakeup(initproc);
344 for (; q != NULL; 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 if (p->p_sa) {
489 /*
490 * Make SA-cached LWPs normal process runnable LWPs so
491 * that they'll also self-destruct.
492 */
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 /* Clear wokenq, the LWPs on the queue will run below */
505 p->p_sa->sa_wokenq_head = NULL;
506 }
507
508 /*
509 * Interrupt LWPs in interruptable sleep, unsuspend suspended
510 * LWPs, make detached LWPs undetached (so we can wait for
511 * them) and then wait for everyone else to finish.
512 */
513 LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
514 l2->l_flag &= ~(L_DETACHED|L_SA);
515
516 if (p->p_sa && l2->l_wchan == p->p_sa)
517 l2->l_flag |= L_SINTR;
518
519 if ((l2->l_stat == LSSLEEP && (l2->l_flag & L_SINTR)) ||
520 l2->l_stat == LSSUSPENDED || l2->l_stat == LSSTOP) {
521 SCHED_LOCK(s);
522 setrunnable(l2);
523 SCHED_UNLOCK(s);
524 DPRINTF(("exit_lwps: Made %d.%d runnable\n",
525 p->p_pid, l2->l_lid));
526 }
527 }
528
529
530 while (p->p_nlwps > 1) {
531 DPRINTF(("exit_lwps: waiting for %d LWPs (%d runnable, %d zombies)\n",
532 p->p_nlwps, p->p_nrlwps, p->p_nzlwps));
533 error = lwp_wait1(l, 0, &waited, LWPWAIT_EXITCONTROL);
534 if (error)
535 panic("exit_lwps: lwp_wait1 failed with error %d\n",
536 error);
537 DPRINTF(("exit_lwps: Got LWP %d from lwp_wait1()\n", waited));
538 }
539
540 p->p_userret = NULL;
541 }
542
543 /* Wrapper function for use in p_userret */
544 static void
545 lwp_exit_hook(struct lwp *l, void *arg)
546 {
547 KERNEL_PROC_LOCK(l);
548 lwp_exit(l);
549 }
550
551 /*
552 * We are called from cpu_exit() once it is safe to schedule the
553 * dead process's resources to be freed (i.e., once we've switched to
554 * the idle PCB for the current CPU).
555 *
556 * NOTE: One must be careful with locking in this routine. It's
557 * called from a critical section in machine-dependent code, so
558 * we should refrain from changing any interrupt state.
559 *
560 * We lock the deadproc list (a spin lock), place the proc on that
561 * list (using the p_dead member), and wake up the reaper.
562 */
563 void
564 exit2(struct lwp *l)
565 {
566 struct proc *p = l->l_proc;
567
568 simple_lock(&deadproc_slock);
569 SLIST_INSERT_HEAD(&deadprocs, p, p_dead);
570 simple_unlock(&deadproc_slock);
571
572 /* lwp_exit2() will wake up deadproc for us. */
573 lwp_exit2(l);
574 }
575
576 /*
577 * Process reaper. This is run by a kernel thread to free the resources
578 * of a dead process. Once the resources are free, the process becomes
579 * a zombie, and the parent is allowed to read the undead's status.
580 */
581 void
582 reaper(void *arg)
583 {
584 struct proc *p, *parent;
585 struct lwp *l;
586
587 KERNEL_PROC_UNLOCK(curlwp);
588
589 for (;;) {
590 simple_lock(&deadproc_slock);
591 p = SLIST_FIRST(&deadprocs);
592 l = LIST_FIRST(&deadlwp);
593 if (p == NULL && l == NULL) {
594 /* No work for us; go to sleep until someone exits. */
595 (void) ltsleep(&deadprocs, PVM|PNORELOCK,
596 "reaper", 0, &deadproc_slock);
597 continue;
598 }
599
600 if (l != NULL ) {
601 p = l->l_proc;
602
603 /* Remove lwp from the deadlwp list. */
604 LIST_REMOVE(l, l_list);
605 simple_unlock(&deadproc_slock);
606 KERNEL_PROC_LOCK(curlwp);
607
608 /*
609 * Give machine-dependent code a chance to free any
610 * resources it couldn't free while still running on
611 * that process's context. This must be done before
612 * uvm_lwp_exit(), in case these resources are in the
613 * PCB.
614 */
615 cpu_wait(l);
616
617 /*
618 * Free the VM resources we're still holding on to.
619 */
620 uvm_lwp_exit(l);
621
622 l->l_stat = LSZOMB;
623 if (l->l_flag & L_DETACHED) {
624 /* Nobody waits for detached LWPs. */
625 LIST_REMOVE(l, l_sibling);
626 p->p_nlwps--;
627 pool_put(&lwp_pool, l);
628 } else {
629 p->p_nzlwps++;
630 wakeup(&p->p_nlwps);
631 }
632 /* XXXNJW where should this be with respect to
633 * the wakeup() above? */
634 KERNEL_PROC_UNLOCK(curlwp);
635 } else {
636 /* Remove proc from the deadproc list. */
637 SLIST_REMOVE_HEAD(&deadprocs, p_dead);
638 simple_unlock(&deadproc_slock);
639 KERNEL_PROC_LOCK(curlwp);
640
641 /*
642 * Free the VM resources we're still holding on to.
643 * We must do this from a valid thread because doing
644 * so may block.
645 */
646 uvm_proc_exit(p);
647
648 /* Process is now a true zombie. */
649 p->p_stat = SZOMB;
650 parent = p->p_pptr;
651 parent->p_nstopchild++;
652 if (LIST_FIRST(&parent->p_children) != p) {
653 /* Put child where it can be found quickly */
654 LIST_REMOVE(p, p_sibling);
655 LIST_INSERT_HEAD(&parent->p_children,
656 p, p_sibling);
657 }
658
659 /* Wake up the parent so it can get exit status. */
660 if ((p->p_flag & P_FSTRACE) == 0 && p->p_exitsig != 0)
661 exit_psignal(p, p->p_pptr);
662 KERNEL_PROC_UNLOCK(curlwp);
663 wakeup(p->p_pptr);
664 }
665 }
666 }
667
668 int
669 sys_wait4(struct lwp *l, void *v, register_t *retval)
670 {
671 struct sys_wait4_args /* {
672 syscallarg(int) pid;
673 syscallarg(int *) status;
674 syscallarg(int) options;
675 syscallarg(struct rusage *) rusage;
676 } */ *uap = v;
677 struct proc *child, *parent;
678 int status, error;
679
680 parent = l->l_proc;
681
682 if (SCARG(uap, pid) == 0)
683 SCARG(uap, pid) = -parent->p_pgid;
684 if (SCARG(uap, options) & ~(WUNTRACED|WNOHANG|WALTSIG|WALLSIG))
685 return (EINVAL);
686
687 error = find_stopped_child(parent, SCARG(uap,pid), SCARG(uap,options),
688 &child);
689 if (error != 0)
690 return error;
691 if (child == NULL) {
692 *retval = 0;
693 return 0;
694 }
695
696 retval[0] = child->p_pid;
697
698 if (child->p_stat == SZOMB) {
699 if (SCARG(uap, status)) {
700 status = child->p_xstat; /* convert to int */
701 error = copyout(&status, SCARG(uap, status),
702 sizeof(status));
703 if (error)
704 return (error);
705 }
706 if (SCARG(uap, rusage)) {
707 error = copyout(child->p_ru, SCARG(uap, rusage),
708 sizeof(struct rusage));
709 if (error)
710 return (error);
711 }
712
713 proc_free(child);
714 return 0;
715 }
716
717 /* child state must be SSTOP */
718 if (SCARG(uap, status)) {
719 status = W_STOPCODE(child->p_xstat);
720 return copyout(&status, SCARG(uap, status), sizeof(status));
721 }
722 return 0;
723 }
724
725 /*
726 * Scan list of child processes for a child process that has stopped or
727 * exited. Used by sys_wait4 and 'compat' equivalents.
728 */
729 int
730 find_stopped_child(struct proc *parent, pid_t pid, int options,
731 struct proc **child_p)
732 {
733 struct proc *child;
734 int error;
735
736 for (;;) {
737 proclist_lock_read();
738 error = ECHILD;
739 LIST_FOREACH(child, &parent->p_children, p_sibling) {
740 if (pid >= 0) {
741 if (child->p_pid != pid) {
742 child = p_find(pid, PFIND_ZOMBIE |
743 PFIND_LOCKED);
744 if (child == NULL
745 || child->p_pptr != parent) {
746 child = NULL;
747 break;
748 }
749 }
750 } else
751 if (pid != WAIT_ANY && child->p_pgid != -pid)
752 /* child not in correct pgrp */
753 continue;
754 /*
755 * Wait for processes with p_exitsig != SIGCHLD
756 * processes only if WALTSIG is set; wait for
757 * processes with p_exitsig == SIGCHLD only
758 * if WALTSIG is clear.
759 */
760 if (((options & WALLSIG) == 0) &&
761 (options & WALTSIG ? child->p_exitsig == SIGCHLD
762 : P_EXITSIG(child) != SIGCHLD)){
763 if (child->p_pid == pid) {
764 child = NULL;
765 break;
766 }
767 continue;
768 }
769
770 error = 0;
771 if (child->p_stat == SZOMB && !(options & WNOZOMBIE))
772 break;
773
774 if (child->p_stat == SSTOP &&
775 (child->p_flag & P_WAITED) == 0 &&
776 (child->p_flag & P_TRACED || options & WUNTRACED)) {
777 if ((options & WNOWAIT) == 0) {
778 child->p_flag |= P_WAITED;
779 parent->p_nstopchild--;
780 }
781 break;
782 }
783 if (parent->p_nstopchild == 0 || child->p_pid == pid) {
784 child = NULL;
785 break;
786 }
787 }
788 proclist_unlock_read();
789 if (child != NULL || error != 0 || options & WNOHANG) {
790 *child_p = child;
791 return error;
792 }
793 error = tsleep(parent, PWAIT | PCATCH, "wait", 0);
794 if (error != 0)
795 return error;
796 }
797 }
798
799 /*
800 * Free a process after parent has taken all the state info.
801 */
802 void
803 proc_free(struct proc *p)
804 {
805 struct proc *parent = p->p_pptr;
806 int s;
807
808 /*
809 * If we got the child via ptrace(2) or procfs, and
810 * the parent is different (meaning the process was
811 * attached, rather than run as a child), then we need
812 * to give it back to the old parent, and send the
813 * parent the exit signal. The rest of the cleanup
814 * will be done when the old parent waits on the child.
815 */
816 if ((p->p_flag & P_TRACED) && p->p_opptr != parent){
817 parent = p->p_opptr;
818 if (parent == NULL)
819 parent = initproc;
820 proc_reparent(p, parent);
821 p->p_opptr = NULL;
822 p->p_flag &= ~(P_TRACED|P_WAITED|P_FSTRACE);
823 if (p->p_exitsig != 0)
824 exit_psignal(p, parent);
825 wakeup(parent);
826 return;
827 }
828
829 scheduler_wait_hook(parent, p);
830 p->p_xstat = 0;
831
832 ruadd(&parent->p_stats->p_cru, p->p_ru);
833
834 /*
835 * At this point we are going to start freeing the final resources.
836 * If anyone tries to access the proc structure after here they
837 * will get a shock - bits are missing.
838 * Attempt to make it hard!
839 */
840
841 p->p_stat = SIDL; /* not even a zombie any more */
842
843 pool_put(&rusage_pool, p->p_ru);
844
845 /*
846 * Finally finished with old proc entry.
847 * Unlink it from its process group and free it.
848 */
849 leavepgrp(p);
850
851 s = proclist_lock_write();
852 LIST_REMOVE(p, p_list); /* off zombproc */
853 p->p_pptr->p_nstopchild--;
854 LIST_REMOVE(p, p_sibling);
855 proclist_unlock_write(s);
856
857 /*
858 * Decrement the count of procs running with this uid.
859 */
860 (void)chgproccnt(p->p_cred->p_ruid, -1);
861
862 /*
863 * Free up credentials.
864 */
865 if (--p->p_cred->p_refcnt == 0) {
866 crfree(p->p_cred->pc_ucred);
867 pool_put(&pcred_pool, p->p_cred);
868 }
869
870 /*
871 * Release reference to text vnode
872 */
873 if (p->p_textvp)
874 vrele(p->p_textvp);
875
876 /* Release any SA state. */
877 if (p->p_sa)
878 sa_release(p);
879
880 /* Free proc structure and let pid be reallocated */
881 proc_free_mem(p);
882 }
883
884 /*
885 * make process 'parent' the new parent of process 'child'.
886 *
887 * Must be called with proclist_lock_write() held.
888 */
889 void
890 proc_reparent(struct proc *child, struct proc *parent)
891 {
892
893 if (child->p_pptr == parent)
894 return;
895
896 if (child->p_stat == SZOMB
897 || (child->p_stat == SSTOP && !(child->p_flag & P_WAITED))) {
898 child->p_pptr->p_nstopchild--;
899 parent->p_nstopchild++;
900 }
901 if (parent == initproc)
902 child->p_exitsig = SIGCHLD;
903
904 LIST_REMOVE(child, p_sibling);
905 LIST_INSERT_HEAD(&parent->p_children, child, p_sibling);
906 child->p_pptr = parent;
907 }
908