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 * Returns: the device major/minor number.
141 _public_ dev_t udev_device_get_devnum(struct udev_device *udev_device)
143 if (udev_device == NULL)
144 return makedev(0, 0);
145 if (!udev_device->info_loaded)
146 udev_device_read_uevent_file(udev_device);
147 return udev_device->devnum;
150 static int udev_device_set_devnum(struct udev_device *udev_device, dev_t devnum)
154 udev_device->devnum = devnum;
156 snprintf(num, sizeof(num), "%u", major(devnum));
157 udev_device_add_property(udev_device, "MAJOR", num);
158 snprintf(num, sizeof(num), "%u", minor(devnum));
159 udev_device_add_property(udev_device, "MINOR", num);
163 const char *udev_device_get_devpath_old(struct udev_device *udev_device)
165 return udev_device->devpath_old;
168 static int udev_device_set_devpath_old(struct udev_device *udev_device, const char *devpath_old)
172 free(udev_device->devpath_old);
173 udev_device->devpath_old = strdup(devpath_old);
174 if (udev_device->devpath_old == NULL)
176 udev_device_add_property(udev_device, "DEVPATH_OLD", udev_device->devpath_old);
178 pos = strrchr(udev_device->devpath_old, '/');
185 * udev_device_get_driver:
186 * @udev_device: udev device
188 * Returns: the driver string, or #NULL if there is no driver attached.
190 _public_ const char *udev_device_get_driver(struct udev_device *udev_device)
192 char driver[UTIL_NAME_SIZE];
194 if (udev_device == NULL)
196 if (!udev_device->driver_set) {
197 udev_device->driver_set = true;
198 if (util_get_sys_core_link_value(udev_device->udev, "driver", udev_device->syspath, driver, sizeof(driver)) > 0)
199 udev_device->driver = strdup(driver);
201 return udev_device->driver;
204 static int udev_device_set_driver(struct udev_device *udev_device, const char *driver)
206 free(udev_device->driver);
207 udev_device->driver = strdup(driver);
208 if (udev_device->driver == NULL)
210 udev_device->driver_set = true;
211 udev_device_add_property(udev_device, "DRIVER", udev_device->driver);
216 * udev_device_get_devtype:
217 * @udev_device: udev device
219 * Retrieve the devtype string of the udev device.
221 * Returns: the devtype name of the udev device, or #NULL if it can not be determined
223 _public_ const char *udev_device_get_devtype(struct udev_device *udev_device)
225 if (udev_device == NULL)
227 if (!udev_device->devtype_set) {
228 udev_device->devtype_set = true;
229 udev_device_read_uevent_file(udev_device);
231 return udev_device->devtype;
234 static int udev_device_set_devtype(struct udev_device *udev_device, const char *devtype)
236 free(udev_device->devtype);
237 udev_device->devtype = strdup(devtype);
238 if (udev_device->devtype == NULL)
240 udev_device->devtype_set = true;
241 udev_device_add_property(udev_device, "DEVTYPE", udev_device->devtype);
245 static int udev_device_set_subsystem(struct udev_device *udev_device, const char *subsystem)
247 free(udev_device->subsystem);
248 udev_device->subsystem = strdup(subsystem);
249 if (udev_device->subsystem == NULL)
251 udev_device->subsystem_set = true;
252 udev_device_add_property(udev_device, "SUBSYSTEM", udev_device->subsystem);
257 * udev_device_get_subsystem:
258 * @udev_device: udev device
260 * Retrieve the subsystem string of the udev device. The string does not
263 * Returns: the subsystem name of the udev device, or #NULL if it can not be determined
265 _public_ const char *udev_device_get_subsystem(struct udev_device *udev_device)
267 char subsystem[UTIL_NAME_SIZE];
269 if (udev_device == NULL)
271 if (!udev_device->subsystem_set) {
272 udev_device->subsystem_set = true;
273 /* read "subsystem" link */
274 if (util_get_sys_core_link_value(udev_device->udev, "subsystem", udev_device->syspath, subsystem, sizeof(subsystem)) > 0) {
275 udev_device_set_subsystem(udev_device, subsystem);
276 return udev_device->subsystem;
279 if (strncmp(udev_device->devpath, "/module/", 8) == 0) {
280 udev_device_set_subsystem(udev_device, "module");
281 return udev_device->subsystem;
283 if (strstr(udev_device->devpath, "/drivers/") != NULL) {
284 udev_device_set_subsystem(udev_device, "drivers");
285 return udev_device->subsystem;
287 if (strncmp(udev_device->devpath, "/subsystem/", 11) == 0 ||
288 strncmp(udev_device->devpath, "/class/", 7) == 0 ||
289 strncmp(udev_device->devpath, "/bus/", 5) == 0) {
290 udev_device_set_subsystem(udev_device, "subsystem");
291 return udev_device->subsystem;
294 return udev_device->subsystem;
297 mode_t udev_device_get_devnode_mode(struct udev_device *udev_device)
299 if (!udev_device->info_loaded)
300 udev_device_read_uevent_file(udev_device);
301 return udev_device->devnode_mode;
304 static int udev_device_set_devnode_mode(struct udev_device *udev_device, mode_t mode)
308 udev_device->devnode_mode = mode;
309 snprintf(num, sizeof(num), "%#o", mode);
310 udev_device_add_property(udev_device, "DEVMODE", num);
314 struct udev_list_entry *udev_device_add_property(struct udev_device *udev_device, const char *key, const char *value)
316 udev_device->envp_uptodate = false;
318 struct udev_list_entry *list_entry;
320 list_entry = udev_device_get_properties_list_entry(udev_device);
321 list_entry = udev_list_entry_get_by_name(list_entry, key);
322 if (list_entry != NULL)
323 udev_list_entry_delete(list_entry);
326 return udev_list_entry_add(&udev_device->properties_list, key, value);
329 static struct udev_list_entry *udev_device_add_property_from_string(struct udev_device *udev_device, const char *property)
331 char name[UTIL_LINE_SIZE];
334 util_strscpy(name, sizeof(name), property);
335 val = strchr(name, '=');
342 return udev_device_add_property(udev_device, name, val);
346 * parse property string, and if needed, update internal values accordingly
348 * udev_device_add_property_from_string_parse_finish() needs to be
349 * called after adding properties, and its return value checked
351 * udev_device_set_info_loaded() needs to be set, to avoid trying
352 * to use a device without a DEVPATH set
354 void udev_device_add_property_from_string_parse(struct udev_device *udev_device, const char *property)
356 if (strncmp(property, "DEVPATH=", 8) == 0) {
357 char path[UTIL_PATH_SIZE];
359 util_strscpyl(path, sizeof(path), udev_get_sys_path(udev_device->udev), &property[8], NULL);
360 udev_device_set_syspath(udev_device, path);
361 } else if (strncmp(property, "SUBSYSTEM=", 10) == 0) {
362 udev_device_set_subsystem(udev_device, &property[10]);
363 } else if (strncmp(property, "DEVTYPE=", 8) == 0) {
364 udev_device_set_devtype(udev_device, &property[8]);
365 } else if (strncmp(property, "DEVNAME=", 8) == 0) {
366 udev_device_set_devnode(udev_device, &property[8]);
367 } else if (strncmp(property, "DEVLINKS=", 9) == 0) {
368 char devlinks[UTIL_PATH_SIZE];
372 util_strscpy(devlinks, sizeof(devlinks), &property[9]);
374 next = strchr(slink, ' ');
375 while (next != NULL) {
377 udev_device_add_devlink(udev_device, slink, 0);
379 next = strchr(slink, ' ');
381 if (slink[0] != '\0')
382 udev_device_add_devlink(udev_device, slink, 0);
383 } else if (strncmp(property, "TAGS=", 5) == 0) {
384 char tags[UTIL_PATH_SIZE];
387 util_strscpy(tags, sizeof(tags), &property[5]);
388 next = strchr(tags, ':');
391 while (next[0] != '\0') {
395 next = strchr(tag, ':');
400 udev_device_add_tag(udev_device, tag);
403 } else if (strncmp(property, "USEC_INITIALIZED=", 19) == 0) {
404 udev_device_set_usec_initialized(udev_device, strtoull(&property[19], NULL, 10));
405 } else if (strncmp(property, "DRIVER=", 7) == 0) {
406 udev_device_set_driver(udev_device, &property[7]);
407 } else if (strncmp(property, "ACTION=", 7) == 0) {
408 udev_device_set_action(udev_device, &property[7]);
409 } else if (strncmp(property, "MAJOR=", 6) == 0) {
410 udev_device->maj = strtoull(&property[6], NULL, 10);
411 } else if (strncmp(property, "MINOR=", 6) == 0) {
412 udev_device->min = strtoull(&property[6], NULL, 10);
413 } else if (strncmp(property, "DEVPATH_OLD=", 12) == 0) {
414 udev_device_set_devpath_old(udev_device, &property[12]);
415 } else if (strncmp(property, "SEQNUM=", 7) == 0) {
416 udev_device_set_seqnum(udev_device, strtoull(&property[7], NULL, 10));
417 } else if (strncmp(property, "IFINDEX=", 8) == 0) {
418 udev_device_set_ifindex(udev_device, strtoull(&property[8], NULL, 10));
419 } else if (strncmp(property, "DEVMODE=", 8) == 0) {
420 udev_device_set_devnode_mode(udev_device, strtoul(&property[8], NULL, 8));
422 udev_device_add_property_from_string(udev_device, property);
426 int udev_device_add_property_from_string_parse_finish(struct udev_device *udev_device)
428 if (udev_device->maj > 0)
429 udev_device_set_devnum(udev_device, makedev(udev_device->maj, udev_device->min));
430 udev_device->maj = 0;
431 udev_device->min = 0;
433 if (udev_device->devpath == NULL || udev_device->subsystem == NULL)
439 * udev_device_get_property_value:
440 * @udev_device: udev device
441 * @key: property name
443 * Returns: the value of a device property, or #NULL if there is no such property.
445 _public_ const char *udev_device_get_property_value(struct udev_device *udev_device, const char *key)
447 struct udev_list_entry *list_entry;
449 if (udev_device == NULL)
454 list_entry = udev_device_get_properties_list_entry(udev_device);
455 list_entry = udev_list_entry_get_by_name(list_entry, key);
456 return udev_list_entry_get_value(list_entry);
459 int udev_device_read_db(struct udev_device *udev_device, const char *dbfile)
461 char filename[UTIL_PATH_SIZE];
462 char line[UTIL_LINE_SIZE];
465 /* providing a database file will always force-load it */
466 if (dbfile == NULL) {
469 if (udev_device->db_loaded)
471 udev_device->db_loaded = true;
473 id = udev_device_get_id_filename(udev_device);
476 util_strscpyl(filename, sizeof(filename), udev_get_run_path(udev_device->udev), "/data/", id, NULL);
480 f = fopen(dbfile, "re");
482 dbg(udev_device->udev, "no db file to read %s: %m\n", dbfile);
485 udev_device->is_initialized = true;
487 while (fgets(line, sizeof(line), f)) {
490 struct udev_list_entry *entry;
499 util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev_device->udev), "/", val, NULL);
500 udev_device_add_devlink(udev_device, filename, 0);
503 udev_device_set_devlink_priority(udev_device, atoi(val));
506 entry = udev_device_add_property_from_string(udev_device, val);
507 udev_list_entry_set_num(entry, true);
510 udev_device_add_tag(udev_device, val);
513 udev_device_set_watch_handle(udev_device, atoi(val));
516 udev_device_set_usec_initialized(udev_device, strtoull(val, NULL, 10));
522 dbg(udev_device->udev, "device %p filled with db file data\n", udev_device);
526 int udev_device_read_uevent_file(struct udev_device *udev_device)
528 char filename[UTIL_PATH_SIZE];
530 char line[UTIL_LINE_SIZE];
534 if (udev_device->uevent_loaded)
537 util_strscpyl(filename, sizeof(filename), udev_device->syspath, "/uevent", NULL);
538 f = fopen(filename, "re");
541 udev_device->uevent_loaded = true;
543 while (fgets(line, sizeof(line), f)) {
546 pos = strchr(line, '\n');
551 if (strncmp(line, "DEVTYPE=", 8) == 0) {
552 udev_device_set_devtype(udev_device, &line[8]);
555 if (strncmp(line, "IFINDEX=", 8) == 0) {
556 udev_device_set_ifindex(udev_device, strtoull(&line[8], NULL, 10));
559 if (strncmp(line, "DEVNAME=", 8) == 0) {
560 udev_device_set_devnode(udev_device, &line[8]);
564 if (strncmp(line, "MAJOR=", 6) == 0)
565 maj = strtoull(&line[6], NULL, 10);
566 else if (strncmp(line, "MINOR=", 6) == 0)
567 min = strtoull(&line[6], NULL, 10);
568 else if (strncmp(line, "DEVMODE=", 8) == 0)
569 udev_device->devnode_mode = strtoul(&line[8], NULL, 8);
571 udev_device_add_property_from_string(udev_device, line);
574 udev_device->devnum = makedev(maj, min);
579 void udev_device_set_info_loaded(struct udev_device *device)
581 device->info_loaded = true;
584 struct udev_device *udev_device_new(struct udev *udev)
586 struct udev_device *udev_device;
587 struct udev_list_entry *list_entry;
592 udev_device = calloc(1, sizeof(struct udev_device));
593 if (udev_device == NULL)
595 udev_device->refcount = 1;
596 udev_device->udev = udev;
597 udev_list_init(udev, &udev_device->devlinks_list, true);
598 udev_list_init(udev, &udev_device->properties_list, true);
599 udev_list_init(udev, &udev_device->sysattr_value_list, true);
600 udev_list_init(udev, &udev_device->sysattr_list, false);
601 udev_list_init(udev, &udev_device->tags_list, true);
602 udev_device->watch_handle = -1;
603 /* copy global properties */
604 udev_list_entry_foreach(list_entry, udev_get_properties_list_entry(udev))
605 udev_device_add_property(udev_device,
606 udev_list_entry_get_name(list_entry),
607 udev_list_entry_get_value(list_entry));
612 * udev_device_new_from_syspath:
613 * @udev: udev library context
614 * @syspath: sys device path including sys directory
616 * Create new udev device, and fill in information from the sys
617 * device and the udev database entry. The syspath is the absolute
618 * path to the device, including the sys mount point.
620 * The initial refcount is 1, and needs to be decremented to
621 * release the resources of the udev device.
623 * Returns: a new udev device, or #NULL, if it does not exist
625 _public_ struct udev_device *udev_device_new_from_syspath(struct udev *udev, const char *syspath)
629 char path[UTIL_PATH_SIZE];
632 struct udev_device *udev_device;
639 /* path starts in sys */
640 len = strlen(udev_get_sys_path(udev));
641 if (strncmp(syspath, udev_get_sys_path(udev), len) != 0) {
642 dbg(udev, "not in sys :%s\n", syspath);
646 /* path is not a root directory */
647 subdir = &syspath[len+1];
648 pos = strrchr(subdir, '/');
649 if (pos == NULL || pos[1] == '\0' || pos < &subdir[2])
652 /* resolve possible symlink to real path */
653 util_strscpy(path, sizeof(path), syspath);
654 util_resolve_sys_link(udev, path, sizeof(path));
656 if (strncmp(&path[len], "/devices/", 9) == 0) {
657 char file[UTIL_PATH_SIZE];
659 /* all "devices" require a "uevent" file */
660 util_strscpyl(file, sizeof(file), path, "/uevent", NULL);
661 if (stat(file, &statbuf) != 0)
664 /* everything else just needs to be a directory */
665 if (stat(path, &statbuf) != 0 || !S_ISDIR(statbuf.st_mode))
669 udev_device = udev_device_new(udev);
670 if (udev_device == NULL)
673 udev_device_set_syspath(udev_device, path);
674 dbg(udev, "device %p has devpath '%s'\n", udev_device, udev_device_get_devpath(udev_device));
680 * udev_device_new_from_devnum:
681 * @udev: udev library context
682 * @type: char or block device
683 * @devnum: device major/minor number
685 * Create new udev device, and fill in information from the sys
686 * device and the udev database entry. The device is looked-up
687 * by its major/minor number and type. Character and block device
688 * numbers are not unique across the two types.
690 * The initial refcount is 1, and needs to be decremented to
691 * release the resources of the udev device.
693 * Returns: a new udev device, or #NULL, if it does not exist
695 _public_ struct udev_device *udev_device_new_from_devnum(struct udev *udev, char type, dev_t devnum)
697 char path[UTIL_PATH_SIZE];
698 const char *type_str;
702 else if (type == 'c')
707 /* use /sys/dev/{block,char}/<maj>:<min> link */
708 snprintf(path, sizeof(path), "%s/dev/%s/%u:%u",
709 udev_get_sys_path(udev), type_str, major(devnum), minor(devnum));
710 return udev_device_new_from_syspath(udev, path);
713 struct udev_device *udev_device_new_from_id_filename(struct udev *udev, char *id)
717 char subsys[UTIL_PATH_SIZE];
723 if (sscanf(id, "%c%i:%i", &type, &maj, &min) != 3)
725 return udev_device_new_from_devnum(udev, type, makedev(maj, min));
729 struct udev_device *dev;
732 ifindex = strtoul(&id[1], NULL, 10);
736 sk = socket(PF_INET, SOCK_DGRAM, 0);
739 memset(&ifr, 0x00, sizeof(struct ifreq));
740 ifr.ifr_ifindex = ifindex;
741 if (ioctl(sk, SIOCGIFNAME, &ifr) != 0) {
747 dev = udev_device_new_from_subsystem_sysname(udev, "net", ifr.ifr_name);
750 if (udev_device_get_ifindex(dev) == ifindex)
752 udev_device_unref(dev);
756 util_strscpy(subsys, sizeof(subsys), &id[1]);
757 sysname = strchr(subsys, ':');
761 sysname = &sysname[1];
762 return udev_device_new_from_subsystem_sysname(udev, subsys, sysname);
769 * udev_device_new_from_subsystem_sysname:
770 * @udev: udev library context
771 * @subsystem: the subsystem of the device
772 * @sysname: the name of the device
774 * Create new udev device, and fill in information from the sys device
775 * and the udev database entry. The device is looked up by the subsystem
776 * and name string of the device, like "mem" / "zero", or "block" / "sda".
778 * The initial refcount is 1, and needs to be decremented to
779 * release the resources of the udev device.
781 * Returns: a new udev device, or #NULL, if it does not exist
783 _public_ struct udev_device *udev_device_new_from_subsystem_sysname(struct udev *udev, const char *subsystem, const char *sysname)
785 char path_full[UTIL_PATH_SIZE];
791 l = util_strpcpyl(&path, sizeof(path_full), udev_get_sys_path(udev), NULL);
793 if (strcmp(subsystem, "subsystem") == 0) {
794 util_strscpyl(path, l, "/subsystem/", sysname, NULL);
795 if (stat(path_full, &statbuf) == 0)
798 util_strscpyl(path, l, "/bus/", sysname, NULL);
799 if (stat(path_full, &statbuf) == 0)
802 util_strscpyl(path, l, "/class/", sysname, NULL);
803 if (stat(path_full, &statbuf) == 0)
808 if (strcmp(subsystem, "module") == 0) {
809 util_strscpyl(path, l, "/module/", sysname, NULL);
810 if (stat(path_full, &statbuf) == 0)
815 if (strcmp(subsystem, "drivers") == 0) {
816 char subsys[UTIL_NAME_SIZE];
819 util_strscpy(subsys, sizeof(subsys), sysname);
820 driver = strchr(subsys, ':');
821 if (driver != NULL) {
825 util_strscpyl(path, l, "/subsystem/", subsys, "/drivers/", driver, NULL);
826 if (stat(path_full, &statbuf) == 0)
829 util_strscpyl(path, l, "/bus/", subsys, "/drivers/", driver, NULL);
830 if (stat(path_full, &statbuf) == 0)
836 util_strscpyl(path, l, "/subsystem/", subsystem, "/devices/", sysname, NULL);
837 if (stat(path_full, &statbuf) == 0)
840 util_strscpyl(path, l, "/bus/", subsystem, "/devices/", sysname, NULL);
841 if (stat(path_full, &statbuf) == 0)
844 util_strscpyl(path, l, "/class/", subsystem, "/", sysname, NULL);
845 if (stat(path_full, &statbuf) == 0)
850 return udev_device_new_from_syspath(udev, path_full);
854 * udev_device_new_from_environment
855 * @udev: udev library context
857 * Create new udev device, and fill in information from the
858 * current process environment. This only works reliable if
859 * the process is called from a udev rule. It is usually used
860 * for tools executed from IMPORT= rules.
862 * The initial refcount is 1, and needs to be decremented to
863 * release the resources of the udev device.
865 * Returns: a new udev device, or #NULL, if it does not exist
867 _public_ struct udev_device *udev_device_new_from_environment(struct udev *udev)
870 struct udev_device *udev_device;
872 udev_device = udev_device_new(udev);
873 if (udev_device == NULL)
875 udev_device_set_info_loaded(udev_device);
877 for (i = 0; environ[i] != NULL; i++)
878 udev_device_add_property_from_string_parse(udev_device, environ[i]);
880 if (udev_device_add_property_from_string_parse_finish(udev_device) < 0) {
881 dbg(udev, "missing values, invalid device\n");
882 udev_device_unref(udev_device);
889 static struct udev_device *device_new_from_parent(struct udev_device *udev_device)
891 struct udev_device *udev_device_parent = NULL;
892 char path[UTIL_PATH_SIZE];
895 util_strscpy(path, sizeof(path), udev_device->syspath);
896 subdir = &path[strlen(udev_get_sys_path(udev_device->udev))+1];
900 pos = strrchr(subdir, '/');
901 if (pos == NULL || pos < &subdir[2])
904 udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
905 if (udev_device_parent != NULL)
906 return udev_device_parent;
912 * udev_device_get_parent:
913 * @udev_device: the device to start searching from
915 * Find the next parent device, and fill in information from the sys
916 * device and the udev database entry.
918 * The returned the device is not referenced. It is attached to the
919 * child device, and will be cleaned up when the child device
922 * It is not necessarily just the upper level directory, empty or not
923 * recognized sys directories are ignored.
925 * It can be called as many times as needed, without caring about
928 * Returns: a new udev device, or #NULL, if it no parent exist.
930 _public_ struct udev_device *udev_device_get_parent(struct udev_device *udev_device)
932 if (udev_device == NULL)
934 if (!udev_device->parent_set) {
935 udev_device->parent_set = true;
936 udev_device->parent_device = device_new_from_parent(udev_device);
938 return udev_device->parent_device;
942 * udev_device_get_parent_with_subsystem_devtype:
943 * @udev_device: udev device to start searching from
944 * @subsystem: the subsystem of the device
945 * @devtype: the type (DEVTYPE) of the device
947 * Find the next parent device, with a matching subsystem and devtype
948 * value, and fill in information from the sys device and the udev
951 * If devtype is #NULL, only subsystem is checked, and any devtype will
954 * The returned the device is not referenced. It is attached to the
955 * child device, and will be cleaned up when the child device
958 * It can be called as many times as needed, without caring about
961 * Returns: a new udev device, or #NULL if no matching parent exists.
963 _public_ struct udev_device *udev_device_get_parent_with_subsystem_devtype(struct udev_device *udev_device, const char *subsystem, const char *devtype)
965 struct udev_device *parent;
967 if (subsystem == NULL)
970 parent = udev_device_get_parent(udev_device);
971 while (parent != NULL) {
972 const char *parent_subsystem;
973 const char *parent_devtype;
975 parent_subsystem = udev_device_get_subsystem(parent);
976 if (parent_subsystem != NULL && strcmp(parent_subsystem, subsystem) == 0) {
979 parent_devtype = udev_device_get_devtype(parent);
980 if (parent_devtype != NULL && strcmp(parent_devtype, devtype) == 0)
983 parent = udev_device_get_parent(parent);
989 * udev_device_get_udev:
990 * @udev_device: udev device
992 * Retrieve the udev library context the device was created with.
994 * Returns: the udev library context
996 _public_ struct udev *udev_device_get_udev(struct udev_device *udev_device)
998 if (udev_device == NULL)
1000 return udev_device->udev;
1005 * @udev_device: udev device
1007 * Take a reference of a udev device.
1009 * Returns: the passed udev device
1011 _public_ struct udev_device *udev_device_ref(struct udev_device *udev_device)
1013 if (udev_device == NULL)
1015 udev_device->refcount++;
1020 * udev_device_unref:
1021 * @udev_device: udev device
1023 * Drop a reference of a udev device. If the refcount reaches zero,
1024 * the resources of the device will be released.
1027 _public_ void 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);
1056 * udev_device_get_devpath:
1057 * @udev_device: udev device
1059 * Retrieve the kernel devpath value of the udev device. The path
1060 * does not contain the sys mount point, and starts with a '/'.
1062 * Returns: the devpath of the udev device
1064 _public_ const char *udev_device_get_devpath(struct udev_device *udev_device)
1066 if (udev_device == NULL)
1068 return udev_device->devpath;
1072 * udev_device_get_syspath:
1073 * @udev_device: udev device
1075 * Retrieve the sys path of the udev device. The path is an
1076 * absolute path and starts with the sys mount point.
1078 * Returns: the sys path of the udev device
1080 _public_ const char *udev_device_get_syspath(struct udev_device *udev_device)
1082 if (udev_device == NULL)
1084 return udev_device->syspath;
1088 * udev_device_get_sysname:
1089 * @udev_device: udev device
1091 * Returns: the sys name of the device device
1093 _public_ const char *udev_device_get_sysname(struct udev_device *udev_device)
1095 if (udev_device == NULL)
1097 return udev_device->sysname;
1101 * udev_device_get_sysnum:
1102 * @udev_device: udev device
1104 * Returns: the trailing number of of the device name
1106 _public_ const char *udev_device_get_sysnum(struct udev_device *udev_device)
1108 if (udev_device == NULL)
1110 return udev_device->sysnum;
1114 * udev_device_get_devnode:
1115 * @udev_device: udev device
1117 * Retrieve the device node file name belonging to the udev device.
1118 * The path is an absolute path, and starts with the device directory.
1120 * Returns: the device node file name of the udev device, or #NULL if no device node exists
1122 _public_ const char *udev_device_get_devnode(struct udev_device *udev_device)
1124 if (udev_device == NULL)
1126 if (udev_device->devnode != NULL)
1127 return udev_device->devnode;
1128 if (!udev_device->info_loaded)
1129 udev_device_read_uevent_file(udev_device);
1130 return udev_device->devnode;
1134 * udev_device_get_devlinks_list_entry:
1135 * @udev_device: udev device
1137 * Retrieve the list of device links pointing to the device file of
1138 * the udev device. The next list entry can be retrieved with
1139 * udev_list_entry_next(), which returns #NULL if no more entries exist.
1140 * The devlink path can be retrieved from the list entry by
1141 * udev_list_entry_get_name(). The path is an absolute path, and starts with
1142 * the device directory.
1144 * Returns: the first entry of the device node link list
1146 _public_ struct udev_list_entry *udev_device_get_devlinks_list_entry(struct udev_device *udev_device)
1148 if (udev_device == NULL)
1150 if (!udev_device->info_loaded)
1151 udev_device_read_db(udev_device, NULL);
1152 return udev_list_get_entry(&udev_device->devlinks_list);
1155 void udev_device_cleanup_devlinks_list(struct udev_device *udev_device)
1157 udev_device->devlinks_uptodate = false;
1158 udev_list_cleanup(&udev_device->devlinks_list);
1162 * udev_device_get_properties_list_entry:
1163 * @udev_device: udev device
1165 * Retrieve the list of key/value device properties of the udev
1166 * device. The next list entry can be retrieved with udev_list_entry_next(),
1167 * which returns #NULL if no more entries exist. The property name
1168 * can be retrieved from the list entry by udev_list_get_name(),
1169 * the property value by udev_list_get_value().
1171 * Returns: the first entry of the property list
1173 _public_ struct udev_list_entry *udev_device_get_properties_list_entry(struct udev_device *udev_device)
1175 if (udev_device == NULL)
1177 if (!udev_device->info_loaded) {
1178 udev_device_read_uevent_file(udev_device);
1179 udev_device_read_db(udev_device, NULL);
1181 if (!udev_device->devlinks_uptodate) {
1182 char symlinks[UTIL_PATH_SIZE];
1183 struct udev_list_entry *list_entry;
1185 udev_device->devlinks_uptodate = true;
1186 list_entry = udev_device_get_devlinks_list_entry(udev_device);
1187 if (list_entry != NULL) {
1192 l = util_strpcpyl(&s, sizeof(symlinks), udev_list_entry_get_name(list_entry), NULL);
1193 udev_list_entry_foreach(list_entry, udev_list_entry_get_next(list_entry))
1194 l = util_strpcpyl(&s, l, " ", udev_list_entry_get_name(list_entry), NULL);
1195 udev_device_add_property(udev_device, "DEVLINKS", symlinks);
1198 if (!udev_device->tags_uptodate) {
1199 udev_device->tags_uptodate = true;
1200 if (udev_device_get_tags_list_entry(udev_device) != NULL) {
1201 char tags[UTIL_PATH_SIZE];
1202 struct udev_list_entry *list_entry;
1207 l = util_strpcpyl(&s, sizeof(tags), ":", NULL);
1208 udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(udev_device))
1209 l = util_strpcpyl(&s, l, udev_list_entry_get_name(list_entry), ":", NULL);
1210 udev_device_add_property(udev_device, "TAGS", tags);
1213 return udev_list_get_entry(&udev_device->properties_list);
1217 * udev_device_get_action:
1218 * @udev_device: udev device
1220 * This is only valid if the device was received through a monitor. Devices read from
1221 * sys do not have an action string. Usual actions are: add, remove, change, online,
1224 * Returns: the kernel action value, or #NULL if there is no action value available.
1226 _public_ const char *udev_device_get_action(struct udev_device *udev_device)
1228 if (udev_device == NULL)
1230 return udev_device->action;
1234 * udev_device_get_usec_since_initialized:
1235 * @udev_device: udev device
1237 * Return the number of microseconds passed since udev set up the
1238 * device for the first time.
1240 * This is only implemented for devices with need to store properties
1241 * in the udev database. All other devices return 0 here.
1243 * Returns: the number of microseconds since the device was first seen.
1245 _public_ unsigned long long int udev_device_get_usec_since_initialized(struct udev_device *udev_device)
1247 unsigned long long now;
1249 if (udev_device == NULL)
1251 if (!udev_device->info_loaded)
1252 udev_device_read_db(udev_device, NULL);
1253 if (udev_device->usec_initialized == 0)
1258 return now - udev_device->usec_initialized;
1261 unsigned long long udev_device_get_usec_initialized(struct udev_device *udev_device)
1263 return udev_device->usec_initialized;
1266 void udev_device_set_usec_initialized(struct udev_device *udev_device, unsigned long long usec_initialized)
1270 udev_device->usec_initialized = usec_initialized;
1271 snprintf(num, sizeof(num), "%llu", usec_initialized);
1272 udev_device_add_property(udev_device, "USEC_INITIALIZED", num);
1276 * udev_device_get_sysattr_value:
1277 * @udev_device: udev device
1278 * @sysattr: attribute name
1280 * The retrieved value is cached in the device. Repeated calls will return the same
1281 * value and not open the attribute again.
1283 * Returns: the content of a sys attribute file, or #NULL if there is no sys attribute value.
1285 _public_ const char *udev_device_get_sysattr_value(struct udev_device *udev_device, const char *sysattr)
1287 struct udev_list_entry *list_entry;
1288 char path[UTIL_PATH_SIZE];
1290 struct stat statbuf;
1293 const char *val = NULL;
1295 if (udev_device == NULL)
1297 if (sysattr == NULL)
1300 /* look for possibly already cached result */
1301 list_entry = udev_list_get_entry(&udev_device->sysattr_value_list);
1302 list_entry = udev_list_entry_get_by_name(list_entry, sysattr);
1303 if (list_entry != NULL)
1304 return udev_list_entry_get_value(list_entry);
1306 util_strscpyl(path, sizeof(path), udev_device_get_syspath(udev_device), "/", sysattr, NULL);
1307 if (lstat(path, &statbuf) != 0) {
1308 udev_list_entry_add(&udev_device->sysattr_value_list, sysattr, NULL);
1312 if (S_ISLNK(statbuf.st_mode)) {
1313 struct udev_device *dev;
1316 * Some core links return only the last element of the target path,
1317 * these are just values, the paths should not be exposed.
1319 if (strcmp(sysattr, "driver") == 0 ||
1320 strcmp(sysattr, "subsystem") == 0 ||
1321 strcmp(sysattr, "module") == 0) {
1322 if (util_get_sys_core_link_value(udev_device->udev, sysattr,
1323 udev_device->syspath, value, sizeof(value)) < 0)
1325 list_entry = udev_list_entry_add(&udev_device->sysattr_value_list, sysattr, value);
1326 val = udev_list_entry_get_value(list_entry);
1330 /* resolve link to a device and return its syspath */
1331 util_strscpyl(path, sizeof(path), udev_device->syspath, "/", sysattr, NULL);
1332 dev = udev_device_new_from_syspath(udev_device->udev, path);
1334 list_entry = udev_list_entry_add(&udev_device->sysattr_value_list, sysattr,
1335 udev_device_get_syspath(dev));
1336 val = udev_list_entry_get_value(list_entry);
1337 udev_device_unref(dev);
1343 /* skip directories */
1344 if (S_ISDIR(statbuf.st_mode))
1347 /* skip non-readable files */
1348 if ((statbuf.st_mode & S_IRUSR) == 0)
1351 /* read attribute value */
1352 fd = open(path, O_RDONLY|O_CLOEXEC);
1355 size = read(fd, value, sizeof(value));
1359 if (size == sizeof(value))
1362 /* got a valid value, store it in cache and return it */
1364 util_remove_trailing_chars(value, '\n');
1365 list_entry = udev_list_entry_add(&udev_device->sysattr_value_list, sysattr, value);
1366 val = udev_list_entry_get_value(list_entry);
1371 static int udev_device_sysattr_list_read(struct udev_device *udev_device)
1373 struct dirent *dent;
1377 if (udev_device == NULL)
1379 if (udev_device->sysattr_list_read)
1382 dir = opendir(udev_device_get_syspath(udev_device));
1386 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
1387 char path[UTIL_PATH_SIZE];
1388 struct stat statbuf;
1390 /* only handle symlinks and regular files */
1391 if (dent->d_type != DT_LNK && dent->d_type != DT_REG)
1394 util_strscpyl(path, sizeof(path), udev_device_get_syspath(udev_device), "/", dent->d_name, NULL);
1395 if (lstat(path, &statbuf) != 0)
1397 if ((statbuf.st_mode & S_IRUSR) == 0)
1400 udev_list_entry_add(&udev_device->sysattr_list, dent->d_name, NULL);
1405 udev_device->sysattr_list_read = true;
1411 * udev_device_get_sysattr_list_entry:
1412 * @udev_device: udev device
1414 * Retrieve the list of available sysattrs, with value being empty;
1415 * This just return all available sysfs attributes for a particular
1416 * device without reading their values.
1418 * Returns: the first entry of the property list
1420 _public_ struct udev_list_entry *udev_device_get_sysattr_list_entry(struct udev_device *udev_device)
1422 if (!udev_device->sysattr_list_read) {
1424 ret = udev_device_sysattr_list_read(udev_device);
1429 return udev_list_get_entry(&udev_device->sysattr_list);
1432 int udev_device_set_syspath(struct udev_device *udev_device, const char *syspath)
1437 free(udev_device->syspath);
1438 udev_device->syspath = strdup(syspath);
1439 if (udev_device->syspath == NULL)
1441 udev_device->devpath = &udev_device->syspath[strlen(udev_get_sys_path(udev_device->udev))];
1442 udev_device_add_property(udev_device, "DEVPATH", udev_device->devpath);
1444 pos = strrchr(udev_device->syspath, '/');
1447 udev_device->sysname = strdup(&pos[1]);
1448 if (udev_device->sysname == NULL)
1451 /* some devices have '!' in their name, change that to '/' */
1453 while (udev_device->sysname[len] != '\0') {
1454 if (udev_device->sysname[len] == '!')
1455 udev_device->sysname[len] = '/';
1459 /* trailing number */
1460 while (len > 0 && isdigit(udev_device->sysname[--len]))
1461 udev_device->sysnum = &udev_device->sysname[len];
1463 /* sysname is completely numeric */
1465 udev_device->sysnum = NULL;
1470 int udev_device_set_devnode(struct udev_device *udev_device, const char *devnode)
1472 free(udev_device->devnode);
1473 if (devnode[0] != '/') {
1474 if (asprintf(&udev_device->devnode, "%s/%s", udev_get_dev_path(udev_device->udev), devnode) < 0)
1475 udev_device->devnode = NULL;
1477 udev_device->devnode = strdup(devnode);
1479 if (udev_device->devnode == NULL)
1481 udev_device_add_property(udev_device, "DEVNAME", udev_device->devnode);
1485 int udev_device_add_devlink(struct udev_device *udev_device, const char *devlink, int unique)
1487 struct udev_list_entry *list_entry;
1489 udev_device->devlinks_uptodate = false;
1490 list_entry = udev_list_entry_add(&udev_device->devlinks_list, devlink, NULL);
1491 if (list_entry == NULL)
1494 udev_list_entry_set_num(list_entry, true);
1498 const char *udev_device_get_id_filename(struct udev_device *udev_device)
1500 if (udev_device->id_filename == NULL) {
1501 if (udev_device_get_subsystem(udev_device) == NULL)
1504 if (major(udev_device_get_devnum(udev_device)) > 0) {
1505 /* use dev_t -- b259:131072, c254:0 */
1506 if (asprintf(&udev_device->id_filename, "%c%u:%u",
1507 strcmp(udev_device_get_subsystem(udev_device), "block") == 0 ? 'b' : 'c',
1508 major(udev_device_get_devnum(udev_device)),
1509 minor(udev_device_get_devnum(udev_device))) < 0)
1510 udev_device->id_filename = NULL;
1511 } else if (udev_device_get_ifindex(udev_device) > 0) {
1512 /* use netdev ifindex -- n3 */
1513 if (asprintf(&udev_device->id_filename, "n%u", udev_device_get_ifindex(udev_device)) < 0)
1514 udev_device->id_filename = NULL;
1517 * use $subsys:$syname -- pci:0000:00:1f.2
1518 * sysname() has '!' translated, get it from devpath
1520 const char *sysname;
1521 sysname = strrchr(udev_device->devpath, '/');
1522 if (sysname == NULL)
1524 sysname = &sysname[1];
1525 if (asprintf(&udev_device->id_filename, "+%s:%s", udev_device_get_subsystem(udev_device), sysname) < 0)
1526 udev_device->id_filename = NULL;
1529 return udev_device->id_filename;
1533 * udev_device_get_is_initialized:
1534 * @udev_device: udev device
1536 * Check if udev has already handled the device and has set up
1537 * device node permissions and context, or has renamed a network
1540 * This is only implemented for devices with a device node
1541 * or network interfaces. All other devices return 1 here.
1543 * Returns: 1 if the device is set up. 0 otherwise.
1545 _public_ int udev_device_get_is_initialized(struct udev_device *udev_device)
1547 if (!udev_device->info_loaded)
1548 udev_device_read_db(udev_device, NULL);
1549 return udev_device->is_initialized;
1552 void udev_device_set_is_initialized(struct udev_device *udev_device)
1554 udev_device->is_initialized = true;
1557 int udev_device_add_tag(struct udev_device *udev_device, const char *tag)
1559 if (strchr(tag, ':') != NULL || strchr(tag, ' ') != NULL)
1561 udev_device->tags_uptodate = false;
1562 if (udev_list_entry_add(&udev_device->tags_list, tag, NULL) != NULL)
1567 void udev_device_cleanup_tags_list(struct udev_device *udev_device)
1569 udev_device->tags_uptodate = false;
1570 udev_list_cleanup(&udev_device->tags_list);
1574 * udev_device_get_tags_list_entry:
1575 * @udev_device: udev device
1577 * Retrieve the list of tags attached to the udev device. The next
1578 * list entry can be retrieved with udev_list_entry_next(),
1579 * which returns #NULL if no more entries exist. The tag string
1580 * can be retrieved from the list entry by udev_list_get_name().
1582 * Returns: the first entry of the tag list
1584 _public_ struct udev_list_entry *udev_device_get_tags_list_entry(struct udev_device *udev_device)
1586 if (udev_device == NULL)
1588 if (!udev_device->info_loaded)
1589 udev_device_read_db(udev_device, NULL);
1590 return udev_list_get_entry(&udev_device->tags_list);
1593 _public_ int udev_device_has_tag(struct udev_device *udev_device, const char *tag)
1595 struct udev_list_entry *list_entry;
1597 if (udev_device == NULL)
1599 if (!udev_device->info_loaded)
1600 udev_device_read_db(udev_device, NULL);
1601 list_entry = udev_device_get_tags_list_entry(udev_device);
1602 if (udev_list_entry_get_by_name(list_entry, tag) != NULL)
1607 #define ENVP_SIZE 128
1608 #define MONITOR_BUF_SIZE 4096
1609 static int update_envp_monitor_buf(struct udev_device *udev_device)
1611 struct udev_list_entry *list_entry;
1616 /* monitor buffer of property strings */
1617 free(udev_device->monitor_buf);
1618 udev_device->monitor_buf_len = 0;
1619 udev_device->monitor_buf = malloc(MONITOR_BUF_SIZE);
1620 if (udev_device->monitor_buf == NULL)
1623 /* envp array, strings will point into monitor buffer */
1624 if (udev_device->envp == NULL)
1625 udev_device->envp = malloc(sizeof(char *) * ENVP_SIZE);
1626 if (udev_device->envp == NULL)
1630 s = udev_device->monitor_buf;
1631 l = MONITOR_BUF_SIZE;
1632 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device)) {
1635 key = udev_list_entry_get_name(list_entry);
1636 /* skip private variables */
1640 /* add string to envp array */
1641 udev_device->envp[i++] = s;
1642 if (i+1 >= ENVP_SIZE)
1645 /* add property string to monitor buffer */
1646 l = util_strpcpyl(&s, l, key, "=", udev_list_entry_get_value(list_entry), NULL);
1649 /* advance past the trailing '\0' that util_strpcpyl() guarantees */
1653 udev_device->envp[i] = NULL;
1654 udev_device->monitor_buf_len = s - udev_device->monitor_buf;
1655 udev_device->envp_uptodate = true;
1659 char **udev_device_get_properties_envp(struct udev_device *udev_device)
1661 if (!udev_device->envp_uptodate)
1662 if (update_envp_monitor_buf(udev_device) != 0)
1664 return udev_device->envp;
1667 ssize_t udev_device_get_properties_monitor_buf(struct udev_device *udev_device, const char **buf)
1669 if (!udev_device->envp_uptodate)
1670 if (update_envp_monitor_buf(udev_device) != 0)
1672 *buf = udev_device->monitor_buf;
1673 return udev_device->monitor_buf_len;
1676 int udev_device_set_action(struct udev_device *udev_device, const char *action)
1678 free(udev_device->action);
1679 udev_device->action = strdup(action);
1680 if (udev_device->action == NULL)
1682 udev_device_add_property(udev_device, "ACTION", udev_device->action);
1686 int udev_device_get_devlink_priority(struct udev_device *udev_device)
1688 if (!udev_device->info_loaded)
1689 udev_device_read_db(udev_device, NULL);
1690 return udev_device->devlink_priority;
1693 int udev_device_set_devlink_priority(struct udev_device *udev_device, int prio)
1695 udev_device->devlink_priority = prio;
1699 int udev_device_get_watch_handle(struct udev_device *udev_device)
1701 if (!udev_device->info_loaded)
1702 udev_device_read_db(udev_device, NULL);
1703 return udev_device->watch_handle;
1706 int udev_device_set_watch_handle(struct udev_device *udev_device, int handle)
1708 udev_device->watch_handle = handle;
1712 bool udev_device_get_db_persist(struct udev_device *udev_device)
1714 return udev_device->db_persist;
1717 void udev_device_set_db_persist(struct udev_device *udev_device)
1719 udev_device->db_persist = true;