1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
10 #include "alloc-util.h"
11 #include "chase-symlinks.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"
38 int device_new_aux(sd_device
**ret
) {
43 device
= new(sd_device
, 1);
47 *device
= (sd_device
) {
50 .devmode
= MODE_INVALID
,
51 .devuid
= UID_INVALID
,
52 .devgid
= GID_INVALID
,
53 .action
= _SD_DEVICE_ACTION_INVALID
,
60 static sd_device
*device_free(sd_device
*device
) {
63 sd_device_unref(device
->parent
);
64 free(device
->syspath
);
65 free(device
->sysname
);
66 free(device
->devtype
);
67 free(device
->devname
);
68 free(device
->subsystem
);
69 free(device
->driver_subsystem
);
71 free(device
->device_id
);
72 free(device
->properties_strv
);
73 free(device
->properties_nulstr
);
75 ordered_hashmap_free(device
->properties
);
76 ordered_hashmap_free(device
->properties_db
);
77 hashmap_free(device
->sysattr_values
);
78 set_free(device
->sysattrs
);
79 set_free(device
->all_tags
);
80 set_free(device
->current_tags
);
81 set_free(device
->devlinks
);
86 DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_device
, sd_device
, device_free
);
88 int device_add_property_aux(sd_device
*device
, const char *key
, const char *value
, bool db
) {
89 OrderedHashmap
**properties
;
95 properties
= &device
->properties_db
;
97 properties
= &device
->properties
;
100 _unused_ _cleanup_free_
char *old_value
= NULL
;
101 _cleanup_free_
char *new_key
= NULL
, *new_value
= NULL
, *old_key
= NULL
;
104 r
= ordered_hashmap_ensure_allocated(properties
, &string_hash_ops_free_free
);
108 new_key
= strdup(key
);
112 new_value
= strdup(value
);
116 old_value
= ordered_hashmap_get2(*properties
, key
, (void**) &old_key
);
118 /* ordered_hashmap_replace() does not fail when the hashmap already has the entry. */
119 r
= ordered_hashmap_replace(*properties
, new_key
, new_value
);
126 _unused_ _cleanup_free_
char *old_value
= NULL
;
127 _cleanup_free_
char *old_key
= NULL
;
129 old_value
= ordered_hashmap_remove2(*properties
, key
, (void**) &old_key
);
133 device
->properties_generation
++;
134 device
->properties_buf_outdated
= true;
140 int device_set_syspath(sd_device
*device
, const char *_syspath
, bool verify
) {
141 _cleanup_free_
char *syspath
= NULL
;
149 _cleanup_close_
int fd
= -1;
151 /* The input path maybe a symlink located outside of /sys. Let's try to chase the symlink at first.
152 * The primary usecase is that e.g. /proc/device-tree is a symlink to /sys/firmware/devicetree/base.
153 * By chasing symlinks in the path at first, we can call sd_device_new_from_path() with such path. */
154 r
= chase_symlinks(_syspath
, NULL
, 0, &syspath
, &fd
);
156 /* the device does not exist (any more?) */
157 return log_debug_errno(SYNTHETIC_ERRNO(ENODEV
),
158 "sd-device: Failed to chase symlinks in \"%s\".", _syspath
);
160 return log_debug_errno(r
, "sd-device: Failed to get target of '%s': %m", _syspath
);
162 if (!path_startswith(syspath
, "/sys")) {
163 _cleanup_free_
char *real_sys
= NULL
, *new_syspath
= NULL
;
166 /* /sys is a symlink to somewhere sysfs is mounted on? In that case, we convert the path to real sysfs to "/sys". */
167 r
= chase_symlinks("/sys", NULL
, 0, &real_sys
, NULL
);
169 return log_debug_errno(r
, "sd-device: Failed to chase symlink /sys: %m");
171 p
= path_startswith(syspath
, real_sys
);
173 return log_debug_errno(SYNTHETIC_ERRNO(ENODEV
),
174 "sd-device: Canonicalized path '%s' does not starts with sysfs mount point '%s'",
177 new_syspath
= path_join("/sys", p
);
179 return log_oom_debug();
181 free_and_replace(syspath
, new_syspath
);
182 path_simplify(syspath
);
185 if (path_startswith(syspath
, "/sys/devices/")) {
186 /* For proper devices, stricter rules apply: they must have a 'uevent' file,
187 * otherwise we won't allow them */
189 if (faccessat(fd
, "uevent", F_OK
, 0) < 0) {
191 /* This is not a valid device. Note, this condition is quite often
192 * satisfied when enumerating devices or finding a parent device.
193 * Hence, use log_trace_errno() here. */
194 return log_trace_errno(SYNTHETIC_ERRNO(ENODEV
),
195 "sd-device: the uevent file \"%s/uevent\" does not exist.", syspath
);
196 if (errno
== ENOTDIR
)
197 /* Not actually a directory. */
198 return log_debug_errno(SYNTHETIC_ERRNO(ENODEV
),
199 "sd-device: the syspath \"%s\" is not a directory.", syspath
);
201 return log_debug_errno(errno
, "sd-device: cannot find uevent file for %s: %m", syspath
);
206 /* For everything else lax rules apply: they just need to be a directory */
208 if (fstat(fd
, &st
) < 0)
209 return log_debug_errno(errno
, "sd-device: failed to check if syspath \"%s\" is a directory: %m", syspath
);
210 if (!S_ISDIR(st
.st_mode
))
211 return log_debug_errno(SYNTHETIC_ERRNO(ENODEV
),
212 "sd-device: the syspath \"%s\" is not a directory.", syspath
);
215 /* Only operate on sysfs, i.e. refuse going down into /sys/fs/cgroup/ or similar places where
216 * things are not arranged as kobjects in kernel, and hence don't necessarily have
217 * kobject/attribute structure. */
218 r
= getenv_bool_secure("SYSTEMD_DEVICE_VERIFY_SYSFS");
219 if (r
< 0 && r
!= -ENXIO
)
220 log_debug_errno(r
, "Failed to parse $SYSTEMD_DEVICE_VERIFY_SYSFS value: %m");
222 r
= fd_is_fs_type(fd
, SYSFS_MAGIC
);
224 return log_debug_errno(r
, "sd-device: failed to check if syspath \"%s\" is backed by sysfs.", syspath
);
226 return log_debug_errno(SYNTHETIC_ERRNO(ENODEV
),
227 "sd-device: the syspath \"%s\" is outside of sysfs, refusing.", syspath
);
230 /* must be a subdirectory of /sys */
231 if (!path_startswith(_syspath
, "/sys/"))
232 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL
),
233 "sd-device: Syspath '%s' is not a subdirectory of /sys",
236 syspath
= strdup(_syspath
);
238 return log_oom_debug();
240 path_simplify(syspath
);
243 assert_se(devpath
= startswith(syspath
, "/sys"));
244 if (devpath
[0] != '/')
245 return log_debug_errno(SYNTHETIC_ERRNO(ENODEV
), "sd-device: \"/sys\" alone is not a valid device path.");
247 r
= device_add_property_internal(device
, "DEVPATH", devpath
);
249 return log_debug_errno(r
, "sd-device: Failed to add \"DEVPATH\" property for device \"%s\": %m", syspath
);
251 free_and_replace(device
->syspath
, syspath
);
252 device
->devpath
= devpath
;
256 static int device_new_from_syspath(sd_device
**ret
, const char *syspath
, bool strict
) {
257 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
260 assert_return(ret
, -EINVAL
);
261 assert_return(syspath
, -EINVAL
);
263 if (strict
&& !path_startswith(syspath
, "/sys/"))
266 r
= device_new_aux(&device
);
270 r
= device_set_syspath(device
, syspath
, /* verify= */ true);
274 *ret
= TAKE_PTR(device
);
278 _public_
int sd_device_new_from_syspath(sd_device
**ret
, const char *syspath
) {
279 return device_new_from_syspath(ret
, syspath
, /* strict = */ true);
282 static int device_new_from_mode_and_devnum(sd_device
**ret
, mode_t mode
, dev_t devnum
) {
283 _cleanup_(sd_device_unrefp
) sd_device
*dev
= NULL
;
284 _cleanup_free_
char *syspath
= NULL
;
285 const char *t
, *subsystem
;
293 else if (S_ISBLK(mode
))
298 if (major(devnum
) == 0)
301 if (asprintf(&syspath
, "/sys/dev/%s/%u:%u", t
, major(devnum
), minor(devnum
)) < 0)
304 r
= sd_device_new_from_syspath(&dev
, syspath
);
308 r
= sd_device_get_devnum(dev
, &n
);
316 r
= sd_device_get_subsystem(dev
, &subsystem
);
317 if (r
< 0 && r
!= -ENOENT
)
319 if (r
>= 0 && streq(subsystem
, "block") != !!S_ISBLK(mode
))
322 *ret
= TAKE_PTR(dev
);
326 _public_
int sd_device_new_from_devnum(sd_device
**ret
, char type
, dev_t devnum
) {
327 assert_return(ret
, -EINVAL
);
328 assert_return(IN_SET(type
, 'b', 'c'), -EINVAL
);
330 return device_new_from_mode_and_devnum(ret
, type
== 'b' ? S_IFBLK
: S_IFCHR
, devnum
);
333 static int device_new_from_main_ifname(sd_device
**ret
, const char *ifname
) {
339 syspath
= strjoina("/sys/class/net/", ifname
);
340 return sd_device_new_from_syspath(ret
, syspath
);
343 _public_
int sd_device_new_from_ifname(sd_device
**ret
, const char *ifname
) {
344 _cleanup_free_
char *main_name
= NULL
;
347 assert_return(ret
, -EINVAL
);
348 assert_return(ifname
, -EINVAL
);
350 r
= parse_ifindex(ifname
);
352 return sd_device_new_from_ifindex(ret
, r
);
354 if (ifname_valid(ifname
)) {
355 r
= device_new_from_main_ifname(ret
, ifname
);
360 r
= rtnl_resolve_link_alternative_name(NULL
, ifname
, &main_name
);
364 return device_new_from_main_ifname(ret
, main_name
);
367 _public_
int sd_device_new_from_ifindex(sd_device
**ret
, int ifindex
) {
368 _cleanup_(sd_device_unrefp
) sd_device
*dev
= NULL
;
369 char ifname
[IF_NAMESIZE
];
372 assert_return(ret
, -EINVAL
);
373 assert_return(ifindex
> 0, -EINVAL
);
375 if (format_ifname(ifindex
, ifname
) < 0)
378 r
= device_new_from_main_ifname(&dev
, ifname
);
382 r
= sd_device_get_ifindex(dev
, &i
);
390 *ret
= TAKE_PTR(dev
);
394 static int device_strjoin_new(
404 p
= strjoina(a
, b
, c
, d
);
405 if (access(p
, F_OK
) < 0)
406 return IN_SET(errno
, ENOENT
, ENAMETOOLONG
) ? 0 : -errno
; /* If this sysfs is too long then it doesn't exist either */
408 r
= sd_device_new_from_syspath(ret
, p
);
415 _public_
int sd_device_new_from_subsystem_sysname(
417 const char *subsystem
,
418 const char *sysname
) {
423 assert_return(ret
, -EINVAL
);
424 assert_return(path_is_normalized(subsystem
), -EINVAL
);
425 assert_return(path_is_normalized(sysname
), -EINVAL
);
427 /* translate sysname back to sysfs filename */
428 name
= strdupa_safe(sysname
);
429 string_replace_char(name
, '/', '!');
431 if (streq(subsystem
, "subsystem")) {
432 FOREACH_STRING(s
, "/sys/bus/", "/sys/class/") {
433 r
= device_strjoin_new(s
, name
, NULL
, NULL
, ret
);
440 } else if (streq(subsystem
, "module")) {
441 r
= device_strjoin_new("/sys/module/", name
, NULL
, NULL
, ret
);
447 } else if (streq(subsystem
, "drivers")) {
450 sep
= strchr(name
, ':');
451 if (sep
&& sep
[1] != '\0') { /* Require ":" and something non-empty after that. */
453 const char *subsys
= memdupa_suffix0(name
, sep
- name
);
456 if (streq(sep
, "drivers")) /* If the sysname is "drivers", then it's the drivers directory itself that is meant. */
457 r
= device_strjoin_new("/sys/bus/", subsys
, "/drivers", NULL
, ret
);
459 r
= device_strjoin_new("/sys/bus/", subsys
, "/drivers/", sep
, ret
);
467 r
= device_strjoin_new("/sys/bus/", subsystem
, "/devices/", name
, ret
);
473 r
= device_strjoin_new("/sys/class/", subsystem
, "/", name
, ret
);
479 r
= device_strjoin_new("/sys/firmware/", subsystem
, "/", name
, ret
);
488 _public_
int sd_device_new_from_stat_rdev(sd_device
**ret
, const struct stat
*st
) {
489 assert_return(ret
, -EINVAL
);
490 assert_return(st
, -EINVAL
);
492 return device_new_from_mode_and_devnum(ret
, st
->st_mode
, st
->st_rdev
);
495 _public_
int sd_device_new_from_devname(sd_device
**ret
, const char *devname
) {
500 assert_return(ret
, -EINVAL
);
501 assert_return(devname
, -EINVAL
);
503 /* This function actually accepts both devlinks and devnames, i.e. both symlinks and device
504 * nodes below /dev/. */
506 /* Also ignore when the specified path is "/dev". */
507 if (isempty(path_startswith(devname
, "/dev")))
510 if (device_path_parse_major_minor(devname
, &mode
, &devnum
) >= 0)
511 /* Let's shortcut when "/dev/block/maj:min" or "/dev/char/maj:min" is specified.
512 * In that case, we can directly convert the path to syspath, hence it is not necessary
513 * that the specified path exists. So, this works fine without udevd being running. */
514 return device_new_from_mode_and_devnum(ret
, mode
, devnum
);
516 if (stat(devname
, &st
) < 0)
517 return ERRNO_IS_DEVICE_ABSENT(errno
) ? -ENODEV
: -errno
;
519 return sd_device_new_from_stat_rdev(ret
, &st
);
522 _public_
int sd_device_new_from_path(sd_device
**ret
, const char *path
) {
523 assert_return(ret
, -EINVAL
);
524 assert_return(path
, -EINVAL
);
526 if (path_startswith(path
, "/dev"))
527 return sd_device_new_from_devname(ret
, path
);
529 return device_new_from_syspath(ret
, path
, /* strict = */ false);
532 int device_set_devtype(sd_device
*device
, const char *devtype
) {
533 _cleanup_free_
char *t
= NULL
;
543 r
= device_add_property_internal(device
, "DEVTYPE", t
);
547 return free_and_replace(device
->devtype
, t
);
550 int device_set_ifindex(sd_device
*device
, const char *name
) {
556 ifindex
= parse_ifindex(name
);
560 r
= device_add_property_internal(device
, "IFINDEX", name
);
564 device
->ifindex
= ifindex
;
569 int device_set_devname(sd_device
*device
, const char *devname
) {
570 _cleanup_free_
char *t
= NULL
;
576 if (devname
[0] != '/')
577 t
= strjoin("/dev/", devname
);
583 r
= device_add_property_internal(device
, "DEVNAME", t
);
587 return free_and_replace(device
->devname
, t
);
590 int device_set_devmode(sd_device
*device
, const char *_devmode
) {
597 r
= safe_atou(_devmode
, &devmode
);
604 r
= device_add_property_internal(device
, "DEVMODE", _devmode
);
608 device
->devmode
= devmode
;
613 int device_set_devnum(sd_device
*device
, const char *major
, const char *minor
) {
614 unsigned maj
, min
= 0;
620 r
= safe_atou(major
, &maj
);
625 if (!DEVICE_MAJOR_VALID(maj
))
629 r
= safe_atou(minor
, &min
);
632 if (!DEVICE_MINOR_VALID(min
))
636 r
= device_add_property_internal(device
, "MAJOR", major
);
641 r
= device_add_property_internal(device
, "MINOR", minor
);
646 device
->devnum
= makedev(maj
, min
);
651 int device_set_diskseq(sd_device
*device
, const char *str
) {
658 r
= safe_atou64(str
, &diskseq
);
664 r
= device_add_property_internal(device
, "DISKSEQ", str
);
668 device
->diskseq
= diskseq
;
673 static int handle_uevent_line(
678 const char **minor
) {
686 if (streq(key
, "DEVTYPE"))
687 return device_set_devtype(device
, value
);
688 if (streq(key
, "IFINDEX"))
689 return device_set_ifindex(device
, value
);
690 if (streq(key
, "DEVNAME"))
691 return device_set_devname(device
, value
);
692 if (streq(key
, "DEVMODE"))
693 return device_set_devmode(device
, value
);
694 if (streq(key
, "DISKSEQ"))
695 return device_set_diskseq(device
, value
);
696 if (streq(key
, "MAJOR"))
698 else if (streq(key
, "MINOR"))
701 return device_add_property_internal(device
, key
, value
);
706 int device_read_uevent_file(sd_device
*device
) {
707 _cleanup_free_
char *uevent
= NULL
;
708 const char *syspath
, *key
= NULL
, *value
= NULL
, *major
= NULL
, *minor
= NULL
;
723 if (device
->uevent_loaded
|| device
->sealed
)
726 r
= sd_device_get_syspath(device
, &syspath
);
730 device
->uevent_loaded
= true;
732 path
= strjoina(syspath
, "/uevent");
734 r
= read_full_virtual_file(path
, &uevent
, &uevent_len
);
736 /* The uevent files may be write-only, the device may be already removed, or the device
737 * may not have the uevent file. */
738 if (r
== -EACCES
|| ERRNO_IS_DEVICE_ABSENT(r
))
741 return log_device_debug_errno(device
, r
, "sd-device: Failed to read uevent file '%s': %m", path
);
744 for (size_t i
= 0; i
< uevent_len
; i
++)
747 if (!strchr(NEWLINE
, uevent
[i
])) {
755 if (uevent
[i
] == '=') {
759 } else if (strchr(NEWLINE
, uevent
[i
])) {
761 log_device_debug(device
, "sd-device: Invalid uevent line '%s', ignoring", key
);
771 _fallthrough_
; /* to handle empty property */
773 if (strchr(NEWLINE
, uevent
[i
])) {
776 r
= handle_uevent_line(device
, key
, value
, &major
, &minor
);
778 log_device_debug_errno(device
, r
, "sd-device: Failed to handle uevent entry '%s=%s', ignoring: %m", key
, value
);
785 assert_not_reached();
789 r
= device_set_devnum(device
, major
, minor
);
791 log_device_debug_errno(device
, r
, "sd-device: Failed to set 'MAJOR=%s' or 'MINOR=%s' from '%s', ignoring: %m", major
, strna(minor
), path
);
797 _public_
int sd_device_get_ifindex(sd_device
*device
, int *ifindex
) {
800 assert_return(device
, -EINVAL
);
802 r
= device_read_uevent_file(device
);
806 if (device
->ifindex
<= 0)
810 *ifindex
= device
->ifindex
;
815 _public_
int sd_device_new_from_device_id(sd_device
**ret
, const char *id
) {
818 assert_return(ret
, -EINVAL
);
819 assert_return(id
, -EINVAL
);
829 r
= parse_devnum(id
+ 1, &devt
);
833 return sd_device_new_from_devnum(ret
, id
[0], devt
);
839 ifindex
= parse_ifindex(id
+ 1);
843 return sd_device_new_from_ifindex(ret
, ifindex
);
847 const char *subsys
, *sep
;
849 sep
= strchr(id
+ 1, ':');
850 if (!sep
|| sep
- id
- 1 > NAME_MAX
)
853 subsys
= memdupa_suffix0(id
+ 1, sep
- id
- 1);
855 return sd_device_new_from_subsystem_sysname(ret
, subsys
, sep
+ 1);
863 _public_
int sd_device_get_syspath(sd_device
*device
, const char **ret
) {
864 assert_return(device
, -EINVAL
);
866 assert(path_startswith(device
->syspath
, "/sys/"));
869 *ret
= device
->syspath
;
874 _public_
int sd_device_new_child(sd_device
**ret
, sd_device
*device
, const char *suffix
) {
875 _cleanup_free_
char *path
= NULL
;
879 assert_return(ret
, -EINVAL
);
880 assert_return(device
, -EINVAL
);
881 assert_return(suffix
, -EINVAL
);
883 if (!path_is_normalized(suffix
))
886 r
= sd_device_get_syspath(device
, &s
);
890 path
= path_join(s
, suffix
);
894 return sd_device_new_from_syspath(ret
, path
);
897 static int device_new_from_child(sd_device
**ret
, sd_device
*child
) {
898 _cleanup_free_
char *path
= NULL
;
905 r
= sd_device_get_syspath(child
, &syspath
);
910 _cleanup_free_
char *p
= NULL
;
912 r
= path_extract_directory(path
?: syspath
, &p
);
916 if (path_equal(p
, "/sys"))
919 r
= sd_device_new_from_syspath(ret
, p
);
923 free_and_replace(path
, p
);
927 _public_
int sd_device_get_parent(sd_device
*child
, sd_device
**ret
) {
930 assert_return(child
, -EINVAL
);
932 if (!child
->parent_set
) {
933 r
= device_new_from_child(&child
->parent
, child
);
934 if (r
< 0 && r
!= -ENODEV
)
937 child
->parent_set
= true;
944 *ret
= child
->parent
;
948 int device_set_subsystem(sd_device
*device
, const char *subsystem
) {
949 _cleanup_free_
char *s
= NULL
;
955 s
= strdup(subsystem
);
960 r
= device_add_property_internal(device
, "SUBSYSTEM", s
);
964 device
->subsystem_set
= true;
965 return free_and_replace(device
->subsystem
, s
);
968 int device_set_drivers_subsystem(sd_device
*device
) {
969 _cleanup_free_
char *subsystem
= NULL
;
970 const char *devpath
, *drivers
, *p
;
975 r
= sd_device_get_devpath(device
, &devpath
);
979 drivers
= strstr(devpath
, "/drivers/");
981 drivers
= endswith(devpath
, "/drivers");
985 /* Find the path component immediately before the "/drivers/" string */
986 r
= path_find_last_component(devpath
, /* accept_dot_dot= */ false, &drivers
, &p
);
992 subsystem
= strndup(p
, r
);
996 r
= device_set_subsystem(device
, "drivers");
1000 return free_and_replace(device
->driver_subsystem
, subsystem
);
1003 _public_
int sd_device_get_subsystem(sd_device
*device
, const char **ret
) {
1006 assert_return(device
, -EINVAL
);
1008 if (!device
->subsystem_set
) {
1009 _cleanup_free_
char *subsystem
= NULL
;
1010 const char *syspath
;
1013 r
= sd_device_get_syspath(device
, &syspath
);
1017 /* read 'subsystem' link */
1018 path
= strjoina(syspath
, "/subsystem");
1019 r
= readlink_value(path
, &subsystem
);
1020 if (r
< 0 && r
!= -ENOENT
)
1021 return log_device_debug_errno(device
, r
,
1022 "sd-device: Failed to read subsystem for %s: %m",
1026 r
= device_set_subsystem(device
, subsystem
);
1027 /* use implicit names */
1028 else if (!isempty(path_startswith(device
->devpath
, "/module/")))
1029 r
= device_set_subsystem(device
, "module");
1030 else if (strstr(syspath
, "/drivers/") || endswith(syspath
, "/drivers"))
1031 r
= device_set_drivers_subsystem(device
);
1032 else if (!isempty(PATH_STARTSWITH_SET(device
->devpath
, "/class/", "/bus/")))
1033 r
= device_set_subsystem(device
, "subsystem");
1035 device
->subsystem_set
= true;
1039 return log_device_debug_errno(device
, r
,
1040 "sd-device: Failed to set subsystem for %s: %m",
1044 if (!device
->subsystem
)
1048 *ret
= device
->subsystem
;
1052 _public_
int sd_device_get_devtype(sd_device
*device
, const char **devtype
) {
1055 assert_return(device
, -EINVAL
);
1057 r
= device_read_uevent_file(device
);
1061 if (!device
->devtype
)
1065 *devtype
= device
->devtype
;
1067 return !!device
->devtype
;
1070 _public_
int sd_device_get_parent_with_subsystem_devtype(sd_device
*child
, const char *subsystem
, const char *devtype
, sd_device
**ret
) {
1071 sd_device
*parent
= NULL
;
1074 assert_return(child
, -EINVAL
);
1075 assert_return(subsystem
, -EINVAL
);
1077 r
= sd_device_get_parent(child
, &parent
);
1079 const char *parent_subsystem
= NULL
;
1081 (void) sd_device_get_subsystem(parent
, &parent_subsystem
);
1082 if (streq_ptr(parent_subsystem
, subsystem
)) {
1083 const char *parent_devtype
= NULL
;
1088 (void) sd_device_get_devtype(parent
, &parent_devtype
);
1089 if (streq_ptr(parent_devtype
, devtype
))
1092 r
= sd_device_get_parent(parent
, &parent
);
1103 _public_
int sd_device_get_devnum(sd_device
*device
, dev_t
*devnum
) {
1106 assert_return(device
, -EINVAL
);
1108 r
= device_read_uevent_file(device
);
1112 if (major(device
->devnum
) <= 0)
1116 *devnum
= device
->devnum
;
1121 int device_set_driver(sd_device
*device
, const char *driver
) {
1122 _cleanup_free_
char *d
= NULL
;
1133 r
= device_add_property_internal(device
, "DRIVER", d
);
1137 device
->driver_set
= true;
1138 return free_and_replace(device
->driver
, d
);
1141 _public_
int sd_device_get_driver(sd_device
*device
, const char **ret
) {
1142 assert_return(device
, -EINVAL
);
1144 if (!device
->driver_set
) {
1145 _cleanup_free_
char *driver
= NULL
;
1146 const char *syspath
;
1150 r
= sd_device_get_syspath(device
, &syspath
);
1154 path
= strjoina(syspath
, "/driver");
1155 r
= readlink_value(path
, &driver
);
1156 if (r
< 0 && r
!= -ENOENT
)
1157 return log_device_debug_errno(device
, r
,
1158 "sd-device: readlink(\"%s\") failed: %m", path
);
1160 r
= device_set_driver(device
, driver
);
1162 return log_device_debug_errno(device
, r
,
1163 "sd-device: Failed to set driver \"%s\": %m", driver
);
1166 if (!device
->driver
)
1170 *ret
= device
->driver
;
1174 _public_
int sd_device_get_devpath(sd_device
*device
, const char **ret
) {
1175 assert_return(device
, -EINVAL
);
1177 assert(device
->devpath
);
1178 assert(device
->devpath
[0] == '/');
1181 *ret
= device
->devpath
;
1186 _public_
int sd_device_get_devname(sd_device
*device
, const char **devname
) {
1189 assert_return(device
, -EINVAL
);
1191 r
= device_read_uevent_file(device
);
1195 if (!device
->devname
)
1198 assert(path_startswith(device
->devname
, "/dev/"));
1201 *devname
= device
->devname
;
1205 static int device_set_sysname_and_sysnum(sd_device
*device
) {
1206 _cleanup_free_
char *sysname
= NULL
;
1212 r
= path_extract_filename(device
->devpath
, &sysname
);
1215 if (r
== O_DIRECTORY
)
1218 /* some devices have '!' in their name, change that to '/' */
1219 string_replace_char(sysname
, '!', '/');
1221 n
= strspn_from_end(sysname
, DIGITS
);
1222 len
= strlen(sysname
);
1225 n
= 0; /* Do not set sysnum for number only sysname. */
1227 device
->sysnum
= n
> 0 ? sysname
+ len
- n
: NULL
;
1228 return free_and_replace(device
->sysname
, sysname
);
1231 _public_
int sd_device_get_sysname(sd_device
*device
, const char **ret
) {
1234 assert_return(device
, -EINVAL
);
1236 if (!device
->sysname
) {
1237 r
= device_set_sysname_and_sysnum(device
);
1243 *ret
= device
->sysname
;
1247 _public_
int sd_device_get_sysnum(sd_device
*device
, const char **ret
) {
1250 assert_return(device
, -EINVAL
);
1252 if (!device
->sysname
) {
1253 r
= device_set_sysname_and_sysnum(device
);
1258 if (!device
->sysnum
)
1262 *ret
= device
->sysnum
;
1266 _public_
int sd_device_get_action(sd_device
*device
, sd_device_action_t
*ret
) {
1267 assert_return(device
, -EINVAL
);
1269 if (device
->action
< 0)
1273 *ret
= device
->action
;
1278 _public_
int sd_device_get_seqnum(sd_device
*device
, uint64_t *ret
) {
1279 assert_return(device
, -EINVAL
);
1281 if (device
->seqnum
== 0)
1285 *ret
= device
->seqnum
;
1290 _public_
int sd_device_get_diskseq(sd_device
*device
, uint64_t *ret
) {
1293 assert_return(device
, -EINVAL
);
1295 r
= device_read_uevent_file(device
);
1299 if (device
->diskseq
== 0)
1303 *ret
= device
->diskseq
;
1308 static bool is_valid_tag(const char *tag
) {
1311 return !strchr(tag
, ':') && !strchr(tag
, ' ');
1314 int device_add_tag(sd_device
*device
, const char *tag
, bool both
) {
1320 if (!is_valid_tag(tag
))
1323 /* Definitely add to the "all" list of tags (i.e. the sticky list) */
1324 added
= set_put_strdup(&device
->all_tags
, tag
);
1328 /* And optionally, also add it to the current list of tags */
1330 r
= set_put_strdup(&device
->current_tags
, tag
);
1333 (void) set_remove(device
->all_tags
, tag
);
1339 device
->tags_generation
++;
1340 device
->property_tags_outdated
= true;
1345 int device_add_devlink(sd_device
*device
, const char *devlink
) {
1351 r
= set_put_strdup(&device
->devlinks
, devlink
);
1355 device
->devlinks_generation
++;
1356 device
->property_devlinks_outdated
= true;
1361 bool device_has_devlink(sd_device
*device
, const char *devlink
) {
1365 return set_contains(device
->devlinks
, devlink
);
1368 static int device_add_property_internal_from_string(sd_device
*device
, const char *str
) {
1369 _cleanup_free_
char *key
= NULL
;
1380 value
= strchr(key
, '=');
1386 if (isempty(++value
))
1389 /* Add the property to both sd_device::properties and sd_device::properties_db,
1390 * as this is called by only handle_db_line(). */
1391 r
= device_add_property_aux(device
, key
, value
, false);
1395 return device_add_property_aux(device
, key
, value
, true);
1398 int device_set_usec_initialized(sd_device
*device
, usec_t when
) {
1399 char s
[DECIMAL_STR_MAX(usec_t
)];
1404 xsprintf(s
, USEC_FMT
, when
);
1406 r
= device_add_property_internal(device
, "USEC_INITIALIZED", s
);
1410 device
->usec_initialized
= when
;
1414 static int handle_db_line(sd_device
*device
, char key
, const char *value
) {
1421 case 'G': /* Any tag */
1422 case 'Q': /* Current tag */
1423 return device_add_tag(device
, value
, key
== 'Q');
1428 path
= strjoina("/dev/", value
);
1429 return device_add_devlink(device
, path
);
1432 return device_add_property_internal_from_string(device
, value
);
1437 r
= safe_atou64(value
, &t
);
1441 return device_set_usec_initialized(device
, t
);
1444 return safe_atoi(value
, &device
->devlink_priority
);
1447 /* Deprecated. Previously, watch handle is both saved in database and /run/udev/watch.
1448 * However, the handle saved in database may not be updated when the handle is updated
1449 * or removed. Moreover, it is not necessary to store the handle within the database,
1450 * as its value becomes meaningless when udevd is restarted. */
1454 return safe_atou(value
, &device
->database_version
);
1457 log_device_debug(device
, "sd-device: Unknown key '%c' in device db, ignoring", key
);
1462 int device_get_device_id(sd_device
*device
, const char **ret
) {
1466 if (!device
->device_id
) {
1467 _cleanup_free_
char *id
= NULL
;
1468 const char *subsystem
;
1472 r
= sd_device_get_subsystem(device
, &subsystem
);
1476 if (sd_device_get_devnum(device
, &devnum
) >= 0) {
1477 /* use dev_t — b259:131072, c254:0 */
1478 if (asprintf(&id
, "%c%u:%u",
1479 streq(subsystem
, "block") ? 'b' : 'c',
1480 major(devnum
), minor(devnum
)) < 0)
1482 } else if (sd_device_get_ifindex(device
, &ifindex
) >= 0) {
1483 /* use netdev ifindex — n3 */
1484 if (asprintf(&id
, "n%u", (unsigned) ifindex
) < 0)
1487 _cleanup_free_
char *sysname
= NULL
;
1489 /* use $subsys:$sysname — pci:0000:00:1f.2
1490 * sd_device_get_sysname() has '!' translated, get it from devpath */
1491 r
= path_extract_filename(device
->devpath
, &sysname
);
1494 if (r
== O_DIRECTORY
)
1497 if (streq(subsystem
, "drivers")) {
1498 /* the 'drivers' pseudo-subsystem is special, and needs the real
1499 * subsystem encoded as well */
1500 assert(device
->driver_subsystem
);
1501 id
= strjoin("+drivers:", device
->driver_subsystem
, ":", sysname
);
1503 id
= strjoin("+", subsystem
, ":", sysname
);
1508 if (!filename_is_valid(id
))
1511 device
->device_id
= TAKE_PTR(id
);
1514 *ret
= device
->device_id
;
1518 int device_read_db_internal_filename(sd_device
*device
, const char *filename
) {
1519 _cleanup_free_
char *db
= NULL
;
1522 char key
= '\0'; /* Unnecessary initialization to appease gcc-12.0.0-0.4.fc36 */
1536 r
= read_full_file(filename
, &db
, &db_len
);
1541 return log_device_debug_errno(device
, r
, "sd-device: Failed to read db '%s': %m", filename
);
1544 /* devices with a database entry are initialized */
1545 device
->is_initialized
= true;
1547 device
->db_loaded
= true;
1549 for (size_t i
= 0; i
< db_len
; i
++)
1552 if (!strchr(NEWLINE
, db
[i
])) {
1561 log_device_debug(device
, "sd-device: Invalid db entry with key '%c', ignoring", key
);
1563 state
= INVALID_LINE
;
1578 if (strchr(NEWLINE
, db
[i
]))
1583 if (strchr(NEWLINE
, db
[i
])) {
1585 r
= handle_db_line(device
, key
, value
);
1587 log_device_debug_errno(device
, r
, "sd-device: Failed to handle db entry '%c:%s', ignoring: %m",
1595 return log_device_debug_errno(device
, SYNTHETIC_ERRNO(EINVAL
), "sd-device: invalid db syntax.");
1601 int device_read_db_internal(sd_device
*device
, bool force
) {
1602 const char *id
, *path
;
1607 if (device
->db_loaded
|| (!force
&& device
->sealed
))
1610 r
= device_get_device_id(device
, &id
);
1614 path
= strjoina("/run/udev/data/", id
);
1616 return device_read_db_internal_filename(device
, path
);
1619 _public_
int sd_device_get_is_initialized(sd_device
*device
) {
1622 assert_return(device
, -EINVAL
);
1624 r
= device_read_db(device
);
1626 /* The device may be already removed or renamed. */
1631 return device
->is_initialized
;
1634 _public_
int sd_device_get_usec_initialized(sd_device
*device
, uint64_t *ret
) {
1637 assert_return(device
, -EINVAL
);
1639 r
= sd_device_get_is_initialized(device
);
1645 if (device
->usec_initialized
== 0)
1649 *ret
= device
->usec_initialized
;
1654 _public_
int sd_device_get_usec_since_initialized(sd_device
*device
, uint64_t *ret
) {
1658 assert_return(device
, -EINVAL
);
1660 r
= sd_device_get_usec_initialized(device
, &ts
);
1664 now_ts
= now(CLOCK_MONOTONIC
);
1670 *ret
= usec_sub_unsigned(now_ts
, ts
);
1675 _public_
const char *sd_device_get_tag_first(sd_device
*device
) {
1678 assert_return(device
, NULL
);
1680 (void) device_read_db(device
);
1682 device
->all_tags_iterator_generation
= device
->tags_generation
;
1683 device
->all_tags_iterator
= ITERATOR_FIRST
;
1685 (void) set_iterate(device
->all_tags
, &device
->all_tags_iterator
, &v
);
1689 _public_
const char *sd_device_get_tag_next(sd_device
*device
) {
1692 assert_return(device
, NULL
);
1694 (void) device_read_db(device
);
1696 if (device
->all_tags_iterator_generation
!= device
->tags_generation
)
1699 (void) set_iterate(device
->all_tags
, &device
->all_tags_iterator
, &v
);
1703 static bool device_database_supports_current_tags(sd_device
*device
) {
1706 (void) device_read_db(device
);
1708 /* The current tags (saved in Q field) feature is implemented in database version 1.
1709 * If the database version is 0, then the tags (NOT current tags, saved in G field) are not
1710 * sticky. Thus, we can safely bypass the operations for the current tags (Q) to tags (G). */
1712 return device
->database_version
>= 1;
1715 _public_
const char *sd_device_get_current_tag_first(sd_device
*device
) {
1718 assert_return(device
, NULL
);
1720 if (!device_database_supports_current_tags(device
))
1721 return sd_device_get_tag_first(device
);
1723 (void) device_read_db(device
);
1725 device
->current_tags_iterator_generation
= device
->tags_generation
;
1726 device
->current_tags_iterator
= ITERATOR_FIRST
;
1728 (void) set_iterate(device
->current_tags
, &device
->current_tags_iterator
, &v
);
1732 _public_
const char *sd_device_get_current_tag_next(sd_device
*device
) {
1735 assert_return(device
, NULL
);
1737 if (!device_database_supports_current_tags(device
))
1738 return sd_device_get_tag_next(device
);
1740 (void) device_read_db(device
);
1742 if (device
->current_tags_iterator_generation
!= device
->tags_generation
)
1745 (void) set_iterate(device
->current_tags
, &device
->current_tags_iterator
, &v
);
1749 _public_
const char *sd_device_get_devlink_first(sd_device
*device
) {
1752 assert_return(device
, NULL
);
1754 (void) device_read_db(device
);
1756 device
->devlinks_iterator_generation
= device
->devlinks_generation
;
1757 device
->devlinks_iterator
= ITERATOR_FIRST
;
1759 (void) set_iterate(device
->devlinks
, &device
->devlinks_iterator
, &v
);
1763 _public_
const char *sd_device_get_devlink_next(sd_device
*device
) {
1766 assert_return(device
, NULL
);
1768 (void) device_read_db(device
);
1770 if (device
->devlinks_iterator_generation
!= device
->devlinks_generation
)
1773 (void) set_iterate(device
->devlinks
, &device
->devlinks_iterator
, &v
);
1777 int device_properties_prepare(sd_device
*device
) {
1782 r
= device_read_uevent_file(device
);
1786 r
= device_read_db(device
);
1790 if (device
->property_devlinks_outdated
) {
1791 _cleanup_free_
char *devlinks
= NULL
;
1793 r
= set_strjoin(device
->devlinks
, " ", false, &devlinks
);
1797 if (!isempty(devlinks
)) {
1798 r
= device_add_property_internal(device
, "DEVLINKS", devlinks
);
1803 device
->property_devlinks_outdated
= false;
1806 if (device
->property_tags_outdated
) {
1807 _cleanup_free_
char *tags
= NULL
;
1809 r
= set_strjoin(device
->all_tags
, ":", true, &tags
);
1813 if (!isempty(tags
)) {
1814 r
= device_add_property_internal(device
, "TAGS", tags
);
1820 r
= set_strjoin(device
->current_tags
, ":", true, &tags
);
1824 if (!isempty(tags
)) {
1825 r
= device_add_property_internal(device
, "CURRENT_TAGS", tags
);
1830 device
->property_tags_outdated
= false;
1836 _public_
const char *sd_device_get_property_first(sd_device
*device
, const char **_value
) {
1840 assert_return(device
, NULL
);
1842 r
= device_properties_prepare(device
);
1846 device
->properties_iterator_generation
= device
->properties_generation
;
1847 device
->properties_iterator
= ITERATOR_FIRST
;
1849 (void) ordered_hashmap_iterate(device
->properties
, &device
->properties_iterator
, (void**)_value
, (const void**)&key
);
1853 _public_
const char *sd_device_get_property_next(sd_device
*device
, const char **_value
) {
1857 assert_return(device
, NULL
);
1859 r
= device_properties_prepare(device
);
1863 if (device
->properties_iterator_generation
!= device
->properties_generation
)
1866 (void) ordered_hashmap_iterate(device
->properties
, &device
->properties_iterator
, (void**)_value
, (const void**)&key
);
1870 static int device_sysattrs_read_all_internal(sd_device
*device
, const char *subdir
) {
1871 _cleanup_free_
char *path_dir
= NULL
;
1872 _cleanup_closedir_
DIR *dir
= NULL
;
1873 const char *syspath
;
1876 r
= sd_device_get_syspath(device
, &syspath
);
1881 _cleanup_free_
char *p
= NULL
;
1883 p
= path_join(syspath
, subdir
, "uevent");
1887 if (access(p
, F_OK
) >= 0)
1888 /* this is a child device, skipping */
1890 if (errno
!= ENOENT
) {
1891 log_device_debug_errno(device
, errno
, "sd-device: Failed to stat %s, ignoring subdir: %m", p
);
1895 path_dir
= path_join(syspath
, subdir
);
1900 dir
= opendir(path_dir
?: syspath
);
1904 FOREACH_DIRENT_ALL(de
, dir
, return -errno
) {
1905 _cleanup_free_
char *path
= NULL
, *p
= NULL
;
1906 struct stat statbuf
;
1908 if (dot_or_dot_dot(de
->d_name
))
1911 /* only handle symlinks, regular files, and directories */
1912 if (!IN_SET(de
->d_type
, DT_LNK
, DT_REG
, DT_DIR
))
1916 p
= path_join(subdir
, de
->d_name
);
1921 if (de
->d_type
== DT_DIR
) {
1922 /* read subdirectory */
1923 r
= device_sysattrs_read_all_internal(device
, p
?: de
->d_name
);
1930 path
= path_join(syspath
, p
?: de
->d_name
);
1934 if (lstat(path
, &statbuf
) != 0)
1937 if ((statbuf
.st_mode
& (S_IRUSR
| S_IWUSR
)) == 0)
1940 r
= set_put_strdup(&device
->sysattrs
, p
?: de
->d_name
);
1948 static int device_sysattrs_read_all(sd_device
*device
) {
1953 if (device
->sysattrs_read
)
1956 r
= device_sysattrs_read_all_internal(device
, NULL
);
1960 device
->sysattrs_read
= true;
1965 _public_
const char *sd_device_get_sysattr_first(sd_device
*device
) {
1969 assert_return(device
, NULL
);
1971 if (!device
->sysattrs_read
) {
1972 r
= device_sysattrs_read_all(device
);
1979 device
->sysattrs_iterator
= ITERATOR_FIRST
;
1981 (void) set_iterate(device
->sysattrs
, &device
->sysattrs_iterator
, &v
);
1985 _public_
const char *sd_device_get_sysattr_next(sd_device
*device
) {
1988 assert_return(device
, NULL
);
1990 if (!device
->sysattrs_read
)
1993 (void) set_iterate(device
->sysattrs
, &device
->sysattrs_iterator
, &v
);
1997 _public_
int sd_device_has_tag(sd_device
*device
, const char *tag
) {
1998 assert_return(device
, -EINVAL
);
1999 assert_return(tag
, -EINVAL
);
2001 (void) device_read_db(device
);
2003 return set_contains(device
->all_tags
, tag
);
2006 _public_
int sd_device_has_current_tag(sd_device
*device
, const char *tag
) {
2007 assert_return(device
, -EINVAL
);
2008 assert_return(tag
, -EINVAL
);
2010 if (!device_database_supports_current_tags(device
))
2011 return sd_device_has_tag(device
, tag
);
2013 (void) device_read_db(device
);
2015 return set_contains(device
->current_tags
, tag
);
2018 _public_
int sd_device_get_property_value(sd_device
*device
, const char *key
, const char **ret_value
) {
2022 assert_return(device
, -EINVAL
);
2023 assert_return(key
, -EINVAL
);
2025 r
= device_properties_prepare(device
);
2029 value
= ordered_hashmap_get(device
->properties
, key
);
2038 int device_get_property_bool(sd_device
*device
, const char *key
) {
2045 r
= sd_device_get_property_value(device
, key
, &value
);
2049 return parse_boolean(value
);
2052 _public_
int sd_device_get_trigger_uuid(sd_device
*device
, sd_id128_t
*ret
) {
2057 assert_return(device
, -EINVAL
);
2059 /* Retrieves the UUID attached to a uevent when triggering it from userspace via
2060 * sd_device_trigger_with_uuid() or an equivalent interface. Returns -ENOENT if the record is not
2061 * caused by a synthetic event and -ENODATA if it was but no UUID was specified */
2063 r
= sd_device_get_property_value(device
, "SYNTH_UUID", &s
);
2067 if (streq(s
, "0")) /* SYNTH_UUID=0 is set whenever a device is triggered by userspace without specifying a UUID */
2070 r
= sd_id128_from_string(s
, &id
);
2080 void device_clear_sysattr_cache(sd_device
*device
) {
2081 device
->sysattr_values
= hashmap_free(device
->sysattr_values
);
2084 int device_cache_sysattr_value(sd_device
*device
, const char *key
, char *value
) {
2085 _unused_ _cleanup_free_
char *old_value
= NULL
;
2086 _cleanup_free_
char *new_key
= NULL
;
2092 /* This takes the reference of the input value. The input value may be NULL.
2093 * This replaces the value if it already exists. */
2095 /* First, remove the old cache entry. So, we do not need to clear cache on error. */
2096 old_value
= hashmap_remove2(device
->sysattr_values
, key
, (void **) &new_key
);
2098 new_key
= strdup(key
);
2103 r
= hashmap_ensure_put(&device
->sysattr_values
, &string_hash_ops_free_free
, new_key
, value
);
2112 int device_get_cached_sysattr_value(sd_device
*device
, const char *key
, const char **ret_value
) {
2113 const char *k
= NULL
, *value
;
2118 value
= hashmap_get2(device
->sysattr_values
, key
, (void **) &k
);
2120 return -ESTALE
; /* We have not read the attribute. */
2122 return -ENOENT
; /* We have looked up the attribute before and it did not exist. */
2128 /* We cache all sysattr lookups. If an attribute does not exist, it is stored
2129 * with a NULL value in the cache, otherwise the returned string is stored */
2130 _public_
int sd_device_get_sysattr_value(sd_device
*device
, const char *sysattr
, const char **ret_value
) {
2131 _cleanup_free_
char *value
= NULL
;
2132 const char *path
, *syspath
;
2133 struct stat statbuf
;
2136 assert_return(device
, -EINVAL
);
2137 assert_return(sysattr
, -EINVAL
);
2139 /* look for possibly already cached result */
2140 r
= device_get_cached_sysattr_value(device
, sysattr
, ret_value
);
2144 r
= sd_device_get_syspath(device
, &syspath
);
2148 path
= prefix_roota(syspath
, sysattr
);
2149 if (lstat(path
, &statbuf
) < 0) {
2154 /* remember that we could not access the sysattr */
2155 k
= device_cache_sysattr_value(device
, sysattr
, NULL
);
2157 log_device_debug_errno(device
, k
,
2158 "sd-device: failed to cache attribute '%s' with NULL, ignoring: %m",
2162 } else if (S_ISLNK(statbuf
.st_mode
)) {
2163 /* Some core links return only the last element of the target path,
2164 * these are just values, the paths should not be exposed. */
2165 if (STR_IN_SET(sysattr
, "driver", "subsystem", "module")) {
2166 r
= readlink_value(path
, &value
);
2171 } else if (S_ISDIR(statbuf
.st_mode
))
2172 /* skip directories */
2174 else if (!(statbuf
.st_mode
& S_IRUSR
))
2175 /* skip non-readable files */
2180 /* Read attribute value, Some attributes contain embedded '\0'. So, it is necessary to
2181 * also get the size of the result. See issue #20025. */
2182 r
= read_full_virtual_file(path
, &value
, &size
);
2186 /* drop trailing newlines */
2187 while (size
> 0 && strchr(NEWLINE
, value
[--size
]))
2191 /* Unfortunately, we need to return 'const char*' instead of 'char*'. Hence, failure in caching
2192 * sysattr value is critical unlike the other places. */
2193 r
= device_cache_sysattr_value(device
, sysattr
, value
);
2195 log_device_debug_errno(device
, r
,
2196 "sd-device: failed to cache attribute '%s' with '%s'%s: %m",
2197 sysattr
, value
, ret_value
? "" : ", ignoring");
2200 } else if (ret_value
)
2201 *ret_value
= TAKE_PTR(value
);
2206 int device_get_sysattr_bool(sd_device
*device
, const char *sysattr
) {
2213 r
= sd_device_get_sysattr_value(device
, sysattr
, &value
);
2217 return parse_boolean(value
);
2220 static void device_remove_cached_sysattr_value(sd_device
*device
, const char *_key
) {
2221 _cleanup_free_
char *key
= NULL
;
2226 free(hashmap_remove2(device
->sysattr_values
, _key
, (void **) &key
));
2229 _public_
int sd_device_set_sysattr_value(sd_device
*device
, const char *sysattr
, const char *_value
) {
2230 _cleanup_free_
char *value
= NULL
;
2231 const char *syspath
, *path
;
2235 assert_return(device
, -EINVAL
);
2236 assert_return(sysattr
, -EINVAL
);
2238 /* Set the attribute and save it in the cache. */
2241 /* If input value is NULL, then clear cache and not write anything. */
2242 device_remove_cached_sysattr_value(device
, sysattr
);
2246 r
= sd_device_get_syspath(device
, &syspath
);
2250 path
= prefix_roota(syspath
, sysattr
);
2252 len
= strlen(_value
);
2254 /* drop trailing newlines */
2255 while (len
> 0 && strchr(NEWLINE
, _value
[len
- 1]))
2258 /* value length is limited to 4k */
2262 value
= strndup(_value
, len
);
2266 r
= write_string_file(path
, value
, WRITE_STRING_FILE_DISABLE_BUFFER
| WRITE_STRING_FILE_NOFOLLOW
);
2268 /* On failure, clear cache entry, as we do not know how it fails. */
2269 device_remove_cached_sysattr_value(device
, sysattr
);
2273 /* Do not cache action string written into uevent file. */
2274 if (streq(sysattr
, "uevent"))
2277 r
= device_cache_sysattr_value(device
, sysattr
, value
);
2279 log_device_debug_errno(device
, r
,
2280 "sd-device: failed to cache attribute '%s' with '%s', ignoring: %m",
2288 _public_
int sd_device_set_sysattr_valuef(sd_device
*device
, const char *sysattr
, const char *format
, ...) {
2289 _cleanup_free_
char *value
= NULL
;
2293 assert_return(device
, -EINVAL
);
2294 assert_return(sysattr
, -EINVAL
);
2297 device_remove_cached_sysattr_value(device
, sysattr
);
2301 va_start(ap
, format
);
2302 r
= vasprintf(&value
, format
, ap
);
2308 return sd_device_set_sysattr_value(device
, sysattr
, value
);
2311 _public_
int sd_device_trigger(sd_device
*device
, sd_device_action_t action
) {
2314 assert_return(device
, -EINVAL
);
2316 s
= device_action_to_string(action
);
2320 /* This uses the simple no-UUID interface of kernel < 4.13 */
2321 return sd_device_set_sysattr_value(device
, "uevent", s
);
2324 _public_
int sd_device_trigger_with_uuid(
2326 sd_device_action_t action
,
2327 sd_id128_t
*ret_uuid
) {
2333 assert_return(device
, -EINVAL
);
2335 /* If no one wants to know the UUID, use the simple interface from pre-4.13 times */
2337 return sd_device_trigger(device
, action
);
2339 s
= device_action_to_string(action
);
2343 r
= sd_id128_randomize(&u
);
2347 j
= strjoina(s
, " ", SD_ID128_TO_UUID_STRING(u
));
2349 r
= sd_device_set_sysattr_value(device
, "uevent", j
);
2357 _public_
int sd_device_open(sd_device
*device
, int flags
) {
2358 _cleanup_close_
int fd
= -1, fd2
= -1;
2359 const char *devname
, *subsystem
= NULL
;
2360 uint64_t q
, diskseq
= 0;
2365 assert_return(device
, -EINVAL
);
2366 assert_return(FLAGS_SET(flags
, O_PATH
) || !FLAGS_SET(flags
, O_NOFOLLOW
), -EINVAL
);
2368 r
= sd_device_get_devname(device
, &devname
);
2374 r
= sd_device_get_devnum(device
, &devnum
);
2380 r
= sd_device_get_subsystem(device
, &subsystem
);
2381 if (r
< 0 && r
!= -ENOENT
)
2384 fd
= open(devname
, FLAGS_SET(flags
, O_PATH
) ? flags
: O_CLOEXEC
|O_NOFOLLOW
|O_PATH
);
2388 if (fstat(fd
, &st
) < 0)
2391 if (st
.st_rdev
!= devnum
)
2394 if (streq_ptr(subsystem
, "block") ? !S_ISBLK(st
.st_mode
) : !S_ISCHR(st
.st_mode
))
2397 /* If flags has O_PATH, then we cannot check diskseq. Let's return earlier. */
2398 if (FLAGS_SET(flags
, O_PATH
))
2401 r
= device_get_property_bool(device
, "ID_IGNORE_DISKSEQ");
2402 if (r
< 0 && r
!= -ENOENT
)
2405 r
= sd_device_get_diskseq(device
, &diskseq
);
2406 if (r
< 0 && r
!= -ENOENT
)
2410 fd2
= open(FORMAT_PROC_FD_PATH(fd
), flags
);
2415 return TAKE_FD(fd2
);
2417 r
= fd_get_diskseq(fd2
, &q
);
2424 return TAKE_FD(fd2
);