chiark / gitweb /
allow programs in /lib/udev called without the path
[elogind.git] / libsysfs / sysfs_class.c
index a93de573d43b4b083db00f40537ba0c73b96a36a..102f09f17af260fe92f66a9d412627e432c50496 100644 (file)
@@ -40,6 +40,43 @@ void sysfs_close_class_device(struct sysfs_class_device *dev)
        }
 }
 
+static void sysfs_close_cls_dev(void *dev)
+{
+       sysfs_close_class_device((struct sysfs_class_device *)dev);
+}
+
+/**
+ * sysfs_close_class: close the given class
+ * @cls: sysfs_class to close
+ */ 
+void sysfs_close_class(struct sysfs_class *cls)
+{
+       if (cls) {
+               if (cls->devices)
+                       dlist_destroy(cls->devices);
+               if (cls->attrlist)
+                       dlist_destroy(cls->attrlist);
+               free(cls);
+       }
+}
+
+static int cdev_name_equal(void *a, void *b)
+{
+       if (!a || !b)
+               return 0;
+
+       if (strncmp((char *)a, ((struct sysfs_class_device *)b)->name, 
+                               strlen((char *)a)) == 0)
+               return 1;
+
+       return 0;
+}
+
+static struct sysfs_class *alloc_class(void)
+{
+       return (struct sysfs_class *) calloc(1, sizeof(struct sysfs_class));
+}
+
 /**
  * alloc_class_device: mallocs and initializes new class device struct.
  * returns sysfs_class_device or NULL.
@@ -91,29 +128,46 @@ static void set_classdev_classname(struct sysfs_class_device *cdev)
 struct sysfs_class_device *sysfs_open_class_device_path(const char *path)
 {
        struct sysfs_class_device *cdev;
+       char temp_path[SYSFS_PATH_MAX];
 
        if (!path) {
                errno = EINVAL;
                return NULL;
        }
-       if ((sysfs_path_is_dir(path)) != 0) {
-               dprintf("%s is not a valid path to a class device\n", path);
-               return NULL;
-       }
+
+       /*
+        * Post linux-2.6.14 driver model supports nested classes with
+        * links to the nested hierarchy at /sys/class/xxx/. Check for
+        * a link to the actual class device if a directory isn't found
+        */
+       if (sysfs_path_is_dir(path)) {
+               dprintf("%s: Directory not found, checking for a link\n", path);
+               if (!sysfs_path_is_link(path)) {
+                       if (sysfs_get_link(path, temp_path, SYSFS_PATH_MAX)) {
+                               dprintf("Error retrieving link at %s\n", path);
+                               return NULL;
+                       }
+               } else {
+                       dprintf("%s is not a valid class device path\n", path);
+                       return NULL;
+               }
+       } else
+               safestrcpy(temp_path, path);
+
        cdev = alloc_class_device();
        if (!cdev) {
                dprintf("calloc failed\n");
                return NULL;
        }
