From patchwork Mon Jan 2 19:41:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Selva Nair X-Patchwork-Id: 2968 Return-Path: Delivered-To: patchwork@openvpn.net Received: by 2002:a05:7300:c95:b0:82:e4b3:40a0 with SMTP id p21csp6539493dyk; Mon, 2 Jan 2023 11:43:28 -0800 (PST) X-Google-Smtp-Source: AMrXdXs4B41tzAIhBuI9ffSQQo8vZfsY+6u6Z8LRsn8o77NK/j4Er9YV621F0xsdscSFbRUzaaNt X-Received: by 2002:a17:903:54d:b0:189:81a2:d616 with SMTP id jo13-20020a170903054d00b0018981a2d616mr42150324plb.16.1672688607983; Mon, 02 Jan 2023 11:43:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1672688607; cv=none; d=google.com; s=arc-20160816; b=tErFSTxqV1quIpa1MHEPfQYwMYY13mCyyhLogRAxLv2Vk6MZfsSxWOu6lTDQvcI9gY XpUIHKFFL9GBi2XJdIFI+x51wsE/Jnf+WNAcxzIk2eswGF7Ynz/rOt4lIoKv//tsI+7c OF6K0MA5yGmTtqm48TFEAi+ujF1wuSNEwUOKtHq5/+okYNkXekuq1qsFS1uoGyM8LYtI RZDKR08HgohyjNhn0IvdAxrfbwm9HCKhUBtQY5ym6PrajT+L41KgEcvzpvillmhA/96F F+Lzei5NY0jEu//CjE2IoeAA6eo9CmvZsIQywPBD6RrtV0l3MoWOAJ2XlStXP1TAOzBM htYg== 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=TQ/S2bkE2DYTS9ikCPobDxtjtJ3dwTDSy4LUdgZqgyg=; b=bYs+TQCKhqA+0dSfPj+MOBXb4mFYdMGd1HwiD4khz34F4DmzEiP49gme/UHpAXh73E vavUGbEABf9+XqDW2EOVe8s6n7J3q/BKCwsTyo3vfJYlX2u2gpSRoBDg34WcFdx+RkM9 Kv5nz7YoZl+cFRjyGkW3vVc0mrP63kxK3QAjreEe5fvyhVPrV/gEyvIAEsbLbJha4e9W w4x0XnB2Ol+i612c/NZPw99RhU6c3O4L1tgiraukez7AbO4kONOxoHDSI8jbx0haUDW1 pGAzbQnvbqN6vJI5orHHxh9Nl86eCc3pwlOkTRImhEQk30LYICLS9hXjOXdd9KRg8zqj NGHw== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@sourceforge.net header.s=x header.b=WeyEdMMv; dkim=neutral (body hash did not verify) header.i=@sf.net header.s=x header.b=lYUocvaK; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=ZSF3rmS0; 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 5-20020a170902e9c500b00192b19d0910si8944607plk.432.2023.01.02.11.43.27 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 02 Jan 2023 11:43:27 -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=WeyEdMMv; dkim=neutral (body hash did not verify) header.i=@sf.net header.s=x header.b=lYUocvaK; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=ZSF3rmS0; 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 1pCQhW-00020P-Gr; Mon, 02 Jan 2023 19:42:26 +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 1pCQhM-00020E-JF for openvpn-devel@lists.sourceforge.net; Mon, 02 Jan 2023 19:42:16 +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=KDZb1WRjp7xDvpwsTnzllQhLI1vltBMG3NOoY0BToUU=; b=WeyEdMMvNF17AJUNiUZxlHM6nj qA6yBgZ4JKWHi7MJqKCKNPBpPTAp6mrPnGGq6J87KRondwu4sLPB2fx4VlVilpf8J8WECVRq/K2LL RWZCIATaUi5zu9v4FfExPaxxUPzP/ZnlRPcPP4q0qS4jZgui++2AciA76+JnDumCHn8o=; 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=KDZb1WRjp7xDvpwsTnzllQhLI1vltBMG3NOoY0BToUU=; b=lYUocvaKPK91u64OHUpMMR0MOJ Ymx7MXli6ZM0swpgS2oJPUwssjcorv13Fx3yDRz/16okamO3qq4GN+Y7Y90kFpfXbjjlqvh2/pai/ 2rfkbWFq1JQb3vAF5Z+nK0PsbJF4bW0CHcU2d+QNB28vLLa5zJz5rYk3wS0Awv5KOY7s=; Received: from mail-il1-f181.google.com ([209.85.166.181]) by sfi-mx-1.v28.lw.sourceforge.com with esmtps (TLS1.2:ECDHE-RSA-AES128-GCM-SHA256:128) (Exim 4.95) id 1pCQhF-002WA1-MO for openvpn-devel@lists.sourceforge.net; Mon, 02 Jan 2023 19:42:13 +0000 Received: by mail-il1-f181.google.com with SMTP id d10so16993411ilc.12 for ; Mon, 02 Jan 2023 11:42:09 -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=KDZb1WRjp7xDvpwsTnzllQhLI1vltBMG3NOoY0BToUU=; b=ZSF3rmS0DruLHv3ApXT8vsmeIMiY2m+Gw02QMVjUTrUBp8cVVOY7xCvA2mkTqhEJIy 05E4doNVJXAVYzscnGnHWIUNq2wDDqt1Ibd1YRtVVW8E+oyeasxQtm1MBqnQrrUPrbTs FN/4K0lJjuJ8jcUB++5gZe5SxMjsMb4anO61aI991l/0vaeD9m1SVGNxBVKEWHgZaUtn TRwgwM15Ym21gtw4SJUr/mIWVa+rXolUKRSxVgbrBRHsGYZ9BUjfeqRu6eccEO4vruFv qE4vqEQSMfAqZN7FSuKUWu/rdxAfITE92NWzjVUExjqwQzvjEF4VMAQxXq5Y00EsgTuj RNEg== 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=KDZb1WRjp7xDvpwsTnzllQhLI1vltBMG3NOoY0BToUU=; b=7kibSZUIBC4ICWfCuo2ZR2xuaBJdr1mL9aoctHJBg4kPK+82vtI/D6lM+OkVDS5FMJ L6+jvMCSYClf4PWxCIAKk9IN8Zs3NqnvpliyrAVYdTiDqVBxp0jZTA8UuCkrJfViN1Wc ciRxzG/0zqetwxUgdSD88oLbPQdC1bCU+d8o4jGE0nlKvtVP7+/FpiFfvmTGiLBHXfhN jL7gfSGaQzEUHheAUtZBHQ4NYiibOubmF9wHawRMj3VHSLZVTfpjU2somAmkSiflpAnE RGWcLUX7Puzt7SAoh92U0xVQ9LD0A6hljAS+oDk7TdkKA+hmADOCHOzSrfj8vn5UK/UG ivgQ== X-Gm-Message-State: AFqh2kpdZG07LzdWjHpYD+fbw7u1uuCT2m5J14ifqfU+bfeJpcn3I+nZ K2+LzzAJiO0pk4LQn3tfn8TdOVwSyAMcZg== X-Received: by 2002:a05:6e02:13e1:b0:30c:276b:af80 with SMTP id w1-20020a056e0213e100b0030c276baf80mr2235469ilj.0.1672688523587; Mon, 02 Jan 2023 11:42:03 -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 z65-20020a0293c7000000b00374fe4f0bc3sm9269943jah.158.2023.01.02.11.42.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Jan 2023 11:42:03 -0800 (PST) From: selva.nair@gmail.com To: openvpn-devel@lists.sourceforge.net Date: Mon, 2 Jan 2023 14:41:56 -0500 Message-Id: <20230102194156.1560195-1-selva.nair@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230101215109.1521549-5-selva.nair@gmail.com> References: <20230101215109.1521549-5-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-2.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 - In win32_signal_get() re-order the check so that Windows signals are picked up even if signal_received is non-zero - When management is not active, management_sleep() becomes sleep() but it is not interruptible by signals on Windows. Fix this by periodically checking for signal. Content analysis details: (-0.2 points, 6.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [209.85.166.181 listed in list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider [selva.nair[at]gmail.com] 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.0 RCVD_IN_MSPIKE_H2 RBL: Average reputation (+2) [209.85.166.181 listed in wl.mailspike.net] 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature -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 X-Headers-End: 1pCQhF-002WA1-MO Subject: [Openvpn-devel] [PATCH v2 4/5] Fix signal handling on Windows 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?1753858675529086307?= X-GMAIL-MSGID: =?utf-8?q?1753941129710975250?= From: Selva Nair - In win32_signal_get() re-order the check so that Windows signals are picked up even if signal_received is non-zero - When management is not active, management_sleep() becomes sleep() but it is not interruptible by signals on Windows. Fix this by periodically checking for signal. Fixes Trac #311 #639 (windows specific part) Github: Fixes OpenVPN/openvpn#205 (windows specific part) Note: if stuck in address resolution, press ctrl-C and wait for getaddrinfo() to timeout. v2: WIN32 --> _WIN32 add a chunk in management_sleep that was missed by sloppy conflict-resolution Signed-off-by: Selva Nair --- src/openvpn/manage.c | 16 +++++++- src/openvpn/win32.c | 98 +++++++++++++++++++++++++++++--------------- src/openvpn/win32.h | 3 ++ 3 files changed, 81 insertions(+), 36 deletions(-) diff --git a/src/openvpn/manage.c b/src/openvpn/manage.c index 5465b7e9..9bd5ef4e 100644 --- a/src/openvpn/manage.c +++ b/src/openvpn/manage.c @@ -4045,9 +4045,16 @@ management_sleep(const int n) { management_event_loop_n_seconds(management, n); } - else if (n > 0) + else { - sleep(n); +#ifdef _WIN32 + win32_sleep(n); +#else + if (n > 0) + { + sleep(n); + } +#endif } } @@ -4088,13 +4095,18 @@ man_persist_client_stats(struct management *man, struct context *c) #else /* ifdef ENABLE_MANAGEMENT */ +#include "win32.h" void management_sleep(const int n) { +#ifdef _WIN32 + win32_sleep(n); +#else if (n > 0) { sleep(n); } +#endif /* ifdef _WIN32 */ } #endif /* ENABLE_MANAGEMENT */ diff --git a/src/openvpn/win32.c b/src/openvpn/win32.c index c3520bca..e16d5461 100644 --- a/src/openvpn/win32.c +++ b/src/openvpn/win32.c @@ -642,50 +642,44 @@ int win32_signal_get(struct win32_signal *ws) { int ret = 0; - if (siginfo_static.signal_received) - { - ret = siginfo_static.signal_received; - } - else + + if (ws->mode == WSO_MODE_SERVICE) { - if (ws->mode == WSO_MODE_SERVICE) + if (win32_service_interrupt(ws)) { - if (win32_service_interrupt(ws)) - { - ret = SIGTERM; - } + ret = SIGTERM; } - else if (ws->mode == WSO_MODE_CONSOLE) + } + else if (ws->mode == WSO_MODE_CONSOLE) + { + switch (win32_keyboard_get(ws)) { - switch (win32_keyboard_get(ws)) - { - case 0x3B: /* F1 -> USR1 */ - ret = SIGUSR1; - break; + case 0x3B: /* F1 -> USR1 */ + ret = SIGUSR1; + break; - case 0x3C: /* F2 -> USR2 */ - ret = SIGUSR2; - break; + case 0x3C: /* F2 -> USR2 */ + ret = SIGUSR2; + break; - case 0x3D: /* F3 -> HUP */ - ret = SIGHUP; - break; + case 0x3D: /* F3 -> HUP */ + ret = SIGHUP; + break; - case 0x3E: /* F4 -> TERM */ - ret = SIGTERM; - break; + case 0x3E: /* F4 -> TERM */ + ret = SIGTERM; + break; - case 0x03: /* CTRL-C -> TERM */ - ret = SIGTERM; - break; - } - } - if (ret) - { - throw_signal(ret); /* this will update signinfo_static.signal received */ + case 0x03: /* CTRL-C -> TERM */ + ret = SIGTERM; + break; } } - return ret; + if (ret) + { + throw_signal(ret); /* this will update signinfo_static.signal received */ + } + return (siginfo_static.signal_received); } void @@ -1603,4 +1597,40 @@ set_openssl_env_vars() } } +void +win32_sleep(const int n) +{ + if (n < 0) + { + return; + } + + /* Sleep() is not interruptible. Use a WAIT_OBJECT to catch signal */ + if (HANDLE_DEFINED(win32_signal.in.read)) + { + time_t expire = 0; + update_time(); + expire = now + n; + while (expire >= now) + { + DWORD status = WaitForSingleObject(win32_signal.in.read, (expire - now)*1000); + if (win32_signal_get(&win32_signal) || status == WAIT_TIMEOUT) + { + return; + } + + update_time(); + + if (status == WAIT_FAILED && expire > now) + { + Sleep((expire-now)*1000); + return; + } + } + } + else + { + Sleep(n*1000); + } +} #endif /* ifdef _WIN32 */ diff --git a/src/openvpn/win32.h b/src/openvpn/win32.h index b1371999..26d5ef8f 100644 --- a/src/openvpn/win32.h +++ b/src/openvpn/win32.h @@ -330,5 +330,8 @@ openvpn_execve(const struct argv *a, const struct env_set *es, const unsigned in bool openvpn_swprintf(wchar_t *const str, const size_t size, const wchar_t *const format, ...); +/* Sleep that can be interrupted by signals and exit event */ +void win32_sleep(const int n); + #endif /* ifndef OPENVPN_WIN32_H */ #endif /* ifdef _WIN32 */ From patchwork Mon Jan 2 19:43:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Selva Nair X-Patchwork-Id: 2969 Return-Path: Delivered-To: patchwork@openvpn.net Received: by 2002:a05:7300:c95:b0:82:e4b3:40a0 with SMTP id p21csp6539735dyk; Mon, 2 Jan 2023 11:44:10 -0800 (PST) X-Google-Smtp-Source: AMrXdXvnj2LsNLfRQqz2WHA+vdsy7L2W82foR2BHf22ehl954saWz+lRY0YDcAF7XCoXAn6ktg2n X-Received: by 2002:a17:90a:fa6:b0:225:da94:58e0 with SMTP id 35-20020a17090a0fa600b00225da9458e0mr31924674pjz.23.1672688650659; Mon, 02 Jan 2023 11:44:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1672688650; cv=none; d=google.com; s=arc-20160816; b=U2ay43dJx3naH7kQ1Wr6yAG/X8P0FzaEGCGJdwFKwXsT4VVbfmh8UyRQwFf5bLOUJL bP+oMvOHcA2qY9RyknzJZtRM30/1D/zS4ELpUdiS7RW/QHpanqJygxZEsr5WUKt6a8Ed TO5qMW6felMfPPvmDm0asDEUy/kpaX7BVPFbixvUAFwt4caKFsBeSo7lUehOPH0pP3oN NPm6LwVT+OREnYrC3etjvQwygLDUiAy9ajxebWe28xskcf8548UJdZNIPy1c2pzU8Zq0 SLPBgjy5/5/ft+W2GEmA96yn+Bzt9r79bRlMreGiL1e+SiRsesefvqRlP9IPqNnCmuae APQw== 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=L2lXXKarotb/82bTkgPAG8ToSHkmB/Jb9s9URgyK5UQ=; b=iO1W9/mO065RDTufzqVG7j+hOtxPrxUj2J7bhYC+kPQPBEcfnAXVoX3230RYh0vwWx IBsE09EPGlxT7F2awPyc3qYCxQGTuB7NxT+/eizuaLA09FXfAQGr16R+4nNRkgqH9Su4 BTg92EmuH8UFsQAFCgLFNMJIEvpyJtitv9LDViElBU64kF5Vymgjk0fY80engRj/ubeX 8BTEQp70SZF0wrlHHOw8kgzmqZL+93wqDJXjjJNG7BuZ4UuWjdakkFa88WkQAdGs1R4Q 17TKjz9421Vz+kks2suAimaldFrKXYLoAfz8m/6FAirS6pyV/6VBMpQONCho3ws2tEng cEoQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@sourceforge.net header.s=x header.b=UCqvf54R; dkim=neutral (body hash did not verify) header.i=@sf.net header.s=x header.b=dxN60Ayv; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=Les9Nue2; 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 pl8-20020a17090b268800b001fdcb564d73si22589765pjb.79.2023.01.02.11.44.10 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 02 Jan 2023 11:44:10 -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=UCqvf54R; dkim=neutral (body hash did not verify) header.i=@sf.net header.s=x header.b=dxN60Ayv; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=Les9Nue2; 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-1.v29.lw.sourceforge.com) by sfs-ml-1.v29.lw.sourceforge.com with esmtp (Exim 4.95) (envelope-from ) id 1pCQiq-0002Lc-0x; Mon, 02 Jan 2023 19:43:48 +0000 Received: from [172.30.20.202] (helo=mx.sourceforge.net) by sfs-ml-1.v29.lw.sourceforge.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.95) (envelope-from ) id 1pCQio-0002LV-Kz for openvpn-devel@lists.sourceforge.net; Mon, 02 Jan 2023 19:43:46 +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=tP55yZ//1qpdLgQ8jwxTQ5DsUMhry1+ncg3j/Vlfi5Y=; b=UCqvf54RNnujozPwYpWRc1D2r5 g2KHvoJ6c1bpwoEhFVD+B98rr1PGLw145Lk5pzFgOjbv3IWAtuaDC63r4uTLudcvUn/r9WZWVmm7v JNoOE4bSBbZxR1hcaIVw3TE8OvcPciBMxQ3M3MChMOGERcbms0VxsKS5WCxfah3kiZNg=; 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=tP55yZ//1qpdLgQ8jwxTQ5DsUMhry1+ncg3j/Vlfi5Y=; b=dxN60Ayv3ArMiG13rXzEOi2i7e 9AN9ImUcxuVNq28zSL5P/l5LY0GXZ88T1m2SzWc93GFKi5In/SGWBmByeFghHNDG4rc3g9vq4R+F8 LOXhHs6kXeCvXQrX0OZdn7eNSIi1DunYV3aL/xu3ATryeeZsFnGfibUkMqDY441e/CmE=; Received: from mail-il1-f177.google.com ([209.85.166.177]) by sfi-mx-2.v28.lw.sourceforge.com with esmtps (TLS1.2:ECDHE-RSA-AES128-GCM-SHA256:128) (Exim 4.95) id 1pCQiZ-00072H-Am for openvpn-devel@lists.sourceforge.net; Mon, 02 Jan 2023 19:43:36 +0000 Received: by mail-il1-f177.google.com with SMTP id o8so17033613ilo.1 for ; Mon, 02 Jan 2023 11:43:31 -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=tP55yZ//1qpdLgQ8jwxTQ5DsUMhry1+ncg3j/Vlfi5Y=; b=Les9Nue2dfbNnk1CxR5GtCRJ7PmagYhw3+i4ciD+tTn4GvDDtSYG4jw7Wg7rUc45i1 0pVA0tDcV/nodwWUO8EGmlJp2iK53OqjILgGNLqlbcusApbBfPbXeqa6JFuwuqYNMfIZ xgSMrkW5ad+hGkyMfzJJ+jm+yW2ukx3Y0E64Nb+uOJLFI7zKyye07r+nziFm4Hp4Uye0 vkWvzHhDjcIJ73JZS7eHHvnADoFvuaVANAUoKpkBY1UuzhsWf5l/lZj+rJAXccHxDM8u vnRONIU8SPeYKZJng1rTCpvisEu6o9AjtAZQByJ7yik6vCn+GfqtU819Nog87e/cIVcZ n6CA== 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=tP55yZ//1qpdLgQ8jwxTQ5DsUMhry1+ncg3j/Vlfi5Y=; b=ZWv7+i1IXVJV1RYa47LFRBrDZ6nH8XIHRnaR5LDDLEJ2Cz6Rprr0O5fblNcY+0cbv7 5zCZdRSaOIVmJXHLIr6H0mXoO7pdzODrTX/X8YXT4SjpHdq5j8UAFQx74Y2hQii7Jhs9 g1FwH5LPS+yw/jnKRgMlFfi7ddEJXzP9YpY1FRs20Tikg6DgHZPbJjaZlQPnLvgnMt/f t/NGTbmGqIuthrRX80K1I0gpBSJzaEnnk7y03XNDmut64PIgVT0W5BuAHG+Ips00O+7G oZexCIH2M4ZDc6nuYza0srTxS7zpOkgGGKNbIEOseoLlo9M5G1Bczr1eQzmeglg4YsQH FlyA== X-Gm-Message-State: AFqh2kp3dooXhX7d4mJvFQDMC8oISS3FCP1jG5n5qPP6Jm7K1bwcHl/c QDowf/N8518GKYgHbKAI6BN1b8UyT5tjgg== X-Received: by 2002:a05:6e02:13e1:b0:30c:276b:af80 with SMTP id w1-20020a056e0213e100b0030c276baf80mr2235751ilj.0.1672688605272; Mon, 02 Jan 2023 11:43:25 -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 j12-20020a056e02014c00b003029afc3672sm9309498ilr.36.2023.01.02.11.43.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Jan 2023 11:43:25 -0800 (PST) From: selva.nair@gmail.com To: openvpn-devel@lists.sourceforge.net Date: Mon, 2 Jan 2023 14:43:22 -0500 Message-Id: <20230102194322.1560285-1-selva.nair@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230101215109.1521549-6-selva.nair@gmail.com> References: <20230101215109.1521549-6-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 Currently we use the old signal API which follows system-V or BSD semantics depending on the platform and/or feature-set macros. Further, signal has many weaknesses which makes proper masking (blockin [...] Content analysis details: (-0.2 points, 6.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider [selva.nair[at]gmail.com] 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.0 SPF_PASS SPF: sender matches SPF record -0.0 RCVD_IN_MSPIKE_H2 RBL: Average reputation (+2) [209.85.166.177 listed in wl.mailspike.net] -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [209.85.166.177 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_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature X-Headers-End: 1pCQiZ-00072H-Am Subject: [Openvpn-devel] [PATCH v2 5/5] Improve signal handling using POSIX sigaction 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?1753858667657035684?= X-GMAIL-MSGID: =?utf-8?q?1753941174287022711?= From: Selva Nair Currently we use the old signal API which follows system-V or BSD semantics depending on the platform and/or feature-set macros. Further, signal has many weaknesses which makes proper masking (blocking) of signals during update not possible. Improve this: - Use sigaction to properly mask signals when modifying. - Change signal_reset() to read the current value and reset in one operation. This is required to avoid change of signal state between check and reset-operations. This also allows us to eliminate resetting signal to 0 in init_instance() which can potentially lose signals. Instead, the signal is reset at the end of the SIGUSR1 and SIGHUP loops where their values are checked. Notes: SIG_SOURCE_CONNECTION_FAILED is retained in a hackish way. This value has the same meaning as SIG_SOURCE_SOFT everywhere except where the signal is printed. Looks cosmetic --- could be eliminated? SIGUSR1 during dns resolution is ignored and reset to zero in the original and that behaviour is retained. Not sure why this is needed. Special handling of signals in openvpn_getaddrinfo() as if it is a syscall is retained but looks superfluous. In pre_init_signal_catch() we ignore some unix signals, but the same signals from management are not ignored though both are treated as "HARD" signals. For example, during auth-user-pass query, "kill -SIGUSR1 " will be ignored, but "signal SIGUSR1" from management interface will cause M_FATAL and exit. This is the current behaviour, but could be improved? v2: A buggy version was submitted as v1 -- NULL check missing around line 1785 in socket.c Fixed. Signed-off-by: Selva Nair --- src/openvpn/init.c | 3 - src/openvpn/multi.c | 5 +- src/openvpn/openvpn.c | 4 +- src/openvpn/sig.c | 294 ++++++++++++++++++++++++++++++++---------- src/openvpn/sig.h | 7 +- src/openvpn/socket.c | 7 +- 6 files changed, 238 insertions(+), 82 deletions(-) diff --git a/src/openvpn/init.c b/src/openvpn/init.c index eec25acf..eabc8ea1 100644 --- a/src/openvpn/init.c +++ b/src/openvpn/init.c @@ -4228,9 +4228,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 c2254399..1e7e76f2 100644 --- a/src/openvpn/multi.c +++ b/src/openvpn/multi.c @@ -3828,7 +3828,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); } /* @@ -3838,12 +3838,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 413a750b..d590b166 100644 --- a/src/openvpn/openvpn.c +++ b/src/openvpn/openvpn.c @@ -333,14 +333,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 d6b18cb1..87063913 100644 --- a/src/openvpn/sig.c +++ b/src/openvpn/sig.c @@ -6,6 +6,7 @@ * packet compression. * * Copyright (C) 2002-2022 OpenVPN Inc + * Copyright (C) 2016-2022 Selva Nair * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 @@ -60,6 +61,9 @@ static const struct signame signames[] = { { SIGUSR2, 1, "SIGUSR2", "sigusr2" } }; +/* mask for hard signals from management or windows */ +static unsigned long long ignored_hard_signals_mask; + int parse_signal(const char *signame) { @@ -114,36 +118,174 @@ signal_description(const int signum, const char *sigtext) } } +/** + * Block (i.e., defer) all unix signals. + * Used while directly modifying the volatile elements of + * siginfo_static. + */ +static inline void +block_async_signals(void) +{ +#ifndef _WIN32 + sigset_t all; + sigfillset(&all); /* all signals */ + + sigprocmask(SIG_BLOCK, &all, NULL); +#endif +} + +/** + * Unblock all unix signals. + */ +static inline void +unblock_async_signals(void) +{ +#ifndef _WIN32 + sigset_t none; + sigemptyset(&none); + sigprocmask(SIG_SETMASK, &none, NULL); +#endif +} + +/** + * Private function for registering a signal in the specified + * signal_info struct. This could be the global siginfo_static + * or a context specific signinfo struct. + * + * A signal is allowed to override an already registered + * one only if it has a higher priority. + * Returns true if the signal is set, false otherwise. + * + * Do not call any "AS-unsafe" functions such as printf from here + * as this may be called from signal_handler(). + */ +static bool +try_throw_signal(struct signal_info *si, int signum, int source) +{ + bool ret = false; + if (signal_priority(signum) >= signal_priority(si->signal_received)) + { + si->signal_received = signum; + si->source = source; + ret = true; + } + return ret; +} + +/** + * Throw a hard signal. Called from management and when windows + * signals are received through ctrl-c, exit event etc. + */ void throw_signal(const int signum) { - if (signal_priority(signum) >= signal_priority(siginfo_static.signal_received)) + if (ignored_hard_signals_mask & (1LL << signum)) + { + dmsg(D_LOW, "Signal %s is currently ignored", signal_name(signum, true)); + return; + } + block_async_signals(); + + if (!try_throw_signal(&siginfo_static, signum, SIG_SOURCE_HARD)) + { + dmsg(D_LOW, "Ignoring %s when %s has been received", signal_name(signum, true), + signal_name(siginfo_static.signal_received, true)); + } + else { - siginfo_static.signal_received = signum; - siginfo_static.source = SIG_SOURCE_HARD; + dmsg(D_LOW, "Throw signal: %s ", signal_name(signum, true)); } + + unblock_async_signals(); } +/** + * Throw a soft global signal. Used to register internally generated signals + * due to errors that require a restart or exit, or restart requests + * received from the server. A textual description of the signal may + * be provided. + */ void throw_signal_soft(const int signum, const char *signal_text) { - if (signal_priority(signum) >= signal_priority(siginfo_static.signal_received)) + block_async_signals(); + + if (try_throw_signal(&siginfo_static, signum, SIG_SOURCE_SOFT)) { - siginfo_static.signal_received = signum; - siginfo_static.source = SIG_SOURCE_SOFT; siginfo_static.signal_text = signal_text; } + else + { + dmsg(D_LOW, "Ignoring %s when %s has been received", signal_name(signum, true), + signal_name(siginfo_static.signal_received, true)); + } + + unblock_async_signals(); } +/** + * Register a soft signal in the signal_info struct si respecting priority. + * si may be a pointer to the global siginfo_static or a context-specific + * signal in a multi-instance or a temporary variable. + */ void -signal_reset(struct signal_info *si) +register_signal(struct signal_info *si, int signum, const char *signal_text) { + if (si == &siginfo_static) /* attempting to alter the global signal */ + { + block_async_signals(); + } + + if (try_throw_signal(si, signum, SIG_SOURCE_SOFT)) + { + si->signal_text = signal_text; + if (signal_text && strcmp(signal_text, "connection-failed") == 0) + { + si->source = SIG_SOURCE_CONNECTION_FAILED; + } + } + else + { + dmsg(D_LOW, "Ignoring %s when %s has been received", signal_name(signum, true), + signal_name(si->signal_received, true)); + } + + if (si == &siginfo_static) + { + unblock_async_signals(); + } +} + +/** + * 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; + } + + if (si == &siginfo_static) + { + unblock_async_signals(); + } } + return sig_saved; } void @@ -239,12 +381,10 @@ signal_restart_status(const struct signal_info *si) static void signal_handler(const int signum) { - throw_signal(signum); - signal(signum, signal_handler); + try_throw_signal(&siginfo_static, signum, SIG_SOURCE_HARD); } #endif - /* set handlers for unix signals */ #define SM_UNDEF 0 @@ -256,28 +396,65 @@ void pre_init_signal_catch(void) { #ifndef _WIN32 + sigset_t block_mask; + struct sigaction sa; + CLEAR(sa); + + sigfillset(&block_mask); /* all signals */ + sa.sa_handler = signal_handler; + sa.sa_mask = block_mask; /* signals blocked inside the handler */ + sa.sa_flags = SA_RESTART; /* match with the behaviour of signal() on Linux and BSD */ + signal_mode = SM_PRE_INIT; - signal(SIGINT, signal_handler); - signal(SIGTERM, signal_handler); - signal(SIGHUP, SIG_IGN); - signal(SIGUSR1, SIG_IGN); - signal(SIGUSR2, SIG_IGN); - signal(SIGPIPE, SIG_IGN); + sigaction(SIGINT, &sa, NULL); + sigaction(SIGTERM, &sa, NULL); + + sa.sa_handler = SIG_IGN; + sigaction(SIGHUP, &sa, NULL); + sigaction(SIGUSR1, &sa, NULL); + sigaction(SIGUSR2, &sa, NULL); + sigaction(SIGPIPE, &sa, NULL); #endif /* _WIN32 */ + + /* similar "hard" signals from management not masked -- why ? */ } void post_init_signal_catch(void) { #ifndef _WIN32 + sigset_t block_mask; + struct sigaction sa; + CLEAR(sa); + + sigfillset(&block_mask); /* all signals */ + sa.sa_handler = signal_handler; + sa.sa_mask = block_mask; /* signals blocked inside the handler */ + sa.sa_flags = SA_RESTART; /* match with the behaviour of signal() on Linux and BSD */ + signal_mode = SM_POST_INIT; - signal(SIGINT, signal_handler); - signal(SIGTERM, signal_handler); - signal(SIGHUP, signal_handler); - signal(SIGUSR1, signal_handler); - signal(SIGUSR2, signal_handler); - signal(SIGPIPE, SIG_IGN); -#endif + sigaction(SIGINT, &sa, NULL); + sigaction(SIGTERM, &sa, NULL); + sigaction(SIGHUP, &sa, NULL); + sigaction(SIGUSR1, &sa, NULL); + sigaction(SIGUSR2, &sa, NULL); + sa.sa_handler = SIG_IGN; + sigaction(SIGPIPE, &sa, NULL); +#endif /* _WIN32 */ +} + +void +halt_low_priority_signals() +{ +#ifndef _WIN32 + struct sigaction sa; + CLEAR(sa); + sa.sa_handler = SIG_IGN; + sigaction(SIGHUP, &sa, NULL); + sigaction(SIGUSR1, &sa, NULL); + sigaction(SIGUSR2, &sa, NULL); +#endif /* _WIN32 */ + ignored_hard_signals_mask = (1LL << SIGHUP) | (1LL << SIGUSR1) | (1LL << SIGUSR2); } /* called after daemonization to retain signal settings */ @@ -341,7 +518,6 @@ print_status(const struct context *c, struct status_output *so) gc_free(&gc); } - /* Small helper function to determine if we should send the exit notification * via control channel */ static inline bool @@ -371,8 +547,15 @@ process_explicit_exit_notification_init(struct context *c) event_timeout_init(&c->c2.explicit_exit_notification_interval, 1, 0); reset_coarse_timers(c); - signal_reset(c->sig); + /* Windows exit event will continue trigering SIGTERM -- halt it */ halt_non_edge_triggered_signals(); + + /* Before resetting the signal, ensure hard low priority signals + * will be ignored during the exit notification period. + */ + halt_low_priority_signals(); /* Set hard SIGUSR1/SIGHUP/SIGUSR2 to be ignored */ + signal_reset(c->sig, 0); + c->c2.explicit_exit_notification_time_wait = now; /* Check if we are in TLS mode and should send the notification via data @@ -422,7 +605,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 @@ -439,33 +622,21 @@ process_sigterm(struct context *c) } /** - * If a restart signal is received during exit-notification, reset the - * signal and return true. If its a soft restart signal from the event loop - * which implies the loop cannot continue, remap to SIGTERM to exit promptly. + * If a soft restart signal is received during exit-notification, it + * implies the event loop cannot continue: remap to SIGTERM to exit promptly. + * Hard restart signals are ignored during exit notification wait. */ -static bool -ignore_restart_signals(struct context *c) +static void +remap_restart_signals(struct context *c) { - bool ret = false; - if ( (c->sig->signal_received == SIGUSR1 || c->sig->signal_received == SIGHUP) - && event_timeout_defined(&c->c2.explicit_exit_notification_interval) ) + if ((c->sig->signal_received == SIGUSR1 || c->sig->signal_received == SIGHUP) + && event_timeout_defined(&c->c2.explicit_exit_notification_interval) + && c->sig->source != SIG_SOURCE_HARD) { - if (c->sig->source == SIG_SOURCE_HARD) - { - msg(M_INFO, "Ignoring %s received during exit notification", - signal_name(c->sig->signal_received, true)); - signal_reset(c->sig); - ret = true; - } - else - { - msg(M_INFO, "Converting soft %s received during exit notification to SIGTERM", - signal_name(c->sig->signal_received, true)); - register_signal(c->sig, SIGTERM, "exit-with-notification"); - ret = false; - } + msg(M_INFO, "Converting soft %s received during exit notification to SIGTERM", + signal_name(c->sig->signal_received, true)); + register_signal(c->sig, SIGTERM, "exit-with-notification"); } - return ret; } bool @@ -473,11 +644,9 @@ process_signal(struct context *c) { bool ret = true; - if (ignore_restart_signals(c)) - { - ret = false; - } - else if (c->sig->signal_received == SIGTERM || c->sig->signal_received == SIGINT) + remap_restart_signals(c); + + if (c->sig->signal_received == SIGTERM || c->sig->signal_received == SIGINT) { ret = process_sigterm(c); } @@ -488,14 +657,3 @@ process_signal(struct context *c) } return ret; } - -void -register_signal(struct signal_info *si, int sig, const char *text) -{ - if (signal_priority(sig) >= signal_priority(si->signal_received)) - { - si->signal_received = sig; - si->signal_text = text; - si->source = SIG_SOURCE_SOFT; - } -} diff --git a/src/openvpn/sig.h b/src/openvpn/sig.h index 83adc543..b2845d97 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 59d89352..65de7456 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 { @@ -1588,7 +1587,6 @@ socket_connect(socket_descriptor_t *sd, openvpn_close_socket(*sd); *sd = SOCKET_UNDEFINED; register_signal(sig_info, SIGUSR1, "connection-failed"); - sig_info->source = SIG_SOURCE_CONNECTION_FAILED; } else { @@ -1784,7 +1782,6 @@ resolve_remote(struct link_socket *sock, { if (signal_received) { - /* potential overwrite of signal */ register_signal(sig_info, SIGUSR1, "socks-resolve-failure"); } goto done; @@ -2177,7 +2174,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 */