From: Wei Liu <wei.liu2@citrix.com>
To: xen-devel@lists.xen.org
Cc: Wei Liu <wei.liu2@citrix.com>,
ian.jackson@eu.citrix.com, ian.campbell@citrix.com
Subject: [PATCH v1 02/10] libxl_internal: functions to lock / unlock domain configuration
Date: Thu, 10 Jul 2014 15:32:17 +0100 [thread overview]
Message-ID: <1405002745-5034-3-git-send-email-wei.liu2@citrix.com> (raw)
In-Reply-To: <1405002745-5034-1-git-send-email-wei.liu2@citrix.com>
Simple file lock taken from xl to serialise access to "libxl-json" file.
If a thread cannot get hold of the lock it waits due to F_SETLKW.
In order to generate lock file name, rename userdata_path to
libxl__userdata_path and declare it in libxl_internal.h
Signed-off-by: Wei Liu <wei.liu2@citrix.com>
---
tools/libxl/libxl_dom.c | 14 ++++----
tools/libxl/libxl_internal.c | 76 ++++++++++++++++++++++++++++++++++++++++++
tools/libxl/libxl_internal.h | 11 ++++++
3 files changed, 94 insertions(+), 7 deletions(-)
diff --git a/tools/libxl/libxl_dom.c b/tools/libxl/libxl_dom.c
index 83eb29a..a41e8fd 100644
--- a/tools/libxl/libxl_dom.c
+++ b/tools/libxl/libxl_dom.c
@@ -1762,9 +1762,9 @@ char *libxl__uuid2string(libxl__gc *gc, const libxl_uuid uuid)
return GCSPRINTF(LIBXL_UUID_FMT, LIBXL_UUID_BYTES(uuid));
}
-static const char *userdata_path(libxl__gc *gc, uint32_t domid,
- const char *userdata_userid,
- const char *wh)
+const char *libxl__userdata_path(libxl__gc *gc, uint32_t domid,
+ const char *userdata_userid,
+ const char *wh)
{
libxl_ctx *ctx = libxl__gc_owner(gc);
char *uuid_string;
@@ -1799,7 +1799,7 @@ void libxl__userdata_destroyall(libxl__gc *gc, uint32_t domid)
glob_t gl;
int r, i;
- pattern = userdata_path(gc, domid, "*", "?");
+ pattern = libxl__userdata_path(gc, domid, "*", "?");
if (!pattern)
goto out;
@@ -1830,7 +1830,7 @@ int libxl_userdata_store(libxl_ctx *ctx, uint32_t domid,
int e, rc;
int fd = -1;
- filename = userdata_path(gc, domid, userdata_userid, "d");
+ filename = libxl__userdata_path(gc, domid, userdata_userid, "d");
if (!filename) {
rc = ERROR_NOMEM;
goto out;
@@ -1841,7 +1841,7 @@ int libxl_userdata_store(libxl_ctx *ctx, uint32_t domid,
goto out;
}
- newfilename = userdata_path(gc, domid, userdata_userid, "n");
+ newfilename = libxl__userdata_path(gc, domid, userdata_userid, "n");
if (!newfilename) {
rc = ERROR_NOMEM;
goto out;
@@ -1892,7 +1892,7 @@ int libxl_userdata_retrieve(libxl_ctx *ctx, uint32_t domid,
int datalen = 0;
void *data = 0;
- filename = userdata_path(gc, domid, userdata_userid, "d");
+ filename = libxl__userdata_path(gc, domid, userdata_userid, "d");
if (!filename) {
rc = ERROR_NOMEM;
goto out;
diff --git a/tools/libxl/libxl_internal.c b/tools/libxl/libxl_internal.c
index dc47177..9cc60e8 100644
--- a/tools/libxl/libxl_internal.c
+++ b/tools/libxl/libxl_internal.c
@@ -436,6 +436,82 @@ out:
return rc;
}
+int libxl__lock_domain_configuration(libxl__gc *gc, uint32_t domid,
+ int *fd_lock)
+{
+ int rc;
+ struct flock fl;
+ const char *lockfile;
+
+ if (*fd_lock >= 0)
+ return ERROR_INVAL;
+
+ lockfile = libxl__userdata_path(gc, domid, "libxl-json.lock", "d");
+ if (!lockfile)
+ return ERROR_FAIL;
+
+ *fd_lock = open(lockfile, O_WRONLY|O_CREAT, S_IWUSR);
+ if (*fd_lock < 0) {
+ LOGE(ERROR, "cannot open lockfile %s errno=%d\n", lockfile, errno);
+ return ERROR_FAIL;
+ }
+
+ if (fcntl(*fd_lock, F_SETFD, FD_CLOEXEC) < 0) {
+ close(*fd_lock);
+ LOGE(ERROR, "cannot set cloexec to lockfile %s errno=%d\n",
+ lockfile, errno);
+ return ERROR_FAIL;
+ }
+
+get_lock:
+ fl.l_type = F_WRLCK;
+ 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 get_lock;
+
+ if (rc < 0) {
+ LOGE(ERROR, "cannot acquire lock %s errno=%d\n", lockfile, errno);
+ rc = ERROR_FAIL;
+ } else
+ rc = 0;
+
+ return rc;
+}
+
+int libxl__unlock_domain_configuration(libxl__gc *gc, uint32_t domid,
+ int *fd_lock)
+{
+ int rc;
+ struct flock fl;
+
+ 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) {
+ const char *lockfile;
+ lockfile = libxl__userdata_path(gc, domid, "libxl-json.lock", "d");
+ LOGE(ERROR, "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:
diff --git a/tools/libxl/libxl_internal.h b/tools/libxl/libxl_internal.h
index ef2111b..e9d93ac 100644
--- a/tools/libxl/libxl_internal.h
+++ b/tools/libxl/libxl_internal.h
@@ -991,6 +991,9 @@ _hidden int libxl__toolstack_restore(uint32_t domid, const uint8_t *buf,
uint32_t size, void *data);
_hidden int libxl__domain_resume_device_model(libxl__gc *gc, uint32_t domid);
+_hidden const char *libxl__userdata_path(libxl__gc *gc, uint32_t domid,
+ const char *userdata_userid,
+ const char *wh);
_hidden void libxl__userdata_destroyall(libxl__gc *gc, uint32_t domid);
_hidden int libxl__domain_resume(libxl__gc *gc, uint32_t domid,
@@ -3228,6 +3231,14 @@ int libxl__get_domain_configuration(libxl__gc *gc, uint32_t domid,
libxl_domain_config *d_config);
int libxl__set_domain_configuration(libxl__gc *gc, uint32_t domid,
libxl_domain_config *d_config);
+/*
+ * Lock / unlock domain configuration in libxl private data store.
+ * fd_lock contains the file descriptor pointing to the lock file.
+ */
+int libxl__lock_domain_configuration(libxl__gc *gc, uint32_t domid,
+ int *fd_lock);
+int libxl__unlock_domain_configuration(libxl__gc *gc, uint32_t domid,
+ int *fd_lock);
#endif
--
1.7.10.4
next prev parent reply other threads:[~2014-07-10 14:32 UTC|newest]
Thread overview: 70+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-07-10 14:32 [PATCH v1 00/10] libxl: synchronise domain configuration Wei Liu
2014-07-10 14:32 ` [PATCH v1 01/10] libxl: libxl-json format and internal functions to get / set it Wei Liu
2014-07-16 16:11 ` Ian Campbell
2014-07-16 16:44 ` Wei Liu
2014-07-24 18:09 ` Ian Jackson
2014-07-24 18:29 ` Ian Jackson
2014-07-25 10:30 ` Wei Liu
2014-07-25 14:51 ` Ian Jackson
2014-07-10 14:32 ` Wei Liu [this message]
2014-07-16 16:15 ` [PATCH v1 02/10] libxl_internal: functions to lock / unlock domain configuration Ian Campbell
2014-07-16 16:44 ` Wei Liu
2014-07-17 11:29 ` Ian Campbell
2014-07-17 11:41 ` Wei Liu
2014-07-17 11:48 ` Ian Campbell
2014-07-24 18:24 ` Ian Jackson
2014-07-25 10:36 ` Wei Liu
2014-07-10 14:32 ` [PATCH v1 03/10] libxl: store a copy of vanilla domain configuration when creating domain Wei Liu
2014-07-16 16:18 ` Ian Campbell
2014-07-16 16:47 ` Wei Liu
2014-07-17 11:06 ` Ian Campbell
2014-07-17 11:46 ` Wei Liu
2014-07-24 18:52 ` Ian Jackson
2014-07-25 10:53 ` Wei Liu
2014-07-25 15:01 ` Ian Jackson
2014-07-25 15:43 ` Wei Liu
2014-07-25 17:14 ` Ian Jackson
2014-07-25 17:34 ` Wei Liu
2014-07-25 18:31 ` Ian Jackson
2014-07-25 19:47 ` Wei Liu
2014-07-28 9:42 ` Ian Campbell
2014-07-28 9:50 ` Ian Jackson
2014-07-10 14:32 ` [PATCH v1 04/10] libxl: separate device add/rm complete callbacks Wei Liu
2014-07-16 16:26 ` Ian Campbell
2014-07-16 16:48 ` Wei Liu
2014-07-10 14:32 ` [PATCH v1 05/10] libxl: synchronise configuration when we hotplug a device Wei Liu
2014-07-16 16:48 ` Ian Campbell
2014-07-16 17:12 ` Wei Liu
2014-07-17 11:44 ` Ian Campbell
2014-07-17 14:13 ` Wei Liu
2014-07-18 8:49 ` Ian Campbell
2014-07-18 11:22 ` Wei Liu
2014-07-18 12:20 ` Ian Campbell
2014-07-18 13:41 ` Wei Liu
2014-07-18 13:44 ` Ian Campbell
2014-07-25 16:06 ` Ian Jackson
2014-07-25 16:40 ` Wei Liu
2014-07-25 17:11 ` Ian Jackson
2014-07-25 17:19 ` Wei Liu
2014-07-10 14:32 ` [PATCH v1 06/10] libxl: synchronise configuration when we remove/destroy " Wei Liu
2014-07-16 16:58 ` Ian Campbell
2014-07-10 14:32 ` [PATCH v1 07/10] libxl: make libxl_cd_insert "eject" + "insert" Wei Liu
2014-07-17 10:44 ` Ian Campbell
2014-07-17 14:20 ` Wei Liu
2014-07-10 14:32 ` [PATCH v1 08/10] libxl: introduce libxl_get_memory_static_max Wei Liu
2014-07-17 10:47 ` Ian Campbell
2014-07-17 12:02 ` Wei Liu
2014-07-17 13:59 ` Ian Campbell
2014-07-29 13:39 ` Ian Jackson
2014-07-10 14:32 ` [PATCH v1 09/10] libxl: introduce libxl_retrieve_domain_configuration Wei Liu
2014-07-17 10:59 ` Ian Campbell
2014-07-17 12:11 ` Wei Liu
2014-07-17 14:02 ` Ian Campbell
2014-07-17 14:28 ` Wei Liu
2014-07-18 8:52 ` Ian Campbell
2014-07-18 11:17 ` Wei Liu
2014-07-29 15:31 ` Ian Jackson
2014-07-29 15:29 ` Ian Jackson
2014-07-10 14:32 ` [PATCH v1 10/10] xl: use libxl_retrieve_domain_configuration and JSON format Wei Liu
2014-07-17 11:13 ` Ian Campbell
2014-07-17 12:14 ` Wei Liu
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1405002745-5034-3-git-send-email-wei.liu2@citrix.com \
--to=wei.liu2@citrix.com \
--cc=ian.campbell@citrix.com \
--cc=ian.jackson@eu.citrix.com \
--cc=xen-devel@lists.xen.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).