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