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;
60 size_t monitor_buf_len;
61 struct udev_list_node devlinks_list;
62 struct udev_list_node properties_list;
63 struct udev_list_node sysattr_list;
64 struct udev_list_node tags_list;
65 unsigned long long int seqnum;
77 bool devlinks_uptodate;
86 struct udev_list_entry *udev_device_add_property(struct udev_device *udev_device, const char *key, const char *value)
88 udev_device->envp_uptodate = false;
90 struct udev_list_entry *list_entry;
92 list_entry = udev_device_get_properties_list_entry(udev_device);
93 list_entry = udev_list_entry_get_by_name(list_entry, key);
94 if (list_entry != NULL)
95 udev_list_entry_delete(list_entry);
98 return udev_list_entry_add(udev_device->udev, &udev_device->properties_list, key, value, 1, 0);
101 static struct udev_list_entry *udev_device_add_property_from_string(struct udev_device *udev_device, const char *property)
103 char name[UTIL_LINE_SIZE];
106 util_strscpy(name, sizeof(name), property);
107 val = strchr(name, '=');
114 return udev_device_add_property(udev_device, name, val);
118 * parse property string, and if needed, update internal values accordingly
120 * udev_device_add_property_from_string_parse_finish() needs to be
121 * called after adding properties, and its return value checked
123 * udev_device_set_info_loaded() needs to be set, to avoid trying
124 * to use a device without a DEVPATH set
126 void udev_device_add_property_from_string_parse(struct udev_device *udev_device, const char *property)
128 if (strncmp(property, "DEVPATH=", 8) == 0) {
129 char path[UTIL_PATH_SIZE];
131 util_strscpyl(path, sizeof(path), udev_get_sys_path(udev_device->udev), &property[8], NULL);
132 udev_device_set_syspath(udev_device, path);
133 } else if (strncmp(property, "SUBSYSTEM=", 10) == 0) {
134 udev_device_set_subsystem(udev_device, &property[10]);
135 } else if (strncmp(property, "DEVTYPE=", 8) == 0) {
136 udev_device_set_devtype(udev_device, &property[8]);
137 } else if (strncmp(property, "DEVNAME=", 8) == 0) {
138 if (property[8] == '/')
139 udev_device_set_devnode(udev_device, &property[8]);
141 udev_device_set_knodename(udev_device, &property[8]);
142 } else if (strncmp(property, "DEVLINKS=", 9) == 0) {
143 char devlinks[UTIL_PATH_SIZE];
147 util_strscpy(devlinks, sizeof(devlinks), &property[9]);
149 next = strchr(slink, ' ');
150 while (next != NULL) {
152 udev_device_add_devlink(udev_device, slink, 0);
154 next = strchr(slink, ' ');
156 if (slink[0] != '\0')
157 udev_device_add_devlink(udev_device, slink, 0);
158 } else if (strncmp(property, "TAGS=", 5) == 0) {
159 char tags[UTIL_PATH_SIZE];
162 util_strscpy(tags, sizeof(tags), &property[5]);
163 next = strchr(tags, ':');
166 while (next[0] != '\0') {
170 next = strchr(tag, ':');
175 udev_device_add_tag(udev_device, tag);
178 } else if (strncmp(property, "DRIVER=", 7) == 0) {
179 udev_device_set_driver(udev_device, &property[7]);
180 } else if (strncmp(property, "ACTION=", 7) == 0) {
181 udev_device_set_action(udev_device, &property[7]);
182 } else if (strncmp(property, "MAJOR=", 6) == 0) {
183 udev_device->maj = strtoull(&property[6], NULL, 10);
184 } else if (strncmp(property, "MINOR=", 6) == 0) {
185 udev_device->min = strtoull(&property[6], NULL, 10);
186 } else if (strncmp(property, "DEVPATH_OLD=", 12) == 0) {
187 udev_device_set_devpath_old(udev_device, &property[12]);
188 } else if (strncmp(property, "SEQNUM=", 7) == 0) {
189 udev_device_set_seqnum(udev_device, strtoull(&property[7], NULL, 10));
190 } else if (strncmp(property, "TIMEOUT=", 8) == 0) {
191 udev_device_set_timeout(udev_device, strtoull(&property[8], NULL, 10));
192 } else if (strncmp(property, "IFINDEX=", 8) == 0) {
193 udev_device_set_ifindex(udev_device, strtoull(&property[8], NULL, 10));
195 udev_device_add_property_from_string(udev_device, property);
199 int udev_device_add_property_from_string_parse_finish(struct udev_device *udev_device)
201 if (udev_device->maj > 0)
202 udev_device_set_devnum(udev_device, makedev(udev_device->maj, udev_device->min));
203 udev_device->maj = 0;
204 udev_device->min = 0;
206 if (udev_device->devpath == NULL || udev_device->subsystem == NULL)
212 * udev_device_get_property_value:
213 * @udev_device: udev device
214 * @key: property name
216 * Returns: the value of a device property, or #NULL if there is no such property.
218 const char *udev_device_get_property_value(struct udev_device *udev_device, const char *key)
220 struct udev_list_entry *list_entry;
222 if (udev_device == NULL)
227 list_entry = udev_device_get_properties_list_entry(udev_device);
228 list_entry = udev_list_entry_get_by_name(list_entry, key);
229 return udev_list_entry_get_value(list_entry);
232 int udev_device_read_db(struct udev_device *udev_device)
236 char filename[UTIL_PATH_SIZE];
237 char line[UTIL_LINE_SIZE];
240 if (udev_device->db_loaded)
243 id = udev_device_get_id_filename(udev_device);
246 util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev_device->udev), "/.udev/db/", id, NULL);
248 if (lstat(filename, &stats) != 0) {
249 dbg(udev_device->udev, "no db file to read %s: %m\n", filename);
252 if ((stats.st_mode & S_IFMT) == S_IFLNK) {
253 char target[UTIL_PATH_SIZE];
254 char devnode[UTIL_PATH_SIZE];
258 target_len = readlink(filename, target, sizeof(target));
259 if (target_len <= 0 || target_len == sizeof(target)) {
260 info(udev_device->udev, "error reading db link %s: %m\n", filename);
263 target[target_len] = '\0';
265 next = strchr(target, ' ');
270 util_strscpyl(devnode, sizeof(devnode), udev_get_dev_path(udev_device->udev), "/", target, NULL);
271 udev_device_set_devnode(udev_device, devnode);
272 while (next != NULL) {
273 char devlink[UTIL_PATH_SIZE];
277 next = strchr(next, ' ');
282 util_strscpyl(devlink, sizeof(devlink), udev_get_dev_path(udev_device->udev), "/", lnk, NULL);
283 udev_device_add_devlink(udev_device, devlink, 0);
285 info(udev_device->udev, "device %p filled with db symlink data '%s'\n", udev_device, udev_device->devnode);
289 f = fopen(filename, "re");
291 dbg(udev_device->udev, "error reading db file %s: %m\n", filename);
294 udev_device->db_loaded = true;
296 while (fgets(line, sizeof(line), f)) {
307 util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev_device->udev), "/", val, NULL);
308 udev_device_set_devnode(udev_device, filename);
311 util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev_device->udev), "/", val, NULL);
312 udev_device_add_devlink(udev_device, filename, 0);
315 udev_device_set_devlink_priority(udev_device, atoi(val));
318 udev_device_set_event_timeout(udev_device, atoi(val));
321 udev_device_add_property_from_string(udev_device, val);
324 udev_device_add_tag(udev_device, val);
327 udev_device_set_watch_handle(udev_device, atoi(val));
333 info(udev_device->udev, "device %p filled with db file data\n", udev_device);
337 int udev_device_read_uevent_file(struct udev_device *udev_device)
339 char filename[UTIL_PATH_SIZE];
341 char line[UTIL_LINE_SIZE];
345 if (udev_device->uevent_loaded)
348 util_strscpyl(filename, sizeof(filename), udev_device->syspath, "/uevent", NULL);
349 f = fopen(filename, "re");
352 udev_device->uevent_loaded = true;
354 while (fgets(line, sizeof(line), f)) {
357 pos = strchr(line, '\n');
362 if (strncmp(line, "DEVTYPE=", 8) == 0)
363 udev_device_set_devtype(udev_device, &line[8]);
364 else if (strncmp(line, "MAJOR=", 6) == 0)
365 maj = strtoull(&line[6], NULL, 10);
366 else if (strncmp(line, "MINOR=", 6) == 0)
367 min = strtoull(&line[6], NULL, 10);
368 else if (strncmp(line, "IFINDEX=", 8) == 0)
369 udev_device_set_ifindex(udev_device, strtoull(&line[8], NULL, 10));
370 else if (strncmp(line, "DEVNAME=", 8) == 0)
371 udev_device_set_knodename(udev_device, &line[8]);
373 udev_device_add_property_from_string(udev_device, line);
376 udev_device->devnum = makedev(maj, min);
381 void udev_device_set_info_loaded(struct udev_device *device)
383 device->info_loaded = true;
386 struct udev_device *udev_device_new(struct udev *udev)
388 struct udev_device *udev_device;
389 struct udev_list_entry *list_entry;
394 udev_device = calloc(1, sizeof(struct udev_device));
395 if (udev_device == NULL)
397 udev_device->refcount = 1;
398 udev_device->udev = udev;
399 udev_list_init(&udev_device->devlinks_list);
400 udev_list_init(&udev_device->properties_list);
401 udev_list_init(&udev_device->sysattr_list);
402 udev_list_init(&udev_device->tags_list);
403 udev_device->event_timeout = -1;
404 udev_device->watch_handle = -1;
405 /* copy global properties */
406 udev_list_entry_foreach(list_entry, udev_get_properties_list_entry(udev))
407 udev_device_add_property(udev_device,
408 udev_list_entry_get_name(list_entry),
409 udev_list_entry_get_value(list_entry));
410 dbg(udev_device->udev, "udev_device: %p created\n", udev_device);
415 * udev_device_new_from_syspath:
416 * @udev: udev library context
417 * @syspath: sys device path including sys directory
419 * Create new udev device, and fill in information from the sys
420 * device and the udev database entry. The syspath is the absolute
421 * path to the device, including the sys mount point.
423 * The initial refcount is 1, and needs to be decremented to
424 * release the resources of the udev device.
426 * Returns: a new udev device, or #NULL, if it does not exist
428 struct udev_device *udev_device_new_from_syspath(struct udev *udev, const char *syspath)
432 char path[UTIL_PATH_SIZE];
435 struct udev_device *udev_device;
442 /* path starts in sys */
443 len = strlen(udev_get_sys_path(udev));
444 if (strncmp(syspath, udev_get_sys_path(udev), len) != 0) {
445 info(udev, "not in sys :%s\n", syspath);
449 /* path is not a root directory */
450 subdir = &syspath[len+1];
451 pos = strrchr(subdir, '/');
452 if (pos == NULL || pos[1] == '\0' || pos < &subdir[2]) {
453 dbg(udev, "not a subdir :%s\n", syspath);
457 /* resolve possible symlink to real path */
458 util_strscpy(path, sizeof(path), syspath);
459 util_resolve_sys_link(udev, path, sizeof(path));
461 if (strncmp(&path[len], "/devices/", 9) == 0) {
462 char file[UTIL_PATH_SIZE];
464 /* all "devices" require a "uevent" file */
465 util_strscpyl(file, sizeof(file), path, "/uevent", NULL);
466 if (stat(file, &statbuf) != 0) {
467 dbg(udev, "not a device: %s\n", syspath);
471 /* everything else just needs to be a directory */
472 if (stat(path, &statbuf) != 0 || !S_ISDIR(statbuf.st_mode)) {
473 dbg(udev, "directory not found: %s\n", syspath);
478 udev_device = udev_device_new(udev);
479 if (udev_device == NULL)
482 udev_device_set_syspath(udev_device, path);
483 info(udev, "device %p has devpath '%s'\n", udev_device, udev_device_get_devpath(udev_device));
489 * udev_device_new_from_devnum:
490 * @udev: udev library context
491 * @type: char or block device
492 * @devnum: device major/minor number
494 * Create new udev device, and fill in information from the sys
495 * device and the udev database entry. The device is looked-up
496 * by its major/minor number and type. Character and block device
497 * numbers are not unique across the two types.
499 * The initial refcount is 1, and needs to be decremented to
500 * release the resources of the udev device.
502 * Returns: a new udev device, or #NULL, if it does not exist
504 struct udev_device *udev_device_new_from_devnum(struct udev *udev, char type, dev_t devnum)
506 char path[UTIL_PATH_SIZE];
507 const char *type_str;
511 else if (type == 'c')
516 /* use /sys/dev/{block,char}/<maj>:<min> link */
517 snprintf(path, sizeof(path), "%s/dev/%s/%u:%u",
518 udev_get_sys_path(udev), type_str, major(devnum), minor(devnum));
519 return udev_device_new_from_syspath(udev, path);
523 * udev_device_new_from_subsystem_sysname:
524 * @udev: udev library context
525 * @subsystem: the subsystem of the device
526 * @sysname: the name of the device
528 * Create new udev device, and fill in information from the sys device
529 * and the udev database entry. The device is looked up by the subsystem
530 * and name string of the device, like "mem" / "zero", or "block" / "sda".
532 * The initial refcount is 1, and needs to be decremented to
533 * release the resources of the udev device.
535 * Returns: a new udev device, or #NULL, if it does not exist
537 struct udev_device *udev_device_new_from_subsystem_sysname(struct udev *udev, const char *subsystem, const char *sysname)
539 char path_full[UTIL_PATH_SIZE];
545 l = util_strpcpyl(&path, sizeof(path_full), udev_get_sys_path(udev), NULL);
547 if (strcmp(subsystem, "subsystem") == 0) {
548 util_strscpyl(path, l, "/subsystem/", sysname, NULL);
549 if (stat(path_full, &statbuf) == 0)
552 util_strscpyl(path, l, "/bus/", sysname, NULL);
553 if (stat(path_full, &statbuf) == 0)
556 util_strscpyl(path, l, "/class/", sysname, NULL);
557 if (stat(path_full, &statbuf) == 0)
562 if (strcmp(subsystem, "module") == 0) {
563 util_strscpyl(path, l, "/module/", sysname, NULL);
564 if (stat(path_full, &statbuf) == 0)
569 if (strcmp(subsystem, "drivers") == 0) {
570 char subsys[UTIL_NAME_SIZE];
573 util_strscpy(subsys, sizeof(subsys), sysname);
574 driver = strchr(subsys, ':');
575 if (driver != NULL) {
579 util_strscpyl(path, l, "/subsystem/", subsys, "/drivers/", driver, NULL);
580 if (stat(path_full, &statbuf) == 0)
583 util_strscpyl(path, l, "/bus/", subsys, "/drivers/", driver, NULL);
584 if (stat(path_full, &statbuf) == 0)
590 util_strscpyl(path, l, "/subsystem/", subsystem, "/devices/", sysname, NULL);
591 if (stat(path_full, &statbuf) == 0)
594 util_strscpyl(path, l, "/bus/", subsystem, "/devices/", sysname, NULL);
595 if (stat(path_full, &statbuf) == 0)
598 util_strscpyl(path, l, "/class/", subsystem, "/", sysname, NULL);
599 if (stat(path_full, &statbuf) == 0)
604 return udev_device_new_from_syspath(udev, path_full);
608 * udev_device_new_from_environment
609 * @udev: udev library context
611 * Create new udev device, and fill in information from the
612 * current process environment. This only works reliable if
613 * the process is called from a udev rule. It is usually used
614 * for tools executed from IMPORT= rules.
616 * The initial refcount is 1, and needs to be decremented to
617 * release the resources of the udev device.
619 * Returns: a new udev device, or #NULL, if it does not exist
621 struct udev_device *udev_device_new_from_environment(struct udev *udev)
624 struct udev_device *udev_device;
626 udev_device = udev_device_new(udev);
627 if (udev_device == NULL)
629 udev_device_set_info_loaded(udev_device);
631 for (i = 0; environ[i] != NULL; i++)
632 udev_device_add_property_from_string_parse(udev_device, environ[i]);
634 if (udev_device_add_property_from_string_parse_finish(udev_device) < 0) {
635 info(udev, "missing values, invalid device\n");
636 udev_device_unref(udev_device);
643 static struct udev_device *device_new_from_parent(struct udev_device *udev_device)
645 struct udev_device *udev_device_parent = NULL;
646 char path[UTIL_PATH_SIZE];
649 util_strscpy(path, sizeof(path), udev_device->syspath);
650 subdir = &path[strlen(udev_get_sys_path(udev_device->udev))+1];
654 pos = strrchr(subdir, '/');
655 if (pos == NULL || pos < &subdir[2])
658 udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
659 if (udev_device_parent != NULL)
660 return udev_device_parent;
666 * udev_device_get_parent:
667 * @udev_device: the device to start searching from
669 * Find the next parent device, and fill in information from the sys
670 * device and the udev database entry.
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 is not necessarily just the upper level directory, empty or not
677 * recognized sys directories are ignored.
679 * It can be called as many times as needed, without caring about
682 * Returns: a new udev device, or #NULL, if it no parent exist.
684 struct udev_device *udev_device_get_parent(struct udev_device *udev_device)
686 if (udev_device == NULL)
688 if (!udev_device->parent_set) {
689 udev_device->parent_set = true;
690 udev_device->parent_device = device_new_from_parent(udev_device);
692 if (udev_device->parent_device != NULL)
693 dbg(udev_device->udev, "returning existing parent %p\n", udev_device->parent_device);
694 return udev_device->parent_device;
698 * udev_device_get_parent_with_subsystem_devtype:
699 * @udev_device: udev device to start searching from
700 * @subsystem: the subsystem of the device
701 * @devtype: the type (DEVTYPE) of the device
703 * Find the next parent device, with a matching subsystem and devtype
704 * value, and fill in information from the sys device and the udev
707 * If devtype is #NULL, only subsystem is checked, and any devtype will
710 * The returned the device is not referenced. It is attached to the
711 * child device, and will be cleaned up when the child device
714 * It can be called as many times as needed, without caring about
717 * Returns: a new udev device, or #NULL if no matching parent exists.
719 struct udev_device *udev_device_get_parent_with_subsystem_devtype(struct udev_device *udev_device, const char *subsystem, const char *devtype)
721 struct udev_device *parent;
723 if (subsystem == NULL)
726 parent = udev_device_get_parent(udev_device);
727 while (parent != NULL) {
728 const char *parent_subsystem;
729 const char *parent_devtype;
731 parent_subsystem = udev_device_get_subsystem(parent);
732 if (parent_subsystem != NULL && strcmp(parent_subsystem, subsystem) == 0) {
735 parent_devtype = udev_device_get_devtype(parent);
736 if (parent_devtype != NULL && strcmp(parent_devtype, devtype) == 0)
739 parent = udev_device_get_parent(parent);
745 * udev_device_get_udev:
746 * @udev_device: udev device
748 * Retrieve the udev library context the device was created with.
750 * Returns: the udev library context
752 struct udev *udev_device_get_udev(struct udev_device *udev_device)
754 if (udev_device == NULL)
756 return udev_device->udev;
761 * @udev_device: udev device
763 * Take a reference of a udev device.
765 * Returns: the passed udev device
767 struct udev_device *udev_device_ref(struct udev_device *udev_device)
769 if (udev_device == NULL)
771 udev_device->refcount++;
777 * @udev_device: udev device
779 * Drop a reference of a udev device. If the refcount reaches zero,
780 * the resources of the device will be released.
783 void udev_device_unref(struct udev_device *udev_device)
785 if (udev_device == NULL)
787 udev_device->refcount--;
788 if (udev_device->refcount > 0)
790 if (udev_device->parent_device != NULL)
791 udev_device_unref(udev_device->parent_device);
792 free(udev_device->syspath);
793 free(udev_device->sysname);
794 free(udev_device->devnode);
795 free(udev_device->subsystem);
796 free(udev_device->devtype);
797 udev_list_cleanup_entries(udev_device->udev, &udev_device->devlinks_list);
798 udev_list_cleanup_entries(udev_device->udev, &udev_device->properties_list);
799 udev_list_cleanup_entries(udev_device->udev, &udev_device->sysattr_list);
800 udev_list_cleanup_entries(udev_device->udev, &udev_device->tags_list);
801 free(udev_device->action);
802 free(udev_device->driver);
803 free(udev_device->devpath_old);
804 free(udev_device->sysname_old);
805 free(udev_device->knodename);
806 free(udev_device->id_filename);
807 free(udev_device->envp);
808 free(udev_device->monitor_buf);
809 dbg(udev_device->udev, "udev_device: %p released\n", udev_device);
814 * udev_device_get_devpath:
815 * @udev_device: udev device
817 * Retrieve the kernel devpath value of the udev device. The path
818 * does not contain the sys mount point, and starts with a '/'.
820 * Returns: the devpath of the udev device
822 const char *udev_device_get_devpath(struct udev_device *udev_device)
824 if (udev_device == NULL)
826 return udev_device->devpath;
830 * udev_device_get_syspath:
831 * @udev_device: udev device
833 * Retrieve the sys path of the udev device. The path is an
834 * absolute path and starts with the sys mount point.
836 * Returns: the sys path of the udev device
838 const char *udev_device_get_syspath(struct udev_device *udev_device)
840 if (udev_device == NULL)
842 return udev_device->syspath;
846 * udev_device_get_sysname:
847 * @udev_device: udev device
849 * Returns: the sys name of the device device
851 const char *udev_device_get_sysname(struct udev_device *udev_device)
853 if (udev_device == NULL)
855 return udev_device->sysname;
859 * udev_device_get_sysnum:
860 * @udev_device: udev device
862 * Returns: the trailing number of of the device name
864 const char *udev_device_get_sysnum(struct udev_device *udev_device)
866 if (udev_device == NULL)
868 return udev_device->sysnum;
872 * udev_device_get_devnode:
873 * @udev_device: udev device
875 * Retrieve the device node file name belonging to the udev device.
876 * The path is an absolute path, and starts with the device directory.
878 * Returns: the device node file name of the udev device, or #NULL if no device node exists
880 const char *udev_device_get_devnode(struct udev_device *udev_device)
882 if (udev_device == NULL)
884 if (!udev_device->info_loaded) {
885 udev_device_read_uevent_file(udev_device);
886 udev_device_read_db(udev_device);
889 /* we might get called before we handled an event and have a db, use the kernel-provided name */
890 if (udev_device->devnode == NULL && udev_device_get_knodename(udev_device) != NULL) {
891 if (asprintf(&udev_device->devnode, "%s/%s",
892 udev_get_dev_path(udev_device->udev), udev_device_get_knodename(udev_device)) < 0)
894 return udev_device->devnode;
897 return udev_device->devnode;
901 * udev_device_get_subsystem:
902 * @udev_device: udev device
904 * Retrieve the subsystem string of the udev device. The string does not
907 * Returns: the subsystem name of the udev device, or #NULL if it can not be determined
909 const char *udev_device_get_subsystem(struct udev_device *udev_device)
911 char subsystem[UTIL_NAME_SIZE];
913 if (udev_device == NULL)
915 if (!udev_device->subsystem_set) {
916 udev_device->subsystem_set = true;
917 /* read "subsystem" link */
918 if (util_get_sys_subsystem(udev_device->udev, udev_device->syspath, subsystem, sizeof(subsystem)) > 0) {
919 udev_device_set_subsystem(udev_device, subsystem);
920 return udev_device->subsystem;
923 if (strncmp(udev_device->devpath, "/module/", 8) == 0) {
924 udev_device_set_subsystem(udev_device, "module");
925 return udev_device->subsystem;
927 if (strstr(udev_device->devpath, "/drivers/") != NULL) {
928 udev_device_set_subsystem(udev_device, "drivers");
929 return udev_device->subsystem;
931 if (strncmp(udev_device->devpath, "/subsystem/", 11) == 0 ||
932 strncmp(udev_device->devpath, "/class/", 7) == 0 ||
933 strncmp(udev_device->devpath, "/bus/", 5) == 0) {
934 udev_device_set_subsystem(udev_device, "subsystem");
935 return udev_device->subsystem;
938 return udev_device->subsystem;
942 * udev_device_get_devtype:
943 * @udev_device: udev device
945 * Retrieve the devtype string of the udev device.
947 * Returns: the devtype name of the udev device, or #NULL if it can not be determined
949 const char *udev_device_get_devtype(struct udev_device *udev_device)
951 if (udev_device == NULL)
953 if (!udev_device->devtype_set) {
954 udev_device->devtype_set = true;
955 udev_device_read_uevent_file(udev_device);
957 return udev_device->devtype;
961 * udev_device_get_devlinks_list_entry:
962 * @udev_device: udev device
964 * Retrieve the list of device links pointing to the device file of
965 * the udev device. The next list entry can be retrieved with
966 * udev_list_entry_next(), which returns #NULL if no more entries exist.
967 * The devlink path can be retrieved from the list entry by
968 * udev_list_entry_get_name(). The path is an absolute path, and starts with
969 * the device directory.
971 * Returns: the first entry of the device node link list
973 struct udev_list_entry *udev_device_get_devlinks_list_entry(struct udev_device *udev_device)
975 if (udev_device == NULL)
977 if (!udev_device->info_loaded)
978 udev_device_read_db(udev_device);
979 return udev_list_get_entry(&udev_device->devlinks_list);
982 void udev_device_cleanup_devlinks_list(struct udev_device *udev_device)
984 udev_device->devlinks_uptodate = false;
985 udev_list_cleanup_entries(udev_device->udev, &udev_device->devlinks_list);
989 * udev_device_get_properties_list_entry:
990 * @udev_device: udev device
992 * Retrieve the list of key/value device properties of the udev
993 * device. The next list entry can be retrieved with udev_list_entry_next(),
994 * which returns #NULL if no more entries exist. The property name
995 * can be retrieved from the list entry by udev_list_get_name(),
996 * the property value by udev_list_get_value().
998 * Returns: the first entry of the property list
1000 struct udev_list_entry *udev_device_get_properties_list_entry(struct udev_device *udev_device)
1002 if (udev_device == NULL)
1004 if (!udev_device->info_loaded) {
1005 udev_device_read_uevent_file(udev_device);
1006 udev_device_read_db(udev_device);
1008 if (!udev_device->devlinks_uptodate) {
1009 char symlinks[UTIL_PATH_SIZE];
1010 struct udev_list_entry *list_entry;
1012 udev_device->devlinks_uptodate = true;
1013 list_entry = udev_device_get_devlinks_list_entry(udev_device);
1014 if (list_entry != NULL) {
1019 l = util_strpcpyl(&s, sizeof(symlinks), udev_list_entry_get_name(list_entry), NULL);
1020 udev_list_entry_foreach(list_entry, udev_list_entry_get_next(list_entry))
1021 l = util_strpcpyl(&s, l, " ", udev_list_entry_get_name(list_entry), NULL);
1022 udev_device_add_property(udev_device, "DEVLINKS", symlinks);
1025 if (!udev_device->tags_uptodate) {
1026 udev_device->tags_uptodate = true;
1027 if (udev_device_get_tags_list_entry(udev_device) != NULL) {
1028 char tags[UTIL_PATH_SIZE];
1029 struct udev_list_entry *list_entry;
1034 l = util_strpcpyl(&s, sizeof(tags), ":", NULL);
1035 udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(udev_device))
1036 l = util_strpcpyl(&s, l, udev_list_entry_get_name(list_entry), ":", NULL);
1037 udev_device_add_property(udev_device, "TAGS", tags);
1040 return udev_list_get_entry(&udev_device->properties_list);
1044 * udev_device_get_driver:
1045 * @udev_device: udev device
1047 * Returns: the driver string, or #NULL if there is no driver attached.
1049 const char *udev_device_get_driver(struct udev_device *udev_device)
1051 char driver[UTIL_NAME_SIZE];
1053 if (udev_device == NULL)
1055 if (!udev_device->driver_set) {
1056 udev_device->driver_set = true;
1057 if (util_get_sys_driver(udev_device->udev, udev_device->syspath, driver, sizeof(driver)) > 0)
1058 udev_device->driver = strdup(driver);
1060 return udev_device->driver;
1064 * udev_device_get_devnum:
1065 * @udev_device: udev device
1067 * Returns: the device major/minor number.
1069 dev_t udev_device_get_devnum(struct udev_device *udev_device)
1071 if (udev_device == NULL)
1072 return makedev(0, 0);
1073 if (!udev_device->info_loaded)
1074 udev_device_read_uevent_file(udev_device);
1075 return udev_device->devnum;
1079 * udev_device_get_action:
1080 * @udev_device: udev device
1082 * This is only valid if the device was received through a monitor. Devices read from
1083 * sys do not have an action string. Usual actions are: add, remove, change, online,
1086 * Returns: the kernel action value, or #NULL if there is no action value available.
1088 const char *udev_device_get_action(struct udev_device *udev_device)
1090 if (udev_device == NULL)
1092 return udev_device->action;
1096 * udev_device_get_devnum:
1097 * @udev_device: udev device
1099 * This is only valid if the device was received through a monitor. Devices read from
1100 * sys do not have a sequence number.
1102 * Returns: the kernel event sequence number, or 0 if there is no sequence number available.
1104 unsigned long long int udev_device_get_seqnum(struct udev_device *udev_device)
1106 if (udev_device == NULL)
1108 return udev_device->seqnum;
1112 * udev_device_get_sysattr_value:
1113 * @udev_device: udev device
1114 * @sysattr: attribute name
1116 * The retrieved value is cached in the device. Repeated calls will return the same
1117 * value and not open the attribute again.
1119 * Returns: the content of a sys attribute file, or #NULL if there is no sys attribute value.
1121 const char *udev_device_get_sysattr_value(struct udev_device *udev_device, const char *sysattr)
1123 struct udev_list_entry *list_entry;
1124 char path[UTIL_PATH_SIZE];
1126 struct stat statbuf;
1129 const char *val = NULL;
1131 if (udev_device == NULL)
1133 if (sysattr == NULL)
1136 /* look for possibly already cached result */
1137 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_device->sysattr_list)) {
1138 if (strcmp(udev_list_entry_get_name(list_entry), sysattr) == 0) {
1139 dbg(udev_device->udev, "got '%s' (%s) from cache\n",
1140 sysattr, udev_list_entry_get_value(list_entry));
1141 return udev_list_entry_get_value(list_entry);
1145 util_strscpyl(path, sizeof(path), udev_device_get_syspath(udev_device), "/", sysattr, NULL);
1146 if (lstat(path, &statbuf) != 0) {
1147 dbg(udev_device->udev, "no attribute '%s', keep negative entry\n", path);
1148 udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, NULL, 0, 0);
1152 if (S_ISLNK(statbuf.st_mode)) {
1153 char target[UTIL_NAME_SIZE];
1157 /* some core links return the last element of the target path */
1158 if (strcmp(sysattr, "driver") != 0 &&
1159 strcmp(sysattr, "subsystem") != 0 &&
1160 strcmp(sysattr, "module") != 0)
1163 len = readlink(path, target, sizeof(target));
1164 if (len <= 0 || len == sizeof(target))
1168 pos = strrchr(target, '/');
1171 dbg(udev_device->udev, "cache '%s' with link value '%s'\n", sysattr, pos);
1172 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, pos, 0, 0);
1173 val = udev_list_entry_get_value(list_entry);
1179 /* skip directories */
1180 if (S_ISDIR(statbuf.st_mode))
1183 /* skip non-readable files */
1184 if ((statbuf.st_mode & S_IRUSR) == 0)
1187 /* read attribute value */
1188 fd = open(path, O_RDONLY|O_CLOEXEC);
1190 dbg(udev_device->udev, "attribute '%s' can not be opened\n", path);
1193 size = read(fd, value, sizeof(value));
1197 if (size == sizeof(value))
1200 /* got a valid value, store it in cache and return it */
1202 util_remove_trailing_chars(value, '\n');
1203 dbg(udev_device->udev, "'%s' has attribute value '%s'\n", path, value);
1204 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, value, 0, 0);
1205 val = udev_list_entry_get_value(list_entry);
1210 int udev_device_set_syspath(struct udev_device *udev_device, const char *syspath)
1215 free(udev_device->syspath);
1216 udev_device->syspath = strdup(syspath);
1217 if (udev_device->syspath == NULL)
1219 udev_device->devpath = &udev_device->syspath[strlen(udev_get_sys_path(udev_device->udev))];
1220 udev_device_add_property(udev_device, "DEVPATH", udev_device->devpath);
1222 pos = strrchr(udev_device->syspath, '/');
1225 udev_device->sysname = strdup(&pos[1]);
1226 if (udev_device->sysname == NULL)
1229 /* some devices have '!' in their name, change that to '/' */
1231 while (udev_device->sysname[len] != '\0') {
1232 if (udev_device->sysname[len] == '!')
1233 udev_device->sysname[len] = '/';
1237 /* trailing number */
1238 while (len > 0 && isdigit(udev_device->sysname[--len]))
1239 udev_device->sysnum = &udev_device->sysname[len];
1241 /* sysname is completely numeric */
1243 udev_device->sysnum = NULL;
1248 int udev_device_set_subsystem(struct udev_device *udev_device, const char *subsystem)
1250 free(udev_device->subsystem);
1251 udev_device->subsystem = strdup(subsystem);
1252 if (udev_device->subsystem == NULL)
1254 udev_device->subsystem_set = true;
1255 udev_device_add_property(udev_device, "SUBSYSTEM", udev_device->subsystem);
1259 int udev_device_set_devtype(struct udev_device *udev_device, const char *devtype)
1261 free(udev_device->devtype);
1262 udev_device->devtype = strdup(devtype);
1263 if (udev_device->devtype == NULL)
1265 udev_device->devtype_set = true;
1266 udev_device_add_property(udev_device, "DEVTYPE", udev_device->devtype);
1270 int udev_device_set_devnode(struct udev_device *udev_device, const char *devnode)
1272 free(udev_device->devnode);
1273 udev_device->devnode = strdup(devnode);
1274 if (devnode == NULL)
1276 if (udev_device->devnode == NULL)
1278 udev_device_add_property(udev_device, "DEVNAME", udev_device->devnode);
1282 int udev_device_add_devlink(struct udev_device *udev_device, const char *devlink, int unique)
1284 struct udev_list_entry *list_entry;
1286 udev_device->devlinks_uptodate = false;
1287 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->devlinks_list, devlink, NULL, 1, 0);
1288 if (list_entry == NULL)
1291 udev_list_entry_set_flags(list_entry, 1);
1295 const char *udev_device_get_id_filename(struct udev_device *udev_device)
1297 if (udev_device->id_filename == NULL) {
1298 if (udev_device_get_subsystem(udev_device) == NULL)
1301 if (major(udev_device_get_devnum(udev_device)) > 0) {
1302 /* use dev_t -- b259:131072, c254:0 */
1303 if (asprintf(&udev_device->id_filename, "%c%u:%u",
1304 strcmp(udev_device_get_subsystem(udev_device), "block") == 0 ? 'b' : 'c',
1305 major(udev_device_get_devnum(udev_device)),
1306 minor(udev_device_get_devnum(udev_device))) < 0)
1307 udev_device->id_filename = NULL;
1308 } else if (strcmp(udev_device_get_subsystem(udev_device), "net") == 0) {
1309 /* use netdev ifindex -- n3 */
1310 if (asprintf(&udev_device->id_filename, "n%u", udev_device_get_ifindex(udev_device)) < 0)
1311 udev_device->id_filename = NULL;
1314 * use $subsys:$syname -- pci:0000:00:1f.2
1315 * sysname() has '!' translated, get it from devpath
1317 const char *sysname;
1318 sysname = strrchr(udev_device->devpath, '/');
1319 if (sysname == NULL)
1321 sysname = &sysname[1];
1322 if (asprintf(&udev_device->id_filename, "+%s:%s", udev_device_get_subsystem(udev_device), sysname) < 0)
1323 udev_device->id_filename = NULL;
1326 return udev_device->id_filename;
1329 int udev_device_add_tag(struct udev_device *udev_device, const char *tag)
1331 if (strchr(tag, ':') != NULL || strchr(tag, ' ') != NULL)
1333 udev_device->tags_uptodate = false;
1334 if (udev_list_entry_add(udev_device->udev, &udev_device->tags_list, tag, NULL, 1, 0) != NULL)
1339 void udev_device_cleanup_tags_list(struct udev_device *udev_device)
1341 udev_device->tags_uptodate = false;
1342 udev_list_cleanup_entries(udev_device->udev, &udev_device->tags_list);
1346 * udev_device_get_tags_list_entry:
1347 * @udev_device: udev device
1349 * Retrieve the list of tags attached to the udev device. The next
1350 * list entry can be retrieved with udev_list_entry_next(),
1351 * which returns #NULL if no more entries exist. The tag string
1352 * can be retrieved from the list entry by udev_list_get_name().
1354 * Returns: the first entry of the tag list
1356 struct udev_list_entry *udev_device_get_tags_list_entry(struct udev_device *udev_device)
1358 if (udev_device == NULL)
1360 return udev_list_get_entry(&udev_device->tags_list);
1363 int udev_device_has_tag(struct udev_device *udev_device, const char *tag)
1365 struct udev_list_entry *list_entry;
1367 if (!udev_device->info_loaded)
1368 udev_device_read_db(udev_device);
1369 list_entry = udev_device_get_tags_list_entry(udev_device);
1370 list_entry = udev_list_entry_get_by_name(list_entry, tag);
1371 if (list_entry != NULL)
1376 #define ENVP_SIZE 128
1377 #define MONITOR_BUF_SIZE 4096
1378 static int update_envp_monitor_buf(struct udev_device *udev_device)
1380 struct udev_list_entry *list_entry;
1385 /* monitor buffer of property strings */
1386 free(udev_device->monitor_buf);
1387 udev_device->monitor_buf_len = 0;
1388 udev_device->monitor_buf = malloc(MONITOR_BUF_SIZE);
1389 if (udev_device->monitor_buf == NULL)
1392 /* envp array, strings will point into monitor buffer */
1393 if (udev_device->envp == NULL)
1394 udev_device->envp = malloc(sizeof(char *) * ENVP_SIZE);
1395 if (udev_device->envp == NULL)
1399 s = udev_device->monitor_buf;
1400 l = MONITOR_BUF_SIZE;
1401 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device)) {
1404 key = udev_list_entry_get_name(list_entry);
1405 /* skip private variables */
1409 /* add string to envp array */
1410 udev_device->envp[i++] = s;
1411 if (i+1 >= ENVP_SIZE)
1414 /* add property string to monitor buffer */
1415 l = util_strpcpyl(&s, l, key, "=", udev_list_entry_get_value(list_entry), NULL);
1418 /* advance past the trailing '\0' that util_strpcpyl() guarantees */
1422 udev_device->envp[i] = NULL;
1423 udev_device->monitor_buf_len = s - udev_device->monitor_buf;
1424 udev_device->envp_uptodate = true;
1425 dbg(udev_device->udev, "filled envp/monitor buffer, %u properties, %zu bytes\n",
1426 i, udev_device->monitor_buf_len);
1430 char **udev_device_get_properties_envp(struct udev_device *udev_device)
1432 if (!udev_device->envp_uptodate)
1433 if (update_envp_monitor_buf(udev_device) != 0)
1435 return udev_device->envp;
1438 ssize_t udev_device_get_properties_monitor_buf(struct udev_device *udev_device, const char **buf)
1440 if (!udev_device->envp_uptodate)
1441 if (update_envp_monitor_buf(udev_device) != 0)
1443 *buf = udev_device->monitor_buf;
1444 return udev_device->monitor_buf_len;
1447 int udev_device_set_action(struct udev_device *udev_device, const char *action)
1449 free(udev_device->action);
1450 udev_device->action = strdup(action);
1451 if (udev_device->action == NULL)
1453 udev_device_add_property(udev_device, "ACTION", udev_device->action);
1457 int udev_device_set_driver(struct udev_device *udev_device, const char *driver)
1459 free(udev_device->driver);
1460 udev_device->driver = strdup(driver);
1461 if (udev_device->driver == NULL)
1463 udev_device->driver_set = true;
1464 udev_device_add_property(udev_device, "DRIVER", udev_device->driver);
1468 const char *udev_device_get_devpath_old(struct udev_device *udev_device)
1470 return udev_device->devpath_old;
1473 int udev_device_set_devpath_old(struct udev_device *udev_device, const char *devpath_old)
1478 free(udev_device->devpath_old);
1479 udev_device->devpath_old = strdup(devpath_old);
1480 if (udev_device->devpath_old == NULL)
1482 udev_device_add_property(udev_device, "DEVPATH_OLD", udev_device->devpath_old);
1484 pos = strrchr(udev_device->devpath_old, '/');
1487 udev_device->sysname_old = strdup(&pos[1]);
1488 if (udev_device->sysname_old == NULL)
1491 /* some devices have '!' in their name, change that to '/' */
1493 while (udev_device->sysname_old[len] != '\0') {
1494 if (udev_device->sysname_old[len] == '!')
1495 udev_device->sysname_old[len] = '/';
1501 const char *udev_device_get_sysname_old(struct udev_device *udev_device)
1503 if (udev_device == NULL)
1505 return udev_device->sysname_old;
1508 const char *udev_device_get_knodename(struct udev_device *udev_device)
1510 return udev_device->knodename;
1513 int udev_device_set_knodename(struct udev_device *udev_device, const char *knodename)
1515 free(udev_device->knodename);
1516 udev_device->knodename = strdup(knodename);
1517 if (udev_device->knodename == NULL)
1519 udev_device_add_property(udev_device, "DEVNAME", udev_device->knodename);
1523 int udev_device_get_timeout(struct udev_device *udev_device)
1525 return udev_device->timeout;
1528 int udev_device_set_timeout(struct udev_device *udev_device, int timeout)
1530 udev_device->timeout = timeout;
1533 int udev_device_get_event_timeout(struct udev_device *udev_device)
1535 if (!udev_device->info_loaded)
1536 udev_device_read_db(udev_device);
1537 return udev_device->event_timeout;
1540 int udev_device_set_event_timeout(struct udev_device *udev_device, int event_timeout)
1544 udev_device->event_timeout = event_timeout;
1545 snprintf(num, sizeof(num), "%u", event_timeout);
1546 udev_device_add_property(udev_device, "TIMEOUT", num);
1550 int udev_device_set_seqnum(struct udev_device *udev_device, unsigned long long int seqnum)
1554 udev_device->seqnum = seqnum;
1555 snprintf(num, sizeof(num), "%llu", seqnum);
1556 udev_device_add_property(udev_device, "SEQNUM", num);
1560 int udev_device_set_devnum(struct udev_device *udev_device, dev_t devnum)
1564 udev_device->devnum = devnum;
1566 snprintf(num, sizeof(num), "%u", major(devnum));
1567 udev_device_add_property(udev_device, "MAJOR", num);
1568 snprintf(num, sizeof(num), "%u", minor(devnum));
1569 udev_device_add_property(udev_device, "MINOR", num);
1573 int udev_device_get_devlink_priority(struct udev_device *udev_device)
1575 if (!udev_device->info_loaded)
1576 udev_device_read_db(udev_device);
1577 return udev_device->devlink_priority;
1580 int udev_device_set_devlink_priority(struct udev_device *udev_device, int prio)
1582 udev_device->devlink_priority = prio;
1586 int udev_device_get_watch_handle(struct udev_device *udev_device)
1588 if (!udev_device->info_loaded)
1589 udev_device_read_db(udev_device);
1590 return udev_device->watch_handle;
1593 int udev_device_set_watch_handle(struct udev_device *udev_device, int handle)
1595 udev_device->watch_handle = handle;
1599 int udev_device_get_ifindex(struct udev_device *udev_device)
1601 return udev_device->ifindex;
1604 int udev_device_set_ifindex(struct udev_device *udev_device, int ifindex)
1608 udev_device->ifindex = ifindex;
1609 snprintf(num, sizeof(num), "%u", ifindex);
1610 udev_device_add_property(udev_device, "IFINDEX", num);