X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=libsysfs%2Fsysfs_device.c;h=f2f8d2d611b6c121985b32b93b5fdd0d99445d1f;hb=6818c51d7abeab9914eb7193968b1efa9459a853;hp=66d5f9aef7c71b65b9e9272b053eb4bac8a21525;hpb=bcbe2d8e7d4ccd975e79d0c7defbe1d64d1b129c;p=elogind.git diff --git a/libsysfs/sysfs_device.c b/libsysfs/sysfs_device.c index 66d5f9aef..f2f8d2d61 100644 --- a/libsysfs/sysfs_device.c +++ b/libsysfs/sysfs_device.c @@ -24,15 +24,66 @@ #include "sysfs.h" /** - * get_device_bus: retrieves the bus name the device is on, checks path to - * bus' link to make sure it has correct device. + * 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. */ -static int get_device_bus(struct sysfs_device *dev) +int sysfs_get_device_bus(struct sysfs_device *dev) { - unsigned char subsys[SYSFS_NAME_LEN], path[SYSFS_PATH_MAX]; - unsigned char target[SYSFS_PATH_MAX], *bus = NULL, *c = NULL; + 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) { @@ -41,13 +92,12 @@ static int get_device_bus(struct sysfs_device *dev) } memset(subsys, 0, SYSFS_NAME_LEN); - strcat(subsys, "/"); - strcpy(subsys, SYSFS_BUS_NAME); /* subsys = /bus */ + 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); - strcpy(path, dev->path); + safestrcpy(path, dev->path); c = strstr(path, "/devices"); if (c == NULL) { dprintf("Invalid path to device %s\n", path); @@ -55,25 +105,25 @@ static int get_device_bus(struct sysfs_device *dev) return -1; } *c = '\0'; - strcat(path, "/"); - strcat(path, SYSFS_BUS_NAME); - strcat(path, "/"); - strcat(path, bus); - strcat(path, "/"); - strcat(path, SYSFS_DEVICES_NAME); - strcat(path, "/"); - strcat(path, dev->bus_id); + 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) { + SYSFS_PATH_MAX)) != 0) { dprintf("Error getting link target\n"); sysfs_close_list(buslist); return -1; } if (!(strncmp(target, dev->path, SYSFS_PATH_MAX))) { - strcpy(dev->bus, bus); + safestrcpy(dev->bus, bus); sysfs_close_list(buslist); return 0; } @@ -89,7 +139,7 @@ static int get_device_bus(struct sysfs_device *dev) * closing children only. * @devroot: device root of tree. */ -static void sysfs_close_device_tree(struct sysfs_device *devroot) +void sysfs_close_device_tree(struct sysfs_device *devroot) { if (devroot != NULL) { if (devroot->children != NULL) { @@ -143,7 +193,7 @@ static struct sysfs_device *alloc_device(void) * @name: name of root * returns struct sysfs_directory with success and NULL with error */ -static struct sysfs_directory *open_device_dir(const unsigned char *path) +static struct sysfs_directory *open_device_dir(const char *path) { struct sysfs_directory *rdir = NULL; @@ -172,7 +222,7 @@ static struct sysfs_directory *open_device_dir(const unsigned char *path) * @path: path to device, this is the /sys/devices/ path * returns sysfs_device structure with success or NULL with error */ -struct sysfs_device *sysfs_open_device_path(const unsigned char *path) +struct sysfs_device *sysfs_open_device_path(const char *path) { struct sysfs_device *dev = NULL; @@ -196,16 +246,26 @@ struct sysfs_device *sysfs_open_device_path(const unsigned char *path) sysfs_close_device(dev); return NULL; } - strcpy(dev->path, path); + safestrcpy(dev->path, path); + if ((sysfs_remove_trailing_slash(dev->path)) != 0) { + dprintf("Invalid path to device %s\n", dev->path); + sysfs_close_device(dev); + return NULL; + } /* * The "name" attribute no longer exists... return the device's * sysfs representation instead, in the "dev->name" field, which * implies that the dev->name and dev->bus_id contain same data. */ - strncpy(dev->name, dev->bus_id, SYSFS_NAME_LEN); + safestrcpy(dev->name, dev->bus_id); + + if (sysfs_get_device_bus(dev) != 0) + dprintf("Could not get device bus\n"); - if (get_device_bus(dev) != 0) - strcpy(dev->bus, SYSFS_UNKNOWN); + if (get_dev_driver(dev) != 0) { + dprintf("Could not get device %s's driver\n", dev->bus_id); + safestrcpy(dev->driver_name, SYSFS_UNKNOWN); + } return dev; } @@ -217,7 +277,7 @@ struct sysfs_device *sysfs_open_device_path(const unsigned char *path) * returns struct sysfs_device and its children with success or NULL with * error. */ -static struct sysfs_device *sysfs_open_device_tree(const unsigned char *path) +struct sysfs_device *sysfs_open_device_tree(const char *path) { struct sysfs_device *rootdev = NULL, *new = NULL; struct sysfs_directory *cur = NULL; @@ -250,7 +310,8 @@ static struct sysfs_device *sysfs_open_device_tree(const unsigned char *path) rootdev->children = dlist_new_with_delete (sizeof(struct sysfs_device), sysfs_close_dev_tree); - dlist_unshift(rootdev->children, new); + dlist_unshift_sorted(rootdev->children, + new, sort_list); } } @@ -306,7 +367,7 @@ struct dlist *sysfs_get_root_devices(struct sysfs_root_device *root) root->devices = dlist_new_with_delete (sizeof(struct sysfs_device), sysfs_close_dev_tree); - dlist_unshift(root->devices, dev); + dlist_unshift_sorted(root->devices, dev, sort_list); } return root->devices; @@ -318,10 +379,10 @@ struct dlist *sysfs_get_root_devices(struct sysfs_root_device *root) * @name: name of /sys/devices/root to open * returns struct sysfs_root_device if success and NULL with error */ -struct sysfs_root_device *sysfs_open_root_device(const unsigned char *name) +struct sysfs_root_device *sysfs_open_root_device(const char *name) { struct sysfs_root_device *root = NULL; - unsigned char rootpath[SYSFS_PATH_MAX]; + char rootpath[SYSFS_PATH_MAX]; if (name == NULL) { errno = EINVAL; @@ -334,11 +395,10 @@ struct sysfs_root_device *sysfs_open_root_device(const unsigned char *name) return NULL; } - if (sysfs_trailing_slash(rootpath) == 0) - strcat(rootpath, "/"); - strcat(rootpath, SYSFS_DEVICES_NAME); - strcat(rootpath, "/"); - strcat(rootpath, name); + safestrcat(rootpath, "/"); + safestrcat(rootpath, SYSFS_DEVICES_NAME); + safestrcat(rootpath, "/"); + safestrcat(rootpath, name); if ((sysfs_path_is_dir(rootpath)) != 0) { errno = EINVAL; dprintf("Invalid root device: %s\n", name); @@ -350,8 +410,13 @@ struct sysfs_root_device *sysfs_open_root_device(const unsigned char *name) dprintf("calloc failure\n"); return NULL; } - strcpy(root->name, name); - strcpy(root->path, rootpath); + safestrcpy(root->name, name); + safestrcpy(root->path, rootpath); + if ((sysfs_remove_trailing_slash(root->path)) != 0) { + dprintf("Invalid path to root device %s\n", root->path); + sysfs_close_root_device(root); + return NULL; + } return root; } @@ -362,8 +427,10 @@ struct sysfs_root_device *sysfs_open_root_device(const unsigned char *name) */ struct dlist *sysfs_get_device_attributes(struct sysfs_device *device) { - if (device == NULL) + if (device == NULL) { + errno = EINVAL; return NULL; + } if (device->directory == NULL) { device->directory = sysfs_open_directory(device->path); @@ -373,20 +440,37 @@ struct dlist *sysfs_get_device_attributes(struct sysfs_device *device) if (device->directory->attributes == NULL) { if ((sysfs_read_dir_attributes(device->directory)) != 0) return NULL; - } else { - if ((sysfs_path_is_dir(device->path)) != 0) { - dprintf("Device at %s no longer exists", device->path); - return NULL; - } - if ((sysfs_refresh_attributes - (device->directory->attributes)) != 0) { - dprintf("Error refreshing device attributes\n"); - return NULL; - } } return (device->directory->attributes); } +/** + * sysfs_refresh_device_attributes: refreshes the device's list of attributes + * @device: sysfs_device whose attributes to refresh + * + * NOTE: Upon return, prior references to sysfs_attributes for this device + * _may_ not be valid + * + * Returns list of attributes on success and NULL on failure + */ +struct dlist *sysfs_refresh_device_attributes(struct sysfs_device *device) +{ + if (device == NULL) { + errno = EINVAL; + return NULL; + } + + if (device->directory == NULL) + return (sysfs_get_device_attributes(device)); + + if ((sysfs_refresh_dir_attributes(device->directory)) != 0) { + dprintf("Error refreshing device attributes\n"); + return NULL; + } + + return (device->directory->attributes); +} + /** * sysfs_get_device_attr: searches dev's attributes by name * @dev: device to look through @@ -394,24 +478,20 @@ struct dlist *sysfs_get_device_attributes(struct sysfs_device *device) * returns sysfs_attribute reference with success or NULL with error. */ struct sysfs_attribute *sysfs_get_device_attr(struct sysfs_device *dev, - const unsigned char *name) + const char *name) { - struct sysfs_attribute *cur = NULL; struct dlist *attrlist = NULL; if (dev == NULL || name == NULL) { errno = EINVAL; return NULL; } - + attrlist = sysfs_get_device_attributes(dev); if (attrlist == NULL) return NULL; - cur = sysfs_get_directory_attribute(dev->directory, - (unsigned char *)name); - - return cur; + return sysfs_get_directory_attribute(dev->directory, (char *)name); } /** @@ -422,10 +502,10 @@ 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 unsigned char *device, - const unsigned char *bus, unsigned char *path, size_t psize) +static int get_device_absolute_path(const char *device, const char *bus, + char *path, size_t psize) { - unsigned char bus_path[SYSFS_PATH_MAX]; + char bus_path[SYSFS_PATH_MAX]; if (device == NULL || path == NULL) { errno = EINVAL; @@ -437,20 +517,19 @@ static int get_device_absolute_path(const unsigned char *device, dprintf ("Sysfs not supported on this system\n"); return -1; } - if (sysfs_trailing_slash(bus_path) == 0) - strcat(bus_path, "/"); - strcat(bus_path, SYSFS_BUS_NAME); - strcat(bus_path, "/"); - strcat(bus_path, bus); - strcat(bus_path, "/"); - strcat(bus_path, SYSFS_DEVICES_NAME); - strcat(bus_path, "/"); - strcat(bus_path, device); + safestrcat(bus_path, "/"); + safestrcat(bus_path, SYSFS_BUS_NAME); + safestrcat(bus_path, "/"); + safestrcat(bus_path, bus); + safestrcat(bus_path, "/"); + safestrcat(bus_path, SYSFS_DEVICES_NAME); + safestrcat(bus_path, "/"); + safestrcat(bus_path, device); /* * We now are at /sys/bus/"bus_name"/devices/"device" which is a link. * Now read this link to reach to the device. */ - if ((sysfs_get_link(bus_path, path, SYSFS_PATH_MAX)) != 0) { + if ((sysfs_get_link(bus_path, path, psize)) != 0) { dprintf("Error getting to device %s\n", device); return -1; } @@ -459,17 +538,16 @@ static int get_device_absolute_path(const unsigned char *device, /** * sysfs_open_device: open a device by id (use the "bus" subsystem) + * @bus: bus the device belongs to * @bus_id: bus_id of the device to open - has to be the "bus_id" in * /sys/bus/xxx/devices - * @bus: bus the device belongs to * returns struct sysfs_device if found, NULL otherwise * NOTE: * 1. Use sysfs_close_device to close the device * 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 unsigned char *bus_id, - const unsigned char *bus) +struct sysfs_device *sysfs_open_device(const char *bus, const char *bus_id) { char sysfs_path[SYSFS_PATH_MAX]; struct sysfs_device *device = NULL; @@ -502,7 +580,7 @@ struct sysfs_device *sysfs_open_device(const unsigned char *bus_id, */ struct sysfs_device *sysfs_get_device_parent(struct sysfs_device *dev) { - unsigned char ppath[SYSFS_PATH_MAX], *tmp = NULL; + char ppath[SYSFS_PATH_MAX], *tmp = NULL; if (dev == NULL) { errno = EINVAL; @@ -513,13 +591,13 @@ struct sysfs_device *sysfs_get_device_parent(struct sysfs_device *dev) return (dev->parent); memset(ppath, 0, SYSFS_PATH_MAX); - strcpy(ppath, dev->path); + safestrcpy(ppath, dev->path); tmp = strrchr(ppath, '/'); if (tmp == NULL) { dprintf("Invalid path to device %s\n", ppath); return NULL; } - if (*(tmp +1) == '\0') { + if (*(tmp + 1) == '\0') { *tmp = '\0'; tmp = strrchr(tmp, '/'); if (tmp == NULL) { @@ -532,7 +610,7 @@ struct sysfs_device *sysfs_get_device_parent(struct sysfs_device *dev) /* * All "devices" have the "detach_state" attribute - validate here */ - strcat(ppath, "/detach_state"); + safestrcat(ppath, "/detach_state"); if ((sysfs_path_is_file(ppath)) != 0) { dprintf("Device at %s does not have a parent\n", dev->path); return NULL; @@ -559,11 +637,11 @@ struct sysfs_device *sysfs_get_device_parent(struct sysfs_device *dev) * A call to sysfs_close_attribute() is required to close * the attribute returned and free memory. */ -struct sysfs_attribute *sysfs_open_device_attr(const unsigned char *bus, - const unsigned char *bus_id, const unsigned char *attrib) +struct sysfs_attribute *sysfs_open_device_attr(const char *bus, + const char *bus_id, const char *attrib) { struct sysfs_attribute *attribute = NULL; - unsigned char devpath[SYSFS_PATH_MAX]; + char devpath[SYSFS_PATH_MAX]; if (bus == NULL || bus_id == NULL || attrib == NULL) { errno = EINVAL; @@ -576,8 +654,8 @@ struct sysfs_attribute *sysfs_open_device_attr(const unsigned char *bus, dprintf("Error getting to device %s\n", bus_id); return NULL; } - strcat(devpath, "/"); - strcat(devpath, attrib); + safestrcat(devpath, "/"); + safestrcat(devpath, attrib); attribute = sysfs_open_attribute(devpath); if (attribute == NULL) { dprintf("Error opening attribute %s for device %s\n",