4 * Directory utility functions for libsysfs
6 * Copyright (C) 2003 International Business Machines, Inc.
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_close_attribute: closes and cleans up attribute
28 * @sysattr: attribute to close.
30 void sysfs_close_attribute(struct sysfs_attribute *sysattr)
32 if (sysattr != NULL) {
33 if (sysattr->value != NULL)
40 * alloc_attribute: allocates and initializes attribute structure
41 * returns struct sysfs_attribute with success and NULL with error.
43 static struct sysfs_attribute *alloc_attribute(void)
45 return (struct sysfs_attribute *)
46 calloc(1, sizeof(struct sysfs_attribute));
50 * sysfs_open_attribute: creates sysfs_attribute structure
51 * @path: path to attribute.
52 * returns sysfs_attribute struct with success and NULL with error.
54 struct sysfs_attribute *sysfs_open_attribute(const char *path)
56 struct sysfs_attribute *sysattr = NULL;
63 sysattr = alloc_attribute();
64 if (sysattr == NULL) {
65 dprintf(stderr, "Error allocating attribute at %s\n", path);
68 strncpy(sysattr->path, path, sizeof(sysattr->path));
69 if ((stat(sysattr->path, &fileinfo)) != 0) {
73 if (fileinfo.st_mode & S_IRUSR)
74 sysattr->method |= SYSFS_METHOD_SHOW;
75 if (fileinfo.st_mode & S_IWUSR)
76 sysattr->method |= SYSFS_METHOD_STORE;
83 * sysfs_read_attribute: reads value from attribute
84 * @sysattr: attribute to read
85 * returns 0 with success and -1 with error.
87 int sysfs_read_attribute(struct sysfs_attribute *sysattr)
95 if (sysattr == NULL) {
99 if (!(sysattr->method & SYSFS_METHOD_SHOW)) {
100 dprintf (stderr, "Show method not supported for attribute %s\n",
104 pgsize = getpagesize();
105 fbuf = (char *)calloc(1, pgsize+1);
110 if ((fd = open(sysattr->path, O_RDONLY)) < 0) {
111 dprintf (stderr, "Error reading attribute %s\n", sysattr->path);
115 length = read(fd, fbuf, pgsize);
117 dprintf (stderr, "Error reading from attribute %s\n",
123 sysattr->len = length;
125 vbuf = (char *)realloc(fbuf, length+1);
131 sysattr->value = vbuf;
137 * sysfs_read_attribute_value: given path to attribute, return its value.
138 * values can be up to a pagesize, if buffer is smaller the value will
140 * @attrpath: sysfs path to attribute
141 * @value: buffer to put value
142 * @vsize: size of value buffer
143 * returns 0 with success and -1 with error.
145 int sysfs_read_attribute_value(const char *attrpath, char *value, size_t vsize)
147 struct sysfs_attribute *attr = NULL;
150 if (attrpath == NULL || value == NULL) {
155 attr = sysfs_open_attribute(attrpath);
157 dprintf(stderr, "Invalid attribute path %s\n", attrpath);
161 if((sysfs_read_attribute(attr)) != 0 || attr->value == NULL) {
162 dprintf(stderr, "Error reading from attribute %s\n", attrpath);
163 sysfs_close_attribute(attr);
166 length = strlen(attr->value);
169 "Value length %d is larger than supplied buffer %d\n",
171 strncpy(value, attr->value, vsize);
172 sysfs_close_attribute(attr);
178 * sysfs_get_value_from_attrbutes: given a linked list of attributes and an
179 * attribute name, return its value
180 * @attr: attribute to search
181 * @name: name to look for
182 * returns char * value - could be NULL
184 char *sysfs_get_value_from_attributes(struct sysfs_attribute *attr,
187 struct sysfs_attribute *cur = NULL;
188 char tmpname[SYSFS_NAME_LEN];
190 if (attr == NULL || name == NULL) {
195 while (cur != NULL) {
196 memset(tmpname, 0, SYSFS_NAME_LEN);
197 if ((sysfs_get_name_from_path(cur->path, tmpname,
198 SYSFS_NAME_LEN)) != 0) {
202 if (strcmp(tmpname, name) == 0)
210 * add_subdir_to_dir: adds subdirectory to directory's subdirs
211 * @sysdir: directory to add subdir to
212 * @subdir: subdirectory to add.
214 static void add_subdir_to_dir(struct sysfs_directory *sysdir,
215 struct sysfs_directory *subdir)
217 if (sysdir != NULL && subdir != NULL) {
218 subdir->next = sysdir->subdirs;
219 sysdir->subdirs = subdir;
224 * add_attr_to_dir: adds attribute to directory's attributes
225 * @sysdir: directory to add attribute to
226 * @sysattr: attribute to add.
228 static void add_attr_to_dir(struct sysfs_directory *sysdir,
229 struct sysfs_attribute *sysattr)
231 if (sysdir != NULL && sysattr != NULL) {
232 sysattr->next = sysdir->attributes;
233 sysdir->attributes = sysattr;
238 * sysfs_close_dlink: closes and cleans up directory link.
239 * @dlink: directory link to close.
241 void sysfs_close_dlink(struct sysfs_dlink *dlink)
245 if (dlink->target != NULL)
246 sysfs_close_directory(dlink->target);
252 * add_dlink_to_dir: adds directory link to directory's links list.
253 * @sysdir: directory to add it to.
254 * @dlink: link to add.
256 static void add_dlink_to_dir(struct sysfs_directory *sysdir,
257 struct sysfs_dlink *dlink)
259 if (sysdir != NULL && dlink != NULL) {
260 dlink->next = sysdir->links;
261 sysdir->links = dlink;
266 * sysfs_close_directory: closes directory, cleans up attributes and links
267 * @sysdir: sysfs_directory to close
269 void sysfs_close_directory(struct sysfs_directory *sysdir)
271 struct sysfs_directory *sdir = NULL, *dnext = NULL;
272 struct sysfs_dlink *dlink = NULL, *nextl = NULL;
273 struct sysfs_attribute *attr = NULL, *anext = NULL;
275 if (sysdir != NULL) {
276 if (sysdir->subdirs != NULL) {
277 for (sdir = sysdir->subdirs; sdir != NULL;
280 sysfs_close_directory(sdir);
283 if (sysdir->links != NULL) {
284 for (dlink = sysdir->links; dlink != NULL;
287 sysfs_close_dlink(dlink);
290 if (sysdir->attributes != NULL) {
291 for (attr = sysdir->attributes; attr != NULL;
294 /* sysfs_close_attribute(attr); */
295 if (attr->value != NULL)
305 * alloc_directory: allocates and initializes directory structure
306 * returns struct sysfs_directory with success or NULL with error.
308 static struct sysfs_directory *alloc_directory(void)
310 return (struct sysfs_directory *)
311 calloc(1, sizeof(struct sysfs_directory));
315 * alloc_dlink: allocates and initializes directory link structure
316 * returns struct sysfs_dlink with success or NULL with error.
318 static struct sysfs_dlink *alloc_dlink(void)
320 return (struct sysfs_dlink *)calloc(1, sizeof(struct sysfs_dlink));
324 * sysfs_open_directory: opens a sysfs directory, creates dir struct, and
326 * @path: path of directory to open.
327 * returns: struct sysfs_directory * with success and NULL on error.
329 struct sysfs_directory *sysfs_open_directory(const char *path)
331 struct sysfs_directory *sdir = NULL;
337 sdir = alloc_directory();
339 dprintf(stderr, "Error allocating directory %s\n", path);
342 strncpy(sdir->path, path, sizeof(sdir->path));
348 * sysfs_open_dlink: opens a sysfs directory link, creates struct, and returns
349 * @path: path of link to open.
350 * returns: struct sysfs_dlink * with success and NULL on error.
352 struct sysfs_dlink *sysfs_open_dlink(const char *linkpath)
354 struct sysfs_dlink *dlink = NULL;
355 struct sysfs_directory *tdir = NULL;
356 char name[SYSFS_NAME_LEN];
357 char target[SYSFS_PATH_MAX];
359 if (linkpath == NULL) {
364 memset(name, 0, SYSFS_NAME_LEN);
365 memset(target, 0, SYSFS_PATH_MAX);
366 if ((sysfs_get_name_from_path(linkpath, name, SYSFS_NAME_LEN)) != 0
367 || (sysfs_get_link(linkpath, target, SYSFS_PATH_MAX)) != 0) {
369 dprintf(stderr, "Invalid link path %s\n", linkpath);
372 dlink = alloc_dlink();
375 "Error allocating directory link %s\n", linkpath);
378 strcpy(dlink->name, name);
379 tdir = sysfs_open_directory(target);
381 dprintf(stderr, "Invalid directory link target %s\n", target);
382 sysfs_close_dlink(dlink);
385 dlink->target = tdir;
391 * sysfs_read_directory: grabs attributes, links, and subdirectories
392 * @sysdir: sysfs directory to open
393 * returns 0 with success and -1 with error.
395 int sysfs_read_directory(struct sysfs_directory *sysdir)
398 struct dirent *dirent = NULL;
400 struct sysfs_attribute *attr = NULL;
401 struct sysfs_directory *subdir = NULL;
402 struct sysfs_dlink *dlink = NULL;
403 char file_path[SYSFS_PATH_MAX];
406 if (sysdir == NULL) {
410 dir = opendir(sysdir->path);
415 while(((dirent = readdir(dir)) != NULL) && retval == 0) {
416 if (0 == strcmp(dirent->d_name, "."))
418 if (0 == strcmp(dirent->d_name, ".."))
420 memset(file_path, 0, SYSFS_PATH_MAX);
421 strncpy(file_path, sysdir->path, sizeof(file_path));
422 strncat(file_path, "/", sizeof(file_path));
423 strncat(file_path, dirent->d_name, sizeof(file_path));
424 if ((lstat(file_path, &astats)) != 0) {
428 if (S_ISREG(astats.st_mode)) {
429 attr = sysfs_open_attribute(file_path);
431 dprintf (stderr, "Error opening attribute %s\n",
436 if (attr->method & SYSFS_METHOD_SHOW) {
437 if ((sysfs_read_attribute(attr)) != 0) {
439 "Error reading attribute %s\n",
441 sysfs_close_attribute(attr);
445 add_attr_to_dir(sysdir, attr);
446 } else if (S_ISDIR(astats.st_mode)) {
447 subdir = sysfs_open_directory(file_path);
448 if (subdir == NULL) {
449 dprintf (stderr, "Error opening directory %s\n",
454 add_subdir_to_dir(sysdir, subdir);
455 } else if (S_ISLNK(astats.st_mode)) {
456 dlink = sysfs_open_dlink(file_path);
458 dprintf(stderr, "Error opening link %s\n",
463 add_dlink_to_dir(sysdir, dlink);
471 * sysfs_read_dlinks: reads a directory link's target directory. Can
472 * supply a linked list of links.
473 * @dlink: directory link to read.
474 * returns 0 with success or -1 with error.
476 int sysfs_read_dlinks(struct sysfs_dlink *dlink)
478 struct sysfs_dlink *cur = NULL;
480 if (dlink == NULL || dlink->target == NULL) {
485 while (cur != NULL) {
486 if ((sysfs_read_directory(cur->target)) != 0) {
488 "Error reading directory link target %s\n",