+/**
+ * get_dev_driver: fills in the dev->driver_name field
+ *
+ * Returns 0 on SUCCESS and 1 on error
+ */
+static int get_dev_driver(struct sysfs_device *dev)
+{
+ struct dlist *drvlist = NULL;
+ char path[SYSFS_PATH_MAX], devpath[SYSFS_PATH_MAX];
+ char *drv = NULL, *c = NULL;
+
+ if (dev == NULL) {
+ errno = EINVAL;
+ return 1;
+ }
+ if (dev->bus[0] == '\0')
+ return 1;
+ memset(path, 0, SYSFS_PATH_MAX);
+ memset(devpath, 0, SYSFS_PATH_MAX);
+ safestrcpy(path, SYSFS_BUS_NAME);
+ safestrcat(path, "/");
+ safestrcat(path, dev->bus);
+ safestrcat(path, "/");
+ safestrcat(path, SYSFS_DRIVERS_NAME);
+
+ safestrcpy(devpath, dev->path);
+ c = strstr(devpath, SYSFS_DEVICES_NAME);
+ if (c == NULL)
+ return 1;
+ *c = '\0';
+ safestrcatmax(c, path, (sizeof(devpath) - strlen(devpath)));
+
+ drvlist = sysfs_open_subsystem_list(path);
+ if (drvlist != NULL) {
+ dlist_for_each_data(drvlist, drv, char) {
+ safestrcpy(path, devpath);
+ safestrcat(path, "/");
+ safestrcat(path, drv);
+ safestrcat(path, "/");
+ safestrcat(path, dev->bus_id);
+ if (sysfs_path_is_link(path) == 0) {
+ safestrcpy(dev->driver_name, drv);
+ sysfs_close_list(drvlist);
+ return 0;
+ }
+ }
+ sysfs_close_list(drvlist);
+ }
+ return 1;
+}
+
+/**
+ * sysfs_get_device_bus: retrieves the bus name the device is on, checks path
+ * to bus' link to make sure it has correct device.
+ * @dev: device to get busname.
+ * returns 0 with success and -1 with error.
+ */
+int sysfs_get_device_bus(struct sysfs_device *dev)
+{
+ char subsys[SYSFS_NAME_LEN], path[SYSFS_PATH_MAX];
+ char target[SYSFS_PATH_MAX], *bus = NULL, *c = NULL;
+ struct dlist *buslist = NULL;
+
+ if (dev == NULL) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ memset(subsys, 0, SYSFS_NAME_LEN);
+ safestrcpy(subsys, SYSFS_BUS_NAME); /* subsys = bus */
+ buslist = sysfs_open_subsystem_list(subsys);
+ if (buslist != NULL) {
+ dlist_for_each_data(buslist, bus, char) {
+ memset(path, 0, SYSFS_PATH_MAX);
+ safestrcpy(path, dev->path);
+ c = strstr(path, "/devices");
+ if (c == NULL) {
+ dprintf("Invalid path to device %s\n", path);
+ sysfs_close_list(buslist);
+ return -1;
+ }
+ *c = '\0';
+ safestrcat(path, "/");
+ safestrcat(path, SYSFS_BUS_NAME);
+ 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)) == 0) {
+ memset(target, 0, SYSFS_PATH_MAX);
+ if ((sysfs_get_link(path, target,
+ SYSFS_PATH_MAX)) != 0) {
+ 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;
+}
+