2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 /* Copyright (c) 1995, 1997 Apple Computer, Inc. All Rights Reserved */
24 * Copyright (c) 1982, 1986, 1989, 1991, 1993
25 * The Regents of the University of California. All rights reserved.
26 * (c) UNIX System Laboratories, Inc.
27 * All or some portions of this file are derived from material licensed
28 * to the University of California by American Telephone and Telegraph
29 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
30 * the permission of UNIX System Laboratories, Inc.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * @(#)kern_exit.c 8.7 (Berkeley) 2/12/94
63 #include <machine/reg.h>
64 #include <machine/psl.h>
66 #include "compat_43.h"
68 #include <sys/param.h>
69 #include <sys/systm.h>
70 #include <sys/ioctl.h>
71 #include <sys/proc_internal.h>
72 #include <sys/kauth.h>
75 #include <sys/resource.h>
76 #include <sys/kernel.h>
78 #include <sys/file_internal.h>
79 #include <sys/vnode_internal.h>
80 #include <sys/syslog.h>
81 #include <sys/malloc.h>
82 #include <sys/resourcevar.h>
83 #include <sys/ptrace.h>
85 #include <sys/aio_kern.h>
86 #include <sys/sysproto.h>
87 #include <sys/signalvar.h>
88 #include <sys/filedesc.h> /* fdfree */
89 #include <sys/shm_internal.h> /* shmexit */
90 #include <sys/acct.h> /* acct_process */
91 #include <machine/spl.h>
93 #include <bsm/audit_kernel.h>
94 #include <bsm/audit_kevents.h>
96 #include <mach/mach_types.h>
98 #include <kern/kern_types.h>
99 #include <kern/kalloc.h>
100 #include <kern/task.h>
101 #include <kern/thread.h>
102 #include <kern/sched_prim.h>
103 #include <kern/assert.h>
105 #include <sys/ktrace.h>
108 #include <mach/mach_types.h>
109 #include <mach/task.h>
110 #include <mach/thread_act.h>
111 #include <mach/mach_traps.h> /* init_process */
113 extern char init_task_failure_data
[];
114 int exit1(struct proc
*, int, int *);
115 void proc_prepareexit(struct proc
*p
);
116 void vfork_exit(struct proc
*p
, int rv
);
117 void vproc_exit(struct proc
*p
);
118 __private_extern__
void munge_rusage(struct rusage
*a_rusage_p
, struct user_rusage
*a_user_rusage_p
);
121 * Things which should have prototypes in headers, but don't
123 void unix_syscall_return(int);
124 void *get_bsduthreadarg(thread_t
);
125 void proc_exit(struct proc
*p
);
126 int wait1continue(int result
);
127 int waitidcontinue(int result
);
128 int *get_bsduthreadrval(thread_t
);
129 kern_return_t
sys_perf_notify(struct task
*task
, exception_data_t code
,
130 mach_msg_type_number_t codeCnt
);
133 * NOTE: Source and target may *NOT* overlap!
134 * XXX Should share code with bsd/dev/ppc/unix_signal.c
137 siginfo_64to32(user_siginfo_t
*in
, siginfo_t
*out
)
139 out
->si_signo
= in
->si_signo
;
140 out
->si_errno
= in
->si_errno
;
141 out
->si_code
= in
->si_code
;
142 out
->si_pid
= in
->si_pid
;
143 out
->si_uid
= in
->si_uid
;
144 out
->si_status
= in
->si_status
;
145 out
->si_addr
= CAST_DOWN(void *,in
->si_addr
);
146 /* following cast works for sival_int because of padding */
147 out
->si_value
.sival_ptr
= CAST_DOWN(void *,in
->si_value
.sival_ptr
);
148 out
->si_band
= in
->si_band
; /* range reduction */
149 out
->pad
[0] = in
->pad
[0]; /* mcontext.ss.r1 */
157 exit(struct proc
*p
, struct exit_args
*uap
, int *retval
)
159 exit1(p
, W_EXITCODE(uap
->rval
, 0), retval
);
161 /* drop funnel before we return */
162 thread_funnel_set(kernel_flock
, FALSE
);
163 thread_exception_return();
166 thread_block(THREAD_CONTINUE_NULL
);
171 * Exit: deallocate address space and other resources, change proc state
172 * to zombie, and unlink proc from allproc and parent's lists. Save exit
173 * status and rusage for wait(). Check for child processes and orphan them.
176 exit1(struct proc
*p
, int rv
, int *retval
)
178 thread_t self
= current_thread();
179 struct task
*task
= p
->task
;
184 * If a thread in this task has already
185 * called exit(), then halt any others
189 ut
= get_bsdthread_info(self
);
190 if (ut
->uu_flag
& UT_VFORK
) {
192 vfork_return(self
, p
->p_pptr
, p
, retval
);
193 unix_syscall_return(0);
196 AUDIT_SYSCALL_EXIT(0, p
, ut
); /* Exit is always successfull */
198 while (p
->exit_thread
!= self
) {
199 if (sig_try_locked(p
) <= 0) {
200 if (get_threadtask(self
) != task
) {
205 thread_terminate(self
);
206 thread_funnel_set(kernel_flock
, FALSE
);
207 thread_exception_return();
214 printf("pid 1 exited (signal %d, exit %d)",
215 WTERMSIG(rv
), WEXITSTATUS(rv
));
216 panic("init died\nState at Last Exception:\n\n%s",
217 init_task_failure_data
);
221 p
->p_flag
|= P_WEXIT
;
226 /* task terminate will call proc_terminate and that cleans it up */
227 task_terminate_internal(task
);
233 proc_prepareexit(struct proc
*p
)
236 exception_data_t code
[EXCEPTION_CODE_MAX
];
237 thread_t self
= current_thread();
239 code
[0] = (exception_data_t
)0xFF000001; /* Set terminate code */
240 code
[1] = (exception_data_t
)p
->p_pid
; /* Pass out the pid */
241 /* Notify the perf server */
242 (void)sys_perf_notify(p
->task
, (exception_data_t
)&code
, 2);
245 * Remove proc from allproc queue and from pidhash chain.
246 * Need to do this before we do anything that can block.
247 * Not doing causes things like mount() find this on allproc
248 * in partially cleaned state.
250 LIST_REMOVE(p
, p_list
);
251 LIST_INSERT_HEAD(&zombproc
, p
, p_list
); /* Place onto zombproc. */
252 LIST_REMOVE(p
, p_hash
);
258 * If parent is waiting for us to exit or exec,
259 * P_PPWAIT is set; we will wakeup the parent below.
261 p
->p_flag
&= ~(P_TRACED
| P_PPWAIT
);
262 p
->p_sigignore
= ~(sigcantmask
);
264 ut
= get_bsdthread_info(self
);
266 untimeout(realitexpire
, (caddr_t
)p
->p_pid
);
270 proc_exit(struct proc
*p
)
272 register struct proc
*q
, *nq
, *pp
;
273 struct task
*task
= p
->task
;
275 boolean_t funnel_state
;
277 /* This can happen if thread_terminate of the single thread
281 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
282 if( !(p
->p_flag
& P_WEXIT
)) {
284 p
->p_flag
|= P_WEXIT
;
289 p
->p_lflag
|= P_LPEXIT
;
290 /* XXX Zombie allocation may fail, in which case stats get lost */
291 MALLOC_ZONE(p
->p_ru
, struct rusage
*,
292 sizeof (*p
->p_ru
), M_ZOMBIE
, M_WAITOK
);
295 * need to cancel async IO requests that can be cancelled and wait for those
296 * already active. MAY BLOCK!
299 p
->p_lflag
|= P_LREFDRAIN
;
300 while (p
->p_internalref
) {
301 p
->p_lflag
|= P_LREFDRAINWAIT
;
302 msleep(&p
->p_internalref
, (lck_mtx_t
*)0, 0, "proc_refdrain", 0) ;
304 p
->p_lflag
&= ~P_LREFDRAIN
;
305 p
->p_lflag
|= P_LREFDEAD
;
310 * Close open files and release open-file table.
315 /* Close ref SYSV Shared memory*/
318 /* Release SYSV semaphores */
321 if (SESS_LEADER(p
)) {
322 register struct session
*sp
= p
->p_session
;
326 struct vfs_context context
;
329 * Controlling process.
330 * Signal foreground pgrp,
331 * drain controlling terminal
332 * and revoke access to controlling terminal.
334 if (sp
->s_ttyp
->t_session
== sp
) {
335 if (sp
->s_ttyp
->t_pgrp
)
336 pgsignal(sp
->s_ttyp
->t_pgrp
, SIGHUP
, 1);
337 (void) ttywait(sp
->s_ttyp
);
339 * The tty could have been revoked
343 context
.vc_ucred
= kauth_cred_proc_ref(p
);
345 VNOP_REVOKE(sp
->s_ttyvp
, REVOKEALL
, &context
);
346 kauth_cred_unref(&context
.vc_ucred
);
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 (IS_VALID_CRED(child
->p_ucred
)) {
578 kauth_cred_unref(&child
->p_ucred
);
582 * Release reference to text vnode
584 tvp
= child
->p_textvp
;
585 child
->p_textvp
= NULL
;
590 * Finally finished with old proc entry.
591 * Unlink it from its process group and free it.
594 LIST_REMOVE(child
, p_list
); /* off zombproc */
595 LIST_REMOVE(child
, p_sibling
);
596 child
->p_lflag
&= ~P_LWAITING
;
597 wakeup(&child
->p_stat
);
599 lck_mtx_destroy(&child
->p_mlock
, proc_lck_grp
);
600 lck_mtx_destroy(&child
->p_fdmlock
, proc_lck_grp
);
601 FREE_ZONE(child
, sizeof *child
, M_PROC
);
608 wait1continue(int result
)
619 thread
= current_thread();
620 vt
= get_bsduthreadarg(thread
);
621 retval
= get_bsduthreadrval(thread
);
622 return(wait4((struct proc
*)p
, (struct wait4_args
*)vt
, retval
));
626 wait4(struct proc
*q
, struct wait4_args
*uap
, register_t
*retval
)
629 register struct proc
*p
;
633 uap
->pid
= -q
->p_pgid
;
637 for (p
= q
->p_children
.lh_first
; p
!= 0; p
= p
->p_sibling
.le_next
) {
638 if (uap
->pid
!= WAIT_ANY
&&
639 p
->p_pid
!= uap
->pid
&&
640 p
->p_pgid
!= -(uap
->pid
))
644 /* XXX This is racy because we don't get the lock!!!! */
646 if (p
->p_lflag
& P_LWAITING
) {
647 (void)tsleep(&p
->p_stat
, PWAIT
, "waitcoll", 0);
650 p
->p_lflag
|= P_LWAITING
; /* only allow single thread to wait() */
652 if (p
->p_stat
== SZOMB
) {
653 retval
[0] = p
->p_pid
;
655 status
= p
->p_xstat
; /* convert to int */
656 error
= copyout((caddr_t
)&status
,
660 p
->p_lflag
&= ~P_LWAITING
;
666 if (p
->p_ru
== NULL
) {
669 if (IS_64BIT_PROCESS(q
)) {
670 struct user_rusage my_rusage
;
671 munge_rusage(p
->p_ru
, &my_rusage
);
672 error
= copyout((caddr_t
)&my_rusage
,
677 error
= copyout((caddr_t
)p
->p_ru
,
679 sizeof (struct rusage
));
682 /* information unavailable? */
684 p
->p_lflag
&= ~P_LWAITING
;
691 if (!reap_child_process(q
, p
)) {
692 p
->p_lflag
&= ~P_LWAITING
;
698 if (p
->p_stat
== SSTOP
&& (p
->p_flag
& P_WAITED
) == 0 &&
699 (p
->p_flag
& P_TRACED
|| uap
->options
& WUNTRACED
)) {
700 p
->p_flag
|= P_WAITED
;
701 retval
[0] = p
->p_pid
;
703 status
= W_STOPCODE(p
->p_xstat
);
704 error
= copyout((caddr_t
)&status
,
709 p
->p_lflag
&= ~P_LWAITING
;
713 p
->p_lflag
&= ~P_LWAITING
;
719 if (uap
->options
& WNOHANG
) {
724 if ((error
= tsleep0((caddr_t
)q
, PWAIT
| PCATCH
, "wait", 0, wait1continue
)))
732 waitidcontinue(int result
)
743 thread
= current_thread();
744 vt
= get_bsduthreadarg(thread
);
745 retval
= get_bsduthreadrval(thread
);
746 return(waitid((struct proc
*)p
, (struct waitid_args
*)vt
, retval
));
750 * Description: Suspend the calling thread until one child of the process
751 * containing the calling thread changes state.
753 * Parameters: uap->idtype one of P_PID, P_PGID, P_ALL
754 * uap->id pid_t or gid_t or ignored
755 * uap->infop Address of signinfo_t struct in
756 * user space into which to return status
757 * uap->options flag values
760 * !0 Error returning status to user space
763 waitid(struct proc
*q
, struct waitid_args
*uap
, register_t
*retval
)
765 user_siginfo_t collect64
; /* siginfo data to return to caller */
768 register struct proc
*p
;
773 for (p
= q
->p_children
.lh_first
; p
!= 0; p
= p
->p_sibling
.le_next
) {
774 switch(uap
->idtype
) {
775 case P_PID
: /* child with process ID equal to... */
776 if (p
->p_pid
!= (pid_t
)uap
->id
)
779 case P_PGID
: /* child with process group ID equal to... */
780 if (p
->p_pgid
!= (pid_t
)uap
->id
)
783 case P_ALL
: /* any child */
787 /* XXX This is racy because we don't get the lock!!!! */
790 * Wait collision; go to sleep and restart; used to maintain
791 * the single return for waited process guarantee.
793 if (p
->p_lflag
& P_LWAITING
) {
794 (void)tsleep(&p
->p_stat
, PWAIT
, "waitidcoll", 0);
797 p
->p_lflag
|= P_LWAITING
; /* mark busy */
802 * Types of processes we are interested in
804 * XXX Don't know what to do for WCONTINUED?!?
807 case SZOMB
: /* Exited */
808 if (!(uap
->options
& WEXITED
))
811 /* Collect "siginfo" information for caller */
812 collect64
.si_signo
= 0;
813 collect64
.si_code
= 0;
814 collect64
.si_errno
= 0;
815 collect64
.si_pid
= 0;
816 collect64
.si_uid
= 0;
817 collect64
.si_addr
= 0;
818 collect64
.si_status
= p
->p_xstat
;
819 collect64
.si_band
= 0;
821 if (IS_64BIT_PROCESS(p
)) {
822 error
= copyout((caddr_t
)&collect64
,
827 siginfo_64to32(&collect64
,&collect
);
828 error
= copyout((caddr_t
)&collect
,
832 /* information unavailable? */
834 p
->p_lflag
&= ~P_LWAITING
;
839 /* Prevent other process for waiting for this event? */
840 if (!(uap
->options
& WNOWAIT
)) {
842 if (!reap_child_process(q
, p
)) {
843 p
->p_lflag
&= ~P_LWAITING
;
850 case SSTOP
: /* Stopped */
852 * If we are not interested in stopped processes, then
855 if (!(uap
->options
& WSTOPPED
))
859 * If someone has already waited it, we lost a race
860 * to be the one to return status.
862 if ((p
->p_flag
& P_WAITED
) != 0)
866 * If this is not a traced process, and they haven't
867 * indicated an interest in untraced processes, then
870 if (!(p
->p_flag
& P_TRACED
) && !(uap
->options
& WUNTRACED
))
873 /* Collect "siginfo" information for caller */
874 collect64
.si_signo
= 0;
875 collect64
.si_code
= 0;
876 collect64
.si_errno
= 0;
877 collect64
.si_pid
= 0;
878 collect64
.si_uid
= 0;
879 collect64
.si_addr
= 0;
880 collect64
.si_status
= p
->p_xstat
;
881 collect64
.si_band
= 0;
883 if (IS_64BIT_PROCESS(p
)) {
884 error
= copyout((caddr_t
)&collect64
,
889 siginfo_64to32(&collect64
,&collect
);
890 error
= copyout((caddr_t
)&collect
,
894 /* information unavailable? */
896 p
->p_lflag
&= ~P_LWAITING
;
901 /* Prevent other process for waiting for this event? */
902 if (!(uap
->options
& WNOWAIT
)) {
903 p
->p_flag
|= P_WAITED
;
906 p
->p_lflag
&= ~P_LWAITING
;
910 default: /* All others */
911 /* ...meaning Continued */
912 if (!(uap
->options
& WCONTINUED
))
916 * If the flag isn't set, then this process has not
917 * been stopped and continued, or the status has
918 * already been reaped by another caller of waitid().
920 if ((p
->p_flag
& P_CONTINUED
) == 0)
923 /* Collect "siginfo" information for caller */
924 collect64
.si_signo
= 0;
925 collect64
.si_code
= 0;
926 collect64
.si_errno
= 0;
927 collect64
.si_pid
= 0;
928 collect64
.si_uid
= 0;
929 collect64
.si_addr
= 0;
930 collect64
.si_status
= p
->p_xstat
;
931 collect64
.si_band
= 0;
933 if (IS_64BIT_PROCESS(p
)) {
934 error
= copyout((caddr_t
)&collect64
,
939 siginfo_64to32(&collect64
,&collect
);
940 error
= copyout((caddr_t
)&collect
,
944 /* information unavailable? */
946 p
->p_lflag
&= ~P_LWAITING
;
951 /* Prevent other process for waiting for this event? */
952 if (!(uap
->options
& WNOWAIT
)) {
953 p
->p_flag
&= ~P_CONTINUED
;
956 p
->p_lflag
&= ~P_LWAITING
;
964 /* Not a process we are interested in; go on to next child */
965 p
->p_lflag
&= ~P_LWAITING
;
969 /* No child processes that could possibly satisfy the request? */
973 if (uap
->options
& WNOHANG
) {
978 if ((error
= tsleep0((caddr_t
)q
, PWAIT
| PCATCH
, "waitid", 0, waitidcontinue
)))
985 * make process 'parent' the new parent of process 'child'.
988 proc_reparent(struct proc
*child
, struct proc
*parent
)
991 if (child
->p_pptr
== parent
)
994 LIST_REMOVE(child
, p_sibling
);
995 LIST_INSERT_HEAD(&parent
->p_children
, child
, p_sibling
);
996 child
->p_pptr
= parent
;
998 if (initproc
== parent
&& child
->p_stat
== SZOMB
)
999 psignal(initproc
, SIGCHLD
);
1003 * Make the current process an "init" process, meaning
1004 * that it doesn't have a parent, and that it won't be
1005 * gunned down by kill(-1, 0).
1008 init_process(__unused
struct init_process_args
*args
)
1010 register struct proc
*p
= current_proc();
1012 AUDIT_MACH_SYSCALL_ENTER(AUE_INITPROCESS
);
1013 if (suser(kauth_cred_get(), &p
->p_acflag
)) {
1014 AUDIT_MACH_SYSCALL_EXIT(KERN_NO_ACCESS
);
1015 return(KERN_NO_ACCESS
);
1018 if (p
->p_pid
!= 1 && p
->p_pgid
!= p
->p_pid
)
1019 enterpgrp(p
, p
->p_pid
, 0);
1020 p
->p_flag
|= P_SYSTEM
;
1023 * Take us out of the sibling chain, and
1024 * out of our parent's child chain.
1026 LIST_REMOVE(p
, p_sibling
);
1027 p
->p_sibling
.le_prev
= NULL
;
1028 p
->p_sibling
.le_next
= NULL
;
1029 p
->p_pptr
= kernproc
;
1031 AUDIT_MACH_SYSCALL_EXIT(KERN_SUCCESS
);
1032 return(KERN_SUCCESS
);
1037 * Exit: deallocate address space and other resources, change proc state
1038 * to zombie, and unlink proc from allproc and parent's lists. Save exit
1039 * status and rusage for wait(). Check for child processes and orphan them.
1043 vfork_exit(struct proc
*p
, int rv
)
1045 thread_t self
= current_thread();
1047 struct task
*task
= p
->task
;
1051 exception_data_t code
[EXCEPTION_CODE_MAX
];
1054 * If a thread in this task has already
1055 * called exit(), then halt any others
1059 ut
= get_bsdthread_info(self
);
1062 while (p
->exit_thread
!= self
) {
1063 if (sig_try_locked(p
) <= 0) {
1064 if (get_threadtask(self
) != task
) {
1069 thread_terminate(self
);
1070 thread_funnel_set(kernel_flock
, FALSE
);
1071 thread_exception_return();
1074 sig_lock_to_exit(p
);
1077 if (p
->p_pid
== 1) {
1078 printf("pid 1 exited (signal %d, exit %d)",
1079 WTERMSIG(rv
), WEXITSTATUS(rv
));
1080 panic("init died\nState at Last Exception:\n\n%s", init_task_failure_data
);
1085 p
->p_flag
|= P_WEXIT
;
1086 p
->p_lflag
|= P_LPEXIT
;
1089 code
[0] = (exception_data_t
)0xFF000001; /* Set terminate code */
1090 code
[1] = (exception_data_t
)p
->p_pid
; /* Pass out the pid */
1091 /* Notify the perf server */
1092 (void)sys_perf_notify(p
->task
, (exception_data_t
)&code
, 2);
1095 * Remove proc from allproc queue and from pidhash chain.
1096 * Need to do this before we do anything that can block.
1097 * Not doing causes things like mount() find this on allproc
1098 * in partially cleaned state.
1100 LIST_REMOVE(p
, p_list
);
1101 LIST_INSERT_HEAD(&zombproc
, p
, p_list
); /* Place onto zombproc. */
1102 LIST_REMOVE(p
, p_hash
);
1104 * If parent is waiting for us to exit or exec,
1105 * P_PPWAIT is set; we will wakeup the parent below.
1107 p
->p_flag
&= ~(P_TRACED
| P_PPWAIT
);
1108 p
->p_sigignore
= ~0;
1112 untimeout(realitexpire
, (caddr_t
)p
->p_pid
);
1120 vproc_exit(struct proc
*p
)
1122 register struct proc
*q
, *nq
, *pp
;
1124 struct task
*task
= p
->task
;
1127 /* XXX Zombie allocation may fail, in which case stats get lost */
1128 MALLOC_ZONE(p
->p_ru
, struct rusage
*,
1129 sizeof (*p
->p_ru
), M_ZOMBIE
, M_WAITOK
);
1132 * Close open files and release open-file table.
1137 if (SESS_LEADER(p
)) {
1138 register struct session
*sp
= p
->p_session
;
1141 struct vnode
*ttyvp
;
1142 struct vfs_context context
;
1145 * Controlling process.
1146 * Signal foreground pgrp,
1147 * drain controlling terminal
1148 * and revoke access to controlling terminal.
1150 if (sp
->s_ttyp
->t_session
== sp
) {
1151 if (sp
->s_ttyp
->t_pgrp
)
1152 pgsignal(sp
->s_ttyp
->t_pgrp
, SIGHUP
, 1);
1153 (void) ttywait(sp
->s_ttyp
);
1155 * The tty could have been revoked
1158 context
.vc_proc
= p
;
1159 context
.vc_ucred
= kauth_cred_proc_ref(p
);
1161 VNOP_REVOKE(sp
->s_ttyvp
, REVOKEALL
, &context
);
1162 kauth_cred_unref(&context
.vc_ucred
);
1164 ttyvp
= sp
->s_ttyvp
;
1170 * s_ttyp is not zero'd; we use this to indicate
1171 * that the session once had a controlling terminal.
1172 * (for logging and informational purposes)
1175 sp
->s_leader
= NULL
;
1178 fixjobc(p
, p
->p_pgrp
, 0);
1179 p
->p_rlimit
[RLIMIT_FSIZE
].rlim_cur
= RLIM_INFINITY
;
1183 * release trace file
1185 p
->p_traceflag
= 0; /* don't trace the vnode_rele() */
1187 struct vnode
*tvp
= p
->p_tracep
;
1193 while (q
= p
->p_children
.lh_first
) {
1194 proc_reparent(q
, initproc
);
1196 * Traced processes are killed
1197 * since their existence means someone is messing up.
1199 if (q
->p_flag
& P_TRACED
) {
1200 q
->p_flag
&= ~P_TRACED
;
1201 if (q
->sigwait_thread
) {
1203 * The sigwait_thread could be stopped at a
1204 * breakpoint. Wake it up to kill.
1205 * Need to do this as it could be a thread which is not
1206 * the first thread in the task. So any attempts to kill
1207 * the process would result into a deadlock on q->sigwait.
1209 thread_resume((thread_t
)q
->sigwait_thread
);
1210 clear_wait(q
->sigwait_thread
, THREAD_INTERRUPTED
);
1211 threadsignal((thread_t
)q
->sigwait_thread
, SIGKILL
, 0);
1213 psignal(q
, SIGKILL
);
1218 * Save exit status and final rusage info, adding in child rusage
1219 * info and self times. If we were unable to allocate a zombie
1220 * structure, this information is lost.
1222 if (p
->p_ru
!= NULL
) {
1223 *p
->p_ru
= p
->p_stats
->p_ru
;
1224 timerclear(&p
->p_ru
->ru_utime
);
1225 timerclear(&p
->p_ru
->ru_stime
);
1229 task_basic_info_data_t tinfo
;
1230 task_thread_times_info_data_t ttimesinfo
;
1231 int task_info_stuff
, task_ttimes_stuff
;
1232 struct timeval ut
,st
;
1234 task_info_stuff
= TASK_BASIC_INFO_COUNT
;
1235 task_info(task
, TASK_BASIC_INFO
,
1236 &tinfo
, &task_info_stuff
);
1237 p
->p_ru
->ru_utime
.tv_sec
= tinfo
.user_time
.seconds
;
1238 p
->p_ru
->ru_utime
.tv_usec
= tinfo
.user_time
.microseconds
;
1239 p
->p_ru
->ru_stime
.tv_sec
= tinfo
.system_time
.seconds
;
1240 p
->p_ru
->ru_stime
.tv_usec
= tinfo
.system_time
.microseconds
;
1242 task_ttimes_stuff
= TASK_THREAD_TIMES_INFO_COUNT
;
1243 task_info(task
, TASK_THREAD_TIMES_INFO
,
1244 &ttimesinfo
, &task_ttimes_stuff
);
1246 ut
.tv_sec
= ttimesinfo
.user_time
.seconds
;
1247 ut
.tv_usec
= ttimesinfo
.user_time
.microseconds
;
1248 st
.tv_sec
= ttimesinfo
.system_time
.seconds
;
1249 st
.tv_usec
= ttimesinfo
.system_time
.microseconds
;
1250 timeradd(&ut
,&p
->p_ru
->ru_utime
,&p
->p_ru
->ru_utime
);
1251 timeradd(&st
,&p
->p_ru
->ru_stime
,&p
->p_ru
->ru_stime
);
1255 ruadd(p
->p_ru
, &p
->p_stats
->p_cru
);
1259 * Free up profiling buffers.
1262 struct uprof
*p0
= &p
->p_stats
->p_prof
, *p1
, *pn
;
1268 for (; p1
!= NULL
; p1
= pn
) {
1270 kfree(p1
, sizeof *p1
);
1275 * Other substructures are freed from wait().
1277 FREE_ZONE(p
->p_stats
, sizeof *p
->p_stats
, M_SUBPROC
);
1280 FREE_ZONE(p
->p_sigacts
, sizeof *p
->p_sigacts
, M_SUBPROC
);
1281 p
->p_sigacts
= NULL
;
1283 if (--p
->p_limit
->p_refcnt
== 0)
1284 FREE_ZONE(p
->p_limit
, sizeof *p
->p_limit
, M_SUBPROC
);
1288 * Finish up by terminating the task
1289 * and halt this thread (only if a
1290 * member of the task exiting).
1292 p
->task
= TASK_NULL
;
1295 * Notify parent that we're gone.
1298 if (pp
!= initproc
) {
1299 pp
->si_pid
= p
->p_pid
;
1300 pp
->si_status
= p
->p_xstat
;
1301 pp
->si_code
= CLD_EXITED
;
1302 pp
->si_uid
= p
->p_ucred
->cr_ruid
;
1304 /* mark as a zombie */
1307 psignal(p
->p_pptr
, SIGCHLD
);
1309 /* and now wakeup the parent */
1310 wakeup((caddr_t
)p
->p_pptr
);
1316 * LP64 support - long is 64 bits if we are dealing with a 64 bit user
1317 * process. We munge the kernel (32 bit) version of rusage into the
1320 __private_extern__
void
1321 munge_rusage(struct rusage
*a_rusage_p
, struct user_rusage
*a_user_rusage_p
)
1323 /* timeval changes size, so utime and stime need special handling */
1324 a_user_rusage_p
->ru_utime
.tv_sec
= a_rusage_p
->ru_utime
.tv_sec
;
1325 a_user_rusage_p
->ru_utime
.tv_usec
= a_rusage_p
->ru_utime
.tv_usec
;
1326 a_user_rusage_p
->ru_stime
.tv_sec
= a_rusage_p
->ru_stime
.tv_sec
;
1327 a_user_rusage_p
->ru_stime
.tv_usec
= a_rusage_p
->ru_stime
.tv_usec
;
1329 * everything else can be a direct assign, since there is no loss
1330 * of precision implied boing 32->64.
1332 a_user_rusage_p
->ru_maxrss
= a_rusage_p
->ru_maxrss
;
1333 a_user_rusage_p
->ru_ixrss
= a_rusage_p
->ru_ixrss
;
1334 a_user_rusage_p
->ru_idrss
= a_rusage_p
->ru_idrss
;
1335 a_user_rusage_p
->ru_isrss
= a_rusage_p
->ru_isrss
;
1336 a_user_rusage_p
->ru_minflt
= a_rusage_p
->ru_minflt
;
1337 a_user_rusage_p
->ru_majflt
= a_rusage_p
->ru_majflt
;
1338 a_user_rusage_p
->ru_nswap
= a_rusage_p
->ru_nswap
;
1339 a_user_rusage_p
->ru_inblock
= a_rusage_p
->ru_inblock
;
1340 a_user_rusage_p
->ru_oublock
= a_rusage_p
->ru_oublock
;
1341 a_user_rusage_p
->ru_msgsnd
= a_rusage_p
->ru_msgsnd
;
1342 a_user_rusage_p
->ru_msgrcv
= a_rusage_p
->ru_msgrcv
;
1343 a_user_rusage_p
->ru_nsignals
= a_rusage_p
->ru_nsignals
;
1344 a_user_rusage_p
->ru_nvcsw
= a_rusage_p
->ru_nvcsw
;
1345 a_user_rusage_p
->ru_nivcsw
= a_rusage_p
->ru_nivcsw
;