]> git.saurik.com Git - apple/xnu.git/blame - bsd/kern/kern_exit.c
xnu-4570.1.46.tar.gz
[apple/xnu.git] / bsd / kern / kern_exit.c
CommitLineData
1c79356b 1/*
39037602 2 * Copyright (c) 2000-2016 Apple Inc. All rights reserved.
5d5c5d0d 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
A
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.
8f6c56a5 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/* Copyright (c) 1995, 1997 Apple Computer, Inc. All Rights Reserved */
29/*
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.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
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.
53 *
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
64 * SUCH DAMAGE.
65 *
66 * @(#)kern_exit.c 8.7 (Berkeley) 2/12/94
67 */
2d21ac55
A
68/*
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,
72 * Version 2.0.
73 */
1c79356b
A
74
75#include <machine/reg.h>
76#include <machine/psl.h>
5ba3f43e 77#include <stdatomic.h>
1c79356b
A
78
79#include "compat_43.h"
80
81#include <sys/param.h>
82#include <sys/systm.h>
83#include <sys/ioctl.h>
91447636 84#include <sys/proc_internal.h>
2d21ac55 85#include <sys/proc.h>
91447636 86#include <sys/kauth.h>
1c79356b
A
87#include <sys/tty.h>
88#include <sys/time.h>
89#include <sys/resource.h>
90#include <sys/kernel.h>
1c79356b 91#include <sys/wait.h>
91447636
A
92#include <sys/file_internal.h>
93#include <sys/vnode_internal.h>
1c79356b
A
94#include <sys/syslog.h>
95#include <sys/malloc.h>
96#include <sys/resourcevar.h>
97#include <sys/ptrace.h>
3e170ce0 98#include <sys/proc_info.h>
39037602 99#include <sys/reason.h>
3e170ce0 100#include <sys/_types/_timeval64.h>
1c79356b 101#include <sys/user.h>
55e303ae 102#include <sys/aio_kern.h>
91447636
A
103#include <sys/sysproto.h>
104#include <sys/signalvar.h>
b0d623f7 105#include <sys/kdebug.h>
5ba3f43e
A
106#include <sys/filedesc.h> /* fdfree */
107#include <sys/acct.h> /* acct_process */
108#include <sys/codesign.h>
109#include <sys/event.h> /* kevent_proc_copy_uptrs */
110#include <sys/sdt.h>
e5568f75 111
b0d623f7 112#include <security/audit/audit.h>
e5568f75 113#include <bsm/audit_kevents.h>
1c79356b
A
114
115#include <mach/mach_types.h>
5ba3f43e
A
116#include <mach/task.h>
117#include <mach/thread_act.h>
91447636 118
5ba3f43e 119#include <kern/exc_resource.h>
91447636
A
120#include <kern/kern_types.h>
121#include <kern/kalloc.h>
122#include <kern/task.h>
3e170ce0 123#include <corpses/task_corpse.h>
1c79356b 124#include <kern/thread.h>
2d21ac55 125#include <kern/thread_call.h>
9bccf70c 126#include <kern/sched_prim.h>
1c79356b 127#include <kern/assert.h>
39037602 128#include <kern/policy_internal.h>
5ba3f43e 129#include <kern/exc_guard.h>
c331a0be 130
5ba3f43e
A
131#include <vm/vm_protos.h>
132
133#include <pexpert/pexpert.h>
316670eb 134
5ba3f43e
A
135#if SYSV_SHM
136#include <sys/shm_internal.h> /* shmexit */
137#endif /* SYSV_SHM */
138#if CONFIG_PERSONAS
139#include <sys/persona.h>
140#endif /* CONFIG_PERSONAS */
316670eb
A
141#if CONFIG_MEMORYSTATUS
142#include <sys/kern_memorystatus.h>
5ba3f43e 143#endif /* CONFIG_MEMORYSTATUS */
2d21ac55
A
144#if CONFIG_DTRACE
145/* Do not include dtrace.h, it redefines kmem_[alloc/free] */
39037602 146void dtrace_proc_exit(proc_t p);
2d21ac55 147#include <sys/dtrace_ptss.h>
5ba3f43e 148#endif /* CONFIG_DTRACE */
2d21ac55 149#if CONFIG_MACF
5ba3f43e 150#include <security/mac_framework.h>
39037602 151#include <security/mac_mach_internal.h>
2d21ac55 152#include <sys/syscall.h>
5ba3f43e 153#endif /* CONFIG_MACF */
2d21ac55 154
316670eb 155void proc_prepareexit(proc_t p, int rv, boolean_t perf_notify);
5ba3f43e
A
156void gather_populate_corpse_crashinfo(proc_t p, task_t corpse_task,
157 mach_exception_data_type_t code, mach_exception_data_type_t subcode,
158 uint64_t *udata_buffer, int num_udata, void *reason);
39037602 159mach_exception_data_type_t proc_encode_exit_exception_code(proc_t p);
2d21ac55 160void vfork_exit(proc_t p, int rv);
b0d623f7
A
161__private_extern__ void munge_user64_rusage(struct rusage *a_rusage_p, struct user64_rusage *a_user_rusage_p);
162__private_extern__ void munge_user32_rusage(struct rusage *a_rusage_p, struct user32_rusage *a_user_rusage_p);
4b17d6b6 163static int reap_child_locked(proc_t parent, proc_t child, int deadparent, int reparentedtoinit, int locked, int droplock);
5ba3f43e
A
164static void populate_corpse_crashinfo(proc_t p, task_t corpse_task,
165 struct rusage_superset *rup, mach_exception_data_type_t code,
166 mach_exception_data_type_t subcode, uint64_t *udata_buffer,
167 int num_udata, os_reason_t reason);
39037602
A
168static void proc_update_corpse_exception_codes(proc_t p, mach_exception_data_type_t *code, mach_exception_data_type_t *subcode);
169extern int proc_pidpathinfo_internal(proc_t p, uint64_t arg, char *buffer, uint32_t buffersize, int32_t *retval);
3e170ce0
A
170static __attribute__((noinline)) void launchd_crashed_panic(proc_t p, int rv);
171extern void proc_piduniqidentifierinfo(proc_t p, struct proc_uniqidentifierinfo *p_uniqidinfo);
39037602
A
172extern void task_coalition_ids(task_t task, uint64_t ids[COALITION_NUM_TYPES]);
173extern uint64_t get_task_phys_footprint_limit(task_t);
174int proc_list_uptrs(void *p, uint64_t *udata_buffer, int size);
5ba3f43e 175extern uint64_t task_corpse_get_crashed_thread_id(task_t corpse_task);
3e170ce0 176
91447636
A
177
178/*
179 * Things which should have prototypes in headers, but don't
180 */
2d21ac55 181void proc_exit(proc_t p);
91447636
A
182int wait1continue(int result);
183int waitidcontinue(int result);
2d21ac55 184kern_return_t sys_perf_notify(thread_t thread, int pid);
316670eb
A
185kern_return_t task_exception_notify(exception_type_t exception,
186 mach_exception_data_type_t code, mach_exception_data_type_t subcode);
5ba3f43e 187kern_return_t task_violated_guard(mach_exception_code_t, mach_exception_subcode_t, void *);
2d21ac55 188void delay(int);
fe8ab488 189void gather_rusage_info(proc_t p, rusage_info_current *ru, int flavor);
39236c6e 190
91447636
A
191/*
192 * NOTE: Source and target may *NOT* overlap!
193 * XXX Should share code with bsd/dev/ppc/unix_signal.c
194 */
316670eb 195void
b0d623f7 196siginfo_user_to_user32(user_siginfo_t *in, user32_siginfo_t *out)
91447636
A
197{
198 out->si_signo = in->si_signo;
199 out->si_errno = in->si_errno;
200 out->si_code = in->si_code;
201 out->si_pid = in->si_pid;
202 out->si_uid = in->si_uid;
203 out->si_status = in->si_status;
b0d623f7 204 out->si_addr = CAST_DOWN_EXPLICIT(user32_addr_t,in->si_addr);
91447636 205 /* following cast works for sival_int because of padding */
b0d623f7
A
206 out->si_value.sival_ptr = CAST_DOWN_EXPLICIT(user32_addr_t,in->si_value.sival_ptr);
207 out->si_band = in->si_band; /* range reduction */
208}
209
316670eb 210void
b0d623f7
A
211siginfo_user_to_user64(user_siginfo_t *in, user64_siginfo_t *out)
212{
213 out->si_signo = in->si_signo;
214 out->si_errno = in->si_errno;
215 out->si_code = in->si_code;
216 out->si_pid = in->si_pid;
217 out->si_uid = in->si_uid;
218 out->si_status = in->si_status;
219 out->si_addr = in->si_addr;
220 /* following cast works for sival_int because of padding */
221 out->si_value.sival_ptr = in->si_value.sival_ptr;
91447636 222 out->si_band = in->si_band; /* range reduction */
91447636 223}
1c79356b 224
316670eb
A
225static int
226copyoutsiginfo(user_siginfo_t *native, boolean_t is64, user_addr_t uaddr)
227{
228 if (is64) {
229 user64_siginfo_t sinfo64;
5ba3f43e 230
316670eb
A
231 bzero(&sinfo64, sizeof (sinfo64));
232 siginfo_user_to_user64(native, &sinfo64);
233 return (copyout(&sinfo64, uaddr, sizeof (sinfo64)));
234 } else {
235 user32_siginfo_t sinfo32;
236
237 bzero(&sinfo32, sizeof (sinfo32));
238 siginfo_user_to_user32(native, &sinfo32);
239 return (copyout(&sinfo32, uaddr, sizeof (sinfo32)));
240 }
241}
242
5ba3f43e
A
243void gather_populate_corpse_crashinfo(proc_t p, task_t corpse_task,
244 mach_exception_data_type_t code, mach_exception_data_type_t subcode,
245 uint64_t *udata_buffer, int num_udata, void *reason)
39037602
A
246{
247 struct rusage_superset rup;
248
249 gather_rusage_info(p, &rup.ri, RUSAGE_INFO_CURRENT);
250 rup.ri.ri_phys_footprint = 0;
5ba3f43e
A
251 populate_corpse_crashinfo(p, corpse_task, &rup, code, subcode,
252 udata_buffer, num_udata, reason);
39037602
A
253}
254
255static void proc_update_corpse_exception_codes(proc_t p, mach_exception_data_type_t *code, mach_exception_data_type_t *subcode)
256{
257 mach_exception_data_type_t code_update = *code;
258 mach_exception_data_type_t subcode_update = *subcode;
259 if (p->p_exit_reason == OS_REASON_NULL) {
260 return;
261 }
262
263 switch (p->p_exit_reason->osr_namespace) {
264 case OS_REASON_JETSAM:
265 if (p->p_exit_reason->osr_code == JETSAM_REASON_MEMORY_PERPROCESSLIMIT) {
266 /* Update the code with EXC_RESOURCE code for high memory watermark */
267 EXC_RESOURCE_ENCODE_TYPE(code_update, RESOURCE_TYPE_MEMORY);
268 EXC_RESOURCE_ENCODE_FLAVOR(code_update, FLAVOR_HIGH_WATERMARK);
269 EXC_RESOURCE_HWM_ENCODE_LIMIT(code_update, ((get_task_phys_footprint_limit(p->task)) >> 20));
270 subcode_update = 0;
271 break;
272 }
273
274 break;
275 default:
276 break;
277 }
278
279 *code = code_update;
280 *subcode = subcode_update;
281 return;
282}
283
284mach_exception_data_type_t proc_encode_exit_exception_code(proc_t p)
285{
286 uint64_t subcode = 0;
287
288 if (p->p_exit_reason == OS_REASON_NULL) {
289 return 0;
290 }
291
292 /* Embed first 32 bits of osr_namespace and osr_code in exception code */
293 ENCODE_OSR_NAMESPACE_TO_MACH_EXCEPTION_CODE(subcode, p->p_exit_reason->osr_namespace);
294 ENCODE_OSR_CODE_TO_MACH_EXCEPTION_CODE(subcode, p->p_exit_reason->osr_code);
295 return (mach_exception_data_type_t)subcode;
296}
297
5ba3f43e
A
298static void
299populate_corpse_crashinfo(proc_t p, task_t corpse_task, struct rusage_superset *rup,
300 mach_exception_data_type_t code, mach_exception_data_type_t subcode,
301 uint64_t *udata_buffer, int num_udata, os_reason_t reason)
3e170ce0
A
302{
303 mach_vm_address_t uaddr = 0;
304 mach_exception_data_type_t exc_codes[EXCEPTION_CODE_MAX];
305 exc_codes[0] = code;
306 exc_codes[1] = subcode;
307 cpu_type_t cputype;
308 struct proc_uniqidentifierinfo p_uniqidinfo;
309 struct proc_workqueueinfo pwqinfo;
310 int retval = 0;
5ba3f43e 311 uint64_t crashed_threadid = task_corpse_get_crashed_thread_id(corpse_task);
3e170ce0 312 unsigned int pflags = 0;
39037602
A
313 uint64_t max_footprint_mb;
314 uint64_t max_footprint;
5ba3f43e 315 void *crash_info_ptr = task_get_corpseinfo(corpse_task);
3e170ce0
A
316
317#if CONFIG_MEMORYSTATUS
318 int memstat_dirty_flags = 0;
319#endif
320
321 if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_EXCEPTION_CODES, sizeof(exc_codes), &uaddr)) {
39037602 322 kcdata_memcpy(crash_info_ptr, uaddr, exc_codes, sizeof(exc_codes));
3e170ce0
A
323 }
324
325 if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_PID, sizeof(p->p_pid), &uaddr)) {
39037602 326 kcdata_memcpy(crash_info_ptr, uaddr, &p->p_pid, sizeof(p->p_pid));
3e170ce0
A
327 }
328
329 if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_PPID, sizeof(p->p_ppid), &uaddr)) {
39037602 330 kcdata_memcpy(crash_info_ptr, uaddr, &p->p_ppid, sizeof(p->p_ppid));
3e170ce0
A
331 }
332
5ba3f43e
A
333 /* Don't include the crashed thread ID if there's an exit reason that indicates it's irrelevant */
334 if ((p->p_exit_reason == OS_REASON_NULL) || !(p->p_exit_reason->osr_flags & OS_REASON_FLAG_NO_CRASHED_TID)) {
335 if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_CRASHED_THREADID, sizeof(uint64_t), &uaddr)) {
336 kcdata_memcpy(crash_info_ptr, uaddr, &crashed_threadid, sizeof(uint64_t));
337 }
3e170ce0
A
338 }
339
340 if (KERN_SUCCESS ==
341 kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_BSDINFOWITHUNIQID, sizeof(struct proc_uniqidentifierinfo), &uaddr)) {
342 proc_piduniqidentifierinfo(p, &p_uniqidinfo);
39037602 343 kcdata_memcpy(crash_info_ptr, uaddr, &p_uniqidinfo, sizeof(struct proc_uniqidentifierinfo));
3e170ce0
A
344 }
345
346 if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_RUSAGE_INFO, sizeof(rusage_info_current), &uaddr)) {
39037602 347 kcdata_memcpy(crash_info_ptr, uaddr, &rup->ri, sizeof(rusage_info_current));
3e170ce0
A
348 }
349
350 if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_PROC_CSFLAGS, sizeof(p->p_csflags), &uaddr)) {
39037602 351 kcdata_memcpy(crash_info_ptr, uaddr, &p->p_csflags, sizeof(p->p_csflags));
3e170ce0
A
352 }
353
354 if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_PROC_NAME, sizeof(p->p_comm), &uaddr)) {
39037602 355 kcdata_memcpy(crash_info_ptr, uaddr, &p->p_comm, sizeof(p->p_comm));
3e170ce0
A
356 }
357
358 if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_PROC_STARTTIME, sizeof(p->p_start), &uaddr)) {
359 struct timeval64 t64;
360 t64.tv_sec = (int64_t)p->p_start.tv_sec;
361 t64.tv_usec = (int64_t)p->p_start.tv_usec;
39037602 362 kcdata_memcpy(crash_info_ptr, uaddr, &t64, sizeof(t64));
3e170ce0
A
363 }
364
365 if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_USERSTACK, sizeof(p->user_stack), &uaddr)) {
39037602 366 kcdata_memcpy(crash_info_ptr, uaddr, &p->user_stack, sizeof(p->user_stack));
3e170ce0
A
367 }
368
369 if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_ARGSLEN, sizeof(p->p_argslen), &uaddr)) {
39037602 370 kcdata_memcpy(crash_info_ptr, uaddr, &p->p_argslen, sizeof(p->p_argslen));
3e170ce0
A
371 }
372
373 if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_PROC_ARGC, sizeof(p->p_argc), &uaddr)) {
39037602 374 kcdata_memcpy(crash_info_ptr, uaddr, &p->p_argc, sizeof(p->p_argc));
3e170ce0
A
375 }
376
377 if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_PROC_PATH, MAXPATHLEN, &uaddr)) {
39037602
A
378 char *buf = (char *) kalloc(MAXPATHLEN);
379 if (buf != NULL) {
380 bzero(buf, MAXPATHLEN);
381 proc_pidpathinfo_internal(p, 0, buf, MAXPATHLEN, &retval);
382 kcdata_memcpy(crash_info_ptr, uaddr, buf, MAXPATHLEN);
383 kfree(buf, MAXPATHLEN);
384 }
3e170ce0
A
385 }
386
387 pflags = p->p_flag & (P_LP64 | P_SUGID);
388 if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_PROC_FLAGS, sizeof(pflags), &uaddr)) {
39037602 389 kcdata_memcpy(crash_info_ptr, uaddr, &pflags, sizeof(pflags));
3e170ce0
A
390 }
391
392 if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_UID, sizeof(p->p_uid), &uaddr)) {
39037602 393 kcdata_memcpy(crash_info_ptr, uaddr, &p->p_uid, sizeof(p->p_uid));
3e170ce0
A
394 }
395
396 if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_GID, sizeof(p->p_gid), &uaddr)) {
39037602 397 kcdata_memcpy(crash_info_ptr, uaddr, &p->p_gid, sizeof(p->p_gid));
3e170ce0
A
398 }
399
400 cputype = cpu_type() & ~CPU_ARCH_MASK;
401 if (IS_64BIT_PROCESS(p))
402 cputype |= CPU_ARCH_ABI64;
403
404 if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_CPUTYPE, sizeof(cpu_type_t), &uaddr)) {
39037602
A
405 kcdata_memcpy(crash_info_ptr, uaddr, &cputype, sizeof(cpu_type_t));
406 }
407
408 if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_MEMORY_LIMIT, sizeof(max_footprint_mb), &uaddr)) {
409 max_footprint = get_task_phys_footprint_limit(p->task);
410 max_footprint_mb = max_footprint >> 20;
411 kcdata_memcpy(crash_info_ptr, uaddr, &max_footprint_mb, sizeof(max_footprint_mb));
3e170ce0
A
412 }
413
414 bzero(&pwqinfo, sizeof(struct proc_workqueueinfo));
415 retval = fill_procworkqueue(p, &pwqinfo);
416 if (retval == 0) {
417 if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_WORKQUEUEINFO, sizeof(struct proc_workqueueinfo), &uaddr)) {
39037602 418 kcdata_memcpy(crash_info_ptr, uaddr, &pwqinfo, sizeof(struct proc_workqueueinfo));
3e170ce0
A
419 }
420 }
421
422 if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_RESPONSIBLE_PID, sizeof(p->p_responsible_pid), &uaddr)) {
39037602
A
423 kcdata_memcpy(crash_info_ptr, uaddr, &p->p_responsible_pid, sizeof(p->p_responsible_pid));
424 }
425
426#if CONFIG_COALITIONS
427 if (KERN_SUCCESS == kcdata_get_memory_addr_for_array(crash_info_ptr, TASK_CRASHINFO_COALITION_ID, sizeof(uint64_t), COALITION_NUM_TYPES, &uaddr)) {
428 uint64_t coalition_ids[COALITION_NUM_TYPES];
429 task_coalition_ids(p->task, coalition_ids);
430 kcdata_memcpy(crash_info_ptr, uaddr, coalition_ids, sizeof(coalition_ids));
3e170ce0 431 }
39037602 432#endif /* CONFIG_COALITIONS */
3e170ce0
A
433
434#if CONFIG_MEMORYSTATUS
435 memstat_dirty_flags = memorystatus_dirty_get(p);
436 if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_DIRTY_FLAGS, sizeof(memstat_dirty_flags), &uaddr)) {
39037602 437 kcdata_memcpy(crash_info_ptr, uaddr, &memstat_dirty_flags, sizeof(memstat_dirty_flags));
3e170ce0
A
438 }
439#endif
440
5ba3f43e
A
441 if (p->p_exit_reason != OS_REASON_NULL && reason == OS_REASON_NULL) {
442 reason = p->p_exit_reason;
443 }
444 if (reason != OS_REASON_NULL) {
39037602
A
445 if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, EXIT_REASON_SNAPSHOT, sizeof(struct exit_reason_snapshot), &uaddr)) {
446 struct exit_reason_snapshot ers = {
5ba3f43e
A
447 .ers_namespace = reason->osr_namespace,
448 .ers_code = reason->osr_code,
449 .ers_flags = reason->osr_flags
39037602
A
450 };
451
452 kcdata_memcpy(crash_info_ptr, uaddr, &ers, sizeof(ers));
453 }
454
5ba3f43e
A
455 if (reason->osr_kcd_buf != 0) {
456 uint32_t reason_buf_size = kcdata_memory_get_used_bytes(&reason->osr_kcd_descriptor);
39037602
A
457 assert(reason_buf_size != 0);
458
459 if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, KCDATA_TYPE_NESTED_KCDATA, reason_buf_size, &uaddr)) {
5ba3f43e 460 kcdata_memcpy(crash_info_ptr, uaddr, reason->osr_kcd_buf, reason_buf_size);
39037602
A
461 }
462 }
463 }
464
465 if (num_udata > 0) {
466 if (KERN_SUCCESS == kcdata_get_memory_addr_for_array(crash_info_ptr, TASK_CRASHINFO_UDATA_PTRS,
467 sizeof(uint64_t), num_udata, &uaddr)) {
468 kcdata_memcpy(crash_info_ptr, uaddr, udata_buffer, sizeof(uint64_t) * num_udata);
469 }
470 }
471}
472
473/*
474 * We only parse exit reason kcdata blobs for launchd when it dies
475 * and we're going to panic.
476 *
477 * Meant to be called immediately before panicking.
478 */
479char *
480launchd_exit_reason_get_string_desc(os_reason_t exit_reason)
481{
482 kcdata_iter_t iter;
483
484 if (exit_reason == OS_REASON_NULL || exit_reason->osr_kcd_buf == NULL ||
485 exit_reason->osr_bufsize == 0) {
486 return NULL;
487 }
488
489 iter = kcdata_iter(exit_reason->osr_kcd_buf, exit_reason->osr_bufsize);
490 if (!kcdata_iter_valid(iter)) {
491#if DEBUG || DEVELOPMENT
492 printf("launchd exit reason has invalid exit reason buffer\n");
493#endif
494 return NULL;
495 }
496
497 if (kcdata_iter_type(iter) != KCDATA_BUFFER_BEGIN_OS_REASON) {
498#if DEBUG || DEVELOPMENT
499 printf("launchd exit reason buffer type mismatch, expected %d got %d\n",
500 KCDATA_BUFFER_BEGIN_OS_REASON, kcdata_iter_type(iter));
501#endif
502 return NULL;
503 }
504
505 iter = kcdata_iter_find_type(iter, EXIT_REASON_USER_DESC);
506 if (!kcdata_iter_valid(iter)) {
507 return NULL;
508 }
509
510 return (char *)kcdata_iter_payload(iter);
3e170ce0
A
511}
512
513static __attribute__((noinline)) void
514launchd_crashed_panic(proc_t p, int rv)
515{
39037602
A
516 char *launchd_exit_reason_desc = launchd_exit_reason_get_string_desc(p->p_exit_reason);
517
518 if (p->p_exit_reason == OS_REASON_NULL) {
519 printf("pid 1 exited -- no exit reason available -- (signal %d, exit %d)\n",
520 WTERMSIG(rv), WEXITSTATUS(rv));
521 } else {
522 printf("pid 1 exited -- exit reason namespace %d subcode 0x%llx, description %s\n",
523 p->p_exit_reason->osr_namespace, p->p_exit_reason->osr_code, launchd_exit_reason_desc ?
524 launchd_exit_reason_desc : "none");
525 }
3e170ce0 526
5ba3f43e
A
527 const char *launchd_crashed_prefix_str;
528
529 if (strnstr(p->p_name, "preinit", sizeof(p->p_name))) {
530 launchd_crashed_prefix_str = "LTE preinit process exited";
531 } else {
532 launchd_crashed_prefix_str = "initproc exited";
533 }
534
39037602 535#if (DEVELOPMENT || DEBUG) && CONFIG_COREDUMP
3e170ce0
A
536 /*
537 * For debugging purposes, generate a core file of initproc before
538 * panicking. Leave at least 300 MB free on the root volume, and ignore
539 * the process's corefile ulimit. fsync() the file to ensure it lands on disk
540 * before the panic hits.
541 */
542
543 int err;
544 uint64_t coredump_start = mach_absolute_time();
545 uint64_t coredump_end;
546 clock_sec_t tv_sec;
547 clock_usec_t tv_usec;
548 uint32_t tv_msec;
549
5ba3f43e 550
3e170ce0
A
551 err = coredump(p, 300, COREDUMP_IGNORE_ULIMIT | COREDUMP_FULLFSYNC);
552
553 coredump_end = mach_absolute_time();
554
555 absolutetime_to_microtime(coredump_end - coredump_start, &tv_sec, &tv_usec);
556
557 tv_msec = tv_usec / 1000;
558
559 if (err != 0) {
560 printf("Failed to generate initproc core file: error %d, took %d.%03d seconds\n",
561 err, (uint32_t)tv_sec, tv_msec);
562 } else {
563 printf("Generated initproc core file in %d.%03d seconds\n",
564 (uint32_t)tv_sec, tv_msec);
565 }
39037602 566#endif /* (DEVELOPMENT || DEBUG) && CONFIG_COREDUMP */
3e170ce0
A
567
568 sync(p, (void *)NULL, (int *)NULL);
569
39037602 570 if (p->p_exit_reason == OS_REASON_NULL) {
5ba3f43e
A
571 panic_with_options(0, NULL, DEBUGGER_OPTION_INITPROC_PANIC, "%s -- no exit reason available -- (signal %d, exit status %d %s)",
572 launchd_crashed_prefix_str, WTERMSIG(rv), WEXITSTATUS(rv), ((p->p_csflags & CS_KILLED) ? "CS_KILLED" : ""));
39037602 573 } else {
5ba3f43e 574 panic_with_options(0, NULL, DEBUGGER_OPTION_INITPROC_PANIC, "%s %s -- exit reason namespace %d subcode 0x%llx description: %." LAUNCHD_PANIC_REASON_STRING_MAXLEN "s",
39037602 575 ((p->p_csflags & CS_KILLED) ? "CS_KILLED" : ""),
5ba3f43e 576 launchd_crashed_prefix_str, p->p_exit_reason->osr_namespace, p->p_exit_reason->osr_code,
39037602
A
577 launchd_exit_reason_desc ? launchd_exit_reason_desc : "none");
578 }
579}
580
5ba3f43e
A
581#define OS_REASON_IFLAG_USER_FAULT 0x1
582
583#define OS_REASON_TOTAL_USER_FAULTS_PER_PROC 5
584
585static int
586abort_with_payload_internal(proc_t p,
587 uint32_t reason_namespace, uint64_t reason_code,
588 user_addr_t payload, uint32_t payload_size,
589 user_addr_t reason_string, uint64_t reason_flags,
590 uint32_t internal_flags)
39037602
A
591{
592 os_reason_t exit_reason = OS_REASON_NULL;
5ba3f43e
A
593 kern_return_t kr = KERN_SUCCESS;
594
595 if (internal_flags & OS_REASON_IFLAG_USER_FAULT) {
596 uint32_t old_value = atomic_load_explicit(&p->p_user_faults,
597 memory_order_relaxed);
598 for (;;) {
599 if (old_value >= OS_REASON_TOTAL_USER_FAULTS_PER_PROC) {
600 return EQFULL;
601 }
602 // this reloads the value in old_value
603 if (atomic_compare_exchange_strong_explicit(&p->p_user_faults,
604 &old_value, old_value + 1, memory_order_relaxed,
605 memory_order_relaxed)) {
606 break;
607 }
608 }
609 }
39037602
A
610
611 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC, BSD_PROC_EXITREASON_CREATE) | DBG_FUNC_NONE,
612 p->p_pid, reason_namespace,
613 reason_code, 0, 0);
614
5ba3f43e
A
615 exit_reason = build_userspace_exit_reason(reason_namespace, reason_code,
616 payload, payload_size, reason_string, reason_flags);
39037602 617
5ba3f43e
A
618 if (internal_flags & OS_REASON_IFLAG_USER_FAULT) {
619 mach_exception_code_t code = 0;
39037602 620
5ba3f43e
A
621 EXC_GUARD_ENCODE_TYPE(code, GUARD_TYPE_USER); /* simulated EXC_GUARD */
622 EXC_GUARD_ENCODE_FLAVOR(code, 0);
623 EXC_GUARD_ENCODE_TARGET(code, reason_namespace);
624
625 if (exit_reason == OS_REASON_NULL) {
626 kr = KERN_RESOURCE_SHORTAGE;
627 } else {
628 kr = task_violated_guard(code, reason_code, exit_reason);
629 }
630 os_reason_free(exit_reason);
631 } else {
632 /*
633 * We use SIGABRT (rather than calling exit directly from here) so that
634 * the debugger can catch abort_with_{reason,payload} calls.
635 */
636 psignal_try_thread_with_reason(p, current_thread(), SIGABRT, exit_reason);
637 }
638
639 switch (kr) {
640 case KERN_SUCCESS:
641 return 0;
642 case KERN_NOT_SUPPORTED:
643 return ENOTSUP;
644 case KERN_INVALID_ARGUMENT:
645 return EINVAL;
646 case KERN_RESOURCE_SHORTAGE:
647 default:
648 return EBUSY;
649 }
39037602
A
650}
651
652int
653abort_with_payload(struct proc *cur_proc, struct abort_with_payload_args *args,
654 __unused void *retval)
655{
5ba3f43e
A
656 abort_with_payload_internal(cur_proc, args->reason_namespace,
657 args->reason_code, args->payload, args->payload_size,
658 args->reason_string, args->reason_flags, 0);
39037602
A
659
660 return 0;
3e170ce0
A
661}
662
5ba3f43e
A
663int
664os_fault_with_payload(struct proc *cur_proc,
665 struct os_fault_with_payload_args *args, __unused int *retval)
666{
667 return abort_with_payload_internal(cur_proc, args->reason_namespace,
668 args->reason_code, args->payload, args->payload_size,
669 args->reason_string, args->reason_flags, OS_REASON_IFLAG_USER_FAULT);
670}
671
39037602 672
1c79356b
A
673/*
674 * exit --
675 * Death of process.
676 */
39037602 677__attribute__((noreturn))
1c79356b 678void
2d21ac55 679exit(proc_t p, struct exit_args *uap, int *retval)
1c79356b 680{
5ba3f43e 681 p->p_xhighbits = ((uint32_t)(uap->rval) & 0xFF000000) >> 24;
0b4e3aa0 682 exit1(p, W_EXITCODE(uap->rval, 0), retval);
1c79356b 683
1c79356b
A
684 thread_exception_return();
685 /* NOTREACHED */
686 while (TRUE)
9bccf70c 687 thread_block(THREAD_CONTINUE_NULL);
1c79356b
A
688 /* NOTREACHED */
689}
690
691/*
692 * Exit: deallocate address space and other resources, change proc state
693 * to zombie, and unlink proc from allproc and parent's lists. Save exit
694 * status and rusage for wait(). Check for child processes and orphan them.
695 */
0b4e3aa0 696int
2d21ac55 697exit1(proc_t p, int rv, int *retval)
316670eb 698{
39236c6e 699 return exit1_internal(p, rv, retval, TRUE, TRUE, 0);
316670eb
A
700}
701
702int
39236c6e
A
703exit1_internal(proc_t p, int rv, int *retval, boolean_t thread_can_terminate, boolean_t perf_notify,
704 int jetsam_flags)
39037602
A
705{
706 return exit_with_reason(p, rv, retval, thread_can_terminate, perf_notify, jetsam_flags, OS_REASON_NULL);
707}
708
709/*
710 * NOTE: exit_with_reason drops a reference on the passed exit_reason
711 */
712int
713exit_with_reason(proc_t p, int rv, int *retval, boolean_t thread_can_terminate, boolean_t perf_notify,
714 int jetsam_flags, struct os_reason *exit_reason)
1c79356b 715{
91447636 716 thread_t self = current_thread();
1c79356b 717 struct task *task = p->task;
1c79356b 718 struct uthread *ut;
316670eb 719 int error = 0;
1c79356b
A
720
721 /*
722 * If a thread in this task has already
723 * called exit(), then halt any others
724 * right here.
725 */
0b4e3aa0 726
55e303ae 727 ut = get_bsdthread_info(self);
813fb2f6
A
728 if ((p == current_proc()) &&
729 (ut->uu_flag & UT_VFORK)) {
39037602 730 os_reason_free(exit_reason);
316670eb
A
731 if (!thread_can_terminate) {
732 return EINVAL;
733 }
734
735 vfork_exit(p, rv);
736 vfork_return(p , retval, p->p_pid);
737 unix_syscall_return(0);
738 /* NOT REACHED */
0b4e3aa0 739 }
2d21ac55
A
740
741 /*
742 * The parameter list of audit_syscall_exit() was augmented to
743 * take the Darwin syscall number as the first parameter,
744 * which is currently required by mac_audit_postselect().
745 */
746
b0d623f7
A
747 /*
748 * The BSM token contains two components: an exit status as passed
749 * to exit(), and a return value to indicate what sort of exit it
750 * was. The exit status is WEXITSTATUS(rv), but it's not clear
751 * what the return value is.
752 */
753 AUDIT_ARG(exit, WEXITSTATUS(rv), 0);
813fb2f6
A
754 /*
755 * TODO: what to audit here when jetsam calls exit and the uthread,
756 * 'ut' does not belong to the proc, 'p'.
757 */
2d21ac55
A
758 AUDIT_SYSCALL_EXIT(SYS_exit, p, ut, 0); /* Exit is always successfull */
759
760 DTRACE_PROC1(exit, int, CLD_EXITED);
761
316670eb 762 /* mark process is going to exit and pull out of DBG/disk throttle */
39236c6e 763 /* TODO: This should be done after becoming exit thread */
39037602 764 proc_set_task_policy(p->task, TASK_POLICY_ATTRIBUTE,
39236c6e 765 TASK_POLICY_TERMINATED, TASK_POLICY_ENABLE);
316670eb 766
2d21ac55 767 proc_lock(p);
39037602 768 error = proc_transstart(p, 1, (jetsam_flags ? 1 : 0));
316670eb 769 if (error == EDEADLK) {
39037602
A
770 /*
771 * If proc_transstart() returns EDEADLK, then another thread
772 * is either exec'ing or exiting. Return an error and allow
773 * the other thread to continue.
316670eb
A
774 */
775 proc_unlock(p);
39037602 776 os_reason_free(exit_reason);
316670eb 777 if (current_proc() == p){
39037602 778 if (p->exit_thread == self) {
39236c6e
A
779 printf("exit_thread failed to exit, leaving process %s[%d] in unkillable limbo\n",
780 p->p_comm, p->p_pid);
39037602
A
781 }
782
783 if (thread_can_terminate) {
784 thread_exception_return();
785 }
316670eb 786 }
39037602
A
787
788 return error;
316670eb
A
789 }
790
1c79356b
A
791 while (p->exit_thread != self) {
792 if (sig_try_locked(p) <= 0) {
6d2010ae 793 proc_transend(p, 1);
39037602
A
794 os_reason_free(exit_reason);
795
55e303ae 796 if (get_threadtask(self) != task) {
2d21ac55 797 proc_unlock(p);
0b4e3aa0 798 return(0);
1c79356b 799 }
2d21ac55 800 proc_unlock(p);
316670eb 801
55e303ae 802 thread_terminate(self);
316670eb
A
803 if (!thread_can_terminate) {
804 return 0;
805 }
806
1c79356b
A
807 thread_exception_return();
808 /* NOTREACHED */
809 }
810 sig_lock_to_exit(p);
811 }
3e170ce0 812
39037602
A
813 if (exit_reason != OS_REASON_NULL) {
814 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC, BSD_PROC_EXITREASON_COMMIT) | DBG_FUNC_NONE,
815 p->p_pid, exit_reason->osr_namespace,
816 exit_reason->osr_code, 0, 0);
1c79356b 817 }
39037602
A
818
819 assert(p->p_exit_reason == OS_REASON_NULL);
820 p->p_exit_reason = exit_reason;
821
2d21ac55 822 p->p_lflag |= P_LEXIT;
1c79356b 823 p->p_xstat = rv;
39236c6e 824 p->p_lflag |= jetsam_flags;
1c79356b 825
6d2010ae 826 proc_transend(p, 1);
2d21ac55
A
827 proc_unlock(p);
828
316670eb 829 proc_prepareexit(p, rv, perf_notify);
2d21ac55 830
6d2010ae 831 /* Last thread to terminate will call proc_exit() */
1c79356b
A
832 task_terminate_internal(task);
833
0b4e3aa0 834 return(0);
1c79356b
A
835}
836
837void
316670eb 838proc_prepareexit(proc_t p, int rv, boolean_t perf_notify)
1c79356b 839{
3e170ce0
A
840 mach_exception_data_type_t code = 0, subcode = 0;
841
1c79356b 842 struct uthread *ut;
91447636 843 thread_t self = current_thread();
2d21ac55 844 ut = get_bsdthread_info(self);
39236c6e 845 struct rusage_superset *rup;
3e170ce0
A
846 int kr = 0;
847 int create_corpse = FALSE;
848
849 if (p == initproc) {
850 launchd_crashed_panic(p, rv);
851 /* NOTREACHED */
852 }
55e303ae 853
d190cdc3
A
854 /*
855 * Generate a corefile/crashlog if:
856 * The process doesn't have an exit reason that indicates no crash report should be created
857 * AND any of the following are true:
858 * - The process was terminated due to a fatal signal that generates a core
859 * - The process was killed due to a code signing violation
860 * - The process has an exit reason that indicates we should generate a crash report
861 *
862 * The first condition is necessary because abort_with_reason()/payload() use SIGABRT
863 * (which normally triggers a core) but may indicate that no crash report should be created.
864 */
865 if (!(PROC_HAS_EXITREASON(p) && (PROC_EXITREASON_FLAGS(p) & OS_REASON_FLAG_NO_CRASH_REPORT)) &&
866 (hassigprop(WTERMSIG(rv), SA_CORE) || ((p->p_csflags & CS_KILLED) != 0) ||
867 (PROC_HAS_EXITREASON(p) && (PROC_EXITREASON_FLAGS(p) &
868 OS_REASON_FLAG_GENERATE_CRASH_REPORT)))) {
2d21ac55
A
869 /*
870 * Workaround for processes checking up on PT_DENY_ATTACH:
871 * should be backed out post-Leopard (details in 5431025).
872 */
873 if ((SIGSEGV == WTERMSIG(rv)) &&
874 (p->p_pptr->p_lflag & P_LNOATTACH)) {
875 goto skipcheck;
876 }
877
878 /*
879 * Crash Reporter looks for the signal value, original exception
880 * type, and low 20 bits of the original code in code[0]
881 * (8, 4, and 20 bits respectively). code[1] is unmodified.
882 */
883 code = ((WTERMSIG(rv) & 0xff) << 24) |
884 ((ut->uu_exception & 0x0f) << 20) |
885 ((int)ut->uu_code & 0xfffff);
886 subcode = ut->uu_subcode;
3e170ce0
A
887
888 kr = task_exception_notify(EXC_CRASH, code, subcode);
889
890 /* Nobody handled EXC_CRASH?? remember to make corpse */
891 if (kr != 0) {
892 create_corpse = TRUE;
893 }
2d21ac55
A
894 }
895
896skipcheck:
316670eb
A
897 /* Notify the perf server? */
898 if (perf_notify) {
899 (void)sys_perf_notify(self, p->p_pid);
900 }
1c79356b 901
3e170ce0
A
902
903 /* stash the usage into corpse data if making_corpse == true */
904 if (create_corpse == TRUE) {
d190cdc3 905 kr = task_mark_corpse(p->task);
3e170ce0
A
906 if (kr != KERN_SUCCESS) {
907 if (kr == KERN_NO_SPACE) {
908 printf("Process[%d] has no vm space for corpse info.\n", p->p_pid);
909 } else if (kr == KERN_NOT_SUPPORTED) {
910 printf("Process[%d] was destined to be corpse. But corpse is disabled by config.\n", p->p_pid);
911 } else {
912 printf("Process[%d] crashed: %s. Too many corpses being created.\n", p->p_pid, p->p_comm);
913 }
914 create_corpse = FALSE;
3e170ce0
A
915 }
916 }
917
39236c6e
A
918 /*
919 * Before this process becomes a zombie, stash resource usage
920 * stats in the proc for external observers to query
921 * via proc_pid_rusage().
922 *
923 * If the zombie allocation fails, just punt the stats.
924 */
925 MALLOC_ZONE(rup, struct rusage_superset *,
926 sizeof (*rup), M_ZOMBIE, M_WAITOK);
927 if (rup != NULL) {
fe8ab488 928 gather_rusage_info(p, &rup->ri, RUSAGE_INFO_CURRENT);
39236c6e
A
929 rup->ri.ri_phys_footprint = 0;
930 rup->ri.ri_proc_exit_abstime = mach_absolute_time();
931
932 /*
933 * Make the rusage_info visible to external observers
934 * only after it has been completely filled in.
935 */
936 p->p_ru = rup;
937 }
3e170ce0 938 if (create_corpse) {
39037602
A
939 int est_knotes = 0, num_knotes = 0;
940 uint64_t *buffer = NULL;
941 int buf_size = 0;
942
943 /* Get all the udata pointers from kqueue */
5ba3f43e 944 est_knotes = kevent_proc_copy_uptrs(p, NULL, 0);
39037602
A
945 if (est_knotes > 0) {
946 buf_size = (est_knotes + 32) * sizeof(uint64_t);
947 buffer = (uint64_t *) kalloc(buf_size);
5ba3f43e 948 num_knotes = kevent_proc_copy_uptrs(p, buffer, buf_size);
39037602
A
949 if (num_knotes > est_knotes + 32) {
950 num_knotes = est_knotes + 32;
951 }
952 }
953
954 /* Update the code, subcode based on exit reason */
955 proc_update_corpse_exception_codes(p, &code, &subcode);
5ba3f43e
A
956 populate_corpse_crashinfo(p, p->task, rup,
957 code, subcode, buffer, num_knotes, NULL);
39037602
A
958 if (buffer != NULL) {
959 kfree(buffer, buf_size);
960 }
3e170ce0 961 }
1c79356b
A
962 /*
963 * Remove proc from allproc queue and from pidhash chain.
964 * Need to do this before we do anything that can block.
965 * Not doing causes things like mount() find this on allproc
966 * in partially cleaned state.
967 */
2d21ac55
A
968
969 proc_list_lock();
970
39236c6e
A
971#if CONFIG_MEMORYSTATUS
972 memorystatus_remove(p, TRUE);
973#endif
974
1c79356b 975 LIST_REMOVE(p, p_list);
55e303ae 976 LIST_INSERT_HEAD(&zombproc, p, p_list); /* Place onto zombproc. */
2d21ac55
A
977 /* will not be visible via proc_find */
978 p->p_listflag |= P_LIST_EXITED;
979
980 proc_list_unlock();
981
1c79356b
A
982
983#ifdef PGINPROF
984 vmsizmon();
985#endif
986 /*
987 * If parent is waiting for us to exit or exec,
2d21ac55 988 * P_LPPWAIT is set; we will wakeup the parent below.
1c79356b 989 */
2d21ac55
A
990 proc_lock(p);
991 p->p_lflag &= ~(P_LTRACED | P_LPPWAIT);
91447636 992 p->p_sigignore = ~(sigcantmask);
9bccf70c 993 ut->uu_siglist = 0;
2d21ac55 994 proc_unlock(p);
1c79356b
A
995}
996
997void
2d21ac55 998proc_exit(proc_t p)
1c79356b 999{
2d21ac55
A
1000 proc_t q;
1001 proc_t pp;
1c79356b 1002 struct task *task = p->task;
2d21ac55
A
1003 vnode_t tvp = NULLVP;
1004 struct pgrp * pg;
1005 struct session *sessp;
1006 struct uthread * uth;
b0d623f7
A
1007 pid_t pid;
1008 int exitval;
99c3a104 1009 int knote_hint;
1c79356b 1010
fe8ab488 1011 uth = current_uthread();
2d21ac55
A
1012
1013 proc_lock(p);
fe8ab488 1014 proc_transstart(p, 1, 0);
2d21ac55 1015 if( !(p->p_lflag & P_LEXIT)) {
6d2010ae
A
1016 /*
1017 * This can happen if a thread_terminate() occurs
1018 * in a single-threaded process.
1019 */
2d21ac55 1020 p->p_lflag |= P_LEXIT;
6d2010ae 1021 proc_transend(p, 1);
2d21ac55 1022 proc_unlock(p);
316670eb 1023 proc_prepareexit(p, 0, TRUE);
6d2010ae 1024 (void) task_terminate_internal(task);
2d21ac55 1025 proc_lock(p);
6d2010ae
A
1026 } else {
1027 proc_transend(p, 1);
1c79356b
A
1028 }
1029
91447636 1030 p->p_lflag |= P_LPEXIT;
6d2010ae
A
1031
1032 /*
1033 * Other kernel threads may be in the middle of signalling this process.
1034 * Wait for those threads to wrap it up before making the process
1035 * disappear on them.
1036 */
1037 if ((p->p_lflag & P_LINSIGNAL) || (p->p_sigwaitcnt > 0)) {
1038 p->p_sigwaitcnt++;
1039 while ((p->p_lflag & P_LINSIGNAL) || (p->p_sigwaitcnt > 1))
1040 msleep(&p->p_sigmask, &p->p_mlock, PWAIT, "proc_sigdrain", NULL);
1041 p->p_sigwaitcnt--;
1042 }
1043
2d21ac55 1044 proc_unlock(p);
b0d623f7
A
1045 pid = p->p_pid;
1046 exitval = p->p_xstat;
316670eb
A
1047 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_COMMON,
1048 BSDDBG_CODE(DBG_BSD_PROC, BSD_PROC_EXIT) | DBG_FUNC_START,
1049 pid, exitval, 0, 0, 0);
2d21ac55
A
1050
1051#if CONFIG_DTRACE
39037602 1052 dtrace_proc_exit(p);
2d21ac55
A
1053#endif
1054
6d2010ae
A
1055 nspace_proc_exit(p);
1056
55e303ae
A
1057 /*
1058 * need to cancel async IO requests that can be cancelled and wait for those
1059 * already active. MAY BLOCK!
1060 */
ff6e181a 1061
2d21ac55
A
1062 proc_refdrain(p);
1063
316670eb 1064 /* if any pending cpu limits action, clear it */
39236c6e 1065 task_clear_cpuusage(p->task, TRUE);
316670eb 1066
13f56ec4 1067 workqueue_mark_exiting(p);
ff6e181a 1068
55e303ae
A
1069 _aio_exit( p );
1070
1c79356b
A
1071 /*
1072 * Close open files and release open-file table.
1073 * This may block!
1074 */
1075 fdfree(p);
1076
5ba3f43e
A
1077 /*
1078 * Once all the knotes, kqueues & workloops are destroyed, get rid of the
1079 * workqueue.
1080 */
1081 workqueue_exit(p);
1082
593a1d5f
A
1083 if (uth->uu_lowpri_window) {
1084 /*
1085 * task is marked as a low priority I/O type
b0d623f7 1086 * and the I/O we issued while in flushing files on close
593a1d5f
A
1087 * collided with normal I/O operations...
1088 * no need to throttle this thread since its going away
1089 * but we do need to update our bookeeping w/r to throttled threads
1090 */
39236c6e 1091 throttle_lowpri_io(0);
316670eb 1092 }
316670eb 1093
2d21ac55 1094#if SYSV_SHM
1c79356b
A
1095 /* Close ref SYSV Shared memory*/
1096 if (p->vm_shm)
1097 shmexit(p);
2d21ac55
A
1098#endif
1099#if SYSV_SEM
9bccf70c
A
1100 /* Release SYSV semaphores */
1101 semexit(p);
2d21ac55 1102#endif
1c79356b 1103
b0d623f7
A
1104#if PSYNCH
1105 pth_proc_hashdelete(p);
1106#endif /* PSYNCH */
1107
2d21ac55
A
1108 sessp = proc_session(p);
1109 if (SESS_LEADER(p, sessp)) {
1110
2d21ac55 1111 if (sessp->s_ttyvp != NULLVP) {
fa4905b1 1112 struct vnode *ttyvp;
2d21ac55 1113 int ttyvid;
39236c6e 1114 int cttyflag = 0;
91447636 1115 struct vfs_context context;
316670eb 1116 struct tty *tp;
fa4905b1 1117
1c79356b
A
1118 /*
1119 * Controlling process.
1120 * Signal foreground pgrp,
1121 * drain controlling terminal
1122 * and revoke access to controlling terminal.
1123 */
316670eb 1124 session_lock(sessp);
b0d623f7 1125 tp = SESSION_TP(sessp);
2d21ac55 1126 if ((tp != TTY_NULL) && (tp->t_session == sessp)) {
2d21ac55
A
1127 session_unlock(sessp);
1128
fe8ab488
A
1129 /*
1130 * We're going to SIGHUP the foreground process
1131 * group. It can't change from this point on
1132 * until the revoke is complete.
1133 * The process group changes under both the tty
1134 * lock and proc_list_lock but we need only one
1135 */
1136 tty_lock(tp);
1137 ttysetpgrphup(tp);
1138 tty_unlock(tp);
1139
316670eb 1140 tty_pgsignal(tp, SIGHUP, 1);
b0d623f7 1141
2d21ac55 1142 session_lock(sessp);
b0d623f7 1143 tp = SESSION_TP(sessp);
316670eb 1144 }
39236c6e
A
1145 cttyflag = sessp->s_flags & S_CTTYREF;
1146 sessp->s_flags &= ~S_CTTYREF;
316670eb
A
1147 ttyvp = sessp->s_ttyvp;
1148 ttyvid = sessp->s_ttyvid;
1149 sessp->s_ttyvp = NULLVP;
1150 sessp->s_ttyvid = 0;
1151 sessp->s_ttyp = TTY_NULL;
1152 sessp->s_ttypgrpid = NO_PID;
1153 session_unlock(sessp);
1154
1155 if ((ttyvp != NULLVP) && (vnode_getwithvid(ttyvp, ttyvid) == 0)) {
1156 if (tp != TTY_NULL) {
1157 tty_lock(tp);
1158 (void) ttywait(tp);
1159 tty_unlock(tp);
1160 }
1161 context.vc_thread = proc_thread(p); /* XXX */
1162 context.vc_ucred = kauth_cred_proc_ref(p);
316670eb 1163 VNOP_REVOKE(ttyvp, REVOKEALL, &context);
39236c6e
A
1164 if (cttyflag) {
1165 /*
1166 * Release the extra usecount taken in cttyopen.
1167 * usecount should be released after VNOP_REVOKE is called.
fe8ab488
A
1168 * This usecount was taken to ensure that
1169 * the VNOP_REVOKE results in a close to
1170 * the tty since cttyclose is a no-op.
39236c6e
A
1171 */
1172 vnode_rele(ttyvp);
1173 }
316670eb
A
1174 vnode_put(ttyvp);
1175 kauth_cred_unref(&context.vc_ucred);
1176 ttyvp = NULLVP;
1c79356b 1177 }
fe8ab488
A
1178 if (tp) {
1179 /*
1180 * This is cleared even if not set. This is also done in
1181 * spec_close to ensure that the flag is cleared.
1182 */
1183 tty_lock(tp);
1184 ttyclrpgrphup(tp);
1185 tty_unlock(tp);
1186
316670eb 1187 ttyfree(tp);
fe8ab488 1188 }
1c79356b 1189 }
2d21ac55
A
1190 session_lock(sessp);
1191 sessp->s_leader = NULL;
1192 session_unlock(sessp);
1c79356b 1193 }
2d21ac55
A
1194 session_rele(sessp);
1195
1196 pg = proc_pgrp(p);
1197 fixjobc(p, pg, 0);
1198 pg_rele(pg);
1c79356b 1199
1c79356b 1200 p->p_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
9bccf70c
A
1201 (void)acct_process(p);
1202
2d21ac55 1203 proc_list_lock();
b0d623f7
A
1204
1205 if ((p->p_listflag & P_LIST_EXITCOUNT) == P_LIST_EXITCOUNT) {
1206 p->p_listflag &= ~P_LIST_EXITCOUNT;
1207 proc_shutdown_exitcount--;
1208 if (proc_shutdown_exitcount == 0)
1209 wakeup(&proc_shutdown_exitcount);
1210 }
1211
2d21ac55
A
1212 /* wait till parentrefs are dropped and grant no more */
1213 proc_childdrainstart(p);
1214 while ((q = p->p_children.lh_first) != NULL) {
4b17d6b6 1215 int reparentedtoinit = (q->p_listflag & P_LIST_DEADPARENT) ? 1 : 0;
2d21ac55
A
1216 if (q->p_stat == SZOMB) {
1217 if (p != q->p_pptr)
1218 panic("parent child linkage broken");
1219 /* check for sysctl zomb lookup */
1220 while ((q->p_listflag & P_LIST_WAITING) == P_LIST_WAITING) {
1221 msleep(&q->p_stat, proc_list_mlock, PWAIT, "waitcoll", 0);
1222 }
1223 q->p_listflag |= P_LIST_WAITING;
1224 /*
1225 * This is a named reference and it is not granted
1226 * if the reap is already in progress. So we get
1227 * the reference here exclusively and their can be
1228 * no waiters. So there is no need for a wakeup
6d2010ae 1229 * after we are done. Also the reap frees the structure
2d21ac55
A
1230 * and the proc struct cannot be used for wakeups as well.
1231 * It is safe to use q here as this is system reap
1232 */
4b17d6b6 1233 (void)reap_child_locked(p, q, 1, reparentedtoinit, 1, 0);
2d21ac55 1234 } else {
2d21ac55
A
1235 /*
1236 * Traced processes are killed
1237 * since their existence means someone is messing up.
1238 */
1239 if (q->p_lflag & P_LTRACED) {
39236c6e
A
1240 struct proc *opp;
1241
6d2010ae
A
1242 /*
1243 * Take a reference on the child process to
1244 * ensure it doesn't exit and disappear between
1245 * the time we drop the list_lock and attempt
1246 * to acquire its proc_lock.
1247 */
1248 if (proc_ref_locked(q) != q)
1249 continue;
1250
2d21ac55 1251 proc_list_unlock();
39236c6e
A
1252
1253 opp = proc_find(q->p_oppid);
1254 if (opp != PROC_NULL) {
1255 proc_list_lock();
1256 q->p_oppid = 0;
1257 proc_list_unlock();
1258 proc_reparentlocked(q, opp, 0, 0);
1259 proc_rele(opp);
1260 } else {
1261 /* original parent exited while traced */
1262 proc_list_lock();
1263 q->p_listflag |= P_LIST_DEADPARENT;
1264 q->p_oppid = 0;
1265 proc_list_unlock();
1266 proc_reparentlocked(q, initproc, 0, 0);
1267 }
1268
2d21ac55
A
1269 proc_lock(q);
1270 q->p_lflag &= ~P_LTRACED;
39236c6e 1271
2d21ac55 1272 if (q->sigwait_thread) {
6d2010ae
A
1273 thread_t thread = q->sigwait_thread;
1274
2d21ac55
A
1275 proc_unlock(q);
1276 /*
1277 * The sigwait_thread could be stopped at a
1278 * breakpoint. Wake it up to kill.
1279 * Need to do this as it could be a thread which is not
1280 * the first thread in the task. So any attempts to kill
1281 * the process would result into a deadlock on q->sigwait.
1282 */
6d2010ae
A
1283 thread_resume(thread);
1284 clear_wait(thread, THREAD_INTERRUPTED);
39037602 1285 threadsignal(thread, SIGKILL, 0, TRUE);
6d2010ae 1286 } else {
2d21ac55 1287 proc_unlock(q);
6d2010ae
A
1288 }
1289
2d21ac55
A
1290 psignal(q, SIGKILL);
1291 proc_list_lock();
6d2010ae 1292 proc_rele_locked(q);
39236c6e
A
1293 } else {
1294 q->p_listflag |= P_LIST_DEADPARENT;
1295 proc_reparentlocked(q, initproc, 0, 1);
1c79356b 1296 }
1c79356b
A
1297 }
1298 }
1299
2d21ac55
A
1300 proc_childdrainend(p);
1301 proc_list_unlock();
1302
5ba3f43e
A
1303#if CONFIG_MACF
1304 /*
1305 * Notify MAC policies that proc is dead.
1306 * This should be replaced with proper label management
1307 * (rdar://problem/32126399).
1308 */
1309 mac_proc_notify_exit(p);
1310#endif
1311
2d21ac55
A
1312 /*
1313 * Release reference to text vnode
1314 */
1315 tvp = p->p_textvp;
1316 p->p_textvp = NULL;
1317 if (tvp != NULLVP) {
1318 vnode_rele(tvp);
1319 }
1320
1c79356b
A
1321 /*
1322 * Save exit status and final rusage info, adding in child rusage
91447636
A
1323 * info and self times. If we were unable to allocate a zombie
1324 * structure, this information is lost.
1c79356b 1325 */
91447636 1326 if (p->p_ru != NULL) {
6d2010ae 1327 calcru(p, &p->p_stats->p_ru.ru_utime, &p->p_stats->p_ru.ru_stime, NULL);
39236c6e 1328 p->p_ru->ru = p->p_stats->p_ru;
1c79356b 1329
39236c6e 1330 ruadd(&(p->p_ru->ru), &p->p_stats->p_cru);
91447636 1331 }
1c79356b
A
1332
1333 /*
1334 * Free up profiling buffers.
1335 */
1336 {
1337 struct uprof *p0 = &p->p_stats->p_prof, *p1, *pn;
1338
1339 p1 = p0->pr_next;
1340 p0->pr_next = NULL;
1341 p0->pr_scale = 0;
1342
1343 for (; p1 != NULL; p1 = pn) {
1344 pn = p1->pr_next;
91447636 1345 kfree(p1, sizeof *p1);
1c79356b
A
1346 }
1347 }
1348
39037602 1349 proc_free_realitimer(p);
2d21ac55 1350
1c79356b
A
1351 /*
1352 * Other substructures are freed from wait().
1353 */
2d21ac55 1354 FREE_ZONE(p->p_stats, sizeof *p->p_stats, M_PSTATS);
1c79356b
A
1355 p->p_stats = NULL;
1356
2d21ac55 1357 FREE_ZONE(p->p_sigacts, sizeof *p->p_sigacts, M_SIGACTS);
1c79356b
A
1358 p->p_sigacts = NULL;
1359
2d21ac55 1360 proc_limitdrop(p, 1);
1c79356b
A
1361 p->p_limit = NULL;
1362
fe8ab488 1363 vm_purgeable_disown(p->task);
2d21ac55 1364
1c79356b
A
1365 /*
1366 * Finish up by terminating the task
1367 * and halt this thread (only if a
1368 * member of the task exiting).
1369 */
1370 p->task = TASK_NULL;
1c79356b
A
1371 set_bsdtask_info(task, NULL);
1372
99c3a104 1373 knote_hint = NOTE_EXIT | (p->p_xstat & 0xffff);
99c3a104 1374 proc_knote(p, knote_hint);
55e303ae 1375
2d21ac55
A
1376 /* mark the thread as the one that is doing proc_exit
1377 * no need to hold proc lock in uthread_free
1378 */
1379 uth->uu_flag |= UT_PROCEXIT;
1c79356b
A
1380 /*
1381 * Notify parent that we're gone.
1382 */
2d21ac55
A
1383 pp = proc_parent(p);
1384 if (pp->p_flag & P_NOCLDWAIT) {
9bccf70c 1385
39236c6e
A
1386 if (p->p_ru != NULL) {
1387 proc_lock(pp);
2d21ac55
A
1388#if 3839178
1389 /*
1390 * If the parent is ignoring SIGCHLD, then POSIX requires
1391 * us to not add the resource usage to the parent process -
1392 * we are only going to hand it off to init to get reaped.
1393 * We should contest the standard in this case on the basis
1394 * of RLIMIT_CPU.
1395 */
1396#else /* !3839178 */
55e303ae
A
1397 /*
1398 * Add child resource usage to parent before giving
91447636
A
1399 * zombie to init. If we were unable to allocate a
1400 * zombie structure, this information is lost.
55e303ae 1401 */
39236c6e
A
1402 ruadd(&pp->p_stats->p_cru, &p->p_ru->ru);
1403#endif /* !3839178 */
1404 update_rusage_info_child(&pp->p_stats->ri_child, &p->p_ru->ri);
2d21ac55
A
1405 proc_unlock(pp);
1406 }
39236c6e 1407
2d21ac55
A
1408 /* kernel can reap this one, no need to move it to launchd */
1409 proc_list_lock();
1410 p->p_listflag |= P_LIST_DEADPARENT;
1411 proc_list_unlock();
9bccf70c 1412 }
4b17d6b6 1413 if ((p->p_listflag & P_LIST_DEADPARENT) == 0 || p->p_oppid) {
2d21ac55
A
1414 if (pp != initproc) {
1415 proc_lock(pp);
1416 pp->si_pid = p->p_pid;
5ba3f43e
A
1417 pp->p_xhighbits = p->p_xhighbits;
1418 p->p_xhighbits = 0;
2d21ac55
A
1419 pp->si_status = p->p_xstat;
1420 pp->si_code = CLD_EXITED;
1421 /*
1422 * p_ucred usage is safe as it is an exiting process
1423 * and reference is dropped in reap
1424 */
6d2010ae 1425 pp->si_uid = kauth_cred_getruid(p->p_ucred);
2d21ac55
A
1426 proc_unlock(pp);
1427 }
1428 /* mark as a zombie */
1429 /* No need to take proc lock as all refs are drained and
1430 * no one except parent (reaping ) can look at this.
1431 * The write is to an int and is coherent. Also parent is
1432 * keyed off of list lock for reaping
1433 */
316670eb
A
1434 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_COMMON,
1435 BSDDBG_CODE(DBG_BSD_PROC, BSD_PROC_EXIT) | DBG_FUNC_END,
1436 pid, exitval, 0, 0, 0);
2d21ac55
A
1437 p->p_stat = SZOMB;
1438 /*
1439 * The current process can be reaped so, no one
1440 * can depend on this
1441 */
1c79356b 1442
2d21ac55
A
1443 psignal(pp, SIGCHLD);
1444
1445 /* and now wakeup the parent */
1446 proc_list_lock();
1447 wakeup((caddr_t)pp);
1448 proc_list_unlock();
1449 } else {
1450 /* should be fine as parent proc would be initproc */
1451 /* mark as a zombie */
1452 /* No need to take proc lock as all refs are drained and
1453 * no one except parent (reaping ) can look at this.
1454 * The write is to an int and is coherent. Also parent is
1455 * keyed off of list lock for reaping
1456 */
1457 proc_list_lock();
316670eb
A
1458 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_COMMON,
1459 BSDDBG_CODE(DBG_BSD_PROC, BSD_PROC_EXIT) | DBG_FUNC_END,
1460 pid, exitval, 0, 0, 0);
2d21ac55
A
1461 /* check for sysctl zomb lookup */
1462 while ((p->p_listflag & P_LIST_WAITING) == P_LIST_WAITING) {
1463 msleep(&p->p_stat, proc_list_mlock, PWAIT, "waitcoll", 0);
1464 }
1465 /* safe to use p as this is a system reap */
d1ecb069 1466 p->p_stat = SZOMB;
2d21ac55 1467 p->p_listflag |= P_LIST_WAITING;
d1ecb069 1468
2d21ac55
A
1469 /*
1470 * This is a named reference and it is not granted
1471 * if the reap is already in progress. So we get
1472 * the reference here exclusively and their can be
1473 * no waiters. So there is no need for a wakeup
1474 * after we are done. AlsO the reap frees the structure
1475 * and the proc struct cannot be used for wakeups as well.
1476 * It is safe to use p here as this is system reap
1477 */
4b17d6b6 1478 (void)reap_child_locked(pp, p, 1, 0, 1, 1);
2d21ac55
A
1479 /* list lock dropped by reap_child_locked */
1480 }
593a1d5f
A
1481 if (uth->uu_lowpri_window) {
1482 /*
1483 * task is marked as a low priority I/O type and we've
1484 * somehow picked up another throttle during exit processing...
1485 * no need to throttle this thread since its going away
1486 * but we do need to update our bookeeping w/r to throttled threads
1487 */
39236c6e 1488 throttle_lowpri_io(0);
593a1d5f 1489 }
91447636 1490
2d21ac55 1491 proc_rele(pp);
1c79356b 1492
1c79356b
A
1493}
1494
1495
91447636 1496/*
2d21ac55 1497 * reap_child_locked
91447636
A
1498 *
1499 * Description: Given a process from which all status information needed
1500 * has already been extracted, if the process is a ptrace
1501 * attach process, detach it and give it back to its real
1502 * parent, else recover all resources remaining associated
1503 * with it.
1504 *
2d21ac55
A
1505 * Parameters: proc_t parent Parent of process being reaped
1506 * proc_t child Process to reap
91447636
A
1507 *
1508 * Returns: 0 Process was not reaped because it
1509 * came from an attach
1510 * 1 Process was reaped
1511 */
1512static int
4b17d6b6 1513reap_child_locked(proc_t parent, proc_t child, int deadparent, int reparentedtoinit, int locked, int droplock)
1c79356b 1514{
b0d623f7 1515 proc_t trace_parent = PROC_NULL; /* Traced parent process, if tracing */
1c79356b 1516
b0d623f7
A
1517 if (locked == 1)
1518 proc_list_unlock();
1519
91447636
A
1520 /*
1521 * If we got the child via a ptrace 'attach',
1522 * we need to give it back to the old parent.
b0d623f7
A
1523 *
1524 * Exception: someone who has been reparented to launchd before being
1525 * ptraced can simply be reaped, refer to radar 5677288
1526 * p_oppid -> ptraced
1527 * trace_parent == initproc -> away from launchd
4b17d6b6 1528 * reparentedtoinit -> came to launchd by reparenting
91447636 1529 */
4b17d6b6
A
1530 if (child->p_oppid) {
1531 int knote_hint;
1532 pid_t oppid;
1533
2d21ac55 1534 proc_lock(child);
4b17d6b6 1535 oppid = child->p_oppid;
91447636 1536 child->p_oppid = 0;
4b17d6b6 1537 knote_hint = NOTE_EXIT | (child->p_xstat & 0xffff);
2d21ac55 1538 proc_unlock(child);
4b17d6b6
A
1539
1540 if ((trace_parent = proc_find(oppid))
1541 && !((trace_parent == initproc) && reparentedtoinit)) {
1542
1543 if (trace_parent != initproc) {
1544 /*
1545 * proc internal fileds and p_ucred usage safe
1546 * here as child is dead and is not reaped or
1547 * reparented yet
1548 */
1549 proc_lock(trace_parent);
1550 trace_parent->si_pid = child->p_pid;
1551 trace_parent->si_status = child->p_xstat;
1552 trace_parent->si_code = CLD_CONTINUED;
1553 trace_parent->si_uid = kauth_cred_getruid(child->p_ucred);
1554 proc_unlock(trace_parent);
1555 }
1556 proc_reparentlocked(child, trace_parent, 1, 0);
1557
1558 /* resend knote to original parent (and others) after reparenting */
1559 proc_knote(child, knote_hint);
1560
1561 psignal(trace_parent, SIGCHLD);
2d21ac55 1562 proc_list_lock();
4b17d6b6
A
1563 wakeup((caddr_t)trace_parent);
1564 child->p_listflag &= ~P_LIST_WAITING;
1565 wakeup(&child->p_stat);
1566 proc_list_unlock();
1567 proc_rele(trace_parent);
1568 if ((locked == 1) && (droplock == 0))
1569 proc_list_lock();
1570 return (0);
1571 }
1572
1573 /*
1574 * If we can't reparent (e.g. the original parent exited while child was being debugged, or
1575 * original parent is the same as the debugger currently exiting), we still need to satisfy
1576 * the knote lifecycle for other observers on the system. While the debugger was attached,
1577 * the NOTE_EXIT would not have been broadcast during initial child termination.
1578 */
1579 proc_knote(child, knote_hint);
1580
1581 if (trace_parent != PROC_NULL) {
1582 proc_rele(trace_parent);
1583 }
b0d623f7
A
1584 }
1585
39236c6e
A
1586#pragma clang diagnostic push
1587#pragma clang diagnostic ignored "-Wdeprecated-declarations"
2d21ac55 1588 proc_knote(child, NOTE_REAP);
39236c6e
A
1589#pragma clang diagnostic pop
1590
b0d623f7 1591 proc_knote_drain(child);
2d21ac55 1592
91447636
A
1593 child->p_xstat = 0;
1594 if (child->p_ru) {
2d21ac55
A
1595 proc_lock(parent);
1596#if 3839178
1597 /*
1598 * If the parent is ignoring SIGCHLD, then POSIX requires
1599 * us to not add the resource usage to the parent process -
1600 * we are only going to hand it off to init to get reaped.
1601 * We should contest the standard in this case on the basis
1602 * of RLIMIT_CPU.
1603 */
1604 if (!(parent->p_flag & P_NOCLDWAIT))
1605#endif /* 3839178 */
39236c6e
A
1606 ruadd(&parent->p_stats->p_cru, &child->p_ru->ru);
1607 update_rusage_info_child(&parent->p_stats->ri_child, &child->p_ru->ri);
2d21ac55 1608 proc_unlock(parent);
91447636
A
1609 FREE_ZONE(child->p_ru, sizeof *child->p_ru, M_ZOMBIE);
1610 child->p_ru = NULL;
1611 } else {
1612 printf("Warning : lost p_ru for %s\n", child->p_comm);
1613 }
1c79356b 1614
6d2010ae 1615 AUDIT_SESSION_PROCEXIT(child);
b0d623f7 1616
91447636
A
1617 /*
1618 * Decrement the count of procs running with this uid.
2d21ac55
A
1619 * p_ucred usage is safe here as it is an exited process.
1620 * and refernce is dropped after these calls down below
1621 * (locking protection is provided by list lock held in chgproccnt)
91447636 1622 */
490019cf
A
1623#if CONFIG_PERSONAS
1624 /*
1625 * persona_proc_drop calls chgproccnt(-1) on the persona uid,
1626 * and (+1) on the child->p_ucred uid
1627 */
1628 persona_proc_drop(child);
1629#endif
3e170ce0 1630 (void)chgproccnt(kauth_cred_getruid(child->p_ucred), -1);
2d21ac55 1631
39037602
A
1632 os_reason_free(child->p_exit_reason);
1633
91447636
A
1634 /*
1635 * Free up credentials.
1636 */
0c530ab8
A
1637 if (IS_VALID_CRED(child->p_ucred)) {
1638 kauth_cred_unref(&child->p_ucred);
1639 }
1c79356b 1640
2d21ac55
A
1641 /* XXXX Note NOT SAFE TO USE p_ucred from this point onwards */
1642
91447636
A
1643 /*
1644 * Finally finished with old proc entry.
1645 * Unlink it from its process group and free it.
1646 */
1647 leavepgrp(child);
2d21ac55
A
1648
1649 proc_list_lock();
91447636 1650 LIST_REMOVE(child, p_list); /* off zombproc */
2d21ac55 1651 parent->p_childrencnt--;
91447636 1652 LIST_REMOVE(child, p_sibling);
2d21ac55
A
1653 /* If there are no more children wakeup parent */
1654 if ((deadparent != 0) && (LIST_EMPTY(&parent->p_children)))
1655 wakeup((caddr_t)parent); /* with list lock held */
1656 child->p_listflag &= ~P_LIST_WAITING;
b36670ce 1657 wakeup(&child->p_stat);
1c79356b 1658
2d21ac55
A
1659 /* Take it out of process hash */
1660 LIST_REMOVE(child, p_hash);
1661 child->p_listflag &= ~P_LIST_INHASH;
1662 proc_checkdeadrefs(child);
1663 nprocs--;
1664
39236c6e
A
1665 if (deadparent) {
1666 /*
1667 * If a child zombie is being reaped because its parent
1668 * is exiting, make sure we update the list flag
1669 */
1670 child->p_listflag |= P_LIST_DEADPARENT;
1671 }
1672
2d21ac55
A
1673 proc_list_unlock();
1674
6d2010ae 1675#if CONFIG_FINE_LOCK_GROUPS
b0d623f7 1676 lck_mtx_destroy(&child->p_mlock, proc_mlock_grp);
ecc0ceb4 1677 lck_mtx_destroy(&child->p_ucred_mlock, proc_ucred_mlock_grp);
5ba3f43e 1678 lck_mtx_destroy(&child->p_fdmlock, proc_fdmlock_grp);
b0d623f7
A
1679#if CONFIG_DTRACE
1680 lck_mtx_destroy(&child->p_dtrace_sprlock, proc_lck_grp);
1681#endif
1682 lck_spin_destroy(&child->p_slock, proc_slock_grp);
6d2010ae
A
1683#else /* CONFIG_FINE_LOCK_GROUPS */
1684 lck_mtx_destroy(&child->p_mlock, proc_lck_grp);
ecc0ceb4 1685 lck_mtx_destroy(&child->p_ucred_mlock, proc_lck_grp);
5ba3f43e 1686 lck_mtx_destroy(&child->p_fdmlock, proc_lck_grp);
6d2010ae
A
1687#if CONFIG_DTRACE
1688 lck_mtx_destroy(&child->p_dtrace_sprlock, proc_lck_grp);
b0d623f7 1689#endif
6d2010ae
A
1690 lck_spin_destroy(&child->p_slock, proc_lck_grp);
1691#endif /* CONFIG_FINE_LOCK_GROUPS */
2d21ac55 1692
91447636 1693 FREE_ZONE(child, sizeof *child, M_PROC);
2d21ac55
A
1694 if ((locked == 1) && (droplock == 0))
1695 proc_list_lock();
1696
91447636 1697 return (1);
1c79356b
A
1698}
1699
1c79356b
A
1700
1701int
91447636 1702wait1continue(int result)
1c79356b 1703{
fe8ab488 1704 proc_t p;
91447636 1705 thread_t thread;
fe8ab488
A
1706 uthread_t uth;
1707 struct _wait4_data *wait4_data;
1708 struct wait4_nocancel_args *uap;
7b1edb79 1709 int *retval;
1c79356b 1710
7b1edb79
A
1711 if (result)
1712 return(result);
1c79356b 1713
7b1edb79 1714 p = current_proc();
91447636 1715 thread = current_thread();
fe8ab488
A
1716 uth = (struct uthread *)get_bsdthread_info(thread);
1717
1718 wait4_data = &uth->uu_kevent.uu_wait4_data;
1719 uap = wait4_data->args;
1720 retval = wait4_data->retval;
1721 return(wait4_nocancel(p, uap, retval));
1c79356b
A
1722}
1723
1724int
b0d623f7 1725wait4(proc_t q, struct wait4_args *uap, int32_t *retval)
1c79356b 1726{
2d21ac55
A
1727 __pthread_testcancel(1);
1728 return(wait4_nocancel(q, (struct wait4_nocancel_args *)uap, retval));
1729}
1730
1731int
b0d623f7 1732wait4_nocancel(proc_t q, struct wait4_nocancel_args *uap, int32_t *retval)
2d21ac55
A
1733{
1734 int nfound;
b0d623f7 1735 int sibling_count;
2d21ac55 1736 proc_t p;
1c79356b 1737 int status, error;
fe8ab488
A
1738 uthread_t uth;
1739 struct _wait4_data *wait4_data;
1c79356b 1740
b0d623f7
A
1741 AUDIT_ARG(pid, uap->pid);
1742
1c79356b 1743 if (uap->pid == 0)
2d21ac55 1744 uap->pid = -q->p_pgrpid;
1c79356b
A
1745
1746loop:
2d21ac55
A
1747 proc_list_lock();
1748loop1:
1c79356b 1749 nfound = 0;
b0d623f7
A
1750 sibling_count = 0;
1751
39037602 1752 PCHILDREN_FOREACH(q, p) {
b0d623f7
A
1753 if ( p->p_sibling.le_next != 0 )
1754 sibling_count++;
1c79356b
A
1755 if (uap->pid != WAIT_ANY &&
1756 p->p_pid != uap->pid &&
2d21ac55 1757 p->p_pgrpid != -(uap->pid))
1c79356b 1758 continue;
2d21ac55 1759
1c79356b 1760 nfound++;
91447636
A
1761
1762 /* XXX This is racy because we don't get the lock!!!! */
1763
2d21ac55
A
1764 if (p->p_listflag & P_LIST_WAITING) {
1765 (void)msleep(&p->p_stat, proc_list_mlock, PWAIT, "waitcoll", 0);
1766 goto loop1;
7b1edb79 1767 }
2d21ac55
A
1768 p->p_listflag |= P_LIST_WAITING; /* only allow single thread to wait() */
1769
7b1edb79 1770
1c79356b 1771 if (p->p_stat == SZOMB) {
4b17d6b6
A
1772 int reparentedtoinit = (p->p_listflag & P_LIST_DEADPARENT) ? 1 : 0;
1773
2d21ac55
A
1774 proc_list_unlock();
1775#if CONFIG_MACF
1776 if ((error = mac_proc_check_wait(q, p)) != 0)
1777 goto out;
1778#endif
1c79356b 1779 retval[0] = p->p_pid;
1c79356b 1780 if (uap->status) {
2d21ac55
A
1781 /* Legacy apps expect only 8 bits of status */
1782 status = 0xffff & p->p_xstat; /* convert to int */
91447636
A
1783 error = copyout((caddr_t)&status,
1784 uap->status,
1785 sizeof(status));
2d21ac55
A
1786 if (error)
1787 goto out;
1c79356b 1788 }
91447636
A
1789 if (uap->rusage) {
1790 if (p->p_ru == NULL) {
1791 error = ENOMEM;
1792 } else {
1793 if (IS_64BIT_PROCESS(q)) {
b0d623f7 1794 struct user64_rusage my_rusage;
39236c6e 1795 munge_user64_rusage(&p->p_ru->ru, &my_rusage);
91447636
A
1796 error = copyout((caddr_t)&my_rusage,
1797 uap->rusage,
1798 sizeof (my_rusage));
1799 }
1800 else {
b0d623f7 1801 struct user32_rusage my_rusage;
39236c6e 1802 munge_user32_rusage(&p->p_ru->ru, &my_rusage);
b0d623f7 1803 error = copyout((caddr_t)&my_rusage,
91447636 1804 uap->rusage,
b0d623f7 1805 sizeof (my_rusage));
91447636 1806 }
9bccf70c 1807 }
91447636 1808 /* information unavailable? */
2d21ac55
A
1809 if (error)
1810 goto out;
1c79356b
A
1811 }
1812
b0d623f7
A
1813 /* Conformance change for 6577252.
1814 * When SIGCHLD is blocked and wait() returns because the status
1815 * of a child process is available and there are no other
1816 * children processes, then any pending SIGCHLD signal is cleared.
1817 */
1818 if ( sibling_count == 0 ) {
1819 int mask = sigmask(SIGCHLD);
fe8ab488 1820 uth = current_uthread();
b0d623f7
A
1821
1822 if ( (uth->uu_sigmask & mask) != 0 ) {
1823 /* we are blocking SIGCHLD signals. clear any pending SIGCHLD.
1824 * This locking looks funny but it is protecting access to the
1825 * thread via p_uthlist.
1826 */
1827 proc_lock(q);
1828 uth->uu_siglist &= ~mask; /* clear pending signal */
1829 proc_unlock(q);
1830 }
b36670ce 1831 }
b0d623f7
A
1832
1833 /* Clean up */
4b17d6b6 1834 (void)reap_child_locked(q, p, 0, reparentedtoinit, 0, 0);
91447636 1835
1c79356b
A
1836 return (0);
1837 }
2d21ac55
A
1838 if (p->p_stat == SSTOP && (p->p_lflag & P_LWAITED) == 0 &&
1839 (p->p_lflag & P_LTRACED || uap->options & WUNTRACED)) {
1840 proc_list_unlock();
1841#if CONFIG_MACF
1842 if ((error = mac_proc_check_wait(q, p)) != 0)
1843 goto out;
1844#endif
1845 proc_lock(p);
1846 p->p_lflag |= P_LWAITED;
1847 proc_unlock(p);
1c79356b 1848 retval[0] = p->p_pid;
1c79356b
A
1849 if (uap->status) {
1850 status = W_STOPCODE(p->p_xstat);
1851 error = copyout((caddr_t)&status,
91447636 1852 uap->status,
1c79356b
A
1853 sizeof(status));
1854 } else
1855 error = 0;
2d21ac55
A
1856 goto out;
1857 }
1858 /*
1859 * If we are waiting for continued processses, and this
1860 * process was continued
1861 */
1862 if ((uap->options & WCONTINUED) &&
1863 (p->p_flag & P_CONTINUED)) {
1864 proc_list_unlock();
1865#if CONFIG_MACF
1866 if ((error = mac_proc_check_wait(q, p)) != 0)
1867 goto out;
1868#endif
1869
1870 /* Prevent other process for waiting for this event */
b0d623f7 1871 OSBitAndAtomic(~((uint32_t)P_CONTINUED), &p->p_flag);
2d21ac55
A
1872 retval[0] = p->p_pid;
1873 if (uap->status) {
1874 status = W_STOPCODE(SIGCONT);
1875 error = copyout((caddr_t)&status,
1876 uap->status,
1877 sizeof(status));
1878 } else
1879 error = 0;
1880 goto out;
1c79356b 1881 }
2d21ac55 1882 p->p_listflag &= ~P_LIST_WAITING;
7b1edb79 1883 wakeup(&p->p_stat);
1c79356b 1884 }
2d21ac55
A
1885 /* list lock is held when we get here any which way */
1886 if (nfound == 0) {
1887 proc_list_unlock();
1c79356b 1888 return (ECHILD);
2d21ac55 1889 }
7b1edb79 1890
1c79356b
A
1891 if (uap->options & WNOHANG) {
1892 retval[0] = 0;
2d21ac55 1893 proc_list_unlock();
1c79356b
A
1894 return (0);
1895 }
1896
fe8ab488
A
1897 /* Save arguments for continuation. Backing storage is in uthread->uu_arg, and will not be deallocated */
1898 uth = current_uthread();
1899 wait4_data = &uth->uu_kevent.uu_wait4_data;
1900 wait4_data->args = uap;
1901 wait4_data->retval = retval;
1902
2d21ac55 1903 if ((error = msleep0((caddr_t)q, proc_list_mlock, PWAIT | PCATCH | PDROP, "wait", 0, wait1continue)))
91447636
A
1904 return (error);
1905
1906 goto loop;
2d21ac55
A
1907out:
1908 proc_list_lock();
1909 p->p_listflag &= ~P_LIST_WAITING;
1910 wakeup(&p->p_stat);
1911 proc_list_unlock();
1912 return (error);
91447636
A
1913}
1914
316670eb
A
1915#if DEBUG
1916#define ASSERT_LCK_MTX_OWNED(lock) \
1917 lck_mtx_assert(lock, LCK_MTX_ASSERT_OWNED)
1918#else
1919#define ASSERT_LCK_MTX_OWNED(lock) /* nothing */
1920#endif
91447636
A
1921
1922int
1923waitidcontinue(int result)
1924{
fe8ab488 1925 proc_t p;
91447636 1926 thread_t thread;
fe8ab488
A
1927 uthread_t uth;
1928 struct _waitid_data *waitid_data;
1929 struct waitid_nocancel_args *uap;
91447636 1930 int *retval;
91447636
A
1931
1932 if (result)
316670eb 1933 return (result);
91447636 1934
fe8ab488 1935 p = current_proc();
91447636 1936 thread = current_thread();
fe8ab488
A
1937 uth = (struct uthread *)get_bsdthread_info(thread);
1938
1939 waitid_data = &uth->uu_kevent.uu_waitid_data;
1940 uap = waitid_data->args;
1941 retval = waitid_data->retval;
1942 return(waitid_nocancel(p, uap, retval));
91447636
A
1943}
1944
1945/*
1946 * Description: Suspend the calling thread until one child of the process
1947 * containing the calling thread changes state.
1948 *
1949 * Parameters: uap->idtype one of P_PID, P_PGID, P_ALL
1950 * uap->id pid_t or gid_t or ignored
316670eb 1951 * uap->infop Address of siginfo_t struct in
91447636
A
1952 * user space into which to return status
1953 * uap->options flag values
1954 *
1955 * Returns: 0 Success
1956 * !0 Error returning status to user space
1957 */
1958int
b0d623f7 1959waitid(proc_t q, struct waitid_args *uap, int32_t *retval)
2d21ac55
A
1960{
1961 __pthread_testcancel(1);
316670eb 1962 return (waitid_nocancel(q, (struct waitid_nocancel_args *)uap, retval));
2d21ac55
A
1963}
1964
1965int
316670eb
A
1966waitid_nocancel(proc_t q, struct waitid_nocancel_args *uap,
1967 __unused int32_t *retval)
91447636 1968{
316670eb
A
1969 user_siginfo_t siginfo; /* siginfo data to return to caller */
1970 boolean_t caller64 = IS_64BIT_PROCESS(q);
2d21ac55
A
1971 int nfound;
1972 proc_t p;
91447636 1973 int error;
fe8ab488
A
1974 uthread_t uth;
1975 struct _waitid_data *waitid_data;
91447636 1976
316670eb
A
1977 if (uap->options == 0 ||
1978 (uap->options & ~(WNOHANG|WNOWAIT|WCONTINUED|WSTOPPED|WEXITED)))
2d21ac55
A
1979 return (EINVAL); /* bits set that aren't recognized */
1980
316670eb 1981 switch (uap->idtype) {
2d21ac55
A
1982 case P_PID: /* child with process ID equal to... */
1983 case P_PGID: /* child with process group ID equal to... */
1984 if (((int)uap->id) < 0)
1985 return (EINVAL);
1986 break;
1987 case P_ALL: /* any child */
1988 break;
1989 }
1990
91447636 1991loop:
2d21ac55
A
1992 proc_list_lock();
1993loop1:
91447636 1994 nfound = 0;
316670eb 1995
39037602 1996 PCHILDREN_FOREACH(q, p) {
316670eb 1997 switch (uap->idtype) {
91447636
A
1998 case P_PID: /* child with process ID equal to... */
1999 if (p->p_pid != (pid_t)uap->id)
2000 continue;
2001 break;
2002 case P_PGID: /* child with process group ID equal to... */
2d21ac55 2003 if (p->p_pgrpid != (pid_t)uap->id)
91447636
A
2004 continue;
2005 break;
2006 case P_ALL: /* any child */
2007 break;
2008 }
2009
2010 /* XXX This is racy because we don't get the lock!!!! */
2011
2012 /*
2013 * Wait collision; go to sleep and restart; used to maintain
2014 * the single return for waited process guarantee.
2015 */
2d21ac55 2016 if (p->p_listflag & P_LIST_WAITING) {
316670eb
A
2017 (void) msleep(&p->p_stat, proc_list_mlock,
2018 PWAIT, "waitidcoll", 0);
2d21ac55 2019 goto loop1;
91447636 2020 }
2d21ac55 2021 p->p_listflag |= P_LIST_WAITING; /* mark busy */
91447636
A
2022
2023 nfound++;
2024
316670eb
A
2025 bzero(&siginfo, sizeof (siginfo));
2026
2027 switch (p->p_stat) {
91447636
A
2028 case SZOMB: /* Exited */
2029 if (!(uap->options & WEXITED))
2030 break;
2d21ac55 2031 proc_list_unlock();
316670eb
A
2032#if CONFIG_MACF
2033 if ((error = mac_proc_check_wait(q, p)) != 0)
2034 goto out;
2035#endif
2036 siginfo.si_signo = SIGCHLD;
2037 siginfo.si_pid = p->p_pid;
5ba3f43e
A
2038 siginfo.si_status = (WEXITSTATUS(p->p_xstat) & 0x00FFFFFF) | (((uint32_t)(p->p_xhighbits) << 24) & 0xFF000000);
2039 p->p_xhighbits = 0;
316670eb
A
2040 if (WIFSIGNALED(p->p_xstat)) {
2041 siginfo.si_code = WCOREDUMP(p->p_xstat) ?
2042 CLD_DUMPED : CLD_KILLED;
2043 } else
2044 siginfo.si_code = CLD_EXITED;
2d21ac55 2045
316670eb
A
2046 if ((error = copyoutsiginfo(&siginfo,
2047 caller64, uap->infop)) != 0)
2d21ac55 2048 goto out;
91447636
A
2049
2050 /* Prevent other process for waiting for this event? */
2051 if (!(uap->options & WNOWAIT)) {
4b17d6b6 2052 (void) reap_child_locked(q, p, 0, 0, 0, 0);
316670eb 2053 return (0);
91447636 2054 }
316670eb 2055 goto out;
91447636
A
2056
2057 case SSTOP: /* Stopped */
2058 /*
2059 * If we are not interested in stopped processes, then
2060 * ignore this one.
2061 */
2062 if (!(uap->options & WSTOPPED))
2063 break;
2064
2065 /*
2066 * If someone has already waited it, we lost a race
2067 * to be the one to return status.
2068 */
2d21ac55 2069 if ((p->p_lflag & P_LWAITED) != 0)
91447636 2070 break;
2d21ac55 2071 proc_list_unlock();
316670eb
A
2072#if CONFIG_MACF
2073 if ((error = mac_proc_check_wait(q, p)) != 0)
2074 goto out;
2075#endif
2076 siginfo.si_signo = SIGCHLD;
2077 siginfo.si_pid = p->p_pid;
2078 siginfo.si_status = p->p_xstat; /* signal number */
2079 siginfo.si_code = CLD_STOPPED;
91447636 2080
316670eb
A
2081 if ((error = copyoutsiginfo(&siginfo,
2082 caller64, uap->infop)) != 0)
2d21ac55 2083 goto out;
91447636
A
2084
2085 /* Prevent other process for waiting for this event? */
2086 if (!(uap->options & WNOWAIT)) {
2d21ac55
A
2087 proc_lock(p);
2088 p->p_lflag |= P_LWAITED;
2089 proc_unlock(p);
91447636 2090 }
2d21ac55 2091 goto out;
91447636 2092
316670eb 2093 default: /* All other states => Continued */
91447636
A
2094 if (!(uap->options & WCONTINUED))
2095 break;
2096
2097 /*
2098 * If the flag isn't set, then this process has not
2099 * been stopped and continued, or the status has
2100 * already been reaped by another caller of waitid().
2101 */
2102 if ((p->p_flag & P_CONTINUED) == 0)
2103 break;
2d21ac55 2104 proc_list_unlock();
316670eb
A
2105#if CONFIG_MACF
2106 if ((error = mac_proc_check_wait(q, p)) != 0)
2107 goto out;
2108#endif
2109 siginfo.si_signo = SIGCHLD;
2110 siginfo.si_code = CLD_CONTINUED;
2d21ac55 2111 proc_lock(p);
316670eb
A
2112 siginfo.si_pid = p->p_contproc;
2113 siginfo.si_status = p->p_xstat;
2d21ac55 2114 proc_unlock(p);
91447636 2115
316670eb
A
2116 if ((error = copyoutsiginfo(&siginfo,
2117 caller64, uap->infop)) != 0)
2d21ac55 2118 goto out;
91447636
A
2119
2120 /* Prevent other process for waiting for this event? */
2121 if (!(uap->options & WNOWAIT)) {
316670eb
A
2122 OSBitAndAtomic(~((uint32_t)P_CONTINUED),
2123 &p->p_flag);
91447636 2124 }
2d21ac55 2125 goto out;
91447636 2126 }
316670eb
A
2127 ASSERT_LCK_MTX_OWNED(proc_list_mlock);
2128
91447636 2129 /* Not a process we are interested in; go on to next child */
316670eb 2130
2d21ac55 2131 p->p_listflag &= ~P_LIST_WAITING;
91447636
A
2132 wakeup(&p->p_stat);
2133 }
316670eb 2134 ASSERT_LCK_MTX_OWNED(proc_list_mlock);
91447636
A
2135
2136 /* No child processes that could possibly satisfy the request? */
316670eb 2137
2d21ac55
A
2138 if (nfound == 0) {
2139 proc_list_unlock();
91447636 2140 return (ECHILD);
2d21ac55 2141 }
91447636
A
2142
2143 if (uap->options & WNOHANG) {
2d21ac55 2144 proc_list_unlock();
316670eb
A
2145#if CONFIG_MACF
2146 if ((error = mac_proc_check_wait(q, p)) != 0)
2147 return (error);
2148#endif
2149 /*
2150 * The state of the siginfo structure in this case
2151 * is undefined. Some implementations bzero it, some
2152 * (like here) leave it untouched for efficiency.
2153 *
2154 * Thus the most portable check for "no matching pid with
2155 * WNOHANG" is to store a zero into si_pid before
2156 * invocation, then check for a non-zero value afterwards.
2157 */
91447636
A
2158 return (0);
2159 }
2160
fe8ab488
A
2161 /* Save arguments for continuation. Backing storage is in uthread->uu_arg, and will not be deallocated */
2162 uth = current_uthread();
2163 waitid_data = &uth->uu_kevent.uu_waitid_data;
2164 waitid_data->args = uap;
2165 waitid_data->retval = retval;
2166
316670eb
A
2167 if ((error = msleep0(q, proc_list_mlock,
2168 PWAIT | PCATCH | PDROP, "waitid", 0, waitidcontinue)) != 0)
1c79356b 2169 return (error);
7b1edb79 2170
1c79356b 2171 goto loop;
2d21ac55
A
2172out:
2173 proc_list_lock();
2174 p->p_listflag &= ~P_LIST_WAITING;
2175 wakeup(&p->p_stat);
2176 proc_list_unlock();
2177 return (error);
1c79356b
A
2178}
2179
2180/*
2181 * make process 'parent' the new parent of process 'child'.
2182 */
2183void
2d21ac55 2184proc_reparentlocked(proc_t child, proc_t parent, int cansignal, int locked)
1c79356b 2185{
2d21ac55 2186 proc_t oldparent = PROC_NULL;
1c79356b
A
2187
2188 if (child->p_pptr == parent)
2189 return;
2190
2d21ac55
A
2191 if (locked == 0)
2192 proc_list_lock();
2193
2194 oldparent = child->p_pptr;
2195#if __PROC_INTERNAL_DEBUG
2196 if (oldparent == PROC_NULL)
b0d623f7 2197 panic("proc_reparent: process %p does not have a parent\n", child);
2d21ac55
A
2198#endif
2199
1c79356b 2200 LIST_REMOVE(child, p_sibling);
2d21ac55
A
2201#if __PROC_INTERNAL_DEBUG
2202 if (oldparent->p_childrencnt == 0)
2203 panic("process children count already 0\n");
2204#endif
2205 oldparent->p_childrencnt--;
2206#if __PROC_INTERNAL_DEBUG1
2207 if (oldparent->p_childrencnt < 0)
2208 panic("process children count -ve\n");
2209#endif
1c79356b 2210 LIST_INSERT_HEAD(&parent->p_children, child, p_sibling);
2d21ac55 2211 parent->p_childrencnt++;
1c79356b 2212 child->p_pptr = parent;
2d21ac55
A
2213 child->p_ppid = parent->p_pid;
2214
2215 proc_list_unlock();
91447636 2216
2d21ac55 2217 if ((cansignal != 0) && (initproc == parent) && (child->p_stat == SZOMB))
91447636 2218 psignal(initproc, SIGCHLD);
2d21ac55
A
2219 if (locked == 1)
2220 proc_list_lock();
1c79356b
A
2221}
2222
0b4e3aa0
A
2223/*
2224 * Exit: deallocate address space and other resources, change proc state
2225 * to zombie, and unlink proc from allproc and parent's lists. Save exit
2226 * status and rusage for wait(). Check for child processes and orphan them.
2227 */
2228
91447636 2229void
2d21ac55
A
2230vfork_exit(proc_t p, int rv)
2231{
2232 vfork_exit_internal(p, rv, 0);
2233}
2234
2235void
2236vfork_exit_internal(proc_t p, int rv, int forceexit)
0b4e3aa0 2237{
91447636
A
2238 thread_t self = current_thread();
2239#ifdef FIXME
0b4e3aa0 2240 struct task *task = p->task;
91447636 2241#endif
0b4e3aa0
A
2242 struct uthread *ut;
2243
91447636
A
2244 /*
2245 * If a thread in this task has already
2246 * called exit(), then halt any others
2247 * right here.
2248 */
2249
2250 ut = get_bsdthread_info(self);
91447636 2251
55e303ae 2252
2d21ac55
A
2253 proc_lock(p);
2254 if ((p->p_lflag & P_LPEXIT) == P_LPEXIT) {
2255 /*
2256 * This happens when a parent exits/killed and vfork is in progress
2257 * other threads. But shutdown code for ex has already called exit1()
2258 */
2259 proc_unlock(p);
2260 return;
2261 }
2262 p->p_lflag |= (P_LEXIT | P_LPEXIT);
2263 proc_unlock(p);
2264
2265 if (forceexit == 0) {
2266 /*
2267 * parent of a vfork child has already called exit() and the
2268 * thread that has vfork in proress terminates. So there is no
2269 * separate address space here and it has already been marked for
2270 * termination. This was never covered before and could cause problems
2271 * if we block here for outside code.
2272 */
2273 /* Notify the perf server */
2274 (void)sys_perf_notify(self, p->p_pid);
2275 }
55e303ae 2276
0b4e3aa0
A
2277 /*
2278 * Remove proc from allproc queue and from pidhash chain.
2279 * Need to do this before we do anything that can block.
2280 * Not doing causes things like mount() find this on allproc
2281 * in partially cleaned state.
2282 */
2d21ac55
A
2283
2284 proc_list_lock();
2285
39236c6e
A
2286#if CONFIG_MEMORYSTATUS
2287 memorystatus_remove(p, TRUE);
2288#endif
2289
0b4e3aa0 2290 LIST_REMOVE(p, p_list);
55e303ae 2291 LIST_INSERT_HEAD(&zombproc, p, p_list); /* Place onto zombproc. */
2d21ac55
A
2292 /* will not be visible via proc_find */
2293 p->p_listflag |= P_LIST_EXITED;
0b4e3aa0 2294
2d21ac55 2295 proc_list_unlock();
0b4e3aa0 2296
2d21ac55 2297 proc_lock(p);
0b4e3aa0 2298 p->p_xstat = rv;
2d21ac55
A
2299 p->p_lflag &= ~(P_LTRACED | P_LPPWAIT);
2300 p->p_sigignore = ~0;
2301 proc_unlock(p);
2302
2d21ac55 2303 ut->uu_siglist = 0;
0b4e3aa0 2304
5ba3f43e 2305 /* begin vproc_exit */
0b4e3aa0 2306
2d21ac55
A
2307 proc_t q;
2308 proc_t pp;
5ba3f43e 2309
2d21ac55 2310 vnode_t tvp;
5ba3f43e 2311
2d21ac55
A
2312 struct pgrp * pg;
2313 struct session *sessp;
39236c6e 2314 struct rusage_superset *rup;
0b4e3aa0 2315
91447636 2316 /* XXX Zombie allocation may fail, in which case stats get lost */
39236c6e
A
2317 MALLOC_ZONE(rup, struct rusage_superset *,
2318 sizeof (*rup), M_ZOMBIE, M_WAITOK);
2d21ac55
A
2319
2320 proc_refdrain(p);
2321
0b4e3aa0
A
2322 /*
2323 * Close open files and release open-file table.
2324 * This may block!
2325 */
2326 fdfree(p);
2327
2d21ac55
A
2328 sessp = proc_session(p);
2329 if (SESS_LEADER(p, sessp)) {
2330
2d21ac55 2331 if (sessp->s_ttyvp != NULLVP) {
fa4905b1 2332 struct vnode *ttyvp;
2d21ac55 2333 int ttyvid;
39236c6e 2334 int cttyflag = 0;
91447636 2335 struct vfs_context context;
316670eb 2336 struct tty *tp;
fa4905b1 2337
0b4e3aa0
A
2338 /*
2339 * Controlling process.
2340 * Signal foreground pgrp,
2341 * drain controlling terminal
2342 * and revoke access to controlling terminal.
2343 */
316670eb 2344 session_lock(sessp);
b0d623f7 2345 tp = SESSION_TP(sessp);
2d21ac55 2346 if ((tp != TTY_NULL) && (tp->t_session == sessp)) {
316670eb
A
2347 session_unlock(sessp);
2348
fe8ab488
A
2349 /*
2350 * We're going to SIGHUP the foreground process
2351 * group. It can't change from this point on
2352 * until the revoke is complete.
2353 * The process group changes under both the tty
2354 * lock and proc_list_lock but we need only one
2355 */
2356 tty_lock(tp);
2357 ttysetpgrphup(tp);
2358 tty_unlock(tp);
2359
2d21ac55 2360 tty_pgsignal(tp, SIGHUP, 1);
2d21ac55
A
2361
2362 session_lock(sessp);
b0d623f7 2363 tp = SESSION_TP(sessp);
316670eb 2364 }
39236c6e
A
2365 cttyflag = sessp->s_flags & S_CTTYREF;
2366 sessp->s_flags &= ~S_CTTYREF;
316670eb
A
2367 ttyvp = sessp->s_ttyvp;
2368 ttyvid = sessp->s_ttyvid;
2369 sessp->s_ttyvp = NULL;
2370 sessp->s_ttyvid = 0;
2371 sessp->s_ttyp = TTY_NULL;
2372 sessp->s_ttypgrpid = NO_PID;
2373 session_unlock(sessp);
2374
2375 if ((ttyvp != NULLVP) && (vnode_getwithvid(ttyvp, ttyvid) == 0)) {
2376 if (tp != TTY_NULL) {
2377 tty_lock(tp);
2378 (void) ttywait(tp);
2379 tty_unlock(tp);
2d21ac55 2380 }
316670eb
A
2381 context.vc_thread = proc_thread(p); /* XXX */
2382 context.vc_ucred = kauth_cred_proc_ref(p);
316670eb 2383 VNOP_REVOKE(ttyvp, REVOKEALL, &context);
39236c6e
A
2384 if (cttyflag) {
2385 /*
2386 * Release the extra usecount taken in cttyopen.
2387 * usecount should be released after VNOP_REVOKE is called.
fe8ab488
A
2388 * This usecount was taken to ensure that
2389 * the VNOP_REVOKE results in a close to
2390 * the tty since cttyclose is a no-op.
39236c6e
A
2391 */
2392 vnode_rele(ttyvp);
2393 }
316670eb
A
2394 vnode_put(ttyvp);
2395 kauth_cred_unref(&context.vc_ucred);
2396 ttyvp = NULLVP;
0b4e3aa0 2397 }
fe8ab488
A
2398 if (tp) {
2399 /*
2400 * This is cleared even if not set. This is also done in
2401 * spec_close to ensure that the flag is cleared.
2402 */
2403 tty_lock(tp);
2404 ttyclrpgrphup(tp);
2405 tty_unlock(tp);
2406
316670eb 2407 ttyfree(tp);
fe8ab488 2408 }
0b4e3aa0 2409 }
2d21ac55
A
2410 session_lock(sessp);
2411 sessp->s_leader = NULL;
2412 session_unlock(sessp);
0b4e3aa0 2413 }
2d21ac55 2414 session_rele(sessp);
0b4e3aa0 2415
2d21ac55
A
2416 pg = proc_pgrp(p);
2417 fixjobc(p, pg, 0);
2418 pg_rele(pg);
9bccf70c 2419
2d21ac55 2420 p->p_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
0b4e3aa0 2421
2d21ac55
A
2422 proc_list_lock();
2423 proc_childdrainstart(p);
2424 while ((q = p->p_children.lh_first) != NULL) {
2d21ac55
A
2425 if (q->p_stat == SZOMB) {
2426 if (p != q->p_pptr)
2427 panic("parent child linkage broken");
2428 /* check for lookups by zomb sysctl */
2429 while ((q->p_listflag & P_LIST_WAITING) == P_LIST_WAITING) {
2430 msleep(&q->p_stat, proc_list_mlock, PWAIT, "waitcoll", 0);
2431 }
2432 q->p_listflag |= P_LIST_WAITING;
2433 /*
2434 * This is a named reference and it is not granted
2435 * if the reap is already in progress. So we get
2436 * the reference here exclusively and their can be
2437 * no waiters. So there is no need for a wakeup
2438 * after we are done. AlsO the reap frees the structure
2439 * and the proc struct cannot be used for wakeups as well.
2440 * It is safe to use q here as this is system reap
2441 */
4b17d6b6 2442 (void)reap_child_locked(p, q, 1, 0, 1, 0);
2d21ac55 2443 } else {
2d21ac55
A
2444 /*
2445 * Traced processes are killed
2446 * since their existence means someone is messing up.
2447 */
2448 if (q->p_lflag & P_LTRACED) {
39236c6e
A
2449 struct proc *opp;
2450
2d21ac55 2451 proc_list_unlock();
39236c6e
A
2452
2453 opp = proc_find(q->p_oppid);
2454 if (opp != PROC_NULL) {
2455 proc_list_lock();
2456 q->p_oppid = 0;
2457 proc_list_unlock();
2458 proc_reparentlocked(q, opp, 0, 0);
2459 proc_rele(opp);
2460 } else {
2461 /* original parent exited while traced */
2462 proc_list_lock();
2463 q->p_listflag |= P_LIST_DEADPARENT;
2464 q->p_oppid = 0;
2465 proc_list_unlock();
2466 proc_reparentlocked(q, initproc, 0, 0);
2467 }
2468
2d21ac55
A
2469 proc_lock(q);
2470 q->p_lflag &= ~P_LTRACED;
39236c6e 2471
2d21ac55 2472 if (q->sigwait_thread) {
6d2010ae
A
2473 thread_t thread = q->sigwait_thread;
2474
2d21ac55
A
2475 proc_unlock(q);
2476 /*
2477 * The sigwait_thread could be stopped at a
2478 * breakpoint. Wake it up to kill.
2479 * Need to do this as it could be a thread which is not
2480 * the first thread in the task. So any attempts to kill
2481 * the process would result into a deadlock on q->sigwait.
2482 */
6d2010ae
A
2483 thread_resume(thread);
2484 clear_wait(thread, THREAD_INTERRUPTED);
39037602 2485 threadsignal(thread, SIGKILL, 0, TRUE);
6d2010ae 2486 } else {
2d21ac55 2487 proc_unlock(q);
6d2010ae
A
2488 }
2489
2d21ac55
A
2490 psignal(q, SIGKILL);
2491 proc_list_lock();
39236c6e
A
2492 } else {
2493 q->p_listflag |= P_LIST_DEADPARENT;
2494 proc_reparentlocked(q, initproc, 0, 1);
0b4e3aa0 2495 }
0b4e3aa0
A
2496 }
2497 }
2498
2d21ac55
A
2499 proc_childdrainend(p);
2500 proc_list_unlock();
2501
2502 /*
2503 * Release reference to text vnode
2504 */
2505 tvp = p->p_textvp;
2506 p->p_textvp = NULL;
2507 if (tvp != NULLVP) {
2508 vnode_rele(tvp);
2509 }
2510
0b4e3aa0
A
2511 /*
2512 * Save exit status and final rusage info, adding in child rusage
91447636
A
2513 * info and self times. If we were unable to allocate a zombie
2514 * structure, this information is lost.
0b4e3aa0 2515 */
39236c6e
A
2516 if (rup != NULL) {
2517 rup->ru = p->p_stats->p_ru;
2518 timerclear(&rup->ru.ru_utime);
2519 timerclear(&rup->ru.ru_stime);
0b4e3aa0
A
2520
2521#ifdef FIXME
91447636 2522 if (task) {
316670eb 2523 mach_task_basic_info_data_t tinfo;
0b4e3aa0
A
2524 task_thread_times_info_data_t ttimesinfo;
2525 int task_info_stuff, task_ttimes_stuff;
2526 struct timeval ut,st;
2527
316670eb
A
2528 task_info_stuff = MACH_TASK_BASIC_INFO_COUNT;
2529 task_info(task, MACH_TASK_BASIC_INFO,
0b4e3aa0 2530 &tinfo, &task_info_stuff);
39236c6e
A
2531 p->p_ru->ru.ru_utime.tv_sec = tinfo.user_time.seconds;
2532 p->p_ru->ru.ru_utime.tv_usec = tinfo.user_time.microseconds;
2533 p->p_ru->ru.ru_stime.tv_sec = tinfo.system_time.seconds;
2534 p->p_ru->ru.ru_stime.tv_usec = tinfo.system_time.microseconds;
0b4e3aa0
A
2535
2536 task_ttimes_stuff = TASK_THREAD_TIMES_INFO_COUNT;
2537 task_info(task, TASK_THREAD_TIMES_INFO,
2538 &ttimesinfo, &task_ttimes_stuff);
2539
2540 ut.tv_sec = ttimesinfo.user_time.seconds;
2541 ut.tv_usec = ttimesinfo.user_time.microseconds;
2542 st.tv_sec = ttimesinfo.system_time.seconds;
2543 st.tv_usec = ttimesinfo.system_time.microseconds;
39236c6e
A
2544 timeradd(&ut,&p->p_ru->ru.ru_utime,&p->p_ru->ru.ru_utime);
2545 timeradd(&st,&p->p_ru->ru.ru_stime,&p->p_ru->ru.ru_stime);
91447636 2546 }
0b4e3aa0
A
2547#endif /* FIXME */
2548
39236c6e
A
2549 ruadd(&rup->ru, &p->p_stats->p_cru);
2550
fe8ab488 2551 gather_rusage_info(p, &rup->ri, RUSAGE_INFO_CURRENT);
39236c6e
A
2552 rup->ri.ri_phys_footprint = 0;
2553 rup->ri.ri_proc_exit_abstime = mach_absolute_time();
2554
2555 /*
2556 * Now that we have filled in the rusage info, make it
2557 * visible to an external observer via proc_pid_rusage().
2558 */
2559 p->p_ru = rup;
91447636 2560 }
0b4e3aa0
A
2561
2562 /*
2563 * Free up profiling buffers.
2564 */
2565 {
2566 struct uprof *p0 = &p->p_stats->p_prof, *p1, *pn;
2567
2568 p1 = p0->pr_next;
2569 p0->pr_next = NULL;
2570 p0->pr_scale = 0;
2571
2572 for (; p1 != NULL; p1 = pn) {
2573 pn = p1->pr_next;
91447636 2574 kfree(p1, sizeof *p1);
0b4e3aa0
A
2575 }
2576 }
2577
6d2010ae
A
2578#if PSYNCH
2579 pth_proc_hashdelete(p);
2580#endif /* PSYNCH */
2581
5ba3f43e
A
2582 proc_free_realitimer(p);
2583
0b4e3aa0
A
2584 /*
2585 * Other substructures are freed from wait().
2586 */
2d21ac55 2587 FREE_ZONE(p->p_stats, sizeof *p->p_stats, M_PSTATS);
0b4e3aa0
A
2588 p->p_stats = NULL;
2589
2d21ac55 2590 FREE_ZONE(p->p_sigacts, sizeof *p->p_sigacts, M_SIGACTS);
0b4e3aa0
A
2591 p->p_sigacts = NULL;
2592
2d21ac55 2593 proc_limitdrop(p, 1);
0b4e3aa0
A
2594 p->p_limit = NULL;
2595
2596 /*
2597 * Finish up by terminating the task
2598 * and halt this thread (only if a
2599 * member of the task exiting).
2600 */
2601 p->task = TASK_NULL;
2602
2603 /*
2604 * Notify parent that we're gone.
2605 */
2d21ac55
A
2606 pp = proc_parent(p);
2607 if ((p->p_listflag & P_LIST_DEADPARENT) == 0) {
2608 if (pp != initproc) {
2609 proc_lock(pp);
2610 pp->si_pid = p->p_pid;
5ba3f43e
A
2611 pp->p_xhighbits = p->p_xhighbits;
2612 p->p_xhighbits = 0;
2d21ac55
A
2613 pp->si_status = p->p_xstat;
2614 pp->si_code = CLD_EXITED;
2615 /*
2616 * p_ucred usage is safe as it is an exiting process
2617 * and reference is dropped in reap
2618 */
6d2010ae 2619 pp->si_uid = kauth_cred_getruid(p->p_ucred);
2d21ac55
A
2620 proc_unlock(pp);
2621 }
2622 /* mark as a zombie */
2623 /* mark as a zombie */
2624 /* No need to take proc lock as all refs are drained and
2625 * no one except parent (reaping ) can look at this.
2626 * The write is to an int and is coherent. Also parent is
2627 * keyed off of list lock for reaping
2628 */
2629 p->p_stat = SZOMB;
0b4e3aa0 2630
2d21ac55 2631 psignal(pp, SIGCHLD);
91447636 2632
2d21ac55
A
2633 /* and now wakeup the parent */
2634 proc_list_lock();
2635 wakeup((caddr_t)pp);
2636 proc_list_unlock();
2637 } else {
2638 proc_list_lock();
2d21ac55
A
2639 /* check for lookups by zomb sysctl */
2640 while ((p->p_listflag & P_LIST_WAITING) == P_LIST_WAITING) {
2641 msleep(&p->p_stat, proc_list_mlock, PWAIT, "waitcoll", 0);
2642 }
d1ecb069 2643 p->p_stat = SZOMB;
2d21ac55 2644 p->p_listflag |= P_LIST_WAITING;
d1ecb069 2645
2d21ac55
A
2646 /*
2647 * This is a named reference and it is not granted
2648 * if the reap is already in progress. So we get
2649 * the reference here exclusively and their can be
2650 * no waiters. So there is no need for a wakeup
2651 * after we are done. AlsO the reap frees the structure
2652 * and the proc struct cannot be used for wakeups as well.
2653 * It is safe to use p here as this is system reap
2654 */
4b17d6b6 2655 (void)reap_child_locked(pp, p, 0, 0, 1, 1);
2d21ac55
A
2656 /* list lock dropped by reap_child_locked */
2657 }
2658 proc_rele(pp);
0b4e3aa0 2659}
91447636
A
2660
2661
2662/*
2663 * munge_rusage
2664 * LP64 support - long is 64 bits if we are dealing with a 64 bit user
b0d623f7 2665 * process. We munge the kernel version of rusage into the
91447636
A
2666 * 64 bit version.
2667 */
2668__private_extern__ void
b0d623f7 2669munge_user64_rusage(struct rusage *a_rusage_p, struct user64_rusage *a_user_rusage_p)
91447636
A
2670{
2671 /* timeval changes size, so utime and stime need special handling */
2672 a_user_rusage_p->ru_utime.tv_sec = a_rusage_p->ru_utime.tv_sec;
2673 a_user_rusage_p->ru_utime.tv_usec = a_rusage_p->ru_utime.tv_usec;
2674 a_user_rusage_p->ru_stime.tv_sec = a_rusage_p->ru_stime.tv_sec;
2675 a_user_rusage_p->ru_stime.tv_usec = a_rusage_p->ru_stime.tv_usec;
2676 /*
2677 * everything else can be a direct assign, since there is no loss
2678 * of precision implied boing 32->64.
2679 */
2680 a_user_rusage_p->ru_maxrss = a_rusage_p->ru_maxrss;
2681 a_user_rusage_p->ru_ixrss = a_rusage_p->ru_ixrss;
2682 a_user_rusage_p->ru_idrss = a_rusage_p->ru_idrss;
2683 a_user_rusage_p->ru_isrss = a_rusage_p->ru_isrss;
2684 a_user_rusage_p->ru_minflt = a_rusage_p->ru_minflt;
2685 a_user_rusage_p->ru_majflt = a_rusage_p->ru_majflt;
2686 a_user_rusage_p->ru_nswap = a_rusage_p->ru_nswap;
2687 a_user_rusage_p->ru_inblock = a_rusage_p->ru_inblock;
2688 a_user_rusage_p->ru_oublock = a_rusage_p->ru_oublock;
2689 a_user_rusage_p->ru_msgsnd = a_rusage_p->ru_msgsnd;
2690 a_user_rusage_p->ru_msgrcv = a_rusage_p->ru_msgrcv;
2691 a_user_rusage_p->ru_nsignals = a_rusage_p->ru_nsignals;
2692 a_user_rusage_p->ru_nvcsw = a_rusage_p->ru_nvcsw;
2693 a_user_rusage_p->ru_nivcsw = a_rusage_p->ru_nivcsw;
2694}
b0d623f7
A
2695
2696/* For a 64-bit kernel and 32-bit userspace, munging may be needed */
2697__private_extern__ void
2698munge_user32_rusage(struct rusage *a_rusage_p, struct user32_rusage *a_user_rusage_p)
2699{
2700 /* timeval changes size, so utime and stime need special handling */
2701 a_user_rusage_p->ru_utime.tv_sec = a_rusage_p->ru_utime.tv_sec;
2702 a_user_rusage_p->ru_utime.tv_usec = a_rusage_p->ru_utime.tv_usec;
2703 a_user_rusage_p->ru_stime.tv_sec = a_rusage_p->ru_stime.tv_sec;
2704 a_user_rusage_p->ru_stime.tv_usec = a_rusage_p->ru_stime.tv_usec;
2705 /*
2706 * everything else can be a direct assign. We currently ignore
2707 * the loss of precision
2708 */
2709 a_user_rusage_p->ru_maxrss = a_rusage_p->ru_maxrss;
2710 a_user_rusage_p->ru_ixrss = a_rusage_p->ru_ixrss;
2711 a_user_rusage_p->ru_idrss = a_rusage_p->ru_idrss;
2712 a_user_rusage_p->ru_isrss = a_rusage_p->ru_isrss;
2713 a_user_rusage_p->ru_minflt = a_rusage_p->ru_minflt;
2714 a_user_rusage_p->ru_majflt = a_rusage_p->ru_majflt;
2715 a_user_rusage_p->ru_nswap = a_rusage_p->ru_nswap;
2716 a_user_rusage_p->ru_inblock = a_rusage_p->ru_inblock;
2717 a_user_rusage_p->ru_oublock = a_rusage_p->ru_oublock;
2718 a_user_rusage_p->ru_msgsnd = a_rusage_p->ru_msgsnd;
2719 a_user_rusage_p->ru_msgrcv = a_rusage_p->ru_msgrcv;
2720 a_user_rusage_p->ru_nsignals = a_rusage_p->ru_nsignals;
2721 a_user_rusage_p->ru_nvcsw = a_rusage_p->ru_nvcsw;
2722 a_user_rusage_p->ru_nivcsw = a_rusage_p->ru_nivcsw;
2723}