xref: /freebsd/sys/kern/kern_exit.c (revision adeb92a24c57f97d5cd3c3c45be239cbb23aed68)
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 
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/sysproto.h>
48 #include <sys/kernel.h>
49 #include <sys/malloc.h>
50 #include <sys/lock.h>
51 #include <sys/mutex.h>
52 #include <sys/proc.h>
53 #include <sys/pioctl.h>
54 #include <sys/tty.h>
55 #include <sys/wait.h>
56 #include <sys/vnode.h>
57 #include <sys/vmmeter.h>
58 #include <sys/resourcevar.h>
59 #include <sys/signalvar.h>
60 #include <sys/sx.h>
61 #include <sys/ptrace.h>
62 #include <sys/acct.h>		/* for acct_process() function prototype */
63 #include <sys/filedesc.h>
64 #include <sys/shm.h>
65 #include <sys/sem.h>
66 #include <sys/jail.h>
67 
68 #include <vm/vm.h>
69 #include <vm/vm_param.h>
70 #include <vm/vm_extern.h>
71 #include <vm/pmap.h>
72 #include <vm/vm_map.h>
73 #include <vm/vm_zone.h>
74 #include <sys/user.h>
75 
76 /* Required to be non-static for SysVR4 emulator */
77 MALLOC_DEFINE(M_ZOMBIE, "zombie", "zombie proc status");
78 
79 static MALLOC_DEFINE(M_ATEXIT, "atexit", "atexit callback");
80 
81 static int wait1 __P((struct thread *, struct wait_args *, int));
82 
83 /*
84  * callout list for things to do at exit time
85  */
86 struct exitlist {
87 	exitlist_fn function;
88 	TAILQ_ENTRY(exitlist) next;
89 };
90 
91 TAILQ_HEAD(exit_list_head, exitlist);
92 static struct exit_list_head exit_list = TAILQ_HEAD_INITIALIZER(exit_list);
93 
94 /*
95  * exit --
96  *	Death of process.
97  *
98  * MPSAFE
99  */
100 void
101 sys_exit(td, uap)
102 	struct thread *td;
103 	struct sys_exit_args /* {
104 		int	rval;
105 	} */ *uap;
106 {
107 
108 	mtx_lock(&Giant);
109 	exit1(td, W_EXITCODE(uap->rval, 0));
110 	/* NOTREACHED */
111 }
112 
113 /*
114  * Exit: deallocate address space and other resources, change proc state
115  * to zombie, and unlink proc from allproc and parent's lists.  Save exit
116  * status and rusage for wait().  Check for child processes and orphan them.
117  */
118 void
119 exit1(td, rv)
120 	register struct thread *td;
121 	int rv;
122 {
123 	struct proc *p = td->td_proc;
124 	register struct proc *q, *nq;
125 	register struct vmspace *vm;
126 #ifdef KTRACE
127 	struct vnode *vtmp;
128 #endif
129 	struct exitlist *ep;
130 
131 	GIANT_REQUIRED;
132 
133 	if (p->p_pid == 1) {
134 		printf("init died (signal %d, exit %d)\n",
135 		    WTERMSIG(rv), WEXITSTATUS(rv));
136 		panic("Going nowhere without my init!");
137 	}
138 
139 /* XXXXKSE */
140 /* MUST abort all other threads before proceeding past this point */
141 
142 	/* are we a task leader? */
143 	PROC_LOCK(p);
144 	if(p == p->p_leader) {
145 		q = p->p_peers;
146 		while (q != NULL) {
147 			PROC_LOCK(q);
148 			psignal(q, SIGKILL);
149 			PROC_UNLOCK(q);
150 			q = q->p_peers;
151 		}
152 		while (p->p_peers)
153 			msleep((caddr_t)p, &p->p_mtx, PWAIT, "exit1", 0);
154 	}
155 	PROC_UNLOCK(p);
156 
157 #ifdef PGINPROF
158 	vmsizmon();
159 #endif
160 	STOPEVENT(p, S_EXIT, rv);
161 	wakeup(&p->p_stype);	/* Wakeup anyone in procfs' PIOCWAIT */
162 
163 	/*
164 	 * Check if any loadable modules need anything done at process exit.
165 	 * e.g. SYSV IPC stuff
166 	 * XXX what if one of these generates an error?
167 	 */
168 	TAILQ_FOREACH(ep, &exit_list, next)
169 		(*ep->function)(p);
170 
171 	stopprofclock(p);
172 
173 	MALLOC(p->p_ru, struct rusage *, sizeof(struct rusage),
174 		M_ZOMBIE, M_WAITOK);
175 	/*
176 	 * If parent is waiting for us to exit or exec,
177 	 * P_PPWAIT is set; we will wakeup the parent below.
178 	 */
179 	PROC_LOCK(p);
180 	p->p_flag &= ~(P_TRACED | P_PPWAIT);
181 	p->p_flag |= P_WEXIT;
182 	SIGEMPTYSET(p->p_siglist);
183 	PROC_UNLOCK(p);
184 	if (timevalisset(&p->p_realtimer.it_value))
185 		callout_stop(&p->p_itcallout);
186 
187 	/*
188 	 * Reset any sigio structures pointing to us as a result of
189 	 * F_SETOWN with our pid.
190 	 */
191 	funsetownlst(&p->p_sigiolst);
192 
193 	/*
194 	 * Close open files and release open-file table.
195 	 * This may block!
196 	 */
197 	fdfree(td); /* XXXKSE *//* may not be the one in proc */
198 
199 	/*
200 	 * Remove ourself from our leader's peer list and wake our leader.
201 	 */
202 	PROC_LOCK(p->p_leader);
203 	if(p->p_leader->p_peers) {
204 		q = p->p_leader;
205 		while(q->p_peers != p)
206 			q = q->p_peers;
207 		q->p_peers = p->p_peers;
208 		wakeup((caddr_t)p->p_leader);
209 	}
210 	PROC_UNLOCK(p->p_leader);
211 
212 	/* The next two chunks should probably be moved to vmspace_exit. */
213 	vm = p->p_vmspace;
214 	/*
215 	 * Release user portion of address space.
216 	 * This releases references to vnodes,
217 	 * which could cause I/O if the file has been unlinked.
218 	 * Need to do this early enough that we can still sleep.
219 	 * Can't free the entire vmspace as the kernel stack
220 	 * may be mapped within that space also.
221 	 */
222 	if (vm->vm_refcnt == 1) {
223 		if (vm->vm_shm)
224 			shmexit(p);
225 		pmap_remove_pages(vmspace_pmap(vm), VM_MIN_ADDRESS,
226 		    VM_MAXUSER_ADDRESS);
227 		(void) vm_map_remove(&vm->vm_map, VM_MIN_ADDRESS,
228 		    VM_MAXUSER_ADDRESS);
229 	}
230 
231 	PROC_LOCK(p);
232 	if (SESS_LEADER(p)) {
233 		register struct session *sp = p->p_session;
234 
235 		PROC_UNLOCK(p);
236 		if (sp->s_ttyvp) {
237 			/*
238 			 * Controlling process.
239 			 * Signal foreground pgrp,
240 			 * drain controlling terminal
241 			 * and revoke access to controlling terminal.
242 			 */
243 			if (sp->s_ttyp && (sp->s_ttyp->t_session == sp)) {
244 				if (sp->s_ttyp->t_pgrp)
245 					pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1);
246 				(void) ttywait(sp->s_ttyp);
247 				/*
248 				 * The tty could have been revoked
249 				 * if we blocked.
250 				 */
251 				if (sp->s_ttyvp)
252 					VOP_REVOKE(sp->s_ttyvp, REVOKEALL);
253 			}
254 			if (sp->s_ttyvp)
255 				vrele(sp->s_ttyvp);
256 			sp->s_ttyvp = NULL;
257 			/*
258 			 * s_ttyp is not zero'd; we use this to indicate
259 			 * that the session once had a controlling terminal.
260 			 * (for logging and informational purposes)
261 			 */
262 		}
263 		sp->s_leader = NULL;
264 	} else
265 		PROC_UNLOCK(p);
266 	fixjobc(p, p->p_pgrp, 0);
267 	(void)acct_process(td);
268 #ifdef KTRACE
269 	/*
270 	 * release trace file
271 	 */
272 	p->p_traceflag = 0;	/* don't trace the vrele() */
273 	if ((vtmp = p->p_tracep) != NULL) {
274 		p->p_tracep = NULL;
275 		vrele(vtmp);
276 	}
277 #endif
278 	/*
279 	 * Remove proc from allproc queue and pidhash chain.
280 	 * Place onto zombproc.  Unlink from parent's child list.
281 	 */
282 	sx_xlock(&allproc_lock);
283 	LIST_REMOVE(p, p_list);
284 	LIST_INSERT_HEAD(&zombproc, p, p_list);
285 	LIST_REMOVE(p, p_hash);
286 	sx_xunlock(&allproc_lock);
287 
288 	sx_xlock(&proctree_lock);
289 	q = LIST_FIRST(&p->p_children);
290 	if (q != NULL)		/* only need this if any child is S_ZOMB */
291 		wakeup((caddr_t) initproc);
292 	for (; q != NULL; q = nq) {
293 		nq = LIST_NEXT(q, p_sibling);
294 		PROC_LOCK(q);
295 		proc_reparent(q, initproc);
296 		q->p_sigparent = SIGCHLD;
297 		/*
298 		 * Traced processes are killed
299 		 * since their existence means someone is screwing up.
300 		 */
301 		if (q->p_flag & P_TRACED) {
302 			q->p_flag &= ~P_TRACED;
303 			psignal(q, SIGKILL);
304 		}
305 		PROC_UNLOCK(q);
306 	}
307 
308 	/*
309 	 * Save exit status and final rusage info, adding in child rusage
310 	 * info and self times.
311 	 */
312 	p->p_xstat = rv;
313 	*p->p_ru = p->p_stats->p_ru;
314 	mtx_lock_spin(&sched_lock);
315 	calcru(p, &p->p_ru->ru_utime, &p->p_ru->ru_stime, NULL);
316 	mtx_unlock_spin(&sched_lock);
317 	ruadd(p->p_ru, &p->p_stats->p_cru);
318 
319 	/*
320 	 * Pretend that an mi_switch() to the next process occurs now.  We
321 	 * must set `switchtime' directly since we will call cpu_switch()
322 	 * directly.  Set it now so that the rest of the exit time gets
323 	 * counted somewhere if possible.
324 	 */
325 	mtx_lock_spin(&sched_lock);
326 	microuptime(PCPU_PTR(switchtime));
327 	PCPU_SET(switchticks, ticks);
328 	mtx_unlock_spin(&sched_lock);
329 
330 	/*
331 	 * notify interested parties of our demise.
332 	 */
333 	PROC_LOCK(p);
334 	KNOTE(&p->p_klist, NOTE_EXIT);
335 
336 	/*
337 	 * Notify parent that we're gone.  If parent has the PS_NOCLDWAIT
338 	 * flag set, notify process 1 instead (and hope it will handle
339 	 * this situation).
340 	 */
341 	if (p->p_pptr->p_procsig->ps_flag & PS_NOCLDWAIT) {
342 		struct proc *pp = p->p_pptr;
343 		proc_reparent(p, initproc);
344 		/*
345 		 * If this was the last child of our parent, notify
346 		 * parent, so in case he was wait(2)ing, he will
347 		 * continue.
348 		 */
349 		if (LIST_EMPTY(&pp->p_children))
350 			wakeup((caddr_t)pp);
351 	}
352 
353 	PROC_LOCK(p->p_pptr);
354 	if (p->p_sigparent && p->p_pptr != initproc)
355 	        psignal(p->p_pptr, p->p_sigparent);
356 	else
357 	        psignal(p->p_pptr, SIGCHLD);
358 	PROC_UNLOCK(p->p_pptr);
359 
360 	/*
361 	 * If this is a kthread, then wakeup anyone waiting for it to exit.
362 	 */
363 	if (p->p_flag & P_KTHREAD)
364 		wakeup((caddr_t)p);
365 	PROC_UNLOCK(p);
366 	sx_xunlock(&proctree_lock);
367 
368 	/*
369 	 * Clear curproc after we've done all operations
370 	 * that could block, and before tearing down the rest
371 	 * of the process state that might be used from clock, etc.
372 	 * Also, can't clear curproc while we're still runnable,
373 	 * as we're not on a run queue (we are current, just not
374 	 * a proper proc any longer!).
375 	 *
376 	 * Other substructures are freed from wait().
377 	 */
378 	mtx_assert(&Giant, MA_OWNED);
379 	if (--p->p_limit->p_refcnt == 0) {
380 		FREE(p->p_limit, M_SUBPROC);
381 		p->p_limit = NULL;
382 	}
383 
384 	/*
385 	 * Release this thread's reference to the ucred.  The actual proc
386 	 * reference will stay around until the proc is harvested by
387 	 * wait().  At this point the ucred is immutable (no other threads
388 	 * from this proc are around that can change it) so we leave the
389 	 * per-thread ucred pointer intact in case it is needed although
390 	 * in theory nothing should be using it at this point.
391 	 */
392 	crfree(td->td_ucred);
393 
394 	/*
395 	 * Finally, call machine-dependent code to release the remaining
396 	 * resources including address space, the kernel stack and pcb.
397 	 * The address space is released by "vmspace_free(p->p_vmspace)"
398 	 * in vm_waitproc();
399 	 */
400 	cpu_exit(td);
401 
402 	PROC_LOCK(p);
403 	mtx_lock_spin(&sched_lock);
404 	while (mtx_owned(&Giant))
405 		mtx_unlock_flags(&Giant, MTX_NOSWITCH);
406 
407 	/*
408 	 * We have to wait until after releasing all locks before
409 	 * changing p_stat.  If we block on a mutex then we will be
410 	 * back at SRUN when we resume and our parent will never
411 	 * harvest us.
412 	 */
413 	p->p_stat = SZOMB;
414 
415 	wakeup(p->p_pptr);
416 	PROC_UNLOCK_NOSWITCH(p);
417 
418 	cnt.v_swtch++;
419 	cpu_throw();
420 	panic("exit1");
421 }
422 
423 #ifdef COMPAT_43
424 /*
425  * MPSAFE, the dirty work is handled by wait1().
426  */
427 int
428 owait(td, uap)
429 	struct thread *td;
430 	register struct owait_args /* {
431 		int     dummy;
432 	} */ *uap;
433 {
434 	struct wait_args w;
435 
436 	w.options = 0;
437 	w.rusage = NULL;
438 	w.pid = WAIT_ANY;
439 	w.status = NULL;
440 	return (wait1(td, &w, 1));
441 }
442 #endif /* COMPAT_43 */
443 
444 /*
445  * MPSAFE, the dirty work is handled by wait1().
446  */
447 int
448 wait4(td, uap)
449 	struct thread *td;
450 	struct wait_args *uap;
451 {
452 
453 	return (wait1(td, uap, 0));
454 }
455 
456 /*
457  * MPSAFE
458  */
459 static int
460 wait1(td, uap, compat)
461 	register struct thread *td;
462 	register struct wait_args /* {
463 		int pid;
464 		int *status;
465 		int options;
466 		struct rusage *rusage;
467 	} */ *uap;
468 	int compat;
469 {
470 	register int nfound;
471 	register struct proc *q, *p, *t;
472 	int status, error;
473 
474 	mtx_lock(&Giant);
475 	q = td->td_proc;
476 	if (uap->pid == 0)
477 		uap->pid = -q->p_pgid;
478 	if (uap->options &~ (WUNTRACED|WNOHANG|WLINUXCLONE)) {
479 		error = EINVAL;
480 		goto done2;
481 	}
482 loop:
483 	nfound = 0;
484 	sx_slock(&proctree_lock);
485 	LIST_FOREACH(p, &q->p_children, p_sibling) {
486 		if (uap->pid != WAIT_ANY &&
487 		    p->p_pid != uap->pid && p->p_pgid != -uap->pid)
488 			continue;
489 
490 		/*
491 		 * This special case handles a kthread spawned by linux_clone
492 		 * (see linux_misc.c).  The linux_wait4 and linux_waitpid
493 		 * functions need to be able to distinguish between waiting
494 		 * on a process and waiting on a thread.  It is a thread if
495 		 * p_sigparent is not SIGCHLD, and the WLINUXCLONE option
496 		 * signifies we want to wait for threads and not processes.
497 		 */
498 		PROC_LOCK(p);
499 		if ((p->p_sigparent != SIGCHLD) ^
500 		    ((uap->options & WLINUXCLONE) != 0)) {
501 			PROC_UNLOCK(p);
502 			continue;
503 		}
504 
505 		nfound++;
506 		mtx_lock_spin(&sched_lock);
507 		if (p->p_stat == SZOMB) {
508 			/*
509 			 * charge childs scheduling cpu usage to parent
510 			 * XXXKSE assume only one thread & kse & ksegrp
511 			 * keep estcpu in each ksegrp
512 			 * so charge it to the ksegrp that did the wait
513 			 * since process estcpu is sum of all ksegrps,
514 			 * this is strictly as expected.
515 			 * Assume that the child process aggregated all
516 			 * tke estcpu into the 'build-in' ksegrp.
517 			 * XXXKSE
518 			 */
519 			if (curthread->td_proc->p_pid != 1) {
520 				curthread->td_ksegrp->kg_estcpu =
521 				    ESTCPULIM(curthread->td_ksegrp->kg_estcpu +
522 				    p->p_ksegrp.kg_estcpu);
523 			}
524 
525 			mtx_unlock_spin(&sched_lock);
526 			PROC_UNLOCK(p);
527 			sx_sunlock(&proctree_lock);
528 
529 			td->td_retval[0] = p->p_pid;
530 #ifdef COMPAT_43
531 			if (compat)
532 				td->td_retval[1] = p->p_xstat;
533 			else
534 #endif
535 			if (uap->status) {
536 				status = p->p_xstat;	/* convert to int */
537 				if ((error = copyout((caddr_t)&status,
538 				    (caddr_t)uap->status, sizeof(status)))) {
539 					goto done2;
540 				}
541 			}
542 			if (uap->rusage && (error = copyout((caddr_t)p->p_ru,
543 			    (caddr_t)uap->rusage, sizeof (struct rusage)))) {
544 				goto done2;
545 			}
546 			/*
547 			 * If we got the child via a ptrace 'attach',
548 			 * we need to give it back to the old parent.
549 			 */
550 			sx_xlock(&proctree_lock);
551 			if (p->p_oppid) {
552 				if ((t = pfind(p->p_oppid)) != NULL) {
553 					PROC_LOCK(p);
554 					p->p_oppid = 0;
555 					proc_reparent(p, t);
556 					PROC_UNLOCK(p);
557 					psignal(t, SIGCHLD);
558 					wakeup((caddr_t)t);
559 					PROC_UNLOCK(t);
560 					sx_xunlock(&proctree_lock);
561 					error = 0;
562 					goto done2;
563 				}
564 			}
565 			sx_xunlock(&proctree_lock);
566 			PROC_LOCK(p);
567 			p->p_xstat = 0;
568 			PROC_UNLOCK(p);
569 			ruadd(&q->p_stats->p_cru, p->p_ru);
570 			FREE(p->p_ru, M_ZOMBIE);
571 			p->p_ru = NULL;
572 
573 			/*
574 			 * Decrement the count of procs running with this uid.
575 			 */
576 			(void)chgproccnt(p->p_ucred->cr_ruidinfo, -1, 0);
577 
578 			/*
579 			 * Release reference to text vnode
580 			 */
581 			if (p->p_textvp)
582 				vrele(p->p_textvp);
583 
584 			/*
585 			 * Finally finished with old proc entry.
586 			 * Unlink it from its process group and free it.
587 			 */
588 			leavepgrp(p);
589 
590 			sx_xlock(&allproc_lock);
591 			LIST_REMOVE(p, p_list);	/* off zombproc */
592 			sx_xunlock(&allproc_lock);
593 
594 			sx_xlock(&proctree_lock);
595 			LIST_REMOVE(p, p_sibling);
596 			sx_xunlock(&proctree_lock);
597 
598 			/*
599 			 * Free up credentials.
600 			 */
601 			crfree(p->p_ucred);
602 			p->p_ucred = NULL;
603 
604 			/*
605 			 * Remove unused arguments
606 			 */
607 			if (p->p_args && --p->p_args->ar_ref == 0)
608 				FREE(p->p_args, M_PARGS);
609 
610 			if (--p->p_procsig->ps_refcnt == 0) {
611 				if (p->p_sigacts != &p->p_uarea->u_sigacts)
612 					FREE(p->p_sigacts, M_SUBPROC);
613 			        FREE(p->p_procsig, M_SUBPROC);
614 				p->p_procsig = NULL;
615 			}
616 
617 			/*
618 			 * Give vm and machine-dependent layer a chance
619 			 * to free anything that cpu_exit couldn't
620 			 * release while still running in process context.
621 			 */
622 			vm_waitproc(p);
623 			mtx_destroy(&p->p_mtx);
624 			zfree(proc_zone, p);
625 			nprocs--;
626 			error = 0;
627 			goto done2;
628 		}
629 		if (p->p_stat == SSTOP && (p->p_flag & P_WAITED) == 0 &&
630 		    (p->p_flag & P_TRACED || uap->options & WUNTRACED)) {
631 			mtx_unlock_spin(&sched_lock);
632 			p->p_flag |= P_WAITED;
633 			PROC_UNLOCK(p);
634 			sx_sunlock(&proctree_lock);
635 			td->td_retval[0] = p->p_pid;
636 #ifdef COMPAT_43
637 			if (compat) {
638 				td->td_retval[1] = W_STOPCODE(p->p_xstat);
639 				error = 0;
640 			} else
641 #endif
642 			if (uap->status) {
643 				status = W_STOPCODE(p->p_xstat);
644 				error = copyout((caddr_t)&status,
645 					(caddr_t)uap->status, sizeof(status));
646 			} else
647 				error = 0;
648 			goto done2;
649 		}
650 		mtx_unlock_spin(&sched_lock);
651 		PROC_UNLOCK(p);
652 	}
653 	sx_sunlock(&proctree_lock);
654 	if (nfound == 0) {
655 		error = ECHILD;
656 		goto done2;
657 	}
658 	if (uap->options & WNOHANG) {
659 		td->td_retval[0] = 0;
660 		error = 0;
661 		goto done2;
662 	}
663 	if ((error = tsleep((caddr_t)q, PWAIT | PCATCH, "wait", 0)) != 0)
664 		goto done2;
665 	goto loop;
666 done2:
667 	mtx_unlock(&Giant);
668 	return(error);
669 }
670 
671 /*
672  * Make process 'parent' the new parent of process 'child'.
673  * Must be called with an exclusive hold of proctree lock.
674  */
675 void
676 proc_reparent(child, parent)
677 	register struct proc *child;
678 	register struct proc *parent;
679 {
680 
681 	sx_assert(&proctree_lock, SX_XLOCKED);
682 	PROC_LOCK_ASSERT(child, MA_OWNED);
683 	if (child->p_pptr == parent)
684 		return;
685 
686 	LIST_REMOVE(child, p_sibling);
687 	LIST_INSERT_HEAD(&parent->p_children, child, p_sibling);
688 	child->p_pptr = parent;
689 }
690 
691 /*
692  * The next two functions are to handle adding/deleting items on the
693  * exit callout list
694  *
695  * at_exit():
696  * Take the arguments given and put them onto the exit callout list,
697  * However first make sure that it's not already there.
698  * returns 0 on success.
699  */
700 
701 int
702 at_exit(function)
703 	exitlist_fn function;
704 {
705 	struct exitlist *ep;
706 
707 #ifdef INVARIANTS
708 	/* Be noisy if the programmer has lost track of things */
709 	if (rm_at_exit(function))
710 		printf("WARNING: exit callout entry (%p) already present\n",
711 		    function);
712 #endif
713 	ep = malloc(sizeof(*ep), M_ATEXIT, M_NOWAIT);
714 	if (ep == NULL)
715 		return (ENOMEM);
716 	ep->function = function;
717 	TAILQ_INSERT_TAIL(&exit_list, ep, next);
718 	return (0);
719 }
720 
721 /*
722  * Scan the exit callout list for the given item and remove it.
723  * Returns the number of items removed (0 or 1)
724  */
725 int
726 rm_at_exit(function)
727 	exitlist_fn function;
728 {
729 	struct exitlist *ep;
730 
731 	TAILQ_FOREACH(ep, &exit_list, next) {
732 		if (ep->function == function) {
733 			TAILQ_REMOVE(&exit_list, ep, next);
734 			free(ep, M_ATEXIT);
735 			return(1);
736 		}
737 	}
738 	return (0);
739 }
740