xref: /dragonfly/sys/kern/kern_exit.c (revision d600454b)
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: src/sys/kern/kern_exit.c,v 1.92.2.11 2003/01/13 22:51:16 dillon Exp $
40  * $DragonFly: src/sys/kern/kern_exit.c,v 1.52 2005/12/28 19:13:34 dillon Exp $
41  */
42 
43 #include "opt_compat.h"
44 #include "opt_ktrace.h"
45 
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/sysproto.h>
49 #include <sys/kernel.h>
50 #include <sys/malloc.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 #include <sys/kern_syscall.h>
66 #include <sys/upcall.h>
67 #include <sys/caps.h>
68 
69 #include <vm/vm.h>
70 #include <vm/vm_param.h>
71 #include <sys/lock.h>
72 #include <vm/pmap.h>
73 #include <vm/vm_map.h>
74 #include <vm/vm_zone.h>
75 #include <vm/vm_extern.h>
76 #include <sys/user.h>
77 
78 #include <sys/thread2.h>
79 
80 /* Required to be non-static for SysVR4 emulator */
81 MALLOC_DEFINE(M_ZOMBIE, "zombie", "zombie proc status");
82 
83 static MALLOC_DEFINE(M_ATEXIT, "atexit", "atexit callback");
84 
85 /*
86  * callout list for things to do at exit time
87  */
88 struct exitlist {
89 	exitlist_fn function;
90 	TAILQ_ENTRY(exitlist) next;
91 };
92 
93 TAILQ_HEAD(exit_list_head, exitlist);
94 static struct exit_list_head exit_list = TAILQ_HEAD_INITIALIZER(exit_list);
95 
96 /*
97  * exit --
98  *	Death of process.
99  *
100  * SYS_EXIT_ARGS(int rval)
101  */
102 void
103 sys_exit(struct sys_exit_args *uap)
104 {
105 	exit1(W_EXITCODE(uap->rval, 0));
106 	/* NOTREACHED */
107 }
108 
109 /*
110  * Exit: deallocate address space and other resources, change proc state
111  * to zombie, and unlink proc from allproc and parent's lists.  Save exit
112  * status and rusage for wait().  Check for child processes and orphan them.
113  */
114 void
115 exit1(int rv)
116 {
117 	struct proc *p = curproc;
118 	struct lwp *lp;
119 	struct proc *q, *nq;
120 	struct vmspace *vm;
121 	struct vnode *vtmp;
122 	struct exitlist *ep;
123 
124 	if (p->p_pid == 1) {
125 		printf("init died (signal %d, exit %d)\n",
126 		    WTERMSIG(rv), WEXITSTATUS(rv));
127 		panic("Going nowhere without my init!");
128 	}
129 
130 	lp = &p->p_lwp;		/* XXX lwp kill other threads */
131 
132 	sysmsg_rundown(lp, 1);
133 	caps_exit(lp->lwp_thread);
134 	aio_proc_rundown(p);
135 
136 	/* are we a task leader? */
137 	if(p == p->p_leader) {
138         	struct kill_args killArgs;
139 		killArgs.signum = SIGKILL;
140 		q = p->p_peers;
141 		while(q) {
142 			killArgs.pid = q->p_pid;
143 			/*
144 		         * The interface for kill is better
145 			 * than the internal signal
146 			 */
147 			kill(&killArgs);
148 			nq = q;
149 			q = q->p_peers;
150 		}
151 		while (p->p_peers)
152 		  tsleep((caddr_t)p, 0, "exit1", 0);
153 	}
154 
155 #ifdef PGINPROF
156 	vmsizmon();
157 #endif
158 	STOPEVENT(p, S_EXIT, rv);
159 	wakeup(&p->p_stype);	/* Wakeup anyone in procfs' PIOCWAIT */
160 
161 	/*
162 	 * Check if any loadable modules need anything done at process exit.
163 	 * e.g. SYSV IPC stuff
164 	 * XXX what if one of these generates an error?
165 	 */
166 	TAILQ_FOREACH(ep, &exit_list, next)
167 		(*ep->function)(p->p_thread);
168 
169 	if (p->p_flag & P_PROFIL)
170 		stopprofclock(p);
171 	MALLOC(p->p_ru, struct rusage *, sizeof(struct rusage),
172 		M_ZOMBIE, M_WAITOK);
173 	/*
174 	 * If parent is waiting for us to exit or exec,
175 	 * P_PPWAIT is set; we will wakeup the parent below.
176 	 */
177 	p->p_flag &= ~(P_TRACED | P_PPWAIT);
178 	p->p_flag |= P_WEXIT;
179 	SIGEMPTYSET(p->p_siglist);
180 	if (timevalisset(&p->p_realtimer.it_value))
181 		callout_stop(&p->p_ithandle);
182 
183 	/*
184 	 * Reset any sigio structures pointing to us as a result of
185 	 * F_SETOWN with our pid.
186 	 */
187 	funsetownlst(&p->p_sigiolst);
188 
189 	/*
190 	 * Close open files and release open-file table.
191 	 * This may block!
192 	 */
193 	fdfree(p);
194 	p->p_fd = NULL;
195 
196 	if(p->p_leader->p_peers) {
197 		q = p->p_leader;
198 		while(q->p_peers != p)
199 			q = q->p_peers;
200 		q->p_peers = p->p_peers;
201 		wakeup((caddr_t)p->p_leader);
202 	}
203 
204 	/*
205 	 * XXX Shutdown SYSV semaphores
206 	 */
207 	semexit(p);
208 
209 	KKASSERT(p->p_numposixlocks == 0);
210 
211 	/* The next two chunks should probably be moved to vmspace_exit. */
212 	vm = p->p_vmspace;
213 
214 	/*
215 	 * Release upcalls associated with this process
216 	 */
217 	if (vm->vm_upcalls)
218 		upc_release(vm, &p->p_lwp);
219 
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 	 * Processes sharing the same vmspace may exit in one order, and
229 	 * get cleaned up by vmspace_exit() in a different order.  The
230 	 * last exiting process to reach this point releases as much of
231 	 * the environment as it can, and the last process cleaned up
232 	 * by vmspace_exit() (which decrements exitingcnt) cleans up the
233 	 * remainder.
234 	 */
235 	++vm->vm_exitingcnt;
236 	if (--vm->vm_refcnt == 0) {
237 		shmexit(vm);
238 		pmap_remove_pages(vmspace_pmap(vm), VM_MIN_ADDRESS,
239 		    VM_MAXUSER_ADDRESS);
240 		(void) vm_map_remove(&vm->vm_map, VM_MIN_ADDRESS,
241 		    VM_MAXUSER_ADDRESS);
242 	}
243 
244 	if (SESS_LEADER(p)) {
245 		struct session *sp = p->p_session;
246 		struct vnode *vp;
247 
248 		if (sp->s_ttyvp) {
249 			/*
250 			 * We are the controlling process.  Signal the
251 			 * foreground process group, drain the controlling
252 			 * terminal, and revoke access to the controlling
253 			 * terminal.
254 			 *
255 			 * NOTE: while waiting for the process group to exit
256 			 * it is possible that one of the processes in the
257 			 * group will revoke the tty, so we have to recheck.
258 			 */
259 			if (sp->s_ttyp && (sp->s_ttyp->t_session == sp)) {
260 				if (sp->s_ttyp->t_pgrp)
261 					pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1);
262 				(void) ttywait(sp->s_ttyp);
263 				/*
264 				 * The tty could have been revoked
265 				 * if we blocked.
266 				 */
267 				if ((vp = sp->s_ttyvp) != NULL) {
268 					ttyclosesession(sp, 0);
269 					if (vx_lock(vp) == 0) {
270 						VOP_REVOKE(vp, REVOKEALL);
271 						vx_unlock(vp);
272 					}
273 					vrele(vp);	/* s_ttyvp ref */
274 				}
275 			}
276 			/*
277 			 * Release the tty.  If someone has it open via
278 			 * /dev/tty then close it (since they no longer can
279 			 * once we've NULL'd it out).
280 			 */
281 			if (sp->s_ttyvp)
282 				ttyclosesession(sp, 1);
283 			/*
284 			 * s_ttyp is not zero'd; we use this to indicate
285 			 * that the session once had a controlling terminal.
286 			 * (for logging and informational purposes)
287 			 */
288 		}
289 		sp->s_leader = NULL;
290 	}
291 	fixjobc(p, p->p_pgrp, 0);
292 	(void)acct_process(p);
293 #ifdef KTRACE
294 	/*
295 	 * release trace file
296 	 */
297 	p->p_traceflag = 0;	/* don't trace the vrele() */
298 	if ((vtmp = p->p_tracep) != NULL) {
299 		p->p_tracep = NULL;
300 		vrele(vtmp);
301 	}
302 #endif
303 	/*
304 	 * Release reference to text vnode
305 	 */
306 	if ((vtmp = p->p_textvp) != NULL) {
307 		p->p_textvp = NULL;
308 		vrele(vtmp);
309 	}
310 
311 	/*
312 	 * Once we set SZOMB the process can get reaped.  The wait1 code
313 	 * will also wait for TDF_EXITING to be set and for both TDF_RUNNING
314 	 * and TDF_PREEMPT_LOCK to be cleared in the thread's flags,
315 	 * indicating that it has been completely switched out for the last
316 	 * time.
317 	 */
318 
319 	/*
320 	 * Remove proc from allproc queue and pidhash chain.
321 	 * Place onto zombproc.  Unlink from parent's child list.
322 	 *
323 	 * Interlock the SZOMB state with a tsleep against p_lock
324 	 * (PHOLD/PRELE) so allproc loops don't get confused.  Get
325 	 * our own ref on p_lock to prevent us from getting reaped
326 	 * too early.
327 	 */
328 	LIST_REMOVE(p, p_list);
329 	LIST_INSERT_HEAD(&zombproc, p, p_list);
330 	p->p_flag |= P_ZOMBIE;
331 	while (p->p_lock)
332 		tsleep(p, 0, "reap1", hz / 10);
333 	LIST_REMOVE(p, p_hash);
334 
335 	q = LIST_FIRST(&p->p_children);
336 	if (q)		/* only need this if any child is S_ZOMB */
337 		wakeup((caddr_t) initproc);
338 	for (; q != 0; q = nq) {
339 		nq = LIST_NEXT(q, p_sibling);
340 		LIST_REMOVE(q, p_sibling);
341 		LIST_INSERT_HEAD(&initproc->p_children, q, p_sibling);
342 		q->p_pptr = initproc;
343 		q->p_sigparent = SIGCHLD;
344 		/*
345 		 * Traced processes are killed
346 		 * since their existence means someone is screwing up.
347 		 */
348 		if (q->p_flag & P_TRACED) {
349 			q->p_flag &= ~P_TRACED;
350 			psignal(q, SIGKILL);
351 		}
352 	}
353 
354 	/*
355 	 * Save exit status and final rusage info, adding in child rusage
356 	 * info and self times.
357 	 */
358 	p->p_xstat = rv;
359 	*p->p_ru = p->p_stats->p_ru;
360 	calcru(p, &p->p_ru->ru_utime, &p->p_ru->ru_stime, NULL);
361 	ruadd(p->p_ru, &p->p_stats->p_cru);
362 
363 	/*
364 	 * notify interested parties of our demise.
365 	 */
366 	KNOTE(&p->p_klist, NOTE_EXIT);
367 
368 	/*
369 	 * Notify parent that we're gone.  If parent has the PS_NOCLDWAIT
370 	 * flag set, notify process 1 instead (and hope it will handle
371 	 * this situation).
372 	 */
373 	if (p->p_pptr->p_procsig->ps_flag & PS_NOCLDWAIT) {
374 		struct proc *pp = p->p_pptr;
375 		proc_reparent(p, initproc);
376 		/*
377 		 * If this was the last child of our parent, notify
378 		 * parent, so in case he was wait(2)ing, he will
379 		 * continue.
380 		 */
381 		if (LIST_EMPTY(&pp->p_children))
382 			wakeup((caddr_t)pp);
383 	}
384 
385 	if (p->p_sigparent && p->p_pptr != initproc) {
386 	        psignal(p->p_pptr, p->p_sigparent);
387 	} else {
388 	        psignal(p->p_pptr, SIGCHLD);
389 	}
390 
391 	wakeup((caddr_t)p->p_pptr);
392 	/*
393 	 * cpu_exit is responsible for clearing curproc, since
394 	 * it is heavily integrated with the thread/switching sequence.
395 	 *
396 	 * Other substructures are freed from wait().
397 	 */
398 	if (--p->p_limit->p_refcnt == 0) {
399 		FREE(p->p_limit, M_SUBPROC);
400 		p->p_limit = NULL;
401 	}
402 
403 	/*
404 	 * Release the current user process designation on the process so
405 	 * the userland scheduler can work in someone else.
406 	 */
407 	p->p_usched->release_curproc(lp);
408 
409 	/*
410 	 * Finally, call machine-dependent code to release the remaining
411 	 * resources including address space, the kernel stack and pcb.
412 	 * The address space is released by "vmspace_free(p->p_vmspace)";
413 	 * This is machine-dependent, as we may have to change stacks
414 	 * or ensure that the current one isn't reallocated before we
415 	 * finish.  cpu_exit will end with a call to cpu_switch(), finishing
416 	 * our execution (pun intended).
417 	 */
418 	cpu_proc_exit();
419 }
420 
421 int
422 wait4(struct wait_args *uap)
423 {
424 	struct rusage rusage;
425 	int error, status;
426 
427 	error = kern_wait(uap->pid, uap->status ? &status : NULL,
428 	    uap->options, uap->rusage ? &rusage : NULL, &uap->sysmsg_fds[0]);
429 
430 	if (error == 0 && uap->status)
431 		error = copyout(&status, uap->status, sizeof(*uap->status));
432 	if (error == 0 && uap->rusage)
433 		error = copyout(&rusage, uap->rusage, sizeof(*uap->rusage));
434 	return (error);
435 }
436 
437 /*
438  * wait1()
439  *
440  * wait_args(int pid, int *status, int options, struct rusage *rusage)
441  */
442 int
443 kern_wait(pid_t pid, int *status, int options, struct rusage *rusage, int *res)
444 {
445 	struct thread *td = curthread;
446 	struct proc *q = td->td_proc;
447 	struct proc *p, *t;
448 	int nfound, error;
449 
450 	if (pid == 0)
451 		pid = -q->p_pgid;
452 	if (options &~ (WUNTRACED|WNOHANG|WLINUXCLONE))
453 		return (EINVAL);
454 loop:
455 	/*
456 	 * Hack for backwards compatibility with badly written user code.
457 	 * Or perhaps we have to do this anyway, it is unclear. XXX
458 	 *
459 	 * The problem is that if a process group is stopped and the parent
460 	 * is doing a wait*(..., WUNTRACED, ...), it will see the STOP
461 	 * of the child and then stop itself when it tries to return from the
462 	 * system call.  When the process group is resumed the parent will
463 	 * then get the STOP status even though the child has now resumed
464 	 * (a followup wait*() will get the CONT status).
465 	 *
466 	 * Previously the CONT would overwrite the STOP because the tstop
467 	 * was handled within tsleep(), and the parent would only see
468 	 * the CONT when both are stopped and continued together.  This litte
469 	 * two-line hack restores this effect.
470 	 */
471 	while (q->p_flag & P_STOPPED)
472             tstop(q);
473 
474 	nfound = 0;
475 	LIST_FOREACH(p, &q->p_children, p_sibling) {
476 		if (pid != WAIT_ANY &&
477 		    p->p_pid != pid && p->p_pgid != -pid)
478 			continue;
479 
480 		/* This special case handles a kthread spawned by linux_clone
481 		 * (see linux_misc.c).  The linux_wait4 and linux_waitpid
482 		 * functions need to be able to distinguish between waiting
483 		 * on a process and waiting on a thread.  It is a thread if
484 		 * p_sigparent is not SIGCHLD, and the WLINUXCLONE option
485 		 * signifies we want to wait for threads and not processes.
486 		 */
487 		if ((p->p_sigparent != SIGCHLD) ^
488 		    ((options & WLINUXCLONE) != 0)) {
489 			continue;
490 		}
491 
492 		nfound++;
493 		if (p->p_flag & P_ZOMBIE) {
494 			/*
495 			 * Other kernel threads may be in the middle of
496 			 * accessing the proc.  For example, kern/kern_proc.c
497 			 * could be blocked writing proc data to a sysctl.
498 			 * At the moment, if this occurs, we are not woken
499 			 * up and rely on a one-second retry.
500 			 */
501 			if (p->p_lock) {
502 				while (p->p_lock)
503 					tsleep(p, 0, "reap3", hz);
504 			}
505 			lwkt_wait_free(p->p_thread);
506 
507 			/*
508 			 * The process's thread may still be in the middle
509 			 * of switching away, we can't rip its stack out from
510 			 * under it until TDF_EXITING is set and both
511 			 * TDF_RUNNING and TDF_PREEMPT_LOCK are clear.
512 			 * TDF_PREEMPT_LOCK must be checked because TDF_RUNNING
513 			 * will be cleared temporarily if a thread gets
514 			 * preempted.
515 			 *
516 			 * YYY no wakeup occurs so we depend on the timeout.
517 			 */
518 			if ((p->p_thread->td_flags & (TDF_RUNNING|TDF_PREEMPT_LOCK|TDF_EXITING)) != TDF_EXITING) {
519 				tsleep(p->p_thread, 0, "reap2", 1);
520 				goto loop;
521 			}
522 
523 			/* scheduling hook for heuristic */
524 			p->p_usched->heuristic_exiting(td->td_lwp, &p->p_lwp);
525 
526 			/* Take care of our return values. */
527 			*res = p->p_pid;
528 			if (status)
529 				*status = p->p_xstat;
530 			if (rusage)
531 				*rusage = *p->p_ru;
532 			/*
533 			 * If we got the child via a ptrace 'attach',
534 			 * we need to give it back to the old parent.
535 			 */
536 			if (p->p_oppid && (t = pfind(p->p_oppid))) {
537 				p->p_oppid = 0;
538 				proc_reparent(p, t);
539 				psignal(t, SIGCHLD);
540 				wakeup((caddr_t)t);
541 				return (0);
542 			}
543 			p->p_xstat = 0;
544 			ruadd(&q->p_stats->p_cru, p->p_ru);
545 			FREE(p->p_ru, M_ZOMBIE);
546 			p->p_ru = NULL;
547 
548 			/*
549 			 * Decrement the count of procs running with this uid.
550 			 */
551 			chgproccnt(p->p_ucred->cr_ruidinfo, -1, 0);
552 
553 			/*
554 			 * Free up credentials.
555 			 */
556 			crfree(p->p_ucred);
557 			p->p_ucred = NULL;
558 
559 			/*
560 			 * Remove unused arguments
561 			 */
562 			if (p->p_args && --p->p_args->ar_ref == 0)
563 				FREE(p->p_args, M_PARGS);
564 
565 			/*
566 			 * Finally finished with old proc entry.
567 			 * Unlink it from its process group and free it.
568 			 */
569 			leavepgrp(p);
570 			LIST_REMOVE(p, p_list);	/* off zombproc */
571 			LIST_REMOVE(p, p_sibling);
572 
573 			if (--p->p_procsig->ps_refcnt == 0) {
574 				if (p->p_sigacts != &p->p_addr->u_sigacts)
575 					FREE(p->p_sigacts, M_SUBPROC);
576 			        FREE(p->p_procsig, M_SUBPROC);
577 				p->p_procsig = NULL;
578 			}
579 
580 			vm_waitproc(p);
581 			zfree(proc_zone, p);
582 			nprocs--;
583 			return (0);
584 		}
585 		if ((p->p_flag & P_STOPPED) && (p->p_flag & P_WAITED) == 0 &&
586 		    (p->p_flag & P_TRACED || options & WUNTRACED)) {
587 			p->p_flag |= P_WAITED;
588 
589 			*res = p->p_pid;
590 			if (status)
591 				*status = W_STOPCODE(p->p_xstat);
592 			/* Zero rusage so we get something consistent. */
593 			if (rusage)
594 				bzero(rusage, sizeof(rusage));
595 			return (0);
596 		}
597 	}
598 	if (nfound == 0)
599 		return (ECHILD);
600 	if (options & WNOHANG) {
601 		*res = 0;
602 		return (0);
603 	}
604 	error = tsleep((caddr_t)q, PCATCH, "wait", 0);
605 	if (error)
606 		return (error);
607 	goto loop;
608 }
609 
610 /*
611  * make process 'parent' the new parent of process 'child'.
612  */
613 void
614 proc_reparent(struct proc *child, struct proc *parent)
615 {
616 
617 	if (child->p_pptr == parent)
618 		return;
619 
620 	LIST_REMOVE(child, p_sibling);
621 	LIST_INSERT_HEAD(&parent->p_children, child, p_sibling);
622 	child->p_pptr = parent;
623 }
624 
625 /*
626  * The next two functions are to handle adding/deleting items on the
627  * exit callout list
628  *
629  * at_exit():
630  * Take the arguments given and put them onto the exit callout list,
631  * However first make sure that it's not already there.
632  * returns 0 on success.
633  */
634 
635 int
636 at_exit(exitlist_fn function)
637 {
638 	struct exitlist *ep;
639 
640 #ifdef INVARIANTS
641 	/* Be noisy if the programmer has lost track of things */
642 	if (rm_at_exit(function))
643 		printf("WARNING: exit callout entry (%p) already present\n",
644 		    function);
645 #endif
646 	ep = malloc(sizeof(*ep), M_ATEXIT, M_NOWAIT);
647 	if (ep == NULL)
648 		return (ENOMEM);
649 	ep->function = function;
650 	TAILQ_INSERT_TAIL(&exit_list, ep, next);
651 	return (0);
652 }
653 
654 /*
655  * Scan the exit callout list for the given item and remove it.
656  * Returns the number of items removed (0 or 1)
657  */
658 int
659 rm_at_exit(exitlist_fn function)
660 {
661 	struct exitlist *ep;
662 
663 	TAILQ_FOREACH(ep, &exit_list, next) {
664 		if (ep->function == function) {
665 			TAILQ_REMOVE(&exit_list, ep, next);
666 			free(ep, M_ATEXIT);
667 			return(1);
668 		}
669 	}
670 	return (0);
671 }
672 
673 void
674 check_sigacts(void)
675 {
676 	struct proc *p = curproc;
677 	struct sigacts *pss;
678 
679 	if (p->p_procsig->ps_refcnt == 1 &&
680 	    p->p_sigacts != &p->p_addr->u_sigacts) {
681 		pss = p->p_sigacts;
682 		crit_enter();
683 		p->p_addr->u_sigacts = *pss;
684 		p->p_sigacts = &p->p_addr->u_sigacts;
685 		crit_exit();
686 		FREE(pss, M_SUBPROC);
687 	}
688 }
689 
690