xref: /freebsd/sys/kern/kern_exit.c (revision 628f583ce90d3587595c2f4dd16d57eec3511af3)
1 /*
2  * Copyright (c) 1982, 1986, 1989, 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  * (c) UNIX System Laboratories, Inc.
5  * All or some portions of this file are derived from material licensed
6  * to the University of California by American Telephone and Telegraph
7  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8  * the permission of UNIX System Laboratories, Inc.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the University of
21  *	California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *	@(#)kern_exit.c	8.7 (Berkeley) 2/12/94
39  * $FreeBSD$
40  */
41 
42 #include "opt_compat.h"
43 #include "opt_ktrace.h"
44 #include "opt_mac.h"
45 
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/sysproto.h>
49 #include <sys/eventhandler.h>
50 #include <sys/kernel.h>
51 #include <sys/malloc.h>
52 #include <sys/lock.h>
53 #include <sys/mutex.h>
54 #include <sys/proc.h>
55 #include <sys/pioctl.h>
56 #include <sys/tty.h>
57 #include <sys/wait.h>
58 #include <sys/vmmeter.h>
59 #include <sys/vnode.h>
60 #include <sys/resourcevar.h>
61 #include <sys/signalvar.h>
62 #include <sys/sched.h>
63 #include <sys/sx.h>
64 #include <sys/ptrace.h>
65 #include <sys/acct.h>		/* for acct_process() function prototype */
66 #include <sys/filedesc.h>
67 #include <sys/mac.h>
68 #include <sys/shm.h>
69 #include <sys/sem.h>
70 #include <sys/jail.h>
71 #ifdef KTRACE
72 #include <sys/ktrace.h>
73 #endif
74 
75 #include <vm/vm.h>
76 #include <vm/vm_extern.h>
77 #include <vm/vm_param.h>
78 #include <vm/pmap.h>
79 #include <vm/vm_map.h>
80 #include <vm/vm_page.h>
81 #include <vm/uma.h>
82 #include <sys/user.h>
83 
84 /* Required to be non-static for SysVR4 emulator */
85 MALLOC_DEFINE(M_ZOMBIE, "zombie", "zombie proc status");
86 
87 static int wait1(struct thread *, struct wait_args *, int);
88 
89 /*
90  * exit --
91  *	Death of process.
92  *
93  * MPSAFE
94  */
95 void
96 sys_exit(struct thread *td, struct sys_exit_args *uap)
97 {
98 
99 	mtx_lock(&Giant);
100 	exit1(td, W_EXITCODE(uap->rval, 0));
101 	/* NOTREACHED */
102 }
103 
104 /*
105  * Exit: deallocate address space and other resources, change proc state
106  * to zombie, and unlink proc from allproc and parent's lists.  Save exit
107  * status and rusage for wait().  Check for child processes and orphan them.
108  */
109 void
110 exit1(struct thread *td, int rv)
111 {
112 	struct proc *p, *nq, *q;
113 	struct tty *tp;
114 	struct vnode *ttyvp;
115 	struct vmspace *vm;
116 	struct vnode *vtmp;
117 #ifdef KTRACE
118 	struct vnode *tracevp;
119 	struct ucred *tracecred;
120 #endif
121 
122 	GIANT_REQUIRED;
123 
124 	p = td->td_proc;
125 	if (p == initproc) {
126 		printf("init died (signal %d, exit %d)\n",
127 		    WTERMSIG(rv), WEXITSTATUS(rv));
128 		panic("Going nowhere without my init!");
129 	}
130 
131 	/*
132 	 * MUST abort all other threads before proceeding past here.
133 	 */
134 	PROC_LOCK(p);
135 	if (p->p_flag & P_THREADED || p->p_numthreads > 1) {
136 		/*
137 		 * First check if some other thread got here before us..
138 		 * if so, act apropriatly, (exit or suspend);
139 		 */
140 		DROP_GIANT();
141 		thread_suspend_check(0);
142 		PICKUP_GIANT();
143 
144 		/*
145 		 * Kill off the other threads. This requires
146 		 * Some co-operation from other parts of the kernel
147 		 * so it may not be instant.
148 		 * With this state set:
149 		 * Any thread entering the kernel from userspace will
150 		 * thread_exit() in trap().  Any thread attempting to
151 		 * sleep will return immediatly
152 		 * with EINTR or EWOULDBLOCK, which will hopefully force them
153 		 * to back out to userland, freeing resources as they go, and
154 		 * anything attempting to return to userland will thread_exit()
155 		 * from userret().  thread_exit() will unsuspend us
156 		 * when the last other thread exits.
157 		 */
158 		if (thread_single(SINGLE_EXIT)) {
159 			panic ("Exit: Single threading fouled up");
160 		}
161 		/*
162 		 * All other activity in this process is now stopped.
163 		 * Remove excess KSEs and KSEGRPS. XXXKSE (when we have them)
164 		 * ...
165 		 * Turn off threading support.
166 		 */
167 		p->p_flag &= ~P_THREADED;
168 		thread_single_end();	/* Don't need this any more. */
169 	}
170 	/*
171 	 * With this state set:
172 	 * Any thread entering the kernel from userspace will thread_exit()
173 	 * in trap().  Any thread attempting to sleep will return immediatly
174 	 * with EINTR or EWOULDBLOCK, which will hopefully force them
175 	 * to back out to userland, freeing resources as they go, and
176 	 * anything attempting to return to userland will thread_exit()
177 	 * from userret().  thread_exit() will do a wakeup on p->p_numthreads
178 	 * if it transitions to 1.
179 	 */
180 
181 	p->p_flag |= P_WEXIT;
182 	PROC_UNLOCK(p);
183 
184 	/* Are we a task leader? */
185 	if (p == p->p_leader) {
186 		mtx_lock(&ppeers_lock);
187 		q = p->p_peers;
188 		while (q != NULL) {
189 			PROC_LOCK(q);
190 			psignal(q, SIGKILL);
191 			PROC_UNLOCK(q);
192 			q = q->p_peers;
193 		}
194 		while (p->p_peers != NULL)
195 			msleep(p, &ppeers_lock, PWAIT, "exit1", 0);
196 		mtx_unlock(&ppeers_lock);
197 	}
198 
199 #ifdef PGINPROF
200 	vmsizmon();
201 #endif
202 	STOPEVENT(p, S_EXIT, rv);
203 	wakeup(&p->p_stype);	/* Wakeup anyone in procfs' PIOCWAIT */
204 
205 	/*
206 	 * Check if any loadable modules need anything done at process exit.
207 	 * e.g. SYSV IPC stuff
208 	 * XXX what if one of these generates an error?
209 	 */
210 	EVENTHANDLER_INVOKE(process_exit, p);
211 
212 	MALLOC(p->p_ru, struct rusage *, sizeof(struct rusage),
213 		M_ZOMBIE, M_WAITOK);
214 	/*
215 	 * If parent is waiting for us to exit or exec,
216 	 * P_PPWAIT is set; we will wakeup the parent below.
217 	 */
218 	PROC_LOCK(p);
219 	stopprofclock(p);
220 	p->p_flag &= ~(P_TRACED | P_PPWAIT);
221 	SIGEMPTYSET(p->p_siglist);
222 	SIGEMPTYSET(td->td_siglist);
223 	if (timevalisset(&p->p_realtimer.it_value))
224 		callout_stop(&p->p_itcallout);
225 	PROC_UNLOCK(p);
226 
227 	/*
228 	 * Reset any sigio structures pointing to us as a result of
229 	 * F_SETOWN with our pid.
230 	 */
231 	funsetownlst(&p->p_sigiolst);
232 
233 	/*
234 	 * Close open files and release open-file table.
235 	 * This may block!
236 	 */
237 	fdfree(td);
238 
239 	/*
240 	 * Remove ourself from our leader's peer list and wake our leader.
241 	 */
242 	mtx_lock(&ppeers_lock);
243 	if (p->p_leader->p_peers) {
244 		q = p->p_leader;
245 		while (q->p_peers != p)
246 			q = q->p_peers;
247 		q->p_peers = p->p_peers;
248 		wakeup(p->p_leader);
249 	}
250 	mtx_unlock(&ppeers_lock);
251 
252 	/* The next two chunks should probably be moved to vmspace_exit. */
253 	vm = p->p_vmspace;
254 	/*
255 	 * Release user portion of address space.
256 	 * This releases references to vnodes,
257 	 * which could cause I/O if the file has been unlinked.
258 	 * Need to do this early enough that we can still sleep.
259 	 * Can't free the entire vmspace as the kernel stack
260 	 * may be mapped within that space also.
261 	 *
262 	 * Processes sharing the same vmspace may exit in one order, and
263 	 * get cleaned up by vmspace_exit() in a different order.  The
264 	 * last exiting process to reach this point releases as much of
265 	 * the environment as it can, and the last process cleaned up
266 	 * by vmspace_exit() (which decrements exitingcnt) cleans up the
267 	 * remainder.
268 	 */
269 	++vm->vm_exitingcnt;
270 	if (--vm->vm_refcnt == 0) {
271 		shmexit(vm);
272 		vm_page_lock_queues();
273 		pmap_remove_pages(vmspace_pmap(vm), vm_map_min(&vm->vm_map),
274 		    vm_map_max(&vm->vm_map));
275 		vm_page_unlock_queues();
276 		(void) vm_map_remove(&vm->vm_map, vm_map_min(&vm->vm_map),
277 		    vm_map_max(&vm->vm_map));
278 	}
279 
280 	sx_xlock(&proctree_lock);
281 	if (SESS_LEADER(p)) {
282 		struct session *sp;
283 
284 		sp = p->p_session;
285 		if (sp->s_ttyvp) {
286 			/*
287 			 * Controlling process.
288 			 * Signal foreground pgrp,
289 			 * drain controlling terminal
290 			 * and revoke access to controlling terminal.
291 			 */
292 			if (sp->s_ttyp && (sp->s_ttyp->t_session == sp)) {
293 				tp = sp->s_ttyp;
294 				if (sp->s_ttyp->t_pgrp) {
295 					PGRP_LOCK(sp->s_ttyp->t_pgrp);
296 					pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1);
297 					PGRP_UNLOCK(sp->s_ttyp->t_pgrp);
298 				}
299 				/* XXX tp should be locked. */
300 				sx_xunlock(&proctree_lock);
301 				(void) ttywait(tp);
302 				sx_xlock(&proctree_lock);
303 				/*
304 				 * The tty could have been revoked
305 				 * if we blocked.
306 				 */
307 				if (sp->s_ttyvp) {
308 					ttyvp = sp->s_ttyvp;
309 					SESS_LOCK(p->p_session);
310 					sp->s_ttyvp = NULL;
311 					SESS_UNLOCK(p->p_session);
312 					sx_xunlock(&proctree_lock);
313 					VOP_REVOKE(ttyvp, REVOKEALL);
314 					vrele(ttyvp);
315 					sx_xlock(&proctree_lock);
316 				}
317 			}
318 			if (sp->s_ttyvp) {
319 				ttyvp = sp->s_ttyvp;
320 				SESS_LOCK(p->p_session);
321 				sp->s_ttyvp = NULL;
322 				SESS_UNLOCK(p->p_session);
323 				vrele(ttyvp);
324 			}
325 			/*
326 			 * s_ttyp is not zero'd; we use this to indicate
327 			 * that the session once had a controlling terminal.
328 			 * (for logging and informational purposes)
329 			 */
330 		}
331 		SESS_LOCK(p->p_session);
332 		sp->s_leader = NULL;
333 		SESS_UNLOCK(p->p_session);
334 	}
335 	fixjobc(p, p->p_pgrp, 0);
336 	sx_xunlock(&proctree_lock);
337 	(void)acct_process(td);
338 #ifdef KTRACE
339 	/*
340 	 * release trace file
341 	 */
342 	PROC_LOCK(p);
343 	mtx_lock(&ktrace_mtx);
344 	p->p_traceflag = 0;	/* don't trace the vrele() */
345 	tracevp = p->p_tracevp;
346 	p->p_tracevp = NULL;
347 	tracecred = p->p_tracecred;
348 	p->p_tracecred = NULL;
349 	mtx_unlock(&ktrace_mtx);
350 	PROC_UNLOCK(p);
351 	if (tracevp != NULL)
352 		vrele(tracevp);
353 	if (tracecred != NULL)
354 		crfree(tracecred);
355 #endif
356 	/*
357 	 * Release reference to text vnode
358 	 */
359 	if ((vtmp = p->p_textvp) != NULL) {
360 		p->p_textvp = NULL;
361 		vrele(vtmp);
362 	}
363 
364 	/*
365 	 * Release our limits structure.
366 	 */
367 	mtx_assert(&Giant, MA_OWNED);
368 	if (--p->p_limit->p_refcnt == 0) {
369 		FREE(p->p_limit, M_SUBPROC);
370 		p->p_limit = NULL;
371 	}
372 
373 	/*
374 	 * Release this thread's reference to the ucred.  The actual proc
375 	 * reference will stay around until the proc is harvested by
376 	 * wait().  At this point the ucred is immutable (no other threads
377 	 * from this proc are around that can change it) so we leave the
378 	 * per-thread ucred pointer intact in case it is needed although
379 	 * in theory nothing should be using it at this point.
380 	 */
381 	crfree(td->td_ucred);
382 
383 	/*
384 	 * Remove proc from allproc queue and pidhash chain.
385 	 * Place onto zombproc.  Unlink from parent's child list.
386 	 */
387 	sx_xlock(&allproc_lock);
388 	LIST_REMOVE(p, p_list);
389 	LIST_INSERT_HEAD(&zombproc, p, p_list);
390 	LIST_REMOVE(p, p_hash);
391 	sx_xunlock(&allproc_lock);
392 
393 	sx_xlock(&proctree_lock);
394 	q = LIST_FIRST(&p->p_children);
395 	if (q != NULL)		/* only need this if any child is S_ZOMB */
396 		wakeup(initproc);
397 	for (; q != NULL; q = nq) {
398 		nq = LIST_NEXT(q, p_sibling);
399 		PROC_LOCK(q);
400 		proc_reparent(q, initproc);
401 		q->p_sigparent = SIGCHLD;
402 		/*
403 		 * Traced processes are killed
404 		 * since their existence means someone is screwing up.
405 		 */
406 		if (q->p_flag & P_TRACED) {
407 			q->p_flag &= ~P_TRACED;
408 			psignal(q, SIGKILL);
409 		}
410 		PROC_UNLOCK(q);
411 	}
412 
413 	/*
414 	 * Save exit status and final rusage info, adding in child rusage
415 	 * info and self times.
416 	 */
417 	PROC_LOCK(p);
418 	p->p_xstat = rv;
419 	*p->p_ru = p->p_stats->p_ru;
420 	mtx_lock_spin(&sched_lock);
421 	calcru(p, &p->p_ru->ru_utime, &p->p_ru->ru_stime, NULL);
422 	mtx_unlock_spin(&sched_lock);
423 	ruadd(p->p_ru, &p->p_stats->p_cru);
424 
425 	/*
426 	 * Notify interested parties of our demise.
427 	 */
428 	KNOTE(&p->p_klist, NOTE_EXIT);
429 
430 	/*
431 	 * Notify parent that we're gone.  If parent has the PS_NOCLDWAIT
432 	 * flag set, or if the handler is set to SIG_IGN, notify process
433 	 * 1 instead (and hope it will handle this situation).
434 	 */
435 	PROC_LOCK(p->p_pptr);
436 	if (p->p_pptr->p_procsig->ps_flag & (PS_NOCLDWAIT | PS_CLDSIGIGN)) {
437 		struct proc *pp;
438 
439 		pp = p->p_pptr;
440 		PROC_UNLOCK(pp);
441 		proc_reparent(p, initproc);
442 		PROC_LOCK(p->p_pptr);
443 		/*
444 		 * If this was the last child of our parent, notify
445 		 * parent, so in case he was wait(2)ing, he will
446 		 * continue.
447 		 */
448 		if (LIST_EMPTY(&pp->p_children))
449 			wakeup(pp);
450 	}
451 
452 	if (p->p_sigparent && p->p_pptr != initproc)
453 		psignal(p->p_pptr, p->p_sigparent);
454 	else
455 		psignal(p->p_pptr, SIGCHLD);
456 	PROC_UNLOCK(p->p_pptr);
457 
458 	/*
459 	 * If this is a kthread, then wakeup anyone waiting for it to exit.
460 	 */
461 	if (p->p_flag & P_KTHREAD)
462 		wakeup(p);
463 	PROC_UNLOCK(p);
464 
465 	/*
466 	 * Finally, call machine-dependent code to release the remaining
467 	 * resources including address space.
468 	 * The address space is released by "vmspace_exitfree(p)" in
469 	 * vm_waitproc().
470 	 */
471 	cpu_exit(td);
472 
473 	PROC_LOCK(p);
474 	PROC_LOCK(p->p_pptr);
475 	sx_xunlock(&proctree_lock);
476 	mtx_lock_spin(&sched_lock);
477 
478 	while (mtx_owned(&Giant))
479 		mtx_unlock(&Giant);
480 
481 	/*
482 	 * We have to wait until after releasing all locks before
483 	 * changing p_state.  If we block on a mutex then we will be
484 	 * back at SRUN when we resume and our parent will never
485 	 * harvest us.
486 	 */
487 	p->p_state = PRS_ZOMBIE;
488 
489 	wakeup(p->p_pptr);
490 	PROC_UNLOCK(p->p_pptr);
491 	cnt.v_swtch++;
492 	binuptime(PCPU_PTR(switchtime));
493 	PCPU_SET(switchticks, ticks);
494 
495 	cpu_sched_exit(td); /* XXXKSE check if this should be in thread_exit */
496 	/*
497 	 * Make sure the scheduler takes this thread out of its tables etc.
498 	 * This will also release this thread's reference to the ucred.
499 	 * Other thread parts to release include pcb bits and such.
500 	 */
501 	thread_exit();
502 }
503 
504 #ifdef COMPAT_43
505 /*
506  * MPSAFE.  The dirty work is handled by wait1().
507  */
508 int
509 owait(struct thread *td, struct owait_args *uap __unused)
510 {
511 	struct wait_args w;
512 
513 	w.options = 0;
514 	w.rusage = NULL;
515 	w.pid = WAIT_ANY;
516 	w.status = NULL;
517 	return (wait1(td, &w, 1));
518 }
519 #endif /* COMPAT_43 */
520 
521 /*
522  * MPSAFE.  The dirty work is handled by wait1().
523  */
524 int
525 wait4(struct thread *td, struct wait_args *uap)
526 {
527 
528 	return (wait1(td, uap, 0));
529 }
530 
531 /*
532  * MPSAFE
533  */
534 static int
535 wait1(struct thread *td, struct wait_args *uap, int compat)
536 {
537 	struct rusage ru;
538 	int nfound;
539 	struct proc *p, *q, *t;
540 	int status, error;
541 
542 	q = td->td_proc;
543 	if (uap->pid == 0) {
544 		PROC_LOCK(q);
545 		uap->pid = -q->p_pgid;
546 		PROC_UNLOCK(q);
547 	}
548 	if (uap->options &~ (WUNTRACED|WNOHANG|WCONTINUED|WLINUXCLONE))
549 		return (EINVAL);
550 	mtx_lock(&Giant);
551 loop:
552 	nfound = 0;
553 	sx_xlock(&proctree_lock);
554 	LIST_FOREACH(p, &q->p_children, p_sibling) {
555 		PROC_LOCK(p);
556 		if (uap->pid != WAIT_ANY &&
557 		    p->p_pid != uap->pid && p->p_pgid != -uap->pid) {
558 			PROC_UNLOCK(p);
559 			continue;
560 		}
561 
562 		/*
563 		 * This special case handles a kthread spawned by linux_clone
564 		 * (see linux_misc.c).  The linux_wait4 and linux_waitpid
565 		 * functions need to be able to distinguish between waiting
566 		 * on a process and waiting on a thread.  It is a thread if
567 		 * p_sigparent is not SIGCHLD, and the WLINUXCLONE option
568 		 * signifies we want to wait for threads and not processes.
569 		 */
570 		if ((p->p_sigparent != SIGCHLD) ^
571 		    ((uap->options & WLINUXCLONE) != 0)) {
572 			PROC_UNLOCK(p);
573 			continue;
574 		}
575 
576 		nfound++;
577 		if (p->p_state == PRS_ZOMBIE) {
578 			/*
579 			 * Allow the scheduler to adjust the priority of the
580 			 * parent when a kseg is exiting.
581 			 */
582 			if (curthread->td_proc->p_pid != 1) {
583 				mtx_lock_spin(&sched_lock);
584 				sched_exit(curthread->td_ksegrp,
585 				    FIRST_KSEGRP_IN_PROC(p));
586 				mtx_unlock_spin(&sched_lock);
587 			}
588 
589 			td->td_retval[0] = p->p_pid;
590 #ifdef COMPAT_43
591 			if (compat)
592 				td->td_retval[1] = p->p_xstat;
593 			else
594 #endif
595 			if (uap->status) {
596 				status = p->p_xstat;	/* convert to int */
597 				PROC_UNLOCK(p);
598 				if ((error = copyout(&status,
599 				    uap->status, sizeof(status)))) {
600 					sx_xunlock(&proctree_lock);
601 					mtx_unlock(&Giant);
602 					return (error);
603 				}
604 				PROC_LOCK(p);
605 			}
606 			if (uap->rusage) {
607 				bcopy(p->p_ru, &ru, sizeof(ru));
608 				PROC_UNLOCK(p);
609 				if ((error = copyout(&ru,
610 				    uap->rusage, sizeof (struct rusage)))) {
611 					sx_xunlock(&proctree_lock);
612 					mtx_unlock(&Giant);
613 					return (error);
614 				}
615 			} else
616 				PROC_UNLOCK(p);
617 			/*
618 			 * If we got the child via a ptrace 'attach',
619 			 * we need to give it back to the old parent.
620 			 */
621 			if (p->p_oppid && (t = pfind(p->p_oppid)) != NULL) {
622 				PROC_LOCK(p);
623 				p->p_oppid = 0;
624 				proc_reparent(p, t);
625 				PROC_UNLOCK(p);
626 				psignal(t, SIGCHLD);
627 				wakeup(t);
628 				PROC_UNLOCK(t);
629 				sx_xunlock(&proctree_lock);
630 				mtx_unlock(&Giant);
631 				return (0);
632 			}
633 
634 			/*
635 			 * Remove other references to this process to ensure
636 			 * we have an exclusive reference.
637 			 */
638 			sx_xlock(&allproc_lock);
639 			LIST_REMOVE(p, p_list);	/* off zombproc */
640 			sx_xunlock(&allproc_lock);
641 			LIST_REMOVE(p, p_sibling);
642 			leavepgrp(p);
643 			sx_xunlock(&proctree_lock);
644 
645 			/*
646 			 * As a side effect of this lock, we know that
647 			 * all other writes to this proc are visible now, so
648 			 * no more locking is needed for p.
649 			 */
650 			PROC_LOCK(p);
651 			p->p_xstat = 0;		/* XXX: why? */
652 			PROC_UNLOCK(p);
653 			PROC_LOCK(q);
654 			ruadd(&q->p_stats->p_cru, p->p_ru);
655 			PROC_UNLOCK(q);
656 			FREE(p->p_ru, M_ZOMBIE);
657 			p->p_ru = NULL;
658 
659 			/*
660 			 * Decrement the count of procs running with this uid.
661 			 */
662 			(void)chgproccnt(p->p_ucred->cr_ruidinfo, -1, 0);
663 
664 			/*
665 			 * Free up credentials.
666 			 */
667 			crfree(p->p_ucred);
668 			p->p_ucred = NULL;	/* XXX: why? */
669 
670 			/*
671 			 * Remove unused arguments
672 			 */
673 			pargs_drop(p->p_args);
674 			p->p_args = NULL;
675 
676 			if (--p->p_procsig->ps_refcnt == 0) {
677 				if (p->p_sigacts != &p->p_uarea->u_sigacts)
678 					FREE(p->p_sigacts, M_SUBPROC);
679 				FREE(p->p_procsig, M_SUBPROC);
680 				p->p_procsig = NULL;
681 			}
682 
683 			/*
684 			 * do any thread-system specific cleanups
685 			 */
686 			thread_wait(p);
687 
688 			/*
689 			 * Give vm and machine-dependent layer a chance
690 			 * to free anything that cpu_exit couldn't
691 			 * release while still running in process context.
692 			 */
693 			vm_waitproc(p);
694 			mtx_destroy(&p->p_mtx);
695 #ifdef MAC
696 			mac_destroy_proc(p);
697 #endif
698 			KASSERT(FIRST_THREAD_IN_PROC(p),
699 			    ("wait1: no residual thread!"));
700 			uma_zfree(proc_zone, p);
701 			sx_xlock(&allproc_lock);
702 			nprocs--;
703 			sx_xunlock(&allproc_lock);
704 			mtx_unlock(&Giant);
705 			return (0);
706 		}
707 		if (P_SHOULDSTOP(p) && (p->p_suspcount == p->p_numthreads) &&
708 		    ((p->p_flag & P_WAITED) == 0) &&
709 		    (p->p_flag & P_TRACED || uap->options & WUNTRACED)) {
710 			p->p_flag |= P_WAITED;
711 			sx_xunlock(&proctree_lock);
712 			td->td_retval[0] = p->p_pid;
713 #ifdef COMPAT_43
714 			if (compat) {
715 				td->td_retval[1] = W_STOPCODE(p->p_xstat);
716 				PROC_UNLOCK(p);
717 				error = 0;
718 			} else
719 #endif
720 			if (uap->status) {
721 				status = W_STOPCODE(p->p_xstat);
722 				PROC_UNLOCK(p);
723 				error = copyout(&status,
724 					uap->status, sizeof(status));
725 			} else {
726 				PROC_UNLOCK(p);
727 				error = 0;
728 			}
729 			mtx_unlock(&Giant);
730 			return (error);
731 		}
732 		if (uap->options & WCONTINUED && (p->p_flag & P_CONTINUED)) {
733 			sx_xunlock(&proctree_lock);
734 			td->td_retval[0] = p->p_pid;
735 			p->p_flag &= ~P_CONTINUED;
736 			PROC_UNLOCK(p);
737 
738 			if (uap->status) {
739 				status = SIGCONT;
740 				error = copyout(&status,
741 				    uap->status, sizeof(status));
742 			} else
743 				error = 0;
744 
745 			mtx_unlock(&Giant);
746 			return (error);
747 		}
748 		PROC_UNLOCK(p);
749 	}
750 	if (nfound == 0) {
751 		sx_xunlock(&proctree_lock);
752 		mtx_unlock(&Giant);
753 		return (ECHILD);
754 	}
755 	if (uap->options & WNOHANG) {
756 		sx_xunlock(&proctree_lock);
757 		td->td_retval[0] = 0;
758 		mtx_unlock(&Giant);
759 		return (0);
760 	}
761 	PROC_LOCK(q);
762 	sx_xunlock(&proctree_lock);
763 	error = msleep(q, &q->p_mtx, PWAIT | PCATCH, "wait", 0);
764 	PROC_UNLOCK(q);
765 	if (error) {
766 		mtx_unlock(&Giant);
767 		return (error);
768 	}
769 	goto loop;
770 }
771 
772 /*
773  * Make process 'parent' the new parent of process 'child'.
774  * Must be called with an exclusive hold of proctree lock.
775  */
776 void
777 proc_reparent(struct proc *child, struct proc *parent)
778 {
779 
780 	sx_assert(&proctree_lock, SX_XLOCKED);
781 	PROC_LOCK_ASSERT(child, MA_OWNED);
782 	if (child->p_pptr == parent)
783 		return;
784 
785 	LIST_REMOVE(child, p_sibling);
786 	LIST_INSERT_HEAD(&parent->p_children, child, p_sibling);
787 	child->p_pptr = parent;
788 }
789