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 if (strncmp(&syspath[len], "/devices/", 9) == 0) {
307 char file[UTIL_PATH_SIZE];
309 /* all "devices" require a "uevent" file */
310 util_strscpyl(file, sizeof(file), path, "/uevent", NULL);
311 if (stat(file, &statbuf) != 0) {
312 dbg(udev, "not a device: %s\n", syspath);
316 /* everything else just needs to be a directory */
317 if (stat(path, &statbuf) != 0 || !S_ISDIR(statbuf.st_mode)) {
318 dbg(udev, "directory not found: %s\n", syspath);
323 udev_device = udev_device_new(udev);
324 if (udev_device == NULL)
327 udev_device_set_syspath(udev_device, path);
328 info(udev, "device %p has devpath '%s'\n", udev_device, udev_device_get_devpath(udev_device));
334 * udev_device_new_from_devnum:
335 * @udev: udev library context
336 * @type: char or block device
337 * @devnum: device major/minor number
339 * Create new udev device, and fill in information from the sys
340 * device and the udev database entry. The device is looked up
341 * by its major/minor number. Character and block device numbers
342 * are not unique across the two types, they do not share the same
345 * The initial refcount is 1, and needs to be decremented to
346 * release the resources of the udev device.
348 * Returns: a new udev device, or #NULL, if it does not exist
350 struct udev_device *udev_device_new_from_devnum(struct udev *udev, char type, dev_t devnum)
352 char path[UTIL_PATH_SIZE];
353 const char *type_str;
357 else if (type == 'c')
362 /* use /sys/dev/{block,char}/<maj>:<min> link */
363 snprintf(path, sizeof(path), "%s/dev/%s/%u:%u",
364 udev_get_sys_path(udev), type_str, major(devnum), minor(devnum));
365 return udev_device_new_from_syspath(udev, path);
369 * udev_device_new_from_subsystem_sysname:
370 * @udev: udev library context
371 * @subsystem: the subsystem of the device
372 * @sysname: the name of the device
374 * Create new udev device, and fill in information from the sys
375 * device and the udev database entry. The device is looked up
376 * by the subsystem and name string of the device, like "mem",
377 * "zero", or "block", "sda".
379 * The initial refcount is 1, and needs to be decremented to
380 * release the resources of the udev device.
382 * Returns: a new udev device, or #NULL, if it does not exist
384 struct udev_device *udev_device_new_from_subsystem_sysname(struct udev *udev, const char *subsystem, const char *sysname)
386 char path_full[UTIL_PATH_SIZE];
392 l = util_strpcpyl(&path, sizeof(path_full), udev_get_sys_path(udev), NULL);
394 if (strcmp(subsystem, "subsystem") == 0) {
395 util_strscpyl(path, l, "/subsystem/", sysname, NULL);
396 if (stat(path_full, &statbuf) == 0)
399 util_strscpyl(path, l, "/bus/", sysname, NULL);
400 if (stat(path_full, &statbuf) == 0)
403 util_strscpyl(path, l, "/class/", sysname, NULL);
404 if (stat(path_full, &statbuf) == 0)
409 if (strcmp(subsystem, "module") == 0) {
410 util_strscpyl(path, l, "/module/", sysname, NULL);
411 if (stat(path_full, &statbuf) == 0)
416 if (strcmp(subsystem, "drivers") == 0) {
417 char subsys[UTIL_NAME_SIZE];
420 util_strscpy(subsys, sizeof(subsys), sysname);
421 driver = strchr(subsys, ':');
422 if (driver != NULL) {
426 util_strscpyl(path, l, "/subsystem/", subsys, "/drivers/", driver, NULL);
427 if (stat(path_full, &statbuf) == 0)
430 util_strscpyl(path, l, "/bus/", subsys, "/drivers/", driver, NULL);
431 if (stat(path_full, &statbuf) == 0)
437 util_strscpyl(path, l, "/subsystem/", subsystem, "/devices/", sysname, NULL);
438 if (stat(path_full, &statbuf) == 0)
441 util_strscpyl(path, l, "/bus/", subsystem, "/devices/", sysname, NULL);
442 if (stat(path_full, &statbuf) == 0)
445 util_strscpyl(path, l, "/class/", subsystem, "/", sysname, NULL);
446 if (stat(path_full, &statbuf) == 0)
451 return udev_device_new_from_syspath(udev, path_full);
454 static struct udev_device *device_new_from_parent(struct udev_device *udev_device)
456 struct udev_device *udev_device_parent = NULL;
457 char path[UTIL_PATH_SIZE];
460 util_strscpy(path, sizeof(path), udev_device->syspath);
461 subdir = &path[strlen(udev_get_sys_path(udev_device->udev))+1];
465 pos = strrchr(subdir, '/');
466 if (pos == NULL || pos < &subdir[2])
469 udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
470 if (udev_device_parent != NULL)
471 return udev_device_parent;
477 * udev_device_get_parent:
478 * @udev_device: the device to start searching from
480 * Find the next parent device, and fill in information from the sys
481 * device and the udev database entry.
483 * The returned the device is not referenced. It is attached to the
484 * child device, and will be cleaned up when the child device
487 * It is not necessarily just the upper level directory, empty or not
488 * recognized sys directories are ignored.
490 * It can be called as many times as needed, without caring about
493 * Returns: a new udev device, or #NULL, if it no parent exist.
495 struct udev_device *udev_device_get_parent(struct udev_device *udev_device)
497 if (udev_device == NULL)
499 if (!udev_device->parent_set) {
500 udev_device->parent_set = 1;
501 udev_device->parent_device = device_new_from_parent(udev_device);
503 if (udev_device->parent_device != NULL)
504 dbg(udev_device->udev, "returning existing parent %p\n", udev_device->parent_device);
505 return udev_device->parent_device;
509 * udev_device_get_parent_with_subsystem_devtype:
510 * @udev_device: udev device to start searching from
511 * @subsystem: the subsystem of the device
512 * @devtype: the type (DEVTYPE) of the device
514 * Find the next parent device, with a matching subsystem and devtype
515 * value, and fill in information from the sys device and the udev
518 * If devtype is #NULL, only subsystem is checked, and any devtype will
521 * The returned the device is not referenced. It is attached to the
522 * child device, and will be cleaned up when the child device
525 * It can be called as many times as needed, without caring about
528 * Returns: a new udev device, or #NULL if no matching parent exists.
530 struct udev_device *udev_device_get_parent_with_subsystem_devtype(struct udev_device *udev_device, const char *subsystem, const char *devtype)
532 struct udev_device *parent;
534 if (subsystem == NULL)
537 parent = udev_device_get_parent(udev_device);
538 while (parent != NULL) {
539 const char *parent_subsystem;
540 const char *parent_devtype;
542 parent_subsystem = udev_device_get_subsystem(parent);
543 if (parent_subsystem != NULL && strcmp(parent_subsystem, subsystem) == 0) {
546 parent_devtype = udev_device_get_devtype(parent);
547 if (parent_devtype != NULL && strcmp(parent_devtype, devtype) == 0)
550 parent = udev_device_get_parent(parent);
556 * udev_device_get_udev:
557 * @udev_device: udev device
559 * Retrieve the udev library context the device was created with.
561 * Returns: the udev library context
563 struct udev *udev_device_get_udev(struct udev_device *udev_device)
565 if (udev_device == NULL)
567 return udev_device->udev;
572 * @udev_device: udev device
574 * Take a reference of a udev device.
576 * Returns: the passed udev device
578 struct udev_device *udev_device_ref(struct udev_device *udev_device)
580 if (udev_device == NULL)
582 udev_device->refcount++;
588 * @udev_device: udev device
590 * Drop a reference of a udev device. If the refcount reaches zero,
591 * the resources of the device will be released.
594 void udev_device_unref(struct udev_device *udev_device)
596 if (udev_device == NULL)
598 udev_device->refcount--;
599 if (udev_device->refcount > 0)
601 if (udev_device->parent_device != NULL)
602 udev_device_unref(udev_device->parent_device);
603 free(udev_device->syspath);
604 free(udev_device->sysname);
605 free(udev_device->devnode);
606 free(udev_device->subsystem);
607 free(udev_device->devtype);
608 udev_list_cleanup_entries(udev_device->udev, &udev_device->devlinks_list);
609 udev_list_cleanup_entries(udev_device->udev, &udev_device->properties_list);
610 free(udev_device->action);
611 free(udev_device->driver);
612 free(udev_device->devpath_old);
613 free(udev_device->sysname_old);
614 free(udev_device->knodename);
615 udev_list_cleanup_entries(udev_device->udev, &udev_device->sysattr_list);
616 free(udev_device->envp);
617 free(udev_device->monitor_buf);
618 dbg(udev_device->udev, "udev_device: %p released\n", udev_device);
623 * udev_device_get_devpath:
624 * @udev_device: udev device
626 * Retrieve the kernel devpath value of the udev device. The path
627 * does not contain the sys mount point, and starts with a '/'.
629 * Returns: the devpath of the udev device
631 const char *udev_device_get_devpath(struct udev_device *udev_device)
633 if (udev_device == NULL)
635 return udev_device->devpath;
639 * udev_device_get_syspath:
640 * @udev_device: udev device
642 * Retrieve the sys path of the udev device. The path is an
643 * absolute path and starts with the sys mount point.
645 * Returns: the sys path of the udev device
647 const char *udev_device_get_syspath(struct udev_device *udev_device)
649 if (udev_device == NULL)
651 return udev_device->syspath;
655 * udev_device_get_sysname:
656 * @udev_device: udev device
658 * Returns: the sys name of the device device
660 const char *udev_device_get_sysname(struct udev_device *udev_device)
662 if (udev_device == NULL)
664 return udev_device->sysname;
668 * udev_device_get_sysnum:
669 * @udev_device: udev device
671 * Returns: the trailing number of of the device name
673 const char *udev_device_get_sysnum(struct udev_device *udev_device)
675 if (udev_device == NULL)
677 return udev_device->sysnum;
681 * udev_device_get_devnode:
682 * @udev_device: udev device
684 * Retrieve the device node file name belonging to the udev device.
685 * The path is an absolute path, and starts with the device directory.
687 * Returns: the device node file name of the udev device, or #NULL if no device node exists
689 const char *udev_device_get_devnode(struct udev_device *udev_device)
691 if (udev_device == NULL)
693 if (!udev_device->info_loaded)
694 device_load_info(udev_device);
695 return udev_device->devnode;
699 * udev_device_get_subsystem:
700 * @udev_device: udev device
702 * Retrieve the subsystem string of the udev device. The string does not
705 * Returns: the subsystem name of the udev device, or #NULL if it can not be determined
707 const char *udev_device_get_subsystem(struct udev_device *udev_device)
709 char subsystem[UTIL_NAME_SIZE];
711 if (udev_device == NULL)
713 if (!udev_device->subsystem_set) {
714 udev_device->subsystem_set = 1;
715 /* read "subsystem" link */
716 if (util_get_sys_subsystem(udev_device->udev, udev_device->syspath, subsystem, sizeof(subsystem)) > 0) {
717 udev_device_set_subsystem(udev_device, subsystem);
718 return udev_device->subsystem;
721 if (strncmp(udev_device->devpath, "/module/", 8) == 0) {
722 udev_device_set_subsystem(udev_device, "module");
723 return udev_device->subsystem;
725 if (strstr(udev_device->devpath, "/drivers/") != NULL) {
726 udev_device_set_subsystem(udev_device, "drivers");
727 return udev_device->subsystem;
729 if (strncmp(udev_device->devpath, "/subsystem/", 11) == 0 ||
730 strncmp(udev_device->devpath, "/class/", 7) == 0 ||
731 strncmp(udev_device->devpath, "/bus/", 5) == 0) {
732 udev_device_set_subsystem(udev_device, "subsystem");
733 return udev_device->subsystem;
736 return udev_device->subsystem;
740 * udev_device_get_devtype:
741 * @udev_device: udev device
743 * Retrieve the devtype string of the udev device.
745 * Returns: the devtype name of the udev device, or #NULL if it can not be determined
747 const char *udev_device_get_devtype(struct udev_device *udev_device)
749 if (udev_device == NULL)
751 if (!udev_device->devtype_set) {
752 udev_device->devtype_set = 1;
753 if (!udev_device->info_loaded)
754 udev_device_read_uevent_file(udev_device);
756 return udev_device->devtype;
760 * udev_device_get_devlinks_list_entry:
761 * @udev_device: udev device
763 * Retrieve the list of device links pointing to the device file of
764 * the udev device. The next list entry can be retrieved with
765 * udev_list_entry_next(), which returns #NULL if no more entries exist.
766 * The devlink path can be retrieved from the list entry by
767 * udev_list_entry_get_name(). The path is an absolute path, and starts with
768 * the device directory.
770 * Returns: the first entry of the device node link list
772 struct udev_list_entry *udev_device_get_devlinks_list_entry(struct udev_device *udev_device)
774 if (udev_device == NULL)
776 if (!udev_device->info_loaded)
777 device_load_info(udev_device);
778 return udev_list_get_entry(&udev_device->devlinks_list);
781 void udev_device_cleanup_devlinks_list(struct udev_device *udev_device)
783 udev_device->devlinks_uptodate = 0;
784 udev_list_cleanup_entries(udev_device->udev, &udev_device->devlinks_list);
788 * udev_device_get_properties_list_entry:
789 * @udev_device: udev device
791 * Retrieve the list of key/value device properties of the udev
792 * device. The next list entry can be retrieved with udev_list_entry_next(),
793 * which returns #NULL if no more entries exist. The property name
794 * can be retrieved from the list entry by udev_list_get_name(),
795 * the property value by udev_list_get_value().
797 * Returns: the first entry of the property list
799 struct udev_list_entry *udev_device_get_properties_list_entry(struct udev_device *udev_device)
801 if (udev_device == NULL)
803 if (!udev_device->info_loaded)
804 device_load_info(udev_device);
805 if (!udev_device->devlinks_uptodate) {
806 char symlinks[UTIL_PATH_SIZE];
807 struct udev_list_entry *list_entry;
809 udev_device->devlinks_uptodate = 1;
810 list_entry = udev_device_get_devlinks_list_entry(udev_device);
811 if (list_entry != NULL) {
816 l = util_strpcpyl(&s, sizeof(symlinks), udev_list_entry_get_name(list_entry), NULL);
817 udev_list_entry_foreach(list_entry, udev_list_entry_get_next(list_entry))
818 l = util_strpcpyl(&s, l, " ", udev_list_entry_get_name(list_entry), NULL);
819 udev_device_add_property(udev_device, "DEVLINKS", symlinks);
822 return udev_list_get_entry(&udev_device->properties_list);
826 * udev_device_get_driver:
827 * @udev_device: udev device
829 * Returns: the driver string, or #NULL if there is no driver attached.
831 const char *udev_device_get_driver(struct udev_device *udev_device)
833 char driver[UTIL_NAME_SIZE];
835 if (udev_device == NULL)
837 if (!udev_device->driver_set) {
838 udev_device->driver_set = 1;
839 if (util_get_sys_driver(udev_device->udev, udev_device->syspath, driver, sizeof(driver)) > 0)
840 udev_device->driver = strdup(driver);
842 return udev_device->driver;
846 * udev_device_get_devnum:
847 * @udev_device: udev device
849 * Returns: the device major/minor number.
851 dev_t udev_device_get_devnum(struct udev_device *udev_device)
853 if (udev_device == NULL)
854 return makedev(0, 0);
855 if (!udev_device->info_loaded)
856 device_load_info(udev_device);
857 return udev_device->devnum;
861 * udev_device_get_action:
862 * @udev_device: udev device
864 * This is only valid if the device was received through a monitor. Devices read from
865 * sys do not have an action string. Usual actions are: add, remove, change, online,
868 * Returns: the kernel action value, or #NULL if there is no action value available.
870 const char *udev_device_get_action(struct udev_device *udev_device)
872 if (udev_device == NULL)
874 return udev_device->action;
878 * udev_device_get_devnum:
879 * @udev_device: udev device
881 * This is only valid if the device was received through a monitor. Devices read from
882 * sys do not have a sequence number.
884 * Returns: the kernel event sequence number, or 0 if there is no sequence number available.
886 unsigned long long int udev_device_get_seqnum(struct udev_device *udev_device)
888 if (udev_device == NULL)
890 return udev_device->seqnum;
894 * udev_device_get_sysattr_value:
895 * @udev_device: udev device
896 * @sysattr: attribute name
898 * The retrieved value is cached in the device. Repeated calls will return the same
899 * value and not open the attribute again.
901 * Returns: the content of a sys attribute file, or #NULL if there is no sys attribute value.
903 const char *udev_device_get_sysattr_value(struct udev_device *udev_device, const char *sysattr)
905 struct udev_list_entry *list_entry;
906 char path[UTIL_PATH_SIZE];
911 const char *val = NULL;
913 if (udev_device == NULL)
918 /* look for possibly already cached result */
919 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_device->sysattr_list)) {
920 if (strcmp(udev_list_entry_get_name(list_entry), sysattr) == 0) {
921 dbg(udev_device->udev, "got '%s' (%s) from cache\n",
922 sysattr, udev_list_entry_get_value(list_entry));
923 return udev_list_entry_get_value(list_entry);
927 util_strscpyl(path, sizeof(path), udev_device_get_syspath(udev_device), "/", sysattr, NULL);
928 if (lstat(path, &statbuf) != 0) {
929 dbg(udev_device->udev, "no attribute '%s', keep negative entry\n", path);
930 udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, NULL, 0, 0);
934 if (S_ISLNK(statbuf.st_mode)) {
935 char target[UTIL_NAME_SIZE];
939 /* some core links return the last element of the target path */
940 if (strcmp(sysattr, "driver") != 0 &&
941 strcmp(sysattr, "subsystem") != 0 &&
942 strcmp(sysattr, "module") != 0)
945 len = readlink(path, target, sizeof(target));
948 pos = strrchr(target, '/');
951 dbg(udev_device->udev, "cache '%s' with link value '%s'\n", sysattr, pos);
952 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, pos, 0, 0);
953 val = udev_list_entry_get_value(list_entry);
959 /* skip directories */
960 if (S_ISDIR(statbuf.st_mode))
963 /* skip non-readable files */
964 if ((statbuf.st_mode & S_IRUSR) == 0)
967 /* read attribute value */
968 fd = open(path, O_RDONLY);
970 dbg(udev_device->udev, "attribute '%s' can not be opened\n", path);
973 size = read(fd, value, sizeof(value));
977 if (size == sizeof(value))
980 /* got a valid value, store it in cache and return it */
982 util_remove_trailing_chars(value, '\n');
983 dbg(udev_device->udev, "'%s' has attribute value '%s'\n", path, value);
984 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, value, 0, 0);
985 val = udev_list_entry_get_value(list_entry);
990 int udev_device_set_syspath(struct udev_device *udev_device, const char *syspath)
995 free(udev_device->syspath);
996 udev_device->syspath = strdup(syspath);
997 if (udev_device->syspath == NULL)
999 udev_device->devpath = &udev_device->syspath[strlen(udev_get_sys_path(udev_device->udev))];
1000 udev_device_add_property(udev_device, "DEVPATH", udev_device->devpath);
1002 pos = strrchr(udev_device->syspath, '/');
1005 udev_device->sysname = strdup(&pos[1]);
1006 if (udev_device->sysname == NULL)
1009 /* some devices have '!' in their name, change that to '/' */
1011 while (udev_device->sysname[len] != '\0') {
1012 if (udev_device->sysname[len] == '!')
1013 udev_device->sysname[len] = '/';
1017 /* trailing number */
1018 while (len > 0 && isdigit(udev_device->sysname[--len]))
1019 udev_device->sysnum = &udev_device->sysname[len];
1021 /* sysname is completely numeric */
1023 udev_device->sysnum = NULL;
1028 int udev_device_set_subsystem(struct udev_device *udev_device, const char *subsystem)
1030 free(udev_device->subsystem);
1031 udev_device->subsystem = strdup(subsystem);
1032 if (udev_device->subsystem == NULL)
1034 udev_device->subsystem_set = 1;
1035 udev_device_add_property(udev_device, "SUBSYSTEM", udev_device->subsystem);
1039 int udev_device_set_devtype(struct udev_device *udev_device, const char *devtype)
1041 free(udev_device->devtype);
1042 udev_device->devtype = strdup(devtype);
1043 if (udev_device->devtype == NULL)
1045 udev_device->devtype_set = 1;
1046 udev_device_add_property(udev_device, "DEVTYPE", udev_device->devtype);
1050 int udev_device_set_devnode(struct udev_device *udev_device, const char *devnode)
1052 free(udev_device->devnode);
1053 udev_device->devnode = strdup(devnode);
1054 if (devnode == NULL)
1056 if (udev_device->devnode == NULL)
1058 udev_device_add_property(udev_device, "DEVNAME", udev_device->devnode);
1062 int udev_device_add_devlink(struct udev_device *udev_device, const char *devlink, int unique)
1064 struct udev_list_entry *list_entry;
1066 udev_device->devlinks_uptodate = 0;
1067 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->devlinks_list, devlink, NULL, 1, 0);
1068 if (list_entry == NULL)
1071 udev_list_entry_set_flag(list_entry, 1);
1075 struct udev_list_entry *udev_device_add_property(struct udev_device *udev_device, const char *key, const char *value)
1077 udev_device->envp_uptodate = 0;
1078 if (value == NULL) {
1079 struct udev_list_entry *list_entry;
1081 list_entry = udev_device_get_properties_list_entry(udev_device);
1082 list_entry = udev_list_entry_get_by_name(list_entry, key);
1083 if (list_entry != NULL)
1084 udev_list_entry_delete(list_entry);
1087 return udev_list_entry_add(udev_device->udev, &udev_device->properties_list, key, value, 1, 0);
1090 struct udev_list_entry *udev_device_add_property_from_string(struct udev_device *udev_device, const char *property)
1092 char name[UTIL_PATH_SIZE];
1095 util_strscpy(name, sizeof(name), property);
1096 val = strchr(name, '=');
1103 return udev_device_add_property(udev_device, name, val);
1107 * udev_device_get_property_value:
1108 * @udev_device: udev device
1109 * @key: property name
1111 * Returns: the value of a device property, or #NULL if there is no such property.
1113 const char *udev_device_get_property_value(struct udev_device *udev_device, const char *key)
1115 struct udev_list_entry *list_entry;
1117 if (udev_device == NULL)
1122 list_entry = udev_device_get_properties_list_entry(udev_device);
1123 list_entry = udev_list_entry_get_by_name(list_entry, key);
1124 return udev_list_entry_get_value(list_entry);
1127 #define ENVP_SIZE 128
1128 #define MONITOR_BUF_SIZE 4096
1129 static int update_envp_monitor_buf(struct udev_device *udev_device)
1131 struct udev_list_entry *list_entry;
1136 /* monitor buffer of property strings */
1137 free(udev_device->monitor_buf);
1138 udev_device->monitor_buf_len = 0;
1139 udev_device->monitor_buf = malloc(MONITOR_BUF_SIZE);
1140 if (udev_device->monitor_buf == NULL)
1143 /* envp array, strings will point into monitor buffer */
1144 if (udev_device->envp == NULL)
1145 udev_device->envp = malloc(sizeof(char *) * ENVP_SIZE);
1146 if (udev_device->envp == NULL)
1150 s = udev_device->monitor_buf;
1151 l = MONITOR_BUF_SIZE;
1152 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device)) {
1155 key = udev_list_entry_get_name(list_entry);
1156 /* skip private variables */
1160 /* add string to envp array */
1161 udev_device->envp[i++] = s;
1162 if (i+1 >= ENVP_SIZE)
1165 /* add property string to monitor buffer */
1166 l = util_strpcpyl(&s, l, key, "=", udev_list_entry_get_value(list_entry), NULL);
1171 udev_device->envp[i] = NULL;
1172 udev_device->monitor_buf_len = s - udev_device->monitor_buf;
1173 udev_device->envp_uptodate = 1;
1174 dbg(udev_device->udev, "filled envp/monitor buffer, %u properties, %zu bytes\n",
1175 i, udev_device->monitor_buf_len);
1179 char **udev_device_get_properties_envp(struct udev_device *udev_device)
1181 if (!udev_device->envp_uptodate)
1182 if (update_envp_monitor_buf(udev_device) != 0)
1184 return udev_device->envp;
1187 ssize_t udev_device_get_properties_monitor_buf(struct udev_device *udev_device, const char **buf)
1189 if (!udev_device->envp_uptodate)
1190 if (update_envp_monitor_buf(udev_device) != 0)
1192 *buf = udev_device->monitor_buf;
1193 return udev_device->monitor_buf_len;
1196 int udev_device_set_action(struct udev_device *udev_device, const char *action)
1198 free(udev_device->action);
1199 udev_device->action = strdup(action);
1200 if (udev_device->action == NULL)
1202 udev_device_add_property(udev_device, "ACTION", udev_device->action);
1206 int udev_device_set_driver(struct udev_device *udev_device, const char *driver)
1208 free(udev_device->driver);
1209 udev_device->driver = strdup(driver);
1210 if (udev_device->driver == NULL)
1212 udev_device->driver_set = 1;
1213 udev_device_add_property(udev_device, "DRIVER", udev_device->driver);
1217 const char *udev_device_get_devpath_old(struct udev_device *udev_device)
1219 return udev_device->devpath_old;
1222 int udev_device_set_devpath_old(struct udev_device *udev_device, const char *devpath_old)
1227 free(udev_device->devpath_old);
1228 udev_device->devpath_old = strdup(devpath_old);
1229 if (udev_device->devpath_old == NULL)
1231 udev_device_add_property(udev_device, "DEVPATH_OLD", udev_device->devpath_old);
1233 pos = strrchr(udev_device->devpath_old, '/');
1236 udev_device->sysname_old = strdup(&pos[1]);
1237 if (udev_device->sysname_old == NULL)
1240 /* some devices have '!' in their name, change that to '/' */
1242 while (udev_device->sysname_old[len] != '\0') {
1243 if (udev_device->sysname_old[len] == '!')
1244 udev_device->sysname_old[len] = '/';
1250 const char *udev_device_get_sysname_old(struct udev_device *udev_device)
1252 if (udev_device == NULL)
1254 return udev_device->sysname_old;
1257 const char *udev_device_get_knodename(struct udev_device *udev_device)
1259 return udev_device->knodename;
1262 int udev_device_set_knodename(struct udev_device *udev_device, const char *knodename)
1264 free(udev_device->knodename);
1265 udev_device->knodename = strdup(knodename);
1266 if (udev_device->knodename == NULL)
1268 udev_device_add_property(udev_device, "DEVNAME", udev_device->knodename);
1272 int udev_device_get_timeout(struct udev_device *udev_device)
1274 return udev_device->timeout;
1277 int udev_device_set_timeout(struct udev_device *udev_device, int timeout)
1279 udev_device->timeout = timeout;
1282 int udev_device_get_event_timeout(struct udev_device *udev_device)
1284 if (!udev_device->info_loaded)
1285 device_load_info(udev_device);
1286 return udev_device->event_timeout;
1289 int udev_device_set_event_timeout(struct udev_device *udev_device, int event_timeout)
1291 udev_device->event_timeout = event_timeout;
1295 int udev_device_set_seqnum(struct udev_device *udev_device, unsigned long long int seqnum)
1299 udev_device->seqnum = seqnum;
1300 snprintf(num, sizeof(num), "%llu", seqnum);
1301 udev_device_add_property(udev_device, "SEQNUM", num);
1305 int udev_device_set_devnum(struct udev_device *udev_device, dev_t devnum)
1309 udev_device->devnum = devnum;
1311 snprintf(num, sizeof(num), "%u", major(devnum));
1312 udev_device_add_property(udev_device, "MAJOR", num);
1313 snprintf(num, sizeof(num), "%u", minor(devnum));
1314 udev_device_add_property(udev_device, "MINOR", num);
1318 int udev_device_get_num_fake_partitions(struct udev_device *udev_device)
1320 if (!udev_device->info_loaded)
1321 device_load_info(udev_device);
1322 return udev_device->num_fake_partitions;
1325 int udev_device_set_num_fake_partitions(struct udev_device *udev_device, int num)
1327 udev_device->num_fake_partitions = num;
1331 int udev_device_get_devlink_priority(struct udev_device *udev_device)
1333 if (!udev_device->info_loaded)
1334 device_load_info(udev_device);
1335 return udev_device->devlink_priority;
1338 int udev_device_set_devlink_priority(struct udev_device *udev_device, int prio)
1340 udev_device->devlink_priority = prio;
1344 int udev_device_get_ignore_remove(struct udev_device *udev_device)
1346 if (!udev_device->info_loaded)
1347 device_load_info(udev_device);
1348 return udev_device->ignore_remove;
1351 int udev_device_set_ignore_remove(struct udev_device *udev_device, int ignore)
1353 udev_device->ignore_remove = ignore;
1357 int udev_device_get_watch_handle(struct udev_device *udev_device)
1359 if (!udev_device->info_loaded)
1360 device_load_info(udev_device);
1361 return udev_device->watch_handle;
1364 int udev_device_set_watch_handle(struct udev_device *udev_device, int handle)
1366 udev_device->watch_handle = handle;