chiark / gitweb /
[PATCH] 055 release
[elogind.git] / libsysfs / sysfs_bus.c
index 3e6c22bbb15d382eb2a92479535402e1bdf2869f..d47b94ca24681fee52af3d2b4cb410d0fd65bf2c 100644 (file)
@@ -44,7 +44,7 @@ static int bus_device_id_equal(void *a, void *b)
        if (a == NULL || b == NULL)
                return 0;
 
-       if (strcmp(((unsigned char *)a), ((struct sysfs_device *)b)->bus_id) 
+       if (strcmp(((char *)a), ((struct sysfs_device *)b)->bus_id) 
            == 0)
                return 1;
        return 0;
@@ -61,7 +61,7 @@ static int bus_driver_name_equal(void *a, void *b)
        if (a == NULL || b == NULL)
                return 0;
 
-       if (strcmp(((unsigned char *)a), ((struct sysfs_driver *)b)->name) == 0)
+       if (strcmp(((char *)a), ((struct sysfs_driver *)b)->name) == 0)
                return 1;
        return 0;
 }
@@ -102,16 +102,16 @@ struct dlist *sysfs_get_bus_devices(struct sysfs_bus *bus)
        struct sysfs_device *bdev = NULL;
        struct sysfs_directory *devdir = NULL;
        struct sysfs_link *curl = NULL;
-       unsigned char path[SYSFS_PATH_MAX];
+       char path[SYSFS_PATH_MAX];
 
        if (bus == NULL) {
                errno = EINVAL;
                return NULL;
        }
        memset(path, 0, SYSFS_PATH_MAX);
-       strcpy(path, bus->path);
-       strcat(path, "/");
-       strcat(path, SYSFS_DEVICES_NAME);
+       safestrcpy(path, bus->path);
+       safestrcat(path, "/");
+       safestrcat(path, SYSFS_DEVICES_NAME);
        devdir = sysfs_open_directory(path);
        if (devdir == NULL) 
                return NULL;
@@ -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 != NULL) {
+               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_sorted(bus->devices, bdev, sort_list);
                }
-               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);
 
@@ -147,16 +151,16 @@ struct dlist *sysfs_get_bus_drivers(struct sysfs_bus *bus)
        struct sysfs_driver *driver = NULL;
        struct sysfs_directory *drvdir = NULL;
        struct sysfs_directory *cursub = NULL;
-       unsigned char path[SYSFS_PATH_MAX];
+       char path[SYSFS_PATH_MAX];
 
        if (bus == NULL) {
                errno = EINVAL;
                return NULL;
        }
        memset(path, 0, SYSFS_PATH_MAX);
-       strcpy(path, bus->path);
-       strcat(path, "/");
-       strcat(path, SYSFS_DRIVERS_NAME);
+       safestrcpy(path, bus->path);
+       safestrcat(path, "/");
+       safestrcat(path, SYSFS_DRIVERS_NAME);
        drvdir = sysfs_open_directory(path);
        if (drvdir == NULL) 
                return NULL;
@@ -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_sorted(bus->drivers, driver, sort_list);
                }
-               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);
@@ -184,10 +193,10 @@ struct dlist *sysfs_get_bus_drivers(struct sysfs_bus *bus)
  * sysfs_open_bus: opens specific bus and all its devices on system
  * returns sysfs_bus structure with success or NULL with error.
  */
