#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.h>
#include <sys/tty.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/kernel.h>
#include <sys/buf.h>
#include <sys/wait.h>
#include <sys/file.h>
#include <sys/vnode.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 <bsm/audit_kernel.h>
#include <bsm/audit_kevents.h>
#include <mach/mach_types.h>
#include <kern/thread.h>
#include <kern/thread_act.h>
#include <kern/sched_prim.h>
#include <kern/assert.h>
#if KTRACE
#include <sys/ktrace.h>
#include <sys/ubc.h>
#endif
extern char init_task_failure_data[];
int exit1 __P((struct proc *, int, int *));
void proc_prepareexit(struct proc *p);
int vfork_exit(struct proc *p, int rv);
void vproc_exit(struct proc *p);
struct exit_args {
int rval;
};
void
exit(p, uap, retval)
struct proc *p;
struct exit_args *uap;
int *retval;
{
exit1(p, W_EXITCODE(uap->rval, 0), retval);
thread_funnel_set(kernel_flock, FALSE);
thread_exception_return();
while (TRUE)
thread_block(THREAD_CONTINUE_NULL);
}
int
exit1(p, rv, retval)
register struct proc *p;
int rv;
int * retval;
{
register struct proc *q, *nq;
thread_act_t self = current_act();
struct task *task = p->task;
register int i,s;
struct uthread *ut;
ut = get_bsdthread_info(self);
if (ut->uu_flag & P_VFORK) {
if (!vfork_exit(p, rv)) {
vfork_return(self, p->p_pptr, p , retval);
unix_syscall_return(0);
}
return(EINVAL);
}
AUDIT_SYSCALL_EXIT(0, p, ut);
signal_lock(p);
while (p->exit_thread != self) {
if (sig_try_locked(p) <= 0) {
if (get_threadtask(self) != task) {
signal_unlock(p);
return(0);
}
signal_unlock(p);
thread_terminate(self);
thread_funnel_set(kernel_flock, FALSE);
thread_exception_return();
}
sig_lock_to_exit(p);
}
signal_unlock(p);
if (p->p_pid == 1) {
printf("pid 1 exited (signal %d, exit %d)",
WTERMSIG(rv), WEXITSTATUS(rv));
panic("init died\nState at Last Exception:\n\n%s",
init_task_failure_data);
}
s = splsched();
p->p_flag |= P_WEXIT;
splx(s);
proc_prepareexit(p);
p->p_xstat = rv;
task_terminate_internal(task);
return(0);
}
void
proc_prepareexit(struct proc *p)
{
int s;
struct uthread *ut;
exception_data_t code[EXCEPTION_CODE_MAX];
thread_act_t self = current_act();
code[0] = 0xFF000001;
code[1] = p->p_pid;
(void)sys_perf_notify(p->task, &code, 2);
LIST_REMOVE(p, p_list);
LIST_INSERT_HEAD(&zombproc, p, p_list);
LIST_REMOVE(p, p_hash);
#ifdef PGINPROF
vmsizmon();
#endif
p->p_flag &= ~(P_TRACED | P_PPWAIT);
p->p_sigignore = ~0;
p->p_siglist = 0;
ut = get_bsdthread_info(self);
ut->uu_siglist = 0;
untimeout(realitexpire, (caddr_t)p->p_pid);
}
void
proc_exit(struct proc *p)
{
register struct proc *q, *nq, *pp;
struct task *task = p->task;
register int i,s;
boolean_t funnel_state;
funnel_state = thread_funnel_set(kernel_flock, TRUE);
if( !(p->p_flag & P_WEXIT)) {
s = splsched();
p->p_flag |= P_WEXIT;
splx(s);
proc_prepareexit(p);
}
MALLOC_ZONE(p->p_ru, struct rusage *,
sizeof (*p->p_ru), M_ZOMBIE, M_WAITOK);
_aio_exit( p );
fdfree(p);
if (p->vm_shm)
shmexit(p);
semexit(p);
if (SESS_LEADER(p)) {
register struct session *sp = p->p_session;
if (sp->s_ttyvp) {
struct vnode *ttyvp;
if (sp->s_ttyp->t_session == sp) {
if (sp->s_ttyp->t_pgrp)
pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1);
(void) ttywait(sp->s_ttyp);
if (sp->s_ttyvp)
VOP_REVOKE(sp->s_ttyvp, REVOKEALL);
}
ttyvp = sp->s_ttyvp;
sp->s_ttyvp = NULL;
if (ttyvp)
vrele(ttyvp);
}
sp->s_leader = NULL;
}
fixjobc(p, p->p_pgrp, 0);
p->p_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
(void)acct_process(p);
#if KTRACE
p->p_traceflag = 0;
if (p->p_tracep) {
struct vnode *tvp = p->p_tracep;
p->p_tracep = NULL;
if (UBCINFOEXISTS(tvp))
ubc_rele(tvp);
vrele(tvp);
}
#endif
q = p->p_children.lh_first;
if (q)
wakeup((caddr_t) initproc);
for (; q != 0; q = nq) {
nq = q->p_sibling.le_next;
proc_reparent(q, initproc);
if (q->p_flag & P_TRACED) {
q->p_flag &= ~P_TRACED;
if (q->sigwait_thread) {
thread_resume((thread_act_t)q->sigwait_thread);
clear_wait(q->sigwait_thread, THREAD_INTERRUPTED);
threadsignal((thread_act_t)q->sigwait_thread, SIGKILL, 0);
}
psignal(q, SIGKILL);
}
}
*p->p_ru = p->p_stats->p_ru;
timerclear(&p->p_ru->ru_utime);
timerclear(&p->p_ru->ru_stime);
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);
}
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((vm_offset_t)p1, sizeof *p1);
}
}
FREE_ZONE(p->p_stats, sizeof *p->p_stats, M_SUBPROC);
p->p_stats = NULL;
FREE_ZONE(p->p_sigacts, sizeof *p->p_sigacts, M_SUBPROC);
p->p_sigacts = NULL;
if (--p->p_limit->p_refcnt == 0)
FREE_ZONE(p->p_limit, sizeof *p->p_limit, M_SUBPROC);
p->p_limit = NULL;
audit_proc_free(p);
p->task = TASK_NULL;
set_bsdtask_info(task, NULL);
KNOTE(&p->p_klist, NOTE_EXIT);
if (p->p_pptr->p_flag & P_NOCLDWAIT) {
struct proc * pp = p->p_pptr;
ruadd(&p->p_pptr->p_stats->p_cru, p->p_ru);
proc_reparent(p, initproc);
if (LIST_EMPTY(&pp->p_children))
wakeup((caddr_t)pp);
}
pp = p->p_pptr;
if (pp != initproc) {
pp->si_pid = p->p_pid;
pp->si_status = p->p_xstat;
pp->si_code = CLD_EXITED;
pp->si_uid = p->p_cred->p_ruid;
}
psignal(pp, SIGCHLD);
p->p_stat = SZOMB;
wakeup((caddr_t)p->p_pptr);
(void) thread_funnel_set(kernel_flock, funnel_state);
}
struct wait4_args {
int pid;
int *status;
int options;
struct rusage *rusage;
};
#if COMPAT_43
int
owait(p, uap, retval)
struct proc *p;
void *uap;
int *retval;
{
struct wait4_args *a;
a = (struct wait4_args *)get_bsduthreadarg(current_act());
a->options = 0;
a->rusage = NULL;
a->pid = WAIT_ANY;
a->status = NULL;
return (wait1(p, a, retval, 1));
}
int
wait4(p, uap, retval)
struct proc *p;
struct wait4_args *uap;
int *retval;
{
return (wait1(p, uap, retval, 0));
}
struct owait3_args {
int *status;
int options;
struct rusage *rusage;
};
int
owait3(p, uap, retval)
struct proc *p;
struct owait3_args *uap;
int *retval;
{
struct wait4_args *a;
a = (struct wait4_args *)get_bsduthreadarg(current_act());
a->rusage = uap->rusage;
a->options = uap->options;
a->status = uap->status;
a->pid = WAIT_ANY;
return (wait1(p, a, retval, 1));
}
#else
#define wait1 wait4
#endif
int
wait1continue(result)
{
void *vt;
thread_act_t thread;
int *retval;
struct proc *p;
if (result)
return(result);
p = current_proc();
thread = current_act();
vt = (void *)get_bsduthreadarg(thread);
retval = (int *)get_bsduthreadrval(thread);
return(wait1((struct proc *)p, (struct wait4_args *)vt, retval, 0));
}
int
wait1(q, uap, retval, compat)
register struct proc *q;
register struct wait4_args *uap;
register_t *retval;
#if COMPAT_43
int compat;
#endif
{
register int nfound;
register struct proc *p, *t;
int status, error;
struct vnode *tvp;
retry:
if (uap->pid == 0)
uap->pid = -q->p_pgid;
loop:
nfound = 0;
for (p = q->p_children.lh_first; p != 0; p = p->p_sibling.le_next) {
if (uap->pid != WAIT_ANY &&
p->p_pid != uap->pid &&
p->p_pgid != -(uap->pid))
continue;
nfound++;
if (p->p_flag & P_WAITING) {
(void)tsleep(&p->p_stat, PWAIT, "waitcoll", 0);
goto loop;
}
p->p_flag |= P_WAITING;
if (p->p_stat == SZOMB) {
retval[0] = p->p_pid;
#if COMPAT_43
if (compat)
retval[1] = p->p_xstat;
else
#endif
if (uap->status) {
status = p->p_xstat;
if (error = copyout((caddr_t)&status,
(caddr_t)uap->status,
sizeof(status))) {
p->p_flag &= ~P_WAITING;
wakeup(&p->p_stat);
return (error);
}
}
if (uap->rusage &&
(error = copyout((caddr_t)p->p_ru,
(caddr_t)uap->rusage,
sizeof (struct rusage)))) {
p->p_flag &= ~P_WAITING;
wakeup(&p->p_stat);
return (error);
}
if (p->p_oppid && (t = pfind(p->p_oppid))) {
p->p_oppid = 0;
proc_reparent(p, t);
if (t != initproc) {
t->si_pid = p->p_pid;
t->si_status = p->p_xstat;
t->si_code = CLD_CONTINUED;
t->si_uid = p->p_cred->p_ruid;
}
psignal(t, SIGCHLD);
wakeup((caddr_t)t);
p->p_flag &= ~P_WAITING;
wakeup(&p->p_stat);
return (0);
}
p->p_xstat = 0;
if (p->p_ru) {
ruadd(&q->p_stats->p_cru, p->p_ru);
FREE_ZONE(p->p_ru, sizeof *p->p_ru, M_ZOMBIE);
p->p_ru = NULL;
} else {
printf("Warning : lost p_ru for %s\n", p->p_comm);
}
(void)chgproccnt(p->p_cred->p_ruid, -1);
if (--p->p_cred->p_refcnt == 0) {
struct ucred *ucr = p->p_ucred;
struct pcred *pcr;
if (ucr != NOCRED) {
p->p_ucred = NOCRED;
crfree(ucr);
}
pcr = p->p_cred;
p->p_cred = NULL;
FREE_ZONE(pcr, sizeof *pcr, M_SUBPROC);
}
tvp = p->p_textvp;
p->p_textvp = NULL;
if (tvp)
vrele(tvp);
leavepgrp(p);
LIST_REMOVE(p, p_list);
LIST_REMOVE(p, p_sibling);
p->p_flag &= ~P_WAITING;
FREE_ZONE(p, sizeof *p, M_PROC);
nprocs--;
wakeup(&p->p_stat);
return (0);
}
if (p->p_stat == SSTOP && (p->p_flag & P_WAITED) == 0 &&
(p->p_flag & P_TRACED || uap->options & WUNTRACED)) {
p->p_flag |= P_WAITED;
retval[0] = p->p_pid;
#if COMPAT_43
if (compat) {
retval[1] = W_STOPCODE(p->p_xstat);
error = 0;
} else
#endif
if (uap->status) {
status = W_STOPCODE(p->p_xstat);
error = copyout((caddr_t)&status,
(caddr_t)uap->status,
sizeof(status));
} else
error = 0;
p->p_flag &= ~P_WAITING;
wakeup(&p->p_stat);
return (error);
}
p->p_flag &= ~P_WAITING;
wakeup(&p->p_stat);
}
if (nfound == 0)
return (ECHILD);
if (uap->options & WNOHANG) {
retval[0] = 0;
return (0);
}
if (error = tsleep0((caddr_t)q, PWAIT | PCATCH, "wait", 0, wait1continue))
return (error);
goto loop;
}
void
proc_reparent(child, parent)
register struct proc *child;
register struct proc *parent;
{
if (child->p_pptr == parent)
return;
LIST_REMOVE(child, p_sibling);
LIST_INSERT_HEAD(&parent->p_children, child, p_sibling);
child->p_pptr = parent;
}
kern_return_t
init_process(void)
{
register struct proc *p = current_proc();
AUDIT_MACH_SYSCALL_ENTER(AUE_INITPROCESS);
if (suser(p->p_ucred, &p->p_acflag)) {
AUDIT_MACH_SYSCALL_EXIT(KERN_NO_ACCESS);
return(KERN_NO_ACCESS);
}
if (p->p_pid != 1 && p->p_pgid != p->p_pid)
enterpgrp(p, p->p_pid, 0);
p->p_flag |= P_SYSTEM;
LIST_REMOVE(p, p_sibling);
p->p_sibling.le_prev = NULL;
p->p_sibling.le_next = NULL;
p->p_pptr = kernproc;
AUDIT_MACH_SYSCALL_EXIT(KERN_SUCCESS);
return(KERN_SUCCESS);
}
void
process_terminate_self(void)
{
struct proc *p = current_proc();
if (p != NULL) {
exit1(p, W_EXITCODE(0, SIGKILL), (int *)NULL);
}
}
int
vfork_exit(p, rv)
struct proc *p;
int rv;
{
register struct proc *q, *nq;
thread_act_t self = current_act();
struct task *task = p->task;
register int i,s;
struct uthread *ut;
exception_data_t code[EXCEPTION_CODE_MAX];
ut = get_bsdthread_info(self);
if (p->exit_thread) {
return(1);
}
p->exit_thread = self;
s = splsched();
p->p_flag |= P_WEXIT;
splx(s);
code[0] = 0xFF000001;
code[1] = p->p_pid;
(void)sys_perf_notify(p->task, &code, 2);
LIST_REMOVE(p, p_list);
LIST_INSERT_HEAD(&zombproc, p, p_list);
LIST_REMOVE(p, p_hash);
p->p_flag &= ~(P_TRACED | P_PPWAIT);
p->p_sigignore = ~0;
p->p_siglist = 0;
ut->uu_siglist = 0;
untimeout(realitexpire, (caddr_t)p->p_pid);
p->p_xstat = rv;
vproc_exit(p);
return(0);
}
void
vproc_exit(struct proc *p)
{
register struct proc *q, *nq, *pp;
struct task *task = p->task;
register int i,s;
boolean_t funnel_state;
MALLOC_ZONE(p->p_ru, struct rusage *,
sizeof (*p->p_ru), M_ZOMBIE, M_WAITOK);
fdfree(p);
if (SESS_LEADER(p)) {
register struct session *sp = p->p_session;
if (sp->s_ttyvp) {
struct vnode *ttyvp;
if (sp->s_ttyp->t_session == sp) {
if (sp->s_ttyp->t_pgrp)
pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1);
(void) ttywait(sp->s_ttyp);
if (sp->s_ttyvp)
VOP_REVOKE(sp->s_ttyvp, REVOKEALL);
}
ttyvp = sp->s_ttyvp;
sp->s_ttyvp = NULL;
if (ttyvp)
vrele(ttyvp);
}
sp->s_leader = NULL;
}
fixjobc(p, p->p_pgrp, 0);
p->p_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
#if KTRACE
p->p_traceflag = 0;
if (p->p_tracep) {
struct vnode *tvp = p->p_tracep;
p->p_tracep = NULL;
if (UBCINFOEXISTS(tvp))
ubc_rele(tvp);
vrele(tvp);
}
#endif
q = p->p_children.lh_first;
if (q)
wakeup((caddr_t) initproc);
for (; q != 0; q = nq) {
nq = q->p_sibling.le_next;
proc_reparent(q, initproc);
if (q->p_flag & P_TRACED) {
q->p_flag &= ~P_TRACED;
if (q->sigwait_thread) {
thread_resume((thread_act_t)q->sigwait_thread);
clear_wait(q->sigwait_thread, THREAD_INTERRUPTED);
threadsignal((thread_act_t)q->sigwait_thread, SIGKILL, 0);
}
psignal(q, SIGKILL);
}
}
*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((vm_offset_t)p1, sizeof *p1);
}
}
FREE_ZONE(p->p_stats, sizeof *p->p_stats, M_SUBPROC);
p->p_stats = NULL;
FREE_ZONE(p->p_sigacts, sizeof *p->p_sigacts, M_SUBPROC);
p->p_sigacts = NULL;
if (--p->p_limit->p_refcnt == 0)
FREE_ZONE(p->p_limit, sizeof *p->p_limit, M_SUBPROC);
p->p_limit = NULL;
p->task = TASK_NULL;
pp = p->p_pptr;
if (pp != initproc) {
pp->si_pid = p->p_pid;
pp->si_status = p->p_xstat;
pp->si_code = CLD_EXITED;
pp->si_uid = p->p_cred->p_ruid;
}
psignal(p->p_pptr, SIGCHLD);
p->p_stat = SZOMB;
wakeup((caddr_t)p->p_pptr);
}