2 * libudev - interface to udev device information
4 * Copyright (C) 2008-2009 Kay Sievers <kay.sievers@vrfy.org>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
24 #include "libudev-private.h"
27 * SECTION:libudev-device
28 * @short_description: kernel sys devices
30 * Representation of kernel sys devices. Devices are uniquely identified
31 * by their syspath, every device has exactly one path in the kernel sys
32 * filesystem. Devices usually belong to a kernel subsystem, and and have
33 * a unique name inside that subsytem.
39 * Opaque object representing one kernel sys device.
43 struct udev_device *parent_device;
57 size_t monitor_buf_len;
58 struct udev_list_node devlinks_list;
59 struct udev_list_node properties_list;
60 struct udev_list_node sysattr_list;
61 unsigned long long int seqnum;
64 int num_fake_partitions;
69 unsigned int parent_set:1;
70 unsigned int subsystem_set:1;
71 unsigned int devtype_set:1;
72 unsigned int devlinks_uptodate:1;
73 unsigned int envp_uptodate:1;
74 unsigned int driver_set:1;
75 unsigned int info_loaded:1;
76 unsigned int ignore_remove:1;
79 static size_t devpath_to_db_path(struct udev *udev, const char *devpath, char *filename, size_t len)
85 l = util_strpcpyl(&s, len, udev_get_dev_path(udev), "/.udev/db/", NULL);
86 return util_path_encode(devpath, s, l);
89 int udev_device_read_db(struct udev_device *udev_device)
92 char filename[UTIL_PATH_SIZE];
93 char line[UTIL_LINE_SIZE];
96 devpath_to_db_path(udev_device->udev, udev_device->devpath, filename, sizeof(filename));
98 if (lstat(filename, &stats) != 0) {
99 dbg(udev_device->udev, "no db file to read %s: %m\n", filename);
102 if ((stats.st_mode & S_IFMT) == S_IFLNK) {
103 char target[UTIL_PATH_SIZE];
104 char devnode[UTIL_PATH_SIZE];
108 target_len = readlink(filename, target, sizeof(target));
110 target[target_len] = '\0';
112 dbg(udev_device->udev, "error reading db link %s: %m\n", filename);
116 next = strchr(target, ' ');
121 util_strscpyl(devnode, sizeof(devnode), udev_get_dev_path(udev_device->udev), "/", target, NULL);
122 udev_device_set_devnode(udev_device, devnode);
123 while (next != NULL) {
124 char devlink[UTIL_PATH_SIZE];
128 next = strchr(next, ' ');
133 util_strscpyl(devlink, sizeof(devlink), udev_get_dev_path(udev_device->udev), "/", lnk, NULL);
134 udev_device_add_devlink(udev_device, devlink);
136 info(udev_device->udev, "device %p filled with db symlink data '%s'\n", udev_device, udev_device->devnode);
140 f = fopen(filename, "r");
142 dbg(udev_device->udev, "error reading db file %s: %m\n", filename);
145 while (fgets(line, sizeof(line), f)) {
156 util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev_device->udev), "/", val, NULL);
157 udev_device_set_devnode(udev_device, filename);
160 util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev_device->udev), "/", val, NULL);
161 udev_device_add_devlink(udev_device, filename);
164 udev_device_set_devlink_priority(udev_device, atoi(val));
167 udev_device_set_event_timeout(udev_device, atoi(val));
170 udev_device_set_num_fake_partitions(udev_device, atoi(val));
173 udev_device_set_ignore_remove(udev_device, atoi(val));
176 udev_device_add_property_from_string(udev_device, val);
179 udev_device_set_watch_handle(udev_device, atoi(val));
185 info(udev_device->udev, "device %p filled with db file data\n", udev_device);
189 int udev_device_read_uevent_file(struct udev_device *udev_device)
191 char filename[UTIL_PATH_SIZE];
193 char line[UTIL_LINE_SIZE];
197 util_strscpyl(filename, sizeof(filename), udev_device->syspath, "/uevent", NULL);
198 f = fopen(filename, "r");
202 while (fgets(line, sizeof(line), f)) {
205 pos = strchr(line, '\n');
210 if (strncmp(line, "DEVTYPE=", 8) == 0)
211 udev_device_set_devtype(udev_device, &line[8]);
212 else if (strncmp(line, "MAJOR=", 6) == 0)
213 maj = strtoull(&line[6], NULL, 10);
214 else if (strncmp(line, "MINOR=", 6) == 0)
215 min = strtoull(&line[6], NULL, 10);
216 else if (strncmp(line, "DEVNAME=", 8) == 0)
217 udev_device_set_knodename(udev_device, &line[8]);
219 udev_device_add_property_from_string(udev_device, line);
222 udev_device->devnum = makedev(maj, min);
228 static void device_load_info(struct udev_device *device)
230 device->info_loaded = 1;
231 udev_device_read_uevent_file(device);
232 udev_device_read_db(device);
235 void udev_device_set_info_loaded(struct udev_device *device)
237 device->info_loaded = 1;
240 struct udev_device *udev_device_new(struct udev *udev)
242 struct udev_device *udev_device;
243 struct udev_list_entry *list_entry;
248 udev_device = calloc(1, sizeof(struct udev_device));
249 if (udev_device == NULL)
251 udev_device->refcount = 1;
252 udev_device->udev = udev;
253 udev_list_init(&udev_device->devlinks_list);
254 udev_list_init(&udev_device->properties_list);
255 udev_list_init(&udev_device->sysattr_list);
256 udev_device->event_timeout = -1;
257 udev_device->watch_handle = -1;
258 /* copy global properties */
259 udev_list_entry_foreach(list_entry, udev_get_properties_list_entry(udev))
260 udev_device_add_property(udev_device,
261 udev_list_entry_get_name(list_entry),
262 udev_list_entry_get_value(list_entry));
263 dbg(udev_device->udev, "udev_device: %p created\n", udev_device);
268 * udev_device_new_from_syspath:
269 * @udev: udev library context
270 * @syspath: sys device path including sys directory
272 * Create new udev device, and fill in information from the sys
273 * device and the udev database entry. The sypath is the absolute
274 * path to the device, including the sys mount point.
276 * The initial refcount is 1, and needs to be decremented to
277 * release the resources of the udev device.
279 * Returns: a new udev device, or #NULL, if it does not exist
281 struct udev_device *udev_device_new_from_syspath(struct udev *udev, const char *syspath)
285 char path[UTIL_PATH_SIZE];
288 struct udev_device *udev_device;
295 /* path starts in sys */
296 len = strlen(udev_get_sys_path(udev));
297 if (strncmp(syspath, udev_get_sys_path(udev), len) != 0) {
298 info(udev, "not in sys :%s\n", syspath);
302 /* path is not a root directory */
303 subdir = &syspath[len+1];
304 pos = strrchr(subdir, '/');
305 if (pos == NULL || pos[1] == '\0' || pos < &subdir[2]) {
306 dbg(udev, "not a subdir :%s\n", syspath);
310 /* resolve possible symlink to real path */
311 util_strscpy(path, sizeof(path), syspath);
312 util_resolve_sys_link(udev, path, sizeof(path));
314 /* try to resolve the silly block layout if needed */
315 if (strncmp(&path[len], "/block/", 7) == 0) {
316 char block[UTIL_PATH_SIZE];
317 char part[UTIL_PATH_SIZE];
319 util_strscpy(block, sizeof(block), path);
320 pos = strrchr(block, '/');
323 util_strscpy(part, sizeof(part), pos);
325 if (util_resolve_sys_link(udev, block, sizeof(block)) == 0)
326 util_strscpyl(path, sizeof(path), block, part, NULL);
329 /* path exists in sys */
330 if (strncmp(&syspath[len], "/devices/", 9) == 0 ||
331 strncmp(&syspath[len], "/class/", 7) == 0 ||
332 strncmp(&syspath[len], "/block/", 7) == 0) {
333 char file[UTIL_PATH_SIZE];
335 /* all "devices" require a "uevent" file */
336 util_strscpyl(file, sizeof(file), path, "/uevent", NULL);
337 if (stat(file, &statbuf) != 0) {
338 dbg(udev, "not a device: %s\n", syspath);
342 /* everything else just needs to be a directory */
343 if (stat(path, &statbuf) != 0 || !S_ISDIR(statbuf.st_mode)) {
344 dbg(udev, "directory not found: %s\n", syspath);
349 udev_device = udev_device_new(udev);
350 if (udev_device == NULL)
353 udev_device_set_syspath(udev_device, path);
354 info(udev, "device %p has devpath '%s'\n", udev_device, udev_device_get_devpath(udev_device));
360 * udev_device_new_from_devnum:
361 * @udev: udev library context
362 * @type: char or block device
363 * @devnum: device major/minor number
365 * Create new udev device, and fill in information from the sys
366 * device and the udev database entry. The device is looked up
367 * by its major/minor number. Character and block device numbers
368 * are not unique across the two types, they do not share the same
371 * The initial refcount is 1, and needs to be decremented to
372 * release the resources of the udev device.
374 * Returns: a new udev device, or #NULL, if it does not exist
376 struct udev_device *udev_device_new_from_devnum(struct udev *udev, char type, dev_t devnum)
378 char path[UTIL_PATH_SIZE];
379 const char *type_str;
380 struct udev_enumerate *udev_enumerate;
381 struct udev_list_entry *list_entry;
382 struct udev_device *device = NULL;
386 else if (type == 'c')
391 /* /sys/dev/{block,char}/<maj>:<min> link */
392 snprintf(path, sizeof(path), "%s/dev/%s/%u:%u", udev_get_sys_path(udev),
393 type_str, major(devnum), minor(devnum));
394 if (util_resolve_sys_link(udev, path, sizeof(path)) == 0)
395 return udev_device_new_from_syspath(udev, path);
397 udev_enumerate = udev_enumerate_new(udev);
398 if (udev_enumerate == NULL)
401 /* fallback to search sys devices for the major/minor */
403 udev_enumerate_add_match_subsystem(udev_enumerate, "block");
404 else if (type == 'c')
405 udev_enumerate_add_nomatch_subsystem(udev_enumerate, "block");
406 udev_enumerate_scan_devices(udev_enumerate);
407 udev_list_entry_foreach(list_entry, udev_enumerate_get_list_entry(udev_enumerate)) {
408 struct udev_device *device_loop;
410 device_loop = udev_device_new_from_syspath(udev, udev_list_entry_get_name(list_entry));
411 if (device_loop != NULL) {
412 if (udev_device_get_devnum(device_loop) == devnum) {
413 if (type == 'b' && strcmp(udev_device_get_subsystem(device_loop), "block") != 0)
415 if (type == 'c' && strcmp(udev_device_get_subsystem(device_loop), "block") == 0)
417 device = device_loop;
420 udev_device_unref(device_loop);
423 udev_enumerate_unref(udev_enumerate);
428 * udev_device_new_from_subsystem_sysname:
429 * @udev: udev library context
430 * @subsystem: the subsytem of the device
431 * @sysname: the name of the device
433 * Create new udev device, and fill in information from the sys
434 * device and the udev database entry. The device is looked up
435 * by the subsytem and name string of the device, like "mem",
436 * "zero", or "block", "sda".
438 * The initial refcount is 1, and needs to be decremented to
439 * release the resources of the udev device.
441 * Returns: a new udev device, or #NULL, if it does not exist
443 struct udev_device *udev_device_new_from_subsystem_sysname(struct udev *udev, const char *subsystem, const char *sysname)
445 char path_full[UTIL_PATH_SIZE];
451 l = util_strpcpyl(&path, sizeof(path_full), udev_get_sys_path(udev), NULL);
453 if (strcmp(subsystem, "subsystem") == 0) {
454 util_strscpyl(path, l, "/subsystem/", sysname, NULL);
455 if (stat(path_full, &statbuf) == 0)
458 util_strscpyl(path, l, "/bus/", sysname, NULL);
459 if (stat(path_full, &statbuf) == 0)
462 util_strscpyl(path, l, "/class/", sysname, NULL);
463 if (stat(path_full, &statbuf) == 0)
468 if (strcmp(subsystem, "module") == 0) {
469 util_strscpyl(path, l, "/module/", sysname, NULL);
470 if (stat(path_full, &statbuf) == 0)
475 if (strcmp(subsystem, "drivers") == 0) {
476 char subsys[UTIL_NAME_SIZE];
479 util_strscpy(subsys, sizeof(subsys), sysname);
480 driver = strchr(subsys, ':');
481 if (driver != NULL) {
485 util_strscpyl(path, l, "/subsystem/", subsys, "/drivers/", driver, NULL);
486 if (stat(path_full, &statbuf) == 0)
489 util_strscpyl(path, l, "/bus/", subsys, "/drivers/", driver, NULL);
490 if (stat(path_full, &statbuf) == 0)
496 util_strscpyl(path, l, "/subsystem/", subsystem, "/devices/", sysname, NULL);
497 if (stat(path_full, &statbuf) == 0)
500 util_strscpyl(path, l, "/bus/", subsystem, "/devices/", sysname, NULL);
501 if (stat(path_full, &statbuf) == 0)
504 util_strscpyl(path, l, "/class/", subsystem, "/", sysname, NULL);
505 if (stat(path_full, &statbuf) == 0)
510 return udev_device_new_from_syspath(udev, path_full);
513 static struct udev_device *device_new_from_parent(struct udev_device *udev_device)
515 struct udev_device *udev_device_parent = NULL;
516 char path[UTIL_PATH_SIZE];
519 /* follow "device" link in deprecated sys layout */
520 if (strncmp(udev_device->devpath, "/class/", 7) == 0 ||
521 strncmp(udev_device->devpath, "/block/", 7) == 0) {
522 util_strscpyl(path, sizeof(path), udev_device->syspath, "/device", NULL);
523 if (util_resolve_sys_link(udev_device->udev, path, sizeof(path)) == 0) {
524 udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
525 if (udev_device_parent != NULL)
526 return udev_device_parent;
530 util_strscpy(path, sizeof(path), udev_device->syspath);
531 subdir = &path[strlen(udev_get_sys_path(udev_device->udev))+1];
535 pos = strrchr(subdir, '/');
536 if (pos == NULL || pos < &subdir[2])
539 udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
540 if (udev_device_parent != NULL)
541 return udev_device_parent;
547 * udev_device_get_parent:
548 * @udev_device: the device to start searching from
550 * Find the next parent device, and fill in information from the sys
551 * device and the udev database entry.
553 * The returned the device is not referenced. It is attached to the
554 * child device, and will be cleaned up when the child device
557 * It is not neccessarily just the upper level directory, empty or not
558 * recognized sys directories are ignored.
560 * It can be called as many times as needed, without caring about
563 * Returns: a new udev device, or #NULL, if it no parent exist.
565 struct udev_device *udev_device_get_parent(struct udev_device *udev_device)
567 if (udev_device == NULL)
569 if (!udev_device->parent_set) {
570 udev_device->parent_set = 1;
571 udev_device->parent_device = device_new_from_parent(udev_device);
573 if (udev_device->parent_device != NULL)
574 dbg(udev_device->udev, "returning existing parent %p\n", udev_device->parent_device);
575 return udev_device->parent_device;
579 * udev_device_get_parent_with_subsystem_devtype:
580 * @udev_device: udev device to start searching from
581 * @subsystem: the subsytem of the device
582 * @devtype: the type (DEVTYPE) of the device
584 * Find the next parent device, with a matching subsystem and devtype
585 * value, and fill in information from the sys device and the udev
588 * The returned the device is not referenced. It is attached to the
589 * child device, and will be cleaned up when the child device
592 * It can be called as many times as needed, without caring about
595 * Returns: a new udev device, or #NULL if no matching parent exists.
597 struct udev_device *udev_device_get_parent_with_subsystem_devtype(struct udev_device *udev_device, const char *subsystem, const char *devtype)
599 struct udev_device *parent;
601 if (subsystem == NULL)
604 parent = udev_device_get_parent(udev_device);
605 while (parent != NULL) {
606 const char *parent_subsystem;
607 const char *parent_devtype;
609 parent_subsystem = udev_device_get_subsystem(parent);
610 if (parent_subsystem != NULL && strcmp(parent_subsystem, subsystem) == 0) {
613 parent_devtype = udev_device_get_devtype(parent);
614 if (parent_devtype != NULL && strcmp(parent_devtype, devtype) == 0)
617 parent = udev_device_get_parent(parent);
623 * udev_device_get_udev:
624 * @udev_device: udev device
626 * Retrieve the udev library context the device was created with.
628 * Returns: the udev library context
630 struct udev *udev_device_get_udev(struct udev_device *udev_device)
632 if (udev_device == NULL)
634 return udev_device->udev;
639 * @udev_device: udev device
641 * Take a reference of a udev device.
643 * Returns: the passed udev device
645 struct udev_device *udev_device_ref(struct udev_device *udev_device)
647 if (udev_device == NULL)
649 udev_device->refcount++;
655 * @udev_device: udev device
657 * Drop a reference of a udev device. If the refcount reaches zero,
658 * the resources of the device will be released.
661 void udev_device_unref(struct udev_device *udev_device)
663 if (udev_device == NULL)
665 udev_device->refcount--;
666 if (udev_device->refcount > 0)
668 if (udev_device->parent_device != NULL)
669 udev_device_unref(udev_device->parent_device);
670 free(udev_device->syspath);
671 free(udev_device->sysname);
672 free(udev_device->devnode);
673 free(udev_device->subsystem);
674 free(udev_device->devtype);
675 udev_list_cleanup_entries(udev_device->udev, &udev_device->devlinks_list);
676 udev_list_cleanup_entries(udev_device->udev, &udev_device->properties_list);
677 free(udev_device->action);
678 free(udev_device->driver);
679 free(udev_device->devpath_old);
680 free(udev_device->knodename);
681 udev_list_cleanup_entries(udev_device->udev, &udev_device->sysattr_list);
682 free(udev_device->envp);
683 free(udev_device->monitor_buf);
684 dbg(udev_device->udev, "udev_device: %p released\n", udev_device);
689 * udev_device_get_devpath:
690 * @udev_device: udev device
692 * Retrieve the kernel devpath value of the udev device. The path
693 * does not contain the sys mount point, and starts with a '/'.
695 * Returns: the devpath of the udev device
697 const char *udev_device_get_devpath(struct udev_device *udev_device)
699 if (udev_device == NULL)
701 return udev_device->devpath;
705 * udev_device_get_syspath:
706 * @udev_device: udev device
708 * Retrieve the sys path of the udev device. The path is an
709 * absolute path and starts with the sys mount point.
711 * Returns: the sys path of the udev device
713 const char *udev_device_get_syspath(struct udev_device *udev_device)
715 if (udev_device == NULL)
717 return udev_device->syspath;
721 * udev_device_get_sysname:
722 * @udev_device: udev device
724 * Returns: the sys name of the device device
726 const char *udev_device_get_sysname(struct udev_device *udev_device)
728 if (udev_device == NULL)
730 return udev_device->sysname;
734 * udev_device_get_sysnum:
735 * @udev_device: udev device
737 * Returns: the trailing number of of the device name
739 const char *udev_device_get_sysnum(struct udev_device *udev_device)
741 if (udev_device == NULL)
743 return udev_device->sysnum;
747 * udev_device_get_devnode:
748 * @udev_device: udev device
750 * Retrieve the device node file name belonging to the udev device.
751 * The path is an absolute path, and starts with the device directory.
753 * Returns: the device node file name of the udev device, or #NULL if no device node exists
755 const char *udev_device_get_devnode(struct udev_device *udev_device)
757 if (udev_device == NULL)
759 if (!udev_device->info_loaded)
760 device_load_info(udev_device);
761 return udev_device->devnode;
765 * udev_device_get_subsystem:
766 * @udev_device: udev device
768 * Retrieve the subsystem string of the udev device. The string does not
771 * Returns: the subsystem name of the udev device, or #NULL if it can not be determined
773 const char *udev_device_get_subsystem(struct udev_device *udev_device)
775 char subsystem[UTIL_NAME_SIZE];
777 if (udev_device == NULL)
779 if (!udev_device->subsystem_set) {
780 udev_device->subsystem_set = 1;
781 /* read "subsytem" link */
782 if (util_get_sys_subsystem(udev_device->udev, udev_device->syspath, subsystem, sizeof(subsystem)) > 0) {
783 udev_device_set_subsystem(udev_device, subsystem);
784 return udev_device->subsystem;
787 if (strncmp(udev_device->devpath, "/module/", 8) == 0) {
788 udev_device_set_subsystem(udev_device, "module");
789 return udev_device->subsystem;
791 if (strstr(udev_device->devpath, "/drivers/") != NULL) {
792 udev_device_set_subsystem(udev_device, "drivers");
793 return udev_device->subsystem;
795 if (strncmp(udev_device->devpath, "/subsystem/", 11) == 0 ||
796 strncmp(udev_device->devpath, "/class/", 7) == 0 ||
797 strncmp(udev_device->devpath, "/bus/", 5) == 0) {
798 udev_device_set_subsystem(udev_device, "subsystem");
799 return udev_device->subsystem;
802 return udev_device->subsystem;
806 * udev_device_get_devtype:
807 * @udev_device: udev device
809 * Retrieve the devtype string of the udev device.
811 * Returns: the devtype name of the udev device, or #NULL if it can not be determined
813 const char *udev_device_get_devtype(struct udev_device *udev_device)
815 if (udev_device == NULL)
817 if (!udev_device->devtype_set) {
818 udev_device->devtype_set = 1;
819 if (!udev_device->info_loaded)
820 udev_device_read_uevent_file(udev_device);
822 return udev_device->devtype;
826 * udev_device_get_devlinks_list_entry:
827 * @udev_device: udev device
829 * Retrieve the list of device links pointing to the device file of
830 * the udev device. The next list entry can be retrieved with
831 * udev_list_entry_next(), which returns #NULL if no more entries exist.
832 * The devlink path can be retrieved from the list entry by
833 * udev_list_entry_get_name(). The path is an absolute path, and starts with
834 * the device directory.
836 * Returns: the first entry of the device node link list
838 struct udev_list_entry *udev_device_get_devlinks_list_entry(struct udev_device *udev_device)
840 if (udev_device == NULL)
842 if (!udev_device->info_loaded)
843 device_load_info(udev_device);
844 return udev_list_get_entry(&udev_device->devlinks_list);
847 void udev_device_cleanup_devlinks_list(struct udev_device *udev_device)
849 udev_device->devlinks_uptodate = 0;
850 udev_list_cleanup_entries(udev_device->udev, &udev_device->devlinks_list);
854 * udev_device_get_properties_list_entry:
855 * @udev_device: udev device
857 * Retrieve the list of key/value device properties of the udev
858 * device. The next list entry can be retrieved with udev_list_entry_next(),
859 * which returns #NULL if no more entries exist. The property name
860 * can be retrieved from the list entry by udev_list_get_name(),
861 * the property value by udev_list_get_value().
863 * Returns: the first entry of the property list
865 struct udev_list_entry *udev_device_get_properties_list_entry(struct udev_device *udev_device)
867 if (udev_device == NULL)
869 if (!udev_device->info_loaded)
870 device_load_info(udev_device);
871 if (!udev_device->devlinks_uptodate) {
872 char symlinks[UTIL_PATH_SIZE];
873 struct udev_list_entry *list_entry;
875 udev_device->devlinks_uptodate = 1;
876 list_entry = udev_device_get_devlinks_list_entry(udev_device);
877 if (list_entry != NULL) {
882 l = util_strpcpyl(&s, sizeof(symlinks), udev_list_entry_get_name(list_entry), NULL);
883 udev_list_entry_foreach(list_entry, udev_list_entry_get_next(list_entry))
884 l = util_strpcpyl(&s, l, " ", udev_list_entry_get_name(list_entry), NULL);
885 udev_device_add_property(udev_device, "DEVLINKS", symlinks);
888 return udev_list_get_entry(&udev_device->properties_list);
892 * udev_device_get_driver:
893 * @udev_device: udev device
895 * Returns: the driver string, or #NULL if there is no driver attached.
897 const char *udev_device_get_driver(struct udev_device *udev_device)
899 char driver[UTIL_NAME_SIZE];
901 if (udev_device == NULL)
903 if (!udev_device->driver_set) {
904 udev_device->driver_set = 1;
905 if (util_get_sys_driver(udev_device->udev, udev_device->syspath, driver, sizeof(driver)) > 0)
906 udev_device->driver = strdup(driver);
908 return udev_device->driver;
912 * udev_device_get_devnum:
913 * @udev_device: udev device
915 * Returns: the device major/minor number.
917 dev_t udev_device_get_devnum(struct udev_device *udev_device)
919 if (udev_device == NULL)
920 return makedev(0, 0);
921 if (!udev_device->info_loaded)
922 device_load_info(udev_device);
923 return udev_device->devnum;
927 * udev_device_get_action:
928 * @udev_device: udev device
930 * This is only valid if the device was received through a monitor. Devices read from
931 * sys do not have an action string. Usual actions are: add, remove, change, online,
934 * Returns: the kernel action value, or #NULL if there is no action value available.
936 const char *udev_device_get_action(struct udev_device *udev_device)
938 if (udev_device == NULL)
940 return udev_device->action;
944 * udev_device_get_devnum:
945 * @udev_device: udev device
947 * This is only valid if the device was received through a monitor. Devices read from
948 * sys do not have a sequence number.
950 * Returns: the kernel event sequence number, or 0 if there is no sequence number available.
952 unsigned long long int udev_device_get_seqnum(struct udev_device *udev_device)
954 if (udev_device == NULL)
956 return udev_device->seqnum;
960 * udev_device_get_sysattr_value:
961 * @udev_device: udev device
962 * @sysattr: attribute name
964 * The retrieved value is cached in the device. Repeated calls will return the same
965 * value and not open the attribute again.
967 * Returns: the content of a sys attribute file, or #NULL if there is no sys attribute value.
969 const char *udev_device_get_sysattr_value(struct udev_device *udev_device, const char *sysattr)
971 struct udev_list_entry *list_entry;
972 char path[UTIL_PATH_SIZE];
977 const char *val = NULL;
979 if (udev_device == NULL)
984 /* look for possibly already cached result */
985 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_device->sysattr_list)) {
986 if (strcmp(udev_list_entry_get_name(list_entry), sysattr) == 0) {
987 dbg(udev_device->udev, "got '%s' (%s) from cache\n",
988 sysattr, udev_list_entry_get_value(list_entry));
989 return udev_list_entry_get_value(list_entry);
993 util_strscpyl(path, sizeof(path), udev_device_get_syspath(udev_device), "/", sysattr, NULL);
994 if (lstat(path, &statbuf) != 0) {
995 dbg(udev_device->udev, "no attribute '%s', keep negative entry\n", path);
996 udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, NULL, 0, 0);
1000 if (S_ISLNK(statbuf.st_mode)) {
1001 char target[UTIL_NAME_SIZE];
1005 /* some core links return the last element of the target path */
1006 if (strcmp(sysattr, "driver") != 0 &&
1007 strcmp(sysattr, "subsystem") != 0 &&
1008 strcmp(sysattr, "module") != 0)
1011 len = readlink(path, target, sizeof(target));
1014 pos = strrchr(target, '/');
1017 dbg(udev_device->udev, "cache '%s' with link value '%s'\n", sysattr, pos);
1018 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, pos, 0, 0);
1019 val = udev_list_entry_get_value(list_entry);
1025 /* skip directories */
1026 if (S_ISDIR(statbuf.st_mode))
1029 /* skip non-readable files */
1030 if ((statbuf.st_mode & S_IRUSR) == 0)
1033 /* read attribute value */
1034 fd = open(path, O_RDONLY);
1036 dbg(udev_device->udev, "attribute '%s' can not be opened\n", path);
1039 size = read(fd, value, sizeof(value));
1043 if (size == sizeof(value))
1046 /* got a valid value, store it in cache and return it */
1048 util_remove_trailing_chars(value, '\n');
1049 dbg(udev_device->udev, "'%s' has attribute value '%s'\n", path, value);
1050 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, value, 0, 0);
1051 val = udev_list_entry_get_value(list_entry);
1056 int udev_device_set_syspath(struct udev_device *udev_device, const char *syspath)
1061 free(udev_device->syspath);
1062 udev_device->syspath = strdup(syspath);
1063 if (udev_device->syspath == NULL)
1065 udev_device->devpath = &udev_device->syspath[strlen(udev_get_sys_path(udev_device->udev))];
1066 udev_device_add_property(udev_device, "DEVPATH", udev_device->devpath);
1068 pos = strrchr(udev_device->syspath, '/');
1071 udev_device->sysname = strdup(&pos[1]);
1072 if (udev_device->sysname == NULL)
1075 /* some devices have '!' in their name, change that to '/' */
1077 while (udev_device->sysname[len] != '\0') {
1078 if (udev_device->sysname[len] == '!')
1079 udev_device->sysname[len] = '/';
1083 /* trailing number */
1084 while (len > 0 && isdigit(udev_device->sysname[--len]))
1085 udev_device->sysnum = &udev_device->sysname[len];
1087 /* sysname is completely numeric */
1089 udev_device->sysnum = NULL;
1094 int udev_device_set_subsystem(struct udev_device *udev_device, const char *subsystem)
1096 free(udev_device->subsystem);
1097 udev_device->subsystem = strdup(subsystem);
1098 if (udev_device->subsystem == NULL)
1100 udev_device->subsystem_set = 1;
1101 udev_device_add_property(udev_device, "SUBSYSTEM", udev_device->subsystem);
1105 int udev_device_set_devtype(struct udev_device *udev_device, const char *devtype)
1107 free(udev_device->devtype);
1108 udev_device->devtype = strdup(devtype);
1109 if (udev_device->devtype == NULL)
1111 udev_device->devtype_set = 1;
1112 udev_device_add_property(udev_device, "DEVTYPE", udev_device->devtype);
1116 int udev_device_set_devnode(struct udev_device *udev_device, const char *devnode)
1118 free(udev_device->devnode);
1119 udev_device->devnode = strdup(devnode);
1120 if (devnode == NULL)
1122 if (udev_device->devnode == NULL)
1124 udev_device_add_property(udev_device, "DEVNAME", udev_device->devnode);
1128 int udev_device_add_devlink(struct udev_device *udev_device, const char *devlink)
1130 udev_device->devlinks_uptodate = 0;
1131 if (udev_list_entry_add(udev_device->udev, &udev_device->devlinks_list, devlink, NULL, 1, 0) == NULL)
1136 struct udev_list_entry *udev_device_add_property(struct udev_device *udev_device, const char *key, const char *value)
1138 udev_device->envp_uptodate = 0;
1139 if (value == NULL) {
1140 struct udev_list_entry *list_entry;
1142 list_entry = udev_device_get_properties_list_entry(udev_device);
1143 list_entry = udev_list_entry_get_by_name(list_entry, key);
1144 if (list_entry != NULL)
1145 udev_list_entry_delete(list_entry);
1148 return udev_list_entry_add(udev_device->udev, &udev_device->properties_list, key, value, 1, 0);
1151 struct udev_list_entry *udev_device_add_property_from_string(struct udev_device *udev_device, const char *property)
1153 char name[UTIL_PATH_SIZE];
1156 util_strscpy(name, sizeof(name), property);
1157 val = strchr(name, '=');
1164 return udev_device_add_property(udev_device, name, val);
1168 * udev_device_get_property_value:
1169 * @udev_device: udev device
1170 * @key: property name
1172 * Returns: the value of a device property, or #NULL if there is no such property.
1174 const char *udev_device_get_property_value(struct udev_device *udev_device, const char *key)
1176 struct udev_list_entry *list_entry;
1178 if (udev_device == NULL)
1183 list_entry = udev_device_get_properties_list_entry(udev_device);
1184 list_entry = udev_list_entry_get_by_name(list_entry, key);
1185 return udev_list_entry_get_value(list_entry);
1188 #define ENVP_SIZE 128
1189 #define MONITOR_BUF_SIZE 4096
1190 static int update_envp_monitor_buf(struct udev_device *udev_device)
1192 struct udev_list_entry *list_entry;
1197 /* monitor buffer of property strings */
1198 free(udev_device->monitor_buf);
1199 udev_device->monitor_buf_len = 0;
1200 udev_device->monitor_buf = malloc(MONITOR_BUF_SIZE);
1201 if (udev_device->monitor_buf == NULL)
1204 /* envp array, strings will point into monitor buffer */
1205 if (udev_device->envp == NULL)
1206 udev_device->envp = malloc(sizeof(char *) * ENVP_SIZE);
1207 if (udev_device->envp == NULL)
1211 s = udev_device->monitor_buf;
1212 l = MONITOR_BUF_SIZE;
1213 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device)) {
1216 key = udev_list_entry_get_name(list_entry);
1217 /* skip private variables */
1221 /* add string to envp array */
1222 udev_device->envp[i++] = s;
1223 if (i+1 >= ENVP_SIZE)
1226 /* add property string to monitor buffer */
1227 l = util_strpcpyl(&s, l, key, "=", udev_list_entry_get_value(list_entry), NULL);
1232 udev_device->envp[i] = NULL;
1233 udev_device->monitor_buf_len = s - udev_device->monitor_buf;
1234 udev_device->envp_uptodate = 1;
1235 dbg(udev_device->udev, "filled envp/monitor buffer, %u properties, %zu bytes\n",
1236 i, udev_device->monitor_buf_len);
1240 char **udev_device_get_properties_envp(struct udev_device *udev_device)
1242 if (!udev_device->envp_uptodate)
1243 if (update_envp_monitor_buf(udev_device) != 0)
1245 return udev_device->envp;
1248 ssize_t udev_device_get_properties_monitor_buf(struct udev_device *udev_device, const char **buf)
1250 if (!udev_device->envp_uptodate)
1251 if (update_envp_monitor_buf(udev_device) != 0)
1253 *buf = udev_device->monitor_buf;
1254 return udev_device->monitor_buf_len;
1257 int udev_device_set_action(struct udev_device *udev_device, const char *action)
1259 free(udev_device->action);
1260 udev_device->action = strdup(action);
1261 if (udev_device->action == NULL)
1263 udev_device_add_property(udev_device, "ACTION", udev_device->action);
1267 int udev_device_set_driver(struct udev_device *udev_device, const char *driver)
1269 free(udev_device->driver);
1270 udev_device->driver = strdup(driver);
1271 if (udev_device->driver == NULL)
1273 udev_device->driver_set = 1;
1274 udev_device_add_property(udev_device, "DRIVER", udev_device->driver);
1278 const char *udev_device_get_devpath_old(struct udev_device *udev_device)
1280 return udev_device->devpath_old;
1283 int udev_device_set_devpath_old(struct udev_device *udev_device, const char *devpath_old)
1285 free(udev_device->devpath_old);
1286 udev_device->devpath_old = strdup(devpath_old);
1287 if (udev_device->devpath_old == NULL)
1289 udev_device_add_property(udev_device, "DEVPATH_OLD", udev_device->devpath_old);
1293 const char *udev_device_get_knodename(struct udev_device *udev_device)
1295 return udev_device->knodename;
1298 int udev_device_set_knodename(struct udev_device *udev_device, const char *knodename)
1300 free(udev_device->knodename);
1301 udev_device->knodename = strdup(knodename);
1302 if (udev_device->knodename == NULL)
1304 udev_device_add_property(udev_device, "DEVNAME", udev_device->knodename);
1308 int udev_device_get_timeout(struct udev_device *udev_device)
1310 return udev_device->timeout;
1313 int udev_device_set_timeout(struct udev_device *udev_device, int timeout)
1315 udev_device->timeout = timeout;
1318 int udev_device_get_event_timeout(struct udev_device *udev_device)
1320 if (!udev_device->info_loaded)
1321 device_load_info(udev_device);
1322 return udev_device->event_timeout;
1325 int udev_device_set_event_timeout(struct udev_device *udev_device, int event_timeout)
1327 udev_device->event_timeout = event_timeout;
1331 int udev_device_set_seqnum(struct udev_device *udev_device, unsigned long long int seqnum)
1335 udev_device->seqnum = seqnum;
1336 snprintf(num, sizeof(num), "%llu", seqnum);
1337 udev_device_add_property(udev_device, "SEQNUM", num);
1341 int udev_device_set_devnum(struct udev_device *udev_device, dev_t devnum)
1345 udev_device->devnum = devnum;
1347 snprintf(num, sizeof(num), "%u", major(devnum));
1348 udev_device_add_property(udev_device, "MAJOR", num);
1349 snprintf(num, sizeof(num), "%u", minor(devnum));
1350 udev_device_add_property(udev_device, "MINOR", num);
1354 int udev_device_get_num_fake_partitions(struct udev_device *udev_device)
1356 if (!udev_device->info_loaded)
1357 device_load_info(udev_device);
1358 return udev_device->num_fake_partitions;
1361 int udev_device_set_num_fake_partitions(struct udev_device *udev_device, int num)
1363 udev_device->num_fake_partitions = num;
1367 int udev_device_get_devlink_priority(struct udev_device *udev_device)
1369 if (!udev_device->info_loaded)
1370 device_load_info(udev_device);
1371 return udev_device->devlink_priority;
1374 int udev_device_set_devlink_priority(struct udev_device *udev_device, int prio)
1376 udev_device->devlink_priority = prio;
1380 int udev_device_get_ignore_remove(struct udev_device *udev_device)
1382 if (!udev_device->info_loaded)
1383 device_load_info(udev_device);
1384 return udev_device->ignore_remove;
1387 int udev_device_set_ignore_remove(struct udev_device *udev_device, int ignore)
1389 udev_device->ignore_remove = ignore;
1393 int udev_device_get_watch_handle(struct udev_device *udev_device)
1395 if (!udev_device->info_loaded)
1396 device_load_info(udev_device);
1397 return udev_device->watch_handle;
1400 int udev_device_set_watch_handle(struct udev_device *udev_device, int handle)
1402 udev_device->watch_handle = handle;