1:1 Userland threading stage 4.4/4:
[dragonfly.git] / sys / kern / kern_exit.c
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.76 2007/02/24 14:25:06 corecode 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/ktrace.h>
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/taskqueue.h>
60 #include <sys/ptrace.h>
61 #include <sys/acct.h>           /* for acct_process() function prototype */
62 #include <sys/filedesc.h>
63 #include <sys/shm.h>
64 #include <sys/sem.h>
65 #include <sys/aio.h>
66 #include <sys/jail.h>
67 #include <sys/kern_syscall.h>
68 #include <sys/upcall.h>
69 #include <sys/caps.h>
70
71 #include <vm/vm.h>
72 #include <vm/vm_param.h>
73 #include <sys/lock.h>
74 #include <vm/pmap.h>
75 #include <vm/vm_map.h>
76 #include <vm/vm_zone.h>
77 #include <vm/vm_extern.h>
78 #include <sys/user.h>
79
80 #include <sys/thread2.h>
81
82 static MALLOC_DEFINE(M_ATEXIT, "atexit", "atexit callback");
83 static MALLOC_DEFINE(M_ZOMBIE, "zombie", "zombie proc status");
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  * LWP reaper data
98  */
99 struct task *deadlwp_task[MAXCPU];
100 struct lwplist deadlwp_list[MAXCPU];
101
102 /*
103  * exit --
104  *      Death of process.
105  *
106  * SYS_EXIT_ARGS(int rval)
107  */
108 int
109 sys_exit(struct exit_args *uap)
110 {
111         exit1(W_EXITCODE(uap->rval, 0));
112         /* NOTREACHED */
113 }
114
115 void
116 killlwps(struct lwp *lp)
117 {
118         struct proc *p = lp->lwp_proc;
119         struct lwp *tlp;
120
121         FOREACH_LWP_IN_PROC(tlp, p) {
122                 if (tlp == lp)
123                         continue;       /* don't kill the current lwp */
124                 tlp->lwp_flag |= LWP_WEXIT;
125                 lwp_signotify(tlp);
126         }
127
128         while (p->p_nthreads > 1) {
129                 if (bootverbose)
130                         kprintf("killlwps: waiting for %d lwps of pid %d to die\n",
131                                 p->p_nthreads - 1, p->p_pid);
132                 tsleep(&p->p_nthreads, 0, "killlwps", hz);
133         }
134 }
135
136 /*
137  * Exit: deallocate address space and other resources, change proc state
138  * to zombie, and unlink proc from allproc and parent's lists.  Save exit
139  * status and rusage for wait().  Check for child processes and orphan them.
140  */
141 void
142 exit1(int rv)
143 {
144         struct thread *td = curthread;
145         struct proc *p = td->td_proc;
146         struct lwp *lp = td->td_lwp;
147         struct proc *q, *nq;
148         struct vmspace *vm;
149         struct vnode *vtmp;
150         struct exitlist *ep;
151
152         if (p->p_pid == 1) {
153                 kprintf("init died (signal %d, exit %d)\n",
154                     WTERMSIG(rv), WEXITSTATUS(rv));
155                 panic("Going nowhere without my init!");
156         }
157
158         /*
159          * Kill all other threads if there are any.
160          *
161          * XXX TGEN Need to protect against multiple lwps of the same proc
162          *          entering this function?
163          */
164         if (p->p_nthreads > 1)
165                 killlwps(lp);
166
167         caps_exit(lp->lwp_thread);
168         aio_proc_rundown(p);
169
170         /* are we a task leader? */
171         if(p == p->p_leader) {
172                 struct kill_args killArgs;
173                 killArgs.signum = SIGKILL;
174                 q = p->p_peers;
175                 while(q) {
176                         killArgs.pid = q->p_pid;
177                         /*
178                          * The interface for kill is better
179                          * than the internal signal
180                          */
181                         sys_kill(&killArgs);
182                         nq = q;
183                         q = q->p_peers;
184                 }
185                 while (p->p_peers) 
186                   tsleep((caddr_t)p, 0, "exit1", 0);
187         } 
188
189 #ifdef PGINPROF
190         vmsizmon();
191 #endif
192         STOPEVENT(p, S_EXIT, rv);
193         wakeup(&p->p_stype);    /* Wakeup anyone in procfs' PIOCWAIT */
194
195         /* 
196          * Check if any loadable modules need anything done at process exit.
197          * e.g. SYSV IPC stuff
198          * XXX what if one of these generates an error?
199          */
200         TAILQ_FOREACH(ep, &exit_list, next) 
201                 (*ep->function)(td);
202
203         if (p->p_flag & P_PROFIL)
204                 stopprofclock(p);
205         /*
206          * If parent is waiting for us to exit or exec,
207          * P_PPWAIT is set; we will wakeup the parent below.
208          */
209         p->p_flag &= ~(P_TRACED | P_PPWAIT);
210         p->p_flag |= P_WEXIT;
211         SIGEMPTYSET(p->p_siglist);
212         SIGEMPTYSET(lp->lwp_siglist);
213         if (timevalisset(&p->p_realtimer.it_value))
214                 callout_stop(&p->p_ithandle);
215
216         /*
217          * Reset any sigio structures pointing to us as a result of
218          * F_SETOWN with our pid.
219          */
220         funsetownlst(&p->p_sigiolst);
221
222         /*
223          * Close open files and release open-file table.
224          * This may block!
225          */
226         fdfree(p);
227         p->p_fd = NULL;
228
229         if(p->p_leader->p_peers) {
230                 q = p->p_leader;
231                 while(q->p_peers != p)
232                         q = q->p_peers;
233                 q->p_peers = p->p_peers;
234                 wakeup((caddr_t)p->p_leader);
235         }
236
237         /*
238          * XXX Shutdown SYSV semaphores
239          */
240         semexit(p);
241
242         KKASSERT(p->p_numposixlocks == 0);
243
244         /* The next two chunks should probably be moved to vmspace_exit. */
245         vm = p->p_vmspace;
246
247         /*
248          * Release upcalls associated with this process
249          */
250         if (vm->vm_upcalls)
251                 upc_release(vm, lp);
252
253         /* clean up data related to virtual kernel operation */
254         if (p->p_vkernel)
255                 vkernel_exit(p);
256
257         /*
258          * Release user portion of address space.
259          * This releases references to vnodes,
260          * which could cause I/O if the file has been unlinked.
261          * Need to do this early enough that we can still sleep.
262          * Can't free the entire vmspace as the kernel stack
263          * may be mapped within that space also.
264          *
265          * Processes sharing the same vmspace may exit in one order, and
266          * get cleaned up by vmspace_exit() in a different order.  The
267          * last exiting process to reach this point releases as much of
268          * the environment as it can, and the last process cleaned up
269          * by vmspace_exit() (which decrements exitingcnt) cleans up the
270          * remainder.
271          */
272         ++vm->vm_exitingcnt;
273         if (--vm->vm_refcnt == 0) {
274                 shmexit(vm);
275                 pmap_remove_pages(vmspace_pmap(vm), VM_MIN_USER_ADDRESS,
276                                   VM_MAX_USER_ADDRESS);
277                 vm_map_remove(&vm->vm_map, VM_MIN_USER_ADDRESS,
278                               VM_MAX_USER_ADDRESS);
279         }
280
281         if (SESS_LEADER(p)) {
282                 struct session *sp = p->p_session;
283                 struct vnode *vp;
284
285                 if (sp->s_ttyvp) {
286                         /*
287                          * We are the controlling process.  Signal the 
288                          * foreground process group, drain the controlling
289                          * terminal, and revoke access to the controlling
290                          * terminal.
291                          *
292                          * NOTE: while waiting for the process group to exit
293                          * it is possible that one of the processes in the
294                          * group will revoke the tty, so we have to recheck.
295                          */
296                         if (sp->s_ttyp && (sp->s_ttyp->t_session == sp)) {
297                                 if (sp->s_ttyp->t_pgrp)
298                                         pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1);
299                                 (void) ttywait(sp->s_ttyp);
300                                 /*
301                                  * The tty could have been revoked
302                                  * if we blocked.
303                                  */
304                                 if ((vp = sp->s_ttyvp) != NULL) {
305                                         ttyclosesession(sp, 0);
306                                         vx_lock(vp);
307                                         VOP_REVOKE(vp, REVOKEALL);
308                                         vx_unlock(vp);
309                                         vrele(vp);      /* s_ttyvp ref */
310                                 }
311                         }
312                         /*
313                          * Release the tty.  If someone has it open via
314                          * /dev/tty then close it (since they no longer can
315                          * once we've NULL'd it out).
316                          */
317                         if (sp->s_ttyvp)
318                                 ttyclosesession(sp, 1);
319                         /*
320                          * s_ttyp is not zero'd; we use this to indicate
321                          * that the session once had a controlling terminal.
322                          * (for logging and informational purposes)
323                          */
324                 }
325                 sp->s_leader = NULL;
326         }
327         fixjobc(p, p->p_pgrp, 0);
328         (void)acct_process(p);
329 #ifdef KTRACE
330         /*
331          * release trace file
332          */
333         if (p->p_tracenode)
334                 ktrdestroy(&p->p_tracenode);
335         p->p_traceflag = 0;
336 #endif
337         /*
338          * Release reference to text vnode
339          */
340         if ((vtmp = p->p_textvp) != NULL) {
341                 p->p_textvp = NULL;
342                 vrele(vtmp);
343         }
344
345         /*
346          * Move the process to the zombie list.  This will block
347          * until the process p_lock count reaches 0.  The process will
348          * not be reaped until TDF_EXITING is set by cpu_thread_exit(),
349          * which is called from cpu_proc_exit().
350          */
351         proc_move_allproc_zombie(p);
352
353         q = LIST_FIRST(&p->p_children);
354         if (q)          /* only need this if any child is S_ZOMB */
355                 wakeup((caddr_t) initproc);
356         for (; q != 0; q = nq) {
357                 nq = LIST_NEXT(q, p_sibling);
358                 LIST_REMOVE(q, p_sibling);
359                 LIST_INSERT_HEAD(&initproc->p_children, q, p_sibling);
360                 q->p_pptr = initproc;
361                 q->p_sigparent = SIGCHLD;
362                 /*
363                  * Traced processes are killed
364                  * since their existence means someone is screwing up.
365                  */
366                 if (q->p_flag & P_TRACED) {
367                         q->p_flag &= ~P_TRACED;
368                         ksignal(q, SIGKILL);
369                 }
370         }
371
372         /*
373          * Save exit status and final rusage info, adding in child rusage
374          * info and self times.
375          */
376         p->p_xstat = rv;
377         calcru_proc(p, &p->p_ru);
378         ruadd(&p->p_ru, &p->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, notify process 1 instead (and hope it will handle
388          * this situation).
389          */
390         if (p->p_pptr->p_procsig->ps_flag & PS_NOCLDWAIT) {
391                 struct proc *pp = p->p_pptr;
392                 proc_reparent(p, initproc);
393                 /*
394                  * If this was the last child of our parent, notify
395                  * parent, so in case he was wait(2)ing, he will
396                  * continue.
397                  */
398                 if (LIST_EMPTY(&pp->p_children))
399                         wakeup((caddr_t)pp);
400         }
401
402         if (p->p_sigparent && p->p_pptr != initproc) {
403                 ksignal(p->p_pptr, p->p_sigparent);
404         } else {
405                 ksignal(p->p_pptr, SIGCHLD);
406         }
407
408         wakeup((caddr_t)p->p_pptr);
409         /*
410          * cpu_exit is responsible for clearing curproc, since
411          * it is heavily integrated with the thread/switching sequence.
412          *
413          * Other substructures are freed from wait().
414          */
415         plimit_free(&p->p_limit);
416
417         /*
418          * Release the current user process designation on the process so
419          * the userland scheduler can work in someone else.
420          */
421         p->p_usched->release_curproc(lp);
422
423         /*
424          * Finally, call machine-dependent code to release the remaining
425          * resources including address space, the kernel stack and pcb.
426          * The address space is released by "vmspace_free(p->p_vmspace)";
427          * This is machine-dependent, as we may have to change stacks
428          * or ensure that the current one isn't reallocated before we
429          * finish.  cpu_exit will end with a call to cpu_switch(), finishing
430          * our execution (pun intended).
431          */
432         lwp_exit();
433 }
434
435 void
436 lwp_exit(void)
437 {
438         struct lwp *lp = curthread->td_lwp;
439         struct proc *p = lp->lwp_proc;
440
441         /*
442          * Nobody actually wakes us when the lock
443          * count reaches zero, so just wait one tick.
444          */
445         while (lp->lwp_lock > 0)
446                 tsleep(lp, 0, "lwpexit", 1);
447
448         /* Hand down resource usage to our proc */
449         ruadd(&p->p_ru, &lp->lwp_ru);
450
451         --p->p_nthreads;
452         LIST_REMOVE(lp, lwp_list);
453         wakeup(&p->p_nthreads);
454         LIST_INSERT_HEAD(&deadlwp_list[mycpuid], lp, lwp_list);
455         taskqueue_enqueue(taskqueue_thread[mycpuid], deadlwp_task[mycpuid]);
456         cpu_lwp_exit();
457 }
458
459 /*
460  * Wait until a lwp is completely dead.
461  *
462  * If the thread is still executing, which can't be waited upon,
463  * return failure.  The caller is responsible of waiting a little
464  * bit and checking again.
465  *
466  * Suggested use:
467  * while (!lwp_wait(lp))
468  *      tsleep(lp, 0, "lwpwait", 1);
469  */
470 static int
471 lwp_wait(struct lwp *lp)
472 {
473         struct thread *td = lp->lwp_thread;;
474
475         KKASSERT(lwkt_preempted_proc() != lp);
476
477         while (lp->lwp_lock > 0)
478                 tsleep(lp, 0, "lwpwait1", 1);
479
480         lwkt_wait_free(td);
481
482         /*
483          * The lwp's thread may still be in the middle
484          * of switching away, we can't rip its stack out from
485          * under it until TDF_EXITING is set and both
486          * TDF_RUNNING and TDF_PREEMPT_LOCK are clear.
487          * TDF_PREEMPT_LOCK must be checked because TDF_RUNNING
488          * will be cleared temporarily if a thread gets
489          * preempted.
490          *
491          * YYY no wakeup occurs, so we simply return failure
492          * and let the caller deal with sleeping and calling
493          * us again.
494          */
495         if ((td->td_flags & (TDF_RUNNING|TDF_PREEMPT_LOCK|TDF_EXITING)) !=
496             TDF_EXITING)
497                 return (0);
498
499         return (1);
500 }
501
502 /*
503  * Release the resources associated with a lwp.
504  * The lwp must be completely dead.
505  */
506 void
507 lwp_dispose(struct lwp *lp)
508 {
509         struct thread *td = lp->lwp_thread;;
510
511         KKASSERT(lwkt_preempted_proc() != lp);
512         KKASSERT(td->td_refs == 0);
513         KKASSERT((td->td_flags & (TDF_RUNNING|TDF_PREEMPT_LOCK|TDF_EXITING)) ==
514                  TDF_EXITING);
515
516         if (td != NULL) {
517                 td->td_proc = NULL;
518                 td->td_lwp = NULL;
519                 lp->lwp_thread = NULL;
520                 lwkt_free_thread(td);
521         }
522         zfree(lwp_zone, lp);
523 }
524
525 int
526 sys_wait4(struct wait_args *uap)
527 {
528         struct rusage rusage;
529         int error, status;
530
531         error = kern_wait(uap->pid, uap->status ? &status : NULL,
532             uap->options, uap->rusage ? &rusage : NULL, &uap->sysmsg_fds[0]);
533
534         if (error == 0 && uap->status)
535                 error = copyout(&status, uap->status, sizeof(*uap->status));
536         if (error == 0 && uap->rusage)
537                 error = copyout(&rusage, uap->rusage, sizeof(*uap->rusage));
538         return (error);
539 }
540
541 /*
542  * wait1()
543  *
544  * wait_args(int pid, int *status, int options, struct rusage *rusage)
545  */
546 int
547 kern_wait(pid_t pid, int *status, int options, struct rusage *rusage, int *res)
548 {
549         struct thread *td = curthread;
550         struct proc *q = td->td_proc;
551         struct proc *p, *t;
552         int nfound, error;
553
554         if (pid == 0)
555                 pid = -q->p_pgid;
556         if (options &~ (WUNTRACED|WNOHANG|WLINUXCLONE))
557                 return (EINVAL);
558 loop:
559         /*
560          * Hack for backwards compatibility with badly written user code.  
561          * Or perhaps we have to do this anyway, it is unclear. XXX
562          *
563          * The problem is that if a process group is stopped and the parent
564          * is doing a wait*(..., WUNTRACED, ...), it will see the STOP
565          * of the child and then stop itself when it tries to return from the
566          * system call.  When the process group is resumed the parent will
567          * then get the STOP status even though the child has now resumed
568          * (a followup wait*() will get the CONT status).
569          *
570          * Previously the CONT would overwrite the STOP because the tstop
571          * was handled within tsleep(), and the parent would only see
572          * the CONT when both are stopped and continued together.  This litte
573          * two-line hack restores this effect.
574          */
575         while (q->p_stat == SSTOP)
576             tstop();
577
578         nfound = 0;
579         LIST_FOREACH(p, &q->p_children, p_sibling) {
580                 if (pid != WAIT_ANY &&
581                     p->p_pid != pid && p->p_pgid != -pid)
582                         continue;
583
584                 /* This special case handles a kthread spawned by linux_clone 
585                  * (see linux_misc.c).  The linux_wait4 and linux_waitpid 
586                  * functions need to be able to distinguish between waiting
587                  * on a process and waiting on a thread.  It is a thread if
588                  * p_sigparent is not SIGCHLD, and the WLINUXCLONE option
589                  * signifies we want to wait for threads and not processes.
590                  */
591                 if ((p->p_sigparent != SIGCHLD) ^ 
592                     ((options & WLINUXCLONE) != 0)) {
593                         continue;
594                 }
595
596                 nfound++;
597                 if (p->p_stat == SZOMB) {
598                         /*
599                          * Other kernel threads may be in the middle of 
600                          * accessing the proc.  For example, kern/kern_proc.c
601                          * could be blocked writing proc data to a sysctl.
602                          * At the moment, if this occurs, we are not woken
603                          * up and rely on a one-second retry.
604                          */
605                         while (p->p_lock)
606                                 tsleep(p, 0, "reap3", hz);
607
608                         /* scheduling hook for heuristic */
609                         /* XXX no lwp available, we need a different heuristic */
610                         /*
611                         p->p_usched->heuristic_exiting(td->td_lwp, deadlp);
612                         */
613
614                         /* Take care of our return values. */
615                         *res = p->p_pid;
616                         if (status)
617                                 *status = p->p_xstat;
618                         if (rusage)
619                                 *rusage = p->p_ru;
620                         /*
621                          * If we got the child via a ptrace 'attach',
622                          * we need to give it back to the old parent.
623                          */
624                         if (p->p_oppid && (t = pfind(p->p_oppid))) {
625                                 p->p_oppid = 0;
626                                 proc_reparent(p, t);
627                                 ksignal(t, SIGCHLD);
628                                 wakeup((caddr_t)t);
629                                 return (0);
630                         }
631                         p->p_xstat = 0;
632                         ruadd(&q->p_cru, &p->p_ru);
633
634                         /*
635                          * Decrement the count of procs running with this uid.
636                          */
637                         chgproccnt(p->p_ucred->cr_ruidinfo, -1, 0);
638
639                         /*
640                          * Free up credentials.
641                          */
642                         crfree(p->p_ucred);
643                         p->p_ucred = NULL;
644
645                         /*
646                          * Remove unused arguments
647                          */
648                         if (p->p_args && --p->p_args->ar_ref == 0)
649                                 FREE(p->p_args, M_PARGS);
650
651                         /*
652                          * Finally finished with old proc entry.
653                          * Unlink it from its process group and free it.
654                          */
655                         proc_remove_zombie(p);
656                         leavepgrp(p);
657
658                         if (--p->p_procsig->ps_refcnt == 0) {
659                                 if (p->p_sigacts != &p->p_addr->u_sigacts)
660                                         FREE(p->p_sigacts, M_SUBPROC);
661                                 FREE(p->p_procsig, M_SUBPROC);
662                                 p->p_procsig = NULL;
663                         }
664
665                         vm_waitproc(p);
666                         zfree(proc_zone, p);
667                         nprocs--;
668                         return (0);
669                 }
670                 if (p->p_stat == SSTOP && (p->p_flag & P_WAITED) == 0 &&
671                     (p->p_flag & P_TRACED || options & WUNTRACED)) {
672                         p->p_flag |= P_WAITED;
673
674                         *res = p->p_pid;
675                         if (status)
676                                 *status = W_STOPCODE(p->p_xstat);
677                         /* Zero rusage so we get something consistent. */
678                         if (rusage)
679                                 bzero(rusage, sizeof(rusage));
680                         return (0);
681                 }
682         }
683         if (nfound == 0)
684                 return (ECHILD);
685         if (options & WNOHANG) {
686                 *res = 0;
687                 return (0);
688         }
689         error = tsleep((caddr_t)q, PCATCH, "wait", 0);
690         if (error)
691                 return (error);
692         goto loop;
693 }
694
695 /*
696  * make process 'parent' the new parent of process 'child'.
697  */
698 void
699 proc_reparent(struct proc *child, struct proc *parent)
700 {
701
702         if (child->p_pptr == parent)
703                 return;
704
705         LIST_REMOVE(child, p_sibling);
706         LIST_INSERT_HEAD(&parent->p_children, child, p_sibling);
707         child->p_pptr = parent;
708 }
709
710 /*
711  * The next two functions are to handle adding/deleting items on the
712  * exit callout list
713  * 
714  * at_exit():
715  * Take the arguments given and put them onto the exit callout list,
716  * However first make sure that it's not already there.
717  * returns 0 on success.
718  */
719
720 int
721 at_exit(exitlist_fn function)
722 {
723         struct exitlist *ep;
724
725 #ifdef INVARIANTS
726         /* Be noisy if the programmer has lost track of things */
727         if (rm_at_exit(function)) 
728                 kprintf("WARNING: exit callout entry (%p) already present\n",
729                     function);
730 #endif
731         ep = kmalloc(sizeof(*ep), M_ATEXIT, M_NOWAIT);
732         if (ep == NULL)
733                 return (ENOMEM);
734         ep->function = function;
735         TAILQ_INSERT_TAIL(&exit_list, ep, next);
736         return (0);
737 }
738
739 /*
740  * Scan the exit callout list for the given item and remove it.
741  * Returns the number of items removed (0 or 1)
742  */
743 int
744 rm_at_exit(exitlist_fn function)
745 {
746         struct exitlist *ep;
747
748         TAILQ_FOREACH(ep, &exit_list, next) {
749                 if (ep->function == function) {
750                         TAILQ_REMOVE(&exit_list, ep, next);
751                         kfree(ep, M_ATEXIT);
752                         return(1);
753                 }
754         }       
755         return (0);
756 }
757
758 void
759 check_sigacts(void)
760 {
761         struct proc *p = curproc;
762         struct sigacts *pss;
763
764         if (p->p_procsig->ps_refcnt == 1 &&
765             p->p_sigacts != &p->p_addr->u_sigacts) {
766                 pss = p->p_sigacts;
767                 crit_enter();
768                 p->p_addr->u_sigacts = *pss;
769                 p->p_sigacts = &p->p_addr->u_sigacts;
770                 crit_exit();
771                 FREE(pss, M_SUBPROC);
772         }
773 }
774
775
776 /*
777  * LWP reaper related code.
778  */
779
780 static void
781 reaplwps(void *context, int dummy)
782 {
783         struct lwplist *lwplist = context;
784         struct lwp *lp;
785
786         while ((lp = LIST_FIRST(lwplist))) {
787                 if (!lwp_wait(lp))
788                         tsleep(lp, 0, "lwpreap", 1);
789                 LIST_REMOVE(lp, lwp_list);
790                 lwp_dispose(lp);
791         }
792 }
793
794 static void
795 deadlwp_init(void)
796 {
797         int cpu;
798
799         for (cpu = 0; cpu < ncpus; cpu++) {
800                 LIST_INIT(&deadlwp_list[cpu]);
801                 deadlwp_task[cpu] = kmalloc(sizeof(*deadlwp_task[cpu]), M_DEVBUF, M_WAITOK);
802                 TASK_INIT(deadlwp_task[cpu], 0, reaplwps, &deadlwp_list[cpu]);
803         }
804 }
805
806 SYSINIT(deadlwpinit, SI_SUB_CONFIGURE, SI_ORDER_ANY, deadlwp_init, NULL);