2 This file is part of systemd.
4 Copyright 2008-2012 Kay Sievers <kay@vrfy.org>
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
32 #include <sys/ioctl.h>
33 #include <sys/socket.h>
34 #include <linux/sockios.h>
37 #include "libudev-private.h"
40 * SECTION:libudev-device
41 * @short_description: kernel sys devices
43 * Representation of kernel sys devices. Devices are uniquely identified
44 * by their syspath, every device has exactly one path in the kernel sys
45 * filesystem. Devices usually belong to a kernel subsystem, and and have
46 * a unique name inside that subsystem.
52 * Opaque object representing one kernel sys device.
56 struct udev_device *parent_device;
71 size_t monitor_buf_len;
72 struct udev_list devlinks_list;
73 struct udev_list properties_list;
74 struct udev_list sysattr_value_list;
75 struct udev_list sysattr_list;
76 struct udev_list tags_list;
77 unsigned long long int seqnum;
78 usec_t usec_initialized;
88 bool devlinks_uptodate;
96 bool sysattr_list_read;
101 * udev_device_get_seqnum:
102 * @udev_device: udev device
104 * This is only valid if the device was received through a monitor. Devices read from
105 * sys do not have a sequence number.
107 * Returns: the kernel event sequence number, or 0 if there is no sequence number available.
109 _public_ unsigned long long int udev_device_get_seqnum(struct udev_device *udev_device)
111 if (udev_device == NULL)
113 return udev_device->seqnum;
116 static int udev_device_set_seqnum(struct udev_device *udev_device, unsigned long long int seqnum)
120 udev_device->seqnum = seqnum;
121 snprintf(num, sizeof(num), "%llu", seqnum);
122 udev_device_add_property(udev_device, "SEQNUM", num);
126 int udev_device_get_ifindex(struct udev_device *udev_device)
128 if (!udev_device->info_loaded)
129 udev_device_read_uevent_file(udev_device);
130 return udev_device->ifindex;
133 static int udev_device_set_ifindex(struct udev_device *udev_device, int ifindex)
137 udev_device->ifindex = ifindex;
138 snprintf(num, sizeof(num), "%u", ifindex);
139 udev_device_add_property(udev_device, "IFINDEX", num);
144 * udev_device_get_devnum:
145 * @udev_device: udev device
147 * Get the device major/minor number.
149 * Returns: the dev_t number.
151 _public_ dev_t udev_device_get_devnum(struct udev_device *udev_device)
153 if (udev_device == NULL)
154 return makedev(0, 0);
155 if (!udev_device->info_loaded)
156 udev_device_read_uevent_file(udev_device);
157 return udev_device->devnum;
160 static int udev_device_set_devnum(struct udev_device *udev_device, dev_t devnum)
164 udev_device->devnum = devnum;
166 snprintf(num, sizeof(num), "%u", major(devnum));
167 udev_device_add_property(udev_device, "MAJOR", num);
168 snprintf(num, sizeof(num), "%u", minor(devnum));
169 udev_device_add_property(udev_device, "MINOR", num);
173 const char *udev_device_get_devpath_old(struct udev_device *udev_device)
175 return udev_device->devpath_old;
178 static int udev_device_set_devpath_old(struct udev_device *udev_device, const char *devpath_old)
182 free(udev_device->devpath_old);
183 udev_device->devpath_old = strdup(devpath_old);
184 if (udev_device->devpath_old == NULL)
186 udev_device_add_property(udev_device, "DEVPATH_OLD", udev_device->devpath_old);
188 pos = strrchr(udev_device->devpath_old, '/');
195 * udev_device_get_driver:
196 * @udev_device: udev device
198 * Get the kernel driver name.
200 * Returns: the driver name string, or #NULL if there is no driver attached.
202 _public_ const char *udev_device_get_driver(struct udev_device *udev_device)
204 char driver[UTIL_NAME_SIZE];
206 if (udev_device == NULL)
208 if (!udev_device->driver_set) {
209 udev_device->driver_set = true;
210 if (util_get_sys_core_link_value(udev_device->udev, "driver", udev_device->syspath, driver, sizeof(driver)) > 0)
211 udev_device->driver = strdup(driver);
213 return udev_device->driver;
216 static int udev_device_set_driver(struct udev_device *udev_device, const char *driver)
218 free(udev_device->driver);
219 udev_device->driver = strdup(driver);
220 if (udev_device->driver == NULL)
222 udev_device->driver_set = true;
223 udev_device_add_property(udev_device, "DRIVER", udev_device->driver);
228 * udev_device_get_devtype:
229 * @udev_device: udev device
231 * Retrieve the devtype string of the udev device.
233 * Returns: the devtype name of the udev device, or #NULL if it can not be determined
235 _public_ const char *udev_device_get_devtype(struct udev_device *udev_device)
237 if (udev_device == NULL)
239 if (!udev_device->devtype_set) {
240 udev_device->devtype_set = true;
241 udev_device_read_uevent_file(udev_device);
243 return udev_device->devtype;
246 static int udev_device_set_devtype(struct udev_device *udev_device, const char *devtype)
248 free(udev_device->devtype);
249 udev_device->devtype = strdup(devtype);
250 if (udev_device->devtype == NULL)
252 udev_device->devtype_set = true;
253 udev_device_add_property(udev_device, "DEVTYPE", udev_device->devtype);
257 int udev_device_set_subsystem(struct udev_device *udev_device, const char *subsystem)
259 free(udev_device->subsystem);
260 udev_device->subsystem = strdup(subsystem);
261 if (udev_device->subsystem == NULL)
263 udev_device->subsystem_set = true;
264 udev_device_add_property(udev_device, "SUBSYSTEM", udev_device->subsystem);
269 * udev_device_get_subsystem:
270 * @udev_device: udev device
272 * Retrieve the subsystem string of the udev device. The string does not
275 * Returns: the subsystem name of the udev device, or #NULL if it can not be determined
277 _public_ const char *udev_device_get_subsystem(struct udev_device *udev_device)
279 char subsystem[UTIL_NAME_SIZE];
281 if (udev_device == NULL)
283 if (!udev_device->subsystem_set) {
284 udev_device->subsystem_set = true;
285 /* read "subsystem" link */
286 if (util_get_sys_core_link_value(udev_device->udev, "subsystem", udev_device->syspath, subsystem, sizeof(subsystem)) > 0) {
287 udev_device_set_subsystem(udev_device, subsystem);
288 return udev_device->subsystem;
291 if (startswith(udev_device->devpath, "/module/")) {
292 udev_device_set_subsystem(udev_device, "module");
293 return udev_device->subsystem;
295 if (strstr(udev_device->devpath, "/drivers/") != NULL) {
296 udev_device_set_subsystem(udev_device, "drivers");
297 return udev_device->subsystem;
299 if (startswith(udev_device->devpath, "/subsystem/") ||
300 startswith(udev_device->devpath, "/class/") ||
301 startswith(udev_device->devpath, "/bus/")) {
302 udev_device_set_subsystem(udev_device, "subsystem");
303 return udev_device->subsystem;
306 return udev_device->subsystem;
309 mode_t udev_device_get_devnode_mode(struct udev_device *udev_device)
311 if (!udev_device->info_loaded)
312 udev_device_read_uevent_file(udev_device);
313 return udev_device->devnode_mode;
316 static int udev_device_set_devnode_mode(struct udev_device *udev_device, mode_t mode)
320 udev_device->devnode_mode = mode;
321 snprintf(num, sizeof(num), "%#o", mode);
322 udev_device_add_property(udev_device, "DEVMODE", num);
326 struct udev_list_entry *udev_device_add_property(struct udev_device *udev_device, const char *key, const char *value)
328 udev_device->envp_uptodate = false;
330 struct udev_list_entry *list_entry;
332 list_entry = udev_device_get_properties_list_entry(udev_device);
333 list_entry = udev_list_entry_get_by_name(list_entry, key);
334 if (list_entry != NULL)
335 udev_list_entry_delete(list_entry);
338 return udev_list_entry_add(&udev_device->properties_list, key, value);
341 static struct udev_list_entry *udev_device_add_property_from_string(struct udev_device *udev_device, const char *property)
343 char name[UTIL_LINE_SIZE];
346 util_strscpy(name, sizeof(name), property);
347 val = strchr(name, '=');
354 return udev_device_add_property(udev_device, name, val);
358 * parse property string, and if needed, update internal values accordingly
360 * udev_device_add_property_from_string_parse_finish() needs to be
361 * called after adding properties, and its return value checked
363 * udev_device_set_info_loaded() needs to be set, to avoid trying
364 * to use a device without a DEVPATH set
366 void udev_device_add_property_from_string_parse(struct udev_device *udev_device, const char *property)
368 if (startswith(property, "DEVPATH=")) {
369 char path[UTIL_PATH_SIZE];
371 util_strscpyl(path, sizeof(path), "/sys", &property[8], NULL);
372 udev_device_set_syspath(udev_device, path);
373 } else if (startswith(property, "SUBSYSTEM=")) {
374 udev_device_set_subsystem(udev_device, &property[10]);
375 } else if (startswith(property, "DEVTYPE=")) {
376 udev_device_set_devtype(udev_device, &property[8]);
377 } else if (startswith(property, "DEVNAME=")) {
378 udev_device_set_devnode(udev_device, &property[8]);
379 } else if (startswith(property, "DEVLINKS=")) {
380 char devlinks[UTIL_PATH_SIZE];
384 util_strscpy(devlinks, sizeof(devlinks), &property[9]);
386 next = strchr(slink, ' ');
387 while (next != NULL) {
389 udev_device_add_devlink(udev_device, slink);
391 next = strchr(slink, ' ');
393 if (slink[0] != '\0')
394 udev_device_add_devlink(udev_device, slink);
395 } else if (startswith(property, "TAGS=")) {
396 char tags[UTIL_PATH_SIZE];
399 util_strscpy(tags, sizeof(tags), &property[5]);
400 next = strchr(tags, ':');
403 while (next[0] != '\0') {
407 next = strchr(tag, ':');
412 udev_device_add_tag(udev_device, tag);
415 } else if (startswith(property, "USEC_INITIALIZED=")) {
416 udev_device_set_usec_initialized(udev_device, strtoull(&property[19], NULL, 10));
417 } else if (startswith(property, "DRIVER=")) {
418 udev_device_set_driver(udev_device, &property[7]);
419 } else if (startswith(property, "ACTION=")) {
420 udev_device_set_action(udev_device, &property[7]);
421 } else if (startswith(property, "MAJOR=")) {
422 udev_device->maj = strtoull(&property[6], NULL, 10);
423 } else if (startswith(property, "MINOR=")) {
424 udev_device->min = strtoull(&property[6], NULL, 10);
425 } else if (startswith(property, "DEVPATH_OLD=")) {
426 udev_device_set_devpath_old(udev_device, &property[12]);
427 } else if (startswith(property, "SEQNUM=")) {
428 udev_device_set_seqnum(udev_device, strtoull(&property[7], NULL, 10));
429 } else if (startswith(property, "IFINDEX=")) {
430 udev_device_set_ifindex(udev_device, strtoull(&property[8], NULL, 10));
431 } else if (startswith(property, "DEVMODE=")) {
432 udev_device_set_devnode_mode(udev_device, strtoul(&property[8], NULL, 8));
434 udev_device_add_property_from_string(udev_device, property);
438 int udev_device_add_property_from_string_parse_finish(struct udev_device *udev_device)
440 if (udev_device->maj > 0)
441 udev_device_set_devnum(udev_device, makedev(udev_device->maj, udev_device->min));
442 udev_device->maj = 0;
443 udev_device->min = 0;
445 if (udev_device->devpath == NULL || udev_device->subsystem == NULL)
451 * udev_device_get_property_value:
452 * @udev_device: udev device
453 * @key: property name
455 * Get the value of a given property.
457 * Returns: the property string, or #NULL if there is no such property.
459 _public_ const char *udev_device_get_property_value(struct udev_device *udev_device, const char *key)
461 struct udev_list_entry *list_entry;
463 if (udev_device == NULL)
468 list_entry = udev_device_get_properties_list_entry(udev_device);
469 list_entry = udev_list_entry_get_by_name(list_entry, key);
470 return udev_list_entry_get_value(list_entry);
473 int udev_device_read_db(struct udev_device *udev_device, const char *dbfile)
475 char filename[UTIL_PATH_SIZE];
476 char line[UTIL_LINE_SIZE];
479 /* providing a database file will always force-load it */
480 if (dbfile == NULL) {
483 if (udev_device->db_loaded)
485 udev_device->db_loaded = true;
487 id = udev_device_get_id_filename(udev_device);
490 util_strscpyl(filename, sizeof(filename), "/run/udev/data/", id, NULL);
494 f = fopen(dbfile, "re");
496 udev_dbg(udev_device->udev, "no db file to read %s: %m\n", dbfile);
499 udev_device->is_initialized = true;
501 while (fgets(line, sizeof(line), f)) {
504 struct udev_list_entry *entry;
513 util_strscpyl(filename, sizeof(filename), "/dev/", val, NULL);
514 udev_device_add_devlink(udev_device, filename);
517 udev_device_set_devlink_priority(udev_device, atoi(val));
520 entry = udev_device_add_property_from_string(udev_device, val);
521 udev_list_entry_set_num(entry, true);
524 udev_device_add_tag(udev_device, val);
527 udev_device_set_watch_handle(udev_device, atoi(val));
530 udev_device_set_usec_initialized(udev_device, strtoull(val, NULL, 10));
536 udev_dbg(udev_device->udev, "device %p filled with db file data\n", udev_device);
540 int udev_device_read_uevent_file(struct udev_device *udev_device)
542 char filename[UTIL_PATH_SIZE];
544 char line[UTIL_LINE_SIZE];
548 if (udev_device->uevent_loaded)
551 util_strscpyl(filename, sizeof(filename), udev_device->syspath, "/uevent", NULL);
552 f = fopen(filename, "re");
555 udev_device->uevent_loaded = true;
557 while (fgets(line, sizeof(line), f)) {
560 pos = strchr(line, '\n');
565 if (startswith(line, "DEVTYPE=")) {
566 udev_device_set_devtype(udev_device, &line[8]);
569 if (startswith(line, "IFINDEX=")) {
570 udev_device_set_ifindex(udev_device, strtoull(&line[8], NULL, 10));
573 if (startswith(line, "DEVNAME=")) {
574 udev_device_set_devnode(udev_device, &line[8]);
578 if (startswith(line, "MAJOR="))
579 maj = strtoull(&line[6], NULL, 10);
580 else if (startswith(line, "MINOR="))
581 min = strtoull(&line[6], NULL, 10);
582 else if (startswith(line, "DEVMODE="))
583 udev_device->devnode_mode = strtoul(&line[8], NULL, 8);
585 udev_device_add_property_from_string(udev_device, line);
588 udev_device->devnum = makedev(maj, min);
593 void udev_device_set_info_loaded(struct udev_device *device)
595 device->info_loaded = true;
598 struct udev_device *udev_device_new(struct udev *udev)
600 struct udev_device *udev_device;
601 struct udev_list_entry *list_entry;
606 udev_device = calloc(1, sizeof(struct udev_device));
607 if (udev_device == NULL)
609 udev_device->refcount = 1;
610 udev_device->udev = udev;
611 udev_list_init(udev, &udev_device->devlinks_list, true);
612 udev_list_init(udev, &udev_device->properties_list, true);
613 udev_list_init(udev, &udev_device->sysattr_value_list, true);
614 udev_list_init(udev, &udev_device->sysattr_list, false);
615 udev_list_init(udev, &udev_device->tags_list, true);
616 udev_device->watch_handle = -1;
617 /* copy global properties */
618 udev_list_entry_foreach(list_entry, udev_get_properties_list_entry(udev))
619 udev_device_add_property(udev_device,
620 udev_list_entry_get_name(list_entry),
621 udev_list_entry_get_value(list_entry));
626 * udev_device_new_from_syspath:
627 * @udev: udev library context
628 * @syspath: sys device path including sys directory
630 * Create new udev device, and fill in information from the sys
631 * device and the udev database entry. The syspath is the absolute
632 * path to the device, including the sys mount point.
634 * The initial refcount is 1, and needs to be decremented to
635 * release the resources of the udev device.
637 * Returns: a new udev device, or #NULL, if it does not exist
639 _public_ struct udev_device *udev_device_new_from_syspath(struct udev *udev, const char *syspath)
642 char path[UTIL_PATH_SIZE];
645 struct udev_device *udev_device;
652 /* path starts in sys */
653 if (!startswith(syspath, "/sys")) {
654 udev_dbg(udev, "not in sys :%s\n", syspath);
658 /* path is not a root directory */
659 subdir = syspath + strlen("/sys");
660 pos = strrchr(subdir, '/');
661 if (pos == NULL || pos[1] == '\0' || pos < &subdir[2])
664 /* resolve possible symlink to real path */
665 util_strscpy(path, sizeof(path), syspath);
666 util_resolve_sys_link(udev, path, sizeof(path));
668 if (startswith(path + strlen("/sys"), "/devices/")) {
669 char file[UTIL_PATH_SIZE];
671 /* all "devices" require a "uevent" file */
672 util_strscpyl(file, sizeof(file), path, "/uevent", NULL);
673 if (stat(file, &statbuf) != 0)
676 /* everything else just needs to be a directory */
677 if (stat(path, &statbuf) != 0 || !S_ISDIR(statbuf.st_mode))
681 udev_device = udev_device_new(udev);
682 if (udev_device == NULL)
685 udev_device_set_syspath(udev_device, path);
686 udev_dbg(udev, "device %p has devpath '%s'\n", udev_device, udev_device_get_devpath(udev_device));
692 * udev_device_new_from_devnum:
693 * @udev: udev library context
694 * @type: char or block device
695 * @devnum: device major/minor number
697 * Create new udev device, and fill in information from the sys
698 * device and the udev database entry. The device is looked-up
699 * by its major/minor number and type. Character and block device
700 * numbers are not unique across the two types.
702 * The initial refcount is 1, and needs to be decremented to
703 * release the resources of the udev device.
705 * Returns: a new udev device, or #NULL, if it does not exist
707 _public_ struct udev_device *udev_device_new_from_devnum(struct udev *udev, char type, dev_t devnum)
709 char path[UTIL_PATH_SIZE];
710 const char *type_str;
714 else if (type == 'c')
719 /* use /sys/dev/{block,char}/<maj>:<min> link */
720 snprintf(path, sizeof(path), "/sys/dev/%s/%u:%u",
721 type_str, major(devnum), minor(devnum));
722 return udev_device_new_from_syspath(udev, path);
726 * udev_device_new_from_device_id:
727 * @udev: udev library context
728 * @id: text string identifying a kernel device
730 * Create new udev device, and fill in information from the sys
731 * device and the udev database entry. The device is looked-up
732 * by a special string:
733 * b8:2 - block device major:minor
734 * c128:1 - char device major:minor
735 * n3 - network device ifindex
736 * +sound:card29 - kernel driver core subsystem:device name
738 * The initial refcount is 1, and needs to be decremented to
739 * release the resources of the udev device.
741 * Returns: a new udev device, or #NULL, if it does not exist
743 _public_ struct udev_device *udev_device_new_from_device_id(struct udev *udev, char *id)
747 char subsys[UTIL_PATH_SIZE];
753 if (sscanf(id, "%c%i:%i", &type, &maj, &min) != 3)
755 return udev_device_new_from_devnum(udev, type, makedev(maj, min));
759 struct udev_device *dev;
762 ifindex = strtoul(&id[1], NULL, 10);
766 sk = socket(PF_INET, SOCK_DGRAM, 0);
769 memset(&ifr, 0x00, sizeof(struct ifreq));
770 ifr.ifr_ifindex = ifindex;
771 if (ioctl(sk, SIOCGIFNAME, &ifr) != 0) {
777 dev = udev_device_new_from_subsystem_sysname(udev, "net", ifr.ifr_name);
780 if (udev_device_get_ifindex(dev) == ifindex)
782 udev_device_unref(dev);
786 util_strscpy(subsys, sizeof(subsys), &id[1]);
787 sysname = strchr(subsys, ':');
791 sysname = &sysname[1];
792 return udev_device_new_from_subsystem_sysname(udev, subsys, sysname);
799 * udev_device_new_from_subsystem_sysname:
800 * @udev: udev library context
801 * @subsystem: the subsystem of the device
802 * @sysname: the name of the device
804 * Create new udev device, and fill in information from the sys device
805 * and the udev database entry. The device is looked up by the subsystem
806 * and name string of the device, like "mem" / "zero", or "block" / "sda".
808 * The initial refcount is 1, and needs to be decremented to
809 * release the resources of the udev device.
811 * Returns: a new udev device, or #NULL, if it does not exist
813 _public_ struct udev_device *udev_device_new_from_subsystem_sysname(struct udev *udev, const char *subsystem, const char *sysname)
815 char path[UTIL_PATH_SIZE];
818 if (streq(subsystem, "subsystem")) {
819 util_strscpyl(path, sizeof(path), "/sys/subsystem/", sysname, NULL);
820 if (stat(path, &statbuf) == 0)
823 util_strscpyl(path, sizeof(path), "/sys/bus/", sysname, NULL);
824 if (stat(path, &statbuf) == 0)
827 util_strscpyl(path, sizeof(path), "/sys/class/", sysname, NULL);
828 if (stat(path, &statbuf) == 0)
833 if (streq(subsystem, "module")) {
834 util_strscpyl(path, sizeof(path), "/sys/module/", sysname, NULL);
835 if (stat(path, &statbuf) == 0)
840 if (streq(subsystem, "drivers")) {
841 char subsys[UTIL_NAME_SIZE];
844 util_strscpy(subsys, sizeof(subsys), sysname);
845 driver = strchr(subsys, ':');
846 if (driver != NULL) {
850 util_strscpyl(path, sizeof(path), "/sys/subsystem/", subsys, "/drivers/", driver, NULL);
851 if (stat(path, &statbuf) == 0)
854 util_strscpyl(path, sizeof(path), "/sys/bus/", subsys, "/drivers/", driver, NULL);
855 if (stat(path, &statbuf) == 0)
861 util_strscpyl(path, sizeof(path), "/sys/subsystem/", subsystem, "/devices/", sysname, NULL);
862 if (stat(path, &statbuf) == 0)
865 util_strscpyl(path, sizeof(path), "/sys/bus/", subsystem, "/devices/", sysname, NULL);
866 if (stat(path, &statbuf) == 0)
869 util_strscpyl(path, sizeof(path), "/sys/class/", subsystem, "/", sysname, NULL);
870 if (stat(path, &statbuf) == 0)
875 return udev_device_new_from_syspath(udev, path);
879 * udev_device_new_from_environment
880 * @udev: udev library context
882 * Create new udev device, and fill in information from the
883 * current process environment. This only works reliable if
884 * the process is called from a udev rule. It is usually used
885 * for tools executed from IMPORT= rules.
887 * The initial refcount is 1, and needs to be decremented to
888 * release the resources of the udev device.
890 * Returns: a new udev device, or #NULL, if it does not exist
892 _public_ struct udev_device *udev_device_new_from_environment(struct udev *udev)
895 struct udev_device *udev_device;
897 udev_device = udev_device_new(udev);
898 if (udev_device == NULL)
900 udev_device_set_info_loaded(udev_device);
902 for (i = 0; environ[i] != NULL; i++)
903 udev_device_add_property_from_string_parse(udev_device, environ[i]);
905 if (udev_device_add_property_from_string_parse_finish(udev_device) < 0) {
906 udev_dbg(udev, "missing values, invalid device\n");
907 udev_device_unref(udev_device);
914 static struct udev_device *device_new_from_parent(struct udev_device *udev_device)
916 struct udev_device *udev_device_parent = NULL;
917 char path[UTIL_PATH_SIZE];
920 util_strscpy(path, sizeof(path), udev_device->syspath);
921 subdir = path + strlen("/sys/");
925 pos = strrchr(subdir, '/');
926 if (pos == NULL || pos < &subdir[2])
929 udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
930 if (udev_device_parent != NULL)
931 return udev_device_parent;
937 * udev_device_get_parent:
938 * @udev_device: the device to start searching from
940 * Find the next parent device, and fill in information from the sys
941 * device and the udev database entry.
943 * The returned the device is not referenced. It is attached to the
944 * child device, and will be cleaned up when the child device
947 * It is not necessarily just the upper level directory, empty or not
948 * recognized sys directories are ignored.
950 * It can be called as many times as needed, without caring about
953 * Returns: a new udev device, or #NULL, if it no parent exist.
955 _public_ struct udev_device *udev_device_get_parent(struct udev_device *udev_device)
957 if (udev_device == NULL)
959 if (!udev_device->parent_set) {
960 udev_device->parent_set = true;
961 udev_device->parent_device = device_new_from_parent(udev_device);
963 return udev_device->parent_device;
967 * udev_device_get_parent_with_subsystem_devtype:
968 * @udev_device: udev device to start searching from
969 * @subsystem: the subsystem of the device
970 * @devtype: the type (DEVTYPE) of the device
972 * Find the next parent device, with a matching subsystem and devtype
973 * value, and fill in information from the sys device and the udev
976 * If devtype is #NULL, only subsystem is checked, and any devtype will
979 * The returned the device is not referenced. It is attached to the
980 * child device, and will be cleaned up when the child device
983 * It can be called as many times as needed, without caring about
986 * Returns: a new udev device, or #NULL if no matching parent exists.
988 _public_ struct udev_device *udev_device_get_parent_with_subsystem_devtype(struct udev_device *udev_device, const char *subsystem, const char *devtype)
990 struct udev_device *parent;
992 if (subsystem == NULL)
995 parent = udev_device_get_parent(udev_device);
996 while (parent != NULL) {
997 const char *parent_subsystem;
998 const char *parent_devtype;
1000 parent_subsystem = udev_device_get_subsystem(parent);
1001 if (parent_subsystem != NULL && streq(parent_subsystem, subsystem)) {
1002 if (devtype == NULL)
1004 parent_devtype = udev_device_get_devtype(parent);
1005 if (parent_devtype != NULL && streq(parent_devtype, devtype))
1008 parent = udev_device_get_parent(parent);
1014 * udev_device_get_udev:
1015 * @udev_device: udev device
1017 * Retrieve the udev library context the device was created with.
1019 * Returns: the udev library context
1021 _public_ struct udev *udev_device_get_udev(struct udev_device *udev_device)
1023 if (udev_device == NULL)
1025 return udev_device->udev;
1030 * @udev_device: udev device
1032 * Take a reference of a udev device.
1034 * Returns: the passed udev device
1036 _public_ struct udev_device *udev_device_ref(struct udev_device *udev_device)
1038 if (udev_device == NULL)
1040 udev_device->refcount++;
1045 * udev_device_unref:
1046 * @udev_device: udev device
1048 * Drop a reference of a udev device. If the refcount reaches zero,
1049 * the resources of the device will be released.
1051 * Returns: the passed udev device if it has still an active reference, or #NULL otherwise.
1053 _public_ struct udev_device *udev_device_unref(struct udev_device *udev_device)
1055 if (udev_device == NULL)
1057 udev_device->refcount--;
1058 if (udev_device->refcount > 0)
1060 if (udev_device->parent_device != NULL)
1061 udev_device_unref(udev_device->parent_device);
1062 free(udev_device->syspath);
1063 free(udev_device->sysname);
1064 free(udev_device->devnode);
1065 free(udev_device->subsystem);
1066 free(udev_device->devtype);
1067 udev_list_cleanup(&udev_device->devlinks_list);
1068 udev_list_cleanup(&udev_device->properties_list);
1069 udev_list_cleanup(&udev_device->sysattr_value_list);
1070 udev_list_cleanup(&udev_device->sysattr_list);
1071 udev_list_cleanup(&udev_device->tags_list);
1072 free(udev_device->action);
1073 free(udev_device->driver);
1074 free(udev_device->devpath_old);
1075 free(udev_device->id_filename);
1076 free(udev_device->envp);
1077 free(udev_device->monitor_buf);
1083 * udev_device_get_devpath:
1084 * @udev_device: udev device
1086 * Retrieve the kernel devpath value of the udev device. The path
1087 * does not contain the sys mount point, and starts with a '/'.
1089 * Returns: the devpath of the udev device
1091 _public_ const char *udev_device_get_devpath(struct udev_device *udev_device)
1093 if (udev_device == NULL)
1095 return udev_device->devpath;
1099 * udev_device_get_syspath:
1100 * @udev_device: udev device
1102 * Retrieve the sys path of the udev device. The path is an
1103 * absolute path and starts with the sys mount point.
1105 * Returns: the sys path of the udev device
1107 _public_ const char *udev_device_get_syspath(struct udev_device *udev_device)
1109 if (udev_device == NULL)
1111 return udev_device->syspath;
1115 * udev_device_get_sysname:
1116 * @udev_device: udev device
1118 * Get the kernel device name in /sys.
1120 * Returns: the name string of the device device
1122 _public_ const char *udev_device_get_sysname(struct udev_device *udev_device)
1124 if (udev_device == NULL)
1126 return udev_device->sysname;
1130 * udev_device_get_sysnum:
1131 * @udev_device: udev device
1133 * Get the instance number of the device.
1135 * Returns: the trailing number string of the device name
1137 _public_ const char *udev_device_get_sysnum(struct udev_device *udev_device)
1139 if (udev_device == NULL)
1141 return udev_device->sysnum;
1145 * udev_device_get_devnode:
1146 * @udev_device: udev device
1148 * Retrieve the device node file name belonging to the udev device.
1149 * The path is an absolute path, and starts with the device directory.
1151 * Returns: the device node file name of the udev device, or #NULL if no device node exists
1153 _public_ const char *udev_device_get_devnode(struct udev_device *udev_device)
1155 if (udev_device == NULL)
1157 if (udev_device->devnode != NULL)
1158 return udev_device->devnode;
1159 if (!udev_device->info_loaded)
1160 udev_device_read_uevent_file(udev_device);
1161 return udev_device->devnode;
1165 * udev_device_get_devlinks_list_entry:
1166 * @udev_device: udev device
1168 * Retrieve the list of device links pointing to the device file of
1169 * the udev device. The next list entry can be retrieved with
1170 * udev_list_entry_get_next(), which returns #NULL if no more entries exist.
1171 * The devlink path can be retrieved from the list entry by
1172 * udev_list_entry_get_name(). The path is an absolute path, and starts with
1173 * the device directory.
1175 * Returns: the first entry of the device node link list
1177 _public_ struct udev_list_entry *udev_device_get_devlinks_list_entry(struct udev_device *udev_device)
1179 if (udev_device == NULL)
1181 if (!udev_device->info_loaded)
1182 udev_device_read_db(udev_device, NULL);
1183 return udev_list_get_entry(&udev_device->devlinks_list);
1186 void udev_device_cleanup_devlinks_list(struct udev_device *udev_device)
1188 udev_device->devlinks_uptodate = false;
1189 udev_list_cleanup(&udev_device->devlinks_list);
1193 * udev_device_get_properties_list_entry:
1194 * @udev_device: udev device
1196 * Retrieve the list of key/value device properties of the udev
1197 * device. The next list entry can be retrieved with udev_list_entry_get_next(),
1198 * which returns #NULL if no more entries exist. The property name
1199 * can be retrieved from the list entry by udev_list_entry_get_name(),
1200 * the property value by udev_list_entry_get_value().
1202 * Returns: the first entry of the property list
1204 _public_ struct udev_list_entry *udev_device_get_properties_list_entry(struct udev_device *udev_device)
1206 if (udev_device == NULL)
1208 if (!udev_device->info_loaded) {
1209 udev_device_read_uevent_file(udev_device);
1210 udev_device_read_db(udev_device, NULL);
1212 if (!udev_device->devlinks_uptodate) {
1213 char symlinks[UTIL_PATH_SIZE];
1214 struct udev_list_entry *list_entry;
1216 udev_device->devlinks_uptodate = true;
1217 list_entry = udev_device_get_devlinks_list_entry(udev_device);
1218 if (list_entry != NULL) {
1223 l = util_strpcpyl(&s, sizeof(symlinks), udev_list_entry_get_name(list_entry), NULL);
1224 udev_list_entry_foreach(list_entry, udev_list_entry_get_next(list_entry))
1225 l = util_strpcpyl(&s, l, " ", udev_list_entry_get_name(list_entry), NULL);
1226 udev_device_add_property(udev_device, "DEVLINKS", symlinks);
1229 if (!udev_device->tags_uptodate) {
1230 udev_device->tags_uptodate = true;
1231 if (udev_device_get_tags_list_entry(udev_device) != NULL) {
1232 char tags[UTIL_PATH_SIZE];
1233 struct udev_list_entry *list_entry;
1238 l = util_strpcpyl(&s, sizeof(tags), ":", NULL);
1239 udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(udev_device))
1240 l = util_strpcpyl(&s, l, udev_list_entry_get_name(list_entry), ":", NULL);
1241 udev_device_add_property(udev_device, "TAGS", tags);
1244 return udev_list_get_entry(&udev_device->properties_list);
1248 * udev_device_get_action:
1249 * @udev_device: udev device
1251 * This is only valid if the device was received through a monitor. Devices read from
1252 * sys do not have an action string. Usual actions are: add, remove, change, online,
1255 * Returns: the kernel action value, or #NULL if there is no action value available.
1257 _public_ const char *udev_device_get_action(struct udev_device *udev_device)
1259 if (udev_device == NULL)
1261 return udev_device->action;
1265 * udev_device_get_usec_since_initialized:
1266 * @udev_device: udev device
1268 * Return the number of microseconds passed since udev set up the
1269 * device for the first time.
1271 * This is only implemented for devices with need to store properties
1272 * in the udev database. All other devices return 0 here.
1274 * Returns: the number of microseconds since the device was first seen.
1276 _public_ unsigned long long int udev_device_get_usec_since_initialized(struct udev_device *udev_device)
1280 if (udev_device == NULL)
1282 if (!udev_device->info_loaded)
1283 udev_device_read_db(udev_device, NULL);
1284 if (udev_device->usec_initialized == 0)
1286 now_ts = now(CLOCK_MONOTONIC);
1289 return now_ts - udev_device->usec_initialized;
1292 usec_t udev_device_get_usec_initialized(struct udev_device *udev_device)
1294 return udev_device->usec_initialized;
1297 void udev_device_set_usec_initialized(struct udev_device *udev_device, usec_t usec_initialized)
1301 udev_device->usec_initialized = usec_initialized;
1302 snprintf(num, sizeof(num), "%llu", (unsigned long long)usec_initialized);
1303 udev_device_add_property(udev_device, "USEC_INITIALIZED", num);
1307 * udev_device_get_sysattr_value:
1308 * @udev_device: udev device
1309 * @sysattr: attribute name
1311 * The retrieved value is cached in the device. Repeated calls will return the same
1312 * value and not open the attribute again.
1314 * Returns: the content of a sys attribute file, or #NULL if there is no sys attribute value.
1316 _public_ const char *udev_device_get_sysattr_value(struct udev_device *udev_device, const char *sysattr)
1318 struct udev_list_entry *list_entry;
1319 char path[UTIL_PATH_SIZE];
1321 struct stat statbuf;
1324 const char *val = NULL;
1326 if (udev_device == NULL)
1328 if (sysattr == NULL)
1331 /* look for possibly already cached result */
1332 list_entry = udev_list_get_entry(&udev_device->sysattr_value_list);
1333 list_entry = udev_list_entry_get_by_name(list_entry, sysattr);
1334 if (list_entry != NULL)
1335 return udev_list_entry_get_value(list_entry);
1337 util_strscpyl(path, sizeof(path), udev_device_get_syspath(udev_device), "/", sysattr, NULL);
1338 if (lstat(path, &statbuf) != 0) {
1339 udev_list_entry_add(&udev_device->sysattr_value_list, sysattr, NULL);
1343 if (S_ISLNK(statbuf.st_mode)) {
1344 struct udev_device *dev;
1347 * Some core links return only the last element of the target path,
1348 * these are just values, the paths should not be exposed.
1350 if (streq(sysattr, "driver") ||
1351 streq(sysattr, "subsystem") ||
1352 streq(sysattr, "module")) {
1353 if (util_get_sys_core_link_value(udev_device->udev, sysattr,
1354 udev_device->syspath, value, sizeof(value)) < 0)
1356 list_entry = udev_list_entry_add(&udev_device->sysattr_value_list, sysattr, value);
1357 val = udev_list_entry_get_value(list_entry);
1361 /* resolve custom link to a device and return its syspath */
1362 if (!streq(sysattr, "device")) {
1363 util_strscpyl(path, sizeof(path), udev_device->syspath, "/", sysattr, NULL);
1364 dev = udev_device_new_from_syspath(udev_device->udev, path);
1366 list_entry = udev_list_entry_add(&udev_device->sysattr_value_list, sysattr,
1367 udev_device_get_syspath(dev));
1368 val = udev_list_entry_get_value(list_entry);
1369 udev_device_unref(dev);
1375 /* skip directories */
1376 if (S_ISDIR(statbuf.st_mode))
1379 /* skip non-readable files */
1380 if ((statbuf.st_mode & S_IRUSR) == 0)
1383 /* read attribute value */
1384 fd = open(path, O_RDONLY|O_CLOEXEC);
1387 size = read(fd, value, sizeof(value));
1391 if (size == sizeof(value))
1394 /* got a valid value, store it in cache and return it */
1396 util_remove_trailing_chars(value, '\n');
1397 list_entry = udev_list_entry_add(&udev_device->sysattr_value_list, sysattr, value);
1398 val = udev_list_entry_get_value(list_entry);
1403 static int udev_device_sysattr_list_read(struct udev_device *udev_device)
1405 struct dirent *dent;
1409 if (udev_device == NULL)
1411 if (udev_device->sysattr_list_read)
1414 dir = opendir(udev_device_get_syspath(udev_device));
1418 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
1419 char path[UTIL_PATH_SIZE];
1420 struct stat statbuf;
1422 /* only handle symlinks and regular files */
1423 if (dent->d_type != DT_LNK && dent->d_type != DT_REG)
1426 util_strscpyl(path, sizeof(path), udev_device_get_syspath(udev_device), "/", dent->d_name, NULL);
1427 if (lstat(path, &statbuf) != 0)
1429 if ((statbuf.st_mode & S_IRUSR) == 0)
1432 udev_list_entry_add(&udev_device->sysattr_list, dent->d_name, NULL);
1437 udev_device->sysattr_list_read = true;
1443 * udev_device_get_sysattr_list_entry:
1444 * @udev_device: udev device
1446 * Retrieve the list of available sysattrs, with value being empty;
1447 * This just return all available sysfs attributes for a particular
1448 * device without reading their values.
1450 * Returns: the first entry of the property list
1452 _public_ struct udev_list_entry *udev_device_get_sysattr_list_entry(struct udev_device *udev_device)
1454 if (!udev_device->sysattr_list_read) {
1456 ret = udev_device_sysattr_list_read(udev_device);
1461 return udev_list_get_entry(&udev_device->sysattr_list);
1464 int udev_device_set_syspath(struct udev_device *udev_device, const char *syspath)
1469 free(udev_device->syspath);
1470 udev_device->syspath = strdup(syspath);
1471 if (udev_device->syspath == NULL)
1473 udev_device->devpath = udev_device->syspath + strlen("/sys");
1474 udev_device_add_property(udev_device, "DEVPATH", udev_device->devpath);
1476 pos = strrchr(udev_device->syspath, '/');
1479 udev_device->sysname = strdup(&pos[1]);
1480 if (udev_device->sysname == NULL)
1483 /* some devices have '!' in their name, change that to '/' */
1485 while (udev_device->sysname[len] != '\0') {
1486 if (udev_device->sysname[len] == '!')
1487 udev_device->sysname[len] = '/';
1491 /* trailing number */
1492 while (len > 0 && isdigit(udev_device->sysname[--len]))
1493 udev_device->sysnum = &udev_device->sysname[len];
1495 /* sysname is completely numeric */
1497 udev_device->sysnum = NULL;
1502 int udev_device_set_devnode(struct udev_device *udev_device, const char *devnode)
1504 free(udev_device->devnode);
1505 if (devnode[0] != '/') {
1506 if (asprintf(&udev_device->devnode, "/dev/%s", devnode) < 0)
1507 udev_device->devnode = NULL;
1509 udev_device->devnode = strdup(devnode);
1511 if (udev_device->devnode == NULL)
1513 udev_device_add_property(udev_device, "DEVNAME", udev_device->devnode);
1517 int udev_device_add_devlink(struct udev_device *udev_device, const char *devlink)
1519 struct udev_list_entry *list_entry;
1521 udev_device->devlinks_uptodate = false;
1522 list_entry = udev_list_entry_add(&udev_device->devlinks_list, devlink, NULL);
1523 if (list_entry == NULL)
1528 const char *udev_device_get_id_filename(struct udev_device *udev_device)
1530 if (udev_device->id_filename == NULL) {
1531 if (udev_device_get_subsystem(udev_device) == NULL)
1534 if (major(udev_device_get_devnum(udev_device)) > 0) {
1535 /* use dev_t -- b259:131072, c254:0 */
1536 if (asprintf(&udev_device->id_filename, "%c%u:%u",
1537 streq(udev_device_get_subsystem(udev_device), "block") ? 'b' : 'c',
1538 major(udev_device_get_devnum(udev_device)),
1539 minor(udev_device_get_devnum(udev_device))) < 0)
1540 udev_device->id_filename = NULL;
1541 } else if (udev_device_get_ifindex(udev_device) > 0) {
1542 /* use netdev ifindex -- n3 */
1543 if (asprintf(&udev_device->id_filename, "n%u", udev_device_get_ifindex(udev_device)) < 0)
1544 udev_device->id_filename = NULL;
1547 * use $subsys:$syname -- pci:0000:00:1f.2
1548 * sysname() has '!' translated, get it from devpath
1550 const char *sysname;
1551 sysname = strrchr(udev_device->devpath, '/');
1552 if (sysname == NULL)
1554 sysname = &sysname[1];
1555 if (asprintf(&udev_device->id_filename, "+%s:%s", udev_device_get_subsystem(udev_device), sysname) < 0)
1556 udev_device->id_filename = NULL;
1559 return udev_device->id_filename;
1563 * udev_device_get_is_initialized:
1564 * @udev_device: udev device
1566 * Check if udev has already handled the device and has set up
1567 * device node permissions and context, or has renamed a network
1570 * This is only implemented for devices with a device node
1571 * or network interfaces. All other devices return 1 here.
1573 * Returns: 1 if the device is set up. 0 otherwise.
1575 _public_ int udev_device_get_is_initialized(struct udev_device *udev_device)
1577 if (!udev_device->info_loaded)
1578 udev_device_read_db(udev_device, NULL);
1579 return udev_device->is_initialized;
1582 void udev_device_set_is_initialized(struct udev_device *udev_device)
1584 udev_device->is_initialized = true;
1587 int udev_device_add_tag(struct udev_device *udev_device, const char *tag)
1589 if (strchr(tag, ':') != NULL || strchr(tag, ' ') != NULL)
1591 udev_device->tags_uptodate = false;
1592 if (udev_list_entry_add(&udev_device->tags_list, tag, NULL) != NULL)
1597 void udev_device_cleanup_tags_list(struct udev_device *udev_device)
1599 udev_device->tags_uptodate = false;
1600 udev_list_cleanup(&udev_device->tags_list);
1604 * udev_device_get_tags_list_entry:
1605 * @udev_device: udev device
1607 * Retrieve the list of tags attached to the udev device. The next
1608 * list entry can be retrieved with udev_list_entry_get_next(),
1609 * which returns #NULL if no more entries exist. The tag string
1610 * can be retrieved from the list entry by udev_list_entry_get_name().
1612 * Returns: the first entry of the tag list
1614 _public_ struct udev_list_entry *udev_device_get_tags_list_entry(struct udev_device *udev_device)
1616 if (udev_device == NULL)
1618 if (!udev_device->info_loaded)
1619 udev_device_read_db(udev_device, NULL);
1620 return udev_list_get_entry(&udev_device->tags_list);
1624 * udev_device_has_tag:
1625 * @udev_device: udev device
1628 * Check if a given device has a certain tag associated.
1630 * Returns: 1 if the tag is found. 0 otherwise.
1632 _public_ int udev_device_has_tag(struct udev_device *udev_device, const char *tag)
1634 struct udev_list_entry *list_entry;
1636 if (udev_device == NULL)
1638 if (!udev_device->info_loaded)
1639 udev_device_read_db(udev_device, NULL);
1640 list_entry = udev_device_get_tags_list_entry(udev_device);
1641 if (udev_list_entry_get_by_name(list_entry, tag) != NULL)
1646 #define ENVP_SIZE 128
1647 #define MONITOR_BUF_SIZE 4096
1648 static int update_envp_monitor_buf(struct udev_device *udev_device)
1650 struct udev_list_entry *list_entry;
1655 /* monitor buffer of property strings */
1656 free(udev_device->monitor_buf);
1657 udev_device->monitor_buf_len = 0;
1658 udev_device->monitor_buf = malloc(MONITOR_BUF_SIZE);
1659 if (udev_device->monitor_buf == NULL)
1662 /* envp array, strings will point into monitor buffer */
1663 if (udev_device->envp == NULL)
1664 udev_device->envp = malloc(sizeof(char *) * ENVP_SIZE);
1665 if (udev_device->envp == NULL)
1669 s = udev_device->monitor_buf;
1670 l = MONITOR_BUF_SIZE;
1671 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device)) {
1674 key = udev_list_entry_get_name(list_entry);
1675 /* skip private variables */
1679 /* add string to envp array */
1680 udev_device->envp[i++] = s;
1681 if (i+1 >= ENVP_SIZE)
1684 /* add property string to monitor buffer */
1685 l = util_strpcpyl(&s, l, key, "=", udev_list_entry_get_value(list_entry), NULL);
1688 /* advance past the trailing '\0' that util_strpcpyl() guarantees */
1692 udev_device->envp[i] = NULL;
1693 udev_device->monitor_buf_len = s - udev_device->monitor_buf;
1694 udev_device->envp_uptodate = true;
1698 char **udev_device_get_properties_envp(struct udev_device *udev_device)
1700 if (!udev_device->envp_uptodate)
1701 if (update_envp_monitor_buf(udev_device) != 0)
1703 return udev_device->envp;
1706 ssize_t udev_device_get_properties_monitor_buf(struct udev_device *udev_device, const char **buf)
1708 if (!udev_device->envp_uptodate)
1709 if (update_envp_monitor_buf(udev_device) != 0)
1711 *buf = udev_device->monitor_buf;
1712 return udev_device->monitor_buf_len;
1715 int udev_device_set_action(struct udev_device *udev_device, const char *action)
1717 free(udev_device->action);
1718 udev_device->action = strdup(action);
1719 if (udev_device->action == NULL)
1721 udev_device_add_property(udev_device, "ACTION", udev_device->action);
1725 int udev_device_get_devlink_priority(struct udev_device *udev_device)
1727 if (!udev_device->info_loaded)
1728 udev_device_read_db(udev_device, NULL);
1729 return udev_device->devlink_priority;
1732 int udev_device_set_devlink_priority(struct udev_device *udev_device, int prio)
1734 udev_device->devlink_priority = prio;
1738 int udev_device_get_watch_handle(struct udev_device *udev_device)
1740 if (!udev_device->info_loaded)
1741 udev_device_read_db(udev_device, NULL);
1742 return udev_device->watch_handle;
1745 int udev_device_set_watch_handle(struct udev_device *udev_device, int handle)
1747 udev_device->watch_handle = handle;
1751 bool udev_device_get_db_persist(struct udev_device *udev_device)
1753 return udev_device->db_persist;
1756 void udev_device_set_db_persist(struct udev_device *udev_device)
1758 udev_device->db_persist = true;