37 #include <sys/cdefs.h>
40 #include "opt_compat.h"
41 #include "opt_kdtrace.h"
42 #include "opt_ktrace.h"
43 #include "opt_procdesc.h"
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/sysproto.h>
48 #include <sys/capability.h>
49 #include <sys/eventhandler.h>
50 #include <sys/kernel.h>
51 #include <sys/malloc.h>
53 #include <sys/mutex.h>
55 #include <sys/procdesc.h>
56 #include <sys/pioctl.h>
60 #include <sys/vmmeter.h>
61 #include <sys/vnode.h>
62 #include <sys/racct.h>
63 #include <sys/resourcevar.h>
65 #include <sys/signalvar.h>
66 #include <sys/sched.h>
68 #include <sys/syscallsubr.h>
69 #include <sys/syslog.h>
70 #include <sys/ptrace.h>
72 #include <sys/filedesc.h>
77 #include <sys/ktrace.h>
80 #include <security/audit/audit.h>
81 #include <security/mac/mac_framework.h>
84 #include <vm/vm_extern.h>
85 #include <vm/vm_param.h>
87 #include <vm/vm_map.h>
88 #include <vm/vm_page.h>
92 #include <sys/dtrace_bsd.h>
93 dtrace_execexit_func_t dtrace_fasttrap_exit;
108 if ((child->p_treeflag & P_TREE_ORPHANED) == 0) {
109 if (child->p_oppid == 0 ||
110 child->p_pptr->p_pid == child->p_oppid)
111 parent = child->p_pptr;
116 for (p = child; (p->p_treeflag & P_TREE_FIRST_ORPHAN) == 0;) {
118 p = __containerof(p->p_orphan.le_prev,
struct proc,
120 KASSERT((p->p_treeflag & P_TREE_ORPHANED) != 0,
121 (
"missing P_ORPHAN %p", p));
123 parent = __containerof(p->p_orphan.le_prev,
struct proc,
134 if ((p->p_treeflag & P_TREE_ORPHANED) == 0)
136 if ((p->p_treeflag & P_TREE_FIRST_ORPHAN) != 0) {
137 p1 = LIST_NEXT(p, p_orphan);
139 p1->p_treeflag |= P_TREE_FIRST_ORPHAN;
140 p->p_treeflag &= ~P_TREE_FIRST_ORPHAN;
142 LIST_REMOVE(p, p_orphan);
143 p->p_treeflag &= ~P_TREE_ORPHANED;
153 exit1(td, W_EXITCODE(uap->rval, 0));
165 struct proc *p, *nq, *q, *t;
168 struct vnode *ttyvp = NULL;
172 mtx_assert(&
Giant, MA_NOTOWNED);
181 printf(
"init died (signal %d, exit %d)\n",
182 WTERMSIG(rv), WEXITSTATUS(rv));
183 panic(
"Going nowhere without my init!");
190 while (p->p_flag & P_HADTHREADS) {
222 KASSERT(p->p_numthreads == 1,
223 (
"exit1: proc %p exiting with %d threads", p, p->p_numthreads));
232 _STOPEVENT(p, S_EXIT, rv);
239 p->p_flag &= ~P_STOPPED_SIG;
240 KASSERT(!P_SHOULDSTOP(p), (
"exiting process is stopped"));
247 p->p_flag |= P_WEXIT;
254 while (p->p_lock > 0)
255 msleep(&p->p_lock, &p->p_mtx, PWAIT,
"exithold", 0);
260 callout_drain(&p->p_limco);
269 AUDIT_ARG_EXIT(WEXITSTATUS(rv), 0);
270 AUDIT_SYSCALL_EXIT(0, td);
274 if (p == p->p_leader) {
283 while (p->p_peers != NULL)
293 EVENTHANDLER_INVOKE(process_exit, p);
302 p->p_flag &= ~(P_TRACED | P_PPWAIT | P_PPTRACE);
308 if (timevalisset(&p->p_realtimer.it_value) &&
309 callout_stop(&p->p_itcallout) == 0) {
310 timevalclear(&p->p_realtimer.it_interval);
311 msleep(&p->p_itcallout, &p->p_mtx, PWAIT,
"ritwait", 0);
312 KASSERT(!timevalisset(&p->p_realtimer.it_value),
313 (
"realtime timer is still armed"));
327 (*nlminfo_release_p)(p);
339 if (td->td_pflags & TDP_GEOM)
346 if (p->p_leader->p_peers) {
348 while (q->p_peers != p)
350 q->p_peers = p->p_peers;
358 if (SESS_LEADER(p)) {
359 struct session *sp = p->p_session;
389 if (tp->t_session == sp)
396 if (vn_lock(ttyvp, LK_EXCLUSIVE) == 0) {
397 VOP_REVOKE(ttyvp, REVOKEALL);
398 VOP_UNLOCK(ttyvp, 0);
416 if ((vtmp = p->p_textvp) != NULL) {
418 locked = VFS_LOCK_GIANT(vtmp->v_mount);
420 VFS_UNLOCK_GIANT(locked);
439 LIST_REMOVE(p, p_list);
440 LIST_INSERT_HEAD(&
zombproc, p, p_list);
441 LIST_REMOVE(p, p_hash);
452 WITNESS_WARN(WARN_PANIC, NULL,
"process (pid %d) exiting", p->p_pid);
460 q = LIST_FIRST(&p->p_children);
463 for (; q != NULL; q = nq) {
464 nq = LIST_NEXT(q, p_sibling);
466 q->p_sigparent = SIGCHLD;
468 if (!(q->p_flag & P_TRACED)) {
490 q->p_flag &= ~(P_TRACED | P_STOPPED_TRACE);
491 FOREACH_THREAD_IN_PROC(q, tdt)
492 tdt->td_dbgflags &= ~TDB_SUSPEND;
501 while ((q = LIST_FIRST(&p->p_orphans)) != NULL) {
503 CTR2(KTR_PTRACE,
"exit: pid %d, clearing orphan %d", p->p_pid,
521 if (dtrace_fasttrap_exit)
522 dtrace_fasttrap_exit(p);
528 KNOTE_LOCKED(&p->p_klist, NOTE_EXIT);
531 int reason = CLD_EXITED;
534 else if (WIFSIGNALED(rv))
536 SDT_PROBE1(proc, kernel, , exit, reason);
544 knlist_clear(&p->p_klist, 1);
553 if (p->p_procdesc == NULL || procdesc_exit(p)) {
561 PROC_LOCK(p->p_pptr);
562 mtx_lock(&p->p_pptr->p_sigacts->ps_mtx);
563 if (p->p_pptr->p_sigacts->ps_flag &
564 (PS_NOCLDWAIT | PS_CLDSIGIGN)) {
567 mtx_unlock(&p->p_pptr->p_sigacts->ps_mtx);
571 p->p_sigparent = SIGCHLD;
572 PROC_LOCK(p->p_pptr);
581 mtx_unlock(&p->p_pptr->p_sigacts->ps_mtx);
585 else if (p->p_sigparent != 0) {
586 if (p->p_sigparent == SIGCHLD)
593 PROC_LOCK(p->p_pptr);
616 cv_broadcast(&p->p_pwait);
619 p->p_state = PRS_ZOMBIE;
620 PROC_UNLOCK(p->p_pptr);
631 ruadd(&p->p_ru, &p->p_rux, &p->p_stats->p_cru, &p->p_crux);
642 #ifndef _SYS_SYSPROTO_H_
653 struct proc *p = td->td_proc;
663 sb =
sbuf_new(NULL, NULL, 512, SBUF_FIXEDLEN);
666 p->p_comm, p->p_pid, td->td_ucred->cr_uid);
675 if (uap->
nargs > 0) {
676 if (uap->
args == NULL)
678 error = copyin(uap->
args, uargs, uap->
nargs *
sizeof(
void *));
686 if (uap->
why != NULL) {
693 if (uap->
nargs > 0) {
695 for (i = 0;i < uap->
nargs; i++)
696 sbuf_printf(sb,
"%s%p", i == 0 ?
"" :
", ", uargs[i]);
706 if (sig == SIGKILL) {
714 exit1(td, W_EXITCODE(0, sig));
724 owait(
struct thread *td,
struct owait_args *uap __unused)
728 error =
kern_wait(td, WAIT_ANY, &status, 0, NULL);
730 td->td_retval[1] = status;
741 struct rusage ru, *rup;
744 if (uap->rusage != NULL)
748 error =
kern_wait(td, uap->pid, &status, uap->options, rup);
749 if (uap->status != NULL && error == 0)
750 error = copyout(&status, uap->status,
sizeof(status));
751 if (uap->rusage != NULL && error == 0)
752 error = copyout(&ru, uap->rusage,
sizeof(
struct rusage));
759 struct __wrusage wru, *wrup;
765 idtype = uap->idtype;
768 if (uap->wrusage != NULL)
773 if (uap->info != NULL) {
775 bzero(sip,
sizeof(*sip));
783 error =
kern_wait6(td, idtype,
id, &status, uap->options, wrup, sip);
785 if (uap->status != NULL && error == 0)
786 error = copyout(&status, uap->status,
sizeof(status));
787 if (uap->wrusage != NULL && error == 0)
788 error = copyout(&wru, uap->wrusage,
sizeof(wru));
789 if (uap->info != NULL && error == 0)
790 error = copyout(&si, uap->info,
sizeof(si));
800 proc_reap(
struct thread *td,
struct proc *p,
int *status,
int options)
805 PROC_LOCK_ASSERT(p, MA_OWNED);
806 PROC_SLOCK_ASSERT(p, MA_OWNED);
807 KASSERT(p->p_state == PRS_ZOMBIE, (
"proc_reap: !PRS_ZOMBIE"));
812 td->td_retval[0] = p->p_pid;
814 *status = p->p_xstat;
815 if (options & WNOWAIT) {
833 if (p->p_oppid != 0 && p->p_oppid != p->p_pptr->p_pid) {
839 "wait: traced child %d moved back to parent %d", p->p_pid,
846 cv_broadcast(&p->p_pwait);
859 LIST_REMOVE(p, p_list);
861 LIST_REMOVE(p, p_sibling);
867 if (p->p_procdesc != NULL)
880 ruadd(&q->p_stats->p_cru, &q->p_crux, &p->p_ru, &p->p_rux);
886 (void)
chgproccnt(p->p_ucred->cr_ruidinfo, -1, 0);
921 KASSERT(FIRST_THREAD_IN_PROC(p),
922 (
"proc_reap: no residual thread!"));
930 proc_to_reap(
struct thread *td,
struct proc *p, idtype_t idtype, id_t
id,
931 int *status,
int options,
struct __wrusage *wrusage, siginfo_t *siginfo,
946 if (p->p_pid != (pid_t)
id) {
952 if (p->p_pgid != (pid_t)
id) {
958 if (p->p_session->s_sid != (pid_t)
id) {
964 if (p->p_ucred->cr_uid != (uid_t)
id) {
970 if (p->p_ucred->cr_gid != (gid_t)
id) {
976 if (p->p_ucred->cr_prison == NULL ||
977 (p->p_ucred->cr_prison->pr_id != (
int)
id)) {
997 if (((options & WEXITED) == 0) && (p->p_state == PRS_ZOMBIE)) {
1010 if ((p->p_sigparent != SIGCHLD) ^
1011 ((options & WLINUXCLONE) != 0)) {
1018 if (siginfo != NULL) {
1019 bzero(siginfo,
sizeof(*siginfo));
1020 siginfo->si_errno = 0;
1028 siginfo->si_signo = SIGCHLD;
1034 if (WCOREDUMP(p->p_xstat)) {
1035 siginfo->si_code = CLD_DUMPED;
1036 siginfo->si_status = WTERMSIG(p->p_xstat);
1037 }
else if (WIFSIGNALED(p->p_xstat)) {
1038 siginfo->si_code = CLD_KILLED;
1039 siginfo->si_status = WTERMSIG(p->p_xstat);
1041 siginfo->si_code = CLD_EXITED;
1042 siginfo->si_status = WEXITSTATUS(p->p_xstat);
1045 siginfo->si_pid = p->p_pid;
1046 siginfo->si_uid = p->p_ucred->cr_uid;
1061 if (wrusage != NULL) {
1062 rup = &wrusage->wru_self;
1064 calcru(p, &rup->ru_utime, &rup->ru_stime);
1066 rup = &wrusage->wru_children;
1067 *rup = p->p_stats->p_cru;
1068 calccru(p, &rup->ru_utime, &rup->ru_stime);
1071 if (p->p_state == PRS_ZOMBIE && !check_only) {
1081 kern_wait(
struct thread *td, pid_t pid,
int *status,
int options,
1082 struct rusage *rusage)
1084 struct __wrusage wru, *wrup;
1094 if (pid == WAIT_ANY) {
1097 }
else if (pid < 0) {
1114 options |= WEXITED | WTRAPPED;
1115 ret =
kern_wait6(td, idtype,
id, status, options, wrup, NULL);
1117 *rusage = wru.wru_self;
1122 kern_wait6(
struct thread *td, idtype_t idtype, id_t
id,
int *status,
1123 int options,
struct __wrusage *wrusage, siginfo_t *siginfo)
1126 int error, nfound, ret;
1128 AUDIT_ARG_VALUE((
int)idtype);
1129 AUDIT_ARG_PID((pid_t)
id);
1130 AUDIT_ARG_VALUE(options);
1134 if ((pid_t)
id == WAIT_MYPGRP && (idtype == P_PID || idtype == P_PGID)) {
1136 id = (id_t)q->p_pgid;
1142 if ((options & ~(WUNTRACED | WNOHANG | WCONTINUED | WNOWAIT |
1143 WEXITED | WTRAPPED | WLINUXCLONE)) != 0)
1145 if ((options & (WEXITED | WUNTRACED | WCONTINUED | WTRAPPED)) == 0) {
1156 if (q->p_flag & P_STATCHILD) {
1158 q->p_flag &= ~P_STATCHILD;
1163 LIST_FOREACH(p, &q->p_children, p_sibling) {
1165 wrusage, siginfo, 0);
1176 if ((options & WTRAPPED) != 0 &&
1177 (p->p_flag & P_TRACED) != 0 &&
1178 (p->p_flag & (P_STOPPED_TRACE | P_STOPPED_SIG)) != 0 &&
1179 (p->p_suspcount == p->p_numthreads) &&
1180 ((p->p_flag & P_WAITED) == 0)) {
1182 if ((options & WNOWAIT) == 0)
1183 p->p_flag |= P_WAITED;
1185 td->td_retval[0] = p->p_pid;
1188 *status = W_STOPCODE(p->p_xstat);
1189 if (siginfo != NULL) {
1190 siginfo->si_status = p->p_xstat;
1191 siginfo->si_code = CLD_TRAPPED;
1193 if ((options & WNOWAIT) == 0) {
1200 "wait: returning trapped pid %d status %#x (xstat %d) xthread %d",
1201 p->p_pid, W_STOPCODE(p->p_xstat), p->p_xstat,
1202 p->p_xthread != NULL ? p->p_xthread->td_tid : -1);
1206 if ((options & WUNTRACED) != 0 &&
1207 (p->p_flag & P_STOPPED_SIG) != 0 &&
1208 (p->p_suspcount == p->p_numthreads) &&
1209 ((p->p_flag & P_WAITED) == 0)) {
1211 if ((options & WNOWAIT) == 0)
1212 p->p_flag |= P_WAITED;
1214 td->td_retval[0] = p->p_pid;
1217 *status = W_STOPCODE(p->p_xstat);
1218 if (siginfo != NULL) {
1219 siginfo->si_status = p->p_xstat;
1220 siginfo->si_code = CLD_STOPPED;
1222 if ((options & WNOWAIT) == 0) {
1232 if ((options & WCONTINUED) != 0 &&
1233 (p->p_flag & P_CONTINUED) != 0) {
1235 td->td_retval[0] = p->p_pid;
1236 if ((options & WNOWAIT) == 0) {
1237 p->p_flag &= ~P_CONTINUED;
1246 if (siginfo != NULL) {
1247 siginfo->si_status = SIGCONT;
1248 siginfo->si_code = CLD_CONTINUED;
1268 LIST_FOREACH(p, &q->p_orphans, p_orphan) {
1272 KASSERT(ret != -1, (
"reaped an orphan (pid %d)",
1273 (
int)td->td_retval[0]));
1283 if (options & WNOHANG) {
1285 td->td_retval[0] = 0;
1290 if (q->p_flag & P_STATCHILD) {
1291 q->p_flag &= ~P_STATCHILD;
1294 error = msleep(q, &q->p_mtx, PWAIT | PCATCH,
"wait", 0);
1310 PROC_LOCK_ASSERT(child, MA_OWNED);
1311 if (child->p_pptr == parent)
1314 PROC_LOCK(child->p_pptr);
1316 PROC_UNLOCK(child->p_pptr);
1317 LIST_REMOVE(child, p_sibling);
1318 LIST_INSERT_HEAD(&parent->p_children, child, p_sibling);
1321 if (child->p_flag & P_TRACED) {
1322 if (LIST_EMPTY(&child->p_pptr->p_orphans)) {
1323 child->p_treeflag |= P_TREE_FIRST_ORPHAN;
1324 LIST_INSERT_HEAD(&child->p_pptr->p_orphans, child,
1327 LIST_INSERT_AFTER(LIST_FIRST(&child->p_pptr->p_orphans),
1330 child->p_treeflag |= P_TREE_ORPHANED;
struct proc * proc_realparent(struct proc *child)
int p_canwait(struct thread *td, struct proc *p)
static void clear_orphan(struct proc *p)
int pksignal(struct proc *p, int sig, ksiginfo_t *ksi)
void fixjobc(struct proc *p, struct pgrp *pgrp, int entering)
void sched_exit(struct proc *p, struct thread *td)
void sigqueue_take(ksiginfo_t *ksi)
int leavepgrp(struct proc *p)
void pargs_drop(struct pargs *pa)
void panic(const char *fmt,...)
void sigacts_free(struct sigacts *ps)
void funsetownlst(struct sigiolst *sigiolst)
SDT_PROBE_DEFINE1(proc, kernel,, exit,"int")
int kern_wait(struct thread *td, pid_t pid, int *status, int options, struct rusage *rusage)
void(* nlminfo_release_p)(struct proc *p)
void knlist_destroy(struct knlist *knl)
void kern_psignal(struct proc *p, int sig)
void sigqueue_flush(sigqueue_t *sq)
SDT_PROVIDER_DECLARE(proc)
void racct_sub(struct proc *p, int resource, uint64_t amount)
void calccru(struct proc *p, struct timeval *up, struct timeval *sp)
void sbuf_clear(struct sbuf *s)
void childproc_exited(struct proc *p)
void exit1(struct thread *td, int rv)
int acct_process(struct thread *td)
int sbuf_printf(struct sbuf *s, const char *fmt,...)
static int proc_to_reap(struct thread *td, struct proc *p, idtype_t idtype, id_t id, int *status, int options, struct __wrusage *wrusage, siginfo_t *siginfo, int check_only)
int chgproccnt(struct uidinfo *uip, int diff, rlim_t max)
void crfree(struct ucred *cr)
void stopprofclock(struct proc *p)
void log(int level, const char *fmt,...)
void proc_reap(struct thread *td, struct proc *p, int *status, int options)
void prison_proc_free(struct prison *pr)
int thread_single(int mode)
int sys_wait4(struct thread *td, struct wait4_args *uap)
struct sbuf * sbuf_new(struct sbuf *s, char *buf, int length, int flags)
int kern_wait6(struct thread *td, idtype_t idtype, id_t id, int *status, int options, struct __wrusage *wrusage, siginfo_t *siginfo)
int printf(const char *fmt,...)
void sys_sys_exit(struct thread *td, struct sys_exit_args *uap)
void fdfree(struct thread *td)
int sys_wait6(struct thread *td, struct wait6_args *uap)
void sbuf_delete(struct sbuf *s)
void racct_proc_exit(struct proc *p)
void vrele(struct vnode *vp)
char * sbuf_data(struct sbuf *s)
int sbuf_finish(struct sbuf *s)
int sbuf_cat(struct sbuf *s, const char *str)
int sbuf_trim(struct sbuf *s)
void tidhash_remove(struct thread *td)
void proc_reparent(struct proc *child, struct proc *parent)
int sbuf_copyin(struct sbuf *s, const void *uaddr, size_t len)
void lim_free(struct plimit *limp)
void calcru(struct proc *p, struct timeval *up, struct timeval *sp)
int sys_abort2(struct thread *td, struct abort2_args *uap)
void tty_signal_pgrp(struct tty *tp, int sig)
void thread_wait(struct proc *p)
void ruadd(struct rusage *ru, struct rusage_ext *rux, struct rusage *ru2, struct rusage_ext *rux2)
int thread_suspend_check(int return_instead)