]> git.saurik.com Git - apple/xnu.git/blobdiff - bsd/kern/kern_exit.c
xnu-792.6.22.tar.gz
[apple/xnu.git] / bsd / kern / kern_exit.c
index 3f6aa72361a9680a301d9e8d1f2036df2fe9d2b1..3cc43dcceb8d201557e5f2b115073409f958e9a2 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2000-2001 Apple Computer, Inc. All rights reserved.
+ * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
  *
  * @APPLE_LICENSE_HEADER_START@
  * 
 #include <sys/param.h>
 #include <sys/systm.h>
 #include <sys/ioctl.h>
-#include <sys/proc.h>
+#include <sys/proc_internal.h>
+#include <sys/kauth.h>
 #include <sys/tty.h>
 #include <sys/time.h>
 #include <sys/resource.h>
 #include <sys/kernel.h>
-#include <sys/buf.h>
 #include <sys/wait.h>
-#include <sys/file.h>
-#include <sys/vnode.h>
+#include <sys/file_internal.h>
+#include <sys/vnode_internal.h>
 #include <sys/syslog.h>
 #include <sys/malloc.h>
 #include <sys/resourcevar.h>
 #include <sys/ptrace.h>
 #include <sys/user.h>
+#include <sys/aio_kern.h>
+#include <sys/sysproto.h>
+#include <sys/signalvar.h>
+#include <sys/filedesc.h>      /* fdfree */
+#include <sys/shm_internal.h>  /* shmexit */
+#include <sys/acct.h>          /* acct_process */
+#include <machine/spl.h>
+
+#include <bsm/audit_kernel.h>
+#include <bsm/audit_kevents.h>
 
 #include <mach/mach_types.h>
+
+#include <kern/kern_types.h>
+#include <kern/kalloc.h>
+#include <kern/task.h>
 #include <kern/thread.h>
-#include <kern/thread_act.h>
 #include <kern/sched_prim.h>
 #include <kern/assert.h>
 #if KTRACE   
 #include <sys/ktrace.h>
 #endif
 
+#include <mach/mach_types.h>
+#include <mach/task.h>
+#include <mach/thread_act.h>
+#include <mach/mach_traps.h>   /* init_process */
+
 extern char init_task_failure_data[];
-int exit1 __P((struct proc *, int, int *));
+int exit1(struct proc *, int, int *);
+void proc_prepareexit(struct proc *p);
+void vfork_exit(struct proc *p, int rv);
+void vproc_exit(struct proc *p);
+__private_extern__ void munge_rusage(struct rusage *a_rusage_p, struct user_rusage *a_user_rusage_p);
+
+/*
+ * Things which should have prototypes in headers, but don't
+ */
+void   unix_syscall_return(int);
+void   *get_bsduthreadarg(thread_t);
+void   proc_exit(struct proc *p);
+int    wait1continue(int result);
+int    waitidcontinue(int result);
+int    *get_bsduthreadrval(thread_t);
+kern_return_t  sys_perf_notify(struct task *task, exception_data_t code,
+                       mach_msg_type_number_t codeCnt);
+
+/*
+ * NOTE: Source and target may *NOT* overlap!
+ * XXX Should share code with bsd/dev/ppc/unix_signal.c
+ */
+static void
+siginfo_64to32(user_siginfo_t *in, siginfo_t *out)
+{
+       out->si_signo   = in->si_signo;
+       out->si_errno   = in->si_errno;
+       out->si_code    = in->si_code;
+       out->si_pid     = in->si_pid;
+       out->si_uid     = in->si_uid;
+       out->si_status  = in->si_status;
+       out->si_addr    = CAST_DOWN(void *,in->si_addr);
+       /* following cast works for sival_int because of padding */
+       out->si_value.sival_ptr = CAST_DOWN(void *,in->si_value.sival_ptr);
+       out->si_band    = in->si_band;                  /* range reduction */
+       out->pad[0]     = in->pad[0];                   /* mcontext.ss.r1 */
+}
 
 /*
  * exit --
  *     Death of process.
  */
-struct exit_args {
-       int     rval;
-};
 void
