summaryrefslogtreecommitdiff
path: root/lib/sysfs_dir.c
blob: e6ad906dde716626c9b69d6eed9f31d87245c100 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/*
 * syfs_dir.c
 *
 * Directory utility functions for libsysfs
 *
 * Copyright (C) 2003 International Business Machines, Inc.
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 */
#include "libsysfs.h"
#include "sysfs.h"

/**
 * sysfs_close_attribute: closes and cleans up attribute
 * @sysattr: attribute to close.
 */
void sysfs_close_attribute(struct sysfs_attribute *sysattr)
{
	if (sysattr != NULL) {
		if (sysattr->value != NULL)
			free(sysattr->value);
		free(sysattr);
	}
}

/**
 * alloc_attribute: allocates and initializes attribute structure
 * returns struct sysfs_attribute with success and NULL with error.
 */
static struct sysfs_attribute *alloc_attribute(void)
{
	return (struct sysfs_attribute *)
			calloc(1, sizeof(struct sysfs_attribute));
}

/**
 * sysfs_open_attribute: creates sysfs_attribute structure
 * @path: path to attribute.
 * returns sysfs_attribute struct with success and NULL with error.
 */
struct sysfs_attribute *sysfs_open_attribute(const char *path)
{
	struct sysfs_attribute *sysattr = NULL;
	struct stat fileinfo;
	
	if (path == NULL) {
		errno = EINVAL;
		return NULL;
	}
	sysattr = alloc_attribute();
	if (sysattr == NULL) {
		dprintf(stderr, "Error allocating attribute at %s\n", path);
		return NULL;
	}
	strncpy(sysattr->path, path, sizeof(sysattr->path));
	if ((stat(sysattr->path, &fileinfo)) != 0) {
		perror("stat");
		sysattr->method = 0;
	} else {
		if (fileinfo.st_mode & S_IRUSR)
			sysattr->method |= SYSFS_METHOD_SHOW;
		if (fileinfo.st_mode & S_IWUSR)
			sysattr->method |= SYSFS_METHOD_STORE;
	}

	return sysattr;
}

/**
 * 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;
	size_t length = 0;
	int pgsize = 0;
	int fd;

	if (sysattr == NULL) {
		errno = EINVAL;
		return -1;
	}
	if (!(sysattr->method & SYSFS_METHOD_SHOW)) {
		dprintf (stderr, "Show method not supported for attribute %s\n",
			sysattr->path);
		return -1;
	}
	pgsize = getpagesize();
	fbuf = (char *)calloc(1, pgsize+1);
	if (fbuf == NULL) {
		perror("calloc");
		return -1;
	}
	if ((fd = open(sysattr->path, O_RDONLY)) < 0) {
		dprintf (stderr, "Error reading attribute %s\n", sysattr->path);
		free(fbuf);
		return -1;
	}
	length = read(fd, fbuf, pgsize);
	if (length < 0) {
		dprintf (stderr, "Error reading from attribute %s\n",
			sysattr->path);
		close(fd);
		free(fbuf);
		return -1;
	}
	sysattr->len = length;
	close(fd);
	vbuf = (char *)realloc(fbuf, length+1);
	if (vbuf == NULL) {
		perror("realloc");
		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) {
		errno = EINVAL;
		return -1;
	}

	attr = sysfs_open_attribute(attrpath);
	if (attr == NULL) {
		dprintf(stderr, "Invalid attribute path %s\n", attrpath);
		errno = EINVAL;
		return -1;
	}
	if((sysfs_read_attribute(attr)) != 0 || attr->value == NULL) {
		dprintf(stderr, "Error reading from attribute %s\n", attrpath);
		sysfs_close_attribute(attr);
		return -1;
	}
	length = strlen(attr->value);
	if (length > vsize) 
		dprintf(stderr, 
			"Value length %d is larger than supplied buffer %d\n",
			length, vsize);
	strncpy(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 sysfs_attribute *attr, 
					const char *name)
{	
	struct sysfs_attribute *cur = NULL;
	char tmpname[SYSFS_NAME_LEN];
	
	if (attr == NULL || name == NULL) {
		errno = EINVAL;
		return NULL;
	}	
	cur = attr;
	while (cur != NULL) {
		memset(tmpname, 0, SYSFS_NAME_LEN);	
		if ((sysfs_get_name_from_path(cur->path, tmpname,
		    SYSFS_NAME_LEN)) != 0) {
			cur = cur->next;
			continue;
		}
		if (strcmp(tmpname, name) == 0)
			return cur->value;
		cur = cur->next;
	}
	return NULL;
}

/**
 * add_subdir_to_dir: adds subdirectory to directory's subdirs
 * @sysdir: directory to add subdir to
 * @subdir: subdirectory to add.
 */
static void add_subdir_to_dir(struct sysfs_directory *sysdir, 
		     struct sysfs_directory *subdir)
{
	if (sysdir != NULL && subdir != NULL) {
		subdir->next = sysdir->subdirs;
		sysdir->subdirs = subdir;
	}
}

/**
 * add_attr_to_dir: adds attribute to directory's attributes
 * @sysdir: directory to add attribute to
 * @sysattr: attribute to add.
 */
