From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id qJqrFtmLyGDEZAAAWB0awg (envelope-from ) for ; Tue, 15 Jun 2021 07:15:37 -0400 Received: by simark.ca (Postfix, from userid 112) id 57E671F163; Tue, 15 Jun 2021 07:15:37 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-1.0 required=5.0 tests=MAILING_LIST_MULTI, URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.2 Received: from sourceware.org (server2.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 5F3551E813 for ; Tue, 15 Jun 2021 07:15:36 -0400 (EDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 2934939730F3 for ; Tue, 15 Jun 2021 11:15:36 +0000 (GMT) Received: from mail-wm1-f52.google.com (mail-wm1-f52.google.com [209.85.128.52]) by sourceware.org (Postfix) with ESMTPS id 4323A3892474 for ; Tue, 15 Jun 2021 11:14:39 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 4323A3892474 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-wm1-f52.google.com with SMTP id h11-20020a05600c350bb02901b59c28e8b4so1502442wmq.1 for ; Tue, 15 Jun 2021 04:14:39 -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=ZUWA8oxK0NDrDsoAnR/nB5rD8xJqx4wHfvNGflNT51Q=; b=eMcQ0FUm+f21G897aTmCWk9RNaPYazjjd4SpdhjFMXpNk2oxk0xV8sw12ydTI5bEy+ CKno6qS7Rk4WNjWyd6TIPrESte1c4OEzsCbSPyliSXpmiCb1cY/PsHkGJW79O1/iZ7+e rRySRQuhv7a897IzMgtOmVjI8XLq1inwcrthzeYXVrKh8z0BKLMeKyI7HNUUd5xEjkZN Pu4ggFhU/ORfR4C0cTn6MpCnGb2tP/spJEf1EvuYpVvR9OvejRi5KdE/vdYGkAlNbH1J 4q1Ua0HimzuSeKy+auF3HDcs/Es2lkYwFJDxMx/02U9n+Str+Xn86nZLr7DQ2wADZc/s na7A== X-Gm-Message-State: AOAM530+PmoqtCs/W3tdWKqk9TM4ma07KJ/2lmcV9k0fm+ifCgtT6IdC WQMhdvp76h78J/G/RmSGtrGzUoz6SlEyVA== X-Google-Smtp-Source: ABdhPJyDTAX3Xmh1rUq9MwTcNI6ma1NseKmZ0D57EWrxsxlxgH5VgXQ1NKZOywi6UD2nTByPzhIDkA== X-Received: by 2002:a1c:7918:: with SMTP id l24mr4583051wme.126.1623755677502; Tue, 15 Jun 2021 04:14:37 -0700 (PDT) Received: from localhost ([2001:8a0:f932:6a00:6b6e:c7b6:c5a7:aac3]) by smtp.gmail.com with ESMTPSA id i6sm1841038wmb.15.2021.06.15.04.14.36 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 15 Jun 2021 04:14:36 -0700 (PDT) From: Pedro Alves To: gdb-patches@sourceware.org Subject: [PATCH 3/4] scoped_ignore_signal: Use sigprocmask+sigtimedwait instead of signal Date: Tue, 15 Jun 2021 12:14:28 +0100 Message-Id: <20210615111429.1879286-4-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-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: , Errors-To: gdb-patches-bounces+public-inbox=simark.ca@sourceware.org Sender: "Gdb-patches" The problem with using signal(...) to temporarily ignore a signal, is that that changes the the signal disposition for the whole process. If multiple threads do it at the same time, you have a race. Fix this by using sigprocmask + sigtimedwait to implement the ignoring instead, if available, which I think probably means everywhere except Windows nowadays. This way, we only change the signal mask for the current thread, so there's no race. Change-Id: Idfe3fb08327ef8cae926f3de9ee81c56a83b1738 gdbsupport/ChangeLog: yyyy-mm-dd Pedro Alves * scoped_ignore_signal.h (scoped_ignore_signal::scoped_ignore_signal) [HAVE_SIGPROCMASK]: Use sigprocmask to block the signal instead of changing the signal disposition for the whole process. (scoped_ignore_signal::~scoped_ignore_signal) [HAVE_SIGPROCMASK]: Use sigtimedwait and sigprocmask to flush and unblock the signal. --- gdbsupport/scoped_ignore_signal.h | 37 +++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/gdbsupport/scoped_ignore_signal.h b/gdbsupport/scoped_ignore_signal.h index cccd390529b..55a921cb332 100644 --- a/gdbsupport/scoped_ignore_signal.h +++ b/gdbsupport/scoped_ignore_signal.h @@ -22,7 +22,10 @@ #include -/* RAII class used to ignore a signal in a scope. */ +/* 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 class scoped_ignore_signal @@ -30,18 +33,48 @@ class scoped_ignore_signal public: scoped_ignore_signal () { +#ifdef HAVE_SIGPROCMASK + sigset_t set, old_state; + + sigemptyset (&set); + sigaddset (&set, Sig); + sigprocmask (SIG_BLOCK, &set, &old_state); + m_was_blocked = sigismember (&old_state, Sig); +#else m_osig = signal (Sig, SIG_IGN); +#endif } ~scoped_ignore_signal () { +#ifdef HAVE_SIGPROCMASK + if (!m_was_blocked) + { + sigset_t set; + const timespec zero_timeout = {}; + + sigemptyset (&set); + sigaddset (&set, Sig); + + /* If we got a pending Sig signal, consume it before + unblocking. */ + sigtimedwait (&set, nullptr, &zero_timeout); + + sigprocmask (SIG_UNBLOCK, &set, nullptr); + } +#else signal (Sig, m_osig); +#endif } DISABLE_COPY_AND_ASSIGN (scoped_ignore_signal); private: - sighandler_t m_osig = nullptr; +#ifdef HAVE_SIGPROCMASK + bool m_was_blocked; +#else + sighandler_t m_osig; +#endif }; struct scoped_ignore_signal_nop -- 2.26.2