[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Xen-devel] [RFC PATCH 1/4] xl: move {acquire, release}_lock to xl_utils.c
Pure code motion, no functional change. Signed-off-by: Wei Liu <wei.liu2@xxxxxxxxxx> --- tools/xl/xl_utils.c | 67 +++++++++++++++++++++++++++++++++++++++++++++++++ tools/xl/xl_utils.h | 3 +++ tools/xl/xl_vmcontrol.c | 67 ------------------------------------------------- 3 files changed, 70 insertions(+), 67 deletions(-) diff --git a/tools/xl/xl_utils.c b/tools/xl/xl_utils.c index 4503ac7ea0..331a67bc95 100644 --- a/tools/xl/xl_utils.c +++ b/tools/xl/xl_utils.c @@ -316,6 +316,73 @@ out: return ret; } +static int fd_lock = -1; + +int acquire_lock(void) +{ + int rc; + struct flock fl; + + /* lock already acquired */ + 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); + 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); + return ERROR_FAIL; + } +get_lock: + 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); + rc = ERROR_FAIL; + } else + rc = 0; + return rc; +} + +int release_lock(void) +{ + int rc; + struct flock fl; + + /* lock not acquired */ + if (fd_lock < 0) + return ERROR_INVAL; + +release_lock: + fl.l_type = F_UNLCK; + fl.l_whence = SEEK_SET; + fl.l_start = 0; + fl.l_len = 0; + + 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); + rc = ERROR_FAIL; + } else + rc = 0; + close(fd_lock); + fd_lock = -1; + + return rc; +} + + /* * Local variables: * mode: C diff --git a/tools/xl/xl_utils.h b/tools/xl/xl_utils.h index 7b9ccca30a..3ee1543e56 100644 --- a/tools/xl/xl_utils.h +++ b/tools/xl/xl_utils.h @@ -146,6 +146,9 @@ uint32_t find_domain(const char *p) __attribute__((warn_unused_result)); 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); #endif /* XL_UTILS_H */ /* diff --git a/tools/xl/xl_vmcontrol.c b/tools/xl/xl_vmcontrol.c index 89c2b25ded..9b5a4cb001 100644 --- a/tools/xl/xl_vmcontrol.c +++ b/tools/xl/xl_vmcontrol.c @@ -30,8 +30,6 @@ #include "xl_utils.h" #include "xl_parse.h" -static int fd_lock = -1; - static void pause_domain(uint32_t domid) { libxl_domain_pause(ctx, domid); @@ -550,71 +548,6 @@ static void autoconnect_vncviewer(uint32_t domid, int autopass) _exit(EXIT_FAILURE); } -static int acquire_lock(void) -{ - int rc; - struct flock fl; - - /* lock already acquired */ - 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); - 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); - return ERROR_FAIL; - } -get_lock: - 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); - rc = ERROR_FAIL; - } else - rc = 0; - return rc; -} - -static int release_lock(void) -{ - int rc; - struct flock fl; - - /* lock not acquired */ - if (fd_lock < 0) - return ERROR_INVAL; - -release_lock: - fl.l_type = F_UNLCK; - fl.l_whence = SEEK_SET; - fl.l_start = 0; - fl.l_len = 0; - - 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); - rc = ERROR_FAIL; - } else - rc = 0; - close(fd_lock); - fd_lock = -1; - - return rc; -} - - static void autoconnect_console(libxl_ctx *ctx_ignored, libxl_event *ev, void *priv) { -- 2.11.0 _______________________________________________ Xen-devel mailing list Xen-devel@xxxxxxxxxxxxx https://lists.xen.org/xen-devel
|
Lists.xenproject.org is hosted with RackSpace, monitoring our |