+ if (sysfs_get_mnt_path(sysfs_path, SYSFS_PATH_MAX) != 0) {
+ dprintf("Error getting sysfs mount point\n");
+ return NULL;
+ }
+
+ safestrcat(sysfs_path, "/");
+ safestrcat(sysfs_path, name);
+ dir = sysfs_open_directory(sysfs_path);
+ if (dir == NULL) {
+ dprintf("Error opening sysfs_directory at %s\n", sysfs_path);
+ return NULL;
+ }
+
+ if ((sysfs_read_dir_subdirs(dir)) != 0) {
+ dprintf("Error reading sysfs_directory at %s\n", sysfs_path);
+ sysfs_close_directory(dir);
+ return NULL;
+ }
+
+ if (dir->subdirs != NULL) {
+ list = dlist_new_with_delete(SYSFS_NAME_LEN,
+ sysfs_del_name);
+ if (list == NULL) {
+ dprintf("Error creating list\n");
+ sysfs_close_directory(dir);
+ return NULL;
+ }
+
+ dlist_for_each_data(dir->subdirs, cur,
+ struct sysfs_directory) {
+ subsys_name = (char *)calloc(1, SYSFS_NAME_LEN);
+ safestrcpymax(subsys_name, cur->name, SYSFS_NAME_LEN);
+ dlist_unshift_sorted(list, subsys_name, sort_char);
+ }
+ }
+ 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_NAME) == 0) {
+ c = strstr(sysfs_path, SYSFS_CLASS_NAME);
+ if (c == NULL)
+ goto out;
+ *c = '\0';
+ safestrcpymax(c, SYSFS_BLOCK_NAME,
+ sizeof(sysfs_path) - strlen(sysfs_path));
+ if ((sysfs_path_is_dir(sysfs_path)) == 0) {
+ subsys_name = (char *)calloc(1, SYSFS_NAME_LEN);
+ safestrcpymax(subsys_name, SYSFS_BLOCK_NAME,
+ SYSFS_NAME_LEN);
+ dlist_unshift_sorted(list, subsys_name, sort_char);
+ }
+ }
+out:
+ return list;
+}
+
+
+/**
+ * sysfs_open_bus_devices_list: gets a list of all devices on "name" bus
+ * @name: name of the subsystem, eg., "pci", "scsi", "usb"
+ * Returns a dlist of supported names or NULL if subsystem not supported
+ */
+struct dlist *sysfs_open_bus_devices_list(char *name)
+{
+ char sysfs_path[SYSFS_PATH_MAX], *device_name = NULL;
+ struct sysfs_directory *dir = NULL;
+ struct sysfs_link *cur = NULL;
+ struct dlist *list = NULL;
+
+ if (name == NULL)
+ return NULL;
+
+ if (sysfs_get_mnt_path(sysfs_path, SYSFS_PATH_MAX) != 0) {
+ dprintf("Error getting sysfs mount point\n");
+ return NULL;
+ }
+
+ safestrcat(sysfs_path, "/");
+ safestrcat(sysfs_path, SYSFS_BUS_NAME);
+ safestrcat(sysfs_path, "/");
+ safestrcat(sysfs_path, name);
+ safestrcat(sysfs_path, "/");
+ safestrcat(sysfs_path, SYSFS_DEVICES_NAME);
+ dir = sysfs_open_directory(sysfs_path);
+ if (dir == NULL) {
+ dprintf("Error opening sysfs_directory at %s\n", sysfs_path);
+ return NULL;
+ }
+
+ if ((sysfs_read_dir_links(dir)) != 0) {
+ dprintf("Error reading sysfs_directory at %s\n", sysfs_path);
+ sysfs_close_directory(dir);
+ return NULL;
+ }
+
+ if (dir->links != NULL) {
+ list = dlist_new_with_delete(SYSFS_NAME_LEN,
+ sysfs_del_name);
+ if (list == NULL) {
+ dprintf("Error creating list\n");
+ sysfs_close_directory(dir);
+ return NULL;
+ }
+
+ dlist_for_each_data(dir->links, cur,
+ struct sysfs_link) {
+ device_name = (char *)calloc(1, SYSFS_NAME_LEN);
+ safestrcpymax(device_name, cur->name, SYSFS_NAME_LEN);
+ dlist_unshift_sorted(list, device_name, sort_char);
+ }
+ }
+ sysfs_close_directory(dir);
+ return list;
+}
+
+/**
+ * sysfs_path_is_dir: Check if the path supplied points to a directory
+ * @path: path to validate
+ * Returns 0 if path points to dir, 1 otherwise
+ */
+int sysfs_path_is_dir(const char *path)
+{
+ struct stat astats;
+
+ if (path == NULL) {
+ errno = EINVAL;
+ return 1;
+ }
+ if ((lstat(path, &astats)) != 0) {
+ dprintf("stat() failed\n");
+ return 1;
+ }
+ if (S_ISDIR(astats.st_mode))
+ return 0;
+
+ return 1;
+}
+
+/**
+ * sysfs_path_is_link: Check if the path supplied points to a link
+ * @path: path to validate
+ * Returns 0 if path points to link, 1 otherwise
+ */
+int sysfs_path_is_link(const char *path)
+{
+ struct stat astats;
+
+ if (path == NULL) {
+ errno = EINVAL;
+ return 1;
+ }
+ if ((lstat(path, &astats)) != 0) {
+ dprintf("stat() failed\n");
+ return 1;
+ }
+ if (S_ISLNK(astats.st_mode))
+ return 0;
+
+ return 1;
+}
+
+/**
+ * sysfs_path_is_file: Check if the path supplied points to a file
+ * @path: path to validate
+ * Returns 0 if path points to file, 1 otherwise
+ */
+int sysfs_path_is_file(const char *path)
+{
+ struct stat astats;
+
+ if (path == NULL) {
+ errno = EINVAL;
+ return 1;
+ }
+ if ((lstat(path, &astats)) != 0) {
+ dprintf("stat() failed\n");
+ return 1;
+ }
+ if (S_ISREG(astats.st_mode))
+ return 0;
+
+ return 1;