From patchwork Mon Jul 6 22:42:20 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gert Doering X-Patchwork-Id: 1208 Return-Path: Delivered-To: patchwork@openvpn.net Delivered-To: patchwork@openvpn.net Received: from director9.mail.ord1d.rsapps.net ([172.30.191.6]) by backend30.mail.ord1d.rsapps.net with LMTP id MEjrI486BF+SUAAAIUCqbw for ; Tue, 07 Jul 2020 05:04:15 -0400 Received: from proxy13.mail.ord1d.rsapps.net ([172.30.191.6]) by director9.mail.ord1d.rsapps.net with LMTP id gELOI486BF9QEAAAalYnBA ; Tue, 07 Jul 2020 05:04:15 -0400 Received: from smtp36.gate.ord1c ([172.30.191.6]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) by proxy13.mail.ord1d.rsapps.net with LMTP id kEKWI486BF9vSwAAgjf6aA ; Tue, 07 Jul 2020 05:04:15 -0400 X-Spam-Threshold: 95 X-Spam-Score: 0 X-Spam-Flag: NO X-Virus-Scanned: OK X-Orig-To: openvpnslackdevel@openvpn.net X-Originating-Ip: [216.105.38.7] Authentication-Results: smtp36.gate.ord1c.rsapps.net; iprev=pass policy.iprev="216.105.38.7"; spf=pass smtp.mailfrom="openvpn-devel-bounces@lists.sourceforge.net" smtp.helo="lists.sourceforge.net"; dkim=fail (signature verification failed) header.d=sourceforge.net; dkim=fail (signature verification failed) header.d=sf.net; dmarc=none (p=nil; dis=none) header.from=greenie.muc.de X-Suspicious-Flag: YES X-Classification-ID: d454bbf2-c030-11ea-9e57-5452006630bd-1-1 Received: from [216.105.38.7] ([216.105.38.7:44732] helo=lists.sourceforge.net) by smtp36.gate.ord1c.rsapps.net (envelope-from ) (ecelerity 4.2.38.62370 r(:)) with ESMTPS (cipher=DHE-RSA-AES256-GCM-SHA384) id 43/B2-17061-E8A340F5; Tue, 07 Jul 2020 05:04:14 -0400 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.90_1) (envelope-from ) id 1jsjVk-00040d-U5; Tue, 07 Jul 2020 09:03:32 +0000 Received: from [172.30.20.202] (helo=mx.sourceforge.net) by sfs-ml-1.v29.lw.sourceforge.com with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jsjVj-00040D-GL for openvpn-devel@lists.sourceforge.net; Tue, 07 Jul 2020 09:03:31 +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:To:From:Sender:Reply-To:Cc: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=mnc9fw5DqpO5G82FkmU+Ba+KBiTV36xO3L5NEkC8MEs=; b=XBaJZXECy0QjLJ1yhNNc1GoPmZ wFQF++5r6tYqtKqM9gEryLSnoPlCob6JMRIzNzvTo0B1+toFyNAvgd0JAWBZBNWYe7MWEB7y+Fl2R 0o7gA+WbRnjbV+zRXs49iAw+46wGpEduFk4PuuVkvroFlSUw+rZZRtNF5+nkB6vMBN+c=; 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:To:From:Sender:Reply-To:Cc: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=mnc9fw5DqpO5G82FkmU+Ba+KBiTV36xO3L5NEkC8MEs=; b=T+l/YhsgyKmJw8GablYtvmIgY3 eawujZhyRS2T8yhmSJoCq3Lkau1qzZjzZ8tfhN4d8dO4CbAIMkoDnMqWO8wOZ9ZRYZ0KPLy/i435q oJ8f5b8+5Oh003RkyZ5D1LoDnwqZQhirT//nfPCG5sfAk2WTb7ad87xu9Gt6Ndzin4Fs=; Received: from vmail1.greenie.net ([195.30.8.66]) by sfi-mx-3.v28.lw.sourceforge.com with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.92.2) id 1jsjVg-00A1eJ-TO for openvpn-devel@lists.sourceforge.net; Tue, 07 Jul 2020 09:03:31 +0000 Received: from fbsd-tc.ov.greenie.net (fbsd-tc.ov.greenie.net [IPv6:2001:608:0:814:0:0:f000:21]) by vmail1.greenie.net (8.15.2/8.12.11) with ESMTPS id 0678gKYJ061665 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL) for ; Tue, 7 Jul 2020 10:42:20 +0200 (CEST) Received: from fbsd-tc.ov.greenie.net (localhost [127.0.0.1]) by fbsd-tc.ov.greenie.net (8.15.2/8.15.2) with ESMTPS id 0678gK9v045797 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NO) for ; Tue, 7 Jul 2020 10:42:20 +0200 (CEST) (envelope-from gert@fbsd-tc.ov.greenie.net) Received: (from gert@localhost) by fbsd-tc.ov.greenie.net (8.15.2/8.15.2/Submit) id 0678gKeP045796 for openvpn-devel@lists.sourceforge.net; Tue, 7 Jul 2020 10:42:20 +0200 (CEST) (envelope-from gert) From: Gert Doering To: openvpn-devel@lists.sourceforge.net Date: Tue, 7 Jul 2020 10:42:20 +0200 Message-Id: <20200707084220.45753-1-gert@greenie.muc.de> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20200706190030.49301-1-gert@greenie.muc.de> References: <20200706190030.49301-1-gert@greenie.muc.de> MIME-Version: 1.0 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.6.2 (vmail1.greenie.net [IPv6:2001:608:1:995a:20c:29ff:feb8:10eb]); Tue, 07 Jul 2020 10:42:20 +0200 (CEST) X-Spam-Report: Spam Filtering performed by mx.sourceforge.net. See http://spamassassin.org/tag/ for more details. 0.0 URIBL_BLOCKED ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [URIs: muc.de] 0.2 HEADER_FROM_DIFFERENT_DOMAINS From and EnvelopeFrom 2nd level mail domains are different 0.0 SPF_NONE SPF: sender does not publish an SPF Record 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record X-Headers-End: 1jsjVg-00A1eJ-TO Subject: [Openvpn-devel] [PATCH v2] Remove --writepid file on program exit. 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 For whatever reason, we never removed the pid file on program exit. Not only this is unclean, but it also makes testing for "I want this test case to FAIL" in t_client.sh more annoying to code for "is the OpenVPN process still around?"... Do not unlink the file if chroot() is active (might be outside the chroot arena - testing for realpath etc. is left for someone else). Signed-off-by: Gert Doering Acked-by: Antonio Quartulli --- v2: make this work on M_FATAL exit, by unlinking from openvpn_exit() in error.h - this requires moving write_pid() to init.c so module hierarchy is maintained and introducing a static variable to save the PID file name (otherwise it is no longer available when the top level GC is gone). --- src/openvpn/error.c | 1 + src/openvpn/init.c | 42 ++++++++++++++++++++++++++++++++++++++++++ src/openvpn/init.h | 3 +++ src/openvpn/openvpn.c | 24 +----------------------- 4 files changed, 47 insertions(+), 23 deletions(-) diff --git a/src/openvpn/error.c b/src/openvpn/error.c index ad4f0ef4..d6247fec 100644 --- a/src/openvpn/error.c +++ b/src/openvpn/error.c @@ -743,6 +743,7 @@ openvpn_exit(const int status) #ifdef _WIN32 uninit_win32(); #endif + remove_pid_file(); close_syslog(); diff --git a/src/openvpn/init.c b/src/openvpn/init.c index dd1747f3..cb850bc0 100644 --- a/src/openvpn/init.c +++ b/src/openvpn/init.c @@ -58,6 +58,7 @@ static struct context *static_context; /* GLOBAL */ +static const char *saved_pid_file_name; /* GLOBAL */ /* * Crypto initialization flags @@ -4687,6 +4688,47 @@ close_context(struct context *c, int sig, unsigned int flags) } } +/* Write our PID to a file */ +void +write_pid_file(const char *filename, const char *chroot_dir) +{ + if (filename) + { + unsigned int pid = 0; + FILE *fp = platform_fopen(filename, "w"); + if (!fp) + { + msg(M_ERR, "Open error on pid file %s", filename); + return; + } + + pid = platform_getpid(); + fprintf(fp, "%u\n", pid); + if (fclose(fp)) + { + msg(M_ERR, "Close error on pid file %s", filename); + } + + /* remember file name so it can be deleted "out of context" later */ + /* (the chroot case is more complex and not handled today) */ + if (!chroot_dir) + { + saved_pid_file_name = strdup(filename); + } + } +} + +/* remove PID file on exit, called from openvpn_exit() */ +void +remove_pid_file(void) +{ + if (saved_pid_file_name) + { + platform_unlink(saved_pid_file_name); + } +} + + /* * Do a loopback test * on the crypto subsystem. diff --git a/src/openvpn/init.h b/src/openvpn/init.h index 0e6258f0..a2fdccd3 100644 --- a/src/openvpn/init.h +++ b/src/openvpn/init.h @@ -143,4 +143,7 @@ void open_plugins(struct context *c, const bool import_options, int init_point); void tun_abort(void); +void write_pid_file(const char *filename, const char *chroot_dir); +void remove_pid_file(void); + #endif /* ifndef INIT_H */ diff --git a/src/openvpn/openvpn.c b/src/openvpn/openvpn.c index dc7001dc..857c5faa 100644 --- a/src/openvpn/openvpn.c +++ b/src/openvpn/openvpn.c @@ -46,28 +46,6 @@ process_signal_p2p(struct context *c) return process_signal(c); } -/* Write our PID to a file */ -static void -write_pid(const char *filename) -{ - if (filename) - { - unsigned int pid = 0; - FILE *fp = platform_fopen(filename, "w"); - if (!fp) - { - msg(M_ERR, "Open error on pid file %s", filename); - } - - pid = platform_getpid(); - fprintf(fp, "%u\n", pid); - if (fclose(fp)) - { - msg(M_ERR, "Close error on pid file %s", filename); - } - } -} - /**************************************************************************/ /** @@ -274,7 +252,7 @@ openvpn_main(int argc, char *argv[]) if (c.first_time) { c.did_we_daemonize = possibly_become_daemon(&c.options); - write_pid(c.options.writepid); + write_pid_file(c.options.writepid, c.options.chroot_dir); } #ifdef ENABLE_MANAGEMENT