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