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 util_strscpy(path, sizeof(path), udev_device->syspath);
512 subdir = &path[strlen(udev_get_sys_path(udev_device->udev))+1];
516 pos = strrchr(subdir, '/');
517 if (pos == NULL || pos < &subdir[2])
520 udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
521 if (udev_device_parent != NULL)
522 return udev_device_parent;
528 * udev_device_get_parent:
529 * @udev_device: the device to start searching from
531 * Find the next parent device, and fill in information from the sys
532 * device and the udev database entry.
534 * The returned the device is not referenced. It is attached to the
535 * child device, and will be cleaned up when the child device
538 * It is not necessarily just the upper level directory, empty or not
539 * recognized sys directories are ignored.
541 * It can be called as many times as needed, without caring about
544 * Returns: a new udev device, or #NULL, if it no parent exist.
546 struct udev_device *udev_device_get_parent(struct udev_device *udev_device)
548 if (udev_device == NULL)
550 if (!udev_device->parent_set) {
551 udev_device->parent_set = 1;
552 udev_device->parent_device = device_new_from_parent(udev_device);
554 if (udev_device->parent_device != NULL)
555 dbg(udev_device->udev, "returning existing parent %p\n", udev_device->parent_device);
556 return udev_device->parent_device;
560 * udev_device_get_parent_with_subsystem_devtype:
561 * @udev_device: udev device to start searching from
562 * @subsystem: the subsystem of the device
563 * @devtype: the type (DEVTYPE) of the device
565 * Find the next parent device, with a matching subsystem and devtype
566 * value, and fill in information from the sys device and the udev
569 * If devtype is #NULL, only subsystem is checked, and any devtype will
572 * The returned the device is not referenced. It is attached to the
573 * child device, and will be cleaned up when the child device
576 * It can be called as many times as needed, without caring about
579 * Returns: a new udev device, or #NULL if no matching parent exists.
581 struct udev_device *udev_device_get_parent_with_subsystem_devtype(struct udev_device *udev_device, const char *subsystem, const char *devtype)
583 struct udev_device *parent;
585 if (subsystem == NULL)
588 parent = udev_device_get_parent(udev_device);
589 while (parent != NULL) {
590 const char *parent_subsystem;
591 const char *parent_devtype;
593 parent_subsystem = udev_device_get_subsystem(parent);
594 if (parent_subsystem != NULL && strcmp(parent_subsystem, subsystem) == 0) {
597 parent_devtype = udev_device_get_devtype(parent);
598 if (parent_devtype != NULL && strcmp(parent_devtype, devtype) == 0)
601 parent = udev_device_get_parent(parent);
607 * udev_device_get_udev:
608 * @udev_device: udev device
610 * Retrieve the udev library context the device was created with.
612 * Returns: the udev library context
614 struct udev *udev_device_get_udev(struct udev_device *udev_device)
616 if (udev_device == NULL)
618 return udev_device->udev;
623 * @udev_device: udev device
625 * Take a reference of a udev device.
627 * Returns: the passed udev device
629 struct udev_device *udev_device_ref(struct udev_device *udev_device)
631 if (udev_device == NULL)
633 udev_device->refcount++;
639 * @udev_device: udev device
641 * Drop a reference of a udev device. If the refcount reaches zero,
642 * the resources of the device will be released.
645 void udev_device_unref(struct udev_device *udev_device)
647 if (udev_device == NULL)
649 udev_device->refcount--;
650 if (udev_device->refcount > 0)
652 if (udev_device->parent_device != NULL)
653 udev_device_unref(udev_device->parent_device);
654 free(udev_device->syspath);
655 free(udev_device->sysname);
656 free(udev_device->devnode);
657 free(udev_device->subsystem);
658 free(udev_device->devtype);
659 udev_list_cleanup_entries(udev_device->udev, &udev_device->devlinks_list);
660 udev_list_cleanup_entries(udev_device->udev, &udev_device->properties_list);
661 free(udev_device->action);
662 free(udev_device->driver);
663 free(udev_device->devpath_old);
664 free(udev_device->sysname_old);
665 free(udev_device->knodename);
666 udev_list_cleanup_entries(udev_device->udev, &udev_device->sysattr_list);
667 free(udev_device->envp);
668 free(udev_device->monitor_buf);
669 dbg(udev_device->udev, "udev_device: %p released\n", udev_device);
674 * udev_device_get_devpath:
675 * @udev_device: udev device
677 * Retrieve the kernel devpath value of the udev device. The path
678 * does not contain the sys mount point, and starts with a '/'.
680 * Returns: the devpath of the udev device
682 const char *udev_device_get_devpath(struct udev_device *udev_device)
684 if (udev_device == NULL)
686 return udev_device->devpath;
690 * udev_device_get_syspath:
691 * @udev_device: udev device
693 * Retrieve the sys path of the udev device. The path is an
694 * absolute path and starts with the sys mount point.
696 * Returns: the sys path of the udev device
698 const char *udev_device_get_syspath(struct udev_device *udev_device)
700 if (udev_device == NULL)
702 return udev_device->syspath;
706 * udev_device_get_sysname:
707 * @udev_device: udev device
709 * Returns: the sys name of the device device
711 const char *udev_device_get_sysname(struct udev_device *udev_device)
713 if (udev_device == NULL)
715 return udev_device->sysname;
719 * udev_device_get_sysnum:
720 * @udev_device: udev device
722 * Returns: the trailing number of of the device name
724 const char *udev_device_get_sysnum(struct udev_device *udev_device)
726 if (udev_device == NULL)
728 return udev_device->sysnum;
732 * udev_device_get_devnode:
733 * @udev_device: udev device
735 * Retrieve the device node file name belonging to the udev device.
736 * The path is an absolute path, and starts with the device directory.
738 * Returns: the device node file name of the udev device, or #NULL if no device node exists
740 const char *udev_device_get_devnode(struct udev_device *udev_device)
742 if (udev_device == NULL)
744 if (!udev_device->info_loaded)
745 device_load_info(udev_device);
746 return udev_device->devnode;
750 * udev_device_get_subsystem:
751 * @udev_device: udev device
753 * Retrieve the subsystem string of the udev device. The string does not
756 * Returns: the subsystem name of the udev device, or #NULL if it can not be determined
758 const char *udev_device_get_subsystem(struct udev_device *udev_device)
760 char subsystem[UTIL_NAME_SIZE];
762 if (udev_device == NULL)
764 if (!udev_device->subsystem_set) {
765 udev_device->subsystem_set = 1;
766 /* read "subsystem" link */
767 if (util_get_sys_subsystem(udev_device->udev, udev_device->syspath, subsystem, sizeof(subsystem)) > 0) {
768 udev_device_set_subsystem(udev_device, subsystem);
769 return udev_device->subsystem;
772 if (strncmp(udev_device->devpath, "/module/", 8) == 0) {
773 udev_device_set_subsystem(udev_device, "module");
774 return udev_device->subsystem;
776 if (strstr(udev_device->devpath, "/drivers/") != NULL) {
777 udev_device_set_subsystem(udev_device, "drivers");
778 return udev_device->subsystem;
780 if (strncmp(udev_device->devpath, "/subsystem/", 11) == 0 ||
781 strncmp(udev_device->devpath, "/class/", 7) == 0 ||
782 strncmp(udev_device->devpath, "/bus/", 5) == 0) {
783 udev_device_set_subsystem(udev_device, "subsystem");
784 return udev_device->subsystem;
787 return udev_device->subsystem;
791 * udev_device_get_devtype:
792 * @udev_device: udev device
794 * Retrieve the devtype string of the udev device.
796 * Returns: the devtype name of the udev device, or #NULL if it can not be determined
798 const char *udev_device_get_devtype(struct udev_device *udev_device)
800 if (udev_device == NULL)
802 if (!udev_device->devtype_set) {
803 udev_device->devtype_set = 1;
804 if (!udev_device->info_loaded)
805 udev_device_read_uevent_file(udev_device);
807 return udev_device->devtype;
811 * udev_device_get_devlinks_list_entry:
812 * @udev_device: udev device
814 * Retrieve the list of device links pointing to the device file of
815 * the udev device. The next list entry can be retrieved with
816 * udev_list_entry_next(), which returns #NULL if no more entries exist.
817 * The devlink path can be retrieved from the list entry by
818 * udev_list_entry_get_name(). The path is an absolute path, and starts with
819 * the device directory.
821 * Returns: the first entry of the device node link list
823 struct udev_list_entry *udev_device_get_devlinks_list_entry(struct udev_device *udev_device)
825 if (udev_device == NULL)
827 if (!udev_device->info_loaded)
828 device_load_info(udev_device);
829 return udev_list_get_entry(&udev_device->devlinks_list);
832 void udev_device_cleanup_devlinks_list(struct udev_device *udev_device)
834 udev_device->devlinks_uptodate = 0;
835 udev_list_cleanup_entries(udev_device->udev, &udev_device->devlinks_list);
839 * udev_device_get_properties_list_entry:
840 * @udev_device: udev device
842 * Retrieve the list of key/value device properties of the udev
843 * device. The next list entry can be retrieved with udev_list_entry_next(),
844 * which returns #NULL if no more entries exist. The property name
845 * can be retrieved from the list entry by udev_list_get_name(),
846 * the property value by udev_list_get_value().
848 * Returns: the first entry of the property list
850 struct udev_list_entry *udev_device_get_properties_list_entry(struct udev_device *udev_device)
852 if (udev_device == NULL)
854 if (!udev_device->info_loaded)
855 device_load_info(udev_device);
856 if (!udev_device->devlinks_uptodate) {
857 char symlinks[UTIL_PATH_SIZE];
858 struct udev_list_entry *list_entry;
860 udev_device->devlinks_uptodate = 1;
861 list_entry = udev_device_get_devlinks_list_entry(udev_device);
862 if (list_entry != NULL) {
867 l = util_strpcpyl(&s, sizeof(symlinks), udev_list_entry_get_name(list_entry), NULL);
868 udev_list_entry_foreach(list_entry, udev_list_entry_get_next(list_entry))
869 l = util_strpcpyl(&s, l, " ", udev_list_entry_get_name(list_entry), NULL);
870 udev_device_add_property(udev_device, "DEVLINKS", symlinks);
873 return udev_list_get_entry(&udev_device->properties_list);
877 * udev_device_get_driver:
878 * @udev_device: udev device
880 * Returns: the driver string, or #NULL if there is no driver attached.
882 const char *udev_device_get_driver(struct udev_device *udev_device)
884 char driver[UTIL_NAME_SIZE];
886 if (udev_device == NULL)
888 if (!udev_device->driver_set) {
889 udev_device->driver_set = 1;
890 if (util_get_sys_driver(udev_device->udev, udev_device->syspath, driver, sizeof(driver)) > 0)
891 udev_device->driver = strdup(driver);
893 return udev_device->driver;
897 * udev_device_get_devnum:
898 * @udev_device: udev device
900 * Returns: the device major/minor number.
902 dev_t udev_device_get_devnum(struct udev_device *udev_device)
904 if (udev_device == NULL)
905 return makedev(0, 0);
906 if (!udev_device->info_loaded)
907 device_load_info(udev_device);
908 return udev_device->devnum;
912 * udev_device_get_action:
913 * @udev_device: udev device
915 * This is only valid if the device was received through a monitor. Devices read from
916 * sys do not have an action string. Usual actions are: add, remove, change, online,
919 * Returns: the kernel action value, or #NULL if there is no action value available.
921 const char *udev_device_get_action(struct udev_device *udev_device)
923 if (udev_device == NULL)
925 return udev_device->action;
929 * udev_device_get_devnum:
930 * @udev_device: udev device
932 * This is only valid if the device was received through a monitor. Devices read from
933 * sys do not have a sequence number.
935 * Returns: the kernel event sequence number, or 0 if there is no sequence number available.
937 unsigned long long int udev_device_get_seqnum(struct udev_device *udev_device)
939 if (udev_device == NULL)
941 return udev_device->seqnum;
945 * udev_device_get_sysattr_value:
946 * @udev_device: udev device
947 * @sysattr: attribute name
949 * The retrieved value is cached in the device. Repeated calls will return the same
950 * value and not open the attribute again.
952 * Returns: the content of a sys attribute file, or #NULL if there is no sys attribute value.
954 const char *udev_device_get_sysattr_value(struct udev_device *udev_device, const char *sysattr)
956 struct udev_list_entry *list_entry;
957 char path[UTIL_PATH_SIZE];
962 const char *val = NULL;
964 if (udev_device == NULL)
969 /* look for possibly already cached result */
970 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_device->sysattr_list)) {
971 if (strcmp(udev_list_entry_get_name(list_entry), sysattr) == 0) {
972 dbg(udev_device->udev, "got '%s' (%s) from cache\n",
973 sysattr, udev_list_entry_get_value(list_entry));
974 return udev_list_entry_get_value(list_entry);
978 util_strscpyl(path, sizeof(path), udev_device_get_syspath(udev_device), "/", sysattr, NULL);
979 if (lstat(path, &statbuf) != 0) {
980 dbg(udev_device->udev, "no attribute '%s', keep negative entry\n", path);
981 udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, NULL, 0, 0);
985 if (S_ISLNK(statbuf.st_mode)) {
986 char target[UTIL_NAME_SIZE];
990 /* some core links return the last element of the target path */
991 if (strcmp(sysattr, "driver") != 0 &&
992 strcmp(sysattr, "subsystem") != 0 &&
993 strcmp(sysattr, "module") != 0)
996 len = readlink(path, target, sizeof(target));
999 pos = strrchr(target, '/');
1002 dbg(udev_device->udev, "cache '%s' with link value '%s'\n", sysattr, pos);
1003 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, pos, 0, 0);
1004 val = udev_list_entry_get_value(list_entry);
1010 /* skip directories */
1011 if (S_ISDIR(statbuf.st_mode))
1014 /* skip non-readable files */
1015 if ((statbuf.st_mode & S_IRUSR) == 0)
1018 /* read attribute value */
1019 fd = open(path, O_RDONLY);
1021 dbg(udev_device->udev, "attribute '%s' can not be opened\n", path);
1024 size = read(fd, value, sizeof(value));
1028 if (size == sizeof(value))
1031 /* got a valid value, store it in cache and return it */
1033 util_remove_trailing_chars(value, '\n');
1034 dbg(udev_device->udev, "'%s' has attribute value '%s'\n", path, value);
1035 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, value, 0, 0);
1036 val = udev_list_entry_get_value(list_entry);
1041 int udev_device_set_syspath(struct udev_device *udev_device, const char *syspath)
1046 free(udev_device->syspath);
1047 udev_device->syspath = strdup(syspath);
1048 if (udev_device->syspath == NULL)
1050 udev_device->devpath = &udev_device->syspath[strlen(udev_get_sys_path(udev_device->udev))];
1051 udev_device_add_property(udev_device, "DEVPATH", udev_device->devpath);
1053 pos = strrchr(udev_device->syspath, '/');
1056 udev_device->sysname = strdup(&pos[1]);
1057 if (udev_device->sysname == NULL)
1060 /* some devices have '!' in their name, change that to '/' */
1062 while (udev_device->sysname[len] != '\0') {
1063 if (udev_device->sysname[len] == '!')
1064 udev_device->sysname[len] = '/';
1068 /* trailing number */
1069 while (len > 0 && isdigit(udev_device->sysname[--len]))
1070 udev_device->sysnum = &udev_device->sysname[len];
1072 /* sysname is completely numeric */
1074 udev_device->sysnum = NULL;
1079 int udev_device_set_subsystem(struct udev_device *udev_device, const char *subsystem)
1081 free(udev_device->subsystem);
1082 udev_device->subsystem = strdup(subsystem);
1083 if (udev_device->subsystem == NULL)
1085 udev_device->subsystem_set = 1;
1086 udev_device_add_property(udev_device, "SUBSYSTEM", udev_device->subsystem);
1090 int udev_device_set_devtype(struct udev_device *udev_device, const char *devtype)
1092 free(udev_device->devtype);
1093 udev_device->devtype = strdup(devtype);
1094 if (udev_device->devtype == NULL)
1096 udev_device->devtype_set = 1;
1097 udev_device_add_property(udev_device, "DEVTYPE", udev_device->devtype);
1101 int udev_device_set_devnode(struct udev_device *udev_device, const char *devnode)
1103 free(udev_device->devnode);
1104 udev_device->devnode = strdup(devnode);
1105 if (devnode == NULL)
1107 if (udev_device->devnode == NULL)
1109 udev_device_add_property(udev_device, "DEVNAME", udev_device->devnode);
1113 int udev_device_add_devlink(struct udev_device *udev_device, const char *devlink, int unique)
1115 struct udev_list_entry *list_entry;
1117 udev_device->devlinks_uptodate = 0;
1118 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->devlinks_list, devlink, NULL, 1, 0);
1119 if (list_entry == NULL)
1122 udev_list_entry_set_flag(list_entry, 1);
1126 struct udev_list_entry *udev_device_add_property(struct udev_device *udev_device, const char *key, const char *value)
1128 udev_device->envp_uptodate = 0;
1129 if (value == NULL) {
1130 struct udev_list_entry *list_entry;
1132 list_entry = udev_device_get_properties_list_entry(udev_device);
1133 list_entry = udev_list_entry_get_by_name(list_entry, key);
1134 if (list_entry != NULL)
1135 udev_list_entry_delete(list_entry);
1138 return udev_list_entry_add(udev_device->udev, &udev_device->properties_list, key, value, 1, 0);
1141 struct udev_list_entry *udev_device_add_property_from_string(struct udev_device *udev_device, const char *property)
1143 char name[UTIL_PATH_SIZE];
1146 util_strscpy(name, sizeof(name), property);
1147 val = strchr(name, '=');
1154 return udev_device_add_property(udev_device, name, val);
1158 * udev_device_get_property_value:
1159 * @udev_device: udev device
1160 * @key: property name
1162 * Returns: the value of a device property, or #NULL if there is no such property.
1164 const char *udev_device_get_property_value(struct udev_device *udev_device, const char *key)
1166 struct udev_list_entry *list_entry;
1168 if (udev_device == NULL)
1173 list_entry = udev_device_get_properties_list_entry(udev_device);
1174 list_entry = udev_list_entry_get_by_name(list_entry, key);
1175 return udev_list_entry_get_value(list_entry);
1178 #define ENVP_SIZE 128
1179 #define MONITOR_BUF_SIZE 4096
1180 static int update_envp_monitor_buf(struct udev_device *udev_device)
1182 struct udev_list_entry *list_entry;
1187 /* monitor buffer of property strings */
1188 free(udev_device->monitor_buf);
1189 udev_device->monitor_buf_len = 0;
1190 udev_device->monitor_buf = malloc(MONITOR_BUF_SIZE);
1191 if (udev_device->monitor_buf == NULL)
1194 /* envp array, strings will point into monitor buffer */
1195 if (udev_device->envp == NULL)
1196 udev_device->envp = malloc(sizeof(char *) * ENVP_SIZE);
1197 if (udev_device->envp == NULL)
1201 s = udev_device->monitor_buf;
1202 l = MONITOR_BUF_SIZE;
1203 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device)) {
1206 key = udev_list_entry_get_name(list_entry);
1207 /* skip private variables */
1211 /* add string to envp array */
1212 udev_device->envp[i++] = s;
1213 if (i+1 >= ENVP_SIZE)
1216 /* add property string to monitor buffer */
1217 l = util_strpcpyl(&s, l, key, "=", udev_list_entry_get_value(list_entry), NULL);
1222 udev_device->envp[i] = NULL;
1223 udev_device->monitor_buf_len = s - udev_device->monitor_buf;
1224 udev_device->envp_uptodate = 1;
1225 dbg(udev_device->udev, "filled envp/monitor buffer, %u properties, %zu bytes\n",
1226 i, udev_device->monitor_buf_len);
1230 char **udev_device_get_properties_envp(struct udev_device *udev_device)
1232 if (!udev_device->envp_uptodate)
1233 if (update_envp_monitor_buf(udev_device) != 0)
1235 return udev_device->envp;
1238 ssize_t udev_device_get_properties_monitor_buf(struct udev_device *udev_device, const char **buf)
1240 if (!udev_device->envp_uptodate)
1241 if (update_envp_monitor_buf(udev_device) != 0)
1243 *buf = udev_device->monitor_buf;
1244 return udev_device->monitor_buf_len;
1247 int udev_device_set_action(struct udev_device *udev_device, const char *action)
1249 free(udev_device->action);
1250 udev_device->action = strdup(action);
1251 if (udev_device->action == NULL)
1253 udev_device_add_property(udev_device, "ACTION", udev_device->action);
1257 int udev_device_set_driver(struct udev_device *udev_device, const char *driver)
1259 free(udev_device->driver);
1260 udev_device->driver = strdup(driver);
1261 if (udev_device->driver == NULL)
1263 udev_device->driver_set = 1;
1264 udev_device_add_property(udev_device, "DRIVER", udev_device->driver);
1268 const char *udev_device_get_devpath_old(struct udev_device *udev_device)
1270 return udev_device->devpath_old;
1273 int udev_device_set_devpath_old(struct udev_device *udev_device, const char *devpath_old)
1278 free(udev_device->devpath_old);
1279 udev_device->devpath_old = strdup(devpath_old);
1280 if (udev_device->devpath_old == NULL)
1282 udev_device_add_property(udev_device, "DEVPATH_OLD", udev_device->devpath_old);
1284 pos = strrchr(udev_device->devpath_old, '/');
1287 udev_device->sysname_old = strdup(&pos[1]);
1288 if (udev_device->sysname_old == NULL)
1291 /* some devices have '!' in their name, change that to '/' */
1293 while (udev_device->sysname_old[len] != '\0') {
1294 if (udev_device->sysname_old[len] == '!')
1295 udev_device->sysname_old[len] = '/';
1301 const char *udev_device_get_sysname_old(struct udev_device *udev_device)
1303 if (udev_device == NULL)
1305 return udev_device->sysname_old;
1308 const char *udev_device_get_knodename(struct udev_device *udev_device)
1310 return udev_device->knodename;
1313 int udev_device_set_knodename(struct udev_device *udev_device, const char *knodename)
1315 free(udev_device->knodename);
1316 udev_device->knodename = strdup(knodename);
1317 if (udev_device->knodename == NULL)
1319 udev_device_add_property(udev_device, "DEVNAME", udev_device->knodename);
1323 int udev_device_get_timeout(struct udev_device *udev_device)
1325 return udev_device->timeout;
1328 int udev_device_set_timeout(struct udev_device *udev_device, int timeout)
1330 udev_device->timeout = timeout;
1333 int udev_device_get_event_timeout(struct udev_device *udev_device)
1335 if (!udev_device->info_loaded)
1336 device_load_info(udev_device);
1337 return udev_device->event_timeout;
1340 int udev_device_set_event_timeout(struct udev_device *udev_device, int event_timeout)
1342 udev_device->event_timeout = event_timeout;
1346 int udev_device_set_seqnum(struct udev_device *udev_device, unsigned long long int seqnum)
1350 udev_device->seqnum = seqnum;
1351 snprintf(num, sizeof(num), "%llu", seqnum);
1352 udev_device_add_property(udev_device, "SEQNUM", num);
1356 int udev_device_set_devnum(struct udev_device *udev_device, dev_t devnum)
1360 udev_device->devnum = devnum;
1362 snprintf(num, sizeof(num), "%u", major(devnum));
1363 udev_device_add_property(udev_device, "MAJOR", num);
1364 snprintf(num, sizeof(num), "%u", minor(devnum));
1365 udev_device_add_property(udev_device, "MINOR", num);
1369 int udev_device_get_num_fake_partitions(struct udev_device *udev_device)
1371 if (!udev_device->info_loaded)
1372 device_load_info(udev_device);
1373 return udev_device->num_fake_partitions;
1376 int udev_device_set_num_fake_partitions(struct udev_device *udev_device, int num)
1378 udev_device->num_fake_partitions = num;
1382 int udev_device_get_devlink_priority(struct udev_device *udev_device)
1384 if (!udev_device->info_loaded)
1385 device_load_info(udev_device);
1386 return udev_device->devlink_priority;
1389 int udev_device_set_devlink_priority(struct udev_device *udev_device, int prio)
1391 udev_device->devlink_priority = prio;
1395 int udev_device_get_ignore_remove(struct udev_device *udev_device)
1397 if (!udev_device->info_loaded)
1398 device_load_info(udev_device);
1399 return udev_device->ignore_remove;
1402 int udev_device_set_ignore_remove(struct udev_device *udev_device, int ignore)
1404 udev_device->ignore_remove = ignore;
1408 int udev_device_get_watch_handle(struct udev_device *udev_device)
1410 if (!udev_device->info_loaded)
1411 device_load_info(udev_device);
1412 return udev_device->watch_handle;
1415 int udev_device_set_watch_handle(struct udev_device *udev_device, int handle)
1417 udev_device->watch_handle = handle;