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.
422 /* Close ref SYSV Shared memory*/
427 /* Release SYSV semaphores */
431 sessp
= proc_session(p
);
432 if (SESS_LEADER(p
, sessp
)) {
434 /* Protected by funnel for tty accesses */
435 fstate
= thread_funnel_set(kernel_flock
, TRUE
);
437 if (sessp
->s_ttyvp
!= NULLVP
) {
440 struct vfs_context context
;
445 * Controlling process.
446 * Signal foreground pgrp,
447 * drain controlling terminal
448 * and revoke access to controlling terminal.
452 if ((tp
!= TTY_NULL
) && (tp
->t_session
== sessp
)) {
453 tty_pgsignal(tp
, SIGHUP
, 1);
456 * The tty could have been revoked
461 ttyvp
= sessp
->s_ttyvp
;
462 ttyvid
= sessp
->s_ttyvid
;
463 sessp
->s_ttyvp
= NULL
;
465 sessp
->s_ttyp
= NULL
;
466 sessp
->s_ttypgrpid
= NO_PID
;
467 session_unlock(sessp
);
469 if ((ttyvp
!= NULLVP
) && (vnode_getwithvid(ttyvp
, ttyvid
) == 0)) {
470 context
.vc_thread
= proc_thread(p
); /* XXX */
471 context
.vc_ucred
= kauth_cred_proc_ref(p
);
472 VNOP_REVOKE(ttyvp
, REVOKEALL
, &context
);
474 kauth_cred_unref(&context
.vc_ucred
);
478 ttyvp
= sessp
->s_ttyvp
;
479 sessp
->s_ttyvp
= NULL
;
481 sessp
->s_ttyp
= NULL
;
482 sessp
->s_ttypgrpid
= NO_PID
;
483 session_unlock(sessp
);
488 * s_ttyp is not zero'd; we use this to indicate
489 * that the session once had a controlling terminal.
490 * (for logging and informational purposes)
494 (void) thread_funnel_set(kernel_flock
, fstate
);
496 sessp
->s_leader
= NULL
;
497 session_unlock(sessp
);
505 p
->p_rlimit
[RLIMIT_FSIZE
].rlim_cur
= RLIM_INFINITY
;
506 (void)acct_process(p
);
509 /* wait till parentrefs are dropped and grant no more */
510 proc_childdrainstart(p
);
511 while ((q
= p
->p_children
.lh_first
) != NULL
) {
512 q
->p_listflag
|= P_LIST_DEADPARENT
;
513 if (q
->p_stat
== SZOMB
) {
515 panic("parent child linkage broken");
516 /* check for sysctl zomb lookup */
517 while ((q
->p_listflag
& P_LIST_WAITING
) == P_LIST_WAITING
) {
518 msleep(&q
->p_stat
, proc_list_mlock
, PWAIT
, "waitcoll", 0);
520 q
->p_listflag
|= P_LIST_WAITING
;
522 * This is a named reference and it is not granted
523 * if the reap is already in progress. So we get
524 * the reference here exclusively and their can be
525 * no waiters. So there is no need for a wakeup
526 * after we are done. AlsO the reap frees the structure
527 * and the proc struct cannot be used for wakeups as well.
528 * It is safe to use q here as this is system reap
530 (void)reap_child_locked(p
, q
, 1, 1, 0);
532 proc_reparentlocked(q
, initproc
, 0, 1);
534 * Traced processes are killed
535 * since their existence means someone is messing up.
537 if (q
->p_lflag
& P_LTRACED
) {
540 q
->p_lflag
&= ~P_LTRACED
;
541 if (q
->sigwait_thread
) {
544 * The sigwait_thread could be stopped at a
545 * breakpoint. Wake it up to kill.
546 * Need to do this as it could be a thread which is not
547 * the first thread in the task. So any attempts to kill
548 * the process would result into a deadlock on q->sigwait.
550 thread_resume((thread_t
)q
->sigwait_thread
);
551 clear_wait(q
->sigwait_thread
, THREAD_INTERRUPTED
);
552 threadsignal((thread_t
)q
->sigwait_thread
, SIGKILL
, 0);
561 proc_childdrainend(p
);
565 * Release reference to text vnode
574 * Save exit status and final rusage info, adding in child rusage
575 * info and self times. If we were unable to allocate a zombie
576 * structure, this information is lost.
578 /* No need for locking here as no one than this thread can access this */
579 if (p
->p_ru
!= NULL
) {
580 *p
->p_ru
= p
->p_stats
->p_ru
;
582 timerclear(&p
->p_ru
->ru_utime
);
583 timerclear(&p
->p_ru
->ru_stime
);
586 task_basic_info_32_data_t tinfo
;
587 task_thread_times_info_data_t ttimesinfo
;
588 task_events_info_data_t teventsinfo
;
589 mach_msg_type_number_t task_info_stuff
, task_ttimes_stuff
;
590 mach_msg_type_number_t task_events_stuff
;
591 struct timeval ut
,st
;
593 task_info_stuff
= TASK_BASIC_INFO_32_COUNT
;
594 task_info(task
, TASK_BASIC2_INFO_32
,
595 (task_info_t
)&tinfo
, &task_info_stuff
);
596 p
->p_ru
->ru_utime
.tv_sec
= tinfo
.user_time
.seconds
;
597 p
->p_ru
->ru_utime
.tv_usec
= tinfo
.user_time
.microseconds
;
598 p
->p_ru
->ru_stime
.tv_sec
= tinfo
.system_time
.seconds
;
599 p
->p_ru
->ru_stime
.tv_usec
= tinfo
.system_time
.microseconds
;
601 p
->p_ru
->ru_maxrss
= tinfo
.resident_size
;
603 task_ttimes_stuff
= TASK_THREAD_TIMES_INFO_COUNT
;
604 task_info(task
, TASK_THREAD_TIMES_INFO
,
605 (task_info_t
)&ttimesinfo
, &task_ttimes_stuff
);
607 ut
.tv_sec
= ttimesinfo
.user_time
.seconds
;
608 ut
.tv_usec
= ttimesinfo
.user_time
.microseconds
;
609 st
.tv_sec
= ttimesinfo
.system_time
.seconds
;
610 st
.tv_usec
= ttimesinfo
.system_time
.microseconds
;
611 timeradd(&ut
,&p
->p_ru
->ru_utime
,&p
->p_ru
->ru_utime
);
612 timeradd(&st
,&p
->p_ru
->ru_stime
,&p
->p_ru
->ru_stime
);
614 task_events_stuff
= TASK_EVENTS_INFO_COUNT
;
615 task_info(task
, TASK_EVENTS_INFO
,
616 (task_info_t
)&teventsinfo
, &task_events_stuff
);
618 p
->p_ru
->ru_minflt
= (teventsinfo
.faults
-
619 teventsinfo
.pageins
);
620 p
->p_ru
->ru_majflt
= teventsinfo
.pageins
;
621 p
->p_ru
->ru_nivcsw
= (teventsinfo
.csw
-
623 if (p
->p_ru
->ru_nivcsw
< 0)
624 p
->p_ru
->ru_nivcsw
= 0;
627 ruadd(p
->p_ru
, &p
->p_stats
->p_cru
);
631 * Free up profiling buffers.
634 struct uprof
*p0
= &p
->p_stats
->p_prof
, *p1
, *pn
;
640 for (; p1
!= NULL
; p1
= pn
) {
642 kfree(p1
, sizeof *p1
);
647 if (thread_call_cancel(p
->p_rcall
))
650 while (p
->p_ractive
> 0) {
659 thread_call_free(p
->p_rcall
);
663 * Other substructures are freed from wait().
665 FREE_ZONE(p
->p_stats
, sizeof *p
->p_stats
, M_PSTATS
);
668 FREE_ZONE(p
->p_sigacts
, sizeof *p
->p_sigacts
, M_SIGACTS
);
671 proc_limitdrop(p
, 1);
676 * Finish up by terminating the task
677 * and halt this thread (only if a
678 * member of the task exiting).
681 set_bsdtask_info(task
, NULL
);
683 proc_knote(p
, NOTE_EXIT
);
685 /* mark the thread as the one that is doing proc_exit
686 * no need to hold proc lock in uthread_free
688 uth
->uu_flag
|= UT_PROCEXIT
;
690 * Notify parent that we're gone.
693 if (pp
->p_flag
& P_NOCLDWAIT
) {
697 * If the parent is ignoring SIGCHLD, then POSIX requires
698 * us to not add the resource usage to the parent process -
699 * we are only going to hand it off to init to get reaped.
700 * We should contest the standard in this case on the basis
705 * Add child resource usage to parent before giving
706 * zombie to init. If we were unable to allocate a
707 * zombie structure, this information is lost.
709 if (p
->p_ru
!= NULL
) {
711 ruadd(&pp
->p_stats
->p_cru
, p
->p_ru
);
714 #endif /* !3839178 */
716 /* kernel can reap this one, no need to move it to launchd */
718 p
->p_listflag
|= P_LIST_DEADPARENT
;
721 if ((p
->p_listflag
& P_LIST_DEADPARENT
) == 0) {
722 if (pp
!= initproc
) {
724 pp
->si_pid
= p
->p_pid
;
725 pp
->si_status
= p
->p_xstat
;
726 pp
->si_code
= CLD_EXITED
;
728 * p_ucred usage is safe as it is an exiting process
729 * and reference is dropped in reap
731 pp
->si_uid
= p
->p_ucred
->cr_ruid
;
734 /* mark as a zombie */
735 /* No need to take proc lock as all refs are drained and
736 * no one except parent (reaping ) can look at this.
737 * The write is to an int and is coherent. Also parent is
738 * keyed off of list lock for reaping
742 * The current process can be reaped so, no one
746 psignal(pp
, SIGCHLD
);
748 /* and now wakeup the parent */
753 /* should be fine as parent proc would be initproc */
754 /* mark as a zombie */
755 /* No need to take proc lock as all refs are drained and
756 * no one except parent (reaping ) can look at this.
757 * The write is to an int and is coherent. Also parent is
758 * keyed off of list lock for reaping
762 /* check for sysctl zomb lookup */
763 while ((p
->p_listflag
& P_LIST_WAITING
) == P_LIST_WAITING
) {
764 msleep(&p
->p_stat
, proc_list_mlock
, PWAIT
, "waitcoll", 0);
766 /* safe to use p as this is a system reap */
767 p
->p_listflag
|= P_LIST_WAITING
;
769 * This is a named reference and it is not granted
770 * if the reap is already in progress. So we get
771 * the reference here exclusively and their can be
772 * no waiters. So there is no need for a wakeup
773 * after we are done. AlsO the reap frees the structure
774 * and the proc struct cannot be used for wakeups as well.
775 * It is safe to use p here as this is system reap
777 (void)reap_child_locked(pp
, p
, 1, 1, 1);
778 /* list lock dropped by reap_child_locked */
789 * Description: Given a process from which all status information needed
790 * has already been extracted, if the process is a ptrace
791 * attach process, detach it and give it back to its real
792 * parent, else recover all resources remaining associated
795 * Parameters: proc_t parent Parent of process being reaped
796 * proc_t child Process to reap
798 * Returns: 0 Process was not reaped because it
799 * came from an attach
800 * 1 Process was reaped
803 reap_child_locked(proc_t parent
, proc_t child
, int deadparent
, int locked
, int droplock
)
805 proc_t trace_parent
; /* Traced parent process, if tracing */
808 * If we got the child via a ptrace 'attach',
809 * we need to give it back to the old parent.
813 if (child
->p_oppid
&& (trace_parent
= proc_find(child
->p_oppid
))) {
817 if (trace_parent
!= initproc
) {
819 * proc internal fileds and p_ucred usage safe
820 * here as child is dead and is not reaped or
823 proc_lock(trace_parent
);
824 trace_parent
->si_pid
= child
->p_pid
;
825 trace_parent
->si_status
= child
->p_xstat
;
826 trace_parent
->si_code
= CLD_CONTINUED
;
827 trace_parent
->si_uid
= child
->p_ucred
->cr_ruid
;
828 proc_unlock(trace_parent
);
830 proc_reparentlocked(child
, trace_parent
, 1, 0);
831 psignal(trace_parent
, SIGCHLD
);
833 wakeup((caddr_t
)trace_parent
);
834 child
->p_listflag
&= ~P_LIST_WAITING
;
835 wakeup(&child
->p_stat
);
837 proc_rele(trace_parent
);
838 if ((locked
== 1) && (droplock
== 0))
843 proc_knote(child
, NOTE_REAP
);
850 * If the parent is ignoring SIGCHLD, then POSIX requires
851 * us to not add the resource usage to the parent process -
852 * we are only going to hand it off to init to get reaped.
853 * We should contest the standard in this case on the basis
856 if (!(parent
->p_flag
& P_NOCLDWAIT
))
858 ruadd(&parent
->p_stats
->p_cru
, child
->p_ru
);
860 FREE_ZONE(child
->p_ru
, sizeof *child
->p_ru
, M_ZOMBIE
);
863 printf("Warning : lost p_ru for %s\n", child
->p_comm
);
867 * Decrement the count of procs running with this uid.
868 * p_ucred usage is safe here as it is an exited process.
869 * and refernce is dropped after these calls down below
870 * (locking protection is provided by list lock held in chgproccnt)
872 (void)chgproccnt(child
->p_ucred
->cr_ruid
, -1);
881 * Free up credentials.
883 if (IS_VALID_CRED(child
->p_ucred
)) {
884 kauth_cred_unref(&child
->p_ucred
);
887 /* XXXX Note NOT SAFE TO USE p_ucred from this point onwards */
890 * Finally finished with old proc entry.
891 * Unlink it from its process group and free it.
896 LIST_REMOVE(child
, p_list
); /* off zombproc */
897 parent
->p_childrencnt
--;
898 LIST_REMOVE(child
, p_sibling
);
899 /* If there are no more children wakeup parent */
900 if ((deadparent
!= 0) && (LIST_EMPTY(&parent
->p_children
)))
901 wakeup((caddr_t
)parent
); /* with list lock held */
902 child
->p_listflag
&= ~P_LIST_WAITING
;
903 wakeup(&child
->p_stat
);
905 /* Take it out of process hash */
906 LIST_REMOVE(child
, p_hash
);
907 child
->p_listflag
&= ~P_LIST_INHASH
;
908 proc_checkdeadrefs(child
);
913 lck_mtx_destroy(&child
->p_mlock
, proc_lck_grp
);
914 lck_mtx_destroy(&child
->p_fdmlock
, proc_lck_grp
);
916 lck_mtx_destroy(&child
->p_dtrace_sprlock
, proc_lck_grp
);
918 lck_spin_destroy(&child
->p_slock
, proc_lck_grp
);
919 workqueue_destroy_lock(child
);
921 FREE_ZONE(child
, sizeof *child
, M_PROC
);
922 if ((locked
== 1) && (droplock
== 0))
930 wait1continue(int result
)
941 thread
= current_thread();
942 vt
= get_bsduthreadarg(thread
);
943 retval
= get_bsduthreadrval(thread
);
944 return(wait4(p
, (struct wait4_args
*)vt
, retval
));
948 wait4(proc_t q
, struct wait4_args
*uap
, register_t
*retval
)
950 __pthread_testcancel(1);
951 return(wait4_nocancel(q
, (struct wait4_nocancel_args
*)uap
, retval
));
955 wait4_nocancel(proc_t q
, struct wait4_nocancel_args
*uap
, register_t
*retval
)
962 uap
->pid
= -q
->p_pgrpid
;
968 for (p
= q
->p_children
.lh_first
; p
!= 0; p
= p
->p_sibling
.le_next
) {
969 if (uap
->pid
!= WAIT_ANY
&&
970 p
->p_pid
!= uap
->pid
&&
971 p
->p_pgrpid
!= -(uap
->pid
))
976 /* XXX This is racy because we don't get the lock!!!! */
978 if (p
->p_listflag
& P_LIST_WAITING
) {
979 (void)msleep(&p
->p_stat
, proc_list_mlock
, PWAIT
, "waitcoll", 0);
982 p
->p_listflag
|= P_LIST_WAITING
; /* only allow single thread to wait() */
985 if (p
->p_stat
== SZOMB
) {
988 if ((error
= mac_proc_check_wait(q
, p
)) != 0)
991 retval
[0] = p
->p_pid
;
993 /* Legacy apps expect only 8 bits of status */
994 status
= 0xffff & p
->p_xstat
; /* convert to int */
995 error
= copyout((caddr_t
)&status
,
1002 if (p
->p_ru
== NULL
) {
1005 if (IS_64BIT_PROCESS(q
)) {
1006 struct user_rusage my_rusage
;
1007 munge_rusage(p
->p_ru
, &my_rusage
);
1008 error
= copyout((caddr_t
)&my_rusage
,
1010 sizeof (my_rusage
));
1013 error
= copyout((caddr_t
)p
->p_ru
,
1015 sizeof (struct rusage
));
1018 /* information unavailable? */
1024 if (!reap_child_locked(q
, p
, 0, 0, 0)) {
1026 p
->p_listflag
&= ~P_LIST_WAITING
;
1033 if (p
->p_stat
== SSTOP
&& (p
->p_lflag
& P_LWAITED
) == 0 &&
1034 (p
->p_lflag
& P_LTRACED
|| uap
->options
& WUNTRACED
)) {
1037 if ((error
= mac_proc_check_wait(q
, p
)) != 0)
1041 p
->p_lflag
|= P_LWAITED
;
1043 retval
[0] = p
->p_pid
;
1045 status
= W_STOPCODE(p
->p_xstat
);
1046 error
= copyout((caddr_t
)&status
,
1054 * If we are waiting for continued processses, and this
1055 * process was continued
1057 if ((uap
->options
& WCONTINUED
) &&
1058 (p
->p_flag
& P_CONTINUED
)) {
1061 if ((error
= mac_proc_check_wait(q
, p
)) != 0)
1065 /* Prevent other process for waiting for this event */
1066 OSBitAndAtomic(~((uint32_t)P_CONTINUED
), (UInt32
*)&p
->p_flag
);
1067 retval
[0] = p
->p_pid
;
1069 status
= W_STOPCODE(SIGCONT
);
1070 error
= copyout((caddr_t
)&status
,
1077 p
->p_listflag
&= ~P_LIST_WAITING
;
1080 /* list lock is held when we get here any which way */
1086 if (uap
->options
& WNOHANG
) {
1092 if ((error
= msleep0((caddr_t
)q
, proc_list_mlock
, PWAIT
| PCATCH
| PDROP
, "wait", 0, wait1continue
)))
1098 p
->p_listflag
&= ~P_LIST_WAITING
;
1106 waitidcontinue(int result
)
1115 thread
= current_thread();
1116 vt
= get_bsduthreadarg(thread
);
1117 retval
= get_bsduthreadrval(thread
);
1118 return(waitid(current_proc(), (struct waitid_args
*)vt
, retval
));
1122 * Description: Suspend the calling thread until one child of the process
1123 * containing the calling thread changes state.
1125 * Parameters: uap->idtype one of P_PID, P_PGID, P_ALL
1126 * uap->id pid_t or gid_t or ignored
1127 * uap->infop Address of signinfo_t struct in
1128 * user space into which to return status
1129 * uap->options flag values
1131 * Returns: 0 Success
1132 * !0 Error returning status to user space
1135 waitid(proc_t q
, struct waitid_args
*uap
, register_t
*retval
)
1137 __pthread_testcancel(1);
1138 return(waitid_nocancel(q
, (struct waitid_nocancel_args
*)uap
, retval
));
1142 waitid_nocancel(proc_t q
, struct waitid_nocancel_args
*uap
, __unused register_t
*retval
)
1144 user_siginfo_t collect64
; /* siginfo data to return to caller */
1151 * Forced validation of options for T.waitpid 21; should be a TSD!
1152 * This will pass the test, but note that we have more bits than the
1153 * standard specifies that we will allow in, in this case. The test
1154 * passes because they light all the bits, not just the ones we allow,
1155 * and so the following check returns EINVAL like the test wants.
1157 if (((uap
->options
& (WNOHANG
|WNOWAIT
|WCONTINUED
|WUNTRACED
|WSTOPPED
|WEXITED
)) != uap
->options
) ||
1158 (uap
->options
== 0))
1159 return (EINVAL
); /* bits set that aren't recognized */
1162 * Overly critical options checking, per POSIX
1164 switch(uap
->idtype
) {
1165 case P_PID
: /* child with process ID equal to... */
1166 case P_PGID
: /* child with process group ID equal to... */
1167 if (((int)uap
->id
) < 0)
1170 case P_ALL
: /* any child */
1178 for (p
= q
->p_children
.lh_first
; p
!= 0; p
= p
->p_sibling
.le_next
) {
1179 switch(uap
->idtype
) {
1180 case P_PID
: /* child with process ID equal to... */
1181 if (p
->p_pid
!= (pid_t
)uap
->id
)
1184 case P_PGID
: /* child with process group ID equal to... */
1185 if (p
->p_pgrpid
!= (pid_t
)uap
->id
)
1188 case P_ALL
: /* any child */
1192 /* XXX This is racy because we don't get the lock!!!! */
1195 * Wait collision; go to sleep and restart; used to maintain
1196 * the single return for waited process guarantee.
1198 if (p
->p_listflag
& P_LIST_WAITING
) {
1199 (void)msleep(&p
->p_stat
, proc_list_mlock
, PWAIT
, "waitidcoll", 0);
1202 p
->p_listflag
|= P_LIST_WAITING
; /* mark busy */
1207 * Types of processes we are interested in
1209 * XXX Don't know what to do for WCONTINUED?!?
1212 case SZOMB
: /* Exited */
1213 if (!(uap
->options
& WEXITED
))
1216 /* drop the lock and the thread is going to return */
1219 /* Collect "siginfo" information for caller */
1220 collect64
.si_signo
= SIGCHLD
;
1221 collect64
.si_code
= 0;
1222 collect64
.si_errno
= 0;
1223 collect64
.si_pid
= 0;
1224 collect64
.si_uid
= 0;
1225 collect64
.si_addr
= 0;
1226 collect64
.si_status
= WEXITSTATUS(p
->p_xstat
);
1227 collect64
.si_band
= 0;
1229 if (IS_64BIT_PROCESS(p
)) {
1230 error
= copyout((caddr_t
)&collect64
,
1235 siginfo_64to32(&collect64
,&collect
);
1236 error
= copyout((caddr_t
)&collect
,
1240 /* information unavailable? */
1244 /* Prevent other process for waiting for this event? */
1245 if (!(uap
->options
& WNOWAIT
)) {
1247 if (!reap_child_locked(q
, p
, 0, 0, 0)) {
1249 p
->p_listflag
&= ~P_LIST_WAITING
;
1255 p
->p_listflag
&= ~P_LIST_WAITING
;
1261 case SSTOP
: /* Stopped */
1263 * If we are not interested in stopped processes, then
1266 if (!(uap
->options
& WSTOPPED
))
1270 * If someone has already waited it, we lost a race
1271 * to be the one to return status.
1273 if ((p
->p_lflag
& P_LWAITED
) != 0)
1276 /* drop the lock and the thread is going to return */
1279 /* Collect "siginfo" information for caller */
1280 collect64
.si_signo
= SIGCHLD
;
1281 collect64
.si_code
= 0;
1282 collect64
.si_errno
= 0;
1283 collect64
.si_pid
= 0;
1284 collect64
.si_uid
= 0;
1285 collect64
.si_addr
= 0;
1287 collect64
.si_status
= p
->p_xstat
;
1289 collect64
.si_band
= 0;
1291 if (IS_64BIT_PROCESS(p
)) {
1292 error
= copyout((caddr_t
)&collect64
,
1297 siginfo_64to32(&collect64
,&collect
);
1298 error
= copyout((caddr_t
)&collect
,
1302 /* information unavailable? */
1306 /* Prevent other process for waiting for this event? */
1307 if (!(uap
->options
& WNOWAIT
)) {
1309 p
->p_lflag
|= P_LWAITED
;
1316 default: /* All others */
1317 /* ...meaning Continued */
1318 if (!(uap
->options
& WCONTINUED
))
1322 * If the flag isn't set, then this process has not
1323 * been stopped and continued, or the status has
1324 * already been reaped by another caller of waitid().
1326 if ((p
->p_flag
& P_CONTINUED
) == 0)
1329 /* drop the lock and the thread is going to return */
1332 /* Collect "siginfo" information for caller */
1334 collect64
.si_signo
= SIGCHLD
;
1335 collect64
.si_code
= CLD_CONTINUED
;
1336 collect64
.si_errno
= 0;
1337 collect64
.si_pid
= p
->p_contproc
;
1338 collect64
.si_uid
= 0;
1339 collect64
.si_addr
= 0;
1340 collect64
.si_status
= p
->p_xstat
;
1341 collect64
.si_band
= 0;
1344 if (IS_64BIT_PROCESS(p
)) {
1345 error
= copyout((caddr_t
)&collect64
,
1350 siginfo_64to32(&collect64
,&collect
);
1351 error
= copyout((caddr_t
)&collect
,
1355 /* information unavailable? */
1359 /* Prevent other process for waiting for this event? */
1360 if (!(uap
->options
& WNOWAIT
)) {
1361 OSBitAndAtomic(~((uint32_t)P_CONTINUED
), (UInt32
*)&p
->p_flag
);
1367 /* LIST LOCK IS HELD HERE */
1368 /* Not a process we are interested in; go on to next child */
1370 p
->p_listflag
&= ~P_LIST_WAITING
;
1374 /* list lock is always held */
1375 /* No child processes that could possibly satisfy the request? */
1381 if (uap
->options
& WNOHANG
) {
1386 if ((error
= msleep0((caddr_t
)q
, proc_list_mlock
, PWAIT
| PCATCH
| PDROP
, "waitid", 0, waitidcontinue
)))
1392 p
->p_listflag
&= ~P_LIST_WAITING
;
1399 * make process 'parent' the new parent of process 'child'.
1402 proc_reparentlocked(proc_t child
, proc_t parent
, int cansignal
, int locked
)
1404 proc_t oldparent
= PROC_NULL
;
1406 if (child
->p_pptr
== parent
)
1412 oldparent
= child
->p_pptr
;
1413 #if __PROC_INTERNAL_DEBUG
1414 if (oldparent
== PROC_NULL
)
1415 panic("proc_reparent: process %x does not have a parent\n", (unsigned int)child
);
1418 LIST_REMOVE(child
, p_sibling
);
1419 #if __PROC_INTERNAL_DEBUG
1420 if (oldparent
->p_childrencnt
== 0)
1421 panic("process children count already 0\n");
1423 oldparent
->p_childrencnt
--;
1424 #if __PROC_INTERNAL_DEBUG1
1425 if (oldparent
->p_childrencnt
< 0)
1426 panic("process children count -ve\n");
1428 LIST_INSERT_HEAD(&parent
->p_children
, child
, p_sibling
);
1429 parent
->p_childrencnt
++;
1430 child
->p_pptr
= parent
;
1431 child
->p_ppid
= parent
->p_pid
;
1435 if ((cansignal
!= 0) && (initproc
== parent
) && (child
->p_stat
== SZOMB
))
1436 psignal(initproc
, SIGCHLD
);
1442 * Make the current process an "init" process, meaning
1443 * that it doesn't have a parent, and that it won't be
1444 * gunned down by kill(-1, 0).
1447 init_process(__unused
struct init_process_args
*args
)
1449 proc_t p
= current_proc();
1451 AUDIT_MACH_SYSCALL_ENTER(AUE_INITPROCESS
);
1452 if (suser(kauth_cred_get(), &p
->p_acflag
)) {
1453 AUDIT_MACH_SYSCALL_EXIT(KERN_NO_ACCESS
);
1454 return(KERN_NO_ACCESS
);
1457 if (p
->p_pid
!= 1 && p
->p_pgrpid
!= p
->p_pid
)
1458 enterpgrp(p
, p
->p_pid
, 0);
1459 OSBitOrAtomic(P_SYSTEM
, (UInt32
*)&p
->p_flag
);
1462 * Take us out of the sibling chain, and
1463 * out of our parent's child chain.
1466 LIST_REMOVE(p
, p_sibling
);
1467 p
->p_sibling
.le_prev
= NULL
;
1468 p
->p_sibling
.le_next
= NULL
;
1469 p
->p_pptr
= kernproc
;
1474 AUDIT_MACH_SYSCALL_EXIT(KERN_SUCCESS
);
1475 return(KERN_SUCCESS
);
1480 * Exit: deallocate address space and other resources, change proc state
1481 * to zombie, and unlink proc from allproc and parent's lists. Save exit
1482 * status and rusage for wait(). Check for child processes and orphan them.
1486 vfork_exit(proc_t p
, int rv
)
1488 vfork_exit_internal(p
, rv
, 0);
1492 vfork_exit_internal(proc_t p
, int rv
, int forceexit
)
1494 thread_t self
= current_thread();
1496 struct task
*task
= p
->task
;
1501 * If a thread in this task has already
1502 * called exit(), then halt any others
1506 ut
= get_bsdthread_info(self
);
1510 if ((p
->p_lflag
& P_LPEXIT
) == P_LPEXIT
) {
1512 * This happens when a parent exits/killed and vfork is in progress
1513 * other threads. But shutdown code for ex has already called exit1()
1518 p
->p_lflag
|= (P_LEXIT
| P_LPEXIT
);
1521 if (forceexit
== 0) {
1523 * parent of a vfork child has already called exit() and the
1524 * thread that has vfork in proress terminates. So there is no
1525 * separate address space here and it has already been marked for
1526 * termination. This was never covered before and could cause problems
1527 * if we block here for outside code.
1529 /* Notify the perf server */
1530 (void)sys_perf_notify(self
, p
->p_pid
);
1534 * Remove proc from allproc queue and from pidhash chain.
1535 * Need to do this before we do anything that can block.
1536 * Not doing causes things like mount() find this on allproc
1537 * in partially cleaned state.
1542 LIST_REMOVE(p
, p_list
);
1543 LIST_INSERT_HEAD(&zombproc
, p
, p_list
); /* Place onto zombproc. */
1544 /* will not be visible via proc_find */
1545 p
->p_listflag
|= P_LIST_EXITED
;
1551 p
->p_lflag
&= ~(P_LTRACED
| P_LPPWAIT
);
1552 p
->p_sigignore
= ~0;
1556 if (thread_call_cancel(p
->p_rcall
))
1559 while (p
->p_ractive
> 0) {
1568 thread_call_free(p
->p_rcall
);
1577 vproc_exit(proc_t p
)
1584 struct task
*task
= p
->task
;
1587 struct session
*sessp
;
1590 /* XXX Zombie allocation may fail, in which case stats get lost */
1591 MALLOC_ZONE(p
->p_ru
, struct rusage
*,
1592 sizeof (*p
->p_ru
), M_ZOMBIE
, M_WAITOK
);
1598 * Close open files and release open-file table.
1603 sessp
= proc_session(p
);
1604 if (SESS_LEADER(p
, sessp
)) {
1606 /* Protected by funnel for tty accesses */
1607 fstate
= thread_funnel_set(kernel_flock
, TRUE
);
1609 if (sessp
->s_ttyvp
!= NULLVP
) {
1610 struct vnode
*ttyvp
;
1612 struct vfs_context context
;
1616 * Controlling process.
1617 * Signal foreground pgrp,
1618 * drain controlling terminal
1619 * and revoke access to controlling terminal.
1623 if ((tp
!= TTY_NULL
) && (tp
->t_session
== sessp
)) {
1624 tty_pgsignal(tp
, SIGHUP
, 1);
1627 * The tty could have been revoked
1631 session_lock(sessp
);
1632 ttyvp
= sessp
->s_ttyvp
;
1633 ttyvid
= sessp
->s_ttyvid
;
1634 sessp
->s_ttyvp
= NULL
;
1635 sessp
->s_ttyvid
= 0;
1636 sessp
->s_ttyp
= NULL
;
1637 sessp
->s_ttypgrpid
= NO_PID
;
1638 session_unlock(sessp
);
1640 if ((ttyvp
!= NULLVP
) && (vnode_getwithvid(ttyvp
, ttyvid
) == 0)) {
1641 context
.vc_thread
= proc_thread(p
); /* XXX */
1642 context
.vc_ucred
= kauth_cred_proc_ref(p
);
1643 VNOP_REVOKE(ttyvp
, REVOKEALL
, &context
);
1645 kauth_cred_unref(&context
.vc_ucred
);
1648 session_lock(sessp
);
1649 ttyvp
= sessp
->s_ttyvp
;
1650 sessp
->s_ttyvp
= NULL
;
1651 sessp
->s_ttyvid
= 0;
1652 sessp
->s_ttyp
= NULL
;
1653 sessp
->s_ttypgrpid
= NO_PID
;
1654 session_unlock(sessp
);
1659 * s_ttyp is not zero'd; we use this to indicate
1660 * that the session once had a controlling terminal.
1661 * (for logging and informational purposes)
1664 (void) thread_funnel_set(kernel_flock
, fstate
);
1666 session_lock(sessp
);
1667 sessp
->s_leader
= NULL
;
1668 session_unlock(sessp
);
1670 session_rele(sessp
);
1676 p
->p_rlimit
[RLIMIT_FSIZE
].rlim_cur
= RLIM_INFINITY
;
1679 proc_childdrainstart(p
);
1680 while ((q
= p
->p_children
.lh_first
) != NULL
) {
1681 q
->p_listflag
|= P_LIST_DEADPARENT
;
1682 if (q
->p_stat
== SZOMB
) {
1684 panic("parent child linkage broken");
1685 /* check for lookups by zomb sysctl */
1686 while ((q
->p_listflag
& P_LIST_WAITING
) == P_LIST_WAITING
) {
1687 msleep(&q
->p_stat
, proc_list_mlock
, PWAIT
, "waitcoll", 0);
1689 q
->p_listflag
|= P_LIST_WAITING
;
1691 * This is a named reference and it is not granted
1692 * if the reap is already in progress. So we get
1693 * the reference here exclusively and their can be
1694 * no waiters. So there is no need for a wakeup
1695 * after we are done. AlsO the reap frees the structure
1696 * and the proc struct cannot be used for wakeups as well.
1697 * It is safe to use q here as this is system reap
1699 (void)reap_child_locked(p
, q
, 1, 1, 0);
1701 proc_reparentlocked(q
, initproc
, 0, 1);
1703 * Traced processes are killed
1704 * since their existence means someone is messing up.
1706 if (q
->p_lflag
& P_LTRACED
) {
1709 q
->p_lflag
&= ~P_LTRACED
;
1710 if (q
->sigwait_thread
) {
1713 * The sigwait_thread could be stopped at a
1714 * breakpoint. Wake it up to kill.
1715 * Need to do this as it could be a thread which is not
1716 * the first thread in the task. So any attempts to kill
1717 * the process would result into a deadlock on q->sigwait.
1719 thread_resume((thread_t
)q
->sigwait_thread
);
1720 clear_wait(q
->sigwait_thread
, THREAD_INTERRUPTED
);
1721 threadsignal((thread_t
)q
->sigwait_thread
, SIGKILL
, 0);
1725 psignal(q
, SIGKILL
);
1731 proc_childdrainend(p
);
1735 * Release reference to text vnode
1739 if (tvp
!= NULLVP
) {
1744 * Save exit status and final rusage info, adding in child rusage
1745 * info and self times. If we were unable to allocate a zombie
1746 * structure, this information is lost.
1748 /* No need for locking here as no one than this thread can access this */
1749 if (p
->p_ru
!= NULL
) {
1750 *p
->p_ru
= p
->p_stats
->p_ru
;
1751 timerclear(&p
->p_ru
->ru_utime
);
1752 timerclear(&p
->p_ru
->ru_stime
);
1756 task_basic_info_data_t tinfo
;
1757 task_thread_times_info_data_t ttimesinfo
;
1758 int task_info_stuff
, task_ttimes_stuff
;
1759 struct timeval ut
,st
;
1761 task_info_stuff
= TASK_BASIC_INFO_COUNT
;
1762 task_info(task
, TASK_BASIC_INFO
,
1763 &tinfo
, &task_info_stuff
);
1764 p
->p_ru
->ru_utime
.tv_sec
= tinfo
.user_time
.seconds
;
1765 p
->p_ru
->ru_utime
.tv_usec
= tinfo
.user_time
.microseconds
;
1766 p
->p_ru
->ru_stime
.tv_sec
= tinfo
.system_time
.seconds
;
1767 p
->p_ru
->ru_stime
.tv_usec
= tinfo
.system_time
.microseconds
;
1769 task_ttimes_stuff
= TASK_THREAD_TIMES_INFO_COUNT
;
1770 task_info(task
, TASK_THREAD_TIMES_INFO
,
1771 &ttimesinfo
, &task_ttimes_stuff
);
1773 ut
.tv_sec
= ttimesinfo
.user_time
.seconds
;
1774 ut
.tv_usec
= ttimesinfo
.user_time
.microseconds
;
1775 st
.tv_sec
= ttimesinfo
.system_time
.seconds
;
1776 st
.tv_usec
= ttimesinfo
.system_time
.microseconds
;
1777 timeradd(&ut
,&p
->p_ru
->ru_utime
,&p
->p_ru
->ru_utime
);
1778 timeradd(&st
,&p
->p_ru
->ru_stime
,&p
->p_ru
->ru_stime
);
1782 ruadd(p
->p_ru
, &p
->p_stats
->p_cru
);
1786 * Free up profiling buffers.
1789 struct uprof
*p0
= &p
->p_stats
->p_prof
, *p1
, *pn
;
1795 for (; p1
!= NULL
; p1
= pn
) {
1797 kfree(p1
, sizeof *p1
);
1802 * Other substructures are freed from wait().
1804 FREE_ZONE(p
->p_stats
, sizeof *p
->p_stats
, M_PSTATS
);
1807 FREE_ZONE(p
->p_sigacts
, sizeof *p
->p_sigacts
, M_SIGACTS
);
1808 p
->p_sigacts
= NULL
;
1810 proc_limitdrop(p
, 1);
1814 * Finish up by terminating the task
1815 * and halt this thread (only if a
1816 * member of the task exiting).
1818 p
->task
= TASK_NULL
;
1821 * Notify parent that we're gone.
1823 pp
= proc_parent(p
);
1824 if ((p
->p_listflag
& P_LIST_DEADPARENT
) == 0) {
1825 if (pp
!= initproc
) {
1827 pp
->si_pid
= p
->p_pid
;
1828 pp
->si_status
= p
->p_xstat
;
1829 pp
->si_code
= CLD_EXITED
;
1831 * p_ucred usage is safe as it is an exiting process
1832 * and reference is dropped in reap
1834 pp
->si_uid
= p
->p_ucred
->cr_ruid
;
1837 /* mark as a zombie */
1838 /* mark as a zombie */
1839 /* No need to take proc lock as all refs are drained and
1840 * no one except parent (reaping ) can look at this.
1841 * The write is to an int and is coherent. Also parent is
1842 * keyed off of list lock for reaping
1846 psignal(pp
, SIGCHLD
);
1848 /* and now wakeup the parent */
1850 wakeup((caddr_t
)pp
);
1855 /* check for lookups by zomb sysctl */
1856 while ((p
->p_listflag
& P_LIST_WAITING
) == P_LIST_WAITING
) {
1857 msleep(&p
->p_stat
, proc_list_mlock
, PWAIT
, "waitcoll", 0);
1859 p
->p_listflag
|= P_LIST_WAITING
;
1861 * This is a named reference and it is not granted
1862 * if the reap is already in progress. So we get
1863 * the reference here exclusively and their can be
1864 * no waiters. So there is no need for a wakeup
1865 * after we are done. AlsO the reap frees the structure
1866 * and the proc struct cannot be used for wakeups as well.
1867 * It is safe to use p here as this is system reap
1869 (void)reap_child_locked(pp
, p
, 0, 1, 1);
1870 /* list lock dropped by reap_child_locked */
1878 * LP64 support - long is 64 bits if we are dealing with a 64 bit user
1879 * process. We munge the kernel (32 bit) version of rusage into the
1882 __private_extern__
void
1883 munge_rusage(struct rusage
*a_rusage_p
, struct user_rusage
*a_user_rusage_p
)
1885 /* timeval changes size, so utime and stime need special handling */
1886 a_user_rusage_p
->ru_utime
.tv_sec
= a_rusage_p
->ru_utime
.tv_sec
;
1887 a_user_rusage_p
->ru_utime
.tv_usec
= a_rusage_p
->ru_utime
.tv_usec
;
1888 a_user_rusage_p
->ru_stime
.tv_sec
= a_rusage_p
->ru_stime
.tv_sec
;
1889 a_user_rusage_p
->ru_stime
.tv_usec
= a_rusage_p
->ru_stime
.tv_usec
;
1891 * everything else can be a direct assign, since there is no loss
1892 * of precision implied boing 32->64.
1894 a_user_rusage_p
->ru_maxrss
= a_rusage_p
->ru_maxrss
;
1895 a_user_rusage_p
->ru_ixrss
= a_rusage_p
->ru_ixrss
;
1896 a_user_rusage_p
->ru_idrss
= a_rusage_p
->ru_idrss
;
1897 a_user_rusage_p
->ru_isrss
= a_rusage_p
->ru_isrss
;
1898 a_user_rusage_p
->ru_minflt
= a_rusage_p
->ru_minflt
;
1899 a_user_rusage_p
->ru_majflt
= a_rusage_p
->ru_majflt
;
1900 a_user_rusage_p
->ru_nswap
= a_rusage_p
->ru_nswap
;
1901 a_user_rusage_p
->ru_inblock
= a_rusage_p
->ru_inblock
;
1902 a_user_rusage_p
->ru_oublock
= a_rusage_p
->ru_oublock
;
1903 a_user_rusage_p
->ru_msgsnd
= a_rusage_p
->ru_msgsnd
;
1904 a_user_rusage_p
->ru_msgrcv
= a_rusage_p
->ru_msgrcv
;
1905 a_user_rusage_p
->ru_nsignals
= a_rusage_p
->ru_nsignals
;
1906 a_user_rusage_p
->ru_nvcsw
= a_rusage_p
->ru_nvcsw
;
1907 a_user_rusage_p
->ru_nivcsw
= a_rusage_p
->ru_nivcsw
;