chiark / gitweb /
Fix libsysfs issue with relying on the detach_state file to be
[elogind.git] / libsysfs / sysfs_device.c
index 7087730d5f16d7a830c658d3ef22be8b44dd9c77..1c59403edd86f7108dcc2f0be692faae6204d446 100644 (file)
@@ -38,7 +38,6 @@ static int get_dev_driver(struct sysfs_device *dev)
                errno = EINVAL;
                return -1;
        }
-
        memset(path, 0, SYSFS_PATH_MAX);
        memset(devpath, 0, SYSFS_PATH_MAX);
        safestrcpymax(path, dev->path, SYSFS_PATH_MAX);
@@ -53,9 +52,9 @@ static int get_dev_driver(struct sysfs_device *dev)
        }
 
        /*
-        * Devices on on earlier kernels do not have the "driver" link.
+        * Devices on earlier kernels do not have the "driver" link.
         * Look it up in the bus directory.
-        */
+        */ 
        if (dev->bus[0] == '\0')
                return -1;
        memset(path, 0, SYSFS_PATH_MAX);
@@ -81,7 +80,7 @@ static int get_dev_driver(struct sysfs_device *dev)
                        safestrcat(devpath, drv);
                        safestrcat(devpath, "/");
                        safestrcat(devpath, dev->bus_id);
-                       if (sysfs_path_is_link(devpath) == 0) {
+                       if (!sysfs_path_is_link(devpath)) {
                                safestrcpy(dev->driver_name, drv);
                                sysfs_close_list(drvlist);
                                return 0;
@@ -149,7 +148,7 @@ int sysfs_get_device_bus(struct sysfs_device *dev)
                        safestrcat(path, SYSFS_DEVICES_NAME);
                        safestrcat(path, "/");
                        safestrcat(path, dev->bus_id);
-                       if ((sysfs_path_is_link(path)) == 0) {
+                       if (!sysfs_path_is_link(path)) {
                                memset(target, 0, SYSFS_PATH_MAX);
                                if (sysfs_get_link(path, target, 
                                                SYSFS_PATH_MAX)) {
@@ -384,7 +383,7 @@ struct sysfs_device *sysfs_open_device(const char *bus,     const char *bus_id)
  */
 struct sysfs_device *sysfs_get_device_parent(struct sysfs_device *dev)
 {
-       char ppath[SYSFS_PATH_MAX], *tmp;
+       char ppath[SYSFS_PATH_MAX], dpath[SYSFS_PATH_MAX], *tmp;
 
        if (!dev) {
                errno = EINVAL;
@@ -395,6 +394,7 @@ struct sysfs_device *sysfs_get_device_parent(struct sysfs_device *dev)
                return (dev->parent);
 
        memset(ppath, 0, SYSFS_PATH_MAX);
+       memset(dpath, 0, SYSFS_PATH_MAX);
        safestrcpy(ppath, dev->path);
        tmp = strrchr(ppath, '/');
        if (!tmp) {
@@ -411,16 +411,14 @@ struct sysfs_device *sysfs_get_device_parent(struct sysfs_device *dev)
        }
        *tmp = '\0';
 
-       /*
-        * All "devices" have the "detach_state" attribute - validate here
-        */
-       safestrcat(ppath, "/detach_state");
-       if (sysfs_path_is_file(ppath)) {
+       /* Make sure we're not at the top of the device tree */
+       sysfs_get_mnt_path(dpath, SYSFS_PATH_MAX);
+       safestrcat(dpath, "/" SYSFS_DEVICES_NAME);
+       if (strcmp(dpath, ppath) == 0) {
                dprintf("Device at %s does not have a parent\n", dev->path);
                return NULL;
        }
-       tmp = strrchr(ppath, '/');
-       *tmp = '\0';
+
        dev->parent = sysfs_open_device_path(ppath);
        if (!dev->parent) {
                dprintf("Error opening device %s's parent at %s\n",