#include <machine/reg.h>
#include <machine/psl.h>
#include "compat_43.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/ioctl.h>
#include <sys/proc_internal.h>
#include <sys/proc.h>
#include <sys/kauth.h>
#include <sys/tty.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/kernel.h>
#include <sys/wait.h>
#include <sys/file_internal.h>
#include <sys/vnode_internal.h>
#include <sys/syslog.h>
#include <sys/malloc.h>
#include <sys/resourcevar.h>
#include <sys/ptrace.h>
#include <sys/user.h>
#include <sys/aio_kern.h>
#include <sys/sysproto.h>
#include <sys/signalvar.h>
#include <sys/kdebug.h>
#include <sys/filedesc.h>
#if SYSV_SHM
#include <sys/shm_internal.h>
#endif
#include <sys/acct.h>
#include <security/audit/audit.h>
#include <bsm/audit_kevents.h>
#include <mach/mach_types.h>
#include <kern/kern_types.h>
#include <kern/kalloc.h>
#include <kern/task.h>
#include <kern/thread.h>
#include <kern/thread_call.h>
#include <kern/sched_prim.h>
#include <kern/assert.h>
#if CONFIG_DTRACE
extern void (*dtrace_fasttrap_exit_ptr)(proc_t);
extern void (*dtrace_helpers_cleanup)(proc_t);
extern void dtrace_lazy_dofs_destroy(proc_t);
#include <sys/dtrace_ptss.h>
#endif
#if CONFIG_MACF
#include <security/mac.h>
#include <sys/syscall.h>
#endif
#include <mach/mach_types.h>
#include <mach/task.h>
#include <mach/thread_act.h>
#include <sys/sdt.h>
extern char init_task_failure_data[];
void proc_prepareexit(proc_t p, int rv);
void vfork_exit(proc_t p, int rv);
void vproc_exit(proc_t p);
__private_extern__ void munge_user64_rusage(struct rusage *a_rusage_p, struct user64_rusage *a_user_rusage_p);
__private_extern__ void munge_user32_rusage(struct rusage *a_rusage_p, struct user32_rusage *a_user_rusage_p);
static int reap_child_locked(proc_t parent, proc_t child, int deadparent, int locked, int droplock);
void *get_bsduthreadarg(thread_t);
void proc_exit(proc_t p);
int wait1continue(int result);
int waitidcontinue(int result);
int *get_bsduthreadrval(thread_t);
kern_return_t sys_perf_notify(thread_t thread, int pid);
kern_return_t abnormal_exit_notify(mach_exception_data_type_t code,
mach_exception_data_type_t subcode);
void workqueue_exit(struct proc *);
void delay(int);
static void
siginfo_user_to_user32(user_siginfo_t *in, user32_siginfo_t *out)
{
out->si_signo = in->si_signo;
out->si_errno = in->si_errno;
out->si_code = in->si_code;
out->si_pid = in->si_pid;
out->si_uid = in->si_uid;
out->si_status = in->si_status;
out->si_addr = CAST_DOWN_EXPLICIT(user32_addr_t,in->si_addr);
out->si_value.sival_ptr = CAST_DOWN_EXPLICIT(user32_addr_t,in->si_value.sival_ptr);
out->si_band = in->si_band;
}
static void
siginfo_user_to_user64(user_siginfo_t *in, user64_siginfo_t *out)
{
out->si_signo = in->si_signo;
out->si_errno = in->si_errno;
out->si_code = in->si_code;
out->si_pid = in->si_pid;
out->si_uid = in->si_uid;
out->si_status = in->si_status;
out->si_addr = in->si_addr;
out->si_value.sival_ptr = in->si_value.sival_ptr;
out->si_band = in->si_band;
}
void
exit(proc_t p, struct exit_args *uap, int *retval)
{
exit1(p, W_EXITCODE(uap->rval, 0), retval);
thread_exception_return();
while (TRUE)
thread_block(THREAD_CONTINUE_NULL);
}
int
exit1(proc_t p, int rv, int *retval)
{
thread_t self = current_thread();
struct task *task = p->task;
struct uthread *ut;
ut = get_bsdthread_info(self);
if (ut->uu_flag & UT_VFORK) {
vfork_exit(p, rv);
vfork_return(p , retval, p->p_pid);
unix_syscall_return(0);
}
AUDIT_ARG(exit, WEXITSTATUS(rv), 0);
AUDIT_SYSCALL_EXIT(SYS_exit, p, ut, 0);
DTRACE_PROC1(exit, int, CLD_EXITED);
proc_lock(p);
while (p->exit_thread != self) {
if (sig_try_locked(p) <= 0) {
if (get_threadtask(self) != task) {
proc_unlock(p);
return(0);
}
proc_unlock(p);
thread_terminate(self);
thread_exception_return();
}
sig_lock_to_exit(p);
}
if (p == initproc) {
proc_unlock(p);
printf("pid 1 exited (signal %d, exit %d)",
WTERMSIG(rv), WEXITSTATUS(rv));
panic("%s died\nState at Last Exception:\n\n%s",
(p->p_comm[0] != '\0' ?
p->p_comm :
"launchd"),
init_task_failure_data);
}
p->p_lflag |= P_LEXIT;
p->p_xstat = rv;
proc_unlock(p);
proc_prepareexit(p, rv);
task_terminate_internal(task);
return(0);
}
void
proc_prepareexit(proc_t p, int rv)
{
mach_exception_data_type_t code, subcode;
struct uthread *ut;
thread_t self = current_thread();
ut = get_bsdthread_info(self);
if (hassigprop(WTERMSIG(rv), SA_CORE)) {
if ((SIGSEGV == WTERMSIG(rv)) &&
(p->p_pptr->p_lflag & P_LNOATTACH)) {
goto skipcheck;
}
code = ((WTERMSIG(rv) & 0xff) << 24) |
((ut->uu_exception & 0x0f) << 20) |
((int)ut->uu_code & 0xfffff);
subcode = ut->uu_subcode;
(void) abnormal_exit_notify(code, subcode);
}
skipcheck:
(void)sys_perf_notify(self, p->p_pid);
proc_list_lock();
LIST_REMOVE(p, p_list);
LIST_INSERT_HEAD(&zombproc, p, p_list);
p->p_listflag |= P_LIST_EXITED;
proc_list_unlock();
#ifdef PGINPROF
vmsizmon();
#endif
proc_lock(p);
p->p_lflag &= ~(P_LTRACED | P_LPPWAIT);
p->p_sigignore = ~(sigcantmask);
ut->uu_siglist = 0;
proc_unlock(p);
}
void
proc_exit(proc_t p)
{
proc_t q;
proc_t pp;
struct task *task = p->task;
vnode_t tvp = NULLVP;
struct pgrp * pg;
struct session *sessp;
struct uthread * uth;
pid_t pid;
int exitval;
uth = (struct uthread *)get_bsdthread_info(current_thread());
proc_lock(p);
if( !(p->p_lflag & P_LEXIT)) {
p->p_lflag |= P_LEXIT;
proc_unlock(p);
proc_prepareexit(p, 0);
proc_lock(p);
}
p->p_lflag |= P_LPEXIT;
proc_unlock(p);
pid = p->p_pid;
exitval = p->p_xstat;
KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC, BSD_PROC_EXIT) | DBG_FUNC_START,
pid, exitval, 0, 0, 0);
#if CONFIG_DTRACE
dtrace_lazy_dofs_destroy(p);
if (p->p_dtrace_helpers != NULL) {
(*dtrace_helpers_cleanup)(p);
}
proc_lock(p);
if (p->p_dtrace_probes && dtrace_fasttrap_exit_ptr) {
(*dtrace_fasttrap_exit_ptr)(p);
}
proc_unlock(p);
#endif
MALLOC_ZONE(p->p_ru, struct rusage *,
sizeof (*p->p_ru), M_ZOMBIE, M_WAITOK);
proc_refdrain(p);
workqueue_exit(p);
_aio_exit( p );
fdfree(p);
if (uth->uu_lowpri_window) {
throttle_lowpri_io(FALSE);
}
#if SYSV_SHM
if (p->vm_shm)
shmexit(p);
#endif
#if SYSV_SEM
semexit(p);
#endif
#if PSYNCH
pth_proc_hashdelete(p);
#endif
sessp = proc_session(p);
if (SESS_LEADER(p, sessp)) {
if (sessp->s_ttyvp != NULLVP) {
struct vnode *ttyvp;
int ttyvid;
struct vfs_context context;
struct tty * tp;
tp = SESSION_TP(sessp);
if ((tp != TTY_NULL) && (tp->t_session == sessp)) {
tty_pgsignal(tp, SIGHUP, 1);
session_lock(sessp);
tp = SESSION_TP(sessp);
ttyvp = sessp->s_ttyvp;
ttyvid = sessp->s_ttyvid;
sessp->s_ttyvp = NULLVP;
sessp->s_ttyvid = 0;
sessp->s_ttyp = TTY_NULL;
sessp->s_ttypgrpid = NO_PID;
session_unlock(sessp);
if ((ttyvp != NULLVP) && (vnode_getwithvid(ttyvp, ttyvid) == 0)) {
if (tp != TTY_NULL) {
tty_lock(tp);
(void) ttywait(tp);
tty_unlock(tp);
}
context.vc_thread = proc_thread(p);
context.vc_ucred = kauth_cred_proc_ref(p);
VNOP_REVOKE(ttyvp, REVOKEALL, &context);
vnode_put(ttyvp);
kauth_cred_unref(&context.vc_ucred);
}
} else {
session_lock(sessp);
tp = SESSION_TP(sessp);
ttyvp = sessp->s_ttyvp;
sessp->s_ttyvp = NULLVP;
sessp->s_ttyvid = 0;
sessp->s_ttyp = TTY_NULL;
sessp->s_ttypgrpid = NO_PID;
session_unlock(sessp);
}
if (ttyvp)
vnode_rele(ttyvp);
}
session_lock(sessp);
sessp->s_leader = NULL;
session_unlock(sessp);
}
session_rele(sessp);
pg = proc_pgrp(p);
fixjobc(p, pg, 0);
pg_rele(pg);
p->p_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
(void)acct_process(p);
proc_list_lock();
if ((p->p_listflag & P_LIST_EXITCOUNT) == P_LIST_EXITCOUNT) {
p->p_listflag &= ~P_LIST_EXITCOUNT;
proc_shutdown_exitcount--;
if (proc_shutdown_exitcount == 0)
wakeup(&proc_shutdown_exitcount);
}
proc_childdrainstart(p);
while ((q = p->p_children.lh_first) != NULL) {
q->p_listflag |= P_LIST_DEADPARENT;
if (q->p_stat == SZOMB) {
if (p != q->p_pptr)
panic("parent child linkage broken");
while ((q->p_listflag & P_LIST_WAITING) == P_LIST_WAITING) {
msleep(&q->p_stat, proc_list_mlock, PWAIT, "waitcoll", 0);
}
q->p_listflag |= P_LIST_WAITING;
(void)reap_child_locked(p, q, 1, 1, 0);
} else {
proc_reparentlocked(q, initproc, 0, 1);
if (q->p_lflag & P_LTRACED) {
proc_list_unlock();
proc_lock(q);
q->p_lflag &= ~P_LTRACED;
if (q->sigwait_thread) {
proc_unlock(q);
thread_resume((thread_t)q->sigwait_thread);
clear_wait(q->sigwait_thread, THREAD_INTERRUPTED);
threadsignal((thread_t)q->sigwait_thread, SIGKILL, 0);
} else
proc_unlock(q);
psignal(q, SIGKILL);
proc_list_lock();
}
}
}
proc_childdrainend(p);
proc_list_unlock();
tvp = p->p_textvp;
p->p_textvp = NULL;
if (tvp != NULLVP) {
vnode_rele(tvp);
}
if (p->p_ru != NULL) {
*p->p_ru = p->p_stats->p_ru;
calcru(p, &p->p_ru->ru_utime, &p->p_ru->ru_stime, NULL);
ruadd(p->p_ru, &p->p_stats->p_cru);
}
{
struct uprof *p0 = &p->p_stats->p_prof, *p1, *pn;
p1 = p0->pr_next;
p0->pr_next = NULL;
p0->pr_scale = 0;
for (; p1 != NULL; p1 = pn) {
pn = p1->pr_next;
kfree(p1, sizeof *p1);
}
}
proc_spinlock(p);
if (thread_call_cancel(p->p_rcall))
p->p_ractive--;
while (p->p_ractive > 0) {
proc_spinunlock(p);
delay(1);
proc_spinlock(p);
}
proc_spinunlock(p);
thread_call_free(p->p_rcall);
p->p_rcall = NULL;
FREE_ZONE(p->p_stats, sizeof *p->p_stats, M_PSTATS);
p->p_stats = NULL;
FREE_ZONE(p->p_sigacts, sizeof *p->p_sigacts, M_SIGACTS);
p->p_sigacts = NULL;
proc_limitdrop(p, 1);
p->p_limit = NULL;
p->task = TASK_NULL;
set_bsdtask_info(task, NULL);
proc_knote(p, NOTE_EXIT);
uth->uu_flag |= UT_PROCEXIT;
pp = proc_parent(p);
if (pp->p_flag & P_NOCLDWAIT) {
#if 3839178
#else
if (p->p_ru != NULL) {
proc_lock(pp);
ruadd(&pp->p_stats->p_cru, p->p_ru);
proc_unlock(pp);
}
#endif
proc_list_lock();
p->p_listflag |= P_LIST_DEADPARENT;
proc_list_unlock();
}
if ((p->p_listflag & P_LIST_DEADPARENT) == 0) {
if (pp != initproc) {
proc_lock(pp);
pp->si_pid = p->p_pid;
pp->si_status = p->p_xstat;
pp->si_code = CLD_EXITED;
pp->si_uid = p->p_ucred->cr_ruid;
proc_unlock(pp);
}
KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC, BSD_PROC_EXIT) | DBG_FUNC_END,
pid, exitval, 0, 0, 0);
p->p_stat = SZOMB;
psignal(pp, SIGCHLD);
proc_list_lock();
wakeup((caddr_t)pp);
proc_list_unlock();
} else {
proc_list_lock();
KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC, BSD_PROC_EXIT) | DBG_FUNC_END,
pid, exitval, 0, 0, 0);
while ((p->p_listflag & P_LIST_WAITING) == P_LIST_WAITING) {
msleep(&p->p_stat, proc_list_mlock, PWAIT, "waitcoll", 0);
}
p->p_stat = SZOMB;
p->p_listflag |= P_LIST_WAITING;
(void)reap_child_locked(pp, p, 1, 1, 1);
}
if (uth->uu_lowpri_window) {
throttle_lowpri_io(FALSE);
}
proc_rele(pp);
}
static int
reap_child_locked(proc_t parent, proc_t child, int deadparent, int locked, int droplock)
{
proc_t trace_parent = PROC_NULL;
if (locked == 1)
proc_list_unlock();
if (child->p_oppid && (trace_parent = proc_find(child->p_oppid))
&& !((trace_parent == initproc) && (child->p_lflag & P_LIST_DEADPARENT))) {
proc_lock(child);
child->p_oppid = 0;
proc_unlock(child);
if (trace_parent != initproc) {
proc_lock(trace_parent);
trace_parent->si_pid = child->p_pid;
trace_parent->si_status = child->p_xstat;
trace_parent->si_code = CLD_CONTINUED;
trace_parent->si_uid = child->p_ucred->cr_ruid;
proc_unlock(trace_parent);
}
proc_reparentlocked(child, trace_parent, 1, 0);
psignal(trace_parent, SIGCHLD);
proc_list_lock();
wakeup((caddr_t)trace_parent);
child->p_listflag &= ~P_LIST_WAITING;
wakeup(&child->p_stat);
proc_list_unlock();
proc_rele(trace_parent);
if ((locked == 1) && (droplock == 0))
proc_list_lock();
return (0);
}
if (trace_parent != PROC_NULL) {
proc_rele(trace_parent);
}
proc_knote(child, NOTE_REAP);
proc_knote_drain(child);
child->p_xstat = 0;
if (child->p_ru) {
proc_lock(parent);
#if 3839178
if (!(parent->p_flag & P_NOCLDWAIT))
#endif
ruadd(&parent->p_stats->p_cru, child->p_ru);
proc_unlock(parent);
FREE_ZONE(child->p_ru, sizeof *child->p_ru, M_ZOMBIE);
child->p_ru = NULL;
} else {
printf("Warning : lost p_ru for %s\n", child->p_comm);
}
AUDIT_SESSION_PROCEXIT(child->p_ucred);
(void)chgproccnt(child->p_ucred->cr_ruid, -1);
#if CONFIG_LCTX
ALLLCTX_LOCK;
leavelctx(child);
ALLLCTX_UNLOCK;
#endif
if (IS_VALID_CRED(child->p_ucred)) {
kauth_cred_unref(&child->p_ucred);
}
leavepgrp(child);
proc_list_lock();
LIST_REMOVE(child, p_list);
parent->p_childrencnt--;
LIST_REMOVE(child, p_sibling);
if ((deadparent != 0) && (LIST_EMPTY(&parent->p_children)))
wakeup((caddr_t)parent);
child->p_listflag &= ~P_LIST_WAITING;
wakeup(&child->p_stat);
LIST_REMOVE(child, p_hash);
child->p_listflag &= ~P_LIST_INHASH;
proc_checkdeadrefs(child);
nprocs--;
proc_list_unlock();
#ifdef CONFIG_EMBEDDED
lck_mtx_destroy(&child->p_mlock, proc_lck_grp);
lck_mtx_destroy(&child->p_fdmlock, proc_lck_grp);
#if CONFIG_DTRACE
lck_mtx_destroy(&child->p_dtrace_sprlock, proc_lck_grp);
#endif
lck_spin_destroy(&child->p_slock, proc_lck_grp);
#else
lck_mtx_destroy(&child->p_mlock, proc_mlock_grp);
lck_mtx_destroy(&child->p_fdmlock, proc_fdmlock_grp);
#if CONFIG_DTRACE
lck_mtx_destroy(&child->p_dtrace_sprlock, proc_lck_grp);
#endif
lck_spin_destroy(&child->p_slock, proc_slock_grp);
#endif
workqueue_destroy_lock(child);
FREE_ZONE(child, sizeof *child, M_PROC);
if ((locked == 1) && (droplock == 0))
proc_list_lock();
return (1);
}
int
wait1continue(int result)
{
void *vt;
thread_t thread;
int *retval;
proc_t p;
if (result)
return(result);
p = current_proc();
thread = current_thread();
vt = get_bsduthreadarg(thread);
retval = get_bsduthreadrval(thread);
return(wait4(p, (struct wait4_args *)vt, retval));
}
int
wait4(proc_t q, struct wait4_args *uap, int32_t *retval)
{
__pthread_testcancel(1);
return(wait4_nocancel(q, (struct wait4_nocancel_args *)uap, retval));
}
int
wait4_nocancel(proc_t q, struct wait4_nocancel_args *uap, int32_t *retval)
{
int nfound;
int sibling_count;
proc_t p;
int status, error;
AUDIT_ARG(pid, uap->pid);
if (uap->pid == 0)
uap->pid = -q->p_pgrpid;
loop:
proc_list_lock();
loop1:
nfound = 0;
sibling_count = 0;
for (p = q->p_children.lh_first; p != 0; p = p->p_sibling.le_next) {
if ( p->p_sibling.le_next != 0 )
sibling_count++;
if (uap->pid != WAIT_ANY &&
p->p_pid != uap->pid &&
p->p_pgrpid != -(uap->pid))
continue;
nfound++;
if (p->p_listflag & P_LIST_WAITING) {
(void)msleep(&p->p_stat, proc_list_mlock, PWAIT, "waitcoll", 0);
goto loop1;
}
p->p_listflag |= P_LIST_WAITING;
if (p->p_stat == SZOMB) {
proc_list_unlock();
#if CONFIG_MACF
if ((error = mac_proc_check_wait(q, p)) != 0)
goto out;
#endif
retval[0] = p->p_pid;
if (uap->status) {
status = 0xffff & p->p_xstat;
error = copyout((caddr_t)&status,
uap->status,
sizeof(status));
if (error)
goto out;
}
if (uap->rusage) {
if (p->p_ru == NULL) {
error = ENOMEM;
} else {
if (IS_64BIT_PROCESS(q)) {
struct user64_rusage my_rusage;
munge_user64_rusage(p->p_ru, &my_rusage);
error = copyout((caddr_t)&my_rusage,
uap->rusage,
sizeof (my_rusage));
}
else {
struct user32_rusage my_rusage;
munge_user32_rusage(p->p_ru, &my_rusage);
error = copyout((caddr_t)&my_rusage,
uap->rusage,
sizeof (my_rusage));
}
}
if (error)
goto out;
}
if ( sibling_count == 0 ) {
int mask = sigmask(SIGCHLD);
uthread_t uth = (struct uthread *)get_bsdthread_info(current_thread());
if ( (uth->uu_sigmask & mask) != 0 ) {
proc_lock(q);
uth->uu_siglist &= ~mask;
proc_unlock(q);
}
}
(void)reap_child_locked(q, p, 0, 0, 0);
return (0);
}
if (p->p_stat == SSTOP && (p->p_lflag & P_LWAITED) == 0 &&
(p->p_lflag & P_LTRACED || uap->options & WUNTRACED)) {
proc_list_unlock();
#if CONFIG_MACF
if ((error = mac_proc_check_wait(q, p)) != 0)
goto out;
#endif
proc_lock(p);
p->p_lflag |= P_LWAITED;
proc_unlock(p);
retval[0] = p->p_pid;
if (uap->status) {
status = W_STOPCODE(p->p_xstat);
error = copyout((caddr_t)&status,
uap->status,
sizeof(status));
} else
error = 0;
goto out;
}
if ((uap->options & WCONTINUED) &&
(p->p_flag & P_CONTINUED)) {
proc_list_unlock();
#if CONFIG_MACF
if ((error = mac_proc_check_wait(q, p)) != 0)
goto out;
#endif
OSBitAndAtomic(~((uint32_t)P_CONTINUED), &p->p_flag);
retval[0] = p->p_pid;
if (uap->status) {
status = W_STOPCODE(SIGCONT);
error = copyout((caddr_t)&status,
uap->status,
sizeof(status));
} else
error = 0;
goto out;
}
p->p_listflag &= ~P_LIST_WAITING;
wakeup(&p->p_stat);
}
if (nfound == 0) {
proc_list_unlock();
return (ECHILD);
}
if (uap->options & WNOHANG) {
retval[0] = 0;
proc_list_unlock();
return (0);
}
if ((error = msleep0((caddr_t)q, proc_list_mlock, PWAIT | PCATCH | PDROP, "wait", 0, wait1continue)))
return (error);
goto loop;
out:
proc_list_lock();
p->p_listflag &= ~P_LIST_WAITING;
wakeup(&p->p_stat);
proc_list_unlock();
return (error);
}
int
waitidcontinue(int result)
{
void *vt;
thread_t thread;
int *retval;
if (result)
return(result);
thread = current_thread();
vt = get_bsduthreadarg(thread);
retval = get_bsduthreadrval(thread);
return(waitid(current_proc(), (struct waitid_args *)vt, retval));
}
int
waitid(proc_t q, struct waitid_args *uap, int32_t *retval)
{
__pthread_testcancel(1);
return(waitid_nocancel(q, (struct waitid_nocancel_args *)uap, retval));
}
int
waitid_nocancel(proc_t q, struct waitid_nocancel_args *uap, __unused int32_t *retval)
{
user_siginfo_t collect64;
int nfound;
proc_t p;
int error;
if (((uap->options & (WNOHANG|WNOWAIT|WCONTINUED|WUNTRACED|WSTOPPED|WEXITED)) != uap->options) ||
(uap->options == 0))
return (EINVAL);
switch(uap->idtype) {
case P_PID:
case P_PGID:
if (((int)uap->id) < 0)
return (EINVAL);
break;
case P_ALL:
break;
}
loop:
proc_list_lock();
loop1:
nfound = 0;
for (p = q->p_children.lh_first; p != 0; p = p->p_sibling.le_next) {
switch(uap->idtype) {
case P_PID:
if (p->p_pid != (pid_t)uap->id)
continue;
break;
case P_PGID:
if (p->p_pgrpid != (pid_t)uap->id)
continue;
break;
case P_ALL:
break;
}
if (p->p_listflag & P_LIST_WAITING) {
(void)msleep(&p->p_stat, proc_list_mlock, PWAIT, "waitidcoll", 0);
goto loop1;
}
p->p_listflag |= P_LIST_WAITING;
nfound++;
switch(p->p_stat) {
case SZOMB:
if (!(uap->options & WEXITED))
break;
proc_list_unlock();
collect64.si_signo = SIGCHLD;
collect64.si_code = 0;
collect64.si_errno = 0;
collect64.si_pid = 0;
collect64.si_uid = 0;
collect64.si_addr = 0;
collect64.si_status = WEXITSTATUS(p->p_xstat);
collect64.si_band = 0;
if (IS_64BIT_PROCESS(p)) {
user64_siginfo_t sinfo64;
siginfo_user_to_user64(&collect64, &sinfo64);
error = copyout((caddr_t)&sinfo64,
uap->infop,
sizeof(sinfo64));
} else {
user32_siginfo_t sinfo32;
siginfo_user_to_user32(&collect64, &sinfo32);
error = copyout((caddr_t)&sinfo32,
uap->infop,
sizeof(sinfo32));
}
if (error)
goto out;
if (!(uap->options & WNOWAIT)) {
(void)reap_child_locked(q, p, 0, 0, 0);
} else {
proc_list_lock();
p->p_listflag &= ~P_LIST_WAITING;
proc_list_unlock();
}
return (0);
case SSTOP:
if (!(uap->options & WSTOPPED))
break;
if ((p->p_lflag & P_LWAITED) != 0)
break;
proc_list_unlock();
collect64.si_signo = SIGCHLD;
collect64.si_code = 0;
collect64.si_errno = 0;
collect64.si_pid = 0;
collect64.si_uid = 0;
collect64.si_addr = 0;
proc_lock(p);
collect64.si_status = p->p_xstat;
proc_unlock(p);
collect64.si_band = 0;
if (IS_64BIT_PROCESS(p)) {
user64_siginfo_t sinfo64;
siginfo_user_to_user64(&collect64, &sinfo64);
error = copyout((caddr_t)&sinfo64,
uap->infop,
sizeof(sinfo64));
} else {
user32_siginfo_t sinfo32;
siginfo_user_to_user32(&collect64, &sinfo32);
error = copyout((caddr_t)&sinfo32,
uap->infop,
sizeof(sinfo32));
}
if (error)
goto out;
if (!(uap->options & WNOWAIT)) {
proc_lock(p);
p->p_lflag |= P_LWAITED;
proc_unlock(p);
}
error = 0;
goto out;
default:
if (!(uap->options & WCONTINUED))
break;
if ((p->p_flag & P_CONTINUED) == 0)
break;
proc_list_unlock();
proc_lock(p);
collect64.si_signo = SIGCHLD;
collect64.si_code = CLD_CONTINUED;
collect64.si_errno = 0;
collect64.si_pid = p->p_contproc;
collect64.si_uid = 0;
collect64.si_addr = 0;
collect64.si_status = p->p_xstat;
collect64.si_band = 0;
proc_unlock(p);
if (IS_64BIT_PROCESS(p)) {
user64_siginfo_t sinfo64;
siginfo_user_to_user64(&collect64, &sinfo64);
error = copyout((caddr_t)&sinfo64,
uap->infop,
sizeof(sinfo64));
} else {
user32_siginfo_t sinfo32;
siginfo_user_to_user32(&collect64, &sinfo32);
error = copyout((caddr_t)&sinfo32,
uap->infop,
sizeof(sinfo32));
}
if (error)
goto out;
if (!(uap->options & WNOWAIT)) {
OSBitAndAtomic(~((uint32_t)P_CONTINUED), &p->p_flag);
}
error = 0;
goto out;
}
p->p_listflag &= ~P_LIST_WAITING;
wakeup(&p->p_stat);
}
if (nfound == 0) {
proc_list_unlock();
return (ECHILD);
}
if (uap->options & WNOHANG) {
proc_list_unlock();
return (0);
}
if ((error = msleep0((caddr_t)q, proc_list_mlock, PWAIT | PCATCH | PDROP, "waitid", 0, waitidcontinue)))
return (error);
goto loop;
out:
proc_list_lock();
p->p_listflag &= ~P_LIST_WAITING;
wakeup(&p->p_stat);
proc_list_unlock();
return (error);
}
void
proc_reparentlocked(proc_t child, proc_t parent, int cansignal, int locked)
{
proc_t oldparent = PROC_NULL;
if (child->p_pptr == parent)
return;
if (locked == 0)
proc_list_lock();
oldparent = child->p_pptr;
#if __PROC_INTERNAL_DEBUG
if (oldparent == PROC_NULL)
panic("proc_reparent: process %p does not have a parent\n", child);
#endif
LIST_REMOVE(child, p_sibling);
#if __PROC_INTERNAL_DEBUG
if (oldparent->p_childrencnt == 0)
panic("process children count already 0\n");
#endif
oldparent->p_childrencnt--;
#if __PROC_INTERNAL_DEBUG1
if (oldparent->p_childrencnt < 0)
panic("process children count -ve\n");
#endif
LIST_INSERT_HEAD(&parent->p_children, child, p_sibling);
parent->p_childrencnt++;
child->p_pptr = parent;
child->p_ppid = parent->p_pid;
proc_list_unlock();
if ((cansignal != 0) && (initproc == parent) && (child->p_stat == SZOMB))
psignal(initproc, SIGCHLD);
if (locked == 1)
proc_list_lock();
}
void
vfork_exit(proc_t p, int rv)
{
vfork_exit_internal(p, rv, 0);
}
void
vfork_exit_internal(proc_t p, int rv, int forceexit)
{
thread_t self = current_thread();
#ifdef FIXME
struct task *task = p->task;
#endif
struct uthread *ut;
ut = get_bsdthread_info(self);
proc_lock(p);
if ((p->p_lflag & P_LPEXIT) == P_LPEXIT) {
proc_unlock(p);
return;
}
p->p_lflag |= (P_LEXIT | P_LPEXIT);
proc_unlock(p);
if (forceexit == 0) {
(void)sys_perf_notify(self, p->p_pid);
}
proc_list_lock();
LIST_REMOVE(p, p_list);
LIST_INSERT_HEAD(&zombproc, p, p_list);
p->p_listflag |= P_LIST_EXITED;
proc_list_unlock();
proc_lock(p);
p->p_xstat = rv;
p->p_lflag &= ~(P_LTRACED | P_LPPWAIT);
p->p_sigignore = ~0;
proc_unlock(p);
proc_spinlock(p);
if (thread_call_cancel(p->p_rcall))
p->p_ractive--;
while (p->p_ractive > 0) {
proc_spinunlock(p);
delay(1);
proc_spinlock(p);
}
proc_spinunlock(p);
thread_call_free(p->p_rcall);
p->p_rcall = NULL;
ut->uu_siglist = 0;
vproc_exit(p);
}
void
vproc_exit(proc_t p)
{
proc_t q;
proc_t pp;
vnode_t tvp;
#ifdef FIXME
struct task *task = p->task;
#endif
struct pgrp * pg;
struct session *sessp;
MALLOC_ZONE(p->p_ru, struct rusage *,
sizeof (*p->p_ru), M_ZOMBIE, M_WAITOK);
proc_refdrain(p);
fdfree(p);
sessp = proc_session(p);
if (SESS_LEADER(p, sessp)) {
if (sessp->s_ttyvp != NULLVP) {
struct vnode *ttyvp;
int ttyvid;
struct vfs_context context;
struct tty * tp;
tp = SESSION_TP(sessp);
if ((tp != TTY_NULL) && (tp->t_session == sessp)) {
tty_pgsignal(tp, SIGHUP, 1);
tty_lock(tp);
(void) ttywait(tp);
tty_unlock(tp);
session_lock(sessp);
tp = SESSION_TP(sessp);
ttyvp = sessp->s_ttyvp;
ttyvid = sessp->s_ttyvid;
sessp->s_ttyvp = NULL;
sessp->s_ttyvid = 0;
sessp->s_ttyp = TTY_NULL;
sessp->s_ttypgrpid = NO_PID;
session_unlock(sessp);
if ((ttyvp != NULLVP) && (vnode_getwithvid(ttyvp, ttyvid) == 0)) {
context.vc_thread = proc_thread(p);
context.vc_ucred = kauth_cred_proc_ref(p);
VNOP_REVOKE(ttyvp, REVOKEALL, &context);
vnode_put(ttyvp);
kauth_cred_unref(&context.vc_ucred);
}
} else {
session_lock(sessp);
ttyvp = sessp->s_ttyvp;
sessp->s_ttyvp = NULL;
sessp->s_ttyvid = 0;
sessp->s_ttyp = TTY_NULL;
sessp->s_ttypgrpid = NO_PID;
session_unlock(sessp);
}
if (ttyvp)
vnode_rele(ttyvp);
}
session_lock(sessp);
sessp->s_leader = NULL;
session_unlock(sessp);
}
session_rele(sessp);
pg = proc_pgrp(p);
fixjobc(p, pg, 0);
pg_rele(pg);
p->p_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
proc_list_lock();
proc_childdrainstart(p);
while ((q = p->p_children.lh_first) != NULL) {
q->p_listflag |= P_LIST_DEADPARENT;
if (q->p_stat == SZOMB) {
if (p != q->p_pptr)
panic("parent child linkage broken");
while ((q->p_listflag & P_LIST_WAITING) == P_LIST_WAITING) {
msleep(&q->p_stat, proc_list_mlock, PWAIT, "waitcoll", 0);
}
q->p_listflag |= P_LIST_WAITING;
(void)reap_child_locked(p, q, 1, 1, 0);
} else {
proc_reparentlocked(q, initproc, 0, 1);
if (q->p_lflag & P_LTRACED) {
proc_list_unlock();
proc_lock(q);
q->p_lflag &= ~P_LTRACED;
if (q->sigwait_thread) {
proc_unlock(q);
thread_resume((thread_t)q->sigwait_thread);
clear_wait(q->sigwait_thread, THREAD_INTERRUPTED);
threadsignal((thread_t)q->sigwait_thread, SIGKILL, 0);
} else
proc_unlock(q);
psignal(q, SIGKILL);
proc_list_lock();
}
}
}
proc_childdrainend(p);
proc_list_unlock();
tvp = p->p_textvp;
p->p_textvp = NULL;
if (tvp != NULLVP) {
vnode_rele(tvp);
}
if (p->p_ru != NULL) {
*p->p_ru = p->p_stats->p_ru;
timerclear(&p->p_ru->ru_utime);
timerclear(&p->p_ru->ru_stime);
#ifdef FIXME
if (task) {
task_basic_info_data_t tinfo;
task_thread_times_info_data_t ttimesinfo;
int task_info_stuff, task_ttimes_stuff;
struct timeval ut,st;
task_info_stuff = TASK_BASIC_INFO_COUNT;
task_info(task, TASK_BASIC_INFO,
&tinfo, &task_info_stuff);
p->p_ru->ru_utime.tv_sec = tinfo.user_time.seconds;
p->p_ru->ru_utime.tv_usec = tinfo.user_time.microseconds;
p->p_ru->ru_stime.tv_sec = tinfo.system_time.seconds;
p->p_ru->ru_stime.tv_usec = tinfo.system_time.microseconds;
task_ttimes_stuff = TASK_THREAD_TIMES_INFO_COUNT;
task_info(task, TASK_THREAD_TIMES_INFO,
&ttimesinfo, &task_ttimes_stuff);
ut.tv_sec = ttimesinfo.user_time.seconds;
ut.tv_usec = ttimesinfo.user_time.microseconds;
st.tv_sec = ttimesinfo.system_time.seconds;
st.tv_usec = ttimesinfo.system_time.microseconds;
timeradd(&ut,&p->p_ru->ru_utime,&p->p_ru->ru_utime);
timeradd(&st,&p->p_ru->ru_stime,&p->p_ru->ru_stime);
}
#endif
ruadd(p->p_ru, &p->p_stats->p_cru);
}
{
struct uprof *p0 = &p->p_stats->p_prof, *p1, *pn;
p1 = p0->pr_next;
p0->pr_next = NULL;
p0->pr_scale = 0;
for (; p1 != NULL; p1 = pn) {
pn = p1->pr_next;
kfree(p1, sizeof *p1);
}
}
FREE_ZONE(p->p_stats, sizeof *p->p_stats, M_PSTATS);
p->p_stats = NULL;
FREE_ZONE(p->p_sigacts, sizeof *p->p_sigacts, M_SIGACTS);
p->p_sigacts = NULL;
proc_limitdrop(p, 1);
p->p_limit = NULL;
p->task = TASK_NULL;
pp = proc_parent(p);
if ((p->p_listflag & P_LIST_DEADPARENT) == 0) {
if (pp != initproc) {
proc_lock(pp);
pp->si_pid = p->p_pid;
pp->si_status = p->p_xstat;
pp->si_code = CLD_EXITED;
pp->si_uid = p->p_ucred->cr_ruid;
proc_unlock(pp);
}
p->p_stat = SZOMB;
psignal(pp, SIGCHLD);
proc_list_lock();
wakeup((caddr_t)pp);
proc_list_unlock();
} else {
proc_list_lock();
while ((p->p_listflag & P_LIST_WAITING) == P_LIST_WAITING) {
msleep(&p->p_stat, proc_list_mlock, PWAIT, "waitcoll", 0);
}
p->p_stat = SZOMB;
p->p_listflag |= P_LIST_WAITING;
(void)reap_child_locked(pp, p, 0, 1, 1);
}
proc_rele(pp);
}
__private_extern__ void
munge_user64_rusage(struct rusage *a_rusage_p, struct user64_rusage *a_user_rusage_p)
{
a_user_rusage_p->ru_utime.tv_sec = a_rusage_p->ru_utime.tv_sec;
a_user_rusage_p->ru_utime.tv_usec = a_rusage_p->ru_utime.tv_usec;
a_user_rusage_p->ru_stime.tv_sec = a_rusage_p->ru_stime.tv_sec;
a_user_rusage_p->ru_stime.tv_usec = a_rusage_p->ru_stime.tv_usec;
a_user_rusage_p->ru_maxrss = a_rusage_p->ru_maxrss;
a_user_rusage_p->ru_ixrss = a_rusage_p->ru_ixrss;
a_user_rusage_p->ru_idrss = a_rusage_p->ru_idrss;
a_user_rusage_p->ru_isrss = a_rusage_p->ru_isrss;
a_user_rusage_p->ru_minflt = a_rusage_p->ru_minflt;
a_user_rusage_p->ru_majflt = a_rusage_p->ru_majflt;
a_user_rusage_p->ru_nswap = a_rusage_p->ru_nswap;
a_user_rusage_p->ru_inblock = a_rusage_p->ru_inblock;
a_user_rusage_p->ru_oublock = a_rusage_p->ru_oublock;
a_user_rusage_p->ru_msgsnd = a_rusage_p->ru_msgsnd;
a_user_rusage_p->ru_msgrcv = a_rusage_p->ru_msgrcv;
a_user_rusage_p->ru_nsignals = a_rusage_p->ru_nsignals;
a_user_rusage_p->ru_nvcsw = a_rusage_p->ru_nvcsw;
a_user_rusage_p->ru_nivcsw = a_rusage_p->ru_nivcsw;
}
__private_extern__ void
munge_user32_rusage(struct rusage *a_rusage_p, struct user32_rusage *a_user_rusage_p)
{
a_user_rusage_p->ru_utime.tv_sec = a_rusage_p->ru_utime.tv_sec;
a_user_rusage_p->ru_utime.tv_usec = a_rusage_p->ru_utime.tv_usec;
a_user_rusage_p->ru_stime.tv_sec = a_rusage_p->ru_stime.tv_sec;
a_user_rusage_p->ru_stime.tv_usec = a_rusage_p->ru_stime.tv_usec;
a_user_rusage_p->ru_maxrss = a_rusage_p->ru_maxrss;
a_user_rusage_p->ru_ixrss = a_rusage_p->ru_ixrss;
a_user_rusage_p->ru_idrss = a_rusage_p->ru_idrss;
a_user_rusage_p->ru_isrss = a_rusage_p->ru_isrss;
a_user_rusage_p->ru_minflt = a_rusage_p->ru_minflt;
a_user_rusage_p->ru_majflt = a_rusage_p->ru_majflt;
a_user_rusage_p->ru_nswap = a_rusage_p->ru_nswap;
a_user_rusage_p->ru_inblock = a_rusage_p->ru_inblock;
a_user_rusage_p->ru_oublock = a_rusage_p->ru_oublock;
a_user_rusage_p->ru_msgsnd = a_rusage_p->ru_msgsnd;
a_user_rusage_p->ru_msgrcv = a_rusage_p->ru_msgrcv;
a_user_rusage_p->ru_nsignals = a_rusage_p->ru_nsignals;
a_user_rusage_p->ru_nvcsw = a_rusage_p->ru_nvcsw;
a_user_rusage_p->ru_nivcsw = a_rusage_p->ru_nivcsw;
}