diff mbox

[RFC,3/4] xl: introduce facility to run function with per-domain lock held

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

Commit Message

Wei Liu June 14, 2017, 5:19 p.m. UTC
Signed-off-by: Wei Liu <wei.liu2@citrix.com>
---
 tools/xl/xl.h       |  1 +
 tools/xl/xl_utils.c | 19 +++++++++++++++++++
 tools/xl/xl_utils.h |  3 +++
 3 files changed, 23 insertions(+)

Comments

Wei Liu June 14, 2017, 11:24 p.m. UTC | #1
On Wed, Jun 14, 2017 at 06:19:20PM +0100, Wei Liu wrote:
> Signed-off-by: Wei Liu <wei.liu2@citrix.com>
> ---
>  tools/xl/xl.h       |  1 +
>  tools/xl/xl_utils.c | 19 +++++++++++++++++++
>  tools/xl/xl_utils.h |  3 +++
>  3 files changed, 23 insertions(+)
> 
> diff --git a/tools/xl/xl.h b/tools/xl/xl.h
> index 93ec4d7e4c..8d667ff444 100644
> --- a/tools/xl/xl.h
> +++ b/tools/xl/xl.h
> @@ -292,6 +292,7 @@ extern void printf_info_sexp(int domid, libxl_domain_config *d_config, FILE *fh)
>  
>  #define XL_GLOBAL_CONFIG XEN_CONFIG_DIR "/xl.conf"
>  #define XL_LOCK_FILE XEN_LOCK_DIR "/xl"
> +#define XL_DOMAIN_LOCK_FILE_FMT XEN_LOCK_DIR "/xl-%u"
>  
>  #endif /* XL_H */
>  
> diff --git a/tools/xl/xl_utils.c b/tools/xl/xl_utils.c
> index e7038ec324..bb32ba0a1f 100644
> --- a/tools/xl/xl_utils.c
> +++ b/tools/xl/xl_utils.c
> @@ -27,6 +27,25 @@
>  #include "xl.h"
>  #include "xl_utils.h"
>  
> +int with_lock(uint32_t domid, domain_fn fn, void *arg)
> +{
> +    char filename[sizeof(XL_DOMAIN_LOCK_FILE_FMT)+15];
> +    int fd_lock = -1;
> +    int rc;
> +
> +    snprintf(filename, sizeof(filename), XL_DOMAIN_LOCK_FILE_FMT, domid);
> +
> +    rc = acquire_lock(filename, &fd_lock);
> +    if (rc) goto out;
> +

It is necessary to check if the domain is still valid here. And we
should probably accept a string instead of domid in this function and
call find_domain, so that we can retry. Basically:

   retry:
       domid = find_domain();
       snprintf(...)
       rc = acquire_lock()
       if (rc) goto out;

       if (domain is not valid anymore) {
           release_lock();
           goto retry;
       }

       /* ... the rest ...*/
diff mbox

Patch

diff --git a/tools/xl/xl.h b/tools/xl/xl.h
index 93ec4d7e4c..8d667ff444 100644
--- a/tools/xl/xl.h
+++ b/tools/xl/xl.h
@@ -292,6 +292,7 @@  extern void printf_info_sexp(int domid, libxl_domain_config *d_config, FILE *fh)
 
 #define XL_GLOBAL_CONFIG XEN_CONFIG_DIR "/xl.conf"
 #define XL_LOCK_FILE XEN_LOCK_DIR "/xl"
+#define XL_DOMAIN_LOCK_FILE_FMT XEN_LOCK_DIR "/xl-%u"
 
 #endif /* XL_H */
 
diff --git a/tools/xl/xl_utils.c b/tools/xl/xl_utils.c
index e7038ec324..bb32ba0a1f 100644
--- a/tools/xl/xl_utils.c
+++ b/tools/xl/xl_utils.c
@@ -27,6 +27,25 @@ 
 #include "xl.h"
 #include "xl_utils.h"
 
+int with_lock(uint32_t domid, domain_fn fn, void *arg)
+{
+    char filename[sizeof(XL_DOMAIN_LOCK_FILE_FMT)+15];
+    int fd_lock = -1;
+    int rc;
+
+    snprintf(filename, sizeof(filename), XL_DOMAIN_LOCK_FILE_FMT, domid);
+
+    rc = acquire_lock(filename, &fd_lock);
+    if (rc) goto out;
+
+    rc = fn(arg);
+
+    release_lock(filename, &fd_lock);
+
+out:
+    return rc;
+}
+
 void dolog(const char *file, int line, const char *func, char *fmt, ...)
 {
     va_list ap;
diff --git a/tools/xl/xl_utils.h b/tools/xl/xl_utils.h
index 18280d7e84..5e0d502fa6 100644
--- a/tools/xl/xl_utils.h
+++ b/tools/xl/xl_utils.h
@@ -149,6 +149,9 @@  int do_daemonize(char *name, const char *pidfile);
 
 int acquire_lock(const char *lockfile, int *fd_lock);
 int release_lock(const char *lockfile, int *fd_lock);
+
+typedef int (*domain_fn)(void *arg);
+int with_lock(uint32_t domid, domain_fn fn, void *arg);
 #endif /* XL_UTILS_H */
 
 /*