-       if (sysfs_get_name_from_path(path, cdev->name, SYSFS_NAME_LEN)) {
+       if (sysfs_get_name_from_path(temp_path, cdev->name, SYSFS_NAME_LEN)) {
                errno = EINVAL;
                dprintf("Error getting class device name\n");
                sysfs_close_class_device(cdev);
                return NULL;
        }
 
-       safestrcpy(cdev->path, path);
-       if ((sysfs_remove_trailing_slash(cdev->path)) != 0) {
+       safestrcpy(cdev->path, temp_path);
+       if (sysfs_remove_trailing_slash(cdev->path)) {
                dprintf("Invalid path to class device %s\n", cdev->path);
                sysfs_close_class_device(cdev);
                return NULL;
@@ -335,3 +389,169 @@ struct sysfs_device *sysfs_get_classdev_device
        }
        return clsdev->sysdevice;
 }
+
+/**
+ * sysfs_open_class: opens specific class and all its devices on system
+ * returns sysfs_class structure with success or NULL with error.
+ */
+struct sysfs_class *sysfs_open_class(const char *name)
+{
+       struct sysfs_class *cls = NULL;
+       char classpath[SYSFS_PATH_MAX];
+
+       if (!name) {
+               errno = EINVAL;
+               return NULL;
+       }
+
+       memset(classpath, 0, SYSFS_PATH_MAX);
+        if ((sysfs_get_mnt_path(classpath, SYSFS_PATH_MAX)) != 0) {
+                dprintf("Sysfs not supported on this system\n");
+                return NULL;
+        }
+
+       /* 
+        * We shall now treat "block" also as a class. Hence, check here
+        * if "name" is "block" and proceed accordingly
+        */
+       if (strcmp(name, SYSFS_BLOCK_NAME) == 0) {
+               safestrcat(classpath, "/");
+               safestrcat(classpath, SYSFS_BLOCK_NAME);
+       } else {
+               safestrcat(classpath, "/");
+               safestrcat(classpath, SYSFS_CLASS_NAME);
+               safestrcat(classpath, "/");
+               safestrcat(classpath, name);
+       }
+       if (sysfs_path_is_dir(classpath)) {
+               dprintf("Class %s not found on the system\n", name);
+               return NULL;
+       }
+
+       cls = alloc_class();
+       if (cls == NULL) {
+               dprintf("calloc failed\n");
+               return NULL;
+       }
+       safestrcpy(cls->name, name);    
+       safestrcpy(cls->path, classpath);
+       if ((sysfs_remove_trailing_slash(cls->path)) != 0) {
+               dprintf("Invalid path to class device %s\n", cls->path);
+               sysfs_close_class(cls);
+               return NULL;
+       }
+
+       return cls;
+}
+
+/**
+ * sysfs_get_class_device: get specific class device using the device's id
+ * @cls: sysfs_class to find the device on
+ * @name: name of the class device to look for
+ * 
+ * Returns sysfs_class_device * on success and NULL on failure
+ */ 
+struct sysfs_class_device *sysfs_get_class_device(struct sysfs_class *cls,
+               const char *name)
+{
+       char path[SYSFS_PATH_MAX];
+       struct sysfs_class_device *cdev = NULL;
+
+       if (!cls || !name) {
+               errno = EINVAL;
+               return NULL;
+       }
+
+       if (cls->devices) {
+               cdev = (struct sysfs_class_device *)dlist_find_custom
+                       (cls->devices, (void *)name, cdev_name_equal);
+               if (cdev)
+                       return cdev;
+       }
+
+       safestrcpy(path, cls->path);
+       safestrcat(path, "/");
+       safestrcat(path, name);
+       cdev = sysfs_open_class_device_path(path);
+       if (!cdev) {
+               dprintf("Error opening class device at %s\n", path);
+               return NULL;
+       }
+       if (!cls->devices)
+               cls->devices = dlist_new_with_delete
+                       (sizeof(struct sysfs_class_device),
+                                sysfs_close_cls_dev);
+       
+       dlist_unshift_sorted(cls->devices, cdev, sort_list);
+       return cdev;
+}
+
+/**
+ * Add class devices to list
+ */
+static void add_cdevs_to_classlist(struct sysfs_class *cls, struct dlist *list)
+{
+       char path[SYSFS_PATH_MAX], *cdev_name;
+       struct sysfs_class_device *cdev = NULL;
+
+       if (cls == NULL || list == NULL)
+               return;
+
+       dlist_for_each_data(list, cdev_name, char) {
+               if (cls->devices) {
+                       cdev = (struct sysfs_class_device *)
+                               dlist_find_custom(cls->devices,
+                               (void *)cdev_name, cdev_name_equal);
+                       if (cdev)
+                               continue;
+               }
+               safestrcpy(path, cls->path);
+               safestrcat(path, "/");
+               safestrcat(path, cdev_name);
+               cdev = sysfs_open_class_device_path(path);
+               if (cdev) {
+                       if (!cls->devices)
+                               cls->devices = dlist_new_with_delete
+                               (sizeof(struct sysfs_class_device),
+                                sysfs_close_cls_dev);
+                       dlist_unshift_sorted(cls->devices, cdev,
+                                       sort_list);
+               }
+       }
+}
+
+/**
+ * sysfs_get_class_devices: get all class devices in the given class
+ * @cls: sysfs_class whose devices list is needed
+ *
+ * Returns a dlist of sysfs_class_device * on success and NULL on failure
+ */
+struct dlist *sysfs_get_class_devices(struct sysfs_class *cls)
+{
+       char path[SYSFS_PATH_MAX];
+       struct dlist *dirlist, *linklist;
+
+       if (!cls) {
+               errno = EINVAL;
+               return NULL;
+       }
+
+       /*
+        * Post linux-2.6.14, we have nested classes and links under
+        * /sys/class/xxx/. are also valid class devices
+        */
+       safestrcpy(path, cls->path);
+       dirlist = read_dir_subdirs(path);
+       if (dirlist) {
+               add_cdevs_to_classlist(cls, dirlist);
+               sysfs_close_list(dirlist);
+       }
+
+       linklist = read_dir_links(path);
+       if (linklist) {
+               add_cdevs_to_classlist(cls, linklist);
+               sysfs_close_list(linklist);
+       }
+
+       return cls->devices;
+}