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_node devlinks_list;
66 struct udev_list_node properties_list;
67 struct udev_list_node sysattr_value_list;
68 struct udev_list_node sysattr_list;
69 struct udev_list_node 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_devnum:
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_driver(udev_device->udev, 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_subsystem(udev_device->udev, 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->udev, &udev_device->properties_list, key, value, UDEV_LIST_UNIQUE);
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, "DRIVER=", 7) == 0) {
441 udev_device_set_driver(udev_device, &property[7]);
442 } else if (strncmp(property, "ACTION=", 7) == 0) {
443 udev_device_set_action(udev_device, &property[7]);
444 } else if (strncmp(property, "MAJOR=", 6) == 0) {
445 udev_device->maj = strtoull(&property[6], NULL, 10);
446 } else if (strncmp(property, "MINOR=", 6) == 0) {
447 udev_device->min = strtoull(&property[6], NULL, 10);
448 } else if (strncmp(property, "DEVPATH_OLD=", 12) == 0) {
449 udev_device_set_devpath_old(udev_device, &property[12]);
450 } else if (strncmp(property, "SEQNUM=", 7) == 0) {
451 udev_device_set_seqnum(udev_device, strtoull(&property[7], NULL, 10));
452 } else if (strncmp(property, "TIMEOUT=", 8) == 0) {
453 udev_device_set_timeout(udev_device, strtoull(&property[8], NULL, 10));
454 } else if (strncmp(property, "IFINDEX=", 8) == 0) {
455 udev_device_set_ifindex(udev_device, strtoull(&property[8], NULL, 10));
456 } else if (strncmp(property, "DEVMODE=", 8) == 0) {
457 udev_device_set_devnode_mode(udev_device, strtoul(&property[8], NULL, 8));
459 udev_device_add_property_from_string(udev_device, property);
463 int udev_device_add_property_from_string_parse_finish(struct udev_device *udev_device)
465 if (udev_device->maj > 0)
466 udev_device_set_devnum(udev_device, makedev(udev_device->maj, udev_device->min));
467 udev_device->maj = 0;
468 udev_device->min = 0;
470 if (udev_device->devpath == NULL || udev_device->subsystem == NULL)
476 * udev_device_get_property_value:
477 * @udev_device: udev device
478 * @key: property name
480 * Returns: the value of a device property, or #NULL if there is no such property.
482 UDEV_EXPORT const char *udev_device_get_property_value(struct udev_device *udev_device, const char *key)
484 struct udev_list_entry *list_entry;
486 if (udev_device == NULL)
491 list_entry = udev_device_get_properties_list_entry(udev_device);
492 list_entry = udev_list_entry_get_by_name(list_entry, key);
493 return udev_list_entry_get_value(list_entry);
496 int udev_device_read_db(struct udev_device *udev_device, const char *dbfile)
498 char filename[UTIL_PATH_SIZE];
499 char line[UTIL_LINE_SIZE];
502 /* providing a database file will always force-load it */
503 if (dbfile == NULL) {
506 if (udev_device->db_loaded)
508 udev_device->db_loaded = true;
510 id = udev_device_get_id_filename(udev_device);
513 util_strscpyl(filename, sizeof(filename), udev_get_run_path(udev_device->udev), "/data/", id, NULL);
517 f = fopen(dbfile, "re");
519 info(udev_device->udev, "no db file to read %s: %m\n", dbfile);
522 udev_device->is_initialized = true;
524 while (fgets(line, sizeof(line), f)) {
527 struct udev_list_entry *entry;
536 util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev_device->udev), "/", val, NULL);
537 udev_device_set_devnode(udev_device, filename);
540 util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev_device->udev), "/", val, NULL);
541 udev_device_add_devlink(udev_device, filename, 0);
544 udev_device_set_devlink_priority(udev_device, atoi(val));
547 entry = udev_device_add_property_from_string(udev_device, val);
548 udev_list_entry_set_num(entry, true);
551 udev_device_add_tag(udev_device, val);
554 udev_device_set_watch_handle(udev_device, atoi(val));
557 udev_device_set_usec_initialized(udev_device, strtoull(val, NULL, 10));
563 info(udev_device->udev, "device %p filled with db file data\n", udev_device);
567 int udev_device_read_uevent_file(struct udev_device *udev_device)
569 char filename[UTIL_PATH_SIZE];
571 char line[UTIL_LINE_SIZE];
575 if (udev_device->uevent_loaded)
578 util_strscpyl(filename, sizeof(filename), udev_device->syspath, "/uevent", NULL);
579 f = fopen(filename, "re");
582 udev_device->uevent_loaded = true;
584 while (fgets(line, sizeof(line), f)) {
587 pos = strchr(line, '\n');
592 if (strncmp(line, "DEVTYPE=", 8) == 0)
593 udev_device_set_devtype(udev_device, &line[8]);
594 else if (strncmp(line, "MAJOR=", 6) == 0)
595 maj = strtoull(&line[6], NULL, 10);
596 else if (strncmp(line, "MINOR=", 6) == 0)
597 min = strtoull(&line[6], NULL, 10);
598 else if (strncmp(line, "IFINDEX=", 8) == 0)
599 udev_device_set_ifindex(udev_device, strtoull(&line[8], NULL, 10));
600 else if (strncmp(line, "DEVNAME=", 8) == 0)
601 udev_device_set_knodename(udev_device, &line[8]);
602 else if (strncmp(line, "DEVMODE=", 8) == 0)
603 udev_device->devnode_mode = strtoul(&line[8], NULL, 8);
605 udev_device_add_property_from_string(udev_device, line);
608 udev_device->devnum = makedev(maj, min);
613 void udev_device_set_info_loaded(struct udev_device *device)
615 device->info_loaded = true;
618 struct udev_device *udev_device_new(struct udev *udev)
620 struct udev_device *udev_device;
621 struct udev_list_entry *list_entry;
626 udev_device = calloc(1, sizeof(struct udev_device));
627 if (udev_device == NULL)
629 udev_device->refcount = 1;
630 udev_device->udev = udev;
631 udev_list_init(&udev_device->devlinks_list);
632 udev_list_init(&udev_device->properties_list);
633 udev_list_init(&udev_device->sysattr_value_list);
634 udev_list_init(&udev_device->sysattr_list);
635 udev_list_init(&udev_device->tags_list);
636 udev_device->timeout = -1;
637 udev_device->watch_handle = -1;
638 /* copy global properties */
639 udev_list_entry_foreach(list_entry, udev_get_properties_list_entry(udev))
640 udev_device_add_property(udev_device,
641 udev_list_entry_get_name(list_entry),
642 udev_list_entry_get_value(list_entry));
643 dbg(udev_device->udev, "udev_device: %p created\n", udev_device);
648 * udev_device_new_from_syspath:
649 * @udev: udev library context
650 * @syspath: sys device path including sys directory
652 * Create new udev device, and fill in information from the sys
653 * device and the udev database entry. The syspath is the absolute
654 * path to the device, including the sys mount point.
656 * The initial refcount is 1, and needs to be decremented to
657 * release the resources of the udev device.
659 * Returns: a new udev device, or #NULL, if it does not exist
661 UDEV_EXPORT struct udev_device *udev_device_new_from_syspath(struct udev *udev, const char *syspath)
665 char path[UTIL_PATH_SIZE];
668 struct udev_device *udev_device;
675 /* path starts in sys */
676 len = strlen(udev_get_sys_path(udev));
677 if (strncmp(syspath, udev_get_sys_path(udev), len) != 0) {
678 info(udev, "not in sys :%s\n", syspath);
682 /* path is not a root directory */
683 subdir = &syspath[len+1];
684 pos = strrchr(subdir, '/');
685 if (pos == NULL || pos[1] == '\0' || pos < &subdir[2]) {
686 dbg(udev, "not a subdir :%s\n", syspath);
690 /* resolve possible symlink to real path */
691 util_strscpy(path, sizeof(path), syspath);
692 util_resolve_sys_link(udev, path, sizeof(path));
694 if (strncmp(&path[len], "/devices/", 9) == 0) {
695 char file[UTIL_PATH_SIZE];
697 /* all "devices" require a "uevent" file */
698 util_strscpyl(file, sizeof(file), path, "/uevent", NULL);
699 if (stat(file, &statbuf) != 0) {
700 dbg(udev, "not a device: %s\n", syspath);
704 /* everything else just needs to be a directory */
705 if (stat(path, &statbuf) != 0 || !S_ISDIR(statbuf.st_mode)) {
706 dbg(udev, "directory not found: %s\n", syspath);
711 udev_device = udev_device_new(udev);
712 if (udev_device == NULL)
715 udev_device_set_syspath(udev_device, path);
716 info(udev, "device %p has devpath '%s'\n", udev_device, udev_device_get_devpath(udev_device));
722 * udev_device_new_from_devnum:
723 * @udev: udev library context
724 * @type: char or block device
725 * @devnum: device major/minor number
727 * Create new udev device, and fill in information from the sys
728 * device and the udev database entry. The device is looked-up
729 * by its major/minor number and type. Character and block device
730 * numbers are not unique across the two types.
732 * The initial refcount is 1, and needs to be decremented to
733 * release the resources of the udev device.
735 * Returns: a new udev device, or #NULL, if it does not exist
737 UDEV_EXPORT struct udev_device *udev_device_new_from_devnum(struct udev *udev, char type, dev_t devnum)
739 char path[UTIL_PATH_SIZE];
740 const char *type_str;
744 else if (type == 'c')
749 /* use /sys/dev/{block,char}/<maj>:<min> link */
750 snprintf(path, sizeof(path), "%s/dev/%s/%u:%u",
751 udev_get_sys_path(udev), type_str, major(devnum), minor(devnum));
752 return udev_device_new_from_syspath(udev, path);
755 struct udev_device *udev_device_new_from_id_filename(struct udev *udev, char *id)
759 char subsys[UTIL_PATH_SIZE];
765 if (sscanf(id, "%c%i:%i", &type, &maj, &min) != 3)
767 return udev_device_new_from_devnum(udev, type, makedev(maj, min));
771 struct udev_device *dev;
774 ifindex = strtoul(&id[1], NULL, 10);
778 sk = socket(PF_INET, SOCK_DGRAM, 0);
781 memset(&ifr, 0x00, sizeof(struct ifreq));
782 ifr.ifr_ifindex = ifindex;
783 if (ioctl(sk, SIOCGIFNAME, &ifr) != 0) {
789 dev = udev_device_new_from_subsystem_sysname(udev, "net", ifr.ifr_name);
792 if (udev_device_get_ifindex(dev) == ifindex)
794 udev_device_unref(dev);
798 util_strscpy(subsys, sizeof(subsys), &id[1]);
799 sysname = strchr(subsys, ':');
803 sysname = &sysname[1];
804 return udev_device_new_from_subsystem_sysname(udev, subsys, sysname);
811 * udev_device_new_from_subsystem_sysname:
812 * @udev: udev library context
813 * @subsystem: the subsystem of the device
814 * @sysname: the name of the device
816 * Create new udev device, and fill in information from the sys device
817 * and the udev database entry. The device is looked up by the subsystem
818 * and name string of the device, like "mem" / "zero", or "block" / "sda".
820 * The initial refcount is 1, and needs to be decremented to
821 * release the resources of the udev device.
823 * Returns: a new udev device, or #NULL, if it does not exist
825 UDEV_EXPORT struct udev_device *udev_device_new_from_subsystem_sysname(struct udev *udev, const char *subsystem, const char *sysname)
827 char path_full[UTIL_PATH_SIZE];
833 l = util_strpcpyl(&path, sizeof(path_full), udev_get_sys_path(udev), NULL);
835 if (strcmp(subsystem, "subsystem") == 0) {
836 util_strscpyl(path, l, "/subsystem/", sysname, NULL);
837 if (stat(path_full, &statbuf) == 0)
840 util_strscpyl(path, l, "/bus/", sysname, NULL);
841 if (stat(path_full, &statbuf) == 0)
844 util_strscpyl(path, l, "/class/", sysname, NULL);
845 if (stat(path_full, &statbuf) == 0)
850 if (strcmp(subsystem, "module") == 0) {
851 util_strscpyl(path, l, "/module/", sysname, NULL);
852 if (stat(path_full, &statbuf) == 0)
857 if (strcmp(subsystem, "drivers") == 0) {
858 char subsys[UTIL_NAME_SIZE];
861 util_strscpy(subsys, sizeof(subsys), sysname);
862 driver = strchr(subsys, ':');
863 if (driver != NULL) {
867 util_strscpyl(path, l, "/subsystem/", subsys, "/drivers/", driver, NULL);
868 if (stat(path_full, &statbuf) == 0)
871 util_strscpyl(path, l, "/bus/", subsys, "/drivers/", driver, NULL);
872 if (stat(path_full, &statbuf) == 0)
878 util_strscpyl(path, l, "/subsystem/", subsystem, "/devices/", sysname, NULL);
879 if (stat(path_full, &statbuf) == 0)
882 util_strscpyl(path, l, "/bus/", subsystem, "/devices/", sysname, NULL);
883 if (stat(path_full, &statbuf) == 0)
886 util_strscpyl(path, l, "/class/", subsystem, "/", sysname, NULL);
887 if (stat(path_full, &statbuf) == 0)
892 return udev_device_new_from_syspath(udev, path_full);
896 * udev_device_new_from_environment
897 * @udev: udev library context
899 * Create new udev device, and fill in information from the
900 * current process environment. This only works reliable if
901 * the process is called from a udev rule. It is usually used
902 * for tools executed from IMPORT= rules.
904 * The initial refcount is 1, and needs to be decremented to
905 * release the resources of the udev device.
907 * Returns: a new udev device, or #NULL, if it does not exist
909 UDEV_EXPORT struct udev_device *udev_device_new_from_environment(struct udev *udev)
912 struct udev_device *udev_device;
914 udev_device = udev_device_new(udev);
915 if (udev_device == NULL)
917 udev_device_set_info_loaded(udev_device);
919 for (i = 0; environ[i] != NULL; i++)
920 udev_device_add_property_from_string_parse(udev_device, environ[i]);
922 if (udev_device_add_property_from_string_parse_finish(udev_device) < 0) {
923 info(udev, "missing values, invalid device\n");
924 udev_device_unref(udev_device);
931 static struct udev_device *device_new_from_parent(struct udev_device *udev_device)
933 struct udev_device *udev_device_parent = NULL;
934 char path[UTIL_PATH_SIZE];
937 util_strscpy(path, sizeof(path), udev_device->syspath);
938 subdir = &path[strlen(udev_get_sys_path(udev_device->udev))+1];
942 pos = strrchr(subdir, '/');
943 if (pos == NULL || pos < &subdir[2])
946 udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
947 if (udev_device_parent != NULL)
948 return udev_device_parent;
954 * udev_device_get_parent:
955 * @udev_device: the device to start searching from
957 * Find the next parent device, and fill in information from the sys
958 * device and the udev database entry.
960 * The returned the device is not referenced. It is attached to the
961 * child device, and will be cleaned up when the child device
964 * It is not necessarily just the upper level directory, empty or not
965 * recognized sys directories are ignored.
967 * It can be called as many times as needed, without caring about
970 * Returns: a new udev device, or #NULL, if it no parent exist.
972 UDEV_EXPORT struct udev_device *udev_device_get_parent(struct udev_device *udev_device)
974 if (udev_device == NULL)
976 if (!udev_device->parent_set) {
977 udev_device->parent_set = true;
978 udev_device->parent_device = device_new_from_parent(udev_device);
980 if (udev_device->parent_device != NULL)
981 dbg(udev_device->udev, "returning existing parent %p\n", udev_device->parent_device);
982 return udev_device->parent_device;
986 * udev_device_get_parent_with_subsystem_devtype:
987 * @udev_device: udev device to start searching from
988 * @subsystem: the subsystem of the device
989 * @devtype: the type (DEVTYPE) of the device
991 * Find the next parent device, with a matching subsystem and devtype
992 * value, and fill in information from the sys device and the udev
995 * If devtype is #NULL, only subsystem is checked, and any devtype will
998 * The returned the device is not referenced. It is attached to the
999 * child device, and will be cleaned up when the child device
1002 * It can be called as many times as needed, without caring about
1005 * Returns: a new udev device, or #NULL if no matching parent exists.
1007 UDEV_EXPORT struct udev_device *udev_device_get_parent_with_subsystem_devtype(struct udev_device *udev_device, const char *subsystem, const char *devtype)
1009 struct udev_device *parent;
1011 if (subsystem == NULL)
1014 parent = udev_device_get_parent(udev_device);
1015 while (parent != NULL) {
1016 const char *parent_subsystem;
1017 const char *parent_devtype;
1019 parent_subsystem = udev_device_get_subsystem(parent);
1020 if (parent_subsystem != NULL && strcmp(parent_subsystem, subsystem) == 0) {
1021 if (devtype == NULL)
1023 parent_devtype = udev_device_get_devtype(parent);
1024 if (parent_devtype != NULL && strcmp(parent_devtype, devtype) == 0)
1027 parent = udev_device_get_parent(parent);
1033 * udev_device_get_udev:
1034 * @udev_device: udev device
1036 * Retrieve the udev library context the device was created with.
1038 * Returns: the udev library context
1040 UDEV_EXPORT struct udev *udev_device_get_udev(struct udev_device *udev_device)
1042 if (udev_device == NULL)
1044 return udev_device->udev;
1049 * @udev_device: udev device
1051 * Take a reference of a udev device.
1053 * Returns: the passed udev device
1055 UDEV_EXPORT struct udev_device *udev_device_ref(struct udev_device *udev_device)
1057 if (udev_device == NULL)
1059 udev_device->refcount++;
1064 * udev_device_unref:
1065 * @udev_device: udev device
1067 * Drop a reference of a udev device. If the refcount reaches zero,
1068 * the resources of the device will be released.
1071 UDEV_EXPORT void udev_device_unref(struct udev_device *udev_device)
1073 if (udev_device == NULL)
1075 udev_device->refcount--;
1076 if (udev_device->refcount > 0)
1078 if (udev_device->parent_device != NULL)
1079 udev_device_unref(udev_device->parent_device);
1080 free(udev_device->syspath);
1081 free(udev_device->sysname);
1082 free(udev_device->devnode);
1083 free(udev_device->subsystem);
1084 free(udev_device->devtype);
1085 udev_list_cleanup_entries(udev_device->udev, &udev_device->devlinks_list);
1086 udev_list_cleanup_entries(udev_device->udev, &udev_device->properties_list);
1087 udev_list_cleanup_entries(udev_device->udev, &udev_device->sysattr_value_list);
1088 udev_list_cleanup_entries(udev_device->udev, &udev_device->sysattr_list);
1089 udev_list_cleanup_entries(udev_device->udev, &udev_device->tags_list);
1090 free(udev_device->action);
1091 free(udev_device->driver);
1092 free(udev_device->devpath_old);
1093 free(udev_device->knodename);
1094 free(udev_device->id_filename);
1095 free(udev_device->envp);
1096 free(udev_device->monitor_buf);
1097 dbg(udev_device->udev, "udev_device: %p released\n", udev_device);
1102 * udev_device_get_devpath:
1103 * @udev_device: udev device
1105 * Retrieve the kernel devpath value of the udev device. The path
1106 * does not contain the sys mount point, and starts with a '/'.
1108 * Returns: the devpath of the udev device
1110 UDEV_EXPORT const char *udev_device_get_devpath(struct udev_device *udev_device)
1112 if (udev_device == NULL)
1114 return udev_device->devpath;
1118 * udev_device_get_syspath:
1119 * @udev_device: udev device
1121 * Retrieve the sys path of the udev device. The path is an
1122 * absolute path and starts with the sys mount point.
1124 * Returns: the sys path of the udev device
1126 UDEV_EXPORT const char *udev_device_get_syspath(struct udev_device *udev_device)
1128 if (udev_device == NULL)
1130 return udev_device->syspath;
1134 * udev_device_get_sysname:
1135 * @udev_device: udev device
1137 * Returns: the sys name of the device device
1139 UDEV_EXPORT const char *udev_device_get_sysname(struct udev_device *udev_device)
1141 if (udev_device == NULL)
1143 return udev_device->sysname;
1147 * udev_device_get_sysnum:
1148 * @udev_device: udev device
1150 * Returns: the trailing number of of the device name
1152 UDEV_EXPORT const char *udev_device_get_sysnum(struct udev_device *udev_device)
1154 if (udev_device == NULL)
1156 return udev_device->sysnum;
1160 * udev_device_get_devnode:
1161 * @udev_device: udev device
1163 * Retrieve the device node file name belonging to the udev device.
1164 * The path is an absolute path, and starts with the device directory.
1166 * Returns: the device node file name of the udev device, or #NULL if no device node exists
1168 UDEV_EXPORT const char *udev_device_get_devnode(struct udev_device *udev_device)
1170 if (udev_device == NULL)
1172 if (!udev_device->info_loaded) {
1173 udev_device_read_uevent_file(udev_device);
1174 udev_device_read_db(udev_device, NULL);
1177 /* we might get called before we handled an event and have a db, use the kernel-provided name */
1178 if (udev_device->devnode == NULL && udev_device_get_knodename(udev_device) != NULL) {
1179 char filename[UTIL_NAME_SIZE];
1181 util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev_device->udev), "/",
1182 udev_device_get_knodename(udev_device), NULL);
1183 udev_device_set_devnode(udev_device, filename);
1184 return udev_device->devnode;
1187 return udev_device->devnode;
1191 * udev_device_get_devlinks_list_entry:
1192 * @udev_device: udev device
1194 * Retrieve the list of device links pointing to the device file of
1195 * the udev device. The next list entry can be retrieved with
1196 * udev_list_entry_next(), which returns #NULL if no more entries exist.
1197 * The devlink path can be retrieved from the list entry by
1198 * udev_list_entry_get_name(). The path is an absolute path, and starts with
1199 * the device directory.
1201 * Returns: the first entry of the device node link list
1203 UDEV_EXPORT struct udev_list_entry *udev_device_get_devlinks_list_entry(struct udev_device *udev_device)
1205 if (udev_device == NULL)
1207 if (!udev_device->info_loaded)
1208 udev_device_read_db(udev_device, NULL);
1209 return udev_list_get_entry(&udev_device->devlinks_list);
1212 void udev_device_cleanup_devlinks_list(struct udev_device *udev_device)
1214 udev_device->devlinks_uptodate = false;
1215 udev_list_cleanup_entries(udev_device->udev, &udev_device->devlinks_list);
1219 * udev_device_get_properties_list_entry:
1220 * @udev_device: udev device
1222 * Retrieve the list of key/value device properties of the udev
1223 * device. The next list entry can be retrieved with udev_list_entry_next(),
1224 * which returns #NULL if no more entries exist. The property name
1225 * can be retrieved from the list entry by udev_list_get_name(),
1226 * the property value by udev_list_get_value().
1228 * Returns: the first entry of the property list
1230 UDEV_EXPORT struct udev_list_entry *udev_device_get_properties_list_entry(struct udev_device *udev_device)
1232 if (udev_device == NULL)
1234 if (!udev_device->info_loaded) {
1235 udev_device_read_uevent_file(udev_device);
1236 udev_device_read_db(udev_device, NULL);
1238 if (!udev_device->devlinks_uptodate) {
1239 char symlinks[UTIL_PATH_SIZE];
1240 struct udev_list_entry *list_entry;
1242 udev_device->devlinks_uptodate = true;
1243 list_entry = udev_device_get_devlinks_list_entry(udev_device);
1244 if (list_entry != NULL) {
1249 l = util_strpcpyl(&s, sizeof(symlinks), udev_list_entry_get_name(list_entry), NULL);
1250 udev_list_entry_foreach(list_entry, udev_list_entry_get_next(list_entry))
1251 l = util_strpcpyl(&s, l, " ", udev_list_entry_get_name(list_entry), NULL);
1252 udev_device_add_property(udev_device, "DEVLINKS", symlinks);
1255 if (!udev_device->tags_uptodate) {
1256 udev_device->tags_uptodate = true;
1257 if (udev_device_get_tags_list_entry(udev_device) != NULL) {
1258 char tags[UTIL_PATH_SIZE];
1259 struct udev_list_entry *list_entry;
1264 l = util_strpcpyl(&s, sizeof(tags), ":", NULL);
1265 udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(udev_device))
1266 l = util_strpcpyl(&s, l, udev_list_entry_get_name(list_entry), ":", NULL);
1267 udev_device_add_property(udev_device, "TAGS", tags);
1270 return udev_list_get_entry(&udev_device->properties_list);
1274 * udev_device_get_action:
1275 * @udev_device: udev device
1277 * This is only valid if the device was received through a monitor. Devices read from
1278 * sys do not have an action string. Usual actions are: add, remove, change, online,
1281 * Returns: the kernel action value, or #NULL if there is no action value available.
1283 UDEV_EXPORT const char *udev_device_get_action(struct udev_device *udev_device)
1285 if (udev_device == NULL)
1287 return udev_device->action;
1291 * udev_device_get_usec_since_initialized:
1292 * @udev_device: udev device
1294 * Return the number of microseconds passed since udev set up the
1295 * device for the first time.
1297 * This is only implemented for devices with need to store properties
1298 * in the udev database. All other devices return 0 here.
1300 * Returns: the number of microseconds since the device was first seen.
1302 UDEV_EXPORT unsigned long long int udev_device_get_usec_since_initialized(struct udev_device *udev_device)
1304 unsigned long long now;
1306 if (udev_device == NULL)
1308 if (!udev_device->info_loaded)
1309 udev_device_read_db(udev_device, NULL);
1310 if (udev_device->usec_initialized == 0)
1315 return now - udev_device->usec_initialized;
1318 unsigned long long udev_device_get_usec_initialized(struct udev_device *udev_device)
1320 return udev_device->usec_initialized;
1323 void udev_device_set_usec_initialized(struct udev_device *udev_device, unsigned long long usec_initialized)
1325 udev_device->usec_initialized = usec_initialized;
1329 * udev_device_get_sysattr_value:
1330 * @udev_device: udev device
1331 * @sysattr: attribute name
1333 * The retrieved value is cached in the device. Repeated calls will return the same
1334 * value and not open the attribute again.
1336 * Returns: the content of a sys attribute file, or #NULL if there is no sys attribute value.
1338 UDEV_EXPORT const char *udev_device_get_sysattr_value(struct udev_device *udev_device, const char *sysattr)
1340 struct udev_list_entry *list_entry;
1341 char path[UTIL_PATH_SIZE];
1343 struct stat statbuf;
1346 const char *val = NULL;
1348 if (udev_device == NULL)
1350 if (sysattr == NULL)
1353 /* look for possibly already cached result */
1354 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_device->sysattr_value_list)) {
1355 if (strcmp(udev_list_entry_get_name(list_entry), sysattr) == 0) {
1356 dbg(udev_device->udev, "got '%s' (%s) from cache\n",
1357 sysattr, udev_list_entry_get_value(list_entry));
1358 return udev_list_entry_get_value(list_entry);
1362 util_strscpyl(path, sizeof(path), udev_device_get_syspath(udev_device), "/", sysattr, NULL);
1363 if (lstat(path, &statbuf) != 0) {
1364 dbg(udev_device->udev, "no attribute '%s', keep negative entry\n", path);
1365 udev_list_entry_add(udev_device->udev, &udev_device->sysattr_value_list, sysattr, NULL, 0);
1369 if (S_ISLNK(statbuf.st_mode)) {
1370 char target[UTIL_NAME_SIZE];
1374 /* some core links return the last element of the target path */
1375 if (strcmp(sysattr, "driver") != 0 &&
1376 strcmp(sysattr, "subsystem") != 0 &&
1377 strcmp(sysattr, "module") != 0)
1380 len = readlink(path, target, sizeof(target));
1381 if (len <= 0 || len == sizeof(target))
1385 pos = strrchr(target, '/');
1388 dbg(udev_device->udev, "cache '%s' with link value '%s'\n", sysattr, pos);
1389 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->sysattr_value_list, sysattr, pos, 0);
1390 val = udev_list_entry_get_value(list_entry);
1396 /* skip directories */
1397 if (S_ISDIR(statbuf.st_mode))
1400 /* skip non-readable files */
1401 if ((statbuf.st_mode & S_IRUSR) == 0)
1404 /* read attribute value */
1405 fd = open(path, O_RDONLY|O_CLOEXEC);
1407 dbg(udev_device->udev, "attribute '%s' can not be opened\n", path);
1410 size = read(fd, value, sizeof(value));
1414 if (size == sizeof(value))
1417 /* got a valid value, store it in cache and return it */
1419 util_remove_trailing_chars(value, '\n');
1420 dbg(udev_device->udev, "'%s' has attribute value '%s'\n", path, value);
1421 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->sysattr_value_list, sysattr, value, 0);
1422 val = udev_list_entry_get_value(list_entry);
1427 static int udev_device_sysattr_list_read(struct udev_device *udev_device)
1429 struct dirent *dent;
1433 if (udev_device == NULL)
1435 if (udev_device->sysattr_list_read)
1438 dir = opendir(udev_device_get_syspath(udev_device));
1440 dbg(udev_device->udev, "sysfs dir '%s' can not be opened\n",
1441 udev_device_get_syspath(udev_device));
1445 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
1446 char path[UTIL_PATH_SIZE];
1447 struct stat statbuf;
1449 /* only handle symlinks and regular files */
1450 if (dent->d_type != DT_LNK && dent->d_type != DT_REG)
1453 util_strscpyl(path, sizeof(path), udev_device_get_syspath(udev_device), "/", dent->d_name, NULL);
1454 if (lstat(path, &statbuf) != 0)
1456 if ((statbuf.st_mode & S_IRUSR) == 0)
1459 udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list,
1460 dent->d_name, NULL, 0);
1465 dbg(udev_device->udev, "found %d sysattrs for '%s'\n", num, udev_device_get_syspath(udev_device));
1466 udev_device->sysattr_list_read = true;
1472 * udev_device_get_sysattr_list_entry:
1473 * @udev_device: udev device
1475 * Retrieve the list of available sysattrs, with value being empty;
1476 * This just return all available sysfs attributes for a particular
1477 * device without reading their values.
1479 * Returns: the first entry of the property list
1481 UDEV_EXPORT struct udev_list_entry *udev_device_get_sysattr_list_entry(struct udev_device *udev_device)
1483 if (!udev_device->sysattr_list_read) {
1485 ret = udev_device_sysattr_list_read(udev_device);
1490 return udev_list_get_entry(&udev_device->sysattr_list);
1493 int udev_device_set_syspath(struct udev_device *udev_device, const char *syspath)
1498 free(udev_device->syspath);
1499 udev_device->syspath = strdup(syspath);
1500 if (udev_device->syspath == NULL)
1502 udev_device->devpath = &udev_device->syspath[strlen(udev_get_sys_path(udev_device->udev))];
1503 udev_device_add_property(udev_device, "DEVPATH", udev_device->devpath);
1505 pos = strrchr(udev_device->syspath, '/');
1508 udev_device->sysname = strdup(&pos[1]);
1509 if (udev_device->sysname == NULL)
1512 /* some devices have '!' in their name, change that to '/' */
1514 while (udev_device->sysname[len] != '\0') {
1515 if (udev_device->sysname[len] == '!')
1516 udev_device->sysname[len] = '/';
1520 /* trailing number */
1521 while (len > 0 && isdigit(udev_device->sysname[--len]))
1522 udev_device->sysnum = &udev_device->sysname[len];
1524 /* sysname is completely numeric */
1526 udev_device->sysnum = NULL;
1531 int udev_device_set_devnode(struct udev_device *udev_device, const char *devnode)
1533 free(udev_device->devnode);
1534 udev_device->devnode = strdup(devnode);
1535 if (udev_device->devnode == NULL)
1537 udev_device_add_property(udev_device, "DEVNAME", udev_device->devnode);
1541 int udev_device_add_devlink(struct udev_device *udev_device, const char *devlink, int unique)
1543 struct udev_list_entry *list_entry;
1545 udev_device->devlinks_uptodate = false;
1546 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->devlinks_list, devlink, NULL, UDEV_LIST_UNIQUE);
1547 if (list_entry == NULL)
1550 udev_list_entry_set_num(list_entry, true);
1554 const char *udev_device_get_id_filename(struct udev_device *udev_device)
1556 if (udev_device->id_filename == NULL) {
1557 if (udev_device_get_subsystem(udev_device) == NULL)
1560 if (major(udev_device_get_devnum(udev_device)) > 0) {
1561 /* use dev_t -- b259:131072, c254:0 */
1562 if (asprintf(&udev_device->id_filename, "%c%u:%u",
1563 strcmp(udev_device_get_subsystem(udev_device), "block") == 0 ? 'b' : 'c',
1564 major(udev_device_get_devnum(udev_device)),
1565 minor(udev_device_get_devnum(udev_device))) < 0)
1566 udev_device->id_filename = NULL;
1567 } else if (udev_device_get_ifindex(udev_device) > 0) {
1568 /* use netdev ifindex -- n3 */
1569 if (asprintf(&udev_device->id_filename, "n%u", udev_device_get_ifindex(udev_device)) < 0)
1570 udev_device->id_filename = NULL;
1573 * use $subsys:$syname -- pci:0000:00:1f.2
1574 * sysname() has '!' translated, get it from devpath
1576 const char *sysname;
1577 sysname = strrchr(udev_device->devpath, '/');
1578 if (sysname == NULL)
1580 sysname = &sysname[1];
1581 if (asprintf(&udev_device->id_filename, "+%s:%s", udev_device_get_subsystem(udev_device), sysname) < 0)
1582 udev_device->id_filename = NULL;
1585 return udev_device->id_filename;
1589 * udev_device_get_is_initialized:
1590 * @udev_device: udev device
1592 * Check if udev has already handled the device and has set up
1593 * device node permissions and context, or has renamed a network
1596 * This is only implemented for devices with a device node
1597 * or network interfaces. All other devices return 1 here.
1599 * Returns: 1 if the device is set up. 0 otherwise.
1601 UDEV_EXPORT int udev_device_get_is_initialized(struct udev_device *udev_device)
1603 if (!udev_device->info_loaded)
1604 udev_device_read_db(udev_device, NULL);
1605 return udev_device->is_initialized;
1608 void udev_device_set_is_initialized(struct udev_device *udev_device)
1610 udev_device->is_initialized = true;
1613 int udev_device_add_tag(struct udev_device *udev_device, const char *tag)
1615 if (strchr(tag, ':') != NULL || strchr(tag, ' ') != NULL)
1617 udev_device->tags_uptodate = false;
1618 if (udev_list_entry_add(udev_device->udev, &udev_device->tags_list, tag, NULL, UDEV_LIST_UNIQUE) != NULL)
1623 void udev_device_cleanup_tags_list(struct udev_device *udev_device)
1625 udev_device->tags_uptodate = false;
1626 udev_list_cleanup_entries(udev_device->udev, &udev_device->tags_list);
1630 * udev_device_get_tags_list_entry:
1631 * @udev_device: udev device
1633 * Retrieve the list of tags attached to the udev device. The next
1634 * list entry can be retrieved with udev_list_entry_next(),
1635 * which returns #NULL if no more entries exist. The tag string
1636 * can be retrieved from the list entry by udev_list_get_name().
1638 * Returns: the first entry of the tag list
1640 UDEV_EXPORT struct udev_list_entry *udev_device_get_tags_list_entry(struct udev_device *udev_device)
1642 if (udev_device == NULL)
1644 if (!udev_device->info_loaded)
1645 udev_device_read_db(udev_device, NULL);
1646 return udev_list_get_entry(&udev_device->tags_list);
1649 UDEV_EXPORT int udev_device_has_tag(struct udev_device *udev_device, const char *tag)
1651 struct udev_list_entry *list_entry;
1653 if (udev_device == NULL)
1655 if (!udev_device->info_loaded)
1656 udev_device_read_db(udev_device, NULL);
1657 list_entry = udev_device_get_tags_list_entry(udev_device);
1658 if (udev_list_entry_get_by_name(list_entry, tag) != NULL)
1663 #define ENVP_SIZE 128
1664 #define MONITOR_BUF_SIZE 4096
1665 static int update_envp_monitor_buf(struct udev_device *udev_device)
1667 struct udev_list_entry *list_entry;
1672 /* monitor buffer of property strings */
1673 free(udev_device->monitor_buf);
1674 udev_device->monitor_buf_len = 0;
1675 udev_device->monitor_buf = malloc(MONITOR_BUF_SIZE);
1676 if (udev_device->monitor_buf == NULL)
1679 /* envp array, strings will point into monitor buffer */
1680 if (udev_device->envp == NULL)
1681 udev_device->envp = malloc(sizeof(char *) * ENVP_SIZE);
1682 if (udev_device->envp == NULL)
1686 s = udev_device->monitor_buf;
1687 l = MONITOR_BUF_SIZE;
1688 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device)) {
1691 key = udev_list_entry_get_name(list_entry);
1692 /* skip private variables */
1696 /* add string to envp array */
1697 udev_device->envp[i++] = s;
1698 if (i+1 >= ENVP_SIZE)
1701 /* add property string to monitor buffer */
1702 l = util_strpcpyl(&s, l, key, "=", udev_list_entry_get_value(list_entry), NULL);
1705 /* advance past the trailing '\0' that util_strpcpyl() guarantees */
1709 udev_device->envp[i] = NULL;
1710 udev_device->monitor_buf_len = s - udev_device->monitor_buf;
1711 udev_device->envp_uptodate = true;
1712 dbg(udev_device->udev, "filled envp/monitor buffer, %u properties, %zu bytes\n",
1713 i, udev_device->monitor_buf_len);
1717 char **udev_device_get_properties_envp(struct udev_device *udev_device)
1719 if (!udev_device->envp_uptodate)
1720 if (update_envp_monitor_buf(udev_device) != 0)
1722 return udev_device->envp;
1725 ssize_t udev_device_get_properties_monitor_buf(struct udev_device *udev_device, const char **buf)
1727 if (!udev_device->envp_uptodate)
1728 if (update_envp_monitor_buf(udev_device) != 0)
1730 *buf = udev_device->monitor_buf;
1731 return udev_device->monitor_buf_len;
1734 int udev_device_set_action(struct udev_device *udev_device, const char *action)
1736 free(udev_device->action);
1737 udev_device->action = strdup(action);
1738 if (udev_device->action == NULL)
1740 udev_device_add_property(udev_device, "ACTION", udev_device->action);
1744 int udev_device_get_devlink_priority(struct udev_device *udev_device)
1746 if (!udev_device->info_loaded)
1747 udev_device_read_db(udev_device, NULL);
1748 return udev_device->devlink_priority;
1751 int udev_device_set_devlink_priority(struct udev_device *udev_device, int prio)
1753 udev_device->devlink_priority = prio;
1757 int udev_device_get_watch_handle(struct udev_device *udev_device)
1759 if (!udev_device->info_loaded)
1760 udev_device_read_db(udev_device, NULL);
1761 return udev_device->watch_handle;
1764 int udev_device_set_watch_handle(struct udev_device *udev_device, int handle)
1766 udev_device->watch_handle = handle;
1770 bool udev_device_get_db_persist(struct udev_device *udev_device)
1772 return udev_device->db_persist;
1775 void udev_device_set_db_persist(struct udev_device *udev_device)
1777 udev_device->db_persist = true;