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];
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 struct sysfs_device *sysfs_device_get(struct udev *udev, const char *devpath)
105 char path[UTIL_PATH_SIZE];
106 char devpath_real[UTIL_PATH_SIZE];
107 struct sysfs_device *dev;
108 struct sysfs_device *dev_loop;
110 char link_path[UTIL_PATH_SIZE];
111 char link_target[UTIL_PATH_SIZE];
115 /* we handle only these devpathes */
116 if (devpath != NULL &&
117 strncmp(devpath, "/devices/", 9) != 0 &&
118 strncmp(devpath, "/subsystem/", 11) != 0 &&
119 strncmp(devpath, "/module/", 8) != 0 &&
120 strncmp(devpath, "/bus/", 5) != 0 &&
121 strncmp(devpath, "/class/", 7) != 0 &&
122 strncmp(devpath, "/block/", 7) != 0)
125 dbg(udev, "open '%s'\n", devpath);
126 util_strlcpy(devpath_real, devpath, sizeof(devpath_real));
127 util_remove_trailing_chars(devpath_real, '/');
128 if (devpath[0] == '\0' )
131 /* look for device already in cache (we never put an untranslated path in the cache) */
132 list_for_each_entry(dev_loop, &dev_list, node) {
133 if (strcmp(dev_loop->devpath, devpath_real) == 0) {
134 dbg(udev, "found in cache '%s'\n", dev_loop->devpath);
139 /* if we got a link, resolve it to the real device */
140 util_strlcpy(path, udev_get_sys_path(udev), sizeof(path));
141 util_strlcat(path, devpath_real, sizeof(path));
142 if (lstat(path, &statbuf) != 0) {
143 dbg(udev, "stat '%s' failed: %s\n", path, strerror(errno));
146 if (S_ISLNK(statbuf.st_mode)) {
147 if (util_resolve_sys_link(udev, devpath_real, sizeof(devpath_real)) != 0)
150 /* now look for device in cache after path translation */
151 list_for_each_entry(dev_loop, &dev_list, node) {
152 if (strcmp(dev_loop->devpath, devpath_real) == 0) {
153 dbg(udev, "found in cache '%s'\n", dev_loop->devpath);
159 /* it is a new device */
160 dbg(udev, "new uncached device '%s'\n", devpath_real);
161 dev = malloc(sizeof(struct sysfs_device));
164 memset(dev, 0x00, sizeof(struct sysfs_device));
166 sysfs_device_set_values(udev, dev, devpath_real, NULL, NULL);
168 /* get subsystem name */
169 util_strlcpy(link_path, udev_get_sys_path(udev), sizeof(link_path));
170 util_strlcat(link_path, dev->devpath, sizeof(link_path));
171 util_strlcat(link_path, "/subsystem", sizeof(link_path));
172 len = readlink(link_path, link_target, sizeof(link_target));
174 /* get subsystem from "subsystem" link */
175 link_target[len] = '\0';
176 dbg(udev, "subsystem link '%s' points to '%s'\n", link_path, link_target);
177 pos = strrchr(link_target, '/');
179 util_strlcpy(dev->subsystem, &pos[1], sizeof(dev->subsystem));
180 } else if (strstr(dev->devpath, "/drivers/") != NULL) {
181 util_strlcpy(dev->subsystem, "drivers", sizeof(dev->subsystem));
182 } else if (strncmp(dev->devpath, "/module/", 8) == 0) {
183 util_strlcpy(dev->subsystem, "module", sizeof(dev->subsystem));
184 } else if (strncmp(dev->devpath, "/subsystem/", 11) == 0) {
185 pos = strrchr(dev->devpath, '/');
186 if (pos == &dev->devpath[10])
187 util_strlcpy(dev->subsystem, "subsystem", sizeof(dev->subsystem));
188 } else if (strncmp(dev->devpath, "/class/", 7) == 0) {
189 pos = strrchr(dev->devpath, '/');
190 if (pos == &dev->devpath[6])
191 util_strlcpy(dev->subsystem, "subsystem", sizeof(dev->subsystem));
192 } else if (strncmp(dev->devpath, "/bus/", 5) == 0) {
193 pos = strrchr(dev->devpath, '/');
194 if (pos == &dev->devpath[4])
195 util_strlcpy(dev->subsystem, "subsystem", sizeof(dev->subsystem));
198 /* get driver name */
199 util_strlcpy(link_path, udev_get_sys_path(udev), sizeof(link_path));
200 util_strlcat(link_path, dev->devpath, sizeof(link_path));
201 util_strlcat(link_path, "/driver", sizeof(link_path));
202 len = readlink(link_path, link_target, sizeof(link_target));
204 link_target[len] = '\0';
205 dbg(udev, "driver link '%s' points to '%s'\n", link_path, link_target);
206 pos = strrchr(link_target, '/');
208 util_strlcpy(dev->driver, &pos[1], sizeof(dev->driver));
211 dbg(udev, "add to cache 'devpath=%s', subsystem='%s', driver='%s'\n", dev->devpath, dev->subsystem, dev->driver);
212 list_add(&dev->node, &dev_list);
217 struct sysfs_device *sysfs_device_get_parent(struct udev *udev, struct sysfs_device *dev)
219 char parent_devpath[UTIL_PATH_SIZE];
222 dbg(udev, "open '%s'\n", dev->devpath);
224 /* look if we already know the parent */
225 if (dev->parent != NULL)
228 util_strlcpy(parent_devpath, dev->devpath, sizeof(parent_devpath));
229 dbg(udev, "'%s'\n", parent_devpath);
231 /* strip last element */
232 pos = strrchr(parent_devpath, '/');
233 if (pos == NULL || pos == parent_devpath)
237 if (strncmp(parent_devpath, "/class", 6) == 0) {
238 pos = strrchr(parent_devpath, '/');
239 if (pos == &parent_devpath[6] || pos == parent_devpath) {
240 dbg(udev, "/class top level, look for device link\n");
244 if (strcmp(parent_devpath, "/block") == 0) {
245 dbg(udev, "/block top level, look for device link\n");
249 /* are we at the top level? */
250 pos = strrchr(parent_devpath, '/');
251 if (pos == NULL || pos == parent_devpath)
254 /* get parent and remember it */
255 dev->parent = sysfs_device_get(udev, parent_devpath);
259 util_strlcpy(parent_devpath, dev->devpath, sizeof(parent_devpath));
260 util_strlcat(parent_devpath, "/device", sizeof(parent_devpath));
261 if (util_resolve_sys_link(udev, parent_devpath, sizeof(parent_devpath)) != 0)
264 /* get parent and remember it */
265 dev->parent = sysfs_device_get(udev, parent_devpath);
269 struct sysfs_device *sysfs_device_get_parent_with_subsystem(struct udev *udev, struct sysfs_device *dev, const char *subsystem)
271 struct sysfs_device *dev_parent;
273 dev_parent = sysfs_device_get_parent(udev, dev);
274 while (dev_parent != NULL) {
275 if (strcmp(dev_parent->subsystem, subsystem) == 0)
277 dev_parent = sysfs_device_get_parent(udev, dev_parent);
282 char *sysfs_attr_get_value(struct udev *udev, const char *devpath, const char *attr_name)
284 char path_full[UTIL_PATH_SIZE];
286 char value[UTIL_NAME_SIZE];
287 struct sysfs_attr *attr_loop;
288 struct sysfs_attr *attr;
294 dbg(udev, "open '%s'/'%s'\n", devpath, attr_name);
295 sysfs_len = util_strlcpy(path_full, udev_get_sys_path(udev), sizeof(path_full));
296 if(sysfs_len >= sizeof(path_full))
297 sysfs_len = sizeof(path_full) - 1;
298 path = &path_full[sysfs_len];
299 util_strlcat(path_full, devpath, sizeof(path_full));
300 util_strlcat(path_full, "/", sizeof(path_full));
301 util_strlcat(path_full, attr_name, sizeof(path_full));
303 /* look for attribute in cache */
304 list_for_each_entry(attr_loop, &attr_list, node) {
305 if (strcmp(attr_loop->path, path) == 0) {
306 dbg(udev, "found in cache '%s'\n", attr_loop->path);
307 return attr_loop->value;
311 /* store attribute in cache (also negatives are kept in cache) */
312 dbg(udev, "new uncached attribute '%s'\n", path_full);
313 attr = malloc(sizeof(struct sysfs_attr));
316 memset(attr, 0x00, sizeof(struct sysfs_attr));
317 util_strlcpy(attr->path, path, sizeof(attr->path));
318 dbg(udev, "add to cache '%s'\n", path_full);
319 list_add(&attr->node, &attr_list);
321 if (lstat(path_full, &statbuf) != 0) {
322 dbg(udev, "stat '%s' failed: %s\n", path_full, strerror(errno));
326 if (S_ISLNK(statbuf.st_mode)) {
327 /* links return the last element of the target path */
328 char link_target[UTIL_PATH_SIZE];
332 len = readlink(path_full, link_target, sizeof(link_target));
334 link_target[len] = '\0';
335 pos = strrchr(link_target, '/');
337 dbg(udev, "cache '%s' with link value '%s'\n", path_full, value);
338 util_strlcpy(attr->value_local, &pos[1], sizeof(attr->value_local));
339 attr->value = attr->value_local;
345 /* skip directories */
346 if (S_ISDIR(statbuf.st_mode))
349 /* skip non-readable files */
350 if ((statbuf.st_mode & S_IRUSR) == 0)
353 /* read attribute value */
354 fd = open(path_full, O_RDONLY);
356 dbg(udev, "attribute '%s' can not be opened\n", path_full);
359 size = read(fd, value, sizeof(value));
363 if (size == sizeof(value))
366 /* got a valid value, store and return it */
368 util_remove_trailing_chars(value, '\n');
369 dbg(udev, "cache '%s' with attribute value '%s'\n", path_full, value);
370 util_strlcpy(attr->value_local, value, sizeof(attr->value_local));
371 attr->value = attr->value_local;
377 int sysfs_lookup_devpath_by_subsys_id(struct udev *udev, char *devpath_full, size_t len, const char *subsystem, const char *id)
380 char path_full[UTIL_PATH_SIZE];
384 sysfs_len = util_strlcpy(path_full, udev_get_sys_path(udev), sizeof(path_full));
385 path = &path_full[sysfs_len];
387 if (strcmp(subsystem, "subsystem") == 0) {
388 util_strlcpy(path, "/subsystem/", sizeof(path_full) - sysfs_len);
389 util_strlcat(path, id, sizeof(path_full) - sysfs_len);
390 if (stat(path_full, &statbuf) == 0)
393 util_strlcpy(path, "/bus/", sizeof(path_full) - sysfs_len);
394 util_strlcat(path, id, sizeof(path_full) - sysfs_len);
395 if (stat(path_full, &statbuf) == 0)
399 util_strlcpy(path, "/class/", sizeof(path_full) - sysfs_len);
400 util_strlcat(path, id, sizeof(path_full) - sysfs_len);
401 if (stat(path_full, &statbuf) == 0)
405 if (strcmp(subsystem, "module") == 0) {
406 util_strlcpy(path, "/module/", sizeof(path_full) - sysfs_len);
407 util_strlcat(path, id, sizeof(path_full) - sysfs_len);
408 if (stat(path_full, &statbuf) == 0)
413 if (strcmp(subsystem, "drivers") == 0) {
414 char subsys[UTIL_NAME_SIZE];
417 util_strlcpy(subsys, id, sizeof(subsys));
418 driver = strchr(subsys, ':');
419 if (driver != NULL) {
422 util_strlcpy(path, "/subsystem/", sizeof(path_full) - sysfs_len);
423 util_strlcat(path, subsys, sizeof(path_full) - sysfs_len);
424 util_strlcat(path, "/drivers/", sizeof(path_full) - sysfs_len);
425 util_strlcat(path, driver, sizeof(path_full) - sysfs_len);
426 if (stat(path_full, &statbuf) == 0)
429 util_strlcpy(path, "/bus/", sizeof(path_full) - sysfs_len);
430 util_strlcat(path, subsys, sizeof(path_full) - sysfs_len);
431 util_strlcat(path, "/drivers/", sizeof(path_full) - sysfs_len);
432 util_strlcat(path, driver, sizeof(path_full) - sysfs_len);
433 if (stat(path_full, &statbuf) == 0)
439 util_strlcpy(path, "/subsystem/", sizeof(path_full) - sysfs_len);
440 util_strlcat(path, subsystem, sizeof(path_full) - sysfs_len);
441 util_strlcat(path, "/devices/", sizeof(path_full) - sysfs_len);
442 util_strlcat(path, id, sizeof(path_full) - sysfs_len);
443 if (stat(path_full, &statbuf) == 0)
446 util_strlcpy(path, "/bus/", sizeof(path_full) - sysfs_len);
447 util_strlcat(path, subsystem, sizeof(path_full) - sysfs_len);
448 util_strlcat(path, "/devices/", sizeof(path_full) - sysfs_len);
449 util_strlcat(path, id, sizeof(path_full) - sysfs_len);
450 if (stat(path_full, &statbuf) == 0)
453 util_strlcpy(path, "/class/", sizeof(path_full) - sysfs_len);
454 util_strlcat(path, subsystem, sizeof(path_full) - sysfs_len);
455 util_strlcat(path, "/", sizeof(path_full) - sysfs_len);
456 util_strlcat(path, id, sizeof(path_full) - sysfs_len);
457 if (stat(path_full, &statbuf) == 0)
462 if (S_ISLNK(statbuf.st_mode))
463 util_resolve_sys_link(udev, path, sizeof(path_full) - sysfs_len);
464 util_strlcpy(devpath_full, path, len);