- * sysfs_open_device_tree: opens root device and all of its children,
- * creating a tree of devices. Only opens children.
- * @path: sysfs path to devices
- * returns struct sysfs_device and its children with success or NULL with
- * error.
- */
-struct sysfs_device *sysfs_open_device_tree(const char *path)
-{
- struct sysfs_device *rootdev = NULL, *new = NULL;
- struct sysfs_directory *cur = NULL;
-
- if (path == NULL) {
- errno = EINVAL;
- return NULL;
- }
- 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) {
- new = sysfs_open_device_tree(cur->path);
- if (new == NULL) {
- dprintf("Error opening device tree at %s\n",
- cur->path);
- sysfs_close_device_tree(rootdev);
- return NULL;
- }
- if (rootdev->children == NULL)
- rootdev->children = dlist_new_with_delete
- (sizeof(struct sysfs_device),
- sysfs_close_dev_tree);
- dlist_unshift_sorted(rootdev->children,
- new, sort_list);
- }
- }
-
- return rootdev;
-}
-
-/**
- * sysfs_close_root_device: closes root and all devices
- * @root: root device to close
- */
-void sysfs_close_root_device(struct sysfs_root_device *root)
-{
- if (root != NULL) {
- if (root->devices != NULL)
- dlist_destroy(root->devices);
- if (root->directory != NULL)
- sysfs_close_directory(root->directory);
- free(root);
- }
-}
-
-/**
- * sysfs_get_root_devices: opens up all the devices under this root device
- * @root: root device to open devices for
- * returns dlist of devices with success and NULL with error
- */
-struct dlist *sysfs_get_root_devices(struct sysfs_root_device *root)
-{
- struct sysfs_device *dev = NULL;
- struct sysfs_directory *cur = NULL;
-
- if (root == NULL) {
- errno = EINVAL;
- 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;
-
- dlist_for_each_data(root->directory->subdirs, cur,
- struct sysfs_directory) {
- dev = sysfs_open_device_tree(cur->path);
- if (dev == NULL) {
- dprintf ("Error opening device at %s\n", cur->path);
- continue;
- }
- if (root->devices == NULL)
- root->devices = dlist_new_with_delete
- (sizeof(struct sysfs_device),
- sysfs_close_dev_tree);
- dlist_unshift_sorted(root->devices, dev, sort_list);
- }
-
- return root->devices;
-}
-
-/**
- * sysfs_open_root_device: opens sysfs devices root and all of its
- * devices.
- * @name: name of /sys/devices/root to open
- * returns struct sysfs_root_device if success and NULL with error
- */
-struct sysfs_root_device *sysfs_open_root_device(const char *name)
-{
- struct sysfs_root_device *root = NULL;
- 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;
- }
-
- safestrcat(rootpath, "/");
- safestrcat(rootpath, SYSFS_DEVICES_NAME);
- safestrcat(rootpath, "/");
- safestrcat(rootpath, name);
- if ((sysfs_path_is_dir(rootpath)) != 0) {
- errno = EINVAL;
- dprintf("Invalid root device: %s\n", name);
- return NULL;
- }
- root = (struct sysfs_root_device *)calloc
- (1, sizeof(struct sysfs_root_device));
- if (root == NULL) {
- dprintf("calloc failure\n");
- return NULL;
- }
- safestrcpy(root->name, name);
- safestrcpy(root->path, rootpath);
- if ((sysfs_remove_trailing_slash(root->path)) != 0) {
- dprintf("Invalid path to root device %s\n", root->path);
- sysfs_close_root_device(root);
- return NULL;
- }
- return root;
-}
-
-/**
- * sysfs_get_device_attributes: returns a dlist of attributes corresponding to
- * the specific device
- * @device: struct sysfs_device * for which attributes are to be returned
- */
-struct dlist *sysfs_get_device_attributes(struct sysfs_device *device)
-{
- if (device == NULL) {
- errno = EINVAL;
- 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;
- }
- return (device->directory->attributes);
-}
-
-/**
- * sysfs_refresh_device_attributes: refreshes the device's list of attributes
- * @device: sysfs_device whose attributes to refresh
- *
- * NOTE: Upon return, prior references to sysfs_attributes for this device
- * _may_ not be valid
- *
- * Returns list of attributes on success and NULL on failure