From patchwork Wed Jun 14 17:19:19 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wei Liu X-Patchwork-Id: 9787037 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 14C97602D9 for ; Wed, 14 Jun 2017 17:22:36 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id EE01D28210 for ; Wed, 14 Jun 2017 17:22:35 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E24A0283AE; Wed, 14 Jun 2017 17:22:35 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.2 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 6489528210 for ; Wed, 14 Jun 2017 17:22:35 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dLBxH-0004V4-Ok; Wed, 14 Jun 2017 17:19:43 +0000 Received: from mail6.bemta6.messagelabs.com ([193.109.254.103]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dLBxF-0004Tm-W7 for xen-devel@lists.xenproject.org; Wed, 14 Jun 2017 17:19:42 +0000 Received: from [193.109.254.147] by server-8.bemta-6.messagelabs.com id E8/35-03696-D2071495; Wed, 14 Jun 2017 17:19:41 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFupnkeJIrShJLcpLzFFi42JxWrohUlenwDH SYMo5DYvvWyYzOTB6HP5whSWAMYo1My8pvyKBNWPOhibGgh2GFTtWdzM2MP5S7WLk5JAQ8JfY 83kfG4jNJqAs8bOzF8wWEdCTaDrwnBHEZhbwkvixoocdxBYWCJJYeHMhmM0ioCrROKODuYuRg 4NXwELi72QViJHyErvaLrKChDkFLCXWnTUECQsBVczt+coIYStIdEw/xgRi8woISpyc+YQFYp OExMEXL5gnMPLOQpKahSS1gJFpFaNGcWpRWWqRrpGpXlJRZnpGSW5iZo6uoYGZXm5qcXFiemp OYlKxXnJ+7iZGYOAwAMEOxlULAg8xSnIwKYnyThV0jBTiS8pPqcxILM6ILyrNSS0+xCjDwaEk wfsrDygnWJSanlqRlpkDDGGYtAQHj5IIb2wWUJq3uCAxtzgzHSJ1ilGXY8Pq9V+YhFjy8vNSp cR5O/OBigRAijJK8+BGwOLpEqOslDAvI9BRQjwFqUW5mSWo8q8YxTkYlYR5z+YCTeHJzCuB2/ QK6AgmoCOCLjiAHFGSiJCSamCcYrR+kpO7102x+zeWnJ6/vSPE3nAWc5+hzO2+QpEjohpu+ic /ql6cMa0jcfPknamvze4vEwwP4wrI37RB17TiWVWlgbbIRxaZngK9WsF12tnGkV2/+6pFen7M 1HzdxnWhJFvnVKpw20mnLhUTjsTQC9tPPwhgW6w1e1Gv9RPlevHqsi9r1yqxFGckGmoxFxUnA gAtYOKMogIAAA== X-Env-Sender: prvs=3313ff277=wei.liu2@citrix.com X-Msg-Ref: server-10.tower-27.messagelabs.com!1497460774!84485634!2 X-Originating-IP: [66.165.176.89] X-SpamReason: No, hits=0.0 required=7.0 tests=sa_preprocessor: VHJ1c3RlZCBJUDogNjYuMTY1LjE3Ni44OSA9PiAyMDMwMDc=\n, received_headers: No Received headers X-StarScan-Received: X-StarScan-Version: 9.4.19; banners=-,-,- X-VirusChecked: Checked Received: (qmail 63813 invoked from network); 14 Jun 2017 17:19:40 -0000 Received: from smtp.citrix.com (HELO SMTP.CITRIX.COM) (66.165.176.89) by server-10.tower-27.messagelabs.com with RC4-SHA encrypted SMTP; 14 Jun 2017 17:19:40 -0000 X-IronPort-AV: E=Sophos;i="5.39,341,1493683200"; d="scan'208";a="427886216" From: Wei Liu To: Xen-devel Date: Wed, 14 Jun 2017 18:19:19 +0100 Message-ID: <20170614171921.17475-3-wei.liu2@citrix.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20170614171921.17475-1-wei.liu2@citrix.com> References: <20170614171921.17475-1-wei.liu2@citrix.com> MIME-Version: 1.0 Cc: Wei Liu , Ian Jackson Subject: [Xen-devel] [RFC PATCH 2/4] xl: make lock functions work with arbitrary files and fds X-BeenThere: xen-devel@lists.xen.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xen.org Sender: "Xen-devel" X-Virus-Scanned: ClamAV using ClamSMTP Rename the existing lock to xl_global lock. Refactor the functions to take the filename and fd so that they can work with any filename and fd. No functional change. Signed-off-by: Wei Liu --- tools/xl/xl.c | 19 ++++++++++--------- tools/xl/xl.h | 3 ++- tools/xl/xl_utils.c | 38 ++++++++++++++++++++------------------ tools/xl/xl_utils.h | 4 ++-- tools/xl/xl_vmcontrol.c | 6 +++--- 5 files changed, 37 insertions(+), 33 deletions(-) diff --git a/tools/xl/xl.c b/tools/xl/xl.c index 02179a6229..f6740b0a86 100644 --- a/tools/xl/xl.c +++ b/tools/xl/xl.c @@ -35,7 +35,8 @@ int force_execution; int autoballoon = -1; char *blkdev_start; int run_hotplug_scripts = 1; -char *lockfile; +char *xl_global_lockfile; +int xl_global_fd_lock = -1; char *default_vifscript = NULL; char *default_bridge = NULL; char *default_gatewaydev = NULL; @@ -117,14 +118,14 @@ static void parse_global_config(const char *configfile, if (!xlu_cfg_get_long (config, "run_hotplug_scripts", &l, 0)) run_hotplug_scripts = l; - if (!xlu_cfg_get_string (config, "lockfile", &buf, 0)) - lockfile = strdup(buf); + if (!xlu_cfg_get_string (config, "xl_global_lockfile", &buf, 0)) + xl_global_lockfile = strdup(buf); else { - lockfile = strdup(XL_LOCK_FILE); + xl_global_lockfile = strdup(XL_LOCK_FILE); } - if (!lockfile) { - fprintf(stderr, "failed to allocate lockfile\n"); + if (!xl_global_lockfile) { + fprintf(stderr, "failed to allocate xl_global_lockfile\n"); exit(1); } @@ -295,9 +296,9 @@ static void xl_ctx_free(void) xtl_logger_destroy((xentoollog_logger*)logger); logger = NULL; } - if (lockfile) { - free(lockfile); - lockfile = NULL; + if (xl_global_lockfile) { + free(xl_global_lockfile); + xl_global_lockfile = NULL; } } diff --git a/tools/xl/xl.h b/tools/xl/xl.h index aa95b77146..93ec4d7e4c 100644 --- a/tools/xl/xl.h +++ b/tools/xl/xl.h @@ -265,7 +265,8 @@ extern int claim_mode; extern bool progress_use_cr; extern xentoollog_level minmsglevel; #define minmsglevel_default XTL_PROGRESS -extern char *lockfile; +extern char *xl_global_lockfile; +extern int xl_global_fd_lock; extern char *default_vifscript; extern char *default_bridge; extern char *default_gatewaydev; diff --git a/tools/xl/xl_utils.c b/tools/xl/xl_utils.c index 331a67bc95..e7038ec324 100644 --- a/tools/xl/xl_utils.c +++ b/tools/xl/xl_utils.c @@ -316,50 +316,51 @@ out: return ret; } -static int fd_lock = -1; - -int acquire_lock(void) +int acquire_lock(const char *lockfile, int *fd_lock) { int rc; struct flock fl; /* lock already acquired */ - if (fd_lock >= 0) + if (*fd_lock >= 0) return ERROR_INVAL; fl.l_type = F_WRLCK; fl.l_whence = SEEK_SET; fl.l_start = 0; fl.l_len = 0; - fd_lock = open(lockfile, O_WRONLY|O_CREAT, S_IWUSR); - if (fd_lock < 0) { - fprintf(stderr, "cannot open the lockfile %s errno=%d\n", lockfile, errno); + *fd_lock = open(lockfile, O_WRONLY|O_CREAT, S_IWUSR); + if (*fd_lock < 0) { + fprintf(stderr, "cannot open the lockfile %s errno=%d\n", + lockfile, errno); return ERROR_FAIL; } - if (fcntl(fd_lock, F_SETFD, FD_CLOEXEC) < 0) { - close(fd_lock); - fprintf(stderr, "cannot set cloexec to lockfile %s errno=%d\n", lockfile, errno); + if (fcntl(*fd_lock, F_SETFD, FD_CLOEXEC) < 0) { + close(*fd_lock); + fprintf(stderr, "cannot set cloexec to lockfile %s errno=%d\n", + lockfile, errno); return ERROR_FAIL; } get_lock: - rc = fcntl(fd_lock, F_SETLKW, &fl); + rc = fcntl(*fd_lock, F_SETLKW, &fl); if (rc < 0 && errno == EINTR) goto get_lock; if (rc < 0) { - fprintf(stderr, "cannot acquire lock %s errno=%d\n", lockfile, errno); + fprintf(stderr, "cannot acquire lock %s errno=%d\n", + lockfile, errno); rc = ERROR_FAIL; } else rc = 0; return rc; } -int release_lock(void) +int release_lock(const char *lockfile, int *fd_lock) { int rc; struct flock fl; /* lock not acquired */ - if (fd_lock < 0) + if (*fd_lock < 0) return ERROR_INVAL; release_lock: @@ -368,16 +369,17 @@ release_lock: fl.l_start = 0; fl.l_len = 0; - rc = fcntl(fd_lock, F_SETLKW, &fl); + rc = fcntl(*fd_lock, F_SETLKW, &fl); if (rc < 0 && errno == EINTR) goto release_lock; if (rc < 0) { - fprintf(stderr, "cannot release lock %s, errno=%d\n", lockfile, errno); + fprintf(stderr, "cannot release lock %s, errno=%d\n", + lockfile, errno); rc = ERROR_FAIL; } else rc = 0; - close(fd_lock); - fd_lock = -1; + close(*fd_lock); + *fd_lock = -1; return rc; } diff --git a/tools/xl/xl_utils.h b/tools/xl/xl_utils.h index 3ee1543e56..18280d7e84 100644 --- a/tools/xl/xl_utils.h +++ b/tools/xl/xl_utils.h @@ -147,8 +147,8 @@ void print_bitmap(uint8_t *map, int maplen, FILE *stream); int do_daemonize(char *name, const char *pidfile); -int acquire_lock(void); -int release_lock(void); +int acquire_lock(const char *lockfile, int *fd_lock); +int release_lock(const char *lockfile, int *fd_lock); #endif /* XL_UTILS_H */ /* diff --git a/tools/xl/xl_vmcontrol.c b/tools/xl/xl_vmcontrol.c index 9b5a4cb001..53478bfa50 100644 --- a/tools/xl/xl_vmcontrol.c +++ b/tools/xl/xl_vmcontrol.c @@ -776,7 +776,7 @@ int create_domain(struct domain_create *dom_info) start: assert(domid == INVALID_DOMID); - rc = acquire_lock(); + rc = acquire_lock(xl_global_lockfile, &xl_global_fd_lock); if (rc < 0) goto error_out; @@ -838,7 +838,7 @@ start: if ( ret ) goto error_out; - release_lock(); + release_lock(xl_global_lockfile, &xl_global_fd_lock); if (restore_fd_to_close >= 0) { if (close(restore_fd_to_close)) @@ -1012,7 +1012,7 @@ start: } error_out: - release_lock(); + release_lock(xl_global_lockfile, &xl_global_fd_lock); if (libxl_domid_valid_guest(domid)) { libxl_domain_destroy(ctx, domid, 0); domid = INVALID_DOMID;