]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
Don't call sigtimedwait for scoped_ignore_sigttou
authorPedro Alves <pedro@palves.net>
Thu, 17 Jun 2021 15:23:03 +0000 (16:23 +0100)
committerPedro Alves <pedro@palves.net>
Thu, 17 Jun 2021 18:39:08 +0000 (19:39 +0100)
Because SIGTTOU is sent to the whole process instead of to a specific
thread, consuming a pending SIGTTOU in the destructor of
scoped_ignore_sigttou could consume a SIGTTOU signal raised due to
actions done by some other thread.  Simply avoid sigtimedwait in
scoped_ignore_sigttou, thus plugging the race.  This works because we
know that when the thread writes to the terminal and the signal is
blocked, the kernel does not raise the signal at all.

Tested on GNU/Linux, Solaris 11 and FreeBSD.

gdb/ChangeLog:
yyyy-mm-dd  Pedro Alves  <pedro@palves.net>

* scoped_ignore_signal.h (scoped_ignore_signal): Add
ConsumePending template parameter.
(scoped_ignore_signal::~scoped_ignore_signal): Skip calling
sigtimedwait if ConsumePending is false.
(scoped_ignore_sigpipe): Initialize with ConsumePending=true.
* scoped_ignore_sigttou.h (scoped_ignore_sigttou)
<m_ignore_signal>: Initialize with ConsumePending=false.

Change-Id: I92f754dbc45c45819dce2ce68b8c067d8d5c61b1

gdb/ChangeLog
gdbsupport/scoped_ignore_signal.h
gdbsupport/scoped_ignore_sigttou.h

index e4e58173ee6b6e6c9a0edf2943b7493fdeac38f0..c70f6ef5329f8eed5f3d921fa906e1cc762008fc 100644 (file)
@@ -1,3 +1,13 @@
+2021-06-17  Pedro Alves  <pedro@palves.net>
+
+       * scoped_ignore_signal.h (scoped_ignore_signal): Add
+       ConsumePending template parameter.
+       (scoped_ignore_signal::~scoped_ignore_signal): Skip calling
+       sigtimedwait if ConsumePending is false.
+       (scoped_ignore_sigpipe): Initialize with ConsumePending=true.
+       * scoped_ignore_sigttou.h (scoped_ignore_sigttou)
+       <m_ignore_signal>: Initialize with ConsumePending=false.
+
 2021-06-17  Pedro Alves  <pedro@palves.net>
 
        * Makefile.in (SELFTESTS_SRCS): Add
index 55a921cb332891a4a92d265533bddb7f34ef59d2..a14c96779bf214d6a407dc2efcb8e3226c86347b 100644 (file)
 /* RAII class used to ignore a signal in a scope.  If sigprocmask is
    supported, then the signal is only ignored by the calling thread.
    Otherwise, the signal disposition is set to SIG_IGN, which affects
-   the whole process.  */
-
-template <int Sig>
+   the whole process.  If ConsumePending is true, the destructor
+   consumes a pending Sig.  SIGPIPE for example is queued on the
+   thread even if blocked at the time the pipe is written to.  SIGTTOU
+   OTOH is not raised at all if the thread writing to the terminal has
+   it blocked.  Because SIGTTOU is sent to the whole process instead
+   of to a specific thread, consuming a pending SIGTTOU in the
+   destructor could consume a signal raised due to actions done by
+   some other thread.  */
+
+template <int Sig, bool ConsumePending>
 class scoped_ignore_signal
 {
 public:
@@ -58,7 +65,8 @@ public:
 
        /* If we got a pending Sig signal, consume it before
           unblocking.  */
-       sigtimedwait (&set, nullptr, &zero_timeout);
+       if (ConsumePending)
+         sigtimedwait (&set, nullptr, &zero_timeout);
 
        sigprocmask (SIG_UNBLOCK, &set, nullptr);
       }
@@ -89,7 +97,7 @@ struct scoped_ignore_signal_nop
 };
 
 #ifdef SIGPIPE
-using scoped_ignore_sigpipe = scoped_ignore_signal<SIGPIPE>;
+using scoped_ignore_sigpipe = scoped_ignore_signal<SIGPIPE, true>;
 #else
 using scoped_ignore_sigpipe = scoped_ignore_signal_nop;
 #endif
index 1fc8f80d7fd72db877e204e43dbca1e9205cafd6..5695c5db9054b4f0fa34f153b94ff24bdc118e71 100644 (file)
@@ -75,7 +75,7 @@ public:
   DISABLE_COPY_AND_ASSIGN (scoped_ignore_sigttou);
 
 private:
-  lazy_init<scoped_ignore_signal<SIGTTOU>> m_ignore_signal;
+  lazy_init<scoped_ignore_signal<SIGTTOU, false>> m_ignore_signal;
 };
 
 #else