/src/qemu/hw/xen/xen-bus-helper.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2018 Citrix Systems Inc. |
3 | | * |
4 | | * This work is licensed under the terms of the GNU GPL, version 2 or later. |
5 | | * See the COPYING file in the top-level directory. |
6 | | */ |
7 | | |
8 | | #include "qemu/osdep.h" |
9 | | #include "hw/xen/xen.h" |
10 | | #include "hw/xen/xen-bus.h" |
11 | | #include "hw/xen/xen-bus-helper.h" |
12 | | #include "qapi/error.h" |
13 | | #include "trace.h" |
14 | | |
15 | | #include <glib/gprintf.h> |
16 | | |
17 | | struct xs_state { |
18 | | enum xenbus_state statenum; |
19 | | const char *statestr; |
20 | | }; |
21 | | #define XS_STATE(state) { state, #state } |
22 | | |
23 | | static struct xs_state xs_state[] = { |
24 | | XS_STATE(XenbusStateUnknown), |
25 | | XS_STATE(XenbusStateInitialising), |
26 | | XS_STATE(XenbusStateInitWait), |
27 | | XS_STATE(XenbusStateInitialised), |
28 | | XS_STATE(XenbusStateConnected), |
29 | | XS_STATE(XenbusStateClosing), |
30 | | XS_STATE(XenbusStateClosed), |
31 | | XS_STATE(XenbusStateReconfiguring), |
32 | | XS_STATE(XenbusStateReconfigured), |
33 | | }; |
34 | | |
35 | | #undef XS_STATE |
36 | | |
37 | | const char *xs_strstate(enum xenbus_state state) |
38 | 0 | { |
39 | 0 | unsigned int i; |
40 | |
|
41 | 0 | for (i = 0; i < ARRAY_SIZE(xs_state); i++) { |
42 | 0 | if (xs_state[i].statenum == state) { |
43 | 0 | return xs_state[i].statestr; |
44 | 0 | } |
45 | 0 | } |
46 | | |
47 | 0 | return "INVALID"; |
48 | 0 | } |
49 | | |
50 | | void xs_node_create(struct qemu_xs_handle *h, xs_transaction_t tid, |
51 | | const char *node, unsigned int owner, unsigned int domid, |
52 | | unsigned int perms, Error **errp) |
53 | 0 | { |
54 | 0 | trace_xs_node_create(node); |
55 | |
|
56 | 0 | if (!qemu_xen_xs_create(h, tid, owner, domid, perms, node)) { |
57 | 0 | error_setg_errno(errp, errno, "failed to create node '%s'", node); |
58 | 0 | } |
59 | 0 | } |
60 | | |
61 | | void xs_node_destroy(struct qemu_xs_handle *h, xs_transaction_t tid, |
62 | | const char *node, Error **errp) |
63 | 0 | { |
64 | 0 | trace_xs_node_destroy(node); |
65 | |
|
66 | 0 | if (!qemu_xen_xs_destroy(h, tid, node)) { |
67 | 0 | error_setg_errno(errp, errno, "failed to destroy node '%s'", node); |
68 | 0 | } |
69 | 0 | } |
70 | | |
71 | | void xs_node_vprintf(struct qemu_xs_handle *h, xs_transaction_t tid, |
72 | | const char *node, const char *key, Error **errp, |
73 | | const char *fmt, va_list ap) |
74 | 0 | { |
75 | 0 | char *path, *value; |
76 | 0 | int len; |
77 | |
|
78 | 0 | path = (strlen(node) != 0) ? g_strdup_printf("%s/%s", node, key) : |
79 | 0 | g_strdup(key); |
80 | 0 | len = g_vasprintf(&value, fmt, ap); |
81 | |
|
82 | 0 | trace_xs_node_vprintf(path, value); |
83 | |
|
84 | 0 | if (!qemu_xen_xs_write(h, tid, path, value, len)) { |
85 | 0 | error_setg_errno(errp, errno, "failed to write '%s' to '%s'", |
86 | 0 | value, path); |
87 | 0 | } |
88 | |
|
89 | 0 | g_free(value); |
90 | 0 | g_free(path); |
91 | 0 | } |
92 | | |
93 | | void xs_node_printf(struct qemu_xs_handle *h, xs_transaction_t tid, |
94 | | const char *node, const char *key, Error **errp, |
95 | | const char *fmt, ...) |
96 | 0 | { |
97 | 0 | va_list ap; |
98 | |
|
99 | 0 | va_start(ap, fmt); |
100 | 0 | xs_node_vprintf(h, tid, node, key, errp, fmt, ap); |
101 | 0 | va_end(ap); |
102 | 0 | } |
103 | | |
104 | | int xs_node_vscanf(struct qemu_xs_handle *h, xs_transaction_t tid, |
105 | | const char *node, const char *key, Error **errp, |
106 | | const char *fmt, va_list ap) |
107 | 0 | { |
108 | 0 | char *value; |
109 | 0 | int rc; |
110 | |
|
111 | 0 | if (node && strlen(node) != 0) { |
112 | 0 | value = xs_node_read(h, tid, NULL, errp, "%s/%s", node, key); |
113 | 0 | } else { |
114 | 0 | value = xs_node_read(h, tid, NULL, errp, "%s", key); |
115 | 0 | } |
116 | |
|
117 | 0 | if (value) { |
118 | 0 | rc = vsscanf(value, fmt, ap); |
119 | 0 | } else { |
120 | 0 | rc = EOF; |
121 | 0 | } |
122 | |
|
123 | 0 | free(value); |
124 | |
|
125 | 0 | return rc; |
126 | 0 | } |
127 | | |
128 | | int xs_node_scanf(struct qemu_xs_handle *h, xs_transaction_t tid, |
129 | | const char *node, const char *key, Error **errp, |
130 | | const char *fmt, ...) |
131 | 0 | { |
132 | 0 | va_list ap; |
133 | 0 | int rc; |
134 | |
|
135 | 0 | va_start(ap, fmt); |
136 | 0 | rc = xs_node_vscanf(h, tid, node, key, errp, fmt, ap); |
137 | 0 | va_end(ap); |
138 | |
|
139 | 0 | return rc; |
140 | 0 | } |
141 | | |
142 | | char *xs_node_read(struct qemu_xs_handle *h, xs_transaction_t tid, |
143 | | unsigned int *len, Error **errp, |
144 | | const char *path_fmt, ...) |
145 | 0 | { |
146 | 0 | char *path, *value; |
147 | 0 | va_list ap; |
148 | |
|
149 | 0 | va_start(ap, path_fmt); |
150 | 0 | path = g_strdup_vprintf(path_fmt, ap); |
151 | 0 | va_end(ap); |
152 | |
|
153 | 0 | value = qemu_xen_xs_read(h, tid, path, len); |
154 | 0 | trace_xs_node_read(path, value ? value : "<null>"); |
155 | 0 | if (!value) { |
156 | 0 | error_setg_errno(errp, errno, "failed to read from '%s'", path); |
157 | 0 | } |
158 | |
|
159 | 0 | g_free(path); |
160 | |
|
161 | 0 | return value; |
162 | 0 | } |
163 | | |
164 | | struct qemu_xs_watch *xs_node_watch(struct qemu_xs_handle *h, const char *node, |
165 | | const char *key, xs_watch_fn fn, |
166 | | void *opaque, Error **errp) |
167 | 0 | { |
168 | 0 | char *path; |
169 | 0 | struct qemu_xs_watch *w; |
170 | |
|
171 | 0 | path = (strlen(node) != 0) ? g_strdup_printf("%s/%s", node, key) : |
172 | 0 | g_strdup(key); |
173 | |
|
174 | 0 | trace_xs_node_watch(path); |
175 | |
|
176 | 0 | w = qemu_xen_xs_watch(h, path, fn, opaque); |
177 | 0 | if (!w) { |
178 | 0 | error_setg_errno(errp, errno, "failed to watch node '%s'", path); |
179 | 0 | } |
180 | |
|
181 | 0 | g_free(path); |
182 | |
|
183 | 0 | return w; |
184 | 0 | } |
185 | | |
186 | | void xs_node_unwatch(struct qemu_xs_handle *h, struct qemu_xs_watch *w) |
187 | 0 | { |
188 | 0 | qemu_xen_xs_unwatch(h, w); |
189 | 0 | } |