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>
121 /* Do not include dtrace.h, it redefines kmem_[alloc/free] */
122 extern void (*dtrace_fasttrap_exit_ptr
)(proc_t
);
123 extern void (*dtrace_helpers_cleanup
)(proc_t
);
124 extern void dtrace_lazy_dofs_destroy(proc_t
);
126 #include <sys/dtrace_ptss.h>
130 #include <security/mac.h>
131 #include <sys/syscall.h>
134 #include <mach/mach_types.h>
135 #include <mach/task.h>
136 #include <mach/thread_act.h>
140 extern char init_task_failure_data
[];
141 void proc_prepareexit(proc_t p
, int rv
);
142 void vfork_exit(proc_t p
, int rv
);
143 void vproc_exit(proc_t p
);
144 __private_extern__
void munge_user64_rusage(struct rusage
*a_rusage_p
, struct user64_rusage
*a_user_rusage_p
);
145 __private_extern__
void munge_user32_rusage(struct rusage
*a_rusage_p
, struct user32_rusage
*a_user_rusage_p
);
146 static int reap_child_locked(proc_t parent
, proc_t child
, int deadparent
, int locked
, int droplock
);
149 * Things which should have prototypes in headers, but don't
151 void *get_bsduthreadarg(thread_t
);
152 void proc_exit(proc_t p
);
153 int wait1continue(int result
);
154 int waitidcontinue(int result
);
155 int *get_bsduthreadrval(thread_t
);
156 kern_return_t
sys_perf_notify(thread_t thread
, int pid
);
157 kern_return_t
abnormal_exit_notify(mach_exception_data_type_t code
,
158 mach_exception_data_type_t subcode
);
159 void workqueue_exit(struct proc
*);
163 * NOTE: Source and target may *NOT* overlap!
164 * XXX Should share code with bsd/dev/ppc/unix_signal.c
167 siginfo_user_to_user32(user_siginfo_t
*in
, user32_siginfo_t
*out
)
169 out
->si_signo
= in
->si_signo
;
170 out
->si_errno
= in
->si_errno
;
171 out
->si_code
= in
->si_code
;
172 out
->si_pid
= in
->si_pid
;
173 out
->si_uid
= in
->si_uid
;
174 out
->si_status
= in
->si_status
;
175 out
->si_addr
= CAST_DOWN_EXPLICIT(user32_addr_t
,in
->si_addr
);
176 /* following cast works for sival_int because of padding */
177 out
->si_value
.sival_ptr
= CAST_DOWN_EXPLICIT(user32_addr_t
,in
->si_value
.sival_ptr
);
178 out
->si_band
= in
->si_band
; /* range reduction */
182 siginfo_user_to_user64(user_siginfo_t
*in
, user64_siginfo_t
*out
)
184 out
->si_signo
= in
->si_signo
;
185 out
->si_errno
= in
->si_errno
;
186 out
->si_code
= in
->si_code
;
187 out
->si_pid
= in
->si_pid
;
188 out
->si_uid
= in
->si_uid
;
189 out
->si_status
= in
->si_status
;
190 out
->si_addr
= in
->si_addr
;
191 /* following cast works for sival_int because of padding */
192 out
->si_value
.sival_ptr
= in
->si_value
.sival_ptr
;
193 out
->si_band
= in
->si_band
; /* range reduction */
201 exit(proc_t p
, struct exit_args
*uap
, int *retval
)
203 exit1(p
, W_EXITCODE(uap
->rval
, 0), retval
);
205 /* drop funnel before we return */
206 thread_exception_return();
209 thread_block(THREAD_CONTINUE_NULL
);
214 * Exit: deallocate address space and other resources, change proc state
215 * to zombie, and unlink proc from allproc and parent's lists. Save exit
216 * status and rusage for wait(). Check for child processes and orphan them.
219 exit1(proc_t p
, int rv
, int *retval
)
221 thread_t self
= current_thread();
222 struct task
*task
= p
->task
;
226 * If a thread in this task has already
227 * called exit(), then halt any others
231 ut
= get_bsdthread_info(self
);
232 if (ut
->uu_flag
& UT_VFORK
) {
234 vfork_return(p
, retval
, p
->p_pid
);
235 unix_syscall_return(0);
240 * The parameter list of audit_syscall_exit() was augmented to
241 * take the Darwin syscall number as the first parameter,
242 * which is currently required by mac_audit_postselect().
246 * The BSM token contains two components: an exit status as passed
247 * to exit(), and a return value to indicate what sort of exit it
248 * was. The exit status is WEXITSTATUS(rv), but it's not clear
249 * what the return value is.
251 AUDIT_ARG(exit
, WEXITSTATUS(rv
), 0);
252 AUDIT_SYSCALL_EXIT(SYS_exit
, p
, ut
, 0); /* Exit is always successfull */
254 DTRACE_PROC1(exit
, int, CLD_EXITED
);
257 while (p
->exit_thread
!= self
) {
258 if (sig_try_locked(p
) <= 0) {
259 if (get_threadtask(self
) != task
) {
264 thread_terminate(self
);
265 thread_exception_return();
272 printf("pid 1 exited (signal %d, exit %d)",
273 WTERMSIG(rv
), WEXITSTATUS(rv
));
274 panic("%s died\nState at Last Exception:\n\n%s",
275 (p
->p_comm
[0] != '\0' ?
278 init_task_failure_data
);
281 p
->p_lflag
|= P_LEXIT
;
286 proc_prepareexit(p
, rv
);
288 /* task terminate will call proc_terminate and that cleans it up */
289 task_terminate_internal(task
);
295 proc_prepareexit(proc_t p
, int rv
)
297 mach_exception_data_type_t code
, subcode
;
299 thread_t self
= current_thread();
300 ut
= get_bsdthread_info(self
);
302 /* If a core should be generated, notify crash reporter */
303 if (hassigprop(WTERMSIG(rv
), SA_CORE
)) {
305 * Workaround for processes checking up on PT_DENY_ATTACH:
306 * should be backed out post-Leopard (details in 5431025).
308 if ((SIGSEGV
== WTERMSIG(rv
)) &&
309 (p
->p_pptr
->p_lflag
& P_LNOATTACH
)) {
314 * Crash Reporter looks for the signal value, original exception
315 * type, and low 20 bits of the original code in code[0]
316 * (8, 4, and 20 bits respectively). code[1] is unmodified.
318 code
= ((WTERMSIG(rv
) & 0xff) << 24) |
319 ((ut
->uu_exception
& 0x0f) << 20) |
320 ((int)ut
->uu_code
& 0xfffff);
321 subcode
= ut
->uu_subcode
;
322 (void) abnormal_exit_notify(code
, subcode
);
326 /* Notify the perf server */
327 (void)sys_perf_notify(self
, p
->p_pid
);
330 * Remove proc from allproc queue and from pidhash chain.
331 * Need to do this before we do anything that can block.
332 * Not doing causes things like mount() find this on allproc
333 * in partially cleaned state.
338 LIST_REMOVE(p
, p_list
);
339 LIST_INSERT_HEAD(&zombproc
, p
, p_list
); /* Place onto zombproc. */
340 /* will not be visible via proc_find */
341 p
->p_listflag
|= P_LIST_EXITED
;
350 * If parent is waiting for us to exit or exec,
351 * P_LPPWAIT is set; we will wakeup the parent below.
354 p
->p_lflag
&= ~(P_LTRACED
| P_LPPWAIT
);
355 p
->p_sigignore
= ~(sigcantmask
);
365 struct task
*task
= p
->task
;
366 vnode_t tvp
= NULLVP
;
368 struct session
*sessp
;
369 struct uthread
* uth
;
373 /* This can happen if thread_terminate of the single thread
377 uth
= (struct uthread
*)get_bsdthread_info(current_thread());
380 if( !(p
->p_lflag
& P_LEXIT
)) {
381 p
->p_lflag
|= P_LEXIT
;
383 proc_prepareexit(p
, 0);
387 p
->p_lflag
|= P_LPEXIT
;
390 exitval
= p
->p_xstat
;
391 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC
, BSD_PROC_EXIT
) | DBG_FUNC_START
,
392 pid
, exitval
, 0, 0, 0);
396 * Free any outstanding lazy dof entries. It is imperative we
397 * always call dtrace_lazy_dofs_destroy, rather than null check
398 * and call if !NULL. If we NULL test, during lazy dof faulting
399 * we can race with the faulting code and proceed from here to
400 * beyond the helpers cleanup. The lazy dof faulting will then
401 * install new helpers which will never be cleaned up, and leak.
403 dtrace_lazy_dofs_destroy(p
);
406 * Clean up any DTrace helper actions or probes for the process.
408 if (p
->p_dtrace_helpers
!= NULL
) {
409 (*dtrace_helpers_cleanup
)(p
);
413 * Clean up any DTrace probes associated with this process.
416 * APPLE NOTE: We release ptss pages/entries in dtrace_fasttrap_exit_ptr(),
417 * call this after dtrace_helpers_cleanup()
420 if (p
->p_dtrace_probes
&& dtrace_fasttrap_exit_ptr
) {
421 (*dtrace_fasttrap_exit_ptr
)(p
);
426 /* XXX Zombie allocation may fail, in which case stats get lost */
427 MALLOC_ZONE(p
->p_ru
, struct rusage
*,
428 sizeof (*p
->p_ru
), M_ZOMBIE
, M_WAITOK
);
431 * need to cancel async IO requests that can be cancelled and wait for those
432 * already active. MAY BLOCK!
442 * Close open files and release open-file table.
447 if (uth
->uu_lowpri_window
) {
449 * task is marked as a low priority I/O type
450 * and the I/O we issued while in flushing files on close
451 * collided with normal I/O operations...
452 * no need to throttle this thread since its going away
453 * but we do need to update our bookeeping w/r to throttled threads
455 throttle_lowpri_io(FALSE
);
459 /* Close ref SYSV Shared memory*/
464 /* Release SYSV semaphores */
469 pth_proc_hashdelete(p
);
472 sessp
= proc_session(p
);
473 if (SESS_LEADER(p
, sessp
)) {
475 if (sessp
->s_ttyvp
!= NULLVP
) {
478 struct vfs_context context
;
483 * Controlling process.
484 * Signal foreground pgrp,
485 * drain controlling terminal
486 * and revoke access to controlling terminal.
488 tp
= SESSION_TP(sessp
);
490 if ((tp
!= TTY_NULL
) && (tp
->t_session
== sessp
)) {
491 tty_pgsignal(tp
, SIGHUP
, 1);
494 /* reget potentially tp due to revocation */
495 tp
= SESSION_TP(sessp
);
496 ttyvp
= sessp
->s_ttyvp
;
497 ttyvid
= sessp
->s_ttyvid
;
498 sessp
->s_ttyvp
= NULLVP
;
500 sessp
->s_ttyp
= TTY_NULL
;
501 sessp
->s_ttypgrpid
= NO_PID
;
502 session_unlock(sessp
);
504 if ((ttyvp
!= NULLVP
) && (vnode_getwithvid(ttyvp
, ttyvid
) == 0)) {
506 if (tp
!= TTY_NULL
) {
511 context
.vc_thread
= proc_thread(p
); /* XXX */
512 context
.vc_ucred
= kauth_cred_proc_ref(p
);
513 VNOP_REVOKE(ttyvp
, REVOKEALL
, &context
);
515 kauth_cred_unref(&context
.vc_ucred
);
519 /* reget potentially tp due to revocation */
520 tp
= SESSION_TP(sessp
);
521 ttyvp
= sessp
->s_ttyvp
;
522 sessp
->s_ttyvp
= NULLVP
;
524 sessp
->s_ttyp
= TTY_NULL
;
525 sessp
->s_ttypgrpid
= NO_PID
;
526 session_unlock(sessp
);
531 * s_ttyp is not zero'd; we use this to indicate
532 * that the session once had a controlling terminal.
533 * (for logging and informational purposes)
538 sessp
->s_leader
= NULL
;
539 session_unlock(sessp
);
547 p
->p_rlimit
[RLIMIT_FSIZE
].rlim_cur
= RLIM_INFINITY
;
548 (void)acct_process(p
);
552 if ((p
->p_listflag
& P_LIST_EXITCOUNT
) == P_LIST_EXITCOUNT
) {
553 p
->p_listflag
&= ~P_LIST_EXITCOUNT
;
554 proc_shutdown_exitcount
--;
555 if (proc_shutdown_exitcount
== 0)
556 wakeup(&proc_shutdown_exitcount
);
559 /* wait till parentrefs are dropped and grant no more */
560 proc_childdrainstart(p
);
561 while ((q
= p
->p_children
.lh_first
) != NULL
) {
562 q
->p_listflag
|= P_LIST_DEADPARENT
;
563 if (q
->p_stat
== SZOMB
) {
565 panic("parent child linkage broken");
566 /* check for sysctl zomb lookup */
567 while ((q
->p_listflag
& P_LIST_WAITING
) == P_LIST_WAITING
) {
568 msleep(&q
->p_stat
, proc_list_mlock
, PWAIT
, "waitcoll", 0);
570 q
->p_listflag
|= P_LIST_WAITING
;
572 * This is a named reference and it is not granted
573 * if the reap is already in progress. So we get
574 * the reference here exclusively and their can be
575 * no waiters. So there is no need for a wakeup
576 * after we are done. AlsO the reap frees the structure
577 * and the proc struct cannot be used for wakeups as well.
578 * It is safe to use q here as this is system reap
580 (void)reap_child_locked(p
, q
, 1, 1, 0);
582 proc_reparentlocked(q
, initproc
, 0, 1);
584 * Traced processes are killed
585 * since their existence means someone is messing up.
587 if (q
->p_lflag
& P_LTRACED
) {
590 q
->p_lflag
&= ~P_LTRACED
;
591 if (q
->sigwait_thread
) {
594 * The sigwait_thread could be stopped at a
595 * breakpoint. Wake it up to kill.
596 * Need to do this as it could be a thread which is not
597 * the first thread in the task. So any attempts to kill
598 * the process would result into a deadlock on q->sigwait.
600 thread_resume((thread_t
)q
->sigwait_thread
);
601 clear_wait(q
->sigwait_thread
, THREAD_INTERRUPTED
);
602 threadsignal((thread_t
)q
->sigwait_thread
, SIGKILL
, 0);
611 proc_childdrainend(p
);
615 * Release reference to text vnode
624 * Save exit status and final rusage info, adding in child rusage
625 * info and self times. If we were unable to allocate a zombie
626 * structure, this information is lost.
628 /* No need for locking here as no one than this thread can access this */
629 if (p
->p_ru
!= NULL
) {
630 *p
->p_ru
= p
->p_stats
->p_ru
;
632 calcru(p
, &p
->p_ru
->ru_utime
, &p
->p_ru
->ru_stime
, NULL
);
634 ruadd(p
->p_ru
, &p
->p_stats
->p_cru
);
638 * Free up profiling buffers.
641 struct uprof
*p0
= &p
->p_stats
->p_prof
, *p1
, *pn
;
647 for (; p1
!= NULL
; p1
= pn
) {
649 kfree(p1
, sizeof *p1
);
654 if (thread_call_cancel(p
->p_rcall
))
657 while (p
->p_ractive
> 0) {
666 thread_call_free(p
->p_rcall
);
670 * Other substructures are freed from wait().
672 FREE_ZONE(p
->p_stats
, sizeof *p
->p_stats
, M_PSTATS
);
675 FREE_ZONE(p
->p_sigacts
, sizeof *p
->p_sigacts
, M_SIGACTS
);
678 proc_limitdrop(p
, 1);
683 * Finish up by terminating the task
684 * and halt this thread (only if a
685 * member of the task exiting).
688 set_bsdtask_info(task
, NULL
);
690 proc_knote(p
, NOTE_EXIT
);
692 /* mark the thread as the one that is doing proc_exit
693 * no need to hold proc lock in uthread_free
695 uth
->uu_flag
|= UT_PROCEXIT
;
697 * Notify parent that we're gone.
700 if (pp
->p_flag
& P_NOCLDWAIT
) {
704 * If the parent is ignoring SIGCHLD, then POSIX requires
705 * us to not add the resource usage to the parent process -
706 * we are only going to hand it off to init to get reaped.
707 * We should contest the standard in this case on the basis
712 * Add child resource usage to parent before giving
713 * zombie to init. If we were unable to allocate a
714 * zombie structure, this information is lost.
716 if (p
->p_ru
!= NULL
) {
718 ruadd(&pp
->p_stats
->p_cru
, p
->p_ru
);
721 #endif /* !3839178 */
723 /* kernel can reap this one, no need to move it to launchd */
725 p
->p_listflag
|= P_LIST_DEADPARENT
;
728 if ((p
->p_listflag
& P_LIST_DEADPARENT
) == 0) {
729 if (pp
!= initproc
) {
731 pp
->si_pid
= p
->p_pid
;
732 pp
->si_status
= p
->p_xstat
;
733 pp
->si_code
= CLD_EXITED
;
735 * p_ucred usage is safe as it is an exiting process
736 * and reference is dropped in reap
738 pp
->si_uid
= p
->p_ucred
->cr_ruid
;
741 /* mark as a zombie */
742 /* No need to take proc lock as all refs are drained and
743 * no one except parent (reaping ) can look at this.
744 * The write is to an int and is coherent. Also parent is
745 * keyed off of list lock for reaping
747 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC
, BSD_PROC_EXIT
) | DBG_FUNC_END
,
748 pid
, exitval
, 0, 0, 0);
751 * The current process can be reaped so, no one
755 psignal(pp
, SIGCHLD
);
757 /* and now wakeup the parent */
762 /* should be fine as parent proc would be initproc */
763 /* mark as a zombie */
764 /* No need to take proc lock as all refs are drained and
765 * no one except parent (reaping ) can look at this.
766 * The write is to an int and is coherent. Also parent is
767 * keyed off of list lock for reaping
770 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC
, BSD_PROC_EXIT
) | DBG_FUNC_END
,
771 pid
, exitval
, 0, 0, 0);
772 /* check for sysctl zomb lookup */
773 while ((p
->p_listflag
& P_LIST_WAITING
) == P_LIST_WAITING
) {
774 msleep(&p
->p_stat
, proc_list_mlock
, PWAIT
, "waitcoll", 0);
776 /* safe to use p as this is a system reap */
778 p
->p_listflag
|= P_LIST_WAITING
;
781 * This is a named reference and it is not granted
782 * if the reap is already in progress. So we get
783 * the reference here exclusively and their can be
784 * no waiters. So there is no need for a wakeup
785 * after we are done. AlsO the reap frees the structure
786 * and the proc struct cannot be used for wakeups as well.
787 * It is safe to use p here as this is system reap
789 (void)reap_child_locked(pp
, p
, 1, 1, 1);
790 /* list lock dropped by reap_child_locked */
792 if (uth
->uu_lowpri_window
) {
794 * task is marked as a low priority I/O type and we've
795 * somehow picked up another throttle during exit processing...
796 * no need to throttle this thread since its going away
797 * but we do need to update our bookeeping w/r to throttled threads
799 throttle_lowpri_io(FALSE
);
810 * Description: Given a process from which all status information needed
811 * has already been extracted, if the process is a ptrace
812 * attach process, detach it and give it back to its real
813 * parent, else recover all resources remaining associated
816 * Parameters: proc_t parent Parent of process being reaped
817 * proc_t child Process to reap
819 * Returns: 0 Process was not reaped because it
820 * came from an attach
821 * 1 Process was reaped
824 reap_child_locked(proc_t parent
, proc_t child
, int deadparent
, int locked
, int droplock
)
826 proc_t trace_parent
= PROC_NULL
; /* Traced parent process, if tracing */
832 * If we got the child via a ptrace 'attach',
833 * we need to give it back to the old parent.
835 * Exception: someone who has been reparented to launchd before being
836 * ptraced can simply be reaped, refer to radar 5677288
838 * trace_parent == initproc -> away from launchd
839 * P_LIST_DEADPARENT -> came to launchd by reparenting
841 if (child
->p_oppid
&& (trace_parent
= proc_find(child
->p_oppid
))
842 && !((trace_parent
== initproc
) && (child
->p_lflag
& P_LIST_DEADPARENT
))) {
846 if (trace_parent
!= initproc
) {
848 * proc internal fileds and p_ucred usage safe
849 * here as child is dead and is not reaped or
852 proc_lock(trace_parent
);
853 trace_parent
->si_pid
= child
->p_pid
;
854 trace_parent
->si_status
= child
->p_xstat
;
855 trace_parent
->si_code
= CLD_CONTINUED
;
856 trace_parent
->si_uid
= child
->p_ucred
->cr_ruid
;
857 proc_unlock(trace_parent
);
859 proc_reparentlocked(child
, trace_parent
, 1, 0);
860 psignal(trace_parent
, SIGCHLD
);
862 wakeup((caddr_t
)trace_parent
);
863 child
->p_listflag
&= ~P_LIST_WAITING
;
864 wakeup(&child
->p_stat
);
866 proc_rele(trace_parent
);
867 if ((locked
== 1) && (droplock
== 0))
872 if (trace_parent
!= PROC_NULL
) {
873 proc_rele(trace_parent
);
876 proc_knote(child
, NOTE_REAP
);
877 proc_knote_drain(child
);
884 * If the parent is ignoring SIGCHLD, then POSIX requires
885 * us to not add the resource usage to the parent process -
886 * we are only going to hand it off to init to get reaped.
887 * We should contest the standard in this case on the basis
890 if (!(parent
->p_flag
& P_NOCLDWAIT
))
892 ruadd(&parent
->p_stats
->p_cru
, child
->p_ru
);
894 FREE_ZONE(child
->p_ru
, sizeof *child
->p_ru
, M_ZOMBIE
);
897 printf("Warning : lost p_ru for %s\n", child
->p_comm
);
900 AUDIT_SESSION_PROCEXIT(child
->p_ucred
);
903 * Decrement the count of procs running with this uid.
904 * p_ucred usage is safe here as it is an exited process.
905 * and refernce is dropped after these calls down below
906 * (locking protection is provided by list lock held in chgproccnt)
908 (void)chgproccnt(child
->p_ucred
->cr_ruid
, -1);
917 * Free up credentials.
919 if (IS_VALID_CRED(child
->p_ucred
)) {
920 kauth_cred_unref(&child
->p_ucred
);
923 /* XXXX Note NOT SAFE TO USE p_ucred from this point onwards */
926 * Finally finished with old proc entry.
927 * Unlink it from its process group and free it.
932 LIST_REMOVE(child
, p_list
); /* off zombproc */
933 parent
->p_childrencnt
--;
934 LIST_REMOVE(child
, p_sibling
);
935 /* If there are no more children wakeup parent */
936 if ((deadparent
!= 0) && (LIST_EMPTY(&parent
->p_children
)))
937 wakeup((caddr_t
)parent
); /* with list lock held */
938 child
->p_listflag
&= ~P_LIST_WAITING
;
939 wakeup(&child
->p_stat
);
941 /* Take it out of process hash */
942 LIST_REMOVE(child
, p_hash
);
943 child
->p_listflag
&= ~P_LIST_INHASH
;
944 proc_checkdeadrefs(child
);
949 #ifdef CONFIG_EMBEDDED
950 lck_mtx_destroy(&child
->p_mlock
, proc_lck_grp
);
951 lck_mtx_destroy(&child
->p_fdmlock
, proc_lck_grp
);
953 lck_mtx_destroy(&child
->p_dtrace_sprlock
, proc_lck_grp
);
955 lck_spin_destroy(&child
->p_slock
, proc_lck_grp
);
958 lck_mtx_destroy(&child
->p_mlock
, proc_mlock_grp
);
959 lck_mtx_destroy(&child
->p_fdmlock
, proc_fdmlock_grp
);
961 lck_mtx_destroy(&child
->p_dtrace_sprlock
, proc_lck_grp
);
963 lck_spin_destroy(&child
->p_slock
, proc_slock_grp
);
965 workqueue_destroy_lock(child
);
967 FREE_ZONE(child
, sizeof *child
, M_PROC
);
968 if ((locked
== 1) && (droplock
== 0))
976 wait1continue(int result
)
987 thread
= current_thread();
988 vt
= get_bsduthreadarg(thread
);
989 retval
= get_bsduthreadrval(thread
);
990 return(wait4(p
, (struct wait4_args
*)vt
, retval
));
994 wait4(proc_t q
, struct wait4_args
*uap
, int32_t *retval
)
996 __pthread_testcancel(1);
997 return(wait4_nocancel(q
, (struct wait4_nocancel_args
*)uap
, retval
));
1001 wait4_nocancel(proc_t q
, struct wait4_nocancel_args
*uap
, int32_t *retval
)
1008 AUDIT_ARG(pid
, uap
->pid
);
1011 uap
->pid
= -q
->p_pgrpid
;
1019 for (p
= q
->p_children
.lh_first
; p
!= 0; p
= p
->p_sibling
.le_next
) {
1020 if ( p
->p_sibling
.le_next
!= 0 )
1022 if (uap
->pid
!= WAIT_ANY
&&
1023 p
->p_pid
!= uap
->pid
&&
1024 p
->p_pgrpid
!= -(uap
->pid
))
1029 /* XXX This is racy because we don't get the lock!!!! */
1031 if (p
->p_listflag
& P_LIST_WAITING
) {
1032 (void)msleep(&p
->p_stat
, proc_list_mlock
, PWAIT
, "waitcoll", 0);
1035 p
->p_listflag
|= P_LIST_WAITING
; /* only allow single thread to wait() */
1038 if (p
->p_stat
== SZOMB
) {
1041 if ((error
= mac_proc_check_wait(q
, p
)) != 0)
1044 retval
[0] = p
->p_pid
;
1046 /* Legacy apps expect only 8 bits of status */
1047 status
= 0xffff & p
->p_xstat
; /* convert to int */
1048 error
= copyout((caddr_t
)&status
,
1055 if (p
->p_ru
== NULL
) {
1058 if (IS_64BIT_PROCESS(q
)) {
1059 struct user64_rusage my_rusage
;
1060 munge_user64_rusage(p
->p_ru
, &my_rusage
);
1061 error
= copyout((caddr_t
)&my_rusage
,
1063 sizeof (my_rusage
));
1066 struct user32_rusage my_rusage
;
1067 munge_user32_rusage(p
->p_ru
, &my_rusage
);
1068 error
= copyout((caddr_t
)&my_rusage
,
1070 sizeof (my_rusage
));
1073 /* information unavailable? */
1078 /* Conformance change for 6577252.
1079 * When SIGCHLD is blocked and wait() returns because the status
1080 * of a child process is available and there are no other
1081 * children processes, then any pending SIGCHLD signal is cleared.
1083 if ( sibling_count
== 0 ) {
1084 int mask
= sigmask(SIGCHLD
);
1085 uthread_t uth
= (struct uthread
*)get_bsdthread_info(current_thread());
1087 if ( (uth
->uu_sigmask
& mask
) != 0 ) {
1088 /* we are blocking SIGCHLD signals. clear any pending SIGCHLD.
1089 * This locking looks funny but it is protecting access to the
1090 * thread via p_uthlist.
1093 uth
->uu_siglist
&= ~mask
; /* clear pending signal */
1099 (void)reap_child_locked(q
, p
, 0, 0, 0);
1103 if (p
->p_stat
== SSTOP
&& (p
->p_lflag
& P_LWAITED
) == 0 &&
1104 (p
->p_lflag
& P_LTRACED
|| uap
->options
& WUNTRACED
)) {
1107 if ((error
= mac_proc_check_wait(q
, p
)) != 0)
1111 p
->p_lflag
|= P_LWAITED
;
1113 retval
[0] = p
->p_pid
;
1115 status
= W_STOPCODE(p
->p_xstat
);
1116 error
= copyout((caddr_t
)&status
,
1124 * If we are waiting for continued processses, and this
1125 * process was continued
1127 if ((uap
->options
& WCONTINUED
) &&
1128 (p
->p_flag
& P_CONTINUED
)) {
1131 if ((error
= mac_proc_check_wait(q
, p
)) != 0)
1135 /* Prevent other process for waiting for this event */
1136 OSBitAndAtomic(~((uint32_t)P_CONTINUED
), &p
->p_flag
);
1137 retval
[0] = p
->p_pid
;
1139 status
= W_STOPCODE(SIGCONT
);
1140 error
= copyout((caddr_t
)&status
,
1147 p
->p_listflag
&= ~P_LIST_WAITING
;
1150 /* list lock is held when we get here any which way */
1156 if (uap
->options
& WNOHANG
) {
1162 if ((error
= msleep0((caddr_t
)q
, proc_list_mlock
, PWAIT
| PCATCH
| PDROP
, "wait", 0, wait1continue
)))
1168 p
->p_listflag
&= ~P_LIST_WAITING
;
1176 waitidcontinue(int result
)
1185 thread
= current_thread();
1186 vt
= get_bsduthreadarg(thread
);
1187 retval
= get_bsduthreadrval(thread
);
1188 return(waitid(current_proc(), (struct waitid_args
*)vt
, retval
));
1192 * Description: Suspend the calling thread until one child of the process
1193 * containing the calling thread changes state.
1195 * Parameters: uap->idtype one of P_PID, P_PGID, P_ALL
1196 * uap->id pid_t or gid_t or ignored
1197 * uap->infop Address of signinfo_t struct in
1198 * user space into which to return status
1199 * uap->options flag values
1201 * Returns: 0 Success
1202 * !0 Error returning status to user space
1205 waitid(proc_t q
, struct waitid_args
*uap
, int32_t *retval
)
1207 __pthread_testcancel(1);
1208 return(waitid_nocancel(q
, (struct waitid_nocancel_args
*)uap
, retval
));
1212 waitid_nocancel(proc_t q
, struct waitid_nocancel_args
*uap
, __unused
int32_t *retval
)
1214 user_siginfo_t collect64
; /* siginfo data to return to caller */
1221 * Forced validation of options for T.waitpid 21; should be a TSD!
1222 * This will pass the test, but note that we have more bits than the
1223 * standard specifies that we will allow in, in this case. The test
1224 * passes because they light all the bits, not just the ones we allow,
1225 * and so the following check returns EINVAL like the test wants.
1227 if (((uap
->options
& (WNOHANG
|WNOWAIT
|WCONTINUED
|WUNTRACED
|WSTOPPED
|WEXITED
)) != uap
->options
) ||
1228 (uap
->options
== 0))
1229 return (EINVAL
); /* bits set that aren't recognized */
1232 * Overly critical options checking, per POSIX
1234 switch(uap
->idtype
) {
1235 case P_PID
: /* child with process ID equal to... */
1236 case P_PGID
: /* child with process group ID equal to... */
1237 if (((int)uap
->id
) < 0)
1240 case P_ALL
: /* any child */
1248 for (p
= q
->p_children
.lh_first
; p
!= 0; p
= p
->p_sibling
.le_next
) {
1249 switch(uap
->idtype
) {
1250 case P_PID
: /* child with process ID equal to... */
1251 if (p
->p_pid
!= (pid_t
)uap
->id
)
1254 case P_PGID
: /* child with process group ID equal to... */
1255 if (p
->p_pgrpid
!= (pid_t
)uap
->id
)
1258 case P_ALL
: /* any child */
1262 /* XXX This is racy because we don't get the lock!!!! */
1265 * Wait collision; go to sleep and restart; used to maintain
1266 * the single return for waited process guarantee.
1268 if (p
->p_listflag
& P_LIST_WAITING
) {
1269 (void)msleep(&p
->p_stat
, proc_list_mlock
, PWAIT
, "waitidcoll", 0);
1272 p
->p_listflag
|= P_LIST_WAITING
; /* mark busy */
1277 * Types of processes we are interested in
1279 * XXX Don't know what to do for WCONTINUED?!?
1282 case SZOMB
: /* Exited */
1283 if (!(uap
->options
& WEXITED
))
1286 /* drop the lock and the thread is going to return */
1289 /* Collect "siginfo" information for caller */
1290 collect64
.si_signo
= SIGCHLD
;
1291 collect64
.si_code
= 0;
1292 collect64
.si_errno
= 0;
1293 collect64
.si_pid
= 0;
1294 collect64
.si_uid
= 0;
1295 collect64
.si_addr
= 0;
1296 collect64
.si_status
= WEXITSTATUS(p
->p_xstat
);
1297 collect64
.si_band
= 0;
1299 if (IS_64BIT_PROCESS(p
)) {
1300 user64_siginfo_t sinfo64
;
1302 siginfo_user_to_user64(&collect64
, &sinfo64
);
1304 error
= copyout((caddr_t
)&sinfo64
,
1308 user32_siginfo_t sinfo32
;
1310 siginfo_user_to_user32(&collect64
, &sinfo32
);
1312 error
= copyout((caddr_t
)&sinfo32
,
1316 /* information unavailable? */
1320 /* Prevent other process for waiting for this event? */
1321 if (!(uap
->options
& WNOWAIT
)) {
1323 (void)reap_child_locked(q
, p
, 0, 0, 0);
1326 p
->p_listflag
&= ~P_LIST_WAITING
;
1332 case SSTOP
: /* Stopped */
1334 * If we are not interested in stopped processes, then
1337 if (!(uap
->options
& WSTOPPED
))
1341 * If someone has already waited it, we lost a race
1342 * to be the one to return status.
1344 if ((p
->p_lflag
& P_LWAITED
) != 0)
1347 /* drop the lock and the thread is going to return */
1350 /* Collect "siginfo" information for caller */
1351 collect64
.si_signo
= SIGCHLD
;
1352 collect64
.si_code
= 0;
1353 collect64
.si_errno
= 0;
1354 collect64
.si_pid
= 0;
1355 collect64
.si_uid
= 0;
1356 collect64
.si_addr
= 0;
1358 collect64
.si_status
= p
->p_xstat
;
1360 collect64
.si_band
= 0;
1362 if (IS_64BIT_PROCESS(p
)) {
1363 user64_siginfo_t sinfo64
;
1365 siginfo_user_to_user64(&collect64
, &sinfo64
);
1367 error
= copyout((caddr_t
)&sinfo64
,
1371 user32_siginfo_t sinfo32
;
1373 siginfo_user_to_user32(&collect64
, &sinfo32
);
1375 error
= copyout((caddr_t
)&sinfo32
,
1379 /* information unavailable? */
1383 /* Prevent other process for waiting for this event? */
1384 if (!(uap
->options
& WNOWAIT
)) {
1386 p
->p_lflag
|= P_LWAITED
;
1393 default: /* All others */
1394 /* ...meaning Continued */
1395 if (!(uap
->options
& WCONTINUED
))
1399 * If the flag isn't set, then this process has not
1400 * been stopped and continued, or the status has
1401 * already been reaped by another caller of waitid().
1403 if ((p
->p_flag
& P_CONTINUED
) == 0)
1406 /* drop the lock and the thread is going to return */
1409 /* Collect "siginfo" information for caller */
1411 collect64
.si_signo
= SIGCHLD
;
1412 collect64
.si_code
= CLD_CONTINUED
;
1413 collect64
.si_errno
= 0;
1414 collect64
.si_pid
= p
->p_contproc
;
1415 collect64
.si_uid
= 0;
1416 collect64
.si_addr
= 0;
1417 collect64
.si_status
= p
->p_xstat
;
1418 collect64
.si_band
= 0;
1421 if (IS_64BIT_PROCESS(p
)) {
1422 user64_siginfo_t sinfo64
;
1424 siginfo_user_to_user64(&collect64
, &sinfo64
);
1426 error
= copyout((caddr_t
)&sinfo64
,
1430 user32_siginfo_t sinfo32
;
1432 siginfo_user_to_user32(&collect64
, &sinfo32
);
1434 error
= copyout((caddr_t
)&sinfo32
,
1438 /* information unavailable? */
1442 /* Prevent other process for waiting for this event? */
1443 if (!(uap
->options
& WNOWAIT
)) {
1444 OSBitAndAtomic(~((uint32_t)P_CONTINUED
), &p
->p_flag
);
1450 /* LIST LOCK IS HELD HERE */
1451 /* Not a process we are interested in; go on to next child */
1453 p
->p_listflag
&= ~P_LIST_WAITING
;
1457 /* list lock is always held */
1458 /* No child processes that could possibly satisfy the request? */
1464 if (uap
->options
& WNOHANG
) {
1469 if ((error
= msleep0((caddr_t
)q
, proc_list_mlock
, PWAIT
| PCATCH
| PDROP
, "waitid", 0, waitidcontinue
)))
1475 p
->p_listflag
&= ~P_LIST_WAITING
;
1482 * make process 'parent' the new parent of process 'child'.
1485 proc_reparentlocked(proc_t child
, proc_t parent
, int cansignal
, int locked
)
1487 proc_t oldparent
= PROC_NULL
;
1489 if (child
->p_pptr
== parent
)
1495 oldparent
= child
->p_pptr
;
1496 #if __PROC_INTERNAL_DEBUG
1497 if (oldparent
== PROC_NULL
)
1498 panic("proc_reparent: process %p does not have a parent\n", child
);
1501 LIST_REMOVE(child
, p_sibling
);
1502 #if __PROC_INTERNAL_DEBUG
1503 if (oldparent
->p_childrencnt
== 0)
1504 panic("process children count already 0\n");
1506 oldparent
->p_childrencnt
--;
1507 #if __PROC_INTERNAL_DEBUG1
1508 if (oldparent
->p_childrencnt
< 0)
1509 panic("process children count -ve\n");
1511 LIST_INSERT_HEAD(&parent
->p_children
, child
, p_sibling
);
1512 parent
->p_childrencnt
++;
1513 child
->p_pptr
= parent
;
1514 child
->p_ppid
= parent
->p_pid
;
1518 if ((cansignal
!= 0) && (initproc
== parent
) && (child
->p_stat
== SZOMB
))
1519 psignal(initproc
, SIGCHLD
);
1525 * Exit: deallocate address space and other resources, change proc state
1526 * to zombie, and unlink proc from allproc and parent's lists. Save exit
1527 * status and rusage for wait(). Check for child processes and orphan them.
1531 vfork_exit(proc_t p
, int rv
)
1533 vfork_exit_internal(p
, rv
, 0);
1537 vfork_exit_internal(proc_t p
, int rv
, int forceexit
)
1539 thread_t self
= current_thread();
1541 struct task
*task
= p
->task
;
1546 * If a thread in this task has already
1547 * called exit(), then halt any others
1551 ut
= get_bsdthread_info(self
);
1555 if ((p
->p_lflag
& P_LPEXIT
) == P_LPEXIT
) {
1557 * This happens when a parent exits/killed and vfork is in progress
1558 * other threads. But shutdown code for ex has already called exit1()
1563 p
->p_lflag
|= (P_LEXIT
| P_LPEXIT
);
1566 if (forceexit
== 0) {
1568 * parent of a vfork child has already called exit() and the
1569 * thread that has vfork in proress terminates. So there is no
1570 * separate address space here and it has already been marked for
1571 * termination. This was never covered before and could cause problems
1572 * if we block here for outside code.
1574 /* Notify the perf server */
1575 (void)sys_perf_notify(self
, p
->p_pid
);
1579 * Remove proc from allproc queue and from pidhash chain.
1580 * Need to do this before we do anything that can block.
1581 * Not doing causes things like mount() find this on allproc
1582 * in partially cleaned state.
1587 LIST_REMOVE(p
, p_list
);
1588 LIST_INSERT_HEAD(&zombproc
, p
, p_list
); /* Place onto zombproc. */
1589 /* will not be visible via proc_find */
1590 p
->p_listflag
|= P_LIST_EXITED
;
1596 p
->p_lflag
&= ~(P_LTRACED
| P_LPPWAIT
);
1597 p
->p_sigignore
= ~0;
1601 if (thread_call_cancel(p
->p_rcall
))
1604 while (p
->p_ractive
> 0) {
1613 thread_call_free(p
->p_rcall
);
1622 vproc_exit(proc_t p
)
1629 struct task
*task
= p
->task
;
1632 struct session
*sessp
;
1634 /* XXX Zombie allocation may fail, in which case stats get lost */
1635 MALLOC_ZONE(p
->p_ru
, struct rusage
*,
1636 sizeof (*p
->p_ru
), M_ZOMBIE
, M_WAITOK
);
1642 * Close open files and release open-file table.
1647 sessp
= proc_session(p
);
1648 if (SESS_LEADER(p
, sessp
)) {
1650 if (sessp
->s_ttyvp
!= NULLVP
) {
1651 struct vnode
*ttyvp
;
1653 struct vfs_context context
;
1657 * Controlling process.
1658 * Signal foreground pgrp,
1659 * drain controlling terminal
1660 * and revoke access to controlling terminal.
1662 tp
= SESSION_TP(sessp
);
1664 if ((tp
!= TTY_NULL
) && (tp
->t_session
== sessp
)) {
1665 tty_pgsignal(tp
, SIGHUP
, 1);
1670 * The tty could have been revoked
1674 session_lock(sessp
);
1675 /* reget in case of race */
1676 tp
= SESSION_TP(sessp
);
1677 ttyvp
= sessp
->s_ttyvp
;
1678 ttyvid
= sessp
->s_ttyvid
;
1679 sessp
->s_ttyvp
= NULL
;
1680 sessp
->s_ttyvid
= 0;
1681 sessp
->s_ttyp
= TTY_NULL
;
1682 sessp
->s_ttypgrpid
= NO_PID
;
1683 session_unlock(sessp
);
1685 if ((ttyvp
!= NULLVP
) && (vnode_getwithvid(ttyvp
, ttyvid
) == 0)) {
1686 context
.vc_thread
= proc_thread(p
); /* XXX */
1687 context
.vc_ucred
= kauth_cred_proc_ref(p
);
1688 VNOP_REVOKE(ttyvp
, REVOKEALL
, &context
);
1690 kauth_cred_unref(&context
.vc_ucred
);
1693 session_lock(sessp
);
1694 ttyvp
= sessp
->s_ttyvp
;
1695 sessp
->s_ttyvp
= NULL
;
1696 sessp
->s_ttyvid
= 0;
1697 sessp
->s_ttyp
= TTY_NULL
;
1698 sessp
->s_ttypgrpid
= NO_PID
;
1699 session_unlock(sessp
);
1704 * s_ttyp is not zero'd; we use this to indicate
1705 * that the session once had a controlling terminal.
1706 * (for logging and informational purposes)
1710 session_lock(sessp
);
1711 sessp
->s_leader
= NULL
;
1712 session_unlock(sessp
);
1714 session_rele(sessp
);
1720 p
->p_rlimit
[RLIMIT_FSIZE
].rlim_cur
= RLIM_INFINITY
;
1723 proc_childdrainstart(p
);
1724 while ((q
= p
->p_children
.lh_first
) != NULL
) {
1725 q
->p_listflag
|= P_LIST_DEADPARENT
;
1726 if (q
->p_stat
== SZOMB
) {
1728 panic("parent child linkage broken");
1729 /* check for lookups by zomb sysctl */
1730 while ((q
->p_listflag
& P_LIST_WAITING
) == P_LIST_WAITING
) {
1731 msleep(&q
->p_stat
, proc_list_mlock
, PWAIT
, "waitcoll", 0);
1733 q
->p_listflag
|= P_LIST_WAITING
;
1735 * This is a named reference and it is not granted
1736 * if the reap is already in progress. So we get
1737 * the reference here exclusively and their can be
1738 * no waiters. So there is no need for a wakeup
1739 * after we are done. AlsO the reap frees the structure
1740 * and the proc struct cannot be used for wakeups as well.
1741 * It is safe to use q here as this is system reap
1743 (void)reap_child_locked(p
, q
, 1, 1, 0);
1745 proc_reparentlocked(q
, initproc
, 0, 1);
1747 * Traced processes are killed
1748 * since their existence means someone is messing up.
1750 if (q
->p_lflag
& P_LTRACED
) {
1753 q
->p_lflag
&= ~P_LTRACED
;
1754 if (q
->sigwait_thread
) {
1757 * The sigwait_thread could be stopped at a
1758 * breakpoint. Wake it up to kill.
1759 * Need to do this as it could be a thread which is not
1760 * the first thread in the task. So any attempts to kill
1761 * the process would result into a deadlock on q->sigwait.
1763 thread_resume((thread_t
)q
->sigwait_thread
);
1764 clear_wait(q
->sigwait_thread
, THREAD_INTERRUPTED
);
1765 threadsignal((thread_t
)q
->sigwait_thread
, SIGKILL
, 0);
1769 psignal(q
, SIGKILL
);
1775 proc_childdrainend(p
);
1779 * Release reference to text vnode
1783 if (tvp
!= NULLVP
) {
1788 * Save exit status and final rusage info, adding in child rusage
1789 * info and self times. If we were unable to allocate a zombie
1790 * structure, this information is lost.
1792 /* No need for locking here as no one than this thread can access this */
1793 if (p
->p_ru
!= NULL
) {
1794 *p
->p_ru
= p
->p_stats
->p_ru
;
1795 timerclear(&p
->p_ru
->ru_utime
);
1796 timerclear(&p
->p_ru
->ru_stime
);
1800 task_basic_info_data_t tinfo
;
1801 task_thread_times_info_data_t ttimesinfo
;
1802 int task_info_stuff
, task_ttimes_stuff
;
1803 struct timeval ut
,st
;
1805 task_info_stuff
= TASK_BASIC_INFO_COUNT
;
1806 task_info(task
, TASK_BASIC_INFO
,
1807 &tinfo
, &task_info_stuff
);
1808 p
->p_ru
->ru_utime
.tv_sec
= tinfo
.user_time
.seconds
;
1809 p
->p_ru
->ru_utime
.tv_usec
= tinfo
.user_time
.microseconds
;
1810 p
->p_ru
->ru_stime
.tv_sec
= tinfo
.system_time
.seconds
;
1811 p
->p_ru
->ru_stime
.tv_usec
= tinfo
.system_time
.microseconds
;
1813 task_ttimes_stuff
= TASK_THREAD_TIMES_INFO_COUNT
;
1814 task_info(task
, TASK_THREAD_TIMES_INFO
,
1815 &ttimesinfo
, &task_ttimes_stuff
);
1817 ut
.tv_sec
= ttimesinfo
.user_time
.seconds
;
1818 ut
.tv_usec
= ttimesinfo
.user_time
.microseconds
;
1819 st
.tv_sec
= ttimesinfo
.system_time
.seconds
;
1820 st
.tv_usec
= ttimesinfo
.system_time
.microseconds
;
1821 timeradd(&ut
,&p
->p_ru
->ru_utime
,&p
->p_ru
->ru_utime
);
1822 timeradd(&st
,&p
->p_ru
->ru_stime
,&p
->p_ru
->ru_stime
);
1826 ruadd(p
->p_ru
, &p
->p_stats
->p_cru
);
1830 * Free up profiling buffers.
1833 struct uprof
*p0
= &p
->p_stats
->p_prof
, *p1
, *pn
;
1839 for (; p1
!= NULL
; p1
= pn
) {
1841 kfree(p1
, sizeof *p1
);
1846 * Other substructures are freed from wait().
1848 FREE_ZONE(p
->p_stats
, sizeof *p
->p_stats
, M_PSTATS
);
1851 FREE_ZONE(p
->p_sigacts
, sizeof *p
->p_sigacts
, M_SIGACTS
);
1852 p
->p_sigacts
= NULL
;
1854 proc_limitdrop(p
, 1);
1858 * Finish up by terminating the task
1859 * and halt this thread (only if a
1860 * member of the task exiting).
1862 p
->task
= TASK_NULL
;
1865 * Notify parent that we're gone.
1867 pp
= proc_parent(p
);
1868 if ((p
->p_listflag
& P_LIST_DEADPARENT
) == 0) {
1869 if (pp
!= initproc
) {
1871 pp
->si_pid
= p
->p_pid
;
1872 pp
->si_status
= p
->p_xstat
;
1873 pp
->si_code
= CLD_EXITED
;
1875 * p_ucred usage is safe as it is an exiting process
1876 * and reference is dropped in reap
1878 pp
->si_uid
= p
->p_ucred
->cr_ruid
;
1881 /* mark as a zombie */
1882 /* mark as a zombie */
1883 /* No need to take proc lock as all refs are drained and
1884 * no one except parent (reaping ) can look at this.
1885 * The write is to an int and is coherent. Also parent is
1886 * keyed off of list lock for reaping
1890 psignal(pp
, SIGCHLD
);
1892 /* and now wakeup the parent */
1894 wakeup((caddr_t
)pp
);
1898 /* check for lookups by zomb sysctl */
1899 while ((p
->p_listflag
& P_LIST_WAITING
) == P_LIST_WAITING
) {
1900 msleep(&p
->p_stat
, proc_list_mlock
, PWAIT
, "waitcoll", 0);
1903 p
->p_listflag
|= P_LIST_WAITING
;
1906 * This is a named reference and it is not granted
1907 * if the reap is already in progress. So we get
1908 * the reference here exclusively and their can be
1909 * no waiters. So there is no need for a wakeup
1910 * after we are done. AlsO the reap frees the structure
1911 * and the proc struct cannot be used for wakeups as well.
1912 * It is safe to use p here as this is system reap
1914 (void)reap_child_locked(pp
, p
, 0, 1, 1);
1915 /* list lock dropped by reap_child_locked */
1923 * LP64 support - long is 64 bits if we are dealing with a 64 bit user
1924 * process. We munge the kernel version of rusage into the
1927 __private_extern__
void
1928 munge_user64_rusage(struct rusage
*a_rusage_p
, struct user64_rusage
*a_user_rusage_p
)
1930 /* timeval changes size, so utime and stime need special handling */
1931 a_user_rusage_p
->ru_utime
.tv_sec
= a_rusage_p
->ru_utime
.tv_sec
;
1932 a_user_rusage_p
->ru_utime
.tv_usec
= a_rusage_p
->ru_utime
.tv_usec
;
1933 a_user_rusage_p
->ru_stime
.tv_sec
= a_rusage_p
->ru_stime
.tv_sec
;
1934 a_user_rusage_p
->ru_stime
.tv_usec
= a_rusage_p
->ru_stime
.tv_usec
;
1936 * everything else can be a direct assign, since there is no loss
1937 * of precision implied boing 32->64.
1939 a_user_rusage_p
->ru_maxrss
= a_rusage_p
->ru_maxrss
;
1940 a_user_rusage_p
->ru_ixrss
= a_rusage_p
->ru_ixrss
;
1941 a_user_rusage_p
->ru_idrss
= a_rusage_p
->ru_idrss
;
1942 a_user_rusage_p
->ru_isrss
= a_rusage_p
->ru_isrss
;
1943 a_user_rusage_p
->ru_minflt
= a_rusage_p
->ru_minflt
;
1944 a_user_rusage_p
->ru_majflt
= a_rusage_p
->ru_majflt
;
1945 a_user_rusage_p
->ru_nswap
= a_rusage_p
->ru_nswap
;
1946 a_user_rusage_p
->ru_inblock
= a_rusage_p
->ru_inblock
;
1947 a_user_rusage_p
->ru_oublock
= a_rusage_p
->ru_oublock
;
1948 a_user_rusage_p
->ru_msgsnd
= a_rusage_p
->ru_msgsnd
;
1949 a_user_rusage_p
->ru_msgrcv
= a_rusage_p
->ru_msgrcv
;
1950 a_user_rusage_p
->ru_nsignals
= a_rusage_p
->ru_nsignals
;
1951 a_user_rusage_p
->ru_nvcsw
= a_rusage_p
->ru_nvcsw
;
1952 a_user_rusage_p
->ru_nivcsw
= a_rusage_p
->ru_nivcsw
;
1955 /* For a 64-bit kernel and 32-bit userspace, munging may be needed */
1956 __private_extern__
void
1957 munge_user32_rusage(struct rusage
*a_rusage_p
, struct user32_rusage
*a_user_rusage_p
)
1959 /* timeval changes size, so utime and stime need special handling */
1960 a_user_rusage_p
->ru_utime
.tv_sec
= a_rusage_p
->ru_utime
.tv_sec
;
1961 a_user_rusage_p
->ru_utime
.tv_usec
= a_rusage_p
->ru_utime
.tv_usec
;
1962 a_user_rusage_p
->ru_stime
.tv_sec
= a_rusage_p
->ru_stime
.tv_sec
;
1963 a_user_rusage_p
->ru_stime
.tv_usec
= a_rusage_p
->ru_stime
.tv_usec
;
1965 * everything else can be a direct assign. We currently ignore
1966 * the loss of precision
1968 a_user_rusage_p
->ru_maxrss
= a_rusage_p
->ru_maxrss
;
1969 a_user_rusage_p
->ru_ixrss
= a_rusage_p
->ru_ixrss
;
1970 a_user_rusage_p
->ru_idrss
= a_rusage_p
->ru_idrss
;
1971 a_user_rusage_p
->ru_isrss
= a_rusage_p
->ru_isrss
;
1972 a_user_rusage_p
->ru_minflt
= a_rusage_p
->ru_minflt
;
1973 a_user_rusage_p
->ru_majflt
= a_rusage_p
->ru_majflt
;
1974 a_user_rusage_p
->ru_nswap
= a_rusage_p
->ru_nswap
;
1975 a_user_rusage_p
->ru_inblock
= a_rusage_p
->ru_inblock
;
1976 a_user_rusage_p
->ru_oublock
= a_rusage_p
->ru_oublock
;
1977 a_user_rusage_p
->ru_msgsnd
= a_rusage_p
->ru_msgsnd
;
1978 a_user_rusage_p
->ru_msgrcv
= a_rusage_p
->ru_msgrcv
;
1979 a_user_rusage_p
->ru_nsignals
= a_rusage_p
->ru_nsignals
;
1980 a_user_rusage_p
->ru_nvcsw
= a_rusage_p
->ru_nvcsw
;
1981 a_user_rusage_p
->ru_nivcsw
= a_rusage_p
->ru_nivcsw
;