X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=libsysfs%2Fsysfs_bus.c;h=87318c0029c99336ce41720f6954d75e7b4a1792;hb=35b38379bac87ebf4d0cc6884feff588ee859d5f;hp=3e6c22bbb15d382eb2a92479535402e1bdf2869f;hpb=5d4754f19521568b775ba7a31465d3af192ce382;p=elogind.git diff --git a/libsysfs/sysfs_bus.c b/libsysfs/sysfs_bus.c index 3e6c22bbb..87318c002 100644 --- a/libsysfs/sysfs_bus.c +++ b/libsysfs/sysfs_bus.c @@ -20,7 +20,7 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */ -#include "libsysfs.h" +#include "sysfs/libsysfs.h" #include "sysfs.h" static void sysfs_close_dev(void *dev) @@ -121,16 +121,20 @@ struct dlist *sysfs_get_bus_devices(struct sysfs_bus *bus) return NULL; } - dlist_for_each_data(devdir->links, curl, struct sysfs_link) { - bdev = sysfs_open_device(curl->target); - if (bdev == NULL) { - dprintf("Error opening device at %s\n", curl->target); - continue; + if (devdir->links != 0) { + dlist_for_each_data(devdir->links, curl, struct sysfs_link) { + bdev = sysfs_open_device_path(curl->target); + if (bdev == NULL) { + dprintf("Error opening device at %s\n", + curl->target); + continue; + } + if (bus->devices == NULL) + bus->devices = dlist_new_with_delete + (sizeof(struct sysfs_device), + sysfs_close_dev); + dlist_unshift(bus->devices, bdev); } - if (bus->devices == NULL) - bus->devices = dlist_new_with_delete - (sizeof(struct sysfs_device), sysfs_close_dev); - dlist_unshift(bus->devices, bdev); } sysfs_close_directory(devdir); @@ -165,16 +169,21 @@ struct dlist *sysfs_get_bus_drivers(struct sysfs_bus *bus) sysfs_close_directory(drvdir); return NULL; } - dlist_for_each_data(drvdir->subdirs, cursub, struct sysfs_directory) { - driver = sysfs_open_driver(cursub->path); - if (driver == NULL) { - dprintf("Error opening driver at %s\n", cursub->path); - continue; + if (drvdir->subdirs != NULL) { + dlist_for_each_data(drvdir->subdirs, cursub, + struct sysfs_directory) { + driver = sysfs_open_driver_path(cursub->path); + if (driver == NULL) { + dprintf("Error opening driver at %s\n", + cursub->path); + continue; + } + if (bus->drivers == NULL) + bus->drivers = dlist_new_with_delete + (sizeof(struct sysfs_driver), + sysfs_close_drv); + dlist_unshift(bus->drivers, driver); } - if (bus->drivers == NULL) - bus->drivers = dlist_new_with_delete - (sizeof(struct sysfs_driver), sysfs_close_drv); - dlist_unshift(bus->drivers, driver); } sysfs_close_directory(drvdir); return (bus->drivers); @@ -200,9 +209,7 @@ struct sysfs_bus *sysfs_open_bus(const unsigned char *name) return NULL; } - if (sysfs_trailing_slash(buspath) == 0) - strcat(buspath, "/"); - + strcat(buspath, "/"); strcat(buspath, SYSFS_BUS_NAME); strcat(buspath, "/"); strcat(buspath, name); @@ -217,6 +224,11 @@ struct sysfs_bus *sysfs_open_bus(const unsigned char *name) } strcpy(bus->name, name); strcpy(bus->path, buspath); + if ((sysfs_remove_trailing_slash(bus->path)) != 0) { + dprintf("Incorrect path to bus %s\n", bus->path); + sysfs_close_bus(bus); + return NULL; + } return bus; } @@ -287,20 +299,37 @@ struct dlist *sysfs_get_bus_attributes(struct sysfs_bus *bus) if (bus->directory->attributes == NULL) { if ((sysfs_read_dir_attributes(bus->directory)) != 0) return NULL; - } else { - if ((sysfs_path_is_dir(bus->path)) != 0) { - dprintf("Bus at %s no longer exists\n", bus->path); - return NULL; - } - if ((sysfs_refresh_attributes - (bus->directory->attributes)) != 0) { - dprintf("Error refreshing bus attributes\n"); - return NULL; - } } return bus->directory->attributes; } +/** + * sysfs_refresh_bus_attributes: refreshes the bus's list of attributes + * @bus: sysfs_bus whose attributes to refresh + * + * NOTE: Upon return, prior references to sysfs_attributes for this bus + * _may_ not be valid + * + * Returns list of attributes on success and NULL on failure + */ +struct dlist *sysfs_refresh_bus_attributes(struct sysfs_bus *bus) +{ + if (bus == NULL) { + errno = EINVAL; + return NULL; + } + + if (bus->directory == NULL) + return (sysfs_get_bus_attributes(bus)); + + if ((sysfs_refresh_dir_attributes(bus->directory)) != 0) { + dprintf("Error refreshing bus attributes\n"); + return NULL; + } + + return (bus->directory->attributes); +} + /** * sysfs_get_bus_attribute: gets a specific bus attribute, if buses had * attributes. @@ -347,10 +376,8 @@ struct sysfs_device *sysfs_open_bus_device(unsigned char *busname, dprintf("Error getting sysfs mount point\n"); return NULL; } - - if (sysfs_trailing_slash(path) == 0) - strcat(path, "/"); + strcat(path, "/"); strcat(path, SYSFS_BUS_NAME); strcat(path, "/"); strcat(path, busname); @@ -359,7 +386,7 @@ struct sysfs_device *sysfs_open_bus_device(unsigned char *busname, strcat(path, "/"); strcat(path, dev_id); - rdev = sysfs_open_device(path); + rdev = sysfs_open_device_path(path); if (rdev == NULL) { dprintf("Error getting device %s on bus %s\n", dev_id, busname);