-struct sysfs_bus *sysfs_open_bus(const unsigned char *name)
+struct sysfs_bus *sysfs_open_bus(const char *name)
 {
        struct sysfs_bus *bus = NULL;
-       unsigned char buspath[SYSFS_PATH_MAX];
+       char buspath[SYSFS_PATH_MAX];
 
        if (name == NULL) {
                errno = EINVAL;
@@ -200,12 +209,10 @@ struct sysfs_bus *sysfs_open_bus(const unsigned char *name)
                return NULL;
        }
 
-       if (sysfs_trailing_slash(buspath) == 0)
-               strcat(buspath, "/");
-
-       strcat(buspath, SYSFS_BUS_NAME);
-       strcat(buspath, "/");
-       strcat(buspath, name);
+       safestrcat(buspath, "/");
+       safestrcat(buspath, SYSFS_BUS_NAME);
+       safestrcat(buspath, "/");
+       safestrcat(buspath, name);
        if ((sysfs_path_is_dir(buspath)) != 0) {
                dprintf("Invalid path to bus: %s\n", buspath);
                return NULL;
@@ -215,8 +222,13 @@ struct sysfs_bus *sysfs_open_bus(const unsigned char *name)
                dprintf("calloc failed\n");
                return NULL;
        }
-       strcpy(bus->name, name);        
-       strcpy(bus->path, buspath);
+       safestrcpy(bus->name, name);    
+       safestrcpy(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;
 }
@@ -227,8 +239,7 @@ struct sysfs_bus *sysfs_open_bus(const unsigned char *name)
  * @id: bus_id for device
  * returns struct sysfs_device reference or NULL if not found.
  */
-struct sysfs_device *sysfs_get_bus_device(struct sysfs_bus *bus, 
-                                                       unsigned char *id)
+struct sysfs_device *sysfs_get_bus_device(struct sysfs_bus *bus, char *id)
 {
        if (bus == NULL || id == NULL) {
                errno = EINVAL;
@@ -252,7 +263,7 @@ struct sysfs_device *sysfs_get_bus_device(struct sysfs_bus *bus,
  * returns struct sysfs_driver reference or NULL if not found.
  */
 struct sysfs_driver *sysfs_get_bus_driver(struct sysfs_bus *bus, 
-                                                       unsigned char *drvname)
+                                                       char *drvname)
 {
        if (bus == NULL || drvname == NULL) {
                errno = EINVAL;
@@ -287,20 +298,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.
@@ -309,7 +337,7 @@ struct dlist *sysfs_get_bus_attributes(struct sysfs_bus *bus)
  * returns reference to sysfs_attribute if found or NULL if not found
  */
 struct sysfs_attribute *sysfs_get_bus_attribute(struct sysfs_bus *bus,
-                                               unsigned char *attrname)
+                                               char *attrname)
 {
        struct dlist *attrlist = NULL;
        
@@ -324,51 +352,6 @@ struct sysfs_attribute *sysfs_get_bus_attribute(struct sysfs_bus *bus,
        return sysfs_get_directory_attribute(bus->directory, attrname);
 }
 
-/**
- * sysfs_open_bus_device: locates a device on a bus and returns it. Device
- *     must be closed using sysfs_close_device.
- * @busname: Name of bus to search
- * @dev_id: Id of device on bus.
- * returns sysfs_device if found or NULL if not.
- */
-struct sysfs_device *sysfs_open_bus_device(unsigned char *busname, 
-                                                       unsigned char *dev_id)
-{
-       struct sysfs_device *rdev = NULL;
-       char path[SYSFS_PATH_MAX];
-
-       if (busname == NULL || dev_id == NULL) {
-               errno = EINVAL;
-               return NULL;
-       }
-
-       memset(path, 0, SYSFS_PATH_MAX);
-       if (sysfs_get_mnt_path(path, SYSFS_PATH_MAX) != 0) {
-               dprintf("Error getting sysfs mount point\n");
-               return NULL;
-       }
-       
-       if (sysfs_trailing_slash(path) == 0)
-               strcat(path, "/");
-
-       strcat(path, SYSFS_BUS_NAME);
-       strcat(path, "/");
-       strcat(path, busname);
-       strcat(path, "/");
-       strcat(path, SYSFS_DEVICES_NAME);
-       strcat(path, "/");
-       strcat(path, dev_id);
-
-       rdev = sysfs_open_device(path);
-       if (rdev == NULL) {
-               dprintf("Error getting device %s on bus %s\n",
-                               dev_id, busname);
-               return NULL;
-       }
-       
-       return rdev;
-}
-
 /**
  * sysfs_find_driver_bus: locates the bus the driver is on.
  * @driver: name of the driver to locate
@@ -376,10 +359,9 @@ struct sysfs_device *sysfs_open_bus_device(unsigned char *busname,
  * @bsize: buffer size
  * returns 0 with success, -1 with error
  */
-int sysfs_find_driver_bus(const unsigned char *driver, unsigned char *busname,
-                                                       size_t bsize)
+int sysfs_find_driver_bus(const char *driver, char *busname, size_t bsize)
 {
-       unsigned char subsys[SYSFS_PATH_MAX], *bus = NULL, *curdrv = NULL;
+       char subsys[SYSFS_PATH_MAX], *bus = NULL, *curdrv = NULL;
        struct dlist *buslist = NULL, *drivers = NULL;
 
        if (driver == NULL || busname == NULL) {
@@ -388,23 +370,22 @@ int sysfs_find_driver_bus(const unsigned char *driver, unsigned char *busname,
        }
 
        memset(subsys, 0, SYSFS_PATH_MAX);
-       strcat(subsys, "/");
-       strcpy(subsys, SYSFS_BUS_NAME);
+       safestrcpy(subsys, SYSFS_BUS_NAME);
        buslist = sysfs_open_subsystem_list(subsys);
        if (buslist != NULL) {
                dlist_for_each_data(buslist, bus, char) {
                        memset(subsys, 0, SYSFS_PATH_MAX);
-                       strcat(subsys, "/");
-                       strcpy(subsys, SYSFS_BUS_NAME);
-                       strcat(subsys, "/");
-                       strcat(subsys, bus);
-                       strcat(subsys, "/");
-                       strcat(subsys, SYSFS_DRIVERS_NAME);
+                       safestrcpy(subsys, SYSFS_BUS_NAME);
+                       safestrcat(subsys, "/");
+                       safestrcat(subsys, bus);
+                       safestrcat(subsys, "/");
+                       safestrcat(subsys, SYSFS_DRIVERS_NAME);
                        drivers = sysfs_open_subsystem_list(subsys);
                        if (drivers != NULL) {
                                dlist_for_each_data(drivers, curdrv, char) {
                                        if (strcmp(driver, curdrv) == 0) {
-                                               strncpy(busname, bus, bsize);
+                                               safestrcpymax(busname, 
+                                                               bus, bsize);
                                                sysfs_close_list(drivers);
                                                sysfs_close_list(buslist);
                                                return 0;