/*
- * Copyright (c) 2000-2001 Apple Computer, Inc. All rights reserved.
+ * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
*
* @APPLE_LICENSE_HEADER_START@
*
- * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
+ * The contents of this file constitute Original Code as defined in and
+ * are subject to the Apple Public Source License Version 1.1 (the
+ * "License"). You may not use this file except in compliance with the
+ * License. Please obtain a copy of the License at
+ * http://www.apple.com/publicsource and read it before using this file.
*
- * This file contains Original Code and/or Modifications of Original Code
- * as defined in and that are subject to the Apple Public Source License
- * Version 2.0 (the 'License'). You may not use this file except in
- * compliance with the License. Please obtain a copy of the License at
- * http://www.opensource.apple.com/apsl/ and read it before using this
- * file.
- *
- * The Original Code and all software distributed under the License are
- * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
+ * This Original Code and all software distributed under the License are
+ * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
- * Please see the License for the specific language governing rights and
- * limitations under the License.
+ * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
+ * License for the specific language governing rights and limitations
+ * under the License.
*
* @APPLE_LICENSE_HEADER_END@
*/
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/ioctl.h>
-#include <sys/proc.h>
+#include <sys/proc_internal.h>
+#include <sys/kauth.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/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/filedesc.h> /* fdfree */
+#include <sys/shm_internal.h> /* shmexit */
+#include <sys/acct.h> /* acct_process */
+#include <machine/spl.h>
+
+#include <bsm/audit_kernel.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_act.h>
#include <kern/sched_prim.h>
#include <kern/assert.h>
#if KTRACE
#include <sys/ktrace.h>
#endif
+#include <mach/mach_types.h>
+#include <mach/task.h>
+#include <mach/thread_act.h>
+#include <mach/mach_traps.h> /* init_process */
+
extern char init_task_failure_data[];
-int exit1 __P((struct proc *, int, int *));
+int exit1(struct proc *, int, int *);
+void proc_prepareexit(struct proc *p);
+void vfork_exit(struct proc *p, int rv);
+void vproc_exit(struct proc *p);
+__private_extern__ void munge_rusage(struct rusage *a_rusage_p, struct user_rusage *a_user_rusage_p);
+
+/*
+ * Things which should have prototypes in headers, but don't
+ */
+void unix_syscall_return(int);
+void *get_bsduthreadarg(thread_t);
+void proc_exit(struct proc *p);
+int wait1continue(int result);
+int waitidcontinue(int result);
+int *get_bsduthreadrval(thread_t);
+kern_return_t sys_perf_notify(struct task *task, exception_data_t code,
+ mach_msg_type_number_t codeCnt);
+
+/*
+ * NOTE: Source and target may *NOT* overlap!
+ * XXX Should share code with bsd/dev/ppc/unix_signal.c
+ */
+static void
+siginfo_64to32(user_siginfo_t *in, 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(void *,in->si_addr);
+ /* following cast works for sival_int because of padding */
+ out->si_value.sival_ptr = CAST_DOWN(void *,in->si_value.sival_ptr);
+ out->si_band = in->si_band; /* range reduction */
+ out->pad[0] = in->pad[0]; /* mcontext.ss.r1 */
+}
/*
* exit --
* Death of process.
*/
-struct exit_args {
- int rval;
-};
void
-exit(p, uap, retval)
- struct proc *p;
- struct exit_args *uap;
- int *retval;
+exit(struct proc *p, struct exit_args *uap, int *retval)
{
exit1(p, W_EXITCODE(uap->rval, 0), retval);
* status and rusage for wait(). Check for child processes and orphan them.
*/
int
-exit1(p, rv, retval)
- register struct proc *p;
- int rv;
- int * retval;
+exit1(struct proc *p, int rv, int *retval)
{
- register struct proc *q, *nq;
thread_t self = current_thread();
- thread_act_t th_act_self = current_act();
struct task *task = p->task;
- register int i,s;
+ register int s;
struct uthread *ut;
/*
* right here.
*/
- ut = get_bsdthread_info(th_act_self);
- if (ut->uu_flag & P_VFORK) {
- (void)vfork_exit(p, rv);
- vfork_return(th_act_self, p->p_pptr, p , retval);
+ ut = get_bsdthread_info(self);
+ if (ut->uu_flag & UT_VFORK) {
+ vfork_exit(p, rv);
+ vfork_return(self, p->p_pptr, p , retval);
unix_syscall_return(0);
/* NOT REACHED */
}
+ AUDIT_SYSCALL_EXIT(0, p, ut); /* Exit is always successfull */
signal_lock(p);
while (p->exit_thread != self) {
if (sig_try_locked(p) <= 0) {
- if (get_threadtask(th_act_self) != task) {
+ if (get_threadtask(self) != task) {
signal_unlock(p);
return(0);
}
signal_unlock(p);
- thread_terminate(th_act_self);
+ thread_terminate(self);
thread_funnel_set(kernel_flock, FALSE);
thread_exception_return();
/* NOTREACHED */
s = splsched();
p->p_flag |= P_WEXIT;
splx(s);
- (void)proc_prepareexit(p);
+ proc_prepareexit(p);
p->p_xstat = rv;
/* task terminate will call proc_terminate and that cleans it up */
task_terminate_internal(task);
- /*
- * we come back and returns to AST which
- * should cleanup the rest
- */
-#if 0
- if (task == current_task()) {
- thread_exception_return();
- /*NOTREACHED*/
- }
-
- while (task == current_task()) {
- thread_terminate_self();
- /*NOTREACHED*/
- }
-#endif
return(0);
}
void
proc_prepareexit(struct proc *p)
{
- int s;
struct uthread *ut;
+ exception_data_t code[EXCEPTION_CODE_MAX];
thread_t self = current_thread();
- thread_act_t th_act_self = current_act();
+
+ code[0] = (exception_data_t)0xFF000001; /* Set terminate code */
+ code[1] = (exception_data_t)p->p_pid; /* Pass out the pid */
+ /* Notify the perf server */
+ (void)sys_perf_notify(p->task, (exception_data_t)&code, 2);
/*
* Remove proc from allproc queue and from pidhash chain.
* in partially cleaned state.
*/
LIST_REMOVE(p, p_list);
+ LIST_INSERT_HEAD(&zombproc, p, p_list); /* Place onto zombproc. */
LIST_REMOVE(p, p_hash);
#ifdef PGINPROF
* P_PPWAIT is set; we will wakeup the parent below.
*/
p->p_flag &= ~(P_TRACED | P_PPWAIT);
- p->p_sigignore = ~0;
+ p->p_sigignore = ~(sigcantmask);
p->p_siglist = 0;
- ut = get_bsdthread_info(th_act_self);
+ ut = get_bsdthread_info(self);
ut->uu_siglist = 0;
untimeout(realitexpire, (caddr_t)p->p_pid);
}
proc_exit(struct proc *p)
{
register struct proc *q, *nq, *pp;
- thread_t self = current_thread();
- thread_act_t th_act_self = current_act();
struct task *task = p->task;
- register int i,s;
+ register int s;
boolean_t funnel_state;
/* This can happen if thread_terminate of the single thread
proc_prepareexit(p);
}
+ p->p_lflag |= P_LPEXIT;
+ /* XXX Zombie allocation may fail, in which case stats get lost */
MALLOC_ZONE(p->p_ru, struct rusage *,
sizeof (*p->p_ru), M_ZOMBIE, M_WAITOK);
+ /*
+ * need to cancel async IO requests that can be cancelled and wait for those
+ * already active. MAY BLOCK!
+ */
+ _aio_exit( p );
+
/*
* Close open files and release open-file table.
* This may block!
if (sp->s_ttyvp) {
struct vnode *ttyvp;
+ struct vfs_context context;
/*
* Controlling process.
* The tty could have been revoked
* if we blocked.
*/
+ context.vc_proc = p;
+ context.vc_ucred = p->p_ucred;
if (sp->s_ttyvp)
- VOP_REVOKE(sp->s_ttyvp, REVOKEALL);
+ VNOP_REVOKE(sp->s_ttyvp, REVOKEALL, &context);
}
ttyvp = sp->s_ttyvp;
sp->s_ttyvp = NULL;
- if (ttyvp)
- vrele(ttyvp);
+ if (ttyvp) {
+ vnode_rele(ttyvp);
+ }
/*
* s_ttyp is not zero'd; we use this to indicate
* that the session once had a controlling terminal.
/*
* release trace file
*/
- p->p_traceflag = 0; /* don't trace the vrele() */
+ p->p_traceflag = 0; /* don't trace the vnode_put() */
if (p->p_tracep) {
struct vnode *tvp = p->p_tracep;
p->p_tracep = NULL;
- vrele(tvp);
+ vnode_rele(tvp);
}
#endif
- q = p->p_children.lh_first;
- if (q) /* only need this if any child is S_ZOMB */
- wakeup((caddr_t) initproc);
- for (; q != 0; q = nq) {
- nq = q->p_sibling.le_next;
+ while (q = p->p_children.lh_first) {
proc_reparent(q, initproc);
/*
* Traced processes are killed
if (q->p_flag & P_TRACED) {
q->p_flag &= ~P_TRACED;
if (q->sigwait_thread) {
- thread_t sig_shuttle;
-
- sig_shuttle = (thread_t)getshuttle_thread((thread_act_t)q->sigwait_thread);
/*
* The sigwait_thread could be stopped at a
* breakpoint. Wake it up to kill.
* the first thread in the task. So any attempts to kill
* the process would result into a deadlock on q->sigwait.
*/
- thread_resume((thread_act_t)q->sigwait_thread);
- clear_wait(sig_shuttle, THREAD_INTERRUPTED);
- threadsignal((thread_act_t)q->sigwait_thread, SIGKILL, 0);
+ thread_resume((thread_t)q->sigwait_thread);
+ clear_wait(q->sigwait_thread, THREAD_INTERRUPTED);
+ threadsignal((thread_t)q->sigwait_thread, SIGKILL, 0);
}
psignal(q, SIGKILL);
}
/*
* Save exit status and final rusage info, adding in child rusage
- * info and self times.
+ * info and self times. If we were unable to allocate a zombie
+ * structure, this information is lost.
*/
- *p->p_ru = p->p_stats->p_ru;
+ 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);
+ timerclear(&p->p_ru->ru_utime);
+ timerclear(&p->p_ru->ru_stime);
- if (task) {
+ if (task) {
task_basic_info_data_t tinfo;
task_thread_times_info_data_t ttimesinfo;
int task_info_stuff, task_ttimes_stuff;
task_info_stuff = TASK_BASIC_INFO_COUNT;
task_info(task, TASK_BASIC_INFO,
- &tinfo, &task_info_stuff);
+ (task_info_t)&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;
task_ttimes_stuff = TASK_THREAD_TIMES_INFO_COUNT;
task_info(task, TASK_THREAD_TIMES_INFO,
- &ttimesinfo, &task_ttimes_stuff);
+ (task_info_t)&ttimesinfo, &task_ttimes_stuff);
ut.tv_sec = ttimesinfo.user_time.seconds;
ut.tv_usec = ttimesinfo.user_time.microseconds;
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);
+ ruadd(p->p_ru, &p->p_stats->p_cru);
+ }
/*
* Free up profiling buffers.
for (; p1 != NULL; p1 = pn) {
pn = p1->pr_next;
- kfree((vm_offset_t)p1, sizeof *p1);
+ kfree(p1, sizeof *p1);
}
}
//task->proc = NULL;
set_bsdtask_info(task, NULL);
+ KNOTE(&p->p_klist, NOTE_EXIT);
+
/*
* Notify parent that we're gone.
*/
if (p->p_pptr->p_flag & P_NOCLDWAIT) {
- struct proc * pp = p->p_pptr;
+ struct proc *opp = p->p_pptr;
+
+ /*
+ * Add child resource usage to parent before giving
+ * zombie to init. If we were unable to allocate a
+ * zombie structure, this information is lost.
+ */
+ if (p->p_ru != NULL)
+ ruadd(&p->p_pptr->p_stats->p_cru, p->p_ru);
proc_reparent(p, initproc);
/* If there are no more children wakeup parent */
- if (LIST_EMPTY(&pp->p_children))
- wakeup((caddr_t)pp);
+ if (LIST_EMPTY(&opp->p_children))
+ wakeup((caddr_t)opp);
}
/* should be fine as parent proc would be initproc */
pp = p->p_pptr;
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;
+ pp->si_uid = p->p_ucred->cr_ruid;
}
- psignal(pp, SIGCHLD);
-
-
- /* Place onto zombproc. */
- LIST_INSERT_HEAD(&zombproc, p, p_list);
+ /* mark as a zombie */
p->p_stat = SZOMB;
+ psignal(pp, SIGCHLD);
+
/* and now wakeup the parent */
wakeup((caddr_t)p->p_pptr);
}
-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;
+/*
+ * reap_child_process
+ *
+ * Description: Given a process from which all status information needed
+ * has already been extracted, if the process is a ptrace
+ * attach process, detach it and give it back to its real
+ * parent, else recover all resources remaining associated
+ * with it.
+ *
+ * Parameters: struct proc *parent Parent of process being reaped
+ * struct proc *child Process to reap
+ *
+ * Returns: 0 Process was not reaped because it
+ * came from an attach
+ * 1 Process was reaped
+ */
+static int
+reap_child_process(struct proc *parent, struct proc *child)
{
- return (wait1(p, uap, retval, 0));
-}
-
-struct owait3_args {
- int *status;
- int options;
- struct rusage *rusage;
-};
+ struct proc *trace_parent; /* Traced parent process, if tracing */
+ struct vnode *tvp; /* Traced vnode pointer, if used */
-int
-owait3(p, uap, retval)
- struct proc *p;
- struct owait3_args *uap;
- int *retval;
-{
- struct wait4_args *a;
+ /*
+ * If we got the child via a ptrace 'attach',
+ * we need to give it back to the old parent.
+ */
+ if (child->p_oppid && (trace_parent = pfind(child->p_oppid))) {
+ child->p_oppid = 0;
+ proc_reparent(child, trace_parent);
+ if (trace_parent != initproc) {
+ 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;
+ }
+ psignal(trace_parent, SIGCHLD);
+ wakeup((caddr_t)trace_parent);
+ return (0);
+ }
+ child->p_xstat = 0;
+ if (child->p_ru) {
+ ruadd(&parent->p_stats->p_cru, child->p_ru);
+ 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);
+ }
- a = (struct wait4_args *)get_bsduthreadarg(current_act);
+ /*
+ * Decrement the count of procs running with this uid.
+ */
+ (void)chgproccnt(child->p_ucred->cr_ruid, -1);
- a->rusage = uap->rusage;
- a->options = uap->options;
- a->status = uap->status;
- a->pid = WAIT_ANY;
+ /*
+ * Free up credentials.
+ */
+ if (child->p_ucred != NOCRED) {
+ kauth_cred_t ucr = child->p_ucred;
+ child->p_ucred = NOCRED;
+ kauth_cred_rele(ucr);
+ }
- return (wait1(p, a, retval, 1));
+ /*
+ * Release reference to text vnode
+ */
+ tvp = child->p_textvp;
+ child->p_textvp = NULL;
+ if (tvp) {
+ vnode_rele(tvp);
+ }
+ /*
+ * Finally finished with old proc entry.
+ * Unlink it from its process group and free it.
+ */
+ leavepgrp(child);
+ LIST_REMOVE(child, p_list); /* off zombproc */
+ LIST_REMOVE(child, p_sibling);
+ child->p_lflag &= ~P_LWAITING;
+ wakeup(&child->p_stat);
+
+ lck_mtx_destroy(&child->p_mlock, proc_lck_grp);
+ lck_mtx_destroy(&child->p_fdmlock, proc_lck_grp);
+ FREE_ZONE(child, sizeof *child, M_PROC);
+ nprocs--;
+ return (1);
}
-#else
-#define wait1 wait4
-#endif
int
-wait1continue(result)
+wait1continue(int result)
{
void *vt;
- thread_act_t thread;
+ thread_t thread;
int *retval;
struct proc *p;
return(result);
p = current_proc();
- thread = current_act();
- vt = (void *)get_bsduthreadarg(thread);
- retval = (int *)get_bsduthreadrval(thread);
- wait1((struct proc *)p, (struct wait4_args *)vt, retval, 0);
+ thread = current_thread();
+ vt = get_bsduthreadarg(thread);
+ retval = get_bsduthreadrval(thread);
+ return(wait4((struct proc *)p, (struct wait4_args *)vt, retval));
}
int
-wait1(q, uap, retval, compat)
- register struct proc *q;
- register struct wait4_args *uap;
- register_t *retval;
-#if COMPAT_43
- int compat;
-#endif
+wait4(struct proc *q, struct wait4_args *uap, register_t *retval)
{
register int nfound;
- register struct proc *p, *t;
+ register struct proc *p;
int status, error;
- struct vnode *tvp;
-retry:
if (uap->pid == 0)
uap->pid = -q->p_pgid;
p->p_pgid != -(uap->pid))
continue;
nfound++;
- if (p->p_flag & P_WAITING) {
+
+ /* XXX This is racy because we don't get the lock!!!! */
+
+ if (p->p_lflag & P_LWAITING) {
(void)tsleep(&p->p_stat, PWAIT, "waitcoll", 0);
goto loop;
}
- p->p_flag |= P_WAITING; /* only allow single thread to wait() */
+ p->p_lflag |= P_LWAITING; /* only allow single thread to wait() */
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; /* convert to int */
- if (error = copyout((caddr_t)&status,
- (caddr_t)uap->status,
- sizeof(status))) {
- p->p_flag &= ~P_WAITING;
+ error = copyout((caddr_t)&status,
+ uap->status,
+ sizeof(status));
+ if (error) {
+ p->p_lflag &= ~P_LWAITING;
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 we got the child via a ptrace 'attach',
- * we need to give it back to the old parent.
- */
- 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;
+ if (uap->rusage) {
+ if (p->p_ru == NULL) {
+ error = ENOMEM;
+ } else {
+ if (IS_64BIT_PROCESS(q)) {
+ struct user_rusage my_rusage;
+ munge_rusage(p->p_ru, &my_rusage);
+ error = copyout((caddr_t)&my_rusage,
+ uap->rusage,
+ sizeof (my_rusage));
+ }
+ else {
+ error = copyout((caddr_t)p->p_ru,
+ uap->rusage,
+ sizeof (struct rusage));
+ }
}
- 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);
- }
-
- /*
- * Decrement the count of procs running with this uid.
- */
- (void)chgproccnt(p->p_cred->p_ruid, -1);
-
- /*
- * Free up credentials.
- */
- 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);
+ /* information unavailable? */
+ if (error) {
+ p->p_lflag &= ~P_LWAITING;
+ wakeup(&p->p_stat);
+ return (error);
}
- pcr = p->p_cred;
- p->p_cred = NULL;
- FREE_ZONE(pcr, sizeof *pcr, M_SUBPROC);
}
- /*
- * Release reference to text vnode
- */
- tvp = p->p_textvp;
- p->p_textvp = NULL;
- if (tvp)
- vrele(tvp);
+ /* Clean up */
+ if (!reap_child_process(q, p)) {
+ p->p_lflag &= ~P_LWAITING;
+ wakeup(&p->p_stat);
+ }
- /*
- * Finally finished with old proc entry.
- * Unlink it from its process group and free it.
- */
- leavepgrp(p);
- LIST_REMOVE(p, p_list); /* off zombproc */
- 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,
+ uap->status,
sizeof(status));
} else
error = 0;
- p->p_flag &= ~P_WAITING;
+ p->p_lflag &= ~P_LWAITING;
wakeup(&p->p_stat);
return (error);
}
- p->p_flag &= ~P_WAITING;
+ p->p_lflag &= ~P_LWAITING;
wakeup(&p->p_stat);
}
if (nfound == 0)
return (0);
}
- if (error = tsleep0((caddr_t)q, PWAIT | PCATCH, "wait", 0, wait1continue))
+ if ((error = tsleep0((caddr_t)q, PWAIT | PCATCH, "wait", 0, wait1continue)))
+ return (error);
+
+ goto loop;
+}
+
+
+int
+waitidcontinue(int result)
+{
+ void *vt;
+ thread_t thread;
+ int *retval;
+ struct proc *p;
+
+ if (result)
+ return(result);
+
+ p = current_proc();
+ thread = current_thread();
+ vt = get_bsduthreadarg(thread);
+ retval = get_bsduthreadrval(thread);
+ return(waitid((struct proc *)p, (struct waitid_args *)vt, retval));
+}
+
+/*
+ * Description: Suspend the calling thread until one child of the process
+ * containing the calling thread changes state.
+ *
+ * Parameters: uap->idtype one of P_PID, P_PGID, P_ALL
+ * uap->id pid_t or gid_t or ignored
+ * uap->infop Address of signinfo_t struct in
+ * user space into which to return status
+ * uap->options flag values
+ *
+ * Returns: 0 Success
+ * !0 Error returning status to user space
+ */
+int
+waitid(struct proc *q, struct waitid_args *uap, register_t *retval)
+{
+ user_siginfo_t collect64; /* siginfo data to return to caller */
+
+ register int nfound;
+ register struct proc *p;
+ int error;
+
+loop:
+ nfound = 0;
+ for (p = q->p_children.lh_first; p != 0; p = p->p_sibling.le_next) {
+ switch(uap->idtype) {
+ case P_PID: /* child with process ID equal to... */
+ if (p->p_pid != (pid_t)uap->id)
+ continue;
+ break;
+ case P_PGID: /* child with process group ID equal to... */
+ if (p->p_pgid != (pid_t)uap->id)
+ continue;
+ break;
+ case P_ALL: /* any child */
+ break;
+ }
+
+ /* XXX This is racy because we don't get the lock!!!! */
+
+ /*
+ * Wait collision; go to sleep and restart; used to maintain
+ * the single return for waited process guarantee.
+ */
+ if (p->p_lflag & P_LWAITING) {
+ (void)tsleep(&p->p_stat, PWAIT, "waitidcoll", 0);
+ goto loop;
+ }
+ p->p_lflag |= P_LWAITING; /* mark busy */
+
+ nfound++;
+
+ /*
+ * Types of processes we are interested in
+ *
+ * XXX Don't know what to do for WCONTINUED?!?
+ */
+ switch(p->p_stat) {
+ case SZOMB: /* Exited */
+ if (!(uap->options & WEXITED))
+ break;
+
+ /* Collect "siginfo" information for caller */
+ collect64.si_signo = 0;
+ collect64.si_code = 0;
+ collect64.si_errno = 0;
+ collect64.si_pid = 0;
+ collect64.si_uid = 0;
+ collect64.si_addr = 0;
+ collect64.si_status = p->p_xstat;
+ collect64.si_band = 0;
+
+ if (IS_64BIT_PROCESS(p)) {
+ error = copyout((caddr_t)&collect64,
+ uap->infop,
+ sizeof(collect64));
+ } else {
+ siginfo_t collect;
+ siginfo_64to32(&collect64,&collect);
+ error = copyout((caddr_t)&collect,
+ uap->infop,
+ sizeof(collect));
+ }
+ /* information unavailable? */
+ if (error) {
+ p->p_lflag &= ~P_LWAITING;
+ wakeup(&p->p_stat);
+ return (error);
+ }
+
+ /* Prevent other process for waiting for this event? */
+ if (!(uap->options & WNOWAIT)) {
+ /* Clean up */
+ if (!reap_child_process(q, p)) {
+ p->p_lflag &= ~P_LWAITING;
+ wakeup(&p->p_stat);
+ }
+ }
+
+ return (0);
+
+ case SSTOP: /* Stopped */
+ /*
+ * If we are not interested in stopped processes, then
+ * ignore this one.
+ */
+ if (!(uap->options & WSTOPPED))
+ break;
+
+ /*
+ * If someone has already waited it, we lost a race
+ * to be the one to return status.
+ */
+ if ((p->p_flag & P_WAITED) != 0)
+ break;
+
+ /*
+ * If this is not a traced process, and they haven't
+ * indicated an interest in untraced processes, then
+ * ignore this one.
+ */
+ if (!(p->p_flag & P_TRACED) && !(uap->options & WUNTRACED))
+ break;
+
+ /* Collect "siginfo" information for caller */
+ collect64.si_signo = 0;
+ collect64.si_code = 0;
+ collect64.si_errno = 0;
+ collect64.si_pid = 0;
+ collect64.si_uid = 0;
+ collect64.si_addr = 0;
+ collect64.si_status = p->p_xstat;
+ collect64.si_band = 0;
+
+ if (IS_64BIT_PROCESS(p)) {
+ error = copyout((caddr_t)&collect64,
+ uap->infop,
+ sizeof(collect64));
+ } else {
+ siginfo_t collect;
+ siginfo_64to32(&collect64,&collect);
+ error = copyout((caddr_t)&collect,
+ uap->infop,
+ sizeof(collect));
+ }
+ /* information unavailable? */
+ if (error) {
+ p->p_lflag &= ~P_LWAITING;
+ wakeup(&p->p_stat);
+ return (error);
+ }
+
+ /* Prevent other process for waiting for this event? */
+ if (!(uap->options & WNOWAIT)) {
+ p->p_flag |= P_WAITED;
+ }
+
+ p->p_lflag &= ~P_LWAITING;
+ wakeup(&p->p_stat);
+ return (0);
+
+ default: /* All others */
+ /* ...meaning Continued */
+ if (!(uap->options & WCONTINUED))
+ break;
+
+ /*
+ * If the flag isn't set, then this process has not
+ * been stopped and continued, or the status has
+ * already been reaped by another caller of waitid().
+ */
+ if ((p->p_flag & P_CONTINUED) == 0)
+ break;
+
+ /* Collect "siginfo" information for caller */
+ collect64.si_signo = 0;
+ collect64.si_code = 0;
+ collect64.si_errno = 0;
+ collect64.si_pid = 0;
+ collect64.si_uid = 0;
+ collect64.si_addr = 0;
+ collect64.si_status = p->p_xstat;
+ collect64.si_band = 0;
+
+ if (IS_64BIT_PROCESS(p)) {
+ error = copyout((caddr_t)&collect64,
+ uap->infop,
+ sizeof(collect64));
+ } else {
+ siginfo_t collect;
+ siginfo_64to32(&collect64,&collect);
+ error = copyout((caddr_t)&collect,
+ uap->infop,
+ sizeof(collect));
+ }
+ /* information unavailable? */
+ if (error) {
+ p->p_lflag &= ~P_LWAITING;
+ wakeup(&p->p_stat);
+ return (error);
+ }
+
+ /* Prevent other process for waiting for this event? */
+ if (!(uap->options & WNOWAIT)) {
+ p->p_flag &= ~P_CONTINUED;
+ }
+
+ p->p_lflag &= ~P_LWAITING;
+ wakeup(&p->p_stat);
+ return (0);
+
+ break;
+ }
+
+
+ /* Not a process we are interested in; go on to next child */
+ p->p_lflag &= ~P_LWAITING;
+ wakeup(&p->p_stat);
+ }
+
+ /* No child processes that could possibly satisfy the request? */
+ if (nfound == 0)
+ return (ECHILD);
+
+ if (uap->options & WNOHANG) {
+ retval[0] = 0;
+ return (0);
+ }
+
+ if ((error = tsleep0((caddr_t)q, PWAIT | PCATCH, "waitid", 0, waitidcontinue)))
return (error);
goto loop;
* make process 'parent' the new parent of process 'child'.
*/
void
-proc_reparent(child, parent)
- register struct proc *child;
- register struct proc *parent;
+proc_reparent(struct proc *child, struct proc *parent)
{
if (child->p_pptr == parent)
LIST_REMOVE(child, p_sibling);
LIST_INSERT_HEAD(&parent->p_children, child, p_sibling);
child->p_pptr = parent;
+
+ if (initproc == parent && child->p_stat == SZOMB)
+ psignal(initproc, SIGCHLD);
}
/*
* gunned down by kill(-1, 0).
*/
kern_return_t
-init_process(void)
+init_process(__unused struct init_process_args *args)
{
register struct proc *p = current_proc();
- if (suser(p->p_ucred, &p->p_acflag))
+ AUDIT_MACH_SYSCALL_ENTER(AUE_INITPROCESS);
+ if (suser(kauth_cred_get(), &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_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);
- /*NOTREACHED*/
- }
-}
/*
* Exit: deallocate address space and other resources, change proc state
*/
void
-vfork_exit(p, rv)
- struct proc *p;
- int rv;
+vfork_exit(struct proc *p, int rv)
{
- register struct proc *q, *nq;
thread_t self = current_thread();
- thread_act_t th_act_self = current_act();
+#ifdef FIXME
struct task *task = p->task;
- register int i,s;
+#endif
+ register int s;
struct uthread *ut;
+ exception_data_t code[EXCEPTION_CODE_MAX];
/*
* If a thread in this task has already
* right here.
*/
- ut = get_bsdthread_info(th_act_self);
+ ut = get_bsdthread_info(self);
#ifdef FIXME
signal_lock(p);
while (p->exit_thread != self) {
if (sig_try_locked(p) <= 0) {
- if (get_threadtask(th_act_self) != task) {
+ if (get_threadtask(self) != task) {
signal_unlock(p);
return;
}
signal_unlock(p);
- thread_terminate(th_act_self);
+ thread_terminate(self);
thread_funnel_set(kernel_flock, FALSE);
thread_exception_return();
/* NOTREACHED */
s = splsched();
p->p_flag |= P_WEXIT;
+ p->p_lflag |= P_LPEXIT;
splx(s);
+
+ code[0] = (exception_data_t)0xFF000001; /* Set terminate code */
+ code[1] = (exception_data_t)p->p_pid; /* Pass out the pid */
+ /* Notify the perf server */
+ (void)sys_perf_notify(p->task, (exception_data_t)&code, 2);
+
/*
* Remove proc from allproc queue and from pidhash chain.
* Need to do this before we do anything that can block.
* in partially cleaned state.
*/
LIST_REMOVE(p, p_list);
+ LIST_INSERT_HEAD(&zombproc, p, p_list); /* Place onto zombproc. */
LIST_REMOVE(p, p_hash);
/*
* If parent is waiting for us to exit or exec,
p->p_xstat = rv;
- (void)vproc_exit(p);
+ vproc_exit(p);
}
void
vproc_exit(struct proc *p)
{
register struct proc *q, *nq, *pp;
- thread_t self = current_thread();
- thread_act_t th_act_self = current_act();
+#ifdef FIXME
struct task *task = p->task;
- register int i,s;
- boolean_t funnel_state;
+#endif
+ /* XXX Zombie allocation may fail, in which case stats get lost */
MALLOC_ZONE(p->p_ru, struct rusage *,
sizeof (*p->p_ru), M_ZOMBIE, M_WAITOK);
if (sp->s_ttyvp) {
struct vnode *ttyvp;
+ struct vfs_context context;
/*
* Controlling process.
* The tty could have been revoked
* if we blocked.
*/
+ context.vc_proc = p;
+ context.vc_ucred = p->p_ucred;
if (sp->s_ttyvp)
- VOP_REVOKE(sp->s_ttyvp, REVOKEALL);
+ VNOP_REVOKE(sp->s_ttyvp, REVOKEALL, &context);
}
ttyvp = sp->s_ttyvp;
sp->s_ttyvp = NULL;
- if (ttyvp)
- vrele(ttyvp);
+ if (ttyvp) {
+ vnode_rele(ttyvp);
+ }
/*
* s_ttyp is not zero'd; we use this to indicate
* that the session once had a controlling terminal.
/*
* release trace file
*/
- p->p_traceflag = 0; /* don't trace the vrele() */
+ p->p_traceflag = 0; /* don't trace the vnode_rele() */
if (p->p_tracep) {
struct vnode *tvp = p->p_tracep;
p->p_tracep = NULL;
- vrele(tvp);
+ vnode_rele(tvp);
}
#endif
- q = p->p_children.lh_first;
- if (q) /* only need this if any child is S_ZOMB */
- wakeup((caddr_t) initproc);
- for (; q != 0; q = nq) {
- nq = q->p_sibling.le_next;
+ while (q = p->p_children.lh_first) {
proc_reparent(q, initproc);
/*
* Traced processes are killed
if (q->p_flag & P_TRACED) {
q->p_flag &= ~P_TRACED;
if (q->sigwait_thread) {
- thread_t sig_shuttle;
-
- sig_shuttle = (thread_t) getshuttle_thread((thread_act_t)q->sigwait_thread);
/*
* The sigwait_thread could be stopped at a
* breakpoint. Wake it up to kill.
* the first thread in the task. So any attempts to kill
* the process would result into a deadlock on q->sigwait.
*/
- thread_resume((thread_act_t)q->sigwait_thread);
- clear_wait(sig_shuttle, THREAD_INTERRUPTED);
- threadsignal((thread_act_t)q->sigwait_thread, SIGKILL, 0);
+ thread_resume((thread_t)q->sigwait_thread);
+ clear_wait(q->sigwait_thread, THREAD_INTERRUPTED);
+ threadsignal((thread_t)q->sigwait_thread, SIGKILL, 0);
}
psignal(q, SIGKILL);
}
/*
* Save exit status and final rusage info, adding in child rusage
- * info and self times.
+ * info and self times. If we were unable to allocate a zombie
+ * structure, this information is lost.
*/
- *p->p_ru = p->p_stats->p_ru;
-
- timerclear(&p->p_ru->ru_utime);
- timerclear(&p->p_ru->ru_stime);
+ 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) {
+ if (task) {
task_basic_info_data_t tinfo;
task_thread_times_info_data_t ttimesinfo;
int task_info_stuff, task_ttimes_stuff;
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);
- }
+ timeradd(&st,&p->p_ru->ru_stime,&p->p_ru->ru_stime);
+ }
#endif /* FIXME */
- ruadd(p->p_ru, &p->p_stats->p_cru);
+ ruadd(p->p_ru, &p->p_stats->p_cru);
+ }
/*
* Free up profiling buffers.
for (; p1 != NULL; p1 = pn) {
pn = p1->pr_next;
- kfree((vm_offset_t)p1, sizeof *p1);
+ kfree(p1, sizeof *p1);
}
}
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;
+ pp->si_uid = p->p_ucred->cr_ruid;
}
- psignal(p->p_pptr, SIGCHLD);
-
- /* Place onto zombproc. */
- LIST_INSERT_HEAD(&zombproc, p, p_list);
+ /* mark as a zombie */
p->p_stat = SZOMB;
+ psignal(p->p_pptr, SIGCHLD);
+
/* and now wakeup the parent */
wakeup((caddr_t)p->p_pptr);
}
+
+
+/*
+ * munge_rusage
+ * LP64 support - long is 64 bits if we are dealing with a 64 bit user
+ * process. We munge the kernel (32 bit) version of rusage into the
+ * 64 bit version.
+ */
+__private_extern__ void
+munge_rusage(struct rusage *a_rusage_p, struct user_rusage *a_user_rusage_p)
+{
+ /* timeval changes size, so utime and stime need special handling */
+ 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;
+ /*
+ * everything else can be a direct assign, since there is no loss
+ * of precision implied boing 32->64.
+ */
+ 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;
+}