This is an automated email from the ASF dual-hosted git repository. xiaoxiang781216 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit e1b476a67ce004414acd886a0bd782c71b26462d Author: Marco Casaroli <[email protected]> AuthorDate: Sun Nov 15 18:13:33 2026 +0100 sched/signal: Let kill() see a process that ended as POSIX says. kill(pid, 0) gave 0 for a process that its parent had already waited for. The exit wakes the parent before group_leave() and the release of the TCB, and the TCB stays in the table of threads until it is released. When the release blocks (a close in group_leave(), for example) or runs on another CPU, waitpid() returns first. kill(pid, 0) also gave ESRCH for a process that had ended and that its parent had not waited for yet (a zombie), when its TCB was gone. POSIX and Linux: such a process still exists, and a waited-for one does not. nxsig_kill() now asks the new nxsig_kill_target(). A thread that is not in its exit exists. A thread in its exit exists until it leaves its group, because the exit can still block there. But a process does not exist once a wait of its parent has reported its exit: the wait marks its TCB with the new TCB_FLAG_REAPED (nxsched_reaped()) when the TCB is still in the table. waitid(), waitpid() and wait() set the mark, with and without CONFIG_SCHED_CHILD_STATUS and CONFIG_SCHED_HAVE_PARENT; a wait with WNOWAIT does not. A process that its parent has a record of (a zombie) exists until the parent waits for it, also when its TCB is gone. A signal to a zombie or to a thread in its exit succeeds and has no effect. A signal to a process that its parent has waited for gives ESRCH. nxsig_kill_target() is public (include/nuttx/signal.h), so that procfs can use the same test. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <[email protected]> --- include/nuttx/sched.h | 1 + include/nuttx/signal.h | 21 ++++++++++++ sched/sched/sched.h | 4 +++ sched/sched/sched_waitid.c | 6 ++++ sched/sched/sched_waitpid.c | 32 +++++++++++++++++ sched/signal/sig_kill.c | 84 ++++++++++++++++++++++++++++++++++++++++++++- 6 files changed, 147 insertions(+), 1 deletion(-) diff --git a/include/nuttx/sched.h b/include/nuttx/sched.h index 3fe5fa3d9d2..b254a6e1953 100644 --- a/include/nuttx/sched.h +++ b/include/nuttx/sched.h @@ -126,6 +126,7 @@ #define TCB_FLAG_DETACHED (1 << 5) /* Bit 5: Pthread detached */ #define TCB_FLAG_FORCED_CANCEL (1 << 6) /* Bit 6: Pthread cancel is forced */ #define TCB_FLAG_JOIN_COMPLETED (1 << 7) /* Bit 7: Pthread join completed */ +#define TCB_FLAG_REAPED (1 << 8) /* Bit 8: Exit waited for by parent */ /* Values for struct task_group tg_flags */ diff --git a/include/nuttx/signal.h b/include/nuttx/signal.h index 4fcbeda86c5..d3554e1b231 100644 --- a/include/nuttx/signal.h +++ b/include/nuttx/signal.h @@ -495,6 +495,27 @@ int nxsig_queue(int pid, int signo, const union sigval value); int nxsig_kill(pid_t pid, int signo); +/**************************************************************************** + * Name: nxsig_kill_target + * + * Description: + * Tell if the process (or thread) pid exists, as kill() sees it. A + * thread in its exit exists until it leaves its group. A process that + * its parent has waited for does not exist, also when its TCB is still + * in the table. A process that has ended and that its parent has not + * waited for yet (a zombie) exists, also when its TCB is gone. + * + * Input Parameters: + * pid - The process or thread ID + * + * Returned Value: + * 1 if the thread pid runs, 0 if pid exists but ends (a zombie, or a + * thread in its exit), -ESRCH if it does not exist. + * + ****************************************************************************/ + +int nxsig_kill_target(pid_t pid); + /**************************************************************************** * Name: nxsig_kill_foreground * diff --git a/sched/sched/sched.h b/sched/sched/sched.h index 4b7d4a0a9a1..c1d1e553f4b 100644 --- a/sched/sched/sched.h +++ b/sched/sched/sched.h @@ -390,6 +390,10 @@ void nxsched_sporadic_lowpriority(FAR struct tcb_s *tcb); void nxsched_suspend(FAR struct tcb_s *tcb); #endif +#ifdef CONFIG_SCHED_WAITPID +void nxsched_reaped(pid_t pid); +#endif + #if defined(CONFIG_SCHED_WAITPID) && defined(CONFIG_SCHED_HAVE_PARENT) int nxsched_waitid(idtype_t idtype, pid_t id, FAR siginfo_t *info, int options); diff --git a/sched/sched/sched_waitid.c b/sched/sched/sched_waitid.c index 15e72bbc654..5520df26f51 100644 --- a/sched/sched/sched_waitid.c +++ b/sched/sched/sched_waitid.c @@ -105,6 +105,7 @@ static void report_child(FAR struct tcb_s *rtcb, { if (event == CHILD_FLAG_EXITED) { + nxsched_reaped(child->ch_pid); group_remove_child(rtcb->group, child->ch_pid); group_free_child(child); } @@ -376,6 +377,11 @@ static int waittcb(FAR struct tcb_s *rtcb, idtype_t idtype, pid_t id, if (!retains && child_selected(info, idtype, id, options)) { + if (info->si_code == CLD_EXITED && (options & WNOWAIT) == 0) + { + nxsched_reaped(info->si_pid); + } + return OK; } } diff --git a/sched/sched/sched_waitpid.c b/sched/sched/sched_waitpid.c index 3d224534914..3510e21619e 100644 --- a/sched/sched/sched_waitpid.c +++ b/sched/sched/sched_waitpid.c @@ -45,6 +45,33 @@ * Public Functions ****************************************************************************/ +/**************************************************************************** + * Name: nxsched_reaped + * + * Description: + * A wait has reported the exit of the process pid. If its TCB is still + * in the table (in its exit), mark it: kill() does not find it any more. + * The caller is in a critical section. + * + * Input Parameters: + * pid - The process ID that the wait reported + * + * Returned Value: + * None + * + ****************************************************************************/ + +void nxsched_reaped(pid_t pid) +{ + FAR struct tcb_s *tcb = nxsched_get_tcb(pid); + + if (tcb != NULL && (atomic_read(&tcb->shared_flags) & + TCB_FLAG_EXIT_PROCESSING) != 0) + { + atomic_or(&tcb->shared_flags, TCB_FLAG_REAPED); + } +} + /**************************************************************************** * Name: nxsched_waitpid * @@ -228,6 +255,11 @@ pid_t nxsched_waitpid(pid_t pid, FAR int *stat_loc, int options) group_del_waiter(group); + if (ret >= 0) + { + nxsched_reaped(pid); + } + if (ret < 0) { /* Handle the awkward case of whether or not we diff --git a/sched/signal/sig_kill.c b/sched/signal/sig_kill.c index fc376326357..7b0a10939c9 100644 --- a/sched/signal/sig_kill.c +++ b/sched/signal/sig_kill.c @@ -126,6 +126,76 @@ static int nxsig_kill_pgrp(pid_t pid, int signo, bool check) * Public Functions ****************************************************************************/ +/**************************************************************************** + * Name: nxsig_kill_target + * + * Description: + * Tell if the process (or thread) pid exists, as kill() sees it. A + * thread in its exit exists until it leaves its group: the exit can + * still block there (in a close(), for example). But the exit wakes the + * parent first: a process that a wait of its parent has reported does + * not exist any more, also when its TCB is still in the table. A + * process that has ended and that its parent has not waited for yet (a + * zombie: the parent has a record of it) exists, also when its TCB is + * gone. + * + * Input Parameters: + * pid - The process or thread ID + * + * Returned Value: + * 1 if the thread pid runs, 0 if pid exists but ends (a zombie, or a + * thread in its exit), -ESRCH if it does not exist. + * + ****************************************************************************/ + +int nxsig_kill_target(pid_t pid) +{ + FAR struct tcb_s *tcb; + irqstate_t flags; + int ret = -ESRCH; + int tflags; +#ifdef CONFIG_SCHED_CHILD_STATUS + int ndx; +#endif + + flags = enter_critical_section(); + tcb = nxsched_get_tcb(pid); + if (tcb != NULL) + { + /* A wait that reported the exit marks the TCB (nxsched_reaped()) */ + + tflags = atomic_read(&tcb->shared_flags); + if ((tflags & TCB_FLAG_EXIT_PROCESSING) == 0) + { + ret = 1; + } + else if ((tflags & TCB_FLAG_REAPED) == 0 && tcb->group != NULL) + { + ret = 0; + } + } + +#ifdef CONFIG_SCHED_CHILD_STATUS + /* The record of the child stays in its parent until the parent waits + * for it. + */ + + for (ndx = 0; ndx < g_npidhash && ret < 0; ndx++) + { + tcb = g_pidhash[ndx]; + if (tcb != NULL && tcb->group != NULL && + tcb->pid == tcb->group->tg_pid && + group_find_child(tcb->group, pid) != NULL) + { + ret = 0; + } + } +#endif + + leave_critical_section(flags); + return ret; +} + /**************************************************************************** * Name: nxsig_kill * @@ -161,6 +231,9 @@ static int nxsig_kill_pgrp(pid_t pid, int signo, bool check) * ESRCH The pid or process group does not exist. * ENOSYS pid is -1: a signal to all processes is not supported. * + * A process that has ended and that its parent has not waited for yet + * (a zombie) exists: a signal to it succeeds and has no effect. + * ****************************************************************************/ int nxsig_kill(pid_t pid, int signo) @@ -172,6 +245,7 @@ int nxsig_kill(pid_t pid, int signo) siginfo_t info; int ret = -EINVAL; #endif + int target; #ifdef CONFIG_SCHED_PROCESS_GROUPS /* pid 0 is the process group of the caller, -pid the process group pid. @@ -208,9 +282,10 @@ int nxsig_kill(pid_t pid, int signo) pid = -pid; } + target = nxsig_kill_target(pid); if (signo == 0) { - return (nxsched_get_tcb(pid) != NULL) ? 0 : -ESRCH; + return target < 0 ? target : 0; } #ifdef CONFIG_DISABLE_ALL_SIGNALS @@ -220,6 +295,13 @@ int nxsig_kill(pid_t pid, int signo) if (GOOD_SIGNO(signo)) { + /* A thread in its exit and a zombie get no signal */ + + if (target <= 0) + { + return target; + } + /* Zero the whole siginfo first: sigaction-queued signals carry a * si_user field that only the sigaction queue path assigns, so a * stack-garbage si_user here would be memcpy'd into the pending
