about summary refs log tree commit diff
path: root/stdlib
diff options
context:
space:
mode:
authorAdam Yi <ayi@janestreet.com>2023-03-07 07:30:02 -0500
committerFlorian Weimer <fweimer@redhat.com>2023-04-28 16:16:40 +0200
commit48af2676a7c56ecc2511ce48a7550edbc48ff1d4 (patch)
tree2c47218f884288f0537f4fafe59164359a924af0 /stdlib
parent95c5bddc9fbfda12220124dbf13750f97bae4e1d (diff)
downloadglibc-48af2676a7c56ecc2511ce48a7550edbc48ff1d4.tar.gz
glibc-48af2676a7c56ecc2511ce48a7550edbc48ff1d4.tar.xz
glibc-48af2676a7c56ecc2511ce48a7550edbc48ff1d4.zip
posix: Fix system blocks SIGCHLD erroneously [BZ #30163]
Fix bug that SIGCHLD is erroneously blocked forever in the following
scenario:

1. Thread A calls system but hasn't returned yet
2. Thread B calls another system but returns

SIGCHLD would be blocked forever in thread B after its system() returns,
even after the system() in thread A returns.

Although POSIX does not require, glibc system implementation aims to be
thread and cancellation safe. This bug was introduced in
5fb7fc96350575c9adb1316833e48ca11553be49 when we moved reverting signal
mask to happen when the last concurrently running system returns,
despite that signal mask is per thread. This commit reverts this logic
and adds a test.

Signed-off-by: Adam Yi <ayi@janestreet.com>
Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
(cherry picked from commit 436a604b7dc741fc76b5a6704c6cd8bb178518e7)
Diffstat (limited to 'stdlib')
-rw-r--r--stdlib/tst-system.c26
1 files changed, 26 insertions, 0 deletions
diff --git a/stdlib/tst-system.c b/stdlib/tst-system.c
index f7fa74b2a6..5e0c79475f 100644
--- a/stdlib/tst-system.c
+++ b/stdlib/tst-system.c
@@ -25,6 +25,7 @@
 #include <support/check.h>
 #include <support/temp_file.h>
 #include <support/support.h>
+#include <support/xthread.h>
 #include <support/xunistd.h>
 
 static char *tmpdir;
@@ -71,6 +72,20 @@ call_system (void *closure)
     }
 }
 
+static void *
+sleep_and_check_sigchld (void *closure)
+{
+  double *seconds = (double *) closure;
+  char cmd[namemax];
+  sprintf (cmd, "sleep %lf" , *seconds);
+  TEST_COMPARE (system (cmd), 0);
+
+  sigset_t blocked = {0};
+  TEST_COMPARE (sigprocmask (SIG_BLOCK, NULL, &blocked), 0);
+  TEST_COMPARE (sigismember (&blocked, SIGCHLD), 0);
+  return NULL;
+}
+
 static int
 do_test (void)
 {
@@ -154,6 +169,17 @@ do_test (void)
     xchmod (_PATH_BSHELL, st.st_mode);
   }
 
+  {
+    pthread_t long_sleep_thread = xpthread_create (NULL,
+                                                   sleep_and_check_sigchld,
+                                                   &(double) { 0.2 });
+    pthread_t short_sleep_thread = xpthread_create (NULL,
+                                                    sleep_and_check_sigchld,
+                                                    &(double) { 0.1 });
+    xpthread_join (short_sleep_thread);
+    xpthread_join (long_sleep_thread);
+  }
+
   TEST_COMPARE (system (""), 0);
 
   return 0;