[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Xen-devel] [PATCH v2 11/13] tools: split up xen-init-dom0.c



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@xxxxxxxx>

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;
> Â}
> Â
_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxx
http://lists.xen.org/xen-devel

 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.