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-apps.git

commit 1f2505c78e0010d4b8795eb19d47d105abd70565
Author: hongfengchen <[email protected]>
AuthorDate: Mon Apr 13 17:14:55 2026 +0800

    rexec: fix premature exit when stdin is a pipe
    
    When called via pipe (e.g. from dumpstate HAL), stdin POLLHUP
    triggers immediately since the write end is closed. Previously
    this caused rexec to exit before the remote command completed,
    resulting in truncated output.
    
    - stdin end: stop monitoring stdin, keep waiting for sock
    - sock end: exit immediately (remote command done)
    - Ctrl+C still works via SIGINT
    
    Signed-off-by: hongfengchen <[email protected]>
    Assisted-by: Kiro:claude-opus-4
---
 netutils/rexec/rexec.c | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)

diff --git a/netutils/rexec/rexec.c b/netutils/rexec/rexec.c
index 519790c4a..590dd9824 100644
--- a/netutils/rexec/rexec.c
+++ b/netutils/rexec/rexec.c
@@ -103,9 +103,15 @@ static int do_rexec(FAR struct rexec_arg_s *arg)
   fds[1].fd = STDIN_FILENO;
   fds[1].events = POLLIN;
 
-  while (1)
+  /* Relay data between stdin/sock until remote command completes.
+   * - sock end (remote command done): exit immediately.
+   * - stdin end (pipe closed or EOF): stop monitoring stdin,
+   *   keep waiting for sock so remote output is not lost.
+   */
+
+  while (fds[0].fd >= 0 || fds[1].fd >= 0)
     {
-      ret = poll(fds, 2, -1);
+      ret = poll(fds, fds[1].fd >= 0 ? 2 : 1, -1);
       if (ret <= 0)
         {
           continue;
@@ -126,25 +132,31 @@ static int do_rexec(FAR struct rexec_arg_s *arg)
             }
         }
 
+      if (fds[0].revents & POLLHUP)
+        {
+          break;
+        }
+
       if (fds[1].revents & POLLIN)
         {
           ret = read(STDIN_FILENO, buffer, REXEC_BUFSIZE);
           if (ret <= 0)
             {
-              break;
+              fds[1].fd = -1;
+              continue;
             }
 
           ret = write(sock, buffer, ret);
           if (ret < 0)
             {
-              break;
+              fds[1].fd = -1;
+              continue;
             }
         }
 
-      if (((fds[0].revents | fds[1].revents) & POLLHUP) &&
-          ((fds[0].revents | fds[1].revents) & POLLIN) == 0)
+      if (fds[1].revents & POLLHUP)
         {
-          break;
+          fds[1].fd = -1;
         }
     }
 

Reply via email to