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: %s\n", filename, strerror(errno));
83 if ((stats.st_mode & S_IFMT) == S_IFLNK) {
84 char target[UTIL_PATH_SIZE];
87 target_len = readlink(filename, target, sizeof(target));
89 target[target_len] = '\0';
91 info(udev_device->udev, "error reading db link %s: %s\n", filename, strerror(errno));
94 if (asprintf(&udev_device->devnode, "%s/%s", udev_get_dev_path(udev_device->udev), target) < 0)
96 info(udev_device->udev, "device %p filled with db symlink data '%s'\n", udev_device, udev_device->devnode);
100 f = fopen(filename, "r");
102 info(udev_device->udev, "error reading db file %s: %s\n", filename, strerror(errno));
105 while (fgets(line, sizeof(line), f)) {
117 asprintf(&udev_device->devnode, "%s/%s", udev_get_dev_path(udev_device->udev), val);
120 util_strlcpy(filename, udev_get_dev_path(udev_device->udev), sizeof(filename));
121 util_strlcat(filename, "/", sizeof(filename));
122 util_strlcat(filename, val, sizeof(filename));
123 device_add_devlink(udev_device, filename);
126 device_set_devlink_priority(udev_device, atoi(val));
129 device_set_event_timeout(udev_device, atoi(val));
132 device_set_num_fake_partitions(udev_device, atoi(val));
135 device_set_ignore_remove(udev_device, atoi(val));
138 device_add_property_from_string(udev_device, val);
144 info(udev_device->udev, "device %p filled with db file data\n", udev_device);
148 static int device_read_uevent_file(struct udev_device *udev_device)
150 char filename[UTIL_PATH_SIZE];
152 char line[UTIL_LINE_SIZE];
156 util_strlcpy(filename, udev_device->syspath, sizeof(filename));
157 util_strlcat(filename, "/uevent", sizeof(filename));
158 f = fopen(filename, "r");
162 while (fgets(line, sizeof(line), f)) {
165 pos = strchr(line, '\n');
170 if (strncmp(line, "MAJOR=", 6) == 0)
171 maj = strtoull(&line[6], NULL, 10);
172 else if (strncmp(line, "MINOR=", 6) == 0)
173 min = strtoull(&line[6], NULL, 10);
175 device_add_property_from_string(udev_device, line);
178 udev_device->devnum = makedev(maj, min);
184 static void device_load_info(struct udev_device *device)
186 device_read_uevent_file(device);
187 device_read_db(device);
188 device->info_loaded = 1;
191 void device_set_info_loaded(struct udev_device *device)
193 device->info_loaded = 1;
196 struct udev_device *device_init(struct udev *udev)
198 struct udev_device *udev_device;
203 udev_device = malloc(sizeof(struct udev_device));
204 if (udev_device == NULL)
206 memset(udev_device, 0x00, sizeof(struct udev_device));
207 udev_device->refcount = 1;
208 udev_device->udev = udev;
209 list_init(&udev_device->devlink_list);
210 list_init(&udev_device->properties_list);
211 list_init(&udev_device->attr_list);
212 info(udev_device->udev, "udev_device: %p created\n", udev_device);
217 * udev_device_new_from_syspath:
218 * @udev: udev library context
219 * @syspath: sys device path including sys directory
221 * Create new udev device, and fill in information from the sys
222 * device and the udev database entry. The sypath is the absolute
223 * path to the device, including the sys mount point.
225 * The initial refcount is 1, and needs to be decremented to
226 * release the ressources of the udev device.
228 * Returns: a new udev device, or #NULL, if it does not exist
230 struct udev_device *udev_device_new_from_syspath(struct udev *udev, const char *syspath)
235 char path[UTIL_PATH_SIZE];
237 struct udev_device *udev_device;
244 /* path starts in sys */
245 len = strlen(udev_get_sys_path(udev));
246 if (strncmp(syspath, udev_get_sys_path(udev), len) != 0) {
247 info(udev, "not in sys :%s\n", syspath);
251 /* path is not a root directory */
252 subdir = &syspath[len+1];
253 pos = strrchr(subdir, '/');
254 if (pos == NULL || pos < &subdir[2]) {
255 info(udev, "not a subdir :%s\n", syspath);
259 /* resolve possible symlink to real path */
260 util_strlcpy(path, syspath, sizeof(path));
261 util_resolve_sys_link(udev, path, sizeof(path));
263 /* path exists in sys */
264 if (strncmp(&syspath[len], "/devices/", 9) == 0 ||
265 strncmp(&syspath[len], "/class/", 7) == 0 ||
266 strncmp(&syspath[len], "/block/", 7) == 0) {
267 char file[UTIL_PATH_SIZE];
269 /* all "devices" require a "uevent" file */
270 util_strlcpy(file, path, sizeof(file));
271 util_strlcat(file, "/uevent", sizeof(file));
272 if (stat(file, &statbuf) != 0) {
273 info(udev, "not a device: %s\n", syspath);
277 /* everything else just needs to be a directory */
278 if (stat(path, &statbuf) != 0 || !S_ISDIR(statbuf.st_mode)) {
279 info(udev, "directory not found: %s\n", syspath);
284 udev_device = device_init(udev);
285 if (udev_device == NULL)
288 device_set_syspath(udev_device, path);
289 info(udev, "device %p has devpath '%s'\n", udev_device, udev_device_get_devpath(udev_device));
294 struct udev_device *udev_device_new_from_devnum(struct udev *udev, char type, dev_t devnum)
296 char path[UTIL_PATH_SIZE];
297 const char *type_str;
298 struct udev_enumerate *enumerate;
299 struct udev_list_entry *list_entry;
300 struct udev_device *device = NULL;
304 else if (type == 'c')
309 /* /sys/dev/{block,char}/<maj>:<min> link */
310 snprintf(path, sizeof(path), "%s/dev/%s/%u:%u", udev_get_sys_path(udev),
311 type_str, major(devnum), minor(devnum));
312 if (util_resolve_sys_link(udev, path, sizeof(path)) == 0)
313 return udev_device_new_from_syspath(udev, path);
315 /* fallback to search sys devices for the major/minor */
317 enumerate = udev_enumerate_new_from_devices(udev, "block", NULL);
318 else if (type == 'c')
319 enumerate = udev_enumerate_new_from_devices(udev, "!block", NULL);
320 if (enumerate == NULL)
322 udev_list_entry_foreach(list_entry, udev_enumerate_get_list_entry(enumerate)) {
323 struct udev_device *device_loop;
325 device_loop = udev_device_new_from_syspath(udev, udev_list_entry_get_name(list_entry));
326 if (device_loop != NULL) {
327 if (udev_device_get_devnum(device_loop) == devnum) {
328 const char *subsystem;
330 subsystem = udev_device_get_subsystem(device_loop);
331 if (type == 'b' && strcmp(subsystem, "block") != 0)
333 if (type == 'c' && strcmp(subsystem, "block") == 0)
335 device = device_loop;
338 udev_device_unref(device_loop);
341 udev_enumerate_unref(enumerate);
345 static struct udev_device *device_new_from_parent(struct udev_device *udev_device)
347 struct udev_device *udev_device_parent = NULL;
348 char path[UTIL_PATH_SIZE];
351 /* follow "device" link in deprecated sys layout */
352 if (strncmp(udev_device->devpath, "/class/", 7) == 0 ||
353 strncmp(udev_device->devpath, "/block/", 7) == 0) {
354 util_strlcpy(path, udev_device->syspath, sizeof(path));
355 util_strlcat(path, "/device", sizeof(path));
356 if (util_resolve_sys_link(udev_device->udev, path, sizeof(path)) == 0)
357 udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
358 return udev_device_parent;
361 util_strlcpy(path, udev_device->syspath, sizeof(path));
362 subdir = &path[strlen(udev_get_sys_path(udev_device->udev))+1];
366 pos = strrchr(subdir, '/');
367 if (pos == NULL || pos < &subdir[2])
370 udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
371 if (udev_device_parent != NULL)
372 return udev_device_parent;
377 struct udev_device *udev_device_get_parent(struct udev_device *udev_device)
379 if (udev_device == NULL)
382 if (udev_device->parent_device != NULL) {
383 info(udev_device->udev, "returning existing parent %p\n", udev_device->parent_device);
384 return udev_device->parent_device;
386 udev_device->parent_device = device_new_from_parent(udev_device);
387 return udev_device->parent_device;
391 * udev_device_get_udev:
392 * @udev_device: udev device
394 * Retrieve the udev library context the device was created with.
396 * Returns: the udev library context
398 struct udev *udev_device_get_udev(struct udev_device *udev_device)
400 if (udev_device == NULL)
402 return udev_device->udev;
407 * @udev_device: udev device
409 * Take a reference of a udev device.
411 * Returns: the passed udev device
413 struct udev_device *udev_device_ref(struct udev_device *udev_device)
415 if (udev_device == NULL)
417 udev_device->refcount++;
423 * @udev_device: udev device
425 * Drop a reference of a udev device. If the refcount reaches zero,
426 * the ressources of the device will be released.
429 void udev_device_unref(struct udev_device *udev_device)
431 if (udev_device == NULL)
433 udev_device->refcount--;
434 if (udev_device->refcount > 0)
436 if (udev_device->parent_device != NULL)
437 udev_device_unref(udev_device->parent_device);
438 free(udev_device->syspath);
439 free(udev_device->devnode);
440 free(udev_device->subsystem);
441 list_cleanup(udev_device->udev, &udev_device->devlink_list);
442 list_cleanup(udev_device->udev, &udev_device->properties_list);
443 free(udev_device->action);
444 free(udev_device->driver);
445 free(udev_device->devpath_old);
446 free(udev_device->physdevpath);
447 list_cleanup(udev_device->udev, &udev_device->attr_list);
448 info(udev_device->udev, "udev_device: %p released\n", udev_device);
453 * udev_device_get_devpath:
454 * @udev_device: udev device
456 * Retrieve the kernel devpath value of the udev device. The path
457 * does not contain the sys mount point, and starts with a '/'.
459 * Returns: the devpath of the udev device
461 const char *udev_device_get_devpath(struct udev_device *udev_device)
463 if (udev_device == NULL)
465 return udev_device->devpath;
469 * udev_device_get_syspath:
470 * @udev_device: udev device
472 * Retrieve the sys path of the udev device. The path is an
473 * absolute path and starts with the sys mount point.
475 * Returns: the sys path of the udev device
477 const char *udev_device_get_syspath(struct udev_device *udev_device)
479 if (udev_device == NULL)
481 return udev_device->syspath;
484 const char *udev_device_get_sysname(struct udev_device *udev_device)
486 if (udev_device == NULL)
488 return udev_device->sysname;
492 * udev_device_get_devnode:
493 * @udev_device: udev device
495 * Retrieve the device node file name belonging to the udev device.
496 * The path is an absolute path, and starts with the device directory.
498 * Returns: the device node file name of the udev device, or #NULL if no device node exists
500 const char *udev_device_get_devnode(struct udev_device *udev_device)
502 if (udev_device == NULL)
504 if (!udev_device->info_loaded)
505 device_load_info(udev_device);
506 return udev_device->devnode;
510 * udev_device_get_subsystem:
511 * @udev_device: udev device
513 * Retrieve the subsystem string of the udev device. The string does not
516 * Returns: the subsystem name of the udev device, or #NULL if it can not be determined
518 const char *udev_device_get_subsystem(struct udev_device *udev_device)
520 char subsystem[UTIL_NAME_SIZE];
522 if (udev_device == NULL)
524 if (udev_device->subsystem != NULL)
525 return udev_device->subsystem;
527 /* read "subsytem" link */
528 if (util_get_sys_subsystem(udev_device->udev, udev_device->syspath, subsystem, sizeof(subsystem)) > 0) {
529 udev_device->subsystem = strdup(subsystem);
530 return udev_device->subsystem;
534 if (strncmp(udev_device->devpath, "/module/", 8) == 0) {
535 udev_device->subsystem = strdup("module");
536 return udev_device->subsystem;
538 if (strstr(udev_device->devpath, "/drivers/") != NULL) {
539 udev_device->subsystem = strdup("drivers");
540 return udev_device->subsystem;
542 if (strncmp(udev_device->devpath, "/subsystem/", 11) == 0 ||
543 strncmp(udev_device->devpath, "/class/", 7) == 0 ||
544 strncmp(udev_device->devpath, "/bus/", 5) == 0) {
545 udev_device->subsystem = strdup("subsystem");
546 return udev_device->subsystem;
552 * udev_device_get_devlinks_list_entry:
553 * @udev_device: udev device
555 * Retrieve the list of device links pointing to the device file of
556 * the udev device. The next list entry can be retrieved with
557 * udev_list_entry_next(), which returns #NULL if no more entries exist.
558 * The devlink path can be retrieved from the list entry by
559 * udev_list_entry_get_name(). The path is an absolute path, and starts with
560 * the device directory.
562 * Returns: the first entry of the device node link list
564 struct udev_list_entry *udev_device_get_devlinks_list_entry(struct udev_device *udev_device)
566 if (udev_device == NULL)
568 if (!udev_device->info_loaded)
569 device_load_info(udev_device);
570 return list_get_entry(&udev_device->devlink_list);
574 * udev_device_get_properties_list_entry:
575 * @udev_device: udev device
577 * Retrieve the list of key/value device properties of the udev
578 * device. The next list entry can be retrieved with udev_list_entry_next(),
579 * which returns #NULL if no more entries exist. The property name
580 * can be retrieved from the list entry by udev_list_get_name(),
581 * the property value by udev_list_get_value().
583 * Returns: the first entry of the property list
585 struct udev_list_entry *udev_device_get_properties_list_entry(struct udev_device *udev_device)
587 if (udev_device == NULL)
589 if (!udev_device->info_loaded)
590 device_load_info(udev_device);
591 return list_get_entry(&udev_device->properties_list);
594 const char *udev_device_get_driver(struct udev_device *udev_device)
596 char driver[UTIL_NAME_SIZE];
598 if (udev_device == NULL)
600 if (udev_device->driver != NULL)
601 return udev_device->driver;
602 if (util_get_sys_driver(udev_device->udev, udev_device->syspath, driver, sizeof(driver)) < 2)
604 udev_device->driver = strdup(driver);
605 return udev_device->driver;
608 dev_t udev_device_get_devnum(struct udev_device *udev_device)
610 if (udev_device == NULL)
611 return makedev(0, 0);
612 if (!udev_device->info_loaded)
613 device_load_info(udev_device);
614 return udev_device->devnum;
617 const char *udev_device_get_action(struct udev_device *udev_device)
619 if (udev_device == NULL)
621 return udev_device->action;
624 unsigned long long int udev_device_get_seqnum(struct udev_device *udev_device)
626 if (udev_device == NULL)
628 return udev_device->seqnum;
631 const char *udev_device_get_attr_value(struct udev_device *udev_device, const char *attr)
633 struct udev_list_entry *list_entry;
634 char path[UTIL_PATH_SIZE];
635 char value[UTIL_NAME_SIZE];
639 const char *val = NULL;
641 /* look for possibly already cached result */
642 udev_list_entry_foreach(list_entry, list_get_entry(&udev_device->attr_list)) {
643 if (strcmp(udev_list_entry_get_name(list_entry), attr) == 0) {
644 info(udev_device->udev, "got '%s' (%s) from cache\n", attr, udev_list_entry_get_value(list_entry));
645 return udev_list_entry_get_value(list_entry);
649 util_strlcpy(path, udev_device_get_syspath(udev_device), sizeof(path));
650 util_strlcat(path, "/", sizeof(path));
651 util_strlcat(path, attr, sizeof(path));
653 if (lstat(path, &statbuf) != 0) {
654 info(udev_device->udev, "stat '%s' failed: %s\n", path, strerror(errno));
658 if (S_ISLNK(statbuf.st_mode)) {
659 /* links return the last element of the target path */
660 char target[UTIL_NAME_SIZE];
664 len = readlink(path, target, sizeof(target));
667 pos = strrchr(target, '/');
670 info(udev_device->udev, "cache '%s' with link value '%s'\n", attr, pos);
671 list_entry = list_entry_add(udev_device->udev, &udev_device->attr_list, attr, pos, 0, 0);
672 val = udev_list_entry_get_value(list_entry);
678 /* skip directories */
679 if (S_ISDIR(statbuf.st_mode))
682 /* skip non-readable files */
683 if ((statbuf.st_mode & S_IRUSR) == 0)
686 /* read attribute value */
687 fd = open(path, O_RDONLY);
689 info(udev_device->udev, "attribute '%s' can not be opened\n", path);
692 size = read(fd, value, sizeof(value));
696 if (size == sizeof(value))
699 /* got a valid value, store it in cache and return it */
701 util_remove_trailing_chars(value, '\n');
702 info(udev_device->udev, "'%s' has attribute value '%s'\n", path, value);
703 list_entry = list_entry_add(udev_device->udev, &udev_device->attr_list, attr, value, 0, 0);
704 val = udev_list_entry_get_value(list_entry);
708 int device_set_syspath(struct udev_device *udev_device, const char *syspath)
712 udev_device->syspath = strdup(syspath);
713 if (udev_device->syspath == NULL)
715 udev_device->devpath = &udev_device->syspath[strlen(udev_get_sys_path(udev_device->udev))];
716 pos = strrchr(udev_device->syspath, '/');
719 udev_device->sysname = &pos[1];
723 int device_set_subsystem(struct udev_device *udev_device, const char *subsystem)
725 udev_device->subsystem = strdup(subsystem);
726 if (udev_device->subsystem == NULL)
731 int device_set_devnode(struct udev_device *udev_device, const char *devnode)
733 udev_device->devnode = strdup(devnode);
734 if (udev_device->devnode == NULL)
739 int device_add_devlink(struct udev_device *udev_device, const char *devlink)
741 if (list_entry_add(udev_device->udev, &udev_device->devlink_list, devlink, NULL, 1, 0) == NULL)
746 int device_add_property(struct udev_device *udev_device, const char *key, const char *value)
748 if (list_entry_add(udev_device->udev, &udev_device->properties_list, key, value, 1, 0) == NULL)
753 int device_add_property_from_string(struct udev_device *udev_device, const char *property)
755 char name[UTIL_PATH_SIZE];
758 strncpy(name, property, sizeof(name));
759 val = strchr(name, '=');
766 device_add_property(udev_device, name, val);
770 int device_set_action(struct udev_device *udev_device, const char *action)
772 udev_device->action = strdup(action);
773 if (udev_device->action == NULL)
778 int device_set_driver(struct udev_device *udev_device, const char *driver)
780 udev_device->driver = strdup(driver);
781 if (udev_device->driver == NULL)
786 const char *device_get_devpath_old(struct udev_device *udev_device)
788 return udev_device->devpath_old;
791 int device_set_devpath_old(struct udev_device *udev_device, const char *devpath_old)
793 udev_device->devpath_old = strdup(devpath_old);
794 if (udev_device->devpath_old == NULL)
799 const char *device_get_physdevpath(struct udev_device *udev_device)
801 return udev_device->physdevpath;
804 int device_set_physdevpath(struct udev_device *udev_device, const char *physdevpath)
806 udev_device->physdevpath = strdup(physdevpath);
807 if (udev_device->physdevpath == NULL)
812 int device_get_timeout(struct udev_device *udev_device)
814 return udev_device->timeout;
817 int device_set_timeout(struct udev_device *udev_device, int timeout)
819 udev_device->timeout = timeout;
822 int device_get_event_timeout(struct udev_device *udev_device)
824 if (!udev_device->info_loaded)
825 device_load_info(udev_device);
826 return udev_device->event_timeout;
829 int device_set_event_timeout(struct udev_device *udev_device, int event_timeout)
831 udev_device->event_timeout = event_timeout;
835 int device_set_seqnum(struct udev_device *udev_device, unsigned long long int seqnum)
837 udev_device->seqnum = seqnum;
841 int device_set_devnum(struct udev_device *udev_device, dev_t devnum)
843 udev_device->devnum = devnum;
847 int device_get_num_fake_partitions(struct udev_device *udev_device)
849 if (!udev_device->info_loaded)
850 device_load_info(udev_device);
851 return udev_device->num_fake_partitions;
854 int device_set_num_fake_partitions(struct udev_device *udev_device, int num)
856 udev_device->num_fake_partitions = num;
860 int device_get_devlink_priority(struct udev_device *udev_device)
862 if (!udev_device->info_loaded)
863 device_load_info(udev_device);
864 return udev_device->devlink_priority;
867 int device_set_devlink_priority(struct udev_device *udev_device, int prio)
869 udev_device->devlink_priority = prio;
873 int device_get_ignore_remove(struct udev_device *udev_device)
875 if (!udev_device->info_loaded)
876 device_load_info(udev_device);
877 return udev_device->ignore_remove;
880 int device_set_ignore_remove(struct udev_device *udev_device, int ignore)
882 udev_device->ignore_remove = ignore;