diff mbox

[v2,11/13] tools: split up xen-init-dom0.c

Message ID 1450444471-6454-12-git-send-email-jgross@suse.com (mailing list archive)
State New, archived
Headers show

Commit Message

Jürgen Groß Dec. 18, 2015, 1:14 p.m. UTC
Split up tools/helpers/xen-init-dom0.c in order to prepare reusing
generation of the json configuration by init-xenstore-domain.c.

Signed-off-by: Juergen Gross <jgross@suse.com>
---
 tools/helpers/Makefile        |  2 +-
 tools/helpers/init-dom-json.c | 59 ++++++++++++++++++++++++++++++++++
 tools/helpers/init-dom-json.h | 18 +++++++++++
 tools/helpers/xen-init-dom0.c | 73 ++++++-------------------------------------
 4 files changed, 88 insertions(+), 64 deletions(-)
 create mode 100644 tools/helpers/init-dom-json.c
 create mode 100644 tools/helpers/init-dom-json.h

Comments

Ian Campbell Jan. 6, 2016, 4:26 p.m. UTC | #1
On Fri, 2015-12-18 at 14:14 +0100, Juergen Gross wrote:
> Split up tools/helpers/xen-init-dom0.c in order to prepare reusing
> generation of the json configuration by init-xenstore-domain.c.
> 
> Signed-off-by: Juergen Gross <jgross@suse.com>

Wei, was there a reason for using atexit() to free the handles which I'm
not seeing? IOW I think there is a single exit path which could have done
all that today, so Juergen is OK to change it in this way.

Ian.


