X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=libsysfs%2Fsysfs_class.c;h=102f09f17af260fe92f66a9d412627e432c50496;hp=59ef0be48e193024ae313da644189c47027fad24;hb=7a551dc35580988d13ff8e6e3e37398ac779181a;hpb=656703759d7d3eac6e8c86f1121cde7dfd6d8cbd diff --git a/libsysfs/sysfs_class.c b/libsysfs/sysfs_class.c index 59ef0be48..102f09f17 100644 --- a/libsysfs/sysfs_class.c +++ b/libsysfs/sysfs_class.c @@ -3,7 +3,7 @@ * * Generic class utility functions for libsysfs * - * Copyright (C) IBM Corp. 2003 + * Copyright (C) IBM Corp. 2003-2005 * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -20,91 +20,83 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */ -#include "sysfs/libsysfs.h" +#include "libsysfs.h" #include "sysfs.h" -static void sysfs_close_cls_dev(void *dev) -{ - sysfs_close_class_device((struct sysfs_class_device *)dev); -} - -/** - * class_name_equal: compares class_devices' name - * @a: class_name looking for - * @b: sysfs_class_device being compared - */ -static int class_name_equal(void *a, void *b) -{ - if (a == NULL || b == NULL) - return 0; - - if (strcmp(((char *)a), ((struct sysfs_class_device *)b)->name) == 0) - return 1; - - return 0; -} - /** * sysfs_close_class_device: closes a single class device. * @dev: class device to close. */ void sysfs_close_class_device(struct sysfs_class_device *dev) { - if (dev != NULL) { - if (dev->directory != NULL) - sysfs_close_directory(dev->directory); - if (dev->sysdevice != NULL) - sysfs_close_device(dev->sysdevice); - if (dev->driver != NULL) - sysfs_close_driver(dev->driver); - if (dev->parent != NULL) + if (dev) { + if (dev->parent) sysfs_close_class_device(dev->parent); + if (dev->sysdevice) + sysfs_close_device(dev->sysdevice); + if (dev->attrlist) + dlist_destroy(dev->attrlist); free(dev); } } +static void sysfs_close_cls_dev(void *dev) +{ + sysfs_close_class_device((struct sysfs_class_device *)dev); +} + /** - * sysfs_close_class: close single class - * @cls: class structure - */ + * sysfs_close_class: close the given class + * @cls: sysfs_class to close + */ void sysfs_close_class(struct sysfs_class *cls) { - if (cls != NULL) { - if (cls->directory != NULL) - sysfs_close_directory(cls->directory); - if (cls->devices != NULL) + if (cls) { + if (cls->devices) dlist_destroy(cls->devices); + if (cls->attrlist) + dlist_destroy(cls->attrlist); free(cls); } } +static int cdev_name_equal(void *a, void *b) +{ + if (!a || !b) + return 0; + + if (strncmp((char *)a, ((struct sysfs_class_device *)b)->name, + strlen((char *)a)) == 0) + return 1; + + return 0; +} + +static struct sysfs_class *alloc_class(void) +{ + return (struct sysfs_class *) calloc(1, sizeof(struct sysfs_class)); +} + /** * alloc_class_device: mallocs and initializes new class device struct. * returns sysfs_class_device or NULL. */ static struct sysfs_class_device *alloc_class_device(void) { - return (struct sysfs_class_device *) - calloc(1, sizeof(struct sysfs_class_device)); -} + struct sysfs_class_device *dev; -/** - * alloc_class: mallocs new class structure - * returns sysfs_class struct or NULL - */ -static struct sysfs_class *alloc_class(void) -{ - return (struct sysfs_class *)calloc(1, sizeof(struct sysfs_class)); + dev = calloc(1, sizeof(struct sysfs_class_device)); + return dev; } -/** +/** * set_classdev_classname: Grabs classname from path * @cdev: class device to set * Returns nothing */ static void set_classdev_classname(struct sysfs_class_device *cdev) { - char *c = NULL, *e = NULL; + char *c, *e; int count = 0; c = strstr(cdev->path, SYSFS_CLASS_NAME); @@ -135,30 +127,47 @@ static void set_classdev_classname(struct sysfs_class_device *cdev) */ struct sysfs_class_device *sysfs_open_class_device_path(const char *path) { - struct sysfs_class_device *cdev = NULL; + struct sysfs_class_device *cdev; + char temp_path[SYSFS_PATH_MAX]; - if (path == NULL) { + if (!path) { errno = EINVAL; return NULL; } - if ((sysfs_path_is_dir(path)) != 0) { - dprintf("%s is not a valid path to a class device\n", path); - return NULL; - } + + /* + * Post linux-2.6.14 driver model supports nested classes with + * links to the nested hierarchy at /sys/class/xxx/. Check for + * a link to the actual class device if a directory isn't found + */ + if (sysfs_path_is_dir(path)) { + dprintf("%s: Directory not found, checking for a link\n", path); + if (!sysfs_path_is_link(path)) { + if (sysfs_get_link(path, temp_path, SYSFS_PATH_MAX)) { + dprintf("Error retrieving link at %s\n", path); + return NULL; + } + } else { + dprintf("%s is not a valid class device path\n", path); + return NULL; + } + } else + safestrcpy(temp_path, path); + cdev = alloc_class_device(); - if (cdev == NULL) { + if (!cdev) { dprintf("calloc failed\n"); return NULL; } - if ((sysfs_get_name_from_path(path, cdev->name, SYSFS_NAME_LEN)) != 0) { + if (sysfs_get_name_from_path(temp_path, cdev->name, SYSFS_NAME_LEN)) { errno = EINVAL; dprintf("Error getting class device name\n"); sysfs_close_class_device(cdev); return NULL; } - safestrcpy(cdev->path, path); - if ((sysfs_remove_trailing_slash(cdev->path)) != 0) { + safestrcpy(cdev->path, temp_path); + if (sysfs_remove_trailing_slash(cdev->path)) { dprintf("Invalid path to class device %s\n", cdev->path); sysfs_close_class_device(cdev); return NULL; @@ -168,245 +177,6 @@ struct sysfs_class_device *sysfs_open_class_device_path(const char *path) return cdev; } -/** - * sysfs_get_class_devices: gets all devices for class - * @cls: class to get devices for - * returns dlist of class_devices with success and NULL with error - */ -struct dlist *sysfs_get_class_devices(struct sysfs_class *cls) -{ - struct sysfs_class_device *dev = NULL; - struct sysfs_directory *cur = NULL; - - if (cls == NULL) { - errno = EINVAL; - return NULL; - } - - if (cls->devices != NULL) - return cls->devices; - - if (cls->directory == NULL) { - cls->directory = sysfs_open_directory(cls->path); - if (cls->directory == NULL) - return NULL; - } - - if ((sysfs_read_dir_subdirs(cls->directory)) != 0) - return NULL; - - if (cls->directory->subdirs != NULL) { - dlist_for_each_data(cls->directory->subdirs, cur, - struct sysfs_directory) { - dev = sysfs_open_class_device_path(cur->path); - if (dev == NULL) { - dprintf("Error opening device at %s\n", - cur->path); - continue; - } - if (cls->devices == NULL) - cls->devices = dlist_new_with_delete - (sizeof(struct sysfs_class_device), - sysfs_close_cls_dev); - dlist_unshift_sorted(cls->devices, dev, sort_list); - } - } - return cls->devices; -} - -/** - * sysfs_open_class: opens specific class and all its devices on system - * returns sysfs_class structure with success or NULL with error. - */ -struct sysfs_class *sysfs_open_class(const char *name) -{ - struct sysfs_class *cls = NULL; - char classpath[SYSFS_PATH_MAX]; - - if (name == NULL) { - errno = EINVAL; - return NULL; - } - - memset(classpath, 0, SYSFS_PATH_MAX); - if ((sysfs_get_mnt_path(classpath, SYSFS_PATH_MAX)) != 0) { - dprintf("Sysfs not supported on this system\n"); - return NULL; - } - - /* - * 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) { - safestrcat(classpath, "/"); - safestrcat(classpath, SYSFS_BLOCK_NAME); - } else { - safestrcat(classpath, "/"); - safestrcat(classpath, SYSFS_CLASS_NAME); - safestrcat(classpath, "/"); - safestrcat(classpath, name); - } - if ((sysfs_path_is_dir(classpath)) != 0) { - dprintf("Class %s not found on the system\n", name); - return NULL; - } - - cls = alloc_class(); - if (cls == NULL) { - dprintf("calloc failed\n"); - return NULL; - } - safestrcpy(cls->name, name); - safestrcpy(cls->path, classpath); - if ((sysfs_remove_trailing_slash(cls->path)) != 0) { - dprintf("Invalid path to class device %s\n", cls->path); - sysfs_close_class(cls); - return NULL; - } - - return cls; -} - -/** - * sysfs_get_class_device: Get specific class device using the device's id - * @class: class to find device on - * @name: class name of the device - */ -struct sysfs_class_device *sysfs_get_class_device(struct sysfs_class *cls, - char *name) -{ - if (cls == NULL || name == NULL) { - errno = EINVAL; - return NULL; - } - - if (cls->devices == NULL) { - cls->devices = sysfs_get_class_devices(cls); - if (cls->devices == NULL) - return NULL; - } - return (struct sysfs_class_device *)dlist_find_custom(cls->devices, - name, class_name_equal); -} - -/** - * sysfs_get_classdev_device: returns the sysfs_device corresponding to - * sysfs_class_device, if present - * @clsdev: class device whose sysfs_device is required - * Returns sysfs_device on success, NULL on error or if device is not - * implemented - */ -struct sysfs_device *sysfs_get_classdev_device - (struct sysfs_class_device *clsdev) -{ - struct sysfs_link *devlink = NULL; - char devpath[SYSFS_PATH_MAX]; - - if (clsdev == NULL) { - errno = EINVAL; - return NULL; - } - safestrcpy(devpath, clsdev->path); - safestrcat(devpath, "/device"); - if ((sysfs_path_is_link(devpath)) != 0) { - if (clsdev->sysdevice != NULL) { - sysfs_close_device(clsdev->sysdevice); - clsdev->sysdevice = NULL; - } - return NULL; - } - - if (clsdev->directory == NULL) { - clsdev->directory = sysfs_open_directory(clsdev->path); - if (clsdev->directory == NULL) - return NULL; - } - devlink = sysfs_get_directory_link(clsdev->directory, "device"); - if (devlink == NULL) { - if (clsdev->sysdevice != NULL) { - dprintf("Device link no longer exists\n"); - sysfs_close_device(clsdev->sysdevice); - clsdev->sysdevice = NULL; - } - return NULL; - } - - if (clsdev->sysdevice != NULL) { - if (!strncmp(devlink->target, clsdev->sysdevice->path, - SYSFS_PATH_MAX)) - /* sysdevice hasn't changed */ - return (clsdev->sysdevice); - else - /* come here only if the device link for has changed */ - sysfs_close_device(clsdev->sysdevice); - } - - clsdev->sysdevice = sysfs_open_device_path(devlink->target); - if (clsdev->sysdevice == NULL) - return NULL; - - return (clsdev->sysdevice); -} - -/** - * sysfs_get_classdev_driver: returns the sysfs_driver corresponding to - * sysfs_class_device, if present - * @clsdev: class device whose sysfs_device is required - * Returns sysfs_driver on success, NULL on error or if driver is not - * implemented - */ -struct sysfs_driver *sysfs_get_classdev_driver - (struct sysfs_class_device *clsdev) -{ - struct sysfs_link *drvlink = NULL; - char drvpath[SYSFS_PATH_MAX]; - - if (clsdev == NULL) { - errno = EINVAL; - return NULL; - } - safestrcpy(drvpath, clsdev->path); - safestrcat(drvpath, "/driver"); - if ((sysfs_path_is_link(drvpath)) != 0) { - if (clsdev->driver != NULL) { - sysfs_close_driver(clsdev->driver); - clsdev->driver = NULL; - } - return NULL; - } - - if (clsdev->directory == NULL) { - clsdev->directory = sysfs_open_directory(clsdev->path); - if (clsdev->directory == NULL) - return NULL; - } - drvlink = sysfs_get_directory_link(clsdev->directory, "driver"); - if (drvlink == NULL) { - if (clsdev->driver != NULL) { - dprintf("Driver link no longer exists\n"); - sysfs_close_driver(clsdev->driver); - clsdev->driver = NULL; - } - return NULL; - } - if (clsdev->driver != NULL) { - if (!strncmp(drvlink->target, clsdev->driver->path, - SYSFS_PATH_MAX)) - /* driver hasn't changed */ - return (clsdev->driver); - else - /* come here only if the device link for has changed */ - sysfs_close_driver(clsdev->driver); - } - - clsdev->driver = sysfs_open_driver_path(drvlink->target); - if (clsdev->driver == NULL) - return NULL; - - return (clsdev->driver); -} - /** * get_blockdev_parent: Get the parent class device for a "block" subsystem * device if present @@ -415,7 +185,8 @@ struct sysfs_driver *sysfs_get_classdev_driver */ static int get_blockdev_parent(struct sysfs_class_device *clsdev) { - char parent_path[SYSFS_PATH_MAX], *c = NULL; + char parent_path[SYSFS_PATH_MAX]; + char *c; safestrcpy(parent_path, clsdev->path); c = strstr(parent_path, SYSFS_BLOCK_NAME); @@ -430,20 +201,20 @@ static int get_blockdev_parent(struct sysfs_class_device *clsdev) else goto errout; - /* validate whether the given class device is a partition or not */ - if ((strncmp(c, clsdev->name, strlen(clsdev->name))) == 0) { - dprintf("%s not a partition\n", clsdev->name); - return 1; - } - - c = strchr(c, '/'); + /* validate whether the given class device is a partition or not */ + if ((strncmp(c, clsdev->name, strlen(clsdev->name))) == 0) { + dprintf("%s not a partition\n", clsdev->name); + return 1; + } + + c = strchr(c, '/'); if (c == NULL) goto errout; *c = '\0'; - + clsdev->parent = sysfs_open_class_device_path(parent_path); - if (clsdev->parent == NULL) { + if (!clsdev->parent) { dprintf("Error opening the parent class device at %s\n", parent_path); return 1; @@ -466,19 +237,19 @@ errout: struct sysfs_class_device *sysfs_get_classdev_parent (struct sysfs_class_device *clsdev) { - if (clsdev == NULL) { + if (!clsdev) { errno = EINVAL; return NULL; } - if (clsdev->parent != NULL) + if (clsdev->parent) return (clsdev->parent); - - /* + + /* * As of now, only block devices have a parent child heirarchy in sysfs * We do not know, if, in the future, more classes will have a similar * structure. Hence, we now call a specialized function for block and * later we can add support functions for other subsystems as required. - */ + */ if (!(strncmp(clsdev->classname, SYSFS_BLOCK_NAME, sizeof(SYSFS_BLOCK_NAME)))) { if ((get_blockdev_parent(clsdev)) == 0) @@ -499,13 +270,13 @@ struct sysfs_class_device *sysfs_get_classdev_parent static int get_classdev_path(const char *classname, const char *clsdev, char *path, size_t len) { - if (classname == NULL || clsdev == NULL || path == NULL) { + if (!classname || !clsdev || !path) { errno = EINVAL; return -1; } - if (sysfs_get_mnt_path(path, len) != 0) { - dprintf("Error getting sysfs mount path\n"); - return -1; + if (sysfs_get_mnt_path(path, len) != 0) { + dprintf("Error getting sysfs mount path\n"); + return -1; } if (strncmp(classname, SYSFS_BLOCK_NAME, sizeof(SYSFS_BLOCK_NAME)) == 0) { @@ -535,9 +306,9 @@ struct sysfs_class_device *sysfs_open_class_device (const char *classname, const char *name) { char devpath[SYSFS_PATH_MAX]; - struct sysfs_class_device *cdev = NULL; + struct sysfs_class_device *cdev; - if (classname == NULL || name == NULL) { + if (!classname || !name) { errno = EINVAL; return NULL; } @@ -549,9 +320,9 @@ struct sysfs_class_device *sysfs_open_class_device name, classname); return NULL; } - + cdev = sysfs_open_class_device_path(devpath); - if (cdev == NULL) { + if (!cdev) { dprintf("Error getting class device %s from class %s\n", name, classname); return NULL; @@ -560,146 +331,227 @@ struct sysfs_class_device *sysfs_open_class_device } /** - * sysfs_get_classdev_attributes: returns a dlist of attributes for - * the requested class_device - * @cdev: sysfs_class_dev for which attributes are needed - * returns a dlist of attributes if exists, NULL otherwise + * sysfs_get_classdev_attr: searches class device's attributes by name + * @clsdev: class device to look through + * @name: attribute name to get + * returns sysfs_attribute reference with success or NULL with error */ -struct dlist *sysfs_get_classdev_attributes(struct sysfs_class_device *cdev) +struct sysfs_attribute *sysfs_get_classdev_attr + (struct sysfs_class_device *clsdev, const char *name) { - if (cdev == NULL) + if (!clsdev || !name) { + errno = EINVAL; return NULL; - - if (cdev->directory == NULL) { - cdev->directory = sysfs_open_directory(cdev->path); - if (cdev->directory == NULL) - return NULL; } - if (cdev->directory->attributes == NULL) { - if ((sysfs_read_dir_attributes(cdev->directory)) != 0) - return NULL; - } - return (cdev->directory->attributes); + return get_attribute(clsdev, (char *)name); } /** - * sysfs_refresh_clsassdev_attributes: refreshes the driver's list of attributes - * @clsdev: sysfs_class_device whose attributes to refresh - * - * NOTE: Upon return, prior references to sysfs_attributes for this classdev - * _may_ not be valid - * - * Returns list of attributes on success and NULL on failure + * sysfs_get_classdev_attributes: gets list of classdev attributes + * @clsdev: class device whose attributes list is needed + * returns dlist of attributes on success or NULL on error */ -struct dlist *sysfs_refresh_classdev_attributes - (struct sysfs_class_device *clsdev) +struct dlist *sysfs_get_classdev_attributes(struct sysfs_class_device *clsdev) { - if (clsdev == NULL) { + if (!clsdev) { errno = EINVAL; return NULL; } + return get_attributes_list(clsdev); +} - if (clsdev->directory == NULL) - return (sysfs_get_classdev_attributes(clsdev)); +/** + * sysfs_get_classdev_device: gets the sysfs_device associated with the + * given sysfs_class_device + * @clsdev: class device whose associated sysfs_device is needed + * returns struct sysfs_device * on success or NULL on error + */ +struct sysfs_device *sysfs_get_classdev_device + (struct sysfs_class_device *clsdev) +{ + char linkpath[SYSFS_PATH_MAX], devpath[SYSFS_PATH_MAX]; - if ((sysfs_refresh_dir_attributes(clsdev->directory)) != 0) { - dprintf("Error refreshing class_device attributes\n"); + if (!clsdev) { + errno = EINVAL; return NULL; } - return (clsdev->directory->attributes); + if (clsdev->sysdevice) + return clsdev->sysdevice; + + memset(linkpath, 0, SYSFS_PATH_MAX); + safestrcpy(linkpath, clsdev->path); + safestrcat(linkpath, "/device"); + if (!sysfs_path_is_link(linkpath)) { + memset(devpath, 0, SYSFS_PATH_MAX); + if (!sysfs_get_link(linkpath, devpath, SYSFS_PATH_MAX)) + clsdev->sysdevice = sysfs_open_device_path(devpath); + } + return clsdev->sysdevice; } /** - * sysfs_get_classdev_attr: searches class device's attributes by name - * @clsdev: class device to look through - * @name: attribute name to get - * returns sysfs_attribute reference with success or NULL with error + * sysfs_open_class: opens specific class and all its devices on system + * returns sysfs_class structure with success or NULL with error. */ -struct sysfs_attribute *sysfs_get_classdev_attr - (struct sysfs_class_device *clsdev, const char *name) +struct sysfs_class *sysfs_open_class(const char *name) { - struct sysfs_attribute *cur = NULL; - struct sysfs_directory *sdir = NULL; - struct dlist *attrlist = NULL; - - if (clsdev == NULL || name == NULL) { + struct sysfs_class *cls = NULL; + char classpath[SYSFS_PATH_MAX]; + + if (!name) { errno = EINVAL; return NULL; } - + + memset(classpath, 0, SYSFS_PATH_MAX); + if ((sysfs_get_mnt_path(classpath, SYSFS_PATH_MAX)) != 0) { + dprintf("Sysfs not supported on this system\n"); + return NULL; + } + /* - * First, see if it's in the current directory. Then look at - * subdirs since class devices can have subdirs of attributes. - */ - attrlist = sysfs_get_classdev_attributes(clsdev); - if (attrlist != NULL) { - cur = sysfs_get_directory_attribute(clsdev->directory, - (char *)name); - if (cur != NULL) - return cur; - } - - if (clsdev->directory->subdirs == NULL) - if ((sysfs_read_dir_subdirs(clsdev->directory)) != 0 || - clsdev->directory->subdirs == NULL) - return NULL; + * 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) { + safestrcat(classpath, "/"); + safestrcat(classpath, SYSFS_BLOCK_NAME); + } else { + safestrcat(classpath, "/"); + safestrcat(classpath, SYSFS_CLASS_NAME); + safestrcat(classpath, "/"); + safestrcat(classpath, name); + } + if (sysfs_path_is_dir(classpath)) { + dprintf("Class %s not found on the system\n", name); + return NULL; + } - if (clsdev->directory->subdirs != NULL) { - dlist_for_each_data(clsdev->directory->subdirs, sdir, - struct sysfs_directory) { - if ((sysfs_path_is_dir(sdir->path)) != 0) - continue; - cur = sysfs_get_directory_attribute(sdir, - (char *)name); - if (cur == NULL) - continue; - } + cls = alloc_class(); + if (cls == NULL) { + dprintf("calloc failed\n"); + return NULL; } - return cur; + safestrcpy(cls->name, name); + safestrcpy(cls->path, classpath); + if ((sysfs_remove_trailing_slash(cls->path)) != 0) { + dprintf("Invalid path to class device %s\n", cls->path); + sysfs_close_class(cls); + return NULL; + } + + return cls; } /** - * 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 - * Returns sysfs_attribute * on SUCCESS and NULL on error + * sysfs_get_class_device: get specific class device using the device's id + * @cls: sysfs_class to find the device on + * @name: name of the class device to look for * - * NOTE: - * A call to sysfs_close_attribute() is required to close the - * attribute returned and to free memory - */ -struct sysfs_attribute *sysfs_open_classdev_attr(const char *classname, - const char *dev, const char *attrib) + * Returns sysfs_class_device * on success and NULL on failure + */ +struct sysfs_class_device *sysfs_get_class_device(struct sysfs_class *cls, + const char *name) { - struct sysfs_attribute *attribute = NULL; char path[SYSFS_PATH_MAX]; + struct sysfs_class_device *cdev = NULL; - if (classname == NULL || dev == NULL || attrib == NULL) { + if (!cls || !name) { errno = EINVAL; return NULL; } - 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; + + if (cls->devices) { + cdev = (struct sysfs_class_device *)dlist_find_custom + (cls->devices, (void *)name, cdev_name_equal); + if (cdev) + return cdev; } + + safestrcpy(path, cls->path); safestrcat(path, "/"); - safestrcat(path, attrib); - attribute = sysfs_open_attribute(path); - if (attribute == NULL) { - dprintf("Error opening attribute %s on class device %s\n", - attrib, dev); + safestrcat(path, name); + cdev = sysfs_open_class_device_path(path); + if (!cdev) { + dprintf("Error opening class device at %s\n", path); return NULL; } - if ((sysfs_read_attribute(attribute)) != 0) { - dprintf("Error reading attribute %s for class device %s\n", - attrib, dev); - sysfs_close_attribute(attribute); - return NULL; + if (!cls->devices) + cls->devices = dlist_new_with_delete + (sizeof(struct sysfs_class_device), + sysfs_close_cls_dev); + + dlist_unshift_sorted(cls->devices, cdev, sort_list); + return cdev; +} + +/** + * Add class devices to list + */ +static void add_cdevs_to_classlist(struct sysfs_class *cls, struct dlist *list) +{ + char path[SYSFS_PATH_MAX], *cdev_name; + struct sysfs_class_device *cdev = NULL; + + if (cls == NULL || list == NULL) + return; + + dlist_for_each_data(list, cdev_name, char) { + if (cls->devices) { + cdev = (struct sysfs_class_device *) + dlist_find_custom(cls->devices, + (void *)cdev_name, cdev_name_equal); + if (cdev) + continue; + } + safestrcpy(path, cls->path); + safestrcat(path, "/"); + safestrcat(path, cdev_name); + cdev = sysfs_open_class_device_path(path); + if (cdev) { + if (!cls->devices) + cls->devices = dlist_new_with_delete + (sizeof(struct sysfs_class_device), + sysfs_close_cls_dev); + dlist_unshift_sorted(cls->devices, cdev, + sort_list); + } } - return attribute; } +/** + * sysfs_get_class_devices: get all class devices in the given class + * @cls: sysfs_class whose devices list is needed + * + * Returns a dlist of sysfs_class_device * on success and NULL on failure + */ +struct dlist *sysfs_get_class_devices(struct sysfs_class *cls) +{ + char path[SYSFS_PATH_MAX]; + struct dlist *dirlist, *linklist; + + if (!cls) { + errno = EINVAL; + return NULL; + } + + /* + * Post linux-2.6.14, we have nested classes and links under + * /sys/class/xxx/. are also valid class devices + */ + safestrcpy(path, cls->path); + dirlist = read_dir_subdirs(path); + if (dirlist) { + add_cdevs_to_classlist(cls, dirlist); + sysfs_close_list(dirlist); + } + + linklist = read_dir_links(path); + if (linklist) { + add_cdevs_to_classlist(cls, linklist); + sysfs_close_list(linklist); + } + + return cls->devices; +}