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 struct udev_list_node tags_list;
64 unsigned long long int seqnum;
76 bool devlinks_uptodate;
85 struct udev_list_entry *udev_device_add_property(struct udev_device *udev_device, const char *key, const char *value)
87 udev_device->envp_uptodate = false;
89 struct udev_list_entry *list_entry;
91 list_entry = udev_device_get_properties_list_entry(udev_device);
92 list_entry = udev_list_entry_get_by_name(list_entry, key);
93 if (list_entry != NULL)
94 udev_list_entry_delete(list_entry);
97 return udev_list_entry_add(udev_device->udev, &udev_device->properties_list, key, value, 1, 0);
100 static struct udev_list_entry *udev_device_add_property_from_string(struct udev_device *udev_device, const char *property)
102 char name[UTIL_LINE_SIZE];
105 util_strscpy(name, sizeof(name), property);
106 val = strchr(name, '=');
113 return udev_device_add_property(udev_device, name, val);
117 * parse property string, and if needed, update internal values accordingly
119 * udev_device_add_property_from_string_parse_finish() needs to be
120 * called after adding properties, and its return value checked
122 * udev_device_set_info_loaded() needs to be set, to avoid trying
123 * to use a device without a DEVPATH set
125 void udev_device_add_property_from_string_parse(struct udev_device *udev_device, const char *property)
127 if (strncmp(property, "DEVPATH=", 8) == 0) {
128 char path[UTIL_PATH_SIZE];
130 util_strscpyl(path, sizeof(path), udev_get_sys_path(udev_device->udev), &property[8], NULL);
131 udev_device_set_syspath(udev_device, path);
132 } else if (strncmp(property, "SUBSYSTEM=", 10) == 0) {
133 udev_device_set_subsystem(udev_device, &property[10]);
134 } else if (strncmp(property, "DEVTYPE=", 8) == 0) {
135 udev_device_set_devtype(udev_device, &property[8]);
136 } else if (strncmp(property, "DEVNAME=", 8) == 0) {
137 if (property[8] == '/')
138 udev_device_set_devnode(udev_device, &property[8]);
140 udev_device_set_knodename(udev_device, &property[8]);
141 } else if (strncmp(property, "DEVLINKS=", 9) == 0) {
142 char devlinks[UTIL_PATH_SIZE];
146 util_strscpy(devlinks, sizeof(devlinks), &property[9]);
148 next = strchr(slink, ' ');
149 while (next != NULL) {
151 udev_device_add_devlink(udev_device, slink, 0);
153 next = strchr(slink, ' ');
155 if (slink[0] != '\0')
156 udev_device_add_devlink(udev_device, slink, 0);
157 } else if (strncmp(property, "TAGS=", 5) == 0) {
158 char tags[UTIL_PATH_SIZE];
161 util_strscpy(tags, sizeof(tags), &property[5]);
162 next = strchr(tags, ':');
165 while (next[0] != '\0') {
169 next = strchr(tag, ':');
174 udev_device_add_tag(udev_device, tag);
177 } else if (strncmp(property, "DRIVER=", 7) == 0) {
178 udev_device_set_driver(udev_device, &property[7]);
179 } else if (strncmp(property, "ACTION=", 7) == 0) {
180 udev_device_set_action(udev_device, &property[7]);
181 } else if (strncmp(property, "MAJOR=", 6) == 0) {
182 udev_device->maj = strtoull(&property[6], NULL, 10);
183 } else if (strncmp(property, "MINOR=", 6) == 0) {
184 udev_device->min = strtoull(&property[6], NULL, 10);
185 } else if (strncmp(property, "DEVPATH_OLD=", 12) == 0) {
186 udev_device_set_devpath_old(udev_device, &property[12]);
187 } else if (strncmp(property, "SEQNUM=", 7) == 0) {
188 udev_device_set_seqnum(udev_device, strtoull(&property[7], NULL, 10));
189 } else if (strncmp(property, "TIMEOUT=", 8) == 0) {
190 udev_device_set_timeout(udev_device, strtoull(&property[8], NULL, 10));
191 } else if (strncmp(property, "IFINDEX=", 8) == 0) {
192 udev_device_set_ifindex(udev_device, strtoull(&property[8], NULL, 10));
194 udev_device_add_property_from_string(udev_device, property);
198 int udev_device_add_property_from_string_parse_finish(struct udev_device *udev_device)
200 if (udev_device->maj > 0)
201 udev_device_set_devnum(udev_device, makedev(udev_device->maj, udev_device->min));
202 udev_device->maj = 0;
203 udev_device->min = 0;
205 if (udev_device->devpath == NULL || udev_device->subsystem == NULL)
211 * udev_device_get_property_value:
212 * @udev_device: udev device
213 * @key: property name
215 * Returns: the value of a device property, or #NULL if there is no such property.
217 const char *udev_device_get_property_value(struct udev_device *udev_device, const char *key)
219 struct udev_list_entry *list_entry;
221 if (udev_device == NULL)
226 list_entry = udev_device_get_properties_list_entry(udev_device);
227 list_entry = udev_list_entry_get_by_name(list_entry, key);
228 return udev_list_entry_get_value(list_entry);
231 int udev_device_read_db(struct udev_device *udev_device)
234 char filename[UTIL_PATH_SIZE];
235 char line[UTIL_LINE_SIZE];
238 if (udev_device->db_loaded)
241 util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev_device->udev), "/.udev/db/",
242 udev_device_get_subsystem(udev_device), ":", udev_device_get_sysname(udev_device), NULL);
244 if (lstat(filename, &stats) != 0) {
245 dbg(udev_device->udev, "no db file to read %s: %m\n", filename);
248 if ((stats.st_mode & S_IFMT) == S_IFLNK) {
249 char target[UTIL_PATH_SIZE];
250 char devnode[UTIL_PATH_SIZE];
254 target_len = readlink(filename, target, sizeof(target));
255 if (target_len <= 0 || target_len == sizeof(target)) {
256 info(udev_device->udev, "error reading db link %s: %m\n", filename);
259 target[target_len] = '\0';
261 next = strchr(target, ' ');
266 util_strscpyl(devnode, sizeof(devnode), udev_get_dev_path(udev_device->udev), "/", target, NULL);
267 udev_device_set_devnode(udev_device, devnode);
268 while (next != NULL) {
269 char devlink[UTIL_PATH_SIZE];
273 next = strchr(next, ' ');
278 util_strscpyl(devlink, sizeof(devlink), udev_get_dev_path(udev_device->udev), "/", lnk, NULL);
279 udev_device_add_devlink(udev_device, devlink, 0);
281 info(udev_device->udev, "device %p filled with db symlink data '%s'\n", udev_device, udev_device->devnode);
285 f = fopen(filename, "re");
287 dbg(udev_device->udev, "error reading db file %s: %m\n", filename);
290 udev_device->db_loaded = true;
292 while (fgets(line, sizeof(line), f)) {
303 util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev_device->udev), "/", val, NULL);
304 udev_device_set_devnode(udev_device, filename);
307 util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev_device->udev), "/", val, NULL);
308 udev_device_add_devlink(udev_device, filename, 0);
311 udev_device_set_devlink_priority(udev_device, atoi(val));
314 udev_device_set_event_timeout(udev_device, atoi(val));
317 udev_device_add_property_from_string(udev_device, val);
320 udev_device_add_tag(udev_device, val);
323 udev_device_set_watch_handle(udev_device, atoi(val));
329 info(udev_device->udev, "device %p filled with db file data\n", udev_device);
333 int udev_device_read_uevent_file(struct udev_device *udev_device)
335 char filename[UTIL_PATH_SIZE];
337 char line[UTIL_LINE_SIZE];
341 if (udev_device->uevent_loaded)
344 util_strscpyl(filename, sizeof(filename), udev_device->syspath, "/uevent", NULL);
345 f = fopen(filename, "re");
348 udev_device->uevent_loaded = true;
350 while (fgets(line, sizeof(line), f)) {
353 pos = strchr(line, '\n');
358 if (strncmp(line, "DEVTYPE=", 8) == 0)
359 udev_device_set_devtype(udev_device, &line[8]);
360 else if (strncmp(line, "MAJOR=", 6) == 0)
361 maj = strtoull(&line[6], NULL, 10);
362 else if (strncmp(line, "MINOR=", 6) == 0)
363 min = strtoull(&line[6], NULL, 10);
364 else if (strncmp(line, "IFINDEX=", 8) == 0)
365 udev_device_set_ifindex(udev_device, strtoull(&line[8], NULL, 10));
366 else if (strncmp(line, "DEVNAME=", 8) == 0)
367 udev_device_set_knodename(udev_device, &line[8]);
369 udev_device_add_property_from_string(udev_device, line);
372 udev_device->devnum = makedev(maj, min);
377 void udev_device_set_info_loaded(struct udev_device *device)
379 device->info_loaded = true;
382 struct udev_device *udev_device_new(struct udev *udev)
384 struct udev_device *udev_device;
385 struct udev_list_entry *list_entry;
390 udev_device = calloc(1, sizeof(struct udev_device));
391 if (udev_device == NULL)
393 udev_device->refcount = 1;
394 udev_device->udev = udev;
395 udev_list_init(&udev_device->devlinks_list);
396 udev_list_init(&udev_device->properties_list);
397 udev_list_init(&udev_device->sysattr_list);
398 udev_list_init(&udev_device->tags_list);
399 udev_device->event_timeout = -1;
400 udev_device->watch_handle = -1;
401 /* copy global properties */
402 udev_list_entry_foreach(list_entry, udev_get_properties_list_entry(udev))
403 udev_device_add_property(udev_device,
404 udev_list_entry_get_name(list_entry),
405 udev_list_entry_get_value(list_entry));
406 dbg(udev_device->udev, "udev_device: %p created\n", udev_device);
411 * udev_device_new_from_syspath:
412 * @udev: udev library context
413 * @syspath: sys device path including sys directory
415 * Create new udev device, and fill in information from the sys
416 * device and the udev database entry. The syspath is the absolute
417 * path to the device, including the sys mount point.
419 * The initial refcount is 1, and needs to be decremented to
420 * release the resources of the udev device.
422 * Returns: a new udev device, or #NULL, if it does not exist
424 struct udev_device *udev_device_new_from_syspath(struct udev *udev, const char *syspath)
428 char path[UTIL_PATH_SIZE];
431 struct udev_device *udev_device;
438 /* path starts in sys */
439 len = strlen(udev_get_sys_path(udev));
440 if (strncmp(syspath, udev_get_sys_path(udev), len) != 0) {
441 info(udev, "not in sys :%s\n", syspath);
445 /* path is not a root directory */
446 subdir = &syspath[len+1];
447 pos = strrchr(subdir, '/');
448 if (pos == NULL || pos[1] == '\0' || pos < &subdir[2]) {
449 dbg(udev, "not a subdir :%s\n", syspath);
453 /* resolve possible symlink to real path */
454 util_strscpy(path, sizeof(path), syspath);
455 util_resolve_sys_link(udev, path, sizeof(path));
457 if (strncmp(&path[len], "/devices/", 9) == 0) {
458 char file[UTIL_PATH_SIZE];
460 /* all "devices" require a "uevent" file */
461 util_strscpyl(file, sizeof(file), path, "/uevent", NULL);
462 if (stat(file, &statbuf) != 0) {
463 dbg(udev, "not a device: %s\n", syspath);
467 /* everything else just needs to be a directory */
468 if (stat(path, &statbuf) != 0 || !S_ISDIR(statbuf.st_mode)) {
469 dbg(udev, "directory not found: %s\n", syspath);
474 udev_device = udev_device_new(udev);
475 if (udev_device == NULL)
478 udev_device_set_syspath(udev_device, path);
479 info(udev, "device %p has devpath '%s'\n", udev_device, udev_device_get_devpath(udev_device));
485 * udev_device_new_from_devnum:
486 * @udev: udev library context
487 * @type: char or block device
488 * @devnum: device major/minor number
490 * Create new udev device, and fill in information from the sys
491 * device and the udev database entry. The device is looked-up
492 * by its major/minor number and type. Character and block device
493 * numbers are not unique across the two types.
495 * The initial refcount is 1, and needs to be decremented to
496 * release the resources of the udev device.
498 * Returns: a new udev device, or #NULL, if it does not exist
500 struct udev_device *udev_device_new_from_devnum(struct udev *udev, char type, dev_t devnum)
502 char path[UTIL_PATH_SIZE];
503 const char *type_str;
507 else if (type == 'c')
512 /* use /sys/dev/{block,char}/<maj>:<min> link */
513 snprintf(path, sizeof(path), "%s/dev/%s/%u:%u",
514 udev_get_sys_path(udev), type_str, major(devnum), minor(devnum));
515 return udev_device_new_from_syspath(udev, path);
519 * udev_device_new_from_subsystem_sysname:
520 * @udev: udev library context
521 * @subsystem: the subsystem of the device
522 * @sysname: the name of the device
524 * Create new udev device, and fill in information from the sys device
525 * and the udev database entry. The device is looked up by the subsystem
526 * and name string of the device, like "mem" / "zero", or "block" / "sda".
528 * The initial refcount is 1, and needs to be decremented to
529 * release the resources of the udev device.
531 * Returns: a new udev device, or #NULL, if it does not exist
533 struct udev_device *udev_device_new_from_subsystem_sysname(struct udev *udev, const char *subsystem, const char *sysname)
535 char path_full[UTIL_PATH_SIZE];
541 l = util_strpcpyl(&path, sizeof(path_full), udev_get_sys_path(udev), NULL);
543 if (strcmp(subsystem, "subsystem") == 0) {
544 util_strscpyl(path, l, "/subsystem/", sysname, NULL);
545 if (stat(path_full, &statbuf) == 0)
548 util_strscpyl(path, l, "/bus/", sysname, NULL);
549 if (stat(path_full, &statbuf) == 0)
552 util_strscpyl(path, l, "/class/", sysname, NULL);
553 if (stat(path_full, &statbuf) == 0)
558 if (strcmp(subsystem, "module") == 0) {
559 util_strscpyl(path, l, "/module/", sysname, NULL);
560 if (stat(path_full, &statbuf) == 0)
565 if (strcmp(subsystem, "drivers") == 0) {
566 char subsys[UTIL_NAME_SIZE];
569 util_strscpy(subsys, sizeof(subsys), sysname);
570 driver = strchr(subsys, ':');
571 if (driver != NULL) {
575 util_strscpyl(path, l, "/subsystem/", subsys, "/drivers/", driver, NULL);
576 if (stat(path_full, &statbuf) == 0)
579 util_strscpyl(path, l, "/bus/", subsys, "/drivers/", driver, NULL);
580 if (stat(path_full, &statbuf) == 0)
586 util_strscpyl(path, l, "/subsystem/", subsystem, "/devices/", sysname, NULL);
587 if (stat(path_full, &statbuf) == 0)
590 util_strscpyl(path, l, "/bus/", subsystem, "/devices/", sysname, NULL);
591 if (stat(path_full, &statbuf) == 0)
594 util_strscpyl(path, l, "/class/", subsystem, "/", sysname, NULL);
595 if (stat(path_full, &statbuf) == 0)
600 return udev_device_new_from_syspath(udev, path_full);
604 * udev_device_new_from_environment
605 * @udev: udev library context
607 * Create new udev device, and fill in information from the
608 * current process environment. This only works reliable if
609 * the process is called from a udev rule. It is usually used
610 * for tools executed from IMPORT= rules.
612 * The initial refcount is 1, and needs to be decremented to
613 * release the resources of the udev device.
615 * Returns: a new udev device, or #NULL, if it does not exist
617 struct udev_device *udev_device_new_from_environment(struct udev *udev)
620 struct udev_device *udev_device;
622 udev_device = udev_device_new(udev);
623 if (udev_device == NULL)
625 udev_device_set_info_loaded(udev_device);
627 for (i = 0; environ[i] != NULL; i++)
628 udev_device_add_property_from_string_parse(udev_device, environ[i]);
630 if (udev_device_add_property_from_string_parse_finish(udev_device) < 0) {
631 info(udev, "missing values, invalid device\n");
632 udev_device_unref(udev_device);
639 static struct udev_device *device_new_from_parent(struct udev_device *udev_device)
641 struct udev_device *udev_device_parent = NULL;
642 char path[UTIL_PATH_SIZE];
645 util_strscpy(path, sizeof(path), udev_device->syspath);
646 subdir = &path[strlen(udev_get_sys_path(udev_device->udev))+1];
650 pos = strrchr(subdir, '/');
651 if (pos == NULL || pos < &subdir[2])
654 udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
655 if (udev_device_parent != NULL)
656 return udev_device_parent;
662 * udev_device_get_parent:
663 * @udev_device: the device to start searching from
665 * Find the next parent device, and fill in information from the sys
666 * device and the udev database entry.
668 * The returned the device is not referenced. It is attached to the
669 * child device, and will be cleaned up when the child device
672 * It is not necessarily just the upper level directory, empty or not
673 * recognized sys directories are ignored.
675 * It can be called as many times as needed, without caring about
678 * Returns: a new udev device, or #NULL, if it no parent exist.
680 struct udev_device *udev_device_get_parent(struct udev_device *udev_device)
682 if (udev_device == NULL)
684 if (!udev_device->parent_set) {
685 udev_device->parent_set = true;
686 udev_device->parent_device = device_new_from_parent(udev_device);
688 if (udev_device->parent_device != NULL)
689 dbg(udev_device->udev, "returning existing parent %p\n", udev_device->parent_device);
690 return udev_device->parent_device;
694 * udev_device_get_parent_with_subsystem_devtype:
695 * @udev_device: udev device to start searching from
696 * @subsystem: the subsystem of the device
697 * @devtype: the type (DEVTYPE) of the device
699 * Find the next parent device, with a matching subsystem and devtype
700 * value, and fill in information from the sys device and the udev
703 * If devtype is #NULL, only subsystem is checked, and any devtype will
706 * The returned the device is not referenced. It is attached to the
707 * child device, and will be cleaned up when the child device
710 * It can be called as many times as needed, without caring about
713 * Returns: a new udev device, or #NULL if no matching parent exists.
715 struct udev_device *udev_device_get_parent_with_subsystem_devtype(struct udev_device *udev_device, const char *subsystem, const char *devtype)
717 struct udev_device *parent;
719 if (subsystem == NULL)
722 parent = udev_device_get_parent(udev_device);
723 while (parent != NULL) {
724 const char *parent_subsystem;
725 const char *parent_devtype;
727 parent_subsystem = udev_device_get_subsystem(parent);
728 if (parent_subsystem != NULL && strcmp(parent_subsystem, subsystem) == 0) {
731 parent_devtype = udev_device_get_devtype(parent);
732 if (parent_devtype != NULL && strcmp(parent_devtype, devtype) == 0)
735 parent = udev_device_get_parent(parent);
741 * udev_device_get_udev:
742 * @udev_device: udev device
744 * Retrieve the udev library context the device was created with.
746 * Returns: the udev library context
748 struct udev *udev_device_get_udev(struct udev_device *udev_device)
750 if (udev_device == NULL)
752 return udev_device->udev;
757 * @udev_device: udev device
759 * Take a reference of a udev device.
761 * Returns: the passed udev device
763 struct udev_device *udev_device_ref(struct udev_device *udev_device)
765 if (udev_device == NULL)
767 udev_device->refcount++;
773 * @udev_device: udev device
775 * Drop a reference of a udev device. If the refcount reaches zero,
776 * the resources of the device will be released.
779 void udev_device_unref(struct udev_device *udev_device)
781 if (udev_device == NULL)
783 udev_device->refcount--;
784 if (udev_device->refcount > 0)
786 if (udev_device->parent_device != NULL)
787 udev_device_unref(udev_device->parent_device);
788 free(udev_device->syspath);
789 free(udev_device->sysname);
790 free(udev_device->devnode);
791 free(udev_device->subsystem);
792 free(udev_device->devtype);
793 udev_list_cleanup_entries(udev_device->udev, &udev_device->devlinks_list);
794 udev_list_cleanup_entries(udev_device->udev, &udev_device->properties_list);
795 udev_list_cleanup_entries(udev_device->udev, &udev_device->sysattr_list);
796 udev_list_cleanup_entries(udev_device->udev, &udev_device->tags_list);
797 free(udev_device->action);
798 free(udev_device->driver);
799 free(udev_device->devpath_old);
800 free(udev_device->sysname_old);
801 free(udev_device->knodename);
802 free(udev_device->envp);
803 free(udev_device->monitor_buf);
804 dbg(udev_device->udev, "udev_device: %p released\n", udev_device);
809 * udev_device_get_devpath:
810 * @udev_device: udev device
812 * Retrieve the kernel devpath value of the udev device. The path
813 * does not contain the sys mount point, and starts with a '/'.
815 * Returns: the devpath of the udev device
817 const char *udev_device_get_devpath(struct udev_device *udev_device)
819 if (udev_device == NULL)
821 return udev_device->devpath;
825 * udev_device_get_syspath:
826 * @udev_device: udev device
828 * Retrieve the sys path of the udev device. The path is an
829 * absolute path and starts with the sys mount point.
831 * Returns: the sys path of the udev device
833 const char *udev_device_get_syspath(struct udev_device *udev_device)
835 if (udev_device == NULL)
837 return udev_device->syspath;
841 * udev_device_get_sysname:
842 * @udev_device: udev device
844 * Returns: the sys name of the device device
846 const char *udev_device_get_sysname(struct udev_device *udev_device)
848 if (udev_device == NULL)
850 return udev_device->sysname;
854 * udev_device_get_sysnum:
855 * @udev_device: udev device
857 * Returns: the trailing number of of the device name
859 const char *udev_device_get_sysnum(struct udev_device *udev_device)
861 if (udev_device == NULL)
863 return udev_device->sysnum;
867 * udev_device_get_devnode:
868 * @udev_device: udev device
870 * Retrieve the device node file name belonging to the udev device.
871 * The path is an absolute path, and starts with the device directory.
873 * Returns: the device node file name of the udev device, or #NULL if no device node exists
875 const char *udev_device_get_devnode(struct udev_device *udev_device)
877 if (udev_device == NULL)
879 if (!udev_device->info_loaded)
880 udev_device_read_db(udev_device);
881 return udev_device->devnode;
885 * udev_device_get_subsystem:
886 * @udev_device: udev device
888 * Retrieve the subsystem string of the udev device. The string does not
891 * Returns: the subsystem name of the udev device, or #NULL if it can not be determined
893 const char *udev_device_get_subsystem(struct udev_device *udev_device)
895 char subsystem[UTIL_NAME_SIZE];
897 if (udev_device == NULL)
899 if (!udev_device->subsystem_set) {
900 udev_device->subsystem_set = true;
901 /* read "subsystem" link */
902 if (util_get_sys_subsystem(udev_device->udev, udev_device->syspath, subsystem, sizeof(subsystem)) > 0) {
903 udev_device_set_subsystem(udev_device, subsystem);
904 return udev_device->subsystem;
907 if (strncmp(udev_device->devpath, "/module/", 8) == 0) {
908 udev_device_set_subsystem(udev_device, "module");
909 return udev_device->subsystem;
911 if (strstr(udev_device->devpath, "/drivers/") != NULL) {
912 udev_device_set_subsystem(udev_device, "drivers");
913 return udev_device->subsystem;
915 if (strncmp(udev_device->devpath, "/subsystem/", 11) == 0 ||
916 strncmp(udev_device->devpath, "/class/", 7) == 0 ||
917 strncmp(udev_device->devpath, "/bus/", 5) == 0) {
918 udev_device_set_subsystem(udev_device, "subsystem");
919 return udev_device->subsystem;
922 return udev_device->subsystem;
926 * udev_device_get_devtype:
927 * @udev_device: udev device
929 * Retrieve the devtype string of the udev device.
931 * Returns: the devtype name of the udev device, or #NULL if it can not be determined
933 const char *udev_device_get_devtype(struct udev_device *udev_device)
935 if (udev_device == NULL)
937 if (!udev_device->devtype_set) {
938 udev_device->devtype_set = true;
939 udev_device_read_uevent_file(udev_device);
941 return udev_device->devtype;
945 * udev_device_get_devlinks_list_entry:
946 * @udev_device: udev device
948 * Retrieve the list of device links pointing to the device file of
949 * the udev device. The next list entry can be retrieved with
950 * udev_list_entry_next(), which returns #NULL if no more entries exist.
951 * The devlink path can be retrieved from the list entry by
952 * udev_list_entry_get_name(). The path is an absolute path, and starts with
953 * the device directory.
955 * Returns: the first entry of the device node link list
957 struct udev_list_entry *udev_device_get_devlinks_list_entry(struct udev_device *udev_device)
959 if (udev_device == NULL)
961 if (!udev_device->info_loaded)
962 udev_device_read_db(udev_device);
963 return udev_list_get_entry(&udev_device->devlinks_list);
966 void udev_device_cleanup_devlinks_list(struct udev_device *udev_device)
968 udev_device->devlinks_uptodate = false;
969 udev_list_cleanup_entries(udev_device->udev, &udev_device->devlinks_list);
973 * udev_device_get_properties_list_entry:
974 * @udev_device: udev device
976 * Retrieve the list of key/value device properties of the udev
977 * device. The next list entry can be retrieved with udev_list_entry_next(),
978 * which returns #NULL if no more entries exist. The property name
979 * can be retrieved from the list entry by udev_list_get_name(),
980 * the property value by udev_list_get_value().
982 * Returns: the first entry of the property list
984 struct udev_list_entry *udev_device_get_properties_list_entry(struct udev_device *udev_device)
986 if (udev_device == NULL)
988 if (!udev_device->info_loaded) {
989 udev_device_read_uevent_file(udev_device);
990 udev_device_read_db(udev_device);
992 if (!udev_device->devlinks_uptodate) {
993 char symlinks[UTIL_PATH_SIZE];
994 struct udev_list_entry *list_entry;
996 udev_device->devlinks_uptodate = true;
997 list_entry = udev_device_get_devlinks_list_entry(udev_device);
998 if (list_entry != NULL) {
1003 l = util_strpcpyl(&s, sizeof(symlinks), udev_list_entry_get_name(list_entry), NULL);
1004 udev_list_entry_foreach(list_entry, udev_list_entry_get_next(list_entry))
1005 l = util_strpcpyl(&s, l, " ", udev_list_entry_get_name(list_entry), NULL);
1006 udev_device_add_property(udev_device, "DEVLINKS", symlinks);
1009 if (!udev_device->tags_uptodate) {
1010 udev_device->tags_uptodate = true;
1011 if (udev_device_get_tags_list_entry(udev_device) != NULL) {
1012 char tags[UTIL_PATH_SIZE];
1013 struct udev_list_entry *list_entry;
1018 l = util_strpcpyl(&s, sizeof(tags), ":", NULL);
1019 udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(udev_device))
1020 l = util_strpcpyl(&s, l, udev_list_entry_get_name(list_entry), ":", NULL);
1021 udev_device_add_property(udev_device, "TAGS", tags);
1024 return udev_list_get_entry(&udev_device->properties_list);
1028 * udev_device_get_driver:
1029 * @udev_device: udev device
1031 * Returns: the driver string, or #NULL if there is no driver attached.
1033 const char *udev_device_get_driver(struct udev_device *udev_device)
1035 char driver[UTIL_NAME_SIZE];
1037 if (udev_device == NULL)
1039 if (!udev_device->driver_set) {
1040 udev_device->driver_set = true;
1041 if (util_get_sys_driver(udev_device->udev, udev_device->syspath, driver, sizeof(driver)) > 0)
1042 udev_device->driver = strdup(driver);
1044 return udev_device->driver;
1048 * udev_device_get_devnum:
1049 * @udev_device: udev device
1051 * Returns: the device major/minor number.
1053 dev_t udev_device_get_devnum(struct udev_device *udev_device)
1055 if (udev_device == NULL)
1056 return makedev(0, 0);
1057 if (!udev_device->info_loaded)
1058 udev_device_read_uevent_file(udev_device);
1059 return udev_device->devnum;
1063 * udev_device_get_action:
1064 * @udev_device: udev device
1066 * This is only valid if the device was received through a monitor. Devices read from
1067 * sys do not have an action string. Usual actions are: add, remove, change, online,
1070 * Returns: the kernel action value, or #NULL if there is no action value available.
1072 const char *udev_device_get_action(struct udev_device *udev_device)
1074 if (udev_device == NULL)
1076 return udev_device->action;
1080 * udev_device_get_devnum:
1081 * @udev_device: udev device
1083 * This is only valid if the device was received through a monitor. Devices read from
1084 * sys do not have a sequence number.
1086 * Returns: the kernel event sequence number, or 0 if there is no sequence number available.
1088 unsigned long long int udev_device_get_seqnum(struct udev_device *udev_device)
1090 if (udev_device == NULL)
1092 return udev_device->seqnum;
1096 * udev_device_get_sysattr_value:
1097 * @udev_device: udev device
1098 * @sysattr: attribute name
1100 * The retrieved value is cached in the device. Repeated calls will return the same
1101 * value and not open the attribute again.
1103 * Returns: the content of a sys attribute file, or #NULL if there is no sys attribute value.
1105 const char *udev_device_get_sysattr_value(struct udev_device *udev_device, const char *sysattr)
1107 struct udev_list_entry *list_entry;
1108 char path[UTIL_PATH_SIZE];
1110 struct stat statbuf;
1113 const char *val = NULL;
1115 if (udev_device == NULL)
1117 if (sysattr == NULL)
1120 /* look for possibly already cached result */
1121 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_device->sysattr_list)) {
1122 if (strcmp(udev_list_entry_get_name(list_entry), sysattr) == 0) {
1123 dbg(udev_device->udev, "got '%s' (%s) from cache\n",
1124 sysattr, udev_list_entry_get_value(list_entry));
1125 return udev_list_entry_get_value(list_entry);
1129 util_strscpyl(path, sizeof(path), udev_device_get_syspath(udev_device), "/", sysattr, NULL);
1130 if (lstat(path, &statbuf) != 0) {
1131 dbg(udev_device->udev, "no attribute '%s', keep negative entry\n", path);
1132 udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, NULL, 0, 0);
1136 if (S_ISLNK(statbuf.st_mode)) {
1137 char target[UTIL_NAME_SIZE];
1141 /* some core links return the last element of the target path */
1142 if (strcmp(sysattr, "driver") != 0 &&
1143 strcmp(sysattr, "subsystem") != 0 &&
1144 strcmp(sysattr, "module") != 0)
1147 len = readlink(path, target, sizeof(target));
1148 if (len <= 0 || len == sizeof(target))
1152 pos = strrchr(target, '/');
1155 dbg(udev_device->udev, "cache '%s' with link value '%s'\n", sysattr, pos);
1156 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, pos, 0, 0);
1157 val = udev_list_entry_get_value(list_entry);
1163 /* skip directories */
1164 if (S_ISDIR(statbuf.st_mode))
1167 /* skip non-readable files */
1168 if ((statbuf.st_mode & S_IRUSR) == 0)
1171 /* read attribute value */
1172 fd = open(path, O_RDONLY|O_CLOEXEC);
1174 dbg(udev_device->udev, "attribute '%s' can not be opened\n", path);
1177 size = read(fd, value, sizeof(value));
1181 if (size == sizeof(value))
1184 /* got a valid value, store it in cache and return it */
1186 util_remove_trailing_chars(value, '\n');
1187 dbg(udev_device->udev, "'%s' has attribute value '%s'\n", path, value);
1188 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, value, 0, 0);
1189 val = udev_list_entry_get_value(list_entry);
1194 int udev_device_set_syspath(struct udev_device *udev_device, const char *syspath)
1199 free(udev_device->syspath);
1200 udev_device->syspath = strdup(syspath);
1201 if (udev_device->syspath == NULL)
1203 udev_device->devpath = &udev_device->syspath[strlen(udev_get_sys_path(udev_device->udev))];
1204 udev_device_add_property(udev_device, "DEVPATH", udev_device->devpath);
1206 pos = strrchr(udev_device->syspath, '/');
1209 udev_device->sysname = strdup(&pos[1]);
1210 if (udev_device->sysname == NULL)
1213 /* some devices have '!' in their name, change that to '/' */
1215 while (udev_device->sysname[len] != '\0') {
1216 if (udev_device->sysname[len] == '!')
1217 udev_device->sysname[len] = '/';
1221 /* trailing number */
1222 while (len > 0 && isdigit(udev_device->sysname[--len]))
1223 udev_device->sysnum = &udev_device->sysname[len];
1225 /* sysname is completely numeric */
1227 udev_device->sysnum = NULL;
1232 int udev_device_set_subsystem(struct udev_device *udev_device, const char *subsystem)
1234 free(udev_device->subsystem);
1235 udev_device->subsystem = strdup(subsystem);
1236 if (udev_device->subsystem == NULL)
1238 udev_device->subsystem_set = true;
1239 udev_device_add_property(udev_device, "SUBSYSTEM", udev_device->subsystem);
1243 int udev_device_set_devtype(struct udev_device *udev_device, const char *devtype)
1245 free(udev_device->devtype);
1246 udev_device->devtype = strdup(devtype);
1247 if (udev_device->devtype == NULL)
1249 udev_device->devtype_set = true;
1250 udev_device_add_property(udev_device, "DEVTYPE", udev_device->devtype);
1254 int udev_device_set_devnode(struct udev_device *udev_device, const char *devnode)
1256 free(udev_device->devnode);
1257 udev_device->devnode = strdup(devnode);
1258 if (devnode == NULL)
1260 if (udev_device->devnode == NULL)
1262 udev_device_add_property(udev_device, "DEVNAME", udev_device->devnode);
1266 int udev_device_add_devlink(struct udev_device *udev_device, const char *devlink, int unique)
1268 struct udev_list_entry *list_entry;
1270 udev_device->devlinks_uptodate = false;
1271 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->devlinks_list, devlink, NULL, 1, 0);
1272 if (list_entry == NULL)
1275 udev_list_entry_set_flags(list_entry, 1);
1279 int udev_device_add_tag(struct udev_device *udev_device, const char *tag)
1281 if (strchr(tag, ':') != NULL || strchr(tag, ' ') != NULL)
1283 udev_device->tags_uptodate = false;
1284 if (udev_list_entry_add(udev_device->udev, &udev_device->tags_list, tag, NULL, 1, 0) != NULL)
1289 void udev_device_cleanup_tags_list(struct udev_device *udev_device)
1291 udev_device->tags_uptodate = false;
1292 udev_list_cleanup_entries(udev_device->udev, &udev_device->tags_list);
1296 * udev_device_get_tags_list_entry:
1297 * @udev_device: udev device
1299 * Retrieve the list of tags attached to the udev device. The next
1300 * list entry can be retrieved with udev_list_entry_next(),
1301 * which returns #NULL if no more entries exist. The tag string
1302 * can be retrieved from the list entry by udev_list_get_name().
1304 * Returns: the first entry of the tag list
1306 struct udev_list_entry *udev_device_get_tags_list_entry(struct udev_device *udev_device)
1308 if (udev_device == NULL)
1310 return udev_list_get_entry(&udev_device->tags_list);
1313 int udev_device_has_tag(struct udev_device *udev_device, const char *tag)
1315 struct udev_list_entry *list_entry;
1317 if (!udev_device->info_loaded)
1318 udev_device_read_db(udev_device);
1319 list_entry = udev_device_get_tags_list_entry(udev_device);
1320 list_entry = udev_list_entry_get_by_name(list_entry, tag);
1321 if (list_entry != NULL)
1326 #define ENVP_SIZE 128
1327 #define MONITOR_BUF_SIZE 4096
1328 static int update_envp_monitor_buf(struct udev_device *udev_device)
1330 struct udev_list_entry *list_entry;
1335 /* monitor buffer of property strings */
1336 free(udev_device->monitor_buf);
1337 udev_device->monitor_buf_len = 0;
1338 udev_device->monitor_buf = malloc(MONITOR_BUF_SIZE);
1339 if (udev_device->monitor_buf == NULL)
1342 /* envp array, strings will point into monitor buffer */
1343 if (udev_device->envp == NULL)
1344 udev_device->envp = malloc(sizeof(char *) * ENVP_SIZE);
1345 if (udev_device->envp == NULL)
1349 s = udev_device->monitor_buf;
1350 l = MONITOR_BUF_SIZE;
1351 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device)) {
1354 key = udev_list_entry_get_name(list_entry);
1355 /* skip private variables */
1359 /* add string to envp array */
1360 udev_device->envp[i++] = s;
1361 if (i+1 >= ENVP_SIZE)
1364 /* add property string to monitor buffer */
1365 l = util_strpcpyl(&s, l, key, "=", udev_list_entry_get_value(list_entry), NULL);
1368 /* advance past the trailing '\0' that util_strpcpyl() guarantees */
1372 udev_device->envp[i] = NULL;
1373 udev_device->monitor_buf_len = s - udev_device->monitor_buf;
1374 udev_device->envp_uptodate = true;
1375 dbg(udev_device->udev, "filled envp/monitor buffer, %u properties, %zu bytes\n",
1376 i, udev_device->monitor_buf_len);
1380 char **udev_device_get_properties_envp(struct udev_device *udev_device)
1382 if (!udev_device->envp_uptodate)
1383 if (update_envp_monitor_buf(udev_device) != 0)
1385 return udev_device->envp;
1388 ssize_t udev_device_get_properties_monitor_buf(struct udev_device *udev_device, const char **buf)
1390 if (!udev_device->envp_uptodate)
1391 if (update_envp_monitor_buf(udev_device) != 0)
1393 *buf = udev_device->monitor_buf;
1394 return udev_device->monitor_buf_len;
1397 int udev_device_set_action(struct udev_device *udev_device, const char *action)
1399 free(udev_device->action);
1400 udev_device->action = strdup(action);
1401 if (udev_device->action == NULL)
1403 udev_device_add_property(udev_device, "ACTION", udev_device->action);
1407 int udev_device_set_driver(struct udev_device *udev_device, const char *driver)
1409 free(udev_device->driver);
1410 udev_device->driver = strdup(driver);
1411 if (udev_device->driver == NULL)
1413 udev_device->driver_set = true;
1414 udev_device_add_property(udev_device, "DRIVER", udev_device->driver);
1418 const char *udev_device_get_devpath_old(struct udev_device *udev_device)
1420 return udev_device->devpath_old;
1423 int udev_device_set_devpath_old(struct udev_device *udev_device, const char *devpath_old)
1428 free(udev_device->devpath_old);
1429 udev_device->devpath_old = strdup(devpath_old);
1430 if (udev_device->devpath_old == NULL)
1432 udev_device_add_property(udev_device, "DEVPATH_OLD", udev_device->devpath_old);
1434 pos = strrchr(udev_device->devpath_old, '/');
1437 udev_device->sysname_old = strdup(&pos[1]);
1438 if (udev_device->sysname_old == NULL)
1441 /* some devices have '!' in their name, change that to '/' */
1443 while (udev_device->sysname_old[len] != '\0') {
1444 if (udev_device->sysname_old[len] == '!')
1445 udev_device->sysname_old[len] = '/';
1451 const char *udev_device_get_sysname_old(struct udev_device *udev_device)
1453 if (udev_device == NULL)
1455 return udev_device->sysname_old;
1458 const char *udev_device_get_knodename(struct udev_device *udev_device)
1460 return udev_device->knodename;
1463 int udev_device_set_knodename(struct udev_device *udev_device, const char *knodename)
1465 free(udev_device->knodename);
1466 udev_device->knodename = strdup(knodename);
1467 if (udev_device->knodename == NULL)
1469 udev_device_add_property(udev_device, "DEVNAME", udev_device->knodename);
1473 int udev_device_get_timeout(struct udev_device *udev_device)
1475 return udev_device->timeout;
1478 int udev_device_set_timeout(struct udev_device *udev_device, int timeout)
1480 udev_device->timeout = timeout;
1483 int udev_device_get_event_timeout(struct udev_device *udev_device)
1485 if (!udev_device->info_loaded)
1486 udev_device_read_db(udev_device);
1487 return udev_device->event_timeout;
1490 int udev_device_set_event_timeout(struct udev_device *udev_device, int event_timeout)
1492 udev_device->event_timeout = event_timeout;
1496 int udev_device_set_seqnum(struct udev_device *udev_device, unsigned long long int seqnum)
1500 udev_device->seqnum = seqnum;
1501 snprintf(num, sizeof(num), "%llu", seqnum);
1502 udev_device_add_property(udev_device, "SEQNUM", num);
1506 int udev_device_set_devnum(struct udev_device *udev_device, dev_t devnum)
1510 udev_device->devnum = devnum;
1512 snprintf(num, sizeof(num), "%u", major(devnum));
1513 udev_device_add_property(udev_device, "MAJOR", num);
1514 snprintf(num, sizeof(num), "%u", minor(devnum));
1515 udev_device_add_property(udev_device, "MINOR", num);
1519 int udev_device_get_devlink_priority(struct udev_device *udev_device)
1521 if (!udev_device->info_loaded)
1522 udev_device_read_db(udev_device);
1523 return udev_device->devlink_priority;
1526 int udev_device_set_devlink_priority(struct udev_device *udev_device, int prio)
1528 udev_device->devlink_priority = prio;
1532 int udev_device_get_watch_handle(struct udev_device *udev_device)
1534 if (!udev_device->info_loaded)
1535 udev_device_read_db(udev_device);
1536 return udev_device->watch_handle;
1539 int udev_device_set_watch_handle(struct udev_device *udev_device, int handle)
1541 udev_device->watch_handle = handle;
1545 int udev_device_get_ifindex(struct udev_device *udev_device)
1547 return udev_device->ifindex;
1550 int udev_device_set_ifindex(struct udev_device *udev_device, int ifindex)
1552 udev_device->ifindex = ifindex;