chiark / gitweb /
[PATCH] clean up the aoe char device rules, and delete the block one as it's not...
[elogind.git] / libsysfs / sysfs_device.c
index 290fd9723e2094ea07439901d9d2e7a7b7acd128..83c3adc080cd3b81ffc29a13c03392660659fd51 100644 (file)
  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  *
  */
-#include "sysfs/libsysfs.h"
+#include "libsysfs.h"
 #include "sysfs.h"
 
 /**
- * get_dev_driver: fills in the dev->driver_name field 
+ * get_dev_driver: fills in the dev->driver_name field, but searches by
+ *     opening subsystem. Only to be used if no driver link exists in
+ *     device directory.
  *
  * Returns 0 on SUCCESS and 1 on error
  */
@@ -73,6 +75,32 @@ static int get_dev_driver(struct sysfs_device *dev)
        }
        return 1;
 }
+
+/*
+ * get_device_driver_name: gets device's driver name, searches for driver
+ *     link first before going the brute force route.
+ * @dev: device to retrieve driver
+ * returns 0 with success and 1 with error
+ */
+static int get_device_driver_name(struct sysfs_device *dev)
+{
+       char devpath[SYSFS_PATH_MAX], drvpath[SYSFS_PATH_MAX];
+
+       if (dev == NULL) {
+               errno = EINVAL;
+               return 1;
+       }
+       memset(devpath, 0, SYSFS_PATH_MAX);
+       memset(drvpath, 0, SYSFS_PATH_MAX);
+       safestrcpy(devpath, dev->path);
+       safestrcat(devpath, "/driver");
+
+       if ((sysfs_get_link(devpath, drvpath, SYSFS_PATH_MAX)) != 0) 
+               return(get_dev_driver(dev));
+
+       return (sysfs_get_name_from_path(drvpath, dev->driver_name, 
+               SYSFS_NAME_LEN));
+}
        
 /**
  * sysfs_get_device_bus: retrieves the bus name the device is on, checks path 
@@ -262,7 +290,7 @@ struct sysfs_device *sysfs_open_device_path(const char *path)
        if (sysfs_get_device_bus(dev) != 0)
                dprintf("Could not get device bus\n");
        
-       if (get_dev_driver(dev) != 0) {
+       if (get_device_driver_name(dev) != 0) {
                dprintf("Could not get device %s's driver\n", dev->bus_id);
                safestrcpy(dev->driver_name, SYSFS_UNKNOWN);
        }
@@ -502,7 +530,7 @@ struct sysfs_attribute *sysfs_get_device_attr(struct sysfs_device *dev,
  * @psize: size of "path"
  * Returns 0 on success -1 on failure
  */
-static int get_device_absolute_path(const char *device, const char *bus, 
+static int get_device_absolute_path(const char *device,        const char *bus, 
                                char *path, size_t psize)
 {
        char bus_path[SYSFS_PATH_MAX];
@@ -547,7 +575,7 @@ static int get_device_absolute_path(const char *device, const char *bus,
  * 2. Bus the device is on must be supplied
  *     Use sysfs_find_device_bus to get the bus name
  */
-struct sysfs_device *sysfs_open_device(const char *bus, const char *bus_id)
+struct sysfs_device *sysfs_open_device(const char *bus,        const char *bus_id)
 {
        char sysfs_path[SYSFS_PATH_MAX];
        struct sysfs_device *device = NULL;