Sorry, update the correct subject.
Lianbo
On 2/11/26 5:18 PM, Lianbo Jiang wrote:
On 2/11/26 4:08 PM, Lianbo Jiang wrote:
> Date: Thu, 4 Dec 2025 16:46:41 +0800
> From: neilfsun<loyou85(a)gmail.com>
> Subject: [Crash-utility] [PATCH] Fix exit status handling for piped
> commands in input files
> To:devel@lists.crash-utility.osci.io
> Cc: neilfsun<neilfsun(a)tencent.com>, Feng Sun<loyou85(a)gmail.com>
> Message-ID:<20251204084641.31372-1-neilfsun@tencent.com>
>
> 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<neilfsun(a)tencent.com>
> Signed-off-by: Feng Sun<loyou85(a)gmail.com>
> ---
> 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;
> }
Similarly, the close(fileno(pc->stdpipe)) should be replaced with
fclose(pc->stdpipe), right?
static void
restore_sanity(void)
{
int fd, waitstatus;
struct extension_table *ext;
struct command_table_entry *cp;
if (pc->stdpipe) {
close(fileno(pc->stdpipe));
BTW: the pc->stdpipe is opened with fdopen(). Please see the
setup_stdpipe().
Lianbo
> 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