From patchwork Sun Jun 26 12:12:23 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Boca X-Patchwork-Id: 640662 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 3rcrYx6bVbz9t17 for ; Sun, 26 Jun 2016 22:13:09 +1000 (AEST) Received: from archives.nicira.com (localhost [127.0.0.1]) by archives.nicira.com (Postfix) with ESMTP id F0C7F10C4C; Sun, 26 Jun 2016 05:13:08 -0700 (PDT) X-Original-To: dev@openvswitch.org Delivered-To: dev@openvswitch.org Received: from mx3v3.cudamail.com (mx3.cudamail.com [64.34.241.5]) by archives.nicira.com (Postfix) with ESMTPS id BF56110BF7 for ; Sun, 26 Jun 2016 05:13:07 -0700 (PDT) Received: from bar6.cudamail.com (localhost [127.0.0.1]) by mx3v3.cudamail.com (Postfix) with ESMTPS id 55F2A1626B1 for ; Sun, 26 Jun 2016 06:13:07 -0600 (MDT) X-ASG-Debug-ID: 1466943186-0b32375aef453670001-byXFYA Received: from mx1-pf1.cudamail.com ([192.168.24.1]) by bar6.cudamail.com with ESMTP id hGxoq3xQLC9mehr3 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO) for ; Sun, 26 Jun 2016 06:13:06 -0600 (MDT) X-Barracuda-Envelope-From: pboca@cloudbasesolutions.com X-Barracuda-RBL-Trusted-Forwarder: 192.168.24.1 Received: from unknown (HELO cbssmtp1.cloudbase.local) (91.232.152.5) by mx1-pf1.cudamail.com with SMTP; 26 Jun 2016 12:13:05 -0000 Received-SPF: pass (mx1-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 93F1641702 for ; Sun, 26 Jun 2016 15:13:04 +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 t_YaNlHXG1Mf for ; Sun, 26 Jun 2016 15:12:44 +0300 (EEST) Received: from CBSEX1.cloudbase.local (unknown [10.77.78.3]) by cbssmtp1.cloudbase.local (Postfix) with ESMTP id E9A0A41644 for ; Sun, 26 Jun 2016 15:12:23 +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; Sun, 26 Jun 2016 14:12:23 +0200 X-CudaMail-Envelope-Sender: pboca@cloudbasesolutions.com From: Paul Boca To: "dev@openvswitch.org" X-CudaMail-MID: CM-E1-625004963 X-CudaMail-DTE: 062616 X-CudaMail-Originating-IP: 91.232.152.5 Thread-Topic: [PATCH V4 02/20] windows: Added lockf function and lock PID file X-ASG-Orig-Subj: [##CM-E1-625004963##][PATCH V4 02/20] windows: Added lockf function and lock PID file Thread-Index: AQHRz6P/xGNWEjavukWSDLY+3Qu/cQ== Date: Sun, 26 Jun 2016 12:12:23 +0000 Message-ID: <1466943136-10812-3-git-send-email-pboca@cloudbasesolutions.com> References: <1466943136-10812-1-git-send-email-pboca@cloudbasesolutions.com> In-Reply-To: <1466943136-10812-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-Barracuda-Connect: UNKNOWN[192.168.24.1] X-Barracuda-Start-Time: 1466943186 X-Barracuda-Encrypted: ECDHE-RSA-AES256-GCM-SHA384 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.30774 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 V4 02/20] 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. Signed-off-by: Paul-Daniel Boca --- V2: No changes V3: No changes V4: No changes --- lib/daemon-windows.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/lib/daemon-windows.c b/lib/daemon-windows.c index 8cf0fea..161e754 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,12 @@ 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_NB LOCKFILE_FAIL_IMMEDIATELY /* Don't block when locking. */ +#define LOCK_UN 0x80000000 /* Unlock. */ + static bool service_create; /* Was --service specified? */ static bool service_started; /* Have we dispatched service to start? */ @@ -414,6 +421,33 @@ unlink_pidfile(void) } } +static int +flock(FILE* fd, int operation) +{ + NTSTATUS status; + HANDLE hFile; + OVERLAPPED ov = {0}; + + hFile = (HANDLE)_get_osfhandle(fileno(filep_pidfile)); + if (hFile == INVALID_HANDLE_VALUE) { + VLOG_FATAL("Invalid handle value"); + return -1; + } + + if (operation & LOCK_UN) { + if (UnlockFileEx(hFile, 0, 0, 0xFFFF0000, &ov) == FALSE) { + return -1; + } + } else { + if (LockFileEx(hFile, operation, 0, 0, 0xFFFF0000, &ov) == FALSE) { + VLOG_FATAL("LockFileEx failed, status = 0x%08x\n", status); + return -1; + } + } + + return 0; +} + /* If a pidfile has been configured, creates it and stores the running * process's pid in it. Ensures that the pidfile will be deleted when the * process exits. */ @@ -444,6 +478,16 @@ make_pidfile(void) VLOG_FATAL("Failed to write into the pidfile %s", pidfile); } + fflush(filep_pidfile); + error = flock(filep_pidfile, LOCK_SH); + if (error) { + /* Looks like we failed to acquire the lock. Note that, if we failed + * for some other reason (and '!overwrite_pidfile'), we will have + * left 'tmpfile' as garbage in the file system. */ + VLOG_FATAL("%s: fcntl(F_SETLK) failed (%s)", pidfile, + ovs_strerror(error)); + } + /* Don't close the pidfile till the process exits. */ }