aboutsummaryrefslogtreecommitdiff
path: root/stdlib
diff options
context:
space:
mode:
authorAurelien Jarno <aurelien@aurel32.net>2024-12-19 23:55:15 +0100
committerAurelien Jarno <aurelien@aurel32.net>2024-12-20 22:57:06 +0100
commit6fd215d6ae9a4a6e75f7ea18d89db6a10f158eaf (patch)
treeb3d47d641b9306d97618c4327398d7dc12358b79 /stdlib
parent034cd675286db07110a773fdb6a857285485d2d4 (diff)
downloadglibc-6fd215d6ae9a4a6e75f7ea18d89db6a10f158eaf.zip
glibc-6fd215d6ae9a4a6e75f7ea18d89db6a10f158eaf.tar.gz
glibc-6fd215d6ae9a4a6e75f7ea18d89db6a10f158eaf.tar.bz2
posix: fix system when a child cannot be created [BZ #32450]
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 <aurelien@aurel32.net> Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Diffstat (limited to 'stdlib')
-rw-r--r--stdlib/tst-system.c21
1 files changed, 21 insertions, 0 deletions
diff --git a/stdlib/tst-system.c b/stdlib/tst-system.c
index 1581a06..d379020 100644
--- a/stdlib/tst-system.c
+++ b/stdlib/tst-system.c
@@ -20,6 +20,7 @@
#include <string.h>
#include <signal.h>
#include <paths.h>
+#include <sys/resource.h>
#include <support/capture_subprocess.h>
#include <support/check.h>
@@ -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;