> ---
>  tools/helpers/Makefile        |  2 +-
>  tools/helpers/init-dom-json.c | 59 ++++++++++++++++++++++++++++++++++
>  tools/helpers/init-dom-json.h | 18 +++++++++++
>  tools/helpers/xen-init-dom0.c | 73 ++++++-------------------------------
> ------
>  4 files changed, 88 insertions(+), 64 deletions(-)
>  create mode 100644 tools/helpers/init-dom-json.c
>  create mode 100644 tools/helpers/init-dom-json.h
> 
> diff --git a/tools/helpers/Makefile b/tools/helpers/Makefile
> index 92aead4..cfb86f5 100644
> --- a/tools/helpers/Makefile
> +++ b/tools/helpers/Makefile
> @@ -10,7 +10,7 @@ ifeq ($(CONFIG_Linux),y)
>  PROGS += init-xenstore-domain
>  endif
>  
> -XEN_INIT_DOM0_OBJS = xen-init-dom0.o
> +XEN_INIT_DOM0_OBJS = xen-init-dom0.o init-dom-json.o
>  $(XEN_INIT_DOM0_OBJS): CFLAGS += $(CFLAGS_libxenctrl)
>  $(XEN_INIT_DOM0_OBJS): CFLAGS += $(CFLAGS_libxenstore)
>  $(XEN_INIT_DOM0_OBJS): CFLAGS += $(CFLAGS_libxenlight)
> diff --git a/tools/helpers/init-dom-json.c b/tools/helpers/init-dom-
> json.c
> new file mode 100644
> index 0000000..91b1fdf
> --- /dev/null
> +++ b/tools/helpers/init-dom-json.c
> @@ -0,0 +1,59 @@
> +#include <err.h>
> +#include <stdlib.h>
> +#include <stdint.h>
> +#include <string.h>
> +#include <stdio.h>
> +
> +#include <xenctrl.h>
> +#include <libxl.h>
> +
> +int gen_stub_json_config(uint32_t domid)
> +{
> +    int rc = 1;
> +    xentoollog_logger_stdiostream *logger;
> +    libxl_ctx *ctx;
> +    libxl_domain_config dom_config;
> +    char *json = NULL;
> +
> +    logger = xtl_createlogger_stdiostream(stderr, XTL_ERROR, 0);
> +    if (!logger)
> +        return 1;
> +
> +    if (libxl_ctx_alloc(&ctx, LIBXL_VERSION, 0,
> +                        (xentoollog_logger *)logger)) {
> +        fprintf(stderr, "cannot init libxl context\n");
> +        goto outlog;
> +    }
> +
> +    libxl_domain_config_init(&dom_config);
> +
> +    /* Generate stub JSON config. */
> +    dom_config.c_info.type = LIBXL_DOMAIN_TYPE_PV;
> +    libxl_domain_build_info_init_type(&dom_config.b_info,
> +                                      LIBXL_DOMAIN_TYPE_PV);
> +
> +    json = libxl_domain_config_to_json(ctx, &dom_config);
> +    /* libxl-json format requires the string ends with '\0'. Code
> +     * snippet taken from libxl.
> +     */
> +    rc = libxl_userdata_store(ctx, domid, "libxl-json",
> +                              (const uint8_t *)json,
> +                              strlen(json) + 1 /* include '\0' */);
> +    if (rc)
> +        fprintf(stderr, "cannot store stub json config for domain %u\n",
> domid);
> +
> +    libxl_domain_config_dispose(&dom_config);
> +    free(json);
> +    libxl_ctx_free(ctx);
> +outlog:
> +    xtl_logger_destroy((xentoollog_logger *)logger);
> +    return rc;
> +}
> +
> +/*
> + * Local variables:
> + * mode: C
> + * c-basic-offset: 4
> + * indent-tabs-mode: nil
> + * End:
> + */
> diff --git a/tools/helpers/init-dom-json.h b/tools/helpers/init-dom-
> json.h
> new file mode 100644
> index 0000000..58c85df
> --- /dev/null
> +++ b/tools/helpers/init-dom-json.h
> @@ -0,0 +1,18 @@
> +#ifndef __INIT_DOM_JSON_H
> +#define __INIT_DOM_JSON_H
> +
> +/*
> + * Generate a stub JSON config for a domain with the given domid.
> + */
> +int gen_stub_json_config(uint32_t domid);
> +
> +#endif
> +/*
> + * Local variables:
> + * mode: C
> + * c-file-style: "BSD"
> + * c-basic-offset: 4
> + * tab-width: 4
> + * indent-tabs-mode: nil
> + * End:
> + */
> diff --git a/tools/helpers/xen-init-dom0.c b/tools/helpers/xen-init-
> dom0.c
> index 7925d64..9ab8468 100644
> --- a/tools/helpers/xen-init-dom0.c
> +++ b/tools/helpers/xen-init-dom0.c
> @@ -1,65 +1,26 @@
> -#include <err.h>
>  #include <stdlib.h>
>  #include <stdint.h>
>  #include <string.h>
>  #include <stdio.h>
>  
> -#include <xenctrl.h>
>  #include <xenstore.h>
> -#include <libxl.h>
> +
> +#include "init-dom-json.h"
>  
>  #define DOMNAME_PATH   "/local/domain/0/name"
>  #define DOMID_PATH     "/local/domain/0/domid"
>  
> -static libxl_ctx *ctx;
> -static xentoollog_logger_stdiostream *logger;
> -static struct xs_handle *xsh;
> -
> -static void ctx_alloc(void)
> +int main(int argc, char **argv)
>  {
> -    if (libxl_ctx_alloc(&ctx, LIBXL_VERSION, 0,
> -                        (xentoollog_logger *)logger)) {
> -        fprintf(stderr, "cannot init libxl context\n");
> -        exit(1);
> -    }
> +    int rc;
> +    struct xs_handle *xsh;
> +    char *domname_string = NULL, *domid_string = NULL;
> +
>      xsh = xs_open(0);
>      if (!xsh) {
>          fprintf(stderr, "cannot open xenstore connection\n");
>          exit(1);
>      }
> -}
> -
> -static void ctx_free(void)
> -{
> -    if (ctx) {
> -        libxl_ctx_free(ctx);
> -        ctx = NULL;
> -    }
> -    if (logger) {
> -        xtl_logger_destroy((xentoollog_logger *)logger);
> -        logger = NULL;
> -    }
> -    if (xsh) {
> -        xs_close(xsh);
> -        xsh = NULL;
> -    }
> -}
> -
> -int main(int argc, char **argv)
> -{
> -    int rc;
> -    libxl_domain_config dom0_config;
> -    char *domname_string = NULL, *domid_string = NULL;
> -    char *json = NULL;;
> -
> -    logger = xtl_createlogger_stdiostream(stderr, XTL_ERROR, 0);
> -    if (!logger) exit(1);
> -
> -    atexit(ctx_free);
> -
> -    ctx_alloc();
> -
> -    libxl_domain_config_init(&dom0_config);
>  
>      /* Sanity check: this program can only be run once. */
>      domid_string = xs_read(xsh, XBT_NULL, DOMID_PATH, NULL);
> @@ -70,22 +31,9 @@ int main(int argc, char **argv)
>          goto out;
>      }
>  
> -    /* Generate stub JSON config. */
> -    dom0_config.c_info.type = LIBXL_DOMAIN_TYPE_PV;
> -    libxl_domain_build_info_init_type(&dom0_config.b_info,
> -                                      LIBXL_DOMAIN_TYPE_PV);
> -
> -    json = libxl_domain_config_to_json(ctx, &dom0_config);
> -    /* libxl-json format requires the string ends with '\0'. Code
> -     * snippet taken from libxl.
> -     */
> -    rc = libxl_userdata_store(ctx, 0, "libxl-json",
> -                              (const uint8_t *)json,
> -                              strlen(json) + 1 /* include '\0' */);
> -    if (rc) {
> -        fprintf(stderr, "cannot store stub json config for Dom0\n");
> +    rc = gen_stub_json_config(0);
> +    if (rc)
>          goto out;
> -    }
>  
>      /* Write xenstore entries. */
>      if (!xs_write(xsh, XBT_NULL, DOMID_PATH, "0", strlen("0"))) {
> @@ -104,10 +52,9 @@ int main(int argc, char **argv)
>      printf("Done setting up Dom0\n");
>  
>  out:
> -    libxl_domain_config_dispose(&dom0_config);
>      free(domid_string);
>      free(domname_string);
> -    free(json);
> +    xs_close(xsh);
>      return rc;
>  }
>
Wei Liu Jan. 6, 2016, 4:33 p.m. UTC | #2
On Wed, Jan 06, 2016 at 04:26:10PM +0000, Ian Campbell wrote:
> On Fri, 2015-12-18 at 14:14 +0100, Juergen Gross wrote:
> > Split up tools/helpers/xen-init-dom0.c in order to prepare reusing
> > generation of the json configuration by init-xenstore-domain.c.
> > 
> > Signed-off-by: Juergen Gross <jgross@suse.com>
> 
> Wei, was there a reason for using atexit() to free the handles which I'm
> not seeing? IOW I think there is a single exit path which could have done
> all that today, so Juergen is OK to change it in this way.
> 

