xref: /netbsd-src/sys/kern/kern_exit.c (revision 8ac07aec990b9d2e483062509d0a9fa5b4f57cf2)
1 /*	$NetBSD: kern_exit.c,v 1.204 2008/04/24 18:39:24 ad Exp $	*/
2 
3 /*-
4  * Copyright (c) 1998, 1999, 2006, 2007, 2008 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, and by Andrew Doran.
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.204 2008/04/24 18:39:24 ad Exp $");
78 
79 #include "opt_ktrace.h"
80 #include "opt_perfctrs.h"
81 #include "opt_sysv.h"
82 
83 #include <sys/param.h>
84 #include <sys/aio.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/proc.h>
92 #include <sys/buf.h>
93 #include <sys/wait.h>
94 #include <sys/file.h>
95 #include <sys/vnode.h>
96 #include <sys/syslog.h>
97 #include <sys/malloc.h>
98 #include <sys/pool.h>
99 #include <sys/resourcevar.h>
100 #if defined(PERFCTRS)
101 #include <sys/pmc.h>
102 #endif
103 #include <sys/ptrace.h>
104 #include <sys/acct.h>
105 #include <sys/filedesc.h>
106 #include <sys/ras.h>
107 #include <sys/signalvar.h>
108 #include <sys/sched.h>
109 #include <sys/mount.h>
110 #include <sys/syscallargs.h>
111 #include <sys/kauth.h>
112 #include <sys/sleepq.h>
113 #include <sys/lockdebug.h>
114 #include <sys/ktrace.h>
115 #include <sys/cpu.h>
116 #include <sys/lwpctl.h>
117 #include <sys/atomic.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 int find_stopped_child(struct proc *, pid_t, int, struct proc **, int *);
131 static void proc_free(struct proc *, struct rusage *);
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, ksiginfo_t *ksi)
138 {
139 
140 	KSI_INIT(ksi);
141 	if ((ksi->ksi_signo = P_EXITSIG(p)) == SIGCHLD) {
142 		if (WIFSIGNALED(p->p_xstat)) {
143 			if (WCOREDUMP(p->p_xstat))
144 				ksi->ksi_code = CLD_DUMPED;
145 			else
146 				ksi->ksi_code = CLD_KILLED;
147 		} else {
148 			ksi->ksi_code = CLD_EXITED;
149 		}
150 	}
151 	/*
152 	 * We fill those in, even for non-SIGCHLD.
153 	 * It's safe to access p->p_cred unlocked here.
154 	 */
155 	ksi->ksi_pid = p->p_pid;
156 	ksi->ksi_uid = kauth_cred_geteuid(p->p_cred);
157 	ksi->ksi_status = p->p_xstat;
158 	/* XXX: is this still valid? */
159 	ksi->ksi_utime = p->p_stats->p_ru.ru_utime.tv_sec;
160 	ksi->ksi_stime = p->p_stats->p_ru.ru_stime.tv_sec;
161 }
162 
163 /*
164  * exit --
165  *	Death of process.
166  */
167 int
168 sys_exit(struct lwp *l, const struct sys_exit_args *uap, register_t *retval)
169 {
170 	/* {
171 		syscallarg(int)	rval;
172 	} */
173 	struct proc *p = l->l_proc;
174 
175 	/* Don't call exit1() multiple times in the same process. */
176 	mutex_enter(p->p_lock);
177 	if (p->p_sflag & PS_WEXIT) {
178 		mutex_exit(p->p_lock);
179 		lwp_exit(l);
180 	}
181 
182 	/* exit1() will release the mutex. */
183 	exit1(l, W_EXITCODE(SCARG(uap, rval), 0));
184 	/* NOTREACHED */
185 	return (0);
186 }
187 
188 /*
189  * Exit: deallocate address space and other resources, change proc state
190  * to zombie, and unlink proc from allproc and parent's lists.  Save exit
191  * status and rusage for wait().  Check for child processes and orphan them.
192  *
193  * Must be called with p->p_lock held.  Does not return.
194  */
195 void
196 exit1(struct lwp *l, int rv)
197 {
198 	struct proc	*p, *q, *nq;
199 	struct pgrp	*pgrp;
200 	ksiginfo_t	ksi;
201 	ksiginfoq_t	kq;
202 	int		wakeinit;
203 
204 	p = l->l_proc;
205 
206 	KASSERT(mutex_owned(p->p_lock));
207 
208 	if (__predict_false(p == initproc))
209 		panic("init died (signal %d, exit %d)",
210 		    WTERMSIG(rv), WEXITSTATUS(rv));
211 
212 	p->p_sflag |= PS_WEXIT;
213 
214 	/*
215 	 * Force all other LWPs to exit before we do.  Only then can we
216 	 * begin to tear down the rest of the process state.
217 	 */
218 	if (p->p_nlwps > 1)
219 		exit_lwps(l);
220 
221 	ksiginfo_queue_init(&kq);
222 
223 	/*
224 	 * If we have been asked to stop on exit, do so now.
225 	 */
226 	if (p->p_sflag & PS_STOPEXIT) {
227 		KERNEL_UNLOCK_ALL(l, &l->l_biglocks);
228 		sigclearall(p, &contsigmask, &kq);
229 		p->p_waited = 0;
230 		membar_producer();
231 		p->p_stat = SSTOP;
232 		lwp_lock(l);
233 		p->p_nrlwps--;
234 		l->l_stat = LSSTOP;
235 		mutex_exit(p->p_lock);
236 		mi_switch(l);
237 		KERNEL_LOCK(l->l_biglocks, l);
238 	} else
239 		mutex_exit(p->p_lock);
240 
241 	/* Destroy any lwpctl info. */
242 	if (p->p_lwpctl != NULL)
243 		lwp_ctl_exit();
244 
245 	/* Destroy all AIO works */
246 	aio_exit(p, p->p_aio);
247 
248 	/*
249 	 * Drain all remaining references that procfs, ptrace and others may
250 	 * have on the process.
251 	 */
252 	rw_enter(&p->p_reflock, RW_WRITER);
253 
254 	/*
255 	 * Bin any remaining signals and mark the process as dying so it will
256 	 * not be found for, e.g. signals.
257 	 */
258 	mutex_enter(p->p_lock);
259 	sigfillset(&p->p_sigctx.ps_sigignore);
260 	sigclearall(p, NULL, &kq);
261 	p->p_stat = SDYING;
262 	mutex_exit(p->p_lock);
263 	ksiginfo_queue_drain(&kq);
264 
265 	DPRINTF(("exit1: %d.%d exiting.\n", p->p_pid, l->l_lid));
266 
267 #ifdef PGINPROF
268 	vmsizmon();
269 #endif
270 	timers_free(p, TIMERS_ALL);
271 #if defined(__HAVE_RAS)
272 	ras_purgeall();
273 #endif
274 
275 	/*
276 	 * Close open files, release open-file table and free signal
277 	 * actions.  This may block!
278 	 */
279 	fd_free();
280 	cwdfree(p->p_cwdi);
281 	p->p_cwdi = NULL;
282 	doexithooks(p);
283 	sigactsfree(p->p_sigacts);
284 
285 	/*
286 	 * Write out accounting data.
287 	 */
288 	(void)acct_process(l);
289 
290 #ifdef KTRACE
291 	/*
292 	 * Release trace file.
293 	 */
294 	if (p->p_tracep != NULL) {
295 		mutex_enter(&ktrace_lock);
296 		ktrderef(p);
297 		mutex_exit(&ktrace_lock);
298 	}
299 #endif
300 
301 	/*
302 	 * If emulation has process exit hook, call it now.
303 	 * Set the exit status now so that the exit hook has
304 	 * an opportunity to tweak it (COMPAT_LINUX requires
305 	 * this for thread group emulation)
306 	 */
307 	p->p_xstat = rv;
308 	if (p->p_emul->e_proc_exit)
309 		(*p->p_emul->e_proc_exit)(p);
310 
311 	/*
312 	 * Free the VM resources we're still holding on to.
313 	 * We must do this from a valid thread because doing
314 	 * so may block. This frees vmspace, which we don't
315 	 * need anymore. The only remaining lwp is the one
316 	 * we run at this moment, nothing runs in userland
317 	 * anymore.
318 	 */
319 	uvm_proc_exit(p);
320 
321 	/*
322 	 * While we can still block, and mark the LWP as unswappable to
323 	 * prevent conflicts with the with the swapper.  We also shouldn't
324 	 * be swapped out, because we are about to exit and will release
325 	 * memory.
326 	 */
327 	uvm_lwp_hold(l);
328 
329 	/*
330 	 * Stop profiling.
331 	 */
332 	if ((p->p_stflag & PST_PROFIL) != 0) {
333 		mutex_spin_enter(&p->p_stmutex);
334 		stopprofclock(p);
335 		mutex_spin_exit(&p->p_stmutex);
336 	}
337 
338 	/*
339 	 * If parent is waiting for us to exit or exec, P_PPWAIT is set; we
340 	 * wake up the parent early to avoid deadlock.  We can do this once
341 	 * the VM resources are released.
342 	 */
343 	mutex_enter(proc_lock);
344 	mutex_enter(p->p_lock);
345 	if (p->p_sflag & PS_PPWAIT) {
346 		p->p_sflag &= ~PS_PPWAIT;
347 		cv_broadcast(&p->p_pptr->p_waitcv);
348 	}
349 	mutex_exit(p->p_lock);
350 
351 	if (SESS_LEADER(p)) {
352 		struct vnode *vprele = NULL, *vprevoke = NULL;
353 		struct session *sp = p->p_session;
354 		struct tty *tp;
355 
356 		if (sp->s_ttyvp) {
357 			/*
358 			 * Controlling process.
359 			 * Signal foreground pgrp,
360 			 * drain controlling terminal
361 			 * and revoke access to controlling terminal.
362 			 */
363 			tp = sp->s_ttyp;
364 			mutex_spin_enter(&tty_lock);
365 			if (tp->t_session == sp) {
366 				/* we can't guarantee the revoke will do this */
367 				pgrp = tp->t_pgrp;
368 				tp->t_pgrp = NULL;
369 				tp->t_session = NULL;
370 				mutex_spin_exit(&tty_lock);
371 				if (pgrp != NULL) {
372 					pgsignal(pgrp, SIGHUP, 1);
373 				}
374 				mutex_exit(proc_lock);
375 				(void) ttywait(tp);
376 				mutex_enter(proc_lock);
377 
378 				/* The tty could have been revoked. */
379 				vprevoke = sp->s_ttyvp;
380 			} else
381 				mutex_spin_exit(&tty_lock);
382 			vprele = sp->s_ttyvp;
383 			sp->s_ttyvp = NULL;
384 			/*
385 			 * s_ttyp is not zero'd; we use this to indicate
386 			 * that the session once had a controlling terminal.
387 			 * (for logging and informational purposes)
388 			 */
389 		}
390 		sp->s_leader = NULL;
391 
392 		if (vprevoke != NULL || vprele != NULL) {
393 			if (vprevoke != NULL) {
394 				SESSRELE(sp);
395 				mutex_exit(proc_lock);
396 				VOP_REVOKE(vprevoke, REVOKEALL);
397 			} else
398 				mutex_exit(proc_lock);
399 			if (vprele != NULL)
400 				vrele(vprele);
401 			mutex_enter(proc_lock);
402 		}
403 	}
404 	fixjobc(p, p->p_pgrp, 0);
405 
406 	/*
407 	 * Finalize the last LWP's specificdata, as well as the
408 	 * specificdata for the proc itself.
409 	 */
410 	lwp_finispecific(l);
411 	proc_finispecific(p);
412 
413 	/*
414 	 * Notify interested parties of our demise.
415 	 */
416 	KNOTE(&p->p_klist, NOTE_EXIT);
417 
418 
419 
420 #if PERFCTRS
421 	/*
422 	 * Save final PMC information in parent process & clean up.
423 	 */
424 	if (PMC_ENABLED(p)) {
425 		pmc_save_context(p);
426 		pmc_accumulate(p->p_pptr, p);
427 		pmc_process_exit(p);
428 	}
429 #endif
430 
431 	/*
432 	 * Reset p_opptr pointer of all former children which got
433 	 * traced by another process and were reparented. We reset
434 	 * it to NULL here; the trace detach code then reparents
435 	 * the child to initproc. We only check allproc list, since
436 	 * eventual former children on zombproc list won't reference
437 	 * p_opptr anymore.
438 	 */
439 	if (p->p_slflag & PSL_CHTRACED) {
440 		PROCLIST_FOREACH(q, &allproc) {
441 			if (q->p_opptr == p)
442 				q->p_opptr = NULL;
443 		}
444 	}
445 
446 	/*
447 	 * Give orphaned children to init(8).
448 	 */
449 	q = LIST_FIRST(&p->p_children);
450 	wakeinit = (q != NULL);
451 	for (; q != NULL; q = nq) {
452 		nq = LIST_NEXT(q, p_sibling);
453 
454 		/*
455 		 * Traced processes are killed since their existence
456 		 * means someone is screwing up. Since we reset the
457 		 * trace flags, the logic in sys_wait4() would not be
458 		 * triggered to reparent the process to its
459 		 * original parent, so we must do this here.
460 		 */
461 		if (q->p_slflag & PSL_TRACED) {
462 			mutex_enter(p->p_lock);
463 			q->p_slflag &= ~(PSL_TRACED|PSL_FSTRACE|PSL_SYSCALL);
464 			mutex_exit(p->p_lock);
465 			if (q->p_opptr != q->p_pptr) {
466 				struct proc *t = q->p_opptr;
467 				proc_reparent(q, t ? t : initproc);
468 				q->p_opptr = NULL;
469 			} else
470 				proc_reparent(q, initproc);
471 			killproc(q, "orphaned traced process");
472 		} else
473 			proc_reparent(q, initproc);
474 	}
475 
476 	/*
477 	 * Move proc from allproc to zombproc, it's now nearly ready to be
478 	 * collected by parent.
479 	 */
480 	LIST_REMOVE(l, l_list);
481 	LIST_REMOVE(p, p_list);
482 	LIST_INSERT_HEAD(&zombproc, p, p_list);
483 
484 	/*
485 	 * Mark the process as dead.  We must do this before we signal
486 	 * the parent.
487 	 */
488 	p->p_stat = SDEAD;
489 
490 	/* Put in front of parent's sibling list for parent to collect it */
491 	q = p->p_pptr;
492 	q->p_nstopchild++;
493 	if (LIST_FIRST(&q->p_children) != p) {
494 		/* Put child where it can be found quickly */
495 		LIST_REMOVE(p, p_sibling);
496 		LIST_INSERT_HEAD(&q->p_children, p, p_sibling);
497 	}
498 
499 	/*
500 	 * Notify parent that we're gone.  If parent has the P_NOCLDWAIT
501 	 * flag set, notify init instead (and hope it will handle
502 	 * this situation).
503 	 */
504 	mutex_enter(q->p_lock);
505 	if (q->p_flag & (PK_NOCLDWAIT|PK_CLDSIGIGN)) {
506 		proc_reparent(p, initproc);
507 		wakeinit = 1;
508 
509 		/*
510 		 * If this was the last child of our parent, notify
511 		 * parent, so in case he was wait(2)ing, he will
512 		 * continue.
513 		 */
514 		if (LIST_FIRST(&q->p_children) == NULL)
515 			cv_broadcast(&q->p_waitcv);
516 	}
517 	mutex_exit(q->p_lock);
518 
519 	/* Reload parent pointer, since p may have been reparented above */
520 	q = p->p_pptr;
521 
522 	if ((p->p_slflag & PSL_FSTRACE) == 0 && p->p_exitsig != 0) {
523 		exit_psignal(p, q, &ksi);
524 		kpsignal(q, &ksi, NULL);
525 	}
526 
527 	/* Calculate the final rusage info.  */
528 	calcru(p, &p->p_stats->p_ru.ru_utime, &p->p_stats->p_ru.ru_stime,
529 	    NULL, NULL);
530 
531 	if (wakeinit)
532 		cv_broadcast(&initproc->p_waitcv);
533 
534 	callout_destroy(&l->l_timeout_ch);
535 
536 	/*
537 	 * Remaining lwp resources will be freed in lwp_exit2() once we've
538 	 * switch to idle context; at that point, we will be marked as a
539 	 * full blown zombie.
540 	 *
541 	 * XXXSMP disable preemption.
542 	 */
543 	mutex_enter(p->p_lock);
544 	lwp_drainrefs(l);
545 	lwp_lock(l);
546 	l->l_prflag &= ~LPR_DETACHED;
547 	l->l_stat = LSZOMB;
548 	lwp_unlock(l);
549 	KASSERT(curlwp == l);
550 	KASSERT(p->p_nrlwps == 1);
551 	KASSERT(p->p_nlwps == 1);
552 	p->p_stat = SZOMB;
553 	p->p_nrlwps--;
554 	p->p_nzlwps++;
555 	p->p_ndlwps = 0;
556 	mutex_exit(p->p_lock);
557 
558 	/*
559 	 * Signal the parent to collect us, and drop the proclist lock.
560 	 * Drop debugger/procfs lock; no new references can be gained.
561 	 */
562 	cv_broadcast(&p->p_pptr->p_waitcv);
563 	mutex_exit(proc_lock);
564 	rw_exit(&p->p_reflock);
565 
566 	/* Verify that we hold no locks other than the kernel lock. */
567 #ifdef MULTIPROCESSOR
568 	LOCKDEBUG_BARRIER(&kernel_lock, 0);
569 #else
570 	LOCKDEBUG_BARRIER(NULL, 0);
571 #endif
572 
573 	/*
574 	 * NOTE: WE ARE NO LONGER ALLOWED TO SLEEP!
575 	 */
576 
577 	/*
578 	 * Give machine-dependent code a chance to free any MD LWP
579 	 * resources.  This must be done before uvm_lwp_exit(), in
580 	 * case these resources are in the PCB.
581 	 */
582 #ifndef __NO_CPU_LWP_FREE
583 	cpu_lwp_free(l, 1);
584 #endif
585 	pmap_deactivate(l);
586 
587 	/* This process no longer needs to hold the kernel lock. */
588 #ifdef notyet
589 	/* XXXSMP hold in lwp_userret() */
590 	KERNEL_UNLOCK_LAST(l);
591 #else
592 	KERNEL_UNLOCK_ALL(l, NULL);
593 #endif
594 
595 	lwp_exit_switchaway(l);
596 }
597 
598 void
599 exit_lwps(struct lwp *l)
600 {
601 	struct proc *p;
602 	struct lwp *l2;
603 	int error;
604 	lwpid_t waited;
605 #if defined(MULTIPROCESSOR)
606 	int nlocks;
607 #endif
608 
609 	KERNEL_UNLOCK_ALL(l, &nlocks);
610 
611 	p = l->l_proc;
612 	KASSERT(mutex_owned(p->p_lock));
613 
614  retry:
615 	/*
616 	 * Interrupt LWPs in interruptable sleep, unsuspend suspended
617 	 * LWPs and then wait for everyone else to finish.
618 	 */
619 	LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
620 		if (l2 == l)
621 			continue;
622 		lwp_lock(l2);
623 		l2->l_flag |= LW_WEXIT;
624 		if ((l2->l_stat == LSSLEEP && (l2->l_flag & LW_SINTR)) ||
625 		    l2->l_stat == LSSUSPENDED || l2->l_stat == LSSTOP) {
626 		    	/* setrunnable() will release the lock. */
627 			setrunnable(l2);
628 			DPRINTF(("exit_lwps: Made %d.%d runnable\n",
629 			    p->p_pid, l2->l_lid));
630 			continue;
631 		}
632 		lwp_unlock(l2);
633 	}
634 	while (p->p_nlwps > 1) {
635 		DPRINTF(("exit_lwps: waiting for %d LWPs (%d zombies)\n",
636 		    p->p_nlwps, p->p_nzlwps));
637 		error = lwp_wait1(l, 0, &waited, LWPWAIT_EXITCONTROL);
638 		if (p->p_nlwps == 1)
639 			break;
640 		if (error == EDEADLK) {
641 			/*
642 			 * LWPs can get suspended/slept behind us.
643 			 * (eg. sa_setwoken)
644 			 * kick them again and retry.
645 			 */
646 			goto retry;
647 		}
648 		if (error)
649 			panic("exit_lwps: lwp_wait1 failed with error %d",
650 			    error);
651 		DPRINTF(("exit_lwps: Got LWP %d from lwp_wait1()\n", waited));
652 	}
653 
654 #if defined(MULTIPROCESSOR)
655 	if (nlocks > 0) {
656 		mutex_exit(p->p_lock);
657 		KERNEL_LOCK(nlocks, l);
658 		mutex_enter(p->p_lock);
659 	}
660 #endif /* defined(MULTIPROCESSOR) */
661 	KASSERT(p->p_nlwps == 1);
662 }
663 
664 int
665 do_sys_wait(struct lwp *l, int *pid, int *status, int options,
666     struct rusage *ru, int *was_zombie)
667 {
668 	struct proc	*child;
669 	int		error;
670 
671 	mutex_enter(proc_lock);
672 	error = find_stopped_child(l->l_proc, *pid, options, &child, status);
673 
674 	if (child == NULL) {
675 		mutex_exit(proc_lock);
676 		*pid = 0;
677 		return error;
678 	}
679 
680 	*pid = child->p_pid;
681 
682 	if (child->p_stat == SZOMB) {
683 		/* proc_free() will release the proc_lock. */
684 		*was_zombie = 1;
685 		if (options & WNOWAIT)
686 			mutex_exit(proc_lock);
687 		else {
688 			proc_free(child, ru);
689 		}
690 	} else {
691 		/* Child state must have been SSTOP. */
692 		*was_zombie = 0;
693 		mutex_exit(proc_lock);
694 		*status = W_STOPCODE(*status);
695 	}
696 
697 	return 0;
698 }
699 
700 int
701 sys_wait4(struct lwp *l, const struct sys_wait4_args *uap, register_t *retval)
702 {
703 	/* {
704 		syscallarg(int)			pid;
705 		syscallarg(int *)		status;
706 		syscallarg(int)			options;
707 		syscallarg(struct rusage *)	rusage;
708 	} */
709 	int		status, error;
710 	int		was_zombie;
711 	struct rusage	ru;
712 	int pid = SCARG(uap, pid);
713 
714 	error = do_sys_wait(l, &pid, &status, SCARG(uap, options),
715 	    SCARG(uap, rusage) != NULL ? &ru : NULL, &was_zombie);
716 
717 	retval[0] = pid;
718 	if (pid == 0)
719 		return error;
720 
721 	if (SCARG(uap, rusage))
722 		error = copyout(&ru, SCARG(uap, rusage), sizeof(ru));
723 
724 	if (error == 0 && SCARG(uap, status))
725 		error = copyout(&status, SCARG(uap, status), sizeof(status));
726 
727 	return error;
728 }
729 
730 /*
731  * Scan list of child processes for a child process that has stopped or
732  * exited.  Used by sys_wait4 and 'compat' equivalents.
733  *
734  * Must be called with the proc_lock held, and may release while waiting.
735  */
736 static int
737 find_stopped_child(struct proc *parent, pid_t pid, int options,
738 		   struct proc **child_p, int *status_p)
739 {
740 	struct proc *child, *dead;
741 	int error;
742 
743 	KASSERT(mutex_owned(proc_lock));
744 
745 	if (options & ~(WUNTRACED|WNOHANG|WALTSIG|WALLSIG)
746 	    && !(options & WOPTSCHECKED)) {
747 		*child_p = NULL;
748 		return EINVAL;
749 	}
750 
751 	if (pid == 0 && !(options & WOPTSCHECKED))
752 		pid = -parent->p_pgid;
753 
754 	for (;;) {
755 		error = ECHILD;
756 		dead = NULL;
757 
758 		LIST_FOREACH(child, &parent->p_children, p_sibling) {
759 			if (pid >= 0) {
760 				if (child->p_pid != pid) {
761 					child = p_find(pid, PFIND_ZOMBIE |
762 					    PFIND_LOCKED);
763 					if (child == NULL ||
764 					    child->p_pptr != parent) {
765 						child = NULL;
766 						break;
767 					}
768 				}
769 			} else if (pid != WAIT_ANY && child->p_pgid != -pid) {
770 				/* Child not in correct pgrp */
771 				continue;
772 			}
773 
774 			/*
775 			 * Wait for processes with p_exitsig != SIGCHLD
776 			 * processes only if WALTSIG is set; wait for
777 			 * processes with p_exitsig == SIGCHLD only
778 			 * if WALTSIG is clear.
779 			 */
780 			if (((options & WALLSIG) == 0) &&
781 			    (options & WALTSIG ? child->p_exitsig == SIGCHLD
782 						: P_EXITSIG(child) != SIGCHLD)){
783 				if (child->p_pid == pid) {
784 					child = NULL;
785 					break;
786 				}
787 				continue;
788 			}
789 
790 			error = 0;
791 			if ((options & WNOZOMBIE) == 0) {
792 				if (child->p_stat == SZOMB)
793 					break;
794 				if (child->p_stat == SDEAD) {
795 					/*
796 					 * We may occasionally arrive here
797 					 * after receiving a signal, but
798 					 * immediatley before the child
799 					 * process is zombified.  The wait
800 					 * will be short, so avoid returning
801 					 * to userspace.
802 					 */
803 					dead = child;
804 				}
805 			}
806 
807 			if (child->p_stat == SSTOP &&
808 			    child->p_waited == 0 &&
809 			    (child->p_slflag & PSL_TRACED ||
810 			    options & WUNTRACED)) {
811 				if ((options & WNOWAIT) == 0) {
812 					child->p_waited = 1;
813 					parent->p_nstopchild--;
814 				}
815 				break;
816 			}
817 			if (parent->p_nstopchild == 0 || child->p_pid == pid) {
818 				child = NULL;
819 				break;
820 			}
821 		}
822 
823 		if (child != NULL || error != 0 ||
824 		    ((options & WNOHANG) != 0 && dead == NULL)) {
825 		    	if (child != NULL) {
826 			    	*status_p = child->p_xstat;
827 			}
828 			*child_p = child;
829 			return error;
830 		}
831 
832 		/*
833 		 * Wait for another child process to stop.
834 		 */
835 		error = cv_wait_sig(&parent->p_waitcv, proc_lock);
836 
837 		if (error != 0) {
838 			*child_p = NULL;
839 			return error;
840 		}
841 	}
842 }
843 
844 /*
845  * Free a process after parent has taken all the state info.  Must be called
846  * with the proclist lock held, and will release before returning.
847  *
848  * *ru is returned to the caller, and must be freed by the caller.
849  */
850 static void
851 proc_free(struct proc *p, struct rusage *ru)
852 {
853 	struct proc *parent;
854 	struct lwp *l;
855 	ksiginfo_t ksi;
856 	kauth_cred_t cred1, cred2;
857 	uid_t uid;
858 
859 	KASSERT(mutex_owned(proc_lock));
860 	KASSERT(p->p_nlwps == 1);
861 	KASSERT(p->p_nzlwps == 1);
862 	KASSERT(p->p_nrlwps == 0);
863 	KASSERT(p->p_stat == SZOMB);
864 
865 	/*
866 	 * If we got the child via ptrace(2) or procfs, and
867 	 * the parent is different (meaning the process was
868 	 * attached, rather than run as a child), then we need
869 	 * to give it back to the old parent, and send the
870 	 * parent the exit signal.  The rest of the cleanup
871 	 * will be done when the old parent waits on the child.
872 	 */
873 	if ((p->p_slflag & PSL_TRACED) != 0) {
874 		parent = p->p_pptr;
875 		if (p->p_opptr != parent){
876 			mutex_enter(p->p_lock);
877 			p->p_slflag &= ~(PSL_TRACED|PSL_FSTRACE|PSL_SYSCALL);
878 			mutex_exit(p->p_lock);
879 			parent = p->p_opptr;
880 			if (parent == NULL)
881 				parent = initproc;
882 			proc_reparent(p, parent);
883 			p->p_opptr = NULL;
884 			if (p->p_exitsig != 0) {
885 				exit_psignal(p, parent, &ksi);
886 				kpsignal(parent, &ksi, NULL);
887 			}
888 			cv_broadcast(&parent->p_waitcv);
889 			mutex_exit(proc_lock);
890 			return;
891 		}
892 	}
893 
894 	/*
895 	 * Finally finished with old proc entry.  Unlink it from its process
896 	 * group.
897 	 */
898 	leavepgrp(p);
899 
900 	parent = p->p_pptr;
901 	sched_proc_exit(parent, p);
902 
903 	/*
904 	 * Add child times of exiting process onto its own times.
905 	 * This cannot be done any earlier else it might get done twice.
906 	 */
907 	l = LIST_FIRST(&p->p_lwps);
908 	p->p_stats->p_ru.ru_nvcsw += (l->l_ncsw - l->l_nivcsw);
909 	p->p_stats->p_ru.ru_nivcsw += l->l_nivcsw;
910 	ruadd(&p->p_stats->p_ru, &l->l_ru);
911 	ruadd(&p->p_stats->p_ru, &p->p_stats->p_cru);
912 	ruadd(&parent->p_stats->p_cru, &p->p_stats->p_ru);
913 	if (ru != NULL)
914 		*ru = p->p_stats->p_ru;
915 	p->p_xstat = 0;
916 
917 	/*
918 	 * At this point we are going to start freeing the final resources.
919 	 * If anyone tries to access the proc structure after here they will
920 	 * get a shock - bits are missing.  Attempt to make it hard!  We
921 	 * don't bother with any further locking past this point.
922 	 */
923 	p->p_stat = SIDL;		/* not even a zombie any more */
924 	LIST_REMOVE(p, p_list);	/* off zombproc */
925 	parent = p->p_pptr;
926 	p->p_pptr->p_nstopchild--;
927 	LIST_REMOVE(p, p_sibling);
928 
929 	/*
930 	 * Let pid be reallocated.
931 	 */
932 	proc_free_pid(p);
933 	mutex_exit(proc_lock);
934 
935 	/*
936 	 * Delay release until after lwp_free.
937 	 */
938 	cred2 = l->l_cred;
939 
940 	/*
941 	 * Free the last LWP's resources.
942 	 *
943 	 * lwp_free ensures the LWP is no longer running on another CPU.
944 	 */
945 	lwp_free(l, false, true);
946 
947 	/*
948 	 * Now no one except us can reach the process p.
949 	 */
950 
951 	/*
952 	 * Decrement the count of procs running with this uid.
953 	 */
954 	cred1 = p->p_cred;
955 	uid = kauth_cred_getuid(cred1);
956 	(void)chgproccnt(uid, -1);
957 
958 	/*
959 	 * Release substructures.
960 	 */
961 
962 	limfree(p->p_limit);
963 	pstatsfree(p->p_stats);
964 	kauth_cred_free(cred1);
965 	kauth_cred_free(cred2);
966 
967 	/*
968 	 * Release reference to text vnode
969 	 */
970 	if (p->p_textvp)
971 		vrele(p->p_textvp);
972 
973 	mutex_destroy(&p->p_auxlock);
974 	mutex_obj_free(p->p_lock);
975 	mutex_destroy(&p->p_stmutex);
976 	cv_destroy(&p->p_waitcv);
977 	cv_destroy(&p->p_lwpcv);
978 	rw_destroy(&p->p_reflock);
979 
980 	proc_free_mem(p);
981 }
982 
983 /*
984  * make process 'parent' the new parent of process 'child'.
985  *
986  * Must be called with proc_lock held.
987  */
988 void
989 proc_reparent(struct proc *child, struct proc *parent)
990 {
991 
992 	KASSERT(mutex_owned(proc_lock));
993 
994 	if (child->p_pptr == parent)
995 		return;
996 
997 	if (child->p_stat == SZOMB ||
998 	    (child->p_stat == SSTOP && !child->p_waited)) {
999 		child->p_pptr->p_nstopchild--;
1000 		parent->p_nstopchild++;
1001 	}
1002 	if (parent == initproc)
1003 		child->p_exitsig = SIGCHLD;
1004 
1005 	LIST_REMOVE(child, p_sibling);
1006 	LIST_INSERT_HEAD(&parent->p_children, child, p_sibling);
1007 	child->p_pptr = parent;
1008 }
1009