]> git.proxmox.com Git - mirror_qemu.git/blob - hw/xen/xen_pvdev.c
xen: Create a new file xen_pvdev.c
[mirror_qemu.git] / hw / xen / xen_pvdev.c
1 /*
2 * Xen para-virtualization device
3 *
4 * (c) 2008 Gerd Hoffmann <kraxel@redhat.com>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>
18 */
19
20 #include "qemu/osdep.h"
21
22 #include "hw/xen/xen_backend.h"
23 #include "hw/xen/xen_pvdev.h"
24
25 static int debug;
26 /* ------------------------------------------------------------- */
27
28 int xenstore_write_str(const char *base, const char *node, const char *val)
29 {
30 char abspath[XEN_BUFSIZE];
31
32 snprintf(abspath, sizeof(abspath), "%s/%s", base, node);
33 if (!xs_write(xenstore, 0, abspath, val, strlen(val))) {
34 return -1;
35 }
36 return 0;
37 }
38
39 char *xenstore_read_str(const char *base, const char *node)
40 {
41 char abspath[XEN_BUFSIZE];
42 unsigned int len;
43 char *str, *ret = NULL;
44
45 snprintf(abspath, sizeof(abspath), "%s/%s", base, node);
46 str = xs_read(xenstore, 0, abspath, &len);
47 if (str != NULL) {
48 /* move to qemu-allocated memory to make sure
49 * callers can savely g_free() stuff. */
50 ret = g_strdup(str);
51 free(str);
52 }
53 return ret;
54 }
55
56 int xenstore_write_int(const char *base, const char *node, int ival)
57 {
58 char val[12];
59
60 snprintf(val, sizeof(val), "%d", ival);
61 return xenstore_write_str(base, node, val);
62 }
63
64 int xenstore_write_int64(const char *base, const char *node, int64_t ival)
65 {
66 char val[21];
67
68 snprintf(val, sizeof(val), "%"PRId64, ival);
69 return xenstore_write_str(base, node, val);
70 }
71
72 int xenstore_read_int(const char *base, const char *node, int *ival)
73 {
74 char *val;
75 int rc = -1;
76
77 val = xenstore_read_str(base, node);
78 if (val && 1 == sscanf(val, "%d", ival)) {
79 rc = 0;
80 }
81 g_free(val);
82 return rc;
83 }
84
85 int xenstore_read_uint64(const char *base, const char *node, uint64_t *uval)
86 {
87 char *val;
88 int rc = -1;
89
90 val = xenstore_read_str(base, node);
91 if (val && 1 == sscanf(val, "%"SCNu64, uval)) {
92 rc = 0;
93 }
94 g_free(val);
95 return rc;
96 }
97
98 const char *xenbus_strstate(enum xenbus_state state)
99 {
100 static const char *const name[] = {
101 [XenbusStateUnknown] = "Unknown",
102 [XenbusStateInitialising] = "Initialising",
103 [XenbusStateInitWait] = "InitWait",
104 [XenbusStateInitialised] = "Initialised",
105 [XenbusStateConnected] = "Connected",
106 [XenbusStateClosing] = "Closing",
107 [XenbusStateClosed] = "Closed",
108 };
109 return (state < ARRAY_SIZE(name)) ? name[state] : "INVALID";
110 }
111
112 /*
113 * msg_level:
114 * 0 == errors (stderr + logfile).
115 * 1 == informative debug messages (logfile only).
116 * 2 == noisy debug messages (logfile only).
117 * 3 == will flood your log (logfile only).
118 */
119 void xen_be_printf(struct XenDevice *xendev, int msg_level,
120 const char *fmt, ...)
121 {
122 va_list args;
123
124 if (xendev) {
125 if (msg_level > xendev->debug) {
126 return;
127 }
128 qemu_log("xen be: %s: ", xendev->name);
129 if (msg_level == 0) {
130 fprintf(stderr, "xen be: %s: ", xendev->name);
131 }
132 } else {
133 if (msg_level > debug) {
134 return;
135 }
136 qemu_log("xen be core: ");
137 if (msg_level == 0) {
138 fprintf(stderr, "xen be core: ");
139 }
140 }
141 va_start(args, fmt);
142 qemu_log_vprintf(fmt, args);
143 va_end(args);
144 if (msg_level == 0) {
145 va_start(args, fmt);
146 vfprintf(stderr, fmt, args);
147 va_end(args);
148 }
149 qemu_log_flush();
150 }