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.
32 char sysfs_path[PATH_SIZE];
35 static LIST_HEAD(dev_list);
37 /* attribute value cache */
38 static LIST_HEAD(attr_list);
40 struct list_head node;
42 char *value; /* points to value_local if value is cached */
43 char value_local[NAME_SIZE];
50 env = getenv("SYSFS_PATH");
52 strlcpy(sysfs_path, env, sizeof(sysfs_path));
53 remove_trailing_chars(sysfs_path, '/');
55 strlcpy(sysfs_path, "/sys", sizeof(sysfs_path));
56 dbg("sysfs_path='%s'\n", sysfs_path);
58 INIT_LIST_HEAD(&dev_list);
59 INIT_LIST_HEAD(&attr_list);
63 void sysfs_cleanup(void)
65 struct sysfs_attr *attr_loop;
66 struct sysfs_attr *attr_temp;
67 struct sysfs_device *dev_loop;
68 struct sysfs_device *dev_temp;
70 list_for_each_entry_safe(attr_loop, attr_temp, &attr_list, node) {
71 list_del(&attr_loop->node);
75 list_for_each_entry_safe(dev_loop, dev_temp, &dev_list, node) {
76 list_del(&dev_loop->node);
81 void sysfs_device_set_values(struct sysfs_device *dev, const char *devpath,
82 const char *subsystem, const char *driver)
86 strlcpy(dev->devpath, devpath, sizeof(dev->devpath));
87 if (subsystem != NULL)
88 strlcpy(dev->subsystem, subsystem, sizeof(dev->subsystem));
90 strlcpy(dev->driver, driver, sizeof(dev->driver));
93 pos = strrchr(dev->devpath, '/');
96 strlcpy(dev->kernel, &pos[1], sizeof(dev->kernel));
97 dbg("kernel='%s'\n", dev->kernel);
99 /* some devices have '!' in their name, change that to '/' */
101 while (pos[0] != '\0') {
107 /* get kernel number */
108 pos = &dev->kernel[strlen(dev->kernel)];
109 while (isdigit(pos[-1]))
111 strlcpy(dev->kernel_number, pos, sizeof(dev->kernel_number));
112 dbg("kernel_number='%s'\n", dev->kernel_number);
115 int sysfs_resolve_link(char *devpath, size_t size)
117 char link_path[PATH_SIZE];
118 char link_target[PATH_SIZE];
123 strlcpy(link_path, sysfs_path, sizeof(link_path));
124 strlcat(link_path, devpath, sizeof(link_path));
125 len = readlink(link_path, link_target, sizeof(link_target));
128 link_target[len] = '\0';
129 dbg("path link '%s' points to '%s'\n", devpath, link_target);
131 for (back = 0; strncmp(&link_target[back * 3], "../", 3) == 0; back++)
133 dbg("base '%s', tail '%s', back %i\n", devpath, &link_target[back * 3], back);
134 for (i = 0; i <= back; i++) {
135 char *pos = strrchr(devpath, '/');
141 dbg("after moving back '%s'\n", devpath);
142 strlcat(devpath, "/", size);
143 strlcat(devpath, &link_target[back * 3], size);
147 struct sysfs_device *sysfs_device_get(const char *devpath)
149 char path[PATH_SIZE];
150 char devpath_real[PATH_SIZE];
151 struct sysfs_device *dev;
152 struct sysfs_device *dev_loop;
154 char link_path[PATH_SIZE];
155 char link_target[PATH_SIZE];
159 /* we handle only these devpathes */
160 if (devpath != NULL &&
161 strncmp(devpath, "/devices/", 9) != 0 &&
162 strncmp(devpath, "/subsystem/", 11) != 0 &&
163 strncmp(devpath, "/module/", 8) != 0 &&
164 strncmp(devpath, "/bus/", 5) != 0 &&
165 strncmp(devpath, "/class/", 7) != 0 &&
166 strncmp(devpath, "/block/", 7) != 0)
169 dbg("open '%s'\n", devpath);
170 strlcpy(devpath_real, devpath, sizeof(devpath_real));
171 remove_trailing_chars(devpath_real, '/');
172 if (devpath[0] == '\0' )
175 /* look for device already in cache (we never put an untranslated path in the cache) */
176 list_for_each_entry(dev_loop, &dev_list, node) {
177 if (strcmp(dev_loop->devpath, devpath_real) == 0) {
178 dbg("found in cache '%s'\n", dev_loop->devpath);
183 /* if we got a link, resolve it to the real device */
184 strlcpy(path, sysfs_path, sizeof(path));
185 strlcat(path, devpath_real, sizeof(path));
186 if (lstat(path, &statbuf) != 0) {
187 dbg("stat '%s' failed: %s\n", path, strerror(errno));
190 if (S_ISLNK(statbuf.st_mode)) {
191 if (sysfs_resolve_link(devpath_real, sizeof(devpath_real)) != 0)
194 /* now look for device in cache after path translation */
195 list_for_each_entry(dev_loop, &dev_list, node) {
196 if (strcmp(dev_loop->devpath, devpath_real) == 0) {
197 dbg("found in cache '%s'\n", dev_loop->devpath);
203 /* it is a new device */
204 dbg("new uncached device '%s'\n", devpath_real);
205 dev = malloc(sizeof(struct sysfs_device));
208 memset(dev, 0x00, sizeof(struct sysfs_device));
210 sysfs_device_set_values(dev, devpath_real, NULL, NULL);
212 /* get subsystem name */
213 strlcpy(link_path, sysfs_path, sizeof(link_path));
214 strlcat(link_path, dev->devpath, sizeof(link_path));
215 strlcat(link_path, "/subsystem", sizeof(link_path));
216 len = readlink(link_path, link_target, sizeof(link_target));
218 /* get subsystem from "subsystem" link */
219 link_target[len] = '\0';
220 dbg("subsystem link '%s' points to '%s'\n", link_path, link_target);
221 pos = strrchr(link_target, '/');
223 strlcpy(dev->subsystem, &pos[1], sizeof(dev->subsystem));
224 } else if (strstr(dev->devpath, "/drivers/") != NULL) {
225 strlcpy(dev->subsystem, "drivers", sizeof(dev->subsystem));
226 } else if (strncmp(dev->devpath, "/module/", 8) == 0) {
227 strlcpy(dev->subsystem, "module", sizeof(dev->subsystem));
228 } else if (strncmp(dev->devpath, "/subsystem/", 11) == 0) {
229 pos = strrchr(dev->devpath, '/');
230 if (pos == &dev->devpath[10])
231 strlcpy(dev->subsystem, "subsystem", sizeof(dev->subsystem));
232 } else if (strncmp(dev->devpath, "/class/", 7) == 0) {
233 pos = strrchr(dev->devpath, '/');
234 if (pos == &dev->devpath[6])
235 strlcpy(dev->subsystem, "subsystem", sizeof(dev->subsystem));
236 } else if (strncmp(dev->devpath, "/bus/", 5) == 0) {
237 pos = strrchr(dev->devpath, '/');
238 if (pos == &dev->devpath[4])
239 strlcpy(dev->subsystem, "subsystem", sizeof(dev->subsystem));
242 /* get driver name */
243 strlcpy(link_path, sysfs_path, sizeof(link_path));
244 strlcat(link_path, dev->devpath, sizeof(link_path));
245 strlcat(link_path, "/driver", sizeof(link_path));
246 len = readlink(link_path, link_target, sizeof(link_target));
248 link_target[len] = '\0';
249 dbg("driver link '%s' points to '%s'\n", link_path, link_target);
250 pos = strrchr(link_target, '/');
252 strlcpy(dev->driver, &pos[1], sizeof(dev->driver));
255 dbg("add to cache 'devpath=%s', subsystem='%s', driver='%s'\n", dev->devpath, dev->subsystem, dev->driver);
256 list_add(&dev->node, &dev_list);
261 struct sysfs_device *sysfs_device_get_parent(struct sysfs_device *dev)
263 char parent_devpath[PATH_SIZE];
266 dbg("open '%s'\n", dev->devpath);
268 /* look if we already know the parent */
269 if (dev->parent != NULL)
272 strlcpy(parent_devpath, dev->devpath, sizeof(parent_devpath));
273 dbg("'%s'\n", parent_devpath);
275 /* strip last element */
276 pos = strrchr(parent_devpath, '/');
277 if (pos == NULL || pos == parent_devpath)
281 if (strncmp(parent_devpath, "/class", 6) == 0) {
282 pos = strrchr(parent_devpath, '/');
283 if (pos == &parent_devpath[6] || pos == parent_devpath) {
284 dbg("/class top level, look for device link\n");
288 if (strcmp(parent_devpath, "/block") == 0) {
289 dbg("/block top level, look for device link\n");
293 /* are we at the top level? */
294 pos = strrchr(parent_devpath, '/');
295 if (pos == NULL || pos == parent_devpath)
298 /* get parent and remember it */
299 dev->parent = sysfs_device_get(parent_devpath);
303 strlcpy(parent_devpath, dev->devpath, sizeof(parent_devpath));
304 strlcat(parent_devpath, "/device", sizeof(parent_devpath));
305 if (sysfs_resolve_link(parent_devpath, sizeof(parent_devpath)) != 0)
308 /* get parent and remember it */
309 dev->parent = sysfs_device_get(parent_devpath);
313 struct sysfs_device *sysfs_device_get_parent_with_subsystem(struct sysfs_device *dev, const char *subsystem)
315 struct sysfs_device *dev_parent;
317 dev_parent = sysfs_device_get_parent(dev);
318 while (dev_parent != NULL) {
319 if (strcmp(dev_parent->subsystem, subsystem) == 0)
321 dev_parent = sysfs_device_get_parent(dev_parent);
326 char *sysfs_attr_get_value(const char *devpath, const char *attr_name)
328 char path_full[PATH_SIZE];
330 char value[NAME_SIZE];
331 struct sysfs_attr *attr_loop;
332 struct sysfs_attr *attr;
338 dbg("open '%s'/'%s'\n", devpath, attr_name);
339 sysfs_len = strlcpy(path_full, sysfs_path, sizeof(path_full));
340 if(sysfs_len >= sizeof(path_full))
341 sysfs_len = sizeof(path_full) - 1;
342 path = &path_full[sysfs_len];
343 strlcat(path_full, devpath, sizeof(path_full));
344 strlcat(path_full, "/", sizeof(path_full));
345 strlcat(path_full, attr_name, sizeof(path_full));
347 /* look for attribute in cache */
348 list_for_each_entry(attr_loop, &attr_list, node) {
349 if (strcmp(attr_loop->path, path) == 0) {
350 dbg("found in cache '%s'\n", attr_loop->path);
351 return attr_loop->value;
355 /* store attribute in cache (also negatives are kept in cache) */
356 dbg("new uncached attribute '%s'\n", path_full);
357 attr = malloc(sizeof(struct sysfs_attr));
360 memset(attr, 0x00, sizeof(struct sysfs_attr));
361 strlcpy(attr->path, path, sizeof(attr->path));
362 dbg("add to cache '%s'\n", path_full);
363 list_add(&attr->node, &attr_list);
365 if (lstat(path_full, &statbuf) != 0) {
366 dbg("stat '%s' failed: %s\n", path_full, strerror(errno));
370 if (S_ISLNK(statbuf.st_mode)) {
371 /* links return the last element of the target path */
372 char link_target[PATH_SIZE];
376 len = readlink(path_full, link_target, sizeof(link_target));
378 link_target[len] = '\0';
379 pos = strrchr(link_target, '/');
381 dbg("cache '%s' with link value '%s'\n", path_full, value);
382 strlcpy(attr->value_local, &pos[1], sizeof(attr->value_local));
383 attr->value = attr->value_local;
389 /* skip directories */
390 if (S_ISDIR(statbuf.st_mode))
393 /* skip non-readable files */
394 if ((statbuf.st_mode & S_IRUSR) == 0)
397 /* read attribute value */
398 fd = open(path_full, O_RDONLY);
400 dbg("attribute '%s' can not be opened\n", path_full);
403 size = read(fd, value, sizeof(value));
407 if (size == sizeof(value))
410 /* got a valid value, store and return it */
412 remove_trailing_chars(value, '\n');
413 dbg("cache '%s' with attribute value '%s'\n", path_full, value);
414 strlcpy(attr->value_local, value, sizeof(attr->value_local));
415 attr->value = attr->value_local;
421 int sysfs_lookup_devpath_by_subsys_id(char *devpath_full, size_t len, const char *subsystem, const char *id)
424 char path_full[PATH_SIZE];
428 sysfs_len = strlcpy(path_full, sysfs_path, sizeof(path_full));
429 path = &path_full[sysfs_len];
431 if (strcmp(subsystem, "subsystem") == 0) {
432 strlcpy(path, "/subsystem/", sizeof(path_full) - sysfs_len);
433 strlcat(path, id, sizeof(path_full) - sysfs_len);
434 if (stat(path_full, &statbuf) == 0)
437 strlcpy(path, "/bus/", sizeof(path_full) - sysfs_len);
438 strlcat(path, id, sizeof(path_full) - sysfs_len);
439 if (stat(path_full, &statbuf) == 0)
443 strlcpy(path, "/class/", sizeof(path_full) - sysfs_len);
444 strlcat(path, id, sizeof(path_full) - sysfs_len);
445 if (stat(path_full, &statbuf) == 0)
449 if (strcmp(subsystem, "module") == 0) {
450 strlcpy(path, "/module/", sizeof(path_full) - sysfs_len);
451 strlcat(path, id, sizeof(path_full) - sysfs_len);
452 if (stat(path_full, &statbuf) == 0)
457 if (strcmp(subsystem, "drivers") == 0) {
458 char subsys[NAME_SIZE];
461 strlcpy(subsys, id, sizeof(subsys));
462 driver = strchr(subsys, ':');
463 if (driver != NULL) {
466 strlcpy(path, "/subsystem/", sizeof(path_full) - sysfs_len);
467 strlcat(path, subsys, sizeof(path_full) - sysfs_len);
468 strlcat(path, "/drivers/", sizeof(path_full) - sysfs_len);
469 strlcat(path, driver, sizeof(path_full) - sysfs_len);
470 if (stat(path_full, &statbuf) == 0)
473 strlcpy(path, "/bus/", sizeof(path_full) - sysfs_len);
474 strlcat(path, subsys, sizeof(path_full) - sysfs_len);
475 strlcat(path, "/drivers/", sizeof(path_full) - sysfs_len);
476 strlcat(path, driver, sizeof(path_full) - sysfs_len);
477 if (stat(path_full, &statbuf) == 0)
483 strlcpy(path, "/subsystem/", sizeof(path_full) - sysfs_len);
484 strlcat(path, subsystem, sizeof(path_full) - sysfs_len);
485 strlcat(path, "/devices/", sizeof(path_full) - sysfs_len);
486 strlcat(path, id, sizeof(path_full) - sysfs_len);
487 if (stat(path_full, &statbuf) == 0)
490 strlcpy(path, "/bus/", sizeof(path_full) - sysfs_len);
491 strlcat(path, subsystem, sizeof(path_full) - sysfs_len);
492 strlcat(path, "/devices/", sizeof(path_full) - sysfs_len);
493 strlcat(path, id, sizeof(path_full) - sysfs_len);
494 if (stat(path_full, &statbuf) == 0)
497 strlcpy(path, "/class/", sizeof(path_full) - sysfs_len);
498 strlcat(path, subsystem, sizeof(path_full) - sysfs_len);
499 strlcat(path, "/", sizeof(path_full) - sysfs_len);
500 strlcat(path, id, sizeof(path_full) - sysfs_len);
501 if (stat(path_full, &statbuf) == 0)
506 if (S_ISLNK(statbuf.st_mode))
507 sysfs_resolve_link(path, sizeof(path_full) - sysfs_len);
508 strlcpy(devpath_full, path, len);