+
+ proc_childdrainend(p);
+ proc_list_unlock();
+
+ /*
+ * Release reference to text vnode
+ */
+ tvp = p->p_textvp;
+ p->p_textvp = NULL;
+ if (tvp != NULLVP) {
+ vnode_rele(tvp);
+ }
+
+ /*
+ * Save exit status and final rusage info, adding in child rusage
+ * info and self times. If we were unable to allocate a zombie
+ * structure, this information is lost.
+ */
+ if (rup != NULL) {
+ rup->ru = p->p_stats->p_ru;
+ timerclear(&rup->ru.ru_utime);
+ timerclear(&rup->ru.ru_stime);
+
+#ifdef FIXME
+ if (task) {
+ mach_task_basic_info_data_t tinfo;
+ task_thread_times_info_data_t ttimesinfo;
+ int task_info_stuff, task_ttimes_stuff;
+ struct timeval ut,st;
+
+ task_info_stuff = MACH_TASK_BASIC_INFO_COUNT;
+ task_info(task, MACH_TASK_BASIC_INFO,
+ &tinfo, &task_info_stuff);
+ p->p_ru->ru.ru_utime.tv_sec = tinfo.user_time.seconds;
+ p->p_ru->ru.ru_utime.tv_usec = tinfo.user_time.microseconds;
+ p->p_ru->ru.ru_stime.tv_sec = tinfo.system_time.seconds;
+ p->p_ru->ru.ru_stime.tv_usec = tinfo.system_time.microseconds;
+
+ task_ttimes_stuff = TASK_THREAD_TIMES_INFO_COUNT;
+ task_info(task, TASK_THREAD_TIMES_INFO,
+ &ttimesinfo, &task_ttimes_stuff);
+
+ ut.tv_sec = ttimesinfo.user_time.seconds;
+ ut.tv_usec = ttimesinfo.user_time.microseconds;
+ st.tv_sec = ttimesinfo.system_time.seconds;
+ st.tv_usec = ttimesinfo.system_time.microseconds;
+ timeradd(&ut,&p->p_ru->ru.ru_utime,&p->p_ru->ru.ru_utime);
+ timeradd(&st,&p->p_ru->ru.ru_stime,&p->p_ru->ru.ru_stime);
+ }
+#endif /* FIXME */
+
+ ruadd(&rup->ru, &p->p_stats->p_cru);
+
+ gather_rusage_info(p, &rup->ri, RUSAGE_INFO_CURRENT);
+ rup->ri.ri_phys_footprint = 0;
+ rup->ri.ri_proc_exit_abstime = mach_absolute_time();
+
+ /*
+ * Now that we have filled in the rusage info, make it
+ * visible to an external observer via proc_pid_rusage().
+ */
+ p->p_ru = rup;
+ }
+
+ /*
+ * Free up profiling buffers.
+ */
+ {
+ struct uprof *p0 = &p->p_stats->p_prof, *p1, *pn;
+
+ p1 = p0->pr_next;
+ p0->pr_next = NULL;
+ p0->pr_scale = 0;
+
+ for (; p1 != NULL; p1 = pn) {
+ pn = p1->pr_next;
+ kfree(p1, sizeof *p1);
+ }
+ }
+
+#if PSYNCH
+ pth_proc_hashdelete(p);
+#endif /* PSYNCH */
+
+ /*
+ * Other substructures are freed from wait().
+ */
+ FREE_ZONE(p->p_stats, sizeof *p->p_stats, M_PSTATS);
+ p->p_stats = NULL;
+
+ FREE_ZONE(p->p_sigacts, sizeof *p->p_sigacts, M_SIGACTS);
+ p->p_sigacts = NULL;
+
+ proc_limitdrop(p, 1);
+ p->p_limit = NULL;
+
+ /*
+ * Finish up by terminating the task
+ * and halt this thread (only if a
+ * member of the task exiting).
+ */
+ p->task = TASK_NULL;
+
+ /*
+ * Notify parent that we're gone.
+ */
+ pp = proc_parent(p);
+ if ((p->p_listflag & P_LIST_DEADPARENT) == 0) {
+ if (pp != initproc) {
+ proc_lock(pp);
+ pp->si_pid = p->p_pid;
+ pp->si_status = p->p_xstat;
+ pp->si_code = CLD_EXITED;
+ /*
+ * p_ucred usage is safe as it is an exiting process
+ * and reference is dropped in reap
+ */
+ pp->si_uid = kauth_cred_getruid(p->p_ucred);
+ proc_unlock(pp);
+ }
+ /* mark as a zombie */
+ /* mark as a zombie */
+ /* No need to take proc lock as all refs are drained and
+ * no one except parent (reaping ) can look at this.
+ * The write is to an int and is coherent. Also parent is
+ * keyed off of list lock for reaping
+ */
+ p->p_stat = SZOMB;
+
+ psignal(pp, SIGCHLD);
+
+ /* and now wakeup the parent */
+ proc_list_lock();
+ wakeup((caddr_t)pp);
+ proc_list_unlock();
+ } else {
+ proc_list_lock();
+ /* check for lookups by zomb sysctl */
+ while ((p->p_listflag & P_LIST_WAITING) == P_LIST_WAITING) {
+ msleep(&p->p_stat, proc_list_mlock, PWAIT, "waitcoll", 0);
+ }
+ p->p_stat = SZOMB;
+ p->p_listflag |= P_LIST_WAITING;
+
+ /*
+ * This is a named reference and it is not granted
+ * if the reap is already in progress. So we get
+ * the reference here exclusively and their can be
+ * no waiters. So there is no need for a wakeup
+ * after we are done. AlsO the reap frees the structure
+ * and the proc struct cannot be used for wakeups as well.
+ * It is safe to use p here as this is system reap
+ */
+ (void)reap_child_locked(pp, p, 0, 0, 1, 1);
+ /* list lock dropped by reap_child_locked */
+ }
+ proc_rele(pp);
+}
+
+
+/*
+ * munge_rusage
+ * LP64 support - long is 64 bits if we are dealing with a 64 bit user
+ * process. We munge the kernel version of rusage into the
+ * 64 bit version.
+ */
+__private_extern__ void
+munge_user64_rusage(struct rusage *a_rusage_p, struct user64_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;
+}
+
+/* For a 64-bit kernel and 32-bit userspace, munging may be needed */
+__private_extern__ void
+munge_user32_rusage(struct rusage *a_rusage_p, struct user32_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. We currently ignore
+ * the loss of precision
+ */
+ 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;