Rename printf -> kprintf in sys/ and add some defines where necessary
[dragonfly.git] / sys / kern / kern_exit.c
CommitLineData
984263bc
MD
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 $
6ea70f76 40 * $DragonFly: src/sys/kern/kern_exit.c,v 1.66 2006/12/23 00:35:04 swildner Exp $
984263bc
MD
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>
29f58392 52#include <sys/ktrace.h>
984263bc
MD
53#include <sys/pioctl.h>
54#include <sys/tty.h>
55#include <sys/wait.h>
56#include <sys/vnode.h>
57#include <sys/resourcevar.h>
58#include <sys/signalvar.h>
59#include <sys/ptrace.h>
60#include <sys/acct.h> /* for acct_process() function prototype */
61#include <sys/filedesc.h>
62#include <sys/shm.h>
63#include <sys/sem.h>
64#include <sys/aio.h>
65#include <sys/jail.h>
9697c509 66#include <sys/kern_syscall.h>
a722be49 67#include <sys/upcall.h>
f6bf3af1 68#include <sys/caps.h>
984263bc
MD
69
70#include <vm/vm.h>
71#include <vm/vm_param.h>
72#include <sys/lock.h>
73#include <vm/pmap.h>
74#include <vm/vm_map.h>
75#include <vm/vm_zone.h>
76#include <vm/vm_extern.h>
77#include <sys/user.h>
78
e43a034f
MD
79#include <sys/thread2.h>
80
984263bc 81static MALLOC_DEFINE(M_ATEXIT, "atexit", "atexit callback");
d511d5d5 82static MALLOC_DEFINE(M_ZOMBIE, "zombie", "zombie proc status");
984263bc 83
984263bc
MD
84/*
85 * callout list for things to do at exit time
86 */
87struct exitlist {
88 exitlist_fn function;
89 TAILQ_ENTRY(exitlist) next;
90};
91
92TAILQ_HEAD(exit_list_head, exitlist);
93static struct exit_list_head exit_list = TAILQ_HEAD_INITIALIZER(exit_list);
94
95/*
96 * exit --
97 * Death of process.
41c20dac
MD
98 *
99 * SYS_EXIT_ARGS(int rval)
984263bc 100 */
753fd850
MD
101int
102sys_exit(struct exit_args *uap)
984263bc 103{
41c20dac 104 exit1(W_EXITCODE(uap->rval, 0));
984263bc
MD
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 */
113void
41c20dac 114exit1(int rv)
984263bc 115{
bb3cd951
SS
116 struct thread *td = curthread;
117 struct proc *p = td->td_proc;
118 struct lwp *lp = td->td_lwp;
41c20dac
MD
119 struct proc *q, *nq;
120 struct vmspace *vm;
984263bc
MD
121 struct vnode *vtmp;
122 struct exitlist *ep;
123
124 if (p->p_pid == 1) {
6ea70f76 125 kprintf("init died (signal %d, exit %d)\n",
984263bc
MD
126 WTERMSIG(rv), WEXITSTATUS(rv));
127 panic("Going nowhere without my init!");
128 }
129
bb3cd951 130 /* XXX lwp kill other threads */
81eea9f4 131
81eea9f4 132 caps_exit(lp->lwp_thread);
984263bc
MD
133 aio_proc_rundown(p);
134
135 /* are we a task leader? */
136 if(p == p->p_leader) {
137 struct kill_args killArgs;
138 killArgs.signum = SIGKILL;
139 q = p->p_peers;
140 while(q) {
141 killArgs.pid = q->p_pid;
142 /*
143 * The interface for kill is better
144 * than the internal signal
145 */
753fd850 146 sys_kill(&killArgs);
984263bc
MD
147 nq = q;
148 q = q->p_peers;
149 }
150 while (p->p_peers)
377d4740 151 tsleep((caddr_t)p, 0, "exit1", 0);
984263bc
MD
152 }
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)
bb3cd951 166 (*ep->function)(td);
984263bc
MD
167
168 if (p->p_flag & P_PROFIL)
169 stopprofclock(p);
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 p->p_flag &= ~(P_TRACED | P_PPWAIT);
177 p->p_flag |= P_WEXIT;
178 SIGEMPTYSET(p->p_siglist);
179 if (timevalisset(&p->p_realtimer.it_value))
8fbf9130 180 callout_stop(&p->p_ithandle);
984263bc
MD
181
182 /*
183 * Reset any sigio structures pointing to us as a result of
184 * F_SETOWN with our pid.
185 */
186 funsetownlst(&p->p_sigiolst);
187
188 /*
189 * Close open files and release open-file table.
190 * This may block!
191 */
192 fdfree(p);
1fbb5fc0 193 p->p_fd = NULL;
984263bc
MD
194
195 if(p->p_leader->p_peers) {
196 q = p->p_leader;
197 while(q->p_peers != p)
198 q = q->p_peers;
199 q->p_peers = p->p_peers;
200 wakeup((caddr_t)p->p_leader);
201 }
202
203 /*
204 * XXX Shutdown SYSV semaphores
205 */
206 semexit(p);
207
06ecca5a
MD
208 KKASSERT(p->p_numposixlocks == 0);
209
984263bc
MD
210 /* The next two chunks should probably be moved to vmspace_exit. */
211 vm = p->p_vmspace;
a722be49
MD
212
213 /*
214 * Release upcalls associated with this process
215 */
216 if (vm->vm_upcalls)
4170cc8b 217 upc_release(vm, &p->p_lwp);
a722be49 218
0daa37a5 219 /* clean up data related to virtual kernel operation */
4a22e893
MD
220 if (p->p_vkernel)
221 vkernel_exit(p);
0daa37a5 222
984263bc
MD
223 /*
224 * Release user portion of address space.
225 * This releases references to vnodes,
226 * which could cause I/O if the file has been unlinked.
227 * Need to do this early enough that we can still sleep.
228 * Can't free the entire vmspace as the kernel stack
229 * may be mapped within that space also.
230 *
231 * Processes sharing the same vmspace may exit in one order, and
232 * get cleaned up by vmspace_exit() in a different order. The
233 * last exiting process to reach this point releases as much of
234 * the environment as it can, and the last process cleaned up
235 * by vmspace_exit() (which decrements exitingcnt) cleans up the
236 * remainder.
237 */
238 ++vm->vm_exitingcnt;
239 if (--vm->vm_refcnt == 0) {
fef0fdf2 240 shmexit(vm);
88181b08
MD
241 pmap_remove_pages(vmspace_pmap(vm), VM_MIN_USER_ADDRESS,
242 VM_MAX_USER_ADDRESS);
243 vm_map_remove(&vm->vm_map, VM_MIN_USER_ADDRESS,
244 VM_MAX_USER_ADDRESS);
984263bc
MD
245 }
246
247 if (SESS_LEADER(p)) {
1fd87d54 248 struct session *sp = p->p_session;
1fbb5fc0 249 struct vnode *vp;
984263bc
MD
250
251 if (sp->s_ttyvp) {
252 /*
1fbb5fc0
MD
253 * We are the controlling process. Signal the
254 * foreground process group, drain the controlling
255 * terminal, and revoke access to the controlling
256 * terminal.
257 *
258 * NOTE: while waiting for the process group to exit
259 * it is possible that one of the processes in the
260 * group will revoke the tty, so we have to recheck.
984263bc
MD
261 */
262 if (sp->s_ttyp && (sp->s_ttyp->t_session == sp)) {
263 if (sp->s_ttyp->t_pgrp)
264 pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1);
265 (void) ttywait(sp->s_ttyp);
266 /*
267 * The tty could have been revoked
268 * if we blocked.
269 */
1fbb5fc0 270 if ((vp = sp->s_ttyvp) != NULL) {
5fd012e0 271 ttyclosesession(sp, 0);
e3332475
MD
272 vx_lock(vp);
273 VOP_REVOKE(vp, REVOKEALL);
274 vx_unlock(vp);
5fd012e0 275 vrele(vp); /* s_ttyvp ref */
1fbb5fc0 276 }
984263bc 277 }
5fd012e0
MD
278 /*
279 * Release the tty. If someone has it open via
280 * /dev/tty then close it (since they no longer can
281 * once we've NULL'd it out).
282 */
283 if (sp->s_ttyvp)
284 ttyclosesession(sp, 1);
984263bc
MD
285 /*
286 * s_ttyp is not zero'd; we use this to indicate
287 * that the session once had a controlling terminal.
288 * (for logging and informational purposes)
289 */
290 }
291 sp->s_leader = NULL;
292 }
293 fixjobc(p, p->p_pgrp, 0);
294 (void)acct_process(p);
295#ifdef KTRACE
296 /*
297 * release trace file
298 */
29f58392
MD
299 if (p->p_tracenode)
300 ktrdestroy(&p->p_tracenode);
301 p->p_traceflag = 0;
984263bc
MD
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
ae8050a4 311 /*
5bf0d9b5
MD
312 * Move the process to the zombie list. This will block
313 * until the process p_lock count reaches 0. The process will
314 * not be reaped until TDF_EXITING is set by cpu_thread_exit(),
315 * which is called from cpu_proc_exit().
ae8050a4 316 */
5bf0d9b5 317 proc_move_allproc_zombie(p);
984263bc
MD
318
319 q = LIST_FIRST(&p->p_children);
320 if (q) /* only need this if any child is S_ZOMB */
321 wakeup((caddr_t) initproc);
322 for (; q != 0; q = nq) {
323 nq = LIST_NEXT(q, p_sibling);
324 LIST_REMOVE(q, p_sibling);
325 LIST_INSERT_HEAD(&initproc->p_children, q, p_sibling);
326 q->p_pptr = initproc;
327 q->p_sigparent = SIGCHLD;
328 /*
329 * Traced processes are killed
330 * since their existence means someone is screwing up.
331 */
332 if (q->p_flag & P_TRACED) {
333 q->p_flag &= ~P_TRACED;
84204577 334 ksignal(q, SIGKILL);
984263bc
MD
335 }
336 }
337
338 /*
339 * Save exit status and final rusage info, adding in child rusage
340 * info and self times.
341 */
342 p->p_xstat = rv;
343 *p->p_ru = p->p_stats->p_ru;
344 calcru(p, &p->p_ru->ru_utime, &p->p_ru->ru_stime, NULL);
345 ruadd(p->p_ru, &p->p_stats->p_cru);
346
984263bc
MD
347 /*
348 * notify interested parties of our demise.
349 */
350 KNOTE(&p->p_klist, NOTE_EXIT);
351
352 /*
353 * Notify parent that we're gone. If parent has the PS_NOCLDWAIT
354 * flag set, notify process 1 instead (and hope it will handle
355 * this situation).
356 */
357 if (p->p_pptr->p_procsig->ps_flag & PS_NOCLDWAIT) {
358 struct proc *pp = p->p_pptr;
359 proc_reparent(p, initproc);
360 /*
361 * If this was the last child of our parent, notify
362 * parent, so in case he was wait(2)ing, he will
363 * continue.
364 */
365 if (LIST_EMPTY(&pp->p_children))
366 wakeup((caddr_t)pp);
367 }
368
369 if (p->p_sigparent && p->p_pptr != initproc) {
84204577 370 ksignal(p->p_pptr, p->p_sigparent);
984263bc 371 } else {
84204577 372 ksignal(p->p_pptr, SIGCHLD);
984263bc
MD
373 }
374
375 wakeup((caddr_t)p->p_pptr);
984263bc 376 /*
8ad65e08
MD
377 * cpu_exit is responsible for clearing curproc, since
378 * it is heavily integrated with the thread/switching sequence.
379 *
984263bc
MD
380 * Other substructures are freed from wait().
381 */
c0b8a06d 382 plimit_free(&p->p_limit);
984263bc 383
26a0694b 384 /*
0a3f9b47
MD
385 * Release the current user process designation on the process so
386 * the userland scheduler can work in someone else.
26a0694b 387 */
553ea3c8 388 p->p_usched->release_curproc(lp);
26a0694b 389
984263bc
MD
390 /*
391 * Finally, call machine-dependent code to release the remaining
392 * resources including address space, the kernel stack and pcb.
393 * The address space is released by "vmspace_free(p->p_vmspace)";
394 * This is machine-dependent, as we may have to change stacks
395 * or ensure that the current one isn't reallocated before we
396 * finish. cpu_exit will end with a call to cpu_switch(), finishing
397 * our execution (pun intended).
398 */
99df837e 399 cpu_proc_exit();
984263bc
MD
400}
401
984263bc 402int
753fd850 403sys_wait4(struct wait_args *uap)
984263bc 404{
9697c509
DRJ
405 struct rusage rusage;
406 int error, status;
984263bc 407
9697c509
DRJ
408 error = kern_wait(uap->pid, uap->status ? &status : NULL,
409 uap->options, uap->rusage ? &rusage : NULL, &uap->sysmsg_fds[0]);
984263bc 410
9697c509
DRJ
411 if (error == 0 && uap->status)
412 error = copyout(&status, uap->status, sizeof(*uap->status));
413 if (error == 0 && uap->rusage)
414 error = copyout(&rusage, uap->rusage, sizeof(*uap->rusage));
415 return (error);
984263bc
MD
416}
417
41c20dac
MD
418/*
419 * wait1()
420 *
421 * wait_args(int pid, int *status, int options, struct rusage *rusage)
422 */
9697c509
DRJ
423int
424kern_wait(pid_t pid, int *status, int options, struct rusage *rusage, int *res)
984263bc 425{
9697c509 426 struct thread *td = curthread;
bb3cd951 427 struct thread *deadtd;
9697c509 428 struct proc *q = td->td_proc;
41c20dac 429 struct proc *p, *t;
9697c509 430 int nfound, error;
984263bc 431
9697c509
DRJ
432 if (pid == 0)
433 pid = -q->p_pgid;
434 if (options &~ (WUNTRACED|WNOHANG|WLINUXCLONE))
984263bc
MD
435 return (EINVAL);
436loop:
6d5b152e
MD
437 /*
438 * Hack for backwards compatibility with badly written user code.
439 * Or perhaps we have to do this anyway, it is unclear. XXX
440 *
441 * The problem is that if a process group is stopped and the parent
442 * is doing a wait*(..., WUNTRACED, ...), it will see the STOP
443 * of the child and then stop itself when it tries to return from the
444 * system call. When the process group is resumed the parent will
445 * then get the STOP status even though the child has now resumed
446 * (a followup wait*() will get the CONT status).
447 *
448 * Previously the CONT would overwrite the STOP because the tstop
449 * was handled within tsleep(), and the parent would only see
450 * the CONT when both are stopped and continued together. This litte
451 * two-line hack restores this effect.
452 */
453 while (q->p_flag & P_STOPPED)
454 tstop(q);
455
984263bc
MD
456 nfound = 0;
457 LIST_FOREACH(p, &q->p_children, p_sibling) {
9697c509
DRJ
458 if (pid != WAIT_ANY &&
459 p->p_pid != pid && p->p_pgid != -pid)
984263bc
MD
460 continue;
461
462 /* This special case handles a kthread spawned by linux_clone
352f5709
MD
463 * (see linux_misc.c). The linux_wait4 and linux_waitpid
464 * functions need to be able to distinguish between waiting
465 * on a process and waiting on a thread. It is a thread if
466 * p_sigparent is not SIGCHLD, and the WLINUXCLONE option
467 * signifies we want to wait for threads and not processes.
984263bc 468 */
352f5709
MD
469 if ((p->p_sigparent != SIGCHLD) ^
470 ((options & WLINUXCLONE) != 0)) {
984263bc 471 continue;
352f5709 472 }
984263bc
MD
473
474 nfound++;
f026f3b0 475 if (p->p_flag & P_ZOMBIE) {
bb3cd951
SS
476 KKASSERT((p->p_nthreads == 1));
477 deadtd = LIST_FIRST(&p->p_lwps)->lwp_thread;
478
c008d3ad
MD
479 /*
480 * Other kernel threads may be in the middle of
481 * accessing the proc. For example, kern/kern_proc.c
482 * could be blocked writing proc data to a sysctl.
483 * At the moment, if this occurs, we are not woken
484 * up and rely on a one-second retry.
485 */
486 if (p->p_lock) {
c008d3ad 487 while (p->p_lock)
e38910da 488 tsleep(p, 0, "reap3", hz);
c008d3ad 489 }
bb3cd951 490 lwkt_wait_free(deadtd);
ae8050a4 491
c1102e9f
MD
492 /*
493 * The process's thread may still be in the middle
494 * of switching away, we can't rip its stack out from
495 * under it until TDF_EXITING is set and both
496 * TDF_RUNNING and TDF_PREEMPT_LOCK are clear.
497 * TDF_PREEMPT_LOCK must be checked because TDF_RUNNING
498 * will be cleared temporarily if a thread gets
499 * preempted.
500 *
501 * YYY no wakeup occurs so we depend on the timeout.
502 */
bb3cd951
SS
503 if ((deadtd->td_flags & (TDF_RUNNING|TDF_PREEMPT_LOCK|TDF_EXITING)) != TDF_EXITING) {
504 tsleep(deadtd, 0, "reap2", 1);
c1102e9f
MD
505 goto loop;
506 }
507
352f5709 508 /* scheduling hook for heuristic */
bb3cd951 509 p->p_usched->heuristic_exiting(td->td_lwp, deadtd->td_lwp);
984263bc 510
9697c509
DRJ
511 /* Take care of our return values. */
512 *res = p->p_pid;
513 if (status)
514 *status = p->p_xstat;
515 if (rusage)
516 *rusage = *p->p_ru;
984263bc
MD
517 /*
518 * If we got the child via a ptrace 'attach',
519 * we need to give it back to the old parent.
520 */
521 if (p->p_oppid && (t = pfind(p->p_oppid))) {
522 p->p_oppid = 0;
523 proc_reparent(p, t);
84204577 524 ksignal(t, SIGCHLD);
984263bc
MD
525 wakeup((caddr_t)t);
526 return (0);
527 }
528 p->p_xstat = 0;
529 ruadd(&q->p_stats->p_cru, p->p_ru);
530 FREE(p->p_ru, M_ZOMBIE);
531 p->p_ru = NULL;
532
533 /*
534 * Decrement the count of procs running with this uid.
535 */
9697c509 536 chgproccnt(p->p_ucred->cr_ruidinfo, -1, 0);
984263bc
MD
537
538 /*
539 * Free up credentials.
540 */
41c20dac
MD
541 crfree(p->p_ucred);
542 p->p_ucred = NULL;
984263bc
MD
543
544 /*
545 * Remove unused arguments
546 */
547 if (p->p_args && --p->p_args->ar_ref == 0)
548 FREE(p->p_args, M_PARGS);
549
550 /*
551 * Finally finished with old proc entry.
552 * Unlink it from its process group and free it.
553 */
554 leavepgrp(p);
5bf0d9b5 555 proc_remove_zombie(p);
984263bc
MD
556
557 if (--p->p_procsig->ps_refcnt == 0) {
558 if (p->p_sigacts != &p->p_addr->u_sigacts)
559 FREE(p->p_sigacts, M_SUBPROC);
560 FREE(p->p_procsig, M_SUBPROC);
561 p->p_procsig = NULL;
562 }
563
984263bc
MD
564 vm_waitproc(p);
565 zfree(proc_zone, p);
566 nprocs--;
567 return (0);
568 }
344ad853 569 if ((p->p_flag & P_STOPPED) && (p->p_flag & P_WAITED) == 0 &&
9697c509 570 (p->p_flag & P_TRACED || options & WUNTRACED)) {
984263bc 571 p->p_flag |= P_WAITED;
9697c509
DRJ
572
573 *res = p->p_pid;
574 if (status)
a89c93ee 575 *status = W_STOPCODE(p->p_xstat);
9697c509
DRJ
576 /* Zero rusage so we get something consistent. */
577 if (rusage)
578 bzero(rusage, sizeof(rusage));
579 return (0);
984263bc
MD
580 }
581 }
582 if (nfound == 0)
583 return (ECHILD);
9697c509
DRJ
584 if (options & WNOHANG) {
585 *res = 0;
984263bc
MD
586 return (0);
587 }
9697c509
DRJ
588 error = tsleep((caddr_t)q, PCATCH, "wait", 0);
589 if (error)
984263bc
MD
590 return (error);
591 goto loop;
592}
593
594/*
595 * make process 'parent' the new parent of process 'child'.
596 */
597void
77153250 598proc_reparent(struct proc *child, struct proc *parent)
984263bc
MD
599{
600
601 if (child->p_pptr == parent)
602 return;
603
604 LIST_REMOVE(child, p_sibling);
605 LIST_INSERT_HEAD(&parent->p_children, child, p_sibling);
606 child->p_pptr = parent;
607}
608
609/*
610 * The next two functions are to handle adding/deleting items on the
611 * exit callout list
612 *
613 * at_exit():
614 * Take the arguments given and put them onto the exit callout list,
615 * However first make sure that it's not already there.
616 * returns 0 on success.
617 */
618
619int
77153250 620at_exit(exitlist_fn function)
984263bc
MD
621{
622 struct exitlist *ep;
623
624#ifdef INVARIANTS
625 /* Be noisy if the programmer has lost track of things */
626 if (rm_at_exit(function))
6ea70f76 627 kprintf("WARNING: exit callout entry (%p) already present\n",
984263bc
MD
628 function);
629#endif
efda3bd0 630 ep = kmalloc(sizeof(*ep), M_ATEXIT, M_NOWAIT);
984263bc
MD
631 if (ep == NULL)
632 return (ENOMEM);
633 ep->function = function;
634 TAILQ_INSERT_TAIL(&exit_list, ep, next);
635 return (0);
636}
637
638/*
639 * Scan the exit callout list for the given item and remove it.
640 * Returns the number of items removed (0 or 1)
641 */
642int
77153250 643rm_at_exit(exitlist_fn function)
984263bc
MD
644{
645 struct exitlist *ep;
646
647 TAILQ_FOREACH(ep, &exit_list, next) {
648 if (ep->function == function) {
649 TAILQ_REMOVE(&exit_list, ep, next);
efda3bd0 650 kfree(ep, M_ATEXIT);
984263bc
MD
651 return(1);
652 }
653 }
654 return (0);
655}
656
77153250
CP
657void
658check_sigacts(void)
984263bc
MD
659{
660 struct proc *p = curproc;
661 struct sigacts *pss;
984263bc
MD
662
663 if (p->p_procsig->ps_refcnt == 1 &&
664 p->p_sigacts != &p->p_addr->u_sigacts) {
665 pss = p->p_sigacts;
e43a034f 666 crit_enter();
984263bc
MD
667 p->p_addr->u_sigacts = *pss;
668 p->p_sigacts = &p->p_addr->u_sigacts;
e43a034f 669 crit_exit();
984263bc
MD
670 FREE(pss, M_SUBPROC);
671 }
672}
673