No, there isn't particular reason to use atexit. I copied it from xl.
It's OK for Juergen to change it.

Wei.
Ian Campbell Jan. 7, 2016, 10:26 a.m. UTC | #3
On Wed, 2016-01-06 at 16:33 +0000, Wei Liu wrote:
> On Wed, Jan 06, 2016 at 04:26:10PM +0000, Ian Campbell wrote:
> > On Fri, 2015-12-18 at 14:14 +0100, Juergen Gross wrote:
> > > Split up tools/helpers/xen-init-dom0.c in order to prepare reusing
> > > generation of the json configuration by init-xenstore-domain.c.
> > > 
> > > Signed-off-by: Juergen Gross <jgross@suse.com>
> > 
> > Wei, was there a reason for using atexit() to free the handles which
> > I'm
> > not seeing? IOW I think there is a single exit path which could have
> > done
> > all that today, so Juergen is OK to change it in this way.
> > 
> 
> No, there isn't particular reason to use atexit. I copied it from xl.
> It's OK for Juergen to change it.

In which case: Acked-by: Ian Campbell <ian.campbell@citrix.com>

The mixture of logging via xtl and via (f)printf in this code is a bit odd,
but that's unchanged by this patch. Someone who was feeling particularly
enthusiastic might like to make it use xtl throughout and provide a way to
increase the log level.

