From: Stefan Berger Subject: [PATCH] [HVM] [TPM] [2/3] Helper functions for interfacing with the xenstore from qemu Date: Tue, 24 Oct 2006 14:48:21 -0400 Message-ID: <1161715701.26049.4.camel@lt2.watson.ibm.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-IJhF7+SZP0p0IjtI+885" Return-path: List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xensource.com Errors-To: xen-devel-bounces@lists.xensource.com To: Xen-devel List-Id: xen-devel@lists.xenproject.org --=-IJhF7+SZP0p0IjtI+885 Content-Type: text/plain Content-Transfer-Encoding: 7bit This patch provides a couple of helper functions to interface with the xenstore and read device information from it. These are used in patch part 3. The provided functions can be used to: - 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 status Signed-off-by: Stefan Berger --=-IJhF7+SZP0p0IjtI+885 Content-Disposition: attachment; filename=qemu-xenstore-devicedata.diff Content-Type: text/x-patch; name=qemu-xenstore-devicedata.diff; charset=utf-8 Content-Transfer-Encoding: 7bit Index: root/xen-unstable.hg/tools/ioemu/vl.h =================================================================== --- root.orig/xen-unstable.hg/tools/ioemu/vl.h +++ root/xen-unstable.hg/tools/ioemu/vl.h @@ -1217,6 +1217,25 @@ void xenstore_check_new_media_present(in 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); + + /* xen_platform.c */ void pci_xen_platform_init(PCIBus *bus); Index: root/xen-unstable.hg/tools/ioemu/xenstore.c =================================================================== --- root.orig/xen-unstable.hg/tools/ioemu/xenstore.c +++ root/xen-unstable.hg/tools/ioemu/xenstore.c @@ -264,3 +264,140 @@ int xenstore_read_vncpasswd(int domid) 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; +} --=-IJhF7+SZP0p0IjtI+885 Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Xen-devel mailing list Xen-devel@lists.xensource.com http://lists.xensource.com/xen-devel --=-IJhF7+SZP0p0IjtI+885--