diff mbox

[RFC,2/4] xl: make lock functions work with arbitrary files and fds

Message ID 20170614171921.17475-3-wei.liu2@citrix.com (mailing list archive)
State New, archived
Headers show

Commit Message

Wei Liu June 14, 2017, 5:19 p.m. UTC
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 <wei.liu2@citrix.com>
---
 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 mbox

Patch

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;