2 * Copyright (c) 2000-2011, 2015 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* Copyright (c) 1995, 1997 Apple Computer, Inc. All Rights Reserved */
30 * Copyright (c) 1982, 1986, 1989, 1991, 1993
31 * The Regents of the University of California. All rights reserved.
32 * (c) UNIX System Laboratories, Inc.
33 * All or some portions of this file are derived from material licensed
34 * to the University of California by American Telephone and Telegraph
35 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
36 * the permission of UNIX System Laboratories, Inc.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the University of
49 * California, Berkeley and its contributors.
50 * 4. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * @(#)kern_exit.c 8.7 (Berkeley) 2/12/94
69 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
70 * support for mandatory and extensible security protections. This notice
71 * is included in support of clause 2.2 (b) of the Apple Public License,
75 #include <machine/reg.h>
76 #include <machine/psl.h>
78 #include "compat_43.h"
80 #include <sys/param.h>
81 #include <sys/systm.h>
82 #include <sys/ioctl.h>
83 #include <sys/proc_internal.h>
85 #include <sys/kauth.h>
88 #include <sys/resource.h>
89 #include <sys/kernel.h>
91 #include <sys/file_internal.h>
92 #include <sys/vnode_internal.h>
93 #include <sys/syslog.h>
94 #include <sys/malloc.h>
95 #include <sys/resourcevar.h>
96 #include <sys/ptrace.h>
97 #include <sys/proc_info.h>
98 #include <sys/_types/_timeval64.h>
100 #include <sys/aio_kern.h>
101 #include <sys/sysproto.h>
102 #include <sys/signalvar.h>
103 #include <sys/kdebug.h>
104 #include <sys/filedesc.h> /* fdfree */
106 #include <sys/shm_internal.h> /* shmexit */
108 #include <sys/acct.h> /* acct_process */
110 #include <security/audit/audit.h>
111 #include <bsm/audit_kevents.h>
113 #include <mach/mach_types.h>
115 #include <kern/kern_types.h>
116 #include <kern/kalloc.h>
117 #include <kern/task.h>
118 #include <corpses/task_corpse.h>
119 #include <kern/thread.h>
120 #include <kern/thread_call.h>
121 #include <kern/sched_prim.h>
122 #include <kern/assert.h>
123 #include <sys/codesign.h>
125 #if VM_PRESSURE_EVENTS
126 #include <kern/vm_pressure.h>
129 #if CONFIG_MEMORYSTATUS
130 #include <sys/kern_memorystatus.h>
134 /* Do not include dtrace.h, it redefines kmem_[alloc/free] */
135 extern void (*dtrace_fasttrap_exit_ptr
)(proc_t
);
136 extern void (*dtrace_helpers_cleanup
)(proc_t
);
137 extern void dtrace_lazy_dofs_destroy(proc_t
);
139 #include <sys/dtrace_ptss.h>
143 #include <security/mac.h>
144 #include <sys/syscall.h>
147 #include <mach/mach_types.h>
148 #include <mach/task.h>
149 #include <mach/thread_act.h>
151 #include <vm/vm_protos.h>
155 extern boolean_t init_task_died
;
156 void proc_prepareexit(proc_t p
, int rv
, boolean_t perf_notify
);
157 void vfork_exit(proc_t p
, int rv
);
158 void vproc_exit(proc_t p
);
159 __private_extern__
void munge_user64_rusage(struct rusage
*a_rusage_p
, struct user64_rusage
*a_user_rusage_p
);
160 __private_extern__
void munge_user32_rusage(struct rusage
*a_rusage_p
, struct user32_rusage
*a_user_rusage_p
);
161 static int reap_child_locked(proc_t parent
, proc_t child
, int deadparent
, int reparentedtoinit
, int locked
, int droplock
);
162 static void populate_corpse_crashinfo(proc_t p
, void *crash_info_ptr
, struct rusage_superset
*rup
, mach_exception_data_type_t code
, mach_exception_data_type_t subcode
);
163 extern int proc_pidpathinfo(proc_t p
, uint64_t arg
, user_addr_t buffer
, uint32_t buffersize
, int32_t *retval
);
165 static __attribute__((noinline
)) void launchd_crashed_panic(proc_t p
, int rv
);
166 extern void proc_piduniqidentifierinfo(proc_t p
, struct proc_uniqidentifierinfo
*p_uniqidinfo
);
170 * Things which should have prototypes in headers, but don't
172 void proc_exit(proc_t p
);
173 int wait1continue(int result
);
174 int waitidcontinue(int result
);
175 kern_return_t
sys_perf_notify(thread_t thread
, int pid
);
176 kern_return_t
task_exception_notify(exception_type_t exception
,
177 mach_exception_data_type_t code
, mach_exception_data_type_t subcode
);
179 void gather_rusage_info(proc_t p
, rusage_info_current
*ru
, int flavor
);
182 * NOTE: Source and target may *NOT* overlap!
183 * XXX Should share code with bsd/dev/ppc/unix_signal.c
186 siginfo_user_to_user32(user_siginfo_t
*in
, user32_siginfo_t
*out
)
188 out
->si_signo
= in
->si_signo
;
189 out
->si_errno
= in
->si_errno
;
190 out
->si_code
= in
->si_code
;
191 out
->si_pid
= in
->si_pid
;
192 out
->si_uid
= in
->si_uid
;
193 out
->si_status
= in
->si_status
;
194 out
->si_addr
= CAST_DOWN_EXPLICIT(user32_addr_t
,in
->si_addr
);
195 /* following cast works for sival_int because of padding */
196 out
->si_value
.sival_ptr
= CAST_DOWN_EXPLICIT(user32_addr_t
,in
->si_value
.sival_ptr
);
197 out
->si_band
= in
->si_band
; /* range reduction */
201 siginfo_user_to_user64(user_siginfo_t
*in
, user64_siginfo_t
*out
)
203 out
->si_signo
= in
->si_signo
;
204 out
->si_errno
= in
->si_errno
;
205 out
->si_code
= in
->si_code
;
206 out
->si_pid
= in
->si_pid
;
207 out
->si_uid
= in
->si_uid
;
208 out
->si_status
= in
->si_status
;
209 out
->si_addr
= in
->si_addr
;
210 /* following cast works for sival_int because of padding */
211 out
->si_value
.sival_ptr
= in
->si_value
.sival_ptr
;
212 out
->si_band
= in
->si_band
; /* range reduction */
216 copyoutsiginfo(user_siginfo_t
*native
, boolean_t is64
, user_addr_t uaddr
)
219 user64_siginfo_t sinfo64
;
221 bzero(&sinfo64
, sizeof (sinfo64
));
222 siginfo_user_to_user64(native
, &sinfo64
);
223 return (copyout(&sinfo64
, uaddr
, sizeof (sinfo64
)));
225 user32_siginfo_t sinfo32
;
227 bzero(&sinfo32
, sizeof (sinfo32
));
228 siginfo_user_to_user32(native
, &sinfo32
);
229 return (copyout(&sinfo32
, uaddr
, sizeof (sinfo32
)));
233 static void populate_corpse_crashinfo(proc_t p
, void *crash_info_ptr
, struct rusage_superset
*rup
, mach_exception_data_type_t code
, mach_exception_data_type_t subcode
)
235 mach_vm_address_t uaddr
= 0;
236 mach_exception_data_type_t exc_codes
[EXCEPTION_CODE_MAX
];
238 exc_codes
[1] = subcode
;
240 struct proc_uniqidentifierinfo p_uniqidinfo
;
241 struct proc_workqueueinfo pwqinfo
;
243 uint64_t crashed_threadid
= thread_tid(current_thread());
244 unsigned int pflags
= 0;
246 #if CONFIG_MEMORYSTATUS
247 int memstat_dirty_flags
= 0;
250 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_EXCEPTION_CODES
, sizeof(exc_codes
), &uaddr
)) {
251 copyout(exc_codes
, uaddr
, sizeof(exc_codes
));
254 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_PID
, sizeof(p
->p_pid
), &uaddr
)) {
255 copyout(&p
->p_pid
, uaddr
, sizeof(p
->p_pid
));
258 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_PPID
, sizeof(p
->p_ppid
), &uaddr
)) {
259 copyout(&p
->p_ppid
, uaddr
, sizeof(p
->p_ppid
));
262 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_CRASHED_THREADID
, sizeof(uint64_t), &uaddr
)) {
263 copyout(&crashed_threadid
, uaddr
, sizeof(uint64_t));
266 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_RUSAGE
, sizeof(struct rusage
), &uaddr
)) {
267 copyout(&rup
->ru
, uaddr
, sizeof(struct rusage
));
271 kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_BSDINFOWITHUNIQID
, sizeof(struct proc_uniqidentifierinfo
), &uaddr
)) {
272 proc_piduniqidentifierinfo(p
, &p_uniqidinfo
);
273 copyout(&p_uniqidinfo
, uaddr
, sizeof(struct proc_uniqidentifierinfo
));
276 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_RUSAGE_INFO
, sizeof(rusage_info_current
), &uaddr
)) {
277 copyout(&rup
->ri
, uaddr
, sizeof(rusage_info_current
));
280 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_PROC_CSFLAGS
, sizeof(p
->p_csflags
), &uaddr
)) {
281 copyout(&p
->p_csflags
, uaddr
, sizeof(p
->p_csflags
));
284 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_PROC_NAME
, sizeof(p
->p_comm
), &uaddr
)) {
285 copyout(&p
->p_comm
, uaddr
, sizeof(p
->p_comm
));
288 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_PROC_STARTTIME
, sizeof(p
->p_start
), &uaddr
)) {
289 struct timeval64 t64
;
290 t64
.tv_sec
= (int64_t)p
->p_start
.tv_sec
;
291 t64
.tv_usec
= (int64_t)p
->p_start
.tv_usec
;
292 copyout(&t64
, uaddr
, sizeof(t64
));
295 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_USERSTACK
, sizeof(p
->user_stack
), &uaddr
)) {
296 copyout(&p
->user_stack
, uaddr
, sizeof(p
->user_stack
));
299 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_ARGSLEN
, sizeof(p
->p_argslen
), &uaddr
)) {
300 copyout(&p
->p_argslen
, uaddr
, sizeof(p
->p_argslen
));
303 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_PROC_ARGC
, sizeof(p
->p_argc
), &uaddr
)) {
304 copyout(&p
->p_argc
, uaddr
, sizeof(p
->p_argc
));
307 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_PROC_PATH
, MAXPATHLEN
, &uaddr
)) {
308 proc_pidpathinfo(p
, 0, uaddr
, MAXPATHLEN
, &retval
);
311 pflags
= p
->p_flag
& (P_LP64
| P_SUGID
);
312 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_PROC_FLAGS
, sizeof(pflags
), &uaddr
)) {
313 copyout(&pflags
, uaddr
, sizeof(pflags
));
316 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_UID
, sizeof(p
->p_uid
), &uaddr
)) {
317 copyout(&p
->p_uid
, uaddr
, sizeof(p
->p_uid
));
320 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_GID
, sizeof(p
->p_gid
), &uaddr
)) {
321 copyout(&p
->p_gid
, uaddr
, sizeof(p
->p_gid
));
324 cputype
= cpu_type() & ~CPU_ARCH_MASK
;
325 if (IS_64BIT_PROCESS(p
))
326 cputype
|= CPU_ARCH_ABI64
;
328 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_CPUTYPE
, sizeof(cpu_type_t
), &uaddr
)) {
329 copyout(&cputype
, uaddr
, sizeof(cpu_type_t
));
332 bzero(&pwqinfo
, sizeof(struct proc_workqueueinfo
));
333 retval
= fill_procworkqueue(p
, &pwqinfo
);
335 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_WORKQUEUEINFO
, sizeof(struct proc_workqueueinfo
), &uaddr
)) {
336 copyout(&pwqinfo
, uaddr
, sizeof(struct proc_workqueueinfo
));
340 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_RESPONSIBLE_PID
, sizeof(p
->p_responsible_pid
), &uaddr
)) {
341 copyout(&p
->p_responsible_pid
, uaddr
, sizeof(p
->p_responsible_pid
));
344 #if CONFIG_MEMORYSTATUS
345 memstat_dirty_flags
= memorystatus_dirty_get(p
);
346 if (KERN_SUCCESS
== kcdata_get_memory_addr(crash_info_ptr
, TASK_CRASHINFO_DIRTY_FLAGS
, sizeof(memstat_dirty_flags
), &uaddr
)) {
347 copyout(&memstat_dirty_flags
, uaddr
, sizeof(memstat_dirty_flags
));
353 static __attribute__((noinline
)) void
354 launchd_crashed_panic(proc_t p
, int rv
)
356 printf("pid 1 exited (signal %d, exit %d)\n",
357 WTERMSIG(rv
), WEXITSTATUS(rv
));
359 #if (DEVELOPMENT || DEBUG)
361 * For debugging purposes, generate a core file of initproc before
362 * panicking. Leave at least 300 MB free on the root volume, and ignore
363 * the process's corefile ulimit. fsync() the file to ensure it lands on disk
364 * before the panic hits.
368 uint64_t coredump_start
= mach_absolute_time();
369 uint64_t coredump_end
;
371 clock_usec_t tv_usec
;
374 err
= coredump(p
, 300, COREDUMP_IGNORE_ULIMIT
| COREDUMP_FULLFSYNC
);
376 coredump_end
= mach_absolute_time();
378 absolutetime_to_microtime(coredump_end
- coredump_start
, &tv_sec
, &tv_usec
);
380 tv_msec
= tv_usec
/ 1000;
383 printf("Failed to generate initproc core file: error %d, took %d.%03d seconds\n",
384 err
, (uint32_t)tv_sec
, tv_msec
);
386 printf("Generated initproc core file in %d.%03d seconds\n",
387 (uint32_t)tv_sec
, tv_msec
);
391 sync(p
, (void *)NULL
, (int *)NULL
);
393 panic_plain("%s exited (signal %d, exit status %d %s)", (p
->p_name
[0] != '\0' ? p
->p_name
: "initproc"), WTERMSIG(rv
),
394 WEXITSTATUS(rv
), ((p
->p_csflags
& CS_KILLED
) ? "CS_KILLED" : ""));
402 exit(proc_t p
, struct exit_args
*uap
, int *retval
)
404 exit1(p
, W_EXITCODE(uap
->rval
, 0), retval
);
406 thread_exception_return();
409 thread_block(THREAD_CONTINUE_NULL
);
414 * Exit: deallocate address space and other resources, change proc state
415 * to zombie, and unlink proc from allproc and parent's lists. Save exit
416 * status and rusage for wait(). Check for child processes and orphan them.
419 exit1(proc_t p
, int rv
, int *retval
)
421 return exit1_internal(p
, rv
, retval
, TRUE
, TRUE
, 0);
425 exit1_internal(proc_t p
, int rv
, int *retval
, boolean_t thread_can_terminate
, boolean_t perf_notify
,
428 thread_t self
= current_thread();
429 struct task
*task
= p
->task
;
434 * If a thread in this task has already
435 * called exit(), then halt any others
439 ut
= get_bsdthread_info(self
);
440 if (ut
->uu_flag
& UT_VFORK
) {
441 if (!thread_can_terminate
) {
446 vfork_return(p
, retval
, p
->p_pid
);
447 unix_syscall_return(0);
452 * The parameter list of audit_syscall_exit() was augmented to
453 * take the Darwin syscall number as the first parameter,
454 * which is currently required by mac_audit_postselect().
458 * The BSM token contains two components: an exit status as passed
459 * to exit(), and a return value to indicate what sort of exit it
460 * was. The exit status is WEXITSTATUS(rv), but it's not clear
461 * what the return value is.
463 AUDIT_ARG(exit
, WEXITSTATUS(rv
), 0);
464 AUDIT_SYSCALL_EXIT(SYS_exit
, p
, ut
, 0); /* Exit is always successfull */
466 DTRACE_PROC1(exit
, int, CLD_EXITED
);
468 /* mark process is going to exit and pull out of DBG/disk throttle */
469 /* TODO: This should be done after becoming exit thread */
470 proc_set_task_policy(p
->task
, THREAD_NULL
, TASK_POLICY_ATTRIBUTE
,
471 TASK_POLICY_TERMINATED
, TASK_POLICY_ENABLE
);
474 error
= proc_transstart(p
, 1, ((jetsam_flags
& P_JETSAM_VNODE
) ? 1 : 0));
475 if (error
== EDEADLK
) {
476 /* Temp: If deadlock error, then it implies multithreaded exec is
477 * in progress. Instread of letting exit continue and
478 * corrupting the freed memory, let the exit thread
479 * return. This will save corruption in remote case.
482 if (current_proc() == p
){
483 if (p
->exit_thread
== self
)
484 printf("exit_thread failed to exit, leaving process %s[%d] in unkillable limbo\n",
485 p
->p_comm
, p
->p_pid
);
486 thread_exception_return();
488 /* external termination like jetsam */
493 while (p
->exit_thread
!= self
) {
494 if (sig_try_locked(p
) <= 0) {
496 if (get_threadtask(self
) != task
) {
502 thread_terminate(self
);
503 if (!thread_can_terminate
) {
507 thread_exception_return();
513 if (p
== initproc
&& current_proc() == p
) {
514 init_task_died
= TRUE
;
517 p
->p_lflag
|= P_LEXIT
;
519 p
->p_lflag
|= jetsam_flags
;
524 proc_prepareexit(p
, rv
, perf_notify
);
526 /* Last thread to terminate will call proc_exit() */
527 task_terminate_internal(task
);
533 proc_prepareexit(proc_t p
, int rv
, boolean_t perf_notify
)
535 mach_exception_data_type_t code
= 0, subcode
= 0;
538 thread_t self
= current_thread();
539 ut
= get_bsdthread_info(self
);
540 struct rusage_superset
*rup
;
542 int create_corpse
= FALSE
;
545 launchd_crashed_panic(p
, rv
);
549 /* If a core should be generated, notify crash reporter */
550 if (hassigprop(WTERMSIG(rv
), SA_CORE
) || ((p
->p_csflags
& CS_KILLED
) != 0)) {
552 * Workaround for processes checking up on PT_DENY_ATTACH:
553 * should be backed out post-Leopard (details in 5431025).
555 if ((SIGSEGV
== WTERMSIG(rv
)) &&
556 (p
->p_pptr
->p_lflag
& P_LNOATTACH
)) {
561 * Crash Reporter looks for the signal value, original exception
562 * type, and low 20 bits of the original code in code[0]
563 * (8, 4, and 20 bits respectively). code[1] is unmodified.
565 code
= ((WTERMSIG(rv
) & 0xff) << 24) |
566 ((ut
->uu_exception
& 0x0f) << 20) |
567 ((int)ut
->uu_code
& 0xfffff);
568 subcode
= ut
->uu_subcode
;
570 kr
= task_exception_notify(EXC_CRASH
, code
, subcode
);
572 /* Nobody handled EXC_CRASH?? remember to make corpse */
574 create_corpse
= TRUE
;
579 /* Notify the perf server? */
581 (void)sys_perf_notify(self
, p
->p_pid
);
585 /* stash the usage into corpse data if making_corpse == true */
586 if (create_corpse
== TRUE
) {
587 kr
= task_mark_corpse(current_task());
588 if (kr
!= KERN_SUCCESS
) {
589 if (kr
== KERN_NO_SPACE
) {
590 printf("Process[%d] has no vm space for corpse info.\n", p
->p_pid
);
591 } else if (kr
== KERN_NOT_SUPPORTED
) {
592 printf("Process[%d] was destined to be corpse. But corpse is disabled by config.\n", p
->p_pid
);
594 printf("Process[%d] crashed: %s. Too many corpses being created.\n", p
->p_pid
, p
->p_comm
);
596 create_corpse
= FALSE
;
598 /* XXX: <rdar://problem/20491659> Need to sync ATM buffer before crash */
599 kr
= task_send_trace_memory(current_task(), p
->p_pid
, p
->p_uniqueid
);
604 * Before this process becomes a zombie, stash resource usage
605 * stats in the proc for external observers to query
606 * via proc_pid_rusage().
608 * If the zombie allocation fails, just punt the stats.
610 MALLOC_ZONE(rup
, struct rusage_superset
*,
611 sizeof (*rup
), M_ZOMBIE
, M_WAITOK
);
613 gather_rusage_info(p
, &rup
->ri
, RUSAGE_INFO_CURRENT
);
614 rup
->ri
.ri_phys_footprint
= 0;
615 rup
->ri
.ri_proc_exit_abstime
= mach_absolute_time();
618 * Make the rusage_info visible to external observers
619 * only after it has been completely filled in.
624 populate_corpse_crashinfo(p
, task_get_corpseinfo(current_task()), rup
, code
, subcode
);
627 * Remove proc from allproc queue and from pidhash chain.
628 * Need to do this before we do anything that can block.
629 * Not doing causes things like mount() find this on allproc
630 * in partially cleaned state.
635 #if CONFIG_MEMORYSTATUS
636 memorystatus_remove(p
, TRUE
);
639 LIST_REMOVE(p
, p_list
);
640 LIST_INSERT_HEAD(&zombproc
, p
, p_list
); /* Place onto zombproc. */
641 /* will not be visible via proc_find */
642 p
->p_listflag
|= P_LIST_EXITED
;
651 * If parent is waiting for us to exit or exec,
652 * P_LPPWAIT is set; we will wakeup the parent below.
655 p
->p_lflag
&= ~(P_LTRACED
| P_LPPWAIT
);
656 p
->p_sigignore
= ~(sigcantmask
);
666 struct task
*task
= p
->task
;
667 vnode_t tvp
= NULLVP
;
669 struct session
*sessp
;
670 struct uthread
* uth
;
675 uth
= current_uthread();
678 proc_transstart(p
, 1, 0);
679 if( !(p
->p_lflag
& P_LEXIT
)) {
681 * This can happen if a thread_terminate() occurs
682 * in a single-threaded process.
684 p
->p_lflag
|= P_LEXIT
;
687 proc_prepareexit(p
, 0, TRUE
);
688 (void) task_terminate_internal(task
);
694 p
->p_lflag
|= P_LPEXIT
;
697 * Other kernel threads may be in the middle of signalling this process.
698 * Wait for those threads to wrap it up before making the process
701 if ((p
->p_lflag
& P_LINSIGNAL
) || (p
->p_sigwaitcnt
> 0)) {
703 while ((p
->p_lflag
& P_LINSIGNAL
) || (p
->p_sigwaitcnt
> 1))
704 msleep(&p
->p_sigmask
, &p
->p_mlock
, PWAIT
, "proc_sigdrain", NULL
);
710 exitval
= p
->p_xstat
;
711 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_COMMON
,
712 BSDDBG_CODE(DBG_BSD_PROC
, BSD_PROC_EXIT
) | DBG_FUNC_START
,
713 pid
, exitval
, 0, 0, 0);
717 * Free any outstanding lazy dof entries. It is imperative we
718 * always call dtrace_lazy_dofs_destroy, rather than null check
719 * and call if !NULL. If we NULL test, during lazy dof faulting
720 * we can race with the faulting code and proceed from here to
721 * beyond the helpers cleanup. The lazy dof faulting will then
722 * install new helpers which will never be cleaned up, and leak.
724 dtrace_lazy_dofs_destroy(p
);
727 * Clean up any DTrace helper actions or probes for the process.
729 if (p
->p_dtrace_helpers
!= NULL
) {
730 (*dtrace_helpers_cleanup
)(p
);
734 * Clean up any DTrace probes associated with this process.
737 * APPLE NOTE: We release ptss pages/entries in dtrace_fasttrap_exit_ptr(),
738 * call this after dtrace_helpers_cleanup()
741 if (p
->p_dtrace_probes
&& dtrace_fasttrap_exit_ptr
) {
742 (*dtrace_fasttrap_exit_ptr
)(p
);
749 #if VM_PRESSURE_EVENTS
750 vm_pressure_proc_cleanup(p
);
754 * need to cancel async IO requests that can be cancelled and wait for those
755 * already active. MAY BLOCK!
760 /* if any pending cpu limits action, clear it */
761 task_clear_cpuusage(p
->task
, TRUE
);
763 workqueue_mark_exiting(p
);
765 kqueue_dealloc(p
->p_wqkqueue
);
766 p
->p_wqkqueue
= NULL
;
771 * Close open files and release open-file table.
776 if (uth
->uu_lowpri_window
) {
778 * task is marked as a low priority I/O type
779 * and the I/O we issued while in flushing files on close
780 * collided with normal I/O operations...
781 * no need to throttle this thread since its going away
782 * but we do need to update our bookeeping w/r to throttled threads
784 throttle_lowpri_io(0);
788 /* Close ref SYSV Shared memory*/
793 /* Release SYSV semaphores */
798 pth_proc_hashdelete(p
);
801 sessp
= proc_session(p
);
802 if (SESS_LEADER(p
, sessp
)) {
804 if (sessp
->s_ttyvp
!= NULLVP
) {
808 struct vfs_context context
;
812 * Controlling process.
813 * Signal foreground pgrp,
814 * drain controlling terminal
815 * and revoke access to controlling terminal.
818 tp
= SESSION_TP(sessp
);
819 if ((tp
!= TTY_NULL
) && (tp
->t_session
== sessp
)) {
820 session_unlock(sessp
);
823 * We're going to SIGHUP the foreground process
824 * group. It can't change from this point on
825 * until the revoke is complete.
826 * The process group changes under both the tty
827 * lock and proc_list_lock but we need only one
833 tty_pgsignal(tp
, SIGHUP
, 1);
836 tp
= SESSION_TP(sessp
);
838 cttyflag
= sessp
->s_flags
& S_CTTYREF
;
839 sessp
->s_flags
&= ~S_CTTYREF
;
840 ttyvp
= sessp
->s_ttyvp
;
841 ttyvid
= sessp
->s_ttyvid
;
842 sessp
->s_ttyvp
= NULLVP
;
844 sessp
->s_ttyp
= TTY_NULL
;
845 sessp
->s_ttypgrpid
= NO_PID
;
846 session_unlock(sessp
);
848 if ((ttyvp
!= NULLVP
) && (vnode_getwithvid(ttyvp
, ttyvid
) == 0)) {
849 if (tp
!= TTY_NULL
) {
854 context
.vc_thread
= proc_thread(p
); /* XXX */
855 context
.vc_ucred
= kauth_cred_proc_ref(p
);
856 VNOP_REVOKE(ttyvp
, REVOKEALL
, &context
);
859 * Release the extra usecount taken in cttyopen.
860 * usecount should be released after VNOP_REVOKE is called.
861 * This usecount was taken to ensure that
862 * the VNOP_REVOKE results in a close to
863 * the tty since cttyclose is a no-op.
868 kauth_cred_unref(&context
.vc_ucred
);
873 * This is cleared even if not set. This is also done in
874 * spec_close to ensure that the flag is cleared.
884 sessp
->s_leader
= NULL
;
885 session_unlock(sessp
);
893 p
->p_rlimit
[RLIMIT_FSIZE
].rlim_cur
= RLIM_INFINITY
;
894 (void)acct_process(p
);
898 if ((p
->p_listflag
& P_LIST_EXITCOUNT
) == P_LIST_EXITCOUNT
) {
899 p
->p_listflag
&= ~P_LIST_EXITCOUNT
;
900 proc_shutdown_exitcount
--;
901 if (proc_shutdown_exitcount
== 0)
902 wakeup(&proc_shutdown_exitcount
);
905 /* wait till parentrefs are dropped and grant no more */
906 proc_childdrainstart(p
);
907 while ((q
= p
->p_children
.lh_first
) != NULL
) {
908 int reparentedtoinit
= (q
->p_listflag
& P_LIST_DEADPARENT
) ? 1 : 0;
909 if (q
->p_stat
== SZOMB
) {
911 panic("parent child linkage broken");
912 /* check for sysctl zomb lookup */
913 while ((q
->p_listflag
& P_LIST_WAITING
) == P_LIST_WAITING
) {
914 msleep(&q
->p_stat
, proc_list_mlock
, PWAIT
, "waitcoll", 0);
916 q
->p_listflag
|= P_LIST_WAITING
;
918 * This is a named reference and it is not granted
919 * if the reap is already in progress. So we get
920 * the reference here exclusively and their can be
921 * no waiters. So there is no need for a wakeup
922 * after we are done. Also the reap frees the structure
923 * and the proc struct cannot be used for wakeups as well.
924 * It is safe to use q here as this is system reap
926 (void)reap_child_locked(p
, q
, 1, reparentedtoinit
, 1, 0);
929 * Traced processes are killed
930 * since their existence means someone is messing up.
932 if (q
->p_lflag
& P_LTRACED
) {
936 * Take a reference on the child process to
937 * ensure it doesn't exit and disappear between
938 * the time we drop the list_lock and attempt
939 * to acquire its proc_lock.
941 if (proc_ref_locked(q
) != q
)
946 opp
= proc_find(q
->p_oppid
);
947 if (opp
!= PROC_NULL
) {
951 proc_reparentlocked(q
, opp
, 0, 0);
954 /* original parent exited while traced */
956 q
->p_listflag
|= P_LIST_DEADPARENT
;
959 proc_reparentlocked(q
, initproc
, 0, 0);
963 q
->p_lflag
&= ~P_LTRACED
;
965 if (q
->sigwait_thread
) {
966 thread_t thread
= q
->sigwait_thread
;
970 * The sigwait_thread could be stopped at a
971 * breakpoint. Wake it up to kill.
972 * Need to do this as it could be a thread which is not
973 * the first thread in the task. So any attempts to kill
974 * the process would result into a deadlock on q->sigwait.
976 thread_resume(thread
);
977 clear_wait(thread
, THREAD_INTERRUPTED
);
978 threadsignal(thread
, SIGKILL
, 0);
987 q
->p_listflag
|= P_LIST_DEADPARENT
;
988 proc_reparentlocked(q
, initproc
, 0, 1);
993 proc_childdrainend(p
);
997 * Release reference to text vnode
1001 if (tvp
!= NULLVP
) {
1006 * Save exit status and final rusage info, adding in child rusage
1007 * info and self times. If we were unable to allocate a zombie
1008 * structure, this information is lost.
1010 if (p
->p_ru
!= NULL
) {
1011 calcru(p
, &p
->p_stats
->p_ru
.ru_utime
, &p
->p_stats
->p_ru
.ru_stime
, NULL
);
1012 p
->p_ru
->ru
= p
->p_stats
->p_ru
;
1014 ruadd(&(p
->p_ru
->ru
), &p
->p_stats
->p_cru
);
1018 * Free up profiling buffers.
1021 struct uprof
*p0
= &p
->p_stats
->p_prof
, *p1
, *pn
;
1027 for (; p1
!= NULL
; p1
= pn
) {
1029 kfree(p1
, sizeof *p1
);
1034 if (thread_call_cancel(p
->p_rcall
))
1037 while (p
->p_ractive
> 0) {
1046 thread_call_free(p
->p_rcall
);
1050 * Other substructures are freed from wait().
1052 FREE_ZONE(p
->p_stats
, sizeof *p
->p_stats
, M_PSTATS
);
1055 FREE_ZONE(p
->p_sigacts
, sizeof *p
->p_sigacts
, M_SIGACTS
);
1056 p
->p_sigacts
= NULL
;
1058 proc_limitdrop(p
, 1);
1061 vm_purgeable_disown(p
->task
);
1064 * Finish up by terminating the task
1065 * and halt this thread (only if a
1066 * member of the task exiting).
1068 p
->task
= TASK_NULL
;
1069 set_bsdtask_info(task
, NULL
);
1071 knote_hint
= NOTE_EXIT
| (p
->p_xstat
& 0xffff);
1072 proc_knote(p
, knote_hint
);
1074 /* mark the thread as the one that is doing proc_exit
1075 * no need to hold proc lock in uthread_free
1077 uth
->uu_flag
|= UT_PROCEXIT
;
1079 * Notify parent that we're gone.
1081 pp
= proc_parent(p
);
1082 if (pp
->p_flag
& P_NOCLDWAIT
) {
1084 if (p
->p_ru
!= NULL
) {
1088 * If the parent is ignoring SIGCHLD, then POSIX requires
1089 * us to not add the resource usage to the parent process -
1090 * we are only going to hand it off to init to get reaped.
1091 * We should contest the standard in this case on the basis
1094 #else /* !3839178 */
1096 * Add child resource usage to parent before giving
1097 * zombie to init. If we were unable to allocate a
1098 * zombie structure, this information is lost.
1100 ruadd(&pp
->p_stats
->p_cru
, &p
->p_ru
->ru
);
1101 #endif /* !3839178 */
1102 update_rusage_info_child(&pp
->p_stats
->ri_child
, &p
->p_ru
->ri
);
1106 /* kernel can reap this one, no need to move it to launchd */
1108 p
->p_listflag
|= P_LIST_DEADPARENT
;
1111 if ((p
->p_listflag
& P_LIST_DEADPARENT
) == 0 || p
->p_oppid
) {
1112 if (pp
!= initproc
) {
1114 pp
->si_pid
= p
->p_pid
;
1115 pp
->si_status
= p
->p_xstat
;
1116 pp
->si_code
= CLD_EXITED
;
1118 * p_ucred usage is safe as it is an exiting process
1119 * and reference is dropped in reap
1121 pp
->si_uid
= kauth_cred_getruid(p
->p_ucred
);
1124 /* mark as a zombie */
1125 /* No need to take proc lock as all refs are drained and
1126 * no one except parent (reaping ) can look at this.
1127 * The write is to an int and is coherent. Also parent is
1128 * keyed off of list lock for reaping
1130 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_COMMON
,
1131 BSDDBG_CODE(DBG_BSD_PROC
, BSD_PROC_EXIT
) | DBG_FUNC_END
,
1132 pid
, exitval
, 0, 0, 0);
1135 * The current process can be reaped so, no one
1136 * can depend on this
1139 psignal(pp
, SIGCHLD
);
1141 /* and now wakeup the parent */
1143 wakeup((caddr_t
)pp
);
1146 /* should be fine as parent proc would be initproc */
1147 /* mark as a zombie */
1148 /* No need to take proc lock as all refs are drained and
1149 * no one except parent (reaping ) can look at this.
1150 * The write is to an int and is coherent. Also parent is
1151 * keyed off of list lock for reaping
1154 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_COMMON
,
1155 BSDDBG_CODE(DBG_BSD_PROC
, BSD_PROC_EXIT
) | DBG_FUNC_END
,
1156 pid
, exitval
, 0, 0, 0);
1157 /* check for sysctl zomb lookup */
1158 while ((p
->p_listflag
& P_LIST_WAITING
) == P_LIST_WAITING
) {
1159 msleep(&p
->p_stat
, proc_list_mlock
, PWAIT
, "waitcoll", 0);
1161 /* safe to use p as this is a system reap */
1163 p
->p_listflag
|= P_LIST_WAITING
;
1166 * This is a named reference and it is not granted
1167 * if the reap is already in progress. So we get
1168 * the reference here exclusively and their can be
1169 * no waiters. So there is no need for a wakeup
1170 * after we are done. AlsO the reap frees the structure
1171 * and the proc struct cannot be used for wakeups as well.
1172 * It is safe to use p here as this is system reap
1174 (void)reap_child_locked(pp
, p
, 1, 0, 1, 1);
1175 /* list lock dropped by reap_child_locked */
1177 if (uth
->uu_lowpri_window
) {
1179 * task is marked as a low priority I/O type and we've
1180 * somehow picked up another throttle during exit processing...
1181 * no need to throttle this thread since its going away
1182 * but we do need to update our bookeeping w/r to throttled threads
1184 throttle_lowpri_io(0);
1195 * Description: Given a process from which all status information needed
1196 * has already been extracted, if the process is a ptrace
1197 * attach process, detach it and give it back to its real
1198 * parent, else recover all resources remaining associated
1201 * Parameters: proc_t parent Parent of process being reaped
1202 * proc_t child Process to reap
1204 * Returns: 0 Process was not reaped because it
1205 * came from an attach
1206 * 1 Process was reaped
1209 reap_child_locked(proc_t parent
, proc_t child
, int deadparent
, int reparentedtoinit
, int locked
, int droplock
)
1211 proc_t trace_parent
= PROC_NULL
; /* Traced parent process, if tracing */
1217 * If we got the child via a ptrace 'attach',
1218 * we need to give it back to the old parent.
1220 * Exception: someone who has been reparented to launchd before being
1221 * ptraced can simply be reaped, refer to radar 5677288
1222 * p_oppid -> ptraced
1223 * trace_parent == initproc -> away from launchd
1224 * reparentedtoinit -> came to launchd by reparenting
1226 if (child
->p_oppid
) {
1231 oppid
= child
->p_oppid
;
1233 knote_hint
= NOTE_EXIT
| (child
->p_xstat
& 0xffff);
1236 if ((trace_parent
= proc_find(oppid
))
1237 && !((trace_parent
== initproc
) && reparentedtoinit
)) {
1239 if (trace_parent
!= initproc
) {
1241 * proc internal fileds and p_ucred usage safe
1242 * here as child is dead and is not reaped or
1245 proc_lock(trace_parent
);
1246 trace_parent
->si_pid
= child
->p_pid
;
1247 trace_parent
->si_status
= child
->p_xstat
;
1248 trace_parent
->si_code
= CLD_CONTINUED
;
1249 trace_parent
->si_uid
= kauth_cred_getruid(child
->p_ucred
);
1250 proc_unlock(trace_parent
);
1252 proc_reparentlocked(child
, trace_parent
, 1, 0);
1254 /* resend knote to original parent (and others) after reparenting */
1255 proc_knote(child
, knote_hint
);
1257 psignal(trace_parent
, SIGCHLD
);
1259 wakeup((caddr_t
)trace_parent
);
1260 child
->p_listflag
&= ~P_LIST_WAITING
;
1261 wakeup(&child
->p_stat
);
1263 proc_rele(trace_parent
);
1264 if ((locked
== 1) && (droplock
== 0))
1270 * If we can't reparent (e.g. the original parent exited while child was being debugged, or
1271 * original parent is the same as the debugger currently exiting), we still need to satisfy
1272 * the knote lifecycle for other observers on the system. While the debugger was attached,
1273 * the NOTE_EXIT would not have been broadcast during initial child termination.
1275 proc_knote(child
, knote_hint
);
1277 if (trace_parent
!= PROC_NULL
) {
1278 proc_rele(trace_parent
);
1282 #pragma clang diagnostic push
1283 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
1284 proc_knote(child
, NOTE_REAP
);
1285 #pragma clang diagnostic pop
1287 proc_knote_drain(child
);
1294 * If the parent is ignoring SIGCHLD, then POSIX requires
1295 * us to not add the resource usage to the parent process -
1296 * we are only going to hand it off to init to get reaped.
1297 * We should contest the standard in this case on the basis
1300 if (!(parent
->p_flag
& P_NOCLDWAIT
))
1301 #endif /* 3839178 */
1302 ruadd(&parent
->p_stats
->p_cru
, &child
->p_ru
->ru
);
1303 update_rusage_info_child(&parent
->p_stats
->ri_child
, &child
->p_ru
->ri
);
1304 proc_unlock(parent
);
1305 FREE_ZONE(child
->p_ru
, sizeof *child
->p_ru
, M_ZOMBIE
);
1308 printf("Warning : lost p_ru for %s\n", child
->p_comm
);
1311 AUDIT_SESSION_PROCEXIT(child
);
1314 * Decrement the count of procs running with this uid.
1315 * p_ucred usage is safe here as it is an exited process.
1316 * and refernce is dropped after these calls down below
1317 * (locking protection is provided by list lock held in chgproccnt)
1320 (void)chgproccnt(kauth_cred_getruid(child
->p_ucred
), -1);
1323 * Free up credentials.
1325 if (IS_VALID_CRED(child
->p_ucred
)) {
1326 kauth_cred_unref(&child
->p_ucred
);
1329 /* XXXX Note NOT SAFE TO USE p_ucred from this point onwards */
1332 * Finally finished with old proc entry.
1333 * Unlink it from its process group and free it.
1338 LIST_REMOVE(child
, p_list
); /* off zombproc */
1339 parent
->p_childrencnt
--;
1340 LIST_REMOVE(child
, p_sibling
);
1341 /* If there are no more children wakeup parent */
1342 if ((deadparent
!= 0) && (LIST_EMPTY(&parent
->p_children
)))
1343 wakeup((caddr_t
)parent
); /* with list lock held */
1344 child
->p_listflag
&= ~P_LIST_WAITING
;
1345 wakeup(&child
->p_stat
);
1347 /* Take it out of process hash */
1348 LIST_REMOVE(child
, p_hash
);
1349 child
->p_listflag
&= ~P_LIST_INHASH
;
1350 proc_checkdeadrefs(child
);
1355 * If a child zombie is being reaped because its parent
1356 * is exiting, make sure we update the list flag
1358 child
->p_listflag
|= P_LIST_DEADPARENT
;
1363 #if CONFIG_FINE_LOCK_GROUPS
1364 lck_mtx_destroy(&child
->p_mlock
, proc_mlock_grp
);
1365 lck_mtx_destroy(&child
->p_fdmlock
, proc_fdmlock_grp
);
1367 lck_mtx_destroy(&child
->p_dtrace_sprlock
, proc_lck_grp
);
1369 lck_spin_destroy(&child
->p_slock
, proc_slock_grp
);
1370 #else /* CONFIG_FINE_LOCK_GROUPS */
1371 lck_mtx_destroy(&child
->p_mlock
, proc_lck_grp
);
1372 lck_mtx_destroy(&child
->p_fdmlock
, proc_lck_grp
);
1374 lck_mtx_destroy(&child
->p_dtrace_sprlock
, proc_lck_grp
);
1376 lck_spin_destroy(&child
->p_slock
, proc_lck_grp
);
1377 #endif /* CONFIG_FINE_LOCK_GROUPS */
1378 workqueue_destroy_lock(child
);
1380 FREE_ZONE(child
, sizeof *child
, M_PROC
);
1381 if ((locked
== 1) && (droplock
== 0))
1389 wait1continue(int result
)
1394 struct _wait4_data
*wait4_data
;
1395 struct wait4_nocancel_args
*uap
;
1402 thread
= current_thread();
1403 uth
= (struct uthread
*)get_bsdthread_info(thread
);
1405 wait4_data
= &uth
->uu_kevent
.uu_wait4_data
;
1406 uap
= wait4_data
->args
;
1407 retval
= wait4_data
->retval
;
1408 return(wait4_nocancel(p
, uap
, retval
));
1412 wait4(proc_t q
, struct wait4_args
*uap
, int32_t *retval
)
1414 __pthread_testcancel(1);
1415 return(wait4_nocancel(q
, (struct wait4_nocancel_args
*)uap
, retval
));
1419 wait4_nocancel(proc_t q
, struct wait4_nocancel_args
*uap
, int32_t *retval
)
1426 struct _wait4_data
*wait4_data
;
1428 AUDIT_ARG(pid
, uap
->pid
);
1431 uap
->pid
= -q
->p_pgrpid
;
1439 for (p
= q
->p_children
.lh_first
; p
!= 0; p
= p
->p_sibling
.le_next
) {
1440 if ( p
->p_sibling
.le_next
!= 0 )
1442 if (uap
->pid
!= WAIT_ANY
&&
1443 p
->p_pid
!= uap
->pid
&&
1444 p
->p_pgrpid
!= -(uap
->pid
))
1449 /* XXX This is racy because we don't get the lock!!!! */
1451 if (p
->p_listflag
& P_LIST_WAITING
) {
1452 (void)msleep(&p
->p_stat
, proc_list_mlock
, PWAIT
, "waitcoll", 0);
1455 p
->p_listflag
|= P_LIST_WAITING
; /* only allow single thread to wait() */
1458 if (p
->p_stat
== SZOMB
) {
1459 int reparentedtoinit
= (p
->p_listflag
& P_LIST_DEADPARENT
) ? 1 : 0;
1463 if ((error
= mac_proc_check_wait(q
, p
)) != 0)
1466 retval
[0] = p
->p_pid
;
1468 /* Legacy apps expect only 8 bits of status */
1469 status
= 0xffff & p
->p_xstat
; /* convert to int */
1470 error
= copyout((caddr_t
)&status
,
1477 if (p
->p_ru
== NULL
) {
1480 if (IS_64BIT_PROCESS(q
)) {
1481 struct user64_rusage my_rusage
;
1482 munge_user64_rusage(&p
->p_ru
->ru
, &my_rusage
);
1483 error
= copyout((caddr_t
)&my_rusage
,
1485 sizeof (my_rusage
));
1488 struct user32_rusage my_rusage
;
1489 munge_user32_rusage(&p
->p_ru
->ru
, &my_rusage
);
1490 error
= copyout((caddr_t
)&my_rusage
,
1492 sizeof (my_rusage
));
1495 /* information unavailable? */
1500 /* Conformance change for 6577252.
1501 * When SIGCHLD is blocked and wait() returns because the status
1502 * of a child process is available and there are no other
1503 * children processes, then any pending SIGCHLD signal is cleared.
1505 if ( sibling_count
== 0 ) {
1506 int mask
= sigmask(SIGCHLD
);
1507 uth
= current_uthread();
1509 if ( (uth
->uu_sigmask
& mask
) != 0 ) {
1510 /* we are blocking SIGCHLD signals. clear any pending SIGCHLD.
1511 * This locking looks funny but it is protecting access to the
1512 * thread via p_uthlist.
1515 uth
->uu_siglist
&= ~mask
; /* clear pending signal */
1521 (void)reap_child_locked(q
, p
, 0, reparentedtoinit
, 0, 0);
1525 if (p
->p_stat
== SSTOP
&& (p
->p_lflag
& P_LWAITED
) == 0 &&
1526 (p
->p_lflag
& P_LTRACED
|| uap
->options
& WUNTRACED
)) {
1529 if ((error
= mac_proc_check_wait(q
, p
)) != 0)
1533 p
->p_lflag
|= P_LWAITED
;
1535 retval
[0] = p
->p_pid
;
1537 status
= W_STOPCODE(p
->p_xstat
);
1538 error
= copyout((caddr_t
)&status
,
1546 * If we are waiting for continued processses, and this
1547 * process was continued
1549 if ((uap
->options
& WCONTINUED
) &&
1550 (p
->p_flag
& P_CONTINUED
)) {
1553 if ((error
= mac_proc_check_wait(q
, p
)) != 0)
1557 /* Prevent other process for waiting for this event */
1558 OSBitAndAtomic(~((uint32_t)P_CONTINUED
), &p
->p_flag
);
1559 retval
[0] = p
->p_pid
;
1561 status
= W_STOPCODE(SIGCONT
);
1562 error
= copyout((caddr_t
)&status
,
1569 p
->p_listflag
&= ~P_LIST_WAITING
;
1572 /* list lock is held when we get here any which way */
1578 if (uap
->options
& WNOHANG
) {
1584 /* Save arguments for continuation. Backing storage is in uthread->uu_arg, and will not be deallocated */
1585 uth
= current_uthread();
1586 wait4_data
= &uth
->uu_kevent
.uu_wait4_data
;
1587 wait4_data
->args
= uap
;
1588 wait4_data
->retval
= retval
;
1590 if ((error
= msleep0((caddr_t
)q
, proc_list_mlock
, PWAIT
| PCATCH
| PDROP
, "wait", 0, wait1continue
)))
1596 p
->p_listflag
&= ~P_LIST_WAITING
;
1603 #define ASSERT_LCK_MTX_OWNED(lock) \
1604 lck_mtx_assert(lock, LCK_MTX_ASSERT_OWNED)
1606 #define ASSERT_LCK_MTX_OWNED(lock) /* nothing */
1610 waitidcontinue(int result
)
1615 struct _waitid_data
*waitid_data
;
1616 struct waitid_nocancel_args
*uap
;
1623 thread
= current_thread();
1624 uth
= (struct uthread
*)get_bsdthread_info(thread
);
1626 waitid_data
= &uth
->uu_kevent
.uu_waitid_data
;
1627 uap
= waitid_data
->args
;
1628 retval
= waitid_data
->retval
;
1629 return(waitid_nocancel(p
, uap
, retval
));
1633 * Description: Suspend the calling thread until one child of the process
1634 * containing the calling thread changes state.
1636 * Parameters: uap->idtype one of P_PID, P_PGID, P_ALL
1637 * uap->id pid_t or gid_t or ignored
1638 * uap->infop Address of siginfo_t struct in
1639 * user space into which to return status
1640 * uap->options flag values
1642 * Returns: 0 Success
1643 * !0 Error returning status to user space
1646 waitid(proc_t q
, struct waitid_args
*uap
, int32_t *retval
)
1648 __pthread_testcancel(1);
1649 return (waitid_nocancel(q
, (struct waitid_nocancel_args
*)uap
, retval
));
1653 waitid_nocancel(proc_t q
, struct waitid_nocancel_args
*uap
,
1654 __unused
int32_t *retval
)
1656 user_siginfo_t siginfo
; /* siginfo data to return to caller */
1657 boolean_t caller64
= IS_64BIT_PROCESS(q
);
1662 struct _waitid_data
*waitid_data
;
1664 if (uap
->options
== 0 ||
1665 (uap
->options
& ~(WNOHANG
|WNOWAIT
|WCONTINUED
|WSTOPPED
|WEXITED
)))
1666 return (EINVAL
); /* bits set that aren't recognized */
1668 switch (uap
->idtype
) {
1669 case P_PID
: /* child with process ID equal to... */
1670 case P_PGID
: /* child with process group ID equal to... */
1671 if (((int)uap
->id
) < 0)
1674 case P_ALL
: /* any child */
1682 for (p
= q
->p_children
.lh_first
; p
!= 0; p
= p
->p_sibling
.le_next
) {
1684 switch (uap
->idtype
) {
1685 case P_PID
: /* child with process ID equal to... */
1686 if (p
->p_pid
!= (pid_t
)uap
->id
)
1689 case P_PGID
: /* child with process group ID equal to... */
1690 if (p
->p_pgrpid
!= (pid_t
)uap
->id
)
1693 case P_ALL
: /* any child */
1697 /* XXX This is racy because we don't get the lock!!!! */
1700 * Wait collision; go to sleep and restart; used to maintain
1701 * the single return for waited process guarantee.
1703 if (p
->p_listflag
& P_LIST_WAITING
) {
1704 (void) msleep(&p
->p_stat
, proc_list_mlock
,
1705 PWAIT
, "waitidcoll", 0);
1708 p
->p_listflag
|= P_LIST_WAITING
; /* mark busy */
1712 bzero(&siginfo
, sizeof (siginfo
));
1714 switch (p
->p_stat
) {
1715 case SZOMB
: /* Exited */
1716 if (!(uap
->options
& WEXITED
))
1720 if ((error
= mac_proc_check_wait(q
, p
)) != 0)
1723 siginfo
.si_signo
= SIGCHLD
;
1724 siginfo
.si_pid
= p
->p_pid
;
1725 siginfo
.si_status
= WEXITSTATUS(p
->p_xstat
);
1726 if (WIFSIGNALED(p
->p_xstat
)) {
1727 siginfo
.si_code
= WCOREDUMP(p
->p_xstat
) ?
1728 CLD_DUMPED
: CLD_KILLED
;
1730 siginfo
.si_code
= CLD_EXITED
;
1732 if ((error
= copyoutsiginfo(&siginfo
,
1733 caller64
, uap
->infop
)) != 0)
1736 /* Prevent other process for waiting for this event? */
1737 if (!(uap
->options
& WNOWAIT
)) {
1738 (void) reap_child_locked(q
, p
, 0, 0, 0, 0);
1743 case SSTOP
: /* Stopped */
1745 * If we are not interested in stopped processes, then
1748 if (!(uap
->options
& WSTOPPED
))
1752 * If someone has already waited it, we lost a race
1753 * to be the one to return status.
1755 if ((p
->p_lflag
& P_LWAITED
) != 0)
1759 if ((error
= mac_proc_check_wait(q
, p
)) != 0)
1762 siginfo
.si_signo
= SIGCHLD
;
1763 siginfo
.si_pid
= p
->p_pid
;
1764 siginfo
.si_status
= p
->p_xstat
; /* signal number */
1765 siginfo
.si_code
= CLD_STOPPED
;
1767 if ((error
= copyoutsiginfo(&siginfo
,
1768 caller64
, uap
->infop
)) != 0)
1771 /* Prevent other process for waiting for this event? */
1772 if (!(uap
->options
& WNOWAIT
)) {
1774 p
->p_lflag
|= P_LWAITED
;
1779 default: /* All other states => Continued */
1780 if (!(uap
->options
& WCONTINUED
))
1784 * If the flag isn't set, then this process has not
1785 * been stopped and continued, or the status has
1786 * already been reaped by another caller of waitid().
1788 if ((p
->p_flag
& P_CONTINUED
) == 0)
1792 if ((error
= mac_proc_check_wait(q
, p
)) != 0)
1795 siginfo
.si_signo
= SIGCHLD
;
1796 siginfo
.si_code
= CLD_CONTINUED
;
1798 siginfo
.si_pid
= p
->p_contproc
;
1799 siginfo
.si_status
= p
->p_xstat
;
1802 if ((error
= copyoutsiginfo(&siginfo
,
1803 caller64
, uap
->infop
)) != 0)
1806 /* Prevent other process for waiting for this event? */
1807 if (!(uap
->options
& WNOWAIT
)) {
1808 OSBitAndAtomic(~((uint32_t)P_CONTINUED
),
1813 ASSERT_LCK_MTX_OWNED(proc_list_mlock
);
1815 /* Not a process we are interested in; go on to next child */
1817 p
->p_listflag
&= ~P_LIST_WAITING
;
1820 ASSERT_LCK_MTX_OWNED(proc_list_mlock
);
1822 /* No child processes that could possibly satisfy the request? */
1829 if (uap
->options
& WNOHANG
) {
1832 if ((error
= mac_proc_check_wait(q
, p
)) != 0)
1836 * The state of the siginfo structure in this case
1837 * is undefined. Some implementations bzero it, some
1838 * (like here) leave it untouched for efficiency.
1840 * Thus the most portable check for "no matching pid with
1841 * WNOHANG" is to store a zero into si_pid before
1842 * invocation, then check for a non-zero value afterwards.
1847 /* Save arguments for continuation. Backing storage is in uthread->uu_arg, and will not be deallocated */
1848 uth
= current_uthread();
1849 waitid_data
= &uth
->uu_kevent
.uu_waitid_data
;
1850 waitid_data
->args
= uap
;
1851 waitid_data
->retval
= retval
;
1853 if ((error
= msleep0(q
, proc_list_mlock
,
1854 PWAIT
| PCATCH
| PDROP
, "waitid", 0, waitidcontinue
)) != 0)
1860 p
->p_listflag
&= ~P_LIST_WAITING
;
1867 * make process 'parent' the new parent of process 'child'.
1870 proc_reparentlocked(proc_t child
, proc_t parent
, int cansignal
, int locked
)
1872 proc_t oldparent
= PROC_NULL
;
1874 if (child
->p_pptr
== parent
)
1880 oldparent
= child
->p_pptr
;
1881 #if __PROC_INTERNAL_DEBUG
1882 if (oldparent
== PROC_NULL
)
1883 panic("proc_reparent: process %p does not have a parent\n", child
);
1886 LIST_REMOVE(child
, p_sibling
);
1887 #if __PROC_INTERNAL_DEBUG
1888 if (oldparent
->p_childrencnt
== 0)
1889 panic("process children count already 0\n");
1891 oldparent
->p_childrencnt
--;
1892 #if __PROC_INTERNAL_DEBUG1
1893 if (oldparent
->p_childrencnt
< 0)
1894 panic("process children count -ve\n");
1896 LIST_INSERT_HEAD(&parent
->p_children
, child
, p_sibling
);
1897 parent
->p_childrencnt
++;
1898 child
->p_pptr
= parent
;
1899 child
->p_ppid
= parent
->p_pid
;
1903 if ((cansignal
!= 0) && (initproc
== parent
) && (child
->p_stat
== SZOMB
))
1904 psignal(initproc
, SIGCHLD
);
1910 * Exit: deallocate address space and other resources, change proc state
1911 * to zombie, and unlink proc from allproc and parent's lists. Save exit
1912 * status and rusage for wait(). Check for child processes and orphan them.
1916 vfork_exit(proc_t p
, int rv
)
1918 vfork_exit_internal(p
, rv
, 0);
1922 vfork_exit_internal(proc_t p
, int rv
, int forceexit
)
1924 thread_t self
= current_thread();
1926 struct task
*task
= p
->task
;
1931 * If a thread in this task has already
1932 * called exit(), then halt any others
1936 ut
= get_bsdthread_info(self
);
1940 if ((p
->p_lflag
& P_LPEXIT
) == P_LPEXIT
) {
1942 * This happens when a parent exits/killed and vfork is in progress
1943 * other threads. But shutdown code for ex has already called exit1()
1948 p
->p_lflag
|= (P_LEXIT
| P_LPEXIT
);
1951 if (forceexit
== 0) {
1953 * parent of a vfork child has already called exit() and the
1954 * thread that has vfork in proress terminates. So there is no
1955 * separate address space here and it has already been marked for
1956 * termination. This was never covered before and could cause problems
1957 * if we block here for outside code.
1959 /* Notify the perf server */
1960 (void)sys_perf_notify(self
, p
->p_pid
);
1964 * Remove proc from allproc queue and from pidhash chain.
1965 * Need to do this before we do anything that can block.
1966 * Not doing causes things like mount() find this on allproc
1967 * in partially cleaned state.
1972 #if CONFIG_MEMORYSTATUS
1973 memorystatus_remove(p
, TRUE
);
1976 LIST_REMOVE(p
, p_list
);
1977 LIST_INSERT_HEAD(&zombproc
, p
, p_list
); /* Place onto zombproc. */
1978 /* will not be visible via proc_find */
1979 p
->p_listflag
|= P_LIST_EXITED
;
1985 p
->p_lflag
&= ~(P_LTRACED
| P_LPPWAIT
);
1986 p
->p_sigignore
= ~0;
1990 if (thread_call_cancel(p
->p_rcall
))
1993 while (p
->p_ractive
> 0) {
2002 thread_call_free(p
->p_rcall
);
2011 vproc_exit(proc_t p
)
2018 struct task
*task
= p
->task
;
2021 struct session
*sessp
;
2022 struct rusage_superset
*rup
;
2024 /* XXX Zombie allocation may fail, in which case stats get lost */
2025 MALLOC_ZONE(rup
, struct rusage_superset
*,
2026 sizeof (*rup
), M_ZOMBIE
, M_WAITOK
);
2031 * Close open files and release open-file table.
2036 sessp
= proc_session(p
);
2037 if (SESS_LEADER(p
, sessp
)) {
2039 if (sessp
->s_ttyvp
!= NULLVP
) {
2040 struct vnode
*ttyvp
;
2043 struct vfs_context context
;
2047 * Controlling process.
2048 * Signal foreground pgrp,
2049 * drain controlling terminal
2050 * and revoke access to controlling terminal.
2052 session_lock(sessp
);
2053 tp
= SESSION_TP(sessp
);
2054 if ((tp
!= TTY_NULL
) && (tp
->t_session
== sessp
)) {
2055 session_unlock(sessp
);
2058 * We're going to SIGHUP the foreground process
2059 * group. It can't change from this point on
2060 * until the revoke is complete.
2061 * The process group changes under both the tty
2062 * lock and proc_list_lock but we need only one
2068 tty_pgsignal(tp
, SIGHUP
, 1);
2070 session_lock(sessp
);
2071 tp
= SESSION_TP(sessp
);
2073 cttyflag
= sessp
->s_flags
& S_CTTYREF
;
2074 sessp
->s_flags
&= ~S_CTTYREF
;
2075 ttyvp
= sessp
->s_ttyvp
;
2076 ttyvid
= sessp
->s_ttyvid
;
2077 sessp
->s_ttyvp
= NULL
;
2078 sessp
->s_ttyvid
= 0;
2079 sessp
->s_ttyp
= TTY_NULL
;
2080 sessp
->s_ttypgrpid
= NO_PID
;
2081 session_unlock(sessp
);
2083 if ((ttyvp
!= NULLVP
) && (vnode_getwithvid(ttyvp
, ttyvid
) == 0)) {
2084 if (tp
!= TTY_NULL
) {
2089 context
.vc_thread
= proc_thread(p
); /* XXX */
2090 context
.vc_ucred
= kauth_cred_proc_ref(p
);
2091 VNOP_REVOKE(ttyvp
, REVOKEALL
, &context
);
2094 * Release the extra usecount taken in cttyopen.
2095 * usecount should be released after VNOP_REVOKE is called.
2096 * This usecount was taken to ensure that
2097 * the VNOP_REVOKE results in a close to
2098 * the tty since cttyclose is a no-op.
2103 kauth_cred_unref(&context
.vc_ucred
);
2108 * This is cleared even if not set. This is also done in
2109 * spec_close to ensure that the flag is cleared.
2118 session_lock(sessp
);
2119 sessp
->s_leader
= NULL
;
2120 session_unlock(sessp
);
2122 session_rele(sessp
);
2128 p
->p_rlimit
[RLIMIT_FSIZE
].rlim_cur
= RLIM_INFINITY
;
2131 proc_childdrainstart(p
);
2132 while ((q
= p
->p_children
.lh_first
) != NULL
) {
2133 if (q
->p_stat
== SZOMB
) {
2135 panic("parent child linkage broken");
2136 /* check for lookups by zomb sysctl */
2137 while ((q
->p_listflag
& P_LIST_WAITING
) == P_LIST_WAITING
) {
2138 msleep(&q
->p_stat
, proc_list_mlock
, PWAIT
, "waitcoll", 0);
2140 q
->p_listflag
|= P_LIST_WAITING
;
2142 * This is a named reference and it is not granted
2143 * if the reap is already in progress. So we get
2144 * the reference here exclusively and their can be
2145 * no waiters. So there is no need for a wakeup
2146 * after we are done. AlsO the reap frees the structure
2147 * and the proc struct cannot be used for wakeups as well.
2148 * It is safe to use q here as this is system reap
2150 (void)reap_child_locked(p
, q
, 1, 0, 1, 0);
2153 * Traced processes are killed
2154 * since their existence means someone is messing up.
2156 if (q
->p_lflag
& P_LTRACED
) {
2161 opp
= proc_find(q
->p_oppid
);
2162 if (opp
!= PROC_NULL
) {
2166 proc_reparentlocked(q
, opp
, 0, 0);
2169 /* original parent exited while traced */
2171 q
->p_listflag
|= P_LIST_DEADPARENT
;
2174 proc_reparentlocked(q
, initproc
, 0, 0);
2178 q
->p_lflag
&= ~P_LTRACED
;
2180 if (q
->sigwait_thread
) {
2181 thread_t thread
= q
->sigwait_thread
;
2185 * The sigwait_thread could be stopped at a
2186 * breakpoint. Wake it up to kill.
2187 * Need to do this as it could be a thread which is not
2188 * the first thread in the task. So any attempts to kill
2189 * the process would result into a deadlock on q->sigwait.
2191 thread_resume(thread
);
2192 clear_wait(thread
, THREAD_INTERRUPTED
);
2193 threadsignal(thread
, SIGKILL
, 0);
2198 psignal(q
, SIGKILL
);
2201 q
->p_listflag
|= P_LIST_DEADPARENT
;
2202 proc_reparentlocked(q
, initproc
, 0, 1);
2207 proc_childdrainend(p
);
2211 * Release reference to text vnode
2215 if (tvp
!= NULLVP
) {
2220 * Save exit status and final rusage info, adding in child rusage
2221 * info and self times. If we were unable to allocate a zombie
2222 * structure, this information is lost.
2225 rup
->ru
= p
->p_stats
->p_ru
;
2226 timerclear(&rup
->ru
.ru_utime
);
2227 timerclear(&rup
->ru
.ru_stime
);
2231 mach_task_basic_info_data_t tinfo
;
2232 task_thread_times_info_data_t ttimesinfo
;
2233 int task_info_stuff
, task_ttimes_stuff
;
2234 struct timeval ut
,st
;
2236 task_info_stuff
= MACH_TASK_BASIC_INFO_COUNT
;
2237 task_info(task
, MACH_TASK_BASIC_INFO
,
2238 &tinfo
, &task_info_stuff
);
2239 p
->p_ru
->ru
.ru_utime
.tv_sec
= tinfo
.user_time
.seconds
;
2240 p
->p_ru
->ru
.ru_utime
.tv_usec
= tinfo
.user_time
.microseconds
;
2241 p
->p_ru
->ru
.ru_stime
.tv_sec
= tinfo
.system_time
.seconds
;
2242 p
->p_ru
->ru
.ru_stime
.tv_usec
= tinfo
.system_time
.microseconds
;
2244 task_ttimes_stuff
= TASK_THREAD_TIMES_INFO_COUNT
;
2245 task_info(task
, TASK_THREAD_TIMES_INFO
,
2246 &ttimesinfo
, &task_ttimes_stuff
);
2248 ut
.tv_sec
= ttimesinfo
.user_time
.seconds
;
2249 ut
.tv_usec
= ttimesinfo
.user_time
.microseconds
;
2250 st
.tv_sec
= ttimesinfo
.system_time
.seconds
;
2251 st
.tv_usec
= ttimesinfo
.system_time
.microseconds
;
2252 timeradd(&ut
,&p
->p_ru
->ru
.ru_utime
,&p
->p_ru
->ru
.ru_utime
);
2253 timeradd(&st
,&p
->p_ru
->ru
.ru_stime
,&p
->p_ru
->ru
.ru_stime
);
2257 ruadd(&rup
->ru
, &p
->p_stats
->p_cru
);
2259 gather_rusage_info(p
, &rup
->ri
, RUSAGE_INFO_CURRENT
);
2260 rup
->ri
.ri_phys_footprint
= 0;
2261 rup
->ri
.ri_proc_exit_abstime
= mach_absolute_time();
2264 * Now that we have filled in the rusage info, make it
2265 * visible to an external observer via proc_pid_rusage().
2271 * Free up profiling buffers.
2274 struct uprof
*p0
= &p
->p_stats
->p_prof
, *p1
, *pn
;
2280 for (; p1
!= NULL
; p1
= pn
) {
2282 kfree(p1
, sizeof *p1
);
2287 pth_proc_hashdelete(p
);
2291 * Other substructures are freed from wait().
2293 FREE_ZONE(p
->p_stats
, sizeof *p
->p_stats
, M_PSTATS
);
2296 FREE_ZONE(p
->p_sigacts
, sizeof *p
->p_sigacts
, M_SIGACTS
);
2297 p
->p_sigacts
= NULL
;
2299 proc_limitdrop(p
, 1);
2303 * Finish up by terminating the task
2304 * and halt this thread (only if a
2305 * member of the task exiting).
2307 p
->task
= TASK_NULL
;
2310 * Notify parent that we're gone.
2312 pp
= proc_parent(p
);
2313 if ((p
->p_listflag
& P_LIST_DEADPARENT
) == 0) {
2314 if (pp
!= initproc
) {
2316 pp
->si_pid
= p
->p_pid
;
2317 pp
->si_status
= p
->p_xstat
;
2318 pp
->si_code
= CLD_EXITED
;
2320 * p_ucred usage is safe as it is an exiting process
2321 * and reference is dropped in reap
2323 pp
->si_uid
= kauth_cred_getruid(p
->p_ucred
);
2326 /* mark as a zombie */
2327 /* mark as a zombie */
2328 /* No need to take proc lock as all refs are drained and
2329 * no one except parent (reaping ) can look at this.
2330 * The write is to an int and is coherent. Also parent is
2331 * keyed off of list lock for reaping
2335 psignal(pp
, SIGCHLD
);
2337 /* and now wakeup the parent */
2339 wakeup((caddr_t
)pp
);
2343 /* check for lookups by zomb sysctl */
2344 while ((p
->p_listflag
& P_LIST_WAITING
) == P_LIST_WAITING
) {
2345 msleep(&p
->p_stat
, proc_list_mlock
, PWAIT
, "waitcoll", 0);
2348 p
->p_listflag
|= P_LIST_WAITING
;
2351 * This is a named reference and it is not granted
2352 * if the reap is already in progress. So we get
2353 * the reference here exclusively and their can be
2354 * no waiters. So there is no need for a wakeup
2355 * after we are done. AlsO the reap frees the structure
2356 * and the proc struct cannot be used for wakeups as well.
2357 * It is safe to use p here as this is system reap
2359 (void)reap_child_locked(pp
, p
, 0, 0, 1, 1);
2360 /* list lock dropped by reap_child_locked */
2368 * LP64 support - long is 64 bits if we are dealing with a 64 bit user
2369 * process. We munge the kernel version of rusage into the
2372 __private_extern__
void
2373 munge_user64_rusage(struct rusage
*a_rusage_p
, struct user64_rusage
*a_user_rusage_p
)
2375 /* timeval changes size, so utime and stime need special handling */
2376 a_user_rusage_p
->ru_utime
.tv_sec
= a_rusage_p
->ru_utime
.tv_sec
;
2377 a_user_rusage_p
->ru_utime
.tv_usec
= a_rusage_p
->ru_utime
.tv_usec
;
2378 a_user_rusage_p
->ru_stime
.tv_sec
= a_rusage_p
->ru_stime
.tv_sec
;
2379 a_user_rusage_p
->ru_stime
.tv_usec
= a_rusage_p
->ru_stime
.tv_usec
;
2381 * everything else can be a direct assign, since there is no loss
2382 * of precision implied boing 32->64.
2384 a_user_rusage_p
->ru_maxrss
= a_rusage_p
->ru_maxrss
;
2385 a_user_rusage_p
->ru_ixrss
= a_rusage_p
->ru_ixrss
;
2386 a_user_rusage_p
->ru_idrss
= a_rusage_p
->ru_idrss
;
2387 a_user_rusage_p
->ru_isrss
= a_rusage_p
->ru_isrss
;
2388 a_user_rusage_p
->ru_minflt
= a_rusage_p
->ru_minflt
;
2389 a_user_rusage_p
->ru_majflt
= a_rusage_p
->ru_majflt
;
2390 a_user_rusage_p
->ru_nswap
= a_rusage_p
->ru_nswap
;
2391 a_user_rusage_p
->ru_inblock
= a_rusage_p
->ru_inblock
;
2392 a_user_rusage_p
->ru_oublock
= a_rusage_p
->ru_oublock
;
2393 a_user_rusage_p
->ru_msgsnd
= a_rusage_p
->ru_msgsnd
;
2394 a_user_rusage_p
->ru_msgrcv
= a_rusage_p
->ru_msgrcv
;
2395 a_user_rusage_p
->ru_nsignals
= a_rusage_p
->ru_nsignals
;
2396 a_user_rusage_p
->ru_nvcsw
= a_rusage_p
->ru_nvcsw
;
2397 a_user_rusage_p
->ru_nivcsw
= a_rusage_p
->ru_nivcsw
;
2400 /* For a 64-bit kernel and 32-bit userspace, munging may be needed */
2401 __private_extern__
void
2402 munge_user32_rusage(struct rusage
*a_rusage_p
, struct user32_rusage
*a_user_rusage_p
)
2404 /* timeval changes size, so utime and stime need special handling */
2405 a_user_rusage_p
->ru_utime
.tv_sec
= a_rusage_p
->ru_utime
.tv_sec
;
2406 a_user_rusage_p
->ru_utime
.tv_usec
= a_rusage_p
->ru_utime
.tv_usec
;
2407 a_user_rusage_p
->ru_stime
.tv_sec
= a_rusage_p
->ru_stime
.tv_sec
;
2408 a_user_rusage_p
->ru_stime
.tv_usec
= a_rusage_p
->ru_stime
.tv_usec
;
2410 * everything else can be a direct assign. We currently ignore
2411 * the loss of precision
2413 a_user_rusage_p
->ru_maxrss
= a_rusage_p
->ru_maxrss
;
2414 a_user_rusage_p
->ru_ixrss
= a_rusage_p
->ru_ixrss
;
2415 a_user_rusage_p
->ru_idrss
= a_rusage_p
->ru_idrss
;
2416 a_user_rusage_p
->ru_isrss
= a_rusage_p
->ru_isrss
;
2417 a_user_rusage_p
->ru_minflt
= a_rusage_p
->ru_minflt
;
2418 a_user_rusage_p
->ru_majflt
= a_rusage_p
->ru_majflt
;
2419 a_user_rusage_p
->ru_nswap
= a_rusage_p
->ru_nswap
;
2420 a_user_rusage_p
->ru_inblock
= a_rusage_p
->ru_inblock
;
2421 a_user_rusage_p
->ru_oublock
= a_rusage_p
->ru_oublock
;
2422 a_user_rusage_p
->ru_msgsnd
= a_rusage_p
->ru_msgsnd
;
2423 a_user_rusage_p
->ru_msgrcv
= a_rusage_p
->ru_msgrcv
;
2424 a_user_rusage_p
->ru_nsignals
= a_rusage_p
->ru_nsignals
;
2425 a_user_rusage_p
->ru_nvcsw
= a_rusage_p
->ru_nvcsw
;
2426 a_user_rusage_p
->ru_nivcsw
= a_rusage_p
->ru_nivcsw
;