2 * libudev - interface to udev device information
4 * Copyright (C) 2008-2010 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.
25 #include "libudev-private.h"
28 * SECTION:libudev-device
29 * @short_description: kernel sys devices
31 * Representation of kernel sys devices. Devices are uniquely identified
32 * by their syspath, every device has exactly one path in the kernel sys
33 * filesystem. Devices usually belong to a kernel subsystem, and and have
34 * a unique name inside that subsystem.
40 * Opaque object representing one kernel sys device.
44 struct udev_device *parent_device;
59 size_t monitor_buf_len;
60 struct udev_list_node devlinks_list;
61 struct udev_list_node properties_list;
62 struct udev_list_node sysattr_list;
63 unsigned long long int seqnum;
71 unsigned int parent_set:1;
72 unsigned int subsystem_set:1;
73 unsigned int devtype_set:1;
74 unsigned int devlinks_uptodate:1;
75 unsigned int envp_uptodate:1;
76 unsigned int driver_set:1;
77 unsigned int info_loaded:1;
80 struct udev_list_entry *udev_device_add_property(struct udev_device *udev_device, const char *key, const char *value)
82 udev_device->envp_uptodate = 0;
84 struct udev_list_entry *list_entry;
86 list_entry = udev_device_get_properties_list_entry(udev_device);
87 list_entry = udev_list_entry_get_by_name(list_entry, key);
88 if (list_entry != NULL)
89 udev_list_entry_delete(list_entry);
92 return udev_list_entry_add(udev_device->udev, &udev_device->properties_list, key, value, 1, 0);
95 static struct udev_list_entry *udev_device_add_property_from_string(struct udev_device *udev_device, const char *property)
97 char name[UTIL_LINE_SIZE];
100 util_strscpy(name, sizeof(name), property);
101 val = strchr(name, '=');
108 return udev_device_add_property(udev_device, name, val);
112 * parse property string, and if needed, update internal values accordingly
114 * udev_device_add_property_from_string_parse_finish() needs to be
115 * called after adding properties, and its return value checked
117 * udev_device_set_info_loaded() needs to be set, to avoid trying
118 * to use a device without a DEVPATH set
120 void udev_device_add_property_from_string_parse(struct udev_device *udev_device, const char *property)
122 if (strncmp(property, "DEVPATH=", 8) == 0) {
123 char path[UTIL_PATH_SIZE];
125 util_strscpyl(path, sizeof(path), udev_get_sys_path(udev_device->udev), &property[8], NULL);
126 udev_device_set_syspath(udev_device, path);
127 } else if (strncmp(property, "SUBSYSTEM=", 10) == 0) {
128 udev_device_set_subsystem(udev_device, &property[10]);
129 } else if (strncmp(property, "DEVTYPE=", 8) == 0) {
130 udev_device_set_devtype(udev_device, &property[8]);
131 } else if (strncmp(property, "DEVNAME=", 8) == 0) {
132 if (property[8] == '/')
133 udev_device_set_devnode(udev_device, &property[8]);
135 udev_device_set_knodename(udev_device, &property[8]);
136 } else if (strncmp(property, "DEVLINKS=", 9) == 0) {
137 char devlinks[UTIL_PATH_SIZE];
141 util_strscpy(devlinks, sizeof(devlinks), &property[9]);
143 next = strchr(slink, ' ');
144 while (next != NULL) {
146 udev_device_add_devlink(udev_device, slink, 0);
148 next = strchr(slink, ' ');
150 if (slink[0] != '\0')
151 udev_device_add_devlink(udev_device, slink, 0);
152 } else if (strncmp(property, "DRIVER=", 7) == 0) {
153 udev_device_set_driver(udev_device, &property[7]);
154 } else if (strncmp(property, "ACTION=", 7) == 0) {
155 udev_device_set_action(udev_device, &property[7]);
156 } else if (strncmp(property, "MAJOR=", 6) == 0) {
157 udev_device->maj = strtoull(&property[6], NULL, 10);
158 } else if (strncmp(property, "MINOR=", 6) == 0) {
159 udev_device->min = strtoull(&property[6], NULL, 10);
160 } else if (strncmp(property, "DEVPATH_OLD=", 12) == 0) {
161 udev_device_set_devpath_old(udev_device, &property[12]);
162 } else if (strncmp(property, "SEQNUM=", 7) == 0) {
163 udev_device_set_seqnum(udev_device, strtoull(&property[7], NULL, 10));
164 } else if (strncmp(property, "TIMEOUT=", 8) == 0) {
165 udev_device_set_timeout(udev_device, strtoull(&property[8], NULL, 10));
167 udev_device_add_property_from_string(udev_device, property);
171 int udev_device_add_property_from_string_parse_finish(struct udev_device *udev_device)
173 if (udev_device->maj > 0)
174 udev_device_set_devnum(udev_device, makedev(udev_device->maj, udev_device->min));
175 udev_device->maj = 0;
176 udev_device->min = 0;
178 if (udev_device->devpath == NULL || udev_device->subsystem == NULL)
184 * udev_device_get_property_value:
185 * @udev_device: udev device
186 * @key: property name
188 * Returns: the value of a device property, or #NULL if there is no such property.
190 const char *udev_device_get_property_value(struct udev_device *udev_device, const char *key)
192 struct udev_list_entry *list_entry;
194 if (udev_device == NULL)
199 list_entry = udev_device_get_properties_list_entry(udev_device);
200 list_entry = udev_list_entry_get_by_name(list_entry, key);
201 return udev_list_entry_get_value(list_entry);
204 int udev_device_read_db(struct udev_device *udev_device)
207 char filename[UTIL_PATH_SIZE];
208 char line[UTIL_LINE_SIZE];
211 util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev_device->udev), "/.udev/db/",
212 udev_device_get_subsystem(udev_device), ":", udev_device_get_sysname(udev_device), NULL);
214 if (lstat(filename, &stats) != 0) {
215 dbg(udev_device->udev, "no db file to read %s: %m\n", filename);
218 if ((stats.st_mode & S_IFMT) == S_IFLNK) {
219 char target[UTIL_PATH_SIZE];
220 char devnode[UTIL_PATH_SIZE];
224 target_len = readlink(filename, target, sizeof(target));
225 if (target_len <= 0 || target_len == sizeof(target)) {
226 info(udev_device->udev, "error reading db link %s: %m\n", filename);
229 target[target_len] = '\0';
231 next = strchr(target, ' ');
236 util_strscpyl(devnode, sizeof(devnode), udev_get_dev_path(udev_device->udev), "/", target, NULL);
237 udev_device_set_devnode(udev_device, devnode);
238 while (next != NULL) {
239 char devlink[UTIL_PATH_SIZE];
243 next = strchr(next, ' ');
248 util_strscpyl(devlink, sizeof(devlink), udev_get_dev_path(udev_device->udev), "/", lnk, NULL);
249 udev_device_add_devlink(udev_device, devlink, 0);
251 info(udev_device->udev, "device %p filled with db symlink data '%s'\n", udev_device, udev_device->devnode);
255 f = fopen(filename, "r");
257 dbg(udev_device->udev, "error reading db file %s: %m\n", filename);
260 while (fgets(line, sizeof(line), f)) {
271 util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev_device->udev), "/", val, NULL);
272 udev_device_set_devnode(udev_device, filename);
275 util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev_device->udev), "/", val, NULL);
276 udev_device_add_devlink(udev_device, filename, 0);
279 udev_device_set_devlink_priority(udev_device, atoi(val));
282 udev_device_set_event_timeout(udev_device, atoi(val));
285 udev_device_add_property_from_string(udev_device, val);
288 udev_device_set_watch_handle(udev_device, atoi(val));
294 info(udev_device->udev, "device %p filled with db file data\n", udev_device);
298 int udev_device_read_uevent_file(struct udev_device *udev_device)
300 char filename[UTIL_PATH_SIZE];
302 char line[UTIL_LINE_SIZE];
306 util_strscpyl(filename, sizeof(filename), udev_device->syspath, "/uevent", NULL);
307 f = fopen(filename, "r");
311 while (fgets(line, sizeof(line), f)) {
314 pos = strchr(line, '\n');
319 if (strncmp(line, "DEVTYPE=", 8) == 0)
320 udev_device_set_devtype(udev_device, &line[8]);
321 else if (strncmp(line, "MAJOR=", 6) == 0)
322 maj = strtoull(&line[6], NULL, 10);
323 else if (strncmp(line, "MINOR=", 6) == 0)
324 min = strtoull(&line[6], NULL, 10);
325 else if (strncmp(line, "DEVNAME=", 8) == 0)
326 udev_device_set_knodename(udev_device, &line[8]);
328 udev_device_add_property_from_string(udev_device, line);
331 udev_device->devnum = makedev(maj, min);
337 static void device_load_info(struct udev_device *device)
339 device->info_loaded = 1;
340 udev_device_read_uevent_file(device);
341 udev_device_read_db(device);
344 void udev_device_set_info_loaded(struct udev_device *device)
346 device->info_loaded = 1;
349 struct udev_device *udev_device_new(struct udev *udev)
351 struct udev_device *udev_device;
352 struct udev_list_entry *list_entry;
357 udev_device = calloc(1, sizeof(struct udev_device));
358 if (udev_device == NULL)
360 udev_device->refcount = 1;
361 udev_device->udev = udev;
362 udev_list_init(&udev_device->devlinks_list);
363 udev_list_init(&udev_device->properties_list);
364 udev_list_init(&udev_device->sysattr_list);
365 udev_device->event_timeout = -1;
366 udev_device->watch_handle = -1;
367 /* copy global properties */
368 udev_list_entry_foreach(list_entry, udev_get_properties_list_entry(udev))
369 udev_device_add_property(udev_device,
370 udev_list_entry_get_name(list_entry),
371 udev_list_entry_get_value(list_entry));
372 dbg(udev_device->udev, "udev_device: %p created\n", udev_device);
377 * udev_device_new_from_syspath:
378 * @udev: udev library context
379 * @syspath: sys device path including sys directory
381 * Create new udev device, and fill in information from the sys
382 * device and the udev database entry. The syspath is the absolute
383 * path to the device, including the sys mount point.
385 * The initial refcount is 1, and needs to be decremented to
386 * release the resources of the udev device.
388 * Returns: a new udev device, or #NULL, if it does not exist
390 struct udev_device *udev_device_new_from_syspath(struct udev *udev, const char *syspath)
394 char path[UTIL_PATH_SIZE];
397 struct udev_device *udev_device;
404 /* path starts in sys */
405 len = strlen(udev_get_sys_path(udev));
406 if (strncmp(syspath, udev_get_sys_path(udev), len) != 0) {
407 info(udev, "not in sys :%s\n", syspath);
411 /* path is not a root directory */
412 subdir = &syspath[len+1];
413 pos = strrchr(subdir, '/');
414 if (pos == NULL || pos[1] == '\0' || pos < &subdir[2]) {
415 dbg(udev, "not a subdir :%s\n", syspath);
419 /* resolve possible symlink to real path */
420 util_strscpy(path, sizeof(path), syspath);
421 util_resolve_sys_link(udev, path, sizeof(path));
423 if (strncmp(&syspath[len], "/devices/", 9) == 0) {
424 char file[UTIL_PATH_SIZE];
426 /* all "devices" require a "uevent" file */
427 util_strscpyl(file, sizeof(file), path, "/uevent", NULL);
428 if (stat(file, &statbuf) != 0) {
429 dbg(udev, "not a device: %s\n", syspath);
433 /* everything else just needs to be a directory */
434 if (stat(path, &statbuf) != 0 || !S_ISDIR(statbuf.st_mode)) {
435 dbg(udev, "directory not found: %s\n", syspath);
440 udev_device = udev_device_new(udev);
441 if (udev_device == NULL)
444 udev_device_set_syspath(udev_device, path);
445 info(udev, "device %p has devpath '%s'\n", udev_device, udev_device_get_devpath(udev_device));
451 * udev_device_new_from_devnum:
452 * @udev: udev library context
453 * @type: char or block device
454 * @devnum: device major/minor number
456 * Create new udev device, and fill in information from the sys
457 * device and the udev database entry. The device is looked-up
458 * by its major/minor number and type. Character and block device
459 * numbers are not unique across the two types.
461 * The initial refcount is 1, and needs to be decremented to
462 * release the resources of the udev device.
464 * Returns: a new udev device, or #NULL, if it does not exist
466 struct udev_device *udev_device_new_from_devnum(struct udev *udev, char type, dev_t devnum)
468 char path[UTIL_PATH_SIZE];
469 const char *type_str;
473 else if (type == 'c')
478 /* use /sys/dev/{block,char}/<maj>:<min> link */
479 snprintf(path, sizeof(path), "%s/dev/%s/%u:%u",
480 udev_get_sys_path(udev), type_str, major(devnum), minor(devnum));
481 return udev_device_new_from_syspath(udev, path);
485 * udev_device_new_from_subsystem_sysname:
486 * @udev: udev library context
487 * @subsystem: the subsystem of the device
488 * @sysname: the name of the device
490 * Create new udev device, and fill in information from the sys device
491 * and the udev database entry. The device is looked up by the subsystem
492 * and name string of the device, like "mem" / "zero", or "block" / "sda".
494 * The initial refcount is 1, and needs to be decremented to
495 * release the resources of the udev device.
497 * Returns: a new udev device, or #NULL, if it does not exist
499 struct udev_device *udev_device_new_from_subsystem_sysname(struct udev *udev, const char *subsystem, const char *sysname)
501 char path_full[UTIL_PATH_SIZE];
507 l = util_strpcpyl(&path, sizeof(path_full), udev_get_sys_path(udev), NULL);
509 if (strcmp(subsystem, "subsystem") == 0) {
510 util_strscpyl(path, l, "/subsystem/", sysname, NULL);
511 if (stat(path_full, &statbuf) == 0)
514 util_strscpyl(path, l, "/bus/", sysname, NULL);
515 if (stat(path_full, &statbuf) == 0)
518 util_strscpyl(path, l, "/class/", sysname, NULL);
519 if (stat(path_full, &statbuf) == 0)
524 if (strcmp(subsystem, "module") == 0) {
525 util_strscpyl(path, l, "/module/", sysname, NULL);
526 if (stat(path_full, &statbuf) == 0)
531 if (strcmp(subsystem, "drivers") == 0) {
532 char subsys[UTIL_NAME_SIZE];
535 util_strscpy(subsys, sizeof(subsys), sysname);
536 driver = strchr(subsys, ':');
537 if (driver != NULL) {
541 util_strscpyl(path, l, "/subsystem/", subsys, "/drivers/", driver, NULL);
542 if (stat(path_full, &statbuf) == 0)
545 util_strscpyl(path, l, "/bus/", subsys, "/drivers/", driver, NULL);
546 if (stat(path_full, &statbuf) == 0)
552 util_strscpyl(path, l, "/subsystem/", subsystem, "/devices/", sysname, NULL);
553 if (stat(path_full, &statbuf) == 0)
556 util_strscpyl(path, l, "/bus/", subsystem, "/devices/", sysname, NULL);
557 if (stat(path_full, &statbuf) == 0)
560 util_strscpyl(path, l, "/class/", subsystem, "/", sysname, NULL);
561 if (stat(path_full, &statbuf) == 0)
566 return udev_device_new_from_syspath(udev, path_full);
570 * udev_device_new_from_environment
571 * @udev: udev library context
573 * Create new udev device, and fill in information from the
574 * current process environment. This only works reliable if
575 * the process is called from a udev rule. It is usually used
576 * for tools executed from IMPORT= rules.
578 * The initial refcount is 1, and needs to be decremented to
579 * release the resources of the udev device.
581 * Returns: a new udev device, or #NULL, if it does not exist
583 struct udev_device *udev_device_new_from_environment(struct udev *udev)
586 struct udev_device *udev_device;
588 udev_device = udev_device_new(udev);
589 if (udev_device == NULL)
591 udev_device_set_info_loaded(udev_device);
593 for (i = 0; environ[i] != NULL; i++)
594 udev_device_add_property_from_string_parse(udev_device, environ[i]);
596 if (udev_device_add_property_from_string_parse_finish(udev_device) < 0) {
597 info(udev, "missing values, invalid device\n");
598 udev_device_unref(udev_device);
605 static struct udev_device *device_new_from_parent(struct udev_device *udev_device)
607 struct udev_device *udev_device_parent = NULL;
608 char path[UTIL_PATH_SIZE];
611 util_strscpy(path, sizeof(path), udev_device->syspath);
612 subdir = &path[strlen(udev_get_sys_path(udev_device->udev))+1];
616 pos = strrchr(subdir, '/');
617 if (pos == NULL || pos < &subdir[2])
620 udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
621 if (udev_device_parent != NULL)
622 return udev_device_parent;
628 * udev_device_get_parent:
629 * @udev_device: the device to start searching from
631 * Find the next parent device, and fill in information from the sys
632 * device and the udev database entry.
634 * The returned the device is not referenced. It is attached to the
635 * child device, and will be cleaned up when the child device
638 * It is not necessarily just the upper level directory, empty or not
639 * recognized sys directories are ignored.
641 * It can be called as many times as needed, without caring about
644 * Returns: a new udev device, or #NULL, if it no parent exist.
646 struct udev_device *udev_device_get_parent(struct udev_device *udev_device)
648 if (udev_device == NULL)
650 if (!udev_device->parent_set) {
651 udev_device->parent_set = 1;
652 udev_device->parent_device = device_new_from_parent(udev_device);
654 if (udev_device->parent_device != NULL)
655 dbg(udev_device->udev, "returning existing parent %p\n", udev_device->parent_device);
656 return udev_device->parent_device;
660 * udev_device_get_parent_with_subsystem_devtype:
661 * @udev_device: udev device to start searching from
662 * @subsystem: the subsystem of the device
663 * @devtype: the type (DEVTYPE) of the device
665 * Find the next parent device, with a matching subsystem and devtype
666 * value, and fill in information from the sys device and the udev
669 * If devtype is #NULL, only subsystem is checked, and any devtype will
672 * The returned the device is not referenced. It is attached to the
673 * child device, and will be cleaned up when the child device
676 * It can be called as many times as needed, without caring about
679 * Returns: a new udev device, or #NULL if no matching parent exists.
681 struct udev_device *udev_device_get_parent_with_subsystem_devtype(struct udev_device *udev_device, const char *subsystem, const char *devtype)
683 struct udev_device *parent;
685 if (subsystem == NULL)
688 parent = udev_device_get_parent(udev_device);
689 while (parent != NULL) {
690 const char *parent_subsystem;
691 const char *parent_devtype;
693 parent_subsystem = udev_device_get_subsystem(parent);
694 if (parent_subsystem != NULL && strcmp(parent_subsystem, subsystem) == 0) {
697 parent_devtype = udev_device_get_devtype(parent);
698 if (parent_devtype != NULL && strcmp(parent_devtype, devtype) == 0)
701 parent = udev_device_get_parent(parent);
707 * udev_device_get_udev:
708 * @udev_device: udev device
710 * Retrieve the udev library context the device was created with.
712 * Returns: the udev library context
714 struct udev *udev_device_get_udev(struct udev_device *udev_device)
716 if (udev_device == NULL)
718 return udev_device->udev;
723 * @udev_device: udev device
725 * Take a reference of a udev device.
727 * Returns: the passed udev device
729 struct udev_device *udev_device_ref(struct udev_device *udev_device)
731 if (udev_device == NULL)
733 udev_device->refcount++;
739 * @udev_device: udev device
741 * Drop a reference of a udev device. If the refcount reaches zero,
742 * the resources of the device will be released.
745 void udev_device_unref(struct udev_device *udev_device)
747 if (udev_device == NULL)
749 udev_device->refcount--;
750 if (udev_device->refcount > 0)
752 if (udev_device->parent_device != NULL)
753 udev_device_unref(udev_device->parent_device);
754 free(udev_device->syspath);
755 free(udev_device->sysname);
756 free(udev_device->devnode);
757 free(udev_device->subsystem);
758 free(udev_device->devtype);
759 udev_list_cleanup_entries(udev_device->udev, &udev_device->devlinks_list);
760 udev_list_cleanup_entries(udev_device->udev, &udev_device->properties_list);
761 free(udev_device->action);
762 free(udev_device->driver);
763 free(udev_device->devpath_old);
764 free(udev_device->sysname_old);
765 free(udev_device->knodename);
766 udev_list_cleanup_entries(udev_device->udev, &udev_device->sysattr_list);
767 free(udev_device->envp);
768 free(udev_device->monitor_buf);
769 dbg(udev_device->udev, "udev_device: %p released\n", udev_device);
774 * udev_device_get_devpath:
775 * @udev_device: udev device
777 * Retrieve the kernel devpath value of the udev device. The path
778 * does not contain the sys mount point, and starts with a '/'.
780 * Returns: the devpath of the udev device
782 const char *udev_device_get_devpath(struct udev_device *udev_device)
784 if (udev_device == NULL)
786 return udev_device->devpath;
790 * udev_device_get_syspath:
791 * @udev_device: udev device
793 * Retrieve the sys path of the udev device. The path is an
794 * absolute path and starts with the sys mount point.
796 * Returns: the sys path of the udev device
798 const char *udev_device_get_syspath(struct udev_device *udev_device)
800 if (udev_device == NULL)
802 return udev_device->syspath;
806 * udev_device_get_sysname:
807 * @udev_device: udev device
809 * Returns: the sys name of the device device
811 const char *udev_device_get_sysname(struct udev_device *udev_device)
813 if (udev_device == NULL)
815 return udev_device->sysname;
819 * udev_device_get_sysnum:
820 * @udev_device: udev device
822 * Returns: the trailing number of of the device name
824 const char *udev_device_get_sysnum(struct udev_device *udev_device)
826 if (udev_device == NULL)
828 return udev_device->sysnum;
832 * udev_device_get_devnode:
833 * @udev_device: udev device
835 * Retrieve the device node file name belonging to the udev device.
836 * The path is an absolute path, and starts with the device directory.
838 * Returns: the device node file name of the udev device, or #NULL if no device node exists
840 const char *udev_device_get_devnode(struct udev_device *udev_device)
842 if (udev_device == NULL)
844 if (!udev_device->info_loaded)
845 device_load_info(udev_device);
846 return udev_device->devnode;
850 * udev_device_get_subsystem:
851 * @udev_device: udev device
853 * Retrieve the subsystem string of the udev device. The string does not
856 * Returns: the subsystem name of the udev device, or #NULL if it can not be determined
858 const char *udev_device_get_subsystem(struct udev_device *udev_device)
860 char subsystem[UTIL_NAME_SIZE];
862 if (udev_device == NULL)
864 if (!udev_device->subsystem_set) {
865 udev_device->subsystem_set = 1;
866 /* read "subsystem" link */
867 if (util_get_sys_subsystem(udev_device->udev, udev_device->syspath, subsystem, sizeof(subsystem)) > 0) {
868 udev_device_set_subsystem(udev_device, subsystem);
869 return udev_device->subsystem;
872 if (strncmp(udev_device->devpath, "/module/", 8) == 0) {
873 udev_device_set_subsystem(udev_device, "module");
874 return udev_device->subsystem;
876 if (strstr(udev_device->devpath, "/drivers/") != NULL) {
877 udev_device_set_subsystem(udev_device, "drivers");
878 return udev_device->subsystem;
880 if (strncmp(udev_device->devpath, "/subsystem/", 11) == 0 ||
881 strncmp(udev_device->devpath, "/class/", 7) == 0 ||
882 strncmp(udev_device->devpath, "/bus/", 5) == 0) {
883 udev_device_set_subsystem(udev_device, "subsystem");
884 return udev_device->subsystem;
887 return udev_device->subsystem;
891 * udev_device_get_devtype:
892 * @udev_device: udev device
894 * Retrieve the devtype string of the udev device.
896 * Returns: the devtype name of the udev device, or #NULL if it can not be determined
898 const char *udev_device_get_devtype(struct udev_device *udev_device)
900 if (udev_device == NULL)
902 if (!udev_device->devtype_set) {
903 udev_device->devtype_set = 1;
904 if (!udev_device->info_loaded)
905 udev_device_read_uevent_file(udev_device);
907 return udev_device->devtype;
911 * udev_device_get_devlinks_list_entry:
912 * @udev_device: udev device
914 * Retrieve the list of device links pointing to the device file of
915 * the udev device. The next list entry can be retrieved with
916 * udev_list_entry_next(), which returns #NULL if no more entries exist.
917 * The devlink path can be retrieved from the list entry by
918 * udev_list_entry_get_name(). The path is an absolute path, and starts with
919 * the device directory.
921 * Returns: the first entry of the device node link list
923 struct udev_list_entry *udev_device_get_devlinks_list_entry(struct udev_device *udev_device)
925 if (udev_device == NULL)
927 if (!udev_device->info_loaded)
928 device_load_info(udev_device);
929 return udev_list_get_entry(&udev_device->devlinks_list);
932 void udev_device_cleanup_devlinks_list(struct udev_device *udev_device)
934 udev_device->devlinks_uptodate = 0;
935 udev_list_cleanup_entries(udev_device->udev, &udev_device->devlinks_list);
939 * udev_device_get_properties_list_entry:
940 * @udev_device: udev device
942 * Retrieve the list of key/value device properties of the udev
943 * device. The next list entry can be retrieved with udev_list_entry_next(),
944 * which returns #NULL if no more entries exist. The property name
945 * can be retrieved from the list entry by udev_list_get_name(),
946 * the property value by udev_list_get_value().
948 * Returns: the first entry of the property list
950 struct udev_list_entry *udev_device_get_properties_list_entry(struct udev_device *udev_device)
952 if (udev_device == NULL)
954 if (!udev_device->info_loaded)
955 device_load_info(udev_device);
956 if (!udev_device->devlinks_uptodate) {
957 char symlinks[UTIL_PATH_SIZE];
958 struct udev_list_entry *list_entry;
960 udev_device->devlinks_uptodate = 1;
961 list_entry = udev_device_get_devlinks_list_entry(udev_device);
962 if (list_entry != NULL) {
967 l = util_strpcpyl(&s, sizeof(symlinks), udev_list_entry_get_name(list_entry), NULL);
968 udev_list_entry_foreach(list_entry, udev_list_entry_get_next(list_entry))
969 l = util_strpcpyl(&s, l, " ", udev_list_entry_get_name(list_entry), NULL);
970 udev_device_add_property(udev_device, "DEVLINKS", symlinks);
973 return udev_list_get_entry(&udev_device->properties_list);
977 * udev_device_get_driver:
978 * @udev_device: udev device
980 * Returns: the driver string, or #NULL if there is no driver attached.
982 const char *udev_device_get_driver(struct udev_device *udev_device)
984 char driver[UTIL_NAME_SIZE];
986 if (udev_device == NULL)
988 if (!udev_device->driver_set) {
989 udev_device->driver_set = 1;
990 if (util_get_sys_driver(udev_device->udev, udev_device->syspath, driver, sizeof(driver)) > 0)
991 udev_device->driver = strdup(driver);
993 return udev_device->driver;
997 * udev_device_get_devnum:
998 * @udev_device: udev device
1000 * Returns: the device major/minor number.
1002 dev_t udev_device_get_devnum(struct udev_device *udev_device)
1004 if (udev_device == NULL)
1005 return makedev(0, 0);
1006 if (!udev_device->info_loaded)
1007 device_load_info(udev_device);
1008 return udev_device->devnum;
1012 * udev_device_get_action:
1013 * @udev_device: udev device
1015 * This is only valid if the device was received through a monitor. Devices read from
1016 * sys do not have an action string. Usual actions are: add, remove, change, online,
1019 * Returns: the kernel action value, or #NULL if there is no action value available.
1021 const char *udev_device_get_action(struct udev_device *udev_device)
1023 if (udev_device == NULL)
1025 return udev_device->action;
1029 * udev_device_get_devnum:
1030 * @udev_device: udev device
1032 * This is only valid if the device was received through a monitor. Devices read from
1033 * sys do not have a sequence number.
1035 * Returns: the kernel event sequence number, or 0 if there is no sequence number available.
1037 unsigned long long int udev_device_get_seqnum(struct udev_device *udev_device)
1039 if (udev_device == NULL)
1041 return udev_device->seqnum;
1045 * udev_device_get_sysattr_value:
1046 * @udev_device: udev device
1047 * @sysattr: attribute name
1049 * The retrieved value is cached in the device. Repeated calls will return the same
1050 * value and not open the attribute again.
1052 * Returns: the content of a sys attribute file, or #NULL if there is no sys attribute value.
1054 const char *udev_device_get_sysattr_value(struct udev_device *udev_device, const char *sysattr)
1056 struct udev_list_entry *list_entry;
1057 char path[UTIL_PATH_SIZE];
1059 struct stat statbuf;
1062 const char *val = NULL;
1064 if (udev_device == NULL)
1066 if (sysattr == NULL)
1069 /* look for possibly already cached result */
1070 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_device->sysattr_list)) {
1071 if (strcmp(udev_list_entry_get_name(list_entry), sysattr) == 0) {
1072 dbg(udev_device->udev, "got '%s' (%s) from cache\n",
1073 sysattr, udev_list_entry_get_value(list_entry));
1074 return udev_list_entry_get_value(list_entry);
1078 util_strscpyl(path, sizeof(path), udev_device_get_syspath(udev_device), "/", sysattr, NULL);
1079 if (lstat(path, &statbuf) != 0) {
1080 dbg(udev_device->udev, "no attribute '%s', keep negative entry\n", path);
1081 udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, NULL, 0, 0);
1085 if (S_ISLNK(statbuf.st_mode)) {
1086 char target[UTIL_NAME_SIZE];
1090 /* some core links return the last element of the target path */
1091 if (strcmp(sysattr, "driver") != 0 &&
1092 strcmp(sysattr, "subsystem") != 0 &&
1093 strcmp(sysattr, "module") != 0)
1096 len = readlink(path, target, sizeof(target));
1097 if (len <= 0 || len == sizeof(target))
1101 pos = strrchr(target, '/');
1104 dbg(udev_device->udev, "cache '%s' with link value '%s'\n", sysattr, pos);
1105 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, pos, 0, 0);
1106 val = udev_list_entry_get_value(list_entry);
1112 /* skip directories */
1113 if (S_ISDIR(statbuf.st_mode))
1116 /* skip non-readable files */
1117 if ((statbuf.st_mode & S_IRUSR) == 0)
1120 /* read attribute value */
1121 fd = open(path, O_RDONLY);
1123 dbg(udev_device->udev, "attribute '%s' can not be opened\n", path);
1126 size = read(fd, value, sizeof(value));
1130 if (size == sizeof(value))
1133 /* got a valid value, store it in cache and return it */
1135 util_remove_trailing_chars(value, '\n');
1136 dbg(udev_device->udev, "'%s' has attribute value '%s'\n", path, value);
1137 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, value, 0, 0);
1138 val = udev_list_entry_get_value(list_entry);
1143 int udev_device_set_syspath(struct udev_device *udev_device, const char *syspath)
1148 free(udev_device->syspath);
1149 udev_device->syspath = strdup(syspath);
1150 if (udev_device->syspath == NULL)
1152 udev_device->devpath = &udev_device->syspath[strlen(udev_get_sys_path(udev_device->udev))];
1153 udev_device_add_property(udev_device, "DEVPATH", udev_device->devpath);
1155 pos = strrchr(udev_device->syspath, '/');
1158 udev_device->sysname = strdup(&pos[1]);
1159 if (udev_device->sysname == NULL)
1162 /* some devices have '!' in their name, change that to '/' */
1164 while (udev_device->sysname[len] != '\0') {
1165 if (udev_device->sysname[len] == '!')
1166 udev_device->sysname[len] = '/';
1170 /* trailing number */
1171 while (len > 0 && isdigit(udev_device->sysname[--len]))
1172 udev_device->sysnum = &udev_device->sysname[len];
1174 /* sysname is completely numeric */
1176 udev_device->sysnum = NULL;
1181 int udev_device_set_subsystem(struct udev_device *udev_device, const char *subsystem)
1183 free(udev_device->subsystem);
1184 udev_device->subsystem = strdup(subsystem);
1185 if (udev_device->subsystem == NULL)
1187 udev_device->subsystem_set = 1;
1188 udev_device_add_property(udev_device, "SUBSYSTEM", udev_device->subsystem);
1192 int udev_device_set_devtype(struct udev_device *udev_device, const char *devtype)
1194 free(udev_device->devtype);
1195 udev_device->devtype = strdup(devtype);
1196 if (udev_device->devtype == NULL)
1198 udev_device->devtype_set = 1;
1199 udev_device_add_property(udev_device, "DEVTYPE", udev_device->devtype);
1203 int udev_device_set_devnode(struct udev_device *udev_device, const char *devnode)
1205 free(udev_device->devnode);
1206 udev_device->devnode = strdup(devnode);
1207 if (devnode == NULL)
1209 if (udev_device->devnode == NULL)
1211 udev_device_add_property(udev_device, "DEVNAME", udev_device->devnode);
1215 int udev_device_add_devlink(struct udev_device *udev_device, const char *devlink, int unique)
1217 struct udev_list_entry *list_entry;
1219 udev_device->devlinks_uptodate = 0;
1220 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->devlinks_list, devlink, NULL, 1, 0);
1221 if (list_entry == NULL)
1224 udev_list_entry_set_flags(list_entry, 1);
1228 #define ENVP_SIZE 128
1229 #define MONITOR_BUF_SIZE 4096
1230 static int update_envp_monitor_buf(struct udev_device *udev_device)
1232 struct udev_list_entry *list_entry;
1237 /* monitor buffer of property strings */
1238 free(udev_device->monitor_buf);
1239 udev_device->monitor_buf_len = 0;
1240 udev_device->monitor_buf = malloc(MONITOR_BUF_SIZE);
1241 if (udev_device->monitor_buf == NULL)
1244 /* envp array, strings will point into monitor buffer */
1245 if (udev_device->envp == NULL)
1246 udev_device->envp = malloc(sizeof(char *) * ENVP_SIZE);
1247 if (udev_device->envp == NULL)
1251 s = udev_device->monitor_buf;
1252 l = MONITOR_BUF_SIZE;
1253 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device)) {
1256 key = udev_list_entry_get_name(list_entry);
1257 /* skip private variables */
1261 /* add string to envp array */
1262 udev_device->envp[i++] = s;
1263 if (i+1 >= ENVP_SIZE)
1266 /* add property string to monitor buffer */
1267 l = util_strpcpyl(&s, l, key, "=", udev_list_entry_get_value(list_entry), NULL);
1270 /* advance past the trailing '\0' that util_strpcpyl() guarantees */
1274 udev_device->envp[i] = NULL;
1275 udev_device->monitor_buf_len = s - udev_device->monitor_buf;
1276 udev_device->envp_uptodate = 1;
1277 dbg(udev_device->udev, "filled envp/monitor buffer, %u properties, %zu bytes\n",
1278 i, udev_device->monitor_buf_len);
1282 char **udev_device_get_properties_envp(struct udev_device *udev_device)
1284 if (!udev_device->envp_uptodate)
1285 if (update_envp_monitor_buf(udev_device) != 0)
1287 return udev_device->envp;
1290 ssize_t udev_device_get_properties_monitor_buf(struct udev_device *udev_device, const char **buf)
1292 if (!udev_device->envp_uptodate)
1293 if (update_envp_monitor_buf(udev_device) != 0)
1295 *buf = udev_device->monitor_buf;
1296 return udev_device->monitor_buf_len;
1299 int udev_device_set_action(struct udev_device *udev_device, const char *action)
1301 free(udev_device->action);
1302 udev_device->action = strdup(action);
1303 if (udev_device->action == NULL)
1305 udev_device_add_property(udev_device, "ACTION", udev_device->action);
1309 int udev_device_set_driver(struct udev_device *udev_device, const char *driver)
1311 free(udev_device->driver);
1312 udev_device->driver = strdup(driver);
1313 if (udev_device->driver == NULL)
1315 udev_device->driver_set = 1;
1316 udev_device_add_property(udev_device, "DRIVER", udev_device->driver);
1320 const char *udev_device_get_devpath_old(struct udev_device *udev_device)
1322 return udev_device->devpath_old;
1325 int udev_device_set_devpath_old(struct udev_device *udev_device, const char *devpath_old)
1330 free(udev_device->devpath_old);
1331 udev_device->devpath_old = strdup(devpath_old);
1332 if (udev_device->devpath_old == NULL)
1334 udev_device_add_property(udev_device, "DEVPATH_OLD", udev_device->devpath_old);
1336 pos = strrchr(udev_device->devpath_old, '/');
1339 udev_device->sysname_old = strdup(&pos[1]);
1340 if (udev_device->sysname_old == NULL)
1343 /* some devices have '!' in their name, change that to '/' */
1345 while (udev_device->sysname_old[len] != '\0') {
1346 if (udev_device->sysname_old[len] == '!')
1347 udev_device->sysname_old[len] = '/';
1353 const char *udev_device_get_sysname_old(struct udev_device *udev_device)
1355 if (udev_device == NULL)
1357 return udev_device->sysname_old;
1360 const char *udev_device_get_knodename(struct udev_device *udev_device)
1362 return udev_device->knodename;
1365 int udev_device_set_knodename(struct udev_device *udev_device, const char *knodename)
1367 free(udev_device->knodename);
1368 udev_device->knodename = strdup(knodename);
1369 if (udev_device->knodename == NULL)
1371 udev_device_add_property(udev_device, "DEVNAME", udev_device->knodename);
1375 int udev_device_get_timeout(struct udev_device *udev_device)
1377 return udev_device->timeout;
1380 int udev_device_set_timeout(struct udev_device *udev_device, int timeout)
1382 udev_device->timeout = timeout;
1385 int udev_device_get_event_timeout(struct udev_device *udev_device)
1387 if (!udev_device->info_loaded)
1388 device_load_info(udev_device);
1389 return udev_device->event_timeout;
1392 int udev_device_set_event_timeout(struct udev_device *udev_device, int event_timeout)
1394 udev_device->event_timeout = event_timeout;
1398 int udev_device_set_seqnum(struct udev_device *udev_device, unsigned long long int seqnum)
1402 udev_device->seqnum = seqnum;
1403 snprintf(num, sizeof(num), "%llu", seqnum);
1404 udev_device_add_property(udev_device, "SEQNUM", num);
1408 int udev_device_set_devnum(struct udev_device *udev_device, dev_t devnum)
1412 udev_device->devnum = devnum;
1414 snprintf(num, sizeof(num), "%u", major(devnum));
1415 udev_device_add_property(udev_device, "MAJOR", num);
1416 snprintf(num, sizeof(num), "%u", minor(devnum));
1417 udev_device_add_property(udev_device, "MINOR", num);
1421 int udev_device_get_devlink_priority(struct udev_device *udev_device)
1423 if (!udev_device->info_loaded)
1424 device_load_info(udev_device);
1425 return udev_device->devlink_priority;
1428 int udev_device_set_devlink_priority(struct udev_device *udev_device, int prio)
1430 udev_device->devlink_priority = prio;
1434 int udev_device_get_watch_handle(struct udev_device *udev_device)
1436 if (!udev_device->info_loaded)
1437 device_load_info(udev_device);
1438 return udev_device->watch_handle;
1441 int udev_device_set_watch_handle(struct udev_device *udev_device, int handle)
1443 udev_device->watch_handle = handle;