2 This file is part of systemd.
4 Copyright 2008-2012 Kay Sievers <kay@vrfy.org>
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
32 #include <sys/ioctl.h>
33 #include <sys/socket.h>
34 #include <linux/sockios.h>
37 #include "libudev-private.h"
39 static int udev_device_set_devnode(struct udev_device *udev_device, const char *devnode);
40 static struct udev_list_entry *udev_device_add_property_internal(struct udev_device *udev_device, const char *key, const char *value);
43 * SECTION:libudev-device
44 * @short_description: kernel sys devices
46 * Representation of kernel sys devices. Devices are uniquely identified
47 * by their syspath, every device has exactly one path in the kernel sys
48 * filesystem. Devices usually belong to a kernel subsystem, and have
49 * a unique name inside that subsystem.
55 * Opaque object representing one kernel sys device.
59 struct udev_device *parent_device;
76 size_t monitor_buf_len;
77 struct udev_list devlinks_list;
78 struct udev_list properties_list;
79 struct udev_list sysattr_value_list;
80 struct udev_list sysattr_list;
81 struct udev_list tags_list;
82 unsigned long long int seqnum;
83 usec_t usec_initialized;
93 bool devlinks_uptodate;
101 bool sysattr_list_read;
106 * udev_device_get_seqnum:
107 * @udev_device: udev device
109 * This is only valid if the device was received through a monitor. Devices read from
110 * sys do not have a sequence number.
112 * Returns: the kernel event sequence number, or 0 if there is no sequence number available.
114 _public_ unsigned long long int udev_device_get_seqnum(struct udev_device *udev_device)
116 if (udev_device == NULL)
118 return udev_device->seqnum;
121 static int udev_device_set_seqnum(struct udev_device *udev_device, unsigned long long int seqnum)
125 udev_device->seqnum = seqnum;
126 snprintf(num, sizeof(num), "%llu", seqnum);
127 udev_device_add_property_internal(udev_device, "SEQNUM", num);
131 int udev_device_get_ifindex(struct udev_device *udev_device)
133 if (!udev_device->info_loaded)
134 udev_device_read_uevent_file(udev_device);
135 return udev_device->ifindex;
138 static int udev_device_set_ifindex(struct udev_device *udev_device, int ifindex)
142 udev_device->ifindex = ifindex;
143 snprintf(num, sizeof(num), "%d", ifindex);
144 udev_device_add_property_internal(udev_device, "IFINDEX", num);
149 * udev_device_get_devnum:
150 * @udev_device: udev device
152 * Get the device major/minor number.
154 * Returns: the dev_t number.
156 _public_ dev_t udev_device_get_devnum(struct udev_device *udev_device)
158 if (udev_device == NULL)
159 return makedev(0, 0);
160 if (!udev_device->info_loaded)
161 udev_device_read_uevent_file(udev_device);
162 return udev_device->devnum;
165 static int udev_device_set_devnum(struct udev_device *udev_device, dev_t devnum)
169 udev_device->devnum = devnum;
171 snprintf(num, sizeof(num), "%u", major(devnum));
172 udev_device_add_property_internal(udev_device, "MAJOR", num);
173 snprintf(num, sizeof(num), "%u", minor(devnum));
174 udev_device_add_property_internal(udev_device, "MINOR", num);
178 const char *udev_device_get_devpath_old(struct udev_device *udev_device)
180 return udev_device->devpath_old;
183 static int udev_device_set_devpath_old(struct udev_device *udev_device, const char *devpath_old)
187 free(udev_device->devpath_old);
188 udev_device->devpath_old = strdup(devpath_old);
189 if (udev_device->devpath_old == NULL)
191 udev_device_add_property_internal(udev_device, "DEVPATH_OLD", udev_device->devpath_old);
193 pos = strrchr(udev_device->devpath_old, '/');
200 * udev_device_get_driver:
201 * @udev_device: udev device
203 * Get the kernel driver name.
205 * Returns: the driver name string, or #NULL if there is no driver attached.
207 _public_ const char *udev_device_get_driver(struct udev_device *udev_device)
209 char driver[UTIL_NAME_SIZE];
211 if (udev_device == NULL)
213 if (!udev_device->driver_set) {
214 udev_device->driver_set = true;
215 if (util_get_sys_core_link_value(udev_device->udev, "driver", udev_device->syspath, driver, sizeof(driver)) > 0)
216 udev_device->driver = strdup(driver);
218 return udev_device->driver;
221 static int udev_device_set_driver(struct udev_device *udev_device, const char *driver)
223 free(udev_device->driver);
224 udev_device->driver = strdup(driver);
225 if (udev_device->driver == NULL)
227 udev_device->driver_set = true;
228 udev_device_add_property_internal(udev_device, "DRIVER", udev_device->driver);
233 * udev_device_get_devtype:
234 * @udev_device: udev device
236 * Retrieve the devtype string of the udev device.
238 * Returns: the devtype name of the udev device, or #NULL if it can not be determined
240 _public_ const char *udev_device_get_devtype(struct udev_device *udev_device)
242 if (udev_device == NULL)
244 if (!udev_device->devtype_set) {
245 udev_device->devtype_set = true;
246 udev_device_read_uevent_file(udev_device);
248 return udev_device->devtype;
251 static int udev_device_set_devtype(struct udev_device *udev_device, const char *devtype)
253 free(udev_device->devtype);
254 udev_device->devtype = strdup(devtype);
255 if (udev_device->devtype == NULL)
257 udev_device->devtype_set = true;
258 udev_device_add_property_internal(udev_device, "DEVTYPE", udev_device->devtype);
262 static int udev_device_set_subsystem(struct udev_device *udev_device, const char *subsystem)
264 free(udev_device->subsystem);
265 udev_device->subsystem = strdup(subsystem);
266 if (udev_device->subsystem == NULL)
268 udev_device->subsystem_set = true;
269 udev_device_add_property_internal(udev_device, "SUBSYSTEM", udev_device->subsystem);
274 * udev_device_get_subsystem:
275 * @udev_device: udev device
277 * Retrieve the subsystem string of the udev device. The string does not
280 * Returns: the subsystem name of the udev device, or #NULL if it can not be determined
282 _public_ const char *udev_device_get_subsystem(struct udev_device *udev_device)
284 char subsystem[UTIL_NAME_SIZE];
286 if (udev_device == NULL)
288 if (!udev_device->subsystem_set) {
289 udev_device->subsystem_set = true;
290 /* read "subsystem" link */
291 if (util_get_sys_core_link_value(udev_device->udev, "subsystem", udev_device->syspath, subsystem, sizeof(subsystem)) > 0) {
292 udev_device_set_subsystem(udev_device, subsystem);
293 return udev_device->subsystem;
296 if (startswith(udev_device->devpath, "/module/")) {
297 udev_device_set_subsystem(udev_device, "module");
298 return udev_device->subsystem;
300 if (strstr(udev_device->devpath, "/drivers/") != NULL) {
301 udev_device_set_subsystem(udev_device, "drivers");
302 return udev_device->subsystem;
304 if (startswith(udev_device->devpath, "/subsystem/") ||
305 startswith(udev_device->devpath, "/class/") ||
306 startswith(udev_device->devpath, "/bus/")) {
307 udev_device_set_subsystem(udev_device, "subsystem");
308 return udev_device->subsystem;
311 return udev_device->subsystem;
314 mode_t udev_device_get_devnode_mode(struct udev_device *udev_device)
316 if (!udev_device->info_loaded)
317 udev_device_read_uevent_file(udev_device);
318 return udev_device->devnode_mode;
321 static int udev_device_set_devnode_mode(struct udev_device *udev_device, mode_t mode)
325 udev_device->devnode_mode = mode;
326 snprintf(num, sizeof(num), "%#o", mode);
327 udev_device_add_property_internal(udev_device, "DEVMODE", num);
331 uid_t udev_device_get_devnode_uid(struct udev_device *udev_device)
333 if (!udev_device->info_loaded)
334 udev_device_read_uevent_file(udev_device);
335 return udev_device->devnode_uid;
338 static int udev_device_set_devnode_uid(struct udev_device *udev_device, uid_t uid)
342 udev_device->devnode_uid = uid;
343 snprintf(num, sizeof(num), "%u", uid);
344 udev_device_add_property_internal(udev_device, "DEVUID", num);
348 gid_t udev_device_get_devnode_gid(struct udev_device *udev_device)
350 if (!udev_device->info_loaded)
351 udev_device_read_uevent_file(udev_device);
352 return udev_device->devnode_gid;
355 static int udev_device_set_devnode_gid(struct udev_device *udev_device, gid_t gid)
359 udev_device->devnode_gid = gid;
360 snprintf(num, sizeof(num), "%u", gid);
361 udev_device_add_property_internal(udev_device, "DEVGID", num);
365 static struct udev_list_entry *udev_device_add_property_internal(struct udev_device *udev_device, const char *key, const char *value)
367 udev_device->envp_uptodate = false;
369 struct udev_list_entry *list_entry;
371 list_entry = udev_device_get_properties_list_entry(udev_device);
372 list_entry = udev_list_entry_get_by_name(list_entry, key);
373 if (list_entry != NULL)
374 udev_list_entry_delete(list_entry);
377 return udev_list_entry_add(&udev_device->properties_list, key, value);
381 int udev_device_add_property(struct udev_device *udev_device, const char *key, const char *value)
383 struct udev_list_entry *property;
385 property = udev_device_add_property_internal(udev_device, key, value);
387 /* store in db, skip private keys */
389 udev_list_entry_set_num(property, true);
394 static struct udev_list_entry *udev_device_add_property_from_string(struct udev_device *udev_device, const char *property)
396 char name[UTIL_LINE_SIZE];
399 strscpy(name, sizeof(name), property);
400 val = strchr(name, '=');
407 return udev_device_add_property_internal(udev_device, name, val);
410 static int udev_device_set_syspath(struct udev_device *udev_device, const char *syspath)
415 free(udev_device->syspath);
416 udev_device->syspath = strdup(syspath);
417 if (udev_device->syspath == NULL)
419 udev_device->devpath = udev_device->syspath + strlen("/sys");
420 udev_device_add_property_internal(udev_device, "DEVPATH", udev_device->devpath);
422 pos = strrchr(udev_device->syspath, '/');
425 udev_device->sysname = strdup(&pos[1]);
426 if (udev_device->sysname == NULL)
429 /* some devices have '!' in their name, change that to '/' */
431 while (udev_device->sysname[len] != '\0') {
432 if (udev_device->sysname[len] == '!')
433 udev_device->sysname[len] = '/';
437 /* trailing number */
438 while (len > 0 && isdigit(udev_device->sysname[--len]))
439 udev_device->sysnum = &udev_device->sysname[len];
441 /* sysname is completely numeric */
443 udev_device->sysnum = NULL;
449 * parse property string, and if needed, update internal values accordingly
451 * udev_device_add_property_from_string_parse_finish() needs to be
452 * called after adding properties, and its return value checked
454 * udev_device_set_info_loaded() needs to be set, to avoid trying
455 * to use a device without a DEVPATH set
457 static void udev_device_add_property_from_string_parse(struct udev_device *udev_device, const char *property)
459 if (startswith(property, "DEVPATH=")) {
460 char path[UTIL_PATH_SIZE];
462 strscpyl(path, sizeof(path), "/sys", &property[8], NULL);
463 udev_device_set_syspath(udev_device, path);
464 } else if (startswith(property, "SUBSYSTEM=")) {
465 udev_device_set_subsystem(udev_device, &property[10]);
466 } else if (startswith(property, "DEVTYPE=")) {
467 udev_device_set_devtype(udev_device, &property[8]);
468 } else if (startswith(property, "DEVNAME=")) {
469 udev_device_set_devnode(udev_device, &property[8]);
470 } else if (startswith(property, "DEVLINKS=")) {
471 char devlinks[UTIL_PATH_SIZE];
475 strscpy(devlinks, sizeof(devlinks), &property[9]);
477 next = strchr(slink, ' ');
478 while (next != NULL) {
480 udev_device_add_devlink(udev_device, slink);
482 next = strchr(slink, ' ');
484 if (slink[0] != '\0')
485 udev_device_add_devlink(udev_device, slink);
486 } else if (startswith(property, "TAGS=")) {
487 char tags[UTIL_PATH_SIZE];
490 strscpy(tags, sizeof(tags), &property[5]);
491 next = strchr(tags, ':');
494 while (next[0] != '\0') {
498 next = strchr(tag, ':');
503 udev_device_add_tag(udev_device, tag);
506 } else if (startswith(property, "USEC_INITIALIZED=")) {
507 udev_device_set_usec_initialized(udev_device, strtoull(&property[19], NULL, 10));
508 } else if (startswith(property, "DRIVER=")) {
509 udev_device_set_driver(udev_device, &property[7]);
510 } else if (startswith(property, "ACTION=")) {
511 udev_device_set_action(udev_device, &property[7]);
512 } else if (startswith(property, "MAJOR=")) {
513 udev_device->maj = strtoull(&property[6], NULL, 10);
514 } else if (startswith(property, "MINOR=")) {
515 udev_device->min = strtoull(&property[6], NULL, 10);
516 } else if (startswith(property, "DEVPATH_OLD=")) {
517 udev_device_set_devpath_old(udev_device, &property[12]);
518 } else if (startswith(property, "SEQNUM=")) {
519 udev_device_set_seqnum(udev_device, strtoull(&property[7], NULL, 10));
520 } else if (startswith(property, "IFINDEX=")) {
521 udev_device_set_ifindex(udev_device, strtoull(&property[8], NULL, 10));
522 } else if (startswith(property, "DEVMODE=")) {
523 udev_device_set_devnode_mode(udev_device, strtoul(&property[8], NULL, 8));
524 } else if (startswith(property, "DEVUID=")) {
525 udev_device_set_devnode_uid(udev_device, strtoul(&property[7], NULL, 10));
526 } else if (startswith(property, "DEVGID=")) {
527 udev_device_set_devnode_gid(udev_device, strtoul(&property[7], NULL, 10));
529 udev_device_add_property_from_string(udev_device, property);
533 static int udev_device_add_property_from_string_parse_finish(struct udev_device *udev_device)
535 if (udev_device->maj > 0)
536 udev_device_set_devnum(udev_device, makedev(udev_device->maj, udev_device->min));
537 udev_device->maj = 0;
538 udev_device->min = 0;
540 if (udev_device->devpath == NULL || udev_device->subsystem == NULL)
546 * udev_device_get_property_value:
547 * @udev_device: udev device
548 * @key: property name
550 * Get the value of a given property.
552 * Returns: the property string, or #NULL if there is no such property.
554 _public_ const char *udev_device_get_property_value(struct udev_device *udev_device, const char *key)
556 struct udev_list_entry *list_entry;
558 if (udev_device == NULL)
563 list_entry = udev_device_get_properties_list_entry(udev_device);
564 list_entry = udev_list_entry_get_by_name(list_entry, key);
565 return udev_list_entry_get_value(list_entry);
568 int udev_device_read_db(struct udev_device *udev_device)
570 char filename[UTIL_PATH_SIZE];
571 char line[UTIL_LINE_SIZE];
575 if (udev_device->db_loaded)
578 udev_device->db_loaded = true;
580 id = udev_device_get_id_filename(udev_device);
584 strscpyl(filename, sizeof(filename), "/run/udev/data/", id, NULL);
586 f = fopen(filename, "re");
588 return log_debug_errno(errno, "no db file to read %s: %m", filename);
590 /* devices with a database entry are initialized */
591 udev_device->is_initialized = true;
593 while (fgets(line, sizeof(line), f)) {
596 struct udev_list_entry *entry;
605 strscpyl(filename, sizeof(filename), "/dev/", val, NULL);
606 udev_device_add_devlink(udev_device, filename);
609 udev_device_set_devlink_priority(udev_device, atoi(val));
612 entry = udev_device_add_property_from_string(udev_device, val);
613 udev_list_entry_set_num(entry, true);
616 udev_device_add_tag(udev_device, val);
619 udev_device_set_watch_handle(udev_device, atoi(val));
622 udev_device_set_usec_initialized(udev_device, strtoull(val, NULL, 10));
628 log_debug("device %p filled with db file data", udev_device);
632 int udev_device_read_uevent_file(struct udev_device *udev_device)
634 char filename[UTIL_PATH_SIZE];
636 char line[UTIL_LINE_SIZE];
640 if (udev_device->uevent_loaded)
643 strscpyl(filename, sizeof(filename), udev_device->syspath, "/uevent", NULL);
644 f = fopen(filename, "re");
647 udev_device->uevent_loaded = true;
649 while (fgets(line, sizeof(line), f)) {
652 pos = strchr(line, '\n');
657 if (startswith(line, "DEVTYPE=")) {
658 udev_device_set_devtype(udev_device, &line[8]);
661 if (startswith(line, "IFINDEX=")) {
662 udev_device_set_ifindex(udev_device, strtoull(&line[8], NULL, 10));
665 if (startswith(line, "DEVNAME=")) {
666 udev_device_set_devnode(udev_device, &line[8]);
670 if (startswith(line, "MAJOR="))
671 maj = strtoull(&line[6], NULL, 10);
672 else if (startswith(line, "MINOR="))
673 min = strtoull(&line[6], NULL, 10);
674 else if (startswith(line, "DEVMODE="))
675 udev_device->devnode_mode = strtoul(&line[8], NULL, 8);
677 udev_device_add_property_from_string(udev_device, line);
680 udev_device->devnum = makedev(maj, min);
685 void udev_device_set_info_loaded(struct udev_device *device)
687 device->info_loaded = true;
690 static struct udev_device *udev_device_new(struct udev *udev)
692 struct udev_device *udev_device;
699 udev_device = new0(struct udev_device, 1);
700 if (udev_device == NULL) {
704 udev_device->refcount = 1;
705 udev_device->udev = udev;
706 udev_list_init(udev, &udev_device->devlinks_list, true);
707 udev_list_init(udev, &udev_device->properties_list, true);
708 udev_list_init(udev, &udev_device->sysattr_value_list, true);
709 udev_list_init(udev, &udev_device->sysattr_list, false);
710 udev_list_init(udev, &udev_device->tags_list, true);
711 udev_device->watch_handle = -1;
717 * udev_device_new_from_syspath:
718 * @udev: udev library context
719 * @syspath: sys device path including sys directory
721 * Create new udev device, and fill in information from the sys
722 * device and the udev database entry. The syspath is the absolute
723 * path to the device, including the sys mount point.
725 * The initial refcount is 1, and needs to be decremented to
726 * release the resources of the udev device.
728 * Returns: a new udev device, or #NULL, if it does not exist
730 _public_ struct udev_device *udev_device_new_from_syspath(struct udev *udev, const char *syspath)
733 char path[UTIL_PATH_SIZE];
736 struct udev_device *udev_device;
743 if (syspath == NULL) {
748 /* path starts in sys */
749 if (!startswith(syspath, "/sys")) {
750 log_debug("not in sys :%s", syspath);
755 /* path is not a root directory */
756 subdir = syspath + strlen("/sys");
757 pos = strrchr(subdir, '/');
758 if (pos == NULL || pos[1] == '\0' || pos < &subdir[2]) {
763 /* resolve possible symlink to real path */
764 strscpy(path, sizeof(path), syspath);
765 util_resolve_sys_link(udev, path, sizeof(path));
767 if (startswith(path + strlen("/sys"), "/devices/")) {
768 char file[UTIL_PATH_SIZE];
770 /* all "devices" require a "uevent" file */
771 strscpyl(file, sizeof(file), path, "/uevent", NULL);
772 if (stat(file, &statbuf) != 0)
775 /* everything else just needs to be a directory */
776 if (stat(path, &statbuf) != 0)
779 if (!S_ISDIR(statbuf.st_mode)) {
785 udev_device = udev_device_new(udev);
786 if (udev_device == NULL)
789 udev_device_set_syspath(udev_device, path);
790 log_debug("device %p has devpath '%s'", udev_device, udev_device_get_devpath(udev_device));
796 * udev_device_new_from_devnum:
797 * @udev: udev library context
798 * @type: char or block device
799 * @devnum: device major/minor number
801 * Create new udev device, and fill in information from the sys
802 * device and the udev database entry. The device is looked-up
803 * by its major/minor number and type. Character and block device
804 * numbers are not unique across the two types.
806 * The initial refcount is 1, and needs to be decremented to
807 * release the resources of the udev device.
809 * Returns: a new udev device, or #NULL, if it does not exist
811 _public_ struct udev_device *udev_device_new_from_devnum(struct udev *udev, char type, dev_t devnum)
813 char path[UTIL_PATH_SIZE];
814 const char *type_str;
818 else if (type == 'c')
825 /* use /sys/dev/{block,char}/<maj>:<min> link */
826 snprintf(path, sizeof(path), "/sys/dev/%s/%u:%u",
827 type_str, major(devnum), minor(devnum));
828 return udev_device_new_from_syspath(udev, path);
832 * udev_device_new_from_device_id:
833 * @udev: udev library context
834 * @id: text string identifying a kernel device
836 * Create new udev device, and fill in information from the sys
837 * device and the udev database entry. The device is looked-up
838 * by a special string:
839 * b8:2 - block device major:minor
840 * c128:1 - char device major:minor
841 * n3 - network device ifindex
842 * +sound:card29 - kernel driver core subsystem:device name
844 * The initial refcount is 1, and needs to be decremented to
845 * release the resources of the udev device.
847 * Returns: a new udev device, or #NULL, if it does not exist
849 _public_ struct udev_device *udev_device_new_from_device_id(struct udev *udev, const char *id)
853 char subsys[UTIL_PATH_SIZE];
859 if (sscanf(id, "%c%i:%i", &type, &maj, &min) != 3)
861 return udev_device_new_from_devnum(udev, type, makedev(maj, min));
865 struct udev_device *dev;
868 ifindex = strtoul(&id[1], NULL, 10);
874 sk = socket(PF_INET, SOCK_DGRAM, 0);
877 memzero(&ifr, sizeof(struct ifreq));
878 ifr.ifr_ifindex = ifindex;
879 if (ioctl(sk, SIOCGIFNAME, &ifr) != 0) {
885 dev = udev_device_new_from_subsystem_sysname(udev, "net", ifr.ifr_name);
888 if (udev_device_get_ifindex(dev) == ifindex)
891 /* this is racy, so we may end up with the wrong device */
892 udev_device_unref(dev);
897 strscpy(subsys, sizeof(subsys), &id[1]);
898 sysname = strchr(subsys, ':');
899 if (sysname == NULL) {
904 sysname = &sysname[1];
905 return udev_device_new_from_subsystem_sysname(udev, subsys, sysname);
913 * udev_device_new_from_subsystem_sysname:
914 * @udev: udev library context
915 * @subsystem: the subsystem of the device
916 * @sysname: the name of the device
918 * Create new udev device, and fill in information from the sys device
919 * and the udev database entry. The device is looked up by the subsystem
920 * and name string of the device, like "mem" / "zero", or "block" / "sda".
922 * The initial refcount is 1, and needs to be decremented to
923 * release the resources of the udev device.
925 * Returns: a new udev device, or #NULL, if it does not exist
927 _public_ struct udev_device *udev_device_new_from_subsystem_sysname(struct udev *udev, const char *subsystem, const char *sysname)
929 char path[UTIL_PATH_SIZE];
932 if (streq(subsystem, "subsystem")) {
933 strscpyl(path, sizeof(path), "/sys/subsystem/", sysname, NULL);
934 if (stat(path, &statbuf) == 0)
937 strscpyl(path, sizeof(path), "/sys/bus/", sysname, NULL);
938 if (stat(path, &statbuf) == 0)
941 strscpyl(path, sizeof(path), "/sys/class/", sysname, NULL);
942 if (stat(path, &statbuf) == 0)
947 if (streq(subsystem, "module")) {
948 strscpyl(path, sizeof(path), "/sys/module/", sysname, NULL);
949 if (stat(path, &statbuf) == 0)
954 if (streq(subsystem, "drivers")) {
955 char subsys[UTIL_NAME_SIZE];
958 strscpy(subsys, sizeof(subsys), sysname);
959 driver = strchr(subsys, ':');
960 if (driver != NULL) {
964 strscpyl(path, sizeof(path), "/sys/subsystem/", subsys, "/drivers/", driver, NULL);
965 if (stat(path, &statbuf) == 0)
968 strscpyl(path, sizeof(path), "/sys/bus/", subsys, "/drivers/", driver, NULL);
969 if (stat(path, &statbuf) == 0)
977 strscpyl(path, sizeof(path), "/sys/subsystem/", subsystem, "/devices/", sysname, NULL);
978 if (stat(path, &statbuf) == 0)
981 strscpyl(path, sizeof(path), "/sys/bus/", subsystem, "/devices/", sysname, NULL);
982 if (stat(path, &statbuf) == 0)
985 strscpyl(path, sizeof(path), "/sys/class/", subsystem, "/", sysname, NULL);
986 if (stat(path, &statbuf) == 0)
991 return udev_device_new_from_syspath(udev, path);
995 * udev_device_new_from_environment
996 * @udev: udev library context
998 * Create new udev device, and fill in information from the
999 * current process environment. This only works reliable if
1000 * the process is called from a udev rule. It is usually used
1001 * for tools executed from IMPORT= rules.
1003 * The initial refcount is 1, and needs to be decremented to
1004 * release the resources of the udev device.
1006 * Returns: a new udev device, or #NULL, if it does not exist
1008 _public_ struct udev_device *udev_device_new_from_environment(struct udev *udev)
1011 struct udev_device *udev_device;
1013 udev_device = udev_device_new(udev);
1014 if (udev_device == NULL)
1016 udev_device_set_info_loaded(udev_device);
1018 for (i = 0; environ[i] != NULL; i++)
1019 udev_device_add_property_from_string_parse(udev_device, environ[i]);
1021 if (udev_device_add_property_from_string_parse_finish(udev_device) < 0) {
1022 log_debug("missing values, invalid device");
1023 udev_device_unref(udev_device);
1030 static struct udev_device *device_new_from_parent(struct udev_device *udev_device)
1032 struct udev_device *udev_device_parent = NULL;
1033 char path[UTIL_PATH_SIZE];
1036 strscpy(path, sizeof(path), udev_device->syspath);
1037 subdir = path + strlen("/sys/");
1041 pos = strrchr(subdir, '/');
1042 if (pos == NULL || pos < &subdir[2])
1045 udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
1046 if (udev_device_parent != NULL)
1047 return udev_device_parent;
1055 * udev_device_get_parent:
1056 * @udev_device: the device to start searching from
1058 * Find the next parent device, and fill in information from the sys
1059 * device and the udev database entry.
1061 * Returned device is not referenced. It is attached to the child
1062 * device, and will be cleaned up when the child device is cleaned up.
1064 * It is not necessarily just the upper level directory, empty or not
1065 * recognized sys directories are ignored.
1067 * It can be called as many times as needed, without caring about
1070 * Returns: a new udev device, or #NULL, if it no parent exist.
1072 _public_ struct udev_device *udev_device_get_parent(struct udev_device *udev_device)
1074 if (udev_device == NULL) {
1078 if (!udev_device->parent_set) {
1079 udev_device->parent_set = true;
1080 udev_device->parent_device = device_new_from_parent(udev_device);
1082 return udev_device->parent_device;
1086 * udev_device_get_parent_with_subsystem_devtype:
1087 * @udev_device: udev device to start searching from
1088 * @subsystem: the subsystem of the device
1089 * @devtype: the type (DEVTYPE) of the device
1091 * Find the next parent device, with a matching subsystem and devtype
1092 * value, and fill in information from the sys device and the udev
1095 * If devtype is #NULL, only subsystem is checked, and any devtype will
1098 * Returned device is not referenced. It is attached to the child
1099 * device, and will be cleaned up when the child device is cleaned up.
1101 * It can be called as many times as needed, without caring about
1104 * Returns: a new udev device, or #NULL if no matching parent exists.
1106 _public_ struct udev_device *udev_device_get_parent_with_subsystem_devtype(struct udev_device *udev_device, const char *subsystem, const char *devtype)
1108 struct udev_device *parent;
1110 if (subsystem == NULL) {
1115 parent = udev_device_get_parent(udev_device);
1116 while (parent != NULL) {
1117 const char *parent_subsystem;
1118 const char *parent_devtype;
1120 parent_subsystem = udev_device_get_subsystem(parent);
1121 if (parent_subsystem != NULL && streq(parent_subsystem, subsystem)) {
1122 if (devtype == NULL)
1124 parent_devtype = udev_device_get_devtype(parent);
1125 if (parent_devtype != NULL && streq(parent_devtype, devtype))
1128 parent = udev_device_get_parent(parent);
1138 * udev_device_get_udev:
1139 * @udev_device: udev device
1141 * Retrieve the udev library context the device was created with.
1143 * Returns: the udev library context
1145 _public_ struct udev *udev_device_get_udev(struct udev_device *udev_device)
1147 if (udev_device == NULL)
1149 return udev_device->udev;
1154 * @udev_device: udev device
1156 * Take a reference of a udev device.
1158 * Returns: the passed udev device
1160 _public_ struct udev_device *udev_device_ref(struct udev_device *udev_device)
1162 if (udev_device == NULL)
1164 udev_device->refcount++;
1169 * udev_device_unref:
1170 * @udev_device: udev device
1172 * Drop a reference of a udev device. If the refcount reaches zero,
1173 * the resources of the device will be released.
1177 _public_ struct udev_device *udev_device_unref(struct udev_device *udev_device)
1179 if (udev_device == NULL)
1181 udev_device->refcount--;
1182 if (udev_device->refcount > 0)
1184 if (udev_device->parent_device != NULL)
1185 udev_device_unref(udev_device->parent_device);
1186 free(udev_device->syspath);
1187 free(udev_device->sysname);
1188 free(udev_device->devnode);
1189 free(udev_device->subsystem);
1190 free(udev_device->devtype);
1191 udev_list_cleanup(&udev_device->devlinks_list);
1192 udev_list_cleanup(&udev_device->properties_list);
1193 udev_list_cleanup(&udev_device->sysattr_value_list);
1194 udev_list_cleanup(&udev_device->sysattr_list);
1195 udev_list_cleanup(&udev_device->tags_list);
1196 free(udev_device->action);
1197 free(udev_device->driver);
1198 free(udev_device->devpath_old);
1199 free(udev_device->id_filename);
1200 free(udev_device->envp);
1201 free(udev_device->monitor_buf);
1207 * udev_device_get_devpath:
1208 * @udev_device: udev device
1210 * Retrieve the kernel devpath value of the udev device. The path
1211 * does not contain the sys mount point, and starts with a '/'.
1213 * Returns: the devpath of the udev device
1215 _public_ const char *udev_device_get_devpath(struct udev_device *udev_device)
1217 if (udev_device == NULL)
1219 return udev_device->devpath;
1223 * udev_device_get_syspath:
1224 * @udev_device: udev device
1226 * Retrieve the sys path of the udev device. The path is an
1227 * absolute path and starts with the sys mount point.
1229 * Returns: the sys path of the udev device
1231 _public_ const char *udev_device_get_syspath(struct udev_device *udev_device)
1233 if (udev_device == NULL)
1235 return udev_device->syspath;
1239 * udev_device_get_sysname:
1240 * @udev_device: udev device
1242 * Get the kernel device name in /sys.
1244 * Returns: the name string of the device device
1246 _public_ const char *udev_device_get_sysname(struct udev_device *udev_device)
1248 if (udev_device == NULL)
1250 return udev_device->sysname;
1254 * udev_device_get_sysnum:
1255 * @udev_device: udev device
1257 * Get the instance number of the device.
1259 * Returns: the trailing number string of the device name
1261 _public_ const char *udev_device_get_sysnum(struct udev_device *udev_device)
1263 if (udev_device == NULL)
1265 return udev_device->sysnum;
1269 * udev_device_get_devnode:
1270 * @udev_device: udev device
1272 * Retrieve the device node file name belonging to the udev device.
1273 * The path is an absolute path, and starts with the device directory.
1275 * Returns: the device node file name of the udev device, or #NULL if no device node exists
1277 _public_ const char *udev_device_get_devnode(struct udev_device *udev_device)
1279 if (udev_device == NULL)
1281 if (udev_device->devnode != NULL)
1282 return udev_device->devnode;
1283 if (!udev_device->info_loaded)
1284 udev_device_read_uevent_file(udev_device);
1285 return udev_device->devnode;
1289 * udev_device_get_devlinks_list_entry:
1290 * @udev_device: udev device
1292 * Retrieve the list of device links pointing to the device file of
1293 * the udev device. The next list entry can be retrieved with
1294 * udev_list_entry_get_next(), which returns #NULL if no more entries exist.
1295 * The devlink path can be retrieved from the list entry by
1296 * udev_list_entry_get_name(). The path is an absolute path, and starts with
1297 * the device directory.
1299 * Returns: the first entry of the device node link list
1301 _public_ struct udev_list_entry *udev_device_get_devlinks_list_entry(struct udev_device *udev_device)
1303 if (udev_device == NULL)
1305 if (!udev_device->info_loaded)
1306 udev_device_read_db(udev_device);
1307 return udev_list_get_entry(&udev_device->devlinks_list);
1310 void udev_device_cleanup_devlinks_list(struct udev_device *udev_device)
1312 udev_device->devlinks_uptodate = false;
1313 udev_list_cleanup(&udev_device->devlinks_list);
1317 * udev_device_get_properties_list_entry:
1318 * @udev_device: udev device
1320 * Retrieve the list of key/value device properties of the udev
1321 * device. The next list entry can be retrieved with udev_list_entry_get_next(),
1322 * which returns #NULL if no more entries exist. The property name
1323 * can be retrieved from the list entry by udev_list_entry_get_name(),
1324 * the property value by udev_list_entry_get_value().
1326 * Returns: the first entry of the property list
1328 _public_ struct udev_list_entry *udev_device_get_properties_list_entry(struct udev_device *udev_device)
1330 if (udev_device == NULL)
1332 if (!udev_device->info_loaded) {
1333 udev_device_read_uevent_file(udev_device);
1334 udev_device_read_db(udev_device);
1336 if (!udev_device->devlinks_uptodate) {
1337 char symlinks[UTIL_PATH_SIZE];
1338 struct udev_list_entry *list_entry;
1340 udev_device->devlinks_uptodate = true;
1341 list_entry = udev_device_get_devlinks_list_entry(udev_device);
1342 if (list_entry != NULL) {
1347 l = strpcpyl(&s, sizeof(symlinks), udev_list_entry_get_name(list_entry), NULL);
1348 udev_list_entry_foreach(list_entry, udev_list_entry_get_next(list_entry))
1349 l = strpcpyl(&s, l, " ", udev_list_entry_get_name(list_entry), NULL);
1350 udev_device_add_property_internal(udev_device, "DEVLINKS", symlinks);
1353 if (!udev_device->tags_uptodate) {
1354 udev_device->tags_uptodate = true;
1355 if (udev_device_get_tags_list_entry(udev_device) != NULL) {
1356 char tags[UTIL_PATH_SIZE];
1357 struct udev_list_entry *list_entry;
1362 l = strpcpyl(&s, sizeof(tags), ":", NULL);
1363 udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(udev_device))
1364 l = strpcpyl(&s, l, udev_list_entry_get_name(list_entry), ":", NULL);
1365 udev_device_add_property_internal(udev_device, "TAGS", tags);
1368 return udev_list_get_entry(&udev_device->properties_list);
1372 * udev_device_get_action:
1373 * @udev_device: udev device
1375 * This is only valid if the device was received through a monitor. Devices read from
1376 * sys do not have an action string. Usual actions are: add, remove, change, online,
1379 * Returns: the kernel action value, or #NULL if there is no action value available.
1381 _public_ const char *udev_device_get_action(struct udev_device *udev_device)
1383 if (udev_device == NULL)
1385 return udev_device->action;
1389 * udev_device_get_usec_since_initialized:
1390 * @udev_device: udev device
1392 * Return the number of microseconds passed since udev set up the
1393 * device for the first time.
1395 * This is only implemented for devices with need to store properties
1396 * in the udev database. All other devices return 0 here.
1398 * Returns: the number of microseconds since the device was first seen.
1400 _public_ unsigned long long int udev_device_get_usec_since_initialized(struct udev_device *udev_device)
1404 if (udev_device == NULL)
1406 if (!udev_device->info_loaded)
1407 udev_device_read_db(udev_device);
1408 if (udev_device->usec_initialized == 0)
1410 now_ts = now(CLOCK_MONOTONIC);
1413 return now_ts - udev_device->usec_initialized;
1416 usec_t udev_device_get_usec_initialized(struct udev_device *udev_device)
1418 return udev_device->usec_initialized;
1421 void udev_device_set_usec_initialized(struct udev_device *udev_device, usec_t usec_initialized)
1425 udev_device->usec_initialized = usec_initialized;
1426 snprintf(num, sizeof(num), USEC_FMT, usec_initialized);
1427 udev_device_add_property_internal(udev_device, "USEC_INITIALIZED", num);
1431 * udev_device_get_sysattr_value:
1432 * @udev_device: udev device
1433 * @sysattr: attribute name
1435 * The retrieved value is cached in the device. Repeated calls will return the same
1436 * value and not open the attribute again.
1438 * Returns: the content of a sys attribute file, or #NULL if there is no sys attribute value.
1440 _public_ const char *udev_device_get_sysattr_value(struct udev_device *udev_device, const char *sysattr)
1442 struct udev_list_entry *list_entry;
1443 char path[UTIL_PATH_SIZE];
1445 struct stat statbuf;
1448 const char *val = NULL;
1450 if (udev_device == NULL)
1452 if (sysattr == NULL)
1455 /* look for possibly already cached result */
1456 list_entry = udev_list_get_entry(&udev_device->sysattr_value_list);
1457 list_entry = udev_list_entry_get_by_name(list_entry, sysattr);
1458 if (list_entry != NULL)
1459 return udev_list_entry_get_value(list_entry);
1461 strscpyl(path, sizeof(path), udev_device_get_syspath(udev_device), "/", sysattr, NULL);
1462 if (lstat(path, &statbuf) != 0) {
1463 udev_list_entry_add(&udev_device->sysattr_value_list, sysattr, NULL);
1467 if (S_ISLNK(statbuf.st_mode)) {
1469 * Some core links return only the last element of the target path,
1470 * these are just values, the paths should not be exposed.
1472 if (streq(sysattr, "driver") ||
1473 streq(sysattr, "subsystem") ||
1474 streq(sysattr, "module")) {
1475 if (util_get_sys_core_link_value(udev_device->udev, sysattr,
1476 udev_device->syspath, value, sizeof(value)) < 0)
1478 list_entry = udev_list_entry_add(&udev_device->sysattr_value_list, sysattr, value);
1479 val = udev_list_entry_get_value(list_entry);
1486 /* skip directories */
1487 if (S_ISDIR(statbuf.st_mode))
1490 /* skip non-readable files */
1491 if ((statbuf.st_mode & S_IRUSR) == 0)
1494 /* read attribute value */
1495 fd = open(path, O_RDONLY|O_CLOEXEC);
1498 size = read(fd, value, sizeof(value));
1502 if (size == sizeof(value))
1505 /* got a valid value, store it in cache and return it */
1507 util_remove_trailing_chars(value, '\n');
1508 list_entry = udev_list_entry_add(&udev_device->sysattr_value_list, sysattr, value);
1509 val = udev_list_entry_get_value(list_entry);
1515 * udev_device_set_sysattr_value:
1516 * @udev_device: udev device
1517 * @sysattr: attribute name
1518 * @value: new value to be set
1520 * Update the contents of the sys attribute and the cached value of the device.
1522 * Returns: Negative error code on failure or 0 on success.
1524 _public_ int udev_device_set_sysattr_value(struct udev_device *udev_device, const char *sysattr, char *value)
1526 struct udev_device *dev;
1527 char path[UTIL_PATH_SIZE];
1528 struct stat statbuf;
1530 ssize_t size, value_len;
1533 if (udev_device == NULL)
1536 if (sysattr == NULL)
1541 value_len = strlen(value);
1543 strscpyl(path, sizeof(path), udev_device_get_syspath(dev), "/", sysattr, NULL);
1544 if (lstat(path, &statbuf) != 0) {
1545 udev_list_entry_add(&dev->sysattr_value_list, sysattr, NULL);
1550 if (S_ISLNK(statbuf.st_mode)) {
1555 /* skip directories */
1556 if (S_ISDIR(statbuf.st_mode)) {
1561 /* skip non-readable files */
1562 if ((statbuf.st_mode & S_IRUSR) == 0) {
1567 /* Value is limited to 4k */
1568 if (value_len > 4096) {
1572 util_remove_trailing_chars(value, '\n');
1574 /* write attribute value */
1575 fd = open(path, O_WRONLY|O_CLOEXEC);
1580 size = write(fd, value, value_len);
1586 if (size < value_len) {
1591 /* wrote a valid value, store it in cache and return it */
1592 udev_list_entry_add(&dev->sysattr_value_list, sysattr, value);
1594 if (dev != udev_device)
1595 udev_device_unref(dev);
1599 static int udev_device_sysattr_list_read(struct udev_device *udev_device)
1601 struct dirent *dent;
1605 if (udev_device == NULL)
1607 if (udev_device->sysattr_list_read)
1610 dir = opendir(udev_device_get_syspath(udev_device));
1614 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
1615 char path[UTIL_PATH_SIZE];
1616 struct stat statbuf;
1618 /* only handle symlinks and regular files */
1619 if (dent->d_type != DT_LNK && dent->d_type != DT_REG)
1622 strscpyl(path, sizeof(path), udev_device_get_syspath(udev_device), "/", dent->d_name, NULL);
1623 if (lstat(path, &statbuf) != 0)
1625 if ((statbuf.st_mode & S_IRUSR) == 0)
1628 udev_list_entry_add(&udev_device->sysattr_list, dent->d_name, NULL);
1633 udev_device->sysattr_list_read = true;
1639 * udev_device_get_sysattr_list_entry:
1640 * @udev_device: udev device
1642 * Retrieve the list of available sysattrs, with value being empty;
1643 * This just return all available sysfs attributes for a particular
1644 * device without reading their values.
1646 * Returns: the first entry of the property list
1648 _public_ struct udev_list_entry *udev_device_get_sysattr_list_entry(struct udev_device *udev_device)
1650 if (!udev_device->sysattr_list_read) {
1652 ret = udev_device_sysattr_list_read(udev_device);
1657 return udev_list_get_entry(&udev_device->sysattr_list);
1660 static int udev_device_set_devnode(struct udev_device *udev_device, const char *devnode)
1662 free(udev_device->devnode);
1663 if (devnode[0] != '/') {
1664 if (asprintf(&udev_device->devnode, "/dev/%s", devnode) < 0)
1665 udev_device->devnode = NULL;
1667 udev_device->devnode = strdup(devnode);
1669 if (udev_device->devnode == NULL)
1671 udev_device_add_property_internal(udev_device, "DEVNAME", udev_device->devnode);
1675 int udev_device_add_devlink(struct udev_device *udev_device, const char *devlink)
1677 struct udev_list_entry *list_entry;
1679 udev_device->devlinks_uptodate = false;
1680 list_entry = udev_list_entry_add(&udev_device->devlinks_list, devlink, NULL);
1681 if (list_entry == NULL)
1686 const char *udev_device_get_id_filename(struct udev_device *udev_device)
1688 if (udev_device->id_filename == NULL) {
1689 if (udev_device_get_subsystem(udev_device) == NULL)
1692 if (major(udev_device_get_devnum(udev_device)) > 0) {
1693 /* use dev_t -- b259:131072, c254:0 */
1694 if (asprintf(&udev_device->id_filename, "%c%u:%u",
1695 streq(udev_device_get_subsystem(udev_device), "block") ? 'b' : 'c',
1696 major(udev_device_get_devnum(udev_device)),
1697 minor(udev_device_get_devnum(udev_device))) < 0)
1698 udev_device->id_filename = NULL;
1699 } else if (udev_device_get_ifindex(udev_device) > 0) {
1700 /* use netdev ifindex -- n3 */
1701 if (asprintf(&udev_device->id_filename, "n%i", udev_device_get_ifindex(udev_device)) < 0)
1702 udev_device->id_filename = NULL;
1705 * use $subsys:$syname -- pci:0000:00:1f.2
1706 * sysname() has '!' translated, get it from devpath
1708 const char *sysname;
1709 sysname = strrchr(udev_device->devpath, '/');
1710 if (sysname == NULL)
1712 sysname = &sysname[1];
1713 if (asprintf(&udev_device->id_filename, "+%s:%s", udev_device_get_subsystem(udev_device), sysname) < 0)
1714 udev_device->id_filename = NULL;
1717 return udev_device->id_filename;
1721 * udev_device_get_is_initialized:
1722 * @udev_device: udev device
1724 * Check if udev has already handled the device and has set up
1725 * device node permissions and context, or has renamed a network
1728 * This is only implemented for devices with a device node
1729 * or network interfaces. All other devices return 1 here.
1731 * Returns: 1 if the device is set up. 0 otherwise.
1733 _public_ int udev_device_get_is_initialized(struct udev_device *udev_device)
1735 if (!udev_device->info_loaded)
1736 udev_device_read_db(udev_device);
1737 return udev_device->is_initialized;
1740 void udev_device_set_is_initialized(struct udev_device *udev_device)
1742 udev_device->is_initialized = true;
1745 static bool is_valid_tag(const char *tag)
1747 return !strchr(tag, ':') && !strchr(tag, ' ');
1750 int udev_device_add_tag(struct udev_device *udev_device, const char *tag)
1752 if (!is_valid_tag(tag))
1754 udev_device->tags_uptodate = false;
1755 if (udev_list_entry_add(&udev_device->tags_list, tag, NULL) != NULL)
1760 void udev_device_remove_tag(struct udev_device *udev_device, const char *tag)
1762 struct udev_list_entry *e;
1764 if (!is_valid_tag(tag))
1766 e = udev_list_get_entry(&udev_device->tags_list);
1767 e = udev_list_entry_get_by_name(e, tag);
1769 udev_device->tags_uptodate = false;
1770 udev_list_entry_delete(e);
1774 void udev_device_cleanup_tags_list(struct udev_device *udev_device)
1776 udev_device->tags_uptodate = false;
1777 udev_list_cleanup(&udev_device->tags_list);
1781 * udev_device_get_tags_list_entry:
1782 * @udev_device: udev device
1784 * Retrieve the list of tags attached to the udev device. The next
1785 * list entry can be retrieved with udev_list_entry_get_next(),
1786 * which returns #NULL if no more entries exist. The tag string
1787 * can be retrieved from the list entry by udev_list_entry_get_name().
1789 * Returns: the first entry of the tag list
1791 _public_ struct udev_list_entry *udev_device_get_tags_list_entry(struct udev_device *udev_device)
1793 if (udev_device == NULL)
1795 if (!udev_device->info_loaded)
1796 udev_device_read_db(udev_device);
1797 return udev_list_get_entry(&udev_device->tags_list);
1801 * udev_device_has_tag:
1802 * @udev_device: udev device
1805 * Check if a given device has a certain tag associated.
1807 * Returns: 1 if the tag is found. 0 otherwise.
1809 _public_ int udev_device_has_tag(struct udev_device *udev_device, const char *tag)
1811 struct udev_list_entry *list_entry;
1813 if (udev_device == NULL)
1815 if (!udev_device->info_loaded)
1816 udev_device_read_db(udev_device);
1817 list_entry = udev_device_get_tags_list_entry(udev_device);
1818 if (udev_list_entry_get_by_name(list_entry, tag) != NULL)
1823 #define ENVP_SIZE 128
1824 #define MONITOR_BUF_SIZE 4096
1825 static int update_envp_monitor_buf(struct udev_device *udev_device)
1827 struct udev_list_entry *list_entry;
1832 /* monitor buffer of property strings */
1833 free(udev_device->monitor_buf);
1834 udev_device->monitor_buf_len = 0;
1835 udev_device->monitor_buf = malloc(MONITOR_BUF_SIZE);
1836 if (udev_device->monitor_buf == NULL)
1839 /* envp array, strings will point into monitor buffer */
1840 if (udev_device->envp == NULL)
1841 udev_device->envp = malloc(sizeof(char *) * ENVP_SIZE);
1842 if (udev_device->envp == NULL)
1846 s = udev_device->monitor_buf;
1847 l = MONITOR_BUF_SIZE;
1848 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device)) {
1851 key = udev_list_entry_get_name(list_entry);
1852 /* skip private variables */
1856 /* add string to envp array */
1857 udev_device->envp[i++] = s;
1858 if (i+1 >= ENVP_SIZE)
1861 /* add property string to monitor buffer */
1862 l = strpcpyl(&s, l, key, "=", udev_list_entry_get_value(list_entry), NULL);
1865 /* advance past the trailing '\0' that strpcpyl() guarantees */
1869 udev_device->envp[i] = NULL;
1870 udev_device->monitor_buf_len = s - udev_device->monitor_buf;
1871 udev_device->envp_uptodate = true;
1875 char **udev_device_get_properties_envp(struct udev_device *udev_device)
1877 if (!udev_device->envp_uptodate)
1878 if (update_envp_monitor_buf(udev_device) != 0)
1880 return udev_device->envp;
1883 ssize_t udev_device_get_properties_monitor_buf(struct udev_device *udev_device, const char **buf)
1885 if (!udev_device->envp_uptodate)
1886 if (update_envp_monitor_buf(udev_device) != 0)
1888 *buf = udev_device->monitor_buf;
1889 return udev_device->monitor_buf_len;
1892 int udev_device_set_action(struct udev_device *udev_device, const char *action)
1894 free(udev_device->action);
1895 udev_device->action = strdup(action);
1896 if (udev_device->action == NULL)
1898 udev_device_add_property_internal(udev_device, "ACTION", udev_device->action);
1902 int udev_device_get_devlink_priority(struct udev_device *udev_device)
1904 if (!udev_device->info_loaded)
1905 udev_device_read_db(udev_device);
1906 return udev_device->devlink_priority;
1909 int udev_device_set_devlink_priority(struct udev_device *udev_device, int prio)
1911 udev_device->devlink_priority = prio;
1915 int udev_device_get_watch_handle(struct udev_device *udev_device)
1917 if (!udev_device->info_loaded)
1918 udev_device_read_db(udev_device);
1919 return udev_device->watch_handle;
1922 int udev_device_set_watch_handle(struct udev_device *udev_device, int handle)
1924 udev_device->watch_handle = handle;
1928 bool udev_device_get_db_persist(struct udev_device *udev_device)
1930 return udev_device->db_persist;
1933 void udev_device_set_db_persist(struct udev_device *udev_device)
1935 udev_device->db_persist = true;
1938 int udev_device_rename(struct udev_device *udev_device, const char *name)
1940 _cleanup_free_ char *dirname = NULL;
1941 const char *interface;
1945 if (udev_device == NULL || name == NULL)
1948 dirname = dirname_malloc(udev_device->syspath);
1952 new_syspath = strjoina(dirname, "/", name);
1954 r = udev_device_set_syspath(udev_device, new_syspath);
1958 interface = udev_device_get_property_value(udev_device, "INTERFACE");
1960 /* like DEVPATH_OLD, INTERFACE_OLD is not saved to the db, but only stays around for the current event */
1961 udev_device_add_property_internal(udev_device, "INTERFACE_OLD", interface);
1962 udev_device_add_property_internal(udev_device, "INTERFACE", name);
1968 struct udev_device *udev_device_shallow_clone(struct udev_device *old_device)
1970 struct udev_device *device;
1972 if (old_device == NULL)
1975 device = udev_device_new(old_device->udev);
1982 udev_device_set_syspath(device, udev_device_get_syspath(old_device));
1983 udev_device_set_subsystem(device, udev_device_get_subsystem(old_device));
1984 udev_device_set_devnum(device, udev_device_get_devnum(old_device));
1989 struct udev_device *udev_device_clone_with_db(struct udev_device *old_device)
1991 struct udev_device *device;
1993 device = udev_device_shallow_clone(old_device);
1997 udev_device_read_db(device);
1998 udev_device_set_info_loaded(device);
2003 struct udev_device *udev_device_new_from_nulstr(struct udev *udev, char *nulstr, ssize_t buflen) {
2004 struct udev_device *device;
2007 if (nulstr == NULL || buflen <= 0) {
2013 device = udev_device_new(udev);
2020 udev_device_set_info_loaded(device);
2022 while (bufpos < buflen) {
2026 key = nulstr + bufpos;
2027 keylen = strlen(key);
2031 bufpos += keylen + 1;
2032 udev_device_add_property_from_string_parse(device, key);
2035 if (udev_device_add_property_from_string_parse_finish(device) < 0) {
2036 log_debug("missing values, invalid device");
2038 udev_device_unref(device);
2048 int udev_device_copy_properties(struct udev_device *dst, struct udev_device *src) {
2049 struct udev_list_entry *entry;
2051 for ((entry = udev_device_get_properties_list_entry(src)); entry; entry = udev_list_entry_get_next(entry)) {
2052 const char *key, *value;
2054 key = udev_list_entry_get_name(entry);
2055 value = udev_list_entry_get_value(entry);
2057 udev_device_add_property(dst, key, value);