2 * Copyright (c) 2000-2011 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>
98 #include <sys/aio_kern.h>
99 #include <sys/sysproto.h>
100 #include <sys/signalvar.h>
101 #include <sys/kdebug.h>
102 #include <sys/filedesc.h> /* fdfree */
104 #include <sys/shm_internal.h> /* shmexit */
106 #include <sys/acct.h> /* acct_process */
108 #include <security/audit/audit.h>
109 #include <bsm/audit_kevents.h>
111 #include <mach/mach_types.h>
113 #include <kern/kern_types.h>
114 #include <kern/kalloc.h>
115 #include <kern/task.h>
116 #include <kern/thread.h>
117 #include <kern/thread_call.h>
118 #include <kern/sched_prim.h>
119 #include <kern/assert.h>
120 #include <sys/codesign.h>
122 #if VM_PRESSURE_EVENTS
123 #include <kern/vm_pressure.h>
126 #if CONFIG_MEMORYSTATUS
127 #include <sys/kern_memorystatus.h>
131 /* Do not include dtrace.h, it redefines kmem_[alloc/free] */
132 extern void (*dtrace_fasttrap_exit_ptr
)(proc_t
);
133 extern void (*dtrace_helpers_cleanup
)(proc_t
);
134 extern void dtrace_lazy_dofs_destroy(proc_t
);
136 #include <sys/dtrace_ptss.h>
140 #include <security/mac.h>
141 #include <sys/syscall.h>
144 #include <mach/mach_types.h>
145 #include <mach/task.h>
146 #include <mach/thread_act.h>
148 #include <vm/vm_protos.h>
152 extern char init_task_failure_data
[];
153 void proc_prepareexit(proc_t p
, int rv
, boolean_t perf_notify
);
154 void vfork_exit(proc_t p
, int rv
);
155 void vproc_exit(proc_t p
);
156 __private_extern__
void munge_user64_rusage(struct rusage
*a_rusage_p
, struct user64_rusage
*a_user_rusage_p
);
157 __private_extern__
void munge_user32_rusage(struct rusage
*a_rusage_p
, struct user32_rusage
*a_user_rusage_p
);
158 static int reap_child_locked(proc_t parent
, proc_t child
, int deadparent
, int reparentedtoinit
, int locked
, int droplock
);
161 * Things which should have prototypes in headers, but don't
163 void proc_exit(proc_t p
);
164 int wait1continue(int result
);
165 int waitidcontinue(int result
);
166 kern_return_t
sys_perf_notify(thread_t thread
, int pid
);
167 kern_return_t
task_exception_notify(exception_type_t exception
,
168 mach_exception_data_type_t code
, mach_exception_data_type_t subcode
);
170 void gather_rusage_info(proc_t p
, rusage_info_current
*ru
, int flavor
);
173 * NOTE: Source and target may *NOT* overlap!
174 * XXX Should share code with bsd/dev/ppc/unix_signal.c
177 siginfo_user_to_user32(user_siginfo_t
*in
, user32_siginfo_t
*out
)
179 out
->si_signo
= in
->si_signo
;
180 out
->si_errno
= in
->si_errno
;
181 out
->si_code
= in
->si_code
;
182 out
->si_pid
= in
->si_pid
;
183 out
->si_uid
= in
->si_uid
;
184 out
->si_status
= in
->si_status
;
185 out
->si_addr
= CAST_DOWN_EXPLICIT(user32_addr_t
,in
->si_addr
);
186 /* following cast works for sival_int because of padding */
187 out
->si_value
.sival_ptr
= CAST_DOWN_EXPLICIT(user32_addr_t
,in
->si_value
.sival_ptr
);
188 out
->si_band
= in
->si_band
; /* range reduction */
192 siginfo_user_to_user64(user_siginfo_t
*in
, user64_siginfo_t
*out
)
194 out
->si_signo
= in
->si_signo
;
195 out
->si_errno
= in
->si_errno
;
196 out
->si_code
= in
->si_code
;
197 out
->si_pid
= in
->si_pid
;
198 out
->si_uid
= in
->si_uid
;
199 out
->si_status
= in
->si_status
;
200 out
->si_addr
= in
->si_addr
;
201 /* following cast works for sival_int because of padding */
202 out
->si_value
.sival_ptr
= in
->si_value
.sival_ptr
;
203 out
->si_band
= in
->si_band
; /* range reduction */
207 copyoutsiginfo(user_siginfo_t
*native
, boolean_t is64
, user_addr_t uaddr
)
210 user64_siginfo_t sinfo64
;
212 bzero(&sinfo64
, sizeof (sinfo64
));
213 siginfo_user_to_user64(native
, &sinfo64
);
214 return (copyout(&sinfo64
, uaddr
, sizeof (sinfo64
)));
216 user32_siginfo_t sinfo32
;
218 bzero(&sinfo32
, sizeof (sinfo32
));
219 siginfo_user_to_user32(native
, &sinfo32
);
220 return (copyout(&sinfo32
, uaddr
, sizeof (sinfo32
)));
229 exit(proc_t p
, struct exit_args
*uap
, int *retval
)
231 exit1(p
, W_EXITCODE(uap
->rval
, 0), retval
);
233 thread_exception_return();
236 thread_block(THREAD_CONTINUE_NULL
);
241 * Exit: deallocate address space and other resources, change proc state
242 * to zombie, and unlink proc from allproc and parent's lists. Save exit
243 * status and rusage for wait(). Check for child processes and orphan them.
246 exit1(proc_t p
, int rv
, int *retval
)
248 return exit1_internal(p
, rv
, retval
, TRUE
, TRUE
, 0);
252 exit1_internal(proc_t p
, int rv
, int *retval
, boolean_t thread_can_terminate
, boolean_t perf_notify
,
255 thread_t self
= current_thread();
256 struct task
*task
= p
->task
;
261 * If a thread in this task has already
262 * called exit(), then halt any others
266 ut
= get_bsdthread_info(self
);
267 if (ut
->uu_flag
& UT_VFORK
) {
268 if (!thread_can_terminate
) {
273 vfork_return(p
, retval
, p
->p_pid
);
274 unix_syscall_return(0);
279 * The parameter list of audit_syscall_exit() was augmented to
280 * take the Darwin syscall number as the first parameter,
281 * which is currently required by mac_audit_postselect().
285 * The BSM token contains two components: an exit status as passed
286 * to exit(), and a return value to indicate what sort of exit it
287 * was. The exit status is WEXITSTATUS(rv), but it's not clear
288 * what the return value is.
290 AUDIT_ARG(exit
, WEXITSTATUS(rv
), 0);
291 AUDIT_SYSCALL_EXIT(SYS_exit
, p
, ut
, 0); /* Exit is always successfull */
293 DTRACE_PROC1(exit
, int, CLD_EXITED
);
295 /* mark process is going to exit and pull out of DBG/disk throttle */
296 /* TODO: This should be done after becoming exit thread */
297 proc_set_task_policy(p
->task
, THREAD_NULL
, TASK_POLICY_ATTRIBUTE
,
298 TASK_POLICY_TERMINATED
, TASK_POLICY_ENABLE
);
301 error
= proc_transstart(p
, 1, ((jetsam_flags
& P_JETSAM_VNODE
) ? 1 : 0));
302 if (error
== EDEADLK
) {
303 /* Temp: If deadlock error, then it implies multithreaded exec is
304 * in progress. Instread of letting exit continue and
305 * corrupting the freed memory, let the exit thread
306 * return. This will save corruption in remote case.
309 if (current_proc() == p
){
310 if (p
->exit_thread
== self
)
311 printf("exit_thread failed to exit, leaving process %s[%d] in unkillable limbo\n",
312 p
->p_comm
, p
->p_pid
);
313 thread_exception_return();
315 /* external termination like jetsam */
320 while (p
->exit_thread
!= self
) {
321 if (sig_try_locked(p
) <= 0) {
323 if (get_threadtask(self
) != task
) {
329 thread_terminate(self
);
330 if (!thread_can_terminate
) {
334 thread_exception_return();
339 if (p
== initproc
&& current_proc() == p
) {
341 printf("pid 1 exited (signal %d, exit %d)",
342 WTERMSIG(rv
), WEXITSTATUS(rv
));
343 #if (DEVELOPMENT || DEBUG)
346 * For debugging purposes, generate a core file of initproc before
347 * panicking. Leave at least 300 MB free on the root volume, and ignore
348 * the process's corefile ulimit.
350 if ((err
= coredump(p
, 300, 1)) != 0) {
351 printf("Failed to generate initproc core file: error %d", err
);
353 printf("Generated initproc core file");
354 sync(p
, (void *)NULL
, (int *)NULL
);
357 panic("%s died\nState at Last Exception:\n\n%s",
358 (p
->p_comm
[0] != '\0' ?
361 init_task_failure_data
);
364 p
->p_lflag
|= P_LEXIT
;
366 p
->p_lflag
|= jetsam_flags
;
371 proc_prepareexit(p
, rv
, perf_notify
);
373 /* Last thread to terminate will call proc_exit() */
374 task_terminate_internal(task
);
380 proc_prepareexit(proc_t p
, int rv
, boolean_t perf_notify
)
382 mach_exception_data_type_t code
, subcode
;
384 thread_t self
= current_thread();
385 ut
= get_bsdthread_info(self
);
386 struct rusage_superset
*rup
;
388 /* If a core should be generated, notify crash reporter */
389 if (hassigprop(WTERMSIG(rv
), SA_CORE
) || ((p
->p_csflags
& CS_KILLED
) != 0)) {
391 * Workaround for processes checking up on PT_DENY_ATTACH:
392 * should be backed out post-Leopard (details in 5431025).
394 if ((SIGSEGV
== WTERMSIG(rv
)) &&
395 (p
->p_pptr
->p_lflag
& P_LNOATTACH
)) {
400 * Crash Reporter looks for the signal value, original exception
401 * type, and low 20 bits of the original code in code[0]
402 * (8, 4, and 20 bits respectively). code[1] is unmodified.
404 code
= ((WTERMSIG(rv
) & 0xff) << 24) |
405 ((ut
->uu_exception
& 0x0f) << 20) |
406 ((int)ut
->uu_code
& 0xfffff);
407 subcode
= ut
->uu_subcode
;
408 (void) task_exception_notify(EXC_CRASH
, code
, subcode
);
412 /* Notify the perf server? */
414 (void)sys_perf_notify(self
, p
->p_pid
);
418 * Before this process becomes a zombie, stash resource usage
419 * stats in the proc for external observers to query
420 * via proc_pid_rusage().
422 * If the zombie allocation fails, just punt the stats.
424 MALLOC_ZONE(rup
, struct rusage_superset
*,
425 sizeof (*rup
), M_ZOMBIE
, M_WAITOK
);
427 gather_rusage_info(p
, &rup
->ri
, RUSAGE_INFO_CURRENT
);
428 rup
->ri
.ri_phys_footprint
= 0;
429 rup
->ri
.ri_proc_exit_abstime
= mach_absolute_time();
432 * Make the rusage_info visible to external observers
433 * only after it has been completely filled in.
439 * Remove proc from allproc queue and from pidhash chain.
440 * Need to do this before we do anything that can block.
441 * Not doing causes things like mount() find this on allproc
442 * in partially cleaned state.
447 #if CONFIG_MEMORYSTATUS
448 memorystatus_remove(p
, TRUE
);
451 LIST_REMOVE(p
, p_list
);
452 LIST_INSERT_HEAD(&zombproc
, p
, p_list
); /* Place onto zombproc. */
453 /* will not be visible via proc_find */
454 p
->p_listflag
|= P_LIST_EXITED
;
463 * If parent is waiting for us to exit or exec,
464 * P_LPPWAIT is set; we will wakeup the parent below.
467 p
->p_lflag
&= ~(P_LTRACED
| P_LPPWAIT
);
468 p
->p_sigignore
= ~(sigcantmask
);
478 struct task
*task
= p
->task
;
479 vnode_t tvp
= NULLVP
;
481 struct session
*sessp
;
482 struct uthread
* uth
;
487 uth
= current_uthread();
490 proc_transstart(p
, 1, 0);
491 if( !(p
->p_lflag
& P_LEXIT
)) {
493 * This can happen if a thread_terminate() occurs
494 * in a single-threaded process.
496 p
->p_lflag
|= P_LEXIT
;
499 proc_prepareexit(p
, 0, TRUE
);
500 (void) task_terminate_internal(task
);
506 p
->p_lflag
|= P_LPEXIT
;
509 * Other kernel threads may be in the middle of signalling this process.
510 * Wait for those threads to wrap it up before making the process
513 if ((p
->p_lflag
& P_LINSIGNAL
) || (p
->p_sigwaitcnt
> 0)) {
515 while ((p
->p_lflag
& P_LINSIGNAL
) || (p
->p_sigwaitcnt
> 1))
516 msleep(&p
->p_sigmask
, &p
->p_mlock
, PWAIT
, "proc_sigdrain", NULL
);
522 exitval
= p
->p_xstat
;
523 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_COMMON
,
524 BSDDBG_CODE(DBG_BSD_PROC
, BSD_PROC_EXIT
) | DBG_FUNC_START
,
525 pid
, exitval
, 0, 0, 0);
529 * Free any outstanding lazy dof entries. It is imperative we
530 * always call dtrace_lazy_dofs_destroy, rather than null check
531 * and call if !NULL. If we NULL test, during lazy dof faulting
532 * we can race with the faulting code and proceed from here to
533 * beyond the helpers cleanup. The lazy dof faulting will then
534 * install new helpers which will never be cleaned up, and leak.
536 dtrace_lazy_dofs_destroy(p
);
539 * Clean up any DTrace helper actions or probes for the process.
541 if (p
->p_dtrace_helpers
!= NULL
) {
542 (*dtrace_helpers_cleanup
)(p
);
546 * Clean up any DTrace probes associated with this process.
549 * APPLE NOTE: We release ptss pages/entries in dtrace_fasttrap_exit_ptr(),
550 * call this after dtrace_helpers_cleanup()
553 if (p
->p_dtrace_probes
&& dtrace_fasttrap_exit_ptr
) {
554 (*dtrace_fasttrap_exit_ptr
)(p
);
561 #if VM_PRESSURE_EVENTS
562 vm_pressure_proc_cleanup(p
);
566 * need to cancel async IO requests that can be cancelled and wait for those
567 * already active. MAY BLOCK!
572 /* if any pending cpu limits action, clear it */
573 task_clear_cpuusage(p
->task
, TRUE
);
575 workqueue_mark_exiting(p
);
581 * Close open files and release open-file table.
586 if (uth
->uu_lowpri_window
) {
588 * task is marked as a low priority I/O type
589 * and the I/O we issued while in flushing files on close
590 * collided with normal I/O operations...
591 * no need to throttle this thread since its going away
592 * but we do need to update our bookeeping w/r to throttled threads
594 throttle_lowpri_io(0);
598 /* Close ref SYSV Shared memory*/
603 /* Release SYSV semaphores */
608 pth_proc_hashdelete(p
);
611 sessp
= proc_session(p
);
612 if (SESS_LEADER(p
, sessp
)) {
614 if (sessp
->s_ttyvp
!= NULLVP
) {
618 struct vfs_context context
;
622 * Controlling process.
623 * Signal foreground pgrp,
624 * drain controlling terminal
625 * and revoke access to controlling terminal.
628 tp
= SESSION_TP(sessp
);
629 if ((tp
!= TTY_NULL
) && (tp
->t_session
== sessp
)) {
630 session_unlock(sessp
);
633 * We're going to SIGHUP the foreground process
634 * group. It can't change from this point on
635 * until the revoke is complete.
636 * The process group changes under both the tty
637 * lock and proc_list_lock but we need only one
643 tty_pgsignal(tp
, SIGHUP
, 1);
646 tp
= SESSION_TP(sessp
);
648 cttyflag
= sessp
->s_flags
& S_CTTYREF
;
649 sessp
->s_flags
&= ~S_CTTYREF
;
650 ttyvp
= sessp
->s_ttyvp
;
651 ttyvid
= sessp
->s_ttyvid
;
652 sessp
->s_ttyvp
= NULLVP
;
654 sessp
->s_ttyp
= TTY_NULL
;
655 sessp
->s_ttypgrpid
= NO_PID
;
656 session_unlock(sessp
);
658 if ((ttyvp
!= NULLVP
) && (vnode_getwithvid(ttyvp
, ttyvid
) == 0)) {
659 if (tp
!= TTY_NULL
) {
664 context
.vc_thread
= proc_thread(p
); /* XXX */
665 context
.vc_ucred
= kauth_cred_proc_ref(p
);
666 VNOP_REVOKE(ttyvp
, REVOKEALL
, &context
);
669 * Release the extra usecount taken in cttyopen.
670 * usecount should be released after VNOP_REVOKE is called.
671 * This usecount was taken to ensure that
672 * the VNOP_REVOKE results in a close to
673 * the tty since cttyclose is a no-op.
678 kauth_cred_unref(&context
.vc_ucred
);
683 * This is cleared even if not set. This is also done in
684 * spec_close to ensure that the flag is cleared.
694 sessp
->s_leader
= NULL
;
695 session_unlock(sessp
);
703 p
->p_rlimit
[RLIMIT_FSIZE
].rlim_cur
= RLIM_INFINITY
;
704 (void)acct_process(p
);
708 if ((p
->p_listflag
& P_LIST_EXITCOUNT
) == P_LIST_EXITCOUNT
) {
709 p
->p_listflag
&= ~P_LIST_EXITCOUNT
;
710 proc_shutdown_exitcount
--;
711 if (proc_shutdown_exitcount
== 0)
712 wakeup(&proc_shutdown_exitcount
);
715 /* wait till parentrefs are dropped and grant no more */
716 proc_childdrainstart(p
);
717 while ((q
= p
->p_children
.lh_first
) != NULL
) {
718 int reparentedtoinit
= (q
->p_listflag
& P_LIST_DEADPARENT
) ? 1 : 0;
719 if (q
->p_stat
== SZOMB
) {
721 panic("parent child linkage broken");
722 /* check for sysctl zomb lookup */
723 while ((q
->p_listflag
& P_LIST_WAITING
) == P_LIST_WAITING
) {
724 msleep(&q
->p_stat
, proc_list_mlock
, PWAIT
, "waitcoll", 0);
726 q
->p_listflag
|= P_LIST_WAITING
;
728 * This is a named reference and it is not granted
729 * if the reap is already in progress. So we get
730 * the reference here exclusively and their can be
731 * no waiters. So there is no need for a wakeup
732 * after we are done. Also the reap frees the structure
733 * and the proc struct cannot be used for wakeups as well.
734 * It is safe to use q here as this is system reap
736 (void)reap_child_locked(p
, q
, 1, reparentedtoinit
, 1, 0);
739 * Traced processes are killed
740 * since their existence means someone is messing up.
742 if (q
->p_lflag
& P_LTRACED
) {
746 * Take a reference on the child process to
747 * ensure it doesn't exit and disappear between
748 * the time we drop the list_lock and attempt
749 * to acquire its proc_lock.
751 if (proc_ref_locked(q
) != q
)
756 opp
= proc_find(q
->p_oppid
);
757 if (opp
!= PROC_NULL
) {
761 proc_reparentlocked(q
, opp
, 0, 0);
764 /* original parent exited while traced */
766 q
->p_listflag
|= P_LIST_DEADPARENT
;
769 proc_reparentlocked(q
, initproc
, 0, 0);
773 q
->p_lflag
&= ~P_LTRACED
;
775 if (q
->sigwait_thread
) {
776 thread_t thread
= q
->sigwait_thread
;
780 * The sigwait_thread could be stopped at a
781 * breakpoint. Wake it up to kill.
782 * Need to do this as it could be a thread which is not
783 * the first thread in the task. So any attempts to kill
784 * the process would result into a deadlock on q->sigwait.
786 thread_resume(thread
);
787 clear_wait(thread
, THREAD_INTERRUPTED
);
788 threadsignal(thread
, SIGKILL
, 0);
797 q
->p_listflag
|= P_LIST_DEADPARENT
;
798 proc_reparentlocked(q
, initproc
, 0, 1);
803 proc_childdrainend(p
);
807 * Release reference to text vnode
816 * Save exit status and final rusage info, adding in child rusage
817 * info and self times. If we were unable to allocate a zombie
818 * structure, this information is lost.
820 if (p
->p_ru
!= NULL
) {
821 calcru(p
, &p
->p_stats
->p_ru
.ru_utime
, &p
->p_stats
->p_ru
.ru_stime
, NULL
);
822 p
->p_ru
->ru
= p
->p_stats
->p_ru
;
824 ruadd(&(p
->p_ru
->ru
), &p
->p_stats
->p_cru
);
828 * Free up profiling buffers.
831 struct uprof
*p0
= &p
->p_stats
->p_prof
, *p1
, *pn
;
837 for (; p1
!= NULL
; p1
= pn
) {
839 kfree(p1
, sizeof *p1
);
844 if (thread_call_cancel(p
->p_rcall
))
847 while (p
->p_ractive
> 0) {
856 thread_call_free(p
->p_rcall
);
860 * Other substructures are freed from wait().
862 FREE_ZONE(p
->p_stats
, sizeof *p
->p_stats
, M_PSTATS
);
865 FREE_ZONE(p
->p_sigacts
, sizeof *p
->p_sigacts
, M_SIGACTS
);
868 proc_limitdrop(p
, 1);
871 vm_purgeable_disown(p
->task
);
874 * Finish up by terminating the task
875 * and halt this thread (only if a
876 * member of the task exiting).
879 set_bsdtask_info(task
, NULL
);
881 knote_hint
= NOTE_EXIT
| (p
->p_xstat
& 0xffff);
882 proc_knote(p
, knote_hint
);
884 /* mark the thread as the one that is doing proc_exit
885 * no need to hold proc lock in uthread_free
887 uth
->uu_flag
|= UT_PROCEXIT
;
889 * Notify parent that we're gone.
892 if (pp
->p_flag
& P_NOCLDWAIT
) {
894 if (p
->p_ru
!= NULL
) {
898 * If the parent is ignoring SIGCHLD, then POSIX requires
899 * us to not add the resource usage to the parent process -
900 * we are only going to hand it off to init to get reaped.
901 * We should contest the standard in this case on the basis
906 * Add child resource usage to parent before giving
907 * zombie to init. If we were unable to allocate a
908 * zombie structure, this information is lost.
910 ruadd(&pp
->p_stats
->p_cru
, &p
->p_ru
->ru
);
911 #endif /* !3839178 */
912 update_rusage_info_child(&pp
->p_stats
->ri_child
, &p
->p_ru
->ri
);
916 /* kernel can reap this one, no need to move it to launchd */
918 p
->p_listflag
|= P_LIST_DEADPARENT
;
921 if ((p
->p_listflag
& P_LIST_DEADPARENT
) == 0 || p
->p_oppid
) {
922 if (pp
!= initproc
) {
924 pp
->si_pid
= p
->p_pid
;
925 pp
->si_status
= p
->p_xstat
;
926 pp
->si_code
= CLD_EXITED
;
928 * p_ucred usage is safe as it is an exiting process
929 * and reference is dropped in reap
931 pp
->si_uid
= kauth_cred_getruid(p
->p_ucred
);
934 /* mark as a zombie */
935 /* No need to take proc lock as all refs are drained and
936 * no one except parent (reaping ) can look at this.
937 * The write is to an int and is coherent. Also parent is
938 * keyed off of list lock for reaping
940 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_COMMON
,
941 BSDDBG_CODE(DBG_BSD_PROC
, BSD_PROC_EXIT
) | DBG_FUNC_END
,
942 pid
, exitval
, 0, 0, 0);
945 * The current process can be reaped so, no one
949 psignal(pp
, SIGCHLD
);
951 /* and now wakeup the parent */
956 /* should be fine as parent proc would be initproc */
957 /* mark as a zombie */
958 /* No need to take proc lock as all refs are drained and
959 * no one except parent (reaping ) can look at this.
960 * The write is to an int and is coherent. Also parent is
961 * keyed off of list lock for reaping
964 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_COMMON
,
965 BSDDBG_CODE(DBG_BSD_PROC
, BSD_PROC_EXIT
) | DBG_FUNC_END
,
966 pid
, exitval
, 0, 0, 0);
967 /* check for sysctl zomb lookup */
968 while ((p
->p_listflag
& P_LIST_WAITING
) == P_LIST_WAITING
) {
969 msleep(&p
->p_stat
, proc_list_mlock
, PWAIT
, "waitcoll", 0);
971 /* safe to use p as this is a system reap */
973 p
->p_listflag
|= P_LIST_WAITING
;
976 * This is a named reference and it is not granted
977 * if the reap is already in progress. So we get
978 * the reference here exclusively and their can be
979 * no waiters. So there is no need for a wakeup
980 * after we are done. AlsO the reap frees the structure
981 * and the proc struct cannot be used for wakeups as well.
982 * It is safe to use p here as this is system reap
984 (void)reap_child_locked(pp
, p
, 1, 0, 1, 1);
985 /* list lock dropped by reap_child_locked */
987 if (uth
->uu_lowpri_window
) {
989 * task is marked as a low priority I/O type and we've
990 * somehow picked up another throttle during exit processing...
991 * no need to throttle this thread since its going away
992 * but we do need to update our bookeeping w/r to throttled threads
994 throttle_lowpri_io(0);
1005 * Description: Given a process from which all status information needed
1006 * has already been extracted, if the process is a ptrace
1007 * attach process, detach it and give it back to its real
1008 * parent, else recover all resources remaining associated
1011 * Parameters: proc_t parent Parent of process being reaped
1012 * proc_t child Process to reap
1014 * Returns: 0 Process was not reaped because it
1015 * came from an attach
1016 * 1 Process was reaped
1019 reap_child_locked(proc_t parent
, proc_t child
, int deadparent
, int reparentedtoinit
, int locked
, int droplock
)
1021 proc_t trace_parent
= PROC_NULL
; /* Traced parent process, if tracing */
1027 * If we got the child via a ptrace 'attach',
1028 * we need to give it back to the old parent.
1030 * Exception: someone who has been reparented to launchd before being
1031 * ptraced can simply be reaped, refer to radar 5677288
1032 * p_oppid -> ptraced
1033 * trace_parent == initproc -> away from launchd
1034 * reparentedtoinit -> came to launchd by reparenting
1036 if (child
->p_oppid
) {
1041 oppid
= child
->p_oppid
;
1043 knote_hint
= NOTE_EXIT
| (child
->p_xstat
& 0xffff);
1046 if ((trace_parent
= proc_find(oppid
))
1047 && !((trace_parent
== initproc
) && reparentedtoinit
)) {
1049 if (trace_parent
!= initproc
) {
1051 * proc internal fileds and p_ucred usage safe
1052 * here as child is dead and is not reaped or
1055 proc_lock(trace_parent
);
1056 trace_parent
->si_pid
= child
->p_pid
;
1057 trace_parent
->si_status
= child
->p_xstat
;
1058 trace_parent
->si_code
= CLD_CONTINUED
;
1059 trace_parent
->si_uid
= kauth_cred_getruid(child
->p_ucred
);
1060 proc_unlock(trace_parent
);
1062 proc_reparentlocked(child
, trace_parent
, 1, 0);
1064 /* resend knote to original parent (and others) after reparenting */
1065 proc_knote(child
, knote_hint
);
1067 psignal(trace_parent
, SIGCHLD
);
1069 wakeup((caddr_t
)trace_parent
);
1070 child
->p_listflag
&= ~P_LIST_WAITING
;
1071 wakeup(&child
->p_stat
);
1073 proc_rele(trace_parent
);
1074 if ((locked
== 1) && (droplock
== 0))
1080 * If we can't reparent (e.g. the original parent exited while child was being debugged, or
1081 * original parent is the same as the debugger currently exiting), we still need to satisfy
1082 * the knote lifecycle for other observers on the system. While the debugger was attached,
1083 * the NOTE_EXIT would not have been broadcast during initial child termination.
1085 proc_knote(child
, knote_hint
);
1087 if (trace_parent
!= PROC_NULL
) {
1088 proc_rele(trace_parent
);
1092 #pragma clang diagnostic push
1093 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
1094 proc_knote(child
, NOTE_REAP
);
1095 #pragma clang diagnostic pop
1097 proc_knote_drain(child
);
1104 * If the parent is ignoring SIGCHLD, then POSIX requires
1105 * us to not add the resource usage to the parent process -
1106 * we are only going to hand it off to init to get reaped.
1107 * We should contest the standard in this case on the basis
1110 if (!(parent
->p_flag
& P_NOCLDWAIT
))
1111 #endif /* 3839178 */
1112 ruadd(&parent
->p_stats
->p_cru
, &child
->p_ru
->ru
);
1113 update_rusage_info_child(&parent
->p_stats
->ri_child
, &child
->p_ru
->ri
);
1114 proc_unlock(parent
);
1115 FREE_ZONE(child
->p_ru
, sizeof *child
->p_ru
, M_ZOMBIE
);
1118 printf("Warning : lost p_ru for %s\n", child
->p_comm
);
1121 AUDIT_SESSION_PROCEXIT(child
);
1124 * Decrement the count of procs running with this uid.
1125 * p_ucred usage is safe here as it is an exited process.
1126 * and refernce is dropped after these calls down below
1127 * (locking protection is provided by list lock held in chgproccnt)
1129 (void)chgproccnt(kauth_cred_getruid(child
->p_ucred
), -1);
1138 * Free up credentials.
1140 if (IS_VALID_CRED(child
->p_ucred
)) {
1141 kauth_cred_unref(&child
->p_ucred
);
1144 /* XXXX Note NOT SAFE TO USE p_ucred from this point onwards */
1147 * Finally finished with old proc entry.
1148 * Unlink it from its process group and free it.
1153 LIST_REMOVE(child
, p_list
); /* off zombproc */
1154 parent
->p_childrencnt
--;
1155 LIST_REMOVE(child
, p_sibling
);
1156 /* If there are no more children wakeup parent */
1157 if ((deadparent
!= 0) && (LIST_EMPTY(&parent
->p_children
)))
1158 wakeup((caddr_t
)parent
); /* with list lock held */
1159 child
->p_listflag
&= ~P_LIST_WAITING
;
1160 wakeup(&child
->p_stat
);
1162 /* Take it out of process hash */
1163 LIST_REMOVE(child
, p_hash
);
1164 child
->p_listflag
&= ~P_LIST_INHASH
;
1165 proc_checkdeadrefs(child
);
1170 * If a child zombie is being reaped because its parent
1171 * is exiting, make sure we update the list flag
1173 child
->p_listflag
|= P_LIST_DEADPARENT
;
1178 #if CONFIG_FINE_LOCK_GROUPS
1179 lck_mtx_destroy(&child
->p_mlock
, proc_mlock_grp
);
1180 lck_mtx_destroy(&child
->p_fdmlock
, proc_fdmlock_grp
);
1182 lck_mtx_destroy(&child
->p_dtrace_sprlock
, proc_lck_grp
);
1184 lck_spin_destroy(&child
->p_slock
, proc_slock_grp
);
1185 #else /* CONFIG_FINE_LOCK_GROUPS */
1186 lck_mtx_destroy(&child
->p_mlock
, proc_lck_grp
);
1187 lck_mtx_destroy(&child
->p_fdmlock
, proc_lck_grp
);
1189 lck_mtx_destroy(&child
->p_dtrace_sprlock
, proc_lck_grp
);
1191 lck_spin_destroy(&child
->p_slock
, proc_lck_grp
);
1192 #endif /* CONFIG_FINE_LOCK_GROUPS */
1193 workqueue_destroy_lock(child
);
1195 FREE_ZONE(child
, sizeof *child
, M_PROC
);
1196 if ((locked
== 1) && (droplock
== 0))
1204 wait1continue(int result
)
1209 struct _wait4_data
*wait4_data
;
1210 struct wait4_nocancel_args
*uap
;
1217 thread
= current_thread();
1218 uth
= (struct uthread
*)get_bsdthread_info(thread
);
1220 wait4_data
= &uth
->uu_kevent
.uu_wait4_data
;
1221 uap
= wait4_data
->args
;
1222 retval
= wait4_data
->retval
;
1223 return(wait4_nocancel(p
, uap
, retval
));
1227 wait4(proc_t q
, struct wait4_args
*uap
, int32_t *retval
)
1229 __pthread_testcancel(1);
1230 return(wait4_nocancel(q
, (struct wait4_nocancel_args
*)uap
, retval
));
1234 wait4_nocancel(proc_t q
, struct wait4_nocancel_args
*uap
, int32_t *retval
)
1241 struct _wait4_data
*wait4_data
;
1243 AUDIT_ARG(pid
, uap
->pid
);
1246 uap
->pid
= -q
->p_pgrpid
;
1254 for (p
= q
->p_children
.lh_first
; p
!= 0; p
= p
->p_sibling
.le_next
) {
1255 if ( p
->p_sibling
.le_next
!= 0 )
1257 if (uap
->pid
!= WAIT_ANY
&&
1258 p
->p_pid
!= uap
->pid
&&
1259 p
->p_pgrpid
!= -(uap
->pid
))
1264 /* XXX This is racy because we don't get the lock!!!! */
1266 if (p
->p_listflag
& P_LIST_WAITING
) {
1267 (void)msleep(&p
->p_stat
, proc_list_mlock
, PWAIT
, "waitcoll", 0);
1270 p
->p_listflag
|= P_LIST_WAITING
; /* only allow single thread to wait() */
1273 if (p
->p_stat
== SZOMB
) {
1274 int reparentedtoinit
= (p
->p_listflag
& P_LIST_DEADPARENT
) ? 1 : 0;
1278 if ((error
= mac_proc_check_wait(q
, p
)) != 0)
1281 retval
[0] = p
->p_pid
;
1283 /* Legacy apps expect only 8 bits of status */
1284 status
= 0xffff & p
->p_xstat
; /* convert to int */
1285 error
= copyout((caddr_t
)&status
,
1292 if (p
->p_ru
== NULL
) {
1295 if (IS_64BIT_PROCESS(q
)) {
1296 struct user64_rusage my_rusage
;
1297 munge_user64_rusage(&p
->p_ru
->ru
, &my_rusage
);
1298 error
= copyout((caddr_t
)&my_rusage
,
1300 sizeof (my_rusage
));
1303 struct user32_rusage my_rusage
;
1304 munge_user32_rusage(&p
->p_ru
->ru
, &my_rusage
);
1305 error
= copyout((caddr_t
)&my_rusage
,
1307 sizeof (my_rusage
));
1310 /* information unavailable? */
1315 /* Conformance change for 6577252.
1316 * When SIGCHLD is blocked and wait() returns because the status
1317 * of a child process is available and there are no other
1318 * children processes, then any pending SIGCHLD signal is cleared.
1320 if ( sibling_count
== 0 ) {
1321 int mask
= sigmask(SIGCHLD
);
1322 uth
= current_uthread();
1324 if ( (uth
->uu_sigmask
& mask
) != 0 ) {
1325 /* we are blocking SIGCHLD signals. clear any pending SIGCHLD.
1326 * This locking looks funny but it is protecting access to the
1327 * thread via p_uthlist.
1330 uth
->uu_siglist
&= ~mask
; /* clear pending signal */
1336 (void)reap_child_locked(q
, p
, 0, reparentedtoinit
, 0, 0);
1340 if (p
->p_stat
== SSTOP
&& (p
->p_lflag
& P_LWAITED
) == 0 &&
1341 (p
->p_lflag
& P_LTRACED
|| uap
->options
& WUNTRACED
)) {
1344 if ((error
= mac_proc_check_wait(q
, p
)) != 0)
1348 p
->p_lflag
|= P_LWAITED
;
1350 retval
[0] = p
->p_pid
;
1352 status
= W_STOPCODE(p
->p_xstat
);
1353 error
= copyout((caddr_t
)&status
,
1361 * If we are waiting for continued processses, and this
1362 * process was continued
1364 if ((uap
->options
& WCONTINUED
) &&
1365 (p
->p_flag
& P_CONTINUED
)) {
1368 if ((error
= mac_proc_check_wait(q
, p
)) != 0)
1372 /* Prevent other process for waiting for this event */
1373 OSBitAndAtomic(~((uint32_t)P_CONTINUED
), &p
->p_flag
);
1374 retval
[0] = p
->p_pid
;
1376 status
= W_STOPCODE(SIGCONT
);
1377 error
= copyout((caddr_t
)&status
,
1384 p
->p_listflag
&= ~P_LIST_WAITING
;
1387 /* list lock is held when we get here any which way */
1393 if (uap
->options
& WNOHANG
) {
1399 /* Save arguments for continuation. Backing storage is in uthread->uu_arg, and will not be deallocated */
1400 uth
= current_uthread();
1401 wait4_data
= &uth
->uu_kevent
.uu_wait4_data
;
1402 wait4_data
->args
= uap
;
1403 wait4_data
->retval
= retval
;
1405 if ((error
= msleep0((caddr_t
)q
, proc_list_mlock
, PWAIT
| PCATCH
| PDROP
, "wait", 0, wait1continue
)))
1411 p
->p_listflag
&= ~P_LIST_WAITING
;
1418 #define ASSERT_LCK_MTX_OWNED(lock) \
1419 lck_mtx_assert(lock, LCK_MTX_ASSERT_OWNED)
1421 #define ASSERT_LCK_MTX_OWNED(lock) /* nothing */
1425 waitidcontinue(int result
)
1430 struct _waitid_data
*waitid_data
;
1431 struct waitid_nocancel_args
*uap
;
1438 thread
= current_thread();
1439 uth
= (struct uthread
*)get_bsdthread_info(thread
);
1441 waitid_data
= &uth
->uu_kevent
.uu_waitid_data
;
1442 uap
= waitid_data
->args
;
1443 retval
= waitid_data
->retval
;
1444 return(waitid_nocancel(p
, uap
, retval
));
1448 * Description: Suspend the calling thread until one child of the process
1449 * containing the calling thread changes state.
1451 * Parameters: uap->idtype one of P_PID, P_PGID, P_ALL
1452 * uap->id pid_t or gid_t or ignored
1453 * uap->infop Address of siginfo_t struct in
1454 * user space into which to return status
1455 * uap->options flag values
1457 * Returns: 0 Success
1458 * !0 Error returning status to user space
1461 waitid(proc_t q
, struct waitid_args
*uap
, int32_t *retval
)
1463 __pthread_testcancel(1);
1464 return (waitid_nocancel(q
, (struct waitid_nocancel_args
*)uap
, retval
));
1468 waitid_nocancel(proc_t q
, struct waitid_nocancel_args
*uap
,
1469 __unused
int32_t *retval
)
1471 user_siginfo_t siginfo
; /* siginfo data to return to caller */
1472 boolean_t caller64
= IS_64BIT_PROCESS(q
);
1477 struct _waitid_data
*waitid_data
;
1479 if (uap
->options
== 0 ||
1480 (uap
->options
& ~(WNOHANG
|WNOWAIT
|WCONTINUED
|WSTOPPED
|WEXITED
)))
1481 return (EINVAL
); /* bits set that aren't recognized */
1483 switch (uap
->idtype
) {
1484 case P_PID
: /* child with process ID equal to... */
1485 case P_PGID
: /* child with process group ID equal to... */
1486 if (((int)uap
->id
) < 0)
1489 case P_ALL
: /* any child */
1497 for (p
= q
->p_children
.lh_first
; p
!= 0; p
= p
->p_sibling
.le_next
) {
1499 switch (uap
->idtype
) {
1500 case P_PID
: /* child with process ID equal to... */
1501 if (p
->p_pid
!= (pid_t
)uap
->id
)
1504 case P_PGID
: /* child with process group ID equal to... */
1505 if (p
->p_pgrpid
!= (pid_t
)uap
->id
)
1508 case P_ALL
: /* any child */
1512 /* XXX This is racy because we don't get the lock!!!! */
1515 * Wait collision; go to sleep and restart; used to maintain
1516 * the single return for waited process guarantee.
1518 if (p
->p_listflag
& P_LIST_WAITING
) {
1519 (void) msleep(&p
->p_stat
, proc_list_mlock
,
1520 PWAIT
, "waitidcoll", 0);
1523 p
->p_listflag
|= P_LIST_WAITING
; /* mark busy */
1527 bzero(&siginfo
, sizeof (siginfo
));
1529 switch (p
->p_stat
) {
1530 case SZOMB
: /* Exited */
1531 if (!(uap
->options
& WEXITED
))
1535 if ((error
= mac_proc_check_wait(q
, p
)) != 0)
1538 siginfo
.si_signo
= SIGCHLD
;
1539 siginfo
.si_pid
= p
->p_pid
;
1540 siginfo
.si_status
= WEXITSTATUS(p
->p_xstat
);
1541 if (WIFSIGNALED(p
->p_xstat
)) {
1542 siginfo
.si_code
= WCOREDUMP(p
->p_xstat
) ?
1543 CLD_DUMPED
: CLD_KILLED
;
1545 siginfo
.si_code
= CLD_EXITED
;
1547 if ((error
= copyoutsiginfo(&siginfo
,
1548 caller64
, uap
->infop
)) != 0)
1551 /* Prevent other process for waiting for this event? */
1552 if (!(uap
->options
& WNOWAIT
)) {
1553 (void) reap_child_locked(q
, p
, 0, 0, 0, 0);
1558 case SSTOP
: /* Stopped */
1560 * If we are not interested in stopped processes, then
1563 if (!(uap
->options
& WSTOPPED
))
1567 * If someone has already waited it, we lost a race
1568 * to be the one to return status.
1570 if ((p
->p_lflag
& P_LWAITED
) != 0)
1574 if ((error
= mac_proc_check_wait(q
, p
)) != 0)
1577 siginfo
.si_signo
= SIGCHLD
;
1578 siginfo
.si_pid
= p
->p_pid
;
1579 siginfo
.si_status
= p
->p_xstat
; /* signal number */
1580 siginfo
.si_code
= CLD_STOPPED
;
1582 if ((error
= copyoutsiginfo(&siginfo
,
1583 caller64
, uap
->infop
)) != 0)
1586 /* Prevent other process for waiting for this event? */
1587 if (!(uap
->options
& WNOWAIT
)) {
1589 p
->p_lflag
|= P_LWAITED
;
1594 default: /* All other states => Continued */
1595 if (!(uap
->options
& WCONTINUED
))
1599 * If the flag isn't set, then this process has not
1600 * been stopped and continued, or the status has
1601 * already been reaped by another caller of waitid().
1603 if ((p
->p_flag
& P_CONTINUED
) == 0)
1607 if ((error
= mac_proc_check_wait(q
, p
)) != 0)
1610 siginfo
.si_signo
= SIGCHLD
;
1611 siginfo
.si_code
= CLD_CONTINUED
;
1613 siginfo
.si_pid
= p
->p_contproc
;
1614 siginfo
.si_status
= p
->p_xstat
;
1617 if ((error
= copyoutsiginfo(&siginfo
,
1618 caller64
, uap
->infop
)) != 0)
1621 /* Prevent other process for waiting for this event? */
1622 if (!(uap
->options
& WNOWAIT
)) {
1623 OSBitAndAtomic(~((uint32_t)P_CONTINUED
),
1628 ASSERT_LCK_MTX_OWNED(proc_list_mlock
);
1630 /* Not a process we are interested in; go on to next child */
1632 p
->p_listflag
&= ~P_LIST_WAITING
;
1635 ASSERT_LCK_MTX_OWNED(proc_list_mlock
);
1637 /* No child processes that could possibly satisfy the request? */
1644 if (uap
->options
& WNOHANG
) {
1647 if ((error
= mac_proc_check_wait(q
, p
)) != 0)
1651 * The state of the siginfo structure in this case
1652 * is undefined. Some implementations bzero it, some
1653 * (like here) leave it untouched for efficiency.
1655 * Thus the most portable check for "no matching pid with
1656 * WNOHANG" is to store a zero into si_pid before
1657 * invocation, then check for a non-zero value afterwards.
1662 /* Save arguments for continuation. Backing storage is in uthread->uu_arg, and will not be deallocated */
1663 uth
= current_uthread();
1664 waitid_data
= &uth
->uu_kevent
.uu_waitid_data
;
1665 waitid_data
->args
= uap
;
1666 waitid_data
->retval
= retval
;
1668 if ((error
= msleep0(q
, proc_list_mlock
,
1669 PWAIT
| PCATCH
| PDROP
, "waitid", 0, waitidcontinue
)) != 0)
1675 p
->p_listflag
&= ~P_LIST_WAITING
;
1682 * make process 'parent' the new parent of process 'child'.
1685 proc_reparentlocked(proc_t child
, proc_t parent
, int cansignal
, int locked
)
1687 proc_t oldparent
= PROC_NULL
;
1689 if (child
->p_pptr
== parent
)
1695 oldparent
= child
->p_pptr
;
1696 #if __PROC_INTERNAL_DEBUG
1697 if (oldparent
== PROC_NULL
)
1698 panic("proc_reparent: process %p does not have a parent\n", child
);
1701 LIST_REMOVE(child
, p_sibling
);
1702 #if __PROC_INTERNAL_DEBUG
1703 if (oldparent
->p_childrencnt
== 0)
1704 panic("process children count already 0\n");
1706 oldparent
->p_childrencnt
--;
1707 #if __PROC_INTERNAL_DEBUG1
1708 if (oldparent
->p_childrencnt
< 0)
1709 panic("process children count -ve\n");
1711 LIST_INSERT_HEAD(&parent
->p_children
, child
, p_sibling
);
1712 parent
->p_childrencnt
++;
1713 child
->p_pptr
= parent
;
1714 child
->p_ppid
= parent
->p_pid
;
1718 if ((cansignal
!= 0) && (initproc
== parent
) && (child
->p_stat
== SZOMB
))
1719 psignal(initproc
, SIGCHLD
);
1725 * Exit: deallocate address space and other resources, change proc state
1726 * to zombie, and unlink proc from allproc and parent's lists. Save exit
1727 * status and rusage for wait(). Check for child processes and orphan them.
1731 vfork_exit(proc_t p
, int rv
)
1733 vfork_exit_internal(p
, rv
, 0);
1737 vfork_exit_internal(proc_t p
, int rv
, int forceexit
)
1739 thread_t self
= current_thread();
1741 struct task
*task
= p
->task
;
1746 * If a thread in this task has already
1747 * called exit(), then halt any others
1751 ut
= get_bsdthread_info(self
);
1755 if ((p
->p_lflag
& P_LPEXIT
) == P_LPEXIT
) {
1757 * This happens when a parent exits/killed and vfork is in progress
1758 * other threads. But shutdown code for ex has already called exit1()
1763 p
->p_lflag
|= (P_LEXIT
| P_LPEXIT
);
1766 if (forceexit
== 0) {
1768 * parent of a vfork child has already called exit() and the
1769 * thread that has vfork in proress terminates. So there is no
1770 * separate address space here and it has already been marked for
1771 * termination. This was never covered before and could cause problems
1772 * if we block here for outside code.
1774 /* Notify the perf server */
1775 (void)sys_perf_notify(self
, p
->p_pid
);
1779 * Remove proc from allproc queue and from pidhash chain.
1780 * Need to do this before we do anything that can block.
1781 * Not doing causes things like mount() find this on allproc
1782 * in partially cleaned state.
1787 #if CONFIG_MEMORYSTATUS
1788 memorystatus_remove(p
, TRUE
);
1791 LIST_REMOVE(p
, p_list
);
1792 LIST_INSERT_HEAD(&zombproc
, p
, p_list
); /* Place onto zombproc. */
1793 /* will not be visible via proc_find */
1794 p
->p_listflag
|= P_LIST_EXITED
;
1800 p
->p_lflag
&= ~(P_LTRACED
| P_LPPWAIT
);
1801 p
->p_sigignore
= ~0;
1805 if (thread_call_cancel(p
->p_rcall
))
1808 while (p
->p_ractive
> 0) {
1817 thread_call_free(p
->p_rcall
);
1826 vproc_exit(proc_t p
)
1833 struct task
*task
= p
->task
;
1836 struct session
*sessp
;
1837 struct rusage_superset
*rup
;
1839 /* XXX Zombie allocation may fail, in which case stats get lost */
1840 MALLOC_ZONE(rup
, struct rusage_superset
*,
1841 sizeof (*rup
), M_ZOMBIE
, M_WAITOK
);
1846 * Close open files and release open-file table.
1851 sessp
= proc_session(p
);
1852 if (SESS_LEADER(p
, sessp
)) {
1854 if (sessp
->s_ttyvp
!= NULLVP
) {
1855 struct vnode
*ttyvp
;
1858 struct vfs_context context
;
1862 * Controlling process.
1863 * Signal foreground pgrp,
1864 * drain controlling terminal
1865 * and revoke access to controlling terminal.
1867 session_lock(sessp
);
1868 tp
= SESSION_TP(sessp
);
1869 if ((tp
!= TTY_NULL
) && (tp
->t_session
== sessp
)) {
1870 session_unlock(sessp
);
1873 * We're going to SIGHUP the foreground process
1874 * group. It can't change from this point on
1875 * until the revoke is complete.
1876 * The process group changes under both the tty
1877 * lock and proc_list_lock but we need only one
1883 tty_pgsignal(tp
, SIGHUP
, 1);
1885 session_lock(sessp
);
1886 tp
= SESSION_TP(sessp
);
1888 cttyflag
= sessp
->s_flags
& S_CTTYREF
;
1889 sessp
->s_flags
&= ~S_CTTYREF
;
1890 ttyvp
= sessp
->s_ttyvp
;
1891 ttyvid
= sessp
->s_ttyvid
;
1892 sessp
->s_ttyvp
= NULL
;
1893 sessp
->s_ttyvid
= 0;
1894 sessp
->s_ttyp
= TTY_NULL
;
1895 sessp
->s_ttypgrpid
= NO_PID
;
1896 session_unlock(sessp
);
1898 if ((ttyvp
!= NULLVP
) && (vnode_getwithvid(ttyvp
, ttyvid
) == 0)) {
1899 if (tp
!= TTY_NULL
) {
1904 context
.vc_thread
= proc_thread(p
); /* XXX */
1905 context
.vc_ucred
= kauth_cred_proc_ref(p
);
1906 VNOP_REVOKE(ttyvp
, REVOKEALL
, &context
);
1909 * Release the extra usecount taken in cttyopen.
1910 * usecount should be released after VNOP_REVOKE is called.
1911 * This usecount was taken to ensure that
1912 * the VNOP_REVOKE results in a close to
1913 * the tty since cttyclose is a no-op.
1918 kauth_cred_unref(&context
.vc_ucred
);
1923 * This is cleared even if not set. This is also done in
1924 * spec_close to ensure that the flag is cleared.
1933 session_lock(sessp
);
1934 sessp
->s_leader
= NULL
;
1935 session_unlock(sessp
);
1937 session_rele(sessp
);
1943 p
->p_rlimit
[RLIMIT_FSIZE
].rlim_cur
= RLIM_INFINITY
;
1946 proc_childdrainstart(p
);
1947 while ((q
= p
->p_children
.lh_first
) != NULL
) {
1948 if (q
->p_stat
== SZOMB
) {
1950 panic("parent child linkage broken");
1951 /* check for lookups by zomb sysctl */
1952 while ((q
->p_listflag
& P_LIST_WAITING
) == P_LIST_WAITING
) {
1953 msleep(&q
->p_stat
, proc_list_mlock
, PWAIT
, "waitcoll", 0);
1955 q
->p_listflag
|= P_LIST_WAITING
;
1957 * This is a named reference and it is not granted
1958 * if the reap is already in progress. So we get
1959 * the reference here exclusively and their can be
1960 * no waiters. So there is no need for a wakeup
1961 * after we are done. AlsO the reap frees the structure
1962 * and the proc struct cannot be used for wakeups as well.
1963 * It is safe to use q here as this is system reap
1965 (void)reap_child_locked(p
, q
, 1, 0, 1, 0);
1968 * Traced processes are killed
1969 * since their existence means someone is messing up.
1971 if (q
->p_lflag
& P_LTRACED
) {
1976 opp
= proc_find(q
->p_oppid
);
1977 if (opp
!= PROC_NULL
) {
1981 proc_reparentlocked(q
, opp
, 0, 0);
1984 /* original parent exited while traced */
1986 q
->p_listflag
|= P_LIST_DEADPARENT
;
1989 proc_reparentlocked(q
, initproc
, 0, 0);
1993 q
->p_lflag
&= ~P_LTRACED
;
1995 if (q
->sigwait_thread
) {
1996 thread_t thread
= q
->sigwait_thread
;
2000 * The sigwait_thread could be stopped at a
2001 * breakpoint. Wake it up to kill.
2002 * Need to do this as it could be a thread which is not
2003 * the first thread in the task. So any attempts to kill
2004 * the process would result into a deadlock on q->sigwait.
2006 thread_resume(thread
);
2007 clear_wait(thread
, THREAD_INTERRUPTED
);
2008 threadsignal(thread
, SIGKILL
, 0);
2013 psignal(q
, SIGKILL
);
2016 q
->p_listflag
|= P_LIST_DEADPARENT
;
2017 proc_reparentlocked(q
, initproc
, 0, 1);
2022 proc_childdrainend(p
);
2026 * Release reference to text vnode
2030 if (tvp
!= NULLVP
) {
2035 * Save exit status and final rusage info, adding in child rusage
2036 * info and self times. If we were unable to allocate a zombie
2037 * structure, this information is lost.
2040 rup
->ru
= p
->p_stats
->p_ru
;
2041 timerclear(&rup
->ru
.ru_utime
);
2042 timerclear(&rup
->ru
.ru_stime
);
2046 mach_task_basic_info_data_t tinfo
;
2047 task_thread_times_info_data_t ttimesinfo
;
2048 int task_info_stuff
, task_ttimes_stuff
;
2049 struct timeval ut
,st
;
2051 task_info_stuff
= MACH_TASK_BASIC_INFO_COUNT
;
2052 task_info(task
, MACH_TASK_BASIC_INFO
,
2053 &tinfo
, &task_info_stuff
);
2054 p
->p_ru
->ru
.ru_utime
.tv_sec
= tinfo
.user_time
.seconds
;
2055 p
->p_ru
->ru
.ru_utime
.tv_usec
= tinfo
.user_time
.microseconds
;
2056 p
->p_ru
->ru
.ru_stime
.tv_sec
= tinfo
.system_time
.seconds
;
2057 p
->p_ru
->ru
.ru_stime
.tv_usec
= tinfo
.system_time
.microseconds
;
2059 task_ttimes_stuff
= TASK_THREAD_TIMES_INFO_COUNT
;
2060 task_info(task
, TASK_THREAD_TIMES_INFO
,
2061 &ttimesinfo
, &task_ttimes_stuff
);
2063 ut
.tv_sec
= ttimesinfo
.user_time
.seconds
;
2064 ut
.tv_usec
= ttimesinfo
.user_time
.microseconds
;
2065 st
.tv_sec
= ttimesinfo
.system_time
.seconds
;
2066 st
.tv_usec
= ttimesinfo
.system_time
.microseconds
;
2067 timeradd(&ut
,&p
->p_ru
->ru
.ru_utime
,&p
->p_ru
->ru
.ru_utime
);
2068 timeradd(&st
,&p
->p_ru
->ru
.ru_stime
,&p
->p_ru
->ru
.ru_stime
);
2072 ruadd(&rup
->ru
, &p
->p_stats
->p_cru
);
2074 gather_rusage_info(p
, &rup
->ri
, RUSAGE_INFO_CURRENT
);
2075 rup
->ri
.ri_phys_footprint
= 0;
2076 rup
->ri
.ri_proc_exit_abstime
= mach_absolute_time();
2079 * Now that we have filled in the rusage info, make it
2080 * visible to an external observer via proc_pid_rusage().
2086 * Free up profiling buffers.
2089 struct uprof
*p0
= &p
->p_stats
->p_prof
, *p1
, *pn
;
2095 for (; p1
!= NULL
; p1
= pn
) {
2097 kfree(p1
, sizeof *p1
);
2102 pth_proc_hashdelete(p
);
2106 * Other substructures are freed from wait().
2108 FREE_ZONE(p
->p_stats
, sizeof *p
->p_stats
, M_PSTATS
);
2111 FREE_ZONE(p
->p_sigacts
, sizeof *p
->p_sigacts
, M_SIGACTS
);
2112 p
->p_sigacts
= NULL
;
2114 proc_limitdrop(p
, 1);
2118 * Finish up by terminating the task
2119 * and halt this thread (only if a
2120 * member of the task exiting).
2122 p
->task
= TASK_NULL
;
2125 * Notify parent that we're gone.
2127 pp
= proc_parent(p
);
2128 if ((p
->p_listflag
& P_LIST_DEADPARENT
) == 0) {
2129 if (pp
!= initproc
) {
2131 pp
->si_pid
= p
->p_pid
;
2132 pp
->si_status
= p
->p_xstat
;
2133 pp
->si_code
= CLD_EXITED
;
2135 * p_ucred usage is safe as it is an exiting process
2136 * and reference is dropped in reap
2138 pp
->si_uid
= kauth_cred_getruid(p
->p_ucred
);
2141 /* mark as a zombie */
2142 /* mark as a zombie */
2143 /* No need to take proc lock as all refs are drained and
2144 * no one except parent (reaping ) can look at this.
2145 * The write is to an int and is coherent. Also parent is
2146 * keyed off of list lock for reaping
2150 psignal(pp
, SIGCHLD
);
2152 /* and now wakeup the parent */
2154 wakeup((caddr_t
)pp
);
2158 /* check for lookups by zomb sysctl */
2159 while ((p
->p_listflag
& P_LIST_WAITING
) == P_LIST_WAITING
) {
2160 msleep(&p
->p_stat
, proc_list_mlock
, PWAIT
, "waitcoll", 0);
2163 p
->p_listflag
|= P_LIST_WAITING
;
2166 * This is a named reference and it is not granted
2167 * if the reap is already in progress. So we get
2168 * the reference here exclusively and their can be
2169 * no waiters. So there is no need for a wakeup
2170 * after we are done. AlsO the reap frees the structure
2171 * and the proc struct cannot be used for wakeups as well.
2172 * It is safe to use p here as this is system reap
2174 (void)reap_child_locked(pp
, p
, 0, 0, 1, 1);
2175 /* list lock dropped by reap_child_locked */
2183 * LP64 support - long is 64 bits if we are dealing with a 64 bit user
2184 * process. We munge the kernel version of rusage into the
2187 __private_extern__
void
2188 munge_user64_rusage(struct rusage
*a_rusage_p
, struct user64_rusage
*a_user_rusage_p
)
2190 /* timeval changes size, so utime and stime need special handling */
2191 a_user_rusage_p
->ru_utime
.tv_sec
= a_rusage_p
->ru_utime
.tv_sec
;
2192 a_user_rusage_p
->ru_utime
.tv_usec
= a_rusage_p
->ru_utime
.tv_usec
;
2193 a_user_rusage_p
->ru_stime
.tv_sec
= a_rusage_p
->ru_stime
.tv_sec
;
2194 a_user_rusage_p
->ru_stime
.tv_usec
= a_rusage_p
->ru_stime
.tv_usec
;
2196 * everything else can be a direct assign, since there is no loss
2197 * of precision implied boing 32->64.
2199 a_user_rusage_p
->ru_maxrss
= a_rusage_p
->ru_maxrss
;
2200 a_user_rusage_p
->ru_ixrss
= a_rusage_p
->ru_ixrss
;
2201 a_user_rusage_p
->ru_idrss
= a_rusage_p
->ru_idrss
;
2202 a_user_rusage_p
->ru_isrss
= a_rusage_p
->ru_isrss
;
2203 a_user_rusage_p
->ru_minflt
= a_rusage_p
->ru_minflt
;
2204 a_user_rusage_p
->ru_majflt
= a_rusage_p
->ru_majflt
;
2205 a_user_rusage_p
->ru_nswap
= a_rusage_p
->ru_nswap
;
2206 a_user_rusage_p
->ru_inblock
= a_rusage_p
->ru_inblock
;
2207 a_user_rusage_p
->ru_oublock
= a_rusage_p
->ru_oublock
;
2208 a_user_rusage_p
->ru_msgsnd
= a_rusage_p
->ru_msgsnd
;
2209 a_user_rusage_p
->ru_msgrcv
= a_rusage_p
->ru_msgrcv
;
2210 a_user_rusage_p
->ru_nsignals
= a_rusage_p
->ru_nsignals
;
2211 a_user_rusage_p
->ru_nvcsw
= a_rusage_p
->ru_nvcsw
;
2212 a_user_rusage_p
->ru_nivcsw
= a_rusage_p
->ru_nivcsw
;
2215 /* For a 64-bit kernel and 32-bit userspace, munging may be needed */
2216 __private_extern__
void
2217 munge_user32_rusage(struct rusage
*a_rusage_p
, struct user32_rusage
*a_user_rusage_p
)
2219 /* timeval changes size, so utime and stime need special handling */
2220 a_user_rusage_p
->ru_utime
.tv_sec
= a_rusage_p
->ru_utime
.tv_sec
;
2221 a_user_rusage_p
->ru_utime
.tv_usec
= a_rusage_p
->ru_utime
.tv_usec
;
2222 a_user_rusage_p
->ru_stime
.tv_sec
= a_rusage_p
->ru_stime
.tv_sec
;
2223 a_user_rusage_p
->ru_stime
.tv_usec
= a_rusage_p
->ru_stime
.tv_usec
;
2225 * everything else can be a direct assign. We currently ignore
2226 * the loss of precision
2228 a_user_rusage_p
->ru_maxrss
= a_rusage_p
->ru_maxrss
;
2229 a_user_rusage_p
->ru_ixrss
= a_rusage_p
->ru_ixrss
;
2230 a_user_rusage_p
->ru_idrss
= a_rusage_p
->ru_idrss
;
2231 a_user_rusage_p
->ru_isrss
= a_rusage_p
->ru_isrss
;
2232 a_user_rusage_p
->ru_minflt
= a_rusage_p
->ru_minflt
;
2233 a_user_rusage_p
->ru_majflt
= a_rusage_p
->ru_majflt
;
2234 a_user_rusage_p
->ru_nswap
= a_rusage_p
->ru_nswap
;
2235 a_user_rusage_p
->ru_inblock
= a_rusage_p
->ru_inblock
;
2236 a_user_rusage_p
->ru_oublock
= a_rusage_p
->ru_oublock
;
2237 a_user_rusage_p
->ru_msgsnd
= a_rusage_p
->ru_msgsnd
;
2238 a_user_rusage_p
->ru_msgrcv
= a_rusage_p
->ru_msgrcv
;
2239 a_user_rusage_p
->ru_nsignals
= a_rusage_p
->ru_nsignals
;
2240 a_user_rusage_p
->ru_nvcsw
= a_rusage_p
->ru_nvcsw
;
2241 a_user_rusage_p
->ru_nivcsw
= a_rusage_p
->ru_nivcsw
;