-/**
- * sysfs_read_attribute: reads value from attribute
- * @sysattr: attribute to read
- * returns 0 with success and -1 with error.
- */
-int sysfs_read_attribute(struct sysfs_attribute *sysattr)
-{
- char *fbuf = NULL;
- char *vbuf = NULL;
- ssize_t length = 0;
- long pgsize = 0;
- int fd;
-
- if (sysattr == NULL) {
- errno = EINVAL;
- return -1;
- }
- if (!(sysattr->method & SYSFS_METHOD_SHOW)) {
- dprintf("Show method not supported for attribute %s\n",
- sysattr->path);
- errno = EACCES;
- return -1;
- }
- pgsize = getpagesize();
- fbuf = (char *)calloc(1, pgsize+1);
- if (fbuf == NULL) {
- dprintf("calloc failed\n");
- return -1;
- }
- if ((fd = open(sysattr->path, O_RDONLY)) < 0) {
- dprintf("Error reading attribute %s\n", sysattr->path);
- free(fbuf);
- return -1;
- }
- length = read(fd, fbuf, pgsize);
- if (length < 0) {
- dprintf("Error reading from attribute %s\n", sysattr->path);
- close(fd);
- free(fbuf);
- return -1;
- }
- if (sysattr->len > 0) {
- if ((sysattr->len == length) &&
- (!(strncmp(sysattr->value, fbuf, length)))) {
- close(fd);
- free(fbuf);
- return 0;
- }
- free(sysattr->value);
- }
- sysattr->len = length;
- close(fd);
- vbuf = (char *)realloc(fbuf, length+1);
- if (vbuf == NULL) {
- dprintf("realloc failed\n");
- free(fbuf);
- return -1;
- }
- sysattr->value = vbuf;
-
- return 0;
-}
-
-/**
- * sysfs_read_attribute_value: given path to attribute, return its value.
- * values can be up to a pagesize, if buffer is smaller the value will
- * be truncated.
- * @attrpath: sysfs path to attribute
- * @value: buffer to put value
- * @vsize: size of value buffer
- * returns 0 with success and -1 with error.
- */
-int sysfs_read_attribute_value(const char *attrpath,
- char *value, size_t vsize)
-{
- struct sysfs_attribute *attr = NULL;
- size_t length = 0;
-
- if (attrpath == NULL || value == NULL || vsize == 0) {
- errno = EINVAL;
- return -1;
- }
-
- attr = sysfs_open_attribute(attrpath);
- if (attr == NULL) {
- dprintf("Invalid attribute path %s\n", attrpath);
- errno = EINVAL;
- return -1;
- }
- if((sysfs_read_attribute(attr)) != 0 || attr->value == NULL) {
- dprintf("Error reading from attribute %s\n", attrpath);
- sysfs_close_attribute(attr);
- return -1;
- }
- length = strlen(attr->value);
- if (length > vsize)
- dprintf("Value length %d is larger than supplied buffer %d\n",
- length, vsize);
- safestrcpymax(value, attr->value, vsize);
- sysfs_close_attribute(attr);
-
- return 0;
-}
-
-/**
- * sysfs_get_value_from_attrbutes: given a linked list of attributes and an
- * attribute name, return its value
- * @attr: attribute to search
- * @name: name to look for
- * returns char * value - could be NULL
- */
-char *sysfs_get_value_from_attributes(struct dlist *attr, const char *name)
-{
- struct sysfs_attribute *cur = NULL;
-
- if (attr == NULL || name == NULL) {
- errno = EINVAL;
- return NULL;
- }
- dlist_for_each_data(attr, cur, struct sysfs_attribute) {
- if (strcmp(cur->name, name) == 0)
- return cur->value;
- }
- return NULL;
-}
-
-/**
- * sysfs_close_link: closes and cleans up link.
- * @ln: link to close.
- */
-void sysfs_close_link(struct sysfs_link *ln)
-{
- if (ln != NULL)
- free(ln);
-}
-
-/**
- * sysfs_close_directory: closes directory, cleans up attributes and links
- * @sysdir: sysfs_directory to close
- */
-void sysfs_close_directory(struct sysfs_directory *sysdir)
-{
- if (sysdir != NULL) {
- if (sysdir->subdirs != NULL)
- dlist_destroy(sysdir->subdirs);
- if (sysdir->links != NULL)
- dlist_destroy(sysdir->links);
- if (sysdir->attributes != NULL)
- dlist_destroy(sysdir->attributes);
- free(sysdir);
- sysdir = NULL;
- }
-}
-
-/**
- * alloc_directory: allocates and initializes directory structure
- * returns struct sysfs_directory with success or NULL with error.
- */
-static struct sysfs_directory *alloc_directory(void)
-{
- return (struct sysfs_directory *)
- calloc(1, sizeof(struct sysfs_directory));
-}
-
-/**
- * alloc_link: allocates and initializes link structure
- * returns struct sysfs_link with success or NULL with error.
- */
-static struct sysfs_link *alloc_link(void)
-{
- return (struct sysfs_link *)calloc(1, sizeof(struct sysfs_link));
-}
-
-/**
- * sysfs_read_all_subdirs: calls sysfs_read_directory for all subdirs
- * @sysdir: directory whose subdirs need reading.
- * returns 0 with success and -1 with error.
- */
-int sysfs_read_all_subdirs(struct sysfs_directory *sysdir)
-{
- struct sysfs_directory *cursub = NULL;
- int retval = 0;
-
- if (sysdir == NULL) {
- errno = EINVAL;
- return -1;
- }
- 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);
- retval = -1;
- }
- }
- }
- if (!retval)
- errno = 0;
- return retval;
-}
-
-/**
- * sysfs_open_directory: opens a sysfs directory, creates dir struct, and
- * returns.
- * @path: path of directory to open.
- * returns: struct sysfs_directory * with success and NULL on error.
- */
-struct sysfs_directory *sysfs_open_directory(const char *path)
-{
- struct sysfs_directory *sdir = NULL;
-
- if (path == NULL) {
- errno = EINVAL;
- return NULL;
- }
-
- if (sysfs_path_is_dir(path) != 0) {
- dprintf("Invalid path to directory %s\n", path);
- errno = EINVAL;
- return NULL;
- }
-
- sdir = alloc_directory();
- if (sdir == NULL) {
- dprintf("Error allocating directory %s\n", path);
- return NULL;
- }
- if (sysfs_get_name_from_path(path, sdir->name, SYSFS_NAME_LEN) != 0) {
- dprintf("Error getting directory name from path: %s\n", path);
- sysfs_close_directory(sdir);
- return NULL;
- }
- safestrcpy(sdir->path, path);
-
- return sdir;
-}
-
-/**
- * sysfs_open_link: opens a sysfs link, creates struct, and returns
- * @path: path of link to open.
- * returns: struct sysfs_link * with success and NULL on error.
- */
-struct sysfs_link *sysfs_open_link(const char *linkpath)
-{
- struct sysfs_link *ln = NULL;
-
- if (linkpath == NULL || strlen(linkpath) > SYSFS_PATH_MAX) {
- errno = EINVAL;
- return NULL;
- }
-
- ln = alloc_link();
- if (ln == NULL) {
- dprintf("Error allocating link %s\n", linkpath);
- return NULL;
- }
- safestrcpy(ln->path, linkpath);
- if ((sysfs_get_name_from_path(linkpath, ln->name, SYSFS_NAME_LEN)) != 0
- || (sysfs_get_link(linkpath, ln->target, SYSFS_PATH_MAX)) != 0) {
- errno = EINVAL;
- dprintf("Invalid link path %s\n", linkpath);
- return NULL;
- }
-
- return ln;
-}
-