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!
461 workqueue_mark_exiting(p
);
467 * Close open files and release open-file table.
472 if (uth
->uu_lowpri_window
) {
474 * task is marked as a low priority I/O type
475 * and the I/O we issued while in flushing files on close
476 * collided with normal I/O operations...
477 * no need to throttle this thread since its going away
478 * but we do need to update our bookeeping w/r to throttled threads
480 throttle_lowpri_io(FALSE
);
484 /* Close ref SYSV Shared memory*/
489 /* Release SYSV semaphores */
494 pth_proc_hashdelete(p
);
497 sessp
= proc_session(p
);
498 if (SESS_LEADER(p
, sessp
)) {
500 if (sessp
->s_ttyvp
!= NULLVP
) {
503 struct vfs_context context
;
508 * Controlling process.
509 * Signal foreground pgrp,
510 * drain controlling terminal
511 * and revoke access to controlling terminal.
513 tp
= SESSION_TP(sessp
);
515 if ((tp
!= TTY_NULL
) && (tp
->t_session
== sessp
)) {
516 tty_pgsignal(tp
, SIGHUP
, 1);
519 /* reget potentially tp due to revocation */
520 tp
= SESSION_TP(sessp
);
521 ttyvp
= sessp
->s_ttyvp
;
522 ttyvid
= sessp
->s_ttyvid
;
523 sessp
->s_ttyvp
= NULLVP
;
525 sessp
->s_ttyp
= TTY_NULL
;
526 sessp
->s_ttypgrpid
= NO_PID
;
527 session_unlock(sessp
);
529 if ((ttyvp
!= NULLVP
) && (vnode_getwithvid(ttyvp
, ttyvid
) == 0)) {
531 if (tp
!= TTY_NULL
) {
536 context
.vc_thread
= proc_thread(p
); /* XXX */
537 context
.vc_ucred
= kauth_cred_proc_ref(p
);
538 VNOP_REVOKE(ttyvp
, REVOKEALL
, &context
);
540 kauth_cred_unref(&context
.vc_ucred
);
544 /* reget potentially tp due to revocation */
545 tp
= SESSION_TP(sessp
);
546 ttyvp
= sessp
->s_ttyvp
;
547 sessp
->s_ttyvp
= NULLVP
;
549 sessp
->s_ttyp
= TTY_NULL
;
550 sessp
->s_ttypgrpid
= NO_PID
;
551 session_unlock(sessp
);
556 * s_ttyp is not zero'd; we use this to indicate
557 * that the session once had a controlling terminal.
558 * (for logging and informational purposes)
563 sessp
->s_leader
= NULL
;
564 session_unlock(sessp
);
572 p
->p_rlimit
[RLIMIT_FSIZE
].rlim_cur
= RLIM_INFINITY
;
573 (void)acct_process(p
);
577 if ((p
->p_listflag
& P_LIST_EXITCOUNT
) == P_LIST_EXITCOUNT
) {
578 p
->p_listflag
&= ~P_LIST_EXITCOUNT
;
579 proc_shutdown_exitcount
--;
580 if (proc_shutdown_exitcount
== 0)
581 wakeup(&proc_shutdown_exitcount
);
584 /* wait till parentrefs are dropped and grant no more */
585 proc_childdrainstart(p
);
586 while ((q
= p
->p_children
.lh_first
) != NULL
) {
587 q
->p_listflag
|= P_LIST_DEADPARENT
;
588 if (q
->p_stat
== SZOMB
) {
590 panic("parent child linkage broken");
591 /* check for sysctl zomb lookup */
592 while ((q
->p_listflag
& P_LIST_WAITING
) == P_LIST_WAITING
) {
593 msleep(&q
->p_stat
, proc_list_mlock
, PWAIT
, "waitcoll", 0);
595 q
->p_listflag
|= P_LIST_WAITING
;
597 * This is a named reference and it is not granted
598 * if the reap is already in progress. So we get
599 * the reference here exclusively and their can be
600 * no waiters. So there is no need for a wakeup
601 * after we are done. Also the reap frees the structure
602 * and the proc struct cannot be used for wakeups as well.
603 * It is safe to use q here as this is system reap
605 (void)reap_child_locked(p
, q
, 1, 1, 0);
607 proc_reparentlocked(q
, initproc
, 0, 1);
609 * Traced processes are killed
610 * since their existence means someone is messing up.
612 if (q
->p_lflag
& P_LTRACED
) {
614 * Take a reference on the child process to
615 * ensure it doesn't exit and disappear between
616 * the time we drop the list_lock and attempt
617 * to acquire its proc_lock.
619 if (proc_ref_locked(q
) != q
)
624 q
->p_lflag
&= ~P_LTRACED
;
625 if (q
->sigwait_thread
) {
626 thread_t thread
= q
->sigwait_thread
;
630 * The sigwait_thread could be stopped at a
631 * breakpoint. Wake it up to kill.
632 * Need to do this as it could be a thread which is not
633 * the first thread in the task. So any attempts to kill
634 * the process would result into a deadlock on q->sigwait.
636 thread_resume(thread
);
637 clear_wait(thread
, THREAD_INTERRUPTED
);
638 threadsignal(thread
, SIGKILL
, 0);
650 proc_childdrainend(p
);
654 * Release reference to text vnode
663 * Save exit status and final rusage info, adding in child rusage
664 * info and self times. If we were unable to allocate a zombie
665 * structure, this information is lost.
667 /* No need for locking here as no one than this thread can access this */
668 if (p
->p_ru
!= NULL
) {
669 calcru(p
, &p
->p_stats
->p_ru
.ru_utime
, &p
->p_stats
->p_ru
.ru_stime
, NULL
);
670 *p
->p_ru
= p
->p_stats
->p_ru
;
672 ruadd(p
->p_ru
, &p
->p_stats
->p_cru
);
676 * Free up profiling buffers.
679 struct uprof
*p0
= &p
->p_stats
->p_prof
, *p1
, *pn
;
685 for (; p1
!= NULL
; p1
= pn
) {
687 kfree(p1
, sizeof *p1
);
692 if (thread_call_cancel(p
->p_rcall
))
695 while (p
->p_ractive
> 0) {
704 thread_call_free(p
->p_rcall
);
708 * Other substructures are freed from wait().
710 FREE_ZONE(p
->p_stats
, sizeof *p
->p_stats
, M_PSTATS
);
713 FREE_ZONE(p
->p_sigacts
, sizeof *p
->p_sigacts
, M_SIGACTS
);
716 proc_limitdrop(p
, 1);
721 * Finish up by terminating the task
722 * and halt this thread (only if a
723 * member of the task exiting).
726 set_bsdtask_info(task
, NULL
);
728 /* exit status will be seen by parent process */
729 proc_knote(p
, NOTE_EXIT
| (p
->p_xstat
& 0xffff));
731 /* mark the thread as the one that is doing proc_exit
732 * no need to hold proc lock in uthread_free
734 uth
->uu_flag
|= UT_PROCEXIT
;
736 * Notify parent that we're gone.
739 if (pp
->p_flag
& P_NOCLDWAIT
) {
743 * If the parent is ignoring SIGCHLD, then POSIX requires
744 * us to not add the resource usage to the parent process -
745 * we are only going to hand it off to init to get reaped.
746 * We should contest the standard in this case on the basis
751 * Add child resource usage to parent before giving
752 * zombie to init. If we were unable to allocate a
753 * zombie structure, this information is lost.
755 if (p
->p_ru
!= NULL
) {
757 ruadd(&pp
->p_stats
->p_cru
, p
->p_ru
);
760 #endif /* !3839178 */
762 /* kernel can reap this one, no need to move it to launchd */
764 p
->p_listflag
|= P_LIST_DEADPARENT
;
767 if ((p
->p_listflag
& P_LIST_DEADPARENT
) == 0) {
768 if (pp
!= initproc
) {
770 pp
->si_pid
= p
->p_pid
;
771 pp
->si_status
= p
->p_xstat
;
772 pp
->si_code
= CLD_EXITED
;
774 * p_ucred usage is safe as it is an exiting process
775 * and reference is dropped in reap
777 pp
->si_uid
= kauth_cred_getruid(p
->p_ucred
);
780 /* mark as a zombie */
781 /* No need to take proc lock as all refs are drained and
782 * no one except parent (reaping ) can look at this.
783 * The write is to an int and is coherent. Also parent is
784 * keyed off of list lock for reaping
786 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC
, BSD_PROC_EXIT
) | DBG_FUNC_END
,
787 pid
, exitval
, 0, 0, 0);
790 * The current process can be reaped so, no one
794 psignal(pp
, SIGCHLD
);
796 /* and now wakeup the parent */
801 /* should be fine as parent proc would be initproc */
802 /* mark as a zombie */
803 /* No need to take proc lock as all refs are drained and
804 * no one except parent (reaping ) can look at this.
805 * The write is to an int and is coherent. Also parent is
806 * keyed off of list lock for reaping
809 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC
, BSD_PROC_EXIT
) | DBG_FUNC_END
,
810 pid
, exitval
, 0, 0, 0);
811 /* check for sysctl zomb lookup */
812 while ((p
->p_listflag
& P_LIST_WAITING
) == P_LIST_WAITING
) {
813 msleep(&p
->p_stat
, proc_list_mlock
, PWAIT
, "waitcoll", 0);
815 /* safe to use p as this is a system reap */
817 p
->p_listflag
|= P_LIST_WAITING
;
820 * This is a named reference and it is not granted
821 * if the reap is already in progress. So we get
822 * the reference here exclusively and their can be
823 * no waiters. So there is no need for a wakeup
824 * after we are done. AlsO the reap frees the structure
825 * and the proc struct cannot be used for wakeups as well.
826 * It is safe to use p here as this is system reap
828 (void)reap_child_locked(pp
, p
, 1, 1, 1);
829 /* list lock dropped by reap_child_locked */
831 if (uth
->uu_lowpri_window
) {
833 * task is marked as a low priority I/O type and we've
834 * somehow picked up another throttle during exit processing...
835 * no need to throttle this thread since its going away
836 * but we do need to update our bookeeping w/r to throttled threads
838 throttle_lowpri_io(FALSE
);
849 * Description: Given a process from which all status information needed
850 * has already been extracted, if the process is a ptrace
851 * attach process, detach it and give it back to its real
852 * parent, else recover all resources remaining associated
855 * Parameters: proc_t parent Parent of process being reaped
856 * proc_t child Process to reap
858 * Returns: 0 Process was not reaped because it
859 * came from an attach
860 * 1 Process was reaped
863 reap_child_locked(proc_t parent
, proc_t child
, int deadparent
, int locked
, int droplock
)
865 proc_t trace_parent
= PROC_NULL
; /* Traced parent process, if tracing */
871 * If we got the child via a ptrace 'attach',
872 * we need to give it back to the old parent.
874 * Exception: someone who has been reparented to launchd before being
875 * ptraced can simply be reaped, refer to radar 5677288
877 * trace_parent == initproc -> away from launchd
878 * P_LIST_DEADPARENT -> came to launchd by reparenting
880 if (child
->p_oppid
&& (trace_parent
= proc_find(child
->p_oppid
))
881 && !((trace_parent
== initproc
) && (child
->p_lflag
& P_LIST_DEADPARENT
))) {
885 if (trace_parent
!= initproc
) {
887 * proc internal fileds and p_ucred usage safe
888 * here as child is dead and is not reaped or
891 proc_lock(trace_parent
);
892 trace_parent
->si_pid
= child
->p_pid
;
893 trace_parent
->si_status
= child
->p_xstat
;
894 trace_parent
->si_code
= CLD_CONTINUED
;
895 trace_parent
->si_uid
= kauth_cred_getruid(child
->p_ucred
);
896 proc_unlock(trace_parent
);
898 proc_reparentlocked(child
, trace_parent
, 1, 0);
899 psignal(trace_parent
, SIGCHLD
);
901 wakeup((caddr_t
)trace_parent
);
902 child
->p_listflag
&= ~P_LIST_WAITING
;
903 wakeup(&child
->p_stat
);
905 proc_rele(trace_parent
);
906 if ((locked
== 1) && (droplock
== 0))
911 if (trace_parent
!= PROC_NULL
) {
912 proc_rele(trace_parent
);
915 proc_knote(child
, NOTE_REAP
);
916 proc_knote_drain(child
);
923 * If the parent is ignoring SIGCHLD, then POSIX requires
924 * us to not add the resource usage to the parent process -
925 * we are only going to hand it off to init to get reaped.
926 * We should contest the standard in this case on the basis
929 if (!(parent
->p_flag
& P_NOCLDWAIT
))
931 ruadd(&parent
->p_stats
->p_cru
, child
->p_ru
);
933 FREE_ZONE(child
->p_ru
, sizeof *child
->p_ru
, M_ZOMBIE
);
936 printf("Warning : lost p_ru for %s\n", child
->p_comm
);
939 AUDIT_SESSION_PROCEXIT(child
);
942 * Decrement the count of procs running with this uid.
943 * p_ucred usage is safe here as it is an exited process.
944 * and refernce is dropped after these calls down below
945 * (locking protection is provided by list lock held in chgproccnt)
947 (void)chgproccnt(kauth_cred_getruid(child
->p_ucred
), -1);
956 * Free up credentials.
958 if (IS_VALID_CRED(child
->p_ucred
)) {
959 kauth_cred_unref(&child
->p_ucred
);
962 /* XXXX Note NOT SAFE TO USE p_ucred from this point onwards */
965 * Finally finished with old proc entry.
966 * Unlink it from its process group and free it.
971 LIST_REMOVE(child
, p_list
); /* off zombproc */
972 parent
->p_childrencnt
--;
973 LIST_REMOVE(child
, p_sibling
);
974 /* If there are no more children wakeup parent */
975 if ((deadparent
!= 0) && (LIST_EMPTY(&parent
->p_children
)))
976 wakeup((caddr_t
)parent
); /* with list lock held */
977 child
->p_listflag
&= ~P_LIST_WAITING
;
978 wakeup(&child
->p_stat
);
980 /* Take it out of process hash */
981 LIST_REMOVE(child
, p_hash
);
982 child
->p_listflag
&= ~P_LIST_INHASH
;
983 proc_checkdeadrefs(child
);
988 #if CONFIG_FINE_LOCK_GROUPS
989 lck_mtx_destroy(&child
->p_mlock
, proc_mlock_grp
);
990 lck_mtx_destroy(&child
->p_fdmlock
, proc_fdmlock_grp
);
992 lck_mtx_destroy(&child
->p_dtrace_sprlock
, proc_lck_grp
);
994 lck_spin_destroy(&child
->p_slock
, proc_slock_grp
);
995 #else /* CONFIG_FINE_LOCK_GROUPS */
996 lck_mtx_destroy(&child
->p_mlock
, proc_lck_grp
);
997 lck_mtx_destroy(&child
->p_fdmlock
, proc_lck_grp
);
999 lck_mtx_destroy(&child
->p_dtrace_sprlock
, proc_lck_grp
);
1001 lck_spin_destroy(&child
->p_slock
, proc_lck_grp
);
1002 #endif /* CONFIG_FINE_LOCK_GROUPS */
1003 workqueue_destroy_lock(child
);
1005 FREE_ZONE(child
, sizeof *child
, M_PROC
);
1006 if ((locked
== 1) && (droplock
== 0))
1014 wait1continue(int result
)
1025 thread
= current_thread();
1026 vt
= get_bsduthreadarg(thread
);
1027 retval
= get_bsduthreadrval(thread
);
1028 return(wait4(p
, (struct wait4_args
*)vt
, retval
));
1032 wait4(proc_t q
, struct wait4_args
*uap
, int32_t *retval
)
1034 __pthread_testcancel(1);
1035 return(wait4_nocancel(q
, (struct wait4_nocancel_args
*)uap
, retval
));
1039 wait4_nocancel(proc_t q
, struct wait4_nocancel_args
*uap
, int32_t *retval
)
1046 AUDIT_ARG(pid
, uap
->pid
);
1049 uap
->pid
= -q
->p_pgrpid
;
1057 for (p
= q
->p_children
.lh_first
; p
!= 0; p
= p
->p_sibling
.le_next
) {
1058 if ( p
->p_sibling
.le_next
!= 0 )
1060 if (uap
->pid
!= WAIT_ANY
&&
1061 p
->p_pid
!= uap
->pid
&&
1062 p
->p_pgrpid
!= -(uap
->pid
))
1067 /* XXX This is racy because we don't get the lock!!!! */
1069 if (p
->p_listflag
& P_LIST_WAITING
) {
1070 (void)msleep(&p
->p_stat
, proc_list_mlock
, PWAIT
, "waitcoll", 0);
1073 p
->p_listflag
|= P_LIST_WAITING
; /* only allow single thread to wait() */
1076 if (p
->p_stat
== SZOMB
) {
1079 if ((error
= mac_proc_check_wait(q
, p
)) != 0)
1082 retval
[0] = p
->p_pid
;
1084 /* Legacy apps expect only 8 bits of status */
1085 status
= 0xffff & p
->p_xstat
; /* convert to int */
1086 error
= copyout((caddr_t
)&status
,
1093 if (p
->p_ru
== NULL
) {
1096 if (IS_64BIT_PROCESS(q
)) {
1097 struct user64_rusage my_rusage
;
1098 munge_user64_rusage(p
->p_ru
, &my_rusage
);
1099 error
= copyout((caddr_t
)&my_rusage
,
1101 sizeof (my_rusage
));
1104 struct user32_rusage my_rusage
;
1105 munge_user32_rusage(p
->p_ru
, &my_rusage
);
1106 error
= copyout((caddr_t
)&my_rusage
,
1108 sizeof (my_rusage
));
1111 /* information unavailable? */
1116 /* Conformance change for 6577252.
1117 * When SIGCHLD is blocked and wait() returns because the status
1118 * of a child process is available and there are no other
1119 * children processes, then any pending SIGCHLD signal is cleared.
1121 if ( sibling_count
== 0 ) {
1122 int mask
= sigmask(SIGCHLD
);
1123 uthread_t uth
= (struct uthread
*)get_bsdthread_info(current_thread());
1125 if ( (uth
->uu_sigmask
& mask
) != 0 ) {
1126 /* we are blocking SIGCHLD signals. clear any pending SIGCHLD.
1127 * This locking looks funny but it is protecting access to the
1128 * thread via p_uthlist.
1131 uth
->uu_siglist
&= ~mask
; /* clear pending signal */
1137 (void)reap_child_locked(q
, p
, 0, 0, 0);
1141 if (p
->p_stat
== SSTOP
&& (p
->p_lflag
& P_LWAITED
) == 0 &&
1142 (p
->p_lflag
& P_LTRACED
|| uap
->options
& WUNTRACED
)) {
1145 if ((error
= mac_proc_check_wait(q
, p
)) != 0)
1149 p
->p_lflag
|= P_LWAITED
;
1151 retval
[0] = p
->p_pid
;
1153 status
= W_STOPCODE(p
->p_xstat
);
1154 error
= copyout((caddr_t
)&status
,
1162 * If we are waiting for continued processses, and this
1163 * process was continued
1165 if ((uap
->options
& WCONTINUED
) &&
1166 (p
->p_flag
& P_CONTINUED
)) {
1169 if ((error
= mac_proc_check_wait(q
, p
)) != 0)
1173 /* Prevent other process for waiting for this event */
1174 OSBitAndAtomic(~((uint32_t)P_CONTINUED
), &p
->p_flag
);
1175 retval
[0] = p
->p_pid
;
1177 status
= W_STOPCODE(SIGCONT
);
1178 error
= copyout((caddr_t
)&status
,
1185 p
->p_listflag
&= ~P_LIST_WAITING
;
1188 /* list lock is held when we get here any which way */
1194 if (uap
->options
& WNOHANG
) {
1200 if ((error
= msleep0((caddr_t
)q
, proc_list_mlock
, PWAIT
| PCATCH
| PDROP
, "wait", 0, wait1continue
)))
1206 p
->p_listflag
&= ~P_LIST_WAITING
;
1214 waitidcontinue(int result
)
1223 thread
= current_thread();
1224 vt
= get_bsduthreadarg(thread
);
1225 retval
= get_bsduthreadrval(thread
);
1226 return(waitid(current_proc(), (struct waitid_args
*)vt
, retval
));
1230 * Description: Suspend the calling thread until one child of the process
1231 * containing the calling thread changes state.
1233 * Parameters: uap->idtype one of P_PID, P_PGID, P_ALL
1234 * uap->id pid_t or gid_t or ignored
1235 * uap->infop Address of signinfo_t struct in
1236 * user space into which to return status
1237 * uap->options flag values
1239 * Returns: 0 Success
1240 * !0 Error returning status to user space
1243 waitid(proc_t q
, struct waitid_args
*uap
, int32_t *retval
)
1245 __pthread_testcancel(1);
1246 return(waitid_nocancel(q
, (struct waitid_nocancel_args
*)uap
, retval
));
1250 waitid_nocancel(proc_t q
, struct waitid_nocancel_args
*uap
, __unused
int32_t *retval
)
1252 user_siginfo_t collect64
; /* siginfo data to return to caller */
1259 * Forced validation of options for T.waitpid 21; should be a TSD!
1260 * This will pass the test, but note that we have more bits than the
1261 * standard specifies that we will allow in, in this case. The test
1262 * passes because they light all the bits, not just the ones we allow,
1263 * and so the following check returns EINVAL like the test wants.
1265 if (((uap
->options
& (WNOHANG
|WNOWAIT
|WCONTINUED
|WUNTRACED
|WSTOPPED
|WEXITED
)) != uap
->options
) ||
1266 (uap
->options
== 0))
1267 return (EINVAL
); /* bits set that aren't recognized */
1270 * Overly critical options checking, per POSIX
1272 switch(uap
->idtype
) {
1273 case P_PID
: /* child with process ID equal to... */
1274 case P_PGID
: /* child with process group ID equal to... */
1275 if (((int)uap
->id
) < 0)
1278 case P_ALL
: /* any child */
1286 for (p
= q
->p_children
.lh_first
; p
!= 0; p
= p
->p_sibling
.le_next
) {
1287 switch(uap
->idtype
) {
1288 case P_PID
: /* child with process ID equal to... */
1289 if (p
->p_pid
!= (pid_t
)uap
->id
)
1292 case P_PGID
: /* child with process group ID equal to... */
1293 if (p
->p_pgrpid
!= (pid_t
)uap
->id
)
1296 case P_ALL
: /* any child */
1300 /* XXX This is racy because we don't get the lock!!!! */
1303 * Wait collision; go to sleep and restart; used to maintain
1304 * the single return for waited process guarantee.
1306 if (p
->p_listflag
& P_LIST_WAITING
) {
1307 (void)msleep(&p
->p_stat
, proc_list_mlock
, PWAIT
, "waitidcoll", 0);
1310 p
->p_listflag
|= P_LIST_WAITING
; /* mark busy */
1315 * Types of processes we are interested in
1317 * XXX Don't know what to do for WCONTINUED?!?
1320 case SZOMB
: /* Exited */
1321 if (!(uap
->options
& WEXITED
))
1324 /* drop the lock and the thread is going to return */
1327 /* Collect "siginfo" information for caller */
1328 collect64
.si_signo
= SIGCHLD
;
1329 collect64
.si_code
= 0;
1330 collect64
.si_errno
= 0;
1331 collect64
.si_pid
= 0;
1332 collect64
.si_uid
= 0;
1333 collect64
.si_addr
= 0;
1334 collect64
.si_status
= WEXITSTATUS(p
->p_xstat
);
1335 collect64
.si_band
= 0;
1337 if (IS_64BIT_PROCESS(p
)) {
1338 user64_siginfo_t sinfo64
;
1340 siginfo_user_to_user64(&collect64
, &sinfo64
);
1342 error
= copyout((caddr_t
)&sinfo64
,
1346 user32_siginfo_t sinfo32
;
1348 siginfo_user_to_user32(&collect64
, &sinfo32
);
1350 error
= copyout((caddr_t
)&sinfo32
,
1354 /* information unavailable? */
1358 /* Prevent other process for waiting for this event? */
1359 if (!(uap
->options
& WNOWAIT
)) {
1361 (void)reap_child_locked(q
, p
, 0, 0, 0);
1364 p
->p_listflag
&= ~P_LIST_WAITING
;
1370 case SSTOP
: /* Stopped */
1372 * If we are not interested in stopped processes, then
1375 if (!(uap
->options
& WSTOPPED
))
1379 * If someone has already waited it, we lost a race
1380 * to be the one to return status.
1382 if ((p
->p_lflag
& P_LWAITED
) != 0)
1385 /* drop the lock and the thread is going to return */
1388 /* Collect "siginfo" information for caller */
1389 collect64
.si_signo
= SIGCHLD
;
1390 collect64
.si_code
= 0;
1391 collect64
.si_errno
= 0;
1392 collect64
.si_pid
= 0;
1393 collect64
.si_uid
= 0;
1394 collect64
.si_addr
= 0;
1396 collect64
.si_status
= p
->p_xstat
;
1398 collect64
.si_band
= 0;
1400 if (IS_64BIT_PROCESS(p
)) {
1401 user64_siginfo_t sinfo64
;
1403 siginfo_user_to_user64(&collect64
, &sinfo64
);
1405 error
= copyout((caddr_t
)&sinfo64
,
1409 user32_siginfo_t sinfo32
;
1411 siginfo_user_to_user32(&collect64
, &sinfo32
);
1413 error
= copyout((caddr_t
)&sinfo32
,
1417 /* information unavailable? */
1421 /* Prevent other process for waiting for this event? */
1422 if (!(uap
->options
& WNOWAIT
)) {
1424 p
->p_lflag
|= P_LWAITED
;
1431 default: /* All others */
1432 /* ...meaning Continued */
1433 if (!(uap
->options
& WCONTINUED
))
1437 * If the flag isn't set, then this process has not
1438 * been stopped and continued, or the status has
1439 * already been reaped by another caller of waitid().
1441 if ((p
->p_flag
& P_CONTINUED
) == 0)
1444 /* drop the lock and the thread is going to return */
1447 /* Collect "siginfo" information for caller */
1449 collect64
.si_signo
= SIGCHLD
;
1450 collect64
.si_code
= CLD_CONTINUED
;
1451 collect64
.si_errno
= 0;
1452 collect64
.si_pid
= p
->p_contproc
;
1453 collect64
.si_uid
= 0;
1454 collect64
.si_addr
= 0;
1455 collect64
.si_status
= p
->p_xstat
;
1456 collect64
.si_band
= 0;
1459 if (IS_64BIT_PROCESS(p
)) {
1460 user64_siginfo_t sinfo64
;
1462 siginfo_user_to_user64(&collect64
, &sinfo64
);
1464 error
= copyout((caddr_t
)&sinfo64
,
1468 user32_siginfo_t sinfo32
;
1470 siginfo_user_to_user32(&collect64
, &sinfo32
);
1472 error
= copyout((caddr_t
)&sinfo32
,
1476 /* information unavailable? */
1480 /* Prevent other process for waiting for this event? */
1481 if (!(uap
->options
& WNOWAIT
)) {
1482 OSBitAndAtomic(~((uint32_t)P_CONTINUED
), &p
->p_flag
);
1488 /* LIST LOCK IS HELD HERE */
1489 /* Not a process we are interested in; go on to next child */
1491 p
->p_listflag
&= ~P_LIST_WAITING
;
1495 /* list lock is always held */
1496 /* No child processes that could possibly satisfy the request? */
1502 if (uap
->options
& WNOHANG
) {
1507 if ((error
= msleep0((caddr_t
)q
, proc_list_mlock
, PWAIT
| PCATCH
| PDROP
, "waitid", 0, waitidcontinue
)))
1513 p
->p_listflag
&= ~P_LIST_WAITING
;
1520 * make process 'parent' the new parent of process 'child'.
1523 proc_reparentlocked(proc_t child
, proc_t parent
, int cansignal
, int locked
)
1525 proc_t oldparent
= PROC_NULL
;
1527 if (child
->p_pptr
== parent
)
1533 oldparent
= child
->p_pptr
;
1534 #if __PROC_INTERNAL_DEBUG
1535 if (oldparent
== PROC_NULL
)
1536 panic("proc_reparent: process %p does not have a parent\n", child
);
1539 LIST_REMOVE(child
, p_sibling
);
1540 #if __PROC_INTERNAL_DEBUG
1541 if (oldparent
->p_childrencnt
== 0)
1542 panic("process children count already 0\n");
1544 oldparent
->p_childrencnt
--;
1545 #if __PROC_INTERNAL_DEBUG1
1546 if (oldparent
->p_childrencnt
< 0)
1547 panic("process children count -ve\n");
1549 LIST_INSERT_HEAD(&parent
->p_children
, child
, p_sibling
);
1550 parent
->p_childrencnt
++;
1551 child
->p_pptr
= parent
;
1552 child
->p_ppid
= parent
->p_pid
;
1556 if ((cansignal
!= 0) && (initproc
== parent
) && (child
->p_stat
== SZOMB
))
1557 psignal(initproc
, SIGCHLD
);
1563 * Exit: deallocate address space and other resources, change proc state
1564 * to zombie, and unlink proc from allproc and parent's lists. Save exit
1565 * status and rusage for wait(). Check for child processes and orphan them.
1569 vfork_exit(proc_t p
, int rv
)
1571 vfork_exit_internal(p
, rv
, 0);
1575 vfork_exit_internal(proc_t p
, int rv
, int forceexit
)
1577 thread_t self
= current_thread();
1579 struct task
*task
= p
->task
;
1584 * If a thread in this task has already
1585 * called exit(), then halt any others
1589 ut
= get_bsdthread_info(self
);
1593 if ((p
->p_lflag
& P_LPEXIT
) == P_LPEXIT
) {
1595 * This happens when a parent exits/killed and vfork is in progress
1596 * other threads. But shutdown code for ex has already called exit1()
1601 p
->p_lflag
|= (P_LEXIT
| P_LPEXIT
);
1604 if (forceexit
== 0) {
1606 * parent of a vfork child has already called exit() and the
1607 * thread that has vfork in proress terminates. So there is no
1608 * separate address space here and it has already been marked for
1609 * termination. This was never covered before and could cause problems
1610 * if we block here for outside code.
1612 /* Notify the perf server */
1613 (void)sys_perf_notify(self
, p
->p_pid
);
1617 * Remove proc from allproc queue and from pidhash chain.
1618 * Need to do this before we do anything that can block.
1619 * Not doing causes things like mount() find this on allproc
1620 * in partially cleaned state.
1625 LIST_REMOVE(p
, p_list
);
1626 LIST_INSERT_HEAD(&zombproc
, p
, p_list
); /* Place onto zombproc. */
1627 /* will not be visible via proc_find */
1628 p
->p_listflag
|= P_LIST_EXITED
;
1634 p
->p_lflag
&= ~(P_LTRACED
| P_LPPWAIT
);
1635 p
->p_sigignore
= ~0;
1639 if (thread_call_cancel(p
->p_rcall
))
1642 while (p
->p_ractive
> 0) {
1651 thread_call_free(p
->p_rcall
);
1660 vproc_exit(proc_t p
)
1667 struct task
*task
= p
->task
;
1670 struct session
*sessp
;
1672 /* XXX Zombie allocation may fail, in which case stats get lost */
1673 MALLOC_ZONE(p
->p_ru
, struct rusage
*,
1674 sizeof (*p
->p_ru
), M_ZOMBIE
, M_WAITOK
);
1680 * Close open files and release open-file table.
1685 sessp
= proc_session(p
);
1686 if (SESS_LEADER(p
, sessp
)) {
1688 if (sessp
->s_ttyvp
!= NULLVP
) {
1689 struct vnode
*ttyvp
;
1691 struct vfs_context context
;
1695 * Controlling process.
1696 * Signal foreground pgrp,
1697 * drain controlling terminal
1698 * and revoke access to controlling terminal.
1700 tp
= SESSION_TP(sessp
);
1702 if ((tp
!= TTY_NULL
) && (tp
->t_session
== sessp
)) {
1703 tty_pgsignal(tp
, SIGHUP
, 1);
1708 * The tty could have been revoked
1712 session_lock(sessp
);
1713 /* reget in case of race */
1714 tp
= SESSION_TP(sessp
);
1715 ttyvp
= sessp
->s_ttyvp
;
1716 ttyvid
= sessp
->s_ttyvid
;
1717 sessp
->s_ttyvp
= NULL
;
1718 sessp
->s_ttyvid
= 0;
1719 sessp
->s_ttyp
= TTY_NULL
;
1720 sessp
->s_ttypgrpid
= NO_PID
;
1721 session_unlock(sessp
);
1723 if ((ttyvp
!= NULLVP
) && (vnode_getwithvid(ttyvp
, ttyvid
) == 0)) {
1724 context
.vc_thread
= proc_thread(p
); /* XXX */
1725 context
.vc_ucred
= kauth_cred_proc_ref(p
);
1726 VNOP_REVOKE(ttyvp
, REVOKEALL
, &context
);
1728 kauth_cred_unref(&context
.vc_ucred
);
1731 session_lock(sessp
);
1732 ttyvp
= sessp
->s_ttyvp
;
1733 sessp
->s_ttyvp
= NULL
;
1734 sessp
->s_ttyvid
= 0;
1735 sessp
->s_ttyp
= TTY_NULL
;
1736 sessp
->s_ttypgrpid
= NO_PID
;
1737 session_unlock(sessp
);
1742 * s_ttyp is not zero'd; we use this to indicate
1743 * that the session once had a controlling terminal.
1744 * (for logging and informational purposes)
1748 session_lock(sessp
);
1749 sessp
->s_leader
= NULL
;
1750 session_unlock(sessp
);
1752 session_rele(sessp
);
1758 p
->p_rlimit
[RLIMIT_FSIZE
].rlim_cur
= RLIM_INFINITY
;
1761 proc_childdrainstart(p
);
1762 while ((q
= p
->p_children
.lh_first
) != NULL
) {
1763 q
->p_listflag
|= P_LIST_DEADPARENT
;
1764 if (q
->p_stat
== SZOMB
) {
1766 panic("parent child linkage broken");
1767 /* check for lookups by zomb sysctl */
1768 while ((q
->p_listflag
& P_LIST_WAITING
) == P_LIST_WAITING
) {
1769 msleep(&q
->p_stat
, proc_list_mlock
, PWAIT
, "waitcoll", 0);
1771 q
->p_listflag
|= P_LIST_WAITING
;
1773 * This is a named reference and it is not granted
1774 * if the reap is already in progress. So we get
1775 * the reference here exclusively and their can be
1776 * no waiters. So there is no need for a wakeup
1777 * after we are done. AlsO the reap frees the structure
1778 * and the proc struct cannot be used for wakeups as well.
1779 * It is safe to use q here as this is system reap
1781 (void)reap_child_locked(p
, q
, 1, 1, 0);
1783 proc_reparentlocked(q
, initproc
, 0, 1);
1785 * Traced processes are killed
1786 * since their existence means someone is messing up.
1788 if (q
->p_lflag
& P_LTRACED
) {
1791 q
->p_lflag
&= ~P_LTRACED
;
1792 if (q
->sigwait_thread
) {
1793 thread_t thread
= q
->sigwait_thread
;
1797 * The sigwait_thread could be stopped at a
1798 * breakpoint. Wake it up to kill.
1799 * Need to do this as it could be a thread which is not
1800 * the first thread in the task. So any attempts to kill
1801 * the process would result into a deadlock on q->sigwait.
1803 thread_resume(thread
);
1804 clear_wait(thread
, THREAD_INTERRUPTED
);
1805 threadsignal(thread
, SIGKILL
, 0);
1810 psignal(q
, SIGKILL
);
1816 proc_childdrainend(p
);
1820 * Release reference to text vnode
1824 if (tvp
!= NULLVP
) {
1829 * Save exit status and final rusage info, adding in child rusage
1830 * info and self times. If we were unable to allocate a zombie
1831 * structure, this information is lost.
1833 /* No need for locking here as no one than this thread can access this */
1834 if (p
->p_ru
!= NULL
) {
1835 *p
->p_ru
= p
->p_stats
->p_ru
;
1836 timerclear(&p
->p_ru
->ru_utime
);
1837 timerclear(&p
->p_ru
->ru_stime
);
1841 task_basic_info_data_t tinfo
;
1842 task_thread_times_info_data_t ttimesinfo
;
1843 int task_info_stuff
, task_ttimes_stuff
;
1844 struct timeval ut
,st
;
1846 task_info_stuff
= TASK_BASIC_INFO_COUNT
;
1847 task_info(task
, TASK_BASIC_INFO
,
1848 &tinfo
, &task_info_stuff
);
1849 p
->p_ru
->ru_utime
.tv_sec
= tinfo
.user_time
.seconds
;
1850 p
->p_ru
->ru_utime
.tv_usec
= tinfo
.user_time
.microseconds
;
1851 p
->p_ru
->ru_stime
.tv_sec
= tinfo
.system_time
.seconds
;
1852 p
->p_ru
->ru_stime
.tv_usec
= tinfo
.system_time
.microseconds
;
1854 task_ttimes_stuff
= TASK_THREAD_TIMES_INFO_COUNT
;
1855 task_info(task
, TASK_THREAD_TIMES_INFO
,
1856 &ttimesinfo
, &task_ttimes_stuff
);
1858 ut
.tv_sec
= ttimesinfo
.user_time
.seconds
;
1859 ut
.tv_usec
= ttimesinfo
.user_time
.microseconds
;
1860 st
.tv_sec
= ttimesinfo
.system_time
.seconds
;
1861 st
.tv_usec
= ttimesinfo
.system_time
.microseconds
;
1862 timeradd(&ut
,&p
->p_ru
->ru_utime
,&p
->p_ru
->ru_utime
);
1863 timeradd(&st
,&p
->p_ru
->ru_stime
,&p
->p_ru
->ru_stime
);
1867 ruadd(p
->p_ru
, &p
->p_stats
->p_cru
);
1871 * Free up profiling buffers.
1874 struct uprof
*p0
= &p
->p_stats
->p_prof
, *p1
, *pn
;
1880 for (; p1
!= NULL
; p1
= pn
) {
1882 kfree(p1
, sizeof *p1
);
1887 pth_proc_hashdelete(p
);
1891 * Other substructures are freed from wait().
1893 FREE_ZONE(p
->p_stats
, sizeof *p
->p_stats
, M_PSTATS
);
1896 FREE_ZONE(p
->p_sigacts
, sizeof *p
->p_sigacts
, M_SIGACTS
);
1897 p
->p_sigacts
= NULL
;
1899 proc_limitdrop(p
, 1);
1903 * Finish up by terminating the task
1904 * and halt this thread (only if a
1905 * member of the task exiting).
1907 p
->task
= TASK_NULL
;
1910 * Notify parent that we're gone.
1912 pp
= proc_parent(p
);
1913 if ((p
->p_listflag
& P_LIST_DEADPARENT
) == 0) {
1914 if (pp
!= initproc
) {
1916 pp
->si_pid
= p
->p_pid
;
1917 pp
->si_status
= p
->p_xstat
;
1918 pp
->si_code
= CLD_EXITED
;
1920 * p_ucred usage is safe as it is an exiting process
1921 * and reference is dropped in reap
1923 pp
->si_uid
= kauth_cred_getruid(p
->p_ucred
);
1926 /* mark as a zombie */
1927 /* mark as a zombie */
1928 /* No need to take proc lock as all refs are drained and
1929 * no one except parent (reaping ) can look at this.
1930 * The write is to an int and is coherent. Also parent is
1931 * keyed off of list lock for reaping
1935 psignal(pp
, SIGCHLD
);
1937 /* and now wakeup the parent */
1939 wakeup((caddr_t
)pp
);
1943 /* check for lookups by zomb sysctl */
1944 while ((p
->p_listflag
& P_LIST_WAITING
) == P_LIST_WAITING
) {
1945 msleep(&p
->p_stat
, proc_list_mlock
, PWAIT
, "waitcoll", 0);
1948 p
->p_listflag
|= P_LIST_WAITING
;
1951 * This is a named reference and it is not granted
1952 * if the reap is already in progress. So we get
1953 * the reference here exclusively and their can be
1954 * no waiters. So there is no need for a wakeup
1955 * after we are done. AlsO the reap frees the structure
1956 * and the proc struct cannot be used for wakeups as well.
1957 * It is safe to use p here as this is system reap
1959 (void)reap_child_locked(pp
, p
, 0, 1, 1);
1960 /* list lock dropped by reap_child_locked */
1968 * LP64 support - long is 64 bits if we are dealing with a 64 bit user
1969 * process. We munge the kernel version of rusage into the
1972 __private_extern__
void
1973 munge_user64_rusage(struct rusage
*a_rusage_p
, struct user64_rusage
*a_user_rusage_p
)
1975 /* timeval changes size, so utime and stime need special handling */
1976 a_user_rusage_p
->ru_utime
.tv_sec
= a_rusage_p
->ru_utime
.tv_sec
;
1977 a_user_rusage_p
->ru_utime
.tv_usec
= a_rusage_p
->ru_utime
.tv_usec
;
1978 a_user_rusage_p
->ru_stime
.tv_sec
= a_rusage_p
->ru_stime
.tv_sec
;
1979 a_user_rusage_p
->ru_stime
.tv_usec
= a_rusage_p
->ru_stime
.tv_usec
;
1981 * everything else can be a direct assign, since there is no loss
1982 * of precision implied boing 32->64.
1984 a_user_rusage_p
->ru_maxrss
= a_rusage_p
->ru_maxrss
;
1985 a_user_rusage_p
->ru_ixrss
= a_rusage_p
->ru_ixrss
;
1986 a_user_rusage_p
->ru_idrss
= a_rusage_p
->ru_idrss
;
1987 a_user_rusage_p
->ru_isrss
= a_rusage_p
->ru_isrss
;
1988 a_user_rusage_p
->ru_minflt
= a_rusage_p
->ru_minflt
;
1989 a_user_rusage_p
->ru_majflt
= a_rusage_p
->ru_majflt
;
1990 a_user_rusage_p
->ru_nswap
= a_rusage_p
->ru_nswap
;
1991 a_user_rusage_p
->ru_inblock
= a_rusage_p
->ru_inblock
;
1992 a_user_rusage_p
->ru_oublock
= a_rusage_p
->ru_oublock
;
1993 a_user_rusage_p
->ru_msgsnd
= a_rusage_p
->ru_msgsnd
;
1994 a_user_rusage_p
->ru_msgrcv
= a_rusage_p
->ru_msgrcv
;
1995 a_user_rusage_p
->ru_nsignals
= a_rusage_p
->ru_nsignals
;
1996 a_user_rusage_p
->ru_nvcsw
= a_rusage_p
->ru_nvcsw
;
1997 a_user_rusage_p
->ru_nivcsw
= a_rusage_p
->ru_nivcsw
;
2000 /* For a 64-bit kernel and 32-bit userspace, munging may be needed */
2001 __private_extern__
void
2002 munge_user32_rusage(struct rusage
*a_rusage_p
, struct user32_rusage
*a_user_rusage_p
)
2004 /* timeval changes size, so utime and stime need special handling */
2005 a_user_rusage_p
->ru_utime
.tv_sec
= a_rusage_p
->ru_utime
.tv_sec
;
2006 a_user_rusage_p
->ru_utime
.tv_usec
= a_rusage_p
->ru_utime
.tv_usec
;
2007 a_user_rusage_p
->ru_stime
.tv_sec
= a_rusage_p
->ru_stime
.tv_sec
;
2008 a_user_rusage_p
->ru_stime
.tv_usec
= a_rusage_p
->ru_stime
.tv_usec
;
2010 * everything else can be a direct assign. We currently ignore
2011 * the loss of precision
2013 a_user_rusage_p
->ru_maxrss
= a_rusage_p
->ru_maxrss
;
2014 a_user_rusage_p
->ru_ixrss
= a_rusage_p
->ru_ixrss
;
2015 a_user_rusage_p
->ru_idrss
= a_rusage_p
->ru_idrss
;
2016 a_user_rusage_p
->ru_isrss
= a_rusage_p
->ru_isrss
;
2017 a_user_rusage_p
->ru_minflt
= a_rusage_p
->ru_minflt
;
2018 a_user_rusage_p
->ru_majflt
= a_rusage_p
->ru_majflt
;
2019 a_user_rusage_p
->ru_nswap
= a_rusage_p
->ru_nswap
;
2020 a_user_rusage_p
->ru_inblock
= a_rusage_p
->ru_inblock
;
2021 a_user_rusage_p
->ru_oublock
= a_rusage_p
->ru_oublock
;
2022 a_user_rusage_p
->ru_msgsnd
= a_rusage_p
->ru_msgsnd
;
2023 a_user_rusage_p
->ru_msgrcv
= a_rusage_p
->ru_msgrcv
;
2024 a_user_rusage_p
->ru_nsignals
= a_rusage_p
->ru_nsignals
;
2025 a_user_rusage_p
->ru_nvcsw
= a_rusage_p
->ru_nvcsw
;
2026 a_user_rusage_p
->ru_nivcsw
= a_rusage_p
->ru_nivcsw
;