Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Pedro Alves <pedro@palves.net>
To: gdb-patches@sourceware.org
Subject: [PATCH 4/4] Add a unit test for scoped_ignore_sigpipe
Date: Tue, 15 Jun 2021 12:14:29 +0100	[thread overview]
Message-ID: <20210615111429.1879286-5-pedro@palves.net> (raw)
In-Reply-To: <20210615111429.1879286-1-pedro@palves.net>

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

	* Makefile.in (SELFTESTS_SRCS): Add
	unittests/scoped_ignore_signal-selftests.c.
	* unittests/scoped_ignore_signal-selftests.c: New.

Change-Id: Idce24aa9432a3f1eb7065bc9aa030b1d0d7dcad5
---
 gdb/Makefile.in                               |   1 +
 .../scoped_ignore_signal-selftests.c          | 125 ++++++++++++++++++
 2 files changed, 126 insertions(+)
 create mode 100644 gdb/unittests/scoped_ignore_signal-selftests.c

diff --git a/gdb/Makefile.in b/gdb/Makefile.in
index 881ebde8fb0..1bc97885536 100644
--- a/gdb/Makefile.in
+++ b/gdb/Makefile.in
@@ -461,6 +461,7 @@ SELFTESTS_SRCS = \
 	unittests/mkdir-recursive-selftests.c \
 	unittests/rsp-low-selftests.c \
 	unittests/scoped_fd-selftests.c \
+	unittests/scoped_ignore_signal-selftests.c \
 	unittests/scoped_mmap-selftests.c \
 	unittests/scoped_restore-selftests.c \
 	unittests/search-memory-selftests.c \
diff --git a/gdb/unittests/scoped_ignore_signal-selftests.c b/gdb/unittests/scoped_ignore_signal-selftests.c
new file mode 100644
index 00000000000..f727b464567
--- /dev/null
+++ b/gdb/unittests/scoped_ignore_signal-selftests.c
@@ -0,0 +1,125 @@
+/* Self tests for scoped_ignored_signal for GDB, the GNU debugger.
+
+   Copyright (C) 2021 Free Software Foundation, Inc.
+
+   This file is part of GDB.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#include "defs.h"
+#include "gdbsupport/scoped_ignore_signal.h"
+#include "gdbsupport/selftest.h"
+#include "gdbsupport/scope-exit.h"
+#include <unistd.h>
+#include <signal.h>
+
+namespace selftests {
+namespace scoped_ignore_sig {
+
+#ifdef SIGPIPE
+
+/* True if the SIGPIPE handler ran.  */
+static sig_atomic_t got_sigpipe = 0;
+
+/* SIGPIPE handler for testing.  */
+
+static void
+handle_sigpipe (int)
+{
+  got_sigpipe = 1;
+}
+
+/* Test scoped_ignore_sigpipe.  */
+
+static void
+test_sigpipe ()
+{
+  auto *osig = signal (SIGPIPE, handle_sigpipe);
+  SCOPE_EXIT { signal (SIGPIPE, osig); };
+
+#ifdef HAVE_SIGPROCMASK
+  /* Make sure SIGPIPE isn't blocked.  */
+  sigset_t set, old_state;
+  sigemptyset (&set);
+  sigaddset (&set, SIGPIPE);
+  sigprocmask (SIG_UNBLOCK, &set, &old_state);
+  SCOPE_EXIT { sigprocmask (SIG_SETMASK, &old_state, nullptr); };
+#endif
+
+  /* Create pipe, and close read end so that writes to the pipe fail
+     with EPIPE.  */
+
+  int fd[2];
+  char c = 0xff;
+  int r;
+
+  r = pipe (fd);
+  SELF_CHECK (r == 0);
+
+  close (fd[0]); SCOPE_EXIT { close (fd[1]); };
+
+  /* Check that writing to the pipe results in EPIPE.  EXPECT_SIG
+     indicates whether a SIGPIPE signal is expected.  */
+  auto check_pipe_write = [&] (bool expect_sig)
+  {
+    got_sigpipe = 0;
+    errno = 0;
+
+    r = write (fd[1], &c, 1);
+    SELF_CHECK (r == -1 && errno == EPIPE
+		&& got_sigpipe == expect_sig);
+  };
+
+  /* Check that without a scoped_ignore_sigpipe in scope we indeed get
+     a SIGPIPE signal.  */
+  check_pipe_write (true);
+
+  /* Now check that with a scoped_ignore_sigpipe in scope, SIGPIPE is
+     ignored/blocked.  */
+  {
+    scoped_ignore_sigpipe ignore1;
+
+    check_pipe_write (false);
+
+    /* Check that scoped_ignore_sigpipe nests correctly.  */
+    {
+      scoped_ignore_sigpipe ignore2;
+
+      check_pipe_write (false);
+    }
+
+    /* If nesting works correctly, this write results in no
+       SIGPIPE.  */
+    check_pipe_write (false);
+  }
+
+  /* No scoped_ignore_sigpipe is is scope anymore, so this should
+     result in a SIGPIPE signal.  */
+  check_pipe_write (true);
+}
+
+#endif /* SIGPIPE */
+
+} /* namespace scoped_ignore_sig */
+} /* namespace selftests */
+
+void _initialize_scoped_ignore_signal_selftests ();
+void
+_initialize_scoped_ignore_signal_selftests ()
+{
+#ifdef SIGPIPE
+  selftests::register_test ("scoped_ignore_sigpipe",
+			    selftests::scoped_ignore_sig::test_sigpipe);
+#endif
+}
-- 
2.26.2


  parent reply	other threads:[~2021-06-15 11:15 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-06-15 11:14 [PATCH 0/4] Introduce scoped_ignore_signal & make it thread safe Pedro Alves
2021-06-15 11:14 ` [PATCH 1/4] Move scoped_ignore_sigttou to gdbsupport/ Pedro Alves
2021-06-15 11:14 ` [PATCH 2/4] Introduce scoped_restore_signal Pedro Alves
2021-06-17 14:57   ` Pedro Alves
2021-06-15 11:14 ` [PATCH 3/4] scoped_ignore_signal: Use sigprocmask+sigtimedwait instead of signal Pedro Alves
2021-06-26 12:35   ` Simon Marchi via Gdb-patches
2021-06-26 13:41     ` John Baldwin
2021-06-26 19:10       ` Simon Marchi via Gdb-patches
2021-06-27 14:55       ` Pedro Alves
2021-06-27 19:01         ` Simon Marchi via Gdb-patches
2021-06-15 11:14 ` Pedro Alves [this message]
2021-06-15 22:26   ` [PATCH 4/4] Add a unit test for scoped_ignore_sigpipe Lancelot SIX via Gdb-patches
2021-06-17 13:00     ` Pedro Alves
2021-06-15 17:04 ` [PATCH 0/4] Introduce scoped_ignore_signal & make it thread safe John Baldwin
2021-06-17 14:38   ` Pedro Alves
2021-06-17 15:36     ` Pedro Alves
2021-06-17 16:45       ` John Baldwin
2021-06-17 18:44         ` [pushed] Don't call sigtimedwait for scoped_ignore_sigttou Pedro Alves
2021-06-15 20:16 ` [PATCH 0/4] Introduce scoped_ignore_signal & make it thread safe Tom Tromey

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=20210615111429.1879286-5-pedro@palves.net \
    --to=pedro@palves.net \
    --cc=gdb-patches@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