2 * Copyright (c) 2006 Apple Computer, Inc. All Rights Reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
30 /* Copyright (c) 1995, 1997 Apple Computer, Inc. All Rights Reserved */
32 * Copyright (c) 1982, 1986, 1989, 1991, 1993
33 * The Regents of the University of California. All rights reserved.
34 * (c) UNIX System Laboratories, Inc.
35 * All or some portions of this file are derived from material licensed
36 * to the University of California by American Telephone and Telegraph
37 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
38 * the permission of UNIX System Laboratories, Inc.
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 * 3. All advertising materials mentioning features or use of this software
49 * must display the following acknowledgement:
50 * This product includes software developed by the University of
51 * California, Berkeley and its contributors.
52 * 4. Neither the name of the University nor the names of its contributors
53 * may be used to endorse or promote products derived from this software
54 * without specific prior written permission.
56 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
57 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
58 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
59 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
60 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
61 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
62 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
63 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
64 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
65 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 * @(#)kern_exit.c 8.7 (Berkeley) 2/12/94
71 #include <machine/reg.h>
72 #include <machine/psl.h>
74 #include "compat_43.h"
76 #include <sys/param.h>
77 #include <sys/systm.h>
78 #include <sys/ioctl.h>
79 #include <sys/proc_internal.h>
80 #include <sys/kauth.h>
83 #include <sys/resource.h>
84 #include <sys/kernel.h>
86 #include <sys/file_internal.h>
87 #include <sys/vnode_internal.h>
88 #include <sys/syslog.h>
89 #include <sys/malloc.h>
90 #include <sys/resourcevar.h>
91 #include <sys/ptrace.h>
93 #include <sys/aio_kern.h>
94 #include <sys/sysproto.h>
95 #include <sys/signalvar.h>
96 #include <sys/filedesc.h> /* fdfree */
97 #include <sys/shm_internal.h> /* shmexit */
98 #include <sys/acct.h> /* acct_process */
99 #include <machine/spl.h>
101 #include <bsm/audit_kernel.h>
102 #include <bsm/audit_kevents.h>
104 #include <mach/mach_types.h>
106 #include <kern/kern_types.h>
107 #include <kern/kalloc.h>
108 #include <kern/task.h>
109 #include <kern/thread.h>
110 #include <kern/sched_prim.h>
111 #include <kern/assert.h>
113 #include <sys/ktrace.h>
116 #include <mach/mach_types.h>
117 #include <mach/task.h>
118 #include <mach/thread_act.h>
119 #include <mach/mach_traps.h> /* init_process */
121 extern char init_task_failure_data
[];
122 int exit1(struct proc
*, int, int *);
123 void proc_prepareexit(struct proc
*p
);
124 void vfork_exit(struct proc
*p
, int rv
);
125 void vproc_exit(struct proc
*p
);
126 __private_extern__
void munge_rusage(struct rusage
*a_rusage_p
, struct user_rusage
*a_user_rusage_p
);
129 * Things which should have prototypes in headers, but don't
131 void unix_syscall_return(int);
132 void *get_bsduthreadarg(thread_t
);
133 void proc_exit(struct proc
*p
);
134 int wait1continue(int result
);
135 int waitidcontinue(int result
);
136 int *get_bsduthreadrval(thread_t
);
137 kern_return_t
sys_perf_notify(struct task
*task
, exception_data_t code
,
138 mach_msg_type_number_t codeCnt
);
141 * NOTE: Source and target may *NOT* overlap!
142 * XXX Should share code with bsd/dev/ppc/unix_signal.c
145 siginfo_64to32(user_siginfo_t
*in
, siginfo_t
*out
)
147 out
->si_signo
= in
->si_signo
;
148 out
->si_errno
= in
->si_errno
;
149 out
->si_code
= in
->si_code
;
150 out
->si_pid
= in
->si_pid
;
151 out
->si_uid
= in
->si_uid
;
152 out
->si_status
= in
->si_status
;
153 out
->si_addr
= CAST_DOWN(void *,in
->si_addr
);
154 /* following cast works for sival_int because of padding */
155 out
->si_value
.sival_ptr
= CAST_DOWN(void *,in
->si_value
.sival_ptr
);
156 out
->si_band
= in
->si_band
; /* range reduction */
157 out
->pad
[0] = in
->pad
[0]; /* mcontext.ss.r1 */
165 exit(struct proc
*p
, struct exit_args
*uap
, int *retval
)
167 exit1(p
, W_EXITCODE(uap
->rval
, 0), retval
);
169 /* drop funnel before we return */
170 thread_funnel_set(kernel_flock
, FALSE
);
171 thread_exception_return();
174 thread_block(THREAD_CONTINUE_NULL
);
179 * Exit: deallocate address space and other resources, change proc state
180 * to zombie, and unlink proc from allproc and parent's lists. Save exit
181 * status and rusage for wait(). Check for child processes and orphan them.
184 exit1(struct proc
*p
, int rv
, int *retval
)
186 thread_t self
= current_thread();
187 struct task
*task
= p
->task
;
192 * If a thread in this task has already
193 * called exit(), then halt any others
197 ut
= get_bsdthread_info(self
);
198 if (ut
->uu_flag
& UT_VFORK
) {
200 vfork_return(self
, p
->p_pptr
, p
, retval
);
201 unix_syscall_return(0);
204 AUDIT_SYSCALL_EXIT(0, p
, ut
); /* Exit is always successfull */
206 while (p
->exit_thread
!= self
) {
207 if (sig_try_locked(p
) <= 0) {
208 if (get_threadtask(self
) != task
) {
213 thread_terminate(self
);
214 thread_funnel_set(kernel_flock
, FALSE
);
215 thread_exception_return();
222 printf("pid 1 exited (signal %d, exit %d)",
223 WTERMSIG(rv
), WEXITSTATUS(rv
));
224 panic("init died\nState at Last Exception:\n\n%s",
225 init_task_failure_data
);
229 p
->p_flag
|= P_WEXIT
;
234 /* task terminate will call proc_terminate and that cleans it up */
235 task_terminate_internal(task
);
241 proc_prepareexit(struct proc
*p
)
244 exception_data_t code
[EXCEPTION_CODE_MAX
];
245 thread_t self
= current_thread();
247 code
[0] = (exception_data_t
)0xFF000001; /* Set terminate code */
248 code
[1] = (exception_data_t
)p
->p_pid
; /* Pass out the pid */
249 /* Notify the perf server */
250 (void)sys_perf_notify(p
->task
, (exception_data_t
)&code
, 2);
253 * Remove proc from allproc queue and from pidhash chain.
254 * Need to do this before we do anything that can block.
255 * Not doing causes things like mount() find this on allproc
256 * in partially cleaned state.
258 LIST_REMOVE(p
, p_list
);
259 LIST_INSERT_HEAD(&zombproc
, p
, p_list
); /* Place onto zombproc. */
260 LIST_REMOVE(p
, p_hash
);
266 * If parent is waiting for us to exit or exec,
267 * P_PPWAIT is set; we will wakeup the parent below.
269 p
->p_flag
&= ~(P_TRACED
| P_PPWAIT
);
270 p
->p_sigignore
= ~(sigcantmask
);
272 ut
= get_bsdthread_info(self
);
274 untimeout(realitexpire
, (caddr_t
)p
->p_pid
);
278 proc_exit(struct proc
*p
)
280 register struct proc
*q
, *nq
, *pp
;
281 struct task
*task
= p
->task
;
283 boolean_t funnel_state
;
285 /* This can happen if thread_terminate of the single thread
289 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
290 if( !(p
->p_flag
& P_WEXIT
)) {
292 p
->p_flag
|= P_WEXIT
;
297 p
->p_lflag
|= P_LPEXIT
;
298 /* XXX Zombie allocation may fail, in which case stats get lost */
299 MALLOC_ZONE(p
->p_ru
, struct rusage
*,
300 sizeof (*p
->p_ru
), M_ZOMBIE
, M_WAITOK
);
303 * need to cancel async IO requests that can be cancelled and wait for those
304 * already active. MAY BLOCK!
307 p
->p_lflag
|= P_LREFDRAIN
;
308 while (p
->p_internalref
) {
309 p
->p_lflag
|= P_LREFDRAINWAIT
;
310 msleep(&p
->p_internalref
, (lck_mtx_t
*)0, 0, "proc_refdrain", 0) ;
312 p
->p_lflag
&= ~P_LREFDRAIN
;
313 p
->p_lflag
|= P_LREFDEAD
;
318 * Close open files and release open-file table.
323 /* Close ref SYSV Shared memory*/
326 /* Release SYSV semaphores */
329 if (SESS_LEADER(p
)) {
330 register struct session
*sp
= p
->p_session
;
334 struct vfs_context context
;
337 * Controlling process.
338 * Signal foreground pgrp,
339 * drain controlling terminal
340 * and revoke access to controlling terminal.
342 if (sp
->s_ttyp
->t_session
== sp
) {
343 if (sp
->s_ttyp
->t_pgrp
)
344 pgsignal(sp
->s_ttyp
->t_pgrp
, SIGHUP
, 1);
345 (void) ttywait(sp
->s_ttyp
);
347 * The tty could have been revoked
351 context
.vc_ucred
= p
->p_ucred
;
353 VNOP_REVOKE(sp
->s_ttyvp
, REVOKEALL
, &context
);
361 * s_ttyp is not zero'd; we use this to indicate
362 * that the session once had a controlling terminal.
363 * (for logging and informational purposes)
369 fixjobc(p
, p
->p_pgrp
, 0);
370 p
->p_rlimit
[RLIMIT_FSIZE
].rlim_cur
= RLIM_INFINITY
;
371 (void)acct_process(p
);
377 p
->p_traceflag
= 0; /* don't trace the vnode_put() */
379 struct vnode
*tvp
= p
->p_tracep
;
385 while (q
= p
->p_children
.lh_first
) {
386 proc_reparent(q
, initproc
);
388 * Traced processes are killed
389 * since their existence means someone is messing up.
391 if (q
->p_flag
& P_TRACED
) {
392 q
->p_flag
&= ~P_TRACED
;
393 if (q
->sigwait_thread
) {
395 * The sigwait_thread could be stopped at a
396 * breakpoint. Wake it up to kill.
397 * Need to do this as it could be a thread which is not
398 * the first thread in the task. So any attempts to kill
399 * the process would result into a deadlock on q->sigwait.
401 thread_resume((thread_t
)q
->sigwait_thread
);
402 clear_wait(q
->sigwait_thread
, THREAD_INTERRUPTED
);
403 threadsignal((thread_t
)q
->sigwait_thread
, SIGKILL
, 0);
410 * Save exit status and final rusage info, adding in child rusage
411 * info and self times. If we were unable to allocate a zombie
412 * structure, this information is lost.
414 if (p
->p_ru
!= NULL
) {
415 *p
->p_ru
= p
->p_stats
->p_ru
;
417 timerclear(&p
->p_ru
->ru_utime
);
418 timerclear(&p
->p_ru
->ru_stime
);
421 task_basic_info_data_t tinfo
;
422 task_thread_times_info_data_t ttimesinfo
;
423 int task_info_stuff
, task_ttimes_stuff
;
424 struct timeval ut
,st
;
426 task_info_stuff
= TASK_BASIC_INFO_COUNT
;
427 task_info(task
, TASK_BASIC_INFO
,
428 (task_info_t
)&tinfo
, &task_info_stuff
);
429 p
->p_ru
->ru_utime
.tv_sec
= tinfo
.user_time
.seconds
;
430 p
->p_ru
->ru_utime
.tv_usec
= tinfo
.user_time
.microseconds
;
431 p
->p_ru
->ru_stime
.tv_sec
= tinfo
.system_time
.seconds
;
432 p
->p_ru
->ru_stime
.tv_usec
= tinfo
.system_time
.microseconds
;
434 task_ttimes_stuff
= TASK_THREAD_TIMES_INFO_COUNT
;
435 task_info(task
, TASK_THREAD_TIMES_INFO
,
436 (task_info_t
)&ttimesinfo
, &task_ttimes_stuff
);
438 ut
.tv_sec
= ttimesinfo
.user_time
.seconds
;
439 ut
.tv_usec
= ttimesinfo
.user_time
.microseconds
;
440 st
.tv_sec
= ttimesinfo
.system_time
.seconds
;
441 st
.tv_usec
= ttimesinfo
.system_time
.microseconds
;
442 timeradd(&ut
,&p
->p_ru
->ru_utime
,&p
->p_ru
->ru_utime
);
443 timeradd(&st
,&p
->p_ru
->ru_stime
,&p
->p_ru
->ru_stime
);
446 ruadd(p
->p_ru
, &p
->p_stats
->p_cru
);
450 * Free up profiling buffers.
453 struct uprof
*p0
= &p
->p_stats
->p_prof
, *p1
, *pn
;
459 for (; p1
!= NULL
; p1
= pn
) {
461 kfree(p1
, sizeof *p1
);
466 * Other substructures are freed from wait().
468 FREE_ZONE(p
->p_stats
, sizeof *p
->p_stats
, M_SUBPROC
);
471 FREE_ZONE(p
->p_sigacts
, sizeof *p
->p_sigacts
, M_SUBPROC
);
474 if (--p
->p_limit
->p_refcnt
== 0)
475 FREE_ZONE(p
->p_limit
, sizeof *p
->p_limit
, M_SUBPROC
);
479 * Finish up by terminating the task
480 * and halt this thread (only if a
481 * member of the task exiting).
485 set_bsdtask_info(task
, NULL
);
487 KNOTE(&p
->p_klist
, NOTE_EXIT
);
490 * Notify parent that we're gone.
492 if (p
->p_pptr
->p_flag
& P_NOCLDWAIT
) {
493 struct proc
*opp
= p
->p_pptr
;
496 * Add child resource usage to parent before giving
497 * zombie to init. If we were unable to allocate a
498 * zombie structure, this information is lost.
501 ruadd(&p
->p_pptr
->p_stats
->p_cru
, p
->p_ru
);
503 proc_reparent(p
, initproc
);
504 /* If there are no more children wakeup parent */
505 if (LIST_EMPTY(&opp
->p_children
))
506 wakeup((caddr_t
)opp
);
508 /* should be fine as parent proc would be initproc */
510 if (pp
!= initproc
) {
511 pp
->si_pid
= p
->p_pid
;
512 pp
->si_status
= p
->p_xstat
;
513 pp
->si_code
= CLD_EXITED
;
514 pp
->si_uid
= p
->p_ucred
->cr_ruid
;
516 /* mark as a zombie */
519 psignal(pp
, SIGCHLD
);
521 /* and now wakeup the parent */
522 wakeup((caddr_t
)p
->p_pptr
);
524 (void) thread_funnel_set(kernel_flock
, funnel_state
);
531 * Description: Given a process from which all status information needed
532 * has already been extracted, if the process is a ptrace
533 * attach process, detach it and give it back to its real
534 * parent, else recover all resources remaining associated
537 * Parameters: struct proc *parent Parent of process being reaped
538 * struct proc *child Process to reap
540 * Returns: 0 Process was not reaped because it
541 * came from an attach
542 * 1 Process was reaped
545 reap_child_process(struct proc
*parent
, struct proc
*child
)
547 struct proc
*trace_parent
; /* Traced parent process, if tracing */
548 struct vnode
*tvp
; /* Traced vnode pointer, if used */
551 * If we got the child via a ptrace 'attach',
552 * we need to give it back to the old parent.
554 if (child
->p_oppid
&& (trace_parent
= pfind(child
->p_oppid
))) {
556 proc_reparent(child
, trace_parent
);
557 if (trace_parent
!= initproc
) {
558 trace_parent
->si_pid
= child
->p_pid
;
559 trace_parent
->si_status
= child
->p_xstat
;
560 trace_parent
->si_code
= CLD_CONTINUED
;
561 trace_parent
->si_uid
= child
->p_ucred
->cr_ruid
;
563 psignal(trace_parent
, SIGCHLD
);
564 wakeup((caddr_t
)trace_parent
);
569 ruadd(&parent
->p_stats
->p_cru
, child
->p_ru
);
570 FREE_ZONE(child
->p_ru
, sizeof *child
->p_ru
, M_ZOMBIE
);
573 printf("Warning : lost p_ru for %s\n", child
->p_comm
);
577 * Decrement the count of procs running with this uid.
579 (void)chgproccnt(child
->p_ucred
->cr_ruid
, -1);
582 * Free up credentials.
584 if (child
->p_ucred
!= NOCRED
) {
585 kauth_cred_t ucr
= child
->p_ucred
;
586 child
->p_ucred
= NOCRED
;
587 kauth_cred_rele(ucr
);
591 * Release reference to text vnode
593 tvp
= child
->p_textvp
;
594 child
->p_textvp
= NULL
;
599 * Finally finished with old proc entry.
600 * Unlink it from its process group and free it.
603 LIST_REMOVE(child
, p_list
); /* off zombproc */
604 LIST_REMOVE(child
, p_sibling
);
605 child
->p_lflag
&= ~P_LWAITING
;
606 wakeup(&child
->p_stat
);
608 lck_mtx_destroy(&child
->p_mlock
, proc_lck_grp
);
609 lck_mtx_destroy(&child
->p_fdmlock
, proc_lck_grp
);
610 FREE_ZONE(child
, sizeof *child
, M_PROC
);
617 wait1continue(int result
)
628 thread
= current_thread();
629 vt
= get_bsduthreadarg(thread
);
630 retval
= get_bsduthreadrval(thread
);
631 return(wait4((struct proc
*)p
, (struct wait4_args
*)vt
, retval
));
635 wait4(struct proc
*q
, struct wait4_args
*uap
, register_t
*retval
)
638 register struct proc
*p
;
642 uap
->pid
= -q
->p_pgid
;
646 for (p
= q
->p_children
.lh_first
; p
!= 0; p
= p
->p_sibling
.le_next
) {
647 if (uap
->pid
!= WAIT_ANY
&&
648 p
->p_pid
!= uap
->pid
&&
649 p
->p_pgid
!= -(uap
->pid
))
653 /* XXX This is racy because we don't get the lock!!!! */
655 if (p
->p_lflag
& P_LWAITING
) {
656 (void)tsleep(&p
->p_stat
, PWAIT
, "waitcoll", 0);
659 p
->p_lflag
|= P_LWAITING
; /* only allow single thread to wait() */
661 if (p
->p_stat
== SZOMB
) {
662 retval
[0] = p
->p_pid
;
664 status
= p
->p_xstat
; /* convert to int */
665 error
= copyout((caddr_t
)&status
,
669 p
->p_lflag
&= ~P_LWAITING
;
675 if (p
->p_ru
== NULL
) {
678 if (IS_64BIT_PROCESS(q
)) {
679 struct user_rusage my_rusage
;
680 munge_rusage(p
->p_ru
, &my_rusage
);
681 error
= copyout((caddr_t
)&my_rusage
,
686 error
= copyout((caddr_t
)p
->p_ru
,
688 sizeof (struct rusage
));
691 /* information unavailable? */
693 p
->p_lflag
&= ~P_LWAITING
;
700 if (!reap_child_process(q
, p
)) {
701 p
->p_lflag
&= ~P_LWAITING
;
707 if (p
->p_stat
== SSTOP
&& (p
->p_flag
& P_WAITED
) == 0 &&
708 (p
->p_flag
& P_TRACED
|| uap
->options
& WUNTRACED
)) {
709 p
->p_flag
|= P_WAITED
;
710 retval
[0] = p
->p_pid
;
712 status
= W_STOPCODE(p
->p_xstat
);
713 error
= copyout((caddr_t
)&status
,
718 p
->p_lflag
&= ~P_LWAITING
;
722 p
->p_lflag
&= ~P_LWAITING
;
728 if (uap
->options
& WNOHANG
) {
733 if ((error
= tsleep0((caddr_t
)q
, PWAIT
| PCATCH
, "wait", 0, wait1continue
)))
741 waitidcontinue(int result
)
752 thread
= current_thread();
753 vt
= get_bsduthreadarg(thread
);
754 retval
= get_bsduthreadrval(thread
);
755 return(waitid((struct proc
*)p
, (struct waitid_args
*)vt
, retval
));
759 * Description: Suspend the calling thread until one child of the process
760 * containing the calling thread changes state.
762 * Parameters: uap->idtype one of P_PID, P_PGID, P_ALL
763 * uap->id pid_t or gid_t or ignored
764 * uap->infop Address of signinfo_t struct in
765 * user space into which to return status
766 * uap->options flag values
769 * !0 Error returning status to user space
772 waitid(struct proc
*q
, struct waitid_args
*uap
, register_t
*retval
)
774 user_siginfo_t collect64
; /* siginfo data to return to caller */
777 register struct proc
*p
;
782 for (p
= q
->p_children
.lh_first
; p
!= 0; p
= p
->p_sibling
.le_next
) {
783 switch(uap
->idtype
) {
784 case P_PID
: /* child with process ID equal to... */
785 if (p
->p_pid
!= (pid_t
)uap
->id
)
788 case P_PGID
: /* child with process group ID equal to... */
789 if (p
->p_pgid
!= (pid_t
)uap
->id
)
792 case P_ALL
: /* any child */
796 /* XXX This is racy because we don't get the lock!!!! */
799 * Wait collision; go to sleep and restart; used to maintain
800 * the single return for waited process guarantee.
802 if (p
->p_lflag
& P_LWAITING
) {
803 (void)tsleep(&p
->p_stat
, PWAIT
, "waitidcoll", 0);
806 p
->p_lflag
|= P_LWAITING
; /* mark busy */
811 * Types of processes we are interested in
813 * XXX Don't know what to do for WCONTINUED?!?
816 case SZOMB
: /* Exited */
817 if (!(uap
->options
& WEXITED
))
820 /* Collect "siginfo" information for caller */
821 collect64
.si_signo
= 0;
822 collect64
.si_code
= 0;
823 collect64
.si_errno
= 0;
824 collect64
.si_pid
= 0;
825 collect64
.si_uid
= 0;
826 collect64
.si_addr
= 0;
827 collect64
.si_status
= p
->p_xstat
;
828 collect64
.si_band
= 0;
830 if (IS_64BIT_PROCESS(p
)) {
831 error
= copyout((caddr_t
)&collect64
,
836 siginfo_64to32(&collect64
,&collect
);
837 error
= copyout((caddr_t
)&collect
,
841 /* information unavailable? */
843 p
->p_lflag
&= ~P_LWAITING
;
848 /* Prevent other process for waiting for this event? */
849 if (!(uap
->options
& WNOWAIT
)) {
851 if (!reap_child_process(q
, p
)) {
852 p
->p_lflag
&= ~P_LWAITING
;
859 case SSTOP
: /* Stopped */
861 * If we are not interested in stopped processes, then
864 if (!(uap
->options
& WSTOPPED
))
868 * If someone has already waited it, we lost a race
869 * to be the one to return status.
871 if ((p
->p_flag
& P_WAITED
) != 0)
875 * If this is not a traced process, and they haven't
876 * indicated an interest in untraced processes, then
879 if (!(p
->p_flag
& P_TRACED
) && !(uap
->options
& WUNTRACED
))
882 /* Collect "siginfo" information for caller */
883 collect64
.si_signo
= 0;
884 collect64
.si_code
= 0;
885 collect64
.si_errno
= 0;
886 collect64
.si_pid
= 0;
887 collect64
.si_uid
= 0;
888 collect64
.si_addr
= 0;
889 collect64
.si_status
= p
->p_xstat
;
890 collect64
.si_band
= 0;
892 if (IS_64BIT_PROCESS(p
)) {
893 error
= copyout((caddr_t
)&collect64
,
898 siginfo_64to32(&collect64
,&collect
);
899 error
= copyout((caddr_t
)&collect
,
903 /* information unavailable? */
905 p
->p_lflag
&= ~P_LWAITING
;
910 /* Prevent other process for waiting for this event? */
911 if (!(uap
->options
& WNOWAIT
)) {
912 p
->p_flag
|= P_WAITED
;
915 p
->p_lflag
&= ~P_LWAITING
;
919 default: /* All others */
920 /* ...meaning Continued */
921 if (!(uap
->options
& WCONTINUED
))
925 * If the flag isn't set, then this process has not
926 * been stopped and continued, or the status has
927 * already been reaped by another caller of waitid().
929 if ((p
->p_flag
& P_CONTINUED
) == 0)
932 /* Collect "siginfo" information for caller */
933 collect64
.si_signo
= 0;
934 collect64
.si_code
= 0;
935 collect64
.si_errno
= 0;
936 collect64
.si_pid
= 0;
937 collect64
.si_uid
= 0;
938 collect64
.si_addr
= 0;
939 collect64
.si_status
= p
->p_xstat
;
940 collect64
.si_band
= 0;
942 if (IS_64BIT_PROCESS(p
)) {
943 error
= copyout((caddr_t
)&collect64
,
948 siginfo_64to32(&collect64
,&collect
);
949 error
= copyout((caddr_t
)&collect
,
953 /* information unavailable? */
955 p
->p_lflag
&= ~P_LWAITING
;
960 /* Prevent other process for waiting for this event? */
961 if (!(uap
->options
& WNOWAIT
)) {
962 p
->p_flag
&= ~P_CONTINUED
;
965 p
->p_lflag
&= ~P_LWAITING
;
973 /* Not a process we are interested in; go on to next child */
974 p
->p_lflag
&= ~P_LWAITING
;
978 /* No child processes that could possibly satisfy the request? */
982 if (uap
->options
& WNOHANG
) {
987 if ((error
= tsleep0((caddr_t
)q
, PWAIT
| PCATCH
, "waitid", 0, waitidcontinue
)))
994 * make process 'parent' the new parent of process 'child'.
997 proc_reparent(struct proc
*child
, struct proc
*parent
)
1000 if (child
->p_pptr
== parent
)
1003 LIST_REMOVE(child
, p_sibling
);
1004 LIST_INSERT_HEAD(&parent
->p_children
, child
, p_sibling
);
1005 child
->p_pptr
= parent
;
1007 if (initproc
== parent
&& child
->p_stat
== SZOMB
)
1008 psignal(initproc
, SIGCHLD
);
1012 * Make the current process an "init" process, meaning
1013 * that it doesn't have a parent, and that it won't be
1014 * gunned down by kill(-1, 0).
1017 init_process(__unused
struct init_process_args
*args
)
1019 register struct proc
*p
= current_proc();
1021 AUDIT_MACH_SYSCALL_ENTER(AUE_INITPROCESS
);
1022 if (suser(kauth_cred_get(), &p
->p_acflag
)) {
1023 AUDIT_MACH_SYSCALL_EXIT(KERN_NO_ACCESS
);
1024 return(KERN_NO_ACCESS
);
1027 if (p
->p_pid
!= 1 && p
->p_pgid
!= p
->p_pid
)
1028 enterpgrp(p
, p
->p_pid
, 0);
1029 p
->p_flag
|= P_SYSTEM
;
1032 * Take us out of the sibling chain, and
1033 * out of our parent's child chain.
1035 LIST_REMOVE(p
, p_sibling
);
1036 p
->p_sibling
.le_prev
= NULL
;
1037 p
->p_sibling
.le_next
= NULL
;
1038 p
->p_pptr
= kernproc
;
1040 AUDIT_MACH_SYSCALL_EXIT(KERN_SUCCESS
);
1041 return(KERN_SUCCESS
);
1046 * Exit: deallocate address space and other resources, change proc state
1047 * to zombie, and unlink proc from allproc and parent's lists. Save exit
1048 * status and rusage for wait(). Check for child processes and orphan them.
1052 vfork_exit(struct proc
*p
, int rv
)
1054 thread_t self
= current_thread();
1056 struct task
*task
= p
->task
;
1060 exception_data_t code
[EXCEPTION_CODE_MAX
];
1063 * If a thread in this task has already
1064 * called exit(), then halt any others
1068 ut
= get_bsdthread_info(self
);
1071 while (p
->exit_thread
!= self
) {
1072 if (sig_try_locked(p
) <= 0) {
1073 if (get_threadtask(self
) != task
) {
1078 thread_terminate(self
);
1079 thread_funnel_set(kernel_flock
, FALSE
);
1080 thread_exception_return();
1083 sig_lock_to_exit(p
);
1086 if (p
->p_pid
== 1) {
1087 printf("pid 1 exited (signal %d, exit %d)",
1088 WTERMSIG(rv
), WEXITSTATUS(rv
));
1089 panic("init died\nState at Last Exception:\n\n%s", init_task_failure_data
);
1094 p
->p_flag
|= P_WEXIT
;
1095 p
->p_lflag
|= P_LPEXIT
;
1098 code
[0] = (exception_data_t
)0xFF000001; /* Set terminate code */
1099 code
[1] = (exception_data_t
)p
->p_pid
; /* Pass out the pid */
1100 /* Notify the perf server */
1101 (void)sys_perf_notify(p
->task
, (exception_data_t
)&code
, 2);
1104 * Remove proc from allproc queue and from pidhash chain.
1105 * Need to do this before we do anything that can block.
1106 * Not doing causes things like mount() find this on allproc
1107 * in partially cleaned state.
1109 LIST_REMOVE(p
, p_list
);
1110 LIST_INSERT_HEAD(&zombproc
, p
, p_list
); /* Place onto zombproc. */
1111 LIST_REMOVE(p
, p_hash
);
1113 * If parent is waiting for us to exit or exec,
1114 * P_PPWAIT is set; we will wakeup the parent below.
1116 p
->p_flag
&= ~(P_TRACED
| P_PPWAIT
);
1117 p
->p_sigignore
= ~0;
1121 untimeout(realitexpire
, (caddr_t
)p
->p_pid
);
1129 vproc_exit(struct proc
*p
)
1131 register struct proc
*q
, *nq
, *pp
;
1133 struct task
*task
= p
->task
;
1136 /* XXX Zombie allocation may fail, in which case stats get lost */
1137 MALLOC_ZONE(p
->p_ru
, struct rusage
*,
1138 sizeof (*p
->p_ru
), M_ZOMBIE
, M_WAITOK
);
1141 * Close open files and release open-file table.
1146 if (SESS_LEADER(p
)) {
1147 register struct session
*sp
= p
->p_session
;
1150 struct vnode
*ttyvp
;
1151 struct vfs_context context
;
1154 * Controlling process.
1155 * Signal foreground pgrp,
1156 * drain controlling terminal
1157 * and revoke access to controlling terminal.
1159 if (sp
->s_ttyp
->t_session
== sp
) {
1160 if (sp
->s_ttyp
->t_pgrp
)
1161 pgsignal(sp
->s_ttyp
->t_pgrp
, SIGHUP
, 1);
1162 (void) ttywait(sp
->s_ttyp
);
1164 * The tty could have been revoked
1167 context
.vc_proc
= p
;
1168 context
.vc_ucred
= p
->p_ucred
;
1170 VNOP_REVOKE(sp
->s_ttyvp
, REVOKEALL
, &context
);
1172 ttyvp
= sp
->s_ttyvp
;
1178 * s_ttyp is not zero'd; we use this to indicate
1179 * that the session once had a controlling terminal.
1180 * (for logging and informational purposes)
1183 sp
->s_leader
= NULL
;
1186 fixjobc(p
, p
->p_pgrp
, 0);
1187 p
->p_rlimit
[RLIMIT_FSIZE
].rlim_cur
= RLIM_INFINITY
;
1191 * release trace file
1193 p
->p_traceflag
= 0; /* don't trace the vnode_rele() */
1195 struct vnode
*tvp
= p
->p_tracep
;
1201 while (q
= p
->p_children
.lh_first
) {
1202 proc_reparent(q
, initproc
);
1204 * Traced processes are killed
1205 * since their existence means someone is messing up.
1207 if (q
->p_flag
& P_TRACED
) {
1208 q
->p_flag
&= ~P_TRACED
;
1209 if (q
->sigwait_thread
) {
1211 * The sigwait_thread could be stopped at a
1212 * breakpoint. Wake it up to kill.
1213 * Need to do this as it could be a thread which is not
1214 * the first thread in the task. So any attempts to kill
1215 * the process would result into a deadlock on q->sigwait.
1217 thread_resume((thread_t
)q
->sigwait_thread
);
1218 clear_wait(q
->sigwait_thread
, THREAD_INTERRUPTED
);
1219 threadsignal((thread_t
)q
->sigwait_thread
, SIGKILL
, 0);
1221 psignal(q
, SIGKILL
);
1226 * Save exit status and final rusage info, adding in child rusage
1227 * info and self times. If we were unable to allocate a zombie
1228 * structure, this information is lost.
1230 if (p
->p_ru
!= NULL
) {
1231 *p
->p_ru
= p
->p_stats
->p_ru
;
1232 timerclear(&p
->p_ru
->ru_utime
);
1233 timerclear(&p
->p_ru
->ru_stime
);
1237 task_basic_info_data_t tinfo
;
1238 task_thread_times_info_data_t ttimesinfo
;
1239 int task_info_stuff
, task_ttimes_stuff
;
1240 struct timeval ut
,st
;
1242 task_info_stuff
= TASK_BASIC_INFO_COUNT
;
1243 task_info(task
, TASK_BASIC_INFO
,
1244 &tinfo
, &task_info_stuff
);
1245 p
->p_ru
->ru_utime
.tv_sec
= tinfo
.user_time
.seconds
;
1246 p
->p_ru
->ru_utime
.tv_usec
= tinfo
.user_time
.microseconds
;
1247 p
->p_ru
->ru_stime
.tv_sec
= tinfo
.system_time
.seconds
;
1248 p
->p_ru
->ru_stime
.tv_usec
= tinfo
.system_time
.microseconds
;
1250 task_ttimes_stuff
= TASK_THREAD_TIMES_INFO_COUNT
;
1251 task_info(task
, TASK_THREAD_TIMES_INFO
,
1252 &ttimesinfo
, &task_ttimes_stuff
);
1254 ut
.tv_sec
= ttimesinfo
.user_time
.seconds
;
1255 ut
.tv_usec
= ttimesinfo
.user_time
.microseconds
;
1256 st
.tv_sec
= ttimesinfo
.system_time
.seconds
;
1257 st
.tv_usec
= ttimesinfo
.system_time
.microseconds
;
1258 timeradd(&ut
,&p
->p_ru
->ru_utime
,&p
->p_ru
->ru_utime
);
1259 timeradd(&st
,&p
->p_ru
->ru_stime
,&p
->p_ru
->ru_stime
);
1263 ruadd(p
->p_ru
, &p
->p_stats
->p_cru
);
1267 * Free up profiling buffers.
1270 struct uprof
*p0
= &p
->p_stats
->p_prof
, *p1
, *pn
;
1276 for (; p1
!= NULL
; p1
= pn
) {
1278 kfree(p1
, sizeof *p1
);
1283 * Other substructures are freed from wait().
1285 FREE_ZONE(p
->p_stats
, sizeof *p
->p_stats
, M_SUBPROC
);
1288 FREE_ZONE(p
->p_sigacts
, sizeof *p
->p_sigacts
, M_SUBPROC
);
1289 p
->p_sigacts
= NULL
;
1291 if (--p
->p_limit
->p_refcnt
== 0)
1292 FREE_ZONE(p
->p_limit
, sizeof *p
->p_limit
, M_SUBPROC
);
1296 * Finish up by terminating the task
1297 * and halt this thread (only if a
1298 * member of the task exiting).
1300 p
->task
= TASK_NULL
;
1303 * Notify parent that we're gone.
1306 if (pp
!= initproc
) {
1307 pp
->si_pid
= p
->p_pid
;
1308 pp
->si_status
= p
->p_xstat
;
1309 pp
->si_code
= CLD_EXITED
;
1310 pp
->si_uid
= p
->p_ucred
->cr_ruid
;
1312 /* mark as a zombie */
1315 psignal(p
->p_pptr
, SIGCHLD
);
1317 /* and now wakeup the parent */
1318 wakeup((caddr_t
)p
->p_pptr
);
1324 * LP64 support - long is 64 bits if we are dealing with a 64 bit user
1325 * process. We munge the kernel (32 bit) version of rusage into the
1328 __private_extern__
void
1329 munge_rusage(struct rusage
*a_rusage_p
, struct user_rusage
*a_user_rusage_p
)
1331 /* timeval changes size, so utime and stime need special handling */
1332 a_user_rusage_p
->ru_utime
.tv_sec
= a_rusage_p
->ru_utime
.tv_sec
;
1333 a_user_rusage_p
->ru_utime
.tv_usec
= a_rusage_p
->ru_utime
.tv_usec
;
1334 a_user_rusage_p
->ru_stime
.tv_sec
= a_rusage_p
->ru_stime
.tv_sec
;
1335 a_user_rusage_p
->ru_stime
.tv_usec
= a_rusage_p
->ru_stime
.tv_usec
;
1337 * everything else can be a direct assign, since there is no loss
1338 * of precision implied boing 32->64.
1340 a_user_rusage_p
->ru_maxrss
= a_rusage_p
->ru_maxrss
;
1341 a_user_rusage_p
->ru_ixrss
= a_rusage_p
->ru_ixrss
;
1342 a_user_rusage_p
->ru_idrss
= a_rusage_p
->ru_idrss
;
1343 a_user_rusage_p
->ru_isrss
= a_rusage_p
->ru_isrss
;
1344 a_user_rusage_p
->ru_minflt
= a_rusage_p
->ru_minflt
;
1345 a_user_rusage_p
->ru_majflt
= a_rusage_p
->ru_majflt
;
1346 a_user_rusage_p
->ru_nswap
= a_rusage_p
->ru_nswap
;
1347 a_user_rusage_p
->ru_inblock
= a_rusage_p
->ru_inblock
;
1348 a_user_rusage_p
->ru_oublock
= a_rusage_p
->ru_oublock
;
1349 a_user_rusage_p
->ru_msgsnd
= a_rusage_p
->ru_msgsnd
;
1350 a_user_rusage_p
->ru_msgrcv
= a_rusage_p
->ru_msgrcv
;
1351 a_user_rusage_p
->ru_nsignals
= a_rusage_p
->ru_nsignals
;
1352 a_user_rusage_p
->ru_nvcsw
= a_rusage_p
->ru_nvcsw
;
1353 a_user_rusage_p
->ru_nivcsw
= a_rusage_p
->ru_nivcsw
;