1/*
2 * Copyright (C) 2005-2006 Kay Sievers <kay.sievers@vrfy.org>
3 *
4 *	This program is free software; you can redistribute it and/or modify it
5 *	under the terms of the GNU General Public License as published by the
6 *	Free Software Foundation version 2 of the License.
7 *
8 *	This program is distributed in the hope that it will be useful, but
9 *	WITHOUT ANY WARRANTY; without even the implied warranty of
10 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 *	General Public License for more details.
12 *
13 *	You should have received a copy of the GNU General Public License along
14 *	with this program; if not, write to the Free Software Foundation, Inc.,
15 *	51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
16 *
17 */
18
19
20#include <stdlib.h>
21#include <stdio.h>
22#include <stddef.h>
23#include <unistd.h>
24#include <fcntl.h>
25#include <ctype.h>
26#include <errno.h>
27#include <sys/stat.h>
28
29#include "udev.h"
30
31char sysfs_path[PATH_SIZE];
32
33/* device cache */
34static LIST_HEAD(dev_list);
35
36/* attribute value cache */
37static LIST_HEAD(attr_list);
38struct sysfs_attr {
39	struct list_head node;
40	char path[PATH_SIZE];
41	char *value;			/* points to value_local if value is cached */
42	char value_local[NAME_SIZE];
43};
44
45int sysfs_init(void)
46{
47	const char *env;
48
49	env = getenv("SYSFS_PATH");
50	if (env) {
51		strlcpy(sysfs_path, env, sizeof(sysfs_path));
52		remove_trailing_chars(sysfs_path, '/');
53	} else
54		strlcpy(sysfs_path, "/sys", sizeof(sysfs_path));
55	dbg("sysfs_path='%s'", sysfs_path);
56
57	INIT_LIST_HEAD(&dev_list);
58	INIT_LIST_HEAD(&attr_list);
59	return 0;
60}
61
62void sysfs_cleanup(void)
63{
64	struct sysfs_attr *attr_loop;
65	struct sysfs_attr *attr_temp;
66	struct sysfs_device *dev_loop;
67	struct sysfs_device *dev_temp;
68
69	list_for_each_entry_safe(attr_loop, attr_temp, &attr_list, node) {
70		list_del(&attr_loop->node);
71		free(attr_loop);
72	}
73
74	list_for_each_entry_safe(dev_loop, dev_temp, &dev_list, node) {
75		list_del(&dev_loop->node);
76		free(dev_loop);
77	}
78}
79
80void sysfs_device_set_values(struct sysfs_device *dev, const char *devpath,
81			     const char *subsystem, const char *driver)
82{
83	char *pos;
84
85	strlcpy(dev->devpath, devpath, sizeof(dev->devpath));
86	if (subsystem != NULL)
87		strlcpy(dev->subsystem, subsystem, sizeof(dev->subsystem));
88	if (driver != NULL)
89		strlcpy(dev->driver, driver, sizeof(dev->driver));
90
91	/* set kernel name */
92	pos = strrchr(dev->devpath, '/');
93	if (pos == NULL)
94		return;
95	strlcpy(dev->kernel, &pos[1], sizeof(dev->kernel));
96	dbg("kernel='%s'", dev->kernel);
97
98	/* some devices have '!' in their name, change that to '/' */
99	pos = dev->kernel;
100	while (pos[0] != '\0') {
101		if (pos[0] == '!')
102			pos[0] = '/';
103		pos++;
104	}
105
106	/* get kernel number */
107	pos = &dev->kernel[strlen(dev->kernel)];
108	while (isdigit(pos[-1]))
109		pos--;
110	strlcpy(dev->kernel_number, pos, sizeof(dev->kernel_number));
111	dbg("kernel_number='%s'", dev->kernel_number);
112}
113
114int sysfs_resolve_link(char *devpath, size_t size)
115{
116	char link_path[PATH_SIZE];
117	char link_target[PATH_SIZE];
118	int len;
119	int i;
120	int back;
121
122	strlcpy(link_path, sysfs_path, sizeof(link_path));
123	strlcat(link_path, devpath, sizeof(link_path));
124	len = readlink(link_path, link_target, sizeof(link_target));
125	if (len <= 0)
126		return -1;
127	link_target[len] = '\0';
128	dbg("path link '%s' points to '%s'", devpath, link_target);
129
130	for (back = 0; strncmp(&link_target[back * 3], "../", 3) == 0; back++)
131		;
132	dbg("base '%s', tail '%s', back %i", devpath, &link_target[back * 3], back);
133	for (i = 0; i <= back; i++) {
134		char *pos = strrchr(devpath, '/');
135
136		if (pos == NULL)
137			return -1;
138		pos[0] = '\0';
139	}
140	dbg("after moving back '%s'", devpath);
141	strlcat(devpath, "/", size);
142	strlcat(devpath, &link_target[back * 3], size);
143	return 0;
144}
145
146struct sysfs_device *sysfs_device_get(const char *devpath)
147{
148	char path[PATH_SIZE];
149	char devpath_real[PATH_SIZE];
150	struct sysfs_device *dev;
151	struct sysfs_device *dev_loop;
152	struct stat statbuf;
153	char link_path[PATH_SIZE];
154	char link_target[PATH_SIZE];
155	int len;
156	char *pos;
157
158	/* we handle only these devpathes */
159	if (devpath != NULL &&
160	    strncmp(devpath, "/devices/", 9) != 0 &&
161	    strncmp(devpath, "/subsystem/", 11) != 0 &&
162	    strncmp(devpath, "/module/", 8) != 0 &&
163	    strncmp(devpath, "/bus/", 5) != 0 &&
164	    strncmp(devpath, "/class/", 7) != 0 &&
165	    strncmp(devpath, "/block/", 7) != 0)
166		return NULL;
167
168	dbg("open '%s'", devpath);
169	strlcpy(devpath_real, devpath, sizeof(devpath_real));
170	remove_trailing_chars(devpath_real, '/');
171	if (devpath[0] == '\0' )
172		return NULL;
173
174	/* look for device already in cache (we never put an untranslated path in the cache) */
175	list_for_each_entry(dev_loop, &dev_list, node) {
176		if (strcmp(dev_loop->devpath, devpath_real) == 0) {
177			dbg("found in cache '%s'", dev_loop->devpath);
178			return dev_loop;
179		}
180	}
181
182	/* if we got a link, resolve it to the real device */
183	strlcpy(path, sysfs_path, sizeof(path));
184	strlcat(path, devpath_real, sizeof(path));
185	if (lstat(path, &statbuf) != 0) {
186		dbg("stat '%s' failed: %s", path, strerror(errno));
187		return NULL;
188	}
189	if (S_ISLNK(statbuf.st_mode)) {
190		if (sysfs_resolve_link(devpath_real, sizeof(devpath_real)) != 0)
191			return NULL;
192
193		/* now look for device in cache after path translation */
194		list_for_each_entry(dev_loop, &dev_list, node) {
195			if (strcmp(dev_loop->devpath, devpath_real) == 0) {
196				dbg("found in cache '%s'", dev_loop->devpath);
197				return dev_loop;
198			}
199		}
200	}
201
202	/* it is a new device */
203	dbg("new uncached device '%s'", devpath_real);
204	dev = malloc(sizeof(struct sysfs_device));
205	if (dev == NULL)
206		return NULL;
207	memset(dev, 0x00, sizeof(struct sysfs_device));
208
209	sysfs_device_set_values(dev, devpath_real, NULL, NULL);
210
211	/* get subsystem name */
212	strlcpy(link_path, sysfs_path, sizeof(link_path));
213	strlcat(link_path, dev->devpath, sizeof(link_path));
214	strlcat(link_path, "/subsystem", sizeof(link_path));
215	len = readlink(link_path, link_target, sizeof(link_target));
216	if (len > 0) {
217		/* get subsystem from "subsystem" link */
218		link_target[len] = '\0';
219		dbg("subsystem link '%s' points to '%s'", link_path, link_target);
220		pos = strrchr(link_target, '/');
221		if (pos != NULL)
222			strlcpy(dev->subsystem, &pos[1], sizeof(dev->subsystem));
223	} else if (strncmp(dev->devpath, "/class/", 7) == 0) {
224		/* get subsystem from class dir */
225		strlcpy(dev->subsystem, &dev->devpath[7], sizeof(dev->subsystem));
226		pos = strchr(dev->subsystem, '/');
227		if (pos != NULL)
228			pos[0] = '\0';
229		else
230			strlcpy(dev->subsystem, "subsystem", sizeof(dev->subsystem));
231	} else if (strncmp(dev->devpath, "/block/", 7) == 0) {
232		strlcpy(dev->subsystem, "block", sizeof(dev->subsystem));
233	} else if (strncmp(dev->devpath, "/devices/", 9) == 0) {
234		/* get subsystem from "bus" link */
235		strlcpy(link_path, sysfs_path, sizeof(link_path));
236		strlcat(link_path, dev->devpath, sizeof(link_path));
237		strlcat(link_path, "/bus", sizeof(link_path));
238		len = readlink(link_path, link_target, sizeof(link_target));
239		if (len > 0) {
240			link_target[len] = '\0';
241			dbg("bus link '%s' points to '%s'", link_path, link_target);
242			pos = strrchr(link_target, '/');
243			if (pos != NULL)
244				strlcpy(dev->subsystem, &pos[1], sizeof(dev->subsystem));
245		}
246	} else if (strstr(dev->devpath, "/drivers/") != NULL) {
247		strlcpy(dev->subsystem, "drivers", sizeof(dev->subsystem));
248	} else if (strncmp(dev->devpath, "/module/", 8) == 0) {
249		strlcpy(dev->subsystem, "module", sizeof(dev->subsystem));
250	} else if (strncmp(dev->devpath, "/subsystem/", 11) == 0) {
251		pos = strrchr(dev->devpath, '/');
252		if (pos == &dev->devpath[10])
253			strlcpy(dev->subsystem, "subsystem", sizeof(dev->subsystem));
254	} else if (strncmp(dev->devpath, "/bus/", 5) == 0) {
255		pos = strrchr(dev->devpath, '/');
256		if (pos == &dev->devpath[4])
257			strlcpy(dev->subsystem, "subsystem", sizeof(dev->subsystem));
258	}
259
260	/* get driver name */
261	strlcpy(link_path, sysfs_path, sizeof(link_path));
262	strlcat(link_path, dev->devpath, sizeof(link_path));
263	strlcat(link_path, "/driver", sizeof(link_path));
264	len = readlink(link_path, link_target, sizeof(link_target));
265	if (len > 0) {
266		link_target[len] = '\0';
267		dbg("driver link '%s' points to '%s'", link_path, link_target);
268		pos = strrchr(link_target, '/');
269		if (pos != NULL)
270			strlcpy(dev->driver, &pos[1], sizeof(dev->driver));
271	}
272
273	dbg("add to cache 'devpath=%s', subsystem='%s', driver='%s'", dev->devpath, dev->subsystem, dev->driver);
274	list_add(&dev->node, &dev_list);
275
276	return dev;
277}
278
279struct sysfs_device *sysfs_device_get_parent(struct sysfs_device *dev)
280{
281	char parent_devpath[PATH_SIZE];
282	char *pos;
283
284	dbg("open '%s'", dev->devpath);
285
286	/* look if we already know the parent */
287	if (dev->parent != NULL)
288		return dev->parent;
289
290	strlcpy(parent_devpath, dev->devpath, sizeof(parent_devpath));
291	dbg("'%s'", parent_devpath);
292
293	/* strip last element */
294	pos = strrchr(parent_devpath, '/');
295	if (pos == NULL || pos == parent_devpath)
296		return NULL;
297	pos[0] = '\0';
298
299	if (strncmp(parent_devpath, "/class", 6) == 0) {
300		pos = strrchr(parent_devpath, '/');
301		if (pos == &parent_devpath[6] || pos == parent_devpath) {
302			dbg("/class top level, look for device link");
303			goto device_link;
304		}
305	}
306	if (strcmp(parent_devpath, "/block") == 0) {
307		dbg("/block top level, look for device link");
308		goto device_link;
309	}
310
311	/* are we at the top level? */
312	pos = strrchr(parent_devpath, '/');
313	if (pos == NULL || pos == parent_devpath)
314		return NULL;
315
316	/* get parent and remember it */
317	dev->parent = sysfs_device_get(parent_devpath);
318	return dev->parent;
319
320device_link:
321	strlcpy(parent_devpath, dev->devpath, sizeof(parent_devpath));
322	strlcat(parent_devpath, "/device", sizeof(parent_devpath));
323	if (sysfs_resolve_link(parent_devpath, sizeof(parent_devpath)) != 0)
324		return NULL;
325
326	/* get parent and remember it */
327	dev->parent = sysfs_device_get(parent_devpath);
328	return dev->parent;
329}
330
331struct sysfs_device *sysfs_device_get_parent_with_subsystem(struct sysfs_device *dev, const char *subsystem)
332{
333	struct sysfs_device *dev_parent;
334
335	dev_parent = sysfs_device_get_parent(dev);
336	while (dev_parent != NULL) {
337		if (strcmp(dev_parent->subsystem, subsystem) == 0)
338			return dev_parent;
339		dev_parent = sysfs_device_get_parent(dev_parent);
340	}
341	return NULL;
342}
343
344char *sysfs_attr_get_value(const char *devpath, const char *attr_name)
345{
346	char path_full[PATH_SIZE];
347	const char *path;
348	char value[NAME_SIZE];
349	struct sysfs_attr *attr_loop;
350	struct sysfs_attr *attr;
351	struct stat statbuf;
352	int fd;
353	ssize_t size;
354	size_t sysfs_len;
355
356	dbg("open '%s'/'%s'", devpath, attr_name);
357	sysfs_len = strlcpy(path_full, sysfs_path, sizeof(path_full));
358	path = &path_full[sysfs_len];
359	strlcat(path_full, devpath, sizeof(path_full));
360	strlcat(path_full, "/", sizeof(path_full));
361	strlcat(path_full, attr_name, sizeof(path_full));
362
363	/* look for attribute in cache */
364	list_for_each_entry(attr_loop, &attr_list, node) {
365		if (strcmp(attr_loop->path, path) == 0) {
366			dbg("found in cache '%s'", attr_loop->path);
367			return attr_loop->value;
368		}
369	}
370
371	/* store attribute in cache (also negatives are kept in cache) */
372	dbg("new uncached attribute '%s'", path_full);
373	attr = malloc(sizeof(struct sysfs_attr));
374	if (attr == NULL)
375		return NULL;
376	memset(attr, 0x00, sizeof(struct sysfs_attr));
377	strlcpy(attr->path, path, sizeof(attr->path));
378	dbg("add to cache '%s'", path_full);
379	list_add(&attr->node, &attr_list);
380
381	if (lstat(path_full, &statbuf) != 0) {
382		dbg("stat '%s' failed: %s", path_full, strerror(errno));
383		goto out;
384	}
385
386	if (S_ISLNK(statbuf.st_mode)) {
387		/* links return the last element of the target path */
388		char link_target[PATH_SIZE];
389		int len;
390		const char *pos;
391
392		len = readlink(path_full, link_target, sizeof(link_target));
393		if (len > 0) {
394			link_target[len] = '\0';
395			pos = strrchr(link_target, '/');
396			if (pos != NULL) {
397				dbg("cache '%s' with link value '%s'", path_full, value);
398				strlcpy(attr->value_local, &pos[1], sizeof(attr->value_local));
399				attr->value = attr->value_local;
400			}
401		}
402		goto out;
403	}
404
405	/* skip directories */
406	if (S_ISDIR(statbuf.st_mode))
407		goto out;
408
409	/* skip non-readable files */
410	if ((statbuf.st_mode & S_IRUSR) == 0)
411		goto out;
412
413	/* read attribute value */
414	fd = open(path_full, O_RDONLY);
415	if (fd < 0) {
416		dbg("attribute '%s' does not exist", path_full);
417		goto out;
418	}
419	size = read(fd, value, sizeof(value));
420	close(fd);
421	if (size < 0)
422		goto out;
423	if (size == sizeof(value))
424		goto out;
425
426	/* got a valid value, store and return it */
427	value[size] = '\0';
428	remove_trailing_chars(value, '\n');
429	dbg("cache '%s' with attribute value '%s'", path_full, value);
430	strlcpy(attr->value_local, value, sizeof(attr->value_local));
431	attr->value = attr->value_local;
432
433out:
434	return attr->value;
435}
436
437int sysfs_lookup_devpath_by_subsys_id(char *devpath_full, size_t len, const char *subsystem, const char *id)
438{
439	size_t sysfs_len;
440	char path_full[PATH_SIZE];
441	char *path;
442	struct stat statbuf;
443
444	sysfs_len = strlcpy(path_full, sysfs_path, sizeof(path_full));
445	path = &path_full[sysfs_len];
446
447	if (strcmp(subsystem, "subsystem") == 0) {
448		strlcpy(path, "/subsystem/", sizeof(path_full) - sysfs_len);
449		strlcat(path, id, sizeof(path_full) - sysfs_len);
450		if (stat(path_full, &statbuf) == 0)
451			goto found;
452
453		strlcpy(path, "/bus/", sizeof(path_full) - sysfs_len);
454		strlcat(path, id, sizeof(path_full) - sysfs_len);
455		if (stat(path_full, &statbuf) == 0)
456			goto found;
457		goto out;
458
459		strlcpy(path, "/class/", sizeof(path_full) - sysfs_len);
460		strlcat(path, id, sizeof(path_full) - sysfs_len);
461		if (stat(path_full, &statbuf) == 0)
462			goto found;
463	}
464
465	if (strcmp(subsystem, "module") == 0) {
466		strlcpy(path, "/module/", sizeof(path_full) - sysfs_len);
467		strlcat(path, id, sizeof(path_full) - sysfs_len);
468		if (stat(path_full, &statbuf) == 0)
469			goto found;
470		goto out;
471	}
472
473	if (strcmp(subsystem, "drivers") == 0) {
474		char subsys[NAME_SIZE];
475		char *driver;
476
477		strlcpy(subsys, id, sizeof(subsys));
478		driver = strchr(subsys, ':');
479		if (driver != NULL) {
480			driver[0] = '\0';
481			driver = &driver[1];
482			strlcpy(path, "/subsystem/", sizeof(path_full) - sysfs_len);
483			strlcat(path, subsys, sizeof(path_full) - sysfs_len);
484			strlcat(path, "/drivers/", sizeof(path_full) - sysfs_len);
485			strlcat(path, driver, sizeof(path_full) - sysfs_len);
486			if (stat(path_full, &statbuf) == 0)
487				goto found;
488
489			strlcpy(path, "/bus/", sizeof(path_full) - sysfs_len);
490			strlcat(path, subsys, sizeof(path_full) - sysfs_len);
491			strlcat(path, "/drivers/", sizeof(path_full) - sysfs_len);
492			strlcat(path, driver, sizeof(path_full) - sysfs_len);
493			if (stat(path_full, &statbuf) == 0)
494				goto found;
495		}
496		goto out;
497	}
498
499	strlcpy(path, "/subsystem/", sizeof(path_full) - sysfs_len);
500	strlcat(path, subsystem, sizeof(path_full) - sysfs_len);
501	strlcat(path, "/devices/", sizeof(path_full) - sysfs_len);
502	strlcat(path, id, sizeof(path_full) - sysfs_len);
503	if (stat(path_full, &statbuf) == 0)
504		goto found;
505
506	strlcpy(path, "/bus/", sizeof(path_full) - sysfs_len);
507	strlcat(path, subsystem, sizeof(path_full) - sysfs_len);
508	strlcat(path, "/devices/", sizeof(path_full) - sysfs_len);
509	strlcat(path, id, sizeof(path_full) - sysfs_len);
510	if (stat(path_full, &statbuf) == 0)
511		goto found;
512
513	strlcpy(path, "/class/", sizeof(path_full) - sysfs_len);
514	strlcat(path, subsystem, sizeof(path_full) - sysfs_len);
515	strlcat(path, "/", sizeof(path_full) - sysfs_len);
516	strlcat(path, id, sizeof(path_full) - sysfs_len);
517	if (stat(path_full, &statbuf) == 0)
518		goto found;
519out:
520	return 0;
521found:
522	if (S_ISLNK(statbuf.st_mode))
523		sysfs_resolve_link(path, sizeof(path_full) - sysfs_len);
524	strlcpy(devpath_full, path, len);
525	return 1;
526}
527