All of lore.kernel.org
 help / color / mirror / Atom feed
From: Juergen Gross <jgross@suse.com>
To: xen-devel@lists.xen.org, Ian.Campbell@citrix.com,
	ian.jackson@eu.citrix.com, stefano.stabellini@eu.citrix.com,
	wei.liu2@citrix.com, dgdegra@tycho.nsa.gov
Cc: Juergen Gross <jgross@suse.com>
Subject: [PATCH v3 12/15] tools: split up xen-init-dom0.c
Date: Fri,  8 Jan 2016 14:08:43 +0100	[thread overview]
Message-ID: <1452258526-4797-13-git-send-email-jgross@suse.com> (raw)
In-Reply-To: <1452258526-4797-1-git-send-email-jgross@suse.com>

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>
Acked-by: Ian Campbell <ian.campbell@citrix.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

diff --git a/tools/helpers/Makefile b/tools/helpers/Makefile
index 826e0ed..0325ba2 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_libxentoollog)
 $(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 2f7aa7c..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 <xentoollog.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;
 }
 
-- 
2.6.2

  parent reply	other threads:[~2016-01-08 13:08 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-01-08 13:08 [PATCH v3 00/15] xenstore: make it easier to run xenstore in a domain Juergen Gross
2016-01-08 13:08 ` [PATCH v3 01/15] xen: add xenstore domain flag to hypervisor Juergen Gross
2016-01-08 14:07   ` Jan Beulich
2016-01-08 16:13     ` David Vrabel
     [not found]   ` <568FD0B902000078000C4E8F@suse.com>
2016-01-08 14:22     ` Juergen Gross
2016-01-08 14:25       ` Andrew Cooper
2016-01-08 16:03       ` Jan Beulich
     [not found]       ` <568FEBE402000078000C500D@suse.com>
2016-01-08 16:10         ` Juergen Gross
2016-01-15 15:50   ` Ian Campbell
2016-01-08 13:08 ` [PATCH v3 02/15] libxc: support new xenstore domain flag in libxc Juergen Gross
2016-01-08 13:08 ` [PATCH v3 03/15] libxl: provide a flag in dominfo to avoid stopping it Juergen Gross
2016-01-15 15:51   ` Ian Campbell
2016-01-08 13:08 ` [PATCH v3 04/15] xenstore: move init-xenstore-domain to tools/helpers Juergen Gross
2016-01-15 15:51   ` Ian Campbell
2016-01-08 13:08 ` [PATCH v3 05/15] xenstore: adjust coding style of init-xenstore-domain.c Juergen Gross
2016-01-15 15:54   ` Ian Campbell
2016-01-08 13:08 ` [PATCH v3 06/15] libxl: move xen-init-dom0 to tools/helpers Juergen Gross
2016-01-15 15:55   ` Ian Campbell
2016-01-08 13:08 ` [PATCH v3 07/15] xenstore: destroy xenstore domain in case of error after creating it Juergen Gross
2016-01-15 16:02   ` Ian Campbell
2016-01-08 13:08 ` [PATCH v3 08/15] xenstore: add error messages to init-xenstore-domain Juergen Gross
2016-01-08 13:08 ` [PATCH v3 09/15] xenstore: modify init-xenstore-domain parameter syntax Juergen Gross
2016-01-08 13:08 ` [PATCH v3 10/15] xenstore: make use of the "xenstore domain" flag Juergen Gross
2016-01-15 16:03   ` Ian Campbell
2016-01-08 13:08 ` [PATCH v3 11/15] xenstore: add init-xenstore-domain parameter to specify cmdline Juergen Gross
2016-01-08 13:08 ` Juergen Gross [this message]
2016-01-08 13:08 ` [PATCH v3 13/15] xenstore: write xenstore domain data to xenstore Juergen Gross
2016-01-08 13:08 ` [PATCH v3 14/15] tools: don't stop xenstore domain when stopping dom0 Juergen Gross
2016-01-15 16:12   ` Ian Campbell
2016-01-15 16:24     ` Juergen Gross
2016-01-08 13:08 ` [PATCH v3 15/15] docs: document xenstore domain xenstore paths Juergen Gross
2016-01-15 16:14   ` Ian Campbell
2016-01-15 16:22     ` Juergen Gross
2016-01-15 16:27       ` Ian Campbell
2016-01-15 15:04 ` [PATCH v3 00/15] xenstore: make it easier to run xenstore in a domain Juergen Gross

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=1452258526-4797-13-git-send-email-jgross@suse.com \
    --to=jgross@suse.com \
    --cc=Ian.Campbell@citrix.com \
    --cc=dgdegra@tycho.nsa.gov \
    --cc=ian.jackson@eu.citrix.com \
    --cc=stefano.stabellini@eu.citrix.com \
    --cc=wei.liu2@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.