source: trunk/packages/xen-3.1/xen-3.1/tools/ioemu/patches/xenstore-device-info-functions @ 34

Last change on this file since 34 was 34, checked in by hartmans, 18 years ago

Add xen and xen-common

File size: 5.8 KB
Line 
1# HG changeset patch
2# User kaf24@localhost.localdomain
3# Node ID bbcac2aea0e8196cd75a3bf6dbe57bebf8c1e5b2
4# Parent  dc973fe5633386547ce5bc8fd4cf5f2bb5b55174
5[QEMU] Helper functions to interface with the xenstore and read device information from it.
6
7 - detect what types of devices a domain has or whether a domain has a
8   device of a certain type
9 - read the content of a variable related to a device, i.e.,
10   hotplug-status
11 - subscribe to changes of the hotplug status of a device for not
12   having to poll the status
13
14Signed-off-by: Stefan Berger <stefanb@us.ibm.com>
15
16Index: ioemu/xenstore.c
17===================================================================
18--- ioemu.orig/xenstore.c       2007-05-03 15:21:22.000000000 +0100
19+++ ioemu/xenstore.c    2007-05-03 15:22:05.000000000 +0100
20@@ -304,6 +304,143 @@
21     return rc;
22 }
23 
24+
25+/*
26+ * get all device instances of a certain type
27+ */
28+char **xenstore_domain_get_devices(struct xs_handle *handle,
29+                                   const char *devtype, unsigned int *num)
30+{
31+    char *path;
32+    char *buf = NULL;
33+    char **e  = NULL;
34+
35+    path = xs_get_domain_path(handle, domid);
36+    if (path == NULL)
37+        goto out;
38+
39+    if (pasprintf(&buf, "%s/device/%s", path,devtype) == -1)
40+        goto out;
41+
42+    e = xs_directory(handle, XBT_NULL, buf, num);
43+
44+ out:
45+    free(path);
46+    free(buf);
47+    return e;
48+}
49+
50+/*
51+ * Check whether a domain has devices of the given type
52+ */
53+int xenstore_domain_has_devtype(struct xs_handle *handle, const char *devtype)
54+{
55+    int rc = 0;
56+    unsigned int num;
57+    char **e = xenstore_domain_get_devices(handle, devtype, &num);
58+    if (e)
59+        rc = 1;
60+    free(e);
61+    return rc;
62+}
63+
64+/*
65+ * Function that creates a path to a variable of an instance of a
66+ * certain device
67+ */
68+static char *get_device_variable_path(const char *devtype, const char *inst,
69+                                      const char *var)
70+{
71+    char *buf = NULL;
72+    if (pasprintf(&buf, "/local/domain/0/backend/%s/%d/%s/%s",
73+                  devtype,
74+                  domid,
75+                  inst,
76+                  var) == -1) {
77+        free(buf);
78+        buf = NULL;
79+    }
80+    return buf;
81+}
82+
83+char *xenstore_backend_read_variable(struct xs_handle *handle,
84+                                     const char *devtype, const char *inst,
85+                                     const char *var)
86+{
87+    char *value = NULL;
88+    char *buf = NULL;
89+    unsigned int len;
90+
91+    buf = get_device_variable_path(devtype, inst, var);
92+    if (NULL == buf)
93+        goto out;
94+
95+    value = xs_read(handle, XBT_NULL, buf, &len);
96+
97+    free(buf);
98+
99+ out:
100+    return value;
101+}
102+
103+/*
104+  Read the hotplug status variable from the backend given the type
105+  of device and its instance.
106+*/
107+char *xenstore_read_hotplug_status(struct xs_handle *handle,
108+                                   const char *devtype, const char *inst)
109+{
110+    return xenstore_backend_read_variable(handle, devtype, inst,
111+                                          "hotplug-status");
112+}
113+
114+/*
115+   Subscribe to the hotplug status of a device given the type of device and
116+   its instance.
117+   In case an error occurrs, a negative number is returned.
118+ */
119+int xenstore_subscribe_to_hotplug_status(struct xs_handle *handle,
120+                                         const char *devtype,
121+                                         const char *inst,
122+                                         const char *token)
123+{
124+    int rc = 0;
125+    char *path = get_device_variable_path(devtype, inst, "hotplug-status");
126+
127+    if (path == NULL)
128+        return -1;
129+
130+    if (0 == xs_watch(handle, path, token))
131+        rc = -2;
132+
133+    free(path);
134+
135+    return rc;
136+}
137+
138+/*
139+ * Unsubscribe from a subscription to the status of a hotplug variable of
140+ * a device.
141+ */
142+int xenstore_unsubscribe_from_hotplug_status(struct xs_handle *handle,
143+                                             const char *devtype,
144+                                             const char *inst,
145+                                             const char *token)
146+{
147+    int rc = 0;
148+    char *path;
149+    path = get_device_variable_path(devtype, inst, "hotplug-status");
150+    if (path == NULL)
151+        return -1;
152+
153+    if (0 == xs_unwatch(handle, path, token))
154+        rc = -2;
155+
156+    free(path);
157+
158+    return rc;
159+}
160+
161 char *xenstore_vm_read(int domid, char *key, int *len)
162 {
163     char *buf = NULL, *path = NULL, *value = NULL;
164Index: ioemu/vl.h
165===================================================================
166--- ioemu.orig/vl.h     2007-05-03 15:21:09.000000000 +0100
167+++ ioemu/vl.h  2007-05-03 15:21:47.000000000 +0100
168@@ -1217,6 +1217,24 @@
169 void xenstore_write_vncport(int vnc_display);
170 int xenstore_read_vncpasswd(int domid);
171 
172+int xenstore_domain_has_devtype(struct xs_handle *handle,
173+                                const char *devtype);
174+char **xenstore_domain_get_devices(struct xs_handle *handle,
175+                                   const char *devtype, unsigned int *num);
176+char *xenstore_read_hotplug_status(struct xs_handle *handle,
177+                                   const char *devtype, const char *inst);
178+char *xenstore_backend_read_variable(struct xs_handle *,
179+                                     const char *devtype, const char *inst,
180+                                     const char *var);
181+int xenstore_subscribe_to_hotplug_status(struct xs_handle *handle,
182+                                         const char *devtype,
183+                                         const char *inst,
184+                                         const char *token);
185+int xenstore_unsubscribe_from_hotplug_status(struct xs_handle *handle,
186+                                             const char *devtype,
187+                                             const char *inst,
188+                                             const char *token);
189+
190 int xenstore_vm_write(int domid, char *key, char *val);
191 char *xenstore_vm_read(int domid, char *key, int *len);
192 
Note: See TracBrowser for help on using the repository browser.