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