chiark / gitweb /
increase kernel uevent buffer size
[elogind.git] / udev_sysfs.c
index 19fd614e633a5a3204dfd819775e0185e31305c5..8de523dadf66abc3bbf0198c9dac01036ca7f608 100644 (file)
@@ -26,6 +26,7 @@
 #include <fcntl.h>
 #include <ctype.h>
 #include <errno.h>
+#include <sys/stat.h>
 
 #include "udev.h"
 
@@ -128,7 +129,7 @@ struct sysfs_device *sysfs_device_get(const char *devpath)
        strlcpy(path, sysfs_path, sizeof(path));
        strlcat(path, devpath_real, sizeof(path));
        if (lstat(path, &statbuf) != 0) {
-               dbg("stat '%s' failed: %s", devpath, strerror(errno));
+               dbg("stat '%s' failed: %s", path, strerror(errno));
                return NULL;
        }
 
@@ -182,10 +183,10 @@ struct sysfs_device *sysfs_device_get(const char *devpath)
                        pos[0] = '\0';
                else
                        dev->subsystem[0] = '\0';
-       } else if (strncmp(devpath, "/block/", 7) == 0) {
+       } else if (strncmp(dev->devpath, "/block/", 7) == 0) {
                strlcpy(dev->subsystem, "block", sizeof(dev->subsystem));
-       } else if (strncmp(devpath, "/devices/", 9) == 0) {
-               /* get subsystem from bus name */
+       } else if (strncmp(dev->devpath, "/devices/", 9) == 0) {
+               /* get subsystem from "bus" link */
                strlcpy(link_path, sysfs_path, sizeof(link_path));
                strlcat(link_path, dev->devpath, sizeof(link_path));
                strlcat(link_path, "/bus", sizeof(link_path));
@@ -196,8 +197,20 @@ struct sysfs_device *sysfs_device_get(const char *devpath)
                        pos = strrchr(link_target, '/');
                        if (pos != NULL)
                                strlcpy(dev->subsystem, &pos[1], sizeof(dev->subsystem));
+               } else {
+                       /* get subsystem from "subsystem" link */
+                       strlcpy(link_path, sysfs_path, sizeof(link_path));
+                       strlcat(link_path, dev->devpath, sizeof(link_path));
+                       strlcat(link_path, "/subsystem", sizeof(link_path));
+                       len = readlink(link_path, link_target, sizeof(link_target));
+                       if (len > 0) {
+                               link_target[len] = '\0';
+                               dbg("subsystem link '%s' points to '%s'", link_path, link_target);
+                               pos = strrchr(link_target, '/');
+                               if (pos != NULL)
+                                       strlcpy(dev->subsystem, &pos[1], sizeof(dev->subsystem));
+                       }
                }
-
                /* get driver name */
                strlcpy(link_path, sysfs_path, sizeof(link_path));
                strlcat(link_path, dev->devpath, sizeof(link_path));
@@ -210,9 +223,9 @@ struct sysfs_device *sysfs_device_get(const char *devpath)
                        if (pos != NULL)
                                strlcpy(dev->driver, &pos[1], sizeof(dev->driver));
                }
-       } else if (strncmp(devpath, "/bus/", 5) == 0 && strstr(devpath, "/drivers/")) {
+       } else if (strncmp(dev->devpath, "/bus/", 5) == 0 && strstr(dev->devpath, "/drivers/")) {
                strlcpy(dev->subsystem, "drivers", sizeof(dev->subsystem));
-       } else if (strncmp(devpath, "/module/", 8) == 0) {
+       } else if (strncmp(dev->devpath, "/module/", 8) == 0) {
                strlcpy(dev->subsystem, "module", sizeof(dev->subsystem));
        }
 
@@ -262,7 +275,7 @@ struct sysfs_device *sysfs_device_get_parent(struct sysfs_device *dev)
        if (strncmp(parent_devpath, "/class", 6) == 0) {
                pos = strrchr(parent_devpath, '/');
                if (pos == &parent_devpath[6] || pos == parent_devpath) {
-                       dbg("class top level, look for device link");
+                       dbg("/class top level, look for device link");
                        goto device_link;
                }
        }
@@ -294,6 +307,19 @@ device_link:
        return sysfs_device_get(parent_devpath);
 }
 
+struct sysfs_device *sysfs_device_get_parent_with_subsystem(struct sysfs_device *dev, const char *subsystem)
+{
+       struct sysfs_device *dev_parent;
+
+       dev_parent = sysfs_device_get_parent(dev);
+       while (dev_parent != NULL) {
+               if (strcmp(dev_parent->subsystem, subsystem) == 0)
+                       return dev_parent;
+               dev_parent = sysfs_device_get_parent(dev_parent);
+       }
+       return NULL;
+}
+
 char *sysfs_attr_get_value(const char *devpath, const char *attr_name)
 {
        char path_full[PATH_SIZE];