* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
-#include "libsysfs.h"
+#include "sysfs/libsysfs.h"
#include "sysfs.h"
static void sysfs_close_cls_dev(void *dev)
if (a == NULL || b == NULL)
return 0;
- if (strcmp(((unsigned char *)a), ((struct sysfs_class_device *)b)->name)
- == 0)
+ if (strcmp(((char *)a), ((struct sysfs_class_device *)b)->name) == 0)
return 1;
return 0;
/**
* sysfs_close_class: close single class
- * @class: class structure
+ * @cls: class structure
*/
void sysfs_close_class(struct sysfs_class *cls)
{
*/
static void set_classdev_classname(struct sysfs_class_device *cdev)
{
- unsigned char *c = NULL, *e = NULL;
+ char *c = NULL, *e = NULL;
int count = 0;
c = strstr(cdev->path, SYSFS_CLASS_NAME);
}
if (c == NULL)
- strcpy(cdev->classname, SYSFS_UNKNOWN);
+ safestrcpy(cdev->classname, SYSFS_UNKNOWN);
else {
if (*c == '/')
c++;
}
/**
- * sysfs_open_class_device: Opens and populates class device
+ * sysfs_open_class_device_path: Opens and populates class device
* @path: path to class device.
* returns struct sysfs_class_device with success and NULL with error.
*/
-struct sysfs_class_device *sysfs_open_class_device(const unsigned char *path)
+struct sysfs_class_device *sysfs_open_class_device_path(const char *path)
{
struct sysfs_class_device *cdev = NULL;
return NULL;
}
- strcpy(cdev->path, path);
+ safestrcpy(cdev->path, path);
+ if ((sysfs_remove_trailing_slash(cdev->path)) != 0) {
+ dprintf("Invalid path to class device %s\n", cdev->path);
+ sysfs_close_class_device(cdev);
+ return NULL;
+ }
set_classdev_classname(cdev);
return cdev;
/**
* sysfs_get_class_devices: gets all devices for class
- * @class: class to get devices for
+ * @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)
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)
- || cls->directory->subdirs == NULL)
+ if ((sysfs_read_dir_subdirs(cls->directory)) != 0)
return NULL;
- dlist_for_each_data(cls->directory->subdirs, cur,
- struct sysfs_directory) {
- dev = sysfs_open_class_device(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
+ 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(cls->devices, 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 unsigned char *name)
+struct sysfs_class *sysfs_open_class(const char *name)
{
struct sysfs_class *cls = NULL;
- unsigned char classpath[SYSFS_PATH_MAX];
+ char classpath[SYSFS_PATH_MAX];
if (name == NULL) {
errno = EINVAL;
return NULL;
}
- if (sysfs_trailing_slash(classpath) == 0)
- strcat(classpath, "/");
-
/*
* 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_NAME);
+ safestrcat(classpath, "/");
+ safestrcat(classpath, SYSFS_BLOCK_NAME);
} else {
- strcat(classpath, SYSFS_CLASS_NAME);
- strcat(classpath, "/");
- strcat(classpath, name);
+ 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);
dprintf("calloc failed\n");
return NULL;
}
- strcpy(cls->name, name);
- strcpy(cls->path, classpath);
+ 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;
}
* @class: class to find device on
* @name: class name of the device
*/
-struct sysfs_class_device *sysfs_get_class_device(struct sysfs_class *class,
- unsigned char *name)
+struct sysfs_class_device *sysfs_get_class_device(struct sysfs_class *cls,
+ char *name)
{
- struct dlist *devlist = NULL;
-
- if (class == NULL || name == NULL) {
+ if (cls == NULL || name == NULL) {
errno = EINVAL;
return NULL;
}
- if (class->devices == NULL) {
- class->devices = sysfs_get_class_devices(class);
- if (devlist == 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(class->devices,
+ return (struct sysfs_class_device *)dlist_find_custom(cls->devices,
name, class_name_equal);
}
(struct sysfs_class_device *clsdev)
{
struct sysfs_link *devlink = NULL;
+ char devpath[SYSFS_PATH_MAX];
if (clsdev == NULL) {
errno = EINVAL;
return NULL;
}
-
- if (clsdev->sysdevice != NULL)
- return (clsdev->sysdevice);
+ 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);
return NULL;
}
devlink = sysfs_get_directory_link(clsdev->directory, "device");
- if (devlink == NULL)
+ 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(devlink->target);
+ clsdev->sysdevice = sysfs_open_device_path(devlink->target);
if (clsdev->sysdevice == NULL)
return NULL;
- if (clsdev->driver != NULL)
- strcpy(clsdev->sysdevice->driver_name, clsdev->driver->name);
return (clsdev->sysdevice);
}
(struct sysfs_class_device *clsdev)
{
struct sysfs_link *drvlink = NULL;
+ char drvpath[SYSFS_PATH_MAX];
if (clsdev == NULL) {
errno = EINVAL;
return NULL;
}
-
- if (clsdev->driver != NULL)
- return (clsdev->driver);
-
+ 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) {
- clsdev->driver = sysfs_open_driver(drvlink->target);
- if (clsdev->driver == NULL)
- return NULL;
-
+ 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
* @clsdev: block subsystem class device whose parent needs to be found
*/
static int get_blockdev_parent(struct sysfs_class_device *clsdev)
{
- unsigned char parent_path[SYSFS_PATH_MAX], value[256], *c = NULL;
-
- memset(parent_path, 0, SYSFS_PATH_MAX);
- strcpy(parent_path, clsdev->path);
+ char parent_path[SYSFS_PATH_MAX], *c = NULL;
+ safestrcpy(parent_path, clsdev->path);
c = strstr(parent_path, SYSFS_BLOCK_NAME);
if (c == NULL) {
- dprintf("Class device %s does not belong to BLOCK subsystem",
+ dprintf("Class device %s does not belong to BLOCK subsystem\n",
clsdev->name);
return 1;
}
-
c += strlen(SYSFS_BLOCK_NAME);
if (*c == '/')
c++;
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, '/');
- if (c == NULL)
- goto errout;
- *c = '\0';
-
- /* Now validate if the parent has the "dev" attribute */
- memset(value, 0, 256);
- strcat(parent_path, "/dev");
- if ((sysfs_read_attribute_value(parent_path, value, 256)) != 0) {
- dprintf("Block device %s does not have a parent\n",
- clsdev->name);
- return 1;
- }
-
- c = strrchr(parent_path, '/');
+
+ /* 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(parent_path);
+
+ clsdev->parent = sysfs_open_class_device_path(parent_path);
if (clsdev->parent == NULL) {
dprintf("Error opening the parent class device at %s\n",
parent_path);
* structure. Hence, we now call a specialized function for block and
* later we can add support functions for other subsystems as required.
*/
- if (!(strcmp(clsdev->classname, SYSFS_BLOCK_NAME))) {
+ if (!(strncmp(clsdev->classname, SYSFS_BLOCK_NAME,
+ sizeof(SYSFS_BLOCK_NAME)))) {
if ((get_blockdev_parent(clsdev)) == 0)
return (clsdev->parent);
}
* @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)
+static int get_classdev_path(const char *classname, const char *clsdev,
+ char *path, size_t len)
{
if (classname == NULL || clsdev == NULL || path == NULL) {
errno = EINVAL;
dprintf("Error getting sysfs mount path\n");
return -1;
}
-
- if (sysfs_trailing_slash(path) == 0)
- strcat(path, "/");
-
- if (strcmp(classname, SYSFS_BLOCK_NAME) == 0) {
- strcat(path, SYSFS_BLOCK_NAME);
+ if (strncmp(classname, SYSFS_BLOCK_NAME,
+ sizeof(SYSFS_BLOCK_NAME)) == 0) {
+ safestrcatmax(path, "/", len);
+ safestrcatmax(path, SYSFS_BLOCK_NAME, len);
} else {
- strcat(path, SYSFS_CLASS_NAME);
- strcat(path, "/");
- strcat(path, classname);
+ safestrcatmax(path, "/", len);
+ safestrcatmax(path, SYSFS_CLASS_NAME, len);
+ safestrcatmax(path, "/", len);
+ safestrcatmax(path, classname, len);
}
- strcat(path, "/");
- strcat(path, clsdev);
+ safestrcatmax(path, "/", len);
+ safestrcatmax(path, clsdev, len);
return 0;
}
/**
- * sysfs_open_class_device_by_name: Locates a specific class_device and returns it.
+ * sysfs_open_class_device: 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, const unsigned char *name)
+struct sysfs_class_device *sysfs_open_class_device
+ (const char *classname, const char *name)
{
- unsigned char devpath[SYSFS_PATH_MAX];
+ char devpath[SYSFS_PATH_MAX];
struct sysfs_class_device *cdev = NULL;
if (classname == NULL || name == NULL) {
return NULL;
}
- cdev = sysfs_open_class_device(devpath);
+ cdev = sysfs_open_class_device_path(devpath);
if (cdev == NULL) {
dprintf("Error getting class device %s from class %s\n",
name, classname);
return NULL;
}
if (cdev->directory->attributes == NULL) {
- if ((sysfs_read_dir_attributes(cdev->directory)) != 0) {
- dprintf("Error reading attributes for directory %s\n",
- cdev->directory->path);
+ if ((sysfs_read_dir_attributes(cdev->directory)) != 0)
return NULL;
- }
- } else {
- if ((sysfs_path_is_dir(cdev->path)) != 0) {
- dprintf("Class device at %s no longer exists\n",
- cdev->path);
- return NULL;
- }
- if ((sysfs_refresh_attributes
- (cdev->directory->attributes)) != 0) {
- dprintf("Error refreshing classdev attributes\n");
- return NULL;
- }
}
return (cdev->directory->attributes);
}
+/**
+ * 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
+ */
+struct dlist *sysfs_refresh_classdev_attributes
+ (struct sysfs_class_device *clsdev)
+{
+ if (clsdev == NULL) {
+ errno = EINVAL;
+ return NULL;
+ }
+
+ if (clsdev->directory == NULL)
+ return (sysfs_get_classdev_attributes(clsdev));
+
+ if ((sysfs_refresh_dir_attributes(clsdev->directory)) != 0) {
+ dprintf("Error refreshing class_device attributes\n");
+ return NULL;
+ }
+
+ return (clsdev->directory->attributes);
+}
+
/**
* sysfs_get_classdev_attr: searches class device's attributes by name
* @clsdev: class device to look through
* returns sysfs_attribute reference with success or NULL with error
*/
struct sysfs_attribute *sysfs_get_classdev_attr
- (struct sysfs_class_device *clsdev, const unsigned char *name)
+ (struct sysfs_class_device *clsdev, const char *name)
{
struct sysfs_attribute *cur = NULL;
struct sysfs_directory *sdir = NULL;
errno = EINVAL;
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)
- return NULL;
- cur = sysfs_get_directory_attribute(clsdev->directory,
- (unsigned char *)name);
- if (cur != NULL)
- return cur;
+ 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;
- dlist_for_each_data(clsdev->directory->subdirs, sdir,
- struct sysfs_directory) {
- cur = sysfs_get_directory_attribute(sdir,
- (unsigned char *)name);
- if (cur != NULL)
- return cur;
+ 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;
+ }
}
-
- return NULL;
+ return cur;
}
/**
* 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 unsigned char *classname,
- const unsigned char *dev, const unsigned char *attrib)
+struct sysfs_attribute *sysfs_open_classdev_attr(const char *classname,
+ const char *dev, const char *attrib)
{
struct sysfs_attribute *attribute = NULL;
- unsigned char path[SYSFS_PATH_MAX];
+ char path[SYSFS_PATH_MAX];
if (classname == NULL || dev == NULL || attrib == NULL) {
errno = EINVAL;
dev, classname);
return NULL;
}
- strcat(path, "/");
- strcat(path, attrib);
+ safestrcat(path, "/");
+ safestrcat(path, attrib);
attribute = sysfs_open_attribute(path);
if (attribute == NULL) {
dprintf("Error opening attribute %s on class device %s\n",
}
return attribute;
}
+