From patchwork Tue Dec 6 16:45:03 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 61597 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B3F733846997 for ; Tue, 6 Dec 2022 16:46:04 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B3F733846997 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1670345164; bh=B700k5mbzkDGDjagT9h+6o6rsWua9cYx4abMAmgS4Rw=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=aq3Zp30GADLENqjiRksZt6wNLoc311cXGoE/dxmEPCo94vs3UI9z95KMxuXiheKLJ D+1iGfEtZZZtkh1d5UkGQ3T4QNFkLtJQvhOGvRU5LMYozbHRMHHI9AvaEYfseJHyEK vjTi3sx1G/nT8EL/Rkvk/BKhUOyDoRlnjZZknRvQ= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-oa1-x32.google.com (mail-oa1-x32.google.com [IPv6:2001:4860:4864:20::32]) by sourceware.org (Postfix) with ESMTPS id 0EAE73871F96 for ; Tue, 6 Dec 2022 16:45:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 0EAE73871F96 Received: by mail-oa1-x32.google.com with SMTP id 586e51a60fabf-14449b7814bso13125549fac.3 for ; Tue, 06 Dec 2022 08:45:08 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=B700k5mbzkDGDjagT9h+6o6rsWua9cYx4abMAmgS4Rw=; b=2NjeMl4LMeJlT509wW0r0bWKtqjE5WBKQu72vSUlRYUYkGDR1b7MoQWoHfLTuJjDo8 +6XqRwBLEkUgt+pMREvrivIqppTcLbDep2TuWzSi6yFHpKDrmByI8LCKQKv/Yr1AaQEA 6yOxGqCMxoRBB9EHkmzDLoKyUeDoD6ZJNYbCXh75q2UVXcF7llWn6T6ZN+/34HIVsuGw TbYyfxMqzACLtF8XpEvqUkahHaZNDGaj6c8UVzUiXYKtCHkl3ok8RAIuJs3r5X3ddGzQ c1TuHw2HYpJGXS9905aZ6KQuXzdSWqloGpOGyincAcmxrq5igEBVka6jelf1j4/lZMxB BBYw== X-Gm-Message-State: ANoB5pnT+xfke2sljSUAywVx4ei/0GHObziQT4VaOI0h4WqMUAeBOJ5r 2mdONf1Jco30KIN4DNTg+8lOke7Bzu04yPtOq10= X-Google-Smtp-Source: AA0mqf6BXuYfS8nbsFj/R7sItYrMdfo5OubwDFcv/iqfFLkpE4TBskhSAI6sDxmzLWRHoNMQvfwBzQ== X-Received: by 2002:a05:6870:6b8d:b0:144:a2de:1075 with SMTP id ms13-20020a0568706b8d00b00144a2de1075mr4963261oab.194.1670345107094; Tue, 06 Dec 2022 08:45:07 -0800 (PST) Received: from mandiga.. ([2804:1b3:a7c2:6793:6a7:7646:7f3f:684e]) by smtp.gmail.com with ESMTPSA id q204-20020acaf2d5000000b00354d8589a15sm8445490oih.45.2022.12.06.08.45.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Dec 2022 08:45:06 -0800 (PST) To: libc-alpha@sourceware.org, arnaud.lb@gmail.com, Andreas Schwab Subject: [PATCH] stdio: Do not ignore posix_spawn error on popen (BZ #29016) Date: Tue, 6 Dec 2022 13:45:03 -0300 Message-Id: <20221206164503.2534492-1-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Spam-Status: No, score=-12.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GB_TO_NAME_FREEMAIL, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Netto Reply-To: Adhemerval Zanella Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" To correctly return error in case of default shell is not present. Checked on x86_64-linux-gnu. --- libio/iopopen.c | 38 ++++++++++++++++++++++---------------- stdio-common/Makefile | 3 +++ stdio-common/tst-popen3.c | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 16 deletions(-) create mode 100644 stdio-common/tst-popen3.c diff --git a/libio/iopopen.c b/libio/iopopen.c index 06778cf110..e4be6c9779 100644 --- a/libio/iopopen.c +++ b/libio/iopopen.c @@ -66,11 +66,12 @@ unlock (void *not_used) be close (by transversing the proc_file_chain list) and the insertion of a new one after a successful posix_spawn this function should be called with proc_file_chain_lock acquired. */ -static bool +static int spawn_process (posix_spawn_file_actions_t *fa, FILE *fp, const char *command, int do_cloexec, int pipe_fds[2], int parent_end, int child_end, int child_pipe_fd) { + int err = 0; for (struct _IO_proc_file *p = proc_file_chain; p; p = p->next) { @@ -79,15 +80,19 @@ spawn_process (posix_spawn_file_actions_t *fa, FILE *fp, const char *command, /* If any stream from previous popen() calls has fileno child_pipe_fd, it has been already closed by the adddup2 action above. */ - if (fd != child_pipe_fd - && __posix_spawn_file_actions_addclose (fa, fd) != 0) - return false; + if (fd != child_pipe_fd) + { + err = __posix_spawn_file_actions_addclose (fa, fd); + if (err != 0) + return err; + } } - if (__posix_spawn (&((_IO_proc_file *) fp)->pid, _PATH_BSHELL, fa, 0, - (char *const[]){ (char*) "sh", (char*) "-c", - (char *) command, NULL }, __environ) != 0) - return false; + err = __posix_spawn (&((_IO_proc_file *) fp)->pid, _PATH_BSHELL, fa, 0, + (char *const[]){ (char*) "sh", (char*) "-c", + (char *) command, NULL }, __environ); + if (err != 0) + return err; __close_nocancel (pipe_fds[child_end]); @@ -101,7 +106,7 @@ spawn_process (posix_spawn_file_actions_t *fa, FILE *fp, const char *command, ((_IO_proc_file *) fp)->next = proc_file_chain; proc_file_chain = (_IO_proc_file *) fp; - return true; + return 0; } FILE * @@ -112,7 +117,7 @@ _IO_new_proc_open (FILE *fp, const char *command, const char *mode) int parent_end, child_end; int pipe_fds[2]; int child_pipe_fd; - bool spawn_ok; + int err; int do_read = 0; int do_write = 0; @@ -185,16 +190,17 @@ _IO_new_proc_open (FILE *fp, const char *command, const char *mode) pipe_fds[child_end] = tmp; } - if (__posix_spawn_file_actions_adddup2 (&fa, pipe_fds[child_end], - child_pipe_fd) != 0) + err = __posix_spawn_file_actions_adddup2 (&fa, pipe_fds[child_end], + child_pipe_fd); + if (err != 0) goto spawn_failure; #ifdef _IO_MTSAFE_IO _IO_cleanup_region_start_noarg (unlock); _IO_lock_lock (proc_file_chain_lock); #endif - spawn_ok = spawn_process (&fa, fp, command, do_cloexec, pipe_fds, - parent_end, child_end, child_pipe_fd); + err = spawn_process (&fa, fp, command, do_cloexec, pipe_fds, parent_end, + child_end, child_pipe_fd); #ifdef _IO_MTSAFE_IO _IO_lock_unlock (proc_file_chain_lock); _IO_cleanup_region_end (0); @@ -202,12 +208,12 @@ _IO_new_proc_open (FILE *fp, const char *command, const char *mode) __posix_spawn_file_actions_destroy (&fa); - if (!spawn_ok) + if (err != 0) { + __set_errno (err); spawn_failure: __close_nocancel (pipe_fds[child_end]); __close_nocancel (pipe_fds[parent_end]); - __set_errno (ENOMEM); return NULL; } diff --git a/stdio-common/Makefile b/stdio-common/Makefile index fe57dbdf56..9a9a37ae08 100644 --- a/stdio-common/Makefile +++ b/stdio-common/Makefile @@ -215,6 +215,9 @@ tests := \ xbug \ # tests +tests-container += \ + tst-popen3 + generated += \ errlist-data-aux-shared.S \ errlist-data-aux.S \ diff --git a/stdio-common/tst-popen3.c b/stdio-common/tst-popen3.c new file mode 100644 index 0000000000..add5e97c09 --- /dev/null +++ b/stdio-common/tst-popen3.c @@ -0,0 +1,38 @@ +/* Check if popen return a correct error code if the default shell does not + exists (BZ#29016). + + Copyright (C) 2022 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include +#include +#include + +int +do_test (void) +{ + xunlink (_PATH_BSHELL); + + FILE *f = popen ("/non-existent", "r"); + TEST_VERIFY (f == NULL); + TEST_COMPARE (errno, ENOENT); + return 0; +} + +#include