Lines Matching +full:proc +full:- +full:id
1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
51 #include <sys/proc.h>
96 SDT_PROVIDER_DECLARE(proc);
97 SDT_PROBE_DEFINE1(proc, , , exit, "int");
109 struct proc *
110 proc_realparent(struct proc *child)
112 struct proc *p, *parent;
115 if ((child->p_treeflag & P_TREE_ORPHANED) == 0)
116 return (child->p_pptr->p_pid == child->p_oppid ?
117 child->p_pptr : child->p_reaper);
118 for (p = child; (p->p_treeflag & P_TREE_FIRST_ORPHAN) == 0;) {
120 p = __containerof(p->p_orphan.le_prev, struct proc,
122 KASSERT((p->p_treeflag & P_TREE_ORPHANED) != 0,
125 parent = __containerof(p->p_orphan.le_prev, struct proc,
131 reaper_abandon_children(struct proc *p, bool exiting)
133 struct proc *p1, *p2, *ptmp;
137 if ((p->p_treeflag & P_TREE_REAPER) == 0)
139 p1 = p->p_reaper;
140 LIST_FOREACH_SAFE(p2, &p->p_reaplist, p_reapsibling, ptmp) {
142 p2->p_reaper = p1;
143 p2->p_reapsubtree = p->p_reapsubtree;
144 LIST_INSERT_HEAD(&p1->p_reaplist, p2, p_reapsibling);
145 if (exiting && p2->p_pptr == p) {
151 KASSERT(LIST_EMPTY(&p->p_reaplist), ("p_reaplist not empty"));
152 p->p_treeflag &= ~P_TREE_REAPER;
156 reaper_clear(struct proc *p)
158 struct proc *p1;
163 if (p->p_reapsubtree == 1)
166 LIST_FOREACH(p1, &p->p_reaper->p_reaplist, p_reapsibling) {
167 if (p1->p_reapsubtree == p->p_reapsubtree) {
173 proc_id_clear(PROC_ID_REAP, p->p_reapsubtree);
177 proc_clear_orphan(struct proc *p)
179 struct proc *p1;
182 if ((p->p_treeflag & P_TREE_ORPHANED) == 0)
184 if ((p->p_treeflag & P_TREE_FIRST_ORPHAN) != 0) {
187 p1->p_treeflag |= P_TREE_FIRST_ORPHAN;
188 p->p_treeflag &= ~P_TREE_FIRST_ORPHAN;
191 p->p_treeflag &= ~P_TREE_ORPHANED;
195 exit_onexit(struct proc *p)
197 MPASS(p->p_numthreads == 1);
202 * exit -- death of process.
208 exit1(td, uap->rval, 0);
213 proc_set_p2_wexit(struct proc *p)
216 p->p_flag2 |= P2_WEXIT;
220 * Exit: deallocate address space and other resources, change proc state to
221 * zombie, and unlink proc from allproc and parent's lists. Save exit status
227 struct proc *p, *nq, *q, *t;
234 TSPROCEXIT(td->td_proc->p_pid);
236 p = td->td_proc;
241 * going down due to reboot, page-in requests for text are
269 while (p->p_flag & P_HADTHREADS) {
272 * some co-operation from other parts of the kernel
283 * re-check all suspension request, the thread should
300 KASSERT(p->p_numthreads == 1,
301 ("exit1: proc %p exiting with %d threads", p, p->p_numthreads));
305 p->p_xexit = rval;
306 p->p_xsig = signo;
313 p->p_flag &= ~P_STOPPED_SIG;
317 p->p_flag |= P_WEXIT;
323 while (p->p_lock > 0)
324 msleep(&p->p_lock, &p->p_mtx, PWAIT, "exithold", 0);
327 /* Drain the limit callout while we don't have the proc locked */
328 callout_drain(&p->p_limco);
342 if (p->p_peers != NULL && p == p->p_leader) {
344 q = p->p_peers;
349 q = q->p_peers;
351 while (p->p_peers != NULL)
372 p->p_ptevents = 0;
378 if (timevalisset(&p->p_realtimer.it_value) &&
379 callout_stop(&p->p_itcallout) == 0) {
380 timevalclear(&p->p_realtimer.it_interval);
382 callout_drain(&p->p_itcallout);
387 if (p->p_sysent->sv_onexit != NULL)
388 p->p_sysent->sv_onexit(p);
395 funsetownlst(&p->p_sigiolst);
398 * Close open files and release open-file table.
407 if (p->p_leader->p_peers != NULL) {
409 if (p->p_leader->p_peers != NULL) {
410 q = p->p_leader;
411 while (q->p_peers != p)
412 q = q->p_peers;
413 q->p_peers = p->p_peers;
414 wakeup(p->p_leader);
429 if (p->p_textvp != NULL) {
430 vrele(p->p_textvp);
431 p->p_textvp = NULL;
433 if (p->p_textdvp != NULL) {
434 vrele(p->p_textdvp);
435 p->p_textdvp = NULL;
437 if (p->p_binname != NULL) {
438 free(p->p_binname, M_PARGS);
439 p->p_binname = NULL;
445 lim_free(p->p_limit);
446 p->p_limit = NULL;
451 * Call machine-dependent code to release any
452 * machine-dependent resources other than the address space.
458 WITNESS_WARN(WARN_PANIC, NULL, "process (pid %d) exiting", p->p_pid);
471 p->p_list.le_prev = NULL;
473 prison_proc_unlink(p->p_ucred->cr_prison, p);
477 if ((p->p_flag & (P_TRACED | P_PPWAIT | P_PPTRACE)) != 0) {
479 p->p_flag &= ~(P_TRACED | P_PPWAIT | P_PPTRACE);
484 * killjobc() might drop and re-acquire proctree_lock to
491 * - traced ones to the original parent (or init if we are that parent)
492 * - the rest to init
494 q = LIST_FIRST(&p->p_children);
496 wakeup(q->p_reaper);
501 q->p_sigparent = SIGCHLD;
503 if ((q->p_flag & P_TRACED) == 0) {
504 proc_reparent(q, q->p_reaper, true);
505 if (q->p_state == PRS_ZOMBIE) {
515 if (q->p_ksi == NULL) {
518 ksiginfo_copy(q->p_ksi, ksi);
519 ksi->ksi_flags |= KSI_INS;
523 PROC_LOCK(q->p_reaper);
524 pksignal(q->p_reaper, SIGCHLD, ksi1);
525 PROC_UNLOCK(q->p_reaper);
526 } else if (q->p_pdeathsig > 0) {
531 kern_psignal(q, q->p_pdeathsig);
541 proc_reparent(q, q->p_reaper, true);
554 q->p_flag &= ~P_TRACED;
555 q->p_flag2 &= ~P2_PTRACE_FSTP;
556 q->p_ptevents = 0;
557 p->p_xthread = NULL;
559 tdt->td_dbgflags &= ~(TDB_SUSPEND | TDB_XSIG |
561 tdt->td_xsig = 0;
564 q->p_flag &= ~P_STOPPED_TRACE;
566 } else if ((q->p_flag & (P_STOPPED_TRACE |
580 while ((q = LIST_FIRST(&p->p_orphans)) != NULL) {
582 KASSERT(q->p_oppid == p->p_pid,
584 q->p_oppid));
585 q->p_oppid = q->p_reaper->p_pid;
592 if (q->p_pdeathsig > 0)
593 kern_psignal(q, q->p_pdeathsig);
594 CTR2(KTR_PTRACE, "exit: pid %d, clearing orphan %d", p->p_pid,
595 q->p_pid);
607 SDT_PROBE1(proc, , , exit, reason);
613 p->p_xthread = td;
615 if (p->p_sysent->sv_ontdexit != NULL)
616 p->p_sysent->sv_ontdexit(td);
630 KNOTE_LOCKED(p->p_klist, NOTE_EXIT);
639 if (p->p_procdesc == NULL || procdesc_exit(p)) {
646 PROC_LOCK(p->p_pptr);
647 mtx_lock(&p->p_pptr->p_sigacts->ps_mtx);
648 if (p->p_pptr->p_sigacts->ps_flag &
650 struct proc *pp;
652 mtx_unlock(&p->p_pptr->p_sigacts->ps_mtx);
653 pp = p->p_pptr;
655 proc_reparent(p, p->p_reaper, true);
656 p->p_sigparent = SIGCHLD;
657 PROC_LOCK(p->p_pptr);
666 mtx_unlock(&p->p_pptr->p_sigacts->ps_mtx);
668 if (p->p_pptr == p->p_reaper || p->p_pptr == initproc) {
670 } else if (p->p_sigparent != 0) {
671 if (p->p_sigparent == SIGCHLD) {
678 PROC_LOCK(p->p_pptr);
684 kern_psignal(p->p_pptr, p->p_sigparent);
688 prison_proc_free(p->p_ucred->cr_prison);
695 sigqueue_flush(&p->p_sigqueue);
696 sigqueue_flush(&td->td_sigqueue);
703 * before we release the parent process' proc lock to avoid
706 * proc lock.
708 wakeup(p->p_pptr);
709 cv_broadcast(&p->p_pwait);
710 sched_exit(p->p_pptr, td);
712 p->p_state = PRS_ZOMBIE;
713 PROC_UNLOCK(p->p_pptr);
719 ruadd(&p->p_ru, &p->p_rux, &p->p_stats->p_cru, &p->p_crux);
745 nargs = uap->nargs;
747 nargs = -1;
750 if (uap->args != NULL) {
751 error = copyin(uap->args, uargs,
754 nargs = -1;
758 nargs = -1;
760 return (kern_abort2(td, uap->why, nargs, uargsp));
766 * why - user pointer to why
767 * nargs - number of arguments copied or -1 if an error occurred in copying
768 * args - pointer to an array of pointers in kernel format
773 struct proc *p = td->td_proc;
785 p->p_comm, p->p_pid, td->td_ucred->cr_uid);
791 /* Prevent from DoSes from user-space. */
792 if (nargs == -1)
816 * from user-space succeed.
844 td->td_retval[1] = status;
858 if (uap->rusage != NULL)
862 error = kern_wait(td, uap->pid, &status, uap->options, rup);
863 if (uap->status != NULL && error == 0 && td->td_retval[0] != 0)
864 error = copyout(&status, uap->status, sizeof(status));
865 if (uap->rusage != NULL && error == 0 && td->td_retval[0] != 0)
866 error = copyout(&ru, uap->rusage, sizeof(struct rusage));
876 id_t id;
879 idtype = uap->idtype;
880 id = uap->id;
882 if (uap->wrusage != NULL)
887 if (uap->info != NULL) {
897 error = kern_wait6(td, idtype, id, &status, uap->options, wrup, sip);
899 if (uap->status != NULL && error == 0 && td->td_retval[0] != 0)
900 error = copyout(&status, uap->status, sizeof(status));
901 if (uap->wrusage != NULL && error == 0 && td->td_retval[0] != 0)
902 error = copyout(&wru, uap->wrusage, sizeof(wru));
903 if (uap->info != NULL && error == 0)
904 error = copyout(&si, uap->info, sizeof(si));
914 proc_reap(struct thread *td, struct proc *p, int *status, int options)
916 struct proc *q, *t;
920 KASSERT(p->p_state == PRS_ZOMBIE, ("proc_reap: !PRS_ZOMBIE"));
922 mtx_spin_wait_unlocked(&p->p_slock);
924 q = td->td_proc;
927 *status = KW_EXITCODE(p->p_xexit, p->p_xsig);
931 * release the proc struct just yet.
939 sigqueue_take(p->p_ksi);
946 if (p->p_oppid != p->p_pptr->p_pid) {
952 "wait: traced child %d moved back to parent %d", p->p_pid,
953 t->p_pid);
956 pksignal(t, SIGCHLD, p->p_ksi);
958 cv_broadcast(&p->p_pwait);
969 sx_xlock(PIDHASHLOCK(p->p_pid));
971 sx_xunlock(PIDHASHLOCK(p->p_pid));
979 if (p->p_procdesc != NULL)
983 proc_id_clear(PROC_ID_PID, p->p_pid);
986 knlist_detach(p->p_klist);
987 p->p_klist = NULL;
996 p->p_xexit = p->p_xsig = 0; /* XXX: why? */
999 ruadd(&q->p_stats->p_cru, &q->p_crux, &p->p_ru, &p->p_rux);
1019 pargs_drop(p->p_args);
1020 p->p_args = NULL;
1021 sigacts_free(p->p_sigacts);
1022 p->p_sigacts = NULL;
1025 * Do any thread-system specific cleanups.
1030 * Give vm and machine-dependent layer a chance to free anything that
1041 atomic_add_int(&nprocs, -1);
1045 proc_to_reap(struct thread *td, struct proc *p, idtype_t idtype, id_t id,
1057 if (p->p_procdesc == NULL ||
1058 (p->p_pptr == td->td_proc &&
1059 (p->p_flag & P_TRACED) != 0)) {
1066 if (p->p_pid != (pid_t)id) {
1072 if (p->p_pgid != (pid_t)id) {
1078 if (p->p_session->s_sid != (pid_t)id) {
1084 if (p->p_ucred->cr_uid != (uid_t)id) {
1090 if (p->p_ucred->cr_gid != (gid_t)id) {
1096 if (p->p_ucred->cr_prison->pr_id != (int)id) {
1116 if (((options & WEXITED) == 0) && (p->p_state == PRS_ZOMBIE)) {
1129 if ((p->p_sigparent != SIGCHLD) ^
1137 siginfo->si_errno = 0;
1145 siginfo->si_signo = SIGCHLD;
1151 if (WCOREDUMP(p->p_xsig)) {
1152 siginfo->si_code = CLD_DUMPED;
1153 siginfo->si_status = WTERMSIG(p->p_xsig);
1154 } else if (WIFSIGNALED(p->p_xsig)) {
1155 siginfo->si_code = CLD_KILLED;
1156 siginfo->si_status = WTERMSIG(p->p_xsig);
1158 siginfo->si_code = CLD_EXITED;
1159 siginfo->si_status = p->p_xexit;
1162 siginfo->si_pid = p->p_pid;
1163 siginfo->si_uid = p->p_ucred->cr_uid;
1169 * siginfo->si_addr to NULL.
1179 rup = &wrusage->wru_self;
1180 *rup = p->p_ru;
1182 calcru(p, &rup->ru_utime, &rup->ru_stime);
1185 rup = &wrusage->wru_children;
1186 *rup = p->p_stats->p_cru;
1187 calccru(p, &rup->ru_utime, &rup->ru_stime);
1190 if (p->p_state == PRS_ZOMBIE && !check_only) {
1192 return (-1);
1203 id_t id;
1213 id = 0;
1216 id = (id_t)-pid;
1219 id = (id_t)pid;
1232 ret = kern_wait6(td, idtype, id, status, options, wrup, NULL);
1239 report_alive_proc(struct thread *td, struct proc *p, siginfo_t *siginfo,
1252 p->p_flag &= ~P_CONTINUED;
1254 p->p_flag |= P_WAITED;
1256 (td->td_proc->p_sysent->sv_flags & SV_SIG_WAITNDQ) == 0) {
1257 PROC_LOCK(td->td_proc);
1258 sigqueue_take(p->p_ksi);
1259 PROC_UNLOCK(td->td_proc);
1264 siginfo->si_code = si_code;
1265 siginfo->si_status = cont ? SIGCONT : p->p_xsig;
1268 *status = cont ? SIGCONT : W_STOPCODE(p->p_xsig);
1269 td->td_retval[0] = p->p_pid;
1274 kern_wait6(struct thread *td, idtype_t idtype, id_t id, int *status,
1277 struct proc *p, *q;
1282 AUDIT_ARG_VALUE((int)idtype); /* XXX - This is likely wrong! */
1283 AUDIT_ARG_PID((pid_t)id); /* XXX - This may be wrong! */
1286 q = td->td_proc;
1288 if ((pid_t)id == WAIT_MYPGRP && (idtype == P_PID || idtype == P_PGID)) {
1290 id = (id_t)q->p_pgid;
1310 if (q->p_flag & P_STATCHILD) {
1312 q->p_flag &= ~P_STATCHILD;
1318 LIST_FOREACH(p, &q->p_children, p_sibling) {
1319 pid = p->p_pid;
1320 ret = proc_to_reap(td, p, idtype, id, status, options,
1325 td->td_retval[0] = pid;
1332 * disown them - by passing their process descriptor to another
1333 * process - which means it needs to be prevented from touching
1336 if (IN_CAPABILITY_MODE(td) && p->p_procdesc != NULL) {
1345 (p->p_flag & P_TRACED) != 0) {
1348 ((p->p_flag & (P_STOPPED_TRACE | P_STOPPED_SIG)) &&
1349 p->p_suspcount == p->p_numthreads &&
1350 (p->p_flag & P_WAITED) == 0);
1356 p->p_pid, W_STOPCODE(p->p_xsig), p->p_xsig,
1357 p->p_xthread != NULL ?
1358 p->p_xthread->td_tid : -1);
1365 (p->p_flag & P_STOPPED_SIG) != 0) {
1367 report = (p->p_suspcount == p->p_numthreads &&
1368 ((p->p_flag & P_WAITED) == 0));
1377 (p->p_flag & P_CONTINUED) != 0) {
1391 * switch-over from parent to child. At this point due to
1392 * re-parenting the parent loses the child to debugger and a
1398 LIST_FOREACH(p, &q->p_orphans, p_orphan) {
1399 ret = proc_to_reap(td, p, idtype, id, NULL, options,
1402 KASSERT(ret != -1, ("reaped an orphan (pid %d)",
1403 (int)td->td_retval[0]));
1416 td->td_retval[0] = 0;
1420 if (q->p_flag & P_STATCHILD) {
1421 q->p_flag &= ~P_STATCHILD;
1426 error = msleep(q, &q->p_mtx, PWAIT | PCATCH | PDROP, "wait", 0);
1433 proc_add_orphan(struct proc *child, struct proc *parent)
1437 KASSERT((child->p_flag & P_TRACED) != 0,
1440 if (LIST_EMPTY(&parent->p_orphans)) {
1441 child->p_treeflag |= P_TREE_FIRST_ORPHAN;
1442 LIST_INSERT_HEAD(&parent->p_orphans, child, p_orphan);
1444 LIST_INSERT_AFTER(LIST_FIRST(&parent->p_orphans),
1447 child->p_treeflag |= P_TREE_ORPHANED;
1455 proc_reparent(struct proc *child, struct proc *parent, bool set_oppid)
1460 if (child->p_pptr == parent)
1463 PROC_LOCK(child->p_pptr);
1464 sigqueue_take(child->p_ksi);
1465 PROC_UNLOCK(child->p_pptr);
1467 LIST_INSERT_HEAD(&parent->p_children, child, p_sibling);
1470 if ((child->p_flag & P_TRACED) != 0) {
1471 proc_add_orphan(child, child->p_pptr);
1474 child->p_pptr = parent;
1476 child->p_oppid = parent->p_pid;