2 * Copyright (C) 2005-2008 Kay Sievers <kay.sievers@vrfy.org>
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 static LIST_HEAD(dev_list);
33 /* attribute value cache */
34 static LIST_HEAD(attr_list);
37 struct list_head node;
39 char *value; /* points to value_local if value is cached */
40 char value_local[NAME_SIZE];
45 INIT_LIST_HEAD(&dev_list);
46 INIT_LIST_HEAD(&attr_list);
50 void sysfs_cleanup(void)
52 struct sysfs_attr *attr_loop;
53 struct sysfs_attr *attr_temp;
54 struct sysfs_device *dev_loop;
55 struct sysfs_device *dev_temp;
57 list_for_each_entry_safe(attr_loop, attr_temp, &attr_list, node) {
58 list_del(&attr_loop->node);
62 list_for_each_entry_safe(dev_loop, dev_temp, &dev_list, node) {
63 list_del(&dev_loop->node);
68 void sysfs_device_set_values(struct udev *udev,
69 struct sysfs_device *dev, const char *devpath,
70 const char *subsystem, const char *driver)
74 util_strlcpy(dev->devpath, devpath, sizeof(dev->devpath));
75 if (subsystem != NULL)
76 util_strlcpy(dev->subsystem, subsystem, sizeof(dev->subsystem));
78 util_strlcpy(dev->driver, driver, sizeof(dev->driver));
81 pos = strrchr(dev->devpath, '/');
84 util_strlcpy(dev->kernel, &pos[1], sizeof(dev->kernel));
85 dbg(udev, "kernel='%s'\n", dev->kernel);
87 /* some devices have '!' in their name, change that to '/' */
89 while (pos[0] != '\0') {
95 /* get kernel number */
96 pos = &dev->kernel[strlen(dev->kernel)];
97 while (isdigit(pos[-1]))
99 util_strlcpy(dev->kernel_number, pos, sizeof(dev->kernel_number));
100 dbg(udev, "kernel_number='%s'\n", dev->kernel_number);
103 int sysfs_resolve_link(struct udev *udev, char *devpath, size_t size)
105 char link_path[PATH_SIZE];
106 char link_target[PATH_SIZE];
111 util_strlcpy(link_path, udev_get_sys_path(udev), sizeof(link_path));
112 util_strlcat(link_path, devpath, sizeof(link_path));
113 len = readlink(link_path, link_target, sizeof(link_target));
116 link_target[len] = '\0';
117 dbg(udev, "path link '%s' points to '%s'\n", devpath, link_target);
119 for (back = 0; strncmp(&link_target[back * 3], "../", 3) == 0; back++)
121 dbg(udev, "base '%s', tail '%s', back %i\n", devpath, &link_target[back * 3], back);
122 for (i = 0; i <= back; i++) {
123 char *pos = strrchr(devpath, '/');
129 dbg(udev, "after moving back '%s'\n", devpath);
130 util_strlcat(devpath, "/", size);
131 util_strlcat(devpath, &link_target[back * 3], size);
135 struct sysfs_device *sysfs_device_get(struct udev *udev, const char *devpath)
137 char path[PATH_SIZE];
138 char devpath_real[PATH_SIZE];
139 struct sysfs_device *dev;
140 struct sysfs_device *dev_loop;
142 char link_path[PATH_SIZE];
143 char link_target[PATH_SIZE];
147 /* we handle only these devpathes */
148 if (devpath != NULL &&
149 strncmp(devpath, "/devices/", 9) != 0 &&
150 strncmp(devpath, "/subsystem/", 11) != 0 &&
151 strncmp(devpath, "/module/", 8) != 0 &&
152 strncmp(devpath, "/bus/", 5) != 0 &&
153 strncmp(devpath, "/class/", 7) != 0 &&
154 strncmp(devpath, "/block/", 7) != 0)
157 dbg(udev, "open '%s'\n", devpath);
158 util_strlcpy(devpath_real, devpath, sizeof(devpath_real));
159 util_remove_trailing_chars(devpath_real, '/');
160 if (devpath[0] == '\0' )
163 /* look for device already in cache (we never put an untranslated path in the cache) */
164 list_for_each_entry(dev_loop, &dev_list, node) {
165 if (strcmp(dev_loop->devpath, devpath_real) == 0) {
166 dbg(udev, "found in cache '%s'\n", dev_loop->devpath);
171 /* if we got a link, resolve it to the real device */
172 util_strlcpy(path, udev_get_sys_path(udev), sizeof(path));
173 util_strlcat(path, devpath_real, sizeof(path));
174 if (lstat(path, &statbuf) != 0) {
175 dbg(udev, "stat '%s' failed: %s\n", path, strerror(errno));
178 if (S_ISLNK(statbuf.st_mode)) {
179 if (sysfs_resolve_link(udev, devpath_real, sizeof(devpath_real)) != 0)
182 /* now look for device in cache after path translation */
183 list_for_each_entry(dev_loop, &dev_list, node) {
184 if (strcmp(dev_loop->devpath, devpath_real) == 0) {
185 dbg(udev, "found in cache '%s'\n", dev_loop->devpath);
191 /* it is a new device */
192 dbg(udev, "new uncached device '%s'\n", devpath_real);
193 dev = malloc(sizeof(struct sysfs_device));
196 memset(dev, 0x00, sizeof(struct sysfs_device));
198 sysfs_device_set_values(udev, dev, devpath_real, NULL, NULL);
200 /* get subsystem name */
201 util_strlcpy(link_path, udev_get_sys_path(udev), sizeof(link_path));
202 util_strlcat(link_path, dev->devpath, sizeof(link_path));
203 util_strlcat(link_path, "/subsystem", sizeof(link_path));
204 len = readlink(link_path, link_target, sizeof(link_target));
206 /* get subsystem from "subsystem" link */
207 link_target[len] = '\0';
208 dbg(udev, "subsystem link '%s' points to '%s'\n", link_path, link_target);
209 pos = strrchr(link_target, '/');
211 util_strlcpy(dev->subsystem, &pos[1], sizeof(dev->subsystem));
212 } else if (strstr(dev->devpath, "/drivers/") != NULL) {
213 util_strlcpy(dev->subsystem, "drivers", sizeof(dev->subsystem));
214 } else if (strncmp(dev->devpath, "/module/", 8) == 0) {
215 util_strlcpy(dev->subsystem, "module", sizeof(dev->subsystem));
216 } else if (strncmp(dev->devpath, "/subsystem/", 11) == 0) {
217 pos = strrchr(dev->devpath, '/');
218 if (pos == &dev->devpath[10])
219 util_strlcpy(dev->subsystem, "subsystem", sizeof(dev->subsystem));
220 } else if (strncmp(dev->devpath, "/class/", 7) == 0) {
221 pos = strrchr(dev->devpath, '/');
222 if (pos == &dev->devpath[6])
223 util_strlcpy(dev->subsystem, "subsystem", sizeof(dev->subsystem));
224 } else if (strncmp(dev->devpath, "/bus/", 5) == 0) {
225 pos = strrchr(dev->devpath, '/');
226 if (pos == &dev->devpath[4])
227 util_strlcpy(dev->subsystem, "subsystem", sizeof(dev->subsystem));
230 /* get driver name */
231 util_strlcpy(link_path, udev_get_sys_path(udev), sizeof(link_path));
232 util_strlcat(link_path, dev->devpath, sizeof(link_path));
233 util_strlcat(link_path, "/driver", sizeof(link_path));
234 len = readlink(link_path, link_target, sizeof(link_target));
236 link_target[len] = '\0';
237 dbg(udev, "driver link '%s' points to '%s'\n", link_path, link_target);
238 pos = strrchr(link_target, '/');
240 util_strlcpy(dev->driver, &pos[1], sizeof(dev->driver));
243 dbg(udev, "add to cache 'devpath=%s', subsystem='%s', driver='%s'\n", dev->devpath, dev->subsystem, dev->driver);
244 list_add(&dev->node, &dev_list);
249 struct sysfs_device *sysfs_device_get_parent(struct udev *udev, struct sysfs_device *dev)
251 char parent_devpath[PATH_SIZE];
254 dbg(udev, "open '%s'\n", dev->devpath);
256 /* look if we already know the parent */
257 if (dev->parent != NULL)
260 util_strlcpy(parent_devpath, dev->devpath, sizeof(parent_devpath));
261 dbg(udev, "'%s'\n", parent_devpath);
263 /* strip last element */
264 pos = strrchr(parent_devpath, '/');
265 if (pos == NULL || pos == parent_devpath)
269 if (strncmp(parent_devpath, "/class", 6) == 0) {
270 pos = strrchr(parent_devpath, '/');
271 if (pos == &parent_devpath[6] || pos == parent_devpath) {
272 dbg(udev, "/class top level, look for device link\n");
276 if (strcmp(parent_devpath, "/block") == 0) {
277 dbg(udev, "/block top level, look for device link\n");
281 /* are we at the top level? */
282 pos = strrchr(parent_devpath, '/');
283 if (pos == NULL || pos == parent_devpath)
286 /* get parent and remember it */
287 dev->parent = sysfs_device_get(udev, parent_devpath);
291 util_strlcpy(parent_devpath, dev->devpath, sizeof(parent_devpath));
292 util_strlcat(parent_devpath, "/device", sizeof(parent_devpath));
293 if (sysfs_resolve_link(udev, parent_devpath, sizeof(parent_devpath)) != 0)
296 /* get parent and remember it */
297 dev->parent = sysfs_device_get(udev, parent_devpath);
301 struct sysfs_device *sysfs_device_get_parent_with_subsystem(struct udev *udev, struct sysfs_device *dev, const char *subsystem)
303 struct sysfs_device *dev_parent;
305 dev_parent = sysfs_device_get_parent(udev, dev);
306 while (dev_parent != NULL) {
307 if (strcmp(dev_parent->subsystem, subsystem) == 0)
309 dev_parent = sysfs_device_get_parent(udev, dev_parent);
314 char *sysfs_attr_get_value(struct udev *udev, const char *devpath, const char *attr_name)
316 char path_full[PATH_SIZE];
318 char value[NAME_SIZE];
319 struct sysfs_attr *attr_loop;
320 struct sysfs_attr *attr;
326 dbg(udev, "open '%s'/'%s'\n", devpath, attr_name);
327 sysfs_len = util_strlcpy(path_full, udev_get_sys_path(udev), sizeof(path_full));
328 if(sysfs_len >= sizeof(path_full))
329 sysfs_len = sizeof(path_full) - 1;
330 path = &path_full[sysfs_len];
331 util_strlcat(path_full, devpath, sizeof(path_full));
332 util_strlcat(path_full, "/", sizeof(path_full));
333 util_strlcat(path_full, attr_name, sizeof(path_full));
335 /* look for attribute in cache */
336 list_for_each_entry(attr_loop, &attr_list, node) {
337 if (strcmp(attr_loop->path, path) == 0) {
338 dbg(udev, "found in cache '%s'\n", attr_loop->path);
339 return attr_loop->value;
343 /* store attribute in cache (also negatives are kept in cache) */
344 dbg(udev, "new uncached attribute '%s'\n", path_full);
345 attr = malloc(sizeof(struct sysfs_attr));
348 memset(attr, 0x00, sizeof(struct sysfs_attr));
349 util_strlcpy(attr->path, path, sizeof(attr->path));
350 dbg(udev, "add to cache '%s'\n", path_full);
351 list_add(&attr->node, &attr_list);
353 if (lstat(path_full, &statbuf) != 0) {
354 dbg(udev, "stat '%s' failed: %s\n", path_full, strerror(errno));
358 if (S_ISLNK(statbuf.st_mode)) {
359 /* links return the last element of the target path */
360 char link_target[PATH_SIZE];
364 len = readlink(path_full, link_target, sizeof(link_target));
366 link_target[len] = '\0';
367 pos = strrchr(link_target, '/');
369 dbg(udev, "cache '%s' with link value '%s'\n", path_full, value);
370 util_strlcpy(attr->value_local, &pos[1], sizeof(attr->value_local));
371 attr->value = attr->value_local;
377 /* skip directories */
378 if (S_ISDIR(statbuf.st_mode))
381 /* skip non-readable files */
382 if ((statbuf.st_mode & S_IRUSR) == 0)
385 /* read attribute value */
386 fd = open(path_full, O_RDONLY);
388 dbg(udev, "attribute '%s' can not be opened\n", path_full);
391 size = read(fd, value, sizeof(value));
395 if (size == sizeof(value))
398 /* got a valid value, store and return it */
400 util_remove_trailing_chars(value, '\n');
401 dbg(udev, "cache '%s' with attribute value '%s'\n", path_full, value);
402 util_strlcpy(attr->value_local, value, sizeof(attr->value_local));
403 attr->value = attr->value_local;
409 int sysfs_lookup_devpath_by_subsys_id(struct udev *udev, char *devpath_full, size_t len, const char *subsystem, const char *id)
412 char path_full[PATH_SIZE];
416 sysfs_len = util_strlcpy(path_full, udev_get_sys_path(udev), sizeof(path_full));
417 path = &path_full[sysfs_len];
419 if (strcmp(subsystem, "subsystem") == 0) {
420 util_strlcpy(path, "/subsystem/", sizeof(path_full) - sysfs_len);
421 util_strlcat(path, id, sizeof(path_full) - sysfs_len);
422 if (stat(path_full, &statbuf) == 0)
425 util_strlcpy(path, "/bus/", sizeof(path_full) - sysfs_len);
426 util_strlcat(path, id, sizeof(path_full) - sysfs_len);
427 if (stat(path_full, &statbuf) == 0)
431 util_strlcpy(path, "/class/", sizeof(path_full) - sysfs_len);
432 util_strlcat(path, id, sizeof(path_full) - sysfs_len);
433 if (stat(path_full, &statbuf) == 0)
437 if (strcmp(subsystem, "module") == 0) {
438 util_strlcpy(path, "/module/", sizeof(path_full) - sysfs_len);
439 util_strlcat(path, id, sizeof(path_full) - sysfs_len);
440 if (stat(path_full, &statbuf) == 0)
445 if (strcmp(subsystem, "drivers") == 0) {
446 char subsys[NAME_SIZE];
449 util_strlcpy(subsys, id, sizeof(subsys));
450 driver = strchr(subsys, ':');
451 if (driver != NULL) {
454 util_strlcpy(path, "/subsystem/", sizeof(path_full) - sysfs_len);
455 util_strlcat(path, subsys, sizeof(path_full) - sysfs_len);
456 util_strlcat(path, "/drivers/", sizeof(path_full) - sysfs_len);
457 util_strlcat(path, driver, sizeof(path_full) - sysfs_len);
458 if (stat(path_full, &statbuf) == 0)
461 util_strlcpy(path, "/bus/", sizeof(path_full) - sysfs_len);
462 util_strlcat(path, subsys, sizeof(path_full) - sysfs_len);
463 util_strlcat(path, "/drivers/", sizeof(path_full) - sysfs_len);
464 util_strlcat(path, driver, sizeof(path_full) - sysfs_len);
465 if (stat(path_full, &statbuf) == 0)
471 util_strlcpy(path, "/subsystem/", sizeof(path_full) - sysfs_len);
472 util_strlcat(path, subsystem, sizeof(path_full) - sysfs_len);
473 util_strlcat(path, "/devices/", sizeof(path_full) - sysfs_len);
474 util_strlcat(path, id, sizeof(path_full) - sysfs_len);
475 if (stat(path_full, &statbuf) == 0)
478 util_strlcpy(path, "/bus/", sizeof(path_full) - sysfs_len);
479 util_strlcat(path, subsystem, sizeof(path_full) - sysfs_len);
480 util_strlcat(path, "/devices/", sizeof(path_full) - sysfs_len);
481 util_strlcat(path, id, sizeof(path_full) - sysfs_len);
482 if (stat(path_full, &statbuf) == 0)
485 util_strlcpy(path, "/class/", sizeof(path_full) - sysfs_len);
486 util_strlcat(path, subsystem, sizeof(path_full) - sysfs_len);
487 util_strlcat(path, "/", sizeof(path_full) - sysfs_len);
488 util_strlcat(path, id, sizeof(path_full) - sysfs_len);
489 if (stat(path_full, &statbuf) == 0)
494 if (S_ISLNK(statbuf.st_mode))
495 sysfs_resolve_link(udev, path, sizeof(path_full) - sysfs_len);
496 util_strlcpy(devpath_full, path, len);