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