chiark / gitweb /
ac2ecfcbe14bd056fa565b5f3cccf82125a63fb9
[elogind.git] / libsysfs / sysfs_dir.c
1 /*
2  * syfs_dir.c
3  *
4  * Directory utility functions for libsysfs
5  *
6  * Copyright (C) IBM Corp. 2003
7  *
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.
12  *
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.
17  *
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
21  *
22  */
23 #include "libsysfs.h"
24 #include "sysfs.h"
25
26 /**
27  * sysfs_del_attribute: routine for dlist integration
28  */
29 static void sysfs_del_attribute(void *attr)
30 {
31         sysfs_close_attribute((struct sysfs_attribute *)attr);
32 }
33
34 /**
35  * sysfs_del_link: routine for dlist integration
36  */
37 static void sysfs_del_link(void *ln)
38 {
39         sysfs_close_link((struct sysfs_link *)ln);
40 }
41
42 /**
43  * sysfs_del_dir: routine for dlist integration
44  */
45 static void sysfs_del_directory(void *dir)
46 {
47         sysfs_close_directory((struct sysfs_directory *)dir);
48 }
49
50 /**
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
55  */
56 static int dir_attribute_name_equal(void *a, void *b)
57 {
58         if (a == NULL || b == NULL)
59                 return 0;
60
61         if (strcmp(((unsigned char *)a), ((struct sysfs_attribute *)b)->name) 
62             == 0)
63                 return 1;
64         return 0;
65 }
66
67 /**
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
72  */
73 static int dir_link_name_equal(void *a, void *b)
74 {
75         if (a == NULL || b == NULL)
76                 return 0;
77
78         if (strcmp(((unsigned char *)a), ((struct sysfs_link *)b)->name) 
79             == 0)
80                 return 1;
81         return 0;
82 }
83
84 /**
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
89  */
90 static int dir_subdir_name_equal(void *a, void *b)
91 {
92         if (a == NULL || b == NULL)
93                 return 0;
94
95         if (strcmp(((unsigned char *)a), ((struct sysfs_directory *)b)->name)
96             == 0)
97                 return 1;
98         return 0;
99 }
100
101 /**
102  * sysfs_close_attribute: closes and cleans up attribute
103  * @sysattr: attribute to close.
104  */
105 void sysfs_close_attribute(struct sysfs_attribute *sysattr)
106 {
107         if (sysattr != NULL) {
108                 if (sysattr->value != NULL)
109                         free(sysattr->value);
110                 free(sysattr);
111         }
112 }
113
114 /**
115  * alloc_attribute: allocates and initializes attribute structure
116  * returns struct sysfs_attribute with success and NULL with error.
117  */
118 static struct sysfs_attribute *alloc_attribute(void)
119 {
120         return (struct sysfs_attribute *)
121                         calloc(1, sizeof(struct sysfs_attribute));
122 }
123
124 /**
125  * sysfs_open_attribute: creates sysfs_attribute structure
126  * @path: path to attribute.
127  * returns sysfs_attribute struct with success and NULL with error.
128  */
129 struct sysfs_attribute *sysfs_open_attribute(const unsigned char *path)
130 {
131         struct sysfs_attribute *sysattr = NULL;
132         struct stat fileinfo;
133         
134         if (path == NULL) {
135                 errno = EINVAL;
136                 return NULL;
137         }
138         sysattr = alloc_attribute();
139         if (sysattr == NULL) {
140                 dprintf("Error allocating attribute at %s\n", path);
141                 return NULL;
142         }
143         if (sysfs_get_name_from_path(path, sysattr->name, SYSFS_NAME_LEN) 
144             != 0) {
145                 dprintf("Error retrieving attribute name from path: %s\n", 
146                         path);
147                 sysfs_close_attribute(sysattr);
148                 return NULL;
149         }
150         strncpy(sysattr->path, path, sizeof(sysattr->path));
151         if ((stat(sysattr->path, &fileinfo)) != 0) {
152                 dprintf("Stat failed: No such attribute?\n");
153                 sysattr->method = 0;
154                 free(sysattr);
155                 sysattr = NULL;
156         } else {
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;
161         }
162
163         return sysattr;
164 }
165
166 /**
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.
172  */
173 int sysfs_write_attribute(struct sysfs_attribute *sysattr,
174                 const unsigned char *new_value, size_t len)
175 {
176         int fd;
177         int length;
178         
179         if (sysattr == NULL || new_value == NULL || len == 0) {
180                 errno = EINVAL;
181                 return -1;
182         }
183         
184         if (!(sysattr->method & SYSFS_METHOD_STORE)) {
185                 dprintf ("Store method not supported for attribute %s\n",
186                         sysattr->path);
187                 return -1;
188         }
189         if (sysattr->method & SYSFS_METHOD_SHOW) {
190                 /*
191                  * read attribute again to see if we can get an updated value 
192                  */
193                 if ((sysfs_read_attribute(sysattr)) != 0) {
194                         dprintf("Error reading attribute\n");
195                         return -1;
196                 }
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);
200                         return 0;       
201                 }
202         }
203         /* 
204          * open O_WRONLY since some attributes have no "read" but only
205          * "write" permission 
206          */ 
207         if ((fd = open(sysattr->path, O_WRONLY)) < 0) {
208                 dprintf("Error reading attribute %s\n", sysattr->path);
209                 return -1;
210         }
211
212         length = write(fd, new_value, len);
213         if (length < 0) {
214                 dprintf("Error writing to the attribute %s - invalid value?\n",
215                         sysattr->name);
216                 close(fd);
217                 return -1;
218         } else if (length != len) {
219                 dprintf("Could not write %d bytes to attribute %s\n", 
220                                         len, sysattr->name);
221                 /* 
222                  * since we could not write user supplied number of bytes,
223                  * restore the old value if one available
224                  */
225                 if (sysattr->method & SYSFS_METHOD_SHOW) {
226                         length = write(fd, sysattr->value, sysattr->len);
227                         close(fd);
228                         return -1;
229                 }
230         }
231         
232         /*
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
236          */ 
237         if (sysattr->method & SYSFS_METHOD_SHOW) {
238                 if (length != sysattr->len) {
239                         sysattr->value = (char *)realloc(sysattr->value, 
240                                                                 length);
241                         sysattr->len = length;
242                         strncpy(sysattr->value, new_value, length);
243                 } else {
244                         /*"length" of the new value is same as old one */ 
245                         strncpy(sysattr->value, new_value, length);
246                 }
247         }
248                         
249         close(fd);      
250         return 0;
251 }
252
253
254 /**
255  * sysfs_read_attribute: reads value from attribute
256  * @sysattr: attribute to read
257  * returns 0 with success and -1 with error.
258  */
259 int sysfs_read_attribute(struct sysfs_attribute *sysattr)
260 {
261         unsigned char *fbuf = NULL;
262         unsigned char *vbuf = NULL;
263         size_t length = 0;
264         long pgsize = 0;
265         int fd;
266
267         if (sysattr == NULL) {
268                 errno = EINVAL;
269                 return -1;
270         }
271         if (!(sysattr->method & SYSFS_METHOD_SHOW)) {
272                 dprintf("Show method not supported for attribute %s\n",
273                         sysattr->path);
274                 return -1;
275         }
276 #ifdef __KLIBC__
277         pgsize = 0x4000;
278 #else
279         pgsize = sysconf(_SC_PAGESIZE);
280 #endif
281         fbuf = (unsigned char *)calloc(1, pgsize+1);
282         if (fbuf == NULL) {
283                 dprintf("calloc failed\n");
284                 return -1;
285         }
286         if ((fd = open(sysattr->path, O_RDONLY)) < 0) {
287                 dprintf("Error reading attribute %s\n", sysattr->path);
288                 free(fbuf);
289                 return -1;
290         }
291         length = read(fd, fbuf, pgsize);
292         if (length < 0) {
293                 dprintf("Error reading from attribute %s\n", sysattr->path);
294                 close(fd);
295                 free(fbuf);
296                 return -1;
297         }
298         if (sysattr->len > 0) {
299                 if ((sysattr->len == length) && 
300                                 (!(strncmp(sysattr->value, fbuf, length)))) {
301                         close(fd);
302                         return 0;
303                 }
304                 free(sysattr->value);
305         }
306         sysattr->len = length;
307         close(fd);
308         vbuf = (unsigned char *)realloc(fbuf, length+1);
309         if (vbuf == NULL) {
310                 dprintf("realloc failed\n");
311                 free(fbuf);
312                 return -1;
313         }
314         sysattr->value = vbuf;
315
316         return 0;
317 }
318
319 /**
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 
322  *      be truncated. 
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.
327  */
328 int sysfs_read_attribute_value(const unsigned char *attrpath, 
329                                         unsigned char *value, size_t vsize)
330 {
331         struct sysfs_attribute *attr = NULL;
332         size_t length = 0;
333
334         if (attrpath == NULL || value == NULL) {
335                 errno = EINVAL;
336                 return -1;
337         }
338
339         attr = sysfs_open_attribute(attrpath);
340         if (attr == NULL) {
341                 dprintf("Invalid attribute path %s\n", attrpath);
342                 errno = EINVAL;
343                 return -1;
344         }
345         if((sysfs_read_attribute(attr)) != 0 || attr->value == NULL) {
346                 dprintf("Error reading from attribute %s\n", attrpath);
347                 sysfs_close_attribute(attr);
348                 return -1;
349         }
350         length = strlen(attr->value);
351         if (length > vsize) 
352                 dprintf("Value length %d is larger than supplied buffer %d\n",
353                         length, vsize);
354         strncpy(value, attr->value, vsize);
355         sysfs_close_attribute(attr);
356
357         return 0;
358 }
359
360 /**
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
366  */
367 unsigned char *sysfs_get_value_from_attributes(struct dlist *attr, 
368                                         const unsigned char *name)
369 {       
370         struct sysfs_attribute *cur = NULL;
371         
372         if (attr == NULL || name == NULL) {
373                 errno = EINVAL;
374                 return NULL;
375         }
376         dlist_for_each_data(attr, cur, struct sysfs_attribute) {
377                 if (strcmp(cur->name, name) == 0)
378                         return cur->value;
379         }
380         return NULL;
381 }
382
383 /**
384  * sysfs_close_link: closes and cleans up link.
385  * @ln: link to close.
386  */
387 void sysfs_close_link(struct sysfs_link *ln)
388 {
389         if (ln != NULL) 
390                 free(ln);
391 }
392
393 /**
394  * sysfs_close_directory: closes directory, cleans up attributes and links
395  * @sysdir: sysfs_directory to close
396  */
397 void sysfs_close_directory(struct sysfs_directory *sysdir)
398 {
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);
406                 free(sysdir);
407                 sysdir = NULL;
408         }
409 }
410
411 /**
412  * alloc_directory: allocates and initializes directory structure
413  * returns struct sysfs_directory with success or NULL with error.
414  */
415 static struct sysfs_directory *alloc_directory(void)
416 {
417         return (struct sysfs_directory *)
418                         calloc(1, sizeof(struct sysfs_directory));
419 }
420
421 /**
422  * alloc_link: allocates and initializes link structure
423  * returns struct sysfs_link with success or NULL with error.
424  */
425 static struct sysfs_link *alloc_link(void)
426 {
427         return (struct sysfs_link *)calloc(1, sizeof(struct sysfs_link));
428 }
429
430 /**
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.
434  */
435 int sysfs_read_all_subdirs(struct sysfs_directory *sysdir)
436 {
437         struct sysfs_directory *cursub = NULL;
438
439         if (sysdir == NULL) {
440                 errno = EINVAL;
441                 return -1;
442         }
443         if (sysdir->subdirs == NULL) 
444                 if ((sysfs_read_dir_subdirs(sysdir) != 0) 
445                     || sysdir->subdirs == NULL)
446                         return 0;
447         dlist_for_each_data(sysdir->subdirs, cursub, struct sysfs_directory) {
448                 if ((sysfs_read_directory(cursub)) != 0) 
449                         dprintf ("Error reading subdirectory %s\n",
450                                 cursub->name);
451         }
452         return 0;
453 }
454
455 /**
456  * sysfs_open_directory: opens a sysfs directory, creates dir struct, and
457  *              returns.
458  * @path: path of directory to open.
459  * returns: struct sysfs_directory * with success and NULL on error.
460  */
461 struct sysfs_directory *sysfs_open_directory(const unsigned char *path)
462 {
463         struct sysfs_directory *sdir = NULL;
464
465         if (path == NULL) {
466                 errno = EINVAL;
467                 return NULL;
468         }
469         sdir = alloc_directory();
470         if (sdir == NULL) {
471                 dprintf("Error allocating directory %s\n", path);
472                 return NULL;
473         }
474         if (sysfs_get_name_from_path(path, sdir->name, SYSFS_NAME_LEN) != 0) {
475                 dprintf("Error getting directory name from path: %s\n", path);
476                 sysfs_close_directory(sdir);
477                 return NULL;
478         }
479         strncpy(sdir->path, path, sizeof(sdir->path));
480
481         return sdir;
482 }
483
484 /**
485  * sysfs_open_link: opens a sysfs link, creates struct, and returns
486  * @path: path of link to open.
487  * returns: struct sysfs_link * with success and NULL on error.
488  */
489 struct sysfs_link *sysfs_open_link(const unsigned char *linkpath)
490 {
491         struct sysfs_link *ln = NULL;
492
493         if (linkpath == NULL || strlen(linkpath) > SYSFS_PATH_MAX) {
494                 errno = EINVAL;
495                 return NULL;
496         }
497
498         ln = alloc_link();
499         if (ln == NULL) {
500                 dprintf("Error allocating link %s\n", linkpath);
501                 return NULL;
502         }
503         strcpy(ln->path, linkpath);
504         if ((sysfs_get_name_from_path(linkpath, ln->name, SYSFS_NAME_LEN)) != 0
505             || (sysfs_get_link(linkpath, ln->target, SYSFS_PATH_MAX)) != 0) {
506                 errno = EINVAL;
507                 dprintf("Invalid link path %s\n", linkpath);
508                 return NULL;
509         }
510
511         return ln;
512 }
513
514 /**
515  * sysfs_refresh_attributes: Refresh attributes list
516  * @attrlist: list of attributes to refresh
517  * Returns 0 on success, 1 on failure
518  */
519 int sysfs_refresh_attributes(struct dlist *attrlist)
520 {
521         struct sysfs_attribute *attr = NULL;
522
523         if (attrlist == NULL) {
524                 errno = EINVAL;
525                 return 1;
526         }
527         dlist_for_each_data(attrlist, attr, struct sysfs_attribute) {
528                 if (attr->method & SYSFS_METHOD_SHOW) {
529                         if ((sysfs_read_attribute(attr)) != 0) {
530                                 dprintf("Error reading attribute %s\n", attr->path);
531                                 if ((sysfs_path_is_file(attr->path)) != 0) {
532                                         dprintf("Attr %s no longer exists\n", 
533                                                                 attr->name);
534                                 }
535                         }
536                 } else {
537                         if ((sysfs_path_is_file(attr->path)) != 0) {
538                                 dprintf("Attr %s no longer exists\n", 
539                                                                 attr->name);
540                         }
541                 }
542         }
543         if (attrlist->count == 0) {
544                 dprintf("No attributes in the list, destroying list now\n");
545                 dlist_destroy(attrlist);
546                 attrlist = NULL;
547                 return 1;
548         }
549         return 0;
550 }
551
552 /**
553  * add_attribute: open and add attribute at path to given directory
554  * @sysdir: directory to add attribute to
555  * @path: path to attribute
556  * returns 0 with success and -1 with error.
557  */
558 static int add_attribute(struct sysfs_directory *sysdir, 
559                                         const unsigned char *path)
560 {
561         struct sysfs_attribute *attr = NULL;
562
563         attr = sysfs_open_attribute(path);
564         if (attr == NULL) {
565                 dprintf("Error opening attribute %s\n", path);
566                 return -1;
567         }
568         if (attr->method & SYSFS_METHOD_SHOW) {
569                 if ((sysfs_read_attribute(attr)) != 0) {
570                         dprintf("Error reading attribute %s\n", path);
571                         sysfs_close_attribute(attr);
572                         return 0;
573                 }
574         }
575                                                 
576         if (sysdir->attributes == NULL) {
577                 sysdir->attributes = dlist_new_with_delete
578                         (sizeof(struct sysfs_attribute), sysfs_del_attribute);
579         }
580         dlist_unshift(sysdir->attributes, attr);
581
582         return 0;
583 }
584
585 /**
586  * add_subdirectory: open and add subdirectory at path to given directory
587  * @sysdir: directory to add subdir to
588  * @path: path to subdirectory
589  * returns 0 with success and -1 with error.
590  */
591 static int add_subdirectory(struct sysfs_directory *sysdir, 
592                                         const unsigned char *path)
593 {
594         struct sysfs_directory *subdir = NULL;
595
596         subdir = sysfs_open_directory(path);
597         if (subdir == NULL) {
598                 dprintf("Error opening directory %s\n", path);
599                 return -1;
600         }
601         if (sysdir->subdirs == NULL)
602                 sysdir->subdirs = dlist_new_with_delete
603                         (sizeof(struct sysfs_directory), sysfs_del_directory);
604         dlist_unshift(sysdir->subdirs, subdir);
605         return 0;
606 }
607
608 /**
609  * add_link: open and add link at path to given directory
610  * @sysdir: directory to add link to
611  * @path: path to link
612  * returns 0 with success and -1 with error.
613  */
614 static int add_link(struct sysfs_directory *sysdir, const unsigned char *path)
615 {
616         struct sysfs_link *ln = NULL;
617
618         ln = sysfs_open_link(path);
619         if (ln == NULL) {
620                 dprintf("Error opening link %s\n", path);
621                 return -1;
622         }
623         if (sysdir->links == NULL)
624                 sysdir->links = dlist_new_with_delete
625                                 (sizeof(struct sysfs_link), sysfs_del_link);
626         dlist_unshift(sysdir->links, ln);
627         return 0;
628 }
629
630 /**
631  * sysfs_read_dir_attributes: grabs attributes for the given directory
632  * @sysdir: sysfs directory to open
633  * returns 0 with success and -1 with error.
634  */
635 int sysfs_read_dir_attributes(struct sysfs_directory *sysdir)
636 {
637         DIR *dir = NULL;
638         struct dirent *dirent = NULL;
639         struct stat astats;
640         unsigned char file_path[SYSFS_PATH_MAX];
641         int retval = 0;
642
643         if (sysdir == NULL) {
644                 errno = EINVAL;
645                 return -1;
646         }
647         dir = opendir(sysdir->path);
648         if (dir == NULL) {
649                 dprintf("Error opening directory %s\n", sysdir->path);
650                 return -1;
651         }
652         while(((dirent = readdir(dir)) != NULL) && retval == 0) {
653                 if (0 == strcmp(dirent->d_name, "."))
654                          continue;
655                 if (0 == strcmp(dirent->d_name, ".."))
656                         continue;
657                 memset(file_path, 0, SYSFS_PATH_MAX);
658                 strncpy(file_path, sysdir->path, sizeof(file_path));
659                 strncat(file_path, "/", sizeof(file_path));
660                 strncat(file_path, dirent->d_name, sizeof(file_path));
661                 if ((lstat(file_path, &astats)) != 0) {
662                         dprintf("stat failed\n");
663                         continue;
664                 }
665                 if (S_ISREG(astats.st_mode)) 
666                         retval = add_attribute(sysdir, file_path);
667         }
668         closedir(dir);
669         return(retval);
670 }
671
672 /**
673  * sysfs_read_dir_links: grabs links in a specific directory
674  * @sysdir: sysfs directory to read links
675  * returns 0 with success and -1 with error.
676  */
677 int sysfs_read_dir_links(struct sysfs_directory *sysdir)
678 {
679         DIR *dir = NULL;
680         struct dirent *dirent = NULL;
681         struct stat astats;
682         unsigned char file_path[SYSFS_PATH_MAX];
683         int retval = 0;
684
685         if (sysdir == NULL) {
686                 errno = EINVAL;
687                 return -1;
688         }
689         dir = opendir(sysdir->path);
690         if (dir == NULL) {
691                 dprintf("Error opening directory %s\n", sysdir->path);
692                 return -1;
693         }
694         while(((dirent = readdir(dir)) != NULL) && retval == 0) {
695                 if (0 == strcmp(dirent->d_name, "."))
696                          continue;
697                 if (0 == strcmp(dirent->d_name, ".."))
698                         continue;
699                 memset(file_path, 0, SYSFS_PATH_MAX);
700                 strncpy(file_path, sysdir->path, sizeof(file_path));
701                 strncat(file_path, "/", sizeof(file_path));
702                 strncat(file_path, dirent->d_name, sizeof(file_path));
703                 if ((lstat(file_path, &astats)) != 0) {
704                         dprintf("stat failed\n");
705                         continue;
706                 }
707                 if (S_ISLNK(astats.st_mode)) {
708                         retval = add_link(sysdir, file_path);
709                         if (retval != 0)
710                                 break;
711                 }
712         }
713         closedir(dir);
714         return(retval);
715 }
716
717 /**
718  * sysfs_read_dir_subdirs: grabs subdirs in a specific directory
719  * @sysdir: sysfs directory to read links
720  * returns 0 with success and -1 with error.
721  */
722 int sysfs_read_dir_subdirs(struct sysfs_directory *sysdir)
723 {
724         DIR *dir = NULL;
725         struct dirent *dirent = NULL;
726         struct stat astats;
727         unsigned char file_path[SYSFS_PATH_MAX];
728         int retval = 0;
729
730         if (sysdir == NULL) {
731                 errno = EINVAL;
732                 return -1;
733         }
734         dir = opendir(sysdir->path);
735         if (dir == NULL) {
736                 dprintf("Error opening directory %s\n", sysdir->path);
737                 return -1;
738         }
739         while(((dirent = readdir(dir)) != NULL) && retval == 0) {
740                 if (0 == strcmp(dirent->d_name, "."))
741                          continue;
742                 if (0 == strcmp(dirent->d_name, ".."))
743                         continue;
744                 memset(file_path, 0, SYSFS_PATH_MAX);
745                 strncpy(file_path, sysdir->path, sizeof(file_path));
746                 strncat(file_path, "/", sizeof(file_path));
747                 strncat(file_path, dirent->d_name, sizeof(file_path));
748                 if ((lstat(file_path, &astats)) != 0) {
749                         dprintf("stat failed\n");
750                         continue;
751                 }
752                 if (S_ISDIR(astats.st_mode))
753                         retval = add_subdirectory(sysdir, file_path);
754         }
755         closedir(dir);
756         return(retval);
757 }
758
759 /**
760  * sysfs_read_directory: grabs attributes, links, and subdirectories
761  * @sysdir: sysfs directory to open
762  * returns 0 with success and -1 with error.
763  */
764 int sysfs_read_directory(struct sysfs_directory *sysdir)
765 {
766         DIR *dir = NULL;
767         struct dirent *dirent = NULL;
768         struct stat astats;
769         unsigned char file_path[SYSFS_PATH_MAX];
770         int retval = 0;
771
772         if (sysdir == NULL) {
773                 errno = EINVAL;
774                 return -1;
775         }
776         dir = opendir(sysdir->path);
777         if (dir == NULL) {
778                 dprintf("Error opening directory %s\n", sysdir->path);
779                 return -1;
780         }
781         while(((dirent = readdir(dir)) != NULL) && retval == 0) {
782                 if (0 == strcmp(dirent->d_name, "."))
783                          continue;
784                 if (0 == strcmp(dirent->d_name, ".."))
785                         continue;
786                 memset(file_path, 0, SYSFS_PATH_MAX);
787                 strncpy(file_path, sysdir->path, sizeof(file_path));
788                 strncat(file_path, "/", sizeof(file_path));
789                 strncat(file_path, dirent->d_name, sizeof(file_path));
790                 if ((lstat(file_path, &astats)) != 0) {
791                         dprintf("stat failed\n");
792                         continue;
793                 }
794                 if (S_ISDIR(astats.st_mode)) 
795                         retval = add_subdirectory(sysdir, file_path);
796
797                 else if (S_ISLNK(astats.st_mode))
798                         retval = add_link(sysdir, file_path);
799
800                 else if (S_ISREG(astats.st_mode))
801                         retval = add_attribute(sysdir, file_path);
802         }
803         closedir(dir);
804         return(retval);
805 }
806
807 /**
808  * sysfs_get_directory_attribute: retrieves attribute attrname from current
809  *      directory only
810  * @dir: directory to retrieve attribute from
811  * @attrname: name of attribute to look for
812  * returns sysfs_attribute if found and NULL if not found
813  */
814 struct sysfs_attribute *sysfs_get_directory_attribute
815                         (struct sysfs_directory *dir, unsigned char *attrname)
816 {
817         struct sysfs_attribute *attr = NULL;
818         unsigned char new_path[SYSFS_PATH_MAX];
819         
820         if (dir == NULL || attrname == NULL) {
821                 errno = EINVAL;
822                 return NULL;
823         }
824
825         if (dir->attributes == NULL) 
826                 if ((sysfs_read_dir_attributes(dir) != 0) 
827                     || (dir->attributes == NULL))
828                         return NULL;
829
830         attr = (struct sysfs_attribute *)dlist_find_custom
831                         (dir->attributes, attrname, dir_attribute_name_equal);
832         if (attr != NULL) {
833                 /*
834                  * don't read here since we would have read the attribute in 
835                  * in the routine that called this routine
836                  */ 
837                 return attr;
838         } else {
839                 memset(new_path, 0, SYSFS_PATH_MAX);
840                 strcpy(new_path, dir->path);
841                 strcat(new_path, "/");
842                 strcat(new_path, attrname);
843                 if ((sysfs_path_is_file(new_path)) == 0) {
844                         if ((add_attribute(dir, new_path)) == 0) {
845                                 attr = (struct sysfs_attribute *)dlist_find_custom
846                                         (dir->attributes, attrname, dir_attribute_name_equal);
847                         }
848                         return attr;
849                 }
850         }
851         return NULL;
852 }
853
854 /**
855  * sysfs_get_directory_link: retrieves link from one directory list
856  * @dir: directory to retrieve link from
857  * @linkname: name of link to look for
858  * returns reference to sysfs_link if found and NULL if not found
859  */
860 struct sysfs_link *sysfs_get_directory_link
861                         (struct sysfs_directory *dir, unsigned char *linkname)
862 {
863         if (dir == NULL || linkname == NULL) {
864                 errno = EINVAL;
865                 return NULL;
866         }
867         if (dir->links == NULL)
868                 if ((sysfs_read_dir_links(dir) != 0) || (dir->links == NULL))
869                         return NULL;
870
871         return (struct sysfs_link *)dlist_find_custom(dir->links,
872                 linkname, dir_link_name_equal);
873 }
874
875 /**
876  * sysfs_get_subdirectory: retrieves subdirectory by name.
877  * @dir: directory to search for subdirectory.
878  * @subname: subdirectory name to get.
879  * returns reference to subdirectory or NULL if not found
880  */
881 struct sysfs_directory *sysfs_get_subdirectory(struct sysfs_directory *dir,
882                                                 unsigned char *subname)
883 {
884         struct sysfs_directory *sub = NULL, *cursub = NULL;
885
886         if (dir == NULL || subname == NULL) {
887                 errno = EINVAL;
888                 return NULL;
889         }
890
891         if (dir->subdirs == NULL)
892                 if (sysfs_read_dir_subdirs(dir) != 0)
893                         return NULL;
894
895         sub = (struct sysfs_directory *)dlist_find_custom(dir->subdirs,
896                 subname, dir_subdir_name_equal);
897         if (sub != NULL) 
898                 return sub;
899
900         if (dir->subdirs != NULL) {
901                 dlist_for_each_data(dir->subdirs, cursub, 
902                                         struct sysfs_directory) {
903                         if (cursub->subdirs == NULL) {
904                                 if (sysfs_read_dir_subdirs(cursub) != 0)
905                                         continue;
906                                 if (cursub->subdirs == NULL)
907                                         continue;
908                         }
909                         sub = sysfs_get_subdirectory(cursub, subname);
910                         if (sub != NULL)
911                                 return sub;
912                 }
913         }
914         return NULL;
915 }
916
917 /**
918  * sysfs_get_subdirectory_link: looks through all subdirs for specific link.
919  * @dir: directory and subdirectories to search for link.
920  * @linkname: link name to get.
921  * returns reference to link or NULL if not found
922  */
923 struct sysfs_link *sysfs_get_subdirectory_link(struct sysfs_directory *dir,
924                                                 unsigned char *linkname)
925 {
926         struct sysfs_directory *cursub = NULL;
927         struct sysfs_link *ln = NULL;
928
929         if (dir == NULL || linkname == NULL) {
930                 errno = EINVAL;
931                 return NULL;
932         }
933
934         ln = sysfs_get_directory_link(dir, linkname);
935         if (ln != NULL)
936                 return ln;
937
938         if (dir->subdirs == NULL) 
939                 if (sysfs_read_dir_subdirs(dir) != 0)
940                         return NULL;
941
942         if (dir->subdirs != NULL) {
943                 dlist_for_each_data(dir->subdirs, cursub, 
944                                                 struct sysfs_directory) {
945                         ln = sysfs_get_subdirectory_link(cursub, linkname);
946                         if (ln != NULL)
947                                 return ln;
948                 }
949         }
950         return NULL;
951 }