This is an automated email from the ASF dual-hosted git repository.

acassis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git


The following commit(s) were added to refs/heads/master by this push:
     new d12c5545a nshlib: Set $! only for a command in the background.
d12c5545a is described below

commit d12c5545aac636e912141441b33152d24f1b71a2
Author: Marco Casaroli <[email protected]>
AuthorDate: Wed Nov 4 01:10:22 2026 +0100

    nshlib: Set $! only for a command in the background.
    
    $! is the PID of the last command that the shell started in the
    background.  NSH changed it after each command: nsh_command() set it to
    the PID of NSH after each NSH command, and a program in the foreground
    set it to its own PID.  So "echo $!" and then "wait $!" waited for NSH
    itself: pthread_join() of the calling thread failed, cmd_wait() did not
    report it, and $? was 0.  After a program in the foreground, "wait $!"
    reported an error for a PID that had exited.
    
    Set np_lastpid only where NSH starts a command in the background.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 nshlib/nsh.h          | 2 +-
 nshlib/nsh_builtin.c  | 2 +-
 nshlib/nsh_command.c  | 5 +----
 nshlib/nsh_fileapps.c | 2 +-
 4 files changed, 4 insertions(+), 7 deletions(-)

diff --git a/nshlib/nsh.h b/nshlib/nsh.h
index 1f98d4bf2..b493a01c9 100644
--- a/nshlib/nsh.h
+++ b/nshlib/nsh.h
@@ -653,7 +653,7 @@ struct nsh_parser_s
   bool     np_redir_in;  /* true: Input from the last command was re-directed 
*/
   bool     np_redir_err; /* true: Error from the last command was re-directed 
*/
   int      np_status;    /* Exit status of the last command */
-  pid_t    np_lastpid;   /* Pid of the last command executed */
+  pid_t    np_lastpid;   /* Pid of the last background command */
 #ifdef NSH_HAVE_VARS
   char     np_pids[32];  /* String representation of the last pid */
   char     np_rcstr[4];  /* String representation of the exit status */
diff --git a/nshlib/nsh_builtin.c b/nshlib/nsh_builtin.c
index cb0ac3af3..37be6d91b 100644
--- a/nshlib/nsh_builtin.c
+++ b/nshlib/nsh_builtin.c
@@ -104,7 +104,6 @@ int nsh_builtin(FAR struct nsh_vtbl_s *vtbl, FAR const char 
*cmd,
        *     foreground
        */
 
-      vtbl->np.np_lastpid = ret;
 #ifdef CONFIG_SCHED_WAITPID
 
       /* CONFIG_SCHED_WAITPID is selected, so we may run the command in
@@ -206,6 +205,7 @@ int nsh_builtin(FAR struct nsh_vtbl_s *vtbl, FAR const char 
*cmd,
         {
           struct sched_param sched;
 
+          vtbl->np.np_lastpid = ret;
           sched_getparam(ret, &sched);
           nsh_output(vtbl, "%s [%d:%d]\n", cmd, ret, sched.sched_priority);
 
diff --git a/nshlib/nsh_command.c b/nshlib/nsh_command.c
index d4e3654a1..b7aa1b085 100644
--- a/nshlib/nsh_command.c
+++ b/nshlib/nsh_command.c
@@ -1283,7 +1283,6 @@ int nsh_command(FAR struct nsh_vtbl_s *vtbl, int argc, 
FAR char *argv[])
   const struct builtin_s *builtin;
   int                     index;
 #endif
-  int                     ret;
 
   /* The form of argv is:
    *
@@ -1349,9 +1348,7 @@ int nsh_command(FAR struct nsh_vtbl_s *vtbl, int argc, 
FAR char *argv[])
         }
     }
 
-  ret = handler(vtbl, argc, argv);
-  vtbl->np.np_lastpid = getpid();
-  return ret;
+  return handler(vtbl, argc, argv);
 }
 
 /****************************************************************************
diff --git a/nshlib/nsh_fileapps.c b/nshlib/nsh_fileapps.c
index 352453628..565d5ed62 100644
--- a/nshlib/nsh_fileapps.c
+++ b/nshlib/nsh_fileapps.c
@@ -279,7 +279,6 @@ int nsh_fileapp(FAR struct nsh_vtbl_s *vtbl, FAR const char 
*cmd,
        *     foreground
        */
 
-      vtbl->np.np_lastpid = pid;
 #ifdef CONFIG_SCHED_WAITPID
       /* CONFIG_SCHED_WAITPID is selected, so we may run the command in
        * foreground unless we were specifically requested to run the command
@@ -374,6 +373,7 @@ int nsh_fileapp(FAR struct nsh_vtbl_s *vtbl, FAR const char 
*cmd,
         {
           struct sched_param sched;
 
+          vtbl->np.np_lastpid = pid;
           sched_getparam(pid, &sched);
           nsh_output(vtbl, "%s [%d:%d]\n", cmd, pid, sched.sched_priority);
 

Reply via email to