public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Adam Yi <ayi@janestreet.com>
To: libc-alpha@sourceware.org
Cc: adhemerval.zanella@linaro.org, Adam Yi <i@adamyi.com>
Subject: [PATCH] posix: Fix system blocks SIGCHLD erroneously [BZ #30163]
Date: Sat, 25 Feb 2023 01:31:10 +0800	[thread overview]
Message-ID: <CAHwrs-DiqV0SsfHj+B6+PgUJ=6af5bR6+QrYXS0A+yjFoE0SXQ@mail.gmail.com> (raw)

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.
---
 stdlib/tst-system.c       | 27 +++++++++++++++++++++++++++
 support/shell-container.c | 10 ++++++++++
 sysdeps/posix/system.c    |  6 +++---
 3 files changed, 40 insertions(+), 3 deletions(-)

diff --git a/stdlib/tst-system.c b/stdlib/tst-system.c
index 634acfe264..be95c2de44 100644
--- a/stdlib/tst-system.c
+++ b/stdlib/tst-system.c
@@ -20,6 +20,8 @@
 #include <string.h>
 #include <signal.h>
 #include <paths.h>
+#include <pthread.h>
+#include <inttypes.h>

 #include <support/capture_subprocess.h>
 #include <support/check.h>
@@ -71,6 +73,19 @@ call_system (void *closure)
     }
 }

+static void *
+sleep_and_check_sigchld (void *seconds)
+{
+  char cmd[namemax];
+  sprintf(cmd, "sleep %" PRIdPTR, (intptr_t) 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,18 @@ do_test (void)
     xchmod (_PATH_BSHELL, st.st_mode);
   }

+  {
+    pthread_t long_sleep_thread, short_sleep_thread;
+
+    TEST_COMPARE (pthread_create (&long_sleep_thread, NULL,
+                  sleep_and_check_sigchld, (void *) 2), 0);
+    TEST_COMPARE (pthread_create (&short_sleep_thread, NULL,
+                  sleep_and_check_sigchld, (void *) 1), 0);
+
+    TEST_COMPARE (pthread_join (short_sleep_thread, NULL), 0);
+    TEST_COMPARE (pthread_join (long_sleep_thread, NULL), 0);
+  }
+
   TEST_COMPARE (system (""), 0);

   return 0;
diff --git a/support/shell-container.c b/support/shell-container.c
index e9ac9b6d04..083426550b 100644
--- a/support/shell-container.c
+++ b/support/shell-container.c
@@ -171,6 +171,15 @@ kill_func (char **argv)
   return 0;
 }

+/* Emulate the "/bin/sleep" command.  Options are ignored.  */
+static int
+sleep_func (char **argv)
+{
+  int secs = atoi (argv[0]);
+  sleep (secs);
+  return 0;
+}
+
 /* This is a list of all the built-in commands we understand.  */
 static struct {
   const char *name;
@@ -181,6 +190,7 @@ static struct {
   { "cp", copy_func },
   { "exit", exit_func },
   { "kill", kill_func },
+  { "sleep", sleep_func },
   { NULL, NULL }
 };

diff --git a/sysdeps/posix/system.c b/sysdeps/posix/system.c
index 2335a99184..d77720a625 100644
--- a/sysdeps/posix/system.c
+++ b/sysdeps/posix/system.c
@@ -179,16 +179,16 @@ do_system (const char *line)
       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.  */
   DO_LOCK ();
   if (SUB_REF () == 0)
     {
-      /* sigaction can not fail with SIGINT/SIGQUIT used with old
- disposition.  Same applies for sigprocmask.  */
       __sigaction (SIGINT, &intr, NULL);
       __sigaction (SIGQUIT, &quit, NULL);
-      __sigprocmask (SIG_SETMASK, &omask, NULL);
     }
   DO_UNLOCK ();
+  __sigprocmask (SIG_SETMASK, &omask, NULL);

   if (ret != 0)
     __set_errno (ret);
-- 
2.31.1

             reply	other threads:[~2023-02-24 17:31 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-24 17:31 Adam Yi [this message]
2023-02-24 15:27 ` [PATCH v3] " Adam Yi
2023-03-02 16:42   ` Adhemerval Zanella Netto
2023-03-03  5:11     ` Adam Yi
2023-03-03 12:00       ` Adhemerval Zanella Netto
2023-03-06  4:27         ` [PATCH v4] " Adam Yi
2023-03-06  9:04           ` Andreas Schwab
2023-03-07  1:52             ` [PATCH v5] " Adam Yi
2023-03-07 12:18               ` Adhemerval Zanella Netto
2023-03-07 12:33                 ` Florian Weimer
2023-03-07 12:33                   ` Adam Yi
2023-03-07 12:30                     ` [PATCH v6] " Adam Yi
2023-03-07 12:55                       ` Adhemerval Zanella Netto
2023-03-07 22:50                         ` Joseph Myers
2023-03-08  8:17                           ` Adam Yi
2023-02-24 18:00 ` [PATCH] " Adam Yi
2023-02-24 20:18   ` Adhemerval Zanella Netto
2023-03-01  2:36     ` Adam Yi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAHwrs-DiqV0SsfHj+B6+PgUJ=6af5bR6+QrYXS0A+yjFoE0SXQ@mail.gmail.com' \
    --to=ayi@janestreet.com \
    --cc=adhemerval.zanella@linaro.org \
    --cc=i@adamyi.com \
    --cc=libc-alpha@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).