#include <fcntl.h>
#include <ctype.h>
#include <errno.h>
+#include <sys/stat.h>
#include "udev.h"
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;
}
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));
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));
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));
}
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;
}
}
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];