When executing commands with pipes followed by && or || operators from
input files, the exit status was not properly propagated. This caused
shell operators to always receive success status, leading to incorrect
conditional execution.

  crash> sys | grep CPUS && echo yes || echo no
          CPUS: 256
  yes
  crash> sys1 | grep CPUS && echo yes || echo no
  crash: command not found: sys1
  no

  crash> cat test
  sys | grep CPUS && echo yes || echo no
  sys1 | grep CPUS && echo yes || echo no
  crash> < ./test
  crash> sys | grep CPUS && echo yes || echo no
          CPUS: 256
  yes
  crash> sys1 | grep CPUS && echo yes || echo no
  crash: command not found: sys1
  yes

The root cause was in restore_ifile_sanity() which used close() instead
of pclose() to close the pipe.

Also fix the same issue in restore_sanity() for both pc->pipe and
pc->ifile_pipe which were opened with popen() but incorrectly closed
with close().

Signed-off-by: neilfsun <[email protected]>
Signed-off-by: Feng Sun <[email protected]>
---
 cmdline.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/cmdline.c b/cmdline.c
index 030ae56..e9fe92a 100644
--- a/cmdline.c
+++ b/cmdline.c
@@ -1108,7 +1108,7 @@ restore_sanity(void)
                pc->stdpipe_pid = 0;
         }
        if (pc->pipe) {
-               close(fileno(pc->pipe));
+               pclose(pc->pipe);
                pc->pipe = NULL;
                console("wait for redirect %d->%d to finish...\n",
                        pc->pipe_shell_pid, pc->pipe_pid);
@@ -1127,7 +1127,7 @@ restore_sanity(void)
        }
        if (pc->ifile_pipe) {
                fflush(pc->ifile_pipe);
-               close(fileno(pc->ifile_pipe));
+               pclose(pc->ifile_pipe);
                pc->ifile_pipe = NULL;
                if (pc->pipe_pid &&
                    ((pc->redirect & (PIPE_OPTIONS|REDIRECT_PID_KNOWN)) ==
@@ -1259,7 +1259,7 @@ restore_ifile_sanity(void)
        pc->flags &= ~IFILE_ERROR;
 
         if (pc->ifile_pipe) {
-               close(fileno(pc->ifile_pipe));
+               pclose(pc->ifile_pipe);
                 pc->ifile_pipe = NULL;
         }
 
-- 
2.50.1
--
Crash-utility mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://${domain_name}/admin/lists/devel.lists.crash-utility.osci.io/
Contribution Guidelines: https://github.com/crash-utility/crash/wiki

Reply via email to