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.
37 char sysfs_path[PATH_SIZE];
39 /* attribute value cache */
40 static LIST_HEAD(attr_list);
42 struct list_head node;
44 char *value; /* points to value_local if value is cached */
45 char value_local[NAME_SIZE];
48 /* list of sysfs devices */
49 static LIST_HEAD(sysfs_dev_list);
51 struct list_head node;
52 struct sysfs_device dev;
55 int sysfs_init(char *path, size_t len)
58 strlcpy(sysfs_path, path, len);
59 remove_trailing_chars(sysfs_path, '/');
61 strlcpy(sysfs_path, "/sys", sizeof(sysfs_path));
62 dbg("sysfs_path='%s'", sysfs_path);
64 INIT_LIST_HEAD(&attr_list);
65 INIT_LIST_HEAD(&sysfs_dev_list);
69 void sysfs_cleanup(void)
71 struct sysfs_attr *attr_loop;
72 struct sysfs_attr *attr_temp;
74 struct sysfs_dev *sysdev_loop;
75 struct sysfs_dev *sysdev_temp;
77 list_for_each_entry_safe(attr_loop, attr_temp, &attr_list, node) {
78 list_del(&attr_loop->node);
82 list_for_each_entry_safe(sysdev_loop, sysdev_temp, &sysfs_dev_list, node) {
83 list_del(&sysdev_loop->node);
88 void sysfs_device_set_values(struct sysfs_device *dev, const char *devpath,
89 const char *subsystem, const char *driver)
93 strlcpy(dev->devpath, devpath, sizeof(dev->devpath));
94 if (subsystem != NULL)
95 strlcpy(dev->subsystem, subsystem, sizeof(dev->subsystem));
97 strlcpy(dev->driver, driver, sizeof(dev->driver));
100 pos = strrchr(dev->devpath, '/');
103 strlcpy(dev->kernel, &pos[1], sizeof(dev->kernel));
104 dbg("kernel='%s'", dev->kernel);
106 /* some devices have '!' in their name, change that to '/' */
108 while (pos[0] != '\0') {
114 /* get kernel number */
115 pos = &dev->kernel[strlen(dev->kernel)];
116 while (isdigit(pos[-1]))
118 strlcpy(dev->kernel_number, pos, sizeof(dev->kernel_number));
119 dbg("kernel_number='%s'", dev->kernel_number);
122 int sysfs_resolve_link(char *devpath, size_t size)
124 char link_path[PATH_SIZE];
125 char link_target[PATH_SIZE];
130 strlcpy(link_path, sysfs_path, sizeof(link_path));
131 strlcat(link_path, devpath, sizeof(link_path));
132 len = readlink(link_path, link_target, sizeof(link_target));
135 link_target[len] = '\0';
136 dbg("path link '%s' points to '%s'", devpath, link_target);
138 for (back = 0; strncmp(&link_target[back * 3], "../", 3) == 0; back++)
140 dbg("base '%s', tail '%s', back %i", devpath, &link_target[back * 3], back);
141 for (i = 0; i <= back; i++) {
142 char *pos = strrchr(devpath, '/');
148 dbg("after moving back '%s'", devpath);
149 strlcat(devpath, "/", size);
150 strlcat(devpath, &link_target[back * 3], size);
154 struct sysfs_device *sysfs_device_get(const char *devpath)
156 char path[PATH_SIZE];
157 char devpath_real[PATH_SIZE];
158 struct sysfs_device *dev = NULL;
159 struct sysfs_dev *sysdev_loop, *sysdev;
161 char link_path[PATH_SIZE];
162 char link_target[PATH_SIZE];
166 /* we handle only these devpathes */
167 if (devpath != NULL &&
168 strncmp(devpath, "/devices/", 9) != 0 &&
169 strncmp(devpath, "/subsystem/", 11) != 0 &&
170 strncmp(devpath, "/module/", 8) != 0 &&
171 strncmp(devpath, "/bus/", 5) != 0 &&
172 strncmp(devpath, "/class/", 7) != 0 &&
173 strncmp(devpath, "/block/", 7) != 0) {
174 dbg("invalid devpath '%s'", devpath);
178 dbg("open '%s'", devpath);
179 strlcpy(devpath_real, devpath, sizeof(devpath_real));
180 remove_trailing_chars(devpath_real, '/');
181 if (devpath[0] == '\0' )
184 /* if we got a link, resolve it to the real device */
185 strlcpy(path, sysfs_path, sizeof(path));
186 strlcat(path, devpath_real, sizeof(path));
187 if (lstat(path, &statbuf) != 0) {
188 /* if stat fails look in the cache */
189 dbg("stat '%s' failed: %s", path, strerror(errno));
190 list_for_each_entry(sysdev_loop, &sysfs_dev_list, node) {
191 if (strcmp(sysdev_loop->dev.devpath, devpath_real) == 0) {
192 dbg("found vanished dev in cache '%s'",
193 sysdev_loop->dev.devpath);
194 return &sysdev_loop->dev;
200 if (S_ISLNK(statbuf.st_mode)) {
201 if (sysfs_resolve_link(devpath_real, sizeof(devpath_real)) != 0)
205 list_for_each_entry(sysdev_loop, &sysfs_dev_list, node) {
206 if (strcmp(sysdev_loop->dev.devpath, devpath_real) == 0) {
207 dbg("found dev in cache '%s'", sysdev_loop->dev.devpath);
208 dev = &sysdev_loop->dev;
213 /* it is a new device */
214 dbg("new device '%s'", devpath_real);
215 sysdev = malloc(sizeof(struct sysfs_dev));
218 memset(sysdev, 0x00, sizeof(struct sysfs_dev));
219 list_add(&sysdev->node, &sysfs_dev_list);
223 sysfs_device_set_values(dev, devpath_real, NULL, NULL);
225 /* get subsystem name */
226 strlcpy(link_path, sysfs_path, sizeof(link_path));
227 strlcat(link_path, dev->devpath, sizeof(link_path));
228 strlcat(link_path, "/subsystem", sizeof(link_path));
229 len = readlink(link_path, link_target, sizeof(link_target));
231 /* get subsystem from "subsystem" link */
232 link_target[len] = '\0';
233 dbg("subsystem link '%s' points to '%s'", link_path, link_target);
234 pos = strrchr(link_target, '/');
236 strlcpy(dev->subsystem, &pos[1], sizeof(dev->subsystem));
237 } else if (strstr(dev->devpath, "/drivers/") != NULL) {
238 strlcpy(dev->subsystem, "drivers", sizeof(dev->subsystem));
239 } else if (strncmp(dev->devpath, "/module/", 8) == 0) {
240 strlcpy(dev->subsystem, "module", sizeof(dev->subsystem));
241 } else if (strncmp(dev->devpath, "/subsystem/", 11) == 0) {
242 pos = strrchr(dev->devpath, '/');
243 if (pos == &dev->devpath[10])
244 strlcpy(dev->subsystem, "subsystem",
245 sizeof(dev->subsystem));
246 } else if (strncmp(dev->devpath, "/class/", 7) == 0) {
247 pos = strrchr(dev->devpath, '/');
248 if (pos == &dev->devpath[6])
249 strlcpy(dev->subsystem, "subsystem",
250 sizeof(dev->subsystem));
251 } else if (strncmp(dev->devpath, "/bus/", 5) == 0) {
252 pos = strrchr(dev->devpath, '/');
253 if (pos == &dev->devpath[4])
254 strlcpy(dev->subsystem, "subsystem",
255 sizeof(dev->subsystem));
258 /* get driver name */
259 strlcpy(link_path, sysfs_path, sizeof(link_path));
260 strlcat(link_path, dev->devpath, sizeof(link_path));
261 strlcat(link_path, "/driver", sizeof(link_path));
262 len = readlink(link_path, link_target, sizeof(link_target));
264 link_target[len] = '\0';
265 dbg("driver link '%s' points to '%s'", link_path, link_target);
266 pos = strrchr(link_target, '/');
268 strlcpy(dev->driver, &pos[1], sizeof(dev->driver));
274 struct sysfs_device *sysfs_device_get_parent(struct sysfs_device *dev)
276 char parent_devpath[PATH_SIZE];
279 dbg("open '%s'", dev->devpath);
281 /* look if we already know the parent */
282 if (dev->parent != NULL)
285 strlcpy(parent_devpath, dev->devpath, sizeof(parent_devpath));
286 dbg("'%s'", parent_devpath);
288 /* strip last element */
289 pos = strrchr(parent_devpath, '/');
290 if (pos == NULL || pos == parent_devpath)
294 if (strncmp(parent_devpath, "/class", 6) == 0) {
295 pos = strrchr(parent_devpath, '/');
296 if (pos == &parent_devpath[6] || pos == parent_devpath) {
297 dbg("/class top level, look for device link");
301 if (strcmp(parent_devpath, "/block") == 0) {
302 dbg("/block top level, look for device link");
306 /* are we at the top level? */
307 pos = strrchr(parent_devpath, '/');
308 if (pos == NULL || pos == parent_devpath)
311 /* get parent and remember it */
312 dev->parent = sysfs_device_get(parent_devpath);
316 strlcpy(parent_devpath, dev->devpath, sizeof(parent_devpath));
317 strlcat(parent_devpath, "/device", sizeof(parent_devpath));
318 if (sysfs_resolve_link(parent_devpath, sizeof(parent_devpath)) != 0)
321 /* get parent and remember it */
322 dev->parent = sysfs_device_get(parent_devpath);
326 struct sysfs_device *sysfs_device_get_parent_with_subsystem(struct sysfs_device *dev, const char *subsystem)
328 struct sysfs_device *dev_parent;
330 dev_parent = sysfs_device_get_parent(dev);
331 while (dev_parent != NULL) {
332 if (strcmp(dev_parent->subsystem, subsystem) == 0)
334 dev_parent = sysfs_device_get_parent(dev_parent);
339 void sysfs_device_put(struct sysfs_device *dev)
341 struct sysfs_dev *sysdev_loop;
343 list_for_each_entry(sysdev_loop, &sysfs_dev_list, node) {
344 if (&sysdev_loop->dev == dev) {
345 dbg("removed dev '%s' from cache",
346 sysdev_loop->dev.devpath);
347 list_del(&sysdev_loop->node);
352 dbg("dev '%s' not found in cache",
353 sysdev_loop->dev.devpath);
358 char *sysfs_attr_get_value(const char *devpath, const char *attr_name)
360 char path_full[PATH_SIZE];
362 char value[NAME_SIZE];
363 struct sysfs_attr *attr_loop;
364 struct sysfs_attr *attr = NULL;
370 dbg("open '%s'/'%s'", devpath, attr_name);
371 sysfs_len = strlcpy(path_full, sysfs_path, sizeof(path_full));
372 if(sysfs_len >= sizeof(path_full))
373 sysfs_len = sizeof(path_full) - 1;
374 path = &path_full[sysfs_len];
375 strlcat(path_full, devpath, sizeof(path_full));
376 strlcat(path_full, "/", sizeof(path_full));
377 strlcat(path_full, attr_name, sizeof(path_full));
379 /* look for attribute in cache */
380 list_for_each_entry(attr_loop, &attr_list, node) {
381 if (strcmp(attr_loop->path, path) == 0) {
382 dbg("found in cache '%s'", attr_loop->path);
387 /* store attribute in cache */
388 dbg("new uncached attribute '%s'", path_full);
389 attr = malloc(sizeof(struct sysfs_attr));
392 memset(attr, 0x00, sizeof(struct sysfs_attr));
393 strlcpy(attr->path, path, sizeof(attr->path));
394 dbg("add to cache '%s'", path_full);
395 list_add(&attr->node, &attr_list);
397 /* clear old value */
399 memset(attr->value, 0x00, sizeof(attr->value));
402 if (lstat(path_full, &statbuf) != 0) {
403 dbg("stat '%s' failed: %s", path_full, strerror(errno));
407 if (S_ISLNK(statbuf.st_mode)) {
408 /* links return the last element of the target path */
409 char link_target[PATH_SIZE];
413 len = readlink(path_full, link_target, sizeof(link_target));
415 link_target[len] = '\0';
416 pos = strrchr(link_target, '/');
418 dbg("cache '%s' with link value '%s'",
420 strlcpy(attr->value_local, &pos[1],
421 sizeof(attr->value_local));
422 attr->value = attr->value_local;
428 /* skip directories */
429 if (S_ISDIR(statbuf.st_mode))
432 /* skip non-readable files */
433 if ((statbuf.st_mode & S_IRUSR) == 0)
436 /* read attribute value */
437 fd = open(path_full, O_RDONLY);
439 dbg("attribute '%s' can not be opened: %s",
440 path_full, strerror(errno));
443 size = read(fd, value, sizeof(value));
447 if (size == sizeof(value))
450 /* got a valid value, store and return it */
452 remove_trailing_chars(value, '\n');
453 dbg("cache '%s' with attribute value '%s'", path_full, value);
454 strlcpy(attr->value_local, value, sizeof(attr->value_local));
455 attr->value = attr->value_local;
461 int sysfs_lookup_devpath_by_subsys_id(char *devpath_full, size_t len,
462 const char *subsystem, const char *id)
465 char path_full[PATH_SIZE];
469 sysfs_len = strlcpy(path_full, sysfs_path, sizeof(path_full));
470 path = &path_full[sysfs_len];
472 if (strcmp(subsystem, "subsystem") == 0) {
473 strlcpy(path, "/subsystem/", sizeof(path_full) - sysfs_len);
474 strlcat(path, id, sizeof(path_full) - sysfs_len);
475 if (stat(path_full, &statbuf) == 0)
478 strlcpy(path, "/bus/", sizeof(path_full) - sysfs_len);
479 strlcat(path, id, sizeof(path_full) - sysfs_len);
480 if (stat(path_full, &statbuf) == 0)
484 strlcpy(path, "/class/", sizeof(path_full) - sysfs_len);
485 strlcat(path, id, sizeof(path_full) - sysfs_len);
486 if (stat(path_full, &statbuf) == 0)
490 if (strcmp(subsystem, "module") == 0) {
491 strlcpy(path, "/module/", sizeof(path_full) - sysfs_len);
492 strlcat(path, id, sizeof(path_full) - sysfs_len);
493 if (stat(path_full, &statbuf) == 0)
498 if (strcmp(subsystem, "drivers") == 0) {
499 char subsys[NAME_SIZE];
502 strlcpy(subsys, id, sizeof(subsys));
503 driver = strchr(subsys, ':');
504 if (driver != NULL) {
507 strlcpy(path, "/subsystem/", sizeof(path_full) - sysfs_len);
508 strlcat(path, subsys, sizeof(path_full) - sysfs_len);
509 strlcat(path, "/drivers/", sizeof(path_full) - sysfs_len);
510 strlcat(path, driver, sizeof(path_full) - sysfs_len);
511 if (stat(path_full, &statbuf) == 0)
514 strlcpy(path, "/bus/", sizeof(path_full) - sysfs_len);
515 strlcat(path, subsys, sizeof(path_full) - sysfs_len);
516 strlcat(path, "/drivers/", sizeof(path_full) - sysfs_len);
517 strlcat(path, driver, sizeof(path_full) - sysfs_len);
518 if (stat(path_full, &statbuf) == 0)
524 strlcpy(path, "/subsystem/", sizeof(path_full) - sysfs_len);
525 strlcat(path, subsystem, sizeof(path_full) - sysfs_len);
526 strlcat(path, "/devices/", sizeof(path_full) - sysfs_len);
527 strlcat(path, id, sizeof(path_full) - sysfs_len);
528 if (stat(path_full, &statbuf) == 0)
531 strlcpy(path, "/bus/", sizeof(path_full) - sysfs_len);
532 strlcat(path, subsystem, sizeof(path_full) - sysfs_len);
533 strlcat(path, "/devices/", sizeof(path_full) - sysfs_len);
534 strlcat(path, id, sizeof(path_full) - sysfs_len);
535 if (stat(path_full, &statbuf) == 0)
538 strlcpy(path, "/class/", sizeof(path_full) - sysfs_len);
539 strlcat(path, subsystem, sizeof(path_full) - sysfs_len);
540 strlcat(path, "/", sizeof(path_full) - sysfs_len);
541 strlcat(path, id, sizeof(path_full) - sysfs_len);
542 if (stat(path_full, &statbuf) == 0)
547 if (S_ISLNK(statbuf.st_mode))
548 sysfs_resolve_link(path, sizeof(path_full) - sysfs_len);
549 strlcpy(devpath_full, path, len);