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'", 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'", 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'", 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'", devpath, link_target);
130 for (back = 0; strncmp(&link_target[back * 3], "../", 3) == 0; back++)
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, '/');
140 dbg("after moving back '%s'", 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'", 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'", 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", 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'", dev_loop->devpath);
202 /* it is a new device */
203 dbg("new uncached device '%s'", 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'", link_path, link_target);
220 pos = strrchr(link_target, '/');
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, '/');
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));
240 link_target[len] = '\0';
241 dbg("bus link '%s' points to '%s'", link_path, link_target);
242 pos = strrchr(link_target, '/');
244 strlcpy(dev->subsystem, &pos[1], sizeof(dev->subsystem));
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));
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));
266 link_target[len] = '\0';
267 dbg("driver link '%s' points to '%s'", link_path, link_target);
268 pos = strrchr(link_target, '/');
270 strlcpy(dev->driver, &pos[1], sizeof(dev->driver));
273 dbg("add to cache 'devpath=%s', subsystem='%s', driver='%s'", dev->devpath, dev->subsystem, dev->driver);
274 list_add(&dev->node, &dev_list);
279 struct sysfs_device *sysfs_device_get_parent(struct sysfs_device *dev)
281 char parent_devpath[PATH_SIZE];
284 dbg("open '%s'", dev->devpath);
286 /* look if we already know the parent */
287 if (dev->parent != NULL)
290 strlcpy(parent_devpath, dev->devpath, sizeof(parent_devpath));
291 dbg("'%s'", parent_devpath);
293 /* strip last element */
294 pos = strrchr(parent_devpath, '/');
295 if (pos == NULL || pos == parent_devpath)
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");
306 if (strcmp(parent_devpath, "/block") == 0) {
307 dbg("/block top level, look for device link");
311 /* are we at the top level? */
312 pos = strrchr(parent_devpath, '/');
313 if (pos == NULL || pos == parent_devpath)
316 /* get parent and remember it */
317 dev->parent = sysfs_device_get(parent_devpath);
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)
326 /* get parent and remember it */
327 dev->parent = sysfs_device_get(parent_devpath);
331 struct sysfs_device *sysfs_device_get_parent_with_subsystem(struct sysfs_device *dev, const char *subsystem)
333 struct sysfs_device *dev_parent;
335 dev_parent = sysfs_device_get_parent(dev);
336 while (dev_parent != NULL) {
337 if (strcmp(dev_parent->subsystem, subsystem) == 0)
339 dev_parent = sysfs_device_get_parent(dev_parent);
344 char *sysfs_attr_get_value(const char *devpath, const char *attr_name)
346 char path_full[PATH_SIZE];
348 char value[NAME_SIZE];
349 struct sysfs_attr *attr_loop;
350 struct sysfs_attr *attr;
356 dbg("open '%s'/'%s'", devpath, attr_name);
357 sysfs_len = strlcpy(path_full, sysfs_path, sizeof(path_full));
358 if(sysfs_len >= sizeof(path_full))
359 sysfs_len = sizeof(path_full) - 1;
360 path = &path_full[sysfs_len];
361 strlcat(path_full, devpath, sizeof(path_full));
362 strlcat(path_full, "/", sizeof(path_full));
363 strlcat(path_full, attr_name, sizeof(path_full));
365 /* look for attribute in cache */
366 list_for_each_entry(attr_loop, &attr_list, node) {
367 if (strcmp(attr_loop->path, path) == 0) {
368 dbg("found in cache '%s'", attr_loop->path);
369 return attr_loop->value;
373 /* store attribute in cache (also negatives are kept in cache) */
374 dbg("new uncached attribute '%s'", path_full);
375 attr = malloc(sizeof(struct sysfs_attr));
378 memset(attr, 0x00, sizeof(struct sysfs_attr));
379 strlcpy(attr->path, path, sizeof(attr->path));
380 dbg("add to cache '%s'", path_full);
381 list_add(&attr->node, &attr_list);
383 if (lstat(path_full, &statbuf) != 0) {
384 dbg("stat '%s' failed: %s", path_full, strerror(errno));
388 if (S_ISLNK(statbuf.st_mode)) {
389 /* links return the last element of the target path */
390 char link_target[PATH_SIZE];
394 len = readlink(path_full, link_target, sizeof(link_target));
396 link_target[len] = '\0';
397 pos = strrchr(link_target, '/');
399 dbg("cache '%s' with link value '%s'", path_full, value);
400 strlcpy(attr->value_local, &pos[1], sizeof(attr->value_local));
401 attr->value = attr->value_local;
407 /* skip directories */
408 if (S_ISDIR(statbuf.st_mode))
411 /* skip non-readable files */
412 if ((statbuf.st_mode & S_IRUSR) == 0)
415 /* read attribute value */
416 fd = open(path_full, O_RDONLY);
418 dbg("attribute '%s' does not exist", path_full);
421 size = read(fd, value, sizeof(value));
425 if (size == sizeof(value))
428 /* got a valid value, store and return it */
430 remove_trailing_chars(value, '\n');
431 dbg("cache '%s' with attribute value '%s'", path_full, value);
432 strlcpy(attr->value_local, value, sizeof(attr->value_local));
433 attr->value = attr->value_local;
439 int sysfs_lookup_devpath_by_subsys_id(char *devpath_full, size_t len, const char *subsystem, const char *id)
442 char path_full[PATH_SIZE];
446 sysfs_len = strlcpy(path_full, sysfs_path, sizeof(path_full));
447 path = &path_full[sysfs_len];
449 if (strcmp(subsystem, "subsystem") == 0) {
450 strlcpy(path, "/subsystem/", sizeof(path_full) - sysfs_len);
451 strlcat(path, id, sizeof(path_full) - sysfs_len);
452 if (stat(path_full, &statbuf) == 0)
455 strlcpy(path, "/bus/", sizeof(path_full) - sysfs_len);
456 strlcat(path, id, sizeof(path_full) - sysfs_len);
457 if (stat(path_full, &statbuf) == 0)
461 strlcpy(path, "/class/", sizeof(path_full) - sysfs_len);
462 strlcat(path, id, sizeof(path_full) - sysfs_len);
463 if (stat(path_full, &statbuf) == 0)
467 if (strcmp(subsystem, "module") == 0) {
468 strlcpy(path, "/module/", sizeof(path_full) - sysfs_len);
469 strlcat(path, id, sizeof(path_full) - sysfs_len);
470 if (stat(path_full, &statbuf) == 0)
475 if (strcmp(subsystem, "drivers") == 0) {
476 char subsys[NAME_SIZE];
479 strlcpy(subsys, id, sizeof(subsys));
480 driver = strchr(subsys, ':');
481 if (driver != NULL) {
484 strlcpy(path, "/subsystem/", sizeof(path_full) - sysfs_len);
485 strlcat(path, subsys, sizeof(path_full) - sysfs_len);
486 strlcat(path, "/drivers/", sizeof(path_full) - sysfs_len);
487 strlcat(path, driver, sizeof(path_full) - sysfs_len);
488 if (stat(path_full, &statbuf) == 0)
491 strlcpy(path, "/bus/", sizeof(path_full) - sysfs_len);
492 strlcat(path, subsys, sizeof(path_full) - sysfs_len);
493 strlcat(path, "/drivers/", sizeof(path_full) - sysfs_len);
494 strlcat(path, driver, sizeof(path_full) - sysfs_len);
495 if (stat(path_full, &statbuf) == 0)
501 strlcpy(path, "/subsystem/", sizeof(path_full) - sysfs_len);
502 strlcat(path, subsystem, sizeof(path_full) - sysfs_len);
503 strlcat(path, "/devices/", sizeof(path_full) - sysfs_len);
504 strlcat(path, id, sizeof(path_full) - sysfs_len);
505 if (stat(path_full, &statbuf) == 0)
508 strlcpy(path, "/bus/", sizeof(path_full) - sysfs_len);
509 strlcat(path, subsystem, sizeof(path_full) - sysfs_len);
510 strlcat(path, "/devices/", sizeof(path_full) - sysfs_len);
511 strlcat(path, id, sizeof(path_full) - sysfs_len);
512 if (stat(path_full, &statbuf) == 0)
515 strlcpy(path, "/class/", sizeof(path_full) - sysfs_len);
516 strlcat(path, subsystem, sizeof(path_full) - sysfs_len);
517 strlcat(path, "/", sizeof(path_full) - sysfs_len);
518 strlcat(path, id, sizeof(path_full) - sysfs_len);
519 if (stat(path_full, &statbuf) == 0)
524 if (S_ISLNK(statbuf.st_mode))
525 sysfs_resolve_link(path, sizeof(path_full) - sysfs_len);
526 strlcpy(devpath_full, path, len);