2 * Copyright (c) 2018 Citrix Systems Inc.
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.
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"
14 #include <glib/gprintf.h>
17 enum xenbus_state statenum
;
20 #define XS_STATE(state) { state, #state }
22 static struct xs_state xs_state
[] = {
23 XS_STATE(XenbusStateUnknown
),
24 XS_STATE(XenbusStateInitialising
),
25 XS_STATE(XenbusStateInitWait
),
26 XS_STATE(XenbusStateInitialised
),
27 XS_STATE(XenbusStateConnected
),
28 XS_STATE(XenbusStateClosing
),
29 XS_STATE(XenbusStateClosed
),
30 XS_STATE(XenbusStateReconfiguring
),
31 XS_STATE(XenbusStateReconfigured
),
36 const char *xs_strstate(enum xenbus_state state
)
40 for (i
= 0; i
< ARRAY_SIZE(xs_state
); i
++) {
41 if (xs_state
[i
].statenum
== state
) {
42 return xs_state
[i
].statestr
;
49 void xs_node_create(struct xs_handle
*xsh
, xs_transaction_t tid
,
50 const char *node
, struct xs_permissions perms
[],
51 unsigned int nr_perms
, Error
**errp
)
53 trace_xs_node_create(node
);
55 if (!xs_write(xsh
, tid
, node
, "", 0)) {
56 error_setg_errno(errp
, errno
, "failed to create node '%s'", node
);
60 if (!xs_set_permissions(xsh
, tid
, node
, perms
, nr_perms
)) {
61 error_setg_errno(errp
, errno
, "failed to set node '%s' permissions",
66 void xs_node_destroy(struct xs_handle
*xsh
, xs_transaction_t tid
,
67 const char *node
, Error
**errp
)
69 trace_xs_node_destroy(node
);
71 if (!xs_rm(xsh
, tid
, node
)) {
72 error_setg_errno(errp
, errno
, "failed to destroy node '%s'", node
);
76 void xs_node_vprintf(struct xs_handle
*xsh
, xs_transaction_t tid
,
77 const char *node
, const char *key
, Error
**errp
,
78 const char *fmt
, va_list ap
)
83 path
= (strlen(node
) != 0) ? g_strdup_printf("%s/%s", node
, key
) :
85 len
= g_vasprintf(&value
, fmt
, ap
);
87 trace_xs_node_vprintf(path
, value
);
89 if (!xs_write(xsh
, tid
, path
, value
, len
)) {
90 error_setg_errno(errp
, errno
, "failed to write '%s' to '%s'",
98 void xs_node_printf(struct xs_handle
*xsh
, xs_transaction_t tid
,
99 const char *node
, const char *key
, Error
**errp
,
100 const char *fmt
, ...)
105 xs_node_vprintf(xsh
, tid
, node
, key
, errp
, fmt
, ap
);
109 int xs_node_vscanf(struct xs_handle
*xsh
, xs_transaction_t tid
,
110 const char *node
, const char *key
, Error
**errp
,
111 const char *fmt
, va_list ap
)
116 path
= (strlen(node
) != 0) ? g_strdup_printf("%s/%s", node
, key
) :
118 value
= xs_read(xsh
, tid
, path
, NULL
);
120 trace_xs_node_vscanf(path
, value
);
123 rc
= vsscanf(value
, fmt
, ap
);
125 error_setg_errno(errp
, errno
, "failed to read from '%s'",
136 int xs_node_scanf(struct xs_handle
*xsh
, xs_transaction_t tid
,
137 const char *node
, const char *key
, Error
**errp
,
138 const char *fmt
, ...)
144 rc
= xs_node_vscanf(xsh
, tid
, node
, key
, errp
, fmt
, ap
);
150 void xs_node_watch(struct xs_handle
*xsh
, const char *node
, const char *key
,
151 char *token
, Error
**errp
)
155 path
= (strlen(node
) != 0) ? g_strdup_printf("%s/%s", node
, key
) :
158 trace_xs_node_watch(path
);
160 if (!xs_watch(xsh
, path
, token
)) {
161 error_setg_errno(errp
, errno
, "failed to watch node '%s'", path
);
167 void xs_node_unwatch(struct xs_handle
*xsh
, const char *node
,
168 const char *key
, const char *token
, Error
**errp
)
172 path
= (strlen(node
) != 0) ? g_strdup_printf("%s/%s", node
, key
) :
175 trace_xs_node_unwatch(path
);
177 if (!xs_unwatch(xsh
, path
, token
)) {
178 error_setg_errno(errp
, errno
, "failed to unwatch node '%s'", path
);