1 #include <err.h>
2 #include <stdlib.h>
3 #include <stdint.h>
4 #include <string.h>
5 #include <stdio.h>
6 
7 #include <xenctrl.h>
8 #include <libxl.h>
9 
gen_stub_json_config(uint32_t domid)10 int gen_stub_json_config(uint32_t domid)
11 {
12     int rc = 1;
13     xentoollog_logger_stdiostream *logger;
14     libxl_ctx *ctx;
15     libxl_domain_config dom_config;
16     char *json = NULL;
17 
18     logger = xtl_createlogger_stdiostream(stderr, XTL_ERROR, 0);
19     if (!logger)
20         return 1;
21 
22     if (libxl_ctx_alloc(&ctx, LIBXL_VERSION, 0,
23                         (xentoollog_logger *)logger)) {
24         fprintf(stderr, "cannot init libxl context\n");
25         goto outlog;
26     }
27 
28     libxl_domain_config_init(&dom_config);
29 
30     /* Generate stub JSON config. */
31     dom_config.c_info.type = LIBXL_DOMAIN_TYPE_PV;
32     libxl_domain_build_info_init_type(&dom_config.b_info,
33                                       LIBXL_DOMAIN_TYPE_PV);
34 
35     json = libxl_domain_config_to_json(ctx, &dom_config);
36     /* libxl-json format requires the string ends with '\0'. Code
37      * snippet taken from libxl.
38      */
39     rc = libxl_userdata_store(ctx, domid, "libxl-json",
40                               (const uint8_t *)json,
41                               strlen(json) + 1 /* include '\0' */);
42     if (rc)
43         fprintf(stderr, "cannot store stub json config for domain %u\n", domid);
44 
45     libxl_domain_config_dispose(&dom_config);
46     free(json);
47     libxl_ctx_free(ctx);
48 outlog:
49     xtl_logger_destroy((xentoollog_logger *)logger);
50     return rc;
51 }
52 
53 /*
54  * Local variables:
55  * mode: C
56  * c-basic-offset: 4
57  * indent-tabs-mode: nil
58  * End:
59  */
60