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_uevent_file(udev_device);
881 udev_device_read_db(udev_device);
884 /* we might get called before we handled an event and have a db, use the kernel-provided name */
885 if (udev_device->devnode == NULL && udev_device_get_knodename(udev_device) != NULL) {
886 if (asprintf(&udev_device->devnode, "%s/%s",
887 udev_get_dev_path(udev_device->udev), udev_device_get_knodename(udev_device)) < 0)
889 return udev_device->devnode;
892 return udev_device->devnode;
896 * udev_device_get_subsystem:
897 * @udev_device: udev device
899 * Retrieve the subsystem string of the udev device. The string does not
902 * Returns: the subsystem name of the udev device, or #NULL if it can not be determined
904 const char *udev_device_get_subsystem(struct udev_device *udev_device)
906 char subsystem[UTIL_NAME_SIZE];
908 if (udev_device == NULL)
910 if (!udev_device->subsystem_set) {
911 udev_device->subsystem_set = true;
912 /* read "subsystem" link */
913 if (util_get_sys_subsystem(udev_device->udev, udev_device->syspath, subsystem, sizeof(subsystem)) > 0) {
914 udev_device_set_subsystem(udev_device, subsystem);
915 return udev_device->subsystem;
918 if (strncmp(udev_device->devpath, "/module/", 8) == 0) {
919 udev_device_set_subsystem(udev_device, "module");
920 return udev_device->subsystem;
922 if (strstr(udev_device->devpath, "/drivers/") != NULL) {
923 udev_device_set_subsystem(udev_device, "drivers");
924 return udev_device->subsystem;
926 if (strncmp(udev_device->devpath, "/subsystem/", 11) == 0 ||
927 strncmp(udev_device->devpath, "/class/", 7) == 0 ||
928 strncmp(udev_device->devpath, "/bus/", 5) == 0) {
929 udev_device_set_subsystem(udev_device, "subsystem");
930 return udev_device->subsystem;
933 return udev_device->subsystem;
937 * udev_device_get_devtype:
938 * @udev_device: udev device
940 * Retrieve the devtype string of the udev device.
942 * Returns: the devtype name of the udev device, or #NULL if it can not be determined
944 const char *udev_device_get_devtype(struct udev_device *udev_device)
946 if (udev_device == NULL)
948 if (!udev_device->devtype_set) {
949 udev_device->devtype_set = true;
950 udev_device_read_uevent_file(udev_device);
952 return udev_device->devtype;
956 * udev_device_get_devlinks_list_entry:
957 * @udev_device: udev device
959 * Retrieve the list of device links pointing to the device file of
960 * the udev device. The next list entry can be retrieved with
961 * udev_list_entry_next(), which returns #NULL if no more entries exist.
962 * The devlink path can be retrieved from the list entry by
963 * udev_list_entry_get_name(). The path is an absolute path, and starts with
964 * the device directory.
966 * Returns: the first entry of the device node link list
968 struct udev_list_entry *udev_device_get_devlinks_list_entry(struct udev_device *udev_device)
970 if (udev_device == NULL)
972 if (!udev_device->info_loaded)
973 udev_device_read_db(udev_device);
974 return udev_list_get_entry(&udev_device->devlinks_list);
977 void udev_device_cleanup_devlinks_list(struct udev_device *udev_device)
979 udev_device->devlinks_uptodate = false;
980 udev_list_cleanup_entries(udev_device->udev, &udev_device->devlinks_list);
984 * udev_device_get_properties_list_entry:
985 * @udev_device: udev device
987 * Retrieve the list of key/value device properties of the udev
988 * device. The next list entry can be retrieved with udev_list_entry_next(),
989 * which returns #NULL if no more entries exist. The property name
990 * can be retrieved from the list entry by udev_list_get_name(),
991 * the property value by udev_list_get_value().
993 * Returns: the first entry of the property list
995 struct udev_list_entry *udev_device_get_properties_list_entry(struct udev_device *udev_device)
997 if (udev_device == NULL)
999 if (!udev_device->info_loaded) {
1000 udev_device_read_uevent_file(udev_device);
1001 udev_device_read_db(udev_device);
1003 if (!udev_device->devlinks_uptodate) {
1004 char symlinks[UTIL_PATH_SIZE];
1005 struct udev_list_entry *list_entry;
1007 udev_device->devlinks_uptodate = true;
1008 list_entry = udev_device_get_devlinks_list_entry(udev_device);
1009 if (list_entry != NULL) {
1014 l = util_strpcpyl(&s, sizeof(symlinks), udev_list_entry_get_name(list_entry), NULL);
1015 udev_list_entry_foreach(list_entry, udev_list_entry_get_next(list_entry))
1016 l = util_strpcpyl(&s, l, " ", udev_list_entry_get_name(list_entry), NULL);
1017 udev_device_add_property(udev_device, "DEVLINKS", symlinks);
1020 if (!udev_device->tags_uptodate) {
1021 udev_device->tags_uptodate = true;
1022 if (udev_device_get_tags_list_entry(udev_device) != NULL) {
1023 char tags[UTIL_PATH_SIZE];
1024 struct udev_list_entry *list_entry;
1029 l = util_strpcpyl(&s, sizeof(tags), ":", NULL);
1030 udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(udev_device))
1031 l = util_strpcpyl(&s, l, udev_list_entry_get_name(list_entry), ":", NULL);
1032 udev_device_add_property(udev_device, "TAGS", tags);
1035 return udev_list_get_entry(&udev_device->properties_list);
1039 * udev_device_get_driver:
1040 * @udev_device: udev device
1042 * Returns: the driver string, or #NULL if there is no driver attached.
1044 const char *udev_device_get_driver(struct udev_device *udev_device)
1046 char driver[UTIL_NAME_SIZE];
1048 if (udev_device == NULL)
1050 if (!udev_device->driver_set) {
1051 udev_device->driver_set = true;
1052 if (util_get_sys_driver(udev_device->udev, udev_device->syspath, driver, sizeof(driver)) > 0)
1053 udev_device->driver = strdup(driver);
1055 return udev_device->driver;
1059 * udev_device_get_devnum:
1060 * @udev_device: udev device
1062 * Returns: the device major/minor number.
1064 dev_t udev_device_get_devnum(struct udev_device *udev_device)
1066 if (udev_device == NULL)
1067 return makedev(0, 0);
1068 if (!udev_device->info_loaded)
1069 udev_device_read_uevent_file(udev_device);
1070 return udev_device->devnum;
1074 * udev_device_get_action:
1075 * @udev_device: udev device
1077 * This is only valid if the device was received through a monitor. Devices read from
1078 * sys do not have an action string. Usual actions are: add, remove, change, online,
1081 * Returns: the kernel action value, or #NULL if there is no action value available.
1083 const char *udev_device_get_action(struct udev_device *udev_device)
1085 if (udev_device == NULL)
1087 return udev_device->action;
1091 * udev_device_get_devnum:
1092 * @udev_device: udev device
1094 * This is only valid if the device was received through a monitor. Devices read from
1095 * sys do not have a sequence number.
1097 * Returns: the kernel event sequence number, or 0 if there is no sequence number available.
1099 unsigned long long int udev_device_get_seqnum(struct udev_device *udev_device)
1101 if (udev_device == NULL)
1103 return udev_device->seqnum;
1107 * udev_device_get_sysattr_value:
1108 * @udev_device: udev device
1109 * @sysattr: attribute name
1111 * The retrieved value is cached in the device. Repeated calls will return the same
1112 * value and not open the attribute again.
1114 * Returns: the content of a sys attribute file, or #NULL if there is no sys attribute value.
1116 const char *udev_device_get_sysattr_value(struct udev_device *udev_device, const char *sysattr)
1118 struct udev_list_entry *list_entry;
1119 char path[UTIL_PATH_SIZE];
1121 struct stat statbuf;
1124 const char *val = NULL;
1126 if (udev_device == NULL)
1128 if (sysattr == NULL)
1131 /* look for possibly already cached result */
1132 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_device->sysattr_list)) {
1133 if (strcmp(udev_list_entry_get_name(list_entry), sysattr) == 0) {
1134 dbg(udev_device->udev, "got '%s' (%s) from cache\n",
1135 sysattr, udev_list_entry_get_value(list_entry));
1136 return udev_list_entry_get_value(list_entry);
1140 util_strscpyl(path, sizeof(path), udev_device_get_syspath(udev_device), "/", sysattr, NULL);
1141 if (lstat(path, &statbuf) != 0) {
1142 dbg(udev_device->udev, "no attribute '%s', keep negative entry\n", path);
1143 udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, NULL, 0, 0);
1147 if (S_ISLNK(statbuf.st_mode)) {
1148 char target[UTIL_NAME_SIZE];
1152 /* some core links return the last element of the target path */
1153 if (strcmp(sysattr, "driver") != 0 &&
1154 strcmp(sysattr, "subsystem") != 0 &&
1155 strcmp(sysattr, "module") != 0)
1158 len = readlink(path, target, sizeof(target));
1159 if (len <= 0 || len == sizeof(target))
1163 pos = strrchr(target, '/');
1166 dbg(udev_device->udev, "cache '%s' with link value '%s'\n", sysattr, pos);
1167 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, pos, 0, 0);
1168 val = udev_list_entry_get_value(list_entry);
1174 /* skip directories */
1175 if (S_ISDIR(statbuf.st_mode))
1178 /* skip non-readable files */
1179 if ((statbuf.st_mode & S_IRUSR) == 0)
1182 /* read attribute value */
1183 fd = open(path, O_RDONLY|O_CLOEXEC);
1185 dbg(udev_device->udev, "attribute '%s' can not be opened\n", path);
1188 size = read(fd, value, sizeof(value));
1192 if (size == sizeof(value))
1195 /* got a valid value, store it in cache and return it */
1197 util_remove_trailing_chars(value, '\n');
1198 dbg(udev_device->udev, "'%s' has attribute value '%s'\n", path, value);
1199 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, value, 0, 0);
1200 val = udev_list_entry_get_value(list_entry);
1205 int udev_device_set_syspath(struct udev_device *udev_device, const char *syspath)
1210 free(udev_device->syspath);
1211 udev_device->syspath = strdup(syspath);
1212 if (udev_device->syspath == NULL)
1214 udev_device->devpath = &udev_device->syspath[strlen(udev_get_sys_path(udev_device->udev))];
1215 udev_device_add_property(udev_device, "DEVPATH", udev_device->devpath);
1217 pos = strrchr(udev_device->syspath, '/');
1220 udev_device->sysname = strdup(&pos[1]);
1221 if (udev_device->sysname == NULL)
1224 /* some devices have '!' in their name, change that to '/' */
1226 while (udev_device->sysname[len] != '\0') {
1227 if (udev_device->sysname[len] == '!')
1228 udev_device->sysname[len] = '/';
1232 /* trailing number */
1233 while (len > 0 && isdigit(udev_device->sysname[--len]))
1234 udev_device->sysnum = &udev_device->sysname[len];
1236 /* sysname is completely numeric */
1238 udev_device->sysnum = NULL;
1243 int udev_device_set_subsystem(struct udev_device *udev_device, const char *subsystem)
1245 free(udev_device->subsystem);
1246 udev_device->subsystem = strdup(subsystem);
1247 if (udev_device->subsystem == NULL)
1249 udev_device->subsystem_set = true;
1250 udev_device_add_property(udev_device, "SUBSYSTEM", udev_device->subsystem);
1254 int udev_device_set_devtype(struct udev_device *udev_device, const char *devtype)
1256 free(udev_device->devtype);
1257 udev_device->devtype = strdup(devtype);
1258 if (udev_device->devtype == NULL)
1260 udev_device->devtype_set = true;
1261 udev_device_add_property(udev_device, "DEVTYPE", udev_device->devtype);
1265 int udev_device_set_devnode(struct udev_device *udev_device, const char *devnode)
1267 free(udev_device->devnode);
1268 udev_device->devnode = strdup(devnode);
1269 if (devnode == NULL)
1271 if (udev_device->devnode == NULL)
1273 udev_device_add_property(udev_device, "DEVNAME", udev_device->devnode);
1277 int udev_device_add_devlink(struct udev_device *udev_device, const char *devlink, int unique)
1279 struct udev_list_entry *list_entry;
1281 udev_device->devlinks_uptodate = false;
1282 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->devlinks_list, devlink, NULL, 1, 0);
1283 if (list_entry == NULL)
1286 udev_list_entry_set_flags(list_entry, 1);
1290 int udev_device_add_tag(struct udev_device *udev_device, const char *tag)
1292 if (strchr(tag, ':') != NULL || strchr(tag, ' ') != NULL)
1294 udev_device->tags_uptodate = false;
1295 if (udev_list_entry_add(udev_device->udev, &udev_device->tags_list, tag, NULL, 1, 0) != NULL)
1300 void udev_device_cleanup_tags_list(struct udev_device *udev_device)
1302 udev_device->tags_uptodate = false;
1303 udev_list_cleanup_entries(udev_device->udev, &udev_device->tags_list);
1307 * udev_device_get_tags_list_entry:
1308 * @udev_device: udev device
1310 * Retrieve the list of tags attached to the udev device. The next
1311 * list entry can be retrieved with udev_list_entry_next(),
1312 * which returns #NULL if no more entries exist. The tag string
1313 * can be retrieved from the list entry by udev_list_get_name().
1315 * Returns: the first entry of the tag list
1317 struct udev_list_entry *udev_device_get_tags_list_entry(struct udev_device *udev_device)
1319 if (udev_device == NULL)
1321 return udev_list_get_entry(&udev_device->tags_list);
1324 int udev_device_has_tag(struct udev_device *udev_device, const char *tag)
1326 struct udev_list_entry *list_entry;
1328 if (!udev_device->info_loaded)
1329 udev_device_read_db(udev_device);
1330 list_entry = udev_device_get_tags_list_entry(udev_device);
1331 list_entry = udev_list_entry_get_by_name(list_entry, tag);
1332 if (list_entry != NULL)
1337 #define ENVP_SIZE 128
1338 #define MONITOR_BUF_SIZE 4096
1339 static int update_envp_monitor_buf(struct udev_device *udev_device)
1341 struct udev_list_entry *list_entry;
1346 /* monitor buffer of property strings */
1347 free(udev_device->monitor_buf);
1348 udev_device->monitor_buf_len = 0;
1349 udev_device->monitor_buf = malloc(MONITOR_BUF_SIZE);
1350 if (udev_device->monitor_buf == NULL)
1353 /* envp array, strings will point into monitor buffer */
1354 if (udev_device->envp == NULL)
1355 udev_device->envp = malloc(sizeof(char *) * ENVP_SIZE);
1356 if (udev_device->envp == NULL)
1360 s = udev_device->monitor_buf;
1361 l = MONITOR_BUF_SIZE;
1362 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device)) {
1365 key = udev_list_entry_get_name(list_entry);
1366 /* skip private variables */
1370 /* add string to envp array */
1371 udev_device->envp[i++] = s;
1372 if (i+1 >= ENVP_SIZE)
1375 /* add property string to monitor buffer */
1376 l = util_strpcpyl(&s, l, key, "=", udev_list_entry_get_value(list_entry), NULL);
1379 /* advance past the trailing '\0' that util_strpcpyl() guarantees */
1383 udev_device->envp[i] = NULL;
1384 udev_device->monitor_buf_len = s - udev_device->monitor_buf;
1385 udev_device->envp_uptodate = true;
1386 dbg(udev_device->udev, "filled envp/monitor buffer, %u properties, %zu bytes\n",
1387 i, udev_device->monitor_buf_len);
1391 char **udev_device_get_properties_envp(struct udev_device *udev_device)
1393 if (!udev_device->envp_uptodate)
1394 if (update_envp_monitor_buf(udev_device) != 0)
1396 return udev_device->envp;
1399 ssize_t udev_device_get_properties_monitor_buf(struct udev_device *udev_device, const char **buf)
1401 if (!udev_device->envp_uptodate)
1402 if (update_envp_monitor_buf(udev_device) != 0)
1404 *buf = udev_device->monitor_buf;
1405 return udev_device->monitor_buf_len;
1408 int udev_device_set_action(struct udev_device *udev_device, const char *action)
1410 free(udev_device->action);
1411 udev_device->action = strdup(action);
1412 if (udev_device->action == NULL)
1414 udev_device_add_property(udev_device, "ACTION", udev_device->action);
1418 int udev_device_set_driver(struct udev_device *udev_device, const char *driver)
1420 free(udev_device->driver);
1421 udev_device->driver = strdup(driver);
1422 if (udev_device->driver == NULL)
1424 udev_device->driver_set = true;
1425 udev_device_add_property(udev_device, "DRIVER", udev_device->driver);
1429 const char *udev_device_get_devpath_old(struct udev_device *udev_device)
1431 return udev_device->devpath_old;
1434 int udev_device_set_devpath_old(struct udev_device *udev_device, const char *devpath_old)
1439 free(udev_device->devpath_old);
1440 udev_device->devpath_old = strdup(devpath_old);
1441 if (udev_device->devpath_old == NULL)
1443 udev_device_add_property(udev_device, "DEVPATH_OLD", udev_device->devpath_old);
1445 pos = strrchr(udev_device->devpath_old, '/');
1448 udev_device->sysname_old = strdup(&pos[1]);
1449 if (udev_device->sysname_old == NULL)
1452 /* some devices have '!' in their name, change that to '/' */
1454 while (udev_device->sysname_old[len] != '\0') {
1455 if (udev_device->sysname_old[len] == '!')
1456 udev_device->sysname_old[len] = '/';
1462 const char *udev_device_get_sysname_old(struct udev_device *udev_device)
1464 if (udev_device == NULL)
1466 return udev_device->sysname_old;
1469 const char *udev_device_get_knodename(struct udev_device *udev_device)
1471 return udev_device->knodename;
1474 int udev_device_set_knodename(struct udev_device *udev_device, const char *knodename)
1476 free(udev_device->knodename);
1477 udev_device->knodename = strdup(knodename);
1478 if (udev_device->knodename == NULL)
1480 udev_device_add_property(udev_device, "DEVNAME", udev_device->knodename);
1484 int udev_device_get_timeout(struct udev_device *udev_device)
1486 return udev_device->timeout;
1489 int udev_device_set_timeout(struct udev_device *udev_device, int timeout)
1491 udev_device->timeout = timeout;
1494 int udev_device_get_event_timeout(struct udev_device *udev_device)
1496 if (!udev_device->info_loaded)
1497 udev_device_read_db(udev_device);
1498 return udev_device->event_timeout;
1501 int udev_device_set_event_timeout(struct udev_device *udev_device, int event_timeout)
1503 udev_device->event_timeout = event_timeout;
1507 int udev_device_set_seqnum(struct udev_device *udev_device, unsigned long long int seqnum)
1511 udev_device->seqnum = seqnum;
1512 snprintf(num, sizeof(num), "%llu", seqnum);
1513 udev_device_add_property(udev_device, "SEQNUM", num);
1517 int udev_device_set_devnum(struct udev_device *udev_device, dev_t devnum)
1521 udev_device->devnum = devnum;
1523 snprintf(num, sizeof(num), "%u", major(devnum));
1524 udev_device_add_property(udev_device, "MAJOR", num);
1525 snprintf(num, sizeof(num), "%u", minor(devnum));
1526 udev_device_add_property(udev_device, "MINOR", num);
1530 int udev_device_get_devlink_priority(struct udev_device *udev_device)
1532 if (!udev_device->info_loaded)
1533 udev_device_read_db(udev_device);
1534 return udev_device->devlink_priority;
1537 int udev_device_set_devlink_priority(struct udev_device *udev_device, int prio)
1539 udev_device->devlink_priority = prio;
1543 int udev_device_get_watch_handle(struct udev_device *udev_device)
1545 if (!udev_device->info_loaded)
1546 udev_device_read_db(udev_device);
1547 return udev_device->watch_handle;
1550 int udev_device_set_watch_handle(struct udev_device *udev_device, int handle)
1552 udev_device->watch_handle = handle;
1556 int udev_device_get_ifindex(struct udev_device *udev_device)
1558 return udev_device->ifindex;
1561 int udev_device_set_ifindex(struct udev_device *udev_device, int ifindex)
1563 udev_device->ifindex = ifindex;