From patchwork Sat Jan 28 21:59:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Selva Nair X-Patchwork-Id: 3016 Return-Path: Delivered-To: patchwork@openvpn.net Received: by 2002:a05:7300:c95:b0:82:e4b3:40a0 with SMTP id p21csp2213232dyk; Sat, 28 Jan 2023 13:59:42 -0800 (PST) X-Google-Smtp-Source: AK7set/jaJtaBLFMwnWabBBE6yiWu/R8CH1d3fGwkOxBngTsVtmibw0m+tTtSG3REJNUM0/W1l3a X-Received: by 2002:a17:902:dacf:b0:196:2bf1:b68d with SMTP id q15-20020a170902dacf00b001962bf1b68dmr17170136plx.14.1674943181996; Sat, 28 Jan 2023 13:59:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674943181; cv=none; d=google.com; s=arc-20160816; b=Au0zpKBYWxkozkCiMmdEth+MgH9qeXgW/NE1YTDpplIKPH17BH3ZcHQ7CtEJbDc0Br +vlJJdVoFDNu8R7qur8yZGLx8QuYR7vfDunvvbAY7au1nou83dSSgfHgV0xcrXohMfyq QvJRrKKt9HsvFq82A5in/TxPIBEipfk81ZZ0sEkVMcvuwrvbsuTFRx3eV/+4sAB1Cvor v2qO/us8rzu1r4cVDm0yepRejeApNTBKwynz80Tc3B8iDrTVG3cOMC7c6I8xkh1HV0Rr QBzxBhR6QkdjsbekLO8OiUZ8Vd1xX5mYfoG6boe4MhFCZdy+TKEA5ucEbfPb/3REwFny cZ1w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:content-transfer-encoding:list-subscribe:list-help :list-post:list-archive:list-unsubscribe:list-id:precedence:subject :mime-version:references:in-reply-to:message-id:date:to:from :dkim-signature:dkim-signature:dkim-signature; bh=70yRMn+tQYA55qUi7ZaxeK2ZP4QdAF+lmdAI+Or0+28=; b=vwnXHVPOD0jspAlT6Ww7rzyV1Zj63ZnOtJWz3HA3JQHPy9Wu6sVD+0VHtF4xviatvw pwVWjZGTQhPEx/l7fShMDaRnk28oxjNNGeCi6wPTJunlb/RZGr4nYI/VPmyJhFhW8kQm OGntlYvbFsDFygp8ndYxKecwko+DY8QmZdc9EAy40a1TObbwDvOHLiPZfd3uFaQl/BcJ ZTaw6cHHaBUx6HXTMDoX1RXJrrdy9INuMdcNDx+O8bgcaZpmaATC4CH16nAx0dbNKb2I TxFIUNMtoLQWbbGX8R4rovCS2Ht6cB5XzzMFV/KNBABjcwz52gC4XrL+nybUP0upqC/f WBEg== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@sourceforge.net header.s=x header.b=aW6QJPny; dkim=neutral (body hash did not verify) header.i=@sf.net header.s=x header.b=CPkuKYmi; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=gn8qOI2g; spf=pass (google.com: domain of openvpn-devel-bounces@lists.sourceforge.net designates 216.105.38.7 as permitted sender) smtp.mailfrom=openvpn-devel-bounces@lists.sourceforge.net; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from lists.sourceforge.net (lists.sourceforge.net. [216.105.38.7]) by mx.google.com with ESMTPS id a6-20020a170902900600b00194a2577abdsi8349550plp.338.2023.01.28.13.59.41 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sat, 28 Jan 2023 13:59:41 -0800 (PST) Received-SPF: pass (google.com: domain of openvpn-devel-bounces@lists.sourceforge.net designates 216.105.38.7 as permitted sender) client-ip=216.105.38.7; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@sourceforge.net header.s=x header.b=aW6QJPny; dkim=neutral (body hash did not verify) header.i=@sf.net header.s=x header.b=CPkuKYmi; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=gn8qOI2g; spf=pass (google.com: domain of openvpn-devel-bounces@lists.sourceforge.net designates 216.105.38.7 as permitted sender) smtp.mailfrom=openvpn-devel-bounces@lists.sourceforge.net; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from [127.0.0.1] (helo=sfs-ml-2.v29.lw.sourceforge.com) by sfs-ml-2.v29.lw.sourceforge.com with esmtp (Exim 4.95) (envelope-from ) id 1pLtEF-00013Q-2b; Sat, 28 Jan 2023 21:59:18 +0000 Received: from [172.30.20.202] (helo=mx.sourceforge.net) by sfs-ml-2.v29.lw.sourceforge.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.95) (envelope-from ) id 1pLtED-00013J-Nv for openvpn-devel@lists.sourceforge.net; Sat, 28 Jan 2023 21:59:17 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=Content-Transfer-Encoding:MIME-Version:References: In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=a5m7MHn4gOYHJPZW/borIHpue+cdAKONdXdl0kbUgg8=; b=aW6QJPnySyxLH3ihoW3JhmD5YA ctBahNeXgjQS5lSkFC6Bt+b8NQFE/AfFKl6gVBV5jjgylEas9VujSgBPiz4ljlYSLQsRytLrAgMh7 C8Y04o4M1FxxN7j0jC13my0YLLOCvwOmfqWS/dYPLi+9bnEmzHe8JPVeGHkA4lJqToPA=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x ; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To:Message-Id: Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=a5m7MHn4gOYHJPZW/borIHpue+cdAKONdXdl0kbUgg8=; b=CPkuKYmi7q4VRmLgGCZgcp83H4 1N5nJOAu2pnBsDzba/kymMoi7JCGffsbkPrkJlGVMM2kCB3rLosXKUk5yzUdsLza9TUtyZ5FAnTkL ZocTfH6zb2yZBIkF6XIT2k/4deEtQnZ3lEBD6q3eWHESSzp7XZmLjOxuc6Rhdz60nmWI=; Received: from mail-il1-f182.google.com ([209.85.166.182]) by sfi-mx-2.v28.lw.sourceforge.com with esmtps (TLS1.2:ECDHE-RSA-AES128-GCM-SHA256:128) (Exim 4.95) id 1pLtE8-000496-5n for openvpn-devel@lists.sourceforge.net; Sat, 28 Jan 2023 21:59:17 +0000 Received: by mail-il1-f182.google.com with SMTP id u8so3736468ilq.13 for ; Sat, 28 Jan 2023 13:59:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=a5m7MHn4gOYHJPZW/borIHpue+cdAKONdXdl0kbUgg8=; b=gn8qOI2gKQIJhkP29YwJOKppVPj0NfV0EwriFsTux2JQhMWNUnk1NW9RwaHjLK04RS hwQi4v6uTLuCILFmikVILwTA0t+SrOIbfQCRpiNm+sLBNGThC3xvcYqg/qgwRk89wIjH ApUH+M3PrM9zxPC4LTjZl5HO+1NKiExeHl81Cb2xtNrUDQAJPnRLeDFjv8qUClVwcF3R mts4ofUvkqxMN+Df6T7Pt7OrhHzGpqEZ9NK0yf+Bxe+sj3YD9oPpqLZ/8ZDTQfxJb73J YzCnEy9KdypbG1fdW0g/2/fPUPgme8VIk0ietj/eC6qv0ObBePRfcwwFKAnzo1XA1ITv JC9A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=a5m7MHn4gOYHJPZW/borIHpue+cdAKONdXdl0kbUgg8=; b=SBzdbQcRcnX9GNSQ/HsO1nWr1951Fs3JwIv7vBezsc8oNq3gN9gVb1FEqigQEfvrQ3 +qIRACQdK6GHYmiLvRtpUslJQQzrl1hxAYq+WCNVgJZ1C6s+kt2/nNe2IL8Sc+hks3IP amFi4OA9WkKOMKh/MYU9s+Yz35BatycWnllpCsNilVkaE2g5J/xD33c1yz+K1sczksuF vpQp5e/3Xq9BjU5X6n1+S1pA5UTmAxRc/ol5PmhSUsNMIMw+XuibOlnfaHdfpFH7cpA4 AY9RYba6uVnfka6KOYolWYBqYq/ypKFEnrO2ZoWu0UiwAIU+1mckdc31yjkgwKvZ3DjD tyVw== X-Gm-Message-State: AFqh2kpSjNOlHqSPCc5wXTNHUbRF1AvQuDz6DKscPc6OrPyD7bJSQDMV V3STalEVuKba/SZTZOmbBkOc8rLj8hc= X-Received: by 2002:a92:d10:0:b0:30e:f89b:6652 with SMTP id 16-20020a920d10000000b0030ef89b6652mr6147509iln.0.1674943147103; Sat, 28 Jan 2023 13:59:07 -0800 (PST) Received: from uranus.sansel.ca (bras-vprn-tnhlon4053w-lp130-01-70-51-222-66.dsl.bell.ca. [70.51.222.66]) by smtp.gmail.com with ESMTPSA id v20-20020a056e0213d400b00310ce3dd5b1sm1636493ilj.60.2023.01.28.13.59.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 28 Jan 2023 13:59:06 -0800 (PST) From: selva.nair@gmail.com To: openvpn-devel@lists.sourceforge.net Date: Sat, 28 Jan 2023 16:59:01 -0500 Message-Id: <20230128215901.2207208-2-selva.nair@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230128215901.2207208-1-selva.nair@gmail.com> References: <20230128215901.2207208-1-selva.nair@gmail.com> MIME-Version: 1.0 X-Spam-Score: -0.2 (/) X-Spam-Report: Spam detection software, running on the system "util-spamd-1.v13.lw.sourceforge.com", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: From: Selva Nair - "if (sig == X) signal_reset(sig)" now becomes "signal_reset(sig, X)" so that the check and assignment can be done in one place where signals are masked. This is required to avoid change of signal st [...] Content analysis details: (-0.2 points, 6.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 SPF_PASS SPF: sender matches SPF record 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider [selva.nair[at]gmail.com] -0.0 RCVD_IN_MSPIKE_H2 RBL: Average reputation (+2) [209.85.166.182 listed in wl.mailspike.net] -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [209.85.166.182 listed in list.dnswl.org] -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid X-Headers-End: 1pLtE8-000496-5n Subject: [Openvpn-devel] [PATCH 2/2] signal_reset(): combine check and reset operations X-BeenThere: openvpn-devel@lists.sourceforge.net X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: openvpn-devel-bounces@lists.sourceforge.net X-getmail-retrieved-from-mailbox: Inbox X-GMAIL-THRID: =?utf-8?q?1756305222355703759?= X-GMAIL-MSGID: =?utf-8?q?1756305222355703759?= From: Selva Nair - "if (sig == X) signal_reset(sig)" now becomes "signal_reset(sig, X)" so that the check and assignment can be done in one place where signals are masked. This is required to avoid change of signal state between check and reset operations. - Avoid resetting the signal except when absolutely necessary (resetting has the potential of losing signals) - In 'pre_init_signal_catch()', when certain low priority signals are set to SIG_IGN, clear any pending signals of the same type. Also, reset signal at the end of the SIGUSR1 and SIGHUP loops where their values are checked instead of later. This avoids the need for 'signal_reset()' after SIGHUP or in 'init_instance()' which could cause a signal like SIGTERM to be lost. Signed-off-by: Selva Nair Acked-by: Frank Lichtenheld --- src/openvpn/init.c | 3 --- src/openvpn/multi.c | 5 ++--- src/openvpn/openvpn.c | 5 ++--- src/openvpn/sig.c | 40 +++++++++++++++++++++++++++++++++------- src/openvpn/sig.h | 7 ++++++- src/openvpn/socket.c | 5 ++--- src/openvpn/win32.c | 2 +- 7 files changed, 46 insertions(+), 21 deletions(-) diff --git a/src/openvpn/init.c b/src/openvpn/init.c index b500d354..76a7be7b 100644 --- a/src/openvpn/init.c +++ b/src/openvpn/init.c @@ -4299,9 +4299,6 @@ init_instance(struct context *c, const struct env_set *env, const unsigned int f do_inherit_env(c, env); } - /* signals caught here will abort */ - signal_reset(c->sig); - if (c->mode == CM_P2P) { init_management_callback_p2p(c); diff --git a/src/openvpn/multi.c b/src/openvpn/multi.c index f2559016..c52c8f14 100644 --- a/src/openvpn/multi.c +++ b/src/openvpn/multi.c @@ -3868,7 +3868,7 @@ multi_push_restart_schedule_exit(struct multi_context *m, bool next_server) &m->deferred_shutdown_signal.wakeup, compute_wakeup_sigma(&m->deferred_shutdown_signal.wakeup)); - signal_reset(m->top.sig); + signal_reset(m->top.sig, 0); } /* @@ -3878,12 +3878,11 @@ multi_push_restart_schedule_exit(struct multi_context *m, bool next_server) bool multi_process_signal(struct multi_context *m) { - if (m->top.sig->signal_received == SIGUSR2) + if (signal_reset(m->top.sig, SIGUSR2) == SIGUSR2) { struct status_output *so = status_open(NULL, 0, M_INFO, NULL, 0); multi_print_status(m, so, m->status_file_version); status_close(so); - signal_reset(m->top.sig); return false; } else if (proto_is_dgram(m->top.options.ce.proto) diff --git a/src/openvpn/openvpn.c b/src/openvpn/openvpn.c index cba58276..ad0aa8a2 100644 --- a/src/openvpn/openvpn.c +++ b/src/openvpn/openvpn.c @@ -194,7 +194,6 @@ openvpn_main(int argc, char *argv[]) context_clear_all_except_first_time(&c); /* static signal info object */ - CLEAR(siginfo_static); c.sig = &siginfo_static; /* initialize garbage collector scoped to context object */ @@ -333,14 +332,14 @@ openvpn_main(int argc, char *argv[]) /* pass restart status to management subsystem */ signal_restart_status(c.sig); } - while (c.sig->signal_received == SIGUSR1); + while (signal_reset(c.sig, SIGUSR1) == SIGUSR1); env_set_destroy(c.es); uninit_options(&c.options); gc_reset(&c.gc); uninit_early(&c); } - while (c.sig->signal_received == SIGHUP); + while (signal_reset(c.sig, SIGHUP) == SIGHUP); } context_gc_free(&c); diff --git a/src/openvpn/sig.c b/src/openvpn/sig.c index 559ca35d..4eead996 100644 --- a/src/openvpn/sig.c +++ b/src/openvpn/sig.c @@ -259,15 +259,37 @@ register_signal(struct signal_info *si, int signum, const char *signal_text) } } -void -signal_reset(struct signal_info *si) +/** + * Clear the signal if its current value equals signum. If + * signum is zero the signal is cleared independent of its current + * value. Returns the current value of the signal. + */ +int +signal_reset(struct signal_info *si, int signum) { + int sig_saved = 0; if (si) { - si->signal_received = 0; - si->signal_text = NULL; - si->source = SIG_SOURCE_SOFT; + if (si == &siginfo_static) /* attempting to alter the global signal */ + { + block_async_signals(); + } + + sig_saved = si->signal_received; + if (!signum || sig_saved == signum) + { + si->signal_received = 0; + si->signal_text = NULL; + si->source = SIG_SOURCE_SOFT; + msg(D_SIGNAL_DEBUG, "signal_reset: signal %s is cleared", signal_name(signum, true)); + } + + if (si == &siginfo_static) + { + unblock_async_signals(); + } } + return sig_saved; } void @@ -397,6 +419,10 @@ pre_init_signal_catch(void) sigaction(SIGUSR2, &sa, NULL); sigaction(SIGPIPE, &sa, NULL); #endif /* _WIN32 */ + /* clear any pending signals of the ignored type */ + signal_reset(&siginfo_static, SIGUSR1); + signal_reset(&siginfo_static, SIGUSR2); + signal_reset(&siginfo_static, SIGHUP); } void @@ -534,7 +560,7 @@ process_explicit_exit_notification_init(struct context *c) * will be ignored during the exit notification period. */ halt_low_priority_signals(); /* Set hard SIGUSR1/SIGHUP/SIGUSR2 to be ignored */ - signal_reset(c->sig); + signal_reset(c->sig, 0); c->c2.explicit_exit_notification_time_wait = now; @@ -585,7 +611,7 @@ process_sigusr2(const struct context *c) struct status_output *so = status_open(NULL, 0, M_INFO, NULL, 0); print_status(c, so); status_close(so); - signal_reset(c->sig); + signal_reset(c->sig, SIGUSR2); } static bool diff --git a/src/openvpn/sig.h b/src/openvpn/sig.h index 4858eb93..7d76389a 100644 --- a/src/openvpn/sig.h +++ b/src/openvpn/sig.h @@ -81,7 +81,12 @@ void register_signal(struct signal_info *si, int sig, const char *text); void process_explicit_exit_notification_timer_wakeup(struct context *c); -void signal_reset(struct signal_info *si); +/** + * Clear the signal if its current value equals signum. If signum is + * zero the signal is cleared independent of its current value. + * @returns the current value of the signal. + */ +int signal_reset(struct signal_info *si, int signum); static inline void halt_non_edge_triggered_signals(void) diff --git a/src/openvpn/socket.c b/src/openvpn/socket.c index baafe1e6..a2af2498 100644 --- a/src/openvpn/socket.c +++ b/src/openvpn/socket.c @@ -567,12 +567,11 @@ openvpn_getaddrinfo(unsigned int flags, if (sig_info->signal_received) /* were we interrupted by a signal? */ { /* why are we overwriting SIGUSR1 ? */ - if (sig_info->signal_received == SIGUSR1) /* ignore SIGUSR1 */ + if (signal_reset(sig_info, SIGUSR1) == SIGUSR1) /* ignore SIGUSR1 */ { msg(level, "RESOLVE: Ignored SIGUSR1 signal received during " "DNS resolution attempt"); - signal_reset(sig_info); } else { @@ -2176,7 +2175,7 @@ link_socket_init_phase2(struct context *c) if (sig_info->signal_received) { sig_save = *sig_info; - signal_reset(sig_info); + sig_save.signal_received = signal_reset(sig_info, 0); } /* initialize buffers */ diff --git a/src/openvpn/win32.c b/src/openvpn/win32.c index 44176936..60e6c9fd 100644 --- a/src/openvpn/win32.c +++ b/src/openvpn/win32.c @@ -677,7 +677,7 @@ win32_signal_get(struct win32_signal *ws) } if (ret) { - throw_signal(ret); /* this will update signinfo_static.signal received */ + throw_signal(ret); /* this will update siginfo_static.signal received */ } return (siginfo_static.signal_received); }