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 subsystem.
39 * Opaque object representing one kernel sys device.
43 struct udev_device *parent_device;
58 size_t monitor_buf_len;
59 struct udev_list_node devlinks_list;
60 struct udev_list_node properties_list;
61 struct udev_list_node sysattr_list;
62 unsigned long long int seqnum;
65 int num_fake_partitions;
70 unsigned int parent_set:1;
71 unsigned int subsystem_set:1;
72 unsigned int devtype_set:1;
73 unsigned int devlinks_uptodate:1;
74 unsigned int envp_uptodate:1;
75 unsigned int driver_set:1;
76 unsigned int info_loaded:1;
77 unsigned int ignore_remove:1;
80 int udev_device_read_db(struct udev_device *udev_device)
83 char filename[UTIL_PATH_SIZE];
84 char line[UTIL_LINE_SIZE];
87 util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev_device->udev), "/.udev/db/",
88 udev_device_get_subsystem(udev_device), ":", udev_device_get_sysname(udev_device), NULL);
90 if (lstat(filename, &stats) != 0) {
91 dbg(udev_device->udev, "no db file to read %s: %m\n", filename);
94 if ((stats.st_mode & S_IFMT) == S_IFLNK) {
95 char target[UTIL_PATH_SIZE];
96 char devnode[UTIL_PATH_SIZE];
100 target_len = readlink(filename, target, sizeof(target));
102 target[target_len] = '\0';
104 dbg(udev_device->udev, "error reading db link %s: %m\n", filename);
108 next = strchr(target, ' ');
113 util_strscpyl(devnode, sizeof(devnode), udev_get_dev_path(udev_device->udev), "/", target, NULL);
114 udev_device_set_devnode(udev_device, devnode);
115 while (next != NULL) {
116 char devlink[UTIL_PATH_SIZE];
120 next = strchr(next, ' ');
125 util_strscpyl(devlink, sizeof(devlink), udev_get_dev_path(udev_device->udev), "/", lnk, NULL);
126 udev_device_add_devlink(udev_device, devlink, 0);
128 info(udev_device->udev, "device %p filled with db symlink data '%s'\n", udev_device, udev_device->devnode);
132 f = fopen(filename, "r");
134 dbg(udev_device->udev, "error reading db file %s: %m\n", filename);
137 while (fgets(line, sizeof(line), f)) {
148 util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev_device->udev), "/", val, NULL);
149 udev_device_set_devnode(udev_device, filename);
152 util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev_device->udev), "/", val, NULL);
153 udev_device_add_devlink(udev_device, filename, 0);
156 udev_device_set_devlink_priority(udev_device, atoi(val));
159 udev_device_set_event_timeout(udev_device, atoi(val));
162 udev_device_set_num_fake_partitions(udev_device, atoi(val));
165 udev_device_set_ignore_remove(udev_device, atoi(val));
168 udev_device_add_property_from_string(udev_device, val);
171 udev_device_set_watch_handle(udev_device, atoi(val));
177 info(udev_device->udev, "device %p filled with db file data\n", udev_device);
181 int udev_device_read_uevent_file(struct udev_device *udev_device)
183 char filename[UTIL_PATH_SIZE];
185 char line[UTIL_LINE_SIZE];
189 util_strscpyl(filename, sizeof(filename), udev_device->syspath, "/uevent", NULL);
190 f = fopen(filename, "r");
194 while (fgets(line, sizeof(line), f)) {
197 pos = strchr(line, '\n');
202 if (strncmp(line, "DEVTYPE=", 8) == 0)
203 udev_device_set_devtype(udev_device, &line[8]);
204 else if (strncmp(line, "MAJOR=", 6) == 0)
205 maj = strtoull(&line[6], NULL, 10);
206 else if (strncmp(line, "MINOR=", 6) == 0)
207 min = strtoull(&line[6], NULL, 10);
208 else if (strncmp(line, "DEVNAME=", 8) == 0)
209 udev_device_set_knodename(udev_device, &line[8]);
211 udev_device_add_property_from_string(udev_device, line);
214 udev_device->devnum = makedev(maj, min);
220 static void device_load_info(struct udev_device *device)
222 device->info_loaded = 1;
223 udev_device_read_uevent_file(device);
224 udev_device_read_db(device);
227 void udev_device_set_info_loaded(struct udev_device *device)
229 device->info_loaded = 1;
232 struct udev_device *udev_device_new(struct udev *udev)
234 struct udev_device *udev_device;
235 struct udev_list_entry *list_entry;
240 udev_device = calloc(1, sizeof(struct udev_device));
241 if (udev_device == NULL)
243 udev_device->refcount = 1;
244 udev_device->udev = udev;
245 udev_list_init(&udev_device->devlinks_list);
246 udev_list_init(&udev_device->properties_list);
247 udev_list_init(&udev_device->sysattr_list);
248 udev_device->event_timeout = -1;
249 udev_device->watch_handle = -1;
250 /* copy global properties */
251 udev_list_entry_foreach(list_entry, udev_get_properties_list_entry(udev))
252 udev_device_add_property(udev_device,
253 udev_list_entry_get_name(list_entry),
254 udev_list_entry_get_value(list_entry));
255 dbg(udev_device->udev, "udev_device: %p created\n", udev_device);
260 * udev_device_new_from_syspath:
261 * @udev: udev library context
262 * @syspath: sys device path including sys directory
264 * Create new udev device, and fill in information from the sys
265 * device and the udev database entry. The syspath is the absolute
266 * path to the device, including the sys mount point.
268 * The initial refcount is 1, and needs to be decremented to
269 * release the resources of the udev device.
271 * Returns: a new udev device, or #NULL, if it does not exist
273 struct udev_device *udev_device_new_from_syspath(struct udev *udev, const char *syspath)
277 char path[UTIL_PATH_SIZE];
280 struct udev_device *udev_device;
287 /* path starts in sys */
288 len = strlen(udev_get_sys_path(udev));
289 if (strncmp(syspath, udev_get_sys_path(udev), len) != 0) {
290 info(udev, "not in sys :%s\n", syspath);
294 /* path is not a root directory */
295 subdir = &syspath[len+1];
296 pos = strrchr(subdir, '/');
297 if (pos == NULL || pos[1] == '\0' || pos < &subdir[2]) {
298 dbg(udev, "not a subdir :%s\n", syspath);
302 /* resolve possible symlink to real path */
303 util_strscpy(path, sizeof(path), syspath);
304 util_resolve_sys_link(udev, path, sizeof(path));
306 /* try to resolve the silly block layout if needed */
307 if (strncmp(&path[len], "/block/", 7) == 0) {
308 char block[UTIL_PATH_SIZE];
309 char part[UTIL_PATH_SIZE];
311 util_strscpy(block, sizeof(block), path);
312 pos = strrchr(block, '/');
315 util_strscpy(part, sizeof(part), pos);
317 if (util_resolve_sys_link(udev, block, sizeof(block)) == 0)
318 util_strscpyl(path, sizeof(path), block, part, NULL);
321 /* path exists in sys */
322 if (strncmp(&syspath[len], "/devices/", 9) == 0 ||
323 strncmp(&syspath[len], "/class/", 7) == 0 ||
324 strncmp(&syspath[len], "/block/", 7) == 0) {
325 char file[UTIL_PATH_SIZE];
327 /* all "devices" require a "uevent" file */
328 util_strscpyl(file, sizeof(file), path, "/uevent", NULL);
329 if (stat(file, &statbuf) != 0) {
330 dbg(udev, "not a device: %s\n", syspath);
334 /* everything else just needs to be a directory */
335 if (stat(path, &statbuf) != 0 || !S_ISDIR(statbuf.st_mode)) {
336 dbg(udev, "directory not found: %s\n", syspath);
341 udev_device = udev_device_new(udev);
342 if (udev_device == NULL)
345 udev_device_set_syspath(udev_device, path);
346 info(udev, "device %p has devpath '%s'\n", udev_device, udev_device_get_devpath(udev_device));
352 * udev_device_new_from_devnum:
353 * @udev: udev library context
354 * @type: char or block device
355 * @devnum: device major/minor number
357 * Create new udev device, and fill in information from the sys
358 * device and the udev database entry. The device is looked up
359 * by its major/minor number. Character and block device numbers
360 * are not unique across the two types, they do not share the same
363 * The initial refcount is 1, and needs to be decremented to
364 * release the resources of the udev device.
366 * Returns: a new udev device, or #NULL, if it does not exist
368 struct udev_device *udev_device_new_from_devnum(struct udev *udev, char type, dev_t devnum)
370 char path[UTIL_PATH_SIZE];
371 const char *type_str;
372 struct udev_enumerate *udev_enumerate;
373 struct udev_list_entry *list_entry;
374 struct udev_device *device = NULL;
378 else if (type == 'c')
383 /* /sys/dev/{block,char}/<maj>:<min> link */
384 snprintf(path, sizeof(path), "%s/dev/%s/%u:%u", udev_get_sys_path(udev),
385 type_str, major(devnum), minor(devnum));
386 if (util_resolve_sys_link(udev, path, sizeof(path)) == 0)
387 return udev_device_new_from_syspath(udev, path);
389 udev_enumerate = udev_enumerate_new(udev);
390 if (udev_enumerate == NULL)
393 /* fallback to search sys devices for the major/minor */
395 udev_enumerate_add_match_subsystem(udev_enumerate, "block");
396 else if (type == 'c')
397 udev_enumerate_add_nomatch_subsystem(udev_enumerate, "block");
398 udev_enumerate_scan_devices(udev_enumerate);
399 udev_list_entry_foreach(list_entry, udev_enumerate_get_list_entry(udev_enumerate)) {
400 struct udev_device *device_loop;
402 device_loop = udev_device_new_from_syspath(udev, udev_list_entry_get_name(list_entry));
403 if (device_loop != NULL) {
404 if (udev_device_get_devnum(device_loop) == devnum) {
405 if (type == 'b' && strcmp(udev_device_get_subsystem(device_loop), "block") != 0)
407 if (type == 'c' && strcmp(udev_device_get_subsystem(device_loop), "block") == 0)
409 device = device_loop;
412 udev_device_unref(device_loop);
415 udev_enumerate_unref(udev_enumerate);
420 * udev_device_new_from_subsystem_sysname:
421 * @udev: udev library context
422 * @subsystem: the subsystem of the device
423 * @sysname: the name of the device
425 * Create new udev device, and fill in information from the sys
426 * device and the udev database entry. The device is looked up
427 * by the subsystem and name string of the device, like "mem",
428 * "zero", or "block", "sda".
430 * The initial refcount is 1, and needs to be decremented to
431 * release the resources of the udev device.
433 * Returns: a new udev device, or #NULL, if it does not exist
435 struct udev_device *udev_device_new_from_subsystem_sysname(struct udev *udev, const char *subsystem, const char *sysname)
437 char path_full[UTIL_PATH_SIZE];
443 l = util_strpcpyl(&path, sizeof(path_full), udev_get_sys_path(udev), NULL);
445 if (strcmp(subsystem, "subsystem") == 0) {
446 util_strscpyl(path, l, "/subsystem/", sysname, NULL);
447 if (stat(path_full, &statbuf) == 0)
450 util_strscpyl(path, l, "/bus/", sysname, NULL);
451 if (stat(path_full, &statbuf) == 0)
454 util_strscpyl(path, l, "/class/", sysname, NULL);
455 if (stat(path_full, &statbuf) == 0)
460 if (strcmp(subsystem, "module") == 0) {
461 util_strscpyl(path, l, "/module/", sysname, NULL);
462 if (stat(path_full, &statbuf) == 0)
467 if (strcmp(subsystem, "drivers") == 0) {
468 char subsys[UTIL_NAME_SIZE];
471 util_strscpy(subsys, sizeof(subsys), sysname);
472 driver = strchr(subsys, ':');
473 if (driver != NULL) {
477 util_strscpyl(path, l, "/subsystem/", subsys, "/drivers/", driver, NULL);
478 if (stat(path_full, &statbuf) == 0)
481 util_strscpyl(path, l, "/bus/", subsys, "/drivers/", driver, NULL);
482 if (stat(path_full, &statbuf) == 0)
488 util_strscpyl(path, l, "/subsystem/", subsystem, "/devices/", sysname, NULL);
489 if (stat(path_full, &statbuf) == 0)
492 util_strscpyl(path, l, "/bus/", subsystem, "/devices/", sysname, NULL);
493 if (stat(path_full, &statbuf) == 0)
496 util_strscpyl(path, l, "/class/", subsystem, "/", sysname, NULL);
497 if (stat(path_full, &statbuf) == 0)
502 return udev_device_new_from_syspath(udev, path_full);
505 static struct udev_device *device_new_from_parent(struct udev_device *udev_device)
507 struct udev_device *udev_device_parent = NULL;
508 char path[UTIL_PATH_SIZE];
511 /* follow "device" link in deprecated sys layout */
512 if (strncmp(udev_device->devpath, "/class/", 7) == 0 ||
513 strncmp(udev_device->devpath, "/block/", 7) == 0) {
514 util_strscpyl(path, sizeof(path), udev_device->syspath, "/device", NULL);
515 if (util_resolve_sys_link(udev_device->udev, path, sizeof(path)) == 0) {
516 udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
517 if (udev_device_parent != NULL)
518 return udev_device_parent;
522 util_strscpy(path, sizeof(path), udev_device->syspath);
523 subdir = &path[strlen(udev_get_sys_path(udev_device->udev))+1];
527 pos = strrchr(subdir, '/');
528 if (pos == NULL || pos < &subdir[2])
531 udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
532 if (udev_device_parent != NULL)
533 return udev_device_parent;
539 * udev_device_get_parent:
540 * @udev_device: the device to start searching from
542 * Find the next parent device, and fill in information from the sys
543 * device and the udev database entry.
545 * The returned the device is not referenced. It is attached to the
546 * child device, and will be cleaned up when the child device
549 * It is not necessarily just the upper level directory, empty or not
550 * recognized sys directories are ignored.
552 * It can be called as many times as needed, without caring about
555 * Returns: a new udev device, or #NULL, if it no parent exist.
557 struct udev_device *udev_device_get_parent(struct udev_device *udev_device)
559 if (udev_device == NULL)
561 if (!udev_device->parent_set) {
562 udev_device->parent_set = 1;
563 udev_device->parent_device = device_new_from_parent(udev_device);
565 if (udev_device->parent_device != NULL)
566 dbg(udev_device->udev, "returning existing parent %p\n", udev_device->parent_device);
567 return udev_device->parent_device;
571 * udev_device_get_parent_with_subsystem_devtype:
572 * @udev_device: udev device to start searching from
573 * @subsystem: the subsystem of the device
574 * @devtype: the type (DEVTYPE) of the device
576 * Find the next parent device, with a matching subsystem and devtype
577 * value, and fill in information from the sys device and the udev
580 * The returned the device is not referenced. It is attached to the
581 * child device, and will be cleaned up when the child device
584 * It can be called as many times as needed, without caring about
587 * Returns: a new udev device, or #NULL if no matching parent exists.
589 struct udev_device *udev_device_get_parent_with_subsystem_devtype(struct udev_device *udev_device, const char *subsystem, const char *devtype)
591 struct udev_device *parent;
593 if (subsystem == NULL)
596 parent = udev_device_get_parent(udev_device);
597 while (parent != NULL) {
598 const char *parent_subsystem;
599 const char *parent_devtype;
601 parent_subsystem = udev_device_get_subsystem(parent);
602 if (parent_subsystem != NULL && strcmp(parent_subsystem, subsystem) == 0) {
605 parent_devtype = udev_device_get_devtype(parent);
606 if (parent_devtype != NULL && strcmp(parent_devtype, devtype) == 0)
609 parent = udev_device_get_parent(parent);
615 * udev_device_get_udev:
616 * @udev_device: udev device
618 * Retrieve the udev library context the device was created with.
620 * Returns: the udev library context
622 struct udev *udev_device_get_udev(struct udev_device *udev_device)
624 if (udev_device == NULL)
626 return udev_device->udev;
631 * @udev_device: udev device
633 * Take a reference of a udev device.
635 * Returns: the passed udev device
637 struct udev_device *udev_device_ref(struct udev_device *udev_device)
639 if (udev_device == NULL)
641 udev_device->refcount++;
647 * @udev_device: udev device
649 * Drop a reference of a udev device. If the refcount reaches zero,
650 * the resources of the device will be released.
653 void udev_device_unref(struct udev_device *udev_device)
655 if (udev_device == NULL)
657 udev_device->refcount--;
658 if (udev_device->refcount > 0)
660 if (udev_device->parent_device != NULL)
661 udev_device_unref(udev_device->parent_device);
662 free(udev_device->syspath);
663 free(udev_device->sysname);
664 free(udev_device->devnode);
665 free(udev_device->subsystem);
666 free(udev_device->devtype);
667 udev_list_cleanup_entries(udev_device->udev, &udev_device->devlinks_list);
668 udev_list_cleanup_entries(udev_device->udev, &udev_device->properties_list);
669 free(udev_device->action);
670 free(udev_device->driver);
671 free(udev_device->devpath_old);
672 free(udev_device->sysname_old);
673 free(udev_device->knodename);
674 udev_list_cleanup_entries(udev_device->udev, &udev_device->sysattr_list);
675 free(udev_device->envp);
676 free(udev_device->monitor_buf);
677 dbg(udev_device->udev, "udev_device: %p released\n", udev_device);
682 * udev_device_get_devpath:
683 * @udev_device: udev device
685 * Retrieve the kernel devpath value of the udev device. The path
686 * does not contain the sys mount point, and starts with a '/'.
688 * Returns: the devpath of the udev device
690 const char *udev_device_get_devpath(struct udev_device *udev_device)
692 if (udev_device == NULL)
694 return udev_device->devpath;
698 * udev_device_get_syspath:
699 * @udev_device: udev device
701 * Retrieve the sys path of the udev device. The path is an
702 * absolute path and starts with the sys mount point.
704 * Returns: the sys path of the udev device
706 const char *udev_device_get_syspath(struct udev_device *udev_device)
708 if (udev_device == NULL)
710 return udev_device->syspath;
714 * udev_device_get_sysname:
715 * @udev_device: udev device
717 * Returns: the sys name of the device device
719 const char *udev_device_get_sysname(struct udev_device *udev_device)
721 if (udev_device == NULL)
723 return udev_device->sysname;
727 * udev_device_get_sysnum:
728 * @udev_device: udev device
730 * Returns: the trailing number of of the device name
732 const char *udev_device_get_sysnum(struct udev_device *udev_device)
734 if (udev_device == NULL)
736 return udev_device->sysnum;
740 * udev_device_get_devnode:
741 * @udev_device: udev device
743 * Retrieve the device node file name belonging to the udev device.
744 * The path is an absolute path, and starts with the device directory.
746 * Returns: the device node file name of the udev device, or #NULL if no device node exists
748 const char *udev_device_get_devnode(struct udev_device *udev_device)
750 if (udev_device == NULL)
752 if (!udev_device->info_loaded)
753 device_load_info(udev_device);
754 return udev_device->devnode;
758 * udev_device_get_subsystem:
759 * @udev_device: udev device
761 * Retrieve the subsystem string of the udev device. The string does not
764 * Returns: the subsystem name of the udev device, or #NULL if it can not be determined
766 const char *udev_device_get_subsystem(struct udev_device *udev_device)
768 char subsystem[UTIL_NAME_SIZE];
770 if (udev_device == NULL)
772 if (!udev_device->subsystem_set) {
773 udev_device->subsystem_set = 1;
774 /* read "subsystem" link */
775 if (util_get_sys_subsystem(udev_device->udev, udev_device->syspath, subsystem, sizeof(subsystem)) > 0) {
776 udev_device_set_subsystem(udev_device, subsystem);
777 return udev_device->subsystem;
780 if (strncmp(udev_device->devpath, "/module/", 8) == 0) {
781 udev_device_set_subsystem(udev_device, "module");
782 return udev_device->subsystem;
784 if (strstr(udev_device->devpath, "/drivers/") != NULL) {
785 udev_device_set_subsystem(udev_device, "drivers");
786 return udev_device->subsystem;
788 if (strncmp(udev_device->devpath, "/subsystem/", 11) == 0 ||
789 strncmp(udev_device->devpath, "/class/", 7) == 0 ||
790 strncmp(udev_device->devpath, "/bus/", 5) == 0) {
791 udev_device_set_subsystem(udev_device, "subsystem");
792 return udev_device->subsystem;
795 return udev_device->subsystem;
799 * udev_device_get_devtype:
800 * @udev_device: udev device
802 * Retrieve the devtype string of the udev device.
804 * Returns: the devtype name of the udev device, or #NULL if it can not be determined
806 const char *udev_device_get_devtype(struct udev_device *udev_device)
808 if (udev_device == NULL)
810 if (!udev_device->devtype_set) {
811 udev_device->devtype_set = 1;
812 if (!udev_device->info_loaded)
813 udev_device_read_uevent_file(udev_device);
815 return udev_device->devtype;
819 * udev_device_get_devlinks_list_entry:
820 * @udev_device: udev device
822 * Retrieve the list of device links pointing to the device file of
823 * the udev device. The next list entry can be retrieved with
824 * udev_list_entry_next(), which returns #NULL if no more entries exist.
825 * The devlink path can be retrieved from the list entry by
826 * udev_list_entry_get_name(). The path is an absolute path, and starts with
827 * the device directory.
829 * Returns: the first entry of the device node link list
831 struct udev_list_entry *udev_device_get_devlinks_list_entry(struct udev_device *udev_device)
833 if (udev_device == NULL)
835 if (!udev_device->info_loaded)
836 device_load_info(udev_device);
837 return udev_list_get_entry(&udev_device->devlinks_list);
840 void udev_device_cleanup_devlinks_list(struct udev_device *udev_device)
842 udev_device->devlinks_uptodate = 0;
843 udev_list_cleanup_entries(udev_device->udev, &udev_device->devlinks_list);
847 * udev_device_get_properties_list_entry:
848 * @udev_device: udev device
850 * Retrieve the list of key/value device properties of the udev
851 * device. The next list entry can be retrieved with udev_list_entry_next(),
852 * which returns #NULL if no more entries exist. The property name
853 * can be retrieved from the list entry by udev_list_get_name(),
854 * the property value by udev_list_get_value().
856 * Returns: the first entry of the property list
858 struct udev_list_entry *udev_device_get_properties_list_entry(struct udev_device *udev_device)
860 if (udev_device == NULL)
862 if (!udev_device->info_loaded)
863 device_load_info(udev_device);
864 if (!udev_device->devlinks_uptodate) {
865 char symlinks[UTIL_PATH_SIZE];
866 struct udev_list_entry *list_entry;
868 udev_device->devlinks_uptodate = 1;
869 list_entry = udev_device_get_devlinks_list_entry(udev_device);
870 if (list_entry != NULL) {
875 l = util_strpcpyl(&s, sizeof(symlinks), udev_list_entry_get_name(list_entry), NULL);
876 udev_list_entry_foreach(list_entry, udev_list_entry_get_next(list_entry))
877 l = util_strpcpyl(&s, l, " ", udev_list_entry_get_name(list_entry), NULL);
878 udev_device_add_property(udev_device, "DEVLINKS", symlinks);
881 return udev_list_get_entry(&udev_device->properties_list);
885 * udev_device_get_driver:
886 * @udev_device: udev device
888 * Returns: the driver string, or #NULL if there is no driver attached.
890 const char *udev_device_get_driver(struct udev_device *udev_device)
892 char driver[UTIL_NAME_SIZE];
894 if (udev_device == NULL)
896 if (!udev_device->driver_set) {
897 udev_device->driver_set = 1;
898 if (util_get_sys_driver(udev_device->udev, udev_device->syspath, driver, sizeof(driver)) > 0)
899 udev_device->driver = strdup(driver);
901 return udev_device->driver;
905 * udev_device_get_devnum:
906 * @udev_device: udev device
908 * Returns: the device major/minor number.
910 dev_t udev_device_get_devnum(struct udev_device *udev_device)
912 if (udev_device == NULL)
913 return makedev(0, 0);
914 if (!udev_device->info_loaded)
915 device_load_info(udev_device);
916 return udev_device->devnum;
920 * udev_device_get_action:
921 * @udev_device: udev device
923 * This is only valid if the device was received through a monitor. Devices read from
924 * sys do not have an action string. Usual actions are: add, remove, change, online,
927 * Returns: the kernel action value, or #NULL if there is no action value available.
929 const char *udev_device_get_action(struct udev_device *udev_device)
931 if (udev_device == NULL)
933 return udev_device->action;
937 * udev_device_get_devnum:
938 * @udev_device: udev device
940 * This is only valid if the device was received through a monitor. Devices read from
941 * sys do not have a sequence number.
943 * Returns: the kernel event sequence number, or 0 if there is no sequence number available.
945 unsigned long long int udev_device_get_seqnum(struct udev_device *udev_device)
947 if (udev_device == NULL)
949 return udev_device->seqnum;
953 * udev_device_get_sysattr_value:
954 * @udev_device: udev device
955 * @sysattr: attribute name
957 * The retrieved value is cached in the device. Repeated calls will return the same
958 * value and not open the attribute again.
960 * Returns: the content of a sys attribute file, or #NULL if there is no sys attribute value.
962 const char *udev_device_get_sysattr_value(struct udev_device *udev_device, const char *sysattr)
964 struct udev_list_entry *list_entry;
965 char path[UTIL_PATH_SIZE];
970 const char *val = NULL;
972 if (udev_device == NULL)
977 /* look for possibly already cached result */
978 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_device->sysattr_list)) {
979 if (strcmp(udev_list_entry_get_name(list_entry), sysattr) == 0) {
980 dbg(udev_device->udev, "got '%s' (%s) from cache\n",
981 sysattr, udev_list_entry_get_value(list_entry));
982 return udev_list_entry_get_value(list_entry);
986 util_strscpyl(path, sizeof(path), udev_device_get_syspath(udev_device), "/", sysattr, NULL);
987 if (lstat(path, &statbuf) != 0) {
988 dbg(udev_device->udev, "no attribute '%s', keep negative entry\n", path);
989 udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, NULL, 0, 0);
993 if (S_ISLNK(statbuf.st_mode)) {
994 char target[UTIL_NAME_SIZE];
998 /* some core links return the last element of the target path */
999 if (strcmp(sysattr, "driver") != 0 &&
1000 strcmp(sysattr, "subsystem") != 0 &&
1001 strcmp(sysattr, "module") != 0)
1004 len = readlink(path, target, sizeof(target));
1007 pos = strrchr(target, '/');
1010 dbg(udev_device->udev, "cache '%s' with link value '%s'\n", sysattr, pos);
1011 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, pos, 0, 0);
1012 val = udev_list_entry_get_value(list_entry);
1018 /* skip directories */
1019 if (S_ISDIR(statbuf.st_mode))
1022 /* skip non-readable files */
1023 if ((statbuf.st_mode & S_IRUSR) == 0)
1026 /* read attribute value */
1027 fd = open(path, O_RDONLY);
1029 dbg(udev_device->udev, "attribute '%s' can not be opened\n", path);
1032 size = read(fd, value, sizeof(value));
1036 if (size == sizeof(value))
1039 /* got a valid value, store it in cache and return it */
1041 util_remove_trailing_chars(value, '\n');
1042 dbg(udev_device->udev, "'%s' has attribute value '%s'\n", path, value);
1043 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, value, 0, 0);
1044 val = udev_list_entry_get_value(list_entry);
1049 int udev_device_set_syspath(struct udev_device *udev_device, const char *syspath)
1054 free(udev_device->syspath);
1055 udev_device->syspath = strdup(syspath);
1056 if (udev_device->syspath == NULL)
1058 udev_device->devpath = &udev_device->syspath[strlen(udev_get_sys_path(udev_device->udev))];
1059 udev_device_add_property(udev_device, "DEVPATH", udev_device->devpath);
1061 pos = strrchr(udev_device->syspath, '/');
1064 udev_device->sysname = strdup(&pos[1]);
1065 if (udev_device->sysname == NULL)
1068 /* some devices have '!' in their name, change that to '/' */
1070 while (udev_device->sysname[len] != '\0') {
1071 if (udev_device->sysname[len] == '!')
1072 udev_device->sysname[len] = '/';
1076 /* trailing number */
1077 while (len > 0 && isdigit(udev_device->sysname[--len]))
1078 udev_device->sysnum = &udev_device->sysname[len];
1080 /* sysname is completely numeric */
1082 udev_device->sysnum = NULL;
1087 int udev_device_set_subsystem(struct udev_device *udev_device, const char *subsystem)
1089 free(udev_device->subsystem);
1090 udev_device->subsystem = strdup(subsystem);
1091 if (udev_device->subsystem == NULL)
1093 udev_device->subsystem_set = 1;
1094 udev_device_add_property(udev_device, "SUBSYSTEM", udev_device->subsystem);
1098 int udev_device_set_devtype(struct udev_device *udev_device, const char *devtype)
1100 free(udev_device->devtype);
1101 udev_device->devtype = strdup(devtype);
1102 if (udev_device->devtype == NULL)
1104 udev_device->devtype_set = 1;
1105 udev_device_add_property(udev_device, "DEVTYPE", udev_device->devtype);
1109 int udev_device_set_devnode(struct udev_device *udev_device, const char *devnode)
1111 free(udev_device->devnode);
1112 udev_device->devnode = strdup(devnode);
1113 if (devnode == NULL)
1115 if (udev_device->devnode == NULL)
1117 udev_device_add_property(udev_device, "DEVNAME", udev_device->devnode);
1121 int udev_device_add_devlink(struct udev_device *udev_device, const char *devlink, int unique)
1123 struct udev_list_entry *list_entry;
1125 udev_device->devlinks_uptodate = 0;
1126 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->devlinks_list, devlink, NULL, 1, 0);
1127 if (list_entry == NULL)
1130 udev_list_entry_set_flag(list_entry, 1);
1134 struct udev_list_entry *udev_device_add_property(struct udev_device *udev_device, const char *key, const char *value)
1136 udev_device->envp_uptodate = 0;
1137 if (value == NULL) {
1138 struct udev_list_entry *list_entry;
1140 list_entry = udev_device_get_properties_list_entry(udev_device);
1141 list_entry = udev_list_entry_get_by_name(list_entry, key);
1142 if (list_entry != NULL)
1143 udev_list_entry_delete(list_entry);
1146 return udev_list_entry_add(udev_device->udev, &udev_device->properties_list, key, value, 1, 0);
1149 struct udev_list_entry *udev_device_add_property_from_string(struct udev_device *udev_device, const char *property)
1151 char name[UTIL_PATH_SIZE];
1154 util_strscpy(name, sizeof(name), property);
1155 val = strchr(name, '=');
1162 return udev_device_add_property(udev_device, name, val);
1166 * udev_device_get_property_value:
1167 * @udev_device: udev device
1168 * @key: property name
1170 * Returns: the value of a device property, or #NULL if there is no such property.
1172 const char *udev_device_get_property_value(struct udev_device *udev_device, const char *key)
1174 struct udev_list_entry *list_entry;
1176 if (udev_device == NULL)
1181 list_entry = udev_device_get_properties_list_entry(udev_device);
1182 list_entry = udev_list_entry_get_by_name(list_entry, key);
1183 return udev_list_entry_get_value(list_entry);
1186 #define ENVP_SIZE 128
1187 #define MONITOR_BUF_SIZE 4096
1188 static int update_envp_monitor_buf(struct udev_device *udev_device)
1190 struct udev_list_entry *list_entry;
1195 /* monitor buffer of property strings */
1196 free(udev_device->monitor_buf);
1197 udev_device->monitor_buf_len = 0;
1198 udev_device->monitor_buf = malloc(MONITOR_BUF_SIZE);
1199 if (udev_device->monitor_buf == NULL)
1202 /* envp array, strings will point into monitor buffer */
1203 if (udev_device->envp == NULL)
1204 udev_device->envp = malloc(sizeof(char *) * ENVP_SIZE);
1205 if (udev_device->envp == NULL)
1209 s = udev_device->monitor_buf;
1210 l = MONITOR_BUF_SIZE;
1211 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device)) {
1214 key = udev_list_entry_get_name(list_entry);
1215 /* skip private variables */
1219 /* add string to envp array */
1220 udev_device->envp[i++] = s;
1221 if (i+1 >= ENVP_SIZE)
1224 /* add property string to monitor buffer */
1225 l = util_strpcpyl(&s, l, key, "=", udev_list_entry_get_value(list_entry), NULL);
1230 udev_device->envp[i] = NULL;
1231 udev_device->monitor_buf_len = s - udev_device->monitor_buf;
1232 udev_device->envp_uptodate = 1;
1233 dbg(udev_device->udev, "filled envp/monitor buffer, %u properties, %zu bytes\n",
1234 i, udev_device->monitor_buf_len);
1238 char **udev_device_get_properties_envp(struct udev_device *udev_device)
1240 if (!udev_device->envp_uptodate)
1241 if (update_envp_monitor_buf(udev_device) != 0)
1243 return udev_device->envp;
1246 ssize_t udev_device_get_properties_monitor_buf(struct udev_device *udev_device, const char **buf)
1248 if (!udev_device->envp_uptodate)
1249 if (update_envp_monitor_buf(udev_device) != 0)
1251 *buf = udev_device->monitor_buf;
1252 return udev_device->monitor_buf_len;
1255 int udev_device_set_action(struct udev_device *udev_device, const char *action)
1257 free(udev_device->action);
1258 udev_device->action = strdup(action);
1259 if (udev_device->action == NULL)
1261 udev_device_add_property(udev_device, "ACTION", udev_device->action);
1265 int udev_device_set_driver(struct udev_device *udev_device, const char *driver)
1267 free(udev_device->driver);
1268 udev_device->driver = strdup(driver);
1269 if (udev_device->driver == NULL)
1271 udev_device->driver_set = 1;
1272 udev_device_add_property(udev_device, "DRIVER", udev_device->driver);
1276 const char *udev_device_get_devpath_old(struct udev_device *udev_device)
1278 return udev_device->devpath_old;
1281 int udev_device_set_devpath_old(struct udev_device *udev_device, const char *devpath_old)
1286 free(udev_device->devpath_old);
1287 udev_device->devpath_old = strdup(devpath_old);
1288 if (udev_device->devpath_old == NULL)
1290 udev_device_add_property(udev_device, "DEVPATH_OLD", udev_device->devpath_old);
1292 pos = strrchr(udev_device->devpath_old, '/');
1295 udev_device->sysname_old = strdup(&pos[1]);
1296 if (udev_device->sysname_old == NULL)
1299 /* some devices have '!' in their name, change that to '/' */
1301 while (udev_device->sysname_old[len] != '\0') {
1302 if (udev_device->sysname_old[len] == '!')
1303 udev_device->sysname_old[len] = '/';
1309 const char *udev_device_get_sysname_old(struct udev_device *udev_device)
1311 if (udev_device == NULL)
1313 return udev_device->sysname_old;
1316 const char *udev_device_get_knodename(struct udev_device *udev_device)
1318 return udev_device->knodename;
1321 int udev_device_set_knodename(struct udev_device *udev_device, const char *knodename)
1323 free(udev_device->knodename);
1324 udev_device->knodename = strdup(knodename);
1325 if (udev_device->knodename == NULL)
1327 udev_device_add_property(udev_device, "DEVNAME", udev_device->knodename);
1331 int udev_device_get_timeout(struct udev_device *udev_device)
1333 return udev_device->timeout;
1336 int udev_device_set_timeout(struct udev_device *udev_device, int timeout)
1338 udev_device->timeout = timeout;
1341 int udev_device_get_event_timeout(struct udev_device *udev_device)
1343 if (!udev_device->info_loaded)
1344 device_load_info(udev_device);
1345 return udev_device->event_timeout;
1348 int udev_device_set_event_timeout(struct udev_device *udev_device, int event_timeout)
1350 udev_device->event_timeout = event_timeout;
1354 int udev_device_set_seqnum(struct udev_device *udev_device, unsigned long long int seqnum)
1358 udev_device->seqnum = seqnum;
1359 snprintf(num, sizeof(num), "%llu", seqnum);
1360 udev_device_add_property(udev_device, "SEQNUM", num);
1364 int udev_device_set_devnum(struct udev_device *udev_device, dev_t devnum)
1368 udev_device->devnum = devnum;
1370 snprintf(num, sizeof(num), "%u", major(devnum));
1371 udev_device_add_property(udev_device, "MAJOR", num);
1372 snprintf(num, sizeof(num), "%u", minor(devnum));
1373 udev_device_add_property(udev_device, "MINOR", num);
1377 int udev_device_get_num_fake_partitions(struct udev_device *udev_device)
1379 if (!udev_device->info_loaded)
1380 device_load_info(udev_device);
1381 return udev_device->num_fake_partitions;
1384 int udev_device_set_num_fake_partitions(struct udev_device *udev_device, int num)
1386 udev_device->num_fake_partitions = num;
1390 int udev_device_get_devlink_priority(struct udev_device *udev_device)
1392 if (!udev_device->info_loaded)
1393 device_load_info(udev_device);
1394 return udev_device->devlink_priority;
1397 int udev_device_set_devlink_priority(struct udev_device *udev_device, int prio)
1399 udev_device->devlink_priority = prio;
1403 int udev_device_get_ignore_remove(struct udev_device *udev_device)
1405 if (!udev_device->info_loaded)
1406 device_load_info(udev_device);
1407 return udev_device->ignore_remove;
1410 int udev_device_set_ignore_remove(struct udev_device *udev_device, int ignore)
1412 udev_device->ignore_remove = ignore;
1416 int udev_device_get_watch_handle(struct udev_device *udev_device)
1418 if (!udev_device->info_loaded)
1419 device_load_info(udev_device);
1420 return udev_device->watch_handle;
1423 int udev_device_set_watch_handle(struct udev_device *udev_device, int handle)
1425 udev_device->watch_handle = handle;