2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
23 /* Copyright (c) 1995, 1997 Apple Computer, Inc. All Rights Reserved */
25 * Copyright (c) 1982, 1986, 1989, 1991, 1993
26 * The Regents of the University of California. All rights reserved.
27 * (c) UNIX System Laboratories, Inc.
28 * All or some portions of this file are derived from material licensed
29 * to the University of California by American Telephone and Telegraph
30 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
31 * the permission of UNIX System Laboratories, Inc.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. All advertising materials mentioning features or use of this software
42 * must display the following acknowledgement:
43 * This product includes software developed by the University of
44 * California, Berkeley and its contributors.
45 * 4. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * @(#)kern_exit.c 8.7 (Berkeley) 2/12/94
64 #include <machine/reg.h>
65 #include <machine/psl.h>
67 #include "compat_43.h"
69 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <sys/ioctl.h>
72 #include <sys/proc_internal.h>
73 #include <sys/kauth.h>
76 #include <sys/resource.h>
77 #include <sys/kernel.h>
79 #include <sys/file_internal.h>
80 #include <sys/vnode_internal.h>
81 #include <sys/syslog.h>
82 #include <sys/malloc.h>
83 #include <sys/resourcevar.h>
84 #include <sys/ptrace.h>
86 #include <sys/aio_kern.h>
87 #include <sys/sysproto.h>
88 #include <sys/signalvar.h>
89 #include <sys/filedesc.h> /* fdfree */
90 #include <sys/shm_internal.h> /* shmexit */
91 #include <sys/acct.h> /* acct_process */
92 #include <machine/spl.h>
94 #include <bsm/audit_kernel.h>
95 #include <bsm/audit_kevents.h>
97 #include <mach/mach_types.h>
99 #include <kern/kern_types.h>
100 #include <kern/kalloc.h>
101 #include <kern/task.h>
102 #include <kern/thread.h>
103 #include <kern/sched_prim.h>
104 #include <kern/assert.h>
106 #include <sys/ktrace.h>
109 #include <mach/mach_types.h>
110 #include <mach/task.h>
111 #include <mach/thread_act.h>
112 #include <mach/mach_traps.h> /* init_process */
114 extern char init_task_failure_data
[];
115 int exit1(struct proc
*, int, int *);
116 void proc_prepareexit(struct proc
*p
);
117 void vfork_exit(struct proc
*p
, int rv
);
118 void vproc_exit(struct proc
*p
);
119 __private_extern__
void munge_rusage(struct rusage
*a_rusage_p
, struct user_rusage
*a_user_rusage_p
);
122 * Things which should have prototypes in headers, but don't
124 void unix_syscall_return(int);
125 void *get_bsduthreadarg(thread_t
);
126 void proc_exit(struct proc
*p
);
127 int wait1continue(int result
);
128 int waitidcontinue(int result
);
129 int *get_bsduthreadrval(thread_t
);
130 kern_return_t
sys_perf_notify(struct task
*task
, exception_data_t code
,
131 mach_msg_type_number_t codeCnt
);
134 * NOTE: Source and target may *NOT* overlap!
135 * XXX Should share code with bsd/dev/ppc/unix_signal.c
138 siginfo_64to32(user_siginfo_t
*in
, siginfo_t
*out
)
140 out
->si_signo
= in
->si_signo
;
141 out
->si_errno
= in
->si_errno
;
142 out
->si_code
= in
->si_code
;
143 out
->si_pid
= in
->si_pid
;
144 out
->si_uid
= in
->si_uid
;
145 out
->si_status
= in
->si_status
;
146 out
->si_addr
= CAST_DOWN(void *,in
->si_addr
);
147 /* following cast works for sival_int because of padding */
148 out
->si_value
.sival_ptr
= CAST_DOWN(void *,in
->si_value
.sival_ptr
);
149 out
->si_band
= in
->si_band
; /* range reduction */
150 out
->pad
[0] = in
->pad
[0]; /* mcontext.ss.r1 */
158 exit(struct proc
*p
, struct exit_args
*uap
, int *retval
)
160 exit1(p
, W_EXITCODE(uap
->rval
, 0), retval
);
162 /* drop funnel before we return */
163 thread_funnel_set(kernel_flock
, FALSE
);
164 thread_exception_return();
167 thread_block(THREAD_CONTINUE_NULL
);
172 * Exit: deallocate address space and other resources, change proc state
173 * to zombie, and unlink proc from allproc and parent's lists. Save exit
174 * status and rusage for wait(). Check for child processes and orphan them.
177 exit1(struct proc
*p
, int rv
, int *retval
)
179 thread_t self
= current_thread();
180 struct task
*task
= p
->task
;
185 * If a thread in this task has already
186 * called exit(), then halt any others
190 ut
= get_bsdthread_info(self
);
191 if (ut
->uu_flag
& UT_VFORK
) {
193 vfork_return(self
, p
->p_pptr
, p
, retval
);
194 unix_syscall_return(0);
197 AUDIT_SYSCALL_EXIT(0, p
, ut
); /* Exit is always successfull */
199 while (p
->exit_thread
!= self
) {
200 if (sig_try_locked(p
) <= 0) {
201 if (get_threadtask(self
) != task
) {
206 thread_terminate(self
);
207 thread_funnel_set(kernel_flock
, FALSE
);
208 thread_exception_return();
215 printf("pid 1 exited (signal %d, exit %d)",
216 WTERMSIG(rv
), WEXITSTATUS(rv
));
217 panic("init died\nState at Last Exception:\n\n%s",
218 init_task_failure_data
);
222 p
->p_flag
|= P_WEXIT
;
227 /* task terminate will call proc_terminate and that cleans it up */
228 task_terminate_internal(task
);
234 proc_prepareexit(struct proc
*p
)
237 exception_data_t code
[EXCEPTION_CODE_MAX
];
238 thread_t self
= current_thread();
240 code
[0] = (exception_data_t
)0xFF000001; /* Set terminate code */
241 code
[1] = (exception_data_t
)p
->p_pid
; /* Pass out the pid */
242 /* Notify the perf server */
243 (void)sys_perf_notify(p
->task
, (exception_data_t
)&code
, 2);
246 * Remove proc from allproc queue and from pidhash chain.
247 * Need to do this before we do anything that can block.
248 * Not doing causes things like mount() find this on allproc
249 * in partially cleaned state.
251 LIST_REMOVE(p
, p_list
);
252 LIST_INSERT_HEAD(&zombproc
, p
, p_list
); /* Place onto zombproc. */
253 LIST_REMOVE(p
, p_hash
);
259 * If parent is waiting for us to exit or exec,
260 * P_PPWAIT is set; we will wakeup the parent below.
262 p
->p_flag
&= ~(P_TRACED
| P_PPWAIT
);
263 p
->p_sigignore
= ~(sigcantmask
);
265 ut
= get_bsdthread_info(self
);
267 untimeout(realitexpire
, (caddr_t
)p
->p_pid
);
271 proc_exit(struct proc
*p
)
273 register struct proc
*q
, *nq
, *pp
;
274 struct task
*task
= p
->task
;
276 boolean_t funnel_state
;
278 /* This can happen if thread_terminate of the single thread
282 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
283 if( !(p
->p_flag
& P_WEXIT
)) {
285 p
->p_flag
|= P_WEXIT
;
290 p
->p_lflag
|= P_LPEXIT
;
291 /* XXX Zombie allocation may fail, in which case stats get lost */
292 MALLOC_ZONE(p
->p_ru
, struct rusage
*,
293 sizeof (*p
->p_ru
), M_ZOMBIE
, M_WAITOK
);
296 * need to cancel async IO requests that can be cancelled and wait for those
297 * already active. MAY BLOCK!
300 p
->p_lflag
|= P_LREFDRAIN
;
301 while (p
->p_internalref
) {
302 p
->p_lflag
|= P_LREFDRAINWAIT
;
303 msleep(&p
->p_internalref
, (lck_mtx_t
*)0, 0, "proc_refdrain", 0) ;
305 p
->p_lflag
&= ~P_LREFDRAIN
;
306 p
->p_lflag
|= P_LREFDEAD
;
311 * Close open files and release open-file table.
316 /* Close ref SYSV Shared memory*/
319 /* Release SYSV semaphores */
322 if (SESS_LEADER(p
)) {
323 register struct session
*sp
= p
->p_session
;
327 struct vfs_context context
;
330 * Controlling process.
331 * Signal foreground pgrp,
332 * drain controlling terminal
333 * and revoke access to controlling terminal.
335 if (sp
->s_ttyp
->t_session
== sp
) {
336 if (sp
->s_ttyp
->t_pgrp
)
337 pgsignal(sp
->s_ttyp
->t_pgrp
, SIGHUP
, 1);
338 (void) ttywait(sp
->s_ttyp
);
340 * The tty could have been revoked
344 context
.vc_ucred
= p
->p_ucred
;
346 VNOP_REVOKE(sp
->s_ttyvp
, REVOKEALL
, &context
);
354 * s_ttyp is not zero'd; we use this to indicate
355 * that the session once had a controlling terminal.
356 * (for logging and informational purposes)
362 fixjobc(p
, p
->p_pgrp
, 0);
363 p
->p_rlimit
[RLIMIT_FSIZE
].rlim_cur
= RLIM_INFINITY
;
364 (void)acct_process(p
);
370 p
->p_traceflag
= 0; /* don't trace the vnode_put() */
372 struct vnode
*tvp
= p
->p_tracep
;
378 while (q
= p
->p_children
.lh_first
) {
379 proc_reparent(q
, initproc
);
381 * Traced processes are killed
382 * since their existence means someone is messing up.
384 if (q
->p_flag
& P_TRACED
) {
385 q
->p_flag
&= ~P_TRACED
;
386 if (q
->sigwait_thread
) {
388 * The sigwait_thread could be stopped at a
389 * breakpoint. Wake it up to kill.
390 * Need to do this as it could be a thread which is not
391 * the first thread in the task. So any attempts to kill
392 * the process would result into a deadlock on q->sigwait.
394 thread_resume((thread_t
)q
->sigwait_thread
);
395 clear_wait(q
->sigwait_thread
, THREAD_INTERRUPTED
);
396 threadsignal((thread_t
)q
->sigwait_thread
, SIGKILL
, 0);
403 * Save exit status and final rusage info, adding in child rusage
404 * info and self times. If we were unable to allocate a zombie
405 * structure, this information is lost.
407 if (p
->p_ru
!= NULL
) {
408 *p
->p_ru
= p
->p_stats
->p_ru
;
410 timerclear(&p
->p_ru
->ru_utime
);
411 timerclear(&p
->p_ru
->ru_stime
);
414 task_basic_info_data_t tinfo
;
415 task_thread_times_info_data_t ttimesinfo
;
416 int task_info_stuff
, task_ttimes_stuff
;
417 struct timeval ut
,st
;
419 task_info_stuff
= TASK_BASIC_INFO_COUNT
;
420 task_info(task
, TASK_BASIC_INFO
,
421 (task_info_t
)&tinfo
, &task_info_stuff
);
422 p
->p_ru
->ru_utime
.tv_sec
= tinfo
.user_time
.seconds
;
423 p
->p_ru
->ru_utime
.tv_usec
= tinfo
.user_time
.microseconds
;
424 p
->p_ru
->ru_stime
.tv_sec
= tinfo
.system_time
.seconds
;
425 p
->p_ru
->ru_stime
.tv_usec
= tinfo
.system_time
.microseconds
;
427 task_ttimes_stuff
= TASK_THREAD_TIMES_INFO_COUNT
;
428 task_info(task
, TASK_THREAD_TIMES_INFO
,
429 (task_info_t
)&ttimesinfo
, &task_ttimes_stuff
);
431 ut
.tv_sec
= ttimesinfo
.user_time
.seconds
;
432 ut
.tv_usec
= ttimesinfo
.user_time
.microseconds
;
433 st
.tv_sec
= ttimesinfo
.system_time
.seconds
;
434 st
.tv_usec
= ttimesinfo
.system_time
.microseconds
;
435 timeradd(&ut
,&p
->p_ru
->ru_utime
,&p
->p_ru
->ru_utime
);
436 timeradd(&st
,&p
->p_ru
->ru_stime
,&p
->p_ru
->ru_stime
);
439 ruadd(p
->p_ru
, &p
->p_stats
->p_cru
);
443 * Free up profiling buffers.
446 struct uprof
*p0
= &p
->p_stats
->p_prof
, *p1
, *pn
;
452 for (; p1
!= NULL
; p1
= pn
) {
454 kfree(p1
, sizeof *p1
);
459 * Other substructures are freed from wait().
461 FREE_ZONE(p
->p_stats
, sizeof *p
->p_stats
, M_SUBPROC
);
464 FREE_ZONE(p
->p_sigacts
, sizeof *p
->p_sigacts
, M_SUBPROC
);
467 if (--p
->p_limit
->p_refcnt
== 0)
468 FREE_ZONE(p
->p_limit
, sizeof *p
->p_limit
, M_SUBPROC
);
472 * Finish up by terminating the task
473 * and halt this thread (only if a
474 * member of the task exiting).
478 set_bsdtask_info(task
, NULL
);
480 KNOTE(&p
->p_klist
, NOTE_EXIT
);
483 * Notify parent that we're gone.
485 if (p
->p_pptr
->p_flag
& P_NOCLDWAIT
) {
486 struct proc
*opp
= p
->p_pptr
;
489 * Add child resource usage to parent before giving
490 * zombie to init. If we were unable to allocate a
491 * zombie structure, this information is lost.
494 ruadd(&p
->p_pptr
->p_stats
->p_cru
, p
->p_ru
);
496 proc_reparent(p
, initproc
);
497 /* If there are no more children wakeup parent */
498 if (LIST_EMPTY(&opp
->p_children
))
499 wakeup((caddr_t
)opp
);
501 /* should be fine as parent proc would be initproc */
503 if (pp
!= initproc
) {
504 pp
->si_pid
= p
->p_pid
;
505 pp
->si_status
= p
->p_xstat
;
506 pp
->si_code
= CLD_EXITED
;
507 pp
->si_uid
= p
->p_ucred
->cr_ruid
;
509 /* mark as a zombie */
512 psignal(pp
, SIGCHLD
);
514 /* and now wakeup the parent */
515 wakeup((caddr_t
)p
->p_pptr
);
517 (void) thread_funnel_set(kernel_flock
, funnel_state
);
524 * Description: Given a process from which all status information needed
525 * has already been extracted, if the process is a ptrace
526 * attach process, detach it and give it back to its real
527 * parent, else recover all resources remaining associated
530 * Parameters: struct proc *parent Parent of process being reaped
531 * struct proc *child Process to reap
533 * Returns: 0 Process was not reaped because it
534 * came from an attach
535 * 1 Process was reaped
538 reap_child_process(struct proc
*parent
, struct proc
*child
)
540 struct proc
*trace_parent
; /* Traced parent process, if tracing */
541 struct vnode
*tvp
; /* Traced vnode pointer, if used */
544 * If we got the child via a ptrace 'attach',
545 * we need to give it back to the old parent.
547 if (child
->p_oppid
&& (trace_parent
= pfind(child
->p_oppid
))) {
549 proc_reparent(child
, trace_parent
);
550 if (trace_parent
!= initproc
) {
551 trace_parent
->si_pid
= child
->p_pid
;
552 trace_parent
->si_status
= child
->p_xstat
;
553 trace_parent
->si_code
= CLD_CONTINUED
;
554 trace_parent
->si_uid
= child
->p_ucred
->cr_ruid
;
556 psignal(trace_parent
, SIGCHLD
);
557 wakeup((caddr_t
)trace_parent
);
562 ruadd(&parent
->p_stats
->p_cru
, child
->p_ru
);
563 FREE_ZONE(child
->p_ru
, sizeof *child
->p_ru
, M_ZOMBIE
);
566 printf("Warning : lost p_ru for %s\n", child
->p_comm
);
570 * Decrement the count of procs running with this uid.
572 (void)chgproccnt(child
->p_ucred
->cr_ruid
, -1);
575 * Free up credentials.
577 if (child
->p_ucred
!= NOCRED
) {
578 kauth_cred_t ucr
= child
->p_ucred
;
579 child
->p_ucred
= NOCRED
;
580 kauth_cred_rele(ucr
);
584 * Release reference to text vnode
586 tvp
= child
->p_textvp
;
587 child
->p_textvp
= NULL
;
592 * Finally finished with old proc entry.
593 * Unlink it from its process group and free it.
596 LIST_REMOVE(child
, p_list
); /* off zombproc */
597 LIST_REMOVE(child
, p_sibling
);
598 child
->p_lflag
&= ~P_LWAITING
;
599 wakeup(&child
->p_stat
);
601 lck_mtx_destroy(&child
->p_mlock
, proc_lck_grp
);
602 lck_mtx_destroy(&child
->p_fdmlock
, proc_lck_grp
);
603 FREE_ZONE(child
, sizeof *child
, M_PROC
);
610 wait1continue(int result
)
621 thread
= current_thread();
622 vt
= get_bsduthreadarg(thread
);
623 retval
= get_bsduthreadrval(thread
);
624 return(wait4((struct proc
*)p
, (struct wait4_args
*)vt
, retval
));
628 wait4(struct proc
*q
, struct wait4_args
*uap
, register_t
*retval
)
631 register struct proc
*p
;
635 uap
->pid
= -q
->p_pgid
;
639 for (p
= q
->p_children
.lh_first
; p
!= 0; p
= p
->p_sibling
.le_next
) {
640 if (uap
->pid
!= WAIT_ANY
&&
641 p
->p_pid
!= uap
->pid
&&
642 p
->p_pgid
!= -(uap
->pid
))
646 /* XXX This is racy because we don't get the lock!!!! */
648 if (p
->p_lflag
& P_LWAITING
) {
649 (void)tsleep(&p
->p_stat
, PWAIT
, "waitcoll", 0);
652 p
->p_lflag
|= P_LWAITING
; /* only allow single thread to wait() */
654 if (p
->p_stat
== SZOMB
) {
655 retval
[0] = p
->p_pid
;
657 status
= p
->p_xstat
; /* convert to int */
658 error
= copyout((caddr_t
)&status
,
662 p
->p_lflag
&= ~P_LWAITING
;
668 if (p
->p_ru
== NULL
) {
671 if (IS_64BIT_PROCESS(q
)) {
672 struct user_rusage my_rusage
;
673 munge_rusage(p
->p_ru
, &my_rusage
);
674 error
= copyout((caddr_t
)&my_rusage
,
679 error
= copyout((caddr_t
)p
->p_ru
,
681 sizeof (struct rusage
));
684 /* information unavailable? */
686 p
->p_lflag
&= ~P_LWAITING
;
693 if (!reap_child_process(q
, p
)) {
694 p
->p_lflag
&= ~P_LWAITING
;
700 if (p
->p_stat
== SSTOP
&& (p
->p_flag
& P_WAITED
) == 0 &&
701 (p
->p_flag
& P_TRACED
|| uap
->options
& WUNTRACED
)) {
702 p
->p_flag
|= P_WAITED
;
703 retval
[0] = p
->p_pid
;
705 status
= W_STOPCODE(p
->p_xstat
);
706 error
= copyout((caddr_t
)&status
,
711 p
->p_lflag
&= ~P_LWAITING
;
715 p
->p_lflag
&= ~P_LWAITING
;
721 if (uap
->options
& WNOHANG
) {
726 if ((error
= tsleep0((caddr_t
)q
, PWAIT
| PCATCH
, "wait", 0, wait1continue
)))
734 waitidcontinue(int result
)
745 thread
= current_thread();
746 vt
= get_bsduthreadarg(thread
);
747 retval
= get_bsduthreadrval(thread
);
748 return(waitid((struct proc
*)p
, (struct waitid_args
*)vt
, retval
));
752 * Description: Suspend the calling thread until one child of the process
753 * containing the calling thread changes state.
755 * Parameters: uap->idtype one of P_PID, P_PGID, P_ALL
756 * uap->id pid_t or gid_t or ignored
757 * uap->infop Address of signinfo_t struct in
758 * user space into which to return status
759 * uap->options flag values
762 * !0 Error returning status to user space
765 waitid(struct proc
*q
, struct waitid_args
*uap
, register_t
*retval
)
767 user_siginfo_t collect64
; /* siginfo data to return to caller */
770 register struct proc
*p
;
775 for (p
= q
->p_children
.lh_first
; p
!= 0; p
= p
->p_sibling
.le_next
) {
776 switch(uap
->idtype
) {
777 case P_PID
: /* child with process ID equal to... */
778 if (p
->p_pid
!= (pid_t
)uap
->id
)
781 case P_PGID
: /* child with process group ID equal to... */
782 if (p
->p_pgid
!= (pid_t
)uap
->id
)
785 case P_ALL
: /* any child */
789 /* XXX This is racy because we don't get the lock!!!! */
792 * Wait collision; go to sleep and restart; used to maintain
793 * the single return for waited process guarantee.
795 if (p
->p_lflag
& P_LWAITING
) {
796 (void)tsleep(&p
->p_stat
, PWAIT
, "waitidcoll", 0);
799 p
->p_lflag
|= P_LWAITING
; /* mark busy */
804 * Types of processes we are interested in
806 * XXX Don't know what to do for WCONTINUED?!?
809 case SZOMB
: /* Exited */
810 if (!(uap
->options
& WEXITED
))
813 /* Collect "siginfo" information for caller */
814 collect64
.si_signo
= 0;
815 collect64
.si_code
= 0;
816 collect64
.si_errno
= 0;
817 collect64
.si_pid
= 0;
818 collect64
.si_uid
= 0;
819 collect64
.si_addr
= 0;
820 collect64
.si_status
= p
->p_xstat
;
821 collect64
.si_band
= 0;
823 if (IS_64BIT_PROCESS(p
)) {
824 error
= copyout((caddr_t
)&collect64
,
829 siginfo_64to32(&collect64
,&collect
);
830 error
= copyout((caddr_t
)&collect
,
834 /* information unavailable? */
836 p
->p_lflag
&= ~P_LWAITING
;
841 /* Prevent other process for waiting for this event? */
842 if (!(uap
->options
& WNOWAIT
)) {
844 if (!reap_child_process(q
, p
)) {
845 p
->p_lflag
&= ~P_LWAITING
;
852 case SSTOP
: /* Stopped */
854 * If we are not interested in stopped processes, then
857 if (!(uap
->options
& WSTOPPED
))
861 * If someone has already waited it, we lost a race
862 * to be the one to return status.
864 if ((p
->p_flag
& P_WAITED
) != 0)
868 * If this is not a traced process, and they haven't
869 * indicated an interest in untraced processes, then
872 if (!(p
->p_flag
& P_TRACED
) && !(uap
->options
& WUNTRACED
))
875 /* Collect "siginfo" information for caller */
876 collect64
.si_signo
= 0;
877 collect64
.si_code
= 0;
878 collect64
.si_errno
= 0;
879 collect64
.si_pid
= 0;
880 collect64
.si_uid
= 0;
881 collect64
.si_addr
= 0;
882 collect64
.si_status
= p
->p_xstat
;
883 collect64
.si_band
= 0;
885 if (IS_64BIT_PROCESS(p
)) {
886 error
= copyout((caddr_t
)&collect64
,
891 siginfo_64to32(&collect64
,&collect
);
892 error
= copyout((caddr_t
)&collect
,
896 /* information unavailable? */
898 p
->p_lflag
&= ~P_LWAITING
;
903 /* Prevent other process for waiting for this event? */
904 if (!(uap
->options
& WNOWAIT
)) {
905 p
->p_flag
|= P_WAITED
;
908 p
->p_lflag
&= ~P_LWAITING
;
912 default: /* All others */
913 /* ...meaning Continued */
914 if (!(uap
->options
& WCONTINUED
))
918 * If the flag isn't set, then this process has not
919 * been stopped and continued, or the status has
920 * already been reaped by another caller of waitid().
922 if ((p
->p_flag
& P_CONTINUED
) == 0)
925 /* Collect "siginfo" information for caller */
926 collect64
.si_signo
= 0;
927 collect64
.si_code
= 0;
928 collect64
.si_errno
= 0;
929 collect64
.si_pid
= 0;
930 collect64
.si_uid
= 0;
931 collect64
.si_addr
= 0;
932 collect64
.si_status
= p
->p_xstat
;
933 collect64
.si_band
= 0;
935 if (IS_64BIT_PROCESS(p
)) {
936 error
= copyout((caddr_t
)&collect64
,
941 siginfo_64to32(&collect64
,&collect
);
942 error
= copyout((caddr_t
)&collect
,
946 /* information unavailable? */
948 p
->p_lflag
&= ~P_LWAITING
;
953 /* Prevent other process for waiting for this event? */
954 if (!(uap
->options
& WNOWAIT
)) {
955 p
->p_flag
&= ~P_CONTINUED
;
958 p
->p_lflag
&= ~P_LWAITING
;
966 /* Not a process we are interested in; go on to next child */
967 p
->p_lflag
&= ~P_LWAITING
;
971 /* No child processes that could possibly satisfy the request? */
975 if (uap
->options
& WNOHANG
) {
980 if ((error
= tsleep0((caddr_t
)q
, PWAIT
| PCATCH
, "waitid", 0, waitidcontinue
)))
987 * make process 'parent' the new parent of process 'child'.
990 proc_reparent(struct proc
*child
, struct proc
*parent
)
993 if (child
->p_pptr
== parent
)
996 LIST_REMOVE(child
, p_sibling
);
997 LIST_INSERT_HEAD(&parent
->p_children
, child
, p_sibling
);
998 child
->p_pptr
= parent
;
1000 if (initproc
== parent
&& child
->p_stat
== SZOMB
)
1001 psignal(initproc
, SIGCHLD
);
1005 * Make the current process an "init" process, meaning
1006 * that it doesn't have a parent, and that it won't be
1007 * gunned down by kill(-1, 0).
1010 init_process(__unused
struct init_process_args
*args
)
1012 register struct proc
*p
= current_proc();
1014 AUDIT_MACH_SYSCALL_ENTER(AUE_INITPROCESS
);
1015 if (suser(kauth_cred_get(), &p
->p_acflag
)) {
1016 AUDIT_MACH_SYSCALL_EXIT(KERN_NO_ACCESS
);
1017 return(KERN_NO_ACCESS
);
1020 if (p
->p_pid
!= 1 && p
->p_pgid
!= p
->p_pid
)
1021 enterpgrp(p
, p
->p_pid
, 0);
1022 p
->p_flag
|= P_SYSTEM
;
1025 * Take us out of the sibling chain, and
1026 * out of our parent's child chain.
1028 LIST_REMOVE(p
, p_sibling
);
1029 p
->p_sibling
.le_prev
= NULL
;
1030 p
->p_sibling
.le_next
= NULL
;
1031 p
->p_pptr
= kernproc
;
1033 AUDIT_MACH_SYSCALL_EXIT(KERN_SUCCESS
);
1034 return(KERN_SUCCESS
);
1039 * Exit: deallocate address space and other resources, change proc state
1040 * to zombie, and unlink proc from allproc and parent's lists. Save exit
1041 * status and rusage for wait(). Check for child processes and orphan them.
1045 vfork_exit(struct proc
*p
, int rv
)
1047 thread_t self
= current_thread();
1049 struct task
*task
= p
->task
;
1053 exception_data_t code
[EXCEPTION_CODE_MAX
];
1056 * If a thread in this task has already
1057 * called exit(), then halt any others
1061 ut
= get_bsdthread_info(self
);
1064 while (p
->exit_thread
!= self
) {
1065 if (sig_try_locked(p
) <= 0) {
1066 if (get_threadtask(self
) != task
) {
1071 thread_terminate(self
);
1072 thread_funnel_set(kernel_flock
, FALSE
);
1073 thread_exception_return();
1076 sig_lock_to_exit(p
);
1079 if (p
->p_pid
== 1) {
1080 printf("pid 1 exited (signal %d, exit %d)",
1081 WTERMSIG(rv
), WEXITSTATUS(rv
));
1082 panic("init died\nState at Last Exception:\n\n%s", init_task_failure_data
);
1087 p
->p_flag
|= P_WEXIT
;
1088 p
->p_lflag
|= P_LPEXIT
;
1091 code
[0] = (exception_data_t
)0xFF000001; /* Set terminate code */
1092 code
[1] = (exception_data_t
)p
->p_pid
; /* Pass out the pid */
1093 /* Notify the perf server */
1094 (void)sys_perf_notify(p
->task
, (exception_data_t
)&code
, 2);
1097 * Remove proc from allproc queue and from pidhash chain.
1098 * Need to do this before we do anything that can block.
1099 * Not doing causes things like mount() find this on allproc
1100 * in partially cleaned state.
1102 LIST_REMOVE(p
, p_list
);
1103 LIST_INSERT_HEAD(&zombproc
, p
, p_list
); /* Place onto zombproc. */
1104 LIST_REMOVE(p
, p_hash
);
1106 * If parent is waiting for us to exit or exec,
1107 * P_PPWAIT is set; we will wakeup the parent below.
1109 p
->p_flag
&= ~(P_TRACED
| P_PPWAIT
);
1110 p
->p_sigignore
= ~0;
1114 untimeout(realitexpire
, (caddr_t
)p
->p_pid
);
1122 vproc_exit(struct proc
*p
)
1124 register struct proc
*q
, *nq
, *pp
;
1126 struct task
*task
= p
->task
;
1129 /* XXX Zombie allocation may fail, in which case stats get lost */
1130 MALLOC_ZONE(p
->p_ru
, struct rusage
*,
1131 sizeof (*p
->p_ru
), M_ZOMBIE
, M_WAITOK
);
1134 * Close open files and release open-file table.
1139 if (SESS_LEADER(p
)) {
1140 register struct session
*sp
= p
->p_session
;
1143 struct vnode
*ttyvp
;
1144 struct vfs_context context
;
1147 * Controlling process.
1148 * Signal foreground pgrp,
1149 * drain controlling terminal
1150 * and revoke access to controlling terminal.
1152 if (sp
->s_ttyp
->t_session
== sp
) {
1153 if (sp
->s_ttyp
->t_pgrp
)
1154 pgsignal(sp
->s_ttyp
->t_pgrp
, SIGHUP
, 1);
1155 (void) ttywait(sp
->s_ttyp
);
1157 * The tty could have been revoked
1160 context
.vc_proc
= p
;
1161 context
.vc_ucred
= p
->p_ucred
;
1163 VNOP_REVOKE(sp
->s_ttyvp
, REVOKEALL
, &context
);
1165 ttyvp
= sp
->s_ttyvp
;
1171 * s_ttyp is not zero'd; we use this to indicate
1172 * that the session once had a controlling terminal.
1173 * (for logging and informational purposes)
1176 sp
->s_leader
= NULL
;
1179 fixjobc(p
, p
->p_pgrp
, 0);
1180 p
->p_rlimit
[RLIMIT_FSIZE
].rlim_cur
= RLIM_INFINITY
;
1184 * release trace file
1186 p
->p_traceflag
= 0; /* don't trace the vnode_rele() */
1188 struct vnode
*tvp
= p
->p_tracep
;
1194 while (q
= p
->p_children
.lh_first
) {
1195 proc_reparent(q
, initproc
);
1197 * Traced processes are killed
1198 * since their existence means someone is messing up.
1200 if (q
->p_flag
& P_TRACED
) {
1201 q
->p_flag
&= ~P_TRACED
;
1202 if (q
->sigwait_thread
) {
1204 * The sigwait_thread could be stopped at a
1205 * breakpoint. Wake it up to kill.
1206 * Need to do this as it could be a thread which is not
1207 * the first thread in the task. So any attempts to kill
1208 * the process would result into a deadlock on q->sigwait.
1210 thread_resume((thread_t
)q
->sigwait_thread
);
1211 clear_wait(q
->sigwait_thread
, THREAD_INTERRUPTED
);
1212 threadsignal((thread_t
)q
->sigwait_thread
, SIGKILL
, 0);
1214 psignal(q
, SIGKILL
);
1219 * Save exit status and final rusage info, adding in child rusage
1220 * info and self times. If we were unable to allocate a zombie
1221 * structure, this information is lost.
1223 if (p
->p_ru
!= NULL
) {
1224 *p
->p_ru
= p
->p_stats
->p_ru
;
1225 timerclear(&p
->p_ru
->ru_utime
);
1226 timerclear(&p
->p_ru
->ru_stime
);
1230 task_basic_info_data_t tinfo
;
1231 task_thread_times_info_data_t ttimesinfo
;
1232 int task_info_stuff
, task_ttimes_stuff
;
1233 struct timeval ut
,st
;
1235 task_info_stuff
= TASK_BASIC_INFO_COUNT
;
1236 task_info(task
, TASK_BASIC_INFO
,
1237 &tinfo
, &task_info_stuff
);
1238 p
->p_ru
->ru_utime
.tv_sec
= tinfo
.user_time
.seconds
;
1239 p
->p_ru
->ru_utime
.tv_usec
= tinfo
.user_time
.microseconds
;
1240 p
->p_ru
->ru_stime
.tv_sec
= tinfo
.system_time
.seconds
;
1241 p
->p_ru
->ru_stime
.tv_usec
= tinfo
.system_time
.microseconds
;
1243 task_ttimes_stuff
= TASK_THREAD_TIMES_INFO_COUNT
;
1244 task_info(task
, TASK_THREAD_TIMES_INFO
,
1245 &ttimesinfo
, &task_ttimes_stuff
);
1247 ut
.tv_sec
= ttimesinfo
.user_time
.seconds
;
1248 ut
.tv_usec
= ttimesinfo
.user_time
.microseconds
;
1249 st
.tv_sec
= ttimesinfo
.system_time
.seconds
;
1250 st
.tv_usec
= ttimesinfo
.system_time
.microseconds
;
1251 timeradd(&ut
,&p
->p_ru
->ru_utime
,&p
->p_ru
->ru_utime
);
1252 timeradd(&st
,&p
->p_ru
->ru_stime
,&p
->p_ru
->ru_stime
);
1256 ruadd(p
->p_ru
, &p
->p_stats
->p_cru
);
1260 * Free up profiling buffers.
1263 struct uprof
*p0
= &p
->p_stats
->p_prof
, *p1
, *pn
;
1269 for (; p1
!= NULL
; p1
= pn
) {
1271 kfree(p1
, sizeof *p1
);
1276 * Other substructures are freed from wait().
1278 FREE_ZONE(p
->p_stats
, sizeof *p
->p_stats
, M_SUBPROC
);
1281 FREE_ZONE(p
->p_sigacts
, sizeof *p
->p_sigacts
, M_SUBPROC
);
1282 p
->p_sigacts
= NULL
;
1284 if (--p
->p_limit
->p_refcnt
== 0)
1285 FREE_ZONE(p
->p_limit
, sizeof *p
->p_limit
, M_SUBPROC
);
1289 * Finish up by terminating the task
1290 * and halt this thread (only if a
1291 * member of the task exiting).
1293 p
->task
= TASK_NULL
;
1296 * Notify parent that we're gone.
1299 if (pp
!= initproc
) {
1300 pp
->si_pid
= p
->p_pid
;
1301 pp
->si_status
= p
->p_xstat
;
1302 pp
->si_code
= CLD_EXITED
;
1303 pp
->si_uid
= p
->p_ucred
->cr_ruid
;
1305 /* mark as a zombie */
1308 psignal(p
->p_pptr
, SIGCHLD
);
1310 /* and now wakeup the parent */
1311 wakeup((caddr_t
)p
->p_pptr
);
1317 * LP64 support - long is 64 bits if we are dealing with a 64 bit user
1318 * process. We munge the kernel (32 bit) version of rusage into the
1321 __private_extern__
void
1322 munge_rusage(struct rusage
*a_rusage_p
, struct user_rusage
*a_user_rusage_p
)
1324 /* timeval changes size, so utime and stime need special handling */
1325 a_user_rusage_p
->ru_utime
.tv_sec
= a_rusage_p
->ru_utime
.tv_sec
;
1326 a_user_rusage_p
->ru_utime
.tv_usec
= a_rusage_p
->ru_utime
.tv_usec
;
1327 a_user_rusage_p
->ru_stime
.tv_sec
= a_rusage_p
->ru_stime
.tv_sec
;
1328 a_user_rusage_p
->ru_stime
.tv_usec
= a_rusage_p
->ru_stime
.tv_usec
;
1330 * everything else can be a direct assign, since there is no loss
1331 * of precision implied boing 32->64.
1333 a_user_rusage_p
->ru_maxrss
= a_rusage_p
->ru_maxrss
;
1334 a_user_rusage_p
->ru_ixrss
= a_rusage_p
->ru_ixrss
;
1335 a_user_rusage_p
->ru_idrss
= a_rusage_p
->ru_idrss
;
1336 a_user_rusage_p
->ru_isrss
= a_rusage_p
->ru_isrss
;
1337 a_user_rusage_p
->ru_minflt
= a_rusage_p
->ru_minflt
;
1338 a_user_rusage_p
->ru_majflt
= a_rusage_p
->ru_majflt
;
1339 a_user_rusage_p
->ru_nswap
= a_rusage_p
->ru_nswap
;
1340 a_user_rusage_p
->ru_inblock
= a_rusage_p
->ru_inblock
;
1341 a_user_rusage_p
->ru_oublock
= a_rusage_p
->ru_oublock
;
1342 a_user_rusage_p
->ru_msgsnd
= a_rusage_p
->ru_msgsnd
;
1343 a_user_rusage_p
->ru_msgrcv
= a_rusage_p
->ru_msgrcv
;
1344 a_user_rusage_p
->ru_nsignals
= a_rusage_p
->ru_nsignals
;
1345 a_user_rusage_p
->ru_nvcsw
= a_rusage_p
->ru_nvcsw
;
1346 a_user_rusage_p
->ru_nivcsw
= a_rusage_p
->ru_nivcsw
;