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, sizeof(sysattr->path));
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) {
190 if ((strncmp(sysattr->value, new_value, sysattr->len)) == 0) {
191 dprintf("Attribute %s already has the requested value %s\n",
192 sysattr->name, new_value);
197 * open O_WRONLY since some attributes have no "read" but only
200 if ((fd = open(sysattr->path, O_WRONLY)) < 0) {
201 dprintf("Error reading attribute %s\n", sysattr->path);
205 length = write(fd, new_value, len);
207 dprintf("Error writing to the attribute %s - invalid value?\n",
211 } else if (length != len) {
212 dprintf("Could not write %d bytes to attribute %s\n",
215 * since we could not write user supplied number of bytes,
216 * restore the old value if one available
218 if (sysattr->method & SYSFS_METHOD_SHOW) {
219 length = write(fd, sysattr->value, sysattr->len);
226 * Validate length that has been copied. Alloc appropriate area
227 * in sysfs_attribute. Verify first if the attribute supports reading
228 * (show method). If it does not, do not bother
230 if (sysattr->method & SYSFS_METHOD_SHOW) {
231 if (length != sysattr->len) {
232 sysattr->value = (char *)realloc(sysattr->value,
234 sysattr->len = length;
235 strncpy(sysattr->value, new_value, length);
237 /*"length" of the new value is same as old one */
238 strncpy(sysattr->value, new_value, length);
248 * sysfs_read_attribute: reads value from attribute
249 * @sysattr: attribute to read
250 * returns 0 with success and -1 with error.
252 int sysfs_read_attribute(struct sysfs_attribute *sysattr)
254 unsigned char *fbuf = NULL;
255 unsigned char *vbuf = NULL;
260 if (sysattr == NULL) {
264 if (!(sysattr->method & SYSFS_METHOD_SHOW)) {
265 dprintf("Show method not supported for attribute %s\n",
272 pgsize = getpagesize();
274 fbuf = (unsigned char *)calloc(1, pgsize+1);
276 dprintf("calloc failed\n");
279 if ((fd = open(sysattr->path, O_RDONLY)) < 0) {
280 dprintf("Error reading attribute %s\n", sysattr->path);
284 length = read(fd, fbuf, pgsize);
286 dprintf("Error reading from attribute %s\n", sysattr->path);
291 sysattr->len = length;
293 vbuf = (unsigned char *)realloc(fbuf, length+1);
295 dprintf("realloc failed\n");
299 sysattr->value = vbuf;
305 * sysfs_read_attribute_value: given path to attribute, return its value.
306 * values can be up to a pagesize, if buffer is smaller the value will
308 * @attrpath: sysfs path to attribute
309 * @value: buffer to put value
310 * @vsize: size of value buffer
311 * returns 0 with success and -1 with error.
313 int sysfs_read_attribute_value(const unsigned char *attrpath,
314 unsigned char *value, size_t vsize)
316 struct sysfs_attribute *attr = NULL;
319 if (attrpath == NULL || value == NULL) {
324 attr = sysfs_open_attribute(attrpath);
326 dprintf("Invalid attribute path %s\n", attrpath);
330 if((sysfs_read_attribute(attr)) != 0 || attr->value == NULL) {
331 dprintf("Error reading from attribute %s\n", attrpath);
332 sysfs_close_attribute(attr);
335 length = strlen(attr->value);
337 dprintf("Value length %d is larger than supplied buffer %d\n",
339 strncpy(value, attr->value, vsize);
340 sysfs_close_attribute(attr);
346 * sysfs_get_value_from_attrbutes: given a linked list of attributes and an
347 * attribute name, return its value
348 * @attr: attribute to search
349 * @name: name to look for
350 * returns unsigned char * value - could be NULL
352 unsigned char *sysfs_get_value_from_attributes(struct dlist *attr,
353 const unsigned char *name)
355 struct sysfs_attribute *cur = NULL;
357 if (attr == NULL || name == NULL) {
361 dlist_for_each_data(attr, cur, struct sysfs_attribute) {
362 if (strcmp(cur->name, name) == 0)
369 * sysfs_close_link: closes and cleans up link.
370 * @ln: link to close.
372 void sysfs_close_link(struct sysfs_link *ln)
379 * sysfs_close_directory: closes directory, cleans up attributes and links
380 * @sysdir: sysfs_directory to close
382 void sysfs_close_directory(struct sysfs_directory *sysdir)
384 if (sysdir != NULL) {
385 if (sysdir->subdirs != NULL)
386 dlist_destroy(sysdir->subdirs);
387 if (sysdir->links != NULL)
388 dlist_destroy(sysdir->links);
389 if (sysdir->attributes != NULL)
390 dlist_destroy(sysdir->attributes);
396 * alloc_directory: allocates and initializes directory structure
397 * returns struct sysfs_directory with success or NULL with error.
399 static struct sysfs_directory *alloc_directory(void)
401 return (struct sysfs_directory *)
402 calloc(1, sizeof(struct sysfs_directory));
406 * alloc_link: allocates and initializes link structure
407 * returns struct sysfs_link with success or NULL with error.
409 static struct sysfs_link *alloc_link(void)
411 return (struct sysfs_link *)calloc(1, sizeof(struct sysfs_link));
415 * sysfs_read_all_subdirs: calls sysfs_read_directory for all subdirs
416 * @sysdir: directory whose subdirs need reading.
417 * returns 0 with success and -1 with error.
419 int sysfs_read_all_subdirs(struct sysfs_directory *sysdir)
421 struct sysfs_directory *cursub = NULL;
423 if (sysdir == NULL) {
427 if (sysdir->subdirs == NULL)
429 dlist_for_each_data(sysdir->subdirs, cursub, struct sysfs_directory) {
430 if (sysfs_read_directory(cursub) != 0)
431 dprintf ("Error reading subdirectory %s\n",
438 * sysfs_open_directory: opens a sysfs directory, creates dir struct, and
440 * @path: path of directory to open.
441 * returns: struct sysfs_directory * with success and NULL on error.
443 struct sysfs_directory *sysfs_open_directory(const unsigned char *path)
445 struct sysfs_directory *sdir = NULL;
451 sdir = alloc_directory();
453 dprintf("Error allocating directory %s\n", path);
456 if (sysfs_get_name_from_path(path, sdir->name, SYSFS_NAME_LEN) != 0) {
457 dprintf("Error getting directory name from path: %s\n", path);
458 sysfs_close_directory(sdir);
461 strncpy(sdir->path, path, sizeof(sdir->path));
467 * sysfs_open_link: opens a sysfs link, creates struct, and returns
468 * @path: path of link to open.
469 * returns: struct sysfs_link * with success and NULL on error.
471 struct sysfs_link *sysfs_open_link(const unsigned char *linkpath)
473 struct sysfs_link *ln = NULL;
475 if (linkpath == NULL || strlen(linkpath) > SYSFS_PATH_MAX) {
482 dprintf("Error allocating link %s\n", linkpath);
485 strcpy(ln->path, linkpath);
486 if ((sysfs_get_name_from_path(linkpath, ln->name, SYSFS_NAME_LEN)) != 0
487 || (sysfs_get_link(linkpath, ln->target, SYSFS_PATH_MAX)) != 0) {
489 dprintf("Invalid link path %s\n", linkpath);
497 * sysfs_read_directory: grabs attributes, links, and subdirectories
498 * @sysdir: sysfs directory to open
499 * returns 0 with success and -1 with error.
501 int sysfs_read_directory(struct sysfs_directory *sysdir)
504 struct dirent *dirent = NULL;
506 struct sysfs_attribute *attr = NULL;
507 struct sysfs_directory *subdir = NULL;
508 struct sysfs_link *ln = NULL;
509 unsigned char file_path[SYSFS_PATH_MAX];
512 if (sysdir == NULL) {
516 dir = opendir(sysdir->path);
518 dprintf("Error opening directory %s\n", sysdir->path);
521 while(((dirent = readdir(dir)) != NULL) && retval == 0) {
522 if (0 == strcmp(dirent->d_name, "."))
524 if (0 == strcmp(dirent->d_name, ".."))
526 memset(file_path, 0, SYSFS_PATH_MAX);
527 strncpy(file_path, sysdir->path, sizeof(file_path));
528 strncat(file_path, "/", sizeof(file_path));
529 strncat(file_path, dirent->d_name, sizeof(file_path));
530 if ((lstat(file_path, &astats)) != 0) {
531 dprintf("stat failed\n");
534 if (S_ISREG(astats.st_mode)) {
535 attr = sysfs_open_attribute(file_path);
537 dprintf("Error opening attribute %s\n",
542 if (attr->method & SYSFS_METHOD_SHOW) {
543 if ((sysfs_read_attribute(attr)) != 0) {
544 dprintf("Error reading attribute %s\n",
546 sysfs_close_attribute(attr);
551 if (sysdir->attributes == NULL) {
552 sysdir->attributes = dlist_new_with_delete
553 (sizeof(struct sysfs_attribute),
554 sysfs_del_attribute);
556 dlist_unshift(sysdir->attributes, attr);
557 } else if (S_ISDIR(astats.st_mode)) {
558 subdir = sysfs_open_directory(file_path);
559 if (subdir == NULL) {
560 dprintf("Error opening directory %s\n",
565 if (sysdir->subdirs == NULL)
566 sysdir->subdirs = dlist_new_with_delete
567 (sizeof(struct sysfs_directory),
568 sysfs_del_directory);
569 dlist_unshift(sysdir->subdirs, subdir);
570 } else if (S_ISLNK(astats.st_mode)) {
571 ln = sysfs_open_link(file_path);
573 dprintf("Error opening link %s\n", file_path);
577 if (sysdir->links == NULL)
578 sysdir->links = dlist_new_with_delete
579 (sizeof(struct sysfs_link),
581 dlist_unshift(sysdir->links, ln);
589 * sysfs_get_directory_attribute: retrieves attribute attrname
590 * @dir: directory to retrieve attribute from
591 * @attrname: name of attribute to look for
592 * returns sysfs_attribute if found and NULL if not found
594 struct sysfs_attribute *sysfs_get_directory_attribute
595 (struct sysfs_directory *dir, unsigned char *attrname)
597 struct sysfs_directory *sdir = NULL;
598 struct sysfs_attribute *attr = NULL;
600 if (dir == NULL || attrname == NULL) {
605 attr = (struct sysfs_attribute *)dlist_find_custom(dir->attributes,
606 attrname, dir_attribute_name_equal);
610 if (dir->subdirs != NULL) {
611 dlist_for_each_data(dir->subdirs, sdir,
612 struct sysfs_directory) {
613 if (sdir->attributes == NULL)
615 attr = sysfs_get_directory_attribute(sdir, attrname);
624 * sysfs_get_directory_link: retrieves link from one directory list
625 * @dir: directory to retrieve link from
626 * @linkname: name of link to look for
627 * returns reference to sysfs_link if found and NULL if not found
629 struct sysfs_link *sysfs_get_directory_link
630 (struct sysfs_directory *dir, unsigned char *linkname)
632 if (dir == NULL || linkname == NULL) {
636 return (struct sysfs_link *)dlist_find_custom(dir->links,
637 linkname, dir_link_name_equal);
641 * sysfs_get_subdirectory: retrieves subdirectory by name.
642 * @dir: directory to search for subdirectory.
643 * @subname: subdirectory name to get.
644 * returns reference to subdirectory or NULL if not found
646 struct sysfs_directory *sysfs_get_subdirectory(struct sysfs_directory *dir,
647 unsigned char *subname)
649 struct sysfs_directory *sub = NULL, *cursub = NULL;
651 if (dir == NULL || dir->subdirs == NULL || subname == NULL) {
655 sub = (struct sysfs_directory *)dlist_find_custom(dir->subdirs,
656 subname, dir_subdir_name_equal);
660 if (dir->subdirs != NULL) {
661 dlist_for_each_data(dir->subdirs, cursub,
662 struct sysfs_directory) {
663 if (cursub->subdirs == NULL)
665 sub = sysfs_get_subdirectory(cursub, subname);
674 * sysfs_get_subdirectory_link: looks through all subdirs for specific link.
675 * @dir: directory and subdirectories to search for link.
676 * @linkname: link name to get.
677 * returns reference to link or NULL if not found
679 struct sysfs_link *sysfs_get_subdirectory_link(struct sysfs_directory *dir,
680 unsigned char *linkname)
682 struct sysfs_directory *cursub = NULL;
683 struct sysfs_link *ln = NULL;
685 if (dir == NULL || dir->links == NULL || linkname == NULL) {
690 ln = sysfs_get_directory_link(dir, linkname);
694 if (dir->subdirs == NULL)
697 if (dir->subdirs != NULL) {
698 dlist_for_each_data(dir->subdirs, cursub,
699 struct sysfs_directory) {
700 if (cursub->subdirs == NULL)
702 ln = sysfs_get_subdirectory_link(cursub, linkname);