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