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.
33 static LIST_HEAD(dev_list);
35 /* attribute value cache */
36 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];
47 INIT_LIST_HEAD(&dev_list);
48 INIT_LIST_HEAD(&attr_list);
52 void sysfs_cleanup(void)
54 struct sysfs_attr *attr_loop;
55 struct sysfs_attr *attr_temp;
56 struct sysfs_device *dev_loop;
57 struct sysfs_device *dev_temp;
59 list_for_each_entry_safe(attr_loop, attr_temp, &attr_list, node) {
60 list_del(&attr_loop->node);
64 list_for_each_entry_safe(dev_loop, dev_temp, &dev_list, node) {
65 list_del(&dev_loop->node);
70 void sysfs_device_set_values(struct udev *udev,
71 struct sysfs_device *dev, const char *devpath,
72 const char *subsystem, const char *driver)
76 strlcpy(dev->devpath, devpath, sizeof(dev->devpath));
77 if (subsystem != NULL)
78 strlcpy(dev->subsystem, subsystem, sizeof(dev->subsystem));
80 strlcpy(dev->driver, driver, sizeof(dev->driver));
83 pos = strrchr(dev->devpath, '/');
86 strlcpy(dev->kernel, &pos[1], sizeof(dev->kernel));
87 dbg(udev, "kernel='%s'\n", dev->kernel);
89 /* some devices have '!' in their name, change that to '/' */
91 while (pos[0] != '\0') {
97 /* get kernel number */
98 pos = &dev->kernel[strlen(dev->kernel)];
99 while (isdigit(pos[-1]))
101 strlcpy(dev->kernel_number, pos, sizeof(dev->kernel_number));
102 dbg(udev, "kernel_number='%s'\n", dev->kernel_number);
105 int sysfs_resolve_link(struct udev *udev, char *devpath, size_t size)
107 char link_path[PATH_SIZE];
108 char link_target[PATH_SIZE];
113 strlcpy(link_path, udev_get_sys_path(udev), sizeof(link_path));
114 strlcat(link_path, devpath, sizeof(link_path));
115 len = readlink(link_path, link_target, sizeof(link_target));
118 link_target[len] = '\0';
119 dbg(udev, "path link '%s' points to '%s'\n", devpath, link_target);
121 for (back = 0; strncmp(&link_target[back * 3], "../", 3) == 0; back++)
123 dbg(udev, "base '%s', tail '%s', back %i\n", devpath, &link_target[back * 3], back);
124 for (i = 0; i <= back; i++) {
125 char *pos = strrchr(devpath, '/');
131 dbg(udev, "after moving back '%s'\n", devpath);
132 strlcat(devpath, "/", size);
133 strlcat(devpath, &link_target[back * 3], size);
137 struct sysfs_device *sysfs_device_get(struct udev *udev, const char *devpath)
139 char path[PATH_SIZE];
140 char devpath_real[PATH_SIZE];
141 struct sysfs_device *dev;
142 struct sysfs_device *dev_loop;
144 char link_path[PATH_SIZE];
145 char link_target[PATH_SIZE];
149 /* we handle only these devpathes */
150 if (devpath != NULL &&
151 strncmp(devpath, "/devices/", 9) != 0 &&
152 strncmp(devpath, "/subsystem/", 11) != 0 &&
153 strncmp(devpath, "/module/", 8) != 0 &&
154 strncmp(devpath, "/bus/", 5) != 0 &&
155 strncmp(devpath, "/class/", 7) != 0 &&
156 strncmp(devpath, "/block/", 7) != 0)
159 dbg(udev, "open '%s'\n", devpath);
160 strlcpy(devpath_real, devpath, sizeof(devpath_real));
161 remove_trailing_chars(devpath_real, '/');
162 if (devpath[0] == '\0' )
165 /* look for device already in cache (we never put an untranslated path in the cache) */
166 list_for_each_entry(dev_loop, &dev_list, node) {
167 if (strcmp(dev_loop->devpath, devpath_real) == 0) {
168 dbg(udev, "found in cache '%s'\n", dev_loop->devpath);
173 /* if we got a link, resolve it to the real device */
174 strlcpy(path, udev_get_sys_path(udev), sizeof(path));
175 strlcat(path, devpath_real, sizeof(path));
176 if (lstat(path, &statbuf) != 0) {
177 dbg(udev, "stat '%s' failed: %s\n", path, strerror(errno));
180 if (S_ISLNK(statbuf.st_mode)) {
181 if (sysfs_resolve_link(udev, devpath_real, sizeof(devpath_real)) != 0)
184 /* now look for device in cache after path translation */
185 list_for_each_entry(dev_loop, &dev_list, node) {
186 if (strcmp(dev_loop->devpath, devpath_real) == 0) {
187 dbg(udev, "found in cache '%s'\n", dev_loop->devpath);
193 /* it is a new device */
194 dbg(udev, "new uncached device '%s'\n", devpath_real);
195 dev = malloc(sizeof(struct sysfs_device));
198 memset(dev, 0x00, sizeof(struct sysfs_device));
200 sysfs_device_set_values(udev, dev, devpath_real, NULL, NULL);
202 /* get subsystem name */
203 strlcpy(link_path, udev_get_sys_path(udev), sizeof(link_path));
204 strlcat(link_path, dev->devpath, sizeof(link_path));
205 strlcat(link_path, "/subsystem", sizeof(link_path));
206 len = readlink(link_path, link_target, sizeof(link_target));
208 /* get subsystem from "subsystem" link */
209 link_target[len] = '\0';
210 dbg(udev, "subsystem link '%s' points to '%s'\n", link_path, link_target);
211 pos = strrchr(link_target, '/');
213 strlcpy(dev->subsystem, &pos[1], sizeof(dev->subsystem));
214 } else if (strstr(dev->devpath, "/drivers/") != NULL) {
215 strlcpy(dev->subsystem, "drivers", sizeof(dev->subsystem));
216 } else if (strncmp(dev->devpath, "/module/", 8) == 0) {
217 strlcpy(dev->subsystem, "module", sizeof(dev->subsystem));
218 } else if (strncmp(dev->devpath, "/subsystem/", 11) == 0) {
219 pos = strrchr(dev->devpath, '/');
220 if (pos == &dev->devpath[10])
221 strlcpy(dev->subsystem, "subsystem", sizeof(dev->subsystem));
222 } else if (strncmp(dev->devpath, "/class/", 7) == 0) {
223 pos = strrchr(dev->devpath, '/');
224 if (pos == &dev->devpath[6])
225 strlcpy(dev->subsystem, "subsystem", sizeof(dev->subsystem));
226 } else if (strncmp(dev->devpath, "/bus/", 5) == 0) {
227 pos = strrchr(dev->devpath, '/');
228 if (pos == &dev->devpath[4])
229 strlcpy(dev->subsystem, "subsystem", sizeof(dev->subsystem));
232 /* get driver name */
233 strlcpy(link_path, udev_get_sys_path(udev), sizeof(link_path));
234 strlcat(link_path, dev->devpath, sizeof(link_path));
235 strlcat(link_path, "/driver", sizeof(link_path));
236 len = readlink(link_path, link_target, sizeof(link_target));
238 link_target[len] = '\0';
239 dbg(udev, "driver link '%s' points to '%s'\n", link_path, link_target);
240 pos = strrchr(link_target, '/');
242 strlcpy(dev->driver, &pos[1], sizeof(dev->driver));
245 dbg(udev, "add to cache 'devpath=%s', subsystem='%s', driver='%s'\n", dev->devpath, dev->subsystem, dev->driver);
246 list_add(&dev->node, &dev_list);
251 struct sysfs_device *sysfs_device_get_parent(struct udev *udev, struct sysfs_device *dev)
253 char parent_devpath[PATH_SIZE];
256 dbg(udev, "open '%s'\n", dev->devpath);
258 /* look if we already know the parent */
259 if (dev->parent != NULL)
262 strlcpy(parent_devpath, dev->devpath, sizeof(parent_devpath));
263 dbg(udev, "'%s'\n", parent_devpath);
265 /* strip last element */
266 pos = strrchr(parent_devpath, '/');
267 if (pos == NULL || pos == parent_devpath)
271 if (strncmp(parent_devpath, "/class", 6) == 0) {
272 pos = strrchr(parent_devpath, '/');
273 if (pos == &parent_devpath[6] || pos == parent_devpath) {
274 dbg(udev, "/class top level, look for device link\n");
278 if (strcmp(parent_devpath, "/block") == 0) {
279 dbg(udev, "/block top level, look for device link\n");
283 /* are we at the top level? */
284 pos = strrchr(parent_devpath, '/');
285 if (pos == NULL || pos == parent_devpath)
288 /* get parent and remember it */
289 dev->parent = sysfs_device_get(udev, parent_devpath);
293 strlcpy(parent_devpath, dev->devpath, sizeof(parent_devpath));
294 strlcat(parent_devpath, "/device", sizeof(parent_devpath));
295 if (sysfs_resolve_link(udev, parent_devpath, sizeof(parent_devpath)) != 0)
298 /* get parent and remember it */
299 dev->parent = sysfs_device_get(udev, parent_devpath);
303 struct sysfs_device *sysfs_device_get_parent_with_subsystem(struct udev *udev, struct sysfs_device *dev, const char *subsystem)
305 struct sysfs_device *dev_parent;
307 dev_parent = sysfs_device_get_parent(udev, dev);
308 while (dev_parent != NULL) {
309 if (strcmp(dev_parent->subsystem, subsystem) == 0)
311 dev_parent = sysfs_device_get_parent(udev, dev_parent);
316 char *sysfs_attr_get_value(struct udev *udev, const char *devpath, const char *attr_name)
318 char path_full[PATH_SIZE];
320 char value[NAME_SIZE];
321 struct sysfs_attr *attr_loop;
322 struct sysfs_attr *attr;
328 dbg(udev, "open '%s'/'%s'\n", devpath, attr_name);
329 sysfs_len = strlcpy(path_full, udev_get_sys_path(udev), sizeof(path_full));
330 if(sysfs_len >= sizeof(path_full))
331 sysfs_len = sizeof(path_full) - 1;
332 path = &path_full[sysfs_len];
333 strlcat(path_full, devpath, sizeof(path_full));
334 strlcat(path_full, "/", sizeof(path_full));
335 strlcat(path_full, attr_name, sizeof(path_full));
337 /* look for attribute in cache */
338 list_for_each_entry(attr_loop, &attr_list, node) {
339 if (strcmp(attr_loop->path, path) == 0) {
340 dbg(udev, "found in cache '%s'\n", attr_loop->path);
341 return attr_loop->value;
345 /* store attribute in cache (also negatives are kept in cache) */
346 dbg(udev, "new uncached attribute '%s'\n", path_full);
347 attr = malloc(sizeof(struct sysfs_attr));
350 memset(attr, 0x00, sizeof(struct sysfs_attr));
351 strlcpy(attr->path, path, sizeof(attr->path));
352 dbg(udev, "add to cache '%s'\n", path_full);
353 list_add(&attr->node, &attr_list);
355 if (lstat(path_full, &statbuf) != 0) {
356 dbg(udev, "stat '%s' failed: %s\n", path_full, strerror(errno));
360 if (S_ISLNK(statbuf.st_mode)) {
361 /* links return the last element of the target path */
362 char link_target[PATH_SIZE];
366 len = readlink(path_full, link_target, sizeof(link_target));
368 link_target[len] = '\0';
369 pos = strrchr(link_target, '/');
371 dbg(udev, "cache '%s' with link value '%s'\n", path_full, value);
372 strlcpy(attr->value_local, &pos[1], sizeof(attr->value_local));
373 attr->value = attr->value_local;
379 /* skip directories */
380 if (S_ISDIR(statbuf.st_mode))
383 /* skip non-readable files */
384 if ((statbuf.st_mode & S_IRUSR) == 0)
387 /* read attribute value */
388 fd = open(path_full, O_RDONLY);
390 dbg(udev, "attribute '%s' can not be opened\n", path_full);
393 size = read(fd, value, sizeof(value));
397 if (size == sizeof(value))
400 /* got a valid value, store and return it */
402 remove_trailing_chars(value, '\n');
403 dbg(udev, "cache '%s' with attribute value '%s'\n", path_full, value);
404 strlcpy(attr->value_local, value, sizeof(attr->value_local));
405 attr->value = attr->value_local;
411 int sysfs_lookup_devpath_by_subsys_id(struct udev *udev, char *devpath_full, size_t len, const char *subsystem, const char *id)
414 char path_full[PATH_SIZE];
418 sysfs_len = strlcpy(path_full, udev_get_sys_path(udev), sizeof(path_full));
419 path = &path_full[sysfs_len];
421 if (strcmp(subsystem, "subsystem") == 0) {
422 strlcpy(path, "/subsystem/", sizeof(path_full) - sysfs_len);
423 strlcat(path, id, sizeof(path_full) - sysfs_len);
424 if (stat(path_full, &statbuf) == 0)
427 strlcpy(path, "/bus/", sizeof(path_full) - sysfs_len);
428 strlcat(path, id, sizeof(path_full) - sysfs_len);
429 if (stat(path_full, &statbuf) == 0)
433 strlcpy(path, "/class/", sizeof(path_full) - sysfs_len);
434 strlcat(path, id, sizeof(path_full) - sysfs_len);
435 if (stat(path_full, &statbuf) == 0)
439 if (strcmp(subsystem, "module") == 0) {
440 strlcpy(path, "/module/", sizeof(path_full) - sysfs_len);
441 strlcat(path, id, sizeof(path_full) - sysfs_len);
442 if (stat(path_full, &statbuf) == 0)
447 if (strcmp(subsystem, "drivers") == 0) {
448 char subsys[NAME_SIZE];
451 strlcpy(subsys, id, sizeof(subsys));
452 driver = strchr(subsys, ':');
453 if (driver != NULL) {
456 strlcpy(path, "/subsystem/", sizeof(path_full) - sysfs_len);
457 strlcat(path, subsys, sizeof(path_full) - sysfs_len);
458 strlcat(path, "/drivers/", sizeof(path_full) - sysfs_len);
459 strlcat(path, driver, sizeof(path_full) - sysfs_len);
460 if (stat(path_full, &statbuf) == 0)
463 strlcpy(path, "/bus/", sizeof(path_full) - sysfs_len);
464 strlcat(path, subsys, sizeof(path_full) - sysfs_len);
465 strlcat(path, "/drivers/", sizeof(path_full) - sysfs_len);
466 strlcat(path, driver, sizeof(path_full) - sysfs_len);
467 if (stat(path_full, &statbuf) == 0)
473 strlcpy(path, "/subsystem/", sizeof(path_full) - sysfs_len);
474 strlcat(path, subsystem, sizeof(path_full) - sysfs_len);
475 strlcat(path, "/devices/", sizeof(path_full) - sysfs_len);
476 strlcat(path, id, sizeof(path_full) - sysfs_len);
477 if (stat(path_full, &statbuf) == 0)
480 strlcpy(path, "/bus/", sizeof(path_full) - sysfs_len);
481 strlcat(path, subsystem, sizeof(path_full) - sysfs_len);
482 strlcat(path, "/devices/", sizeof(path_full) - sysfs_len);
483 strlcat(path, id, sizeof(path_full) - sysfs_len);
484 if (stat(path_full, &statbuf) == 0)
487 strlcpy(path, "/class/", sizeof(path_full) - sysfs_len);
488 strlcat(path, subsystem, sizeof(path_full) - sysfs_len);
489 strlcat(path, "/", sizeof(path_full) - sysfs_len);
490 strlcat(path, id, sizeof(path_full) - sysfs_len);
491 if (stat(path_full, &statbuf) == 0)
496 if (S_ISLNK(statbuf.st_mode))
497 sysfs_resolve_link(udev, path, sizeof(path_full) - sysfs_len);
498 strlcpy(devpath_full, path, len);