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;
64 size_t monitor_buf_len;
65 struct udev_list devlinks_list;
66 struct udev_list properties_list;
67 struct udev_list sysattr_value_list;
68 struct udev_list sysattr_list;
69 struct udev_list tags_list;
70 unsigned long long int seqnum;
71 unsigned long long int usec_initialized;
82 bool devlinks_uptodate;
90 bool sysattr_list_read;
95 * udev_device_get_seqnum:
96 * @udev_device: udev device
98 * This is only valid if the device was received through a monitor. Devices read from
99 * sys do not have a sequence number.
101 * Returns: the kernel event sequence number, or 0 if there is no sequence number available.
103 UDEV_EXPORT unsigned long long int udev_device_get_seqnum(struct udev_device *udev_device)
105 if (udev_device == NULL)
107 return udev_device->seqnum;
110 static int udev_device_set_seqnum(struct udev_device *udev_device, unsigned long long int seqnum)
114 udev_device->seqnum = seqnum;
115 snprintf(num, sizeof(num), "%llu", seqnum);
116 udev_device_add_property(udev_device, "SEQNUM", num);
120 int udev_device_get_ifindex(struct udev_device *udev_device)
122 if (!udev_device->info_loaded)
123 udev_device_read_uevent_file(udev_device);
124 return udev_device->ifindex;
127 static int udev_device_set_ifindex(struct udev_device *udev_device, int ifindex)
131 udev_device->ifindex = ifindex;
132 snprintf(num, sizeof(num), "%u", ifindex);
133 udev_device_add_property(udev_device, "IFINDEX", num);
138 * udev_device_get_devnum:
139 * @udev_device: udev device
141 * Returns: the device major/minor number.
143 UDEV_EXPORT dev_t udev_device_get_devnum(struct udev_device *udev_device)
145 if (udev_device == NULL)
146 return makedev(0, 0);
147 if (!udev_device->info_loaded)
148 udev_device_read_uevent_file(udev_device);
149 return udev_device->devnum;
152 static int udev_device_set_devnum(struct udev_device *udev_device, dev_t devnum)
156 udev_device->devnum = devnum;
158 snprintf(num, sizeof(num), "%u", major(devnum));
159 udev_device_add_property(udev_device, "MAJOR", num);
160 snprintf(num, sizeof(num), "%u", minor(devnum));
161 udev_device_add_property(udev_device, "MINOR", num);
165 int udev_device_get_timeout(struct udev_device *udev_device)
167 return udev_device->timeout;
170 static int udev_device_set_timeout(struct udev_device *udev_device, int timeout)
174 udev_device->timeout = timeout;
175 snprintf(num, sizeof(num), "%u", timeout);
176 udev_device_add_property(udev_device, "TIMEOUT", num);
180 const char *udev_device_get_knodename(struct udev_device *udev_device)
182 return udev_device->knodename;
185 static int udev_device_set_knodename(struct udev_device *udev_device, const char *knodename)
187 free(udev_device->knodename);
188 udev_device->knodename = strdup(knodename);
189 if (udev_device->knodename == NULL)
191 /* do not overwrite the udev property with the kernel property */
192 if (udev_device->devnode == NULL)
193 udev_device_add_property(udev_device, "DEVNAME", udev_device->knodename);
197 const char *udev_device_get_devpath_old(struct udev_device *udev_device)
199 return udev_device->devpath_old;
202 static int udev_device_set_devpath_old(struct udev_device *udev_device, const char *devpath_old)
206 free(udev_device->devpath_old);
207 udev_device->devpath_old = strdup(devpath_old);
208 if (udev_device->devpath_old == NULL)
210 udev_device_add_property(udev_device, "DEVPATH_OLD", udev_device->devpath_old);
212 pos = strrchr(udev_device->devpath_old, '/');
219 * udev_device_get_driver:
220 * @udev_device: udev device
222 * Returns: the driver string, or #NULL if there is no driver attached.
224 UDEV_EXPORT const char *udev_device_get_driver(struct udev_device *udev_device)
226 char driver[UTIL_NAME_SIZE];
228 if (udev_device == NULL)
230 if (!udev_device->driver_set) {
231 udev_device->driver_set = true;
232 if (util_get_sys_core_link_value(udev_device->udev, "driver", udev_device->syspath, driver, sizeof(driver)) > 0)
233 udev_device->driver = strdup(driver);
235 return udev_device->driver;
238 static int udev_device_set_driver(struct udev_device *udev_device, const char *driver)
240 free(udev_device->driver);
241 udev_device->driver = strdup(driver);
242 if (udev_device->driver == NULL)
244 udev_device->driver_set = true;
245 udev_device_add_property(udev_device, "DRIVER", udev_device->driver);
250 * udev_device_get_devtype:
251 * @udev_device: udev device
253 * Retrieve the devtype string of the udev device.
255 * Returns: the devtype name of the udev device, or #NULL if it can not be determined
257 UDEV_EXPORT const char *udev_device_get_devtype(struct udev_device *udev_device)
259 if (udev_device == NULL)
261 if (!udev_device->devtype_set) {
262 udev_device->devtype_set = true;
263 udev_device_read_uevent_file(udev_device);
265 return udev_device->devtype;
268 static int udev_device_set_devtype(struct udev_device *udev_device, const char *devtype)
270 free(udev_device->devtype);
271 udev_device->devtype = strdup(devtype);
272 if (udev_device->devtype == NULL)
274 udev_device->devtype_set = true;
275 udev_device_add_property(udev_device, "DEVTYPE", udev_device->devtype);
279 static int udev_device_set_subsystem(struct udev_device *udev_device, const char *subsystem)
281 free(udev_device->subsystem);
282 udev_device->subsystem = strdup(subsystem);
283 if (udev_device->subsystem == NULL)
285 udev_device->subsystem_set = true;
286 udev_device_add_property(udev_device, "SUBSYSTEM", udev_device->subsystem);
291 * udev_device_get_subsystem:
292 * @udev_device: udev device
294 * Retrieve the subsystem string of the udev device. The string does not
297 * Returns: the subsystem name of the udev device, or #NULL if it can not be determined
299 UDEV_EXPORT const char *udev_device_get_subsystem(struct udev_device *udev_device)
301 char subsystem[UTIL_NAME_SIZE];
303 if (udev_device == NULL)
305 if (!udev_device->subsystem_set) {
306 udev_device->subsystem_set = true;
307 /* read "subsystem" link */
308 if (util_get_sys_core_link_value(udev_device->udev, "subsystem", udev_device->syspath, subsystem, sizeof(subsystem)) > 0) {
309 udev_device_set_subsystem(udev_device, subsystem);
310 return udev_device->subsystem;
313 if (strncmp(udev_device->devpath, "/module/", 8) == 0) {
314 udev_device_set_subsystem(udev_device, "module");
315 return udev_device->subsystem;
317 if (strstr(udev_device->devpath, "/drivers/") != NULL) {
318 udev_device_set_subsystem(udev_device, "drivers");
319 return udev_device->subsystem;
321 if (strncmp(udev_device->devpath, "/subsystem/", 11) == 0 ||
322 strncmp(udev_device->devpath, "/class/", 7) == 0 ||
323 strncmp(udev_device->devpath, "/bus/", 5) == 0) {
324 udev_device_set_subsystem(udev_device, "subsystem");
325 return udev_device->subsystem;
328 return udev_device->subsystem;
331 mode_t udev_device_get_devnode_mode(struct udev_device *udev_device)
333 if (!udev_device->info_loaded)
334 udev_device_read_uevent_file(udev_device);
335 return udev_device->devnode_mode;
338 static int udev_device_set_devnode_mode(struct udev_device *udev_device, mode_t mode)
342 udev_device->devnode_mode = mode;
343 snprintf(num, sizeof(num), "%#o", mode);
344 udev_device_add_property(udev_device, "DEVMODE", num);
348 struct udev_list_entry *udev_device_add_property(struct udev_device *udev_device, const char *key, const char *value)
350 udev_device->envp_uptodate = false;
352 struct udev_list_entry *list_entry;
354 list_entry = udev_device_get_properties_list_entry(udev_device);
355 list_entry = udev_list_entry_get_by_name(list_entry, key);
356 if (list_entry != NULL)
357 udev_list_entry_delete(list_entry);
360 return udev_list_entry_add(&udev_device->properties_list, key, value);
363 static struct udev_list_entry *udev_device_add_property_from_string(struct udev_device *udev_device, const char *property)
365 char name[UTIL_LINE_SIZE];
368 util_strscpy(name, sizeof(name), property);
369 val = strchr(name, '=');
376 return udev_device_add_property(udev_device, name, val);
380 * parse property string, and if needed, update internal values accordingly
382 * udev_device_add_property_from_string_parse_finish() needs to be
383 * called after adding properties, and its return value checked
385 * udev_device_set_info_loaded() needs to be set, to avoid trying
386 * to use a device without a DEVPATH set
388 void udev_device_add_property_from_string_parse(struct udev_device *udev_device, const char *property)
390 if (strncmp(property, "DEVPATH=", 8) == 0) {
391 char path[UTIL_PATH_SIZE];
393 util_strscpyl(path, sizeof(path), udev_get_sys_path(udev_device->udev), &property[8], NULL);
394 udev_device_set_syspath(udev_device, path);
395 } else if (strncmp(property, "SUBSYSTEM=", 10) == 0) {
396 udev_device_set_subsystem(udev_device, &property[10]);
397 } else if (strncmp(property, "DEVTYPE=", 8) == 0) {
398 udev_device_set_devtype(udev_device, &property[8]);
399 } else if (strncmp(property, "DEVNAME=", 8) == 0) {
400 if (property[8] == '/')
401 udev_device_set_devnode(udev_device, &property[8]);
403 udev_device_set_knodename(udev_device, &property[8]);
404 } else if (strncmp(property, "DEVLINKS=", 9) == 0) {
405 char devlinks[UTIL_PATH_SIZE];
409 util_strscpy(devlinks, sizeof(devlinks), &property[9]);
411 next = strchr(slink, ' ');
412 while (next != NULL) {
414 udev_device_add_devlink(udev_device, slink, 0);
416 next = strchr(slink, ' ');
418 if (slink[0] != '\0')
419 udev_device_add_devlink(udev_device, slink, 0);
420 } else if (strncmp(property, "TAGS=", 5) == 0) {
421 char tags[UTIL_PATH_SIZE];
424 util_strscpy(tags, sizeof(tags), &property[5]);
425 next = strchr(tags, ':');
428 while (next[0] != '\0') {
432 next = strchr(tag, ':');
437 udev_device_add_tag(udev_device, tag);
440 } else if (strncmp(property, "USEC_INITIALIZED=", 19) == 0) {
441 udev_device_set_usec_initialized(udev_device, strtoull(&property[19], NULL, 10));
442 } else if (strncmp(property, "DRIVER=", 7) == 0) {
443 udev_device_set_driver(udev_device, &property[7]);
444 } else if (strncmp(property, "ACTION=", 7) == 0) {
445 udev_device_set_action(udev_device, &property[7]);
446 } else if (strncmp(property, "MAJOR=", 6) == 0) {
447 udev_device->maj = strtoull(&property[6], NULL, 10);
448 } else if (strncmp(property, "MINOR=", 6) == 0) {
449 udev_device->min = strtoull(&property[6], NULL, 10);
450 } else if (strncmp(property, "DEVPATH_OLD=", 12) == 0) {
451 udev_device_set_devpath_old(udev_device, &property[12]);
452 } else if (strncmp(property, "SEQNUM=", 7) == 0) {
453 udev_device_set_seqnum(udev_device, strtoull(&property[7], NULL, 10));
454 } else if (strncmp(property, "TIMEOUT=", 8) == 0) {
455 udev_device_set_timeout(udev_device, strtoull(&property[8], NULL, 10));
456 } else if (strncmp(property, "IFINDEX=", 8) == 0) {
457 udev_device_set_ifindex(udev_device, strtoull(&property[8], NULL, 10));
458 } else if (strncmp(property, "DEVMODE=", 8) == 0) {
459 udev_device_set_devnode_mode(udev_device, strtoul(&property[8], NULL, 8));
461 udev_device_add_property_from_string(udev_device, property);
465 int udev_device_add_property_from_string_parse_finish(struct udev_device *udev_device)
467 if (udev_device->maj > 0)
468 udev_device_set_devnum(udev_device, makedev(udev_device->maj, udev_device->min));
469 udev_device->maj = 0;
470 udev_device->min = 0;
472 if (udev_device->devpath == NULL || udev_device->subsystem == NULL)
478 * udev_device_get_property_value:
479 * @udev_device: udev device
480 * @key: property name
482 * Returns: the value of a device property, or #NULL if there is no such property.
484 UDEV_EXPORT const char *udev_device_get_property_value(struct udev_device *udev_device, const char *key)
486 struct udev_list_entry *list_entry;
488 if (udev_device == NULL)
493 list_entry = udev_device_get_properties_list_entry(udev_device);
494 list_entry = udev_list_entry_get_by_name(list_entry, key);
495 return udev_list_entry_get_value(list_entry);
498 int udev_device_read_db(struct udev_device *udev_device, const char *dbfile)
500 char filename[UTIL_PATH_SIZE];
501 char line[UTIL_LINE_SIZE];
504 /* providing a database file will always force-load it */
505 if (dbfile == NULL) {
508 if (udev_device->db_loaded)
510 udev_device->db_loaded = true;
512 id = udev_device_get_id_filename(udev_device);
515 util_strscpyl(filename, sizeof(filename), udev_get_run_path(udev_device->udev), "/data/", id, NULL);
519 f = fopen(dbfile, "re");
521 info(udev_device->udev, "no db file to read %s: %m\n", dbfile);
524 udev_device->is_initialized = true;
526 while (fgets(line, sizeof(line), f)) {
529 struct udev_list_entry *entry;
538 util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev_device->udev), "/", val, NULL);
539 udev_device_set_devnode(udev_device, filename);
542 util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev_device->udev), "/", val, NULL);
543 udev_device_add_devlink(udev_device, filename, 0);
546 udev_device_set_devlink_priority(udev_device, atoi(val));
549 entry = udev_device_add_property_from_string(udev_device, val);
550 udev_list_entry_set_num(entry, true);
553 udev_device_add_tag(udev_device, val);
556 udev_device_set_watch_handle(udev_device, atoi(val));
559 udev_device_set_usec_initialized(udev_device, strtoull(val, NULL, 10));
565 info(udev_device->udev, "device %p filled with db file data\n", udev_device);
569 int udev_device_read_uevent_file(struct udev_device *udev_device)
571 char filename[UTIL_PATH_SIZE];
573 char line[UTIL_LINE_SIZE];
577 if (udev_device->uevent_loaded)
580 util_strscpyl(filename, sizeof(filename), udev_device->syspath, "/uevent", NULL);
581 f = fopen(filename, "re");
584 udev_device->uevent_loaded = true;
586 while (fgets(line, sizeof(line), f)) {
589 pos = strchr(line, '\n');
594 if (strncmp(line, "DEVTYPE=", 8) == 0)
595 udev_device_set_devtype(udev_device, &line[8]);
596 else if (strncmp(line, "MAJOR=", 6) == 0)
597 maj = strtoull(&line[6], NULL, 10);
598 else if (strncmp(line, "MINOR=", 6) == 0)
599 min = strtoull(&line[6], NULL, 10);
600 else if (strncmp(line, "IFINDEX=", 8) == 0)
601 udev_device_set_ifindex(udev_device, strtoull(&line[8], NULL, 10));
602 else if (strncmp(line, "DEVNAME=", 8) == 0)
603 udev_device_set_knodename(udev_device, &line[8]);
604 else if (strncmp(line, "DEVMODE=", 8) == 0)
605 udev_device->devnode_mode = strtoul(&line[8], NULL, 8);
607 udev_device_add_property_from_string(udev_device, line);
610 udev_device->devnum = makedev(maj, min);
615 void udev_device_set_info_loaded(struct udev_device *device)
617 device->info_loaded = true;
620 struct udev_device *udev_device_new(struct udev *udev)
622 struct udev_device *udev_device;
623 struct udev_list_entry *list_entry;
628 udev_device = calloc(1, sizeof(struct udev_device));
629 if (udev_device == NULL)
631 udev_device->refcount = 1;
632 udev_device->udev = udev;
633 udev_list_init(udev, &udev_device->devlinks_list, true);
634 udev_list_init(udev, &udev_device->properties_list, true);
635 udev_list_init(udev, &udev_device->sysattr_value_list, true);
636 udev_list_init(udev, &udev_device->sysattr_list, false);
637 udev_list_init(udev, &udev_device->tags_list, true);
638 udev_device->timeout = -1;
639 udev_device->watch_handle = -1;
640 /* copy global properties */
641 udev_list_entry_foreach(list_entry, udev_get_properties_list_entry(udev))
642 udev_device_add_property(udev_device,
643 udev_list_entry_get_name(list_entry),
644 udev_list_entry_get_value(list_entry));
645 dbg(udev_device->udev, "udev_device: %p created\n", udev_device);
650 * udev_device_new_from_syspath:
651 * @udev: udev library context
652 * @syspath: sys device path including sys directory
654 * Create new udev device, and fill in information from the sys
655 * device and the udev database entry. The syspath is the absolute
656 * path to the device, including the sys mount point.
658 * The initial refcount is 1, and needs to be decremented to
659 * release the resources of the udev device.
661 * Returns: a new udev device, or #NULL, if it does not exist
663 UDEV_EXPORT struct udev_device *udev_device_new_from_syspath(struct udev *udev, const char *syspath)
667 char path[UTIL_PATH_SIZE];
670 struct udev_device *udev_device;
677 /* path starts in sys */
678 len = strlen(udev_get_sys_path(udev));
679 if (strncmp(syspath, udev_get_sys_path(udev), len) != 0) {
680 info(udev, "not in sys :%s\n", syspath);
684 /* path is not a root directory */
685 subdir = &syspath[len+1];
686 pos = strrchr(subdir, '/');
687 if (pos == NULL || pos[1] == '\0' || pos < &subdir[2]) {
688 dbg(udev, "not a subdir :%s\n", syspath);
692 /* resolve possible symlink to real path */
693 util_strscpy(path, sizeof(path), syspath);
694 util_resolve_sys_link(udev, path, sizeof(path));
696 if (strncmp(&path[len], "/devices/", 9) == 0) {
697 char file[UTIL_PATH_SIZE];
699 /* all "devices" require a "uevent" file */
700 util_strscpyl(file, sizeof(file), path, "/uevent", NULL);
701 if (stat(file, &statbuf) != 0) {
702 dbg(udev, "not a device: %s\n", syspath);
706 /* everything else just needs to be a directory */
707 if (stat(path, &statbuf) != 0 || !S_ISDIR(statbuf.st_mode)) {
708 dbg(udev, "directory not found: %s\n", syspath);
713 udev_device = udev_device_new(udev);
714 if (udev_device == NULL)
717 udev_device_set_syspath(udev_device, path);
718 info(udev, "device %p has devpath '%s'\n", udev_device, udev_device_get_devpath(udev_device));
724 * udev_device_new_from_devnum:
725 * @udev: udev library context
726 * @type: char or block device
727 * @devnum: device major/minor number
729 * Create new udev device, and fill in information from the sys
730 * device and the udev database entry. The device is looked-up
731 * by its major/minor number and type. Character and block device
732 * numbers are not unique across the two types.
734 * The initial refcount is 1, and needs to be decremented to
735 * release the resources of the udev device.
737 * Returns: a new udev device, or #NULL, if it does not exist
739 UDEV_EXPORT struct udev_device *udev_device_new_from_devnum(struct udev *udev, char type, dev_t devnum)
741 char path[UTIL_PATH_SIZE];
742 const char *type_str;
746 else if (type == 'c')
751 /* use /sys/dev/{block,char}/<maj>:<min> link */
752 snprintf(path, sizeof(path), "%s/dev/%s/%u:%u",
753 udev_get_sys_path(udev), type_str, major(devnum), minor(devnum));
754 return udev_device_new_from_syspath(udev, path);
757 struct udev_device *udev_device_new_from_id_filename(struct udev *udev, char *id)
761 char subsys[UTIL_PATH_SIZE];
767 if (sscanf(id, "%c%i:%i", &type, &maj, &min) != 3)
769 return udev_device_new_from_devnum(udev, type, makedev(maj, min));
773 struct udev_device *dev;
776 ifindex = strtoul(&id[1], NULL, 10);
780 sk = socket(PF_INET, SOCK_DGRAM, 0);
783 memset(&ifr, 0x00, sizeof(struct ifreq));
784 ifr.ifr_ifindex = ifindex;
785 if (ioctl(sk, SIOCGIFNAME, &ifr) != 0) {
791 dev = udev_device_new_from_subsystem_sysname(udev, "net", ifr.ifr_name);
794 if (udev_device_get_ifindex(dev) == ifindex)
796 udev_device_unref(dev);
800 util_strscpy(subsys, sizeof(subsys), &id[1]);
801 sysname = strchr(subsys, ':');
805 sysname = &sysname[1];
806 return udev_device_new_from_subsystem_sysname(udev, subsys, sysname);
813 * udev_device_new_from_subsystem_sysname:
814 * @udev: udev library context
815 * @subsystem: the subsystem of the device
816 * @sysname: the name of the device
818 * Create new udev device, and fill in information from the sys device
819 * and the udev database entry. The device is looked up by the subsystem
820 * and name string of the device, like "mem" / "zero", or "block" / "sda".
822 * The initial refcount is 1, and needs to be decremented to
823 * release the resources of the udev device.
825 * Returns: a new udev device, or #NULL, if it does not exist
827 UDEV_EXPORT struct udev_device *udev_device_new_from_subsystem_sysname(struct udev *udev, const char *subsystem, const char *sysname)
829 char path_full[UTIL_PATH_SIZE];
835 l = util_strpcpyl(&path, sizeof(path_full), udev_get_sys_path(udev), NULL);
837 if (strcmp(subsystem, "subsystem") == 0) {
838 util_strscpyl(path, l, "/subsystem/", sysname, NULL);
839 if (stat(path_full, &statbuf) == 0)
842 util_strscpyl(path, l, "/bus/", sysname, NULL);
843 if (stat(path_full, &statbuf) == 0)
846 util_strscpyl(path, l, "/class/", sysname, NULL);
847 if (stat(path_full, &statbuf) == 0)
852 if (strcmp(subsystem, "module") == 0) {
853 util_strscpyl(path, l, "/module/", sysname, NULL);
854 if (stat(path_full, &statbuf) == 0)
859 if (strcmp(subsystem, "drivers") == 0) {
860 char subsys[UTIL_NAME_SIZE];
863 util_strscpy(subsys, sizeof(subsys), sysname);
864 driver = strchr(subsys, ':');
865 if (driver != NULL) {
869 util_strscpyl(path, l, "/subsystem/", subsys, "/drivers/", driver, NULL);
870 if (stat(path_full, &statbuf) == 0)
873 util_strscpyl(path, l, "/bus/", subsys, "/drivers/", driver, NULL);
874 if (stat(path_full, &statbuf) == 0)
880 util_strscpyl(path, l, "/subsystem/", subsystem, "/devices/", sysname, NULL);
881 if (stat(path_full, &statbuf) == 0)
884 util_strscpyl(path, l, "/bus/", subsystem, "/devices/", sysname, NULL);
885 if (stat(path_full, &statbuf) == 0)
888 util_strscpyl(path, l, "/class/", subsystem, "/", sysname, NULL);
889 if (stat(path_full, &statbuf) == 0)
894 return udev_device_new_from_syspath(udev, path_full);
898 * udev_device_new_from_environment
899 * @udev: udev library context
901 * Create new udev device, and fill in information from the
902 * current process environment. This only works reliable if
903 * the process is called from a udev rule. It is usually used
904 * for tools executed from IMPORT= rules.
906 * The initial refcount is 1, and needs to be decremented to
907 * release the resources of the udev device.
909 * Returns: a new udev device, or #NULL, if it does not exist
911 UDEV_EXPORT struct udev_device *udev_device_new_from_environment(struct udev *udev)
914 struct udev_device *udev_device;
916 udev_device = udev_device_new(udev);
917 if (udev_device == NULL)
919 udev_device_set_info_loaded(udev_device);
921 for (i = 0; environ[i] != NULL; i++)
922 udev_device_add_property_from_string_parse(udev_device, environ[i]);
924 if (udev_device_add_property_from_string_parse_finish(udev_device) < 0) {
925 info(udev, "missing values, invalid device\n");
926 udev_device_unref(udev_device);
933 static struct udev_device *device_new_from_parent(struct udev_device *udev_device)
935 struct udev_device *udev_device_parent = NULL;
936 char path[UTIL_PATH_SIZE];
939 util_strscpy(path, sizeof(path), udev_device->syspath);
940 subdir = &path[strlen(udev_get_sys_path(udev_device->udev))+1];
944 pos = strrchr(subdir, '/');
945 if (pos == NULL || pos < &subdir[2])
948 udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
949 if (udev_device_parent != NULL)
950 return udev_device_parent;
956 * udev_device_get_parent:
957 * @udev_device: the device to start searching from
959 * Find the next parent device, and fill in information from the sys
960 * device and the udev database entry.
962 * The returned the device is not referenced. It is attached to the
963 * child device, and will be cleaned up when the child device
966 * It is not necessarily just the upper level directory, empty or not
967 * recognized sys directories are ignored.
969 * It can be called as many times as needed, without caring about
972 * Returns: a new udev device, or #NULL, if it no parent exist.
974 UDEV_EXPORT struct udev_device *udev_device_get_parent(struct udev_device *udev_device)
976 if (udev_device == NULL)
978 if (!udev_device->parent_set) {
979 udev_device->parent_set = true;
980 udev_device->parent_device = device_new_from_parent(udev_device);
982 if (udev_device->parent_device != NULL)
983 dbg(udev_device->udev, "returning existing parent %p\n", udev_device->parent_device);
984 return udev_device->parent_device;
988 * udev_device_get_parent_with_subsystem_devtype:
989 * @udev_device: udev device to start searching from
990 * @subsystem: the subsystem of the device
991 * @devtype: the type (DEVTYPE) of the device
993 * Find the next parent device, with a matching subsystem and devtype
994 * value, and fill in information from the sys device and the udev
997 * If devtype is #NULL, only subsystem is checked, and any devtype will
1000 * The returned the device is not referenced. It is attached to the
1001 * child device, and will be cleaned up when the child device
1004 * It can be called as many times as needed, without caring about
1007 * Returns: a new udev device, or #NULL if no matching parent exists.
1009 UDEV_EXPORT struct udev_device *udev_device_get_parent_with_subsystem_devtype(struct udev_device *udev_device, const char *subsystem, const char *devtype)
1011 struct udev_device *parent;
1013 if (subsystem == NULL)
1016 parent = udev_device_get_parent(udev_device);
1017 while (parent != NULL) {
1018 const char *parent_subsystem;
1019 const char *parent_devtype;
1021 parent_subsystem = udev_device_get_subsystem(parent);
1022 if (parent_subsystem != NULL && strcmp(parent_subsystem, subsystem) == 0) {
1023 if (devtype == NULL)
1025 parent_devtype = udev_device_get_devtype(parent);
1026 if (parent_devtype != NULL && strcmp(parent_devtype, devtype) == 0)
1029 parent = udev_device_get_parent(parent);
1035 * udev_device_get_udev:
1036 * @udev_device: udev device
1038 * Retrieve the udev library context the device was created with.
1040 * Returns: the udev library context
1042 UDEV_EXPORT struct udev *udev_device_get_udev(struct udev_device *udev_device)
1044 if (udev_device == NULL)
1046 return udev_device->udev;
1051 * @udev_device: udev device
1053 * Take a reference of a udev device.
1055 * Returns: the passed udev device
1057 UDEV_EXPORT struct udev_device *udev_device_ref(struct udev_device *udev_device)
1059 if (udev_device == NULL)
1061 udev_device->refcount++;
1066 * udev_device_unref:
1067 * @udev_device: udev device
1069 * Drop a reference of a udev device. If the refcount reaches zero,
1070 * the resources of the device will be released.
1073 UDEV_EXPORT void udev_device_unref(struct udev_device *udev_device)
1075 if (udev_device == NULL)
1077 udev_device->refcount--;
1078 if (udev_device->refcount > 0)
1080 if (udev_device->parent_device != NULL)
1081 udev_device_unref(udev_device->parent_device);
1082 free(udev_device->syspath);
1083 free(udev_device->sysname);
1084 free(udev_device->devnode);
1085 free(udev_device->subsystem);
1086 free(udev_device->devtype);
1087 udev_list_cleanup(&udev_device->devlinks_list);
1088 udev_list_cleanup(&udev_device->properties_list);
1089 udev_list_cleanup(&udev_device->sysattr_value_list);
1090 udev_list_cleanup(&udev_device->sysattr_list);
1091 udev_list_cleanup(&udev_device->tags_list);
1092 free(udev_device->action);
1093 free(udev_device->driver);
1094 free(udev_device->devpath_old);
1095 free(udev_device->knodename);
1096 free(udev_device->id_filename);
1097 free(udev_device->envp);
1098 free(udev_device->monitor_buf);
1099 dbg(udev_device->udev, "udev_device: %p released\n", udev_device);
1104 * udev_device_get_devpath:
1105 * @udev_device: udev device
1107 * Retrieve the kernel devpath value of the udev device. The path
1108 * does not contain the sys mount point, and starts with a '/'.
1110 * Returns: the devpath of the udev device
1112 UDEV_EXPORT const char *udev_device_get_devpath(struct udev_device *udev_device)
1114 if (udev_device == NULL)
1116 return udev_device->devpath;
1120 * udev_device_get_syspath:
1121 * @udev_device: udev device
1123 * Retrieve the sys path of the udev device. The path is an
1124 * absolute path and starts with the sys mount point.
1126 * Returns: the sys path of the udev device
1128 UDEV_EXPORT const char *udev_device_get_syspath(struct udev_device *udev_device)
1130 if (udev_device == NULL)
1132 return udev_device->syspath;
1136 * udev_device_get_sysname:
1137 * @udev_device: udev device
1139 * Returns: the sys name of the device device
1141 UDEV_EXPORT const char *udev_device_get_sysname(struct udev_device *udev_device)
1143 if (udev_device == NULL)
1145 return udev_device->sysname;
1149 * udev_device_get_sysnum:
1150 * @udev_device: udev device
1152 * Returns: the trailing number of of the device name
1154 UDEV_EXPORT const char *udev_device_get_sysnum(struct udev_device *udev_device)
1156 if (udev_device == NULL)
1158 return udev_device->sysnum;
1162 * udev_device_get_devnode:
1163 * @udev_device: udev device
1165 * Retrieve the device node file name belonging to the udev device.
1166 * The path is an absolute path, and starts with the device directory.
1168 * Returns: the device node file name of the udev device, or #NULL if no device node exists
1170 UDEV_EXPORT const char *udev_device_get_devnode(struct udev_device *udev_device)
1172 if (udev_device == NULL)
1174 if (!udev_device->info_loaded) {
1175 udev_device_read_uevent_file(udev_device);
1176 udev_device_read_db(udev_device, NULL);
1179 /* we might get called before we handled an event and have a db, use the kernel-provided name */
1180 if (udev_device->devnode == NULL && udev_device_get_knodename(udev_device) != NULL) {
1181 char filename[UTIL_NAME_SIZE];
1183 util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev_device->udev), "/",
1184 udev_device_get_knodename(udev_device), NULL);
1185 udev_device_set_devnode(udev_device, filename);
1186 return udev_device->devnode;
1189 return udev_device->devnode;
1193 * udev_device_get_devlinks_list_entry:
1194 * @udev_device: udev device
1196 * Retrieve the list of device links pointing to the device file of
1197 * the udev device. The next list entry can be retrieved with
1198 * udev_list_entry_next(), which returns #NULL if no more entries exist.
1199 * The devlink path can be retrieved from the list entry by
1200 * udev_list_entry_get_name(). The path is an absolute path, and starts with
1201 * the device directory.
1203 * Returns: the first entry of the device node link list
1205 UDEV_EXPORT struct udev_list_entry *udev_device_get_devlinks_list_entry(struct udev_device *udev_device)
1207 if (udev_device == NULL)
1209 if (!udev_device->info_loaded)
1210 udev_device_read_db(udev_device, NULL);
1211 return udev_list_get_entry(&udev_device->devlinks_list);
1214 void udev_device_cleanup_devlinks_list(struct udev_device *udev_device)
1216 udev_device->devlinks_uptodate = false;
1217 udev_list_cleanup(&udev_device->devlinks_list);
1221 * udev_device_get_properties_list_entry:
1222 * @udev_device: udev device
1224 * Retrieve the list of key/value device properties of the udev
1225 * device. The next list entry can be retrieved with udev_list_entry_next(),
1226 * which returns #NULL if no more entries exist. The property name
1227 * can be retrieved from the list entry by udev_list_get_name(),
1228 * the property value by udev_list_get_value().
1230 * Returns: the first entry of the property list
1232 UDEV_EXPORT struct udev_list_entry *udev_device_get_properties_list_entry(struct udev_device *udev_device)
1234 if (udev_device == NULL)
1236 if (!udev_device->info_loaded) {
1237 udev_device_read_uevent_file(udev_device);
1238 udev_device_read_db(udev_device, NULL);
1240 if (!udev_device->devlinks_uptodate) {
1241 char symlinks[UTIL_PATH_SIZE];
1242 struct udev_list_entry *list_entry;
1244 udev_device->devlinks_uptodate = true;
1245 list_entry = udev_device_get_devlinks_list_entry(udev_device);
1246 if (list_entry != NULL) {
1251 l = util_strpcpyl(&s, sizeof(symlinks), udev_list_entry_get_name(list_entry), NULL);
1252 udev_list_entry_foreach(list_entry, udev_list_entry_get_next(list_entry))
1253 l = util_strpcpyl(&s, l, " ", udev_list_entry_get_name(list_entry), NULL);
1254 udev_device_add_property(udev_device, "DEVLINKS", symlinks);
1257 if (!udev_device->tags_uptodate) {
1258 udev_device->tags_uptodate = true;
1259 if (udev_device_get_tags_list_entry(udev_device) != NULL) {
1260 char tags[UTIL_PATH_SIZE];
1261 struct udev_list_entry *list_entry;
1266 l = util_strpcpyl(&s, sizeof(tags), ":", NULL);
1267 udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(udev_device))
1268 l = util_strpcpyl(&s, l, udev_list_entry_get_name(list_entry), ":", NULL);
1269 udev_device_add_property(udev_device, "TAGS", tags);
1272 return udev_list_get_entry(&udev_device->properties_list);
1276 * udev_device_get_action:
1277 * @udev_device: udev device
1279 * This is only valid if the device was received through a monitor. Devices read from
1280 * sys do not have an action string. Usual actions are: add, remove, change, online,
1283 * Returns: the kernel action value, or #NULL if there is no action value available.
1285 UDEV_EXPORT const char *udev_device_get_action(struct udev_device *udev_device)
1287 if (udev_device == NULL)
1289 return udev_device->action;
1293 * udev_device_get_usec_since_initialized:
1294 * @udev_device: udev device
1296 * Return the number of microseconds passed since udev set up the
1297 * device for the first time.
1299 * This is only implemented for devices with need to store properties
1300 * in the udev database. All other devices return 0 here.
1302 * Returns: the number of microseconds since the device was first seen.
1304 UDEV_EXPORT unsigned long long int udev_device_get_usec_since_initialized(struct udev_device *udev_device)
1306 unsigned long long now;
1308 if (udev_device == NULL)
1310 if (!udev_device->info_loaded)
1311 udev_device_read_db(udev_device, NULL);
1312 if (udev_device->usec_initialized == 0)
1317 return now - udev_device->usec_initialized;
1320 unsigned long long udev_device_get_usec_initialized(struct udev_device *udev_device)
1322 return udev_device->usec_initialized;
1325 void udev_device_set_usec_initialized(struct udev_device *udev_device, unsigned long long usec_initialized)
1329 udev_device->usec_initialized = usec_initialized;
1330 snprintf(num, sizeof(num), "%llu", usec_initialized);
1331 udev_device_add_property(udev_device, "USEC_INITIALIZED", num);
1335 * udev_device_get_sysattr_value:
1336 * @udev_device: udev device
1337 * @sysattr: attribute name
1339 * The retrieved value is cached in the device. Repeated calls will return the same
1340 * value and not open the attribute again.
1342 * Returns: the content of a sys attribute file, or #NULL if there is no sys attribute value.
1344 UDEV_EXPORT const char *udev_device_get_sysattr_value(struct udev_device *udev_device, const char *sysattr)
1346 struct udev_list_entry *list_entry;
1347 char path[UTIL_PATH_SIZE];
1349 struct stat statbuf;
1352 const char *val = NULL;
1354 if (udev_device == NULL)
1356 if (sysattr == NULL)
1359 /* look for possibly already cached result */
1360 list_entry = udev_list_get_entry(&udev_device->sysattr_value_list);
1361 list_entry = udev_list_entry_get_by_name(list_entry, sysattr);
1362 if (list_entry != NULL) {
1363 dbg(udev_device->udev, "got '%s' (%s) from cache\n",
1364 sysattr, udev_list_entry_get_value(list_entry));
1365 return udev_list_entry_get_value(list_entry);
1368 util_strscpyl(path, sizeof(path), udev_device_get_syspath(udev_device), "/", sysattr, NULL);
1369 if (lstat(path, &statbuf) != 0) {
1370 dbg(udev_device->udev, "no attribute '%s', keep negative entry\n", path);
1371 udev_list_entry_add(&udev_device->sysattr_value_list, sysattr, NULL);
1375 if (S_ISLNK(statbuf.st_mode)) {
1376 struct udev_device *dev;
1379 * Some core links return only the last element of the target path,
1380 * these are just values, the paths should not be exposed.
1382 if (strcmp(sysattr, "driver") == 0 ||
1383 strcmp(sysattr, "subsystem") == 0 ||
1384 strcmp(sysattr, "module") == 0) {
1385 if (util_get_sys_core_link_value(udev_device->udev, sysattr,
1386 udev_device->syspath, value, sizeof(value)) < 0)
1388 dbg(udev_device->udev, "cache '%s' with link value '%s'\n", sysattr, value);
1389 list_entry = udev_list_entry_add(&udev_device->sysattr_value_list, sysattr, value);
1390 val = udev_list_entry_get_value(list_entry);
1394 /* resolve link to a device and return its syspath */
1395 util_strscpyl(path, sizeof(path), udev_device->syspath, "/", sysattr, NULL);
1396 dev = udev_device_new_from_syspath(udev_device->udev, path);
1398 list_entry = udev_list_entry_add(&udev_device->sysattr_value_list, sysattr,
1399 udev_device_get_syspath(dev));
1400 val = udev_list_entry_get_value(list_entry);
1401 udev_device_unref(dev);
1407 /* skip directories */
1408 if (S_ISDIR(statbuf.st_mode))
1411 /* skip non-readable files */
1412 if ((statbuf.st_mode & S_IRUSR) == 0)
1415 /* read attribute value */
1416 fd = open(path, O_RDONLY|O_CLOEXEC);
1418 dbg(udev_device->udev, "attribute '%s' can not be opened\n", path);
1421 size = read(fd, value, sizeof(value));
1425 if (size == sizeof(value))
1428 /* got a valid value, store it in cache and return it */
1430 util_remove_trailing_chars(value, '\n');
1431 dbg(udev_device->udev, "'%s' has attribute value '%s'\n", path, value);
1432 list_entry = udev_list_entry_add(&udev_device->sysattr_value_list, sysattr, value);
1433 val = udev_list_entry_get_value(list_entry);
1438 static int udev_device_sysattr_list_read(struct udev_device *udev_device)
1440 struct dirent *dent;
1444 if (udev_device == NULL)
1446 if (udev_device->sysattr_list_read)
1449 dir = opendir(udev_device_get_syspath(udev_device));
1451 dbg(udev_device->udev, "sysfs dir '%s' can not be opened\n",
1452 udev_device_get_syspath(udev_device));
1456 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
1457 char path[UTIL_PATH_SIZE];
1458 struct stat statbuf;
1460 /* only handle symlinks and regular files */
1461 if (dent->d_type != DT_LNK && dent->d_type != DT_REG)
1464 util_strscpyl(path, sizeof(path), udev_device_get_syspath(udev_device), "/", dent->d_name, NULL);
1465 if (lstat(path, &statbuf) != 0)
1467 if ((statbuf.st_mode & S_IRUSR) == 0)
1470 udev_list_entry_add(&udev_device->sysattr_list, dent->d_name, NULL);
1475 dbg(udev_device->udev, "found %d sysattrs for '%s'\n", num, udev_device_get_syspath(udev_device));
1476 udev_device->sysattr_list_read = true;
1482 * udev_device_get_sysattr_list_entry:
1483 * @udev_device: udev device
1485 * Retrieve the list of available sysattrs, with value being empty;
1486 * This just return all available sysfs attributes for a particular
1487 * device without reading their values.
1489 * Returns: the first entry of the property list
1491 UDEV_EXPORT struct udev_list_entry *udev_device_get_sysattr_list_entry(struct udev_device *udev_device)
1493 if (!udev_device->sysattr_list_read) {
1495 ret = udev_device_sysattr_list_read(udev_device);
1500 return udev_list_get_entry(&udev_device->sysattr_list);
1503 int udev_device_set_syspath(struct udev_device *udev_device, const char *syspath)
1508 free(udev_device->syspath);
1509 udev_device->syspath = strdup(syspath);
1510 if (udev_device->syspath == NULL)
1512 udev_device->devpath = &udev_device->syspath[strlen(udev_get_sys_path(udev_device->udev))];
1513 udev_device_add_property(udev_device, "DEVPATH", udev_device->devpath);
1515 pos = strrchr(udev_device->syspath, '/');
1518 udev_device->sysname = strdup(&pos[1]);
1519 if (udev_device->sysname == NULL)
1522 /* some devices have '!' in their name, change that to '/' */
1524 while (udev_device->sysname[len] != '\0') {
1525 if (udev_device->sysname[len] == '!')
1526 udev_device->sysname[len] = '/';
1530 /* trailing number */
1531 while (len > 0 && isdigit(udev_device->sysname[--len]))
1532 udev_device->sysnum = &udev_device->sysname[len];
1534 /* sysname is completely numeric */
1536 udev_device->sysnum = NULL;
1541 int udev_device_set_devnode(struct udev_device *udev_device, const char *devnode)
1543 free(udev_device->devnode);
1544 udev_device->devnode = strdup(devnode);
1545 if (udev_device->devnode == NULL)
1547 udev_device_add_property(udev_device, "DEVNAME", udev_device->devnode);
1551 int udev_device_add_devlink(struct udev_device *udev_device, const char *devlink, int unique)
1553 struct udev_list_entry *list_entry;
1555 udev_device->devlinks_uptodate = false;
1556 list_entry = udev_list_entry_add(&udev_device->devlinks_list, devlink, NULL);
1557 if (list_entry == NULL)
1560 udev_list_entry_set_num(list_entry, true);
1564 const char *udev_device_get_id_filename(struct udev_device *udev_device)
1566 if (udev_device->id_filename == NULL) {
1567 if (udev_device_get_subsystem(udev_device) == NULL)
1570 if (major(udev_device_get_devnum(udev_device)) > 0) {
1571 /* use dev_t -- b259:131072, c254:0 */
1572 if (asprintf(&udev_device->id_filename, "%c%u:%u",
1573 strcmp(udev_device_get_subsystem(udev_device), "block") == 0 ? 'b' : 'c',
1574 major(udev_device_get_devnum(udev_device)),
1575 minor(udev_device_get_devnum(udev_device))) < 0)
1576 udev_device->id_filename = NULL;
1577 } else if (udev_device_get_ifindex(udev_device) > 0) {
1578 /* use netdev ifindex -- n3 */
1579 if (asprintf(&udev_device->id_filename, "n%u", udev_device_get_ifindex(udev_device)) < 0)
1580 udev_device->id_filename = NULL;
1583 * use $subsys:$syname -- pci:0000:00:1f.2
1584 * sysname() has '!' translated, get it from devpath
1586 const char *sysname;
1587 sysname = strrchr(udev_device->devpath, '/');
1588 if (sysname == NULL)
1590 sysname = &sysname[1];
1591 if (asprintf(&udev_device->id_filename, "+%s:%s", udev_device_get_subsystem(udev_device), sysname) < 0)
1592 udev_device->id_filename = NULL;
1595 return udev_device->id_filename;
1599 * udev_device_get_is_initialized:
1600 * @udev_device: udev device
1602 * Check if udev has already handled the device and has set up
1603 * device node permissions and context, or has renamed a network
1606 * This is only implemented for devices with a device node
1607 * or network interfaces. All other devices return 1 here.
1609 * Returns: 1 if the device is set up. 0 otherwise.
1611 UDEV_EXPORT int udev_device_get_is_initialized(struct udev_device *udev_device)
1613 if (!udev_device->info_loaded)
1614 udev_device_read_db(udev_device, NULL);
1615 return udev_device->is_initialized;
1618 void udev_device_set_is_initialized(struct udev_device *udev_device)
1620 udev_device->is_initialized = true;
1623 int udev_device_add_tag(struct udev_device *udev_device, const char *tag)
1625 if (strchr(tag, ':') != NULL || strchr(tag, ' ') != NULL)
1627 udev_device->tags_uptodate = false;
1628 if (udev_list_entry_add(&udev_device->tags_list, tag, NULL) != NULL)
1633 void udev_device_cleanup_tags_list(struct udev_device *udev_device)
1635 udev_device->tags_uptodate = false;
1636 udev_list_cleanup(&udev_device->tags_list);
1640 * udev_device_get_tags_list_entry:
1641 * @udev_device: udev device
1643 * Retrieve the list of tags attached to the udev device. The next
1644 * list entry can be retrieved with udev_list_entry_next(),
1645 * which returns #NULL if no more entries exist. The tag string
1646 * can be retrieved from the list entry by udev_list_get_name().
1648 * Returns: the first entry of the tag list
1650 UDEV_EXPORT struct udev_list_entry *udev_device_get_tags_list_entry(struct udev_device *udev_device)
1652 if (udev_device == NULL)
1654 if (!udev_device->info_loaded)
1655 udev_device_read_db(udev_device, NULL);
1656 return udev_list_get_entry(&udev_device->tags_list);
1659 UDEV_EXPORT int udev_device_has_tag(struct udev_device *udev_device, const char *tag)
1661 struct udev_list_entry *list_entry;
1663 if (udev_device == NULL)
1665 if (!udev_device->info_loaded)
1666 udev_device_read_db(udev_device, NULL);
1667 list_entry = udev_device_get_tags_list_entry(udev_device);
1668 if (udev_list_entry_get_by_name(list_entry, tag) != NULL)
1673 #define ENVP_SIZE 128
1674 #define MONITOR_BUF_SIZE 4096
1675 static int update_envp_monitor_buf(struct udev_device *udev_device)
1677 struct udev_list_entry *list_entry;
1682 /* monitor buffer of property strings */
1683 free(udev_device->monitor_buf);
1684 udev_device->monitor_buf_len = 0;
1685 udev_device->monitor_buf = malloc(MONITOR_BUF_SIZE);
1686 if (udev_device->monitor_buf == NULL)
1689 /* envp array, strings will point into monitor buffer */
1690 if (udev_device->envp == NULL)
1691 udev_device->envp = malloc(sizeof(char *) * ENVP_SIZE);
1692 if (udev_device->envp == NULL)
1696 s = udev_device->monitor_buf;
1697 l = MONITOR_BUF_SIZE;
1698 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device)) {
1701 key = udev_list_entry_get_name(list_entry);
1702 /* skip private variables */
1706 /* add string to envp array */
1707 udev_device->envp[i++] = s;
1708 if (i+1 >= ENVP_SIZE)
1711 /* add property string to monitor buffer */
1712 l = util_strpcpyl(&s, l, key, "=", udev_list_entry_get_value(list_entry), NULL);
1715 /* advance past the trailing '\0' that util_strpcpyl() guarantees */
1719 udev_device->envp[i] = NULL;
1720 udev_device->monitor_buf_len = s - udev_device->monitor_buf;
1721 udev_device->envp_uptodate = true;
1722 dbg(udev_device->udev, "filled envp/monitor buffer, %u properties, %zu bytes\n",
1723 i, udev_device->monitor_buf_len);
1727 char **udev_device_get_properties_envp(struct udev_device *udev_device)
1729 if (!udev_device->envp_uptodate)
1730 if (update_envp_monitor_buf(udev_device) != 0)
1732 return udev_device->envp;
1735 ssize_t udev_device_get_properties_monitor_buf(struct udev_device *udev_device, const char **buf)
1737 if (!udev_device->envp_uptodate)
1738 if (update_envp_monitor_buf(udev_device) != 0)
1740 *buf = udev_device->monitor_buf;
1741 return udev_device->monitor_buf_len;
1744 int udev_device_set_action(struct udev_device *udev_device, const char *action)
1746 free(udev_device->action);
1747 udev_device->action = strdup(action);
1748 if (udev_device->action == NULL)
1750 udev_device_add_property(udev_device, "ACTION", udev_device->action);
1754 int udev_device_get_devlink_priority(struct udev_device *udev_device)
1756 if (!udev_device->info_loaded)
1757 udev_device_read_db(udev_device, NULL);
1758 return udev_device->devlink_priority;
1761 int udev_device_set_devlink_priority(struct udev_device *udev_device, int prio)
1763 udev_device->devlink_priority = prio;
1767 int udev_device_get_watch_handle(struct udev_device *udev_device)
1769 if (!udev_device->info_loaded)
1770 udev_device_read_db(udev_device, NULL);
1771 return udev_device->watch_handle;
1774 int udev_device_set_watch_handle(struct udev_device *udev_device, int handle)
1776 udev_device->watch_handle = handle;
1780 bool udev_device_get_db_persist(struct udev_device *udev_device)
1782 return udev_device->db_persist;
1785 void udev_device_set_db_persist(struct udev_device *udev_device)
1787 udev_device->db_persist = true;