4 * Directory utility functions for libsysfs
6 * Copyright (C) IBM Corp. 2003
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 * sysfs_del_attribute: routine for dlist integration
29 static void sysfs_del_attribute(void *attr)
31 sysfs_close_attribute((struct sysfs_attribute *)attr);
35 * sysfs_del_link: routine for dlist integration
37 static void sysfs_del_link(void *ln)
39 sysfs_close_link((struct sysfs_link *)ln);
43 * sysfs_del_dir: routine for dlist integration
45 static void sysfs_del_directory(void *dir)
47 sysfs_close_directory((struct sysfs_directory *)dir);
51 * dir_attribute_name_equal: compares dir attributes by name
52 * @a: attribute name for comparison
53 * @b: sysfs_attribute to be compared.
54 * returns 1 if a==b->name or 0 if not equal
56 static int dir_attribute_name_equal(void *a, void *b)
58 if (a == NULL || b == NULL)
61 if (strcmp(((unsigned char *)a), ((struct sysfs_attribute *)b)->name)
68 * dir_link_name_equal: compares dir links by name
69 * @a: link name for comparison
70 * @b: sysfs_link to be compared.
71 * returns 1 if a==b->name or 0 if not equal
73 static int dir_link_name_equal(void *a, void *b)
75 if (a == NULL || b == NULL)
78 if (strcmp(((unsigned char *)a), ((struct sysfs_link *)b)->name)
85 * dir_subdir_name_equal: compares subdirs by name
86 * @a: name of subdirectory to compare
87 * @b: sysfs_directory subdirectory to be compared
88 * returns 1 if a==b->name or 0 if not equal
90 static int dir_subdir_name_equal(void *a, void *b)
92 if (a == NULL || b == NULL)
95 if (strcmp(((unsigned char *)a), ((struct sysfs_directory *)b)->name)
102 * sysfs_close_attribute: closes and cleans up attribute
103 * @sysattr: attribute to close.
105 void sysfs_close_attribute(struct sysfs_attribute *sysattr)
107 if (sysattr != NULL) {
108 if (sysattr->value != NULL)
109 free(sysattr->value);
115 * alloc_attribute: allocates and initializes attribute structure
116 * returns struct sysfs_attribute with success and NULL with error.
118 static struct sysfs_attribute *alloc_attribute(void)
120 return (struct sysfs_attribute *)
121 calloc(1, sizeof(struct sysfs_attribute));
125 * sysfs_open_attribute: creates sysfs_attribute structure
126 * @path: path to attribute.
127 * returns sysfs_attribute struct with success and NULL with error.
129 struct sysfs_attribute *sysfs_open_attribute(const unsigned char *path)
131 struct sysfs_attribute *sysattr = NULL;
132 struct stat fileinfo;
138 sysattr = alloc_attribute();
139 if (sysattr == NULL) {
140 dprintf("Error allocating attribute at %s\n", path);
143 if (sysfs_get_name_from_path(path, sysattr->name, SYSFS_NAME_LEN)
145 dprintf("Error retrieving attribute name from path: %s\n",
147 sysfs_close_attribute(sysattr);
150 strncpy(sysattr->path, path, SYSFS_PATH_MAX);
151 if ((stat(sysattr->path, &fileinfo)) != 0) {
152 dprintf("Stat failed: No such attribute?\n");
157 if (fileinfo.st_mode & S_IRUSR)
158 sysattr->method |= SYSFS_METHOD_SHOW;
159 if (fileinfo.st_mode & S_IWUSR)
160 sysattr->method |= SYSFS_METHOD_STORE;
167 * sysfs_write_attribute: write value to the attribute
168 * @sysattr: attribute to write
169 * @new_value: value to write
170 * @len: length of "new_value"
171 * returns 0 with success and -1 with error.
173 int sysfs_write_attribute(struct sysfs_attribute *sysattr,
174 const unsigned char *new_value, size_t len)
179 if (sysattr == NULL || new_value == NULL || len == 0) {
184 if (!(sysattr->method & SYSFS_METHOD_STORE)) {
185 dprintf ("Store method not supported for attribute %s\n",
189 if (sysattr->method & SYSFS_METHOD_SHOW) {
191 * read attribute again to see if we can get an updated value
193 if ((sysfs_read_attribute(sysattr)) != 0) {
194 dprintf("Error reading attribute\n");
197 if ((strncmp(sysattr->value, new_value, sysattr->len)) == 0) {
198 dprintf("Attribute %s already has the requested value %s\n",
199 sysattr->name, new_value);
204 * open O_WRONLY since some attributes have no "read" but only
207 if ((fd = open(sysattr->path, O_WRONLY)) < 0) {
208 dprintf("Error reading attribute %s\n", sysattr->path);
212 length = write(fd, new_value, len);
214 dprintf("Error writing to the attribute %s - invalid value?\n",
218 } else if (length != len) {
219 dprintf("Could not write %d bytes to attribute %s\n",
222 * since we could not write user supplied number of bytes,
223 * restore the old value if one available
225 if (sysattr->method & SYSFS_METHOD_SHOW) {
226 length = write(fd, sysattr->value, sysattr->len);
233 * Validate length that has been copied. Alloc appropriate area
234 * in sysfs_attribute. Verify first if the attribute supports reading
235 * (show method). If it does not, do not bother
237 if (sysattr->method & SYSFS_METHOD_SHOW) {
238 if (length != sysattr->len) {
239 sysattr->value = (char *)realloc(sysattr->value,
241 sysattr->len = length;
242 strncpy(sysattr->value, new_value, length);
244 /*"length" of the new value is same as old one */
245 strncpy(sysattr->value, new_value, length);
255 * sysfs_read_attribute: reads value from attribute
256 * @sysattr: attribute to read
257 * returns 0 with success and -1 with error.
259 int sysfs_read_attribute(struct sysfs_attribute *sysattr)
261 unsigned char *fbuf = NULL;
262 unsigned char *vbuf = NULL;
267 if (sysattr == NULL) {
271 if (!(sysattr->method & SYSFS_METHOD_SHOW)) {
272 dprintf("Show method not supported for attribute %s\n",
279 pgsize = sysconf(_SC_PAGESIZE);
281 fbuf = (unsigned char *)calloc(1, pgsize+1);
283 dprintf("calloc failed\n");
286 if ((fd = open(sysattr->path, O_RDONLY)) < 0) {
287 dprintf("Error reading attribute %s\n", sysattr->path);
291 length = read(fd, fbuf, pgsize);
293 dprintf("Error reading from attribute %s\n", sysattr->path);
298 if (sysattr->len > 0) {
299 if ((sysattr->len == length) &&
300 (!(strncmp(sysattr->value, fbuf, length)))) {
304 free(sysattr->value);
306 sysattr->len = length;
308 vbuf = (unsigned char *)realloc(fbuf, length+1);
310 dprintf("realloc failed\n");
314 sysattr->value = vbuf;
320 * sysfs_read_attribute_value: given path to attribute, return its value.
321 * values can be up to a pagesize, if buffer is smaller the value will
323 * @attrpath: sysfs path to attribute
324 * @value: buffer to put value
325 * @vsize: size of value buffer
326 * returns 0 with success and -1 with error.
328 int sysfs_read_attribute_value(const unsigned char *attrpath,
329 unsigned char *value, size_t vsize)
331 struct sysfs_attribute *attr = NULL;
334 if (attrpath == NULL || value == NULL) {
339 attr = sysfs_open_attribute(attrpath);
341 dprintf("Invalid attribute path %s\n", attrpath);
345 if((sysfs_read_attribute(attr)) != 0 || attr->value == NULL) {
346 dprintf("Error reading from attribute %s\n", attrpath);
347 sysfs_close_attribute(attr);
350 length = strlen(attr->value);
352 dprintf("Value length %d is larger than supplied buffer %d\n",
354 strncpy(value, attr->value, vsize);
355 sysfs_close_attribute(attr);
361 * sysfs_get_value_from_attrbutes: given a linked list of attributes and an
362 * attribute name, return its value
363 * @attr: attribute to search
364 * @name: name to look for
365 * returns unsigned char * value - could be NULL
367 unsigned char *sysfs_get_value_from_attributes(struct dlist *attr,
368 const unsigned char *name)
370 struct sysfs_attribute *cur = NULL;
372 if (attr == NULL || name == NULL) {
376 dlist_for_each_data(attr, cur, struct sysfs_attribute) {
377 if (strcmp(cur->name, name) == 0)
384 * sysfs_close_link: closes and cleans up link.
385 * @ln: link to close.
387 void sysfs_close_link(struct sysfs_link *ln)
394 * sysfs_close_directory: closes directory, cleans up attributes and links
395 * @sysdir: sysfs_directory to close
397 void sysfs_close_directory(struct sysfs_directory *sysdir)
399 if (sysdir != NULL) {
400 if (sysdir->subdirs != NULL)
401 dlist_destroy(sysdir->subdirs);
402 if (sysdir->links != NULL)
403 dlist_destroy(sysdir->links);
404 if (sysdir->attributes != NULL)
405 dlist_destroy(sysdir->attributes);
412 * alloc_directory: allocates and initializes directory structure
413 * returns struct sysfs_directory with success or NULL with error.
415 static struct sysfs_directory *alloc_directory(void)
417 return (struct sysfs_directory *)
418 calloc(1, sizeof(struct sysfs_directory));
422 * alloc_link: allocates and initializes link structure
423 * returns struct sysfs_link with success or NULL with error.
425 static struct sysfs_link *alloc_link(void)
427 return (struct sysfs_link *)calloc(1, sizeof(struct sysfs_link));
431 * sysfs_read_all_subdirs: calls sysfs_read_directory for all subdirs
432 * @sysdir: directory whose subdirs need reading.
433 * returns 0 with success and -1 with error.
435 int sysfs_read_all_subdirs(struct sysfs_directory *sysdir)
437 struct sysfs_directory *cursub = NULL;
439 if (sysdir == NULL) {
443 if (sysdir->subdirs == NULL)
444 if ((sysfs_read_dir_subdirs(sysdir)) != 0)
446 if (sysdir->subdirs != NULL) {
447 dlist_for_each_data(sysdir->subdirs, cursub,
448 struct sysfs_directory) {
449 if ((sysfs_read_dir_subdirs(cursub)) != 0)
450 dprintf ("Error reading subdirectory %s\n",
458 * sysfs_open_directory: opens a sysfs directory, creates dir struct, and
460 * @path: path of directory to open.
461 * returns: struct sysfs_directory * with success and NULL on error.
463 struct sysfs_directory *sysfs_open_directory(const unsigned char *path)
465 struct sysfs_directory *sdir = NULL;
471 sdir = alloc_directory();
473 dprintf("Error allocating directory %s\n", path);
476 if (sysfs_get_name_from_path(path, sdir->name, SYSFS_NAME_LEN) != 0) {
477 dprintf("Error getting directory name from path: %s\n", path);
478 sysfs_close_directory(sdir);
481 strncpy(sdir->path, path, SYSFS_PATH_MAX);
487 * sysfs_open_link: opens a sysfs link, creates struct, and returns
488 * @path: path of link to open.
489 * returns: struct sysfs_link * with success and NULL on error.
491 struct sysfs_link *sysfs_open_link(const unsigned char *linkpath)
493 struct sysfs_link *ln = NULL;
495 if (linkpath == NULL || strlen(linkpath) > SYSFS_PATH_MAX) {
502 dprintf("Error allocating link %s\n", linkpath);
505 strcpy(ln->path, linkpath);
506 if ((sysfs_get_name_from_path(linkpath, ln->name, SYSFS_NAME_LEN)) != 0
507 || (sysfs_get_link(linkpath, ln->target, SYSFS_PATH_MAX)) != 0) {
509 dprintf("Invalid link path %s\n", linkpath);
517 * sysfs_refresh_attributes: Refresh attributes list
518 * @attrlist: list of attributes to refresh
519 * Returns 0 on success, 1 on failure
521 int sysfs_refresh_attributes(struct dlist *attrlist)
523 struct sysfs_attribute *attr = NULL;
525 if (attrlist == NULL) {
529 dlist_for_each_data(attrlist, attr, struct sysfs_attribute) {
530 if (attr->method & SYSFS_METHOD_SHOW) {
531 if ((sysfs_read_attribute(attr)) != 0) {
532 dprintf("Error reading attribute %s\n",
534 if ((sysfs_path_is_file(attr->path)) != 0) {
535 dprintf("Attr %s no longer exists\n",
540 if ((sysfs_path_is_file(attr->path)) != 0) {
541 dprintf("Attr %s no longer exists\n",
550 * add_attribute: open and add attribute at path to given directory
551 * @sysdir: directory to add attribute to
552 * @path: path to attribute
553 * returns 0 with success and -1 with error.
555 static int add_attribute(struct sysfs_directory *sysdir,
556 const unsigned char *path)
558 struct sysfs_attribute *attr = NULL;
560 attr = sysfs_open_attribute(path);
562 dprintf("Error opening attribute %s\n", path);
565 if (attr->method & SYSFS_METHOD_SHOW) {
566 if ((sysfs_read_attribute(attr)) != 0) {
567 dprintf("Error reading attribute %s\n", path);
568 sysfs_close_attribute(attr);
573 if (sysdir->attributes == NULL) {
574 sysdir->attributes = dlist_new_with_delete
575 (sizeof(struct sysfs_attribute), sysfs_del_attribute);
577 dlist_unshift(sysdir->attributes, attr);
583 * add_subdirectory: open and add subdirectory at path to given directory
584 * @sysdir: directory to add subdir to
585 * @path: path to subdirectory
586 * returns 0 with success and -1 with error.
588 static int add_subdirectory(struct sysfs_directory *sysdir,
589 const unsigned char *path)
591 struct sysfs_directory *subdir = NULL;
593 subdir = sysfs_open_directory(path);
594 if (subdir == NULL) {
595 dprintf("Error opening directory %s\n", path);
598 if (sysdir->subdirs == NULL)
599 sysdir->subdirs = dlist_new_with_delete
600 (sizeof(struct sysfs_directory), sysfs_del_directory);
601 dlist_unshift(sysdir->subdirs, subdir);
606 * add_link: open and add link at path to given directory
607 * @sysdir: directory to add link to
608 * @path: path to link
609 * returns 0 with success and -1 with error.
611 static int add_link(struct sysfs_directory *sysdir, const unsigned char *path)
613 struct sysfs_link *ln = NULL;
615 ln = sysfs_open_link(path);
617 dprintf("Error opening link %s\n", path);
620 if (sysdir->links == NULL)
621 sysdir->links = dlist_new_with_delete
622 (sizeof(struct sysfs_link), sysfs_del_link);
623 dlist_unshift(sysdir->links, ln);
628 * sysfs_read_dir_attributes: grabs attributes for the given directory
629 * @sysdir: sysfs directory to open
630 * returns 0 with success and -1 with error.
632 int sysfs_read_dir_attributes(struct sysfs_directory *sysdir)
635 struct dirent *dirent = NULL;
637 unsigned char file_path[SYSFS_PATH_MAX];
640 if (sysdir == NULL) {
644 dir = opendir(sysdir->path);
646 dprintf("Error opening directory %s\n", sysdir->path);
649 while(((dirent = readdir(dir)) != NULL) && retval == 0) {
650 if (0 == strcmp(dirent->d_name, "."))
652 if (0 == strcmp(dirent->d_name, ".."))
654 memset(file_path, 0, SYSFS_PATH_MAX);
655 strncpy(file_path, sysdir->path, SYSFS_PATH_MAX);
656 strcat(file_path, "/");
657 strcat(file_path, dirent->d_name);
658 if ((lstat(file_path, &astats)) != 0) {
659 dprintf("stat failed\n");
662 if (S_ISREG(astats.st_mode))
663 retval = add_attribute(sysdir, file_path);
670 * sysfs_read_dir_links: grabs links in a specific directory
671 * @sysdir: sysfs directory to read links
672 * returns 0 with success and -1 with error.
674 int sysfs_read_dir_links(struct sysfs_directory *sysdir)
677 struct dirent *dirent = NULL;
679 unsigned char file_path[SYSFS_PATH_MAX];
682 if (sysdir == NULL) {
686 dir = opendir(sysdir->path);
688 dprintf("Error opening directory %s\n", sysdir->path);
691 while(((dirent = readdir(dir)) != NULL) && retval == 0) {
692 if (0 == strcmp(dirent->d_name, "."))
694 if (0 == strcmp(dirent->d_name, ".."))
696 memset(file_path, 0, SYSFS_PATH_MAX);
697 strncpy(file_path, sysdir->path, SYSFS_PATH_MAX);
698 strcat(file_path, "/");
699 strcat(file_path, dirent->d_name);
700 if ((lstat(file_path, &astats)) != 0) {
701 dprintf("stat failed\n");
704 if (S_ISLNK(astats.st_mode)) {
705 retval = add_link(sysdir, file_path);
715 * sysfs_read_dir_subdirs: grabs subdirs in a specific directory
716 * @sysdir: sysfs directory to read links
717 * returns 0 with success and -1 with error.
719 int sysfs_read_dir_subdirs(struct sysfs_directory *sysdir)
722 struct dirent *dirent = NULL;
724 unsigned char file_path[SYSFS_PATH_MAX];
727 if (sysdir == NULL) {
731 dir = opendir(sysdir->path);
733 dprintf("Error opening directory %s\n", sysdir->path);
736 while(((dirent = readdir(dir)) != NULL) && retval == 0) {
737 if (0 == strcmp(dirent->d_name, "."))
739 if (0 == strcmp(dirent->d_name, ".."))
741 memset(file_path, 0, SYSFS_PATH_MAX);
742 strncpy(file_path, sysdir->path, SYSFS_PATH_MAX);
743 strcat(file_path, "/");
744 strcat(file_path, dirent->d_name);
745 if ((lstat(file_path, &astats)) != 0) {
746 dprintf("stat failed\n");
749 if (S_ISDIR(astats.st_mode))
750 retval = add_subdirectory(sysdir, file_path);
757 * sysfs_read_directory: grabs attributes, links, and subdirectories
758 * @sysdir: sysfs directory to open
759 * returns 0 with success and -1 with error.
761 int sysfs_read_directory(struct sysfs_directory *sysdir)
764 struct dirent *dirent = NULL;
766 unsigned char file_path[SYSFS_PATH_MAX];
769 if (sysdir == NULL) {
773 dir = opendir(sysdir->path);
775 dprintf("Error opening directory %s\n", sysdir->path);
778 while(((dirent = readdir(dir)) != NULL) && retval == 0) {
779 if (0 == strcmp(dirent->d_name, "."))
781 if (0 == strcmp(dirent->d_name, ".."))
783 memset(file_path, 0, SYSFS_PATH_MAX);
784 strncpy(file_path, sysdir->path, SYSFS_PATH_MAX);
785 strcat(file_path, "/");
786 strcat(file_path, dirent->d_name);
787 if ((lstat(file_path, &astats)) != 0) {
788 dprintf("stat failed\n");
791 if (S_ISDIR(astats.st_mode))
792 retval = add_subdirectory(sysdir, file_path);
794 else if (S_ISLNK(astats.st_mode))
795 retval = add_link(sysdir, file_path);
797 else if (S_ISREG(astats.st_mode))
798 retval = add_attribute(sysdir, file_path);
805 * sysfs_get_directory_attribute: retrieves attribute attrname from current
807 * @dir: directory to retrieve attribute from
808 * @attrname: name of attribute to look for
809 * returns sysfs_attribute if found and NULL if not found
811 struct sysfs_attribute *sysfs_get_directory_attribute
812 (struct sysfs_directory *dir, unsigned char *attrname)
814 struct sysfs_attribute *attr = NULL;
815 unsigned char new_path[SYSFS_PATH_MAX];
817 if (dir == NULL || attrname == NULL) {
822 if (dir->attributes == NULL)
823 if ((sysfs_read_dir_attributes(dir) != 0)
824 || (dir->attributes == NULL))
827 attr = (struct sysfs_attribute *)dlist_find_custom
828 (dir->attributes, attrname, dir_attribute_name_equal);
830 memset(new_path, 0, SYSFS_PATH_MAX);
831 strcpy(new_path, dir->path);
832 strcat(new_path, "/");
833 strcat(new_path, attrname);
834 if ((sysfs_path_is_file(new_path)) == 0) {
835 if ((add_attribute(dir, new_path)) == 0) {
836 attr = (struct sysfs_attribute *)
837 dlist_find_custom(dir->attributes,
838 attrname, dir_attribute_name_equal);
846 * sysfs_get_directory_link: retrieves link from one directory list
847 * @dir: directory to retrieve link from
848 * @linkname: name of link to look for
849 * returns reference to sysfs_link if found and NULL if not found
851 struct sysfs_link *sysfs_get_directory_link
852 (struct sysfs_directory *dir, unsigned char *linkname)
854 if (dir == NULL || linkname == NULL) {
858 if (dir->links == NULL)
859 if ((sysfs_read_dir_links(dir) != 0) || (dir->links == NULL))
862 return (struct sysfs_link *)dlist_find_custom(dir->links,
863 linkname, dir_link_name_equal);
867 * sysfs_get_subdirectory: retrieves subdirectory by name.
868 * @dir: directory to search for subdirectory.
869 * @subname: subdirectory name to get.
870 * returns reference to subdirectory or NULL if not found
872 struct sysfs_directory *sysfs_get_subdirectory(struct sysfs_directory *dir,
873 unsigned char *subname)
875 struct sysfs_directory *sub = NULL, *cursub = NULL;
877 if (dir == NULL || subname == NULL) {
882 if (dir->subdirs == NULL)
883 if (sysfs_read_dir_subdirs(dir) != 0)
886 sub = (struct sysfs_directory *)dlist_find_custom(dir->subdirs,
887 subname, dir_subdir_name_equal);
891 if (dir->subdirs != NULL) {
892 dlist_for_each_data(dir->subdirs, cursub,
893 struct sysfs_directory) {
894 if (cursub->subdirs == NULL) {
895 if (sysfs_read_dir_subdirs(cursub) != 0)
897 if (cursub->subdirs == NULL)
900 sub = sysfs_get_subdirectory(cursub, subname);
909 * sysfs_get_subdirectory_link: looks through all subdirs for specific link.
910 * @dir: directory and subdirectories to search for link.
911 * @linkname: link name to get.
912 * returns reference to link or NULL if not found
914 struct sysfs_link *sysfs_get_subdirectory_link(struct sysfs_directory *dir,
915 unsigned char *linkname)
917 struct sysfs_directory *cursub = NULL;
918 struct sysfs_link *ln = NULL;
920 if (dir == NULL || linkname == NULL) {
925 ln = sysfs_get_directory_link(dir, linkname);
929 if (dir->subdirs == NULL)
930 if (sysfs_read_dir_subdirs(dir) != 0)
933 if (dir->subdirs != NULL) {
934 dlist_for_each_data(dir->subdirs, cursub,
935 struct sysfs_directory) {
936 ln = sysfs_get_subdirectory_link(cursub, linkname);