📄 xenstore-device-info-functions
字号:
# HG changeset patch# User kaf24@localhost.localdomain# Node ID bbcac2aea0e8196cd75a3bf6dbe57bebf8c1e5b2# Parent dc973fe5633386547ce5bc8fd4cf5f2bb5b55174[QEMU] Helper functions to interface with the xenstore and read device information from it. - detect what types of devices a domain has or whether a domain has a device of a certain type - read the content of a variable related to a device, i.e., hotplug-status - subscribe to changes of the hotplug status of a device for not having to poll the statusSigned-off-by: Stefan Berger <stefanb@us.ibm.com>Index: ioemu/xenstore.c===================================================================--- ioemu.orig/xenstore.c 2007-05-10 15:19:29.000000000 +0100+++ ioemu/xenstore.c 2007-05-10 15:19:29.000000000 +0100@@ -304,6 +304,143 @@ return rc; } ++/*+ * get all device instances of a certain type+ */+char **xenstore_domain_get_devices(struct xs_handle *handle,+ const char *devtype, unsigned int *num)+{+ char *path;+ char *buf = NULL;+ char **e = NULL;++ path = xs_get_domain_path(handle, domid);+ if (path == NULL)+ goto out;++ if (pasprintf(&buf, "%s/device/%s", path,devtype) == -1)+ goto out;++ e = xs_directory(handle, XBT_NULL, buf, num);++ out:+ free(path);+ free(buf);+ return e;+}++/*+ * Check whether a domain has devices of the given type+ */+int xenstore_domain_has_devtype(struct xs_handle *handle, const char *devtype)+{+ int rc = 0;+ unsigned int num;+ char **e = xenstore_domain_get_devices(handle, devtype, &num);+ if (e)+ rc = 1;+ free(e);+ return rc;+}++/*+ * Function that creates a path to a variable of an instance of a+ * certain device+ */+static char *get_device_variable_path(const char *devtype, const char *inst,+ const char *var)+{+ char *buf = NULL;+ if (pasprintf(&buf, "/local/domain/0/backend/%s/%d/%s/%s",+ devtype,+ domid,+ inst,+ var) == -1) {+ free(buf);+ buf = NULL;+ }+ return buf;+}++char *xenstore_backend_read_variable(struct xs_handle *handle,+ const char *devtype, const char *inst,+ const char *var)+{+ char *value = NULL;+ char *buf = NULL;+ unsigned int len;++ buf = get_device_variable_path(devtype, inst, var);+ if (NULL == buf)+ goto out;++ value = xs_read(handle, XBT_NULL, buf, &len);++ free(buf);++ out:+ return value;+}++/*+ Read the hotplug status variable from the backend given the type+ of device and its instance.+*/+char *xenstore_read_hotplug_status(struct xs_handle *handle,+ const char *devtype, const char *inst)+{+ return xenstore_backend_read_variable(handle, devtype, inst,+ "hotplug-status");+}++/*+ Subscribe to the hotplug status of a device given the type of device and+ its instance.+ In case an error occurrs, a negative number is returned.+ */+int xenstore_subscribe_to_hotplug_status(struct xs_handle *handle,+ const char *devtype,+ const char *inst,+ const char *token)+{+ int rc = 0;+ char *path = get_device_variable_path(devtype, inst, "hotplug-status");++ if (path == NULL)+ return -1;++ if (0 == xs_watch(handle, path, token))+ rc = -2;++ free(path);++ return rc;+}++/*+ * Unsubscribe from a subscription to the status of a hotplug variable of+ * a device.+ */+int xenstore_unsubscribe_from_hotplug_status(struct xs_handle *handle,+ const char *devtype,+ const char *inst,+ const char *token)+{+ int rc = 0;+ char *path;+ path = get_device_variable_path(devtype, inst, "hotplug-status");+ if (path == NULL)+ return -1;++ if (0 == xs_unwatch(handle, path, token))+ rc = -2;++ free(path);++ return rc;+}+ char *xenstore_vm_read(int domid, char *key, int *len) { char *buf = NULL, *path = NULL, *value = NULL;Index: ioemu/vl.h===================================================================--- ioemu.orig/vl.h 2007-05-10 15:19:29.000000000 +0100+++ ioemu/vl.h 2007-05-10 15:19:29.000000000 +0100@@ -1434,6 +1434,24 @@ void xenstore_write_vncport(int vnc_display); int xenstore_read_vncpasswd(int domid); +int xenstore_domain_has_devtype(struct xs_handle *handle,+ const char *devtype);+char **xenstore_domain_get_devices(struct xs_handle *handle,+ const char *devtype, unsigned int *num);+char *xenstore_read_hotplug_status(struct xs_handle *handle,+ const char *devtype, const char *inst);+char *xenstore_backend_read_variable(struct xs_handle *,+ const char *devtype, const char *inst,+ const char *var);+int xenstore_subscribe_to_hotplug_status(struct xs_handle *handle,+ const char *devtype,+ const char *inst,+ const char *token);+int xenstore_unsubscribe_from_hotplug_status(struct xs_handle *handle,+ const char *devtype,+ const char *inst,+ const char *token);+ int xenstore_vm_write(int domid, char *key, char *val); char *xenstore_vm_read(int domid, char *key, int *len);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -