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;
38 char path[UTIL_PATH_SIZE];
39 char *value; /* points to value_local if value is cached */
40 char value_local[UTIL_NAME_SIZE];
43 static int resolve_sys_link(struct udev *udev, char *path, size_t size)
45 char link_path[UTIL_PATH_SIZE];
46 char link_target[UTIL_PATH_SIZE];
52 util_strlcpy(link_path, udev_get_sys_path(udev), sizeof(link_path));
53 util_strlcat(link_path, path, sizeof(link_path));
54 len = readlink(link_path, link_target, sizeof(link_target));
57 link_target[len] = '\0';
58 dbg(udev, "path link '%s' points to '%s'\n", path, link_target);
60 for (back = 0; strncmp(&link_target[back * 3], "../", 3) == 0; back++)
62 dbg(udev, "base '%s', tail '%s', back %i\n", path, &link_target[back * 3], back);
63 for (i = 0; i <= back; i++) {
64 char *pos = strrchr(path, '/');
70 dbg(udev, "after moving back '%s'\n", path);
71 util_strlcat(path, "/", size);
72 util_strlcat(path, &link_target[back * 3], size);
78 INIT_LIST_HEAD(&dev_list);
79 INIT_LIST_HEAD(&attr_list);
83 void sysfs_cleanup(void)
85 struct sysfs_attr *attr_loop;
86 struct sysfs_attr *attr_temp;
87 struct sysfs_device *dev_loop;
88 struct sysfs_device *dev_temp;
90 list_for_each_entry_safe(attr_loop, attr_temp, &attr_list, node) {
91 list_del(&attr_loop->node);
95 list_for_each_entry_safe(dev_loop, dev_temp, &dev_list, node) {
96 list_del(&dev_loop->node);
101 void sysfs_device_set_values(struct udev *udev,
102 struct sysfs_device *dev, const char *devpath,
103 const char *subsystem, const char *driver)
107 util_strlcpy(dev->devpath, devpath, sizeof(dev->devpath));
108 if (subsystem != NULL)
109 util_strlcpy(dev->subsystem, subsystem, sizeof(dev->subsystem));
111 util_strlcpy(dev->driver, driver, sizeof(dev->driver));
113 /* set kernel name */
114 pos = strrchr(dev->devpath, '/');
117 util_strlcpy(dev->kernel, &pos[1], sizeof(dev->kernel));
118 dbg(udev, "kernel='%s'\n", dev->kernel);
120 /* some devices have '!' in their name, change that to '/' */
122 while (pos[0] != '\0') {
128 /* get kernel number */
129 pos = &dev->kernel[strlen(dev->kernel)];
130 while (isdigit(pos[-1]))
132 util_strlcpy(dev->kernel_number, pos, sizeof(dev->kernel_number));
133 dbg(udev, "kernel_number='%s'\n", dev->kernel_number);
136 struct sysfs_device *sysfs_device_get(struct udev *udev, const char *devpath)
138 char path[UTIL_PATH_SIZE];
139 char devpath_real[UTIL_PATH_SIZE];
140 struct sysfs_device *dev;
141 struct sysfs_device *dev_loop;
143 char link_path[UTIL_PATH_SIZE];
144 char link_target[UTIL_PATH_SIZE];
148 /* we handle only these devpathes */
149 if (devpath != NULL &&
150 strncmp(devpath, "/devices/", 9) != 0 &&
151 strncmp(devpath, "/subsystem/", 11) != 0 &&
152 strncmp(devpath, "/module/", 8) != 0 &&
153 strncmp(devpath, "/bus/", 5) != 0 &&
154 strncmp(devpath, "/class/", 7) != 0 &&
155 strncmp(devpath, "/block/", 7) != 0)
158 dbg(udev, "open '%s'\n", devpath);
159 util_strlcpy(devpath_real, devpath, sizeof(devpath_real));
160 util_remove_trailing_chars(devpath_real, '/');
161 if (devpath[0] == '\0' )
164 /* look for device already in cache (we never put an untranslated path in the cache) */
165 list_for_each_entry(dev_loop, &dev_list, node) {
166 if (strcmp(dev_loop->devpath, devpath_real) == 0) {
167 dbg(udev, "found in cache '%s'\n", dev_loop->devpath);
172 /* if we got a link, resolve it to the real device */
173 util_strlcpy(path, udev_get_sys_path(udev), sizeof(path));
174 util_strlcat(path, devpath_real, sizeof(path));
175 if (lstat(path, &statbuf) != 0) {
176 dbg(udev, "stat '%s' failed: %m\n", path);
179 if (S_ISLNK(statbuf.st_mode)) {
180 if (resolve_sys_link(udev, devpath_real, sizeof(devpath_real)) != 0)
183 /* now look for device in cache after path translation */
184 list_for_each_entry(dev_loop, &dev_list, node) {
185 if (strcmp(dev_loop->devpath, devpath_real) == 0) {
186 dbg(udev, "found in cache '%s'\n", dev_loop->devpath);
192 /* it is a new device */
193 dbg(udev, "new uncached device '%s'\n", devpath_real);
194 dev = malloc(sizeof(struct sysfs_device));
197 memset(dev, 0x00, sizeof(struct sysfs_device));
199 sysfs_device_set_values(udev, dev, devpath_real, NULL, NULL);
201 /* get subsystem name */
202 util_strlcpy(link_path, udev_get_sys_path(udev), sizeof(link_path));
203 util_strlcat(link_path, dev->devpath, sizeof(link_path));
204 util_strlcat(link_path, "/subsystem", sizeof(link_path));
205 len = readlink(link_path, link_target, sizeof(link_target));
207 /* get subsystem from "subsystem" link */
208 link_target[len] = '\0';
209 dbg(udev, "subsystem link '%s' points to '%s'\n", link_path, link_target);
210 pos = strrchr(link_target, '/');
212 util_strlcpy(dev->subsystem, &pos[1], sizeof(dev->subsystem));
213 } else if (strstr(dev->devpath, "/drivers/") != NULL) {
214 util_strlcpy(dev->subsystem, "drivers", sizeof(dev->subsystem));
215 } else if (strncmp(dev->devpath, "/module/", 8) == 0) {
216 util_strlcpy(dev->subsystem, "module", sizeof(dev->subsystem));
217 } else if (strncmp(dev->devpath, "/subsystem/", 11) == 0) {
218 pos = strrchr(dev->devpath, '/');
219 if (pos == &dev->devpath[10])
220 util_strlcpy(dev->subsystem, "subsystem", sizeof(dev->subsystem));
221 } else if (strncmp(dev->devpath, "/class/", 7) == 0) {
222 pos = strrchr(dev->devpath, '/');
223 if (pos == &dev->devpath[6])
224 util_strlcpy(dev->subsystem, "subsystem", sizeof(dev->subsystem));
225 } else if (strncmp(dev->devpath, "/bus/", 5) == 0) {
226 pos = strrchr(dev->devpath, '/');
227 if (pos == &dev->devpath[4])
228 util_strlcpy(dev->subsystem, "subsystem", sizeof(dev->subsystem));
231 /* get driver name */
232 util_strlcpy(link_path, udev_get_sys_path(udev), sizeof(link_path));
233 util_strlcat(link_path, dev->devpath, sizeof(link_path));
234 util_strlcat(link_path, "/driver", sizeof(link_path));
235 len = readlink(link_path, link_target, sizeof(link_target));
237 link_target[len] = '\0';
238 dbg(udev, "driver link '%s' points to '%s'\n", link_path, link_target);
239 pos = strrchr(link_target, '/');
241 util_strlcpy(dev->driver, &pos[1], sizeof(dev->driver));
244 dbg(udev, "add to cache 'devpath=%s', subsystem='%s', driver='%s'\n", dev->devpath, dev->subsystem, dev->driver);
245 list_add(&dev->node, &dev_list);
250 struct sysfs_device *sysfs_device_get_parent(struct udev *udev, struct sysfs_device *dev)
252 char parent_devpath[UTIL_PATH_SIZE];
255 dbg(udev, "open '%s'\n", dev->devpath);
257 /* look if we already know the parent */
258 if (dev->parent != NULL)
261 util_strlcpy(parent_devpath, dev->devpath, sizeof(parent_devpath));
262 dbg(udev, "'%s'\n", parent_devpath);
264 /* strip last element */
265 pos = strrchr(parent_devpath, '/');
266 if (pos == NULL || pos == parent_devpath)
270 if (strncmp(parent_devpath, "/class", 6) == 0) {
271 pos = strrchr(parent_devpath, '/');
272 if (pos == &parent_devpath[6] || pos == parent_devpath) {
273 dbg(udev, "/class top level, look for device link\n");
277 if (strcmp(parent_devpath, "/block") == 0) {
278 dbg(udev, "/block top level, look for device link\n");
282 /* are we at the top level? */
283 pos = strrchr(parent_devpath, '/');
284 if (pos == NULL || pos == parent_devpath)
287 /* get parent and remember it */
288 dev->parent = sysfs_device_get(udev, parent_devpath);
292 util_strlcpy(parent_devpath, dev->devpath, sizeof(parent_devpath));
293 util_strlcat(parent_devpath, "/device", sizeof(parent_devpath));
294 if (resolve_sys_link(udev, parent_devpath, sizeof(parent_devpath)) != 0)
297 /* get parent and remember it */
298 dev->parent = sysfs_device_get(udev, parent_devpath);
302 struct sysfs_device *sysfs_device_get_parent_with_subsystem(struct udev *udev, struct sysfs_device *dev, const char *subsystem)
304 struct sysfs_device *dev_parent;
306 dev_parent = sysfs_device_get_parent(udev, dev);
307 while (dev_parent != NULL) {
308 if (strcmp(dev_parent->subsystem, subsystem) == 0)
310 dev_parent = sysfs_device_get_parent(udev, dev_parent);
315 char *sysfs_attr_get_value(struct udev *udev, const char *devpath, const char *attr_name)
317 char path_full[UTIL_PATH_SIZE];
319 char value[UTIL_NAME_SIZE];
320 struct sysfs_attr *attr_loop;
321 struct sysfs_attr *attr;
327 dbg(udev, "open '%s'/'%s'\n", devpath, attr_name);
328 sysfs_len = util_strlcpy(path_full, udev_get_sys_path(udev), sizeof(path_full));
329 if(sysfs_len >= sizeof(path_full))
330 sysfs_len = sizeof(path_full) - 1;
331 path = &path_full[sysfs_len];
332 util_strlcat(path_full, devpath, sizeof(path_full));
333 util_strlcat(path_full, "/", sizeof(path_full));
334 util_strlcat(path_full, attr_name, sizeof(path_full));
336 /* look for attribute in cache */
337 list_for_each_entry(attr_loop, &attr_list, node) {
338 if (strcmp(attr_loop->path, path) == 0) {
339 dbg(udev, "found in cache '%s'\n", attr_loop->path);
340 return attr_loop->value;
344 /* store attribute in cache (also negatives are kept in cache) */
345 dbg(udev, "new uncached attribute '%s'\n", path_full);
346 attr = malloc(sizeof(struct sysfs_attr));
349 memset(attr, 0x00, sizeof(struct sysfs_attr));
350 util_strlcpy(attr->path, path, sizeof(attr->path));
351 dbg(udev, "add to cache '%s'\n", path_full);
352 list_add(&attr->node, &attr_list);
354 if (lstat(path_full, &statbuf) != 0) {
355 dbg(udev, "stat '%s' failed: %m\n", path_full);
359 if (S_ISLNK(statbuf.st_mode)) {
360 /* links return the last element of the target path */
361 char link_target[UTIL_PATH_SIZE];
365 len = readlink(path_full, link_target, sizeof(link_target));
367 link_target[len] = '\0';
368 pos = strrchr(link_target, '/');
370 dbg(udev, "cache '%s' with link value '%s'\n", path_full, value);
371 util_strlcpy(attr->value_local, &pos[1], sizeof(attr->value_local));
372 attr->value = attr->value_local;
378 /* skip directories */
379 if (S_ISDIR(statbuf.st_mode))
382 /* skip non-readable files */
383 if ((statbuf.st_mode & S_IRUSR) == 0)
386 /* read attribute value */
387 fd = open(path_full, O_RDONLY);
389 dbg(udev, "attribute '%s' can not be opened\n", path_full);
392 size = read(fd, value, sizeof(value));
396 if (size == sizeof(value))
399 /* got a valid value, store and return it */
401 util_remove_trailing_chars(value, '\n');
402 dbg(udev, "cache '%s' with attribute value '%s'\n", path_full, value);
403 util_strlcpy(attr->value_local, value, sizeof(attr->value_local));
404 attr->value = attr->value_local;
410 int sysfs_lookup_devpath_by_subsys_id(struct udev *udev, char *devpath_full, size_t len, const char *subsystem, const char *id)
413 char path_full[UTIL_PATH_SIZE];
417 sysfs_len = util_strlcpy(path_full, udev_get_sys_path(udev), sizeof(path_full));
418 path = &path_full[sysfs_len];
420 if (strcmp(subsystem, "subsystem") == 0) {
421 util_strlcpy(path, "/subsystem/", sizeof(path_full) - sysfs_len);
422 util_strlcat(path, id, sizeof(path_full) - sysfs_len);
423 if (stat(path_full, &statbuf) == 0)
426 util_strlcpy(path, "/bus/", sizeof(path_full) - sysfs_len);
427 util_strlcat(path, id, sizeof(path_full) - sysfs_len);
428 if (stat(path_full, &statbuf) == 0)
432 util_strlcpy(path, "/class/", sizeof(path_full) - sysfs_len);
433 util_strlcat(path, id, sizeof(path_full) - sysfs_len);
434 if (stat(path_full, &statbuf) == 0)
438 if (strcmp(subsystem, "module") == 0) {
439 util_strlcpy(path, "/module/", sizeof(path_full) - sysfs_len);
440 util_strlcat(path, id, sizeof(path_full) - sysfs_len);
441 if (stat(path_full, &statbuf) == 0)
446 if (strcmp(subsystem, "drivers") == 0) {
447 char subsys[UTIL_NAME_SIZE];
450 util_strlcpy(subsys, id, sizeof(subsys));
451 driver = strchr(subsys, ':');
452 if (driver != NULL) {
455 util_strlcpy(path, "/subsystem/", sizeof(path_full) - sysfs_len);
456 util_strlcat(path, subsys, sizeof(path_full) - sysfs_len);
457 util_strlcat(path, "/drivers/", sizeof(path_full) - sysfs_len);
458 util_strlcat(path, driver, sizeof(path_full) - sysfs_len);
459 if (stat(path_full, &statbuf) == 0)
462 util_strlcpy(path, "/bus/", sizeof(path_full) - sysfs_len);
463 util_strlcat(path, subsys, sizeof(path_full) - sysfs_len);
464 util_strlcat(path, "/drivers/", sizeof(path_full) - sysfs_len);
465 util_strlcat(path, driver, sizeof(path_full) - sysfs_len);
466 if (stat(path_full, &statbuf) == 0)
472 util_strlcpy(path, "/subsystem/", sizeof(path_full) - sysfs_len);
473 util_strlcat(path, subsystem, sizeof(path_full) - sysfs_len);
474 util_strlcat(path, "/devices/", sizeof(path_full) - sysfs_len);
475 util_strlcat(path, id, sizeof(path_full) - sysfs_len);
476 if (stat(path_full, &statbuf) == 0)
479 util_strlcpy(path, "/bus/", sizeof(path_full) - sysfs_len);
480 util_strlcat(path, subsystem, sizeof(path_full) - sysfs_len);
481 util_strlcat(path, "/devices/", sizeof(path_full) - sysfs_len);
482 util_strlcat(path, id, sizeof(path_full) - sysfs_len);
483 if (stat(path_full, &statbuf) == 0)
486 util_strlcpy(path, "/class/", sizeof(path_full) - sysfs_len);
487 util_strlcat(path, subsystem, sizeof(path_full) - sysfs_len);
488 util_strlcat(path, "/", sizeof(path_full) - sysfs_len);
489 util_strlcat(path, id, sizeof(path_full) - sysfs_len);
490 if (stat(path_full, &statbuf) == 0)
495 if (S_ISLNK(statbuf.st_mode))
496 resolve_sys_link(udev, path, sizeof(path_full) - sysfs_len);
497 util_strlcpy(devpath_full, path, len);