From patchwork Fri Jul 15 06:45:17 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Boca X-Patchwork-Id: 648676 X-Patchwork-Delegate: guru@ovn.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from archives.nicira.com (archives.nicira.com [96.126.127.54]) by ozlabs.org (Postfix) with ESMTP id 3rrNPP5cnZz9s5Q for ; Fri, 15 Jul 2016 16:45:45 +1000 (AEST) Received: from archives.nicira.com (localhost [127.0.0.1]) by archives.nicira.com (Postfix) with ESMTP id 830DA10706; Thu, 14 Jul 2016 23:45:44 -0700 (PDT) X-Original-To: dev@openvswitch.org Delivered-To: dev@openvswitch.org Received: from mx1e4.cudamail.com (mx1.cudamail.com [69.90.118.67]) by archives.nicira.com (Postfix) with ESMTPS id 1BF7A106FF for ; Thu, 14 Jul 2016 23:45:44 -0700 (PDT) Received: from bar5.cudamail.com (unknown [192.168.21.12]) by mx1e4.cudamail.com (Postfix) with ESMTPS id 5EAF21E005F for ; Fri, 15 Jul 2016 00:45:43 -0600 (MDT) X-ASG-Debug-ID: 1468565139-09eadd399d09370001-byXFYA Received: from mx3-pf1.cudamail.com ([192.168.14.2]) by bar5.cudamail.com with ESMTP id i4B7nML7i3ANixuG (version=TLSv1 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Fri, 15 Jul 2016 00:45:39 -0600 (MDT) X-Barracuda-Envelope-From: pboca@cloudbasesolutions.com X-Barracuda-RBL-Trusted-Forwarder: 192.168.14.2 Received: from unknown (HELO cbssmtp1.cloudbase.local) (91.232.152.5) by mx3-pf1.cudamail.com with SMTP; 15 Jul 2016 06:45:38 -0000 Received-SPF: pass (mx3-pf1.cudamail.com: SPF record at cloudbasesolutions.com designates 91.232.152.5 as permitted sender) X-Barracuda-Apparent-Source-IP: 91.232.152.5 X-Barracuda-RBL-IP: 91.232.152.5 Received: from localhost (localhost [127.0.0.1]) by cbssmtp1.cloudbase.local (Postfix) with ESMTP id 1BD46404F1 for ; Fri, 15 Jul 2016 09:45:38 +0300 (EEST) X-Virus-Scanned: amavisd-new at cloudbasesolutions.com Received: from cbssmtp1.cloudbase.local ([127.0.0.1]) by localhost (cbssmtp1.cloudbase.local [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id qPMNhddunIDF for ; Fri, 15 Jul 2016 09:45:17 +0300 (EEST) Received: from CBSEX1.cloudbase.local (unknown [10.77.78.3]) by cbssmtp1.cloudbase.local (Postfix) with ESMTP id ADAA9404EF for ; Fri, 15 Jul 2016 09:45:17 +0300 (EEST) Received: from CBSEX1.cloudbase.local ([10.77.78.3]) by CBSEX1.cloudbase.local ([10.77.78.3]) with mapi id 14.03.0301.000; Fri, 15 Jul 2016 08:45:17 +0200 X-CudaMail-Envelope-Sender: pboca@cloudbasesolutions.com From: Paul Boca To: "dev@openvswitch.org" X-CudaMail-MID: CM-V1-714000410 X-CudaMail-DTE: 071516 X-CudaMail-Originating-IP: 91.232.152.5 Thread-Topic: [PATCH V8] windows: Added lockf function and lock PID file X-ASG-Orig-Subj: [##CM-V1-714000410##][PATCH V8] windows: Added lockf function and lock PID file Thread-Index: AQHR3mRyD1Lro1U+CUCdqQIe3r3kuw== Date: Fri, 15 Jul 2016 06:45:17 +0000 Message-ID: <1468565117-4936-1-git-send-email-pboca@cloudbasesolutions.com> Accept-Language: en-US, it-IT Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [10.77.78.1] MIME-Version: 1.0 X-GBUdb-Analysis: 0, 91.232.152.5, Ugly c=0.303425 p=-0.578947 Source Normal X-MessageSniffer-Rules: 0-0-0-9844-c X-Barracuda-Connect: UNKNOWN[192.168.14.2] X-Barracuda-Start-Time: 1468565139 X-Barracuda-Encrypted: DHE-RSA-AES256-SHA X-Barracuda-URL: https://web.cudamail.com:443/cgi-mod/mark.cgi X-Virus-Scanned: by bsmtpd at cudamail.com X-Barracuda-BRTS-Status: 1 X-Barracuda-Spam-Score: 0.60 X-Barracuda-Spam-Status: No, SCORE=0.60 using global scores of TAG_LEVEL=3.5 QUARANTINE_LEVEL=1000.0 KILL_LEVEL=4.0 tests=BSF_RULE7568M, RDNS_NONE X-Barracuda-Spam-Report: Code version 3.2, rules version 3.2.3.31278 Rule breakdown below pts rule name description ---- ---------------------- -------------------------------------------------- 0.50 BSF_RULE7568M Custom Rule 7568M 0.10 RDNS_NONE Delivered to trusted network by a host with no rDNS Subject: [ovs-dev] [PATCH V8] windows: Added lockf function and lock PID file X-BeenThere: dev@openvswitch.org X-Mailman-Version: 2.1.16 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@openvswitch.org Sender: "dev" If the PID file isn't locked then appctl.py detects it as stale and bails out without doing anything. Because of this lots of Python tests fail. Also this protects the PID file from being overwritten. I used only shared lock, in order to be compatible with Python tests, which try to acquire the lock exclusively. On Windows if the exclusive lock is used, than the read access is denied too for other instances of this file. Signed-off-by: Paul-Daniel Boca Acked-by: Alin Gabriel Serdean --- V2: No changes V3: No changes V4: No changes V5: Removed this patch from Python series. Removed unused constants and defined 0xFFFF0000 as constant. Updated commit text. V6: flock returns now the error of GetLastError. Also it uses fd parameter instead of the global filep_pidfile. Removed unused local variable and unnecessary error message. V7: Keep a shared lock on PID file after the fopen. This ensures us that at most one process can write the same PID file. fopen with 'w' on Windows creates a file with FILE_SHARED_WRITE flag, so anyone with write access on file can write it. V8: Added LOCKFILE_FAIL_IMMEDIATELY flag when trying to get exclusive lock on PID file in order to avoid hangs of other daemons that try to acquire exclusive lock over the same PID file --- lib/daemon-windows.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/lib/daemon-windows.c b/lib/daemon-windows.c index 8cf0fea..350e8fc 100644 --- a/lib/daemon-windows.c +++ b/lib/daemon-windows.c @@ -18,6 +18,7 @@ #include "daemon.h" #include "daemon-private.h" #include +#include #include #include "dirs.h" #include "ovs-thread.h" @@ -26,6 +27,14 @@ VLOG_DEFINE_THIS_MODULE(daemon_windows); +/* Constants for flock function */ +#define LOCK_SH 0x0 /* Shared lock. */ +#define LOCK_EX LOCKFILE_EXCLUSIVE_LOCK /* Exclusive lock. */ +#define LOCK_UN 0x80000000 /* Unlock. Custom value. */ + +/* High-order 32 bits of byte range to lock */ +#define LOCK_MAX_SIZE 0xFFFF0000 + static bool service_create; /* Was --service specified? */ static bool service_started; /* Have we dispatched service to start? */ @@ -404,9 +413,38 @@ detach_process(int argc, char *argv[]) } static void +flock(FILE* fd, int operation) +{ + HANDLE hFile; + OVERLAPPED ov = {0}; + + hFile = (HANDLE)_get_osfhandle(fileno(fd)); + if (hFile == INVALID_HANDLE_VALUE) { + VLOG_FATAL("Invalid handle value"); + } + + if (operation & LOCK_UN) { + if (UnlockFileEx(hFile, 0, 0, LOCK_MAX_SIZE, &ov) == 0) { + VLOG_FATAL("Failed to unlock PID file (%s).", + ovs_lasterror_to_string()); + } + } else { + /* Use LOCKFILE_FAIL_IMMEDIATELY flag to avoid hang of another daemon that tries to + acquire exclusive lock over the same PID file */ + if (LockFileEx(hFile, operation | LOCKFILE_FAIL_IMMEDIATELY, 0, 0, + LOCK_MAX_SIZE, &ov) == FALSE) { + VLOG_FATAL("Failed to lock PID file (%s).", + ovs_lasterror_to_string()); + } + } +} + +static void unlink_pidfile(void) { if (filep_pidfile) { + /* Remove the shared lock on file */ + flock(filep_pidfile, LOCK_UN); fclose(filep_pidfile); } if (pidfile) { @@ -437,6 +475,8 @@ make_pidfile(void) VLOG_FATAL("failed to open %s (%s)", pidfile, ovs_strerror(errno)); } + flock(filep_pidfile, LOCK_EX); + fatal_signal_add_hook(unlink_pidfile, NULL, NULL, true); fprintf(filep_pidfile, "%d\n", _getpid()); @@ -444,6 +484,10 @@ make_pidfile(void) VLOG_FATAL("Failed to write into the pidfile %s", pidfile); } + flock(filep_pidfile, LOCK_SH); + /* This will remove the exclusive lock. The shared lock will remain */ + flock(filep_pidfile, LOCK_UN); + /* Don't close the pidfile till the process exits. */ }