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.
24 #include <sys/ioctl.h>
25 #include <sys/socket.h>
26 #include <linux/sockios.h>
29 #include "libudev-private.h"
32 * SECTION:libudev-device
33 * @short_description: kernel sys devices
35 * Representation of kernel sys devices. Devices are uniquely identified
36 * by their syspath, every device has exactly one path in the kernel sys
37 * filesystem. Devices usually belong to a kernel subsystem, and and have
38 * a unique name inside that subsystem.
44 * Opaque object representing one kernel sys device.
48 struct udev_device *parent_device;
63 size_t monitor_buf_len;
64 struct udev_list devlinks_list;
65 struct udev_list properties_list;
66 struct udev_list sysattr_value_list;
67 struct udev_list sysattr_list;
68 struct udev_list tags_list;
69 unsigned long long int seqnum;
70 unsigned long long int usec_initialized;
80 bool devlinks_uptodate;
88 bool sysattr_list_read;
93 * udev_device_get_seqnum:
94 * @udev_device: udev device
96 * This is only valid if the device was received through a monitor. Devices read from
97 * sys do not have a sequence number.
99 * Returns: the kernel event sequence number, or 0 if there is no sequence number available.
101 _public_ unsigned long long int udev_device_get_seqnum(struct udev_device *udev_device)
103 if (udev_device == NULL)
105 return udev_device->seqnum;
108 static int udev_device_set_seqnum(struct udev_device *udev_device, unsigned long long int seqnum)
112 udev_device->seqnum = seqnum;
113 snprintf(num, sizeof(num), "%llu", seqnum);
114 udev_device_add_property(udev_device, "SEQNUM", num);
118 int udev_device_get_ifindex(struct udev_device *udev_device)
120 if (!udev_device->info_loaded)
121 udev_device_read_uevent_file(udev_device);
122 return udev_device->ifindex;
125 static int udev_device_set_ifindex(struct udev_device *udev_device, int ifindex)
129 udev_device->ifindex = ifindex;
130 snprintf(num, sizeof(num), "%u", ifindex);
131 udev_device_add_property(udev_device, "IFINDEX", num);
136 * udev_device_get_devnum:
137 * @udev_device: udev device
139 * Get the device major/minor number.
141 * Returns: the dev_t number.
143 _public_ dev_t udev_device_get_devnum(struct udev_device *udev_device)
145 if (udev_device == NULL)
146 return makedev(0, 0);
147 if (!udev_device->info_loaded)
148 udev_device_read_uevent_file(udev_device);
149 return udev_device->devnum;
152 static int udev_device_set_devnum(struct udev_device *udev_device, dev_t devnum)
156 udev_device->devnum = devnum;
158 snprintf(num, sizeof(num), "%u", major(devnum));
159 udev_device_add_property(udev_device, "MAJOR", num);
160 snprintf(num, sizeof(num), "%u", minor(devnum));
161 udev_device_add_property(udev_device, "MINOR", num);
165 const char *udev_device_get_devpath_old(struct udev_device *udev_device)
167 return udev_device->devpath_old;
170 static int udev_device_set_devpath_old(struct udev_device *udev_device, const char *devpath_old)
174 free(udev_device->devpath_old);
175 udev_device->devpath_old = strdup(devpath_old);
176 if (udev_device->devpath_old == NULL)
178 udev_device_add_property(udev_device, "DEVPATH_OLD", udev_device->devpath_old);
180 pos = strrchr(udev_device->devpath_old, '/');
187 * udev_device_get_driver:
188 * @udev_device: udev device
190 * Get the kernel driver name.
192 * Returns: the driver name string, or #NULL if there is no driver attached.
194 _public_ const char *udev_device_get_driver(struct udev_device *udev_device)
196 char driver[UTIL_NAME_SIZE];
198 if (udev_device == NULL)
200 if (!udev_device->driver_set) {
201 udev_device->driver_set = true;
202 if (util_get_sys_core_link_value(udev_device->udev, "driver", udev_device->syspath, driver, sizeof(driver)) > 0)
203 udev_device->driver = strdup(driver);
205 return udev_device->driver;
208 static int udev_device_set_driver(struct udev_device *udev_device, const char *driver)
210 free(udev_device->driver);
211 udev_device->driver = strdup(driver);
212 if (udev_device->driver == NULL)
214 udev_device->driver_set = true;
215 udev_device_add_property(udev_device, "DRIVER", udev_device->driver);
220 * udev_device_get_devtype:
221 * @udev_device: udev device
223 * Retrieve the devtype string of the udev device.
225 * Returns: the devtype name of the udev device, or #NULL if it can not be determined
227 _public_ const char *udev_device_get_devtype(struct udev_device *udev_device)
229 if (udev_device == NULL)
231 if (!udev_device->devtype_set) {
232 udev_device->devtype_set = true;
233 udev_device_read_uevent_file(udev_device);
235 return udev_device->devtype;
238 static int udev_device_set_devtype(struct udev_device *udev_device, const char *devtype)
240 free(udev_device->devtype);
241 udev_device->devtype = strdup(devtype);
242 if (udev_device->devtype == NULL)
244 udev_device->devtype_set = true;
245 udev_device_add_property(udev_device, "DEVTYPE", udev_device->devtype);
249 static int udev_device_set_subsystem(struct udev_device *udev_device, const char *subsystem)
251 free(udev_device->subsystem);
252 udev_device->subsystem = strdup(subsystem);
253 if (udev_device->subsystem == NULL)
255 udev_device->subsystem_set = true;
256 udev_device_add_property(udev_device, "SUBSYSTEM", udev_device->subsystem);
261 * udev_device_get_subsystem:
262 * @udev_device: udev device
264 * Retrieve the subsystem string of the udev device. The string does not
267 * Returns: the subsystem name of the udev device, or #NULL if it can not be determined
269 _public_ const char *udev_device_get_subsystem(struct udev_device *udev_device)
271 char subsystem[UTIL_NAME_SIZE];
273 if (udev_device == NULL)
275 if (!udev_device->subsystem_set) {
276 udev_device->subsystem_set = true;
277 /* read "subsystem" link */
278 if (util_get_sys_core_link_value(udev_device->udev, "subsystem", udev_device->syspath, subsystem, sizeof(subsystem)) > 0) {
279 udev_device_set_subsystem(udev_device, subsystem);
280 return udev_device->subsystem;
283 if (startswith(udev_device->devpath, "/module/")) {
284 udev_device_set_subsystem(udev_device, "module");
285 return udev_device->subsystem;
287 if (strstr(udev_device->devpath, "/drivers/") != NULL) {
288 udev_device_set_subsystem(udev_device, "drivers");
289 return udev_device->subsystem;
291 if (startswith(udev_device->devpath, "/subsystem/") ||
292 startswith(udev_device->devpath, "/class/") ||
293 startswith(udev_device->devpath, "/bus/")) {
294 udev_device_set_subsystem(udev_device, "subsystem");
295 return udev_device->subsystem;
298 return udev_device->subsystem;
301 mode_t udev_device_get_devnode_mode(struct udev_device *udev_device)
303 if (!udev_device->info_loaded)
304 udev_device_read_uevent_file(udev_device);
305 return udev_device->devnode_mode;
308 static int udev_device_set_devnode_mode(struct udev_device *udev_device, mode_t mode)
312 udev_device->devnode_mode = mode;
313 snprintf(num, sizeof(num), "%#o", mode);
314 udev_device_add_property(udev_device, "DEVMODE", num);
318 struct udev_list_entry *udev_device_add_property(struct udev_device *udev_device, const char *key, const char *value)
320 udev_device->envp_uptodate = false;
322 struct udev_list_entry *list_entry;
324 list_entry = udev_device_get_properties_list_entry(udev_device);
325 list_entry = udev_list_entry_get_by_name(list_entry, key);
326 if (list_entry != NULL)
327 udev_list_entry_delete(list_entry);
330 return udev_list_entry_add(&udev_device->properties_list, key, value);
333 static struct udev_list_entry *udev_device_add_property_from_string(struct udev_device *udev_device, const char *property)
335 char name[UTIL_LINE_SIZE];
338 util_strscpy(name, sizeof(name), property);
339 val = strchr(name, '=');
346 return udev_device_add_property(udev_device, name, val);
350 * parse property string, and if needed, update internal values accordingly
352 * udev_device_add_property_from_string_parse_finish() needs to be
353 * called after adding properties, and its return value checked
355 * udev_device_set_info_loaded() needs to be set, to avoid trying
356 * to use a device without a DEVPATH set
358 void udev_device_add_property_from_string_parse(struct udev_device *udev_device, const char *property)
360 if (startswith(property, "DEVPATH=")) {
361 char path[UTIL_PATH_SIZE];
363 util_strscpyl(path, sizeof(path), TEST_PREFIX "/sys", &property[8], NULL);
364 udev_device_set_syspath(udev_device, path);
365 } else if (startswith(property, "SUBSYSTEM=")) {
366 udev_device_set_subsystem(udev_device, &property[10]);
367 } else if (startswith(property, "DEVTYPE=")) {
368 udev_device_set_devtype(udev_device, &property[8]);
369 } else if (startswith(property, "DEVNAME=")) {
370 udev_device_set_devnode(udev_device, &property[8]);
371 } else if (startswith(property, "DEVLINKS=")) {
372 char devlinks[UTIL_PATH_SIZE];
376 util_strscpy(devlinks, sizeof(devlinks), &property[9]);
378 next = strchr(slink, ' ');
379 while (next != NULL) {
381 udev_device_add_devlink(udev_device, slink, 0);
383 next = strchr(slink, ' ');
385 if (slink[0] != '\0')
386 udev_device_add_devlink(udev_device, slink, 0);
387 } else if (startswith(property, "TAGS=")) {
388 char tags[UTIL_PATH_SIZE];
391 util_strscpy(tags, sizeof(tags), &property[5]);
392 next = strchr(tags, ':');
395 while (next[0] != '\0') {
399 next = strchr(tag, ':');
404 udev_device_add_tag(udev_device, tag);
407 } else if (startswith(property, "USEC_INITIALIZED=")) {
408 udev_device_set_usec_initialized(udev_device, strtoull(&property[19], NULL, 10));
409 } else if (startswith(property, "DRIVER=")) {
410 udev_device_set_driver(udev_device, &property[7]);
411 } else if (startswith(property, "ACTION=")) {
412 udev_device_set_action(udev_device, &property[7]);
413 } else if (startswith(property, "MAJOR=")) {
414 udev_device->maj = strtoull(&property[6], NULL, 10);
415 } else if (startswith(property, "MINOR=")) {
416 udev_device->min = strtoull(&property[6], NULL, 10);
417 } else if (startswith(property, "DEVPATH_OLD=")) {
418 udev_device_set_devpath_old(udev_device, &property[12]);
419 } else if (startswith(property, "SEQNUM=")) {
420 udev_device_set_seqnum(udev_device, strtoull(&property[7], NULL, 10));
421 } else if (startswith(property, "IFINDEX=")) {
422 udev_device_set_ifindex(udev_device, strtoull(&property[8], NULL, 10));
423 } else if (startswith(property, "DEVMODE=")) {
424 udev_device_set_devnode_mode(udev_device, strtoul(&property[8], NULL, 8));
426 udev_device_add_property_from_string(udev_device, property);
430 int udev_device_add_property_from_string_parse_finish(struct udev_device *udev_device)
432 if (udev_device->maj > 0)
433 udev_device_set_devnum(udev_device, makedev(udev_device->maj, udev_device->min));
434 udev_device->maj = 0;
435 udev_device->min = 0;
437 if (udev_device->devpath == NULL || udev_device->subsystem == NULL)
443 * udev_device_get_property_value:
444 * @udev_device: udev device
445 * @key: property name
447 * Get the value of a given property.
449 * Returns: the property string, or #NULL if there is no such property.
451 _public_ const char *udev_device_get_property_value(struct udev_device *udev_device, const char *key)
453 struct udev_list_entry *list_entry;
455 if (udev_device == NULL)
460 list_entry = udev_device_get_properties_list_entry(udev_device);
461 list_entry = udev_list_entry_get_by_name(list_entry, key);
462 return udev_list_entry_get_value(list_entry);
465 int udev_device_read_db(struct udev_device *udev_device, const char *dbfile)
467 char filename[UTIL_PATH_SIZE];
468 char line[UTIL_LINE_SIZE];
471 /* providing a database file will always force-load it */
472 if (dbfile == NULL) {
475 if (udev_device->db_loaded)
477 udev_device->db_loaded = true;
479 id = udev_device_get_id_filename(udev_device);
482 util_strscpyl(filename, sizeof(filename), TEST_PREFIX "/run/udev/data/", id, NULL);
486 f = fopen(dbfile, "re");
488 udev_dbg(udev_device->udev, "no db file to read %s: %m\n", dbfile);
491 udev_device->is_initialized = true;
493 while (fgets(line, sizeof(line), f)) {
496 struct udev_list_entry *entry;
505 util_strscpyl(filename, sizeof(filename), TEST_PREFIX "/dev/", val, NULL);
506 udev_device_add_devlink(udev_device, filename, 0);
509 udev_device_set_devlink_priority(udev_device, atoi(val));
512 entry = udev_device_add_property_from_string(udev_device, val);
513 udev_list_entry_set_num(entry, true);
516 udev_device_add_tag(udev_device, val);
519 udev_device_set_watch_handle(udev_device, atoi(val));
522 udev_device_set_usec_initialized(udev_device, strtoull(val, NULL, 10));
528 udev_dbg(udev_device->udev, "device %p filled with db file data\n", udev_device);
532 int udev_device_read_uevent_file(struct udev_device *udev_device)
534 char filename[UTIL_PATH_SIZE];
536 char line[UTIL_LINE_SIZE];
540 if (udev_device->uevent_loaded)
543 util_strscpyl(filename, sizeof(filename), udev_device->syspath, "/uevent", NULL);
544 f = fopen(filename, "re");
547 udev_device->uevent_loaded = true;
549 while (fgets(line, sizeof(line), f)) {
552 pos = strchr(line, '\n');
557 if (startswith(line, "DEVTYPE=")) {
558 udev_device_set_devtype(udev_device, &line[8]);
561 if (startswith(line, "IFINDEX=")) {
562 udev_device_set_ifindex(udev_device, strtoull(&line[8], NULL, 10));
565 if (startswith(line, "DEVNAME=")) {
566 udev_device_set_devnode(udev_device, &line[8]);
570 if (startswith(line, "MAJOR="))
571 maj = strtoull(&line[6], NULL, 10);
572 else if (startswith(line, "MINOR="))
573 min = strtoull(&line[6], NULL, 10);
574 else if (startswith(line, "DEVMODE="))
575 udev_device->devnode_mode = strtoul(&line[8], NULL, 8);
577 udev_device_add_property_from_string(udev_device, line);
580 udev_device->devnum = makedev(maj, min);
585 void udev_device_set_info_loaded(struct udev_device *device)
587 device->info_loaded = true;
590 struct udev_device *udev_device_new(struct udev *udev)
592 struct udev_device *udev_device;
593 struct udev_list_entry *list_entry;
598 udev_device = calloc(1, sizeof(struct udev_device));
599 if (udev_device == NULL)
601 udev_device->refcount = 1;
602 udev_device->udev = udev;
603 udev_list_init(udev, &udev_device->devlinks_list, true);
604 udev_list_init(udev, &udev_device->properties_list, true);
605 udev_list_init(udev, &udev_device->sysattr_value_list, true);
606 udev_list_init(udev, &udev_device->sysattr_list, false);
607 udev_list_init(udev, &udev_device->tags_list, true);
608 udev_device->watch_handle = -1;
609 /* copy global properties */
610 udev_list_entry_foreach(list_entry, udev_get_properties_list_entry(udev))
611 udev_device_add_property(udev_device,
612 udev_list_entry_get_name(list_entry),
613 udev_list_entry_get_value(list_entry));
618 * udev_device_new_from_syspath:
619 * @udev: udev library context
620 * @syspath: sys device path including sys directory
622 * Create new udev device, and fill in information from the sys
623 * device and the udev database entry. The syspath is the absolute
624 * path to the device, including the sys mount point.
626 * The initial refcount is 1, and needs to be decremented to
627 * release the resources of the udev device.
629 * Returns: a new udev device, or #NULL, if it does not exist
631 _public_ struct udev_device *udev_device_new_from_syspath(struct udev *udev, const char *syspath)
634 char path[UTIL_PATH_SIZE];
637 struct udev_device *udev_device;
644 /* path starts in sys */
645 if (!startswith(syspath, TEST_PREFIX "/sys")) {
646 udev_dbg(udev, "not in sys :%s\n", syspath);
650 /* path is not a root directory */
651 subdir = syspath + strlen(TEST_PREFIX "/sys");
652 pos = strrchr(subdir, '/');
653 if (pos == NULL || pos[1] == '\0' || pos < &subdir[2])
656 /* resolve possible symlink to real path */
657 util_strscpy(path, sizeof(path), syspath);
658 util_resolve_sys_link(udev, path, sizeof(path));
660 if (startswith(path + strlen(TEST_PREFIX "/sys"), "/devices/")) {
661 char file[UTIL_PATH_SIZE];
663 /* all "devices" require a "uevent" file */
664 util_strscpyl(file, sizeof(file), path, "/uevent", NULL);
665 if (stat(file, &statbuf) != 0)
668 /* everything else just needs to be a directory */
669 if (stat(path, &statbuf) != 0 || !S_ISDIR(statbuf.st_mode))
673 udev_device = udev_device_new(udev);
674 if (udev_device == NULL)
677 udev_device_set_syspath(udev_device, path);
678 udev_dbg(udev, "device %p has devpath '%s'\n", udev_device, udev_device_get_devpath(udev_device));
684 * udev_device_new_from_devnum:
685 * @udev: udev library context
686 * @type: char or block device
687 * @devnum: device major/minor number
689 * Create new udev device, and fill in information from the sys
690 * device and the udev database entry. The device is looked-up
691 * by its major/minor number and type. Character and block device
692 * numbers are not unique across the two types.
694 * The initial refcount is 1, and needs to be decremented to
695 * release the resources of the udev device.
697 * Returns: a new udev device, or #NULL, if it does not exist
699 _public_ struct udev_device *udev_device_new_from_devnum(struct udev *udev, char type, dev_t devnum)
701 char path[UTIL_PATH_SIZE];
702 const char *type_str;
706 else if (type == 'c')
711 /* use /sys/dev/{block,char}/<maj>:<min> link */
712 snprintf(path, sizeof(path), TEST_PREFIX "/sys/dev/%s/%u:%u",
713 type_str, major(devnum), minor(devnum));
714 return udev_device_new_from_syspath(udev, path);
717 struct udev_device *udev_device_new_from_id_filename(struct udev *udev, char *id)
721 char subsys[UTIL_PATH_SIZE];
727 if (sscanf(id, "%c%i:%i", &type, &maj, &min) != 3)
729 return udev_device_new_from_devnum(udev, type, makedev(maj, min));
733 struct udev_device *dev;
736 ifindex = strtoul(&id[1], NULL, 10);
740 sk = socket(PF_INET, SOCK_DGRAM, 0);
743 memset(&ifr, 0x00, sizeof(struct ifreq));
744 ifr.ifr_ifindex = ifindex;
745 if (ioctl(sk, SIOCGIFNAME, &ifr) != 0) {
751 dev = udev_device_new_from_subsystem_sysname(udev, "net", ifr.ifr_name);
754 if (udev_device_get_ifindex(dev) == ifindex)
756 udev_device_unref(dev);
760 util_strscpy(subsys, sizeof(subsys), &id[1]);
761 sysname = strchr(subsys, ':');
765 sysname = &sysname[1];
766 return udev_device_new_from_subsystem_sysname(udev, subsys, sysname);
773 * udev_device_new_from_subsystem_sysname:
774 * @udev: udev library context
775 * @subsystem: the subsystem of the device
776 * @sysname: the name of the device
778 * Create new udev device, and fill in information from the sys device
779 * and the udev database entry. The device is looked up by the subsystem
780 * and name string of the device, like "mem" / "zero", or "block" / "sda".
782 * The initial refcount is 1, and needs to be decremented to
783 * release the resources of the udev device.
785 * Returns: a new udev device, or #NULL, if it does not exist
787 _public_ struct udev_device *udev_device_new_from_subsystem_sysname(struct udev *udev, const char *subsystem, const char *sysname)
789 char path[UTIL_PATH_SIZE];
792 if (streq(subsystem, "subsystem")) {
793 util_strscpyl(path, sizeof(path), TEST_PREFIX "/sys/subsystem/", sysname, NULL);
794 if (stat(path, &statbuf) == 0)
797 util_strscpyl(path, sizeof(path), TEST_PREFIX "/sys/bus/", sysname, NULL);
798 if (stat(path, &statbuf) == 0)
801 util_strscpyl(path, sizeof(path), TEST_PREFIX "/sys/class/", sysname, NULL);
802 if (stat(path, &statbuf) == 0)
807 if (streq(subsystem, "module")) {
808 util_strscpyl(path, sizeof(path), TEST_PREFIX "/sys/module/", sysname, NULL);
809 if (stat(path, &statbuf) == 0)
814 if (streq(subsystem, "drivers")) {
815 char subsys[UTIL_NAME_SIZE];
818 util_strscpy(subsys, sizeof(subsys), sysname);
819 driver = strchr(subsys, ':');
820 if (driver != NULL) {
824 util_strscpyl(path, sizeof(path), TEST_PREFIX "/sys/subsystem/", subsys, "/drivers/", driver, NULL);
825 if (stat(path, &statbuf) == 0)
828 util_strscpyl(path, sizeof(path), TEST_PREFIX "/sys/bus/", subsys, "/drivers/", driver, NULL);
829 if (stat(path, &statbuf) == 0)
835 util_strscpyl(path, sizeof(path), TEST_PREFIX "/sys/subsystem/", subsystem, "/devices/", sysname, NULL);
836 if (stat(path, &statbuf) == 0)
839 util_strscpyl(path, sizeof(path), TEST_PREFIX "/sys/bus/", subsystem, "/devices/", sysname, NULL);
840 if (stat(path, &statbuf) == 0)
843 util_strscpyl(path, sizeof(path), TEST_PREFIX "/sys/class/", subsystem, "/", sysname, NULL);
844 if (stat(path, &statbuf) == 0)
849 return udev_device_new_from_syspath(udev, path);
853 * udev_device_new_from_environment
854 * @udev: udev library context
856 * Create new udev device, and fill in information from the
857 * current process environment. This only works reliable if
858 * the process is called from a udev rule. It is usually used
859 * for tools executed from IMPORT= rules.
861 * The initial refcount is 1, and needs to be decremented to
862 * release the resources of the udev device.
864 * Returns: a new udev device, or #NULL, if it does not exist
866 _public_ struct udev_device *udev_device_new_from_environment(struct udev *udev)
869 struct udev_device *udev_device;
871 udev_device = udev_device_new(udev);
872 if (udev_device == NULL)
874 udev_device_set_info_loaded(udev_device);
876 for (i = 0; environ[i] != NULL; i++)
877 udev_device_add_property_from_string_parse(udev_device, environ[i]);
879 if (udev_device_add_property_from_string_parse_finish(udev_device) < 0) {
880 udev_dbg(udev, "missing values, invalid device\n");
881 udev_device_unref(udev_device);
888 static struct udev_device *device_new_from_parent(struct udev_device *udev_device)
890 struct udev_device *udev_device_parent = NULL;
891 char path[UTIL_PATH_SIZE];
894 util_strscpy(path, sizeof(path), udev_device->syspath);
895 subdir = path + strlen(TEST_PREFIX "/sys/");
899 pos = strrchr(subdir, '/');
900 if (pos == NULL || pos < &subdir[2])
903 udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
904 if (udev_device_parent != NULL)
905 return udev_device_parent;
911 * udev_device_get_parent:
912 * @udev_device: the device to start searching from
914 * Find the next parent device, and fill in information from the sys
915 * device and the udev database entry.
917 * The returned the device is not referenced. It is attached to the
918 * child device, and will be cleaned up when the child device
921 * It is not necessarily just the upper level directory, empty or not
922 * recognized sys directories are ignored.
924 * It can be called as many times as needed, without caring about
927 * Returns: a new udev device, or #NULL, if it no parent exist.
929 _public_ struct udev_device *udev_device_get_parent(struct udev_device *udev_device)
931 if (udev_device == NULL)
933 if (!udev_device->parent_set) {
934 udev_device->parent_set = true;
935 udev_device->parent_device = device_new_from_parent(udev_device);
937 return udev_device->parent_device;
941 * udev_device_get_parent_with_subsystem_devtype:
942 * @udev_device: udev device to start searching from
943 * @subsystem: the subsystem of the device
944 * @devtype: the type (DEVTYPE) of the device
946 * Find the next parent device, with a matching subsystem and devtype
947 * value, and fill in information from the sys device and the udev
950 * If devtype is #NULL, only subsystem is checked, and any devtype will
953 * The returned the device is not referenced. It is attached to the
954 * child device, and will be cleaned up when the child device
957 * It can be called as many times as needed, without caring about
960 * Returns: a new udev device, or #NULL if no matching parent exists.
962 _public_ struct udev_device *udev_device_get_parent_with_subsystem_devtype(struct udev_device *udev_device, const char *subsystem, const char *devtype)
964 struct udev_device *parent;
966 if (subsystem == NULL)
969 parent = udev_device_get_parent(udev_device);
970 while (parent != NULL) {
971 const char *parent_subsystem;
972 const char *parent_devtype;
974 parent_subsystem = udev_device_get_subsystem(parent);
975 if (parent_subsystem != NULL && streq(parent_subsystem, subsystem)) {
978 parent_devtype = udev_device_get_devtype(parent);
979 if (parent_devtype != NULL && streq(parent_devtype, devtype))
982 parent = udev_device_get_parent(parent);
988 * udev_device_get_udev:
989 * @udev_device: udev device
991 * Retrieve the udev library context the device was created with.
993 * Returns: the udev library context
995 _public_ struct udev *udev_device_get_udev(struct udev_device *udev_device)
997 if (udev_device == NULL)
999 return udev_device->udev;
1004 * @udev_device: udev device
1006 * Take a reference of a udev device.
1008 * Returns: the passed udev device
1010 _public_ struct udev_device *udev_device_ref(struct udev_device *udev_device)
1012 if (udev_device == NULL)
1014 udev_device->refcount++;
1019 * udev_device_unref:
1020 * @udev_device: udev device
1022 * Drop a reference of a udev device. If the refcount reaches zero,
1023 * the resources of the device will be released.
1025 * Returns: the passed udev device if it has still an active reference, or #NULL otherwise.
1027 _public_ struct udev_device *udev_device_unref(struct udev_device *udev_device)
1029 if (udev_device == NULL)
1031 udev_device->refcount--;
1032 if (udev_device->refcount > 0)
1034 if (udev_device->parent_device != NULL)
1035 udev_device_unref(udev_device->parent_device);
1036 free(udev_device->syspath);
1037 free(udev_device->sysname);
1038 free(udev_device->devnode);
1039 free(udev_device->subsystem);
1040 free(udev_device->devtype);
1041 udev_list_cleanup(&udev_device->devlinks_list);
1042 udev_list_cleanup(&udev_device->properties_list);
1043 udev_list_cleanup(&udev_device->sysattr_value_list);
1044 udev_list_cleanup(&udev_device->sysattr_list);
1045 udev_list_cleanup(&udev_device->tags_list);
1046 free(udev_device->action);
1047 free(udev_device->driver);
1048 free(udev_device->devpath_old);
1049 free(udev_device->id_filename);
1050 free(udev_device->envp);
1051 free(udev_device->monitor_buf);
1057 * udev_device_get_devpath:
1058 * @udev_device: udev device
1060 * Retrieve the kernel devpath value of the udev device. The path
1061 * does not contain the sys mount point, and starts with a '/'.
1063 * Returns: the devpath of the udev device
1065 _public_ const char *udev_device_get_devpath(struct udev_device *udev_device)
1067 if (udev_device == NULL)
1069 return udev_device->devpath;
1073 * udev_device_get_syspath:
1074 * @udev_device: udev device
1076 * Retrieve the sys path of the udev device. The path is an
1077 * absolute path and starts with the sys mount point.
1079 * Returns: the sys path of the udev device
1081 _public_ const char *udev_device_get_syspath(struct udev_device *udev_device)
1083 if (udev_device == NULL)
1085 return udev_device->syspath;
1089 * udev_device_get_sysname:
1090 * @udev_device: udev device
1092 * Get the kernel device name in /sys.
1094 * Returns: the name string of the device device
1096 _public_ const char *udev_device_get_sysname(struct udev_device *udev_device)
1098 if (udev_device == NULL)
1100 return udev_device->sysname;
1104 * udev_device_get_sysnum:
1105 * @udev_device: udev device
1107 * Get the instance number of the device.
1109 * Returns: the trailing number string of of the device name
1111 _public_ const char *udev_device_get_sysnum(struct udev_device *udev_device)
1113 if (udev_device == NULL)
1115 return udev_device->sysnum;
1119 * udev_device_get_devnode:
1120 * @udev_device: udev device
1122 * Retrieve the device node file name belonging to the udev device.
1123 * The path is an absolute path, and starts with the device directory.
1125 * Returns: the device node file name of the udev device, or #NULL if no device node exists
1127 _public_ const char *udev_device_get_devnode(struct udev_device *udev_device)
1129 if (udev_device == NULL)
1131 if (udev_device->devnode != NULL)
1132 return udev_device->devnode;
1133 if (!udev_device->info_loaded)
1134 udev_device_read_uevent_file(udev_device);
1135 return udev_device->devnode;
1139 * udev_device_get_devlinks_list_entry:
1140 * @udev_device: udev device
1142 * Retrieve the list of device links pointing to the device file of
1143 * the udev device. The next list entry can be retrieved with
1144 * udev_list_entry_get_next(), which returns #NULL if no more entries exist.
1145 * The devlink path can be retrieved from the list entry by
1146 * udev_list_entry_get_name(). The path is an absolute path, and starts with
1147 * the device directory.
1149 * Returns: the first entry of the device node link list
1151 _public_ struct udev_list_entry *udev_device_get_devlinks_list_entry(struct udev_device *udev_device)
1153 if (udev_device == NULL)
1155 if (!udev_device->info_loaded)
1156 udev_device_read_db(udev_device, NULL);
1157 return udev_list_get_entry(&udev_device->devlinks_list);
1160 void udev_device_cleanup_devlinks_list(struct udev_device *udev_device)
1162 udev_device->devlinks_uptodate = false;
1163 udev_list_cleanup(&udev_device->devlinks_list);
1167 * udev_device_get_properties_list_entry:
1168 * @udev_device: udev device
1170 * Retrieve the list of key/value device properties of the udev
1171 * device. The next list entry can be retrieved with udev_list_entry_get_next(),
1172 * which returns #NULL if no more entries exist. The property name
1173 * can be retrieved from the list entry by udev_list_entry_get_name(),
1174 * the property value by udev_list_entry_get_value().
1176 * Returns: the first entry of the property list
1178 _public_ struct udev_list_entry *udev_device_get_properties_list_entry(struct udev_device *udev_device)
1180 if (udev_device == NULL)
1182 if (!udev_device->info_loaded) {
1183 udev_device_read_uevent_file(udev_device);
1184 udev_device_read_db(udev_device, NULL);
1186 if (!udev_device->devlinks_uptodate) {
1187 char symlinks[UTIL_PATH_SIZE];
1188 struct udev_list_entry *list_entry;
1190 udev_device->devlinks_uptodate = true;
1191 list_entry = udev_device_get_devlinks_list_entry(udev_device);
1192 if (list_entry != NULL) {
1197 l = util_strpcpyl(&s, sizeof(symlinks), udev_list_entry_get_name(list_entry), NULL);
1198 udev_list_entry_foreach(list_entry, udev_list_entry_get_next(list_entry))
1199 l = util_strpcpyl(&s, l, " ", udev_list_entry_get_name(list_entry), NULL);
1200 udev_device_add_property(udev_device, "DEVLINKS", symlinks);
1203 if (!udev_device->tags_uptodate) {
1204 udev_device->tags_uptodate = true;
1205 if (udev_device_get_tags_list_entry(udev_device) != NULL) {
1206 char tags[UTIL_PATH_SIZE];
1207 struct udev_list_entry *list_entry;
1212 l = util_strpcpyl(&s, sizeof(tags), ":", NULL);
1213 udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(udev_device))
1214 l = util_strpcpyl(&s, l, udev_list_entry_get_name(list_entry), ":", NULL);
1215 udev_device_add_property(udev_device, "TAGS", tags);
1218 return udev_list_get_entry(&udev_device->properties_list);
1222 * udev_device_get_action:
1223 * @udev_device: udev device
1225 * This is only valid if the device was received through a monitor. Devices read from
1226 * sys do not have an action string. Usual actions are: add, remove, change, online,
1229 * Returns: the kernel action value, or #NULL if there is no action value available.
1231 _public_ const char *udev_device_get_action(struct udev_device *udev_device)
1233 if (udev_device == NULL)
1235 return udev_device->action;
1239 * udev_device_get_usec_since_initialized:
1240 * @udev_device: udev device
1242 * Return the number of microseconds passed since udev set up the
1243 * device for the first time.
1245 * This is only implemented for devices with need to store properties
1246 * in the udev database. All other devices return 0 here.
1248 * Returns: the number of microseconds since the device was first seen.
1250 _public_ unsigned long long int udev_device_get_usec_since_initialized(struct udev_device *udev_device)
1252 unsigned long long now_ts;
1254 if (udev_device == NULL)
1256 if (!udev_device->info_loaded)
1257 udev_device_read_db(udev_device, NULL);
1258 if (udev_device->usec_initialized == 0)
1260 now_ts = now_usec();
1263 return now_ts - udev_device->usec_initialized;
1266 unsigned long long udev_device_get_usec_initialized(struct udev_device *udev_device)
1268 return udev_device->usec_initialized;
1271 void udev_device_set_usec_initialized(struct udev_device *udev_device, unsigned long long usec_initialized)
1275 udev_device->usec_initialized = usec_initialized;
1276 snprintf(num, sizeof(num), "%llu", usec_initialized);
1277 udev_device_add_property(udev_device, "USEC_INITIALIZED", num);
1281 * udev_device_get_sysattr_value:
1282 * @udev_device: udev device
1283 * @sysattr: attribute name
1285 * The retrieved value is cached in the device. Repeated calls will return the same
1286 * value and not open the attribute again.
1288 * Returns: the content of a sys attribute file, or #NULL if there is no sys attribute value.
1290 _public_ const char *udev_device_get_sysattr_value(struct udev_device *udev_device, const char *sysattr)
1292 struct udev_list_entry *list_entry;
1293 char path[UTIL_PATH_SIZE];
1295 struct stat statbuf;
1298 const char *val = NULL;
1300 if (udev_device == NULL)
1302 if (sysattr == NULL)
1305 /* look for possibly already cached result */
1306 list_entry = udev_list_get_entry(&udev_device->sysattr_value_list);
1307 list_entry = udev_list_entry_get_by_name(list_entry, sysattr);
1308 if (list_entry != NULL)
1309 return udev_list_entry_get_value(list_entry);
1311 util_strscpyl(path, sizeof(path), udev_device_get_syspath(udev_device), "/", sysattr, NULL);
1312 if (lstat(path, &statbuf) != 0) {
1313 udev_list_entry_add(&udev_device->sysattr_value_list, sysattr, NULL);
1317 if (S_ISLNK(statbuf.st_mode)) {
1318 struct udev_device *dev;
1321 * Some core links return only the last element of the target path,
1322 * these are just values, the paths should not be exposed.
1324 if (streq(sysattr, "driver") ||
1325 streq(sysattr, "subsystem") ||
1326 streq(sysattr, "module")) {
1327 if (util_get_sys_core_link_value(udev_device->udev, sysattr,
1328 udev_device->syspath, value, sizeof(value)) < 0)
1330 list_entry = udev_list_entry_add(&udev_device->sysattr_value_list, sysattr, value);
1331 val = udev_list_entry_get_value(list_entry);
1335 /* resolve link to a device and return its syspath */
1336 util_strscpyl(path, sizeof(path), udev_device->syspath, "/", sysattr, NULL);
1337 dev = udev_device_new_from_syspath(udev_device->udev, path);
1339 list_entry = udev_list_entry_add(&udev_device->sysattr_value_list, sysattr,
1340 udev_device_get_syspath(dev));
1341 val = udev_list_entry_get_value(list_entry);
1342 udev_device_unref(dev);
1348 /* skip directories */
1349 if (S_ISDIR(statbuf.st_mode))
1352 /* skip non-readable files */
1353 if ((statbuf.st_mode & S_IRUSR) == 0)
1356 /* read attribute value */
1357 fd = open(path, O_RDONLY|O_CLOEXEC);
1360 size = read(fd, value, sizeof(value));
1364 if (size == sizeof(value))
1367 /* got a valid value, store it in cache and return it */
1369 util_remove_trailing_chars(value, '\n');
1370 list_entry = udev_list_entry_add(&udev_device->sysattr_value_list, sysattr, value);
1371 val = udev_list_entry_get_value(list_entry);
1376 static int udev_device_sysattr_list_read(struct udev_device *udev_device)
1378 struct dirent *dent;
1382 if (udev_device == NULL)
1384 if (udev_device->sysattr_list_read)
1387 dir = opendir(udev_device_get_syspath(udev_device));
1391 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
1392 char path[UTIL_PATH_SIZE];
1393 struct stat statbuf;
1395 /* only handle symlinks and regular files */
1396 if (dent->d_type != DT_LNK && dent->d_type != DT_REG)
1399 util_strscpyl(path, sizeof(path), udev_device_get_syspath(udev_device), "/", dent->d_name, NULL);
1400 if (lstat(path, &statbuf) != 0)
1402 if ((statbuf.st_mode & S_IRUSR) == 0)
1405 udev_list_entry_add(&udev_device->sysattr_list, dent->d_name, NULL);
1410 udev_device->sysattr_list_read = true;
1416 * udev_device_get_sysattr_list_entry:
1417 * @udev_device: udev device
1419 * Retrieve the list of available sysattrs, with value being empty;
1420 * This just return all available sysfs attributes for a particular
1421 * device without reading their values.
1423 * Returns: the first entry of the property list
1425 _public_ struct udev_list_entry *udev_device_get_sysattr_list_entry(struct udev_device *udev_device)
1427 if (!udev_device->sysattr_list_read) {
1429 ret = udev_device_sysattr_list_read(udev_device);
1434 return udev_list_get_entry(&udev_device->sysattr_list);
1437 int udev_device_set_syspath(struct udev_device *udev_device, const char *syspath)
1442 free(udev_device->syspath);
1443 udev_device->syspath = strdup(syspath);
1444 if (udev_device->syspath == NULL)
1446 udev_device->devpath = udev_device->syspath + strlen(TEST_PREFIX "/sys");
1447 udev_device_add_property(udev_device, "DEVPATH", udev_device->devpath);
1449 pos = strrchr(udev_device->syspath, '/');
1452 udev_device->sysname = strdup(&pos[1]);
1453 if (udev_device->sysname == NULL)
1456 /* some devices have '!' in their name, change that to '/' */
1458 while (udev_device->sysname[len] != '\0') {
1459 if (udev_device->sysname[len] == '!')
1460 udev_device->sysname[len] = '/';
1464 /* trailing number */
1465 while (len > 0 && isdigit(udev_device->sysname[--len]))
1466 udev_device->sysnum = &udev_device->sysname[len];
1468 /* sysname is completely numeric */
1470 udev_device->sysnum = NULL;
1475 int udev_device_set_devnode(struct udev_device *udev_device, const char *devnode)
1477 free(udev_device->devnode);
1478 if (devnode[0] != '/') {
1479 if (asprintf(&udev_device->devnode, TEST_PREFIX "/dev/%s", devnode) < 0)
1480 udev_device->devnode = NULL;
1482 udev_device->devnode = strdup(devnode);
1484 if (udev_device->devnode == NULL)
1486 udev_device_add_property(udev_device, "DEVNAME", udev_device->devnode);
1490 int udev_device_add_devlink(struct udev_device *udev_device, const char *devlink, int unique)
1492 struct udev_list_entry *list_entry;
1494 udev_device->devlinks_uptodate = false;
1495 list_entry = udev_list_entry_add(&udev_device->devlinks_list, devlink, NULL);
1496 if (list_entry == NULL)
1499 udev_list_entry_set_num(list_entry, true);
1503 const char *udev_device_get_id_filename(struct udev_device *udev_device)
1505 if (udev_device->id_filename == NULL) {
1506 if (udev_device_get_subsystem(udev_device) == NULL)
1509 if (major(udev_device_get_devnum(udev_device)) > 0) {
1510 /* use dev_t -- b259:131072, c254:0 */
1511 if (asprintf(&udev_device->id_filename, "%c%u:%u",
1512 streq(udev_device_get_subsystem(udev_device), "block") ? 'b' : 'c',
1513 major(udev_device_get_devnum(udev_device)),
1514 minor(udev_device_get_devnum(udev_device))) < 0)
1515 udev_device->id_filename = NULL;
1516 } else if (udev_device_get_ifindex(udev_device) > 0) {
1517 /* use netdev ifindex -- n3 */
1518 if (asprintf(&udev_device->id_filename, "n%u", udev_device_get_ifindex(udev_device)) < 0)
1519 udev_device->id_filename = NULL;
1522 * use $subsys:$syname -- pci:0000:00:1f.2
1523 * sysname() has '!' translated, get it from devpath
1525 const char *sysname;
1526 sysname = strrchr(udev_device->devpath, '/');
1527 if (sysname == NULL)
1529 sysname = &sysname[1];
1530 if (asprintf(&udev_device->id_filename, "+%s:%s", udev_device_get_subsystem(udev_device), sysname) < 0)
1531 udev_device->id_filename = NULL;
1534 return udev_device->id_filename;
1538 * udev_device_get_is_initialized:
1539 * @udev_device: udev device
1541 * Check if udev has already handled the device and has set up
1542 * device node permissions and context, or has renamed a network
1545 * This is only implemented for devices with a device node
1546 * or network interfaces. All other devices return 1 here.
1548 * Returns: 1 if the device is set up. 0 otherwise.
1550 _public_ int udev_device_get_is_initialized(struct udev_device *udev_device)
1552 if (!udev_device->info_loaded)
1553 udev_device_read_db(udev_device, NULL);
1554 return udev_device->is_initialized;
1557 void udev_device_set_is_initialized(struct udev_device *udev_device)
1559 udev_device->is_initialized = true;
1562 int udev_device_add_tag(struct udev_device *udev_device, const char *tag)
1564 if (strchr(tag, ':') != NULL || strchr(tag, ' ') != NULL)
1566 udev_device->tags_uptodate = false;
1567 if (udev_list_entry_add(&udev_device->tags_list, tag, NULL) != NULL)
1572 void udev_device_cleanup_tags_list(struct udev_device *udev_device)
1574 udev_device->tags_uptodate = false;
1575 udev_list_cleanup(&udev_device->tags_list);
1579 * udev_device_get_tags_list_entry:
1580 * @udev_device: udev device
1582 * Retrieve the list of tags attached to the udev device. The next
1583 * list entry can be retrieved with udev_list_entry_get_next(),
1584 * which returns #NULL if no more entries exist. The tag string
1585 * can be retrieved from the list entry by udev_list_entry_get_name().
1587 * Returns: the first entry of the tag list
1589 _public_ struct udev_list_entry *udev_device_get_tags_list_entry(struct udev_device *udev_device)
1591 if (udev_device == NULL)
1593 if (!udev_device->info_loaded)
1594 udev_device_read_db(udev_device, NULL);
1595 return udev_list_get_entry(&udev_device->tags_list);
1599 * udev_device_has_tag:
1600 * @udev_device: udev device
1603 * Check if a given device has a certain tag associated.
1605 * Returns: 1 if the tag is found. 0 otherwise.
1607 _public_ int udev_device_has_tag(struct udev_device *udev_device, const char *tag)
1609 struct udev_list_entry *list_entry;
1611 if (udev_device == NULL)
1613 if (!udev_device->info_loaded)
1614 udev_device_read_db(udev_device, NULL);
1615 list_entry = udev_device_get_tags_list_entry(udev_device);
1616 if (udev_list_entry_get_by_name(list_entry, tag) != NULL)
1621 #define ENVP_SIZE 128
1622 #define MONITOR_BUF_SIZE 4096
1623 static int update_envp_monitor_buf(struct udev_device *udev_device)
1625 struct udev_list_entry *list_entry;
1630 /* monitor buffer of property strings */
1631 free(udev_device->monitor_buf);
1632 udev_device->monitor_buf_len = 0;
1633 udev_device->monitor_buf = malloc(MONITOR_BUF_SIZE);
1634 if (udev_device->monitor_buf == NULL)
1637 /* envp array, strings will point into monitor buffer */
1638 if (udev_device->envp == NULL)
1639 udev_device->envp = malloc(sizeof(char *) * ENVP_SIZE);
1640 if (udev_device->envp == NULL)
1644 s = udev_device->monitor_buf;
1645 l = MONITOR_BUF_SIZE;
1646 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device)) {
1649 key = udev_list_entry_get_name(list_entry);
1650 /* skip private variables */
1654 /* add string to envp array */
1655 udev_device->envp[i++] = s;
1656 if (i+1 >= ENVP_SIZE)
1659 /* add property string to monitor buffer */
1660 l = util_strpcpyl(&s, l, key, "=", udev_list_entry_get_value(list_entry), NULL);
1663 /* advance past the trailing '\0' that util_strpcpyl() guarantees */
1667 udev_device->envp[i] = NULL;
1668 udev_device->monitor_buf_len = s - udev_device->monitor_buf;
1669 udev_device->envp_uptodate = true;
1673 char **udev_device_get_properties_envp(struct udev_device *udev_device)
1675 if (!udev_device->envp_uptodate)
1676 if (update_envp_monitor_buf(udev_device) != 0)
1678 return udev_device->envp;
1681 ssize_t udev_device_get_properties_monitor_buf(struct udev_device *udev_device, const char **buf)
1683 if (!udev_device->envp_uptodate)
1684 if (update_envp_monitor_buf(udev_device) != 0)
1686 *buf = udev_device->monitor_buf;
1687 return udev_device->monitor_buf_len;
1690 int udev_device_set_action(struct udev_device *udev_device, const char *action)
1692 free(udev_device->action);
1693 udev_device->action = strdup(action);
1694 if (udev_device->action == NULL)
1696 udev_device_add_property(udev_device, "ACTION", udev_device->action);
1700 int udev_device_get_devlink_priority(struct udev_device *udev_device)
1702 if (!udev_device->info_loaded)
1703 udev_device_read_db(udev_device, NULL);
1704 return udev_device->devlink_priority;
1707 int udev_device_set_devlink_priority(struct udev_device *udev_device, int prio)
1709 udev_device->devlink_priority = prio;
1713 int udev_device_get_watch_handle(struct udev_device *udev_device)
1715 if (!udev_device->info_loaded)
1716 udev_device_read_db(udev_device, NULL);
1717 return udev_device->watch_handle;
1720 int udev_device_set_watch_handle(struct udev_device *udev_device, int handle)
1722 udev_device->watch_handle = handle;
1726 bool udev_device_get_db_persist(struct udev_device *udev_device)
1728 return udev_device->db_persist;
1731 void udev_device_set_db_persist(struct udev_device *udev_device)
1733 udev_device->db_persist = true;