X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=libsysfs%2Fsysfs_device.c;h=66d5f9aef7c71b65b9e9272b053eb4bac8a21525;hp=89704dcd8041092abf6b9f8e6c6362a1bf43ba84;hb=ac28b86d631f23b5df74dbeb33e76a2b3f5d88bb;hpb=fe3fe3b29ffbc7d0ce7dca6a371da31d8b3ff7f8 diff --git a/libsysfs/sysfs_device.c b/libsysfs/sysfs_device.c index 89704dcd8..66d5f9aef 100644 --- a/libsysfs/sysfs_device.c +++ b/libsysfs/sysfs_device.c @@ -23,6 +23,67 @@ #include "libsysfs.h" #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. + * @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], path[SYSFS_PATH_MAX]; + unsigned 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); + strcat(subsys, "/"); + strcpy(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); + c = strstr(path, "/devices"); + if (c == NULL) { + dprintf("Invalid path to device %s\n", path); + sysfs_close_list(buslist); + 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); + 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))) { + strcpy(dev->bus, bus); + sysfs_close_list(buslist); + return 0; + } + } + } + sysfs_close_list(buslist); + } + return -1; +} + /** * sysfs_close_device_tree: closes every device in the supplied tree, * closing children only. @@ -43,14 +104,6 @@ static void sysfs_close_device_tree(struct sysfs_device *devroot) } } -/** - * sysfs_del_device: routine for dlist integration - */ -static void sysfs_del_device(void *dev) -{ - sysfs_close_device((struct sysfs_device *)dev); -} - /** * sysfs_close_dev_tree: routine for dlist integration */ @@ -66,6 +119,8 @@ static void sysfs_close_dev_tree(void *dev) void sysfs_close_device(struct sysfs_device *dev) { if (dev != NULL) { + if (dev->parent != NULL) + sysfs_close_device(dev->parent); if (dev->directory != NULL) sysfs_close_directory(dev->directory); if (dev->children != NULL && dev->children->count == 0) @@ -84,72 +139,73 @@ static struct sysfs_device *alloc_device(void) } /** - * sysfs_get_device_attr: searches dev's attributes by name - * @dev: device to look through - * @name: attribute name to get - * returns sysfs_attribute reference with success or NULL with error. + * open_device_dir: opens up sysfs_directory for specific root dev + * @name: name of root + * returns struct sysfs_directory with success and NULL with error */ -struct sysfs_attribute *sysfs_get_device_attr(struct sysfs_device *dev, - const unsigned char *name) +static struct sysfs_directory *open_device_dir(const unsigned char *path) { - struct sysfs_attribute *cur = NULL; + struct sysfs_directory *rdir = NULL; - if (dev == NULL || dev->directory == NULL - || dev->directory->attributes == NULL || name == NULL) { + if (path == NULL) { errno = EINVAL; return NULL; } - - cur = sysfs_get_directory_attribute(dev->directory, - (unsigned char *)name); - if (cur != NULL) - return cur; - return NULL; + rdir = sysfs_open_directory(path); + if (rdir == NULL) { + errno = EINVAL; + dprintf ("Device %s not supported on this system\n", path); + return NULL; + } + if ((sysfs_read_dir_subdirs(rdir)) != 0) { + dprintf ("Error reading device at dir %s\n", path); + sysfs_close_directory(rdir); + return NULL; + } + + return rdir; } /** - * sysfs_open_device: opens and populates device structure + * sysfs_open_device_path: opens and populates device structure * @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(const unsigned char *path) +struct sysfs_device *sysfs_open_device_path(const unsigned char *path) { struct sysfs_device *dev = NULL; - struct sysfs_directory *sdir = NULL; if (path == NULL) { errno = EINVAL; return NULL; } + if ((sysfs_path_is_dir(path)) != 0) { + dprintf("Incorrect path to device: %s\n", path); + return NULL; + } dev = alloc_device(); if (dev == NULL) { dprintf("Error allocating device at %s\n", path); return NULL; } - sdir = sysfs_open_directory(path); - if (sdir == NULL) { - dprintf("Invalid device at %s\n", path); + if ((sysfs_get_name_from_path(path, dev->bus_id, + SYSFS_NAME_LEN)) != 0) { errno = EINVAL; + dprintf("Error getting device bus_id\n"); sysfs_close_device(dev); return NULL; } - if ((sysfs_read_directory(sdir)) != 0) { - dprintf("Error reading device directory at %s\n", path); - sysfs_close_directory(sdir); - sysfs_close_device(dev); - return NULL; - } - dev->directory = sdir; - strcpy(dev->bus_id, sdir->name); - strcpy(dev->path, sdir->path); - + strcpy(dev->path, path); /* * 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, sdir->name, SYSFS_NAME_LEN); + strncpy(dev->name, dev->bus_id, SYSFS_NAME_LEN); + + if (get_device_bus(dev) != 0) + strcpy(dev->bus, SYSFS_UNKNOWN); return dev; } @@ -170,11 +226,16 @@ static struct sysfs_device *sysfs_open_device_tree(const unsigned char *path) errno = EINVAL; return NULL; } - rootdev = sysfs_open_device(path); + rootdev = sysfs_open_device_path(path); if (rootdev == NULL) { dprintf("Error opening root device at %s\n", path); return NULL; } + if (rootdev->directory == NULL) { + rootdev->directory = open_device_dir(rootdev->path); + if (rootdev->directory == NULL) + return NULL; + } if (rootdev->directory->subdirs != NULL) { dlist_for_each_data(rootdev->directory->subdirs, cur, struct sysfs_directory) { @@ -188,7 +249,7 @@ static struct sysfs_device *sysfs_open_device_tree(const unsigned char *path) if (rootdev->children == NULL) rootdev->children = dlist_new_with_delete (sizeof(struct sysfs_device), - sysfs_del_device); + sysfs_close_dev_tree); dlist_unshift(rootdev->children, new); } } @@ -212,60 +273,25 @@ void sysfs_close_root_device(struct sysfs_root_device *root) } /** - * open_root_device_dir: opens up sysfs_directory for specific root dev - * @name: name of root - * returns struct sysfs_directory with success and NULL with error - */ -static struct sysfs_directory *open_root_device_dir(const unsigned char *name) -{ - struct sysfs_directory *rdir = NULL; - unsigned char rootpath[SYSFS_PATH_MAX]; - - if (name == NULL) { - errno = EINVAL; - return NULL; - } - - memset(rootpath, 0, SYSFS_PATH_MAX); - if (sysfs_get_mnt_path(rootpath, SYSFS_PATH_MAX) != 0) { - dprintf ("Sysfs not supported on this system\n"); - return NULL; - } - - strcat(rootpath, SYSFS_DEVICES_DIR); - strcat(rootpath, "/"); - strcat(rootpath, name); - rdir = sysfs_open_directory(rootpath); - if (rdir == NULL) { - errno = EINVAL; - dprintf ("Root device %s not supported on this system\n", - name); - return NULL; - } - if (sysfs_read_directory(rdir) != 0) { - dprintf ("Error reading %s root device at dir %s\n", name, - rootpath); - sysfs_close_directory(rdir); - return NULL; - } - - return rdir; -} - -/** - * get_all_root_devices: opens up all the devices under this root device + * sysfs_get_root_devices: opens up all the devices under this root device * @root: root device to open devices for - * returns 0 with success and -1 with error + * returns dlist of devices with success and NULL with error */ -static int get_all_root_devices(struct sysfs_root_device *root) +struct dlist *sysfs_get_root_devices(struct sysfs_root_device *root) { struct sysfs_device *dev = NULL; struct sysfs_directory *cur = NULL; - if (root == NULL || root->directory == NULL) { + if (root == NULL) { errno = EINVAL; - return -1; + return NULL; + } + if (root->directory == NULL) { + root->directory = open_device_dir(root->path); + if (root->directory == NULL) + return NULL; } + if (root->directory->subdirs == NULL) return 0; @@ -283,7 +309,7 @@ static int get_all_root_devices(struct sysfs_root_device *root) dlist_unshift(root->devices, dev); } - return 0; + return root->devices; } /** @@ -295,33 +321,37 @@ static int get_all_root_devices(struct sysfs_root_device *root) struct sysfs_root_device *sysfs_open_root_device(const unsigned char *name) { struct sysfs_root_device *root = NULL; - struct sysfs_directory *rootdir = NULL; + unsigned char rootpath[SYSFS_PATH_MAX]; if (name == NULL) { errno = EINVAL; return NULL; } - root = (struct sysfs_root_device *)calloc - (1, sizeof(struct sysfs_root_device)); - if (root == NULL) { - dprintf("calloc failure\n"); + memset(rootpath, 0, SYSFS_PATH_MAX); + if (sysfs_get_mnt_path(rootpath, SYSFS_PATH_MAX) != 0) { + dprintf ("Sysfs not supported on this system\n"); return NULL; } - rootdir = open_root_device_dir(name); - if (rootdir == NULL) { - dprintf ("Invalid root device, %s not supported\n", name); - sysfs_close_root_device(root); + + if (sysfs_trailing_slash(rootpath) == 0) + strcat(rootpath, "/"); + strcat(rootpath, SYSFS_DEVICES_NAME); + strcat(rootpath, "/"); + strcat(rootpath, name); + if ((sysfs_path_is_dir(rootpath)) != 0) { + errno = EINVAL; + dprintf("Invalid root device: %s\n", name); return NULL; } - strcpy(root->path, rootdir->path); - root->directory = rootdir; - if (get_all_root_devices(root) != 0) { - dprintf ("Error retrieving devices for root %s\n", name); - sysfs_close_root_device(root); + root = (struct sysfs_root_device *)calloc + (1, sizeof(struct sysfs_root_device)); + if (root == NULL) { + dprintf("calloc failure\n"); return NULL; } - + strcpy(root->name, name); + strcpy(root->path, rootpath); return root; } @@ -332,59 +362,56 @@ 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 || device->directory == NULL) + if (device == NULL) return NULL; + if (device->directory == NULL) { + device->directory = sysfs_open_directory(device->path); + if (device->directory == NULL) + return NULL; + } + 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_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 + * sysfs_get_device_attr: searches dev's attributes by name + * @dev: device to look through + * @name: attribute name to get + * returns sysfs_attribute reference with success or NULL with error. */ -struct sysfs_device *sysfs_open_device_by_id(const unsigned char *bus_id, - const unsigned char *bus, size_t bsize) +struct sysfs_attribute *sysfs_get_device_attr(struct sysfs_device *dev, + const unsigned char *name) { - char sysfs_path[SYSFS_PATH_MAX], device_path[SYSFS_PATH_MAX]; - struct sysfs_device *device = NULL; + struct sysfs_attribute *cur = NULL; + struct dlist *attrlist = NULL; - if (bus_id == NULL || bus == NULL) { + if (dev == NULL || name == 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"); + attrlist = sysfs_get_device_attributes(dev); + if (attrlist == NULL) return NULL; - } - - device = sysfs_open_device(device_path); - if (device == NULL) { - dprintf("Error opening device %s\n", bus_id); - return NULL; - } - return device; + cur = sysfs_get_directory_attribute(dev->directory, + (unsigned char *)name); + + return cur; } /** @@ -395,30 +422,28 @@ 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_PATH_MAX]; 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; - } + memset(bus_path, 0, SYSFS_PATH_MAX); 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); + if (sysfs_trailing_slash(bus_path) == 0) + strcat(bus_path, "/"); + strcat(bus_path, SYSFS_BUS_NAME); strcat(bus_path, "/"); - strcat(bus_path, bus_name); - strcat(bus_path, SYSFS_DEVICES_DIR); + strcat(bus_path, bus); + strcat(bus_path, "/"); + strcat(bus_path, SYSFS_DEVICES_NAME); strcat(bus_path, "/"); strcat(bus_path, device); /* @@ -433,106 +458,138 @@ 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: 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 + * 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_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; + return NULL; + } + 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; } - 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; + device = sysfs_open_device_path(sysfs_path); + if (device == NULL) { + dprintf("Error opening device %s\n", bus_id); + return NULL; } - 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 device; +} + +/** + * sysfs_get_device_parent: opens up given device's parent and returns a + * reference to its sysfs_device + * @dev: sysfs_device whose parent is requested + * Returns sysfs_device of the parent on success and NULL on failure + */ +struct sysfs_device *sysfs_get_device_parent(struct sysfs_device *dev) +{ + unsigned char ppath[SYSFS_PATH_MAX], *tmp = NULL; + + if (dev == NULL) { + errno = EINVAL; + return NULL; + } + + if (dev->parent != NULL) + return (dev->parent); + + memset(ppath, 0, SYSFS_PATH_MAX); + strcpy(ppath, dev->path); + tmp = strrchr(ppath, '/'); + if (tmp == NULL) { + dprintf("Invalid path to device %s\n", ppath); + 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; + if (*(tmp +1) == '\0') { + *tmp = '\0'; + tmp = strrchr(tmp, '/'); + if (tmp == NULL) { + dprintf("Invalid path to device %s\n", ppath); + 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; + *tmp = '\0'; + + /* + * All "devices" have the "detach_state" attribute - validate here + */ + strcat(ppath, "/detach_state"); + if ((sysfs_path_is_file(ppath)) != 0) { + dprintf("Device at %s does not have a parent\n", dev->path); + return NULL; } - sysfs_close_attribute(attribute); - return 0; + tmp = strrchr(ppath, '/'); + *tmp = '\0'; + dev->parent = sysfs_open_device_path(ppath); + if (dev->parent == NULL) { + dprintf("Error opening device %s's parent at %s\n", + dev->bus_id, ppath); + return NULL; + } + return (dev->parent); } -/** - * 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; } +