From patchwork Thu Dec 19 22:56:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aurelien Jarno X-Patchwork-Id: 103478 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 31FBE3858C41 for ; Thu, 19 Dec 2024 22:59:26 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 31FBE3858C41 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, secure) header.d=aurel32.net header.i=@aurel32.net header.a=rsa-sha256 header.s=202004.hall header.b=g1PFqwyj X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from hall.aurel32.net (hall.aurel32.net [IPv6:2001:bc8:30d7:100::1]) by sourceware.org (Postfix) with ESMTPS id 1630B3858D20 for ; Thu, 19 Dec 2024 22:58:46 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 1630B3858D20 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=aurel32.net Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=aurel32.net ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 1630B3858D20 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2001:bc8:30d7:100::1 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1734649126; cv=none; b=SF47oK8w++DhcX3r4RfvyJ6uIfJrc/UaidS5ResuS3mZh/Kgh9vEEfrFE2P8Fjet7eFcNift4CCtBysYpW+7gLCk6u1xzUzsz0CQ7IH9nQzD2eHSYWY+euzF4ihNfpQuFN0EnmcHxEKUrtXyWLuH0aVsMpz1cQiSgMcCuiMty4M= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1734649126; c=relaxed/simple; bh=OhkBPYqT3gr+Ppc8EVfhpFiRDTSNjhV/MbrAtZKsWxE=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=ap5wkfkPu78qRtipwGmaJC4oe5xBqcVwLSLUr+l0xjLF7BEF8EirI1p1YCZ1dz/J5IJAmBEU+mIq8dvhlYVR2JvcejhNaJYnI+T/5VdNYu4tBHyBVPR4tfoV4jxo8YdqpFHX8mZ7NdxlQ4DPKRVLzCU96Aj4DW5Fh+E2pNNtFKY= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1630B3858D20 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=aurel32.net ; s=202004.hall; h=Content-Transfer-Encoding:MIME-Version:Message-ID:Date: Subject:Cc:To:From:Content-Type:From:Reply-To:Subject:Content-ID: Content-Description:In-Reply-To:References:X-Debbugs-Cc; bh=R5bMOkUaa0iT7LOE/CcmoUl5yCnTnciVzJKJwIuYaHs=; b=g1PFqwyjwoFXngOl/oruAuuI8Z UbGqSR+zo244QqxfvSnaF8weiN4b/LydJ9cXVH+POQaEhQjluFFyZ8YCKBGfBoUSvPu8bWSHYX0fZ Oaj0FjAqGv6icY5/uLMvi+wQAqlrONHjmfNmeUY2n3bbzWygKWMesKk0i2Mg0MzCVpfAUyz82bu/Y nt/F0sWlAq8Bk2o58yEZLGytaS71S7wlVlb1r+Cdhu3tkPC9xY1NgtFZAc4fKDLvnnal7811ZqklD jq8OsUiOBF/GekP+bFcLMOmPiauLTWXvOzasotUn6r16anuoDV6l97B0vzTzu/DTBRSWMKL5Blwes pTAve7XQ==; Received: from ohm.aurel32.net ([2001:bc8:30d7:111::2] helo=ohm.rr44.fr) by hall.aurel32.net with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1tOPTg-007mNM-1P; Thu, 19 Dec 2024 23:58:44 +0100 From: Aurelien Jarno To: libc-alpha@sourceware.org Cc: Aurelien Jarno Subject: [PATCH v2] posix: fix system when a child cannot be created [BZ #32450] Date: Thu, 19 Dec 2024 23:56:11 +0100 Message-ID: <20241219225840.1200611-1-aurelien@aurel32.net> X-Mailer: git-send-email 2.45.2 MIME-Version: 1.0 X-Spam-Status: No, score=-13.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_PASS, 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.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org POSIX states that "if a child process cannot be created, or if the termination status for the command language interpreter cannot be obtained, system() shall return -1 and set errno to indicate the error." In the glibc implementation it could happen when posix_spawn fails, which happens when the underlying fork, vfork, or clone call fails. They could fail with EAGAIN and ENOMEM. Resolves: BZ #32450 Signed-off-by: Aurelien Jarno Reviewed-by: Adhemerval Zanella --- stdlib/tst-system.c | 21 +++++++++++++++++++++ sysdeps/posix/system.c | 10 +++++++--- 2 files changed, 28 insertions(+), 3 deletions(-) v1 -> v2: - fix indentation - return FAIL_EXIT1 if getrlimit or setrlimit fail - test both EAGAIN and ENOMEM. diff --git a/stdlib/tst-system.c b/stdlib/tst-system.c index 1581a06e68..d37902088a 100644 --- a/stdlib/tst-system.c +++ b/stdlib/tst-system.c @@ -20,6 +20,7 @@ #include #include #include +#include #include #include @@ -194,6 +195,26 @@ do_test (void) xpthread_join (long_sleep_thread); } + { + struct rlimit rlimit_orig, rlimit_new; + + if (getrlimit (RLIMIT_NPROC, &rlimit_orig) != 0) + FAIL_EXIT1 ("getrlimit (RLIMIT_NPROC) failed: %m"); + + /* Force failure for the system call */ + rlimit_new.rlim_cur = 0; + rlimit_new.rlim_max = rlimit_orig.rlim_max; + + if (setrlimit (RLIMIT_NPROC, &rlimit_new) != 0) + FAIL_EXIT1 ("setrlimit (RLIMIT_NPROC) failed: %m"); + + TEST_COMPARE (system (""), -1); + + /* Restore NPROC limit */ + if (setrlimit (RLIMIT_NPROC, &rlimit_orig) != 0) + FAIL_EXIT1 ("setrlimit (RLIMIT_NPROC) failed: %m"); + } + TEST_COMPARE (system (""), 0); return 0; diff --git a/sysdeps/posix/system.c b/sysdeps/posix/system.c index be32704280..f3e173e465 100644 --- a/sysdeps/posix/system.c +++ b/sysdeps/posix/system.c @@ -175,10 +175,14 @@ do_system (const char *line) __libc_cleanup_region_end (0); #endif } + else if (ret == EAGAIN || ret == ENOMEM) + /* POSIX states that failure to create a child process should + return -1. */ + status = -1; else - /* POSIX states that failure to execute the shell should return - as if the shell had terminated using _exit(127). */ - status = W_EXITCODE (127, 0); + /* POSIX states that failure to execute the shell should return + as if the shell had terminated using _exit(127). */ + status = W_EXITCODE (127, 0); /* sigaction can not fail with SIGINT/SIGQUIT used with old disposition. Same applies for sigprocmask. */