-exit(p, uap, retval)
-       struct proc *p;
-       struct exit_args *uap;
-       int *retval;
+exit(struct proc *p, struct exit_args *uap, int *retval)
 {
        exit1(p, W_EXITCODE(uap->rval, 0), retval);
 
@@ -125,16 +173,11 @@ exit(p, uap, retval)
  * status and rusage for wait().  Check for child processes and orphan them.
  */
 int
-exit1(p, rv, retval)
-       register struct proc *p;
-       int rv;
-       int * retval;
+exit1(struct proc *p, int rv, int *retval)
 {
-       register struct proc *q, *nq;
        thread_t self = current_thread();
-       thread_act_t th_act_self = current_act();
        struct task *task = p->task;
-       register int i,s;
+       register int s;
        struct uthread *ut;
 
        /*
@@ -143,22 +186,23 @@ exit1(p, rv, retval)
         * right here.
         */
 
-        ut = get_bsdthread_info(th_act_self);
-        if (ut->uu_flag & P_VFORK) {
-                       (void)vfork_exit(p, rv);
-                       vfork_return(th_act_self, p->p_pptr, p , retval);
+        ut = get_bsdthread_info(self);
+        if (ut->uu_flag & UT_VFORK) {
+                       vfork_exit(p, rv);
+                       vfork_return(self, p->p_pptr, p , retval);
                        unix_syscall_return(0);
                        /* NOT REACHED */
         }
+       AUDIT_SYSCALL_EXIT(0, p, ut); /* Exit is always successfull */
         signal_lock(p);
        while (p->exit_thread != self) {
                if (sig_try_locked(p) <= 0) {
-                       if (get_threadtask(th_act_self) != task) {
+                       if (get_threadtask(self) != task) {
                                 signal_unlock(p);
                                return(0);
                         }
                        signal_unlock(p);
-                       thread_terminate(th_act_self);
+                       thread_terminate(self);
                        thread_funnel_set(kernel_flock, FALSE);
                        thread_exception_return();
                        /* NOTREACHED */
@@ -176,37 +220,26 @@ exit1(p, rv, retval)
        s = splsched();
        p->p_flag |= P_WEXIT;
        splx(s);
-       (void)proc_prepareexit(p);
+       proc_prepareexit(p);
        p->p_xstat = rv;
 
        /* task terminate will call proc_terminate and that cleans it up */
        task_terminate_internal(task);
 
-       /*
-        * we come back and returns to AST which 
-        * should cleanup the rest 
-        */
-#if 0
-       if (task == current_task()) {
-               thread_exception_return();
-               /*NOTREACHED*/
-       }
-
-       while (task == current_task()) {
-               thread_terminate_self();
-               /*NOTREACHED*/
-       }
-#endif
        return(0);
 }
 
 void
 proc_prepareexit(struct proc *p) 
 {
-       int s;
        struct uthread *ut;
+       exception_data_t        code[EXCEPTION_CODE_MAX];
        thread_t self = current_thread();
-       thread_act_t th_act_self = current_act();
+
+       code[0] = (exception_data_t)0xFF000001;         /* Set terminate code */
+       code[1] = (exception_data_t)p->p_pid;           /* Pass out the pid */
+       /* Notify the perf server */
+       (void)sys_perf_notify(p->task, (exception_data_t)&code, 2);
 
        /*
         * Remove proc from allproc queue and from pidhash chain.
@@ -215,6 +248,7 @@ proc_prepareexit(struct proc *p)
         * in partially cleaned state.
         */
        LIST_REMOVE(p, p_list);
+       LIST_INSERT_HEAD(&zombproc, p, p_list); /* Place onto zombproc. */
        LIST_REMOVE(p, p_hash);
 
 #ifdef PGINPROF
@@ -225,9 +259,9 @@ proc_prepareexit(struct proc *p)
         * P_PPWAIT is set; we will wakeup the parent below.
         */
        p->p_flag &= ~(P_TRACED | P_PPWAIT);
-       p->p_sigignore = ~0;
+       p->p_sigignore = ~(sigcantmask);
        p->p_siglist = 0;
-       ut = get_bsdthread_info(th_act_self);
+       ut = get_bsdthread_info(self);
        ut->uu_siglist = 0;
        untimeout(realitexpire, (caddr_t)p->p_pid);
 }
@@ -236,10 +270,8 @@ void
 proc_exit(struct proc *p)
 {
        register struct proc *q, *nq, *pp;
-       thread_t self = current_thread();
-       thread_act_t th_act_self = current_act();
        struct task *task = p->task;
-       register int i,s;
+       register int s;
        boolean_t funnel_state;
 
        /* This can happen if thread_terminate of the single thread
@@ -254,9 +286,17 @@ proc_exit(struct proc *p)
                proc_prepareexit(p);    
        }
 
+       p->p_lflag |= P_LPEXIT;
+       /* XXX Zombie allocation may fail, in which case stats get lost */
        MALLOC_ZONE(p->p_ru, struct rusage *,
                        sizeof (*p->p_ru), M_ZOMBIE, M_WAITOK);
 
+       /*
+        * need to cancel async IO requests that can be cancelled and wait for those
+        * already active.  MAY BLOCK!
+        */
+       _aio_exit( p );
+
        /*
         * Close open files and release open-file table.
         * This may block!
@@ -274,6 +314,7 @@ proc_exit(struct proc *p)
 
                if (sp->s_ttyvp) {
                        struct vnode *ttyvp;
+                       struct vfs_context context;
 
                        /*
                         * Controlling process.
@@ -289,13 +330,16 @@ proc_exit(struct proc *p)
                                 * The tty could have been revoked
                                 * if we blocked.
                                 */
+                               context.vc_proc = p;
+                               context.vc_ucred = p->p_ucred;
                                if (sp->s_ttyvp)
-                                       VOP_REVOKE(sp->s_ttyvp, REVOKEALL);
+                                       VNOP_REVOKE(sp->s_ttyvp, REVOKEALL, &context);
                        }
                        ttyvp = sp->s_ttyvp;
                        sp->s_ttyvp = NULL;
-                       if (ttyvp)
-                               vrele(ttyvp);
+                       if (ttyvp) {
+                               vnode_rele(ttyvp);
+                       }
                        /*
                         * s_ttyp is not zero'd; we use this to indicate
                         * that the session once had a controlling terminal.
@@ -313,19 +357,15 @@ proc_exit(struct proc *p)
        /* 
         * release trace file
         */
-       p->p_traceflag = 0;     /* don't trace the vrele() */
+       p->p_traceflag = 0;     /* don't trace the vnode_put() */
        if (p->p_tracep) {
                struct vnode *tvp = p->p_tracep;
                p->p_tracep = NULL;
-               vrele(tvp);
+               vnode_rele(tvp);
        }
 #endif
 
-       q = p->p_children.lh_first;
-       if (q)          /* only need this if any child is S_ZOMB */
-               wakeup((caddr_t) initproc);
-       for (; q != 0; q = nq) {
-               nq = q->p_sibling.le_next;
+       while (q = p->p_children.lh_first) {
                proc_reparent(q, initproc);
                /*
                 * Traced processes are killed
@@ -334,9 +374,6 @@ proc_exit(struct proc *p)
                if (q->p_flag & P_TRACED) {
                        q->p_flag &= ~P_TRACED;
                        if (q->sigwait_thread) {
-                               thread_t sig_shuttle;
-
-                               sig_shuttle = (thread_t)getshuttle_thread((thread_act_t)q->sigwait_thread);
                                /*
                                 * The sigwait_thread could be stopped at a
                                 * breakpoint. Wake it up to kill.
@@ -344,9 +381,9 @@ proc_exit(struct proc *p)
                                 * the first thread in the task. So any attempts to kill
                                 * the process would result into a deadlock on q->sigwait.
                                 */
-                               thread_resume((thread_act_t)q->sigwait_thread);
-                               clear_wait(sig_shuttle, THREAD_INTERRUPTED);
-                               threadsignal((thread_act_t)q->sigwait_thread, SIGKILL, 0);
+                               thread_resume((thread_t)q->sigwait_thread);
+                               clear_wait(q->sigwait_thread, THREAD_INTERRUPTED);
+                               threadsignal((thread_t)q->sigwait_thread, SIGKILL, 0);
                        }
                        psignal(q, SIGKILL);
                }
@@ -354,14 +391,16 @@ proc_exit(struct proc *p)
 
        /*
         * Save exit status and final rusage info, adding in child rusage
-        * info and self times.
+        * info and self times.  If we were unable to allocate a zombie
+        * structure, this information is lost.
         */
-       *p->p_ru = p->p_stats->p_ru;
+       if (p->p_ru != NULL) {
+           *p->p_ru = p->p_stats->p_ru;
 
-       timerclear(&p->p_ru->ru_utime);
-       timerclear(&p->p_ru->ru_stime);
+           timerclear(&p->p_ru->ru_utime);
+           timerclear(&p->p_ru->ru_stime);
 
-       if (task) {
+           if (task) {
                task_basic_info_data_t tinfo;
                task_thread_times_info_data_t ttimesinfo;
                int task_info_stuff, task_ttimes_stuff;
@@ -369,7 +408,7 @@ proc_exit(struct proc *p)
 
                task_info_stuff = TASK_BASIC_INFO_COUNT;
                task_info(task, TASK_BASIC_INFO,
-                         &tinfo, &task_info_stuff);
+                         (task_info_t)&tinfo, &task_info_stuff);
                p->p_ru->ru_utime.tv_sec = tinfo.user_time.seconds;
                p->p_ru->ru_utime.tv_usec = tinfo.user_time.microseconds;
                p->p_ru->ru_stime.tv_sec = tinfo.system_time.seconds;
@@ -377,7 +416,7 @@ proc_exit(struct proc *p)
 
                task_ttimes_stuff = TASK_THREAD_TIMES_INFO_COUNT;
                task_info(task, TASK_THREAD_TIMES_INFO,
-                         &ttimesinfo, &task_ttimes_stuff);
+                         (task_info_t)&ttimesinfo, &task_ttimes_stuff);
 
                ut.tv_sec = ttimesinfo.user_time.seconds;
                ut.tv_usec = ttimesinfo.user_time.microseconds;
@@ -385,9 +424,10 @@ proc_exit(struct proc *p)
                st.tv_usec = ttimesinfo.system_time.microseconds;
                timeradd(&ut,&p->p_ru->ru_utime,&p->p_ru->ru_utime);
                timeradd(&st,&p->p_ru->ru_stime,&p->p_ru->ru_stime);
-       }
+           }
 
-       ruadd(p->p_ru, &p->p_stats->p_cru);
+           ruadd(p->p_ru, &p->p_stats->p_cru);
+       }
 
        /*
         * Free up profiling buffers.
@@ -401,7 +441,7 @@ proc_exit(struct proc *p)
 
                for (; p1 != NULL; p1 = pn) {
                        pn = p1->pr_next;
-                       kfree((vm_offset_t)p1, sizeof *p1);
+                       kfree(p1, sizeof *p1);
                }
        }
 
@@ -427,16 +467,26 @@ proc_exit(struct proc *p)
        //task->proc = NULL;
        set_bsdtask_info(task, NULL);
 
+       KNOTE(&p->p_klist, NOTE_EXIT);
+
        /*
         * Notify parent that we're gone.
         */
        if (p->p_pptr->p_flag & P_NOCLDWAIT) {
-               struct proc * pp = p->p_pptr;
+               struct proc *opp = p->p_pptr;
+
+               /*
+                * Add child resource usage to parent before giving
+                * zombie to init.  If we were unable to allocate a
+                * zombie structure, this information is lost.
+                */
+               if (p->p_ru != NULL)
+                       ruadd(&p->p_pptr->p_stats->p_cru, p->p_ru);
 
                proc_reparent(p, initproc);
                /* If there are no more children wakeup parent */
-               if (LIST_EMPTY(&pp->p_children))
-                       wakeup((caddr_t)pp);
+               if (LIST_EMPTY(&opp->p_children))
+                       wakeup((caddr_t)opp);
        }
        /* should be fine as parent proc would be initproc */
        pp = p->p_pptr;
@@ -444,15 +494,13 @@ proc_exit(struct proc *p)
                pp->si_pid = p->p_pid;
                pp->si_status = p->p_xstat;
                pp->si_code = CLD_EXITED;
-               pp->si_uid = p->p_cred->p_ruid;
+               pp->si_uid = p->p_ucred->cr_ruid;
        }
-       psignal(pp, SIGCHLD);
-
-
-       /* Place onto zombproc. */
-       LIST_INSERT_HEAD(&zombproc, p, p_list);
+       /* mark as a zombie */
        p->p_stat = SZOMB;
 
+       psignal(pp, SIGCHLD);
+
        /* and now wakeup the parent */
        wakeup((caddr_t)p->p_pptr);
 
@@ -460,73 +508,99 @@ proc_exit(struct proc *p)
 }
 
 
-struct wait4_args {
-       int     pid;
-       int *status;
-       int options;
-       struct rusage *rusage;
-};
-
-#if COMPAT_43
-int
-owait(p, uap, retval)
-       struct proc *p;
-       void *uap;
-       int *retval;
-{
-       struct wait4_args *a;
-
-       a = (struct wait4_args *)get_bsduthreadarg(current_act());
-
-       a->options = 0;
-       a->rusage = NULL;
-       a->pid = WAIT_ANY;
-       a->status = NULL;
-       return (wait1(p, a, retval, 1));
-}
-
-int
-wait4(p, uap, retval)
-       struct proc *p;
-       struct wait4_args *uap;
-       int *retval;
+/*
+ * reap_child_process
+ *
+ * Description:        Given a process from which all status information needed
+ *             has already been extracted, if the process is a ptrace
+ *             attach process, detach it and give it back to its real
+ *             parent, else recover all resources remaining associated
+ *             with it.
+ *
+ * Parameters: struct proc *parent     Parent of process being reaped
+ *             struct proc *child      Process to reap
+ *
+ * Returns:    0                       Process was not reaped because it
+ *                                     came from an attach
+ *             1                       Process was reaped
+ */
+static int
+reap_child_process(struct proc *parent, struct proc *child)
 {
-       return (wait1(p, uap, retval, 0));
-}
+       struct proc *trace_parent;      /* Traced parent process, if tracing */
+       struct vnode *tvp;              /* Traced vnode pointer, if used */
 
-struct owait3_args {
-       int *status;
-       int options;
-       struct rusage *rusage;
-};
-
-int
-owait3(p, uap, retval)
-       struct proc *p;
-       struct owait3_args *uap;
-       int *retval;
-{
-       struct wait4_args *a;
+       /*
+        * If we got the child via a ptrace 'attach',
+        * we need to give it back to the old parent.
+        */
+       if (child->p_oppid && (trace_parent = pfind(child->p_oppid))) {
+               child->p_oppid = 0;
+               proc_reparent(child, trace_parent);
+               if (trace_parent != initproc) {
+                       trace_parent->si_pid = child->p_pid;
+                       trace_parent->si_status = child->p_xstat;
+                       trace_parent->si_code = CLD_CONTINUED;
+                       trace_parent->si_uid = child->p_ucred->cr_ruid;
+               }
+               psignal(trace_parent, SIGCHLD);
+               wakeup((caddr_t)trace_parent);
+               return (0);
+       }
+       child->p_xstat = 0;
+       if (child->p_ru) {
+               ruadd(&parent->p_stats->p_cru, child->p_ru);
+               FREE_ZONE(child->p_ru, sizeof *child->p_ru, M_ZOMBIE);
+               child->p_ru = NULL;
+       } else {
+               printf("Warning : lost p_ru for %s\n", child->p_comm);
+       }
 
-       a = (struct wait4_args *)get_bsduthreadarg(current_act);
+       /*
+        * Decrement the count of procs running with this uid.
+        */
+       (void)chgproccnt(child->p_ucred->cr_ruid, -1);
 
-       a->rusage = uap->rusage;
-       a->options = uap->options;
-       a->status = uap->status;
-       a->pid = WAIT_ANY;
+       /*
+        * Free up credentials.
+        */
+       if (child->p_ucred != NOCRED) {
+               kauth_cred_t ucr = child->p_ucred;
+                       child->p_ucred = NOCRED;
+                       kauth_cred_rele(ucr);
+               }
 
-       return (wait1(p, a, retval, 1));
+       /*
+        * Release reference to text vnode
+        */
+       tvp = child->p_textvp;
+       child->p_textvp = NULL;
+       if (tvp) {
+               vnode_rele(tvp);
+       }
+       /*
+        * Finally finished with old proc entry.
+        * Unlink it from its process group and free it.
+        */
+       leavepgrp(child);
+       LIST_REMOVE(child, p_list);     /* off zombproc */
+       LIST_REMOVE(child, p_sibling);
+       child->p_lflag &= ~P_LWAITING;
+       wakeup(&child->p_stat);
+
+       lck_mtx_destroy(&child->p_mlock, proc_lck_grp);
+       lck_mtx_destroy(&child->p_fdmlock, proc_lck_grp);
+       FREE_ZONE(child, sizeof *child, M_PROC);
+       nprocs--;
+       return (1);
 }
 
-#else
-#define        wait1   wait4
-#endif
 
 int
-wait1continue(result)
+wait1continue(int result)
 {
        void *vt;
-       thread_act_t thread;
+       thread_t thread;
        int *retval;
        struct proc *p;
 
@@ -534,27 +608,19 @@ wait1continue(result)
                return(result);
 
        p = current_proc();
-       thread = current_act();
-       vt = (void *)get_bsduthreadarg(thread);
-       retval = (int *)get_bsduthreadrval(thread);
-       wait1((struct proc *)p, (struct wait4_args *)vt, retval, 0);
+       thread = current_thread();
+       vt = get_bsduthreadarg(thread);
+       retval = get_bsduthreadrval(thread);
+       return(wait4((struct proc *)p, (struct wait4_args *)vt, retval));
 }
 
 int
-wait1(q, uap, retval, compat)
-       register struct proc *q;
-       register struct wait4_args *uap;
-       register_t *retval;
-#if COMPAT_43
-       int compat;
-#endif
+wait4(struct proc *q, struct wait4_args *uap, register_t *retval)
 {
        register int nfound;
-       register struct proc *p, *t;
+       register struct proc *p;
        int status, error;
-       struct vnode *tvp;
 
-retry:
        if (uap->pid == 0)
                uap->pid = -q->p_pgid;
 
@@ -566,131 +632,333 @@ loop:
                    p->p_pgid != -(uap->pid))
                        continue;
                nfound++;
-               if (p->p_flag & P_WAITING) {
+
+               /* XXX This is racy because we don't get the lock!!!! */
+
+               if (p->p_lflag & P_LWAITING) {
                        (void)tsleep(&p->p_stat, PWAIT, "waitcoll", 0);
                        goto loop;
                }
-               p->p_flag |= P_WAITING;   /* only allow single thread to wait() */
+               p->p_lflag |= P_LWAITING;   /* only allow single thread to wait() */
 
                if (p->p_stat == SZOMB) {
                        retval[0] = p->p_pid;
-#if COMPAT_43
-                       if (compat)
-                               retval[1] = p->p_xstat;
-                       else
-#endif
                        if (uap->status) {
                                status = p->p_xstat;    /* convert to int */
-                               if (error = copyout((caddr_t)&status,
-                                   (caddr_t)uap->status,
-                                                   sizeof(status))) {
-                                       p->p_flag &= ~P_WAITING;
+                               error = copyout((caddr_t)&status,
+                                                       uap->status,
+                                                   sizeof(status));
+                               if (error) {
+                                       p->p_lflag &= ~P_LWAITING;
                                        wakeup(&p->p_stat);
                                        return (error);
                                }
                        }
-                       if (uap->rusage &&
-                           (error = copyout((caddr_t)p->p_ru,
-                           (caddr_t)uap->rusage,
-                                            sizeof (struct rusage)))) {
-                               p->p_flag &= ~P_WAITING;
-                               wakeup(&p->p_stat);
-                               return (error);
-                       }
-                       /*
-                        * If we got the child via a ptrace 'attach',
-                        * we need to give it back to the old parent.
-                        */
-                       if (p->p_oppid && (t = pfind(p->p_oppid))) {
-                               p->p_oppid = 0;
-                               proc_reparent(p, t);
-                               if (t != initproc) {
-                                       t->si_pid = p->p_pid;
-                                       t->si_status = p->p_xstat;
-                                       t->si_code = CLD_CONTINUED;
-                                       t->si_uid = p->p_cred->p_ruid;
+                       if (uap->rusage) {
+                               if (p->p_ru == NULL) {
+                                       error = ENOMEM;
+                               } else {
+                                       if (IS_64BIT_PROCESS(q)) {
+                                               struct user_rusage      my_rusage;
+                                               munge_rusage(p->p_ru, &my_rusage);
+                                               error = copyout((caddr_t)&my_rusage,
+                                                       uap->rusage,
+                                                       sizeof (my_rusage));
+                                       }
+                                       else {
+                                               error = copyout((caddr_t)p->p_ru,
+                                                       uap->rusage,
+                                                       sizeof (struct rusage));
+                                       }
                                }
-                               psignal(t, SIGCHLD);
-                               wakeup((caddr_t)t);
-                               p->p_flag &= ~P_WAITING;
-                               wakeup(&p->p_stat);
-                               return (0);
-                       }
-                       p->p_xstat = 0;
-                       if (p->p_ru) {
-                               ruadd(&q->p_stats->p_cru, p->p_ru);
-                               FREE_ZONE(p->p_ru, sizeof *p->p_ru, M_ZOMBIE);
-                               p->p_ru = NULL;
-                       } else {
-                               printf("Warning : lost p_ru for %s\n", p->p_comm);
-                       }
-
-                       /*
-                        * Decrement the count of procs running with this uid.
-                        */
-                       (void)chgproccnt(p->p_cred->p_ruid, -1);
-
-                       /*
-                        * Free up credentials.
-                        */
-                       if (--p->p_cred->p_refcnt == 0) {
-                               struct ucred *ucr = p->p_ucred;
-                               struct pcred *pcr;
-
-                               if (ucr != NOCRED) {
-                                       p->p_ucred = NOCRED;
-                                       crfree(ucr);
+                               /* information unavailable? */
+                               if (error) {
+                                       p->p_lflag &= ~P_LWAITING;
+                                       wakeup(&p->p_stat);
+                                       return (error);
                                }
-                               pcr = p->p_cred;
-                               p->p_cred = NULL;
-                               FREE_ZONE(pcr, sizeof *pcr, M_SUBPROC);
                        }
 
-                       /*
-                        * Release reference to text vnode
-                        */
-                       tvp = p->p_textvp;
-                       p->p_textvp = NULL;
-                       if (tvp)
-                               vrele(tvp);
+                       /* Clean up */
+                       if (!reap_child_process(q, p)) {
+                               p->p_lflag &= ~P_LWAITING;
+                               wakeup(&p->p_stat);
+                       }
 
-                       /*
-                        * Finally finished with old proc entry.
-                        * Unlink it from its process group and free it.
-                        */
-                       leavepgrp(p);
-                       LIST_REMOVE(p, p_list); /* off zombproc */
-                       LIST_REMOVE(p, p_sibling);
-                       p->p_flag &= ~P_WAITING;
-                       FREE_ZONE(p, sizeof *p, M_PROC);
-                       nprocs--;
-                       wakeup(&p->p_stat);
                        return (0);
                }
                if (p->p_stat == SSTOP && (p->p_flag & P_WAITED) == 0 &&
                    (p->p_flag & P_TRACED || uap->options & WUNTRACED)) {
                        p->p_flag |= P_WAITED;
                        retval[0] = p->p_pid;
-#if COMPAT_43
-                       if (compat) {
-                               retval[1] = W_STOPCODE(p->p_xstat);
-                               error = 0;
-                       } else
-#endif
                        if (uap->status) {
                                status = W_STOPCODE(p->p_xstat);
                                error = copyout((caddr_t)&status,
-                                   (caddr_t)uap->status,
+                                       uap->status,
                                    sizeof(status));
                        } else
                                error = 0;
-                       p->p_flag &= ~P_WAITING;
+                       p->p_lflag &= ~P_LWAITING;
                        wakeup(&p->p_stat);
                        return (error);
                }
-               p->p_flag &= ~P_WAITING;
+               p->p_lflag &= ~P_LWAITING;
+               wakeup(&p->p_stat);
+       }
+       if (nfound == 0)
+               return (ECHILD);
+
+       if (uap->options & WNOHANG) {
+               retval[0] = 0;
+               return (0);
+       }
+
+       if ((error = tsleep0((caddr_t)q, PWAIT | PCATCH, "wait", 0, wait1continue)))
+               return (error);
+
+       goto loop;
+}
+
+
+int
+waitidcontinue(int result)
+{
+       void *vt;
+       thread_t thread;
+       int *retval;
+       struct proc *p;
+
+       if (result)
+               return(result);
+
+       p = current_proc();
+       thread = current_thread();
+       vt = get_bsduthreadarg(thread);
+       retval = get_bsduthreadrval(thread);
+       return(waitid((struct proc *)p, (struct waitid_args *)vt, retval));
+}
+
+/*
+ * Description:        Suspend the calling thread until one child of the process
+ *             containing the calling thread changes state.
+ *
+ * Parameters: uap->idtype             one of P_PID, P_PGID, P_ALL
+ *             uap->id                 pid_t or gid_t or ignored
+ *             uap->infop              Address of signinfo_t struct in
+ *                                     user space into which to return status
+ *             uap->options            flag values
+ *
+ * Returns:    0                       Success
+ *             !0                      Error returning status to user space
+ */
+int
+waitid(struct proc *q, struct waitid_args *uap, register_t *retval)
+{
+       user_siginfo_t  collect64;      /* siginfo data to return to caller */
+
+       register int nfound;
+       register struct proc *p;
+       int error;
+
+loop:
+       nfound = 0;
+       for (p = q->p_children.lh_first; p != 0; p = p->p_sibling.le_next) {
+               switch(uap->idtype) {
+               case P_PID:     /* child with process ID equal to... */
+                       if (p->p_pid != (pid_t)uap->id)
+                               continue;
+                       break;
+               case P_PGID:    /* child with process group ID equal to... */
+                       if (p->p_pgid != (pid_t)uap->id)
+                               continue;
+                       break;
+               case P_ALL:     /* any child */
+                       break;
+               }
+
+               /* XXX This is racy because we don't get the lock!!!! */
+
+               /*
+                * Wait collision; go to sleep and restart; used to maintain
+                * the single return for waited process guarantee.
+                */
+               if (p->p_lflag & P_LWAITING) {
+                       (void)tsleep(&p->p_stat, PWAIT, "waitidcoll", 0);
+                       goto loop;
+               }
+               p->p_lflag |= P_LWAITING;               /* mark busy */
+
+               nfound++;
+
+               /*
+                * Types of processes we are interested in
+                *
+                * XXX Don't know what to do for WCONTINUED?!?
+                */
+               switch(p->p_stat) {
+               case SZOMB:             /* Exited */
+                       if (!(uap->options & WEXITED))
+                               break;
+
+                       /* Collect "siginfo" information for caller */
+                       collect64.si_signo = 0;
+                       collect64.si_code = 0;
+                       collect64.si_errno = 0;
+                       collect64.si_pid = 0;
+                       collect64.si_uid = 0;
+                       collect64.si_addr  = 0;
+                       collect64.si_status = p->p_xstat;
+                       collect64.si_band = 0;
+
+                       if (IS_64BIT_PROCESS(p)) {
+                               error = copyout((caddr_t)&collect64,
+                                       uap->infop,
+                                       sizeof(collect64));
+                       } else {
+                               siginfo_t collect;
+                               siginfo_64to32(&collect64,&collect);
+                               error = copyout((caddr_t)&collect,
+                                       uap->infop,
+                                       sizeof(collect));
+                       }
+                       /* information unavailable? */
+                       if (error) {
+                               p->p_lflag &= ~P_LWAITING;
+                               wakeup(&p->p_stat);
+                               return (error);
+                       }
+
+                       /* Prevent other process for waiting for this event? */
+                       if (!(uap->options & WNOWAIT)) {
+                               /* Clean up */
+                               if (!reap_child_process(q, p)) {
+                                       p->p_lflag &= ~P_LWAITING;
+                                       wakeup(&p->p_stat);
+                               }
+                       }
+
+                       return (0);
+
+               case SSTOP:             /* Stopped */
+                       /*
+                        * If we are not interested in stopped processes, then
+                        * ignore this one.
+                        */
+                       if (!(uap->options & WSTOPPED))
+                               break;
+
+                       /*
+                        * If someone has already waited it, we lost a race
+                        * to be the one to return status.
+                        */
+                       if ((p->p_flag & P_WAITED) != 0)
+                               break;
+
+                       /*
+                        * If this is not a traced process, and they haven't
+                        * indicated an interest in untraced processes, then
+                        * ignore this one.
+                        */
+                       if (!(p->p_flag & P_TRACED) && !(uap->options & WUNTRACED))
+                               break;
+
+                       /* Collect "siginfo" information for caller */
+                       collect64.si_signo = 0;
+                       collect64.si_code = 0;
+                       collect64.si_errno = 0;
+                       collect64.si_pid = 0;
+                       collect64.si_uid = 0;
+                       collect64.si_addr  = 0;
+                       collect64.si_status = p->p_xstat;
+                       collect64.si_band = 0;
+
+                       if (IS_64BIT_PROCESS(p)) {
+                               error = copyout((caddr_t)&collect64,
+                                       uap->infop,
+                                       sizeof(collect64));
+                       } else {
+                               siginfo_t collect;
+                               siginfo_64to32(&collect64,&collect);
+                               error = copyout((caddr_t)&collect,
+                                       uap->infop,
+                                       sizeof(collect));
+                       }
+                       /* information unavailable? */
+                       if (error) {
+                               p->p_lflag &= ~P_LWAITING;
+                               wakeup(&p->p_stat);
+                               return (error);
+                       }
+
+                       /* Prevent other process for waiting for this event? */
+                       if (!(uap->options & WNOWAIT)) {
+                               p->p_flag |= P_WAITED;
+                       }
+
+                       p->p_lflag &= ~P_LWAITING;
+                       wakeup(&p->p_stat);
+                       return (0);
+
+               default:                /* All others */
+                                       /* ...meaning Continued */
+                       if (!(uap->options & WCONTINUED))
+                               break;
+
+                       /*
+                        * If the flag isn't set, then this process has not
+                        * been stopped and continued, or the status has
+                        * already been reaped by another caller of waitid().
+                        */
+                       if ((p->p_flag & P_CONTINUED) == 0)
+                               break;
+
+                       /* Collect "siginfo" information for caller */
+                       collect64.si_signo = 0;
+                       collect64.si_code = 0;
+                       collect64.si_errno = 0;
+                       collect64.si_pid = 0;
+                       collect64.si_uid = 0;
+                       collect64.si_addr  = 0;
+                       collect64.si_status = p->p_xstat;
+                       collect64.si_band = 0;
+
+                       if (IS_64BIT_PROCESS(p)) {
+                               error = copyout((caddr_t)&collect64,
+                                       uap->infop,
+                                       sizeof(collect64));
+                       } else {
+                               siginfo_t collect;
+                               siginfo_64to32(&collect64,&collect);
+                               error = copyout((caddr_t)&collect,
+                                       uap->infop,
+                                       sizeof(collect));
+                       }
+                       /* information unavailable? */
+                       if (error) {
+                               p->p_lflag &= ~P_LWAITING;
+                               wakeup(&p->p_stat);
+                               return (error);
+                       }
+
+                       /* Prevent other process for waiting for this event? */
+                       if (!(uap->options & WNOWAIT)) {
+                               p->p_flag &= ~P_CONTINUED;
+                       }
+
+                       p->p_lflag &= ~P_LWAITING;
+                       wakeup(&p->p_stat);
+                       return (0);
+
+                       break;
+               }
+
+
+               /* Not a process we are interested in; go on to next child */
+               p->p_lflag &= ~P_LWAITING;
                wakeup(&p->p_stat);
        }
+
+       /* No child processes that could possibly satisfy the request? */
        if (nfound == 0)
                return (ECHILD);
 
@@ -699,7 +967,7 @@ loop:
                return (0);
        }
 
-       if (error = tsleep0((caddr_t)q, PWAIT | PCATCH, "wait", 0, wait1continue))
+       if ((error = tsleep0((caddr_t)q, PWAIT | PCATCH, "waitid", 0, waitidcontinue)))
                return (error);
 
        goto loop;
@@ -709,9 +977,7 @@ loop:
  * make process 'parent' the new parent of process 'child'.
  */
 void
-proc_reparent(child, parent)
-       register struct proc *child;
-       register struct proc *parent;
+proc_reparent(struct proc *child, struct proc *parent)
 {
 
        if (child->p_pptr == parent)
@@ -720,6 +986,9 @@ proc_reparent(child, parent)
        LIST_REMOVE(child, p_sibling);
        LIST_INSERT_HEAD(&parent->p_children, child, p_sibling);
        child->p_pptr = parent;
+
+       if (initproc == parent && child->p_stat == SZOMB)
+               psignal(initproc, SIGCHLD);
 }
 
 /*
@@ -728,12 +997,15 @@ proc_reparent(child, parent)
  *     gunned down by kill(-1, 0).
  */
 kern_return_t
-init_process(void)
+init_process(__unused struct init_process_args *args)
 {
        register struct proc *p = current_proc();
 
-       if (suser(p->p_ucred, &p->p_acflag))
+       AUDIT_MACH_SYSCALL_ENTER(AUE_INITPROCESS);
+       if (suser(kauth_cred_get(), &p->p_acflag)) {
+               AUDIT_MACH_SYSCALL_EXIT(KERN_NO_ACCESS);
                return(KERN_NO_ACCESS);
+       }
 
        if (p->p_pid != 1 && p->p_pgid != p->p_pid)
                enterpgrp(p, p->p_pid, 0);
@@ -748,19 +1020,10 @@ init_process(void)
        p->p_sibling.le_next = NULL;
        p->p_pptr = kernproc;
 
+       AUDIT_MACH_SYSCALL_EXIT(KERN_SUCCESS);
        return(KERN_SUCCESS);
 }
 
-void
-process_terminate_self(void)
-{
-       struct proc *p = current_proc();
-
-       if (p != NULL) {
-               exit1(p, W_EXITCODE(0, SIGKILL), (int *)NULL);
-               /*NOTREACHED*/
-       }
-}
 
 /*
  * Exit: deallocate address space and other resources, change proc state
@@ -769,16 +1032,15 @@ process_terminate_self(void)
  */
 
 void
-vfork_exit(p, rv)
-       struct proc *p;
-       int rv;
+vfork_exit(struct proc *p, int rv)
 {
-       register struct proc *q, *nq;
        thread_t self = current_thread();
-       thread_act_t th_act_self = current_act();
+#ifdef FIXME
        struct task *task = p->task;
-       register int i,s;
+#endif
+       register int s;
        struct uthread *ut;
+       exception_data_t        code[EXCEPTION_CODE_MAX];
 
        /*
         * If a thread in this task has already
@@ -786,17 +1048,17 @@ vfork_exit(p, rv)
         * right here.
         */
 
-        ut = get_bsdthread_info(th_act_self);
+        ut = get_bsdthread_info(self);
 #ifdef FIXME
         signal_lock(p);
        while (p->exit_thread != self) {
                if (sig_try_locked(p) <= 0) {
-                       if (get_threadtask(th_act_self) != task) {
+                       if (get_threadtask(self) != task) {
                                 signal_unlock(p);
                                return;
                         }
                        signal_unlock(p);
-                       thread_terminate(th_act_self);
+                       thread_terminate(self);
                        thread_funnel_set(kernel_flock, FALSE);
                        thread_exception_return();
                        /* NOTREACHED */
@@ -813,7 +1075,14 @@ panic("init died\nState at Last Exception:\n\n%s", init_task_failure_data);
 
        s = splsched();
        p->p_flag |= P_WEXIT;
+       p->p_lflag |= P_LPEXIT;
        splx(s);
+
+       code[0] = (exception_data_t)0xFF000001;         /* Set terminate code */
+       code[1] = (exception_data_t)p->p_pid;           /* Pass out the pid */
+       /* Notify the perf server */
+       (void)sys_perf_notify(p->task, (exception_data_t)&code, 2);
+
        /*
         * Remove proc from allproc queue and from pidhash chain.
         * Need to do this before we do anything that can block.
@@ -821,6 +1090,7 @@ panic("init died\nState at Last Exception:\n\n%s", init_task_failure_data);
         * in partially cleaned state.
         */
        LIST_REMOVE(p, p_list);
+       LIST_INSERT_HEAD(&zombproc, p, p_list); /* Place onto zombproc. */
        LIST_REMOVE(p, p_hash);
        /*
         * If parent is waiting for us to exit or exec,
@@ -835,19 +1105,18 @@ panic("init died\nState at Last Exception:\n\n%s", init_task_failure_data);
 
        p->p_xstat = rv;
 
-       (void)vproc_exit(p);
+       vproc_exit(p);
 }
 
 void 
 vproc_exit(struct proc *p)
 {
        register struct proc *q, *nq, *pp;
-       thread_t self = current_thread();
-       thread_act_t th_act_self = current_act();
+#ifdef FIXME
        struct task *task = p->task;
-       register int i,s;
-       boolean_t funnel_state;
+#endif
 
+       /* XXX Zombie allocation may fail, in which case stats get lost */
        MALLOC_ZONE(p->p_ru, struct rusage *,
                        sizeof (*p->p_ru), M_ZOMBIE, M_WAITOK);
 
@@ -862,6 +1131,7 @@ vproc_exit(struct proc *p)
 
                if (sp->s_ttyvp) {
                        struct vnode *ttyvp;
+                       struct vfs_context context;
 
                        /*
                         * Controlling process.
@@ -877,13 +1147,16 @@ vproc_exit(struct proc *p)
                                 * The tty could have been revoked
                                 * if we blocked.
                                 */
+                               context.vc_proc = p;
+                               context.vc_ucred = p->p_ucred;
                                if (sp->s_ttyvp)
-                                       VOP_REVOKE(sp->s_ttyvp, REVOKEALL);
+                                       VNOP_REVOKE(sp->s_ttyvp, REVOKEALL, &context);
                        }
                        ttyvp = sp->s_ttyvp;
                        sp->s_ttyvp = NULL;
-                       if (ttyvp)
-                               vrele(ttyvp);
+                       if (ttyvp) {
+                               vnode_rele(ttyvp);
+                       }
                        /*
                         * s_ttyp is not zero'd; we use this to indicate
                         * that the session once had a controlling terminal.
@@ -900,19 +1173,15 @@ vproc_exit(struct proc *p)
        /* 
         * release trace file
         */
-       p->p_traceflag = 0;     /* don't trace the vrele() */
+       p->p_traceflag = 0;     /* don't trace the vnode_rele() */
        if (p->p_tracep) {
                struct vnode *tvp = p->p_tracep;
                p->p_tracep = NULL;
-               vrele(tvp);
+               vnode_rele(tvp);
        }
 #endif
 
-       q = p->p_children.lh_first;
-       if (q)          /* only need this if any child is S_ZOMB */
-               wakeup((caddr_t) initproc);
-       for (; q != 0; q = nq) {
-               nq = q->p_sibling.le_next;
+       while (q = p->p_children.lh_first) {
                proc_reparent(q, initproc);
                /*
                 * Traced processes are killed
@@ -921,9 +1190,6 @@ vproc_exit(struct proc *p)
                if (q->p_flag & P_TRACED) {
                        q->p_flag &= ~P_TRACED;
                        if (q->sigwait_thread) {
-                               thread_t sig_shuttle;
-
-                               sig_shuttle = (thread_t) getshuttle_thread((thread_act_t)q->sigwait_thread);
                                /*
                                 * The sigwait_thread could be stopped at a
                                 * breakpoint. Wake it up to kill.
@@ -931,9 +1197,9 @@ vproc_exit(struct proc *p)
                                 * the first thread in the task. So any attempts to kill
                                 * the process would result into a deadlock on q->sigwait.
                                 */
-                               thread_resume((thread_act_t)q->sigwait_thread);
-                               clear_wait(sig_shuttle, THREAD_INTERRUPTED);
-                               threadsignal((thread_act_t)q->sigwait_thread, SIGKILL, 0);
+                               thread_resume((thread_t)q->sigwait_thread);
+                               clear_wait(q->sigwait_thread, THREAD_INTERRUPTED);
+                               threadsignal((thread_t)q->sigwait_thread, SIGKILL, 0);
                        }
                        psignal(q, SIGKILL);
                }
@@ -941,15 +1207,16 @@ vproc_exit(struct proc *p)
 
        /*
         * Save exit status and final rusage info, adding in child rusage
-        * info and self times.
+        * info and self times.  If we were unable to allocate a zombie
+        * structure, this information is lost.
         */
-       *p->p_ru = p->p_stats->p_ru;
-
-       timerclear(&p->p_ru->ru_utime);
-       timerclear(&p->p_ru->ru_stime);
+       if (p->p_ru != NULL) {
+           *p->p_ru = p->p_stats->p_ru;
+           timerclear(&p->p_ru->ru_utime);
+           timerclear(&p->p_ru->ru_stime);
 
 #ifdef  FIXME
-       if (task) {
+           if (task) {
                task_basic_info_data_t tinfo;
                task_thread_times_info_data_t ttimesinfo;
                int task_info_stuff, task_ttimes_stuff;
@@ -972,11 +1239,12 @@ vproc_exit(struct proc *p)
                st.tv_sec = ttimesinfo.system_time.seconds;
                st.tv_usec = ttimesinfo.system_time.microseconds;
                timeradd(&ut,&p->p_ru->ru_utime,&p->p_ru->ru_utime);
-               timeradd(&st,&p->p_ru->ru_stime,&p->p_ru->ru_stime);
-       }
+                       timeradd(&st,&p->p_ru->ru_stime,&p->p_ru->ru_stime);
+           }
 #endif /* FIXME */
 
-       ruadd(p->p_ru, &p->p_stats->p_cru);
+           ruadd(p->p_ru, &p->p_stats->p_cru);
+       }
 
        /*
         * Free up profiling buffers.
@@ -990,7 +1258,7 @@ vproc_exit(struct proc *p)
 
                for (; p1 != NULL; p1 = pn) {
                        pn = p1->pr_next;
-                       kfree((vm_offset_t)p1, sizeof *p1);
+                       kfree(p1, sizeof *p1);
                }
        }
 
@@ -1022,14 +1290,48 @@ vproc_exit(struct proc *p)
                pp->si_pid = p->p_pid;
                pp->si_status = p->p_xstat;
                pp->si_code = CLD_EXITED;
-               pp->si_uid = p->p_cred->p_ruid;
+               pp->si_uid = p->p_ucred->cr_ruid;
        }
-       psignal(p->p_pptr, SIGCHLD);
-
-       /* Place onto zombproc. */
-       LIST_INSERT_HEAD(&zombproc, p, p_list);
+       /* mark as a zombie */
        p->p_stat = SZOMB;
 
+       psignal(p->p_pptr, SIGCHLD);
+
        /* and now wakeup the parent */
        wakeup((caddr_t)p->p_pptr);
 }
+
+                                               
+/*
+ * munge_rusage
+ *     LP64 support - long is 64 bits if we are dealing with a 64 bit user
+ *     process.  We munge the kernel (32 bit) version of rusage into the
+ *     64 bit version.
+ */
+__private_extern__  void 
+munge_rusage(struct rusage *a_rusage_p, struct user_rusage *a_user_rusage_p)
+{
+       /* timeval changes size, so utime and stime need special handling */
+       a_user_rusage_p->ru_utime.tv_sec = a_rusage_p->ru_utime.tv_sec;
+       a_user_rusage_p->ru_utime.tv_usec = a_rusage_p->ru_utime.tv_usec;
+       a_user_rusage_p->ru_stime.tv_sec = a_rusage_p->ru_stime.tv_sec;
+       a_user_rusage_p->ru_stime.tv_usec = a_rusage_p->ru_stime.tv_usec;
+       /*
+        * everything else can be a direct assign, since there is no loss
+        * of precision implied boing 32->64.
+        */
+       a_user_rusage_p->ru_maxrss = a_rusage_p->ru_maxrss;
+       a_user_rusage_p->ru_ixrss = a_rusage_p->ru_ixrss;
+       a_user_rusage_p->ru_idrss = a_rusage_p->ru_idrss;
+       a_user_rusage_p->ru_isrss = a_rusage_p->ru_isrss;
+       a_user_rusage_p->ru_minflt = a_rusage_p->ru_minflt;
+       a_user_rusage_p->ru_majflt = a_rusage_p->ru_majflt;
+       a_user_rusage_p->ru_nswap = a_rusage_p->ru_nswap;
+       a_user_rusage_p->ru_inblock = a_rusage_p->ru_inblock;
+       a_user_rusage_p->ru_oublock = a_rusage_p->ru_oublock;
+       a_user_rusage_p->ru_msgsnd = a_rusage_p->ru_msgsnd;
+       a_user_rusage_p->ru_msgrcv = a_rusage_p->ru_msgrcv;
+       a_user_rusage_p->ru_nsignals = a_rusage_p->ru_nsignals;
+       a_user_rusage_p->ru_nvcsw = a_rusage_p->ru_nvcsw;
+       a_user_rusage_p->ru_nivcsw = a_rusage_p->ru_nivcsw;
+}