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 a33d54d6251e889b71b8de3f4221ecc40a8efece
Author: Marco Casaroli <[email protected]>
AuthorDate: Fri Nov 27 18:29:39 2026 +0100

    fs/procfs: Do not show a task that kill() does not find.
    
    A process that ends stays in the table of threads until its CPU
    releases its TCB.  This can be after its parent has waited for it.
    kill(pid, 0) gives ESRCH for such a process (nxsig_kill_target()), but
    procfs looked only for the TCB.  So readdir() of /proc listed the ID of
    a child that waitpid() had already reaped, and /proc/<pid> could still
    be opened.
    
    procfs_readdir() skips an ID of the snapshot that kill() does not find.
    proc_open(), proc_opendir() and proc_stat() give ENOENT for it.  So
    kill(pid, 0) and /proc agree.  A task in its exit that no parent has
    waited for stays visible, as before.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 fs/procfs/fs_procfs.c     | 8 ++++++--
 fs/procfs/fs_procfsproc.c | 6 +++---
 2 files changed, 9 insertions(+), 5 deletions(-)

diff --git a/fs/procfs/fs_procfs.c b/fs/procfs/fs_procfs.c
index 01754c747a2..739929e06f1 100644
--- a/fs/procfs/fs_procfs.c
+++ b/fs/procfs/fs_procfs.c
@@ -45,6 +45,7 @@
 #include <nuttx/irq.h>
 #include <nuttx/arch.h>
 #include <nuttx/sched.h>
+#include <nuttx/signal.h>
 #include <nuttx/kmalloc.h>
 #include <nuttx/fs/fs.h>
 #include <nuttx/fs/procfs.h>
@@ -934,11 +935,14 @@ static int procfs_readdir(FAR struct inode *mountpt,
 
 #ifndef CONFIG_FS_PROCFS_EXCLUDE_PROCESS
       /* Skip the tasks of the snapshot of opendir() that have ended since:
-       * the walk goes on with the next one, as on Linux.
+       * the walk goes on with the next one, as on Linux.  A task has ended
+       * when kill() does not find it (nxsig_kill_target()): its TCB can
+       * stay in the table after its parent has waited for it.
        */
 
       while (priv->index < priv->nentries &&
-             nxsched_get_tcb(level0->pid[priv->index]) == NULL)
+             (nxsched_get_tcb(level0->pid[priv->index]) == NULL ||
+              nxsig_kill_target(level0->pid[priv->index]) < 0))
         {
           priv->index++;
         }
diff --git a/fs/procfs/fs_procfsproc.c b/fs/procfs/fs_procfsproc.c
index a7b8283a904..50d4e1cefea 100644
--- a/fs/procfs/fs_procfsproc.c
+++ b/fs/procfs/fs_procfsproc.c
@@ -1693,7 +1693,7 @@ static int proc_open(FAR struct file *filep, FAR const 
char *relpath,
   pid = (pid_t)tmp;
 
   tcb = nxsched_get_tcb(pid);
-  if (tcb == NULL)
+  if (tcb == NULL || nxsig_kill_target(pid) < 0)
     {
       ferr("ERROR: PID %d is no longer valid\n", pid);
       return -ENOENT;
@@ -2021,7 +2021,7 @@ static int proc_opendir(FAR const char *relpath,
   pid = (pid_t)tmp;
 
   tcb = nxsched_get_tcb(pid);
-  if (tcb == NULL)
+  if (tcb == NULL || nxsig_kill_target(pid) < 0)
     {
       ferr("ERROR: PID %d is not valid\n", pid);
       return -ENOENT;
@@ -2259,7 +2259,7 @@ static int proc_stat(const char *relpath, struct stat 
*buf)
   pid = (pid_t)tmp;
 
   tcb = nxsched_get_tcb(pid);
-  if (tcb == NULL)
+  if (tcb == NULL || nxsig_kill_target(pid) < 0)
     {
       ferr("ERROR: PID %d is no longer valid\n", pid);
       return -ENOENT;

Reply via email to