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;
65 size_t monitor_buf_len;
66 struct udev_list_node devlinks_list;
67 struct udev_list_node properties_list;
68 struct udev_list_node sysattr_value_list;
69 struct udev_list_node sysattr_list;
70 struct udev_list_node tags_list;
71 unsigned long long int seqnum;
72 unsigned long long int usec_initialized;
83 bool devlinks_uptodate;
91 bool sysattr_list_read;
95 struct udev_list_entry *udev_device_add_property(struct udev_device *udev_device, const char *key, const char *value)
97 udev_device->envp_uptodate = false;
99 struct udev_list_entry *list_entry;
101 list_entry = udev_device_get_properties_list_entry(udev_device);
102 list_entry = udev_list_entry_get_by_name(list_entry, key);
103 if (list_entry != NULL)
104 udev_list_entry_delete(list_entry);
107 return udev_list_entry_add(udev_device->udev, &udev_device->properties_list, key, value, UDEV_LIST_UNIQUE);
110 static struct udev_list_entry *udev_device_add_property_from_string(struct udev_device *udev_device, const char *property)
112 char name[UTIL_LINE_SIZE];
115 util_strscpy(name, sizeof(name), property);
116 val = strchr(name, '=');
123 return udev_device_add_property(udev_device, name, val);
127 * parse property string, and if needed, update internal values accordingly
129 * udev_device_add_property_from_string_parse_finish() needs to be
130 * called after adding properties, and its return value checked
132 * udev_device_set_info_loaded() needs to be set, to avoid trying
133 * to use a device without a DEVPATH set
135 void udev_device_add_property_from_string_parse(struct udev_device *udev_device, const char *property)
137 if (strncmp(property, "DEVPATH=", 8) == 0) {
138 char path[UTIL_PATH_SIZE];
140 util_strscpyl(path, sizeof(path), udev_get_sys_path(udev_device->udev), &property[8], NULL);
141 udev_device_set_syspath(udev_device, path);
142 } else if (strncmp(property, "SUBSYSTEM=", 10) == 0) {
143 udev_device_set_subsystem(udev_device, &property[10]);
144 } else if (strncmp(property, "DEVTYPE=", 8) == 0) {
145 udev_device_set_devtype(udev_device, &property[8]);
146 } else if (strncmp(property, "DEVNAME=", 8) == 0) {
147 if (property[8] == '/')
148 udev_device_set_devnode(udev_device, &property[8]);
150 udev_device_set_knodename(udev_device, &property[8]);
151 } else if (strncmp(property, "DEVLINKS=", 9) == 0) {
152 char devlinks[UTIL_PATH_SIZE];
156 util_strscpy(devlinks, sizeof(devlinks), &property[9]);
158 next = strchr(slink, ' ');
159 while (next != NULL) {
161 udev_device_add_devlink(udev_device, slink, 0);
163 next = strchr(slink, ' ');
165 if (slink[0] != '\0')
166 udev_device_add_devlink(udev_device, slink, 0);
167 } else if (strncmp(property, "TAGS=", 5) == 0) {
168 char tags[UTIL_PATH_SIZE];
171 util_strscpy(tags, sizeof(tags), &property[5]);
172 next = strchr(tags, ':');
175 while (next[0] != '\0') {
179 next = strchr(tag, ':');
184 udev_device_add_tag(udev_device, tag);
187 } else if (strncmp(property, "DRIVER=", 7) == 0) {
188 udev_device_set_driver(udev_device, &property[7]);
189 } else if (strncmp(property, "ACTION=", 7) == 0) {
190 udev_device_set_action(udev_device, &property[7]);
191 } else if (strncmp(property, "MAJOR=", 6) == 0) {
192 udev_device->maj = strtoull(&property[6], NULL, 10);
193 } else if (strncmp(property, "MINOR=", 6) == 0) {
194 udev_device->min = strtoull(&property[6], NULL, 10);
195 } else if (strncmp(property, "DEVPATH_OLD=", 12) == 0) {
196 udev_device_set_devpath_old(udev_device, &property[12]);
197 } else if (strncmp(property, "SEQNUM=", 7) == 0) {
198 udev_device_set_seqnum(udev_device, strtoull(&property[7], NULL, 10));
199 } else if (strncmp(property, "TIMEOUT=", 8) == 0) {
200 udev_device_set_timeout(udev_device, strtoull(&property[8], NULL, 10));
201 } else if (strncmp(property, "IFINDEX=", 8) == 0) {
202 udev_device_set_ifindex(udev_device, strtoull(&property[8], NULL, 10));
203 } else if (strncmp(property, "DEVMODE=", 8) == 0) {
204 udev_device_set_devnode_mode(udev_device, strtoul(&property[8], NULL, 8));
206 udev_device_add_property_from_string(udev_device, property);
210 int udev_device_add_property_from_string_parse_finish(struct udev_device *udev_device)
212 if (udev_device->maj > 0)
213 udev_device_set_devnum(udev_device, makedev(udev_device->maj, udev_device->min));
214 udev_device->maj = 0;
215 udev_device->min = 0;
217 if (udev_device->devpath == NULL || udev_device->subsystem == NULL)
223 * udev_device_get_property_value:
224 * @udev_device: udev device
225 * @key: property name
227 * Returns: the value of a device property, or #NULL if there is no such property.
229 const char *udev_device_get_property_value(struct udev_device *udev_device, const char *key)
231 struct udev_list_entry *list_entry;
233 if (udev_device == NULL)
238 list_entry = udev_device_get_properties_list_entry(udev_device);
239 list_entry = udev_list_entry_get_by_name(list_entry, key);
240 return udev_list_entry_get_value(list_entry);
243 int udev_device_read_db(struct udev_device *udev_device, const char *dbfile)
245 char filename[UTIL_PATH_SIZE];
246 char line[UTIL_LINE_SIZE];
249 /* providing a database file will always force-load it */
250 if (dbfile == NULL) {
253 if (udev_device->db_loaded)
255 udev_device->db_loaded = true;
257 id = udev_device_get_id_filename(udev_device);
260 util_strscpyl(filename, sizeof(filename), udev_get_run_path(udev_device->udev), "/data/", id, NULL);
264 f = fopen(dbfile, "re");
266 info(udev_device->udev, "no db file to read %s: %m\n", dbfile);
269 udev_device->is_initialized = true;
271 while (fgets(line, sizeof(line), f)) {
274 struct udev_list_entry *entry;
283 util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev_device->udev), "/", val, NULL);
284 udev_device_set_devnode(udev_device, filename);
287 util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev_device->udev), "/", val, NULL);
288 udev_device_add_devlink(udev_device, filename, 0);
291 udev_device_set_devlink_priority(udev_device, atoi(val));
294 entry = udev_device_add_property_from_string(udev_device, val);
295 udev_list_entry_set_num(entry, true);
298 udev_device_add_tag(udev_device, val);
301 udev_device_set_watch_handle(udev_device, atoi(val));
304 udev_device_set_usec_initialized(udev_device, strtoull(val, NULL, 10));
310 info(udev_device->udev, "device %p filled with db file data\n", udev_device);
314 int udev_device_read_uevent_file(struct udev_device *udev_device)
316 char filename[UTIL_PATH_SIZE];
318 char line[UTIL_LINE_SIZE];
322 if (udev_device->uevent_loaded)
325 util_strscpyl(filename, sizeof(filename), udev_device->syspath, "/uevent", NULL);
326 f = fopen(filename, "re");
329 udev_device->uevent_loaded = true;
331 while (fgets(line, sizeof(line), f)) {
334 pos = strchr(line, '\n');
339 if (strncmp(line, "DEVTYPE=", 8) == 0)
340 udev_device_set_devtype(udev_device, &line[8]);
341 else if (strncmp(line, "MAJOR=", 6) == 0)
342 maj = strtoull(&line[6], NULL, 10);
343 else if (strncmp(line, "MINOR=", 6) == 0)
344 min = strtoull(&line[6], NULL, 10);
345 else if (strncmp(line, "IFINDEX=", 8) == 0)
346 udev_device_set_ifindex(udev_device, strtoull(&line[8], NULL, 10));
347 else if (strncmp(line, "DEVNAME=", 8) == 0)
348 udev_device_set_knodename(udev_device, &line[8]);
349 else if (strncmp(line, "DEVMODE=", 8) == 0)
350 udev_device->devnode_mode = strtoul(&line[8], NULL, 8);
352 udev_device_add_property_from_string(udev_device, line);
355 udev_device->devnum = makedev(maj, min);
360 void udev_device_set_info_loaded(struct udev_device *device)
362 device->info_loaded = true;
365 struct udev_device *udev_device_new(struct udev *udev)
367 struct udev_device *udev_device;
368 struct udev_list_entry *list_entry;
373 udev_device = calloc(1, sizeof(struct udev_device));
374 if (udev_device == NULL)
376 udev_device->refcount = 1;
377 udev_device->udev = udev;
378 udev_list_init(&udev_device->devlinks_list);
379 udev_list_init(&udev_device->properties_list);
380 udev_list_init(&udev_device->sysattr_value_list);
381 udev_list_init(&udev_device->sysattr_list);
382 udev_list_init(&udev_device->tags_list);
383 udev_device->timeout = -1;
384 udev_device->watch_handle = -1;
385 /* copy global properties */
386 udev_list_entry_foreach(list_entry, udev_get_properties_list_entry(udev))
387 udev_device_add_property(udev_device,
388 udev_list_entry_get_name(list_entry),
389 udev_list_entry_get_value(list_entry));
390 dbg(udev_device->udev, "udev_device: %p created\n", udev_device);
395 * udev_device_new_from_syspath:
396 * @udev: udev library context
397 * @syspath: sys device path including sys directory
399 * Create new udev device, and fill in information from the sys
400 * device and the udev database entry. The syspath is the absolute
401 * path to the device, including the sys mount point.
403 * The initial refcount is 1, and needs to be decremented to
404 * release the resources of the udev device.
406 * Returns: a new udev device, or #NULL, if it does not exist
408 struct udev_device *udev_device_new_from_syspath(struct udev *udev, const char *syspath)
412 char path[UTIL_PATH_SIZE];
415 struct udev_device *udev_device;
422 /* path starts in sys */
423 len = strlen(udev_get_sys_path(udev));
424 if (strncmp(syspath, udev_get_sys_path(udev), len) != 0) {
425 info(udev, "not in sys :%s\n", syspath);
429 /* path is not a root directory */
430 subdir = &syspath[len+1];
431 pos = strrchr(subdir, '/');
432 if (pos == NULL || pos[1] == '\0' || pos < &subdir[2]) {
433 dbg(udev, "not a subdir :%s\n", syspath);
437 /* resolve possible symlink to real path */
438 util_strscpy(path, sizeof(path), syspath);
439 util_resolve_sys_link(udev, path, sizeof(path));
441 if (strncmp(&path[len], "/devices/", 9) == 0) {
442 char file[UTIL_PATH_SIZE];
444 /* all "devices" require a "uevent" file */
445 util_strscpyl(file, sizeof(file), path, "/uevent", NULL);
446 if (stat(file, &statbuf) != 0) {
447 dbg(udev, "not a device: %s\n", syspath);
451 /* everything else just needs to be a directory */
452 if (stat(path, &statbuf) != 0 || !S_ISDIR(statbuf.st_mode)) {
453 dbg(udev, "directory not found: %s\n", syspath);
458 udev_device = udev_device_new(udev);
459 if (udev_device == NULL)
462 udev_device_set_syspath(udev_device, path);
463 info(udev, "device %p has devpath '%s'\n", udev_device, udev_device_get_devpath(udev_device));
469 * udev_device_new_from_devnum:
470 * @udev: udev library context
471 * @type: char or block device
472 * @devnum: device major/minor number
474 * Create new udev device, and fill in information from the sys
475 * device and the udev database entry. The device is looked-up
476 * by its major/minor number and type. Character and block device
477 * numbers are not unique across the two types.
479 * The initial refcount is 1, and needs to be decremented to
480 * release the resources of the udev device.
482 * Returns: a new udev device, or #NULL, if it does not exist
484 struct udev_device *udev_device_new_from_devnum(struct udev *udev, char type, dev_t devnum)
486 char path[UTIL_PATH_SIZE];
487 const char *type_str;
491 else if (type == 'c')
496 /* use /sys/dev/{block,char}/<maj>:<min> link */
497 snprintf(path, sizeof(path), "%s/dev/%s/%u:%u",
498 udev_get_sys_path(udev), type_str, major(devnum), minor(devnum));
499 return udev_device_new_from_syspath(udev, path);
502 struct udev_device *udev_device_new_from_id_filename(struct udev *udev, char *id)
506 char subsys[UTIL_PATH_SIZE];
512 if (sscanf(id, "%c%i:%i", &type, &maj, &min) != 3)
514 return udev_device_new_from_devnum(udev, type, makedev(maj, min));
518 struct udev_device *dev;
521 ifindex = strtoul(&id[1], NULL, 10);
525 sk = socket(PF_INET, SOCK_DGRAM, 0);
528 memset(&ifr, 0x00, sizeof(struct ifreq));
529 ifr.ifr_ifindex = ifindex;
530 if (ioctl(sk, SIOCGIFNAME, &ifr) != 0) {
536 dev = udev_device_new_from_subsystem_sysname(udev, "net", ifr.ifr_name);
539 if (udev_device_get_ifindex(dev) == ifindex)
541 udev_device_unref(dev);
545 util_strscpy(subsys, sizeof(subsys), &id[1]);
546 sysname = strchr(subsys, ':');
550 sysname = &sysname[1];
551 return udev_device_new_from_subsystem_sysname(udev, subsys, sysname);
558 * udev_device_new_from_subsystem_sysname:
559 * @udev: udev library context
560 * @subsystem: the subsystem of the device
561 * @sysname: the name of the device
563 * Create new udev device, and fill in information from the sys device
564 * and the udev database entry. The device is looked up by the subsystem
565 * and name string of the device, like "mem" / "zero", or "block" / "sda".
567 * The initial refcount is 1, and needs to be decremented to
568 * release the resources of the udev device.
570 * Returns: a new udev device, or #NULL, if it does not exist
572 struct udev_device *udev_device_new_from_subsystem_sysname(struct udev *udev, const char *subsystem, const char *sysname)
574 char path_full[UTIL_PATH_SIZE];
580 l = util_strpcpyl(&path, sizeof(path_full), udev_get_sys_path(udev), NULL);
582 if (strcmp(subsystem, "subsystem") == 0) {
583 util_strscpyl(path, l, "/subsystem/", sysname, NULL);
584 if (stat(path_full, &statbuf) == 0)
587 util_strscpyl(path, l, "/bus/", sysname, NULL);
588 if (stat(path_full, &statbuf) == 0)
591 util_strscpyl(path, l, "/class/", sysname, NULL);
592 if (stat(path_full, &statbuf) == 0)
597 if (strcmp(subsystem, "module") == 0) {
598 util_strscpyl(path, l, "/module/", sysname, NULL);
599 if (stat(path_full, &statbuf) == 0)
604 if (strcmp(subsystem, "drivers") == 0) {
605 char subsys[UTIL_NAME_SIZE];
608 util_strscpy(subsys, sizeof(subsys), sysname);
609 driver = strchr(subsys, ':');
610 if (driver != NULL) {
614 util_strscpyl(path, l, "/subsystem/", subsys, "/drivers/", driver, NULL);
615 if (stat(path_full, &statbuf) == 0)
618 util_strscpyl(path, l, "/bus/", subsys, "/drivers/", driver, NULL);
619 if (stat(path_full, &statbuf) == 0)
625 util_strscpyl(path, l, "/subsystem/", subsystem, "/devices/", sysname, NULL);
626 if (stat(path_full, &statbuf) == 0)
629 util_strscpyl(path, l, "/bus/", subsystem, "/devices/", sysname, NULL);
630 if (stat(path_full, &statbuf) == 0)
633 util_strscpyl(path, l, "/class/", subsystem, "/", sysname, NULL);
634 if (stat(path_full, &statbuf) == 0)
639 return udev_device_new_from_syspath(udev, path_full);
643 * udev_device_new_from_environment
644 * @udev: udev library context
646 * Create new udev device, and fill in information from the
647 * current process environment. This only works reliable if
648 * the process is called from a udev rule. It is usually used
649 * for tools executed from IMPORT= rules.
651 * The initial refcount is 1, and needs to be decremented to
652 * release the resources of the udev device.
654 * Returns: a new udev device, or #NULL, if it does not exist
656 struct udev_device *udev_device_new_from_environment(struct udev *udev)
659 struct udev_device *udev_device;
661 udev_device = udev_device_new(udev);
662 if (udev_device == NULL)
664 udev_device_set_info_loaded(udev_device);
666 for (i = 0; environ[i] != NULL; i++)
667 udev_device_add_property_from_string_parse(udev_device, environ[i]);
669 if (udev_device_add_property_from_string_parse_finish(udev_device) < 0) {
670 info(udev, "missing values, invalid device\n");
671 udev_device_unref(udev_device);
678 static struct udev_device *device_new_from_parent(struct udev_device *udev_device)
680 struct udev_device *udev_device_parent = NULL;
681 char path[UTIL_PATH_SIZE];
684 util_strscpy(path, sizeof(path), udev_device->syspath);
685 subdir = &path[strlen(udev_get_sys_path(udev_device->udev))+1];
689 pos = strrchr(subdir, '/');
690 if (pos == NULL || pos < &subdir[2])
693 udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
694 if (udev_device_parent != NULL)
695 return udev_device_parent;
701 * udev_device_get_parent:
702 * @udev_device: the device to start searching from
704 * Find the next parent device, and fill in information from the sys
705 * device and the udev database entry.
707 * The returned the device is not referenced. It is attached to the
708 * child device, and will be cleaned up when the child device
711 * It is not necessarily just the upper level directory, empty or not
712 * recognized sys directories are ignored.
714 * It can be called as many times as needed, without caring about
717 * Returns: a new udev device, or #NULL, if it no parent exist.
719 struct udev_device *udev_device_get_parent(struct udev_device *udev_device)
721 if (udev_device == NULL)
723 if (!udev_device->parent_set) {
724 udev_device->parent_set = true;
725 udev_device->parent_device = device_new_from_parent(udev_device);
727 if (udev_device->parent_device != NULL)
728 dbg(udev_device->udev, "returning existing parent %p\n", udev_device->parent_device);
729 return udev_device->parent_device;
733 * udev_device_get_parent_with_subsystem_devtype:
734 * @udev_device: udev device to start searching from
735 * @subsystem: the subsystem of the device
736 * @devtype: the type (DEVTYPE) of the device
738 * Find the next parent device, with a matching subsystem and devtype
739 * value, and fill in information from the sys device and the udev
742 * If devtype is #NULL, only subsystem is checked, and any devtype will
745 * The returned the device is not referenced. It is attached to the
746 * child device, and will be cleaned up when the child device
749 * It can be called as many times as needed, without caring about
752 * Returns: a new udev device, or #NULL if no matching parent exists.
754 struct udev_device *udev_device_get_parent_with_subsystem_devtype(struct udev_device *udev_device, const char *subsystem, const char *devtype)
756 struct udev_device *parent;
758 if (subsystem == NULL)
761 parent = udev_device_get_parent(udev_device);
762 while (parent != NULL) {
763 const char *parent_subsystem;
764 const char *parent_devtype;
766 parent_subsystem = udev_device_get_subsystem(parent);
767 if (parent_subsystem != NULL && strcmp(parent_subsystem, subsystem) == 0) {
770 parent_devtype = udev_device_get_devtype(parent);
771 if (parent_devtype != NULL && strcmp(parent_devtype, devtype) == 0)
774 parent = udev_device_get_parent(parent);
780 * udev_device_get_udev:
781 * @udev_device: udev device
783 * Retrieve the udev library context the device was created with.
785 * Returns: the udev library context
787 struct udev *udev_device_get_udev(struct udev_device *udev_device)
789 if (udev_device == NULL)
791 return udev_device->udev;
796 * @udev_device: udev device
798 * Take a reference of a udev device.
800 * Returns: the passed udev device
802 struct udev_device *udev_device_ref(struct udev_device *udev_device)
804 if (udev_device == NULL)
806 udev_device->refcount++;
812 * @udev_device: udev device
814 * Drop a reference of a udev device. If the refcount reaches zero,
815 * the resources of the device will be released.
818 void udev_device_unref(struct udev_device *udev_device)
820 if (udev_device == NULL)
822 udev_device->refcount--;
823 if (udev_device->refcount > 0)
825 if (udev_device->parent_device != NULL)
826 udev_device_unref(udev_device->parent_device);
827 free(udev_device->syspath);
828 free(udev_device->sysname);
829 free(udev_device->devnode);
830 free(udev_device->subsystem);
831 free(udev_device->devtype);
832 udev_list_cleanup_entries(udev_device->udev, &udev_device->devlinks_list);
833 udev_list_cleanup_entries(udev_device->udev, &udev_device->properties_list);
834 udev_list_cleanup_entries(udev_device->udev, &udev_device->sysattr_value_list);
835 udev_list_cleanup_entries(udev_device->udev, &udev_device->sysattr_list);
836 udev_list_cleanup_entries(udev_device->udev, &udev_device->tags_list);
837 free(udev_device->action);
838 free(udev_device->driver);
839 free(udev_device->devpath_old);
840 free(udev_device->sysname_old);
841 free(udev_device->knodename);
842 free(udev_device->id_filename);
843 free(udev_device->envp);
844 free(udev_device->monitor_buf);
845 dbg(udev_device->udev, "udev_device: %p released\n", udev_device);
850 * udev_device_get_devpath:
851 * @udev_device: udev device
853 * Retrieve the kernel devpath value of the udev device. The path
854 * does not contain the sys mount point, and starts with a '/'.
856 * Returns: the devpath of the udev device
858 const char *udev_device_get_devpath(struct udev_device *udev_device)
860 if (udev_device == NULL)
862 return udev_device->devpath;
866 * udev_device_get_syspath:
867 * @udev_device: udev device
869 * Retrieve the sys path of the udev device. The path is an
870 * absolute path and starts with the sys mount point.
872 * Returns: the sys path of the udev device
874 const char *udev_device_get_syspath(struct udev_device *udev_device)
876 if (udev_device == NULL)
878 return udev_device->syspath;
882 * udev_device_get_sysname:
883 * @udev_device: udev device
885 * Returns: the sys name of the device device
887 const char *udev_device_get_sysname(struct udev_device *udev_device)
889 if (udev_device == NULL)
891 return udev_device->sysname;
895 * udev_device_get_sysnum:
896 * @udev_device: udev device
898 * Returns: the trailing number of of the device name
900 const char *udev_device_get_sysnum(struct udev_device *udev_device)
902 if (udev_device == NULL)
904 return udev_device->sysnum;
908 * udev_device_get_devnode:
909 * @udev_device: udev device
911 * Retrieve the device node file name belonging to the udev device.
912 * The path is an absolute path, and starts with the device directory.
914 * Returns: the device node file name of the udev device, or #NULL if no device node exists
916 const char *udev_device_get_devnode(struct udev_device *udev_device)
918 if (udev_device == NULL)
920 if (!udev_device->info_loaded) {
921 udev_device_read_uevent_file(udev_device);
922 udev_device_read_db(udev_device, NULL);
925 /* we might get called before we handled an event and have a db, use the kernel-provided name */
926 if (udev_device->devnode == NULL && udev_device_get_knodename(udev_device) != NULL) {
927 char filename[UTIL_NAME_SIZE];
929 util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev_device->udev), "/",
930 udev_device_get_knodename(udev_device), NULL);
931 udev_device_set_devnode(udev_device, filename);
932 return udev_device->devnode;
935 return udev_device->devnode;
938 mode_t udev_device_get_devnode_mode(struct udev_device *udev_device)
940 if (!udev_device->info_loaded)
941 udev_device_read_uevent_file(udev_device);
942 return udev_device->devnode_mode;
946 * udev_device_get_subsystem:
947 * @udev_device: udev device
949 * Retrieve the subsystem string of the udev device. The string does not
952 * Returns: the subsystem name of the udev device, or #NULL if it can not be determined
954 const char *udev_device_get_subsystem(struct udev_device *udev_device)
956 char subsystem[UTIL_NAME_SIZE];
958 if (udev_device == NULL)
960 if (!udev_device->subsystem_set) {
961 udev_device->subsystem_set = true;
962 /* read "subsystem" link */
963 if (util_get_sys_subsystem(udev_device->udev, udev_device->syspath, subsystem, sizeof(subsystem)) > 0) {
964 udev_device_set_subsystem(udev_device, subsystem);
965 return udev_device->subsystem;
968 if (strncmp(udev_device->devpath, "/module/", 8) == 0) {
969 udev_device_set_subsystem(udev_device, "module");
970 return udev_device->subsystem;
972 if (strstr(udev_device->devpath, "/drivers/") != NULL) {
973 udev_device_set_subsystem(udev_device, "drivers");
974 return udev_device->subsystem;
976 if (strncmp(udev_device->devpath, "/subsystem/", 11) == 0 ||
977 strncmp(udev_device->devpath, "/class/", 7) == 0 ||
978 strncmp(udev_device->devpath, "/bus/", 5) == 0) {
979 udev_device_set_subsystem(udev_device, "subsystem");
980 return udev_device->subsystem;
983 return udev_device->subsystem;
987 * udev_device_get_devtype:
988 * @udev_device: udev device
990 * Retrieve the devtype string of the udev device.
992 * Returns: the devtype name of the udev device, or #NULL if it can not be determined
994 const char *udev_device_get_devtype(struct udev_device *udev_device)
996 if (udev_device == NULL)
998 if (!udev_device->devtype_set) {
999 udev_device->devtype_set = true;
1000 udev_device_read_uevent_file(udev_device);
1002 return udev_device->devtype;
1006 * udev_device_get_devlinks_list_entry:
1007 * @udev_device: udev device
1009 * Retrieve the list of device links pointing to the device file of
1010 * the udev device. The next list entry can be retrieved with
1011 * udev_list_entry_next(), which returns #NULL if no more entries exist.
1012 * The devlink path can be retrieved from the list entry by
1013 * udev_list_entry_get_name(). The path is an absolute path, and starts with
1014 * the device directory.
1016 * Returns: the first entry of the device node link list
1018 struct udev_list_entry *udev_device_get_devlinks_list_entry(struct udev_device *udev_device)
1020 if (udev_device == NULL)
1022 if (!udev_device->info_loaded)
1023 udev_device_read_db(udev_device, NULL);
1024 return udev_list_get_entry(&udev_device->devlinks_list);
1027 void udev_device_cleanup_devlinks_list(struct udev_device *udev_device)
1029 udev_device->devlinks_uptodate = false;
1030 udev_list_cleanup_entries(udev_device->udev, &udev_device->devlinks_list);
1034 * udev_device_get_properties_list_entry:
1035 * @udev_device: udev device
1037 * Retrieve the list of key/value device properties of the udev
1038 * device. The next list entry can be retrieved with udev_list_entry_next(),
1039 * which returns #NULL if no more entries exist. The property name
1040 * can be retrieved from the list entry by udev_list_get_name(),
1041 * the property value by udev_list_get_value().
1043 * Returns: the first entry of the property list
1045 struct udev_list_entry *udev_device_get_properties_list_entry(struct udev_device *udev_device)
1047 if (udev_device == NULL)
1049 if (!udev_device->info_loaded) {
1050 udev_device_read_uevent_file(udev_device);
1051 udev_device_read_db(udev_device, NULL);
1053 if (!udev_device->devlinks_uptodate) {
1054 char symlinks[UTIL_PATH_SIZE];
1055 struct udev_list_entry *list_entry;
1057 udev_device->devlinks_uptodate = true;
1058 list_entry = udev_device_get_devlinks_list_entry(udev_device);
1059 if (list_entry != NULL) {
1064 l = util_strpcpyl(&s, sizeof(symlinks), udev_list_entry_get_name(list_entry), NULL);
1065 udev_list_entry_foreach(list_entry, udev_list_entry_get_next(list_entry))
1066 l = util_strpcpyl(&s, l, " ", udev_list_entry_get_name(list_entry), NULL);
1067 udev_device_add_property(udev_device, "DEVLINKS", symlinks);
1070 if (!udev_device->tags_uptodate) {
1071 udev_device->tags_uptodate = true;
1072 if (udev_device_get_tags_list_entry(udev_device) != NULL) {
1073 char tags[UTIL_PATH_SIZE];
1074 struct udev_list_entry *list_entry;
1079 l = util_strpcpyl(&s, sizeof(tags), ":", NULL);
1080 udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(udev_device))
1081 l = util_strpcpyl(&s, l, udev_list_entry_get_name(list_entry), ":", NULL);
1082 udev_device_add_property(udev_device, "TAGS", tags);
1085 return udev_list_get_entry(&udev_device->properties_list);
1089 * udev_device_get_driver:
1090 * @udev_device: udev device
1092 * Returns: the driver string, or #NULL if there is no driver attached.
1094 const char *udev_device_get_driver(struct udev_device *udev_device)
1096 char driver[UTIL_NAME_SIZE];
1098 if (udev_device == NULL)
1100 if (!udev_device->driver_set) {
1101 udev_device->driver_set = true;
1102 if (util_get_sys_driver(udev_device->udev, udev_device->syspath, driver, sizeof(driver)) > 0)
1103 udev_device->driver = strdup(driver);
1105 return udev_device->driver;
1109 * udev_device_get_devnum:
1110 * @udev_device: udev device
1112 * Returns: the device major/minor number.
1114 dev_t udev_device_get_devnum(struct udev_device *udev_device)
1116 if (udev_device == NULL)
1117 return makedev(0, 0);
1118 if (!udev_device->info_loaded)
1119 udev_device_read_uevent_file(udev_device);
1120 return udev_device->devnum;
1124 * udev_device_get_action:
1125 * @udev_device: udev device
1127 * This is only valid if the device was received through a monitor. Devices read from
1128 * sys do not have an action string. Usual actions are: add, remove, change, online,
1131 * Returns: the kernel action value, or #NULL if there is no action value available.
1133 const char *udev_device_get_action(struct udev_device *udev_device)
1135 if (udev_device == NULL)
1137 return udev_device->action;
1141 * udev_device_get_devnum:
1142 * @udev_device: udev device
1144 * This is only valid if the device was received through a monitor. Devices read from
1145 * sys do not have a sequence number.
1147 * Returns: the kernel event sequence number, or 0 if there is no sequence number available.
1149 unsigned long long int udev_device_get_seqnum(struct udev_device *udev_device)
1151 if (udev_device == NULL)
1153 return udev_device->seqnum;
1157 * udev_device_get_usec_since_initialized:
1158 * @udev_device: udev device
1160 * Return the number of microseconds passed since udev set up the
1161 * device for the first time.
1163 * This is only implemented for devices with need to store properties
1164 * in the udev database. All other devices return 0 here.
1166 * Returns: the number of microseconds since the device was first seen.
1168 unsigned long long int udev_device_get_usec_since_initialized(struct udev_device *udev_device)
1170 unsigned long long now;
1172 if (udev_device == NULL)
1174 if (!udev_device->info_loaded)
1175 udev_device_read_db(udev_device, NULL);
1176 if (udev_device->usec_initialized == 0)
1181 return now - udev_device->usec_initialized;
1184 unsigned long long udev_device_get_usec_initialized(struct udev_device *udev_device)
1186 return udev_device->usec_initialized;
1189 void udev_device_set_usec_initialized(struct udev_device *udev_device, unsigned long long usec_initialized)
1191 udev_device->usec_initialized = usec_initialized;
1195 * udev_device_get_sysattr_value:
1196 * @udev_device: udev device
1197 * @sysattr: attribute name
1199 * The retrieved value is cached in the device. Repeated calls will return the same
1200 * value and not open the attribute again.
1202 * Returns: the content of a sys attribute file, or #NULL if there is no sys attribute value.
1204 const char *udev_device_get_sysattr_value(struct udev_device *udev_device, const char *sysattr)
1206 struct udev_list_entry *list_entry;
1207 char path[UTIL_PATH_SIZE];
1209 struct stat statbuf;
1212 const char *val = NULL;
1214 if (udev_device == NULL)
1216 if (sysattr == NULL)
1219 /* look for possibly already cached result */
1220 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_device->sysattr_value_list)) {
1221 if (strcmp(udev_list_entry_get_name(list_entry), sysattr) == 0) {
1222 dbg(udev_device->udev, "got '%s' (%s) from cache\n",
1223 sysattr, udev_list_entry_get_value(list_entry));
1224 return udev_list_entry_get_value(list_entry);
1228 util_strscpyl(path, sizeof(path), udev_device_get_syspath(udev_device), "/", sysattr, NULL);
1229 if (lstat(path, &statbuf) != 0) {
1230 dbg(udev_device->udev, "no attribute '%s', keep negative entry\n", path);
1231 udev_list_entry_add(udev_device->udev, &udev_device->sysattr_value_list, sysattr, NULL, 0);
1235 if (S_ISLNK(statbuf.st_mode)) {
1236 char target[UTIL_NAME_SIZE];
1240 /* some core links return the last element of the target path */
1241 if (strcmp(sysattr, "driver") != 0 &&
1242 strcmp(sysattr, "subsystem") != 0 &&
1243 strcmp(sysattr, "module") != 0)
1246 len = readlink(path, target, sizeof(target));
1247 if (len <= 0 || len == sizeof(target))
1251 pos = strrchr(target, '/');
1254 dbg(udev_device->udev, "cache '%s' with link value '%s'\n", sysattr, pos);
1255 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->sysattr_value_list, sysattr, pos, 0);
1256 val = udev_list_entry_get_value(list_entry);
1262 /* skip directories */
1263 if (S_ISDIR(statbuf.st_mode))
1266 /* skip non-readable files */
1267 if ((statbuf.st_mode & S_IRUSR) == 0)
1270 /* read attribute value */
1271 fd = open(path, O_RDONLY|O_CLOEXEC);
1273 dbg(udev_device->udev, "attribute '%s' can not be opened\n", path);
1276 size = read(fd, value, sizeof(value));
1280 if (size == sizeof(value))
1283 /* got a valid value, store it in cache and return it */
1285 util_remove_trailing_chars(value, '\n');
1286 dbg(udev_device->udev, "'%s' has attribute value '%s'\n", path, value);
1287 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->sysattr_value_list, sysattr, value, 0);
1288 val = udev_list_entry_get_value(list_entry);
1293 static int udev_device_sysattr_list_read(struct udev_device *udev_device)
1295 struct dirent *dent;
1299 if (udev_device == NULL)
1301 if (udev_device->sysattr_list_read)
1304 dir = opendir(udev_device_get_syspath(udev_device));
1306 dbg(udev_device->udev, "sysfs dir '%s' can not be opened\n",
1307 udev_device_get_syspath(udev_device));
1311 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
1312 char path[UTIL_PATH_SIZE];
1313 struct stat statbuf;
1315 /* only handle symlinks and regular files */
1316 if (dent->d_type != DT_LNK && dent->d_type != DT_REG)
1319 util_strscpyl(path, sizeof(path), udev_device_get_syspath(udev_device), "/", dent->d_name, NULL);
1320 if (lstat(path, &statbuf) != 0)
1322 if ((statbuf.st_mode & S_IRUSR) == 0)
1325 udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list,
1326 dent->d_name, NULL, 0);
1331 dbg(udev_device->udev, "found %d sysattrs for '%s'\n", num, udev_device_get_syspath(udev_device));
1332 udev_device->sysattr_list_read = true;
1338 * udev_device_get_sysattr_list_entry:
1339 * @udev_device: udev device
1341 * Retrieve the list of available sysattrs, with value being empty;
1342 * This just return all available sysfs attributes for a particular
1343 * device without reading their values.
1345 * Returns: the first entry of the property list
1347 struct udev_list_entry *udev_device_get_sysattr_list_entry(struct udev_device *udev_device)
1349 if (!udev_device->sysattr_list_read) {
1351 ret = udev_device_sysattr_list_read(udev_device);
1356 return udev_list_get_entry(&udev_device->sysattr_list);
1359 int udev_device_set_syspath(struct udev_device *udev_device, const char *syspath)
1364 free(udev_device->syspath);
1365 udev_device->syspath = strdup(syspath);
1366 if (udev_device->syspath == NULL)
1368 udev_device->devpath = &udev_device->syspath[strlen(udev_get_sys_path(udev_device->udev))];
1369 udev_device_add_property(udev_device, "DEVPATH", udev_device->devpath);
1371 pos = strrchr(udev_device->syspath, '/');
1374 udev_device->sysname = strdup(&pos[1]);
1375 if (udev_device->sysname == NULL)
1378 /* some devices have '!' in their name, change that to '/' */
1380 while (udev_device->sysname[len] != '\0') {
1381 if (udev_device->sysname[len] == '!')
1382 udev_device->sysname[len] = '/';
1386 /* trailing number */
1387 while (len > 0 && isdigit(udev_device->sysname[--len]))
1388 udev_device->sysnum = &udev_device->sysname[len];
1390 /* sysname is completely numeric */
1392 udev_device->sysnum = NULL;
1397 int udev_device_set_subsystem(struct udev_device *udev_device, const char *subsystem)
1399 free(udev_device->subsystem);
1400 udev_device->subsystem = strdup(subsystem);
1401 if (udev_device->subsystem == NULL)
1403 udev_device->subsystem_set = true;
1404 udev_device_add_property(udev_device, "SUBSYSTEM", udev_device->subsystem);
1408 int udev_device_set_devtype(struct udev_device *udev_device, const char *devtype)
1410 free(udev_device->devtype);
1411 udev_device->devtype = strdup(devtype);
1412 if (udev_device->devtype == NULL)
1414 udev_device->devtype_set = true;
1415 udev_device_add_property(udev_device, "DEVTYPE", udev_device->devtype);
1419 int udev_device_set_devnode(struct udev_device *udev_device, const char *devnode)
1421 free(udev_device->devnode);
1422 udev_device->devnode = strdup(devnode);
1423 if (udev_device->devnode == NULL)
1425 udev_device_add_property(udev_device, "DEVNAME", udev_device->devnode);
1429 int udev_device_set_devnode_mode(struct udev_device *udev_device, mode_t mode)
1433 udev_device->devnode_mode = mode;
1434 snprintf(num, sizeof(num), "%#o", mode);
1435 udev_device_add_property(udev_device, "DEVMODE", num);
1439 int udev_device_add_devlink(struct udev_device *udev_device, const char *devlink, int unique)
1441 struct udev_list_entry *list_entry;
1443 udev_device->devlinks_uptodate = false;
1444 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->devlinks_list, devlink, NULL, UDEV_LIST_UNIQUE);
1445 if (list_entry == NULL)
1448 udev_list_entry_set_num(list_entry, true);
1452 const char *udev_device_get_id_filename(struct udev_device *udev_device)
1454 if (udev_device->id_filename == NULL) {
1455 if (udev_device_get_subsystem(udev_device) == NULL)
1458 if (major(udev_device_get_devnum(udev_device)) > 0) {
1459 /* use dev_t -- b259:131072, c254:0 */
1460 if (asprintf(&udev_device->id_filename, "%c%u:%u",
1461 strcmp(udev_device_get_subsystem(udev_device), "block") == 0 ? 'b' : 'c',
1462 major(udev_device_get_devnum(udev_device)),
1463 minor(udev_device_get_devnum(udev_device))) < 0)
1464 udev_device->id_filename = NULL;
1465 } else if (udev_device_get_ifindex(udev_device) > 0) {
1466 /* use netdev ifindex -- n3 */
1467 if (asprintf(&udev_device->id_filename, "n%u", udev_device_get_ifindex(udev_device)) < 0)
1468 udev_device->id_filename = NULL;
1471 * use $subsys:$syname -- pci:0000:00:1f.2
1472 * sysname() has '!' translated, get it from devpath
1474 const char *sysname;
1475 sysname = strrchr(udev_device->devpath, '/');
1476 if (sysname == NULL)
1478 sysname = &sysname[1];
1479 if (asprintf(&udev_device->id_filename, "+%s:%s", udev_device_get_subsystem(udev_device), sysname) < 0)
1480 udev_device->id_filename = NULL;
1483 return udev_device->id_filename;
1487 * udev_device_get_is_initialized:
1488 * @udev_device: udev device
1490 * Check if udev has already handled the device and has set up
1491 * device node permissions and context, or has renamed a network
1494 * This is only implemented for devices with a device node
1495 * or network interfaces. All other devices return 1 here.
1497 * Returns: 1 if the device is set up. 0 otherwise.
1499 int udev_device_get_is_initialized(struct udev_device *udev_device)
1501 if (!udev_device->info_loaded)
1502 udev_device_read_db(udev_device, NULL);
1503 return udev_device->is_initialized;
1506 void udev_device_set_is_initialized(struct udev_device *udev_device)
1508 udev_device->is_initialized = true;
1511 int udev_device_add_tag(struct udev_device *udev_device, const char *tag)
1513 if (strchr(tag, ':') != NULL || strchr(tag, ' ') != NULL)
1515 udev_device->tags_uptodate = false;
1516 if (udev_list_entry_add(udev_device->udev, &udev_device->tags_list, tag, NULL, UDEV_LIST_UNIQUE) != NULL)
1521 void udev_device_cleanup_tags_list(struct udev_device *udev_device)
1523 udev_device->tags_uptodate = false;
1524 udev_list_cleanup_entries(udev_device->udev, &udev_device->tags_list);
1528 * udev_device_get_tags_list_entry:
1529 * @udev_device: udev device
1531 * Retrieve the list of tags attached to the udev device. The next
1532 * list entry can be retrieved with udev_list_entry_next(),
1533 * which returns #NULL if no more entries exist. The tag string
1534 * can be retrieved from the list entry by udev_list_get_name().
1536 * Returns: the first entry of the tag list
1538 struct udev_list_entry *udev_device_get_tags_list_entry(struct udev_device *udev_device)
1540 if (udev_device == NULL)
1542 return udev_list_get_entry(&udev_device->tags_list);
1545 int udev_device_has_tag(struct udev_device *udev_device, const char *tag)
1547 struct udev_list_entry *list_entry;
1549 if (!udev_device->info_loaded)
1550 udev_device_read_db(udev_device, NULL);
1551 list_entry = udev_device_get_tags_list_entry(udev_device);
1552 list_entry = udev_list_entry_get_by_name(list_entry, tag);
1553 if (list_entry != NULL)
1558 #define ENVP_SIZE 128
1559 #define MONITOR_BUF_SIZE 4096
1560 static int update_envp_monitor_buf(struct udev_device *udev_device)
1562 struct udev_list_entry *list_entry;
1567 /* monitor buffer of property strings */
1568 free(udev_device->monitor_buf);
1569 udev_device->monitor_buf_len = 0;
1570 udev_device->monitor_buf = malloc(MONITOR_BUF_SIZE);
1571 if (udev_device->monitor_buf == NULL)
1574 /* envp array, strings will point into monitor buffer */
1575 if (udev_device->envp == NULL)
1576 udev_device->envp = malloc(sizeof(char *) * ENVP_SIZE);
1577 if (udev_device->envp == NULL)
1581 s = udev_device->monitor_buf;
1582 l = MONITOR_BUF_SIZE;
1583 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device)) {
1586 key = udev_list_entry_get_name(list_entry);
1587 /* skip private variables */
1591 /* add string to envp array */
1592 udev_device->envp[i++] = s;
1593 if (i+1 >= ENVP_SIZE)
1596 /* add property string to monitor buffer */
1597 l = util_strpcpyl(&s, l, key, "=", udev_list_entry_get_value(list_entry), NULL);
1600 /* advance past the trailing '\0' that util_strpcpyl() guarantees */
1604 udev_device->envp[i] = NULL;
1605 udev_device->monitor_buf_len = s - udev_device->monitor_buf;
1606 udev_device->envp_uptodate = true;
1607 dbg(udev_device->udev, "filled envp/monitor buffer, %u properties, %zu bytes\n",
1608 i, udev_device->monitor_buf_len);
1612 char **udev_device_get_properties_envp(struct udev_device *udev_device)
1614 if (!udev_device->envp_uptodate)
1615 if (update_envp_monitor_buf(udev_device) != 0)
1617 return udev_device->envp;
1620 ssize_t udev_device_get_properties_monitor_buf(struct udev_device *udev_device, const char **buf)
1622 if (!udev_device->envp_uptodate)
1623 if (update_envp_monitor_buf(udev_device) != 0)
1625 *buf = udev_device->monitor_buf;
1626 return udev_device->monitor_buf_len;
1629 int udev_device_set_action(struct udev_device *udev_device, const char *action)
1631 free(udev_device->action);
1632 udev_device->action = strdup(action);
1633 if (udev_device->action == NULL)
1635 udev_device_add_property(udev_device, "ACTION", udev_device->action);
1639 int udev_device_set_driver(struct udev_device *udev_device, const char *driver)
1641 free(udev_device->driver);
1642 udev_device->driver = strdup(driver);
1643 if (udev_device->driver == NULL)
1645 udev_device->driver_set = true;
1646 udev_device_add_property(udev_device, "DRIVER", udev_device->driver);
1650 const char *udev_device_get_devpath_old(struct udev_device *udev_device)
1652 return udev_device->devpath_old;
1655 int udev_device_set_devpath_old(struct udev_device *udev_device, const char *devpath_old)
1660 free(udev_device->devpath_old);
1661 udev_device->devpath_old = strdup(devpath_old);
1662 if (udev_device->devpath_old == NULL)
1664 udev_device_add_property(udev_device, "DEVPATH_OLD", udev_device->devpath_old);
1666 pos = strrchr(udev_device->devpath_old, '/');
1669 udev_device->sysname_old = strdup(&pos[1]);
1670 if (udev_device->sysname_old == NULL)
1673 /* some devices have '!' in their name, change that to '/' */
1675 while (udev_device->sysname_old[len] != '\0') {
1676 if (udev_device->sysname_old[len] == '!')
1677 udev_device->sysname_old[len] = '/';
1683 const char *udev_device_get_sysname_old(struct udev_device *udev_device)
1685 if (udev_device == NULL)
1687 return udev_device->sysname_old;
1690 const char *udev_device_get_knodename(struct udev_device *udev_device)
1692 return udev_device->knodename;
1695 int udev_device_set_knodename(struct udev_device *udev_device, const char *knodename)
1697 free(udev_device->knodename);
1698 udev_device->knodename = strdup(knodename);
1699 if (udev_device->knodename == NULL)
1701 /* do not overwrite the udev property with the kernel property */
1702 if (udev_device->devnode == NULL)
1703 udev_device_add_property(udev_device, "DEVNAME", udev_device->knodename);
1707 int udev_device_get_timeout(struct udev_device *udev_device)
1709 return udev_device->timeout;
1712 int udev_device_set_timeout(struct udev_device *udev_device, int timeout)
1716 udev_device->timeout = timeout;
1717 snprintf(num, sizeof(num), "%u", timeout);
1718 udev_device_add_property(udev_device, "TIMEOUT", num);
1722 int udev_device_set_seqnum(struct udev_device *udev_device, unsigned long long int seqnum)
1726 udev_device->seqnum = seqnum;
1727 snprintf(num, sizeof(num), "%llu", seqnum);
1728 udev_device_add_property(udev_device, "SEQNUM", num);
1732 int udev_device_set_devnum(struct udev_device *udev_device, dev_t devnum)
1736 udev_device->devnum = devnum;
1738 snprintf(num, sizeof(num), "%u", major(devnum));
1739 udev_device_add_property(udev_device, "MAJOR", num);
1740 snprintf(num, sizeof(num), "%u", minor(devnum));
1741 udev_device_add_property(udev_device, "MINOR", num);
1745 int udev_device_get_devlink_priority(struct udev_device *udev_device)
1747 if (!udev_device->info_loaded)
1748 udev_device_read_db(udev_device, NULL);
1749 return udev_device->devlink_priority;
1752 int udev_device_set_devlink_priority(struct udev_device *udev_device, int prio)
1754 udev_device->devlink_priority = prio;
1758 int udev_device_get_watch_handle(struct udev_device *udev_device)
1760 if (!udev_device->info_loaded)
1761 udev_device_read_db(udev_device, NULL);
1762 return udev_device->watch_handle;
1765 int udev_device_set_watch_handle(struct udev_device *udev_device, int handle)
1767 udev_device->watch_handle = handle;
1771 int udev_device_get_ifindex(struct udev_device *udev_device)
1773 if (!udev_device->info_loaded)
1774 udev_device_read_uevent_file(udev_device);
1775 return udev_device->ifindex;
1778 int udev_device_set_ifindex(struct udev_device *udev_device, int ifindex)
1782 udev_device->ifindex = ifindex;
1783 snprintf(num, sizeof(num), "%u", ifindex);
1784 udev_device_add_property(udev_device, "IFINDEX", num);
1788 bool udev_device_get_db_persist(struct udev_device *udev_device)
1790 return udev_device->db_persist;
1793 void udev_device_set_db_persist(struct udev_device *udev_device)
1795 udev_device->db_persist = true;