2 * Copyright (c) 2000-2007 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* Copyright (c) 1995, 1997 Apple Computer, Inc. All Rights Reserved */
30 * Copyright (c) 1982, 1986, 1989, 1991, 1993
31 * The Regents of the University of California. All rights reserved.
32 * (c) UNIX System Laboratories, Inc.
33 * All or some portions of this file are derived from material licensed
34 * to the University of California by American Telephone and Telegraph
35 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
36 * the permission of UNIX System Laboratories, Inc.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the University of
49 * California, Berkeley and its contributors.
50 * 4. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * @(#)kern_exit.c 8.7 (Berkeley) 2/12/94
69 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
70 * support for mandatory and extensible security protections. This notice
71 * is included in support of clause 2.2 (b) of the Apple Public License,
75 #include <machine/reg.h>
76 #include <machine/psl.h>
78 #include "compat_43.h"
80 #include <sys/param.h>
81 #include <sys/systm.h>
82 #include <sys/ioctl.h>
83 #include <sys/proc_internal.h>
85 #include <sys/kauth.h>
88 #include <sys/resource.h>
89 #include <sys/kernel.h>
91 #include <sys/file_internal.h>
92 #include <sys/vnode_internal.h>
93 #include <sys/syslog.h>
94 #include <sys/malloc.h>
95 #include <sys/resourcevar.h>
96 #include <sys/ptrace.h>
98 #include <sys/aio_kern.h>
99 #include <sys/sysproto.h>
100 #include <sys/signalvar.h>
101 #include <sys/filedesc.h> /* fdfree */
103 #include <sys/shm_internal.h> /* shmexit */
105 #include <sys/acct.h> /* acct_process */
107 #include <bsm/audit_kernel.h>
108 #include <bsm/audit_kevents.h>
110 #include <mach/mach_types.h>
112 #include <kern/kern_types.h>
113 #include <kern/kalloc.h>
114 #include <kern/task.h>
115 #include <kern/thread.h>
116 #include <kern/thread_call.h>
117 #include <kern/sched_prim.h>
118 #include <kern/assert.h>
120 /* Do not include dtrace.h, it redefines kmem_[alloc/free] */
121 extern void (*dtrace_fasttrap_exit_ptr
)(proc_t
);
122 extern void (*dtrace_helpers_cleanup
)(proc_t
);
123 extern void dtrace_lazy_dofs_destroy(proc_t
);
125 #include <sys/dtrace_ptss.h>
129 #include <security/mac.h>
130 #include <sys/syscall.h>
133 #include <mach/mach_types.h>
134 #include <mach/task.h>
135 #include <mach/thread_act.h>
136 #include <mach/mach_traps.h> /* init_process */
140 extern char init_task_failure_data
[];
141 void proc_prepareexit(proc_t p
, int rv
);
142 void vfork_exit(proc_t p
, int rv
);
143 void vproc_exit(proc_t p
);
144 __private_extern__
void munge_rusage(struct rusage
*a_rusage_p
, struct user_rusage
*a_user_rusage_p
);
145 static int reap_child_locked(proc_t parent
, proc_t child
, int deadparent
, int locked
, int droplock
);
148 * Things which should have prototypes in headers, but don't
150 void *get_bsduthreadarg(thread_t
);
151 void proc_exit(proc_t p
);
152 int wait1continue(int result
);
153 int waitidcontinue(int result
);
154 int *get_bsduthreadrval(thread_t
);
155 kern_return_t
sys_perf_notify(thread_t thread
, int pid
);
156 kern_return_t
abnormal_exit_notify(mach_exception_data_type_t code
,
157 mach_exception_data_type_t subcode
);
158 int in_shutdown(void);
159 void workqueue_exit(struct proc
*);
163 * NOTE: Source and target may *NOT* overlap!
164 * XXX Should share code with bsd/dev/ppc/unix_signal.c
167 siginfo_64to32(user_siginfo_t
*in
, siginfo_t
*out
)
169 out
->si_signo
= in
->si_signo
;
170 out
->si_errno
= in
->si_errno
;
171 out
->si_code
= in
->si_code
;
172 out
->si_pid
= in
->si_pid
;
173 out
->si_uid
= in
->si_uid
;
174 out
->si_status
= in
->si_status
;
175 out
->si_addr
= CAST_DOWN(void *,in
->si_addr
);
176 /* following cast works for sival_int because of padding */
177 out
->si_value
.sival_ptr
= CAST_DOWN(void *,in
->si_value
.sival_ptr
);
178 out
->si_band
= in
->si_band
; /* range reduction */
179 out
->__pad
[0] = in
->pad
[0]; /* mcontext.ss.r1 */
187 exit(proc_t p
, struct exit_args
*uap
, int *retval
)
189 exit1(p
, W_EXITCODE(uap
->rval
, 0), retval
);
191 /* drop funnel before we return */
192 thread_exception_return();
195 thread_block(THREAD_CONTINUE_NULL
);
200 * Exit: deallocate address space and other resources, change proc state
201 * to zombie, and unlink proc from allproc and parent's lists. Save exit
202 * status and rusage for wait(). Check for child processes and orphan them.
205 exit1(proc_t p
, int rv
, int *retval
)
207 thread_t self
= current_thread();
208 struct task
*task
= p
->task
;
212 * If a thread in this task has already
213 * called exit(), then halt any others
217 ut
= get_bsdthread_info(self
);
218 if (ut
->uu_flag
& UT_VFORK
) {
220 vfork_return(p
, retval
, p
->p_pid
);
221 unix_syscall_return(0);
226 * The parameter list of audit_syscall_exit() was augmented to
227 * take the Darwin syscall number as the first parameter,
228 * which is currently required by mac_audit_postselect().
231 AUDIT_SYSCALL_EXIT(SYS_exit
, p
, ut
, 0); /* Exit is always successfull */
233 DTRACE_PROC1(exit
, int, CLD_EXITED
);
236 while (p
->exit_thread
!= self
) {
237 if (sig_try_locked(p
) <= 0) {
238 if (get_threadtask(self
) != task
) {
243 thread_terminate(self
);
244 thread_exception_return();
251 printf("pid 1 exited (signal %d, exit %d)",
252 WTERMSIG(rv
), WEXITSTATUS(rv
));
253 panic("%s died\nState at Last Exception:\n\n%s",
254 (p
->p_comm
[0] != '\0' ?
257 init_task_failure_data
);
260 p
->p_lflag
|= P_LEXIT
;
265 proc_prepareexit(p
, rv
);
267 /* task terminate will call proc_terminate and that cleans it up */
268 task_terminate_internal(task
);
274 proc_prepareexit(proc_t p
, int rv
)
276 mach_exception_data_type_t code
, subcode
;
278 thread_t self
= current_thread();
279 ut
= get_bsdthread_info(self
);
281 /* If a core should be generated, notify crash reporter */
282 if (!in_shutdown() && hassigprop(WTERMSIG(rv
), SA_CORE
)) {
284 * Workaround for processes checking up on PT_DENY_ATTACH:
285 * should be backed out post-Leopard (details in 5431025).
287 if ((SIGSEGV
== WTERMSIG(rv
)) &&
288 (p
->p_pptr
->p_lflag
& P_LNOATTACH
)) {
293 * Crash Reporter looks for the signal value, original exception
294 * type, and low 20 bits of the original code in code[0]
295 * (8, 4, and 20 bits respectively). code[1] is unmodified.
297 code
= ((WTERMSIG(rv
) & 0xff) << 24) |
298 ((ut
->uu_exception
& 0x0f) << 20) |
299 ((int)ut
->uu_code
& 0xfffff);
300 subcode
= ut
->uu_subcode
;
301 (void) abnormal_exit_notify(code
, subcode
);
305 /* Notify the perf server */
306 (void)sys_perf_notify(self
, p
->p_pid
);
309 * Remove proc from allproc queue and from pidhash chain.
310 * Need to do this before we do anything that can block.
311 * Not doing causes things like mount() find this on allproc
312 * in partially cleaned state.
317 LIST_REMOVE(p
, p_list
);
318 LIST_INSERT_HEAD(&zombproc
, p
, p_list
); /* Place onto zombproc. */
319 /* will not be visible via proc_find */
320 p
->p_listflag
|= P_LIST_EXITED
;
329 * If parent is waiting for us to exit or exec,
330 * P_LPPWAIT is set; we will wakeup the parent below.
333 p
->p_lflag
&= ~(P_LTRACED
| P_LPPWAIT
);
334 p
->p_sigignore
= ~(sigcantmask
);
344 struct task
*task
= p
->task
;
346 vnode_t tvp
= NULLVP
;
348 struct session
*sessp
;
349 struct uthread
* uth
;
351 /* This can happen if thread_terminate of the single thread
355 uth
= (struct uthread
*)get_bsdthread_info(current_thread());
358 if( !(p
->p_lflag
& P_LEXIT
)) {
359 p
->p_lflag
|= P_LEXIT
;
361 proc_prepareexit(p
, 0);
365 p
->p_lflag
|= P_LPEXIT
;
370 * Free any outstanding lazy dof entries. It is imperative we
371 * always call dtrace_lazy_dofs_destroy, rather than null check
372 * and call if !NULL. If we NULL test, during lazy dof faulting
373 * we can race with the faulting code and proceed from here to
374 * beyond the helpers cleanup. The lazy dof faulting will then
375 * install new helpers which will never be cleaned up, and leak.
377 dtrace_lazy_dofs_destroy(p
);
380 * Clean up any DTrace helper actions or probes for the process.
382 if (p
->p_dtrace_helpers
!= NULL
) {
383 (*dtrace_helpers_cleanup
)(p
);
387 * Clean up any DTrace probes associated with this process.
390 * APPLE NOTE: We release ptss pages/entries in dtrace_fasttrap_exit_ptr(),
391 * call this after dtrace_helpers_cleanup()
394 if (p
->p_dtrace_probes
&& dtrace_fasttrap_exit_ptr
) {
395 (*dtrace_fasttrap_exit_ptr
)(p
);
400 /* XXX Zombie allocation may fail, in which case stats get lost */
401 MALLOC_ZONE(p
->p_ru
, struct rusage
*,
402 sizeof (*p
->p_ru
), M_ZOMBIE
, M_WAITOK
);
405 * need to cancel async IO requests that can be cancelled and wait for those
406 * already active. MAY BLOCK!
416 * Close open files and release open-file table.
421 if (uth
->uu_lowpri_window
) {
423 * task is marked as a low priority I/O type
424 * and the I/O we issued while flushing files on close
425 * collided with normal I/O operations...
426 * no need to throttle this thread since its going away
427 * but we do need to update our bookeeping w/r to throttled threads
429 throttle_lowpri_io(FALSE
);
433 /* Close ref SYSV Shared memory*/
438 /* Release SYSV semaphores */
442 sessp
= proc_session(p
);
443 if (SESS_LEADER(p
, sessp
)) {
445 /* Protected by funnel for tty accesses */
446 fstate
= thread_funnel_set(kernel_flock
, TRUE
);
448 if (sessp
->s_ttyvp
!= NULLVP
) {
451 struct vfs_context context
;
456 * Controlling process.
457 * Signal foreground pgrp,
458 * drain controlling terminal
459 * and revoke access to controlling terminal.
463 if ((tp
!= TTY_NULL
) && (tp
->t_session
== sessp
)) {
464 tty_pgsignal(tp
, SIGHUP
, 1);
467 * The tty could have been revoked
472 ttyvp
= sessp
->s_ttyvp
;
473 ttyvid
= sessp
->s_ttyvid
;
474 sessp
->s_ttyvp
= NULL
;
476 sessp
->s_ttyp
= NULL
;
477 sessp
->s_ttypgrpid
= NO_PID
;
478 session_unlock(sessp
);
480 if ((ttyvp
!= NULLVP
) && (vnode_getwithvid(ttyvp
, ttyvid
) == 0)) {
481 context
.vc_thread
= proc_thread(p
); /* XXX */
482 context
.vc_ucred
= kauth_cred_proc_ref(p
);
483 VNOP_REVOKE(ttyvp
, REVOKEALL
, &context
);
485 kauth_cred_unref(&context
.vc_ucred
);
489 ttyvp
= sessp
->s_ttyvp
;
490 sessp
->s_ttyvp
= NULL
;
492 sessp
->s_ttyp
= NULL
;
493 sessp
->s_ttypgrpid
= NO_PID
;
494 session_unlock(sessp
);
499 * s_ttyp is not zero'd; we use this to indicate
500 * that the session once had a controlling terminal.
501 * (for logging and informational purposes)
505 (void) thread_funnel_set(kernel_flock
, fstate
);
507 sessp
->s_leader
= NULL
;
508 session_unlock(sessp
);
516 p
->p_rlimit
[RLIMIT_FSIZE
].rlim_cur
= RLIM_INFINITY
;
517 (void)acct_process(p
);
520 /* wait till parentrefs are dropped and grant no more */
521 proc_childdrainstart(p
);
522 while ((q
= p
->p_children
.lh_first
) != NULL
) {
523 q
->p_listflag
|= P_LIST_DEADPARENT
;
524 if (q
->p_stat
== SZOMB
) {
526 panic("parent child linkage broken");
527 /* check for sysctl zomb lookup */
528 while ((q
->p_listflag
& P_LIST_WAITING
) == P_LIST_WAITING
) {
529 msleep(&q
->p_stat
, proc_list_mlock
, PWAIT
, "waitcoll", 0);
531 q
->p_listflag
|= P_LIST_WAITING
;
533 * This is a named reference and it is not granted
534 * if the reap is already in progress. So we get
535 * the reference here exclusively and their can be
536 * no waiters. So there is no need for a wakeup
537 * after we are done. AlsO the reap frees the structure
538 * and the proc struct cannot be used for wakeups as well.
539 * It is safe to use q here as this is system reap
541 (void)reap_child_locked(p
, q
, 1, 1, 0);
543 proc_reparentlocked(q
, initproc
, 0, 1);
545 * Traced processes are killed
546 * since their existence means someone is messing up.
548 if (q
->p_lflag
& P_LTRACED
) {
551 q
->p_lflag
&= ~P_LTRACED
;
552 if (q
->sigwait_thread
) {
555 * The sigwait_thread could be stopped at a
556 * breakpoint. Wake it up to kill.
557 * Need to do this as it could be a thread which is not
558 * the first thread in the task. So any attempts to kill
559 * the process would result into a deadlock on q->sigwait.
561 thread_resume((thread_t
)q
->sigwait_thread
);
562 clear_wait(q
->sigwait_thread
, THREAD_INTERRUPTED
);
563 threadsignal((thread_t
)q
->sigwait_thread
, SIGKILL
, 0);
572 proc_childdrainend(p
);
576 * Release reference to text vnode
585 * Save exit status and final rusage info, adding in child rusage
586 * info and self times. If we were unable to allocate a zombie
587 * structure, this information is lost.
589 /* No need for locking here as no one than this thread can access this */
590 if (p
->p_ru
!= NULL
) {
591 *p
->p_ru
= p
->p_stats
->p_ru
;
593 timerclear(&p
->p_ru
->ru_utime
);
594 timerclear(&p
->p_ru
->ru_stime
);
597 task_basic_info_32_data_t tinfo
;
598 task_thread_times_info_data_t ttimesinfo
;
599 task_events_info_data_t teventsinfo
;
600 mach_msg_type_number_t task_info_stuff
, task_ttimes_stuff
;
601 mach_msg_type_number_t task_events_stuff
;
602 struct timeval ut
,st
;
604 task_info_stuff
= TASK_BASIC_INFO_32_COUNT
;
605 task_info(task
, TASK_BASIC2_INFO_32
,
606 (task_info_t
)&tinfo
, &task_info_stuff
);
607 p
->p_ru
->ru_utime
.tv_sec
= tinfo
.user_time
.seconds
;
608 p
->p_ru
->ru_utime
.tv_usec
= tinfo
.user_time
.microseconds
;
609 p
->p_ru
->ru_stime
.tv_sec
= tinfo
.system_time
.seconds
;
610 p
->p_ru
->ru_stime
.tv_usec
= tinfo
.system_time
.microseconds
;
612 p
->p_ru
->ru_maxrss
= tinfo
.resident_size
;
614 task_ttimes_stuff
= TASK_THREAD_TIMES_INFO_COUNT
;
615 task_info(task
, TASK_THREAD_TIMES_INFO
,
616 (task_info_t
)&ttimesinfo
, &task_ttimes_stuff
);
618 ut
.tv_sec
= ttimesinfo
.user_time
.seconds
;
619 ut
.tv_usec
= ttimesinfo
.user_time
.microseconds
;
620 st
.tv_sec
= ttimesinfo
.system_time
.seconds
;
621 st
.tv_usec
= ttimesinfo
.system_time
.microseconds
;
622 timeradd(&ut
,&p
->p_ru
->ru_utime
,&p
->p_ru
->ru_utime
);
623 timeradd(&st
,&p
->p_ru
->ru_stime
,&p
->p_ru
->ru_stime
);
625 task_events_stuff
= TASK_EVENTS_INFO_COUNT
;
626 task_info(task
, TASK_EVENTS_INFO
,
627 (task_info_t
)&teventsinfo
, &task_events_stuff
);
629 p
->p_ru
->ru_minflt
= (teventsinfo
.faults
-
630 teventsinfo
.pageins
);
631 p
->p_ru
->ru_majflt
= teventsinfo
.pageins
;
632 p
->p_ru
->ru_nivcsw
= (teventsinfo
.csw
-
634 if (p
->p_ru
->ru_nivcsw
< 0)
635 p
->p_ru
->ru_nivcsw
= 0;
638 ruadd(p
->p_ru
, &p
->p_stats
->p_cru
);
642 * Free up profiling buffers.
645 struct uprof
*p0
= &p
->p_stats
->p_prof
, *p1
, *pn
;
651 for (; p1
!= NULL
; p1
= pn
) {
653 kfree(p1
, sizeof *p1
);
658 if (thread_call_cancel(p
->p_rcall
))
661 while (p
->p_ractive
> 0) {
670 thread_call_free(p
->p_rcall
);
674 * Other substructures are freed from wait().
676 FREE_ZONE(p
->p_stats
, sizeof *p
->p_stats
, M_PSTATS
);
679 FREE_ZONE(p
->p_sigacts
, sizeof *p
->p_sigacts
, M_SIGACTS
);
682 proc_limitdrop(p
, 1);
687 * Finish up by terminating the task
688 * and halt this thread (only if a
689 * member of the task exiting).
692 set_bsdtask_info(task
, NULL
);
694 proc_knote(p
, NOTE_EXIT
);
696 /* mark the thread as the one that is doing proc_exit
697 * no need to hold proc lock in uthread_free
699 uth
->uu_flag
|= UT_PROCEXIT
;
701 * Notify parent that we're gone.
704 if (pp
->p_flag
& P_NOCLDWAIT
) {
708 * If the parent is ignoring SIGCHLD, then POSIX requires
709 * us to not add the resource usage to the parent process -
710 * we are only going to hand it off to init to get reaped.
711 * We should contest the standard in this case on the basis
716 * Add child resource usage to parent before giving
717 * zombie to init. If we were unable to allocate a
718 * zombie structure, this information is lost.
720 if (p
->p_ru
!= NULL
) {
722 ruadd(&pp
->p_stats
->p_cru
, p
->p_ru
);
725 #endif /* !3839178 */
727 /* kernel can reap this one, no need to move it to launchd */
729 p
->p_listflag
|= P_LIST_DEADPARENT
;
732 if ((p
->p_listflag
& P_LIST_DEADPARENT
) == 0) {
733 if (pp
!= initproc
) {
735 pp
->si_pid
= p
->p_pid
;
736 pp
->si_status
= p
->p_xstat
;
737 pp
->si_code
= CLD_EXITED
;
739 * p_ucred usage is safe as it is an exiting process
740 * and reference is dropped in reap
742 pp
->si_uid
= p
->p_ucred
->cr_ruid
;
745 /* mark as a zombie */
746 /* No need to take proc lock as all refs are drained and
747 * no one except parent (reaping ) can look at this.
748 * The write is to an int and is coherent. Also parent is
749 * keyed off of list lock for reaping
753 * The current process can be reaped so, no one
757 psignal(pp
, SIGCHLD
);
759 /* and now wakeup the parent */
764 /* should be fine as parent proc would be initproc */
765 /* mark as a zombie */
766 /* No need to take proc lock as all refs are drained and
767 * no one except parent (reaping ) can look at this.
768 * The write is to an int and is coherent. Also parent is
769 * keyed off of list lock for reaping
773 /* check for sysctl zomb lookup */
774 while ((p
->p_listflag
& P_LIST_WAITING
) == P_LIST_WAITING
) {
775 msleep(&p
->p_stat
, proc_list_mlock
, PWAIT
, "waitcoll", 0);
777 /* safe to use p as this is a system reap */
778 p
->p_listflag
|= P_LIST_WAITING
;
780 * This is a named reference and it is not granted
781 * if the reap is already in progress. So we get
782 * the reference here exclusively and their can be
783 * no waiters. So there is no need for a wakeup
784 * after we are done. AlsO the reap frees the structure
785 * and the proc struct cannot be used for wakeups as well.
786 * It is safe to use p here as this is system reap
788 (void)reap_child_locked(pp
, p
, 1, 1, 1);
789 /* list lock dropped by reap_child_locked */
791 if (uth
->uu_lowpri_window
) {
793 * task is marked as a low priority I/O type and we've
794 * somehow picked up another throttle during exit processing...
795 * no need to throttle this thread since its going away
796 * but we do need to update our bookeeping w/r to throttled threads
798 throttle_lowpri_io(FALSE
);
809 * Description: Given a process from which all status information needed
810 * has already been extracted, if the process is a ptrace
811 * attach process, detach it and give it back to its real
812 * parent, else recover all resources remaining associated
815 * Parameters: proc_t parent Parent of process being reaped
816 * proc_t child Process to reap
818 * Returns: 0 Process was not reaped because it
819 * came from an attach
820 * 1 Process was reaped
823 reap_child_locked(proc_t parent
, proc_t child
, int deadparent
, int locked
, int droplock
)
825 proc_t trace_parent
; /* Traced parent process, if tracing */
828 * If we got the child via a ptrace 'attach',
829 * we need to give it back to the old parent.
833 if (child
->p_oppid
&& (trace_parent
= proc_find(child
->p_oppid
))) {
837 if (trace_parent
!= initproc
) {
839 * proc internal fileds and p_ucred usage safe
840 * here as child is dead and is not reaped or
843 proc_lock(trace_parent
);
844 trace_parent
->si_pid
= child
->p_pid
;
845 trace_parent
->si_status
= child
->p_xstat
;
846 trace_parent
->si_code
= CLD_CONTINUED
;
847 trace_parent
->si_uid
= child
->p_ucred
->cr_ruid
;
848 proc_unlock(trace_parent
);
850 proc_reparentlocked(child
, trace_parent
, 1, 0);
851 psignal(trace_parent
, SIGCHLD
);
853 wakeup((caddr_t
)trace_parent
);
854 child
->p_listflag
&= ~P_LIST_WAITING
;
855 wakeup(&child
->p_stat
);
857 proc_rele(trace_parent
);
858 if ((locked
== 1) && (droplock
== 0))
863 proc_knote(child
, NOTE_REAP
);
870 * If the parent is ignoring SIGCHLD, then POSIX requires
871 * us to not add the resource usage to the parent process -
872 * we are only going to hand it off to init to get reaped.
873 * We should contest the standard in this case on the basis
876 if (!(parent
->p_flag
& P_NOCLDWAIT
))
878 ruadd(&parent
->p_stats
->p_cru
, child
->p_ru
);
880 FREE_ZONE(child
->p_ru
, sizeof *child
->p_ru
, M_ZOMBIE
);
883 printf("Warning : lost p_ru for %s\n", child
->p_comm
);
887 * Decrement the count of procs running with this uid.
888 * p_ucred usage is safe here as it is an exited process.
889 * and refernce is dropped after these calls down below
890 * (locking protection is provided by list lock held in chgproccnt)
892 (void)chgproccnt(child
->p_ucred
->cr_ruid
, -1);
901 * Free up credentials.
903 if (IS_VALID_CRED(child
->p_ucred
)) {
904 kauth_cred_unref(&child
->p_ucred
);
907 /* XXXX Note NOT SAFE TO USE p_ucred from this point onwards */
910 * Finally finished with old proc entry.
911 * Unlink it from its process group and free it.
916 LIST_REMOVE(child
, p_list
); /* off zombproc */
917 parent
->p_childrencnt
--;
918 LIST_REMOVE(child
, p_sibling
);
919 /* If there are no more children wakeup parent */
920 if ((deadparent
!= 0) && (LIST_EMPTY(&parent
->p_children
)))
921 wakeup((caddr_t
)parent
); /* with list lock held */
922 child
->p_listflag
&= ~P_LIST_WAITING
;
923 wakeup(&child
->p_stat
);
925 /* Take it out of process hash */
926 LIST_REMOVE(child
, p_hash
);
927 child
->p_listflag
&= ~P_LIST_INHASH
;
928 proc_checkdeadrefs(child
);
933 lck_mtx_destroy(&child
->p_mlock
, proc_lck_grp
);
934 lck_mtx_destroy(&child
->p_fdmlock
, proc_lck_grp
);
936 lck_mtx_destroy(&child
->p_dtrace_sprlock
, proc_lck_grp
);
938 lck_spin_destroy(&child
->p_slock
, proc_lck_grp
);
939 workqueue_destroy_lock(child
);
941 FREE_ZONE(child
, sizeof *child
, M_PROC
);
942 if ((locked
== 1) && (droplock
== 0))
950 wait1continue(int result
)
961 thread
= current_thread();
962 vt
= get_bsduthreadarg(thread
);
963 retval
= get_bsduthreadrval(thread
);
964 return(wait4(p
, (struct wait4_args
*)vt
, retval
));
968 wait4(proc_t q
, struct wait4_args
*uap
, register_t
*retval
)
970 __pthread_testcancel(1);
971 return(wait4_nocancel(q
, (struct wait4_nocancel_args
*)uap
, retval
));
975 wait4_nocancel(proc_t q
, struct wait4_nocancel_args
*uap
, register_t
*retval
)
982 uap
->pid
= -q
->p_pgrpid
;
988 for (p
= q
->p_children
.lh_first
; p
!= 0; p
= p
->p_sibling
.le_next
) {
989 if (uap
->pid
!= WAIT_ANY
&&
990 p
->p_pid
!= uap
->pid
&&
991 p
->p_pgrpid
!= -(uap
->pid
))
996 /* XXX This is racy because we don't get the lock!!!! */
998 if (p
->p_listflag
& P_LIST_WAITING
) {
999 (void)msleep(&p
->p_stat
, proc_list_mlock
, PWAIT
, "waitcoll", 0);
1002 p
->p_listflag
|= P_LIST_WAITING
; /* only allow single thread to wait() */
1005 if (p
->p_stat
== SZOMB
) {
1008 if ((error
= mac_proc_check_wait(q
, p
)) != 0)
1011 retval
[0] = p
->p_pid
;
1013 /* Legacy apps expect only 8 bits of status */
1014 status
= 0xffff & p
->p_xstat
; /* convert to int */
1015 error
= copyout((caddr_t
)&status
,
1022 if (p
->p_ru
== NULL
) {
1025 if (IS_64BIT_PROCESS(q
)) {
1026 struct user_rusage my_rusage
;
1027 munge_rusage(p
->p_ru
, &my_rusage
);
1028 error
= copyout((caddr_t
)&my_rusage
,
1030 sizeof (my_rusage
));
1033 error
= copyout((caddr_t
)p
->p_ru
,
1035 sizeof (struct rusage
));
1038 /* information unavailable? */
1044 if (!reap_child_locked(q
, p
, 0, 0, 0)) {
1046 p
->p_listflag
&= ~P_LIST_WAITING
;
1053 if (p
->p_stat
== SSTOP
&& (p
->p_lflag
& P_LWAITED
) == 0 &&
1054 (p
->p_lflag
& P_LTRACED
|| uap
->options
& WUNTRACED
)) {
1057 if ((error
= mac_proc_check_wait(q
, p
)) != 0)
1061 p
->p_lflag
|= P_LWAITED
;
1063 retval
[0] = p
->p_pid
;
1065 status
= W_STOPCODE(p
->p_xstat
);
1066 error
= copyout((caddr_t
)&status
,
1074 * If we are waiting for continued processses, and this
1075 * process was continued
1077 if ((uap
->options
& WCONTINUED
) &&
1078 (p
->p_flag
& P_CONTINUED
)) {
1081 if ((error
= mac_proc_check_wait(q
, p
)) != 0)
1085 /* Prevent other process for waiting for this event */
1086 OSBitAndAtomic(~((uint32_t)P_CONTINUED
), (UInt32
*)&p
->p_flag
);
1087 retval
[0] = p
->p_pid
;
1089 status
= W_STOPCODE(SIGCONT
);
1090 error
= copyout((caddr_t
)&status
,
1097 p
->p_listflag
&= ~P_LIST_WAITING
;
1100 /* list lock is held when we get here any which way */
1106 if (uap
->options
& WNOHANG
) {
1112 if ((error
= msleep0((caddr_t
)q
, proc_list_mlock
, PWAIT
| PCATCH
| PDROP
, "wait", 0, wait1continue
)))
1118 p
->p_listflag
&= ~P_LIST_WAITING
;
1126 waitidcontinue(int result
)
1135 thread
= current_thread();
1136 vt
= get_bsduthreadarg(thread
);
1137 retval
= get_bsduthreadrval(thread
);
1138 return(waitid(current_proc(), (struct waitid_args
*)vt
, retval
));
1142 * Description: Suspend the calling thread until one child of the process
1143 * containing the calling thread changes state.
1145 * Parameters: uap->idtype one of P_PID, P_PGID, P_ALL
1146 * uap->id pid_t or gid_t or ignored
1147 * uap->infop Address of signinfo_t struct in
1148 * user space into which to return status
1149 * uap->options flag values
1151 * Returns: 0 Success
1152 * !0 Error returning status to user space
1155 waitid(proc_t q
, struct waitid_args
*uap
, register_t
*retval
)
1157 __pthread_testcancel(1);
1158 return(waitid_nocancel(q
, (struct waitid_nocancel_args
*)uap
, retval
));
1162 waitid_nocancel(proc_t q
, struct waitid_nocancel_args
*uap
, __unused register_t
*retval
)
1164 user_siginfo_t collect64
; /* siginfo data to return to caller */
1171 * Forced validation of options for T.waitpid 21; should be a TSD!
1172 * This will pass the test, but note that we have more bits than the
1173 * standard specifies that we will allow in, in this case. The test
1174 * passes because they light all the bits, not just the ones we allow,
1175 * and so the following check returns EINVAL like the test wants.
1177 if (((uap
->options
& (WNOHANG
|WNOWAIT
|WCONTINUED
|WUNTRACED
|WSTOPPED
|WEXITED
)) != uap
->options
) ||
1178 (uap
->options
== 0))
1179 return (EINVAL
); /* bits set that aren't recognized */
1182 * Overly critical options checking, per POSIX
1184 switch(uap
->idtype
) {
1185 case P_PID
: /* child with process ID equal to... */
1186 case P_PGID
: /* child with process group ID equal to... */
1187 if (((int)uap
->id
) < 0)
1190 case P_ALL
: /* any child */
1198 for (p
= q
->p_children
.lh_first
; p
!= 0; p
= p
->p_sibling
.le_next
) {
1199 switch(uap
->idtype
) {
1200 case P_PID
: /* child with process ID equal to... */
1201 if (p
->p_pid
!= (pid_t
)uap
->id
)
1204 case P_PGID
: /* child with process group ID equal to... */
1205 if (p
->p_pgrpid
!= (pid_t
)uap
->id
)
1208 case P_ALL
: /* any child */
1212 /* XXX This is racy because we don't get the lock!!!! */
1215 * Wait collision; go to sleep and restart; used to maintain
1216 * the single return for waited process guarantee.
1218 if (p
->p_listflag
& P_LIST_WAITING
) {
1219 (void)msleep(&p
->p_stat
, proc_list_mlock
, PWAIT
, "waitidcoll", 0);
1222 p
->p_listflag
|= P_LIST_WAITING
; /* mark busy */
1227 * Types of processes we are interested in
1229 * XXX Don't know what to do for WCONTINUED?!?
1232 case SZOMB
: /* Exited */
1233 if (!(uap
->options
& WEXITED
))
1236 /* drop the lock and the thread is going to return */
1239 /* Collect "siginfo" information for caller */
1240 collect64
.si_signo
= SIGCHLD
;
1241 collect64
.si_code
= 0;
1242 collect64
.si_errno
= 0;
1243 collect64
.si_pid
= 0;
1244 collect64
.si_uid
= 0;
1245 collect64
.si_addr
= 0;
1246 collect64
.si_status
= WEXITSTATUS(p
->p_xstat
);
1247 collect64
.si_band
= 0;
1249 if (IS_64BIT_PROCESS(p
)) {
1250 error
= copyout((caddr_t
)&collect64
,
1255 siginfo_64to32(&collect64
,&collect
);
1256 error
= copyout((caddr_t
)&collect
,
1260 /* information unavailable? */
1264 /* Prevent other process for waiting for this event? */
1265 if (!(uap
->options
& WNOWAIT
)) {
1267 if (!reap_child_locked(q
, p
, 0, 0, 0)) {
1269 p
->p_listflag
&= ~P_LIST_WAITING
;
1275 p
->p_listflag
&= ~P_LIST_WAITING
;
1281 case SSTOP
: /* Stopped */
1283 * If we are not interested in stopped processes, then
1286 if (!(uap
->options
& WSTOPPED
))
1290 * If someone has already waited it, we lost a race
1291 * to be the one to return status.
1293 if ((p
->p_lflag
& P_LWAITED
) != 0)
1296 /* drop the lock and the thread is going to return */
1299 /* Collect "siginfo" information for caller */
1300 collect64
.si_signo
= SIGCHLD
;
1301 collect64
.si_code
= 0;
1302 collect64
.si_errno
= 0;
1303 collect64
.si_pid
= 0;
1304 collect64
.si_uid
= 0;
1305 collect64
.si_addr
= 0;
1307 collect64
.si_status
= p
->p_xstat
;
1309 collect64
.si_band
= 0;
1311 if (IS_64BIT_PROCESS(p
)) {
1312 error
= copyout((caddr_t
)&collect64
,
1317 siginfo_64to32(&collect64
,&collect
);
1318 error
= copyout((caddr_t
)&collect
,
1322 /* information unavailable? */
1326 /* Prevent other process for waiting for this event? */
1327 if (!(uap
->options
& WNOWAIT
)) {
1329 p
->p_lflag
|= P_LWAITED
;
1336 default: /* All others */
1337 /* ...meaning Continued */
1338 if (!(uap
->options
& WCONTINUED
))
1342 * If the flag isn't set, then this process has not
1343 * been stopped and continued, or the status has
1344 * already been reaped by another caller of waitid().
1346 if ((p
->p_flag
& P_CONTINUED
) == 0)
1349 /* drop the lock and the thread is going to return */
1352 /* Collect "siginfo" information for caller */
1354 collect64
.si_signo
= SIGCHLD
;
1355 collect64
.si_code
= CLD_CONTINUED
;
1356 collect64
.si_errno
= 0;
1357 collect64
.si_pid
= p
->p_contproc
;
1358 collect64
.si_uid
= 0;
1359 collect64
.si_addr
= 0;
1360 collect64
.si_status
= p
->p_xstat
;
1361 collect64
.si_band
= 0;
1364 if (IS_64BIT_PROCESS(p
)) {
1365 error
= copyout((caddr_t
)&collect64
,
1370 siginfo_64to32(&collect64
,&collect
);
1371 error
= copyout((caddr_t
)&collect
,
1375 /* information unavailable? */
1379 /* Prevent other process for waiting for this event? */
1380 if (!(uap
->options
& WNOWAIT
)) {
1381 OSBitAndAtomic(~((uint32_t)P_CONTINUED
), (UInt32
*)&p
->p_flag
);
1387 /* LIST LOCK IS HELD HERE */
1388 /* Not a process we are interested in; go on to next child */
1390 p
->p_listflag
&= ~P_LIST_WAITING
;
1394 /* list lock is always held */
1395 /* No child processes that could possibly satisfy the request? */
1401 if (uap
->options
& WNOHANG
) {
1406 if ((error
= msleep0((caddr_t
)q
, proc_list_mlock
, PWAIT
| PCATCH
| PDROP
, "waitid", 0, waitidcontinue
)))
1412 p
->p_listflag
&= ~P_LIST_WAITING
;
1419 * make process 'parent' the new parent of process 'child'.
1422 proc_reparentlocked(proc_t child
, proc_t parent
, int cansignal
, int locked
)
1424 proc_t oldparent
= PROC_NULL
;
1426 if (child
->p_pptr
== parent
)
1432 oldparent
= child
->p_pptr
;
1433 #if __PROC_INTERNAL_DEBUG
1434 if (oldparent
== PROC_NULL
)
1435 panic("proc_reparent: process %x does not have a parent\n", (unsigned int)child
);
1438 LIST_REMOVE(child
, p_sibling
);
1439 #if __PROC_INTERNAL_DEBUG
1440 if (oldparent
->p_childrencnt
== 0)
1441 panic("process children count already 0\n");
1443 oldparent
->p_childrencnt
--;
1444 #if __PROC_INTERNAL_DEBUG1
1445 if (oldparent
->p_childrencnt
< 0)
1446 panic("process children count -ve\n");
1448 LIST_INSERT_HEAD(&parent
->p_children
, child
, p_sibling
);
1449 parent
->p_childrencnt
++;
1450 child
->p_pptr
= parent
;
1451 child
->p_ppid
= parent
->p_pid
;
1455 if ((cansignal
!= 0) && (initproc
== parent
) && (child
->p_stat
== SZOMB
))
1456 psignal(initproc
, SIGCHLD
);
1462 * Make the current process an "init" process, meaning
1463 * that it doesn't have a parent, and that it won't be
1464 * gunned down by kill(-1, 0).
1467 init_process(__unused
struct init_process_args
*args
)
1469 proc_t p
= current_proc();
1471 AUDIT_MACH_SYSCALL_ENTER(AUE_INITPROCESS
);
1472 if (suser(kauth_cred_get(), &p
->p_acflag
)) {
1473 AUDIT_MACH_SYSCALL_EXIT(KERN_NO_ACCESS
);
1474 return(KERN_NO_ACCESS
);
1477 if (p
->p_pid
!= 1 && p
->p_pgrpid
!= p
->p_pid
)
1478 enterpgrp(p
, p
->p_pid
, 0);
1479 OSBitOrAtomic(P_SYSTEM
, (UInt32
*)&p
->p_flag
);
1482 * Take us out of the sibling chain, and
1483 * out of our parent's child chain.
1486 LIST_REMOVE(p
, p_sibling
);
1487 p
->p_sibling
.le_prev
= NULL
;
1488 p
->p_sibling
.le_next
= NULL
;
1489 p
->p_pptr
= kernproc
;
1494 AUDIT_MACH_SYSCALL_EXIT(KERN_SUCCESS
);
1495 return(KERN_SUCCESS
);
1500 * Exit: deallocate address space and other resources, change proc state
1501 * to zombie, and unlink proc from allproc and parent's lists. Save exit
1502 * status and rusage for wait(). Check for child processes and orphan them.
1506 vfork_exit(proc_t p
, int rv
)
1508 vfork_exit_internal(p
, rv
, 0);
1512 vfork_exit_internal(proc_t p
, int rv
, int forceexit
)
1514 thread_t self
= current_thread();
1516 struct task
*task
= p
->task
;
1521 * If a thread in this task has already
1522 * called exit(), then halt any others
1526 ut
= get_bsdthread_info(self
);
1530 if ((p
->p_lflag
& P_LPEXIT
) == P_LPEXIT
) {
1532 * This happens when a parent exits/killed and vfork is in progress
1533 * other threads. But shutdown code for ex has already called exit1()
1538 p
->p_lflag
|= (P_LEXIT
| P_LPEXIT
);
1541 if (forceexit
== 0) {
1543 * parent of a vfork child has already called exit() and the
1544 * thread that has vfork in proress terminates. So there is no
1545 * separate address space here and it has already been marked for
1546 * termination. This was never covered before and could cause problems
1547 * if we block here for outside code.
1549 /* Notify the perf server */
1550 (void)sys_perf_notify(self
, p
->p_pid
);
1554 * Remove proc from allproc queue and from pidhash chain.
1555 * Need to do this before we do anything that can block.
1556 * Not doing causes things like mount() find this on allproc
1557 * in partially cleaned state.
1562 LIST_REMOVE(p
, p_list
);
1563 LIST_INSERT_HEAD(&zombproc
, p
, p_list
); /* Place onto zombproc. */
1564 /* will not be visible via proc_find */
1565 p
->p_listflag
|= P_LIST_EXITED
;
1571 p
->p_lflag
&= ~(P_LTRACED
| P_LPPWAIT
);
1572 p
->p_sigignore
= ~0;
1576 if (thread_call_cancel(p
->p_rcall
))
1579 while (p
->p_ractive
> 0) {
1588 thread_call_free(p
->p_rcall
);
1597 vproc_exit(proc_t p
)
1604 struct task
*task
= p
->task
;
1607 struct session
*sessp
;
1610 /* XXX Zombie allocation may fail, in which case stats get lost */
1611 MALLOC_ZONE(p
->p_ru
, struct rusage
*,
1612 sizeof (*p
->p_ru
), M_ZOMBIE
, M_WAITOK
);
1618 * Close open files and release open-file table.
1623 sessp
= proc_session(p
);
1624 if (SESS_LEADER(p
, sessp
)) {
1626 /* Protected by funnel for tty accesses */
1627 fstate
= thread_funnel_set(kernel_flock
, TRUE
);
1629 if (sessp
->s_ttyvp
!= NULLVP
) {
1630 struct vnode
*ttyvp
;
1632 struct vfs_context context
;
1636 * Controlling process.
1637 * Signal foreground pgrp,
1638 * drain controlling terminal
1639 * and revoke access to controlling terminal.
1643 if ((tp
!= TTY_NULL
) && (tp
->t_session
== sessp
)) {
1644 tty_pgsignal(tp
, SIGHUP
, 1);
1647 * The tty could have been revoked
1651 session_lock(sessp
);
1652 ttyvp
= sessp
->s_ttyvp
;
1653 ttyvid
= sessp
->s_ttyvid
;
1654 sessp
->s_ttyvp
= NULL
;
1655 sessp
->s_ttyvid
= 0;
1656 sessp
->s_ttyp
= NULL
;
1657 sessp
->s_ttypgrpid
= NO_PID
;
1658 session_unlock(sessp
);
1660 if ((ttyvp
!= NULLVP
) && (vnode_getwithvid(ttyvp
, ttyvid
) == 0)) {
1661 context
.vc_thread
= proc_thread(p
); /* XXX */
1662 context
.vc_ucred
= kauth_cred_proc_ref(p
);
1663 VNOP_REVOKE(ttyvp
, REVOKEALL
, &context
);
1665 kauth_cred_unref(&context
.vc_ucred
);
1668 session_lock(sessp
);
1669 ttyvp
= sessp
->s_ttyvp
;
1670 sessp
->s_ttyvp
= NULL
;
1671 sessp
->s_ttyvid
= 0;
1672 sessp
->s_ttyp
= NULL
;
1673 sessp
->s_ttypgrpid
= NO_PID
;
1674 session_unlock(sessp
);
1679 * s_ttyp is not zero'd; we use this to indicate
1680 * that the session once had a controlling terminal.
1681 * (for logging and informational purposes)
1684 (void) thread_funnel_set(kernel_flock
, fstate
);
1686 session_lock(sessp
);
1687 sessp
->s_leader
= NULL
;
1688 session_unlock(sessp
);
1690 session_rele(sessp
);
1696 p
->p_rlimit
[RLIMIT_FSIZE
].rlim_cur
= RLIM_INFINITY
;
1699 proc_childdrainstart(p
);
1700 while ((q
= p
->p_children
.lh_first
) != NULL
) {
1701 q
->p_listflag
|= P_LIST_DEADPARENT
;
1702 if (q
->p_stat
== SZOMB
) {
1704 panic("parent child linkage broken");
1705 /* check for lookups by zomb sysctl */
1706 while ((q
->p_listflag
& P_LIST_WAITING
) == P_LIST_WAITING
) {
1707 msleep(&q
->p_stat
, proc_list_mlock
, PWAIT
, "waitcoll", 0);
1709 q
->p_listflag
|= P_LIST_WAITING
;
1711 * This is a named reference and it is not granted
1712 * if the reap is already in progress. So we get
1713 * the reference here exclusively and their can be
1714 * no waiters. So there is no need for a wakeup
1715 * after we are done. AlsO the reap frees the structure
1716 * and the proc struct cannot be used for wakeups as well.
1717 * It is safe to use q here as this is system reap
1719 (void)reap_child_locked(p
, q
, 1, 1, 0);
1721 proc_reparentlocked(q
, initproc
, 0, 1);
1723 * Traced processes are killed
1724 * since their existence means someone is messing up.
1726 if (q
->p_lflag
& P_LTRACED
) {
1729 q
->p_lflag
&= ~P_LTRACED
;
1730 if (q
->sigwait_thread
) {
1733 * The sigwait_thread could be stopped at a
1734 * breakpoint. Wake it up to kill.
1735 * Need to do this as it could be a thread which is not
1736 * the first thread in the task. So any attempts to kill
1737 * the process would result into a deadlock on q->sigwait.
1739 thread_resume((thread_t
)q
->sigwait_thread
);
1740 clear_wait(q
->sigwait_thread
, THREAD_INTERRUPTED
);
1741 threadsignal((thread_t
)q
->sigwait_thread
, SIGKILL
, 0);
1745 psignal(q
, SIGKILL
);
1751 proc_childdrainend(p
);
1755 * Release reference to text vnode
1759 if (tvp
!= NULLVP
) {
1764 * Save exit status and final rusage info, adding in child rusage
1765 * info and self times. If we were unable to allocate a zombie
1766 * structure, this information is lost.
1768 /* No need for locking here as no one than this thread can access this */
1769 if (p
->p_ru
!= NULL
) {
1770 *p
->p_ru
= p
->p_stats
->p_ru
;
1771 timerclear(&p
->p_ru
->ru_utime
);
1772 timerclear(&p
->p_ru
->ru_stime
);
1776 task_basic_info_data_t tinfo
;
1777 task_thread_times_info_data_t ttimesinfo
;
1778 int task_info_stuff
, task_ttimes_stuff
;
1779 struct timeval ut
,st
;
1781 task_info_stuff
= TASK_BASIC_INFO_COUNT
;
1782 task_info(task
, TASK_BASIC_INFO
,
1783 &tinfo
, &task_info_stuff
);
1784 p
->p_ru
->ru_utime
.tv_sec
= tinfo
.user_time
.seconds
;
1785 p
->p_ru
->ru_utime
.tv_usec
= tinfo
.user_time
.microseconds
;
1786 p
->p_ru
->ru_stime
.tv_sec
= tinfo
.system_time
.seconds
;
1787 p
->p_ru
->ru_stime
.tv_usec
= tinfo
.system_time
.microseconds
;
1789 task_ttimes_stuff
= TASK_THREAD_TIMES_INFO_COUNT
;
1790 task_info(task
, TASK_THREAD_TIMES_INFO
,
1791 &ttimesinfo
, &task_ttimes_stuff
);
1793 ut
.tv_sec
= ttimesinfo
.user_time
.seconds
;
1794 ut
.tv_usec
= ttimesinfo
.user_time
.microseconds
;
1795 st
.tv_sec
= ttimesinfo
.system_time
.seconds
;
1796 st
.tv_usec
= ttimesinfo
.system_time
.microseconds
;
1797 timeradd(&ut
,&p
->p_ru
->ru_utime
,&p
->p_ru
->ru_utime
);
1798 timeradd(&st
,&p
->p_ru
->ru_stime
,&p
->p_ru
->ru_stime
);
1802 ruadd(p
->p_ru
, &p
->p_stats
->p_cru
);
1806 * Free up profiling buffers.
1809 struct uprof
*p0
= &p
->p_stats
->p_prof
, *p1
, *pn
;
1815 for (; p1
!= NULL
; p1
= pn
) {
1817 kfree(p1
, sizeof *p1
);
1822 * Other substructures are freed from wait().
1824 FREE_ZONE(p
->p_stats
, sizeof *p
->p_stats
, M_PSTATS
);
1827 FREE_ZONE(p
->p_sigacts
, sizeof *p
->p_sigacts
, M_SIGACTS
);
1828 p
->p_sigacts
= NULL
;
1830 proc_limitdrop(p
, 1);
1834 * Finish up by terminating the task
1835 * and halt this thread (only if a
1836 * member of the task exiting).
1838 p
->task
= TASK_NULL
;
1841 * Notify parent that we're gone.
1843 pp
= proc_parent(p
);
1844 if ((p
->p_listflag
& P_LIST_DEADPARENT
) == 0) {
1845 if (pp
!= initproc
) {
1847 pp
->si_pid
= p
->p_pid
;
1848 pp
->si_status
= p
->p_xstat
;
1849 pp
->si_code
= CLD_EXITED
;
1851 * p_ucred usage is safe as it is an exiting process
1852 * and reference is dropped in reap
1854 pp
->si_uid
= p
->p_ucred
->cr_ruid
;
1857 /* mark as a zombie */
1858 /* mark as a zombie */
1859 /* No need to take proc lock as all refs are drained and
1860 * no one except parent (reaping ) can look at this.
1861 * The write is to an int and is coherent. Also parent is
1862 * keyed off of list lock for reaping
1866 psignal(pp
, SIGCHLD
);
1868 /* and now wakeup the parent */
1870 wakeup((caddr_t
)pp
);
1875 /* check for lookups by zomb sysctl */
1876 while ((p
->p_listflag
& P_LIST_WAITING
) == P_LIST_WAITING
) {
1877 msleep(&p
->p_stat
, proc_list_mlock
, PWAIT
, "waitcoll", 0);
1879 p
->p_listflag
|= P_LIST_WAITING
;
1881 * This is a named reference and it is not granted
1882 * if the reap is already in progress. So we get
1883 * the reference here exclusively and their can be
1884 * no waiters. So there is no need for a wakeup
1885 * after we are done. AlsO the reap frees the structure
1886 * and the proc struct cannot be used for wakeups as well.
1887 * It is safe to use p here as this is system reap
1889 (void)reap_child_locked(pp
, p
, 0, 1, 1);
1890 /* list lock dropped by reap_child_locked */
1898 * LP64 support - long is 64 bits if we are dealing with a 64 bit user
1899 * process. We munge the kernel (32 bit) version of rusage into the
1902 __private_extern__
void
1903 munge_rusage(struct rusage
*a_rusage_p
, struct user_rusage
*a_user_rusage_p
)
1905 /* timeval changes size, so utime and stime need special handling */
1906 a_user_rusage_p
->ru_utime
.tv_sec
= a_rusage_p
->ru_utime
.tv_sec
;
1907 a_user_rusage_p
->ru_utime
.tv_usec
= a_rusage_p
->ru_utime
.tv_usec
;
1908 a_user_rusage_p
->ru_stime
.tv_sec
= a_rusage_p
->ru_stime
.tv_sec
;
1909 a_user_rusage_p
->ru_stime
.tv_usec
= a_rusage_p
->ru_stime
.tv_usec
;
1911 * everything else can be a direct assign, since there is no loss
1912 * of precision implied boing 32->64.
1914 a_user_rusage_p
->ru_maxrss
= a_rusage_p
->ru_maxrss
;
1915 a_user_rusage_p
->ru_ixrss
= a_rusage_p
->ru_ixrss
;
1916 a_user_rusage_p
->ru_idrss
= a_rusage_p
->ru_idrss
;
1917 a_user_rusage_p
->ru_isrss
= a_rusage_p
->ru_isrss
;
1918 a_user_rusage_p
->ru_minflt
= a_rusage_p
->ru_minflt
;
1919 a_user_rusage_p
->ru_majflt
= a_rusage_p
->ru_majflt
;
1920 a_user_rusage_p
->ru_nswap
= a_rusage_p
->ru_nswap
;
1921 a_user_rusage_p
->ru_inblock
= a_rusage_p
->ru_inblock
;
1922 a_user_rusage_p
->ru_oublock
= a_rusage_p
->ru_oublock
;
1923 a_user_rusage_p
->ru_msgsnd
= a_rusage_p
->ru_msgsnd
;
1924 a_user_rusage_p
->ru_msgrcv
= a_rusage_p
->ru_msgrcv
;
1925 a_user_rusage_p
->ru_nsignals
= a_rusage_p
->ru_nsignals
;
1926 a_user_rusage_p
->ru_nvcsw
= a_rusage_p
->ru_nvcsw
;
1927 a_user_rusage_p
->ru_nivcsw
= a_rusage_p
->ru_nivcsw
;