2  * Copyright (c) 2000-2011, 2015 Apple Inc. All rights reserved. 
   4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ 
   6  * This file contains Original Code and/or Modifications of Original Code 
   7  * as defined in and that are subject to the Apple Public Source License 
   8  * Version 2.0 (the 'License'). You may not use this file except in 
   9  * compliance with the License. The rights granted to you under the License 
  10  * may not be used to create, or enable the creation or redistribution of, 
  11  * unlawful or unlicensed copies of an Apple operating system, or to 
  12  * circumvent, violate, or enable the circumvention or violation of, any 
  13  * terms of an Apple operating system software license agreement. 
  15  * Please obtain a copy of the License at 
  16  * http://www.opensource.apple.com/apsl/ and read it before using this file. 
  18  * The Original Code and all software distributed under the License are 
  19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 
  20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 
  21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 
  22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 
  23  * Please see the License for the specific language governing rights and 
  24  * limitations under the License. 
  26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ 
  28 /* Copyright (c) 1995, 1997 Apple Computer, Inc. All Rights Reserved */ 
  30  * Copyright (c) 1982, 1986, 1989, 1991, 1993 
  31  *      The Regents of the University of California.  All rights reserved. 
  32  * (c) UNIX System Laboratories, Inc. 
  33  * All or some portions of this file are derived from material licensed 
  34  * to the University of California by American Telephone and Telegraph 
  35  * Co. or Unix System Laboratories, Inc. and are reproduced herein with 
  36  * the permission of UNIX System Laboratories, Inc. 
  38  * Redistribution and use in source and binary forms, with or without 
  39  * modification, are permitted provided that the following conditions 
  41  * 1. Redistributions of source code must retain the above copyright 
  42  *    notice, this list of conditions and the following disclaimer. 
  43  * 2. Redistributions in binary form must reproduce the above copyright 
  44  *    notice, this list of conditions and the following disclaimer in the 
  45  *    documentation and/or other materials provided with the distribution. 
  46  * 3. All advertising materials mentioning features or use of this software 
  47  *    must display the following acknowledgement: 
  48  *      This product includes software developed by the University of 
  49  *      California, Berkeley and its contributors. 
  50  * 4. Neither the name of the University nor the names of its contributors 
  51  *    may be used to endorse or promote products derived from this software 
  52  *    without specific prior written permission. 
  54  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 
  55  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
  56  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
  57  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 
  58  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
  59  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 
  60  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
  61  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
  62  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 
  63  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 
  66  *      @(#)kern_exit.c 8.7 (Berkeley) 2/12/94 
  69  * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce 
  70  * support for mandatory and extensible security protections.  This notice 
  71  * is included in support of clause 2.2 (b) of the Apple Public License, 
  75 #include <machine/reg.h> 
  76 #include <machine/psl.h> 
  78 #include "compat_43.h" 
  80 #include <sys/param.h> 
  81 #include <sys/systm.h> 
  82 #include <sys/ioctl.h> 
  83 #include <sys/proc_internal.h> 
  85 #include <sys/kauth.h> 
  88 #include <sys/resource.h> 
  89 #include <sys/kernel.h> 
  91 #include <sys/file_internal.h> 
  92 #include <sys/vnode_internal.h> 
  93 #include <sys/syslog.h> 
  94 #include <sys/malloc.h> 
  95 #include <sys/resourcevar.h> 
  96 #include <sys/ptrace.h> 
  97 #include <sys/proc_info.h> 
  98 #include <sys/_types/_timeval64.h> 
 100 #include <sys/aio_kern.h> 
 101 #include <sys/sysproto.h> 
 102 #include <sys/signalvar.h> 
 103 #include <sys/kdebug.h> 
 104 #include <sys/filedesc.h>       /* fdfree */ 
 106 #include <sys/shm_internal.h>   /* shmexit */ 
 108 #include <sys/acct.h>           /* acct_process */ 
 110 #include <sys/persona.h> 
 113 #include <security/audit/audit.h> 
 114 #include <bsm/audit_kevents.h> 
 116 #include <mach/mach_types.h> 
 118 #include <kern/kern_types.h> 
 119 #include <kern/kalloc.h> 
 120 #include <kern/task.h> 
 121 #include <corpses/task_corpse.h> 
 122 #include <kern/thread.h> 
 123 #include <kern/thread_call.h> 
 124 #include <kern/sched_prim.h> 
 125 #include <kern/assert.h> 
 126 #include <sys/codesign.h> 
 128 #if VM_PRESSURE_EVENTS 
 129 #include <kern/vm_pressure.h> 
 132 #if CONFIG_MEMORYSTATUS 
 133 #include <sys/kern_memorystatus.h> 
 137 /* Do not include dtrace.h, it redefines kmem_[alloc/free] */ 
 138 extern void (*dtrace_fasttrap_exit_ptr
)(proc_t
); 
 139 extern void (*dtrace_helpers_cleanup
)(proc_t
); 
 140 extern void dtrace_lazy_dofs_destroy(proc_t
); 
 142 #include <sys/dtrace_ptss.h> 
 146 #include <security/mac.h> 
 147 #include <sys/syscall.h> 
 150 #include <mach/mach_types.h> 
 151 #include <mach/task.h> 
 152 #include <mach/thread_act.h> 
 154 #include <vm/vm_protos.h> 
 158 extern boolean_t init_task_died
; 
 159 void proc_prepareexit(proc_t p
, int rv
, boolean_t perf_notify
); 
 160 void vfork_exit(proc_t p
, int rv
); 
 161 void vproc_exit(proc_t p
); 
 162 __private_extern__ 
void munge_user64_rusage(struct rusage 
*a_rusage_p
, struct user64_rusage 
*a_user_rusage_p
); 
 163 __private_extern__ 
void munge_user32_rusage(struct rusage 
*a_rusage_p
, struct user32_rusage 
*a_user_rusage_p
); 
 164 static int reap_child_locked(proc_t parent
, proc_t child
, int deadparent
, int reparentedtoinit
, int locked
, int droplock
); 
 165 static void populate_corpse_crashinfo(proc_t p
, void *crash_info_ptr
, struct rusage_superset 
*rup
, mach_exception_data_type_t code
, mach_exception_data_type_t subcode
); 
 166 extern int proc_pidpathinfo(proc_t p
, uint64_t arg
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
); 
 168 static __attribute__((noinline
)) void launchd_crashed_panic(proc_t p
, int rv
); 
 169 extern void proc_piduniqidentifierinfo(proc_t p
, struct proc_uniqidentifierinfo 
*p_uniqidinfo
); 
 173  * Things which should have prototypes in headers, but don't 
 175 void    proc_exit(proc_t p
); 
 176 int     wait1continue(int result
); 
 177 int     waitidcontinue(int result
); 
 178 kern_return_t 
sys_perf_notify(thread_t thread
, int pid
); 
 179 kern_return_t 
task_exception_notify(exception_type_t exception
, 
 180         mach_exception_data_type_t code
, mach_exception_data_type_t subcode
); 
 182 void gather_rusage_info(proc_t p
, rusage_info_current 
*ru
, int flavor
); 
 185  * NOTE: Source and target may *NOT* overlap! 
 186  * XXX Should share code with bsd/dev/ppc/unix_signal.c 
 189 siginfo_user_to_user32(user_siginfo_t 
*in
, user32_siginfo_t 
*out
) 
 191         out
->si_signo   
= in
->si_signo
; 
 192         out
->si_errno   
= in
->si_errno
; 
 193         out
->si_code    
= in
->si_code
; 
 194         out
->si_pid     
= in
->si_pid
; 
 195         out
->si_uid     
= in
->si_uid
; 
 196         out
->si_status  
= in
->si_status
; 
 197         out
->si_addr    
= CAST_DOWN_EXPLICIT(user32_addr_t
,in
->si_addr
); 
 198         /* following cast works for sival_int because of padding */ 
 199         out
->si_value
.sival_ptr 
= CAST_DOWN_EXPLICIT(user32_addr_t
,in
->si_value
.sival_ptr
); 
 200         out
->si_band    
= in
->si_band
;                  /* range reduction */ 
 204 siginfo_user_to_user64(user_siginfo_t 
*in
, user64_siginfo_t 
*out
) 
 206         out
->si_signo   
= in
->si_signo
; 
 207         out
->si_errno   
= in
->si_errno
; 
 208         out
->si_code    
= in
->si_code
; 
 209         out
->si_pid     
= in
->si_pid
; 
 210         out
->si_uid     
= in
->si_uid
; 
 211         out
->si_status  
= in
->si_status
; 
 212         out
->si_addr    
= in
->si_addr
; 
 213         /* following cast works for sival_int because of padding */ 
 214         out
->si_value
.sival_ptr 
= in
->si_value
.sival_ptr
; 
 215         out
->si_band    
= in
->si_band
;                  /* range reduction */ 
 219 copyoutsiginfo(user_siginfo_t 
*native
, boolean_t is64
, user_addr_t uaddr
) 
 222                 user64_siginfo_t sinfo64
; 
 224                 bzero(&sinfo64
, sizeof (sinfo64
)); 
 225                 siginfo_user_to_user64(native
, &sinfo64
); 
 226                 return (copyout(&sinfo64
, uaddr
, sizeof (sinfo64
))); 
 228                 user32_siginfo_t sinfo32
; 
 230                 bzero(&sinfo32
, sizeof (sinfo32
)); 
 231                 siginfo_user_to_user32(native
, &sinfo32
); 
 232                 return (copyout(&sinfo32
, uaddr
, sizeof (sinfo32
))); 
 236 static void populate_corpse_crashinfo(proc_t p
, void *crash_info_ptr
, struct rusage_superset 
*rup
, mach_exception_data_type_t code
, mach_exception_data_type_t subcode
) 
 238         mach_vm_address_t uaddr 
= 0; 
 239         mach_exception_data_type_t exc_codes
[EXCEPTION_CODE_MAX
]; 
 241         exc_codes
[1] = subcode
; 
 243         struct proc_uniqidentifierinfo p_uniqidinfo
; 
 244         struct proc_workqueueinfo pwqinfo
; 
 246         uint64_t crashed_threadid 
= thread_tid(current_thread()); 
 247         unsigned int pflags 
= 0; 
 249 #if CONFIG_MEMORYSTATUS 
 250         int memstat_dirty_flags 
= 0; 
 253         if (KERN_SUCCESS 
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_EXCEPTION_CODES
, sizeof(exc_codes
), &uaddr
)) { 
 254                 copyout(exc_codes
, uaddr
, sizeof(exc_codes
)); 
 257         if (KERN_SUCCESS 
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_PID
, sizeof(p
->p_pid
), &uaddr
)) { 
 258                 copyout(&p
->p_pid
, uaddr
, sizeof(p
->p_pid
)); 
 261         if (KERN_SUCCESS 
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_PPID
, sizeof(p
->p_ppid
), &uaddr
)) { 
 262                 copyout(&p
->p_ppid
, uaddr
, sizeof(p
->p_ppid
)); 
 265         if (KERN_SUCCESS 
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_CRASHED_THREADID
, sizeof(uint64_t), &uaddr
)) { 
 266                 copyout(&crashed_threadid
, uaddr
, sizeof(uint64_t)); 
 269         if (KERN_SUCCESS 
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_RUSAGE
, sizeof(struct rusage
), &uaddr
)) { 
 270                 copyout(&rup
->ru
, uaddr
, sizeof(struct rusage
)); 
 274             kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_BSDINFOWITHUNIQID
, sizeof(struct proc_uniqidentifierinfo
), &uaddr
)) { 
 275                 proc_piduniqidentifierinfo(p
, &p_uniqidinfo
); 
 276                 copyout(&p_uniqidinfo
, uaddr
, sizeof(struct proc_uniqidentifierinfo
)); 
 279         if (KERN_SUCCESS 
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_RUSAGE_INFO
, sizeof(rusage_info_current
), &uaddr
)) { 
 280                 copyout(&rup
->ri
, uaddr
, sizeof(rusage_info_current
)); 
 283         if (KERN_SUCCESS 
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_PROC_CSFLAGS
, sizeof(p
->p_csflags
), &uaddr
)) { 
 284                 copyout(&p
->p_csflags
, uaddr
, sizeof(p
->p_csflags
)); 
 287         if (KERN_SUCCESS 
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_PROC_NAME
, sizeof(p
->p_comm
), &uaddr
)) { 
 288                 copyout(&p
->p_comm
, uaddr
, sizeof(p
->p_comm
)); 
 291         if (KERN_SUCCESS 
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_PROC_STARTTIME
, sizeof(p
->p_start
), &uaddr
)) { 
 292                 struct timeval64 t64
; 
 293                 t64
.tv_sec 
= (int64_t)p
->p_start
.tv_sec
; 
 294                 t64
.tv_usec 
= (int64_t)p
->p_start
.tv_usec
; 
 295                 copyout(&t64
, uaddr
, sizeof(t64
)); 
 298         if (KERN_SUCCESS 
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_USERSTACK
, sizeof(p
->user_stack
), &uaddr
)) { 
 299                 copyout(&p
->user_stack
, uaddr
, sizeof(p
->user_stack
)); 
 302         if (KERN_SUCCESS 
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_ARGSLEN
, sizeof(p
->p_argslen
), &uaddr
)) { 
 303                 copyout(&p
->p_argslen
, uaddr
, sizeof(p
->p_argslen
)); 
 306         if (KERN_SUCCESS 
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_PROC_ARGC
, sizeof(p
->p_argc
), &uaddr
)) { 
 307                 copyout(&p
->p_argc
, uaddr
, sizeof(p
->p_argc
)); 
 310         if (KERN_SUCCESS 
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_PROC_PATH
, MAXPATHLEN
, &uaddr
)) { 
 311                 proc_pidpathinfo(p
, 0, uaddr
, MAXPATHLEN
, &retval
); 
 314         pflags 
= p
->p_flag 
& (P_LP64 
| P_SUGID
); 
 315         if (KERN_SUCCESS 
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_PROC_FLAGS
, sizeof(pflags
), &uaddr
)) { 
 316                 copyout(&pflags
, uaddr
, sizeof(pflags
)); 
 319         if (KERN_SUCCESS 
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_UID
, sizeof(p
->p_uid
), &uaddr
)) { 
 320                 copyout(&p
->p_uid
, uaddr
, sizeof(p
->p_uid
)); 
 323         if (KERN_SUCCESS 
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_GID
, sizeof(p
->p_gid
), &uaddr
)) { 
 324                 copyout(&p
->p_gid
, uaddr
, sizeof(p
->p_gid
)); 
 327         cputype 
= cpu_type() & ~CPU_ARCH_MASK
; 
 328         if (IS_64BIT_PROCESS(p
)) 
 329                 cputype 
|= CPU_ARCH_ABI64
; 
 331         if (KERN_SUCCESS 
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_CPUTYPE
, sizeof(cpu_type_t
), &uaddr
)) { 
 332                 copyout(&cputype
, uaddr
, sizeof(cpu_type_t
)); 
 335         bzero(&pwqinfo
, sizeof(struct proc_workqueueinfo
)); 
 336         retval 
= fill_procworkqueue(p
, &pwqinfo
); 
 338                 if (KERN_SUCCESS 
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_WORKQUEUEINFO
, sizeof(struct proc_workqueueinfo
), &uaddr
)) { 
 339                         copyout(&pwqinfo
, uaddr
, sizeof(struct proc_workqueueinfo
)); 
 343         if (KERN_SUCCESS 
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_RESPONSIBLE_PID
, sizeof(p
->p_responsible_pid
), &uaddr
)) { 
 344                 copyout(&p
->p_responsible_pid
, uaddr
, sizeof(p
->p_responsible_pid
)); 
 347 #if CONFIG_MEMORYSTATUS 
 348         memstat_dirty_flags 
= memorystatus_dirty_get(p
); 
 349         if (KERN_SUCCESS 
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_DIRTY_FLAGS
, sizeof(memstat_dirty_flags
), &uaddr
)) { 
 350                 copyout(&memstat_dirty_flags
, uaddr
, sizeof(memstat_dirty_flags
)); 
 356 static __attribute__((noinline
)) void 
 357 launchd_crashed_panic(proc_t p
, int rv
) 
 359         printf("pid 1 exited (signal %d, exit %d)\n", 
 360             WTERMSIG(rv
), WEXITSTATUS(rv
)); 
 362 #if (DEVELOPMENT || DEBUG) 
 364          * For debugging purposes, generate a core file of initproc before 
 365          * panicking. Leave at least 300 MB free on the root volume, and ignore 
 366          * the process's corefile ulimit. fsync() the file to ensure it lands on disk 
 367          * before the panic hits. 
 371         uint64_t        coredump_start 
= mach_absolute_time(); 
 372         uint64_t        coredump_end
; 
 374         clock_usec_t    tv_usec
; 
 377         err 
= coredump(p
, 300, COREDUMP_IGNORE_ULIMIT 
| COREDUMP_FULLFSYNC
); 
 379         coredump_end 
= mach_absolute_time(); 
 381         absolutetime_to_microtime(coredump_end 
- coredump_start
, &tv_sec
, &tv_usec
); 
 383         tv_msec 
= tv_usec 
/ 1000; 
 386                 printf("Failed to generate initproc core file: error %d, took %d.%03d seconds\n", 
 387                        err
, (uint32_t)tv_sec
, tv_msec
); 
 389                 printf("Generated initproc core file in %d.%03d seconds\n", 
 390                        (uint32_t)tv_sec
, tv_msec
); 
 394         sync(p
, (void *)NULL
, (int *)NULL
); 
 396         panic_plain("%s exited (signal %d, exit status %d %s)", (p
->p_name
[0] != '\0' ? p
->p_name 
: "initproc"), WTERMSIG(rv
), 
 397                     WEXITSTATUS(rv
), ((p
->p_csflags 
& CS_KILLED
) ? "CS_KILLED" : "")); 
 405 exit(proc_t p
, struct exit_args 
*uap
, int *retval
) 
 407         exit1(p
, W_EXITCODE(uap
->rval
, 0), retval
); 
 409         thread_exception_return(); 
 412                 thread_block(THREAD_CONTINUE_NULL
); 
 417  * Exit: deallocate address space and other resources, change proc state 
 418  * to zombie, and unlink proc from allproc and parent's lists.  Save exit 
 419  * status and rusage for wait().  Check for child processes and orphan them. 
 422 exit1(proc_t p
, int rv
, int *retval
) 
 424         return exit1_internal(p
, rv
, retval
, TRUE
, TRUE
, 0); 
 428 exit1_internal(proc_t p
, int rv
, int *retval
, boolean_t thread_can_terminate
, boolean_t perf_notify
, 
 431         thread_t self 
= current_thread(); 
 432         struct task 
*task 
= p
->task
; 
 437          * If a thread in this task has already 
 438          * called exit(), then halt any others 
 442          ut 
= get_bsdthread_info(self
); 
 443          if (ut
->uu_flag 
& UT_VFORK
) { 
 444                 if (!thread_can_terminate
) { 
 449                 vfork_return(p 
, retval
, p
->p_pid
); 
 450                 unix_syscall_return(0); 
 455          * The parameter list of audit_syscall_exit() was augmented to 
 456          * take the Darwin syscall number as the first parameter, 
 457          * which is currently required by mac_audit_postselect(). 
 461          * The BSM token contains two components: an exit status as passed 
 462          * to exit(), and a return value to indicate what sort of exit it  
 463          * was.  The exit status is WEXITSTATUS(rv), but it's not clear 
 464          * what the return value is. 
 466         AUDIT_ARG(exit
, WEXITSTATUS(rv
), 0); 
 467         AUDIT_SYSCALL_EXIT(SYS_exit
, p
, ut
, 0); /* Exit is always successfull */ 
 469         DTRACE_PROC1(exit
, int, CLD_EXITED
); 
 471         /* mark process is going to exit and pull out of DBG/disk throttle */ 
 472         /* TODO: This should be done after becoming exit thread */ 
 473         proc_set_task_policy(p
->task
, THREAD_NULL
, TASK_POLICY_ATTRIBUTE
, 
 474                              TASK_POLICY_TERMINATED
, TASK_POLICY_ENABLE
); 
 477         error 
= proc_transstart(p
, 1, (((jetsam_flags 
& P_JETSAM_MASK
) == P_JETSAM_VNODE
) ? 1 : 0)); 
 478         if (error 
== EDEADLK
) { 
 479                 /* Temp: If deadlock error, then it implies multithreaded exec is 
 480                  * in progress. Instread of letting exit continue and  
 481                  * corrupting the freed memory, let the exit thread 
 482                  * return. This will save corruption in remote case. 
 485                 if (current_proc() == p
){ 
 486                         if (p
->exit_thread 
== self
) 
 487                                 printf("exit_thread failed to exit, leaving process %s[%d] in unkillable limbo\n", 
 488                                        p
->p_comm
, p
->p_pid
); 
 489                         thread_exception_return(); 
 491                         /* external termination like jetsam */ 
 496         while (p
->exit_thread 
!= self
) { 
 497                 if (sig_try_locked(p
) <= 0) { 
 499                         if (get_threadtask(self
) != task
) { 
 505                         thread_terminate(self
); 
 506                         if (!thread_can_terminate
) { 
 510                         thread_exception_return(); 
 516         if (p 
== initproc 
&& current_proc() == p
) { 
 517                 init_task_died 
= TRUE
; 
 520         p
->p_lflag 
|= P_LEXIT
; 
 522         p
->p_lflag 
|= jetsam_flags
; 
 527         proc_prepareexit(p
, rv
, perf_notify
); 
 529         /* Last thread to terminate will call proc_exit() */ 
 530         task_terminate_internal(task
); 
 536 proc_prepareexit(proc_t p
, int rv
, boolean_t perf_notify
)  
 538         mach_exception_data_type_t code 
= 0, subcode 
= 0; 
 541         thread_t self 
= current_thread(); 
 542         ut 
= get_bsdthread_info(self
); 
 543         struct rusage_superset 
*rup
; 
 545         int create_corpse 
= FALSE
; 
 548                 launchd_crashed_panic(p
, rv
); 
 552         /* If a core should be generated, notify crash reporter */ 
 553         if (hassigprop(WTERMSIG(rv
), SA_CORE
) || ((p
->p_csflags 
& CS_KILLED
) != 0)) { 
 555                  * Workaround for processes checking up on PT_DENY_ATTACH: 
 556                  * should be backed out post-Leopard (details in 5431025). 
 558                 if ((SIGSEGV 
== WTERMSIG(rv
)) &&  
 559                                 (p
->p_pptr
->p_lflag 
& P_LNOATTACH
)) { 
 564                  * Crash Reporter looks for the signal value, original exception 
 565                  * type, and low 20 bits of the original code in code[0]  
 566                  * (8, 4, and 20 bits respectively). code[1] is unmodified.  
 568                 code 
= ((WTERMSIG(rv
) & 0xff) << 24) | 
 569                         ((ut
->uu_exception 
& 0x0f) << 20) |  
 570                         ((int)ut
->uu_code 
& 0xfffff); 
 571                 subcode 
= ut
->uu_subcode
; 
 573                 kr 
= task_exception_notify(EXC_CRASH
, code
, subcode
); 
 575                 /* Nobody handled EXC_CRASH?? remember to make corpse */ 
 577                         create_corpse 
= TRUE
; 
 582         /* Notify the perf server? */ 
 584                 (void)sys_perf_notify(self
, p
->p_pid
); 
 588         /* stash the usage into corpse data if making_corpse == true */ 
 589         if (create_corpse 
== TRUE
) { 
 590                 kr 
= task_mark_corpse(current_task()); 
 591                 if (kr 
!= KERN_SUCCESS
) { 
 592                         if (kr 
== KERN_NO_SPACE
) { 
 593                                 printf("Process[%d] has no vm space for corpse info.\n", p
->p_pid
); 
 594                         } else if (kr 
== KERN_NOT_SUPPORTED
) { 
 595                                 printf("Process[%d] was destined to be corpse. But corpse is disabled by config.\n", p
->p_pid
); 
 597                                 printf("Process[%d] crashed: %s. Too many corpses being created.\n", p
->p_pid
, p
->p_comm
); 
 599                         create_corpse 
= FALSE
; 
 601                         /* XXX: <rdar://problem/20491659> Need to sync ATM buffer before crash */ 
 602                         kr 
= task_send_trace_memory(current_task(), p
->p_pid
, p
->p_uniqueid
); 
 607          * Before this process becomes a zombie, stash resource usage 
 608          * stats in the proc for external observers to query 
 609          * via proc_pid_rusage(). 
 611          * If the zombie allocation fails, just punt the stats. 
 613         MALLOC_ZONE(rup
, struct rusage_superset 
*, 
 614                         sizeof (*rup
), M_ZOMBIE
, M_WAITOK
); 
 616                 gather_rusage_info(p
, &rup
->ri
, RUSAGE_INFO_CURRENT
); 
 617                 rup
->ri
.ri_phys_footprint 
= 0; 
 618                 rup
->ri
.ri_proc_exit_abstime 
= mach_absolute_time(); 
 621                  * Make the rusage_info visible to external observers 
 622                  * only after it has been completely filled in. 
 627                 populate_corpse_crashinfo(p
, task_get_corpseinfo(current_task()), rup
, code
, subcode
); 
 630          * Remove proc from allproc queue and from pidhash chain. 
 631          * Need to do this before we do anything that can block. 
 632          * Not doing causes things like mount() find this on allproc 
 633          * in partially cleaned state. 
 638 #if CONFIG_MEMORYSTATUS 
 639         memorystatus_remove(p
, TRUE
); 
 642         LIST_REMOVE(p
, p_list
); 
 643         LIST_INSERT_HEAD(&zombproc
, p
, p_list
); /* Place onto zombproc. */ 
 644         /* will not be visible via proc_find */ 
 645         p
->p_listflag 
|= P_LIST_EXITED
; 
 654          * If parent is waiting for us to exit or exec, 
 655          * P_LPPWAIT is set; we will wakeup the parent below. 
 658         p
->p_lflag 
&= ~(P_LTRACED 
| P_LPPWAIT
); 
 659         p
->p_sigignore 
= ~(sigcantmask
); 
 669         struct task 
*task 
= p
->task
; 
 670         vnode_t tvp 
= NULLVP
; 
 672         struct session 
*sessp
; 
 673         struct uthread 
* uth
; 
 678         uth 
= current_uthread(); 
 681         proc_transstart(p
, 1, 0); 
 682         if( !(p
->p_lflag 
& P_LEXIT
)) { 
 684                  * This can happen if a thread_terminate() occurs 
 685                  * in a single-threaded process. 
 687                 p
->p_lflag 
|= P_LEXIT
; 
 690                 proc_prepareexit(p
, 0, TRUE
);    
 691                 (void) task_terminate_internal(task
); 
 697         p
->p_lflag 
|= P_LPEXIT
; 
 700          * Other kernel threads may be in the middle of signalling this process. 
 701          * Wait for those threads to wrap it up before making the process 
 704         if ((p
->p_lflag 
& P_LINSIGNAL
) || (p
->p_sigwaitcnt 
> 0)) { 
 706                 while ((p
->p_lflag 
& P_LINSIGNAL
) || (p
->p_sigwaitcnt 
> 1))  
 707                         msleep(&p
->p_sigmask
, &p
->p_mlock
, PWAIT
, "proc_sigdrain", NULL
); 
 713         exitval 
= p
->p_xstat
; 
 714         KERNEL_DEBUG_CONSTANT_IST(KDEBUG_COMMON
,  
 715                 BSDDBG_CODE(DBG_BSD_PROC
, BSD_PROC_EXIT
) | DBG_FUNC_START
, 
 716                 pid
, exitval
, 0, 0, 0); 
 720          * Free any outstanding lazy dof entries. It is imperative we 
 721          * always call dtrace_lazy_dofs_destroy, rather than null check 
 722          * and call if !NULL. If we NULL test, during lazy dof faulting 
 723          * we can race with the faulting code and proceed from here to 
 724          * beyond the helpers cleanup. The lazy dof faulting will then 
 725          * install new helpers which will never be cleaned up, and leak. 
 727         dtrace_lazy_dofs_destroy(p
); 
 730          * Clean up any DTrace helper actions or probes for the process. 
 732         if (p
->p_dtrace_helpers 
!= NULL
) { 
 733                 (*dtrace_helpers_cleanup
)(p
); 
 737          * Clean up any DTrace probes associated with this process. 
 740          * APPLE NOTE: We release ptss pages/entries in dtrace_fasttrap_exit_ptr(), 
 741          * call this after dtrace_helpers_cleanup() 
 744         if (p
->p_dtrace_probes 
&& dtrace_fasttrap_exit_ptr
) { 
 745                 (*dtrace_fasttrap_exit_ptr
)(p
); 
 752 #if VM_PRESSURE_EVENTS 
 753         vm_pressure_proc_cleanup(p
); 
 757          * need to cancel async IO requests that can be cancelled and wait for those 
 758          * already active.  MAY BLOCK! 
 763         /* if any pending cpu limits action, clear it */ 
 764         task_clear_cpuusage(p
->task
, TRUE
); 
 766         workqueue_mark_exiting(p
); 
 768         kqueue_dealloc(p
->p_wqkqueue
); 
 769         p
->p_wqkqueue 
= NULL
; 
 774          * Close open files and release open-file table. 
 779         if (uth
->uu_lowpri_window
) { 
 781                  * task is marked as a low priority I/O type 
 782                  * and the I/O we issued while in flushing files on close 
 783                  * collided with normal I/O operations... 
 784                  * no need to throttle this thread since its going away 
 785                  * but we do need to update our bookeeping w/r to throttled threads 
 787                 throttle_lowpri_io(0); 
 791         /* Close ref SYSV Shared memory*/ 
 796         /* Release SYSV semaphores */ 
 801         pth_proc_hashdelete(p
); 
 804         sessp 
= proc_session(p
); 
 805         if (SESS_LEADER(p
, sessp
)) { 
 807                 if (sessp
->s_ttyvp 
!= NULLVP
) { 
 811                         struct vfs_context context
; 
 815                          * Controlling process. 
 816                          * Signal foreground pgrp, 
 817                          * drain controlling terminal 
 818                          * and revoke access to controlling terminal. 
 821                         tp 
= SESSION_TP(sessp
); 
 822                         if ((tp 
!= TTY_NULL
) && (tp
->t_session 
== sessp
)) { 
 823                                 session_unlock(sessp
); 
 826                                  * We're going to SIGHUP the foreground process 
 827                                  * group. It can't change from this point on 
 828                                  * until the revoke is complete. 
 829                                  * The process group changes under both the tty 
 830                                  * lock and proc_list_lock but we need only one 
 836                                 tty_pgsignal(tp
, SIGHUP
, 1); 
 839                                 tp 
= SESSION_TP(sessp
); 
 841                         cttyflag 
= sessp
->s_flags 
& S_CTTYREF
; 
 842                         sessp
->s_flags 
&= ~S_CTTYREF
; 
 843                         ttyvp 
= sessp
->s_ttyvp
; 
 844                         ttyvid 
= sessp
->s_ttyvid
; 
 845                         sessp
->s_ttyvp 
= NULLVP
; 
 847                         sessp
->s_ttyp 
= TTY_NULL
; 
 848                         sessp
->s_ttypgrpid 
= NO_PID
; 
 849                         session_unlock(sessp
); 
 851                         if ((ttyvp 
!= NULLVP
) && (vnode_getwithvid(ttyvp
, ttyvid
) == 0)) { 
 852                                 if (tp 
!= TTY_NULL
) { 
 857                                 context
.vc_thread 
= proc_thread(p
); /* XXX */ 
 858                                 context
.vc_ucred 
= kauth_cred_proc_ref(p
); 
 859                                 VNOP_REVOKE(ttyvp
, REVOKEALL
, &context
); 
 862                                          * Release the extra usecount taken in cttyopen. 
 863                                          * usecount should be released after VNOP_REVOKE is called. 
 864                                          * This usecount was taken to ensure that 
 865                                          * the VNOP_REVOKE results in a close to 
 866                                          * the tty since cttyclose is a no-op. 
 871                                 kauth_cred_unref(&context
.vc_ucred
); 
 876                                  * This is cleared even if not set. This is also done in 
 877                                  * spec_close to ensure that the flag is cleared. 
 887                 sessp
->s_leader 
= NULL
; 
 888                 session_unlock(sessp
); 
 896         p
->p_rlimit
[RLIMIT_FSIZE
].rlim_cur 
= RLIM_INFINITY
; 
 897         (void)acct_process(p
); 
 901         if ((p
->p_listflag 
& P_LIST_EXITCOUNT
) == P_LIST_EXITCOUNT
) { 
 902                 p
->p_listflag 
&= ~P_LIST_EXITCOUNT
; 
 903                 proc_shutdown_exitcount
--; 
 904                 if (proc_shutdown_exitcount 
== 0) 
 905                         wakeup(&proc_shutdown_exitcount
); 
 908         /* wait till parentrefs are dropped and grant no more */ 
 909         proc_childdrainstart(p
); 
 910         while ((q 
= p
->p_children
.lh_first
) != NULL
) { 
 911                 int reparentedtoinit 
= (q
->p_listflag 
& P_LIST_DEADPARENT
) ? 1 : 0; 
 912                 if (q
->p_stat 
== SZOMB
) { 
 914                                 panic("parent child linkage broken"); 
 915                         /* check for sysctl zomb lookup */ 
 916                         while ((q
->p_listflag 
& P_LIST_WAITING
) == P_LIST_WAITING
) { 
 917                                 msleep(&q
->p_stat
, proc_list_mlock
, PWAIT
, "waitcoll", 0); 
 919                         q
->p_listflag 
|= P_LIST_WAITING
; 
 921                          * This is a named reference and it is not granted 
 922                          * if the reap is already in progress. So we get 
 923                          * the reference here exclusively and their can be 
 924                          * no waiters. So there is no need for a wakeup 
 925                          * after we are done.  Also the reap frees the structure 
 926                          * and the proc struct cannot be used for wakeups as well.  
 927                          * It is safe to use q here as this is system reap 
 929                         (void)reap_child_locked(p
, q
, 1, reparentedtoinit
, 1, 0); 
 932                         * Traced processes are killed 
 933                         * since their existence means someone is messing up. 
 935                         if (q
->p_lflag 
& P_LTRACED
) { 
 939                                  * Take a reference on the child process to 
 940                                  * ensure it doesn't exit and disappear between 
 941                                  * the time we drop the list_lock and attempt 
 942                                  * to acquire its proc_lock. 
 944                                 if (proc_ref_locked(q
) != q
) 
 949                                 opp 
= proc_find(q
->p_oppid
); 
 950                                 if (opp 
!= PROC_NULL
) { 
 954                                         proc_reparentlocked(q
, opp
, 0, 0); 
 957                                         /* original parent exited while traced */ 
 959                                         q
->p_listflag 
|= P_LIST_DEADPARENT
; 
 962                                         proc_reparentlocked(q
, initproc
, 0, 0); 
 966                                 q
->p_lflag 
&= ~P_LTRACED
; 
 968                                 if (q
->sigwait_thread
) { 
 969                                         thread_t thread 
= q
->sigwait_thread
; 
 973                                         * The sigwait_thread could be stopped at a 
 974                                         * breakpoint. Wake it up to kill. 
 975                                         * Need to do this as it could be a thread which is not 
 976                                         * the first thread in the task. So any attempts to kill 
 977                                         * the process would result into a deadlock on q->sigwait. 
 979                                         thread_resume(thread
); 
 980                                         clear_wait(thread
, THREAD_INTERRUPTED
); 
 981                                         threadsignal(thread
, SIGKILL
, 0); 
 990                                 q
->p_listflag 
|= P_LIST_DEADPARENT
; 
 991                                 proc_reparentlocked(q
, initproc
, 0, 1); 
 996         proc_childdrainend(p
); 
1000          * Release reference to text vnode 
1004         if (tvp 
!= NULLVP
) { 
1009          * Save exit status and final rusage info, adding in child rusage 
1010          * info and self times.  If we were unable to allocate a zombie 
1011          * structure, this information is lost. 
1013         if (p
->p_ru 
!= NULL
) { 
1014             calcru(p
, &p
->p_stats
->p_ru
.ru_utime
, &p
->p_stats
->p_ru
.ru_stime
, NULL
); 
1015             p
->p_ru
->ru 
= p
->p_stats
->p_ru
; 
1017             ruadd(&(p
->p_ru
->ru
), &p
->p_stats
->p_cru
); 
1021          * Free up profiling buffers. 
1024                 struct uprof 
*p0 
= &p
->p_stats
->p_prof
, *p1
, *pn
; 
1030                 for (; p1 
!= NULL
; p1 
= pn
) { 
1032                         kfree(p1
, sizeof *p1
); 
1037         if (thread_call_cancel(p
->p_rcall
)) 
1040         while (p
->p_ractive 
> 0) { 
1049         thread_call_free(p
->p_rcall
); 
1053          * Other substructures are freed from wait(). 
1055         FREE_ZONE(p
->p_stats
, sizeof *p
->p_stats
, M_PSTATS
); 
1058         FREE_ZONE(p
->p_sigacts
, sizeof *p
->p_sigacts
, M_SIGACTS
); 
1059         p
->p_sigacts 
= NULL
; 
1061         proc_limitdrop(p
, 1); 
1064         vm_purgeable_disown(p
->task
); 
1067          * Finish up by terminating the task 
1068          * and halt this thread (only if a 
1069          * member of the task exiting). 
1071         p
->task 
= TASK_NULL
; 
1072         set_bsdtask_info(task
, NULL
); 
1074         knote_hint 
= NOTE_EXIT 
| (p
->p_xstat 
& 0xffff); 
1075         proc_knote(p
, knote_hint
); 
1077         /* mark the thread as the one that is doing proc_exit 
1078          * no need to hold proc lock in uthread_free 
1080         uth
->uu_flag 
|= UT_PROCEXIT
; 
1082          * Notify parent that we're gone. 
1084         pp 
= proc_parent(p
); 
1085         if (pp
->p_flag 
& P_NOCLDWAIT
) { 
1087                 if (p
->p_ru 
!= NULL
) { 
1091                  * If the parent is ignoring SIGCHLD, then POSIX requires 
1092                  * us to not add the resource usage to the parent process - 
1093                  * we are only going to hand it off to init to get reaped. 
1094                  * We should contest the standard in this case on the basis 
1097 #else   /* !3839178 */ 
1099                  * Add child resource usage to parent before giving 
1100                  * zombie to init.  If we were unable to allocate a 
1101                  * zombie structure, this information is lost. 
1103                         ruadd(&pp
->p_stats
->p_cru
, &p
->p_ru
->ru
); 
1104 #endif  /* !3839178 */ 
1105                         update_rusage_info_child(&pp
->p_stats
->ri_child
, &p
->p_ru
->ri
); 
1109                 /* kernel can reap this one, no need to move it to launchd */ 
1111                 p
->p_listflag 
|= P_LIST_DEADPARENT
; 
1114         if ((p
->p_listflag 
& P_LIST_DEADPARENT
) == 0 || p
->p_oppid
) { 
1115                 if (pp 
!= initproc
) { 
1117                         pp
->si_pid 
= p
->p_pid
; 
1118                         pp
->si_status 
= p
->p_xstat
; 
1119                         pp
->si_code 
= CLD_EXITED
; 
1121                          * p_ucred usage is safe as it is an exiting process 
1122                          * and reference is dropped in reap 
1124                         pp
->si_uid 
= kauth_cred_getruid(p
->p_ucred
); 
1127                 /* mark as a zombie */ 
1128                 /* No need to take proc lock as all refs are drained and 
1129                  * no one except parent (reaping ) can look at this. 
1130                  * The write is to an int and is coherent. Also parent is 
1131                  *  keyed off of list lock for reaping 
1133                 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_COMMON
, 
1134                         BSDDBG_CODE(DBG_BSD_PROC
, BSD_PROC_EXIT
) | DBG_FUNC_END
, 
1135                         pid
, exitval
, 0, 0, 0); 
1138                  * The current process can be reaped so, no one 
1139                  * can depend on this 
1142                 psignal(pp
, SIGCHLD
); 
1144                 /* and now wakeup the parent */ 
1146                 wakeup((caddr_t
)pp
); 
1149                 /* should be fine as parent proc would be initproc */ 
1150                 /* mark as a zombie */ 
1151                 /* No need to take proc lock as all refs are drained and 
1152                  * no one except parent (reaping ) can look at this. 
1153                  * The write is to an int and is coherent. Also parent is 
1154                  *  keyed off of list lock for reaping 
1157                 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_COMMON
, 
1158                         BSDDBG_CODE(DBG_BSD_PROC
, BSD_PROC_EXIT
) | DBG_FUNC_END
, 
1159                         pid
, exitval
, 0, 0, 0); 
1160                 /* check for sysctl zomb lookup */ 
1161                 while ((p
->p_listflag 
& P_LIST_WAITING
) == P_LIST_WAITING
) { 
1162                         msleep(&p
->p_stat
, proc_list_mlock
, PWAIT
, "waitcoll", 0); 
1164                 /* safe to use p as this is a system reap */ 
1166                 p
->p_listflag 
|= P_LIST_WAITING
; 
1169                  * This is a named reference and it is not granted 
1170                  * if the reap is already in progress. So we get 
1171                  * the reference here exclusively and their can be 
1172                  * no waiters. So there is no need for a wakeup 
1173                  * after we are done. AlsO  the reap frees the structure 
1174                  * and the proc struct cannot be used for wakeups as well.  
1175                  * It is safe to use p here as this is system reap 
1177                 (void)reap_child_locked(pp
, p
, 1, 0, 1, 1); 
1178                 /* list lock dropped by reap_child_locked */ 
1180         if (uth
->uu_lowpri_window
) { 
1182                  * task is marked as a low priority I/O type and we've 
1183                  * somehow picked up another throttle during exit processing... 
1184                  * no need to throttle this thread since its going away 
1185                  * but we do need to update our bookeeping w/r to throttled threads 
1187                 throttle_lowpri_io(0); 
1198  * Description: Given a process from which all status information needed 
1199  *              has already been extracted, if the process is a ptrace 
1200  *              attach process, detach it and give it back to its real 
1201  *              parent, else recover all resources remaining associated 
1204  * Parameters:  proc_t parent           Parent of process being reaped 
1205  *              proc_t child            Process to reap 
1207  * Returns:     0                       Process was not reaped because it 
1208  *                                      came from an attach 
1209  *              1                       Process was reaped 
1212 reap_child_locked(proc_t parent
, proc_t child
, int deadparent
, int reparentedtoinit
, int locked
, int droplock
) 
1214         proc_t trace_parent 
= PROC_NULL
;        /* Traced parent process, if tracing */ 
1220          * If we got the child via a ptrace 'attach', 
1221          * we need to give it back to the old parent. 
1223          * Exception: someone who has been reparented to launchd before being 
1224          * ptraced can simply be reaped, refer to radar 5677288 
1225          *      p_oppid                  -> ptraced 
1226          *      trace_parent == initproc -> away from launchd 
1227          *      reparentedtoinit         -> came to launchd by reparenting 
1229         if (child
->p_oppid
) { 
1234                 oppid 
= child
->p_oppid
; 
1236                 knote_hint 
= NOTE_EXIT 
| (child
->p_xstat 
& 0xffff); 
1239                 if ((trace_parent 
= proc_find(oppid
)) 
1240                         && !((trace_parent 
== initproc
) && reparentedtoinit
)) { 
1242                         if (trace_parent 
!= initproc
) { 
1244                                  * proc internal fileds  and p_ucred usage safe  
1245                                  * here as child is dead and is not reaped or  
1248                                 proc_lock(trace_parent
); 
1249                                 trace_parent
->si_pid 
= child
->p_pid
; 
1250                                 trace_parent
->si_status 
= child
->p_xstat
; 
1251                                 trace_parent
->si_code 
= CLD_CONTINUED
; 
1252                                 trace_parent
->si_uid 
= kauth_cred_getruid(child
->p_ucred
); 
1253                                 proc_unlock(trace_parent
); 
1255                         proc_reparentlocked(child
, trace_parent
, 1, 0); 
1257                         /* resend knote to original parent (and others) after reparenting */ 
1258                         proc_knote(child
, knote_hint
); 
1260                         psignal(trace_parent
, SIGCHLD
); 
1262                         wakeup((caddr_t
)trace_parent
); 
1263                         child
->p_listflag 
&= ~P_LIST_WAITING
; 
1264                         wakeup(&child
->p_stat
); 
1266                         proc_rele(trace_parent
); 
1267                         if ((locked 
== 1) && (droplock 
== 0)) 
1273                  * If we can't reparent (e.g. the original parent exited while child was being debugged, or 
1274                  * original parent is the same as the debugger currently exiting), we still need to satisfy 
1275                  * the knote lifecycle for other observers on the system. While the debugger was attached, 
1276                  * the NOTE_EXIT would not have been broadcast during initial child termination. 
1278                 proc_knote(child
, knote_hint
); 
1280                 if (trace_parent 
!= PROC_NULL
) { 
1281                         proc_rele(trace_parent
); 
1285 #pragma clang diagnostic push 
1286 #pragma clang diagnostic ignored "-Wdeprecated-declarations" 
1287         proc_knote(child
, NOTE_REAP
); 
1288 #pragma clang diagnostic pop 
1290         proc_knote_drain(child
); 
1297                  * If the parent is ignoring SIGCHLD, then POSIX requires 
1298                  * us to not add the resource usage to the parent process - 
1299                  * we are only going to hand it off to init to get reaped. 
1300                  * We should contest the standard in this case on the basis 
1303                 if (!(parent
->p_flag 
& P_NOCLDWAIT
)) 
1304 #endif  /* 3839178 */ 
1305                         ruadd(&parent
->p_stats
->p_cru
, &child
->p_ru
->ru
); 
1306                 update_rusage_info_child(&parent
->p_stats
->ri_child
, &child
->p_ru
->ri
); 
1307                 proc_unlock(parent
); 
1308                 FREE_ZONE(child
->p_ru
, sizeof *child
->p_ru
, M_ZOMBIE
); 
1311                 printf("Warning : lost p_ru for %s\n", child
->p_comm
); 
1314         AUDIT_SESSION_PROCEXIT(child
); 
1317          * Decrement the count of procs running with this uid. 
1318          * p_ucred usage is safe here as it is an exited process. 
1319          * and refernce is dropped after these calls down below 
1320          * (locking protection is provided by list lock held in chgproccnt) 
1324          * persona_proc_drop calls chgproccnt(-1) on the persona uid, 
1325          * and (+1) on the child->p_ucred uid 
1327         persona_proc_drop(child
); 
1329         (void)chgproccnt(kauth_cred_getruid(child
->p_ucred
), -1); 
1332          * Free up credentials. 
1334         if (IS_VALID_CRED(child
->p_ucred
)) { 
1335                 kauth_cred_unref(&child
->p_ucred
); 
1338         /*  XXXX Note NOT SAFE TO USE p_ucred from this point onwards */ 
1341          * Finally finished with old proc entry. 
1342          * Unlink it from its process group and free it. 
1347         LIST_REMOVE(child
, p_list
);     /* off zombproc */ 
1348         parent
->p_childrencnt
--; 
1349         LIST_REMOVE(child
, p_sibling
); 
1350         /* If there are no more children wakeup parent */ 
1351         if ((deadparent 
!= 0) && (LIST_EMPTY(&parent
->p_children
))) 
1352                 wakeup((caddr_t
)parent
);        /* with list lock held */ 
1353         child
->p_listflag 
&= ~P_LIST_WAITING
; 
1354         wakeup(&child
->p_stat
); 
1356         /* Take it out of process hash */ 
1357         LIST_REMOVE(child
, p_hash
); 
1358         child
->p_listflag 
&= ~P_LIST_INHASH
; 
1359         proc_checkdeadrefs(child
); 
1364                  * If a child zombie is being reaped because its parent 
1365                  * is exiting, make sure we update the list flag 
1367                 child
->p_listflag 
|= P_LIST_DEADPARENT
; 
1372 #if CONFIG_FINE_LOCK_GROUPS 
1373         lck_mtx_destroy(&child
->p_mlock
, proc_mlock_grp
); 
1374         lck_mtx_destroy(&child
->p_fdmlock
, proc_fdmlock_grp
); 
1375         lck_mtx_destroy(&child
->p_ucred_mlock
, proc_ucred_mlock_grp
); 
1377         lck_mtx_destroy(&child
->p_dtrace_sprlock
, proc_lck_grp
); 
1379         lck_spin_destroy(&child
->p_slock
, proc_slock_grp
); 
1380 #else /* CONFIG_FINE_LOCK_GROUPS */ 
1381         lck_mtx_destroy(&child
->p_mlock
, proc_lck_grp
); 
1382         lck_mtx_destroy(&child
->p_fdmlock
, proc_lck_grp
); 
1383         lck_mtx_destroy(&child
->p_ucred_mlock
, proc_lck_grp
); 
1385         lck_mtx_destroy(&child
->p_dtrace_sprlock
, proc_lck_grp
); 
1387         lck_spin_destroy(&child
->p_slock
, proc_lck_grp
); 
1388 #endif /* CONFIG_FINE_LOCK_GROUPS */ 
1389         workqueue_destroy_lock(child
); 
1391         FREE_ZONE(child
, sizeof *child
, M_PROC
); 
1392         if ((locked 
== 1) && (droplock 
== 0)) 
1400 wait1continue(int result
) 
1405         struct _wait4_data 
*wait4_data
; 
1406         struct wait4_nocancel_args 
*uap
; 
1413         thread 
= current_thread(); 
1414         uth 
= (struct uthread 
*)get_bsdthread_info(thread
); 
1416         wait4_data 
= &uth
->uu_kevent
.uu_wait4_data
; 
1417         uap 
= wait4_data
->args
; 
1418         retval 
= wait4_data
->retval
; 
1419         return(wait4_nocancel(p
, uap
, retval
)); 
1423 wait4(proc_t q
, struct wait4_args 
*uap
, int32_t *retval
) 
1425         __pthread_testcancel(1); 
1426         return(wait4_nocancel(q
, (struct wait4_nocancel_args 
*)uap
, retval
)); 
1430 wait4_nocancel(proc_t q
, struct wait4_nocancel_args 
*uap
, int32_t *retval
) 
1437         struct _wait4_data 
*wait4_data
; 
1439         AUDIT_ARG(pid
, uap
->pid
); 
1442                 uap
->pid 
= -q
->p_pgrpid
; 
1450         for (p 
= q
->p_children
.lh_first
; p 
!= 0; p 
= p
->p_sibling
.le_next
) { 
1451                 if ( p
->p_sibling
.le_next 
!= 0 ) 
1453                 if (uap
->pid 
!= WAIT_ANY 
&& 
1454                     p
->p_pid 
!= uap
->pid 
&& 
1455                     p
->p_pgrpid 
!= -(uap
->pid
)) 
1460                 /* XXX This is racy because we don't get the lock!!!! */ 
1462                 if (p
->p_listflag 
& P_LIST_WAITING
) { 
1463                         (void)msleep(&p
->p_stat
, proc_list_mlock
, PWAIT
, "waitcoll", 0); 
1466                 p
->p_listflag 
|= P_LIST_WAITING
;   /* only allow single thread to wait() */ 
1469                 if (p
->p_stat 
== SZOMB
) { 
1470                         int reparentedtoinit 
= (p
->p_listflag 
& P_LIST_DEADPARENT
) ? 1 : 0; 
1474                         if ((error 
= mac_proc_check_wait(q
, p
)) != 0) 
1477                         retval
[0] = p
->p_pid
; 
1479                                 /* Legacy apps expect only 8 bits of status */ 
1480                                 status 
= 0xffff & p
->p_xstat
;   /* convert to int */ 
1481                                 error 
= copyout((caddr_t
)&status
, 
1488                                 if (p
->p_ru 
== NULL
) { 
1491                                         if (IS_64BIT_PROCESS(q
)) { 
1492                                                 struct user64_rusage    my_rusage
; 
1493                                                 munge_user64_rusage(&p
->p_ru
->ru
, &my_rusage
); 
1494                                                 error 
= copyout((caddr_t
)&my_rusage
, 
1496                                                         sizeof (my_rusage
)); 
1499                                                 struct user32_rusage    my_rusage
; 
1500                                                 munge_user32_rusage(&p
->p_ru
->ru
, &my_rusage
); 
1501                                                 error 
= copyout((caddr_t
)&my_rusage
, 
1503                                                         sizeof (my_rusage
)); 
1506                                 /* information unavailable? */ 
1511                         /* Conformance change for 6577252. 
1512                          * When SIGCHLD is blocked and wait() returns because the status 
1513                          * of a child process is available and there are no other  
1514                          * children processes, then any pending SIGCHLD signal is cleared. 
1516                         if ( sibling_count 
== 0 ) { 
1517                                 int mask 
= sigmask(SIGCHLD
); 
1518                                 uth 
= current_uthread(); 
1520                                 if ( (uth
->uu_sigmask 
& mask
) != 0 ) { 
1521                                         /* we are blocking SIGCHLD signals.  clear any pending SIGCHLD. 
1522                                          * This locking looks funny but it is protecting access to the  
1523                                          * thread via p_uthlist. 
1526                                         uth
->uu_siglist 
&= ~mask
;       /* clear pending signal */ 
1532                         (void)reap_child_locked(q
, p
, 0, reparentedtoinit
, 0, 0); 
1536                 if (p
->p_stat 
== SSTOP 
&& (p
->p_lflag 
& P_LWAITED
) == 0 && 
1537                     (p
->p_lflag 
& P_LTRACED 
|| uap
->options 
& WUNTRACED
)) { 
1540                         if ((error 
= mac_proc_check_wait(q
, p
)) != 0) 
1544                         p
->p_lflag 
|= P_LWAITED
; 
1546                         retval
[0] = p
->p_pid
; 
1548                                 status 
= W_STOPCODE(p
->p_xstat
); 
1549                                 error 
= copyout((caddr_t
)&status
, 
1557                  * If we are waiting for continued processses, and this 
1558                  * process was continued 
1560                 if ((uap
->options 
& WCONTINUED
) && 
1561                     (p
->p_flag 
& P_CONTINUED
)) { 
1564                         if ((error 
= mac_proc_check_wait(q
, p
)) != 0) 
1568                         /* Prevent other process for waiting for this event */ 
1569                         OSBitAndAtomic(~((uint32_t)P_CONTINUED
), &p
->p_flag
); 
1570                         retval
[0] = p
->p_pid
; 
1572                                 status 
= W_STOPCODE(SIGCONT
); 
1573                                 error 
= copyout((caddr_t
)&status
, 
1580                 p
->p_listflag 
&= ~P_LIST_WAITING
; 
1583         /* list lock is held when we get here any which way */ 
1589         if (uap
->options 
& WNOHANG
) { 
1595         /* Save arguments for continuation. Backing storage is in uthread->uu_arg, and will not be deallocated */ 
1596         uth 
= current_uthread(); 
1597         wait4_data 
= &uth
->uu_kevent
.uu_wait4_data
; 
1598         wait4_data
->args 
= uap
; 
1599         wait4_data
->retval 
= retval
; 
1601         if ((error 
= msleep0((caddr_t
)q
, proc_list_mlock
, PWAIT 
| PCATCH 
| PDROP
, "wait", 0, wait1continue
))) 
1607         p
->p_listflag 
&= ~P_LIST_WAITING
; 
1614 #define ASSERT_LCK_MTX_OWNED(lock)      \ 
1615                                 lck_mtx_assert(lock, LCK_MTX_ASSERT_OWNED) 
1617 #define ASSERT_LCK_MTX_OWNED(lock)      /* nothing */ 
1621 waitidcontinue(int result
) 
1626         struct _waitid_data 
*waitid_data
; 
1627         struct waitid_nocancel_args 
*uap
; 
1634         thread 
= current_thread(); 
1635         uth 
= (struct uthread 
*)get_bsdthread_info(thread
); 
1637         waitid_data 
= &uth
->uu_kevent
.uu_waitid_data
; 
1638         uap 
= waitid_data
->args
; 
1639         retval 
= waitid_data
->retval
; 
1640         return(waitid_nocancel(p
, uap
, retval
)); 
1644  * Description: Suspend the calling thread until one child of the process 
1645  *              containing the calling thread changes state. 
1647  * Parameters:  uap->idtype             one of P_PID, P_PGID, P_ALL 
1648  *              uap->id                 pid_t or gid_t or ignored 
1649  *              uap->infop              Address of siginfo_t struct in 
1650  *                                      user space into which to return status 
1651  *              uap->options            flag values 
1653  * Returns:     0                       Success 
1654  *              !0                      Error returning status to user space 
1657 waitid(proc_t q
, struct waitid_args 
*uap
, int32_t *retval
) 
1659         __pthread_testcancel(1); 
1660         return (waitid_nocancel(q
, (struct waitid_nocancel_args 
*)uap
, retval
)); 
1664 waitid_nocancel(proc_t q
, struct waitid_nocancel_args 
*uap
, 
1665         __unused 
int32_t *retval
) 
1667         user_siginfo_t  siginfo
;        /* siginfo data to return to caller */ 
1668         boolean_t caller64 
= IS_64BIT_PROCESS(q
); 
1673         struct _waitid_data 
*waitid_data
; 
1675         if (uap
->options 
== 0 || 
1676             (uap
->options 
& ~(WNOHANG
|WNOWAIT
|WCONTINUED
|WSTOPPED
|WEXITED
))) 
1677                 return (EINVAL
);        /* bits set that aren't recognized */ 
1679         switch (uap
->idtype
) { 
1680         case P_PID
:     /* child with process ID equal to... */ 
1681         case P_PGID
:    /* child with process group ID equal to... */ 
1682                 if (((int)uap
->id
) < 0) 
1685         case P_ALL
:     /* any child */ 
1693         for (p 
= q
->p_children
.lh_first
; p 
!= 0; p 
= p
->p_sibling
.le_next
) { 
1695                 switch (uap
->idtype
) { 
1696                 case P_PID
:     /* child with process ID equal to... */ 
1697                         if (p
->p_pid 
!= (pid_t
)uap
->id
) 
1700                 case P_PGID
:    /* child with process group ID equal to... */ 
1701                         if (p
->p_pgrpid 
!= (pid_t
)uap
->id
) 
1704                 case P_ALL
:     /* any child */ 
1708                 /* XXX This is racy because we don't get the lock!!!! */ 
1711                  * Wait collision; go to sleep and restart; used to maintain 
1712                  * the single return for waited process guarantee. 
1714                 if (p
->p_listflag 
& P_LIST_WAITING
) { 
1715                         (void) msleep(&p
->p_stat
, proc_list_mlock
, 
1716                                 PWAIT
, "waitidcoll", 0); 
1719                 p
->p_listflag 
|= P_LIST_WAITING
;                /* mark busy */ 
1723                 bzero(&siginfo
, sizeof (siginfo
)); 
1725                 switch (p
->p_stat
) { 
1726                 case SZOMB
:             /* Exited */ 
1727                         if (!(uap
->options 
& WEXITED
)) 
1731                         if ((error 
= mac_proc_check_wait(q
, p
)) != 0) 
1734                         siginfo
.si_signo 
= SIGCHLD
; 
1735                         siginfo
.si_pid 
= p
->p_pid
; 
1736                         siginfo
.si_status 
= WEXITSTATUS(p
->p_xstat
); 
1737                         if (WIFSIGNALED(p
->p_xstat
)) { 
1738                                 siginfo
.si_code 
= WCOREDUMP(p
->p_xstat
) ? 
1739                                         CLD_DUMPED 
: CLD_KILLED
; 
1741                                 siginfo
.si_code 
= CLD_EXITED
; 
1743                         if ((error 
= copyoutsiginfo(&siginfo
, 
1744                             caller64
, uap
->infop
)) != 0) 
1747                         /* Prevent other process for waiting for this event? */ 
1748                         if (!(uap
->options 
& WNOWAIT
)) { 
1749                                 (void) reap_child_locked(q
, p
, 0, 0, 0, 0); 
1754                 case SSTOP
:             /* Stopped */ 
1756                          * If we are not interested in stopped processes, then 
1759                         if (!(uap
->options 
& WSTOPPED
)) 
1763                          * If someone has already waited it, we lost a race 
1764                          * to be the one to return status. 
1766                         if ((p
->p_lflag 
& P_LWAITED
) != 0) 
1770                         if ((error 
= mac_proc_check_wait(q
, p
)) != 0) 
1773                         siginfo
.si_signo 
= SIGCHLD
; 
1774                         siginfo
.si_pid 
= p
->p_pid
; 
1775                         siginfo
.si_status 
= p
->p_xstat
; /* signal number */ 
1776                         siginfo
.si_code 
= CLD_STOPPED
; 
1778                         if ((error 
= copyoutsiginfo(&siginfo
, 
1779                             caller64
, uap
->infop
)) != 0) 
1782                         /* Prevent other process for waiting for this event? */ 
1783                         if (!(uap
->options 
& WNOWAIT
)) { 
1785                                 p
->p_lflag 
|= P_LWAITED
; 
1790                 default:                /* All other states => Continued */ 
1791                         if (!(uap
->options 
& WCONTINUED
)) 
1795                          * If the flag isn't set, then this process has not 
1796                          * been stopped and continued, or the status has 
1797                          * already been reaped by another caller of waitid(). 
1799                         if ((p
->p_flag 
& P_CONTINUED
) == 0) 
1803                         if ((error 
= mac_proc_check_wait(q
, p
)) != 0) 
1806                         siginfo
.si_signo 
= SIGCHLD
; 
1807                         siginfo
.si_code 
= CLD_CONTINUED
; 
1809                         siginfo
.si_pid 
= p
->p_contproc
; 
1810                         siginfo
.si_status 
= p
->p_xstat
; 
1813                         if ((error 
= copyoutsiginfo(&siginfo
, 
1814                             caller64
, uap
->infop
)) != 0) 
1817                         /* Prevent other process for waiting for this event? */ 
1818                         if (!(uap
->options 
& WNOWAIT
)) { 
1819                                 OSBitAndAtomic(~((uint32_t)P_CONTINUED
), 
1824                 ASSERT_LCK_MTX_OWNED(proc_list_mlock
); 
1826                 /* Not a process we are interested in; go on to next child */ 
1828                 p
->p_listflag 
&= ~P_LIST_WAITING
; 
1831         ASSERT_LCK_MTX_OWNED(proc_list_mlock
); 
1833         /* No child processes that could possibly satisfy the request? */ 
1840         if (uap
->options 
& WNOHANG
) { 
1843                 if ((error 
= mac_proc_check_wait(q
, p
)) != 0) 
1847                  * The state of the siginfo structure in this case 
1848                  * is undefined.  Some implementations bzero it, some 
1849                  * (like here) leave it untouched for efficiency. 
1851                  * Thus the most portable check for "no matching pid with 
1852                  * WNOHANG" is to store a zero into si_pid before 
1853                  * invocation, then check for a non-zero value afterwards. 
1858         /* Save arguments for continuation. Backing storage is in uthread->uu_arg, and will not be deallocated */ 
1859         uth 
= current_uthread(); 
1860         waitid_data 
= &uth
->uu_kevent
.uu_waitid_data
; 
1861         waitid_data
->args 
= uap
; 
1862         waitid_data
->retval 
= retval
; 
1864         if ((error 
= msleep0(q
, proc_list_mlock
, 
1865             PWAIT 
| PCATCH 
| PDROP
, "waitid", 0, waitidcontinue
)) != 0) 
1871         p
->p_listflag 
&= ~P_LIST_WAITING
; 
1878  * make process 'parent' the new parent of process 'child'. 
1881 proc_reparentlocked(proc_t child
, proc_t parent
, int cansignal
, int locked
) 
1883         proc_t oldparent 
= PROC_NULL
; 
1885         if (child
->p_pptr 
== parent
) 
1891         oldparent 
= child
->p_pptr
; 
1892 #if __PROC_INTERNAL_DEBUG 
1893         if (oldparent 
== PROC_NULL
) 
1894                 panic("proc_reparent: process %p does not have a parent\n", child
); 
1897         LIST_REMOVE(child
, p_sibling
); 
1898 #if __PROC_INTERNAL_DEBUG 
1899         if (oldparent
->p_childrencnt 
== 0) 
1900                 panic("process children count already 0\n"); 
1902         oldparent
->p_childrencnt
--; 
1903 #if __PROC_INTERNAL_DEBUG1 
1904         if (oldparent
->p_childrencnt 
< 0) 
1905                 panic("process children count -ve\n"); 
1907         LIST_INSERT_HEAD(&parent
->p_children
, child
, p_sibling
); 
1908         parent
->p_childrencnt
++;         
1909         child
->p_pptr 
= parent
; 
1910         child
->p_ppid 
= parent
->p_pid
; 
1914         if ((cansignal 
!= 0) && (initproc 
== parent
) && (child
->p_stat 
== SZOMB
)) 
1915                 psignal(initproc
, SIGCHLD
); 
1921  * Exit: deallocate address space and other resources, change proc state 
1922  * to zombie, and unlink proc from allproc and parent's lists.  Save exit 
1923  * status and rusage for wait().  Check for child processes and orphan them. 
1927 vfork_exit(proc_t p
, int rv
) 
1929         vfork_exit_internal(p
, rv
, 0); 
1933 vfork_exit_internal(proc_t p
, int rv
, int forceexit
) 
1935         thread_t self 
= current_thread(); 
1937         struct task 
*task 
= p
->task
; 
1942          * If a thread in this task has already 
1943          * called exit(), then halt any others 
1947          ut 
= get_bsdthread_info(self
); 
1951          if ((p
->p_lflag 
& P_LPEXIT
) == P_LPEXIT
) { 
1953                 * This happens when a parent exits/killed and vfork is in progress   
1954                 * other threads. But shutdown code for ex has already called exit1() 
1959         p
->p_lflag 
|= (P_LEXIT 
| P_LPEXIT
); 
1962         if (forceexit 
== 0) { 
1964                  * parent of a vfork child has already called exit() and the  
1965                  * thread that has vfork in proress terminates. So there is no 
1966                  * separate address space here and it has already been marked for 
1967                  * termination. This was never covered before and could cause problems 
1968                  * if we block here for outside code. 
1970                 /* Notify the perf server */ 
1971                 (void)sys_perf_notify(self
, p
->p_pid
); 
1975          * Remove proc from allproc queue and from pidhash chain. 
1976          * Need to do this before we do anything that can block. 
1977          * Not doing causes things like mount() find this on allproc 
1978          * in partially cleaned state. 
1983 #if CONFIG_MEMORYSTATUS 
1984         memorystatus_remove(p
, TRUE
); 
1987         LIST_REMOVE(p
, p_list
); 
1988         LIST_INSERT_HEAD(&zombproc
, p
, p_list
); /* Place onto zombproc. */ 
1989         /* will not be visible via proc_find */ 
1990         p
->p_listflag 
|= P_LIST_EXITED
; 
1996         p
->p_lflag 
&= ~(P_LTRACED 
| P_LPPWAIT
); 
1997         p
->p_sigignore 
= ~0; 
2001         if (thread_call_cancel(p
->p_rcall
)) 
2004         while (p
->p_ractive 
> 0) { 
2013         thread_call_free(p
->p_rcall
); 
2022 vproc_exit(proc_t p
) 
2029         struct task 
*task 
= p
->task
; 
2032         struct session 
*sessp
; 
2033         struct rusage_superset 
*rup
; 
2035         /* XXX Zombie allocation may fail, in which case stats get lost */ 
2036         MALLOC_ZONE(rup
, struct rusage_superset 
*, 
2037                         sizeof (*rup
), M_ZOMBIE
, M_WAITOK
); 
2042          * Close open files and release open-file table. 
2047         sessp 
= proc_session(p
); 
2048         if (SESS_LEADER(p
, sessp
)) { 
2050                 if (sessp
->s_ttyvp 
!= NULLVP
) { 
2051                         struct vnode 
*ttyvp
; 
2054                         struct vfs_context context
; 
2058                          * Controlling process. 
2059                          * Signal foreground pgrp, 
2060                          * drain controlling terminal 
2061                          * and revoke access to controlling terminal. 
2063                         session_lock(sessp
); 
2064                         tp 
= SESSION_TP(sessp
); 
2065                         if ((tp 
!= TTY_NULL
) && (tp
->t_session 
== sessp
)) { 
2066                                 session_unlock(sessp
); 
2069                                  * We're going to SIGHUP the foreground process 
2070                                  * group. It can't change from this point on 
2071                                  * until the revoke is complete. 
2072                                  * The process group changes under both the tty 
2073                                  * lock and proc_list_lock but we need only one 
2079                                 tty_pgsignal(tp
, SIGHUP
, 1); 
2081                                 session_lock(sessp
); 
2082                                 tp 
= SESSION_TP(sessp
); 
2084                         cttyflag 
= sessp
->s_flags 
& S_CTTYREF
; 
2085                         sessp
->s_flags 
&= ~S_CTTYREF
; 
2086                         ttyvp 
= sessp
->s_ttyvp
; 
2087                         ttyvid 
= sessp
->s_ttyvid
; 
2088                         sessp
->s_ttyvp 
= NULL
; 
2089                         sessp
->s_ttyvid 
= 0; 
2090                         sessp
->s_ttyp 
= TTY_NULL
; 
2091                         sessp
->s_ttypgrpid 
= NO_PID
; 
2092                         session_unlock(sessp
); 
2094                        if ((ttyvp 
!= NULLVP
) && (vnode_getwithvid(ttyvp
, ttyvid
) == 0)) { 
2095                                 if (tp 
!= TTY_NULL
) { 
2100                                 context
.vc_thread 
= proc_thread(p
); /* XXX */ 
2101                                 context
.vc_ucred 
= kauth_cred_proc_ref(p
); 
2102                                 VNOP_REVOKE(ttyvp
, REVOKEALL
, &context
); 
2105                                          * Release the extra usecount taken in cttyopen. 
2106                                          * usecount should be released after VNOP_REVOKE is called. 
2107                                          * This usecount was taken to ensure that 
2108                                          * the VNOP_REVOKE results in a close to 
2109                                          * the tty since cttyclose is a no-op. 
2114                                 kauth_cred_unref(&context
.vc_ucred
); 
2119                                  * This is cleared even if not set. This is also done in 
2120                                  * spec_close to ensure that the flag is cleared. 
2129                 session_lock(sessp
); 
2130                 sessp
->s_leader 
= NULL
; 
2131                 session_unlock(sessp
); 
2133         session_rele(sessp
); 
2139         p
->p_rlimit
[RLIMIT_FSIZE
].rlim_cur 
= RLIM_INFINITY
; 
2142         proc_childdrainstart(p
); 
2143         while ((q 
= p
->p_children
.lh_first
) != NULL
) { 
2144                 if (q
->p_stat 
== SZOMB
) { 
2146                                 panic("parent child linkage broken"); 
2147                         /* check for lookups by zomb sysctl */ 
2148                         while ((q
->p_listflag 
& P_LIST_WAITING
) == P_LIST_WAITING
) { 
2149                                 msleep(&q
->p_stat
, proc_list_mlock
, PWAIT
, "waitcoll", 0); 
2151                         q
->p_listflag 
|= P_LIST_WAITING
; 
2153                          * This is a named reference and it is not granted 
2154                          * if the reap is already in progress. So we get 
2155                          * the reference here exclusively and their can be 
2156                          * no waiters. So there is no need for a wakeup 
2157                          * after we are done. AlsO  the reap frees the structure 
2158                          * and the proc struct cannot be used for wakeups as well.  
2159                          * It is safe to use q here as this is system reap 
2161                         (void)reap_child_locked(p
, q
, 1, 0, 1, 0); 
2164                         * Traced processes are killed 
2165                         * since their existence means someone is messing up. 
2167                         if (q
->p_lflag 
& P_LTRACED
) { 
2172                                 opp 
= proc_find(q
->p_oppid
); 
2173                                 if (opp 
!= PROC_NULL
) { 
2177                                         proc_reparentlocked(q
, opp
, 0, 0); 
2180                                         /* original parent exited while traced */ 
2182                                         q
->p_listflag 
|= P_LIST_DEADPARENT
; 
2185                                         proc_reparentlocked(q
, initproc
, 0, 0); 
2189                                 q
->p_lflag 
&= ~P_LTRACED
; 
2191                                 if (q
->sigwait_thread
) { 
2192                                         thread_t thread 
= q
->sigwait_thread
; 
2196                                         * The sigwait_thread could be stopped at a 
2197                                         * breakpoint. Wake it up to kill. 
2198                                         * Need to do this as it could be a thread which is not 
2199                                         * the first thread in the task. So any attempts to kill 
2200                                         * the process would result into a deadlock on q->sigwait. 
2202                                         thread_resume(thread
); 
2203                                         clear_wait(thread
, THREAD_INTERRUPTED
); 
2204                                         threadsignal(thread
, SIGKILL
, 0); 
2209                                 psignal(q
, SIGKILL
); 
2212                                 q
->p_listflag 
|= P_LIST_DEADPARENT
; 
2213                                 proc_reparentlocked(q
, initproc
, 0, 1); 
2218         proc_childdrainend(p
); 
2222          * Release reference to text vnode 
2226         if (tvp 
!= NULLVP
) { 
2231          * Save exit status and final rusage info, adding in child rusage 
2232          * info and self times.  If we were unable to allocate a zombie 
2233          * structure, this information is lost. 
2236             rup
->ru 
= p
->p_stats
->p_ru
; 
2237             timerclear(&rup
->ru
.ru_utime
); 
2238             timerclear(&rup
->ru
.ru_stime
); 
2242                 mach_task_basic_info_data_t tinfo
; 
2243                 task_thread_times_info_data_t ttimesinfo
; 
2244                 int task_info_stuff
, task_ttimes_stuff
; 
2245                 struct timeval ut
,st
; 
2247                 task_info_stuff 
= MACH_TASK_BASIC_INFO_COUNT
; 
2248                 task_info(task
, MACH_TASK_BASIC_INFO
, 
2249                           &tinfo
, &task_info_stuff
); 
2250                 p
->p_ru
->ru
.ru_utime
.tv_sec 
= tinfo
.user_time
.seconds
; 
2251                 p
->p_ru
->ru
.ru_utime
.tv_usec 
= tinfo
.user_time
.microseconds
; 
2252                 p
->p_ru
->ru
.ru_stime
.tv_sec 
= tinfo
.system_time
.seconds
; 
2253                 p
->p_ru
->ru
.ru_stime
.tv_usec 
= tinfo
.system_time
.microseconds
; 
2255                 task_ttimes_stuff 
= TASK_THREAD_TIMES_INFO_COUNT
; 
2256                 task_info(task
, TASK_THREAD_TIMES_INFO
, 
2257                           &ttimesinfo
, &task_ttimes_stuff
); 
2259                 ut
.tv_sec 
= ttimesinfo
.user_time
.seconds
; 
2260                 ut
.tv_usec 
= ttimesinfo
.user_time
.microseconds
; 
2261                 st
.tv_sec 
= ttimesinfo
.system_time
.seconds
; 
2262                 st
.tv_usec 
= ttimesinfo
.system_time
.microseconds
; 
2263                 timeradd(&ut
,&p
->p_ru
->ru
.ru_utime
,&p
->p_ru
->ru
.ru_utime
); 
2264                         timeradd(&st
,&p
->p_ru
->ru
.ru_stime
,&p
->p_ru
->ru
.ru_stime
); 
2268             ruadd(&rup
->ru
, &p
->p_stats
->p_cru
); 
2270                 gather_rusage_info(p
, &rup
->ri
, RUSAGE_INFO_CURRENT
); 
2271                 rup
->ri
.ri_phys_footprint 
= 0; 
2272                 rup
->ri
.ri_proc_exit_abstime 
= mach_absolute_time(); 
2275                  * Now that we have filled in the rusage info, make it 
2276                  * visible to an external observer via proc_pid_rusage(). 
2282          * Free up profiling buffers. 
2285                 struct uprof 
*p0 
= &p
->p_stats
->p_prof
, *p1
, *pn
; 
2291                 for (; p1 
!= NULL
; p1 
= pn
) { 
2293                         kfree(p1
, sizeof *p1
); 
2298         pth_proc_hashdelete(p
); 
2302          * Other substructures are freed from wait(). 
2304         FREE_ZONE(p
->p_stats
, sizeof *p
->p_stats
, M_PSTATS
); 
2307         FREE_ZONE(p
->p_sigacts
, sizeof *p
->p_sigacts
, M_SIGACTS
); 
2308         p
->p_sigacts 
= NULL
; 
2310         proc_limitdrop(p
, 1); 
2314          * Finish up by terminating the task 
2315          * and halt this thread (only if a 
2316          * member of the task exiting). 
2318         p
->task 
= TASK_NULL
; 
2321          * Notify parent that we're gone. 
2323         pp 
= proc_parent(p
); 
2324         if ((p
->p_listflag 
& P_LIST_DEADPARENT
) == 0) { 
2325                 if (pp 
!= initproc
) { 
2327                         pp
->si_pid 
= p
->p_pid
; 
2328                         pp
->si_status 
= p
->p_xstat
; 
2329                         pp
->si_code 
= CLD_EXITED
; 
2331                          * p_ucred usage is safe as it is an exiting process 
2332                          * and reference is dropped in reap 
2334                         pp
->si_uid 
= kauth_cred_getruid(p
->p_ucred
); 
2337                 /* mark as a zombie */ 
2338                 /* mark as a zombie */ 
2339                 /* No need to take proc lock as all refs are drained and 
2340                  * no one except parent (reaping ) can look at this. 
2341                  * The write is to an int and is coherent. Also parent is 
2342                  *  keyed off of list lock for reaping 
2346                 psignal(pp
, SIGCHLD
); 
2348                 /* and now wakeup the parent */ 
2350                 wakeup((caddr_t
)pp
); 
2354                 /* check for lookups by zomb sysctl */ 
2355                 while ((p
->p_listflag 
& P_LIST_WAITING
) == P_LIST_WAITING
) { 
2356                         msleep(&p
->p_stat
, proc_list_mlock
, PWAIT
, "waitcoll", 0); 
2359                 p
->p_listflag 
|= P_LIST_WAITING
; 
2362                  * This is a named reference and it is not granted 
2363                  * if the reap is already in progress. So we get 
2364                  * the reference here exclusively and their can be 
2365                  * no waiters. So there is no need for a wakeup 
2366                  * after we are done. AlsO  the reap frees the structure 
2367                  * and the proc struct cannot be used for wakeups as well.  
2368                  * It is safe to use p here as this is system reap 
2370                 (void)reap_child_locked(pp
, p
, 0, 0, 1, 1); 
2371                 /* list lock dropped by reap_child_locked */ 
2379  *      LP64 support - long is 64 bits if we are dealing with a 64 bit user 
2380  *      process.  We munge the kernel version of rusage into the 
2383 __private_extern__  
void  
2384 munge_user64_rusage(struct rusage 
*a_rusage_p
, struct user64_rusage 
*a_user_rusage_p
) 
2386         /* timeval changes size, so utime and stime need special handling */ 
2387         a_user_rusage_p
->ru_utime
.tv_sec 
= a_rusage_p
->ru_utime
.tv_sec
; 
2388         a_user_rusage_p
->ru_utime
.tv_usec 
= a_rusage_p
->ru_utime
.tv_usec
; 
2389         a_user_rusage_p
->ru_stime
.tv_sec 
= a_rusage_p
->ru_stime
.tv_sec
; 
2390         a_user_rusage_p
->ru_stime
.tv_usec 
= a_rusage_p
->ru_stime
.tv_usec
; 
2392          * everything else can be a direct assign, since there is no loss 
2393          * of precision implied boing 32->64. 
2395         a_user_rusage_p
->ru_maxrss 
= a_rusage_p
->ru_maxrss
; 
2396         a_user_rusage_p
->ru_ixrss 
= a_rusage_p
->ru_ixrss
; 
2397         a_user_rusage_p
->ru_idrss 
= a_rusage_p
->ru_idrss
; 
2398         a_user_rusage_p
->ru_isrss 
= a_rusage_p
->ru_isrss
; 
2399         a_user_rusage_p
->ru_minflt 
= a_rusage_p
->ru_minflt
; 
2400         a_user_rusage_p
->ru_majflt 
= a_rusage_p
->ru_majflt
; 
2401         a_user_rusage_p
->ru_nswap 
= a_rusage_p
->ru_nswap
; 
2402         a_user_rusage_p
->ru_inblock 
= a_rusage_p
->ru_inblock
; 
2403         a_user_rusage_p
->ru_oublock 
= a_rusage_p
->ru_oublock
; 
2404         a_user_rusage_p
->ru_msgsnd 
= a_rusage_p
->ru_msgsnd
; 
2405         a_user_rusage_p
->ru_msgrcv 
= a_rusage_p
->ru_msgrcv
; 
2406         a_user_rusage_p
->ru_nsignals 
= a_rusage_p
->ru_nsignals
; 
2407         a_user_rusage_p
->ru_nvcsw 
= a_rusage_p
->ru_nvcsw
; 
2408         a_user_rusage_p
->ru_nivcsw 
= a_rusage_p
->ru_nivcsw
; 
2411 /* For a 64-bit kernel and 32-bit userspace, munging may be needed */ 
2412 __private_extern__  
void  
2413 munge_user32_rusage(struct rusage 
*a_rusage_p
, struct user32_rusage 
*a_user_rusage_p
) 
2415         /* timeval changes size, so utime and stime need special handling */ 
2416         a_user_rusage_p
->ru_utime
.tv_sec 
= a_rusage_p
->ru_utime
.tv_sec
; 
2417         a_user_rusage_p
->ru_utime
.tv_usec 
= a_rusage_p
->ru_utime
.tv_usec
; 
2418         a_user_rusage_p
->ru_stime
.tv_sec 
= a_rusage_p
->ru_stime
.tv_sec
; 
2419         a_user_rusage_p
->ru_stime
.tv_usec 
= a_rusage_p
->ru_stime
.tv_usec
; 
2421          * everything else can be a direct assign. We currently ignore 
2422          * the loss of precision 
2424         a_user_rusage_p
->ru_maxrss 
= a_rusage_p
->ru_maxrss
; 
2425         a_user_rusage_p
->ru_ixrss 
= a_rusage_p
->ru_ixrss
; 
2426         a_user_rusage_p
->ru_idrss 
= a_rusage_p
->ru_idrss
; 
2427         a_user_rusage_p
->ru_isrss 
= a_rusage_p
->ru_isrss
; 
2428         a_user_rusage_p
->ru_minflt 
= a_rusage_p
->ru_minflt
; 
2429         a_user_rusage_p
->ru_majflt 
= a_rusage_p
->ru_majflt
; 
2430         a_user_rusage_p
->ru_nswap 
= a_rusage_p
->ru_nswap
; 
2431         a_user_rusage_p
->ru_inblock 
= a_rusage_p
->ru_inblock
; 
2432         a_user_rusage_p
->ru_oublock 
= a_rusage_p
->ru_oublock
; 
2433         a_user_rusage_p
->ru_msgsnd 
= a_rusage_p
->ru_msgsnd
; 
2434         a_user_rusage_p
->ru_msgrcv 
= a_rusage_p
->ru_msgrcv
; 
2435         a_user_rusage_p
->ru_nsignals 
= a_rusage_p
->ru_nsignals
; 
2436         a_user_rusage_p
->ru_nvcsw 
= a_rusage_p
->ru_nvcsw
; 
2437         a_user_rusage_p
->ru_nivcsw 
= a_rusage_p
->ru_nivcsw
;