Ian.
diff mbox

Patch

diff --git a/tools/helpers/Makefile b/tools/helpers/Makefile
index 92aead4..cfb86f5 100644
--- a/tools/helpers/Makefile
+++ b/tools/helpers/Makefile
@@ -10,7 +10,7 @@  ifeq ($(CONFIG_Linux),y)
 PROGS += init-xenstore-domain
 endif
 
-XEN_INIT_DOM0_OBJS = xen-init-dom0.o
+XEN_INIT_DOM0_OBJS = xen-init-dom0.o init-dom-json.o
 $(XEN_INIT_DOM0_OBJS): CFLAGS += $(CFLAGS_libxenctrl)
 $(XEN_INIT_DOM0_OBJS): CFLAGS += $(CFLAGS_libxenstore)
 $(XEN_INIT_DOM0_OBJS): CFLAGS += $(CFLAGS_libxenlight)
diff --git a/tools/helpers/init-dom-json.c b/tools/helpers/init-dom-json.c
new file mode 100644
index 0000000..91b1fdf
--- /dev/null
+++ b/tools/helpers/init-dom-json.c
@@ -0,0 +1,59 @@ 
+#include <err.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+#include <stdio.h>
+
+#include <xenctrl.h>
+#include <libxl.h>
+
+int gen_stub_json_config(uint32_t domid)
+{
+    int rc = 1;
+    xentoollog_logger_stdiostream *logger;
+    libxl_ctx *ctx;
+    libxl_domain_config dom_config;
+    char *json = NULL;
+
+    logger = xtl_createlogger_stdiostream(stderr, XTL_ERROR, 0);
+    if (!logger)
+        return 1;
+
+    if (libxl_ctx_alloc(&ctx, LIBXL_VERSION, 0,
+                        (xentoollog_logger *)logger)) {
+        fprintf(stderr, "cannot init libxl context\n");
+        goto outlog;
+    }
+
+    libxl_domain_config_init(&dom_config);
+
+    /* Generate stub JSON config. */
+    dom_config.c_info.type = LIBXL_DOMAIN_TYPE_PV;
+    libxl_domain_build_info_init_type(&dom_config.b_info,
+                                      LIBXL_DOMAIN_TYPE_PV);
+
+    json = libxl_domain_config_to_json(ctx, &dom_config);
+    /* libxl-json format requires the string ends with '\0'. Code
+     * snippet taken from libxl.
+     */
+    rc = libxl_userdata_store(ctx, domid, "libxl-json",
+                              (const uint8_t *)json,
+                              strlen(json) + 1 /* include '\0' */);
+    if (rc)
+        fprintf(stderr, "cannot store stub json config for domain %u\n", domid);
+
+    libxl_domain_config_dispose(&dom_config);
+    free(json);
+    libxl_ctx_free(ctx);
+outlog:
+    xtl_logger_destroy((xentoollog_logger *)logger);
+    return rc;
+}
+
+/*
+ * Local variables:
+ * mode: C
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/tools/helpers/init-dom-json.h b/tools/helpers/init-dom-json.h
new file mode 100644
index 0000000..58c85df
--- /dev/null
+++ b/tools/helpers/init-dom-json.h
@@ -0,0 +1,18 @@ 
+#ifndef __INIT_DOM_JSON_H
+#define __INIT_DOM_JSON_H
+
+/*
+ * Generate a stub JSON config for a domain with the given domid.
+ */
+int gen_stub_json_config(uint32_t domid);
+
+#endif
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/tools/helpers/xen-init-dom0.c b/tools/helpers/xen-init-dom0.c
index 7925d64..9ab8468 100644
--- a/tools/helpers/xen-init-dom0.c
+++ b/tools/helpers/xen-init-dom0.c
@@ -1,65 +1,26 @@ 
-#include <err.h>
 #include <stdlib.h>
 #include <stdint.h>
 #include <string.h>
 #include <stdio.h>
 
