]> xenbits.xensource.com Git - people/royger/xen.git/commitdiff
libxl: add generic functions to get and free device list
authorOleksandr Grytsov <oleksandr_grytsov@epam.com>
Mon, 10 Jul 2017 13:50:12 +0000 (16:50 +0300)
committerOleksandr Grytsov <oleksandr_grytsov@epam.com>
Wed, 13 Sep 2017 07:54:45 +0000 (10:54 +0300)
Add libxl__device_list and libxl__device_list_free
functions to handle device list using the device
framework.

Signed-off-by: Oleksandr Grytsov <oleksandr_grytsov@epam.com>
Acked-by: Wei Liu <wei.liu2@citrix.com>
tools/libxl/libxl_device.c
tools/libxl/libxl_internal.h

index 3296e8347420db3ddc18a9154584af3ec74494bb..487be28840e60fdd992625bceca4cceb47da04a1 100644 (file)
@@ -1990,6 +1990,67 @@ out:
     return rc;
 }
 
+void *libxl__device_list(libxl__gc *gc, const struct libxl_device_type *dt,
+                         uint32_t domid, const char* name, int *num)
+{
+    void *r = NULL;
+    void *list = NULL;
+    void *item = NULL;
+    char *libxl_path;
+    char **dir = NULL;
+    unsigned int ndirs = 0;
+    int rc;
+
+    *num = 0;
+
+    libxl_path = GCSPRINTF("%s/device/%s",
+                           libxl__xs_libxl_path(gc, domid), name);
+
+    dir = libxl__xs_directory(gc, XBT_NULL, libxl_path, &ndirs);
+
+    if (dir && ndirs) {
+        list = libxl__malloc(NOGC, dt->dev_elem_size * ndirs);
+        item = list;
+
+        while (*num < ndirs) {
+            dt->init(item);
+            ++(*num);
+
+            if (dt->from_xenstore) {
+                char *device_libxl_path = GCSPRINTF("%s/%s", libxl_path, *dir);
+                rc = dt->from_xenstore(gc, device_libxl_path, atoi(*dir), item);
+                if (rc) goto out;
+            }
+
+            item = (uint8_t *)item + dt->dev_elem_size;
+            ++dir;
+        }
+    }
+
+    r = list;
+    list = NULL;
+
+out:
+
+    if (list) {
+        libxl__device_list_free(dt, list, *num);
+        *num = 0;
+    }
+
+    return r;
+}
+
+void libxl__device_list_free(const struct libxl_device_type *dt,
+                             void *list, int num)
+{
+    int i;
+
+    for (i = 0; i < num; i++)
+        dt->dispose((uint8_t*)list + i * dt->dev_elem_size);
+
+    free(list);
+}
+
 /*
  * Local variables:
  * mode: C
index c99ef3baa30e4323f2a38e699494eb7509ba2a35..c94a117abe87aad7143fdf0b8c12641db685764d 100644 (file)
@@ -3505,6 +3505,7 @@ struct libxl_device_type {
     int (*dm_needed)(void *, unsigned);
     void (*update_config)(libxl__gc *, void *, void *);
     int (*update_devid)(libxl__gc *, uint32_t, void *);
+    int (*from_xenstore)(libxl__gc *, const char *, libxl_devid, void *);
     int (*set_xenstore_config)(libxl__gc *, uint32_t, void *, flexarray_t *,
                                flexarray_t *, flexarray_t *);
 };
@@ -4385,6 +4386,13 @@ void libxl__device_add_async(libxl__egc *egc, uint32_t domid,
 int libxl__device_add(libxl__gc *gc, uint32_t domid,
                       const struct libxl_device_type *dt, void *type);
 
+/* Caller is responsible for freeing the memory by calling
+ * libxl__device_list_free
+ */
+void* libxl__device_list(libxl__gc *gc, const struct libxl_device_type *dt,
+                         uint32_t domid, const char* name, int *num);
+void libxl__device_list_free(const struct libxl_device_type *dt,
+                             void *list, int num);
 #endif
 
 /*