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,libxl_uuid * uuid)10 int gen_stub_json_config(uint32_t domid, libxl_uuid *uuid)
11 {
12 int rc = 1;
13 xentoollog_logger_stdiostream *logger;
14 libxl_ctx *ctx;
15 libxl_domain_config dom_config;
16 libxl_dominfo dominfo;
17 char *json = NULL;
18
19 logger = xtl_createlogger_stdiostream(stderr, XTL_ERROR, 0);
20 if (!logger)
21 return 1;
22
23 if (libxl_ctx_alloc(&ctx, LIBXL_VERSION, 0,
24 (xentoollog_logger *)logger)) {
25 fprintf(stderr, "cannot init libxl context\n");
26 goto outlog;
27 }
28
29 libxl_domain_config_init(&dom_config);
30
31 libxl_dominfo_init(&dominfo);
32 if (libxl_domain_info(ctx, &dominfo, domid)) {
33 fprintf(stderr, "cannot get domain type\n");
34 goto outdispose;
35 }
36
37 /* Generate stub JSON config. */
38 dom_config.c_info.type = (dominfo.domain_type == LIBXL_DOMAIN_TYPE_HVM)
39 ? LIBXL_DOMAIN_TYPE_PVH : LIBXL_DOMAIN_TYPE_PV;
40 libxl_domain_build_info_init_type(&dom_config.b_info,
41 dom_config.c_info.type);
42
43 if (uuid && !libxl_uuid_is_nil(uuid))
44 libxl_uuid_copy(ctx, &dom_config.c_info.uuid, uuid);
45
46 json = libxl_domain_config_to_json(ctx, &dom_config);
47 /* libxl-json format requires the string ends with '\0'. Code
48 * snippet taken from libxl.
49 */
50 rc = libxl_userdata_store(ctx, domid, "libxl-json",
51 (const uint8_t *)json,
52 strlen(json) + 1 /* include '\0' */);
53 if (rc)
54 fprintf(stderr, "cannot store stub json config for domain %u\n", domid);
55
56 outdispose:
57 libxl_dominfo_dispose(&dominfo);
58 libxl_domain_config_dispose(&dom_config);
59 free(json);
60 libxl_ctx_free(ctx);
61 outlog:
62 xtl_logger_destroy((xentoollog_logger *)logger);
63 return rc;
64 }
65
66 /*
67 * Local variables:
68 * mode: C
69 * c-basic-offset: 4
70 * indent-tabs-mode: nil
71 * End:
72 */
73