+
+ /*
+ * The parameter list of audit_syscall_exit() was augmented to
+ * take the Darwin syscall number as the first parameter,
+ * which is currently required by mac_audit_postselect().
+ */
+
+ /*
+ * The BSM token contains two components: an exit status as passed
+ * to exit(), and a return value to indicate what sort of exit it
+ * was. The exit status is WEXITSTATUS(rv), but it's not clear
+ * what the return value is.
+ */
+ AUDIT_ARG(exit, WEXITSTATUS(rv), 0);
+ AUDIT_SYSCALL_EXIT(SYS_exit, p, ut, 0); /* Exit is always successfull */
+
+ DTRACE_PROC1(exit, int, CLD_EXITED);
+
+ /* mark process is going to exit and pull out of DBG/disk throttle */
+ /* TODO: This should be done after becoming exit thread */
+ proc_set_task_policy(p->task, THREAD_NULL, TASK_POLICY_ATTRIBUTE,
+ TASK_POLICY_TERMINATED, TASK_POLICY_ENABLE);
+
+ proc_lock(p);
+ error = proc_transstart(p, 1, ((jetsam_flags & P_JETSAM_VNODE) ? 1 : 0));
+ if (error == EDEADLK) {
+ /* Temp: If deadlock error, then it implies multithreaded exec is
+ * in progress. Instread of letting exit continue and
+ * corrupting the freed memory, let the exit thread
+ * return. This will save corruption in remote case.
+ */
+ proc_unlock(p);
+ if (current_proc() == p){
+ if (p->exit_thread == self)
+ printf("exit_thread failed to exit, leaving process %s[%d] in unkillable limbo\n",
+ p->p_comm, p->p_pid);
+ thread_exception_return();
+ } else {
+ /* external termination like jetsam */
+ return(error);
+ }
+ }
+