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