1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
10 #include "alloc-util.h"
12 #include "device-internal.h"
13 #include "device-private.h"
14 #include "device-util.h"
15 #include "devnum-util.h"
16 #include "dirent-util.h"
20 #include "format-util.h"
23 #include "id128-util.h"
25 #include "missing_magic.h"
26 #include "netlink-util.h"
27 #include "parse-util.h"
28 #include "path-util.h"
30 #include "socket-util.h"
31 #include "stdio-util.h"
32 #include "string-util.h"
35 #include "user-util.h"
37 int device_new_aux(sd_device
**ret
) {
42 device
= new(sd_device
, 1);
46 *device
= (sd_device
) {
48 .devmode
= MODE_INVALID
,
49 .devuid
= UID_INVALID
,
50 .devgid
= GID_INVALID
,
51 .action
= _SD_DEVICE_ACTION_INVALID
,
58 static sd_device
*device_free(sd_device
*device
) {
61 sd_device_unref(device
->parent
);
62 free(device
->syspath
);
63 free(device
->sysname
);
64 free(device
->devtype
);
65 free(device
->devname
);
66 free(device
->subsystem
);
67 free(device
->driver_subsystem
);
69 free(device
->device_id
);
70 free(device
->properties_strv
);
71 free(device
->properties_nulstr
);
73 ordered_hashmap_free(device
->properties
);
74 ordered_hashmap_free(device
->properties_db
);
75 hashmap_free(device
->sysattr_values
);
76 set_free(device
->sysattrs
);
77 set_free(device
->all_tags
);
78 set_free(device
->current_tags
);
79 set_free(device
->devlinks
);
80 hashmap_free(device
->children
);
85 DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_device
, sd_device
, device_free
);
87 int device_add_property_aux(sd_device
*device
, const char *key
, const char *value
, bool db
) {
88 OrderedHashmap
**properties
;
94 properties
= &device
->properties_db
;
96 properties
= &device
->properties
;
99 _unused_ _cleanup_free_
char *old_value
= NULL
;
100 _cleanup_free_
char *new_key
= NULL
, *new_value
= NULL
, *old_key
= NULL
;
103 r
= ordered_hashmap_ensure_allocated(properties
, &string_hash_ops_free_free
);
107 new_key
= strdup(key
);
111 new_value
= strdup(value
);
115 old_value
= ordered_hashmap_get2(*properties
, key
, (void**) &old_key
);
117 /* ordered_hashmap_replace() does not fail when the hashmap already has the entry. */
118 r
= ordered_hashmap_replace(*properties
, new_key
, new_value
);
125 _unused_ _cleanup_free_
char *old_value
= NULL
;
126 _cleanup_free_
char *old_key
= NULL
;
128 old_value
= ordered_hashmap_remove2(*properties
, key
, (void**) &old_key
);
132 device
->properties_generation
++;
133 device
->properties_buf_outdated
= true;
139 int device_set_syspath(sd_device
*device
, const char *_syspath
, bool verify
) {
140 _cleanup_free_
char *syspath
= NULL
;
148 _cleanup_close_
int fd
= -EBADF
;
150 /* The input path maybe a symlink located outside of /sys. Let's try to chase the symlink at first.
151 * The primary use case is that e.g. /proc/device-tree is a symlink to /sys/firmware/devicetree/base.
152 * By chasing symlinks in the path at first, we can call sd_device_new_from_path() with such path. */
153 r
= chase(_syspath
, NULL
, 0, &syspath
, &fd
);
155 /* the device does not exist (any more?) */
156 return log_debug_errno(SYNTHETIC_ERRNO(ENODEV
),
157 "sd-device: Failed to chase symlinks in \"%s\".", _syspath
);
159 return log_debug_errno(r
, "sd-device: Failed to get target of '%s': %m", _syspath
);
161 if (!path_startswith(syspath
, "/sys")) {
162 _cleanup_free_
char *real_sys
= NULL
, *new_syspath
= NULL
;
165 /* /sys is a symlink to somewhere sysfs is mounted on? In that case, we convert the path to real sysfs to "/sys". */
166 r
= chase("/sys", NULL
, 0, &real_sys
, NULL
);
168 return log_debug_errno(r
, "sd-device: Failed to chase symlink /sys: %m");
170 p
= path_startswith(syspath
, real_sys
);
172 return log_debug_errno(SYNTHETIC_ERRNO(ENODEV
),
173 "sd-device: Canonicalized path '%s' does not starts with sysfs mount point '%s'",
176 new_syspath
= path_join("/sys", p
);
178 return log_oom_debug();
180 free_and_replace(syspath
, new_syspath
);
181 path_simplify(syspath
);
184 if (path_startswith(syspath
, "/sys/devices/")) {
185 /* For proper devices, stricter rules apply: they must have a 'uevent' file,
186 * otherwise we won't allow them */
188 if (faccessat(fd
, "uevent", F_OK
, 0) < 0) {
190 /* This is not a valid device. Note, this condition is quite often
191 * satisfied when enumerating devices or finding a parent device.
192 * Hence, use log_trace_errno() here. */
193 return log_trace_errno(SYNTHETIC_ERRNO(ENODEV
),
194 "sd-device: the uevent file \"%s/uevent\" does not exist.", syspath
);
195 if (errno
== ENOTDIR
)
196 /* Not actually a directory. */
197 return log_debug_errno(SYNTHETIC_ERRNO(ENODEV
),
198 "sd-device: the syspath \"%s\" is not a directory.", syspath
);
200 return log_debug_errno(errno
, "sd-device: cannot find uevent file for %s: %m", syspath
);
205 /* For everything else lax rules apply: they just need to be a directory */
207 if (fstat(fd
, &st
) < 0)
208 return log_debug_errno(errno
, "sd-device: failed to check if syspath \"%s\" is a directory: %m", syspath
);
209 if (!S_ISDIR(st
.st_mode
))
210 return log_debug_errno(SYNTHETIC_ERRNO(ENODEV
),
211 "sd-device: the syspath \"%s\" is not a directory.", syspath
);
214 /* Only operate on sysfs, i.e. refuse going down into /sys/fs/cgroup/ or similar places where
215 * things are not arranged as kobjects in kernel, and hence don't necessarily have
216 * kobject/attribute structure. */
217 r
= getenv_bool_secure("SYSTEMD_DEVICE_VERIFY_SYSFS");
218 if (r
< 0 && r
!= -ENXIO
)
219 log_debug_errno(r
, "Failed to parse $SYSTEMD_DEVICE_VERIFY_SYSFS value: %m");
221 r
= fd_is_fs_type(fd
, SYSFS_MAGIC
);
223 return log_debug_errno(r
, "sd-device: failed to check if syspath \"%s\" is backed by sysfs.", syspath
);
225 return log_debug_errno(SYNTHETIC_ERRNO(ENODEV
),
226 "sd-device: the syspath \"%s\" is outside of sysfs, refusing.", syspath
);
229 /* must be a subdirectory of /sys */
230 if (!path_startswith(_syspath
, "/sys/"))
231 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL
),
232 "sd-device: Syspath '%s' is not a subdirectory of /sys",
235 r
= path_simplify_alloc(_syspath
, &syspath
);
240 assert_se(devpath
= startswith(syspath
, "/sys"));
241 if (devpath
[0] != '/')
242 return log_debug_errno(SYNTHETIC_ERRNO(ENODEV
), "sd-device: \"/sys\" alone is not a valid device path.");
244 r
= device_add_property_internal(device
, "DEVPATH", devpath
);
246 return log_debug_errno(r
, "sd-device: Failed to add \"DEVPATH\" property for device \"%s\": %m", syspath
);
248 free_and_replace(device
->syspath
, syspath
);
249 device
->devpath
= devpath
;
251 /* Unset sysname and sysnum, they will be assigned when requested. */
252 device
->sysnum
= NULL
;
253 device
->sysname
= mfree(device
->sysname
);
257 static int device_new_from_syspath(sd_device
**ret
, const char *syspath
, bool strict
) {
258 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
261 assert_return(ret
, -EINVAL
);
262 assert_return(syspath
, -EINVAL
);
264 if (strict
&& !path_startswith(syspath
, "/sys/"))
267 r
= device_new_aux(&device
);
271 r
= device_set_syspath(device
, syspath
, /* verify= */ true);
275 *ret
= TAKE_PTR(device
);
279 _public_
int sd_device_new_from_syspath(sd_device
**ret
, const char *syspath
) {
280 return device_new_from_syspath(ret
, syspath
, /* strict = */ true);
283 int device_new_from_mode_and_devnum(sd_device
**ret
, mode_t mode
, dev_t devnum
) {
284 _cleanup_(sd_device_unrefp
) sd_device
*dev
= NULL
;
285 _cleanup_free_
char *syspath
= NULL
;
294 else if (S_ISBLK(mode
))
299 if (major(devnum
) == 0)
302 if (asprintf(&syspath
, "/sys/dev/%s/" DEVNUM_FORMAT_STR
, t
, DEVNUM_FORMAT_VAL(devnum
)) < 0)
305 r
= sd_device_new_from_syspath(&dev
, syspath
);
309 r
= sd_device_get_devnum(dev
, &n
);
317 if (device_in_subsystem(dev
, "block") != !!S_ISBLK(mode
))
320 *ret
= TAKE_PTR(dev
);
324 _public_
int sd_device_new_from_devnum(sd_device
**ret
, char type
, dev_t devnum
) {
325 assert_return(ret
, -EINVAL
);
326 assert_return(IN_SET(type
, 'b', 'c'), -EINVAL
);
328 return device_new_from_mode_and_devnum(ret
, type
== 'b' ? S_IFBLK
: S_IFCHR
, devnum
);
331 static int device_new_from_main_ifname(sd_device
**ret
, const char *ifname
) {
337 syspath
= strjoina("/sys/class/net/", ifname
);
338 return sd_device_new_from_syspath(ret
, syspath
);
341 _public_
int sd_device_new_from_ifname(sd_device
**ret
, const char *ifname
) {
342 _cleanup_free_
char *main_name
= NULL
;
345 assert_return(ret
, -EINVAL
);
346 assert_return(ifname
, -EINVAL
);
348 r
= parse_ifindex(ifname
);
350 return sd_device_new_from_ifindex(ret
, r
);
352 if (ifname_valid(ifname
)) {
353 r
= device_new_from_main_ifname(ret
, ifname
);
358 r
= rtnl_resolve_link_alternative_name(NULL
, ifname
, &main_name
);
362 return device_new_from_main_ifname(ret
, main_name
);
365 _public_
int sd_device_new_from_ifindex(sd_device
**ret
, int ifindex
) {
366 _cleanup_(sd_device_unrefp
) sd_device
*dev
= NULL
;
367 char ifname
[IF_NAMESIZE
];
370 assert_return(ret
, -EINVAL
);
371 assert_return(ifindex
> 0, -EINVAL
);
373 if (format_ifname(ifindex
, ifname
) < 0)
376 r
= device_new_from_main_ifname(&dev
, ifname
);
380 r
= sd_device_get_ifindex(dev
, &i
);
388 *ret
= TAKE_PTR(dev
);
392 static int device_strjoin_new(
402 p
= strjoina(a
, b
, c
, d
);
403 if (access(p
, F_OK
) < 0)
404 return IN_SET(errno
, ENOENT
, ENAMETOOLONG
) ? 0 : -errno
; /* If this sysfs is too long then it doesn't exist either */
406 r
= sd_device_new_from_syspath(ret
, p
);
413 _public_
int sd_device_new_from_subsystem_sysname(
415 const char *subsystem
,
416 const char *sysname
) {
421 assert_return(ret
, -EINVAL
);
422 assert_return(subsystem
, -EINVAL
);
423 assert_return(sysname
, -EINVAL
);
425 if (!path_is_normalized(subsystem
))
427 if (!path_is_normalized(sysname
))
430 /* translate sysname back to sysfs filename */
431 name
= strdupa_safe(sysname
);
432 string_replace_char(name
, '/', '!');
434 if (streq(subsystem
, "subsystem")) {
435 FOREACH_STRING(s
, "/sys/bus/", "/sys/class/") {
436 r
= device_strjoin_new(s
, name
, NULL
, NULL
, ret
);
443 } else if (streq(subsystem
, "module")) {
444 r
= device_strjoin_new("/sys/module/", name
, NULL
, NULL
, ret
);
450 } else if (streq(subsystem
, "drivers")) {
453 sep
= strchr(name
, ':');
454 if (sep
&& sep
[1] != '\0') { /* Require ":" and something non-empty after that. */
456 const char *subsys
= memdupa_suffix0(name
, sep
- name
);
459 if (streq(sep
, "drivers")) /* If the sysname is "drivers", then it's the drivers directory itself that is meant. */
460 r
= device_strjoin_new("/sys/bus/", subsys
, "/drivers", NULL
, ret
);
462 r
= device_strjoin_new("/sys/bus/", subsys
, "/drivers/", sep
, ret
);
470 r
= device_strjoin_new("/sys/bus/", subsystem
, "/devices/", name
, ret
);
476 r
= device_strjoin_new("/sys/class/", subsystem
, "/", name
, ret
);
482 r
= device_strjoin_new("/sys/firmware/", subsystem
, "/", name
, ret
);
491 _public_
int sd_device_new_from_stat_rdev(sd_device
**ret
, const struct stat
*st
) {
492 assert_return(ret
, -EINVAL
);
493 assert_return(st
, -EINVAL
);
495 return device_new_from_mode_and_devnum(ret
, st
->st_mode
, st
->st_rdev
);
498 _public_
int sd_device_new_from_devname(sd_device
**ret
, const char *devname
) {
503 assert_return(ret
, -EINVAL
);
504 assert_return(devname
, -EINVAL
);
506 /* This function actually accepts both devlinks and devnames, i.e. both symlinks and device
507 * nodes below /dev/. */
509 /* Also ignore when the specified path is "/dev". */
510 if (isempty(path_startswith(devname
, "/dev")))
513 if (device_path_parse_major_minor(devname
, &mode
, &devnum
) >= 0)
514 /* Let's shortcut when "/dev/block/maj:min" or "/dev/char/maj:min" is specified.
515 * In that case, we can directly convert the path to syspath, hence it is not necessary
516 * that the specified path exists. So, this works fine without udevd being running. */
517 return device_new_from_mode_and_devnum(ret
, mode
, devnum
);
519 if (stat(devname
, &st
) < 0)
520 return ERRNO_IS_DEVICE_ABSENT(errno
) ? -ENODEV
: -errno
;
522 return sd_device_new_from_stat_rdev(ret
, &st
);
525 _public_
int sd_device_new_from_path(sd_device
**ret
, const char *path
) {
526 assert_return(ret
, -EINVAL
);
527 assert_return(path
, -EINVAL
);
529 if (path_startswith(path
, "/dev"))
530 return sd_device_new_from_devname(ret
, path
);
532 return device_new_from_syspath(ret
, path
, /* strict = */ false);
535 int device_set_devtype(sd_device
*device
, const char *devtype
) {
536 _cleanup_free_
char *t
= NULL
;
546 r
= device_add_property_internal(device
, "DEVTYPE", t
);
550 return free_and_replace(device
->devtype
, t
);
553 int device_set_ifindex(sd_device
*device
, const char *name
) {
559 ifindex
= parse_ifindex(name
);
563 r
= device_add_property_internal(device
, "IFINDEX", name
);
567 device
->ifindex
= ifindex
;
572 static int mangle_devname(const char *p
, char **ret
) {
578 if (!path_is_safe(p
))
581 /* When the path is absolute, it must start with "/dev/", but ignore "/dev/" itself. */
582 if (path_is_absolute(p
)) {
583 if (isempty(path_startswith(p
, "/dev/")))
588 q
= path_join("/dev/", p
);
598 int device_set_devname(sd_device
*device
, const char *devname
) {
599 _cleanup_free_
char *t
= NULL
;
605 r
= mangle_devname(devname
, &t
);
609 r
= device_add_property_internal(device
, "DEVNAME", t
);
613 return free_and_replace(device
->devname
, t
);
616 int device_set_devmode(sd_device
*device
, const char *_devmode
) {
623 r
= safe_atou(_devmode
, &devmode
);
630 r
= device_add_property_internal(device
, "DEVMODE", _devmode
);
634 device
->devmode
= devmode
;
639 int device_set_devnum(sd_device
*device
, const char *major
, const char *minor
) {
640 unsigned maj
, min
= 0;
646 r
= safe_atou(major
, &maj
);
651 if (!DEVICE_MAJOR_VALID(maj
))
655 r
= safe_atou(minor
, &min
);
658 if (!DEVICE_MINOR_VALID(min
))
662 r
= device_add_property_internal(device
, "MAJOR", major
);
667 r
= device_add_property_internal(device
, "MINOR", minor
);
672 device
->devnum
= makedev(maj
, min
);
677 int device_set_diskseq(sd_device
*device
, const char *str
) {
684 r
= safe_atou64(str
, &diskseq
);
690 r
= device_add_property_internal(device
, "DISKSEQ", str
);
694 device
->diskseq
= diskseq
;
699 static int handle_uevent_line(
704 const char **minor
) {
712 if (streq(key
, "DEVTYPE"))
713 return device_set_devtype(device
, value
);
714 if (streq(key
, "IFINDEX"))
715 return device_set_ifindex(device
, value
);
716 if (streq(key
, "DEVNAME"))
717 return device_set_devname(device
, value
);
718 if (streq(key
, "DEVMODE"))
719 return device_set_devmode(device
, value
);
720 if (streq(key
, "DISKSEQ"))
721 return device_set_diskseq(device
, value
);
722 if (streq(key
, "MAJOR"))
724 else if (streq(key
, "MINOR"))
727 return device_add_property_internal(device
, key
, value
);
732 int device_read_uevent_file(sd_device
*device
) {
733 _cleanup_free_
char *uevent
= NULL
;
734 const char *syspath
, *key
= NULL
, *value
= NULL
, *major
= NULL
, *minor
= NULL
;
749 if (device
->uevent_loaded
|| device
->sealed
)
752 r
= sd_device_get_syspath(device
, &syspath
);
756 device
->uevent_loaded
= true;
758 path
= strjoina(syspath
, "/uevent");
760 r
= read_full_virtual_file(path
, &uevent
, &uevent_len
);
761 if (r
== -EACCES
|| ERRNO_IS_NEG_DEVICE_ABSENT(r
))
762 /* The uevent files may be write-only, the device may be already removed, or the device
763 * may not have the uevent file. */
766 return log_device_debug_errno(device
, r
, "sd-device: Failed to read uevent file '%s': %m", path
);
768 for (size_t i
= 0; i
< uevent_len
; i
++)
771 if (!strchr(NEWLINE
, uevent
[i
])) {
779 if (uevent
[i
] == '=') {
783 } else if (strchr(NEWLINE
, uevent
[i
])) {
785 log_device_debug(device
, "sd-device: Invalid uevent line '%s', ignoring", key
);
795 _fallthrough_
; /* to handle empty property */
797 if (strchr(NEWLINE
, uevent
[i
])) {
800 r
= handle_uevent_line(device
, key
, value
, &major
, &minor
);
802 log_device_debug_errno(device
, r
, "sd-device: Failed to handle uevent entry '%s=%s', ignoring: %m", key
, value
);
809 assert_not_reached();
813 r
= device_set_devnum(device
, major
, minor
);
815 log_device_debug_errno(device
, r
, "sd-device: Failed to set 'MAJOR=%s' or 'MINOR=%s' from '%s', ignoring: %m", major
, strna(minor
), path
);
821 _public_
int sd_device_get_ifindex(sd_device
*device
, int *ifindex
) {
824 assert_return(device
, -EINVAL
);
826 r
= device_read_uevent_file(device
);
830 if (device
->ifindex
<= 0)
834 *ifindex
= device
->ifindex
;
839 _public_
int sd_device_new_from_device_id(sd_device
**ret
, const char *id
) {
842 assert_return(ret
, -EINVAL
);
843 assert_return(id
, -EINVAL
);
853 r
= parse_devnum(id
+ 1, &devt
);
857 return sd_device_new_from_devnum(ret
, id
[0], devt
);
863 ifindex
= parse_ifindex(id
+ 1);
867 return sd_device_new_from_ifindex(ret
, ifindex
);
871 const char *subsys
, *sep
;
873 sep
= strchr(id
+ 1, ':');
874 if (!sep
|| sep
- id
- 1 > NAME_MAX
)
877 subsys
= memdupa_suffix0(id
+ 1, sep
- id
- 1);
879 return sd_device_new_from_subsystem_sysname(ret
, subsys
, sep
+ 1);
887 _public_
int sd_device_get_syspath(sd_device
*device
, const char **ret
) {
888 assert_return(device
, -EINVAL
);
890 assert(path_startswith(device
->syspath
, "/sys/"));
893 *ret
= device
->syspath
;
898 DEFINE_PRIVATE_HASH_OPS_FULL(
899 device_by_path_hash_ops
,
900 char, path_hash_func
, path_compare
, free
,
901 sd_device
, sd_device_unref
);
903 static int device_enumerate_children_internal(sd_device
*device
, const char *subdir
, Set
**stack
, Hashmap
**children
) {
904 _cleanup_closedir_
DIR *dir
= NULL
;
911 r
= device_opendir(device
, subdir
, &dir
);
915 FOREACH_DIRENT_ALL(de
, dir
, return -errno
) {
916 _cleanup_(sd_device_unrefp
) sd_device
*child
= NULL
;
917 _cleanup_free_
char *p
= NULL
;
919 if (dot_or_dot_dot(de
->d_name
))
922 if (!IN_SET(de
->d_type
, DT_LNK
, DT_DIR
))
926 p
= path_join(subdir
, de
->d_name
);
928 p
= strdup(de
->d_name
);
932 /* Try to create child device. */
933 r
= sd_device_new_child(&child
, device
, p
);
935 /* OK, this is a child device, saving it. */
936 r
= hashmap_ensure_put(children
, &device_by_path_hash_ops
, p
, child
);
942 } else if (r
== -ENODEV
) {
943 /* This is not a child device. Push the sub-directory into stack, and read it later. */
945 if (de
->d_type
== DT_LNK
)
946 /* Do not follow symlinks, otherwise, we will enter an infinite loop, e.g.,
947 * /sys/class/block/nvme0n1/subsystem/nvme0n1/subsystem/nvme0n1/subsystem/… */
950 r
= set_ensure_consume(stack
, &path_hash_ops_free
, TAKE_PTR(p
));
960 static int device_enumerate_children(sd_device
*device
) {
961 _cleanup_hashmap_free_ Hashmap
*children
= NULL
;
962 _cleanup_set_free_ Set
*stack
= NULL
;
967 if (device
->children_enumerated
)
968 return 0; /* Already enumerated. */
970 r
= device_enumerate_children_internal(device
, NULL
, &stack
, &children
);
975 _cleanup_free_
char *subdir
= NULL
;
977 subdir
= set_steal_first(stack
);
981 r
= device_enumerate_children_internal(device
, subdir
, &stack
, &children
);
986 device
->children_enumerated
= true;
987 device
->children
= TAKE_PTR(children
);
988 return 1; /* Enumerated. */
991 _public_ sd_device
*sd_device_get_child_first(sd_device
*device
, const char **ret_suffix
) {
996 r
= device_enumerate_children(device
);
998 log_device_debug_errno(device
, r
, "sd-device: failed to enumerate child devices: %m");
1004 device
->children_iterator
= ITERATOR_FIRST
;
1006 return sd_device_get_child_next(device
, ret_suffix
);
1009 _public_ sd_device
*sd_device_get_child_next(sd_device
*device
, const char **ret_suffix
) {
1014 (void) hashmap_iterate(device
->children
, &device
->children_iterator
, (void**) &child
, (const void**) ret_suffix
);
1018 _public_
int sd_device_new_child(sd_device
**ret
, sd_device
*device
, const char *suffix
) {
1019 _cleanup_free_
char *path
= NULL
;
1024 assert_return(ret
, -EINVAL
);
1025 assert_return(device
, -EINVAL
);
1026 assert_return(suffix
, -EINVAL
);
1028 if (!path_is_safe(suffix
))
1031 /* If we have already enumerated children, try to find the child from the cache. */
1032 child
= hashmap_get(device
->children
, suffix
);
1034 *ret
= sd_device_ref(child
);
1038 r
= sd_device_get_syspath(device
, &s
);
1042 path
= path_join(s
, suffix
);
1046 return sd_device_new_from_syspath(ret
, path
);
1049 static int device_new_from_child(sd_device
**ret
, sd_device
*child
) {
1050 _cleanup_free_
char *path
= NULL
;
1051 const char *syspath
;
1057 r
= sd_device_get_syspath(child
, &syspath
);
1062 _cleanup_free_
char *p
= NULL
;
1064 r
= path_extract_directory(path
?: syspath
, &p
);
1068 if (path_equal(p
, "/sys"))
1071 r
= sd_device_new_from_syspath(ret
, p
);
1075 free_and_replace(path
, p
);
1079 _public_
int sd_device_get_parent(sd_device
*child
, sd_device
**ret
) {
1082 assert_return(child
, -EINVAL
);
1084 if (!child
->parent_set
) {
1085 r
= device_new_from_child(&child
->parent
, child
);
1086 if (r
< 0 && r
!= -ENODEV
)
1089 child
->parent_set
= true;
1096 *ret
= child
->parent
;
1100 int device_set_subsystem(sd_device
*device
, const char *subsystem
) {
1101 _cleanup_free_
char *s
= NULL
;
1107 s
= strdup(subsystem
);
1112 r
= device_add_property_internal(device
, "SUBSYSTEM", s
);
1116 device
->subsystem_set
= true;
1117 return free_and_replace(device
->subsystem
, s
);
1120 int device_set_drivers_subsystem(sd_device
*device
) {
1121 _cleanup_free_
char *subsystem
= NULL
;
1122 const char *devpath
, *drivers
, *p
;
1127 r
= sd_device_get_devpath(device
, &devpath
);
1131 drivers
= strstr(devpath
, "/drivers/");
1133 drivers
= endswith(devpath
, "/drivers");
1137 /* Find the path component immediately before the "/drivers/" string */
1138 r
= path_find_last_component(devpath
, /* accept_dot_dot= */ false, &drivers
, &p
);
1144 subsystem
= strndup(p
, r
);
1148 r
= device_set_subsystem(device
, "drivers");
1152 return free_and_replace(device
->driver_subsystem
, subsystem
);
1155 _public_
int sd_device_get_subsystem(sd_device
*device
, const char **ret
) {
1158 assert_return(device
, -EINVAL
);
1160 if (!device
->subsystem_set
) {
1161 _cleanup_free_
char *subsystem
= NULL
;
1162 const char *syspath
;
1165 r
= sd_device_get_syspath(device
, &syspath
);
1169 /* read 'subsystem' link */
1170 path
= strjoina(syspath
, "/subsystem");
1171 r
= readlink_value(path
, &subsystem
);
1172 if (r
< 0 && r
!= -ENOENT
)
1173 return log_device_debug_errno(device
, r
,
1174 "sd-device: Failed to read subsystem for %s: %m",
1178 r
= device_set_subsystem(device
, subsystem
);
1179 /* use implicit names */
1180 else if (!isempty(path_startswith(device
->devpath
, "/module/")))
1181 r
= device_set_subsystem(device
, "module");
1182 else if (strstr(syspath
, "/drivers/") || endswith(syspath
, "/drivers"))
1183 r
= device_set_drivers_subsystem(device
);
1184 else if (!isempty(PATH_STARTSWITH_SET(device
->devpath
, "/class/", "/bus/")))
1185 r
= device_set_subsystem(device
, "subsystem");
1187 device
->subsystem_set
= true;
1191 return log_device_debug_errno(device
, r
,
1192 "sd-device: Failed to set subsystem for %s: %m",
1196 if (!device
->subsystem
)
1200 *ret
= device
->subsystem
;
1204 _public_
int sd_device_get_devtype(sd_device
*device
, const char **devtype
) {
1207 assert_return(device
, -EINVAL
);
1209 r
= device_read_uevent_file(device
);
1213 if (!device
->devtype
)
1217 *devtype
= device
->devtype
;
1219 return !!device
->devtype
;
1222 _public_
int sd_device_get_parent_with_subsystem_devtype(sd_device
*device
, const char *subsystem
, const char *devtype
, sd_device
**ret
) {
1225 assert_return(device
, -EINVAL
);
1226 assert_return(subsystem
, -EINVAL
);
1229 r
= sd_device_get_parent(device
, &device
);
1233 if (!device_in_subsystem(device
, subsystem
))
1236 if (devtype
&& !device_is_devtype(device
, devtype
))
1245 _public_
int sd_device_get_devnum(sd_device
*device
, dev_t
*devnum
) {
1248 assert_return(device
, -EINVAL
);
1250 r
= device_read_uevent_file(device
);
1254 if (major(device
->devnum
) <= 0)
1258 *devnum
= device
->devnum
;
1263 int device_set_driver(sd_device
*device
, const char *driver
) {
1264 _cleanup_free_
char *d
= NULL
;
1275 r
= device_add_property_internal(device
, "DRIVER", d
);
1279 device
->driver_set
= true;
1280 return free_and_replace(device
->driver
, d
);
1283 _public_
int sd_device_get_driver(sd_device
*device
, const char **ret
) {
1284 assert_return(device
, -EINVAL
);
1286 if (!device
->driver_set
) {
1287 _cleanup_free_
char *driver
= NULL
;
1288 const char *syspath
;
1292 r
= sd_device_get_syspath(device
, &syspath
);
1296 path
= strjoina(syspath
, "/driver");
1297 r
= readlink_value(path
, &driver
);
1298 if (r
< 0 && r
!= -ENOENT
)
1299 return log_device_debug_errno(device
, r
,
1300 "sd-device: readlink(\"%s\") failed: %m", path
);
1302 r
= device_set_driver(device
, driver
);
1304 return log_device_debug_errno(device
, r
,
1305 "sd-device: Failed to set driver \"%s\": %m", driver
);
1308 if (!device
->driver
)
1312 *ret
= device
->driver
;
1316 _public_
int sd_device_get_devpath(sd_device
*device
, const char **ret
) {
1317 assert_return(device
, -EINVAL
);
1319 assert(device
->devpath
);
1320 assert(device
->devpath
[0] == '/');
1323 *ret
= device
->devpath
;
1328 _public_
int sd_device_get_devname(sd_device
*device
, const char **devname
) {
1331 assert_return(device
, -EINVAL
);
1333 r
= device_read_uevent_file(device
);
1337 if (!device
->devname
)
1340 assert(!isempty(path_startswith(device
->devname
, "/dev/")));
1343 *devname
= device
->devname
;
1347 static int device_set_sysname_and_sysnum(sd_device
*device
) {
1348 _cleanup_free_
char *sysname
= NULL
;
1354 r
= path_extract_filename(device
->devpath
, &sysname
);
1357 if (r
== O_DIRECTORY
)
1360 /* some devices have '!' in their name, change that to '/' */
1361 string_replace_char(sysname
, '!', '/');
1363 n
= strspn_from_end(sysname
, DIGITS
);
1364 len
= strlen(sysname
);
1367 n
= 0; /* Do not set sysnum for number only sysname. */
1369 device
->sysnum
= n
> 0 ? sysname
+ len
- n
: NULL
;
1370 return free_and_replace(device
->sysname
, sysname
);
1373 _public_
int sd_device_get_sysname(sd_device
*device
, const char **ret
) {
1376 assert_return(device
, -EINVAL
);
1378 if (!device
->sysname
) {
1379 r
= device_set_sysname_and_sysnum(device
);
1385 *ret
= device
->sysname
;
1389 _public_
int sd_device_get_sysnum(sd_device
*device
, const char **ret
) {
1392 assert_return(device
, -EINVAL
);
1394 if (!device
->sysname
) {
1395 r
= device_set_sysname_and_sysnum(device
);
1400 if (!device
->sysnum
)
1404 *ret
= device
->sysnum
;
1408 _public_
int sd_device_get_action(sd_device
*device
, sd_device_action_t
*ret
) {
1409 assert_return(device
, -EINVAL
);
1411 if (device
->action
< 0)
1415 *ret
= device
->action
;
1420 _public_
int sd_device_get_seqnum(sd_device
*device
, uint64_t *ret
) {
1421 assert_return(device
, -EINVAL
);
1423 if (device
->seqnum
== 0)
1427 *ret
= device
->seqnum
;
1432 _public_
int sd_device_get_diskseq(sd_device
*device
, uint64_t *ret
) {
1435 assert_return(device
, -EINVAL
);
1437 r
= device_read_uevent_file(device
);
1441 if (device
->diskseq
== 0)
1445 *ret
= device
->diskseq
;
1450 static bool is_valid_tag(const char *tag
) {
1453 return in_charset(tag
, ALPHANUMERICAL
"-_") && filename_is_valid(tag
);
1456 int device_add_tag(sd_device
*device
, const char *tag
, bool both
) {
1462 if (!is_valid_tag(tag
))
1465 /* Definitely add to the "all" list of tags (i.e. the sticky list) */
1466 added
= set_put_strdup(&device
->all_tags
, tag
);
1470 /* And optionally, also add it to the current list of tags */
1472 r
= set_put_strdup(&device
->current_tags
, tag
);
1475 (void) set_remove(device
->all_tags
, tag
);
1481 device
->tags_generation
++;
1482 device
->property_tags_outdated
= true;
1487 int device_add_devlink(sd_device
*device
, const char *devlink
) {
1494 r
= mangle_devname(devlink
, &p
);
1498 r
= set_ensure_consume(&device
->devlinks
, &path_hash_ops_free
, p
);
1502 device
->devlinks_generation
++;
1503 device
->property_devlinks_outdated
= true;
1505 return r
; /* return 1 when newly added, 0 when already exists */
1508 int device_remove_devlink(sd_device
*device
, const char *devlink
) {
1509 _cleanup_free_
char *p
= NULL
, *s
= NULL
;
1515 r
= mangle_devname(devlink
, &p
);
1519 s
= set_remove(device
->devlinks
, p
);
1521 return 0; /* does not exist */
1523 device
->devlinks_generation
++;
1524 device
->property_devlinks_outdated
= true;
1525 return 1; /* removed */
1528 bool device_has_devlink(sd_device
*device
, const char *devlink
) {
1532 return set_contains(device
->devlinks
, devlink
);
1535 static int device_add_property_internal_from_string(sd_device
*device
, const char *str
) {
1536 _cleanup_free_
char *key
= NULL
;
1547 value
= strchr(key
, '=');
1553 if (isempty(++value
))
1556 /* Add the property to both sd_device::properties and sd_device::properties_db,
1557 * as this is called by only handle_db_line(). */
1558 r
= device_add_property_aux(device
, key
, value
, false);
1562 return device_add_property_aux(device
, key
, value
, true);
1565 int device_set_usec_initialized(sd_device
*device
, usec_t when
) {
1566 char s
[DECIMAL_STR_MAX(usec_t
)];
1571 xsprintf(s
, USEC_FMT
, when
);
1573 r
= device_add_property_internal(device
, "USEC_INITIALIZED", s
);
1577 device
->usec_initialized
= when
;
1581 static int handle_db_line(sd_device
*device
, char key
, const char *value
) {
1588 case 'G': /* Any tag */
1589 case 'Q': /* Current tag */
1590 return device_add_tag(device
, value
, key
== 'Q');
1595 path
= strjoina("/dev/", value
);
1596 return device_add_devlink(device
, path
);
1599 return device_add_property_internal_from_string(device
, value
);
1604 r
= safe_atou64(value
, &t
);
1608 return device_set_usec_initialized(device
, t
);
1611 return safe_atoi(value
, &device
->devlink_priority
);
1614 /* Deprecated. Previously, watch handle is both saved in database and /run/udev/watch.
1615 * However, the handle saved in database may not be updated when the handle is updated
1616 * or removed. Moreover, it is not necessary to store the handle within the database,
1617 * as its value becomes meaningless when udevd is restarted. */
1621 return safe_atou(value
, &device
->database_version
);
1624 log_device_debug(device
, "sd-device: Unknown key '%c' in device db, ignoring", key
);
1629 int device_get_device_id(sd_device
*device
, const char **ret
) {
1633 if (!device
->device_id
) {
1634 _cleanup_free_
char *id
= NULL
;
1635 const char *subsystem
;
1639 r
= sd_device_get_subsystem(device
, &subsystem
);
1643 if (sd_device_get_devnum(device
, &devnum
) >= 0) {
1644 /* use dev_t — b259:131072, c254:0 */
1645 if (asprintf(&id
, "%c" DEVNUM_FORMAT_STR
,
1646 streq(subsystem
, "block") ? 'b' : 'c',
1647 DEVNUM_FORMAT_VAL(devnum
)) < 0)
1649 } else if (sd_device_get_ifindex(device
, &ifindex
) >= 0) {
1650 /* use netdev ifindex — n3 */
1651 if (asprintf(&id
, "n%u", (unsigned) ifindex
) < 0)
1654 _cleanup_free_
char *sysname
= NULL
;
1656 /* use $subsys:$sysname — pci:0000:00:1f.2
1657 * sd_device_get_sysname() has '!' translated, get it from devpath */
1658 r
= path_extract_filename(device
->devpath
, &sysname
);
1661 if (r
== O_DIRECTORY
)
1664 if (streq(subsystem
, "drivers")) {
1665 /* the 'drivers' pseudo-subsystem is special, and needs the real
1666 * subsystem encoded as well */
1667 assert(device
->driver_subsystem
);
1668 id
= strjoin("+drivers:", device
->driver_subsystem
, ":", sysname
);
1670 id
= strjoin("+", subsystem
, ":", sysname
);
1675 if (!filename_is_valid(id
))
1678 device
->device_id
= TAKE_PTR(id
);
1681 *ret
= device
->device_id
;
1685 int device_read_db_internal_filename(sd_device
*device
, const char *filename
) {
1686 _cleanup_free_
char *db
= NULL
;
1689 char key
= '\0'; /* Unnecessary initialization to appease gcc-12.0.0-0.4.fc36 */
1703 r
= read_full_file(filename
, &db
, &db_len
);
1708 return log_device_debug_errno(device
, r
, "sd-device: Failed to read db '%s': %m", filename
);
1711 /* devices with a database entry are initialized */
1712 device
->is_initialized
= true;
1714 device
->db_loaded
= true;
1716 for (size_t i
= 0; i
< db_len
; i
++)
1719 if (!strchr(NEWLINE
, db
[i
])) {
1728 log_device_debug(device
, "sd-device: Invalid db entry with key '%c', ignoring", key
);
1730 state
= INVALID_LINE
;
1745 if (strchr(NEWLINE
, db
[i
]))
1750 if (strchr(NEWLINE
, db
[i
])) {
1752 r
= handle_db_line(device
, key
, value
);
1754 log_device_debug_errno(device
, r
, "sd-device: Failed to handle db entry '%c:%s', ignoring: %m",
1762 return log_device_debug_errno(device
, SYNTHETIC_ERRNO(EINVAL
), "sd-device: invalid db syntax.");
1768 _public_
int sd_device_get_is_initialized(sd_device
*device
) {
1771 assert_return(device
, -EINVAL
);
1773 r
= device_read_db(device
);
1775 /* The device may be already removed or renamed. */
1780 return device
->is_initialized
;
1783 _public_
int sd_device_get_usec_initialized(sd_device
*device
, uint64_t *ret
) {
1786 assert_return(device
, -EINVAL
);
1788 r
= sd_device_get_is_initialized(device
);
1794 if (device
->usec_initialized
== 0)
1798 *ret
= device
->usec_initialized
;
1803 _public_
int sd_device_get_usec_since_initialized(sd_device
*device
, uint64_t *ret
) {
1807 assert_return(device
, -EINVAL
);
1809 r
= sd_device_get_usec_initialized(device
, &ts
);
1813 now_ts
= now(CLOCK_MONOTONIC
);
1819 *ret
= usec_sub_unsigned(now_ts
, ts
);
1824 _public_
const char *sd_device_get_tag_first(sd_device
*device
) {
1827 assert_return(device
, NULL
);
1829 (void) device_read_db(device
);
1831 device
->all_tags_iterator_generation
= device
->tags_generation
;
1832 device
->all_tags_iterator
= ITERATOR_FIRST
;
1834 (void) set_iterate(device
->all_tags
, &device
->all_tags_iterator
, &v
);
1838 _public_
const char *sd_device_get_tag_next(sd_device
*device
) {
1841 assert_return(device
, NULL
);
1843 (void) device_read_db(device
);
1845 if (device
->all_tags_iterator_generation
!= device
->tags_generation
)
1848 (void) set_iterate(device
->all_tags
, &device
->all_tags_iterator
, &v
);
1852 static bool device_database_supports_current_tags(sd_device
*device
) {
1855 (void) device_read_db(device
);
1857 /* The current tags (saved in Q field) feature is implemented in database version 1.
1858 * If the database version is 0, then the tags (NOT current tags, saved in G field) are not
1859 * sticky. Thus, we can safely bypass the operations for the current tags (Q) to tags (G). */
1861 return device
->database_version
>= 1;
1864 _public_
const char *sd_device_get_current_tag_first(sd_device
*device
) {
1867 assert_return(device
, NULL
);
1869 if (!device_database_supports_current_tags(device
))
1870 return sd_device_get_tag_first(device
);
1872 (void) device_read_db(device
);
1874 device
->current_tags_iterator_generation
= device
->tags_generation
;
1875 device
->current_tags_iterator
= ITERATOR_FIRST
;
1877 (void) set_iterate(device
->current_tags
, &device
->current_tags_iterator
, &v
);
1881 _public_
const char *sd_device_get_current_tag_next(sd_device
*device
) {
1884 assert_return(device
, NULL
);
1886 if (!device_database_supports_current_tags(device
))
1887 return sd_device_get_tag_next(device
);
1889 (void) device_read_db(device
);
1891 if (device
->current_tags_iterator_generation
!= device
->tags_generation
)
1894 (void) set_iterate(device
->current_tags
, &device
->current_tags_iterator
, &v
);
1898 _public_
const char *sd_device_get_devlink_first(sd_device
*device
) {
1901 assert_return(device
, NULL
);
1903 (void) device_read_db(device
);
1905 device
->devlinks_iterator_generation
= device
->devlinks_generation
;
1906 device
->devlinks_iterator
= ITERATOR_FIRST
;
1908 (void) set_iterate(device
->devlinks
, &device
->devlinks_iterator
, &v
);
1912 _public_
const char *sd_device_get_devlink_next(sd_device
*device
) {
1915 assert_return(device
, NULL
);
1917 (void) device_read_db(device
);
1919 if (device
->devlinks_iterator_generation
!= device
->devlinks_generation
)
1922 (void) set_iterate(device
->devlinks
, &device
->devlinks_iterator
, &v
);
1926 int device_properties_prepare(sd_device
*device
) {
1931 r
= device_read_uevent_file(device
);
1935 r
= device_read_db(device
);
1939 if (device
->property_devlinks_outdated
) {
1940 _cleanup_free_
char *devlinks
= NULL
;
1942 r
= set_strjoin(device
->devlinks
, " ", false, &devlinks
);
1946 if (!isempty(devlinks
)) {
1947 r
= device_add_property_internal(device
, "DEVLINKS", devlinks
);
1952 device
->property_devlinks_outdated
= false;
1955 if (device
->property_tags_outdated
) {
1956 _cleanup_free_
char *tags
= NULL
;
1958 r
= set_strjoin(device
->all_tags
, ":", true, &tags
);
1962 if (!isempty(tags
)) {
1963 r
= device_add_property_internal(device
, "TAGS", tags
);
1969 r
= set_strjoin(device
->current_tags
, ":", true, &tags
);
1973 if (!isempty(tags
)) {
1974 r
= device_add_property_internal(device
, "CURRENT_TAGS", tags
);
1979 device
->property_tags_outdated
= false;
1985 _public_
const char *sd_device_get_property_first(sd_device
*device
, const char **_value
) {
1989 assert_return(device
, NULL
);
1991 r
= device_properties_prepare(device
);
1995 device
->properties_iterator_generation
= device
->properties_generation
;
1996 device
->properties_iterator
= ITERATOR_FIRST
;
1998 (void) ordered_hashmap_iterate(device
->properties
, &device
->properties_iterator
, (void**)_value
, (const void**)&key
);
2002 _public_
const char *sd_device_get_property_next(sd_device
*device
, const char **_value
) {
2006 assert_return(device
, NULL
);
2008 r
= device_properties_prepare(device
);
2012 if (device
->properties_iterator_generation
!= device
->properties_generation
)
2015 (void) ordered_hashmap_iterate(device
->properties
, &device
->properties_iterator
, (void**)_value
, (const void**)&key
);
2019 static int device_sysattrs_read_all_internal(sd_device
*device
, const char *subdir
, Set
**stack
) {
2020 _cleanup_closedir_
DIR *dir
= NULL
;
2026 r
= device_opendir(device
, subdir
, &dir
);
2027 if (r
== -ENOENT
&& subdir
)
2028 return 0; /* Maybe, this is a child device, and is already removed. */
2033 if (faccessat(dirfd(dir
), "uevent", F_OK
, 0) >= 0)
2034 return 0; /* this is a child device, skipping */
2035 if (errno
!= ENOENT
) {
2036 log_device_debug_errno(device
, errno
,
2037 "sd-device: Failed to access %s/uevent, ignoring sub-directory %s: %m",
2043 FOREACH_DIRENT_ALL(de
, dir
, return -errno
) {
2044 _cleanup_free_
char *p
= NULL
;
2045 struct stat statbuf
;
2047 if (dot_or_dot_dot(de
->d_name
))
2050 /* only handle symlinks, regular files, and directories */
2051 if (!IN_SET(de
->d_type
, DT_LNK
, DT_REG
, DT_DIR
))
2055 p
= path_join(subdir
, de
->d_name
);
2060 if (de
->d_type
== DT_DIR
) {
2061 /* push the sub-directory into the stack, and read it later. */
2063 r
= set_ensure_consume(stack
, &path_hash_ops_free
, TAKE_PTR(p
));
2065 r
= set_put_strdup_full(stack
, &path_hash_ops_free
, de
->d_name
);
2072 if (fstatat(dirfd(dir
), de
->d_name
, &statbuf
, AT_SYMLINK_NOFOLLOW
) < 0)
2075 if ((statbuf
.st_mode
& (S_IRUSR
| S_IWUSR
)) == 0)
2079 r
= set_ensure_consume(&device
->sysattrs
, &path_hash_ops_free
, TAKE_PTR(p
));
2081 r
= set_put_strdup_full(&device
->sysattrs
, &path_hash_ops_free
, de
->d_name
);
2089 static int device_sysattrs_read_all(sd_device
*device
) {
2090 _cleanup_set_free_ Set
*stack
= NULL
;
2095 if (device
->sysattrs_read
)
2098 r
= device_sysattrs_read_all_internal(device
, NULL
, &stack
);
2103 _cleanup_free_
char *subdir
= NULL
;
2105 subdir
= set_steal_first(stack
);
2109 r
= device_sysattrs_read_all_internal(device
, subdir
, &stack
);
2114 device
->sysattrs_read
= true;
2119 _public_
const char *sd_device_get_sysattr_first(sd_device
*device
) {
2123 assert_return(device
, NULL
);
2125 if (!device
->sysattrs_read
) {
2126 r
= device_sysattrs_read_all(device
);
2133 device
->sysattrs_iterator
= ITERATOR_FIRST
;
2135 (void) set_iterate(device
->sysattrs
, &device
->sysattrs_iterator
, &v
);
2139 _public_
const char *sd_device_get_sysattr_next(sd_device
*device
) {
2142 assert_return(device
, NULL
);
2144 if (!device
->sysattrs_read
)
2147 (void) set_iterate(device
->sysattrs
, &device
->sysattrs_iterator
, &v
);
2151 _public_
int sd_device_has_tag(sd_device
*device
, const char *tag
) {
2152 assert_return(device
, -EINVAL
);
2153 assert_return(tag
, -EINVAL
);
2155 (void) device_read_db(device
);
2157 return set_contains(device
->all_tags
, tag
);
2160 _public_
int sd_device_has_current_tag(sd_device
*device
, const char *tag
) {
2161 assert_return(device
, -EINVAL
);
2162 assert_return(tag
, -EINVAL
);
2164 if (!device_database_supports_current_tags(device
))
2165 return sd_device_has_tag(device
, tag
);
2167 (void) device_read_db(device
);
2169 return set_contains(device
->current_tags
, tag
);
2172 _public_
int sd_device_get_property_value(sd_device
*device
, const char *key
, const char **ret_value
) {
2176 assert_return(device
, -EINVAL
);
2177 assert_return(key
, -EINVAL
);
2179 r
= device_properties_prepare(device
);
2183 value
= ordered_hashmap_get(device
->properties
, key
);
2192 int device_get_property_bool(sd_device
*device
, const char *key
) {
2199 r
= sd_device_get_property_value(device
, key
, &value
);
2203 return parse_boolean(value
);
2206 int device_get_property_int(sd_device
*device
, const char *key
, int *ret
) {
2213 r
= sd_device_get_property_value(device
, key
, &value
);
2217 r
= safe_atoi(value
, &v
);
2226 _public_
int sd_device_get_trigger_uuid(sd_device
*device
, sd_id128_t
*ret
) {
2231 assert_return(device
, -EINVAL
);
2233 /* Retrieves the UUID attached to a uevent when triggering it from userspace via
2234 * sd_device_trigger_with_uuid() or an equivalent interface. Returns -ENOENT if the record is not
2235 * caused by a synthetic event and -ENODATA if it was but no UUID was specified */
2237 r
= sd_device_get_property_value(device
, "SYNTH_UUID", &s
);
2241 if (streq(s
, "0")) /* SYNTH_UUID=0 is set whenever a device is triggered by userspace without specifying a UUID */
2244 r
= sd_id128_from_string(s
, &id
);
2254 void device_clear_sysattr_cache(sd_device
*device
) {
2255 device
->sysattr_values
= hashmap_free(device
->sysattr_values
);
2258 int device_cache_sysattr_value(sd_device
*device
, const char *key
, char *value
) {
2259 _unused_ _cleanup_free_
char *old_value
= NULL
;
2260 _cleanup_free_
char *new_key
= NULL
;
2266 /* This takes the reference of the input value. The input value may be NULL.
2267 * This replaces the value if it already exists. */
2269 /* First, remove the old cache entry. So, we do not need to clear cache on error. */
2270 old_value
= hashmap_remove2(device
->sysattr_values
, key
, (void **) &new_key
);
2272 new_key
= strdup(key
);
2277 r
= hashmap_ensure_put(&device
->sysattr_values
, &path_hash_ops_free_free
, new_key
, value
);
2286 int device_get_cached_sysattr_value(sd_device
*device
, const char *key
, const char **ret_value
) {
2287 const char *k
= NULL
, *value
;
2292 value
= hashmap_get2(device
->sysattr_values
, key
, (void **) &k
);
2294 return -ESTALE
; /* We have not read the attribute. */
2296 return -ENOENT
; /* We have looked up the attribute before and it did not exist. */
2302 /* We cache all sysattr lookups. If an attribute does not exist, it is stored
2303 * with a NULL value in the cache, otherwise the returned string is stored */
2304 _public_
int sd_device_get_sysattr_value(sd_device
*device
, const char *sysattr
, const char **ret_value
) {
2305 _cleanup_free_
char *value
= NULL
, *path
= NULL
;
2306 const char *syspath
;
2307 struct stat statbuf
;
2310 assert_return(device
, -EINVAL
);
2311 assert_return(sysattr
, -EINVAL
);
2313 /* look for possibly already cached result */
2314 r
= device_get_cached_sysattr_value(device
, sysattr
, ret_value
);
2318 r
= sd_device_get_syspath(device
, &syspath
);
2322 path
= path_join(syspath
, sysattr
);
2326 if (lstat(path
, &statbuf
) < 0) {
2331 /* remember that we could not access the sysattr */
2332 k
= device_cache_sysattr_value(device
, sysattr
, NULL
);
2334 log_device_debug_errno(device
, k
,
2335 "sd-device: failed to cache attribute '%s' with NULL, ignoring: %m",
2339 } else if (S_ISLNK(statbuf
.st_mode
)) {
2340 /* Some core links return only the last element of the target path,
2341 * these are just values, the paths should not be exposed. */
2342 if (STR_IN_SET(sysattr
, "driver", "subsystem", "module")) {
2343 r
= readlink_value(path
, &value
);
2348 } else if (S_ISDIR(statbuf
.st_mode
))
2349 /* skip directories */
2351 else if (!(statbuf
.st_mode
& S_IRUSR
))
2352 /* skip non-readable files */
2357 /* Read attribute value, Some attributes contain embedded '\0'. So, it is necessary to
2358 * also get the size of the result. See issue #20025. */
2359 r
= read_full_virtual_file(path
, &value
, &size
);
2363 /* drop trailing newlines */
2364 while (size
> 0 && strchr(NEWLINE
, value
[--size
]))
2368 /* Unfortunately, we need to return 'const char*' instead of 'char*'. Hence, failure in caching
2369 * sysattr value is critical unlike the other places. */
2370 r
= device_cache_sysattr_value(device
, sysattr
, value
);
2372 log_device_debug_errno(device
, r
,
2373 "sd-device: failed to cache attribute '%s' with '%s'%s: %m",
2374 sysattr
, value
, ret_value
? "" : ", ignoring");
2388 int device_get_sysattr_int(sd_device
*device
, const char *sysattr
, int *ret_value
) {
2392 r
= sd_device_get_sysattr_value(device
, sysattr
, &value
);
2397 r
= safe_atoi(value
, &v
);
2399 return log_device_debug_errno(device
, r
, "Failed to parse '%s' attribute: %m", sysattr
);
2403 /* We return "true" if the value is positive. */
2407 int device_get_sysattr_unsigned(sd_device
*device
, const char *sysattr
, unsigned *ret_value
) {
2411 r
= sd_device_get_sysattr_value(device
, sysattr
, &value
);
2416 r
= safe_atou(value
, &v
);
2418 return log_device_debug_errno(device
, r
, "Failed to parse '%s' attribute: %m", sysattr
);
2422 /* We return "true" if the value is positive. */
2426 int device_get_sysattr_u32(sd_device
*device
, const char *sysattr
, uint32_t *ret_value
) {
2430 r
= sd_device_get_sysattr_value(device
, sysattr
, &value
);
2435 r
= safe_atou32(value
, &v
);
2437 return log_device_debug_errno(device
, r
, "Failed to parse '%s' attribute: %m", sysattr
);
2441 /* We return "true" if the value is positive. */
2445 int device_get_sysattr_bool(sd_device
*device
, const char *sysattr
) {
2452 r
= sd_device_get_sysattr_value(device
, sysattr
, &value
);
2456 return parse_boolean(value
);
2459 static void device_remove_cached_sysattr_value(sd_device
*device
, const char *_key
) {
2460 _cleanup_free_
char *key
= NULL
;
2465 free(hashmap_remove2(device
->sysattr_values
, _key
, (void **) &key
));
2468 _public_
int sd_device_set_sysattr_value(sd_device
*device
, const char *sysattr
, const char *_value
) {
2469 _cleanup_free_
char *value
= NULL
, *path
= NULL
;
2470 const char *syspath
;
2474 assert_return(device
, -EINVAL
);
2475 assert_return(sysattr
, -EINVAL
);
2477 /* Set the attribute and save it in the cache. */
2480 /* If input value is NULL, then clear cache and not write anything. */
2481 device_remove_cached_sysattr_value(device
, sysattr
);
2485 r
= sd_device_get_syspath(device
, &syspath
);
2489 path
= path_join(syspath
, sysattr
);
2493 len
= strlen(_value
);
2495 /* drop trailing newlines */
2496 while (len
> 0 && strchr(NEWLINE
, _value
[len
- 1]))
2499 /* value length is limited to 4k */
2503 value
= strndup(_value
, len
);
2507 r
= write_string_file(path
, value
, WRITE_STRING_FILE_DISABLE_BUFFER
| WRITE_STRING_FILE_NOFOLLOW
);
2509 /* On failure, clear cache entry, as we do not know how it fails. */
2510 device_remove_cached_sysattr_value(device
, sysattr
);
2514 /* Do not cache action string written into uevent file. */
2515 if (streq(sysattr
, "uevent"))
2518 r
= device_cache_sysattr_value(device
, sysattr
, value
);
2520 log_device_debug_errno(device
, r
,
2521 "sd-device: failed to cache attribute '%s' with '%s', ignoring: %m",
2529 _public_
int sd_device_set_sysattr_valuef(sd_device
*device
, const char *sysattr
, const char *format
, ...) {
2530 _cleanup_free_
char *value
= NULL
;
2534 assert_return(device
, -EINVAL
);
2535 assert_return(sysattr
, -EINVAL
);
2538 device_remove_cached_sysattr_value(device
, sysattr
);
2542 va_start(ap
, format
);
2543 r
= vasprintf(&value
, format
, ap
);
2549 return sd_device_set_sysattr_value(device
, sysattr
, value
);
2552 _public_
int sd_device_trigger(sd_device
*device
, sd_device_action_t action
) {
2555 assert_return(device
, -EINVAL
);
2557 s
= device_action_to_string(action
);
2561 /* This uses the simple no-UUID interface of kernel < 4.13 */
2562 return sd_device_set_sysattr_value(device
, "uevent", s
);
2565 _public_
int sd_device_trigger_with_uuid(
2567 sd_device_action_t action
,
2568 sd_id128_t
*ret_uuid
) {
2574 assert_return(device
, -EINVAL
);
2576 /* If no one wants to know the UUID, use the simple interface from pre-4.13 times */
2578 return sd_device_trigger(device
, action
);
2580 s
= device_action_to_string(action
);
2584 r
= sd_id128_randomize(&u
);
2588 j
= strjoina(s
, " ", SD_ID128_TO_UUID_STRING(u
));
2590 r
= sd_device_set_sysattr_value(device
, "uevent", j
);
2598 _public_
int sd_device_open(sd_device
*device
, int flags
) {
2599 _cleanup_close_
int fd
= -EBADF
, fd2
= -EBADF
;
2600 const char *devname
;
2601 uint64_t q
, diskseq
= 0;
2606 assert_return(device
, -EINVAL
);
2607 assert_return(FLAGS_SET(flags
, O_PATH
) || !FLAGS_SET(flags
, O_NOFOLLOW
), -EINVAL
);
2609 r
= sd_device_get_devname(device
, &devname
);
2615 r
= sd_device_get_devnum(device
, &devnum
);
2621 fd
= open(devname
, FLAGS_SET(flags
, O_PATH
) ? flags
: O_CLOEXEC
|O_NOFOLLOW
|O_PATH
);
2625 if (fstat(fd
, &st
) < 0)
2628 if (st
.st_rdev
!= devnum
)
2631 if (device_in_subsystem(device
, "block") ? !S_ISBLK(st
.st_mode
) : !S_ISCHR(st
.st_mode
))
2634 /* If flags has O_PATH, then we cannot check diskseq. Let's return earlier. */
2635 if (FLAGS_SET(flags
, O_PATH
))
2638 /* If the device is not initialized, then we cannot determine if we should check diskseq through
2639 * ID_IGNORE_DISKSEQ property. Let's skip to check diskseq in that case. */
2640 r
= sd_device_get_is_initialized(device
);
2644 r
= device_get_property_bool(device
, "ID_IGNORE_DISKSEQ");
2645 if (r
< 0 && r
!= -ENOENT
)
2648 r
= sd_device_get_diskseq(device
, &diskseq
);
2649 if (r
< 0 && r
!= -ENOENT
)
2654 fd2
= fd_reopen(fd
, flags
);
2659 return TAKE_FD(fd2
);
2661 r
= fd_get_diskseq(fd2
, &q
);
2668 return TAKE_FD(fd2
);
2671 int device_opendir(sd_device
*device
, const char *subdir
, DIR **ret
) {
2672 _cleanup_closedir_
DIR *d
= NULL
;
2673 _cleanup_free_
char *path
= NULL
;
2674 const char *syspath
;
2680 r
= sd_device_get_syspath(device
, &syspath
);
2685 if (!path_is_safe(subdir
))
2688 path
= path_join(syspath
, subdir
);
2693 d
= opendir(path
?: syspath
);