2 * Copyright (C) 2005-2006 Kay Sievers <kay.sievers@vrfy.org>
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.
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.
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.
31 char sysfs_path[PATH_SIZE];
34 static LIST_HEAD(dev_list);
36 /* attribute value cache */
37 static LIST_HEAD(attr_list);
39 struct list_head node;
41 char *value; /* points to value_local if value is cached */
42 char value_local[NAME_SIZE];
49 env = getenv("SYSFS_PATH");
51 strlcpy(sysfs_path, env, sizeof(sysfs_path));
52 remove_trailing_chars(sysfs_path, '/');
54 strlcpy(sysfs_path, "/sys", sizeof(sysfs_path));
55 dbg("sysfs_path='%s'\n", sysfs_path);
57 INIT_LIST_HEAD(&dev_list);
58 INIT_LIST_HEAD(&attr_list);
62 void sysfs_cleanup(void)
64 struct sysfs_attr *attr_loop;
65 struct sysfs_attr *attr_temp;
66 struct sysfs_device *dev_loop;
67 struct sysfs_device *dev_temp;
69 list_for_each_entry_safe(attr_loop, attr_temp, &attr_list, node) {
70 list_del(&attr_loop->node);
74 list_for_each_entry_safe(dev_loop, dev_temp, &dev_list, node) {
75 list_del(&dev_loop->node);
80 void sysfs_device_set_values(struct sysfs_device *dev, const char *devpath,
81 const char *subsystem, const char *driver)
85 strlcpy(dev->devpath, devpath, sizeof(dev->devpath));
86 if (subsystem != NULL)
87 strlcpy(dev->subsystem, subsystem, sizeof(dev->subsystem));
89 strlcpy(dev->driver, driver, sizeof(dev->driver));
92 pos = strrchr(dev->devpath, '/');
95 strlcpy(dev->kernel, &pos[1], sizeof(dev->kernel));
96 dbg("kernel='%s'\n", dev->kernel);
98 /* some devices have '!' in their name, change that to '/' */
100 while (pos[0] != '\0') {
106 /* get kernel number */
107 pos = &dev->kernel[strlen(dev->kernel)];
108 while (isdigit(pos[-1]))
110 strlcpy(dev->kernel_number, pos, sizeof(dev->kernel_number));
111 dbg("kernel_number='%s'\n", dev->kernel_number);
114 int sysfs_resolve_link(char *devpath, size_t size)
116 char link_path[PATH_SIZE];
117 char link_target[PATH_SIZE];
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));
127 link_target[len] = '\0';
128 dbg("path link '%s' points to '%s'\n", devpath, link_target);
130 for (back = 0; strncmp(&link_target[back * 3], "../", 3) == 0; back++)
132 dbg("base '%s', tail '%s', back %i\n", devpath, &link_target[back * 3], back);
133 for (i = 0; i <= back; i++) {
134 char *pos = strrchr(devpath, '/');
140 dbg("after moving back '%s'\n", devpath);
141 strlcat(devpath, "/", size);
142 strlcat(devpath, &link_target[back * 3], size);
146 struct sysfs_device *sysfs_device_get(const char *devpath)
148 char path[PATH_SIZE];
149 char devpath_real[PATH_SIZE];
150 struct sysfs_device *dev;
151 struct sysfs_device *dev_loop;
153 char link_path[PATH_SIZE];
154 char link_target[PATH_SIZE];
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)
168 dbg("open '%s'\n", devpath);
169 strlcpy(devpath_real, devpath, sizeof(devpath_real));
170 remove_trailing_chars(devpath_real, '/');
171 if (devpath[0] == '\0' )
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'\n", dev_loop->devpath);
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\n", path, strerror(errno));
189 if (S_ISLNK(statbuf.st_mode)) {
190 if (sysfs_resolve_link(devpath_real, sizeof(devpath_real)) != 0)
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'\n", dev_loop->devpath);
202 /* it is a new device */
203 dbg("new uncached device '%s'\n", devpath_real);
204 dev = malloc(sizeof(struct sysfs_device));
207 memset(dev, 0x00, sizeof(struct sysfs_device));
209 sysfs_device_set_values(dev, devpath_real, NULL, NULL);
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));
217 /* get subsystem from "subsystem" link */
218 link_target[len] = '\0';
219 dbg("subsystem link '%s' points to '%s'\n", link_path, link_target);
220 pos = strrchr(link_target, '/');
222 strlcpy(dev->subsystem, &pos[1], sizeof(dev->subsystem));
223 } else if (strstr(dev->devpath, "/drivers/") != NULL) {
224 strlcpy(dev->subsystem, "drivers", sizeof(dev->subsystem));
225 } else if (strncmp(dev->devpath, "/module/", 8) == 0) {
226 strlcpy(dev->subsystem, "module", sizeof(dev->subsystem));
227 } else if (strncmp(dev->devpath, "/subsystem/", 11) == 0) {
228 pos = strrchr(dev->devpath, '/');
229 if (pos == &dev->devpath[10])
230 strlcpy(dev->subsystem, "subsystem", sizeof(dev->subsystem));
231 } else if (strncmp(dev->devpath, "/class/", 7) == 0) {
232 pos = strrchr(dev->devpath, '/');
233 if (pos == &dev->devpath[6])
234 strlcpy(dev->subsystem, "subsystem", sizeof(dev->subsystem));
235 } else if (strncmp(dev->devpath, "/bus/", 5) == 0) {
236 pos = strrchr(dev->devpath, '/');
237 if (pos == &dev->devpath[4])
238 strlcpy(dev->subsystem, "subsystem", sizeof(dev->subsystem));
241 /* get driver name */
242 strlcpy(link_path, sysfs_path, sizeof(link_path));
243 strlcat(link_path, dev->devpath, sizeof(link_path));
244 strlcat(link_path, "/driver", sizeof(link_path));
245 len = readlink(link_path, link_target, sizeof(link_target));
247 link_target[len] = '\0';
248 dbg("driver link '%s' points to '%s'\n", link_path, link_target);
249 pos = strrchr(link_target, '/');
251 strlcpy(dev->driver, &pos[1], sizeof(dev->driver));
254 dbg("add to cache 'devpath=%s', subsystem='%s', driver='%s'\n", dev->devpath, dev->subsystem, dev->driver);
255 list_add(&dev->node, &dev_list);
260 struct sysfs_device *sysfs_device_get_parent(struct sysfs_device *dev)
262 char parent_devpath[PATH_SIZE];
265 dbg("open '%s'\n", dev->devpath);
267 /* look if we already know the parent */
268 if (dev->parent != NULL)
271 strlcpy(parent_devpath, dev->devpath, sizeof(parent_devpath));
272 dbg("'%s'\n", parent_devpath);
274 /* strip last element */
275 pos = strrchr(parent_devpath, '/');
276 if (pos == NULL || pos == parent_devpath)
280 if (strncmp(parent_devpath, "/class", 6) == 0) {
281 pos = strrchr(parent_devpath, '/');
282 if (pos == &parent_devpath[6] || pos == parent_devpath) {
283 dbg("/class top level, look for device link\n");
287 if (strcmp(parent_devpath, "/block") == 0) {
288 dbg("/block top level, look for device link\n");
292 /* are we at the top level? */
293 pos = strrchr(parent_devpath, '/');
294 if (pos == NULL || pos == parent_devpath)
297 /* get parent and remember it */
298 dev->parent = sysfs_device_get(parent_devpath);
302 strlcpy(parent_devpath, dev->devpath, sizeof(parent_devpath));
303 strlcat(parent_devpath, "/device", sizeof(parent_devpath));
304 if (sysfs_resolve_link(parent_devpath, sizeof(parent_devpath)) != 0)
307 /* get parent and remember it */
308 dev->parent = sysfs_device_get(parent_devpath);
312 struct sysfs_device *sysfs_device_get_parent_with_subsystem(struct sysfs_device *dev, const char *subsystem)
314 struct sysfs_device *dev_parent;
316 dev_parent = sysfs_device_get_parent(dev);
317 while (dev_parent != NULL) {
318 if (strcmp(dev_parent->subsystem, subsystem) == 0)
320 dev_parent = sysfs_device_get_parent(dev_parent);
325 char *sysfs_attr_get_value(const char *devpath, const char *attr_name)
327 char path_full[PATH_SIZE];
329 char value[NAME_SIZE];
330 struct sysfs_attr *attr_loop;
331 struct sysfs_attr *attr;
337 dbg("open '%s'/'%s'\n", devpath, attr_name);
338 sysfs_len = strlcpy(path_full, sysfs_path, sizeof(path_full));
339 if(sysfs_len >= sizeof(path_full))
340 sysfs_len = sizeof(path_full) - 1;
341 path = &path_full[sysfs_len];
342 strlcat(path_full, devpath, sizeof(path_full));
343 strlcat(path_full, "/", sizeof(path_full));
344 strlcat(path_full, attr_name, sizeof(path_full));
346 /* look for attribute in cache */
347 list_for_each_entry(attr_loop, &attr_list, node) {
348 if (strcmp(attr_loop->path, path) == 0) {
349 dbg("found in cache '%s'\n", attr_loop->path);
350 return attr_loop->value;
354 /* store attribute in cache (also negatives are kept in cache) */
355 dbg("new uncached attribute '%s'\n", path_full);
356 attr = malloc(sizeof(struct sysfs_attr));
359 memset(attr, 0x00, sizeof(struct sysfs_attr));
360 strlcpy(attr->path, path, sizeof(attr->path));
361 dbg("add to cache '%s'\n", path_full);
362 list_add(&attr->node, &attr_list);
364 if (lstat(path_full, &statbuf) != 0) {
365 dbg("stat '%s' failed: %s\n", path_full, strerror(errno));
369 if (S_ISLNK(statbuf.st_mode)) {
370 /* links return the last element of the target path */
371 char link_target[PATH_SIZE];
375 len = readlink(path_full, link_target, sizeof(link_target));
377 link_target[len] = '\0';
378 pos = strrchr(link_target, '/');
380 dbg("cache '%s' with link value '%s'\n", path_full, value);
381 strlcpy(attr->value_local, &pos[1], sizeof(attr->value_local));
382 attr->value = attr->value_local;
388 /* skip directories */
389 if (S_ISDIR(statbuf.st_mode))
392 /* skip non-readable files */
393 if ((statbuf.st_mode & S_IRUSR) == 0)
396 /* read attribute value */
397 fd = open(path_full, O_RDONLY);
399 dbg("attribute '%s' can not be opened\n", path_full);
402 size = read(fd, value, sizeof(value));
406 if (size == sizeof(value))
409 /* got a valid value, store and return it */
411 remove_trailing_chars(value, '\n');
412 dbg("cache '%s' with attribute value '%s'\n", path_full, value);
413 strlcpy(attr->value_local, value, sizeof(attr->value_local));
414 attr->value = attr->value_local;
420 int sysfs_lookup_devpath_by_subsys_id(char *devpath_full, size_t len, const char *subsystem, const char *id)
423 char path_full[PATH_SIZE];
427 sysfs_len = strlcpy(path_full, sysfs_path, sizeof(path_full));
428 path = &path_full[sysfs_len];
430 if (strcmp(subsystem, "subsystem") == 0) {
431 strlcpy(path, "/subsystem/", sizeof(path_full) - sysfs_len);
432 strlcat(path, id, sizeof(path_full) - sysfs_len);
433 if (stat(path_full, &statbuf) == 0)
436 strlcpy(path, "/bus/", sizeof(path_full) - sysfs_len);
437 strlcat(path, id, sizeof(path_full) - sysfs_len);
438 if (stat(path_full, &statbuf) == 0)
442 strlcpy(path, "/class/", sizeof(path_full) - sysfs_len);
443 strlcat(path, id, sizeof(path_full) - sysfs_len);
444 if (stat(path_full, &statbuf) == 0)
448 if (strcmp(subsystem, "module") == 0) {
449 strlcpy(path, "/module/", sizeof(path_full) - sysfs_len);
450 strlcat(path, id, sizeof(path_full) - sysfs_len);
451 if (stat(path_full, &statbuf) == 0)
456 if (strcmp(subsystem, "drivers") == 0) {
457 char subsys[NAME_SIZE];
460 strlcpy(subsys, id, sizeof(subsys));
461 driver = strchr(subsys, ':');
462 if (driver != NULL) {
465 strlcpy(path, "/subsystem/", sizeof(path_full) - sysfs_len);
466 strlcat(path, subsys, sizeof(path_full) - sysfs_len);
467 strlcat(path, "/drivers/", sizeof(path_full) - sysfs_len);
468 strlcat(path, driver, sizeof(path_full) - sysfs_len);
469 if (stat(path_full, &statbuf) == 0)
472 strlcpy(path, "/bus/", sizeof(path_full) - sysfs_len);
473 strlcat(path, subsys, sizeof(path_full) - sysfs_len);
474 strlcat(path, "/drivers/", sizeof(path_full) - sysfs_len);
475 strlcat(path, driver, sizeof(path_full) - sysfs_len);
476 if (stat(path_full, &statbuf) == 0)
482 strlcpy(path, "/subsystem/", sizeof(path_full) - sysfs_len);
483 strlcat(path, subsystem, sizeof(path_full) - sysfs_len);
484 strlcat(path, "/devices/", sizeof(path_full) - sysfs_len);
485 strlcat(path, id, sizeof(path_full) - sysfs_len);
486 if (stat(path_full, &statbuf) == 0)
489 strlcpy(path, "/bus/", sizeof(path_full) - sysfs_len);
490 strlcat(path, subsystem, sizeof(path_full) - sysfs_len);
491 strlcat(path, "/devices/", sizeof(path_full) - sysfs_len);
492 strlcat(path, id, sizeof(path_full) - sysfs_len);
493 if (stat(path_full, &statbuf) == 0)
496 strlcpy(path, "/class/", sizeof(path_full) - sysfs_len);
497 strlcat(path, subsystem, sizeof(path_full) - sysfs_len);
498 strlcat(path, "/", sizeof(path_full) - sysfs_len);
499 strlcat(path, id, sizeof(path_full) - sysfs_len);
500 if (stat(path_full, &statbuf) == 0)
505 if (S_ISLNK(statbuf.st_mode))
506 sysfs_resolve_link(path, sizeof(path_full) - sysfs_len);
507 strlcpy(devpath_full, path, len);