static void add_attr_to_dir(struct sysfs_directory *sysdir, 
		     			struct sysfs_attribute *sysattr)
{
	if (sysdir != NULL && sysattr != NULL) {
		sysattr->next = sysdir->attributes;
		sysdir->attributes = sysattr;
	}
}

/**
 * sysfs_close_link: closes and cleans up link.
 * @ln: link to close.
 */
void sysfs_close_link(struct sysfs_link *ln)
{
	if (ln != NULL) {
		ln->next = NULL;
		free(ln);
	}
}

/**
 * add_link_to_dir: adds link to directory's links list.
 * @sysdir: directory to add it to.
 * @ln: link to add.
 */
static void add_link_to_dir(struct sysfs_directory *sysdir, 
					struct sysfs_link *ln)
{
	if (sysdir != NULL && ln != NULL) {
		ln->next = sysdir->links;
		sysdir->links = ln;
	}
}

/**
 * sysfs_close_directory: closes directory, cleans up attributes and links
 * @sysdir: sysfs_directory to close
 */
void sysfs_close_directory(struct sysfs_directory *sysdir)
{
	struct sysfs_directory *sdir = NULL, *dnext = NULL;
	struct sysfs_link *dlink = NULL, *nextl = NULL;
	struct sysfs_attribute *attr = NULL, *anext = NULL;

	if (sysdir != NULL) {
		if (sysdir->subdirs != NULL) {
			for (sdir = sysdir->subdirs; sdir != NULL;
			     sdir = dnext) {
				dnext = sdir->next;
				sysfs_close_directory(sdir);
			}
		}
		if (sysdir->links != NULL) {
			for (dlink = sysdir->links; dlink != NULL;
			    dlink = nextl) {
				nextl = dlink->next;
				sysfs_close_link(dlink);
			}
		}
		if (sysdir->attributes != NULL) {
			for (attr = sysdir->attributes; attr != NULL;
			     attr = anext) {
				anext = attr->next;
				/* sysfs_close_attribute(attr); */
				if (attr->value != NULL)
					free(attr->value);
				free(attr);
			}
		}
		free(sysdir);
	}
}

/**
 * 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_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;
	}
	sdir = alloc_directory();
	if (sdir == NULL) {
		dprintf(stderr, "Error allocating directory %s\n", path);
		return NULL;
	}
	strncpy(sdir->path, path, sizeof(sdir->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) {
		errno = EINVAL;
		return NULL;
	}

	ln = alloc_link();
	if (ln == NULL) {
		dprintf(stderr, 
			"Error allocating link %s\n", linkpath);
		return NULL;
	}
	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(stderr, "Invalid link path %s\n", linkpath);
		return NULL;
	}

	return ln;
}

/**
 * sysfs_read_directory: grabs attributes, links, and subdirectories
 * @sysdir: sysfs directory to open
 * returns 0 with success and -1 with error.
 */
int sysfs_read_directory(struct sysfs_directory *sysdir)
{
	DIR *dir = NULL;
	struct dirent *dirent = NULL;
	struct stat astats;
	struct sysfs_attribute *attr = NULL;
	struct sysfs_directory *subdir = NULL;
	struct sysfs_link *ln = NULL;
	char file_path[SYSFS_PATH_MAX];
	int retval = 0;

	if (sysdir == NULL) {
		errno = EINVAL;
		return -1;
	}
	dir = opendir(sysdir->path);
	if (dir == NULL) {
		perror("opendir");
		return -1;
	}
	while(((dirent = readdir(dir)) != NULL) && retval == 0) {
		if (0 == strcmp(dirent->d_name, "."))
			 continue;
		if (0 == strcmp(dirent->d_name, ".."))
			continue;
		memset(file_path, 0, SYSFS_PATH_MAX);
		strncpy(file_path, sysdir->path, sizeof(file_path));
		strncat(file_path, "/", sizeof(file_path));
		strncat(file_path, dirent->d_name, sizeof(file_path));
		if ((lstat(file_path, &astats)) != 0) {
			perror("stat");
			continue;
		}
		if (S_ISREG(astats.st_mode)) {	
			attr = sysfs_open_attribute(file_path);
			if (attr == NULL) {
				dprintf (stderr, "Error opening attribute %s\n",
					file_path);
				retval = -1;
				break;
			}
			if (attr->method & SYSFS_METHOD_SHOW) {
				if ((sysfs_read_attribute(attr)) != 0) {
					dprintf (stderr, 
						"Error reading attribute %s\n",
						file_path);
					sysfs_close_attribute(attr);
					continue;
				}
			}
			add_attr_to_dir(sysdir, attr);
		} else if (S_ISDIR(astats.st_mode)) {
			subdir = sysfs_open_directory(file_path);
			if (subdir == NULL) {
				dprintf (stderr, "Error opening directory %s\n",
					file_path);
				retval = -1;
				break;
			}
			add_subdir_to_dir(sysdir, subdir);
		} else if (S_ISLNK(astats.st_mode)) {
			ln = sysfs_open_link(file_path);
			if (ln == NULL) {
				dprintf(stderr, "Error opening link %s\n",
					file_path);
				retval = -1;
				break;
			}
			add_link_to_dir(sysdir, ln);
		}
	}
	closedir(dir);
	return(retval);
}