X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=libsysfs%2Fsysfs_dir.c;h=ce01669754160dfa652e29f5e7dd6462cae845ae;hp=ff2edf461588458b68201051b5a709059b80b4fc;hb=95a6f4c8acafe7031087667aa556a50a6a091c93;hpb=fe3fe3b29ffbc7d0ce7dca6a371da31d8b3ff7f8 diff --git a/libsysfs/sysfs_dir.c b/libsysfs/sysfs_dir.c index ff2edf461..ce0166975 100644 --- a/libsysfs/sysfs_dir.c +++ b/libsysfs/sysfs_dir.c @@ -1,5 +1,5 @@ /* - * syfs_dir.c + * sysfs_dir.c * * Directory utility functions for libsysfs * @@ -147,7 +147,7 @@ struct sysfs_attribute *sysfs_open_attribute(const unsigned char *path) sysfs_close_attribute(sysattr); return NULL; } - strncpy(sysattr->path, path, sizeof(sysattr->path)); + strncpy(sysattr->path, path, SYSFS_PATH_MAX); if ((stat(sysattr->path, &fileinfo)) != 0) { dprintf("Stat failed: No such attribute?\n"); sysattr->method = 0; @@ -187,8 +187,15 @@ int sysfs_write_attribute(struct sysfs_attribute *sysattr, return -1; } if (sysattr->method & SYSFS_METHOD_SHOW) { + /* + * read attribute again to see if we can get an updated value + */ + if ((sysfs_read_attribute(sysattr)) != 0) { + dprintf("Error reading attribute\n"); + return -1; + } if ((strncmp(sysattr->value, new_value, sysattr->len)) == 0) { - dprintf("Attribute %s already has the requested value %s\n", + dprintf("Attr %s already has the requested value %s\n", sysattr->name, new_value); return 0; } @@ -253,8 +260,8 @@ int sysfs_read_attribute(struct sysfs_attribute *sysattr) { unsigned char *fbuf = NULL; unsigned char *vbuf = NULL; - size_t length = 0; - int pgsize = 0; + ssize_t length = 0; + long pgsize = 0; int fd; if (sysattr == NULL) { @@ -266,6 +273,7 @@ int sysfs_read_attribute(struct sysfs_attribute *sysattr) sysattr->path); return -1; } + pgsize = getpagesize(); fbuf = (unsigned char *)calloc(1, pgsize+1); if (fbuf == NULL) { @@ -284,6 +292,14 @@ int sysfs_read_attribute(struct sysfs_attribute *sysattr) free(fbuf); return -1; } + if (sysattr->len > 0) { + if ((sysattr->len == length) && + (!(strncmp(sysattr->value, fbuf, length)))) { + close(fd); + return 0; + } + free(sysattr->value); + } sysattr->len = length; close(fd); vbuf = (unsigned char *)realloc(fbuf, length+1); @@ -385,6 +401,7 @@ void sysfs_close_directory(struct sysfs_directory *sysdir) if (sysdir->attributes != NULL) dlist_destroy(sysdir->attributes); free(sysdir); + sysdir = NULL; } } @@ -420,12 +437,16 @@ int sysfs_read_all_subdirs(struct sysfs_directory *sysdir) errno = EINVAL; return -1; } - if (sysdir->subdirs == NULL) - return 0; - dlist_for_each_data(sysdir->subdirs, cursub, struct sysfs_directory) { - if (sysfs_read_directory(cursub) != 0) - dprintf ("Error reading subdirectory %s\n", - cursub->name); + if (sysdir->subdirs == NULL) + if ((sysfs_read_dir_subdirs(sysdir)) != 0) + return 0; + if (sysdir->subdirs != NULL) { + dlist_for_each_data(sysdir->subdirs, cursub, + struct sysfs_directory) { + if ((sysfs_read_dir_subdirs(cursub)) != 0) + dprintf ("Error reading subdirectory %s\n", + cursub->name); + } } return 0; } @@ -444,6 +465,13 @@ struct sysfs_directory *sysfs_open_directory(const unsigned char *path) errno = EINVAL; return NULL; } + + if (sysfs_path_is_dir(path) != 0) { + dprintf("Invalid path directory %s\n", path); + errno = EINVAL; + return NULL; + } + sdir = alloc_directory(); if (sdir == NULL) { dprintf("Error allocating directory %s\n", path); @@ -454,7 +482,7 @@ struct sysfs_directory *sysfs_open_directory(const unsigned char *path) sysfs_close_directory(sdir); return NULL; } - strncpy(sdir->path, path, sizeof(sdir->path)); + strncpy(sdir->path, path, SYSFS_PATH_MAX); return sdir; } @@ -489,6 +517,198 @@ struct sysfs_link *sysfs_open_link(const unsigned char *linkpath) return ln; } +/** + * add_attribute: open and add attribute at path to given directory + * @sysdir: directory to add attribute to + * @path: path to attribute + * returns 0 with success and -1 with error. + */ +static int add_attribute(struct sysfs_directory *sysdir, + const unsigned char *path) +{ + struct sysfs_attribute *attr = NULL; + + attr = sysfs_open_attribute(path); + if (attr == NULL) { + dprintf("Error opening attribute %s\n", path); + return -1; + } + if (attr->method & SYSFS_METHOD_SHOW) { + if ((sysfs_read_attribute(attr)) != 0) { + dprintf("Error reading attribute %s\n", path); + sysfs_close_attribute(attr); + return 0; + } + } + + if (sysdir->attributes == NULL) { + sysdir->attributes = dlist_new_with_delete + (sizeof(struct sysfs_attribute), sysfs_del_attribute); + } + dlist_unshift(sysdir->attributes, attr); + + return 0; +} + +/** + * add_subdirectory: open and add subdirectory at path to given directory + * @sysdir: directory to add subdir to + * @path: path to subdirectory + * returns 0 with success and -1 with error. + */ +static int add_subdirectory(struct sysfs_directory *sysdir, + const unsigned char *path) +{ + struct sysfs_directory *subdir = NULL; + + subdir = sysfs_open_directory(path); + if (subdir == NULL) { + dprintf("Error opening directory %s\n", path); + return -1; + } + if (sysdir->subdirs == NULL) + sysdir->subdirs = dlist_new_with_delete + (sizeof(struct sysfs_directory), sysfs_del_directory); + dlist_unshift(sysdir->subdirs, subdir); + return 0; +} + +/** + * add_link: open and add link at path to given directory + * @sysdir: directory to add link to + * @path: path to link + * returns 0 with success and -1 with error. + */ +static int add_link(struct sysfs_directory *sysdir, const unsigned char *path) +{ + struct sysfs_link *ln = NULL; + + ln = sysfs_open_link(path); + if (ln == NULL) { + dprintf("Error opening link %s\n", path); + return -1; + } + if (sysdir->links == NULL) + sysdir->links = dlist_new_with_delete + (sizeof(struct sysfs_link), sysfs_del_link); + dlist_unshift(sysdir->links, ln); + return 0; +} + +/** + * sysfs_read_dir_attributes: grabs attributes for the given directory + * @sysdir: sysfs directory to open + * returns 0 with success and -1 with error. + */ +int sysfs_read_dir_attributes(struct sysfs_directory *sysdir) +{ + DIR *dir = NULL; + struct dirent *dirent = NULL; + unsigned char file_path[SYSFS_PATH_MAX]; + int retval = 0; + + if (sysdir == NULL) { + errno = EINVAL; + return -1; + } + dir = opendir(sysdir->path); + if (dir == NULL) { + dprintf("Error opening directory %s\n", sysdir->path); + return -1; + } + while(((dirent = readdir(dir)) != NULL) && retval == 0) { + if (0 == strcmp(dirent->d_name, ".")) + continue; + if (0 == strcmp(dirent->d_name, "..")) + continue; + memset(file_path, 0, SYSFS_PATH_MAX); + strncpy(file_path, sysdir->path, SYSFS_PATH_MAX); + strcat(file_path, "/"); + strcat(file_path, dirent->d_name); + if ((sysfs_path_is_file(file_path)) == 0) + retval = add_attribute(sysdir, file_path); + } + closedir(dir); + return(retval); +} + +/** + * sysfs_read_dir_links: grabs links in a specific directory + * @sysdir: sysfs directory to read links + * returns 0 with success and -1 with error. + */ +int sysfs_read_dir_links(struct sysfs_directory *sysdir) +{ + DIR *dir = NULL; + struct dirent *dirent = NULL; + unsigned char file_path[SYSFS_PATH_MAX]; + int retval = 0; + + if (sysdir == NULL) { + errno = EINVAL; + return -1; + } + dir = opendir(sysdir->path); + if (dir == NULL) { + dprintf("Error opening directory %s\n", sysdir->path); + return -1; + } + while(((dirent = readdir(dir)) != NULL) && retval == 0) { + if (0 == strcmp(dirent->d_name, ".")) + continue; + if (0 == strcmp(dirent->d_name, "..")) + continue; + memset(file_path, 0, SYSFS_PATH_MAX); + strncpy(file_path, sysdir->path, SYSFS_PATH_MAX); + strcat(file_path, "/"); + strcat(file_path, dirent->d_name); + if ((sysfs_path_is_link(file_path)) == 0) { + retval = add_link(sysdir, file_path); + if (retval != 0) + break; + } + } + closedir(dir); + return(retval); +} + +/** + * sysfs_read_dir_subdirs: grabs subdirs in a specific directory + * @sysdir: sysfs directory to read links + * returns 0 with success and -1 with error. + */ +int sysfs_read_dir_subdirs(struct sysfs_directory *sysdir) +{ + DIR *dir = NULL; + struct dirent *dirent = NULL; + unsigned char file_path[SYSFS_PATH_MAX]; + int retval = 0; + + if (sysdir == NULL) { + errno = EINVAL; + return -1; + } + dir = opendir(sysdir->path); + if (dir == NULL) { + dprintf("Error opening directory %s\n", sysdir->path); + return -1; + } + while(((dirent = readdir(dir)) != NULL) && retval == 0) { + if (0 == strcmp(dirent->d_name, ".")) + continue; + if (0 == strcmp(dirent->d_name, "..")) + continue; + memset(file_path, 0, SYSFS_PATH_MAX); + strncpy(file_path, sysdir->path, SYSFS_PATH_MAX); + strcat(file_path, "/"); + strcat(file_path, dirent->d_name); + if ((sysfs_path_is_dir(file_path)) == 0) + retval = add_subdirectory(sysdir, file_path); + } + closedir(dir); + return(retval); +} + /** * sysfs_read_directory: grabs attributes, links, and subdirectories * @sysdir: sysfs directory to open @@ -499,9 +719,6 @@ int sysfs_read_directory(struct sysfs_directory *sysdir) DIR *dir = NULL; struct dirent *dirent = NULL; struct stat astats; - struct sysfs_attribute *attr = NULL; - struct sysfs_directory *subdir = NULL; - struct sysfs_link *ln = NULL; unsigned char file_path[SYSFS_PATH_MAX]; int retval = 0; @@ -520,69 +737,113 @@ int sysfs_read_directory(struct sysfs_directory *sysdir) if (0 == strcmp(dirent->d_name, "..")) continue; memset(file_path, 0, SYSFS_PATH_MAX); - strncpy(file_path, sysdir->path, sizeof(file_path)); - strncat(file_path, "/", sizeof(file_path)); - strncat(file_path, dirent->d_name, sizeof(file_path)); + strncpy(file_path, sysdir->path, SYSFS_PATH_MAX); + strcat(file_path, "/"); + strcat(file_path, dirent->d_name); if ((lstat(file_path, &astats)) != 0) { dprintf("stat failed\n"); continue; } - if (S_ISREG(astats.st_mode)) { - attr = sysfs_open_attribute(file_path); - if (attr == NULL) { - dprintf("Error opening attribute %s\n", - file_path); - retval = -1; - break; - } - if (attr->method & SYSFS_METHOD_SHOW) { - if ((sysfs_read_attribute(attr)) != 0) { - dprintf("Error reading attribute %s\n", - file_path); - sysfs_close_attribute(attr); - continue; - } - } - - if (sysdir->attributes == NULL) { - sysdir->attributes = dlist_new_with_delete - (sizeof(struct sysfs_attribute), - sysfs_del_attribute); - } - dlist_unshift(sysdir->attributes, attr); - } else if (S_ISDIR(astats.st_mode)) { - subdir = sysfs_open_directory(file_path); - if (subdir == NULL) { - dprintf("Error opening directory %s\n", - file_path); - retval = -1; - break; - } - if (sysdir->subdirs == NULL) - sysdir->subdirs = dlist_new_with_delete - (sizeof(struct sysfs_directory), - sysfs_del_directory); - dlist_unshift(sysdir->subdirs, subdir); - } else if (S_ISLNK(astats.st_mode)) { - ln = sysfs_open_link(file_path); - if (ln == NULL) { - dprintf("Error opening link %s\n", file_path); - retval = -1; - break; - } - if (sysdir->links == NULL) - sysdir->links = dlist_new_with_delete - (sizeof(struct sysfs_link), - sysfs_del_link); - dlist_unshift(sysdir->links, ln); - } + if (S_ISDIR(astats.st_mode)) + retval = add_subdirectory(sysdir, file_path); + + else if (S_ISLNK(astats.st_mode)) + retval = add_link(sysdir, file_path); + + else if (S_ISREG(astats.st_mode)) + retval = add_attribute(sysdir, file_path); } closedir(dir); return(retval); } /** - * sysfs_get_directory_attribute: retrieves attribute attrname + * sysfs_refresh_dir_attributes: Refresh attributes list + * @sysdir: directory whose list of attributes to refresh + * Returns 0 on success, 1 on failure + */ +int sysfs_refresh_dir_attributes(struct sysfs_directory *sysdir) +{ + if (sysdir == NULL) { + errno = EINVAL; + return 1; + } + if ((sysfs_path_is_dir(sysdir->path)) != 0) { + dprintf("Invalid path to directory %s\n", sysdir->path); + errno = EINVAL; + return 1; + } + if (sysdir->attributes != NULL) { + dlist_destroy(sysdir->attributes); + sysdir->attributes = NULL; + } + if ((sysfs_read_dir_attributes(sysdir)) != 0) { + dprintf("Error refreshing attributes for directory %s\n", + sysdir->path); + return 1; + } + return 0; +} + +/** + * sysfs_refresh_dir_links: Refresh links list + * @sysdir: directory whose list of links to refresh + * Returns 0 on success, 1 on failure + */ +int sysfs_refresh_dir_links(struct sysfs_directory *sysdir) +{ + if (sysdir == NULL) { + errno = EINVAL; + return 1; + } + if ((sysfs_path_is_dir(sysdir->path)) != 0) { + dprintf("Invalid path to directory %s\n", sysdir->path); + errno = EINVAL; + return 1; + } + if (sysdir->links != NULL) { + dlist_destroy(sysdir->links); + sysdir->links = NULL; + } + if ((sysfs_read_dir_links(sysdir)) != 0) { + dprintf("Error refreshing links for directory %s\n", + sysdir->path); + return 1; + } + return 0; +} + +/** + * sysfs_refresh_dir_subdirs: Refresh subdirs list + * @sysdir: directory whose list of subdirs to refresh + * Returns 0 on success, 1 on failure + */ +int sysfs_refresh_dir_subdirs(struct sysfs_directory *sysdir) +{ + if (sysdir == NULL) { + errno = EINVAL; + return 1; + } + if ((sysfs_path_is_dir(sysdir->path)) != 0) { + dprintf("Invalid path to directory %s\n", sysdir->path); + errno = EINVAL; + return 1; + } + if (sysdir->subdirs != NULL) { + dlist_destroy(sysdir->subdirs); + sysdir->subdirs = NULL; + } + if ((sysfs_read_dir_subdirs(sysdir)) != 0) { + dprintf("Error refreshing subdirs for directory %s\n", + sysdir->path); + return 1; + } + return 0; +} + +/** + * sysfs_get_directory_attribute: retrieves attribute attrname from current + * directory only * @dir: directory to retrieve attribute from * @attrname: name of attribute to look for * returns sysfs_attribute if found and NULL if not found @@ -590,30 +851,41 @@ int sysfs_read_directory(struct sysfs_directory *sysdir) struct sysfs_attribute *sysfs_get_directory_attribute (struct sysfs_directory *dir, unsigned char *attrname) { - struct sysfs_directory *sdir = NULL; struct sysfs_attribute *attr = NULL; + unsigned char new_path[SYSFS_PATH_MAX]; if (dir == NULL || attrname == NULL) { errno = EINVAL; return NULL; } - - attr = (struct sysfs_attribute *)dlist_find_custom(dir->attributes, - attrname, dir_attribute_name_equal); - if (attr != NULL) - return attr; - - if (dir->subdirs != NULL) { - dlist_for_each_data(dir->subdirs, sdir, - struct sysfs_directory) { - if (sdir->attributes == NULL) - continue; - attr = sysfs_get_directory_attribute(sdir, attrname); - if (attr != NULL) - return attr; + + if (dir->attributes == NULL) + if ((sysfs_read_dir_attributes(dir) != 0) + || (dir->attributes == NULL)) + return NULL; + + attr = (struct sysfs_attribute *)dlist_find_custom + (dir->attributes, attrname, dir_attribute_name_equal); + if (attr != NULL) { + if ((sysfs_read_attribute(attr)) != 0) { + dprintf("Error reading attribute %s\n", attr->name); + return NULL; + } + } else { + memset(new_path, 0, SYSFS_PATH_MAX); + strcpy(new_path, dir->path); + strcat(new_path, "/"); + strcat(new_path, attrname); + if ((sysfs_path_is_file(new_path)) == 0) { + if ((add_attribute(dir, new_path)) == 0) { + attr = (struct sysfs_attribute *) + dlist_find_custom(dir->attributes, + attrname, dir_attribute_name_equal); + } } } - return NULL; + + return attr; } /** @@ -629,6 +901,14 @@ struct sysfs_link *sysfs_get_directory_link errno = EINVAL; return NULL; } + if (dir->links == NULL) { + if ((sysfs_read_dir_links(dir) != 0) || (dir->links == NULL)) + return NULL; + } else { + if ((sysfs_refresh_dir_links(dir)) != 0) + return NULL; + } + return (struct sysfs_link *)dlist_find_custom(dir->links, linkname, dir_link_name_equal); } @@ -644,10 +924,15 @@ struct sysfs_directory *sysfs_get_subdirectory(struct sysfs_directory *dir, { struct sysfs_directory *sub = NULL, *cursub = NULL; - if (dir == NULL || dir->subdirs == NULL || subname == NULL) { + if (dir == NULL || subname == NULL) { errno = EINVAL; return NULL; } + + if (dir->subdirs == NULL) + if (sysfs_read_dir_subdirs(dir) != 0) + return NULL; + sub = (struct sysfs_directory *)dlist_find_custom(dir->subdirs, subname, dir_subdir_name_equal); if (sub != NULL) @@ -656,8 +941,12 @@ struct sysfs_directory *sysfs_get_subdirectory(struct sysfs_directory *dir, if (dir->subdirs != NULL) { dlist_for_each_data(dir->subdirs, cursub, struct sysfs_directory) { - if (cursub->subdirs == NULL) - continue; + if (cursub->subdirs == NULL) { + if (sysfs_read_dir_subdirs(cursub) != 0) + continue; + if (cursub->subdirs == NULL) + continue; + } sub = sysfs_get_subdirectory(cursub, subname); if (sub != NULL) return sub; @@ -678,7 +967,7 @@ struct sysfs_link *sysfs_get_subdirectory_link(struct sysfs_directory *dir, struct sysfs_directory *cursub = NULL; struct sysfs_link *ln = NULL; - if (dir == NULL || dir->links == NULL || linkname == NULL) { + if (dir == NULL || linkname == NULL) { errno = EINVAL; return NULL; } @@ -687,14 +976,13 @@ struct sysfs_link *sysfs_get_subdirectory_link(struct sysfs_directory *dir, if (ln != NULL) return ln; - if (dir->subdirs == NULL) - return NULL; + if (dir->subdirs == NULL) + if (sysfs_read_dir_subdirs(dir) != 0) + return NULL; if (dir->subdirs != NULL) { dlist_for_each_data(dir->subdirs, cursub, struct sysfs_directory) { - if (cursub->subdirs == NULL) - continue; ln = sysfs_get_subdirectory_link(cursub, linkname); if (ln != NULL) return ln; @@ -702,3 +990,63 @@ struct sysfs_link *sysfs_get_subdirectory_link(struct sysfs_directory *dir, } return NULL; } + +/** + * sysfs_get_dir_attributes: returns dlist of directory attributes + * @dir: directory to retrieve attributes from + * returns dlist of attributes or NULL + */ +struct dlist *sysfs_get_dir_attributes(struct sysfs_directory *dir) +{ + if (dir == NULL) { + errno = EINVAL; + return NULL; + } + + if (dir->attributes == NULL) { + if (sysfs_read_dir_attributes(dir) != 0) + return NULL; + } + + return (dir->attributes); +} + +/** + * sysfs_get_dir_links: returns dlist of directory links + * @dir: directory to return links for + * returns dlist of links or NULL + */ +struct dlist *sysfs_get_dir_links(struct sysfs_directory *dir) +{ + if (dir == NULL) { + errno = EINVAL; + return NULL; + } + + if (dir->links == NULL) { + if (sysfs_read_dir_links(dir) != 0) + return NULL; + } + + return (dir->links); +} + +/** + * sysfs_get_dir_subdirs: returns dlist of directory subdirectories + * @dir: directory to return subdirs for + * returns dlist of subdirs or NULL + */ +struct dlist *sysfs_get_dir_subdirs(struct sysfs_directory *dir) +{ + if (dir == NULL) { + errno = EINVAL; + return NULL; + } + + if (dir->subdirs == NULL) { + if (sysfs_read_dir_subdirs(dir) != 0) + return NULL; + } + + return (dir->subdirs); +}