2 * libudev - interface to udev device information
4 * Copyright (C) 2008 Kay Sievers <kay.sievers@vrfy.org>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 #include "libudev-private.h"
36 struct udev_device *parent_device;
42 struct list_node devlink_list;
43 struct list_node properties_list;
51 unsigned long long int seqnum;
52 int num_fake_partitions;
55 struct list_node attr_list;
59 static size_t syspath_to_db_path(struct udev_device *udev_device, char *filename, size_t len)
63 /* translate to location of db file */
64 util_strlcpy(filename, udev_get_dev_path(udev_device->udev), len);
65 start = util_strlcat(filename, "/.udev/db/", len);
66 util_strlcat(filename, udev_device->devpath, len);
67 return util_path_encode(&filename[start], len - start);
70 static int device_read_db(struct udev_device *udev_device)
73 char filename[UTIL_PATH_SIZE];
74 char line[UTIL_LINE_SIZE];
77 syspath_to_db_path(udev_device, filename, sizeof(filename));
79 if (lstat(filename, &stats) != 0) {
80 info(udev_device->udev, "no db file to read %s: %m\n", filename);
83 if ((stats.st_mode & S_IFMT) == S_IFLNK) {
84 char target[UTIL_PATH_SIZE];
88 target_len = readlink(filename, target, sizeof(target));
90 target[target_len] = '\0';
92 info(udev_device->udev, "error reading db link %s: %m\n", filename);
96 next = strchr(target, ' ');
101 if (asprintf(&udev_device->devnode, "%s/%s", udev_get_dev_path(udev_device->udev), target) < 0)
103 while (next != NULL) {
104 char linkname[UTIL_PATH_SIZE];
108 next = strchr(next, ' ');
113 util_strlcpy(linkname, udev_get_dev_path(udev_device->udev), sizeof(linkname));
114 util_strlcat(linkname, "/", sizeof(linkname));
115 util_strlcat(linkname, next, sizeof(linkname));
116 device_add_devlink(udev_device, linkname);
118 info(udev_device->udev, "device %p filled with db symlink data '%s'\n", udev_device, udev_device->devnode);
122 f = fopen(filename, "r");
124 info(udev_device->udev, "error reading db file %s: %m\n", filename);
127 while (fgets(line, sizeof(line), f)) {
139 asprintf(&udev_device->devnode, "%s/%s", udev_get_dev_path(udev_device->udev), val);
142 util_strlcpy(filename, udev_get_dev_path(udev_device->udev), sizeof(filename));
143 util_strlcat(filename, "/", sizeof(filename));
144 util_strlcat(filename, val, sizeof(filename));
145 device_add_devlink(udev_device, filename);
148 device_set_devlink_priority(udev_device, atoi(val));
151 device_set_event_timeout(udev_device, atoi(val));
154 device_set_num_fake_partitions(udev_device, atoi(val));
157 device_set_ignore_remove(udev_device, atoi(val));
160 device_add_property_from_string(udev_device, val);
166 info(udev_device->udev, "device %p filled with db file data\n", udev_device);
170 static int device_read_uevent_file(struct udev_device *udev_device)
172 char filename[UTIL_PATH_SIZE];
174 char line[UTIL_LINE_SIZE];
178 util_strlcpy(filename, udev_device->syspath, sizeof(filename));
179 util_strlcat(filename, "/uevent", sizeof(filename));
180 f = fopen(filename, "r");
184 while (fgets(line, sizeof(line), f)) {
187 pos = strchr(line, '\n');
192 if (strncmp(line, "MAJOR=", 6) == 0)
193 maj = strtoull(&line[6], NULL, 10);
194 else if (strncmp(line, "MINOR=", 6) == 0)
195 min = strtoull(&line[6], NULL, 10);
197 device_add_property_from_string(udev_device, line);
200 udev_device->devnum = makedev(maj, min);
206 static void device_load_info(struct udev_device *device)
208 device_read_uevent_file(device);
209 device_read_db(device);
210 device->info_loaded = 1;
213 void device_set_info_loaded(struct udev_device *device)
215 device->info_loaded = 1;
218 struct udev_device *device_new(struct udev *udev)
220 struct udev_device *udev_device;
225 udev_device = malloc(sizeof(struct udev_device));
226 if (udev_device == NULL)
228 memset(udev_device, 0x00, sizeof(struct udev_device));
229 udev_device->refcount = 1;
230 udev_device->udev = udev;
231 list_init(&udev_device->devlink_list);
232 list_init(&udev_device->properties_list);
233 list_init(&udev_device->attr_list);
234 info(udev_device->udev, "udev_device: %p created\n", udev_device);
239 * udev_device_new_from_syspath:
240 * @udev: udev library context
241 * @syspath: sys device path including sys directory
243 * Create new udev device, and fill in information from the sys
244 * device and the udev database entry. The sypath is the absolute
245 * path to the device, including the sys mount point.
247 * The initial refcount is 1, and needs to be decremented to
248 * release the ressources of the udev device.
250 * Returns: a new udev device, or #NULL, if it does not exist
252 struct udev_device *udev_device_new_from_syspath(struct udev *udev, const char *syspath)
256 char path[UTIL_PATH_SIZE];
259 struct udev_device *udev_device;
266 /* path starts in sys */
267 len = strlen(udev_get_sys_path(udev));
268 if (strncmp(syspath, udev_get_sys_path(udev), len) != 0) {
269 info(udev, "not in sys :%s\n", syspath);
273 /* path is not a root directory */
274 subdir = &syspath[len+1];
275 pos = strrchr(subdir, '/');
276 if (pos == NULL || pos < &subdir[2]) {
277 info(udev, "not a subdir :%s\n", syspath);
281 /* resolve possible symlink to real path */
282 util_strlcpy(path, syspath, sizeof(path));
283 util_resolve_sys_link(udev, path, sizeof(path));
285 /* try to resolve the silly block layout if needed */
286 if (strncmp(&path[len], "/block/", 7) == 0) {
287 char block[UTIL_PATH_SIZE];
288 char part[UTIL_PATH_SIZE];
290 util_strlcpy(block, path, sizeof(block));
291 pos = strrchr(block, '/');
294 util_strlcpy(part, pos, sizeof(part));
296 if (util_resolve_sys_link(udev, block, sizeof(block)) == 0) {
297 util_strlcpy(path, block, sizeof(path));
298 util_strlcat(path, part, sizeof(path));
302 /* path exists in sys */
303 if (strncmp(&syspath[len], "/devices/", 9) == 0 ||
304 strncmp(&syspath[len], "/class/", 7) == 0 ||
305 strncmp(&syspath[len], "/block/", 7) == 0) {
306 char file[UTIL_PATH_SIZE];
308 /* all "devices" require a "uevent" file */
309 util_strlcpy(file, path, sizeof(file));
310 util_strlcat(file, "/uevent", sizeof(file));
311 if (stat(file, &statbuf) != 0) {
312 info(udev, "not a device: %s\n", syspath);
316 /* everything else just needs to be a directory */
317 if (stat(path, &statbuf) != 0 || !S_ISDIR(statbuf.st_mode)) {
318 info(udev, "directory not found: %s\n", syspath);
323 udev_device = device_new(udev);
324 if (udev_device == NULL)
327 device_set_syspath(udev_device, path);
328 info(udev, "device %p has devpath '%s'\n", udev_device, udev_device_get_devpath(udev_device));
333 struct udev_device *udev_device_new_from_devnum(struct udev *udev, char type, dev_t devnum)
335 char path[UTIL_PATH_SIZE];
336 const char *type_str;
337 struct udev_enumerate *udev_enumerate;
338 struct udev_list_entry *list_entry;
339 struct udev_device *device = NULL;
343 else if (type == 'c')
348 /* /sys/dev/{block,char}/<maj>:<min> link */
349 snprintf(path, sizeof(path), "%s/dev/%s/%u:%u", udev_get_sys_path(udev),
350 type_str, major(devnum), minor(devnum));
351 if (util_resolve_sys_link(udev, path, sizeof(path)) == 0)
352 return udev_device_new_from_syspath(udev, path);
354 udev_enumerate = udev_enumerate_new(udev);
355 if (udev_enumerate == NULL)
358 /* fallback to search sys devices for the major/minor */
360 udev_enumerate_add_match_subsystem(udev_enumerate, "block");
361 else if (type == 'c')
362 udev_enumerate_add_nomatch_subsystem(udev_enumerate, "block");
363 udev_enumerate_scan_devices(udev_enumerate);
364 udev_list_entry_foreach(list_entry, udev_enumerate_get_list_entry(udev_enumerate)) {
365 struct udev_device *device_loop;
367 device_loop = udev_device_new_from_syspath(udev, udev_list_entry_get_name(list_entry));
368 if (device_loop != NULL) {
369 if (udev_device_get_devnum(device_loop) == devnum) {
370 if (type == 'b' && strcmp(udev_device_get_subsystem(device_loop), "block") != 0)
372 if (type == 'c' && strcmp(udev_device_get_subsystem(device_loop), "block") == 0)
374 device = device_loop;
377 udev_device_unref(device_loop);
380 udev_enumerate_unref(udev_enumerate);
384 static struct udev_device *device_new_from_parent(struct udev_device *udev_device)
386 struct udev_device *udev_device_parent = NULL;
387 char path[UTIL_PATH_SIZE];
390 /* follow "device" link in deprecated sys layout */
391 if (strncmp(udev_device->devpath, "/class/", 7) == 0 ||
392 strncmp(udev_device->devpath, "/block/", 7) == 0) {
393 util_strlcpy(path, udev_device->syspath, sizeof(path));
394 util_strlcat(path, "/device", sizeof(path));
395 if (util_resolve_sys_link(udev_device->udev, path, sizeof(path)) == 0)
396 udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
397 return udev_device_parent;
400 util_strlcpy(path, udev_device->syspath, sizeof(path));
401 subdir = &path[strlen(udev_get_sys_path(udev_device->udev))+1];
405 pos = strrchr(subdir, '/');
406 if (pos == NULL || pos < &subdir[2])
409 udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
410 if (udev_device_parent != NULL)
411 return udev_device_parent;
416 struct udev_device *udev_device_get_parent(struct udev_device *udev_device)
418 if (udev_device == NULL)
421 if (udev_device->parent_device != NULL) {
422 info(udev_device->udev, "returning existing parent %p\n", udev_device->parent_device);
423 return udev_device->parent_device;
425 udev_device->parent_device = device_new_from_parent(udev_device);
426 return udev_device->parent_device;
429 struct udev_device *udev_device_get_parent_with_subsystem(struct udev_device *udev_device, const char *subsystem)
431 struct udev_device *parent;
433 parent = udev_device_get_parent(udev_device);
434 while (parent != NULL) {
435 const char *parent_subsystem;
437 parent_subsystem = udev_device_get_subsystem(parent);
438 if (parent_subsystem != NULL && strcmp(parent_subsystem, subsystem) == 0)
440 parent = udev_device_get_parent(parent);
446 * udev_device_get_udev:
447 * @udev_device: udev device
449 * Retrieve the udev library context the device was created with.
451 * Returns: the udev library context
453 struct udev *udev_device_get_udev(struct udev_device *udev_device)
455 if (udev_device == NULL)
457 return udev_device->udev;
462 * @udev_device: udev device
464 * Take a reference of a udev device.
466 * Returns: the passed udev device
468 struct udev_device *udev_device_ref(struct udev_device *udev_device)
470 if (udev_device == NULL)
472 udev_device->refcount++;
478 * @udev_device: udev device
480 * Drop a reference of a udev device. If the refcount reaches zero,
481 * the ressources of the device will be released.
484 void udev_device_unref(struct udev_device *udev_device)
486 if (udev_device == NULL)
488 udev_device->refcount--;
489 if (udev_device->refcount > 0)
491 if (udev_device->parent_device != NULL)
492 udev_device_unref(udev_device->parent_device);
493 free(udev_device->syspath);
494 free(udev_device->devnode);
495 free(udev_device->subsystem);
496 list_cleanup(udev_device->udev, &udev_device->devlink_list);
497 list_cleanup(udev_device->udev, &udev_device->properties_list);
498 free(udev_device->action);
499 free(udev_device->driver);
500 free(udev_device->devpath_old);
501 free(udev_device->physdevpath);
502 list_cleanup(udev_device->udev, &udev_device->attr_list);
503 info(udev_device->udev, "udev_device: %p released\n", udev_device);
508 * udev_device_get_devpath:
509 * @udev_device: udev device
511 * Retrieve the kernel devpath value of the udev device. The path
512 * does not contain the sys mount point, and starts with a '/'.
514 * Returns: the devpath of the udev device
516 const char *udev_device_get_devpath(struct udev_device *udev_device)
518 if (udev_device == NULL)
520 return udev_device->devpath;
524 * udev_device_get_syspath:
525 * @udev_device: udev device
527 * Retrieve the sys path of the udev device. The path is an
528 * absolute path and starts with the sys mount point.
530 * Returns: the sys path of the udev device
532 const char *udev_device_get_syspath(struct udev_device *udev_device)
534 if (udev_device == NULL)
536 return udev_device->syspath;
539 const char *udev_device_get_sysname(struct udev_device *udev_device)
541 if (udev_device == NULL)
543 return udev_device->sysname;
547 * udev_device_get_devnode:
548 * @udev_device: udev device
550 * Retrieve the device node file name belonging to the udev device.
551 * The path is an absolute path, and starts with the device directory.
553 * Returns: the device node file name of the udev device, or #NULL if no device node exists
555 const char *udev_device_get_devnode(struct udev_device *udev_device)
557 if (udev_device == NULL)
559 if (!udev_device->info_loaded)
560 device_load_info(udev_device);
561 return udev_device->devnode;
565 * udev_device_get_subsystem:
566 * @udev_device: udev device
568 * Retrieve the subsystem string of the udev device. The string does not
571 * Returns: the subsystem name of the udev device, or #NULL if it can not be determined
573 const char *udev_device_get_subsystem(struct udev_device *udev_device)
575 char subsystem[UTIL_NAME_SIZE];
577 if (udev_device == NULL)
579 if (udev_device->subsystem != NULL)
580 return udev_device->subsystem;
582 /* read "subsytem" link */
583 if (util_get_sys_subsystem(udev_device->udev, udev_device->syspath, subsystem, sizeof(subsystem)) > 0) {
584 udev_device->subsystem = strdup(subsystem);
585 return udev_device->subsystem;
589 if (strncmp(udev_device->devpath, "/module/", 8) == 0) {
590 udev_device->subsystem = strdup("module");
591 return udev_device->subsystem;
593 if (strstr(udev_device->devpath, "/drivers/") != NULL) {
594 udev_device->subsystem = strdup("drivers");
595 return udev_device->subsystem;
597 if (strncmp(udev_device->devpath, "/subsystem/", 11) == 0 ||
598 strncmp(udev_device->devpath, "/class/", 7) == 0 ||
599 strncmp(udev_device->devpath, "/bus/", 5) == 0) {
600 udev_device->subsystem = strdup("subsystem");
601 return udev_device->subsystem;
607 * udev_device_get_devlinks_list_entry:
608 * @udev_device: udev device
610 * Retrieve the list of device links pointing to the device file of
611 * the udev device. The next list entry can be retrieved with
612 * udev_list_entry_next(), which returns #NULL if no more entries exist.
613 * The devlink path can be retrieved from the list entry by
614 * udev_list_entry_get_name(). The path is an absolute path, and starts with
615 * the device directory.
617 * Returns: the first entry of the device node link list
619 struct udev_list_entry *udev_device_get_devlinks_list_entry(struct udev_device *udev_device)
621 if (udev_device == NULL)
623 if (!udev_device->info_loaded)
624 device_load_info(udev_device);
625 return list_get_entry(&udev_device->devlink_list);
629 * udev_device_get_properties_list_entry:
630 * @udev_device: udev device
632 * Retrieve the list of key/value device properties of the udev
633 * device. The next list entry can be retrieved with udev_list_entry_next(),
634 * which returns #NULL if no more entries exist. The property name
635 * can be retrieved from the list entry by udev_list_get_name(),
636 * the property value by udev_list_get_value().
638 * Returns: the first entry of the property list
640 struct udev_list_entry *udev_device_get_properties_list_entry(struct udev_device *udev_device)
642 if (udev_device == NULL)
644 if (!udev_device->info_loaded)
645 device_load_info(udev_device);
646 return list_get_entry(&udev_device->properties_list);
649 const char *udev_device_get_driver(struct udev_device *udev_device)
651 char driver[UTIL_NAME_SIZE];
653 if (udev_device == NULL)
655 if (udev_device->driver != NULL)
656 return udev_device->driver;
657 if (util_get_sys_driver(udev_device->udev, udev_device->syspath, driver, sizeof(driver)) < 2)
659 udev_device->driver = strdup(driver);
660 return udev_device->driver;
663 dev_t udev_device_get_devnum(struct udev_device *udev_device)
665 if (udev_device == NULL)
666 return makedev(0, 0);
667 if (!udev_device->info_loaded)
668 device_load_info(udev_device);
669 return udev_device->devnum;
672 const char *udev_device_get_action(struct udev_device *udev_device)
674 if (udev_device == NULL)
676 return udev_device->action;
679 unsigned long long int udev_device_get_seqnum(struct udev_device *udev_device)
681 if (udev_device == NULL)
683 return udev_device->seqnum;
686 const char *udev_device_get_attr_value(struct udev_device *udev_device, const char *attr)
688 struct udev_list_entry *list_entry;
689 char path[UTIL_PATH_SIZE];
690 char value[UTIL_NAME_SIZE];
694 const char *val = NULL;
696 /* look for possibly already cached result */
697 udev_list_entry_foreach(list_entry, list_get_entry(&udev_device->attr_list)) {
698 if (strcmp(udev_list_entry_get_name(list_entry), attr) == 0) {
699 info(udev_device->udev, "got '%s' (%s) from cache\n", attr, udev_list_entry_get_value(list_entry));
700 return udev_list_entry_get_value(list_entry);
704 util_strlcpy(path, udev_device_get_syspath(udev_device), sizeof(path));
705 util_strlcat(path, "/", sizeof(path));
706 util_strlcat(path, attr, sizeof(path));
708 if (lstat(path, &statbuf) != 0) {
709 info(udev_device->udev, "stat '%s' failed: %m\n", path);
713 if (S_ISLNK(statbuf.st_mode)) {
714 /* links return the last element of the target path */
715 char target[UTIL_NAME_SIZE];
719 len = readlink(path, target, sizeof(target));
722 pos = strrchr(target, '/');
725 info(udev_device->udev, "cache '%s' with link value '%s'\n", attr, pos);
726 list_entry = list_entry_add(udev_device->udev, &udev_device->attr_list, attr, pos, 0, 0);
727 val = udev_list_entry_get_value(list_entry);
733 /* skip directories */
734 if (S_ISDIR(statbuf.st_mode))
737 /* skip non-readable files */
738 if ((statbuf.st_mode & S_IRUSR) == 0)
741 /* read attribute value */
742 fd = open(path, O_RDONLY);
744 info(udev_device->udev, "attribute '%s' can not be opened\n", path);
747 size = read(fd, value, sizeof(value));
751 if (size == sizeof(value))
754 /* got a valid value, store it in cache and return it */
756 util_remove_trailing_chars(value, '\n');
757 info(udev_device->udev, "'%s' has attribute value '%s'\n", path, value);
758 list_entry = list_entry_add(udev_device->udev, &udev_device->attr_list, attr, value, 0, 0);
759 val = udev_list_entry_get_value(list_entry);
763 int device_set_syspath(struct udev_device *udev_device, const char *syspath)
767 udev_device->syspath = strdup(syspath);
768 if (udev_device->syspath == NULL)
770 udev_device->devpath = &udev_device->syspath[strlen(udev_get_sys_path(udev_device->udev))];
771 pos = strrchr(udev_device->syspath, '/');
774 udev_device->sysname = &pos[1];
778 int device_set_subsystem(struct udev_device *udev_device, const char *subsystem)
780 udev_device->subsystem = strdup(subsystem);
781 if (udev_device->subsystem == NULL)
786 int device_set_devnode(struct udev_device *udev_device, const char *devnode)
788 udev_device->devnode = strdup(devnode);
789 if (udev_device->devnode == NULL)
794 int device_add_devlink(struct udev_device *udev_device, const char *devlink)
796 if (list_entry_add(udev_device->udev, &udev_device->devlink_list, devlink, NULL, 1, 0) == NULL)
801 int device_add_property(struct udev_device *udev_device, const char *key, const char *value)
803 if (list_entry_add(udev_device->udev, &udev_device->properties_list, key, value, 1, 0) == NULL)
808 int device_add_property_from_string(struct udev_device *udev_device, const char *property)
810 char name[UTIL_PATH_SIZE];
813 strncpy(name, property, sizeof(name));
814 val = strchr(name, '=');
821 device_add_property(udev_device, name, val);
825 int device_set_action(struct udev_device *udev_device, const char *action)
827 udev_device->action = strdup(action);
828 if (udev_device->action == NULL)
833 int device_set_driver(struct udev_device *udev_device, const char *driver)
835 udev_device->driver = strdup(driver);
836 if (udev_device->driver == NULL)
841 const char *device_get_devpath_old(struct udev_device *udev_device)
843 return udev_device->devpath_old;
846 int device_set_devpath_old(struct udev_device *udev_device, const char *devpath_old)
848 udev_device->devpath_old = strdup(devpath_old);
849 if (udev_device->devpath_old == NULL)
854 const char *device_get_physdevpath(struct udev_device *udev_device)
856 return udev_device->physdevpath;
859 int device_set_physdevpath(struct udev_device *udev_device, const char *physdevpath)
861 udev_device->physdevpath = strdup(physdevpath);
862 if (udev_device->physdevpath == NULL)
867 int device_get_timeout(struct udev_device *udev_device)
869 return udev_device->timeout;
872 int device_set_timeout(struct udev_device *udev_device, int timeout)
874 udev_device->timeout = timeout;
877 int device_get_event_timeout(struct udev_device *udev_device)
879 if (!udev_device->info_loaded)
880 device_load_info(udev_device);
881 return udev_device->event_timeout;
884 int device_set_event_timeout(struct udev_device *udev_device, int event_timeout)
886 udev_device->event_timeout = event_timeout;
890 int device_set_seqnum(struct udev_device *udev_device, unsigned long long int seqnum)
892 udev_device->seqnum = seqnum;
896 int device_set_devnum(struct udev_device *udev_device, dev_t devnum)
898 udev_device->devnum = devnum;
902 int device_get_num_fake_partitions(struct udev_device *udev_device)
904 if (!udev_device->info_loaded)
905 device_load_info(udev_device);
906 return udev_device->num_fake_partitions;
909 int device_set_num_fake_partitions(struct udev_device *udev_device, int num)
911 udev_device->num_fake_partitions = num;
915 int device_get_devlink_priority(struct udev_device *udev_device)
917 if (!udev_device->info_loaded)
918 device_load_info(udev_device);
919 return udev_device->devlink_priority;
922 int device_set_devlink_priority(struct udev_device *udev_device, int prio)
924 udev_device->devlink_priority = prio;
928 int device_get_ignore_remove(struct udev_device *udev_device)
930 if (!udev_device->info_loaded)
931 device_load_info(udev_device);
932 return udev_device->ignore_remove;
935 int device_set_ignore_remove(struct udev_device *udev_device, int ignore)
937 udev_device->ignore_remove = ignore;