2 * Copyright (c) 2000-2007 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>
123 /* Do not include dtrace.h, it redefines kmem_[alloc/free] */
124 extern void (*dtrace_fasttrap_exit_ptr
)(proc_t
);
125 extern void (*dtrace_helpers_cleanup
)(proc_t
);
126 extern void dtrace_lazy_dofs_destroy(proc_t
);
128 #include <sys/dtrace_ptss.h>
132 #include <security/mac.h>
133 #include <sys/syscall.h>
136 #include <mach/mach_types.h>
137 #include <mach/task.h>
138 #include <mach/thread_act.h>
142 extern char init_task_failure_data
[];
143 void proc_prepareexit(proc_t p
, int rv
);
144 void vfork_exit(proc_t p
, int rv
);
145 void vproc_exit(proc_t p
);
146 __private_extern__
void munge_user64_rusage(struct rusage
*a_rusage_p
, struct user64_rusage
*a_user_rusage_p
);
147 __private_extern__
void munge_user32_rusage(struct rusage
*a_rusage_p
, struct user32_rusage
*a_user_rusage_p
);
148 static int reap_child_locked(proc_t parent
, proc_t child
, int deadparent
, int locked
, int droplock
);
151 * Things which should have prototypes in headers, but don't
153 void *get_bsduthreadarg(thread_t
);
154 void proc_exit(proc_t p
);
155 int wait1continue(int result
);
156 int waitidcontinue(int result
);
157 int *get_bsduthreadrval(thread_t
);
158 kern_return_t
sys_perf_notify(thread_t thread
, int pid
);
159 kern_return_t
abnormal_exit_notify(mach_exception_data_type_t code
,
160 mach_exception_data_type_t subcode
);
164 * NOTE: Source and target may *NOT* overlap!
165 * XXX Should share code with bsd/dev/ppc/unix_signal.c
168 siginfo_user_to_user32(user_siginfo_t
*in
, user32_siginfo_t
*out
)
170 out
->si_signo
= in
->si_signo
;
171 out
->si_errno
= in
->si_errno
;
172 out
->si_code
= in
->si_code
;
173 out
->si_pid
= in
->si_pid
;
174 out
->si_uid
= in
->si_uid
;
175 out
->si_status
= in
->si_status
;
176 out
->si_addr
= CAST_DOWN_EXPLICIT(user32_addr_t
,in
->si_addr
);
177 /* following cast works for sival_int because of padding */
178 out
->si_value
.sival_ptr
= CAST_DOWN_EXPLICIT(user32_addr_t
,in
->si_value
.sival_ptr
);
179 out
->si_band
= in
->si_band
; /* range reduction */
183 siginfo_user_to_user64(user_siginfo_t
*in
, user64_siginfo_t
*out
)
185 out
->si_signo
= in
->si_signo
;
186 out
->si_errno
= in
->si_errno
;
187 out
->si_code
= in
->si_code
;
188 out
->si_pid
= in
->si_pid
;
189 out
->si_uid
= in
->si_uid
;
190 out
->si_status
= in
->si_status
;
191 out
->si_addr
= in
->si_addr
;
192 /* following cast works for sival_int because of padding */
193 out
->si_value
.sival_ptr
= in
->si_value
.sival_ptr
;
194 out
->si_band
= in
->si_band
; /* range reduction */
202 exit(proc_t p
, struct exit_args
*uap
, int *retval
)
204 exit1(p
, W_EXITCODE(uap
->rval
, 0), retval
);
206 /* drop funnel before we return */
207 thread_exception_return();
210 thread_block(THREAD_CONTINUE_NULL
);
215 * Exit: deallocate address space and other resources, change proc state
216 * to zombie, and unlink proc from allproc and parent's lists. Save exit
217 * status and rusage for wait(). Check for child processes and orphan them.
220 exit1(proc_t p
, int rv
, int *retval
)
222 thread_t self
= current_thread();
223 struct task
*task
= p
->task
;
227 * If a thread in this task has already
228 * called exit(), then halt any others
232 ut
= get_bsdthread_info(self
);
233 if (ut
->uu_flag
& UT_VFORK
) {
235 vfork_return(p
, retval
, p
->p_pid
);
236 unix_syscall_return(0);
241 * The parameter list of audit_syscall_exit() was augmented to
242 * take the Darwin syscall number as the first parameter,
243 * which is currently required by mac_audit_postselect().
247 * The BSM token contains two components: an exit status as passed
248 * to exit(), and a return value to indicate what sort of exit it
249 * was. The exit status is WEXITSTATUS(rv), but it's not clear
250 * what the return value is.
252 AUDIT_ARG(exit
, WEXITSTATUS(rv
), 0);
253 AUDIT_SYSCALL_EXIT(SYS_exit
, p
, ut
, 0); /* Exit is always successfull */
255 DTRACE_PROC1(exit
, int, CLD_EXITED
);
258 proc_transstart(p
, 1);
259 while (p
->exit_thread
!= self
) {
260 if (sig_try_locked(p
) <= 0) {
262 if (get_threadtask(self
) != task
) {
267 thread_terminate(self
);
268 thread_exception_return();
275 printf("pid 1 exited (signal %d, exit %d)",
276 WTERMSIG(rv
), WEXITSTATUS(rv
));
277 panic("%s died\nState at Last Exception:\n\n%s",
278 (p
->p_comm
[0] != '\0' ?
281 init_task_failure_data
);
284 p
->p_lflag
|= P_LEXIT
;
290 proc_prepareexit(p
, rv
);
292 /* Last thread to terminate will call proc_exit() */
293 task_terminate_internal(task
);
299 proc_prepareexit(proc_t p
, int rv
)
301 mach_exception_data_type_t code
, subcode
;
303 thread_t self
= current_thread();
304 ut
= get_bsdthread_info(self
);
306 /* If a core should be generated, notify crash reporter */
307 if (hassigprop(WTERMSIG(rv
), SA_CORE
) || ((p
->p_csflags
& CS_KILLED
) != 0)) {
309 * Workaround for processes checking up on PT_DENY_ATTACH:
310 * should be backed out post-Leopard (details in 5431025).
312 if ((SIGSEGV
== WTERMSIG(rv
)) &&
313 (p
->p_pptr
->p_lflag
& P_LNOATTACH
)) {
318 * Crash Reporter looks for the signal value, original exception
319 * type, and low 20 bits of the original code in code[0]
320 * (8, 4, and 20 bits respectively). code[1] is unmodified.
322 code
= ((WTERMSIG(rv
) & 0xff) << 24) |
323 ((ut
->uu_exception
& 0x0f) << 20) |
324 ((int)ut
->uu_code
& 0xfffff);
325 subcode
= ut
->uu_subcode
;
326 (void) abnormal_exit_notify(code
, subcode
);
330 /* Notify the perf server */
331 (void)sys_perf_notify(self
, p
->p_pid
);
334 * Remove proc from allproc queue and from pidhash chain.
335 * Need to do this before we do anything that can block.
336 * Not doing causes things like mount() find this on allproc
337 * in partially cleaned state.
342 LIST_REMOVE(p
, p_list
);
343 LIST_INSERT_HEAD(&zombproc
, p
, p_list
); /* Place onto zombproc. */
344 /* will not be visible via proc_find */
345 p
->p_listflag
|= P_LIST_EXITED
;
354 * If parent is waiting for us to exit or exec,
355 * P_LPPWAIT is set; we will wakeup the parent below.
358 p
->p_lflag
&= ~(P_LTRACED
| P_LPPWAIT
);
359 p
->p_sigignore
= ~(sigcantmask
);
369 struct task
*task
= p
->task
;
370 vnode_t tvp
= NULLVP
;
372 struct session
*sessp
;
373 struct uthread
* uth
;
377 uth
= (struct uthread
*)get_bsdthread_info(current_thread());
380 proc_transstart(p
, 1);
381 if( !(p
->p_lflag
& P_LEXIT
)) {
383 * This can happen if a thread_terminate() occurs
384 * in a single-threaded process.
386 p
->p_lflag
|= P_LEXIT
;
389 proc_prepareexit(p
, 0);
390 (void) task_terminate_internal(task
);
396 p
->p_lflag
|= P_LPEXIT
;
399 * Other kernel threads may be in the middle of signalling this process.
400 * Wait for those threads to wrap it up before making the process
403 if ((p
->p_lflag
& P_LINSIGNAL
) || (p
->p_sigwaitcnt
> 0)) {
405 while ((p
->p_lflag
& P_LINSIGNAL
) || (p
->p_sigwaitcnt
> 1))
406 msleep(&p
->p_sigmask
, &p
->p_mlock
, PWAIT
, "proc_sigdrain", NULL
);
412 exitval
= p
->p_xstat
;
413 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC
, BSD_PROC_EXIT
) | DBG_FUNC_START
,
414 pid
, exitval
, 0, 0, 0);
418 * Free any outstanding lazy dof entries. It is imperative we
419 * always call dtrace_lazy_dofs_destroy, rather than null check
420 * and call if !NULL. If we NULL test, during lazy dof faulting
421 * we can race with the faulting code and proceed from here to
422 * beyond the helpers cleanup. The lazy dof faulting will then
423 * install new helpers which will never be cleaned up, and leak.
425 dtrace_lazy_dofs_destroy(p
);
428 * Clean up any DTrace helper actions or probes for the process.
430 if (p
->p_dtrace_helpers
!= NULL
) {
431 (*dtrace_helpers_cleanup
)(p
);
435 * Clean up any DTrace probes associated with this process.
438 * APPLE NOTE: We release ptss pages/entries in dtrace_fasttrap_exit_ptr(),
439 * call this after dtrace_helpers_cleanup()
442 if (p
->p_dtrace_probes
&& dtrace_fasttrap_exit_ptr
) {
443 (*dtrace_fasttrap_exit_ptr
)(p
);
448 /* XXX Zombie allocation may fail, in which case stats get lost */
449 MALLOC_ZONE(p
->p_ru
, struct rusage
*,
450 sizeof (*p
->p_ru
), M_ZOMBIE
, M_WAITOK
);
455 * need to cancel async IO requests that can be cancelled and wait for those
456 * already active. MAY BLOCK!
466 * Close open files and release open-file table.
471 if (uth
->uu_lowpri_window
) {
473 * task is marked as a low priority I/O type
474 * and the I/O we issued while in flushing files on close
475 * collided with normal I/O operations...
476 * no need to throttle this thread since its going away
477 * but we do need to update our bookeeping w/r to throttled threads
479 throttle_lowpri_io(FALSE
);
483 /* Close ref SYSV Shared memory*/
488 /* Release SYSV semaphores */
493 pth_proc_hashdelete(p
);
496 sessp
= proc_session(p
);
497 if (SESS_LEADER(p
, sessp
)) {
499 if (sessp
->s_ttyvp
!= NULLVP
) {
502 struct vfs_context context
;
507 * Controlling process.
508 * Signal foreground pgrp,
509 * drain controlling terminal
510 * and revoke access to controlling terminal.
512 tp
= SESSION_TP(sessp
);
514 if ((tp
!= TTY_NULL
) && (tp
->t_session
== sessp
)) {
515 tty_pgsignal(tp
, SIGHUP
, 1);
518 /* reget potentially tp due to revocation */
519 tp
= SESSION_TP(sessp
);
520 ttyvp
= sessp
->s_ttyvp
;
521 ttyvid
= sessp
->s_ttyvid
;
522 sessp
->s_ttyvp
= NULLVP
;
524 sessp
->s_ttyp
= TTY_NULL
;
525 sessp
->s_ttypgrpid
= NO_PID
;
526 session_unlock(sessp
);
528 if ((ttyvp
!= NULLVP
) && (vnode_getwithvid(ttyvp
, ttyvid
) == 0)) {
530 if (tp
!= TTY_NULL
) {
535 context
.vc_thread
= proc_thread(p
); /* XXX */
536 context
.vc_ucred
= kauth_cred_proc_ref(p
);
537 VNOP_REVOKE(ttyvp
, REVOKEALL
, &context
);
539 kauth_cred_unref(&context
.vc_ucred
);
543 /* reget potentially tp due to revocation */
544 tp
= SESSION_TP(sessp
);
545 ttyvp
= sessp
->s_ttyvp
;
546 sessp
->s_ttyvp
= NULLVP
;
548 sessp
->s_ttyp
= TTY_NULL
;
549 sessp
->s_ttypgrpid
= NO_PID
;
550 session_unlock(sessp
);
555 * s_ttyp is not zero'd; we use this to indicate
556 * that the session once had a controlling terminal.
557 * (for logging and informational purposes)
562 sessp
->s_leader
= NULL
;
563 session_unlock(sessp
);
571 p
->p_rlimit
[RLIMIT_FSIZE
].rlim_cur
= RLIM_INFINITY
;
572 (void)acct_process(p
);
576 if ((p
->p_listflag
& P_LIST_EXITCOUNT
) == P_LIST_EXITCOUNT
) {
577 p
->p_listflag
&= ~P_LIST_EXITCOUNT
;
578 proc_shutdown_exitcount
--;
579 if (proc_shutdown_exitcount
== 0)
580 wakeup(&proc_shutdown_exitcount
);
583 /* wait till parentrefs are dropped and grant no more */
584 proc_childdrainstart(p
);
585 while ((q
= p
->p_children
.lh_first
) != NULL
) {
586 q
->p_listflag
|= P_LIST_DEADPARENT
;
587 if (q
->p_stat
== SZOMB
) {
589 panic("parent child linkage broken");
590 /* check for sysctl zomb lookup */
591 while ((q
->p_listflag
& P_LIST_WAITING
) == P_LIST_WAITING
) {
592 msleep(&q
->p_stat
, proc_list_mlock
, PWAIT
, "waitcoll", 0);
594 q
->p_listflag
|= P_LIST_WAITING
;
596 * This is a named reference and it is not granted
597 * if the reap is already in progress. So we get
598 * the reference here exclusively and their can be
599 * no waiters. So there is no need for a wakeup
600 * after we are done. Also the reap frees the structure
601 * and the proc struct cannot be used for wakeups as well.
602 * It is safe to use q here as this is system reap
604 (void)reap_child_locked(p
, q
, 1, 1, 0);
606 proc_reparentlocked(q
, initproc
, 0, 1);
608 * Traced processes are killed
609 * since their existence means someone is messing up.
611 if (q
->p_lflag
& P_LTRACED
) {
613 * Take a reference on the child process to
614 * ensure it doesn't exit and disappear between
615 * the time we drop the list_lock and attempt
616 * to acquire its proc_lock.
618 if (proc_ref_locked(q
) != q
)
623 q
->p_lflag
&= ~P_LTRACED
;
624 if (q
->sigwait_thread
) {
625 thread_t thread
= q
->sigwait_thread
;
629 * The sigwait_thread could be stopped at a
630 * breakpoint. Wake it up to kill.
631 * Need to do this as it could be a thread which is not
632 * the first thread in the task. So any attempts to kill
633 * the process would result into a deadlock on q->sigwait.
635 thread_resume(thread
);
636 clear_wait(thread
, THREAD_INTERRUPTED
);
637 threadsignal(thread
, SIGKILL
, 0);
649 proc_childdrainend(p
);
653 * Release reference to text vnode
662 * Save exit status and final rusage info, adding in child rusage
663 * info and self times. If we were unable to allocate a zombie
664 * structure, this information is lost.
666 /* No need for locking here as no one than this thread can access this */
667 if (p
->p_ru
!= NULL
) {
668 calcru(p
, &p
->p_stats
->p_ru
.ru_utime
, &p
->p_stats
->p_ru
.ru_stime
, NULL
);
669 *p
->p_ru
= p
->p_stats
->p_ru
;
671 ruadd(p
->p_ru
, &p
->p_stats
->p_cru
);
675 * Free up profiling buffers.
678 struct uprof
*p0
= &p
->p_stats
->p_prof
, *p1
, *pn
;
684 for (; p1
!= NULL
; p1
= pn
) {
686 kfree(p1
, sizeof *p1
);
691 if (thread_call_cancel(p
->p_rcall
))
694 while (p
->p_ractive
> 0) {
703 thread_call_free(p
->p_rcall
);
707 * Other substructures are freed from wait().
709 FREE_ZONE(p
->p_stats
, sizeof *p
->p_stats
, M_PSTATS
);
712 FREE_ZONE(p
->p_sigacts
, sizeof *p
->p_sigacts
, M_SIGACTS
);
715 proc_limitdrop(p
, 1);
720 * Finish up by terminating the task
721 * and halt this thread (only if a
722 * member of the task exiting).
725 set_bsdtask_info(task
, NULL
);
727 /* exit status will be seen by parent process */
728 proc_knote(p
, NOTE_EXIT
| (p
->p_xstat
& 0xffff));
730 /* mark the thread as the one that is doing proc_exit
731 * no need to hold proc lock in uthread_free
733 uth
->uu_flag
|= UT_PROCEXIT
;
735 * Notify parent that we're gone.
738 if (pp
->p_flag
& P_NOCLDWAIT
) {
742 * If the parent is ignoring SIGCHLD, then POSIX requires
743 * us to not add the resource usage to the parent process -
744 * we are only going to hand it off to init to get reaped.
745 * We should contest the standard in this case on the basis
750 * Add child resource usage to parent before giving
751 * zombie to init. If we were unable to allocate a
752 * zombie structure, this information is lost.
754 if (p
->p_ru
!= NULL
) {
756 ruadd(&pp
->p_stats
->p_cru
, p
->p_ru
);
759 #endif /* !3839178 */
761 /* kernel can reap this one, no need to move it to launchd */
763 p
->p_listflag
|= P_LIST_DEADPARENT
;
766 if ((p
->p_listflag
& P_LIST_DEADPARENT
) == 0) {
767 if (pp
!= initproc
) {
769 pp
->si_pid
= p
->p_pid
;
770 pp
->si_status
= p
->p_xstat
;
771 pp
->si_code
= CLD_EXITED
;
773 * p_ucred usage is safe as it is an exiting process
774 * and reference is dropped in reap
776 pp
->si_uid
= kauth_cred_getruid(p
->p_ucred
);
779 /* mark as a zombie */
780 /* No need to take proc lock as all refs are drained and
781 * no one except parent (reaping ) can look at this.
782 * The write is to an int and is coherent. Also parent is
783 * keyed off of list lock for reaping
785 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC
, BSD_PROC_EXIT
) | DBG_FUNC_END
,
786 pid
, exitval
, 0, 0, 0);
789 * The current process can be reaped so, no one
793 psignal(pp
, SIGCHLD
);
795 /* and now wakeup the parent */
800 /* should be fine as parent proc would be initproc */
801 /* mark as a zombie */
802 /* No need to take proc lock as all refs are drained and
803 * no one except parent (reaping ) can look at this.
804 * The write is to an int and is coherent. Also parent is
805 * keyed off of list lock for reaping
808 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC
, BSD_PROC_EXIT
) | DBG_FUNC_END
,
809 pid
, exitval
, 0, 0, 0);
810 /* check for sysctl zomb lookup */
811 while ((p
->p_listflag
& P_LIST_WAITING
) == P_LIST_WAITING
) {
812 msleep(&p
->p_stat
, proc_list_mlock
, PWAIT
, "waitcoll", 0);
814 /* safe to use p as this is a system reap */
816 p
->p_listflag
|= P_LIST_WAITING
;
819 * This is a named reference and it is not granted
820 * if the reap is already in progress. So we get
821 * the reference here exclusively and their can be
822 * no waiters. So there is no need for a wakeup
823 * after we are done. AlsO the reap frees the structure
824 * and the proc struct cannot be used for wakeups as well.
825 * It is safe to use p here as this is system reap
827 (void)reap_child_locked(pp
, p
, 1, 1, 1);
828 /* list lock dropped by reap_child_locked */
830 if (uth
->uu_lowpri_window
) {
832 * task is marked as a low priority I/O type and we've
833 * somehow picked up another throttle during exit processing...
834 * no need to throttle this thread since its going away
835 * but we do need to update our bookeeping w/r to throttled threads
837 throttle_lowpri_io(FALSE
);
848 * Description: Given a process from which all status information needed
849 * has already been extracted, if the process is a ptrace
850 * attach process, detach it and give it back to its real
851 * parent, else recover all resources remaining associated
854 * Parameters: proc_t parent Parent of process being reaped
855 * proc_t child Process to reap
857 * Returns: 0 Process was not reaped because it
858 * came from an attach
859 * 1 Process was reaped
862 reap_child_locked(proc_t parent
, proc_t child
, int deadparent
, int locked
, int droplock
)
864 proc_t trace_parent
= PROC_NULL
; /* Traced parent process, if tracing */
870 * If we got the child via a ptrace 'attach',
871 * we need to give it back to the old parent.
873 * Exception: someone who has been reparented to launchd before being
874 * ptraced can simply be reaped, refer to radar 5677288
876 * trace_parent == initproc -> away from launchd
877 * P_LIST_DEADPARENT -> came to launchd by reparenting
879 if (child
->p_oppid
&& (trace_parent
= proc_find(child
->p_oppid
))
880 && !((trace_parent
== initproc
) && (child
->p_lflag
& P_LIST_DEADPARENT
))) {
884 if (trace_parent
!= initproc
) {
886 * proc internal fileds and p_ucred usage safe
887 * here as child is dead and is not reaped or
890 proc_lock(trace_parent
);
891 trace_parent
->si_pid
= child
->p_pid
;
892 trace_parent
->si_status
= child
->p_xstat
;
893 trace_parent
->si_code
= CLD_CONTINUED
;
894 trace_parent
->si_uid
= kauth_cred_getruid(child
->p_ucred
);
895 proc_unlock(trace_parent
);
897 proc_reparentlocked(child
, trace_parent
, 1, 0);
898 psignal(trace_parent
, SIGCHLD
);
900 wakeup((caddr_t
)trace_parent
);
901 child
->p_listflag
&= ~P_LIST_WAITING
;
902 wakeup(&child
->p_stat
);
904 proc_rele(trace_parent
);
905 if ((locked
== 1) && (droplock
== 0))
910 if (trace_parent
!= PROC_NULL
) {
911 proc_rele(trace_parent
);
914 proc_knote(child
, NOTE_REAP
);
915 proc_knote_drain(child
);
922 * If the parent is ignoring SIGCHLD, then POSIX requires
923 * us to not add the resource usage to the parent process -
924 * we are only going to hand it off to init to get reaped.
925 * We should contest the standard in this case on the basis
928 if (!(parent
->p_flag
& P_NOCLDWAIT
))
930 ruadd(&parent
->p_stats
->p_cru
, child
->p_ru
);
932 FREE_ZONE(child
->p_ru
, sizeof *child
->p_ru
, M_ZOMBIE
);
935 printf("Warning : lost p_ru for %s\n", child
->p_comm
);
938 AUDIT_SESSION_PROCEXIT(child
);
941 * Decrement the count of procs running with this uid.
942 * p_ucred usage is safe here as it is an exited process.
943 * and refernce is dropped after these calls down below
944 * (locking protection is provided by list lock held in chgproccnt)
946 (void)chgproccnt(kauth_cred_getruid(child
->p_ucred
), -1);
955 * Free up credentials.
957 if (IS_VALID_CRED(child
->p_ucred
)) {
958 kauth_cred_unref(&child
->p_ucred
);
961 /* XXXX Note NOT SAFE TO USE p_ucred from this point onwards */
964 * Finally finished with old proc entry.
965 * Unlink it from its process group and free it.
970 LIST_REMOVE(child
, p_list
); /* off zombproc */
971 parent
->p_childrencnt
--;
972 LIST_REMOVE(child
, p_sibling
);
973 /* If there are no more children wakeup parent */
974 if ((deadparent
!= 0) && (LIST_EMPTY(&parent
->p_children
)))
975 wakeup((caddr_t
)parent
); /* with list lock held */
976 child
->p_listflag
&= ~P_LIST_WAITING
;
977 wakeup(&child
->p_stat
);
979 /* Take it out of process hash */
980 LIST_REMOVE(child
, p_hash
);
981 child
->p_listflag
&= ~P_LIST_INHASH
;
982 proc_checkdeadrefs(child
);
987 #if CONFIG_FINE_LOCK_GROUPS
988 lck_mtx_destroy(&child
->p_mlock
, proc_mlock_grp
);
989 lck_mtx_destroy(&child
->p_fdmlock
, proc_fdmlock_grp
);
991 lck_mtx_destroy(&child
->p_dtrace_sprlock
, proc_lck_grp
);
993 lck_spin_destroy(&child
->p_slock
, proc_slock_grp
);
994 #else /* CONFIG_FINE_LOCK_GROUPS */
995 lck_mtx_destroy(&child
->p_mlock
, proc_lck_grp
);
996 lck_mtx_destroy(&child
->p_fdmlock
, proc_lck_grp
);
998 lck_mtx_destroy(&child
->p_dtrace_sprlock
, proc_lck_grp
);
1000 lck_spin_destroy(&child
->p_slock
, proc_lck_grp
);
1001 #endif /* CONFIG_FINE_LOCK_GROUPS */
1002 workqueue_destroy_lock(child
);
1004 FREE_ZONE(child
, sizeof *child
, M_PROC
);
1005 if ((locked
== 1) && (droplock
== 0))
1013 wait1continue(int result
)
1024 thread
= current_thread();
1025 vt
= get_bsduthreadarg(thread
);
1026 retval
= get_bsduthreadrval(thread
);
1027 return(wait4(p
, (struct wait4_args
*)vt
, retval
));
1031 wait4(proc_t q
, struct wait4_args
*uap
, int32_t *retval
)
1033 __pthread_testcancel(1);
1034 return(wait4_nocancel(q
, (struct wait4_nocancel_args
*)uap
, retval
));
1038 wait4_nocancel(proc_t q
, struct wait4_nocancel_args
*uap
, int32_t *retval
)
1045 AUDIT_ARG(pid
, uap
->pid
);
1048 uap
->pid
= -q
->p_pgrpid
;
1056 for (p
= q
->p_children
.lh_first
; p
!= 0; p
= p
->p_sibling
.le_next
) {
1057 if ( p
->p_sibling
.le_next
!= 0 )
1059 if (uap
->pid
!= WAIT_ANY
&&
1060 p
->p_pid
!= uap
->pid
&&
1061 p
->p_pgrpid
!= -(uap
->pid
))
1066 /* XXX This is racy because we don't get the lock!!!! */
1068 if (p
->p_listflag
& P_LIST_WAITING
) {
1069 (void)msleep(&p
->p_stat
, proc_list_mlock
, PWAIT
, "waitcoll", 0);
1072 p
->p_listflag
|= P_LIST_WAITING
; /* only allow single thread to wait() */
1075 if (p
->p_stat
== SZOMB
) {
1078 if ((error
= mac_proc_check_wait(q
, p
)) != 0)
1081 retval
[0] = p
->p_pid
;
1083 /* Legacy apps expect only 8 bits of status */
1084 status
= 0xffff & p
->p_xstat
; /* convert to int */
1085 error
= copyout((caddr_t
)&status
,
1092 if (p
->p_ru
== NULL
) {
1095 if (IS_64BIT_PROCESS(q
)) {
1096 struct user64_rusage my_rusage
;
1097 munge_user64_rusage(p
->p_ru
, &my_rusage
);
1098 error
= copyout((caddr_t
)&my_rusage
,
1100 sizeof (my_rusage
));
1103 struct user32_rusage my_rusage
;
1104 munge_user32_rusage(p
->p_ru
, &my_rusage
);
1105 error
= copyout((caddr_t
)&my_rusage
,
1107 sizeof (my_rusage
));
1110 /* information unavailable? */
1115 /* Conformance change for 6577252.
1116 * When SIGCHLD is blocked and wait() returns because the status
1117 * of a child process is available and there are no other
1118 * children processes, then any pending SIGCHLD signal is cleared.
1120 if ( sibling_count
== 0 ) {
1121 int mask
= sigmask(SIGCHLD
);
1122 uthread_t uth
= (struct uthread
*)get_bsdthread_info(current_thread());
1124 if ( (uth
->uu_sigmask
& mask
) != 0 ) {
1125 /* we are blocking SIGCHLD signals. clear any pending SIGCHLD.
1126 * This locking looks funny but it is protecting access to the
1127 * thread via p_uthlist.
1130 uth
->uu_siglist
&= ~mask
; /* clear pending signal */
1136 (void)reap_child_locked(q
, p
, 0, 0, 0);
1140 if (p
->p_stat
== SSTOP
&& (p
->p_lflag
& P_LWAITED
) == 0 &&
1141 (p
->p_lflag
& P_LTRACED
|| uap
->options
& WUNTRACED
)) {
1144 if ((error
= mac_proc_check_wait(q
, p
)) != 0)
1148 p
->p_lflag
|= P_LWAITED
;
1150 retval
[0] = p
->p_pid
;
1152 status
= W_STOPCODE(p
->p_xstat
);
1153 error
= copyout((caddr_t
)&status
,
1161 * If we are waiting for continued processses, and this
1162 * process was continued
1164 if ((uap
->options
& WCONTINUED
) &&
1165 (p
->p_flag
& P_CONTINUED
)) {
1168 if ((error
= mac_proc_check_wait(q
, p
)) != 0)
1172 /* Prevent other process for waiting for this event */
1173 OSBitAndAtomic(~((uint32_t)P_CONTINUED
), &p
->p_flag
);
1174 retval
[0] = p
->p_pid
;
1176 status
= W_STOPCODE(SIGCONT
);
1177 error
= copyout((caddr_t
)&status
,
1184 p
->p_listflag
&= ~P_LIST_WAITING
;
1187 /* list lock is held when we get here any which way */
1193 if (uap
->options
& WNOHANG
) {
1199 if ((error
= msleep0((caddr_t
)q
, proc_list_mlock
, PWAIT
| PCATCH
| PDROP
, "wait", 0, wait1continue
)))
1205 p
->p_listflag
&= ~P_LIST_WAITING
;
1213 waitidcontinue(int result
)
1222 thread
= current_thread();
1223 vt
= get_bsduthreadarg(thread
);
1224 retval
= get_bsduthreadrval(thread
);
1225 return(waitid(current_proc(), (struct waitid_args
*)vt
, retval
));
1229 * Description: Suspend the calling thread until one child of the process
1230 * containing the calling thread changes state.
1232 * Parameters: uap->idtype one of P_PID, P_PGID, P_ALL
1233 * uap->id pid_t or gid_t or ignored
1234 * uap->infop Address of signinfo_t struct in
1235 * user space into which to return status
1236 * uap->options flag values
1238 * Returns: 0 Success
1239 * !0 Error returning status to user space
1242 waitid(proc_t q
, struct waitid_args
*uap
, int32_t *retval
)
1244 __pthread_testcancel(1);
1245 return(waitid_nocancel(q
, (struct waitid_nocancel_args
*)uap
, retval
));
1249 waitid_nocancel(proc_t q
, struct waitid_nocancel_args
*uap
, __unused
int32_t *retval
)
1251 user_siginfo_t collect64
; /* siginfo data to return to caller */
1258 * Forced validation of options for T.waitpid 21; should be a TSD!
1259 * This will pass the test, but note that we have more bits than the
1260 * standard specifies that we will allow in, in this case. The test
1261 * passes because they light all the bits, not just the ones we allow,
1262 * and so the following check returns EINVAL like the test wants.
1264 if (((uap
->options
& (WNOHANG
|WNOWAIT
|WCONTINUED
|WUNTRACED
|WSTOPPED
|WEXITED
)) != uap
->options
) ||
1265 (uap
->options
== 0))
1266 return (EINVAL
); /* bits set that aren't recognized */
1269 * Overly critical options checking, per POSIX
1271 switch(uap
->idtype
) {
1272 case P_PID
: /* child with process ID equal to... */
1273 case P_PGID
: /* child with process group ID equal to... */
1274 if (((int)uap
->id
) < 0)
1277 case P_ALL
: /* any child */
1285 for (p
= q
->p_children
.lh_first
; p
!= 0; p
= p
->p_sibling
.le_next
) {
1286 switch(uap
->idtype
) {
1287 case P_PID
: /* child with process ID equal to... */
1288 if (p
->p_pid
!= (pid_t
)uap
->id
)
1291 case P_PGID
: /* child with process group ID equal to... */
1292 if (p
->p_pgrpid
!= (pid_t
)uap
->id
)
1295 case P_ALL
: /* any child */
1299 /* XXX This is racy because we don't get the lock!!!! */
1302 * Wait collision; go to sleep and restart; used to maintain
1303 * the single return for waited process guarantee.
1305 if (p
->p_listflag
& P_LIST_WAITING
) {
1306 (void)msleep(&p
->p_stat
, proc_list_mlock
, PWAIT
, "waitidcoll", 0);
1309 p
->p_listflag
|= P_LIST_WAITING
; /* mark busy */
1314 * Types of processes we are interested in
1316 * XXX Don't know what to do for WCONTINUED?!?
1319 case SZOMB
: /* Exited */
1320 if (!(uap
->options
& WEXITED
))
1323 /* drop the lock and the thread is going to return */
1326 /* Collect "siginfo" information for caller */
1327 collect64
.si_signo
= SIGCHLD
;
1328 collect64
.si_code
= 0;
1329 collect64
.si_errno
= 0;
1330 collect64
.si_pid
= 0;
1331 collect64
.si_uid
= 0;
1332 collect64
.si_addr
= 0;
1333 collect64
.si_status
= WEXITSTATUS(p
->p_xstat
);
1334 collect64
.si_band
= 0;
1336 if (IS_64BIT_PROCESS(p
)) {
1337 user64_siginfo_t sinfo64
;
1339 siginfo_user_to_user64(&collect64
, &sinfo64
);
1341 error
= copyout((caddr_t
)&sinfo64
,
1345 user32_siginfo_t sinfo32
;
1347 siginfo_user_to_user32(&collect64
, &sinfo32
);
1349 error
= copyout((caddr_t
)&sinfo32
,
1353 /* information unavailable? */
1357 /* Prevent other process for waiting for this event? */
1358 if (!(uap
->options
& WNOWAIT
)) {
1360 (void)reap_child_locked(q
, p
, 0, 0, 0);
1363 p
->p_listflag
&= ~P_LIST_WAITING
;
1369 case SSTOP
: /* Stopped */
1371 * If we are not interested in stopped processes, then
1374 if (!(uap
->options
& WSTOPPED
))
1378 * If someone has already waited it, we lost a race
1379 * to be the one to return status.
1381 if ((p
->p_lflag
& P_LWAITED
) != 0)
1384 /* drop the lock and the thread is going to return */
1387 /* Collect "siginfo" information for caller */
1388 collect64
.si_signo
= SIGCHLD
;
1389 collect64
.si_code
= 0;
1390 collect64
.si_errno
= 0;
1391 collect64
.si_pid
= 0;
1392 collect64
.si_uid
= 0;
1393 collect64
.si_addr
= 0;
1395 collect64
.si_status
= p
->p_xstat
;
1397 collect64
.si_band
= 0;
1399 if (IS_64BIT_PROCESS(p
)) {
1400 user64_siginfo_t sinfo64
;
1402 siginfo_user_to_user64(&collect64
, &sinfo64
);
1404 error
= copyout((caddr_t
)&sinfo64
,
1408 user32_siginfo_t sinfo32
;
1410 siginfo_user_to_user32(&collect64
, &sinfo32
);
1412 error
= copyout((caddr_t
)&sinfo32
,
1416 /* information unavailable? */
1420 /* Prevent other process for waiting for this event? */
1421 if (!(uap
->options
& WNOWAIT
)) {
1423 p
->p_lflag
|= P_LWAITED
;
1430 default: /* All others */
1431 /* ...meaning Continued */
1432 if (!(uap
->options
& WCONTINUED
))
1436 * If the flag isn't set, then this process has not
1437 * been stopped and continued, or the status has
1438 * already been reaped by another caller of waitid().
1440 if ((p
->p_flag
& P_CONTINUED
) == 0)
1443 /* drop the lock and the thread is going to return */
1446 /* Collect "siginfo" information for caller */
1448 collect64
.si_signo
= SIGCHLD
;
1449 collect64
.si_code
= CLD_CONTINUED
;
1450 collect64
.si_errno
= 0;
1451 collect64
.si_pid
= p
->p_contproc
;
1452 collect64
.si_uid
= 0;
1453 collect64
.si_addr
= 0;
1454 collect64
.si_status
= p
->p_xstat
;
1455 collect64
.si_band
= 0;
1458 if (IS_64BIT_PROCESS(p
)) {
1459 user64_siginfo_t sinfo64
;
1461 siginfo_user_to_user64(&collect64
, &sinfo64
);
1463 error
= copyout((caddr_t
)&sinfo64
,
1467 user32_siginfo_t sinfo32
;
1469 siginfo_user_to_user32(&collect64
, &sinfo32
);
1471 error
= copyout((caddr_t
)&sinfo32
,
1475 /* information unavailable? */
1479 /* Prevent other process for waiting for this event? */
1480 if (!(uap
->options
& WNOWAIT
)) {
1481 OSBitAndAtomic(~((uint32_t)P_CONTINUED
), &p
->p_flag
);
1487 /* LIST LOCK IS HELD HERE */
1488 /* Not a process we are interested in; go on to next child */
1490 p
->p_listflag
&= ~P_LIST_WAITING
;
1494 /* list lock is always held */
1495 /* No child processes that could possibly satisfy the request? */
1501 if (uap
->options
& WNOHANG
) {
1506 if ((error
= msleep0((caddr_t
)q
, proc_list_mlock
, PWAIT
| PCATCH
| PDROP
, "waitid", 0, waitidcontinue
)))
1512 p
->p_listflag
&= ~P_LIST_WAITING
;
1519 * make process 'parent' the new parent of process 'child'.
1522 proc_reparentlocked(proc_t child
, proc_t parent
, int cansignal
, int locked
)
1524 proc_t oldparent
= PROC_NULL
;
1526 if (child
->p_pptr
== parent
)
1532 oldparent
= child
->p_pptr
;
1533 #if __PROC_INTERNAL_DEBUG
1534 if (oldparent
== PROC_NULL
)
1535 panic("proc_reparent: process %p does not have a parent\n", child
);
1538 LIST_REMOVE(child
, p_sibling
);
1539 #if __PROC_INTERNAL_DEBUG
1540 if (oldparent
->p_childrencnt
== 0)
1541 panic("process children count already 0\n");
1543 oldparent
->p_childrencnt
--;
1544 #if __PROC_INTERNAL_DEBUG1
1545 if (oldparent
->p_childrencnt
< 0)
1546 panic("process children count -ve\n");
1548 LIST_INSERT_HEAD(&parent
->p_children
, child
, p_sibling
);
1549 parent
->p_childrencnt
++;
1550 child
->p_pptr
= parent
;
1551 child
->p_ppid
= parent
->p_pid
;
1555 if ((cansignal
!= 0) && (initproc
== parent
) && (child
->p_stat
== SZOMB
))
1556 psignal(initproc
, SIGCHLD
);
1562 * Exit: deallocate address space and other resources, change proc state
1563 * to zombie, and unlink proc from allproc and parent's lists. Save exit
1564 * status and rusage for wait(). Check for child processes and orphan them.
1568 vfork_exit(proc_t p
, int rv
)
1570 vfork_exit_internal(p
, rv
, 0);
1574 vfork_exit_internal(proc_t p
, int rv
, int forceexit
)
1576 thread_t self
= current_thread();
1578 struct task
*task
= p
->task
;
1583 * If a thread in this task has already
1584 * called exit(), then halt any others
1588 ut
= get_bsdthread_info(self
);
1592 if ((p
->p_lflag
& P_LPEXIT
) == P_LPEXIT
) {
1594 * This happens when a parent exits/killed and vfork is in progress
1595 * other threads. But shutdown code for ex has already called exit1()
1600 p
->p_lflag
|= (P_LEXIT
| P_LPEXIT
);
1603 if (forceexit
== 0) {
1605 * parent of a vfork child has already called exit() and the
1606 * thread that has vfork in proress terminates. So there is no
1607 * separate address space here and it has already been marked for
1608 * termination. This was never covered before and could cause problems
1609 * if we block here for outside code.
1611 /* Notify the perf server */
1612 (void)sys_perf_notify(self
, p
->p_pid
);
1616 * Remove proc from allproc queue and from pidhash chain.
1617 * Need to do this before we do anything that can block.
1618 * Not doing causes things like mount() find this on allproc
1619 * in partially cleaned state.
1624 LIST_REMOVE(p
, p_list
);
1625 LIST_INSERT_HEAD(&zombproc
, p
, p_list
); /* Place onto zombproc. */
1626 /* will not be visible via proc_find */
1627 p
->p_listflag
|= P_LIST_EXITED
;
1633 p
->p_lflag
&= ~(P_LTRACED
| P_LPPWAIT
);
1634 p
->p_sigignore
= ~0;
1638 if (thread_call_cancel(p
->p_rcall
))
1641 while (p
->p_ractive
> 0) {
1650 thread_call_free(p
->p_rcall
);
1659 vproc_exit(proc_t p
)
1666 struct task
*task
= p
->task
;
1669 struct session
*sessp
;
1671 /* XXX Zombie allocation may fail, in which case stats get lost */
1672 MALLOC_ZONE(p
->p_ru
, struct rusage
*,
1673 sizeof (*p
->p_ru
), M_ZOMBIE
, M_WAITOK
);
1679 * Close open files and release open-file table.
1684 sessp
= proc_session(p
);
1685 if (SESS_LEADER(p
, sessp
)) {
1687 if (sessp
->s_ttyvp
!= NULLVP
) {
1688 struct vnode
*ttyvp
;
1690 struct vfs_context context
;
1694 * Controlling process.
1695 * Signal foreground pgrp,
1696 * drain controlling terminal
1697 * and revoke access to controlling terminal.
1699 tp
= SESSION_TP(sessp
);
1701 if ((tp
!= TTY_NULL
) && (tp
->t_session
== sessp
)) {
1702 tty_pgsignal(tp
, SIGHUP
, 1);
1707 * The tty could have been revoked
1711 session_lock(sessp
);
1712 /* reget in case of race */
1713 tp
= SESSION_TP(sessp
);
1714 ttyvp
= sessp
->s_ttyvp
;
1715 ttyvid
= sessp
->s_ttyvid
;
1716 sessp
->s_ttyvp
= NULL
;
1717 sessp
->s_ttyvid
= 0;
1718 sessp
->s_ttyp
= TTY_NULL
;
1719 sessp
->s_ttypgrpid
= NO_PID
;
1720 session_unlock(sessp
);
1722 if ((ttyvp
!= NULLVP
) && (vnode_getwithvid(ttyvp
, ttyvid
) == 0)) {
1723 context
.vc_thread
= proc_thread(p
); /* XXX */
1724 context
.vc_ucred
= kauth_cred_proc_ref(p
);
1725 VNOP_REVOKE(ttyvp
, REVOKEALL
, &context
);
1727 kauth_cred_unref(&context
.vc_ucred
);
1730 session_lock(sessp
);
1731 ttyvp
= sessp
->s_ttyvp
;
1732 sessp
->s_ttyvp
= NULL
;
1733 sessp
->s_ttyvid
= 0;
1734 sessp
->s_ttyp
= TTY_NULL
;
1735 sessp
->s_ttypgrpid
= NO_PID
;
1736 session_unlock(sessp
);
1741 * s_ttyp is not zero'd; we use this to indicate
1742 * that the session once had a controlling terminal.
1743 * (for logging and informational purposes)
1747 session_lock(sessp
);
1748 sessp
->s_leader
= NULL
;
1749 session_unlock(sessp
);
1751 session_rele(sessp
);
1757 p
->p_rlimit
[RLIMIT_FSIZE
].rlim_cur
= RLIM_INFINITY
;
1760 proc_childdrainstart(p
);
1761 while ((q
= p
->p_children
.lh_first
) != NULL
) {
1762 q
->p_listflag
|= P_LIST_DEADPARENT
;
1763 if (q
->p_stat
== SZOMB
) {
1765 panic("parent child linkage broken");
1766 /* check for lookups by zomb sysctl */
1767 while ((q
->p_listflag
& P_LIST_WAITING
) == P_LIST_WAITING
) {
1768 msleep(&q
->p_stat
, proc_list_mlock
, PWAIT
, "waitcoll", 0);
1770 q
->p_listflag
|= P_LIST_WAITING
;
1772 * This is a named reference and it is not granted
1773 * if the reap is already in progress. So we get
1774 * the reference here exclusively and their can be
1775 * no waiters. So there is no need for a wakeup
1776 * after we are done. AlsO the reap frees the structure
1777 * and the proc struct cannot be used for wakeups as well.
1778 * It is safe to use q here as this is system reap
1780 (void)reap_child_locked(p
, q
, 1, 1, 0);
1782 proc_reparentlocked(q
, initproc
, 0, 1);
1784 * Traced processes are killed
1785 * since their existence means someone is messing up.
1787 if (q
->p_lflag
& P_LTRACED
) {
1790 q
->p_lflag
&= ~P_LTRACED
;
1791 if (q
->sigwait_thread
) {
1792 thread_t thread
= q
->sigwait_thread
;
1796 * The sigwait_thread could be stopped at a
1797 * breakpoint. Wake it up to kill.
1798 * Need to do this as it could be a thread which is not
1799 * the first thread in the task. So any attempts to kill
1800 * the process would result into a deadlock on q->sigwait.
1802 thread_resume(thread
);
1803 clear_wait(thread
, THREAD_INTERRUPTED
);
1804 threadsignal(thread
, SIGKILL
, 0);
1809 psignal(q
, SIGKILL
);
1815 proc_childdrainend(p
);
1819 * Release reference to text vnode
1823 if (tvp
!= NULLVP
) {
1828 * Save exit status and final rusage info, adding in child rusage
1829 * info and self times. If we were unable to allocate a zombie
1830 * structure, this information is lost.
1832 /* No need for locking here as no one than this thread can access this */
1833 if (p
->p_ru
!= NULL
) {
1834 *p
->p_ru
= p
->p_stats
->p_ru
;
1835 timerclear(&p
->p_ru
->ru_utime
);
1836 timerclear(&p
->p_ru
->ru_stime
);
1840 task_basic_info_data_t tinfo
;
1841 task_thread_times_info_data_t ttimesinfo
;
1842 int task_info_stuff
, task_ttimes_stuff
;
1843 struct timeval ut
,st
;
1845 task_info_stuff
= TASK_BASIC_INFO_COUNT
;
1846 task_info(task
, TASK_BASIC_INFO
,
1847 &tinfo
, &task_info_stuff
);
1848 p
->p_ru
->ru_utime
.tv_sec
= tinfo
.user_time
.seconds
;
1849 p
->p_ru
->ru_utime
.tv_usec
= tinfo
.user_time
.microseconds
;
1850 p
->p_ru
->ru_stime
.tv_sec
= tinfo
.system_time
.seconds
;
1851 p
->p_ru
->ru_stime
.tv_usec
= tinfo
.system_time
.microseconds
;
1853 task_ttimes_stuff
= TASK_THREAD_TIMES_INFO_COUNT
;
1854 task_info(task
, TASK_THREAD_TIMES_INFO
,
1855 &ttimesinfo
, &task_ttimes_stuff
);
1857 ut
.tv_sec
= ttimesinfo
.user_time
.seconds
;
1858 ut
.tv_usec
= ttimesinfo
.user_time
.microseconds
;
1859 st
.tv_sec
= ttimesinfo
.system_time
.seconds
;
1860 st
.tv_usec
= ttimesinfo
.system_time
.microseconds
;
1861 timeradd(&ut
,&p
->p_ru
->ru_utime
,&p
->p_ru
->ru_utime
);
1862 timeradd(&st
,&p
->p_ru
->ru_stime
,&p
->p_ru
->ru_stime
);
1866 ruadd(p
->p_ru
, &p
->p_stats
->p_cru
);
1870 * Free up profiling buffers.
1873 struct uprof
*p0
= &p
->p_stats
->p_prof
, *p1
, *pn
;
1879 for (; p1
!= NULL
; p1
= pn
) {
1881 kfree(p1
, sizeof *p1
);
1886 pth_proc_hashdelete(p
);
1890 * Other substructures are freed from wait().
1892 FREE_ZONE(p
->p_stats
, sizeof *p
->p_stats
, M_PSTATS
);
1895 FREE_ZONE(p
->p_sigacts
, sizeof *p
->p_sigacts
, M_SIGACTS
);
1896 p
->p_sigacts
= NULL
;
1898 proc_limitdrop(p
, 1);
1902 * Finish up by terminating the task
1903 * and halt this thread (only if a
1904 * member of the task exiting).
1906 p
->task
= TASK_NULL
;
1909 * Notify parent that we're gone.
1911 pp
= proc_parent(p
);
1912 if ((p
->p_listflag
& P_LIST_DEADPARENT
) == 0) {
1913 if (pp
!= initproc
) {
1915 pp
->si_pid
= p
->p_pid
;
1916 pp
->si_status
= p
->p_xstat
;
1917 pp
->si_code
= CLD_EXITED
;
1919 * p_ucred usage is safe as it is an exiting process
1920 * and reference is dropped in reap
1922 pp
->si_uid
= kauth_cred_getruid(p
->p_ucred
);
1925 /* mark as a zombie */
1926 /* mark as a zombie */
1927 /* No need to take proc lock as all refs are drained and
1928 * no one except parent (reaping ) can look at this.
1929 * The write is to an int and is coherent. Also parent is
1930 * keyed off of list lock for reaping
1934 psignal(pp
, SIGCHLD
);
1936 /* and now wakeup the parent */
1938 wakeup((caddr_t
)pp
);
1942 /* check for lookups by zomb sysctl */
1943 while ((p
->p_listflag
& P_LIST_WAITING
) == P_LIST_WAITING
) {
1944 msleep(&p
->p_stat
, proc_list_mlock
, PWAIT
, "waitcoll", 0);
1947 p
->p_listflag
|= P_LIST_WAITING
;
1950 * This is a named reference and it is not granted
1951 * if the reap is already in progress. So we get
1952 * the reference here exclusively and their can be
1953 * no waiters. So there is no need for a wakeup
1954 * after we are done. AlsO the reap frees the structure
1955 * and the proc struct cannot be used for wakeups as well.
1956 * It is safe to use p here as this is system reap
1958 (void)reap_child_locked(pp
, p
, 0, 1, 1);
1959 /* list lock dropped by reap_child_locked */
1967 * LP64 support - long is 64 bits if we are dealing with a 64 bit user
1968 * process. We munge the kernel version of rusage into the
1971 __private_extern__
void
1972 munge_user64_rusage(struct rusage
*a_rusage_p
, struct user64_rusage
*a_user_rusage_p
)
1974 /* timeval changes size, so utime and stime need special handling */
1975 a_user_rusage_p
->ru_utime
.tv_sec
= a_rusage_p
->ru_utime
.tv_sec
;
1976 a_user_rusage_p
->ru_utime
.tv_usec
= a_rusage_p
->ru_utime
.tv_usec
;
1977 a_user_rusage_p
->ru_stime
.tv_sec
= a_rusage_p
->ru_stime
.tv_sec
;
1978 a_user_rusage_p
->ru_stime
.tv_usec
= a_rusage_p
->ru_stime
.tv_usec
;
1980 * everything else can be a direct assign, since there is no loss
1981 * of precision implied boing 32->64.
1983 a_user_rusage_p
->ru_maxrss
= a_rusage_p
->ru_maxrss
;
1984 a_user_rusage_p
->ru_ixrss
= a_rusage_p
->ru_ixrss
;
1985 a_user_rusage_p
->ru_idrss
= a_rusage_p
->ru_idrss
;
1986 a_user_rusage_p
->ru_isrss
= a_rusage_p
->ru_isrss
;
1987 a_user_rusage_p
->ru_minflt
= a_rusage_p
->ru_minflt
;
1988 a_user_rusage_p
->ru_majflt
= a_rusage_p
->ru_majflt
;
1989 a_user_rusage_p
->ru_nswap
= a_rusage_p
->ru_nswap
;
1990 a_user_rusage_p
->ru_inblock
= a_rusage_p
->ru_inblock
;
1991 a_user_rusage_p
->ru_oublock
= a_rusage_p
->ru_oublock
;
1992 a_user_rusage_p
->ru_msgsnd
= a_rusage_p
->ru_msgsnd
;
1993 a_user_rusage_p
->ru_msgrcv
= a_rusage_p
->ru_msgrcv
;
1994 a_user_rusage_p
->ru_nsignals
= a_rusage_p
->ru_nsignals
;
1995 a_user_rusage_p
->ru_nvcsw
= a_rusage_p
->ru_nvcsw
;
1996 a_user_rusage_p
->ru_nivcsw
= a_rusage_p
->ru_nivcsw
;
1999 /* For a 64-bit kernel and 32-bit userspace, munging may be needed */
2000 __private_extern__
void
2001 munge_user32_rusage(struct rusage
*a_rusage_p
, struct user32_rusage
*a_user_rusage_p
)
2003 /* timeval changes size, so utime and stime need special handling */
2004 a_user_rusage_p
->ru_utime
.tv_sec
= a_rusage_p
->ru_utime
.tv_sec
;
2005 a_user_rusage_p
->ru_utime
.tv_usec
= a_rusage_p
->ru_utime
.tv_usec
;
2006 a_user_rusage_p
->ru_stime
.tv_sec
= a_rusage_p
->ru_stime
.tv_sec
;
2007 a_user_rusage_p
->ru_stime
.tv_usec
= a_rusage_p
->ru_stime
.tv_usec
;
2009 * everything else can be a direct assign. We currently ignore
2010 * the loss of precision
2012 a_user_rusage_p
->ru_maxrss
= a_rusage_p
->ru_maxrss
;
2013 a_user_rusage_p
->ru_ixrss
= a_rusage_p
->ru_ixrss
;
2014 a_user_rusage_p
->ru_idrss
= a_rusage_p
->ru_idrss
;
2015 a_user_rusage_p
->ru_isrss
= a_rusage_p
->ru_isrss
;
2016 a_user_rusage_p
->ru_minflt
= a_rusage_p
->ru_minflt
;
2017 a_user_rusage_p
->ru_majflt
= a_rusage_p
->ru_majflt
;
2018 a_user_rusage_p
->ru_nswap
= a_rusage_p
->ru_nswap
;
2019 a_user_rusage_p
->ru_inblock
= a_rusage_p
->ru_inblock
;
2020 a_user_rusage_p
->ru_oublock
= a_rusage_p
->ru_oublock
;
2021 a_user_rusage_p
->ru_msgsnd
= a_rusage_p
->ru_msgsnd
;
2022 a_user_rusage_p
->ru_msgrcv
= a_rusage_p
->ru_msgrcv
;
2023 a_user_rusage_p
->ru_nsignals
= a_rusage_p
->ru_nsignals
;
2024 a_user_rusage_p
->ru_nvcsw
= a_rusage_p
->ru_nvcsw
;
2025 a_user_rusage_p
->ru_nivcsw
= a_rusage_p
->ru_nivcsw
;