From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id GcynGiApyWARdAAAWB0awg (envelope-from ) for ; Tue, 15 Jun 2021 18:26:40 -0400 Received: by simark.ca (Postfix, from userid 112) id 5EB1C1F163; Tue, 15 Jun 2021 18:26:40 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-0.7 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,MAILING_LIST_MULTI,RDNS_DYNAMIC,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.2 Received: from sourceware.org (ip-8-43-85-97.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id 666F51E813 for ; Tue, 15 Jun 2021 18:26:39 -0400 (EDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 9EB8E3836004 for ; Tue, 15 Jun 2021 22:26:38 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9EB8E3836004 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1623795998; bh=/9n8p9owHNG8YevQZvqTZaj0r00IeRWKXYKyLoPmHD0=; h=Date:To:Subject:References:In-Reply-To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=YAWXEyzvw3QuDYDu84Kw8qQ+aW8sdOGDollUe0P+JiKp9hUcJGyDh07aeHGQFA8WR 0ZPAuXDsxpTZmNgoUBsaRey47d3H4/ySUvPGs5e4LQQV4h2NpuW6uXaBjlgsXERumU XdwARL09xqjlBZStP6g4k5PYNr8EiywUz6MAVRMU= Received: from lndn.lancelotsix.com (lndn.lancelotsix.com [51.195.220.111]) by sourceware.org (Postfix) with ESMTPS id 635B8384803D for ; Tue, 15 Jun 2021 22:26:18 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 635B8384803D Received: from Plymouth (unknown [IPv6:2a02:390:9086:0:536c:722b:f4d9:ae6f]) by lndn.lancelotsix.com (Postfix) with ESMTPSA id F3B5F827B7; Tue, 15 Jun 2021 22:26:16 +0000 (UTC) Date: Tue, 15 Jun 2021 23:26:09 +0100 To: Pedro Alves Subject: Re: [PATCH 4/4] Add a unit test for scoped_ignore_sigpipe Message-ID: <20210615222530.kolr7pn7f2uv5fy5@Plymouth> References: <20210615111429.1879286-1-pedro@palves.net> <20210615111429.1879286-5-pedro@palves.net> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210615111429.1879286-5-pedro@palves.net> X-Greylist: Sender succeeded SMTP AUTH, not delayed by milter-greylist-4.5.11 (lndn.lancelotsix.com [0.0.0.0]); Tue, 15 Jun 2021 22:26:17 +0000 (UTC) X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Lancelot SIX via Gdb-patches Reply-To: Lancelot SIX Cc: gdb-patches@sourceware.org Errors-To: gdb-patches-bounces+public-inbox=simark.ca@sourceware.org Sender: "Gdb-patches" Hi, There is a tiny typo (see above). Lancelot. On Tue, Jun 15, 2021 at 12:14:29PM +0100, Pedro Alves wrote: > gdb/ChangeLog: > yyyy-mm-dd Pedro Alves > > * 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 . */ > + > +#include "defs.h" > +#include "gdbsupport/scoped_ignore_signal.h" > +#include "gdbsupport/selftest.h" > +#include "gdbsupport/scope-exit.h" > +#include > +#include > + > +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 s/is is/is in/ > + 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 >