chiark / gitweb /
add DEVLINKS to the event environment
[elogind.git] / libsysfs / sysfs_device.c
index d1c5b321f03133fca4cbca2f7301b7ab6dc1e1c1..e3d70143f9fd73fd1e399c92717810f4a7b2c38d 100644 (file)
  */
 static int get_dev_driver(struct sysfs_device *dev)
 {
-       struct dlist *drvlist;
        char path[SYSFS_PATH_MAX];
        char devpath[SYSFS_PATH_MAX];
-       char *drv = NULL, *c;
 
        if (!dev) {
                errno = EINVAL;
@@ -50,44 +48,6 @@ static int get_dev_driver(struct sysfs_device *dev)
                }
                return 0;
        }
-
-       /*
-        * 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);
-       memset(devpath, 0, SYSFS_PATH_MAX);
-       safestrcpy(path, dev->path);
-       c = strstr(path, SYSFS_DEVICES_NAME);
-       if (c == NULL) {
-               dprintf("Invalid path to device - %s\n", dev->path);
-               return -1;
-       } else
-               *c = '\0';
-       safestrcat(path, SYSFS_BUS_NAME);
-       safestrcat(path, "/");
-       safestrcat(path, dev->bus);
-       safestrcat(path, "/");
-       safestrcat(path, SYSFS_DRIVERS_NAME);
-
-       drvlist = sysfs_open_directory_list(path);
-       if (drvlist) {
-               dlist_for_each_data(drvlist, drv, char) {
-                       safestrcpy(devpath, path);
-                       safestrcat(devpath, "/");
-                       safestrcat(devpath, drv);
-                       safestrcat(devpath, "/");
-                       safestrcat(devpath, dev->bus_id);
-                       if (!sysfs_path_is_link(devpath)) {
-                               safestrcpy(dev->driver_name, drv);
-                               sysfs_close_list(drvlist);
-                               return 0;
-                       }
-               }
-               sysfs_close_list(drvlist);
-       }
        return -1;
 }
 
@@ -100,11 +60,7 @@ static int get_dev_driver(struct sysfs_device *dev)
 int sysfs_get_device_bus(struct sysfs_device *dev)
 {
        char devpath[SYSFS_PATH_MAX];
-       char subsys[SYSFS_NAME_LEN];
        char path[SYSFS_PATH_MAX];
-       char target[SYSFS_PATH_MAX];
-       char *bus = NULL, *c;
-       struct dlist *buslist;
 
        if (!dev) {
                errno = EINVAL;
@@ -123,49 +79,6 @@ int sysfs_get_device_bus(struct sysfs_device *dev)
                }
                return 0;
        }
-
-       /*
-        * Devices on on earlier kernels do not have the "bus" link.
-        * Look it up in the bus directory.
-        */
-       memset(subsys, 0, SYSFS_NAME_LEN);
-       safestrcpy(subsys, dev->path);
-       c = strstr(subsys, SYSFS_DEVICES_NAME);
-       if (c == NULL) {
-               dprintf("Invalid path to device - %s\n", dev->path);
-               return -1;
-       } else 
-               *c = '\0';
-       safestrcat(subsys, SYSFS_BUS_NAME);
-       buslist = sysfs_open_directory_list(subsys);
-       if (buslist) {
-               dlist_for_each_data(buslist, bus, char) {
-                       memset(path, 0, SYSFS_PATH_MAX);
-                       safestrcpy(path, subsys);
-                       safestrcat(path, "/");
-                       safestrcat(path, bus);
-                       safestrcat(path, "/");
-                       safestrcat(path, SYSFS_DEVICES_NAME);
-                       safestrcat(path, "/");
-                       safestrcat(path, dev->bus_id);
-                       if (!sysfs_path_is_link(path)) {
-                               memset(target, 0, SYSFS_PATH_MAX);
-                               if (sysfs_get_link(path, target, 
-                                               SYSFS_PATH_MAX)) {
-                                       dprintf("Error getting link target\n");
-                                       sysfs_close_list(buslist);
-                                       return -1;
-                               }
-                               if (!(strncmp(target, dev->path, 
-                                                       SYSFS_PATH_MAX))) {
-                                       safestrcpy(dev->bus, bus);
-                                       sysfs_close_list(buslist);
-                                       return 0;
-                               }
-                       }
-               }
-               sysfs_close_list(buslist);
-       }
        return -1;
 }
 
@@ -371,7 +284,6 @@ struct sysfs_device *sysfs_open_device(const char *bus,     const char *bus_id)
                dprintf("Error opening device %s\n", bus_id);
                return NULL;
        }
-
        return device;
 }
 
@@ -383,7 +295,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;
@@ -394,6 +306,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) {
@@ -410,16 +323,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",