From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr1-f45.google.com (mail-wr1-f45.google.com [209.85.221.45]) by sourceware.org (Postfix) with ESMTPS id 08BC53892440 for ; Tue, 15 Jun 2021 11:14:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 08BC53892440 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=palves.net Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-wr1-f45.google.com with SMTP id v9so1589364wrx.6 for ; Tue, 15 Jun 2021 04:14:40 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=7tilYk6goyuTq7tHYYsy1dNOhl251Key+FbERFHr9fs=; b=d1FkD+/GiT7aj+JNAa9RneVXiLxX9/h99l4fXnfYxOwcciJuH6dTvpevQVKqSK0J9I D6UZcmnAoLi3Gpai5dIiKsp3IZmTX6jwT+uolGbaOQcOWDb7NZNI0zGQBpCrCLB+8aGo 4Ro0KnYh4ep7y37NcgMG7AWffmEuQLw51jhMJrsQ7nJ3rJjxTsfuGFVhQ6Nk2vJef6QS ah8vu3SL8hbGENLvXz0n+2deWiyZxnj4441f9KSLiBQJu1d1uWFYa5eCLk4MOzLA6Y8F n/1HOhnXKxDTAAu8gC4e5HUmCsfivqSG0pFnPKFuSzLaAmYd36Sw5gFYgKp1eZZy/bd4 fpNA== X-Gm-Message-State: AOAM5316j+x261bdW404TDh0gpuEMv6ApRMjja2nTwWaOjNuJSQUiL6j x7CrNsTDSHt92oYI2Ea66ZX8XBusLLHZIg== X-Google-Smtp-Source: ABdhPJzb3aAwmOdbYm59AHRrXTI/ircIgcEMFfP4LEyc6X9BaD+yJRS6hnrSKG5Pk/V/wZq+rGLbJg== X-Received: by 2002:a5d:648a:: with SMTP id o10mr24257591wri.274.1623755679363; Tue, 15 Jun 2021 04:14:39 -0700 (PDT) Received: from localhost ([2001:8a0:f932:6a00:6b6e:c7b6:c5a7:aac3]) by smtp.gmail.com with ESMTPSA id v16sm19330589wrr.6.2021.06.15.04.14.38 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 15 Jun 2021 04:14:38 -0700 (PDT) From: Pedro Alves 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 Message-Id: <20210615111429.1879286-5-pedro@palves.net> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210615111429.1879286-1-pedro@palves.net> References: <20210615111429.1879286-1-pedro@palves.net> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-10.1 required=5.0 tests=BAYES_00, FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM, GIT_PATCH_0, HEADER_FROM_DIFFERENT_DOMAINS, KAM_DMARC_STATUS, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org 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: , X-List-Received-Date: Tue, 15 Jun 2021 11:14:42 -0000 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 + 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