-#include <xenctrl.h>
 #include <xenstore.h>
-#include <libxl.h>
+
+#include "init-dom-json.h"
 
 #define DOMNAME_PATH   "/local/domain/0/name"
 #define DOMID_PATH     "/local/domain/0/domid"
 
-static libxl_ctx *ctx;
-static xentoollog_logger_stdiostream *logger;
-static struct xs_handle *xsh;
-
-static void ctx_alloc(void)
+int main(int argc, char **argv)
 {
-    if (libxl_ctx_alloc(&ctx, LIBXL_VERSION, 0,
-                        (xentoollog_logger *)logger)) {
-        fprintf(stderr, "cannot init libxl context\n");
-        exit(1);
-    }
+    int rc;
+    struct xs_handle *xsh;
+    char *domname_string = NULL, *domid_string = NULL;
+
     xsh = xs_open(0);
     if (!xsh) {
         fprintf(stderr, "cannot open xenstore connection\n");
         exit(1);
     }
-}
-
-static void ctx_free(void)
-{
-    if (ctx) {
-        libxl_ctx_free(ctx);
-        ctx = NULL;
-    }
-    if (logger) {
-        xtl_logger_destroy((xentoollog_logger *)logger);
-        logger = NULL;
-    }
-    if (xsh) {
-        xs_close(xsh);
-        xsh = NULL;
-    }
-}
-
-int main(int argc, char **argv)
-{
-    int rc;
-    libxl_domain_config dom0_config;
-    char *domname_string = NULL, *domid_string = NULL;
-    char *json = NULL;;
-
-    logger = xtl_createlogger_stdiostream(stderr, XTL_ERROR, 0);
-    if (!logger) exit(1);
-
-    atexit(ctx_free);
-
-    ctx_alloc();
-
-    libxl_domain_config_init(&dom0_config);
 
     /* Sanity check: this program can only be run once. */
     domid_string = xs_read(xsh, XBT_NULL, DOMID_PATH, NULL);
@@ -70,22 +31,9 @@  int main(int argc, char **argv)
         goto out;
     }
 
-    /* Generate stub JSON config. */
-    dom0_config.c_info.type = LIBXL_DOMAIN_TYPE_PV;
-    libxl_domain_build_info_init_type(&dom0_config.b_info,
-                                      LIBXL_DOMAIN_TYPE_PV);
-
-    json = libxl_domain_config_to_json(ctx, &dom0_config);
-    /* libxl-json format requires the string ends with '\0'. Code
-     * snippet taken from libxl.
-     */
-    rc = libxl_userdata_store(ctx, 0, "libxl-json",
-                              (const uint8_t *)json,
-                              strlen(json) + 1 /* include '\0' */);
-    if (rc) {
-        fprintf(stderr, "cannot store stub json config for Dom0\n");
+    rc = gen_stub_json_config(0);
+    if (rc)
         goto out;
-    }
 
     /* Write xenstore entries. */
     if (!xs_write(xsh, XBT_NULL, DOMID_PATH, "0", strlen("0"))) {
@@ -104,10 +52,9 @@  int main(int argc, char **argv)
     printf("Done setting up Dom0\n");
 
 out:
-    libxl_domain_config_dispose(&dom0_config);
     free(domid_string);
     free(domname_string);
-    free(json);
+    xs_close(xsh);
     return rc;
 }