From ff44a6b0b7e98c9f696ee13c197d982819991de8 Mon Sep 17 00:00:00 2001 From: "dsteklof@us.ibm.com" Date: Mon, 24 Nov 2003 23:47:43 -0800 Subject: [PATCH] [PATCH] libsysfs changes for sysfsutils 0.3.0 Here's the patch to up the library to the sysfsutils-0_3_0 level. The following changes: 1) adds class name to sysfs_class_device structure 2) adds bus to sysfs_device 3) gets rid of code that made assumptions as to bus addresses being unique across buses, which isn't the case. I still owe you: 1) change getpagesize->sysconf. This is in the CVS tree and part of other changes we're currently testing. Patch will follow. 2) you need a function to get a sysfs_class_device's parent. We hadn't considered class devices to have parents, the one example of a multilevel is the block class. We will add this function and send the patch to you. --- libsysfs/libsysfs.h | 36 ++---- libsysfs/sysfs_bus.c | 46 +------ libsysfs/sysfs_class.c | 275 ++++++++++++++++++---------------------- libsysfs/sysfs_device.c | 269 ++++++++++++++++++++------------------- libsysfs/sysfs_driver.c | 167 +++++++----------------- libsysfs/sysfs_utils.c | 40 +++++- 6 files changed, 351 insertions(+), 482 deletions(-) diff --git a/libsysfs/libsysfs.h b/libsysfs/libsysfs.h index b3ffc467d..f7e989e24 100644 --- a/libsysfs/libsysfs.h +++ b/libsysfs/libsysfs.h @@ -32,8 +32,11 @@ #define SYSFS_FSTYPE_NAME "sysfs" #define SYSFS_PROC_MNTS "/proc/mounts" #define SYSFS_BUS_DIR "/bus" +#define SYSFS_BUS_NAME "bus" #define SYSFS_CLASS_DIR "/class" +#define SYSFS_CLASS_NAME "class" #define SYSFS_BLOCK_DIR "/block" +#define SYSFS_BLOCK_NAME "block" #define SYSFS_DEVICES_DIR "/devices" #define SYSFS_DEVICES_NAME "devices" #define SYSFS_DRIVERS_DIR "/drivers" @@ -42,10 +45,6 @@ #define SYSFS_UNKNOWN "unknown" #define SYSFS_PATH_ENV "SYSFS_PATH" -/* Some "block" subsystem specific #defines */ -#define SYSFS_QUEUE_NAME "queue" -#define SYSFS_IOSCHED_NAME "iosched" - #define SYSFS_PATH_MAX 255 #define SYSFS_NAME_LEN 50 #define SYSFS_BUS_ID_SIZE 20 @@ -89,8 +88,9 @@ struct sysfs_device { struct dlist *children; unsigned char name[SYSFS_NAME_LEN]; unsigned char bus_id[SYSFS_NAME_LEN]; - unsigned char path[SYSFS_PATH_MAX]; + unsigned char bus[SYSFS_NAME_LEN]; unsigned char driver_name[SYSFS_NAME_LEN]; + unsigned char path[SYSFS_PATH_MAX]; /* for internal use only */ struct sysfs_directory *directory; @@ -119,6 +119,7 @@ struct sysfs_class_device { struct sysfs_device *sysdevice; /* NULL if virtual */ struct sysfs_driver *driver; /* NULL if not implemented */ unsigned char name[SYSFS_NAME_LEN]; + unsigned char classname[SYSFS_NAME_LEN]; unsigned char path[SYSFS_PATH_MAX]; /* for internal use only */ @@ -185,10 +186,8 @@ extern struct dlist *sysfs_get_driver_links(struct sysfs_driver *driver); extern void sysfs_close_driver_by_name(struct sysfs_driver *driver); extern struct sysfs_driver *sysfs_open_driver_by_name (const unsigned char *drv_name, const unsigned char *bus, size_t bsize); -extern int sysfs_write_driver_attr(unsigned char *drv, unsigned char *attrib, - unsigned char *value, size_t len); -extern int sysfs_read_driver_attr(unsigned char *drv, unsigned char *attrib, - unsigned char *value, size_t len); +extern struct sysfs_attribute *sysfs_open_driver_attr(const unsigned char *bus, + const unsigned char *drv, const unsigned char *attrib); /* generic sysfs device access */ extern void sysfs_close_root_device(struct sysfs_root_device *root); @@ -201,10 +200,8 @@ extern struct sysfs_attribute *sysfs_get_device_attr extern struct dlist *sysfs_get_device_attributes(struct sysfs_device *device); extern struct sysfs_device *sysfs_open_device_by_id (const unsigned char *bus_id, const unsigned char *bus, size_t bsize); -extern int sysfs_write_device_attr(unsigned char *dev, unsigned char *attrib, - unsigned char *value, size_t len); -extern int sysfs_read_device_attr(unsigned char *dev, unsigned char *attrib, - unsigned char *value, size_t len); +extern struct sysfs_attribute *sysfs_open_device_attr(const unsigned char *bus, + const unsigned char *bus_id, const unsigned char *attrib); /* generic sysfs bus access */ extern void sysfs_close_bus(struct sysfs_bus *bus); @@ -218,8 +215,6 @@ extern struct sysfs_attribute *sysfs_get_bus_attribute(struct sysfs_bus *bus, unsigned char *attrname); extern struct sysfs_device *sysfs_open_bus_device(unsigned char *busname, unsigned char *dev_id); -extern int sysfs_find_device_bus(const unsigned char *dev_id, - unsigned char *busname, size_t bsize); extern int sysfs_find_driver_bus(const unsigned char *driver, unsigned char *busname, size_t bsize); @@ -232,17 +227,14 @@ extern struct sysfs_class *sysfs_open_class(const unsigned char *name); extern struct sysfs_class_device *sysfs_get_class_device (struct sysfs_class *class, unsigned char *name); extern struct sysfs_class_device *sysfs_open_class_device_by_name - (const unsigned char *class, unsigned char *name); + (const unsigned char *class, const unsigned char *name); extern struct dlist *sysfs_get_classdev_attributes (struct sysfs_class_device *cdev); -extern int sysfs_find_device_class(const unsigned char *bus_id, - unsigned char *classname, size_t bsize); extern struct sysfs_attribute *sysfs_get_classdev_attr (struct sysfs_class_device *clsdev, const unsigned char *name); -extern int sysfs_write_classdev_attr(unsigned char *dev, unsigned char *attrib, - unsigned char *value, size_t len); -extern int sysfs_read_classdev_attr(unsigned char *dev, unsigned char *attrib, - unsigned char *value, size_t len); +extern struct sysfs_attribute *sysfs_open_classdev_attr + (const unsigned char *classname, const unsigned char *dev, + const unsigned char *attrib); #ifdef __cplusplus } diff --git a/libsysfs/sysfs_bus.c b/libsysfs/sysfs_bus.c index 19fc275d8..3111154cb 100644 --- a/libsysfs/sysfs_bus.c +++ b/libsysfs/sysfs_bus.c @@ -25,12 +25,12 @@ static void sysfs_close_dev(void *dev) { - sysfs_close_device((struct sysfs_device *)dev); + sysfs_close_device((struct sysfs_device *)dev); } static void sysfs_close_drv(void *drv) { - sysfs_close_driver((struct sysfs_driver *)drv); + sysfs_close_driver((struct sysfs_driver *)drv); } /* @@ -423,48 +423,6 @@ struct sysfs_device *sysfs_open_bus_device(unsigned char *busname, return rdev; } -/** - * sysfs_find_device_bus: locates the bus a device is on. - * @dev_id: device id. - * @busname: buffer to copy name to - * @bsize: buffer size - * returns 0 with success or -1 with error - */ -int sysfs_find_device_bus(const unsigned char *dev_id, unsigned char *busname, - size_t bsize) -{ - unsigned char subsys[SYSFS_NAME_LEN], *bus = NULL, *curdev = NULL; - struct dlist *buslist = NULL, *device_list = NULL; - - if (dev_id == NULL || busname == NULL) { - errno = EINVAL; - return -1; - } - - strcpy(subsys, SYSFS_BUS_DIR); /* subsys = /bus */ - buslist = sysfs_open_subsystem_list(subsys); - if (buslist != NULL) { - dlist_for_each_data(buslist, bus, char) { - device_list = sysfs_open_bus_devices_list(bus); - if (device_list != NULL) { - dlist_for_each_data(device_list, - curdev, char) { - if (strcmp(dev_id, curdev) == 0) { - strncpy(busname, - bus, bsize); - sysfs_close_list(device_list); - sysfs_close_list(buslist); - return 0; - } - } - sysfs_close_list(device_list); - } - } - sysfs_close_list(buslist); - } - return -1; -} - /** * sysfs_find_driver_bus: locates the bus the driver is on. * @driver: name of the driver to locate diff --git a/libsysfs/sysfs_class.c b/libsysfs/sysfs_class.c index cb6ca9d00..a0273565d 100644 --- a/libsysfs/sysfs_class.c +++ b/libsysfs/sysfs_class.c @@ -23,7 +23,7 @@ #include "libsysfs.h" #include "sysfs.h" -void sysfs_close_cls_dev(void *dev) +static void sysfs_close_cls_dev(void *dev) { sysfs_close_class_device((struct sysfs_class_device *)dev); } @@ -116,9 +116,17 @@ static struct sysfs_directory *open_class_dir(const unsigned char *name) return NULL; } - strcat(classpath, SYSFS_CLASS_DIR); - strcat(classpath, "/"); - strcat(classpath, name); + /* + * We shall now treat "block" also as a class. Hence, check here + * if "name" is "block" and proceed accordingly + */ + if (strcmp(name, SYSFS_BLOCK_NAME) == 0) { + strcat(classpath, SYSFS_BLOCK_DIR); + } else { + strcat(classpath, SYSFS_CLASS_DIR); + strcat(classpath, "/"); + strcat(classpath, name); + } classdir = sysfs_open_directory(classpath); if (classdir == NULL) { errno = EINVAL; @@ -134,6 +142,39 @@ static struct sysfs_directory *open_class_dir(const unsigned char *name) return classdir; } +/** + * set_classdev_classname: Grabs classname from path + * @cdev: class device to set + * Returns nothing + */ +static void set_classdev_classname(struct sysfs_class_device *cdev) +{ + unsigned char *c = NULL, *e = NULL; + int count = 0; + + c = strstr(cdev->path, SYSFS_CLASS_DIR); + if (c == NULL) + c = strstr(cdev->path, SYSFS_BLOCK_DIR); + else { + c++; + while (c != NULL && *c != '/') + c++; + } + + if (c == NULL) + strcpy(cdev->classname, SYSFS_UNKNOWN); + + else { + c++; + e = c; + while (e != NULL && *e != '/' && *e != '\0') { + e++; + count++; + } + strncpy(cdev->classname, c, count); + } +} + /** * sysfs_open_class_device: Opens and populates class device * @path: path to class device. @@ -178,6 +219,7 @@ struct sysfs_class_device *sysfs_open_class_device(const unsigned char *path) sysfs_read_all_subdirs(dir); cdev->directory = dir; strcpy(cdev->path, dir->path); + set_classdev_classname(cdev); /* get driver and device, if implemented */ if (cdev->directory->links != NULL) { @@ -303,47 +345,73 @@ struct sysfs_class_device *sysfs_get_class_device(struct sysfs_class *class, name, class_name_equal); } +/** + * get_classdev_path: given the class and a device in the class, return the + * absolute path to the device + * @classname: name of the class + * @clsdev: the class device + * @path: buffer to return path + * @psize: size of "path" + * Returns 0 on SUCCESS or -1 on error + */ +static int get_classdev_path(const unsigned char *classname, + const unsigned char *clsdev, unsigned char *path, size_t len) +{ + if (classname == NULL || clsdev == NULL || path == NULL) { + errno = EINVAL; + return -1; + } + if (sysfs_get_mnt_path(path, len) != 0) { + dprintf("Error getting sysfs mount path\n"); + return -1; + } + if (strcmp(classname, SYSFS_BLOCK_NAME) == 0) { + strcat(path, SYSFS_BLOCK_DIR); + } else { + strcat(path, SYSFS_CLASS_DIR); + strcat(path, "/"); + strcat(path, classname); + } + strcat(path, "/"); + strcat(path, clsdev); + return 0; +} + /** * sysfs_open_class_device_by_name: Locates a specific class_device and returns it. * Class_device must be closed using sysfs_close_class_device * @classname: Class to search * @name: name of the class_device + * + * NOTE: + * Call sysfs_close_class_device() to close the class device */ struct sysfs_class_device *sysfs_open_class_device_by_name - (const unsigned char *classname, unsigned char *name) + (const unsigned char *classname, const unsigned char *name) { - struct sysfs_class *class = NULL; - struct sysfs_class_device *cdev = NULL, *rcdev = NULL; + unsigned char devpath[SYSFS_PATH_MAX]; + struct sysfs_class_device *cdev = NULL; if (classname == NULL || name == NULL) { errno = EINVAL; return NULL; } - class = sysfs_open_class(classname); - if (class == NULL) { - dprintf("Error opening class %s\n", classname); + memset(devpath, 0, SYSFS_PATH_MAX); + if ((get_classdev_path(classname, name, devpath, + SYSFS_PATH_MAX)) != 0) { + dprintf("Error getting to device %s on class %s\n", + name, classname); return NULL; } - - cdev = sysfs_get_class_device(class, name); + + cdev = sysfs_open_class_device(devpath); if (cdev == NULL) { dprintf("Error getting class device %s from class %s\n", name, classname); - sysfs_close_class(class); - return NULL; - } - - rcdev = sysfs_open_class_device(cdev->directory->path); - if (rcdev == NULL) { - dprintf("Error getting class device %s from class %s\n", - name, classname); - sysfs_close_class(class); return NULL; } - sysfs_close_class(class); - - return rcdev; + return cdev; } /** @@ -360,53 +428,6 @@ struct dlist *sysfs_get_classdev_attributes(struct sysfs_class_device *cdev) return (cdev->directory->attributes); } -/** - * sysfs_find_device_class: locates the device the device is on - * @bus_id: device to look for - * @classname: buffer to copy class name to - * @bsize: size of buffer - * returns 0 with success and -1 with error - */ -int sysfs_find_device_class(const unsigned char *bus_id, - unsigned char *classname, size_t bsize) -{ - unsigned char class[SYSFS_NAME_LEN], clspath[SYSFS_NAME_LEN]; - unsigned char *cls = NULL, *clsdev = NULL; - struct dlist *clslist = NULL, *clsdev_list = NULL; - - if (bus_id == NULL || classname == NULL) { - errno = EINVAL; - return -1; - } - - strcpy(class, SYSFS_CLASS_DIR); - clslist = sysfs_open_subsystem_list(class); - if (clslist != NULL) { - dlist_for_each_data(clslist, cls, char) { - memset(clspath, 0, SYSFS_NAME_LEN); - strcpy(clspath, SYSFS_CLASS_DIR); - strcat(clspath, "/"); - strcat(clspath, cls); - clsdev_list = sysfs_open_subsystem_list(clspath); - if (clsdev_list != NULL) { - dlist_for_each_data(clsdev_list, - clsdev, char) { - if (strcmp(bus_id, clsdev) == 0) { - strncpy(classname, - cls, bsize); - sysfs_close_list(clsdev_list); - sysfs_close_list(clslist); - return 0; - } - } - sysfs_close_list(clsdev_list); - } - } - sysfs_close_list(clslist); - } - return -1; -} - /** * sysfs_get_classdev_attr: searches class device's attributes by name * @clsdev: class device to look through @@ -433,99 +454,45 @@ struct sysfs_attribute *sysfs_get_classdev_attr } /** - * sysfs_write_classdev_attr: modify writable attribute value for the given - * class device - * @dev: class device name for which the attribute has to be changed - * @attrib: attribute to change - * @value: value to change to - * @len: size of buffer at "value" - * Returns 0 on success and -1 on error - */ -int sysfs_write_classdev_attr(unsigned char *dev, unsigned char *attrib, - unsigned char *value, size_t len) -{ - struct sysfs_class_device *clsdev = NULL; - struct sysfs_attribute *attribute = NULL; - unsigned char class_name[SYSFS_NAME_LEN]; - - if (dev == NULL || attrib == NULL || value == NULL) { - errno = EINVAL; - return -1; - } - - memset(class_name, 0, SYSFS_NAME_LEN); - if ((sysfs_find_device_class(dev, - class_name, SYSFS_NAME_LEN)) < 0) { - dprintf("Class device %s not found\n", dev); - return -1; - } - clsdev = sysfs_open_class_device_by_name(class_name, dev); - if (clsdev == NULL) { - dprintf("Error opening %s in class %s\n", dev, class_name); - return -1; - } - attribute = sysfs_get_directory_attribute(clsdev->directory, attrib); - if (attribute == NULL) { - dprintf("Attribute %s not defined for device %s on class %s\n", - attrib, dev, class_name); - sysfs_close_class_device(clsdev); - return -1; - } - if ((sysfs_write_attribute(attribute, value, len)) < 0) { - dprintf("Error setting %s to %s\n", attrib, value); - sysfs_close_class_device(clsdev); - return -1; - } - sysfs_close_class_device(clsdev); - return 0; -} - -/** - * sysfs_read_classdev_attr: read an attribute for a given class device + * sysfs_open_classdev_attr: read an attribute for a given class device + * @classname: name of the class on which to look * @dev: class device name for which the attribute has to be read * @attrib: attribute to read - * @value: buffer to return value to user - * @len: size of buffer at "value" - * Returns 0 on success and -1 on error + * Returns sysfs_attribute * on SUCCESS and NULL on error + * + * NOTE: + * A call to sysfs_close_attribute() is required to close the + * attribute returned and to free memory */ -int sysfs_read_classdev_attr(unsigned char *dev, unsigned char *attrib, - unsigned char *value, size_t len) +struct sysfs_attribute *sysfs_open_classdev_attr(const unsigned char *classname, + const unsigned char *dev, const unsigned char *attrib) { - struct sysfs_class_device *clsdev = NULL; struct sysfs_attribute *attribute = NULL; - unsigned char class_name[SYSFS_NAME_LEN]; + unsigned char path[SYSFS_PATH_MAX]; - if (dev == NULL || attrib == NULL || value == NULL) { + if (classname == NULL || dev == NULL || attrib == NULL) { errno = EINVAL; - return -1; - } - - memset(class_name, 0, SYSFS_NAME_LEN); - if ((sysfs_find_device_class(dev, - class_name, SYSFS_NAME_LEN)) < 0) { - dprintf("Class device %s not found\n", dev); - return -1; + return NULL; } - clsdev = sysfs_open_class_device_by_name(class_name, dev); - if (clsdev == NULL) { - dprintf("Error opening %s in class %s\n", dev, class_name); - return -1; + memset(path, 0, SYSFS_PATH_MAX); + if ((get_classdev_path(classname, dev, path, SYSFS_PATH_MAX)) != 0) { + dprintf("Error getting to device %s on class %s\n", + dev, classname); + return NULL; } - attribute = sysfs_get_directory_attribute(clsdev->directory, attrib); + strcat(path, "/"); + strcat(path, attrib); + attribute = sysfs_open_attribute(path); if (attribute == NULL) { - dprintf("Attribute %s not defined for device %s on class %s\n", - attrib, dev, class_name); - sysfs_close_class_device(clsdev); - return -1; + dprintf("Error opening attribute %s on class device %s\n", + attrib, dev); + return NULL; } - if (attribute->len > len) { - dprintf("Value length %d is greater that suppled buffer %d\n", - attribute->len, len); - sysfs_close_class_device(clsdev); - return -1; + if ((sysfs_read_attribute(attribute)) != 0) { + dprintf("Error reading attribute %s for class device %s\n", + attrib, dev); + sysfs_close_attribute(attribute); + return NULL; } - strncpy(value, attribute->value, attribute->len); - value[(attribute->len)+1] = 0; - sysfs_close_class_device(clsdev); - return 0; + return attribute; } diff --git a/libsysfs/sysfs_device.c b/libsysfs/sysfs_device.c index 89704dcd8..fbd046f22 100644 --- a/libsysfs/sysfs_device.c +++ b/libsysfs/sysfs_device.c @@ -23,6 +23,77 @@ #include "libsysfs.h" #include "sysfs.h" +static int confirm_device_bus(struct sysfs_device *dev, + unsigned char *busname, unsigned char *bus_id) +{ + struct sysfs_link *devlink = NULL; + unsigned char devpath[SYSFS_PATH_MAX]; + int result = 0; + + if (busname == NULL || bus_id == NULL) + return -1; + + if (sysfs_get_mnt_path(devpath, SYSFS_PATH_MAX) != 0) + return -1; + + strcat(devpath, SYSFS_BUS_DIR); + strcat(devpath, "/"); + strcat(devpath, busname); + strcat(devpath, SYSFS_DEVICES_DIR); + strcat(devpath, "/"); + strcat(devpath, bus_id); + + devlink = sysfs_open_link(devpath); + if (devlink == NULL) + return -1; + + if (strcmp(devlink->target, dev->path) == 0) + result++; + sysfs_close_link(devlink); + return result; +} + +/** + * 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) +{ + unsigned char subsys[SYSFS_NAME_LEN], *bus = NULL, *curdev = NULL; + struct dlist *buslist = NULL, *device_list = NULL; + + if (dev == NULL) { + errno = EINVAL; + return -1; + } + + strcpy(subsys, SYSFS_BUS_DIR); /* subsys = /bus */ + buslist = sysfs_open_subsystem_list(subsys); + if (buslist != NULL) { + dlist_for_each_data(buslist, bus, char) { + device_list = sysfs_open_bus_devices_list(bus); + if (device_list != NULL) { + dlist_for_each_data(device_list, + curdev, char) { + if (strcmp(dev->bus_id, curdev) == 0 + && confirm_device_bus(dev, bus, + curdev) > 0) { + strcpy(dev->bus, bus); + sysfs_close_list(device_list); + sysfs_close_list(buslist); + return 0; + } + } + sysfs_close_list(device_list); + } + } + sysfs_close_list(buslist); + } + return -1; +} + /** * sysfs_close_device_tree: closes every device in the supplied tree, * closing children only. @@ -150,6 +221,9 @@ struct sysfs_device *sysfs_open_device(const unsigned char *path) * implies that the dev->name and dev->bus_id contain same data. */ strncpy(dev->name, sdir->name, SYSFS_NAME_LEN); + + if (get_device_bus(dev) != 0) + strcpy(dev->bus, SYSFS_UNKNOWN); return dev; } @@ -338,55 +412,6 @@ struct dlist *sysfs_get_device_attributes(struct sysfs_device *device) return (device->directory->attributes); } -/** - * sysfs_open_device_by_id: open a device by id (use the "bus" subsystem) - * @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 - * @bsize: size of the bus buffer - * 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_by_id(const unsigned char *bus_id, - const unsigned char *bus, size_t bsize) -{ - char sysfs_path[SYSFS_PATH_MAX], device_path[SYSFS_PATH_MAX]; - struct sysfs_device *device = NULL; - - if (bus_id == NULL || bus == NULL) { - errno = EINVAL; - return NULL; - } - memset(sysfs_path, 0, SYSFS_PATH_MAX); - if ((sysfs_get_mnt_path(sysfs_path, SYSFS_PATH_MAX)) != 0) { - dprintf("Error getting sysfs mount path\n"); - return NULL; - } - strcat(sysfs_path, SYSFS_BUS_DIR); - strcat(sysfs_path, "/"); - strncat(sysfs_path, bus, bsize); - strcat(sysfs_path, SYSFS_DEVICES_DIR); - strcat(sysfs_path, "/"); - strcat(sysfs_path, bus_id); - - /* devices under /sys/bus/xxx/devices are links to devices subsystem */ - if ((sysfs_get_link(sysfs_path, device_path, SYSFS_PATH_MAX)) < 0) { - dprintf("Error getting device path\n"); - return NULL; - } - - device = sysfs_open_device(device_path); - if (device == NULL) { - dprintf("Error opening device %s\n", bus_id); - return NULL; - } - - return device; -} - /** * get_device_absolute_path: looks up the bus the device is on, gets * absolute path to the device @@ -395,29 +420,24 @@ struct sysfs_device *sysfs_open_device_by_id(const unsigned char *bus_id, * @psize: size of "path" * Returns 0 on success -1 on failure */ -static int get_device_absolute_path(const unsigned char *device, - unsigned char *path, size_t psize) +static int get_device_absolute_path(const unsigned char *device, + const unsigned char *bus, unsigned char *path, size_t psize) { - unsigned char bus_name[SYSFS_NAME_LEN], bus_path[SYSFS_PATH_MAX]; + unsigned char bus_path[SYSFS_NAME_LEN]; if (device == NULL || path == NULL) { errno = EINVAL; return -1; } - memset(bus_name, 0, SYSFS_NAME_LEN); memset(bus_path, 0, SYSFS_NAME_LEN); - if ((sysfs_find_device_bus(device, bus_name, SYSFS_NAME_LEN)) != 0) { - dprintf("Device %s not found\n", device); - return -1; - } if (sysfs_get_mnt_path(bus_path, SYSFS_PATH_MAX) != 0) { dprintf ("Sysfs not supported on this system\n"); return -1; } strcat(bus_path, SYSFS_BUS_DIR); strcat(bus_path, "/"); - strcat(bus_path, bus_name); + strcat(bus_path, bus); strcat(bus_path, SYSFS_DEVICES_DIR); strcat(bus_path, "/"); strcat(bus_path, device); @@ -433,106 +453,85 @@ static int get_device_absolute_path(const unsigned char *device, } /** - * sysfs_write_device_attr: modify a "writable" attribute for the given device - * @dev: device bus_id for which attribute has to be changed - * @attrib: attribute to change - * @value: value to change to - * @len: "value" length to write - * Returns 0 on success -1 on error - */ -int sysfs_write_device_attr(unsigned char *dev, unsigned char *attrib, - unsigned char *value, size_t len) + * sysfs_open_device_by_id: open a device by id (use the "bus" subsystem) + * @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 + * @bsize: size of the bus buffer + * 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_by_id(const unsigned char *bus_id, + const unsigned char *bus, size_t bsize) { - struct sysfs_attribute *attribute = NULL; - unsigned char devpath[SYSFS_PATH_MAX]; + char sysfs_path[SYSFS_PATH_MAX]; + struct sysfs_device *device = NULL; - if (dev == NULL || attrib == NULL || value == NULL) { + if (bus_id == NULL || bus == NULL) { errno = EINVAL; - return -1; - } - - memset(devpath, 0, SYSFS_PATH_MAX); - if ((get_device_absolute_path(dev, devpath, SYSFS_PATH_MAX)) != 0) { - dprintf("Error finding absolute path to device %s\n", dev); - return -1; - } - strcat(devpath, "/"); - strcat(devpath, attrib); - attribute = sysfs_open_attribute(devpath); - if (attribute == NULL) { - dprintf("Attribute %s could not be retrieved for device %s\n", - attrib, dev); - return -1; + return NULL; } - if (attribute->method & SYSFS_METHOD_SHOW) { - if ((sysfs_read_attribute(attribute)) != 0) { - dprintf("Error reading attribute %s for device %s\n", - attrib, dev); - sysfs_close_attribute(attribute); - return -1; - } + memset(sysfs_path, 0, SYSFS_PATH_MAX); + if ((get_device_absolute_path(bus_id, bus, sysfs_path, + SYSFS_PATH_MAX)) != 0) { + dprintf("Error getting to device %s\n", bus_id); + return NULL; } - if ((sysfs_write_attribute(attribute, value, len)) < 0) { - dprintf("Error setting %s to %s\n", attrib, value); - sysfs_close_attribute(attribute); - return -1; + + device = sysfs_open_device(sysfs_path); + if (device == NULL) { + dprintf("Error opening device %s\n", bus_id); + return NULL; } - sysfs_close_attribute(attribute); - return 0; + + return device; } -/** - * sysfs_read_device_attr: read an attribute of the given device - * @dev: device bus_id for which attribute has to be changed - * @attrib: attribute to read - * @value: buffer to return value in - * @len: size of buffer available - * Returns 0 on success -1 on error - */ -int sysfs_read_device_attr(unsigned char *dev, unsigned char *attrib, - unsigned char *value, size_t len) +/* + * sysfs_open_device_attr: open the given device's attribute + * @bus: Bus on which to look + * @dev_id: device for which attribute is required + * @attrname: name of the attribute to look for + * Returns struct sysfs_attribute on success and NULL on failure + * + * NOTE: + * 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 *attribute = NULL; unsigned char devpath[SYSFS_PATH_MAX]; - - if (dev == NULL || attrib == NULL || value == NULL) { + + if (bus == NULL || bus_id == NULL || attrib == NULL) { errno = EINVAL; - return -1; + return NULL; } - + memset(devpath, 0, SYSFS_PATH_MAX); - if ((get_device_absolute_path(dev, devpath, SYSFS_PATH_MAX)) != 0) { - dprintf("Error finding absolute path to device %s\n", dev); - return -1; + if ((get_device_absolute_path(bus_id, bus, devpath, + SYSFS_PATH_MAX)) != 0) { + dprintf("Error getting to device %s\n", bus_id); + return NULL; } strcat(devpath, "/"); strcat(devpath, attrib); attribute = sysfs_open_attribute(devpath); if (attribute == NULL) { dprintf("Error opening attribute %s for device %s\n", - attrib, dev); - return -1; - } - if (!(attribute->method & SYSFS_METHOD_SHOW)) { - dprintf("Show method not supported for attribute %s\n", - attrib); - sysfs_close_attribute(attribute); - return -1; + attrib, bus_id); + return NULL; } if ((sysfs_read_attribute(attribute)) != 0) { dprintf("Error reading attribute %s for device %s\n", - attrib, dev); + attrib, bus_id); sysfs_close_attribute(attribute); - return -1; - } - if (attribute->len > len) { - dprintf("Value length %d is larger than supplied buffer %d\n", - attribute->len, len); - sysfs_close_attribute(attribute); - return -1; + return NULL; } - strncpy(value, attribute->value, attribute->len); - value[(attribute->len)+1] = 0; - sysfs_close_attribute(attribute); - return 0; + return attribute; } + diff --git a/libsysfs/sysfs_driver.c b/libsysfs/sysfs_driver.c index f8e842c65..0011177e9 100644 --- a/libsysfs/sysfs_driver.c +++ b/libsysfs/sysfs_driver.c @@ -162,6 +162,35 @@ struct dlist *sysfs_get_driver_links(struct sysfs_driver *driver) return(driver->directory->links); } +/** + * get_driver_path: looks up the bus the driver is on and builds path to + * the driver. + * @bus: bus on which to search + * @drv: driver to look for + * @path: buffer to return path to driver + * @psize: size of "path" + * Returns 0 on success and -1 on error + */ +static int get_driver_path(const unsigned char *bus, const unsigned char *drv, + unsigned char *path, size_t psize) +{ + if (bus == NULL || drv == NULL || path == NULL) { + errno = EINVAL; + return -1; + } + if (sysfs_get_mnt_path(path, psize) != 0) { + dprintf("Error getting sysfs mount path\n"); + return -1; + } + strcat(path, SYSFS_BUS_DIR); + strcat(path, "/"); + strcat(path, bus); + strcat(path, SYSFS_DRIVERS_DIR); + strcat(path, "/"); + strcat(path, drv); + return 0; +} + /** * sysfs_open_driver_by_name: open a driver by name and return the bus * the driver is on. @@ -188,16 +217,10 @@ struct sysfs_driver *sysfs_open_driver_by_name(const unsigned char *drv_name, } memset(path, 0, SYSFS_PATH_MAX); - if (sysfs_get_mnt_path(path, SYSFS_PATH_MAX) != 0) { - dprintf("Error getting sysfs mount path\n"); + if (get_driver_path(bus, drv_name, path, SYSFS_PATH_MAX) != 0) { + dprintf("Error getting to driver %s\n", drv_name); return NULL; } - strcat(path, SYSFS_BUS_DIR); - strcat(path, "/"); - strcat(path, bus); - strcat(path, SYSFS_DRIVERS_DIR); - strcat(path, "/"); - strcat(path, drv_name); driver = sysfs_open_driver(path); if (driver == NULL) { dprintf("Could not open driver %s\n", drv_name); @@ -224,114 +247,33 @@ struct sysfs_driver *sysfs_open_driver_by_name(const unsigned char *drv_name, return driver; } -/** - * get_driver_path: looks up the bus the driver is on and builds path to - * the driver. - * @drv: driver to look for - * @path: buffer to return path to driver - * @psize: size of "path" - * Returns 0 on success and -1 on error - */ -static int get_driver_path(const unsigned char *drv, - unsigned char *path, size_t psize) -{ - unsigned char bus_name[SYSFS_NAME_LEN]; - - if (drv == NULL || path == NULL) { - errno = EINVAL; - return -1; - } - memset(bus_name, 0, SYSFS_NAME_LEN); - memset(path, 0, SYSFS_PATH_MAX); - if ((sysfs_find_driver_bus(drv, bus_name, SYSFS_NAME_LEN)) < 0) { - dprintf("Driver %s not found\n", drv); - return -1; - } - if (sysfs_get_mnt_path(path, SYSFS_PATH_MAX) != 0) { - dprintf("Error getting sysfs mount path\n"); - return -1; - } - strcat(path, SYSFS_BUS_DIR); - strcat(path, "/"); - strcat(path, bus_name); - strcat(path, SYSFS_DRIVERS_DIR); - strcat(path, "/"); - strcat(path, drv); - fprintf(stdout, "get_driver_path %s\n", path); - return 0; -} - -/** - * sysfs_write_driver_attr: modify "writable" driver attribute - * @drv: driver whose attribute has to be modified - * @attrib: Attribute to be modified - * @value: Value to change to - * Returns 0 on success -1 on failure - */ -int sysfs_write_driver_attr(unsigned char *drv, unsigned char *attrib, - unsigned char *value, size_t len) -{ - struct sysfs_attribute *attribute = NULL; - unsigned char path[SYSFS_PATH_MAX]; - - if (drv == NULL || attrib == NULL || value == NULL) { - errno = EINVAL; - return -1; - } - - memset(path, 0, SYSFS_PATH_MAX); - if ((get_driver_path(drv, path, SYSFS_PATH_MAX)) != 0) { - dprintf("Error getting to driver %s\n", drv); - return -1; - } - strcat(path, "/"); - strcat(path, attrib); - attribute = sysfs_open_attribute(path); - if (attribute == NULL) { - dprintf("Attribute %s could not be retrieved for driver %s\n", - attrib, drv); - return -1; - } - if (attribute->method & SYSFS_METHOD_SHOW) { - if ((sysfs_read_attribute(attribute)) != 0) { - dprintf("Error reading attribute %s for driver %s\n", - attrib, drv); - sysfs_close_attribute(attribute); - return -1; - } - } - if ((sysfs_write_attribute(attribute, value, len)) < 0) { - dprintf("Error setting %s to %s\n", attrib, value); - sysfs_close_attribute(attribute); - return -1; - } - sysfs_close_attribute(attribute); - return 0; -} /** - * sysfs_read_driver_attr: read the user supplied driver attribute + * sysfs_open_driver_attr: read the user supplied driver attribute + * @bus: bus on which to look * @drv: driver whose attribute has to be read * @attrib: Attribute to be read - * @value: Buffer to return the read value - * @len: Length of the buffer "value" - * Returns 0 on success -1 on failure + * Returns struct sysfs_attribute on success and NULL on failure + * + * NOTE: + * A call to sysfs_close_attribute() is required to close the + * attribute returned and to free memory */ -int sysfs_read_driver_attr(unsigned char *drv, unsigned char *attrib, - unsigned char *value, size_t len) +struct sysfs_attribute *sysfs_open_driver_attr(const unsigned char *bus, + const unsigned char *drv, const unsigned char *attrib) { struct sysfs_attribute *attribute = NULL; unsigned char path[SYSFS_PATH_MAX]; - if (drv == NULL || attrib == NULL || value == NULL) { + if (bus == NULL || drv == NULL || attrib == NULL) { errno = EINVAL; - return -1; + return NULL; } memset(path, 0, SYSFS_NAME_LEN); - if ((get_driver_path(drv, path, SYSFS_PATH_MAX)) != 0) { + if ((get_driver_path(bus, drv, path, SYSFS_PATH_MAX)) != 0) { dprintf("Error getting to driver %s\n", drv); - return -1; + return NULL; } strcat(path, "/"); strcat(path, attrib); @@ -339,29 +281,14 @@ int sysfs_read_driver_attr(unsigned char *drv, unsigned char *attrib, if (attribute == NULL) { dprintf("Error opening attribute %s for driver %s\n", attrib, drv); - return -1; - } - if (!(attribute->method & SYSFS_METHOD_SHOW)) { - dprintf("Show method not supported for attribute %s\n", - attrib); - sysfs_close_attribute(attribute); - return -1; + return NULL; } if ((sysfs_read_attribute(attribute)) != 0) { dprintf("Error reading attribute %s for driver %s\n", attrib, drv); sysfs_close_attribute(attribute); - return -1; - } - if (attribute->len > len) { - dprintf("Value length %d is larger than supplied buffer %d\n", - attribute->len, len); - sysfs_close_attribute(attribute); - return -1; + return NULL; } - strncpy(value, attribute->value, attribute->len); - value[(attribute->len)+1] = 0; - sysfs_close_attribute(attribute); - return 0; + return attribute; } diff --git a/libsysfs/sysfs_utils.c b/libsysfs/sysfs_utils.c index 3e50bc612..4e96051c7 100644 --- a/libsysfs/sysfs_utils.c +++ b/libsysfs/sysfs_utils.c @@ -92,7 +92,7 @@ int sysfs_get_mnt_path(unsigned char *mnt_path, size_t len) return -1; } sysfs_path = getenv(SYSFS_PATH_ENV); - if (sysfs_path != NULL) + if (sysfs_path != NULL) strncpy(mnt_path, sysfs_path, len); else ret = sysfs_get_fs_mnt_path(SYSFS_FSTYPE_NAME, mnt_path, len); @@ -109,13 +109,19 @@ int sysfs_get_mnt_path(unsigned char *mnt_path, size_t len) int sysfs_get_name_from_path(const unsigned char *path, unsigned char *name, size_t len) { + unsigned char tmp[SYSFS_PATH_MAX]; unsigned char *n = NULL; if (path == NULL || name == NULL) { errno = EINVAL; return -1; } - n = strrchr(path, '/'); + memset(tmp, 0, SYSFS_PATH_MAX); + strcpy(tmp, path); + n = &tmp[strlen(tmp)-1]; + if (strncmp(n, "/", 1) == 0) + *n = '\0'; + n = strrchr(tmp, '/'); if (n == NULL) { errno = EINVAL; return -1; @@ -169,10 +175,7 @@ int sysfs_get_link(const unsigned char *path, unsigned char *target, size_t len) if (*s == '/') count++; } - - if (s == NULL) - return -1; - + strncpy(s, d, (SYSFS_PATH_MAX-strlen(devdir))); strncpy(target, devdir, len); @@ -184,7 +187,7 @@ int sysfs_get_link(const unsigned char *path, unsigned char *target, size_t len) * sysfs_del_name: free function for sysfs_open_subsystem_list * @name: memory area to be freed */ -void sysfs_del_name(void *name) +static void sysfs_del_name(void *name) { free(name); } @@ -210,8 +213,10 @@ void sysfs_close_list(struct dlist *list) struct dlist *sysfs_open_subsystem_list(unsigned char *name) { unsigned char sysfs_path[SYSFS_PATH_MAX], *subsys_name = NULL; + unsigned char *c = NULL; struct sysfs_directory *dir = NULL, *cur = NULL; struct dlist *list = NULL; + struct stat astats; if (name == NULL) return NULL; @@ -251,6 +256,27 @@ struct dlist *sysfs_open_subsystem_list(unsigned char *name) } } sysfs_close_directory(dir); + /* + * We are now considering "block" as a "class". Hence, if the subsys + * name requested here is "class", verify if "block" is supported on + * this system and return the same. + */ + if (strcmp(name, SYSFS_CLASS_DIR) == 0) { + c = strstr(sysfs_path, SYSFS_CLASS_NAME); + if (c == NULL) + goto out; + strcpy(c, SYSFS_BLOCK_NAME); + if ((lstat(sysfs_path, &astats)) != 0) { + dprintf("stat() failed\n"); + goto out; + } + if (S_ISDIR(astats.st_mode)) { + subsys_name = (char *)calloc(1, SYSFS_NAME_LEN); + strcpy(subsys_name, SYSFS_BLOCK_NAME); + dlist_unshift(list, subsys_name); + } + } +out: return list; } -- 2.30.2