1 /* SPDX-License-Identifier: LGPL-2.1+ */
9 #include "alloc-util.h"
10 #include "device-internal.h"
11 #include "device-private.h"
12 #include "device-util.h"
13 #include "dirent-util.h"
19 #include "parse-util.h"
20 #include "path-util.h"
22 #include "socket-util.h"
23 #include "stat-util.h"
24 #include "string-util.h"
29 int device_new_aux(sd_device
**ret
) {
30 sd_device
*device
= NULL
;
34 device
= new(sd_device
, 1);
38 *device
= (sd_device
) {
42 .devmode
= (mode_t
) -1,
51 static sd_device
*device_free(sd_device
*device
) {
54 sd_device_unref(device
->parent
);
55 free(device
->syspath
);
56 free(device
->sysname
);
57 free(device
->devtype
);
58 free(device
->devname
);
59 free(device
->subsystem
);
60 free(device
->driver_subsystem
);
62 free(device
->id_filename
);
63 free(device
->properties_strv
);
64 free(device
->properties_nulstr
);
66 ordered_hashmap_free_free_free(device
->properties
);
67 ordered_hashmap_free_free_free(device
->properties_db
);
68 hashmap_free_free_free(device
->sysattr_values
);
69 set_free_free(device
->sysattrs
);
70 set_free_free(device
->tags
);
71 set_free_free(device
->devlinks
);
76 DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_device
, sd_device
, device_free
);
78 int device_add_property_aux(sd_device
*device
, const char *_key
, const char *_value
, bool db
) {
79 OrderedHashmap
**properties
;
85 properties
= &device
->properties_db
;
87 properties
= &device
->properties
;
90 _cleanup_free_
char *key
= NULL
, *value
= NULL
, *old_key
= NULL
, *old_value
= NULL
;
93 r
= ordered_hashmap_ensure_allocated(properties
, &string_hash_ops
);
101 value
= strdup(_value
);
105 old_value
= ordered_hashmap_get2(*properties
, key
, (void**) &old_key
);
107 r
= ordered_hashmap_replace(*properties
, key
, value
);
114 _cleanup_free_
char *key
= NULL
;
115 _cleanup_free_
char *value
= NULL
;
117 value
= ordered_hashmap_remove2(*properties
, _key
, (void**) &key
);
121 device
->properties_generation
++;
122 device
->properties_buf_outdated
= true;
128 int device_add_property_internal(sd_device
*device
, const char *key
, const char *value
) {
129 return device_add_property_aux(device
, key
, value
, false);
132 int device_set_syspath(sd_device
*device
, const char *_syspath
, bool verify
) {
133 _cleanup_free_
char *syspath
= NULL
;
140 /* must be a subdirectory of /sys */
141 if (!path_startswith(_syspath
, "/sys/")) {
142 log_debug("sd-device: syspath '%s' is not a subdirectory of /sys", _syspath
);
147 r
= chase_symlinks(_syspath
, NULL
, 0, &syspath
);
149 return -ENODEV
; /* the device does not exist (any more?) */
151 return log_debug_errno(r
, "sd-device: could not get target of '%s': %m", _syspath
);
153 if (!path_startswith(syspath
, "/sys")) {
154 _cleanup_free_
char *real_sys
= NULL
, *new_syspath
= NULL
;
157 /* /sys is a symlink to somewhere sysfs is mounted on? In that case, we convert the path to real sysfs to "/sys". */
158 r
= chase_symlinks("/sys", NULL
, 0, &real_sys
);
160 return log_debug_errno(r
, "sd-device: could not chase symlink /sys: %m");
162 p
= path_startswith(syspath
, real_sys
);
164 log_debug("sd-device: canonicalized path '%s' does not starts with sysfs mount point '%s'", syspath
, real_sys
);
168 new_syspath
= strjoin("/sys/", p
);
172 free_and_replace(syspath
, new_syspath
);
173 path_simplify(syspath
, false);
176 if (path_startswith(syspath
, "/sys/devices/")) {
179 /* all 'devices' require an 'uevent' file */
180 path
= strjoina(syspath
, "/uevent");
181 r
= access(path
, F_OK
);
184 /* this is not a valid device */
187 return log_debug_errno(errno
, "sd-device: %s does not have an uevent file: %m", syspath
);
190 /* everything else just needs to be a directory */
191 if (!is_dir(syspath
, false))
195 syspath
= strdup(_syspath
);
200 devpath
= syspath
+ STRLEN("/sys");
202 r
= device_add_property_internal(device
, "DEVPATH", devpath
);
206 free_and_replace(device
->syspath
, syspath
);
208 device
->devpath
= devpath
;
213 _public_
int sd_device_new_from_syspath(sd_device
**ret
, const char *syspath
) {
214 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
217 assert_return(ret
, -EINVAL
);
218 assert_return(syspath
, -EINVAL
);
220 r
= device_new_aux(&device
);
224 r
= device_set_syspath(device
, syspath
, true);
228 *ret
= TAKE_PTR(device
);
233 _public_
int sd_device_new_from_devnum(sd_device
**ret
, char type
, dev_t devnum
) {
235 char id
[DECIMAL_STR_MAX(unsigned) * 2 + 1];
237 assert_return(ret
, -EINVAL
);
238 assert_return(IN_SET(type
, 'b', 'c'), -EINVAL
);
240 /* use /sys/dev/{block,char}/<maj>:<min> link */
241 snprintf(id
, sizeof(id
), "%u:%u", major(devnum
), minor(devnum
));
243 syspath
= strjoina("/sys/dev/", (type
== 'b' ? "block" : "char"), "/", id
);
245 return sd_device_new_from_syspath(ret
, syspath
);
248 _public_
int sd_device_new_from_subsystem_sysname(sd_device
**ret
, const char *subsystem
, const char *sysname
) {
249 char *name
, *syspath
;
252 assert_return(ret
, -EINVAL
);
253 assert_return(subsystem
, -EINVAL
);
254 assert_return(sysname
, -EINVAL
);
256 if (streq(subsystem
, "subsystem")) {
257 syspath
= strjoina("/sys/subsystem/", sysname
);
258 if (access(syspath
, F_OK
) >= 0)
259 return sd_device_new_from_syspath(ret
, syspath
);
261 syspath
= strjoina("/sys/bus/", sysname
);
262 if (access(syspath
, F_OK
) >= 0)
263 return sd_device_new_from_syspath(ret
, syspath
);
265 syspath
= strjoina("/sys/class/", sysname
);
266 if (access(syspath
, F_OK
) >= 0)
267 return sd_device_new_from_syspath(ret
, syspath
);
268 } else if (streq(subsystem
, "module")) {
269 syspath
= strjoina("/sys/module/", sysname
);
270 if (access(syspath
, F_OK
) >= 0)
271 return sd_device_new_from_syspath(ret
, syspath
);
272 } else if (streq(subsystem
, "drivers")) {
273 char subsys
[PATH_MAX
];
276 strscpy(subsys
, sizeof(subsys
), sysname
);
277 driver
= strchr(subsys
, ':');
282 syspath
= strjoina("/sys/subsystem/", subsys
, "/drivers/", driver
);
283 if (access(syspath
, F_OK
) >= 0)
284 return sd_device_new_from_syspath(ret
, syspath
);
286 syspath
= strjoina("/sys/bus/", subsys
, "/drivers/", driver
);
287 if (access(syspath
, F_OK
) >= 0)
288 return sd_device_new_from_syspath(ret
, syspath
);
292 /* translate sysname back to sysfs filename */
293 name
= strdupa(sysname
);
294 while (name
[len
] != '\0') {
295 if (name
[len
] == '/')
301 syspath
= strjoina("/sys/subsystem/", subsystem
, "/devices/", name
);
302 if (access(syspath
, F_OK
) >= 0)
303 return sd_device_new_from_syspath(ret
, syspath
);
305 syspath
= strjoina("/sys/bus/", subsystem
, "/devices/", name
);
306 if (access(syspath
, F_OK
) >= 0)
307 return sd_device_new_from_syspath(ret
, syspath
);
309 syspath
= strjoina("/sys/class/", subsystem
, "/", name
);
310 if (access(syspath
, F_OK
) >= 0)
311 return sd_device_new_from_syspath(ret
, syspath
);
313 syspath
= strjoina("/sys/firmware/", subsystem
, "/", sysname
);
314 if (access(syspath
, F_OK
) >= 0)
315 return sd_device_new_from_syspath(ret
, syspath
);
320 int device_set_devtype(sd_device
*device
, const char *_devtype
) {
321 _cleanup_free_
char *devtype
= NULL
;
327 devtype
= strdup(_devtype
);
331 r
= device_add_property_internal(device
, "DEVTYPE", devtype
);
335 free_and_replace(device
->devtype
, devtype
);
340 int device_set_ifindex(sd_device
*device
, const char *_ifindex
) {
346 r
= parse_ifindex(_ifindex
, &ifindex
);
350 r
= device_add_property_internal(device
, "IFINDEX", _ifindex
);
354 device
->ifindex
= ifindex
;
359 int device_set_devname(sd_device
*device
, const char *_devname
) {
360 _cleanup_free_
char *devname
= NULL
;
366 if (_devname
[0] != '/') {
367 r
= asprintf(&devname
, "/dev/%s", _devname
);
371 devname
= strdup(_devname
);
376 r
= device_add_property_internal(device
, "DEVNAME", devname
);
380 free_and_replace(device
->devname
, devname
);
385 int device_set_devmode(sd_device
*device
, const char *_devmode
) {
392 r
= safe_atou(_devmode
, &devmode
);
399 r
= device_add_property_internal(device
, "DEVMODE", _devmode
);
403 device
->devmode
= devmode
;
408 int device_set_devnum(sd_device
*device
, const char *major
, const char *minor
) {
409 unsigned maj
= 0, min
= 0;
415 r
= safe_atou(major
, &maj
);
422 r
= safe_atou(minor
, &min
);
427 r
= device_add_property_internal(device
, "MAJOR", major
);
432 r
= device_add_property_internal(device
, "MINOR", minor
);
437 device
->devnum
= makedev(maj
, min
);
442 static int handle_uevent_line(sd_device
*device
, const char *key
, const char *value
, const char **major
, const char **minor
) {
451 if (streq(key
, "DEVTYPE")) {
452 r
= device_set_devtype(device
, value
);
455 } else if (streq(key
, "IFINDEX")) {
456 r
= device_set_ifindex(device
, value
);
459 } else if (streq(key
, "DEVNAME")) {
460 r
= device_set_devname(device
, value
);
463 } else if (streq(key
, "DEVMODE")) {
464 r
= device_set_devmode(device
, value
);
467 } else if (streq(key
, "MAJOR"))
469 else if (streq(key
, "MINOR"))
472 r
= device_add_property_internal(device
, key
, value
);
480 int device_read_uevent_file(sd_device
*device
) {
481 _cleanup_free_
char *uevent
= NULL
;
482 const char *syspath
, *key
= NULL
, *value
= NULL
, *major
= NULL
, *minor
= NULL
;
498 if (device
->uevent_loaded
|| device
->sealed
)
501 device
->uevent_loaded
= true;
503 r
= sd_device_get_syspath(device
, &syspath
);
507 path
= strjoina(syspath
, "/uevent");
509 r
= read_full_file(path
, &uevent
, &uevent_len
);
511 /* empty uevent files may be write-only */
513 else if (r
== -ENOENT
)
514 /* some devices may not have uevent files, see set_syspath() */
517 return log_debug_errno(r
, "sd-device: failed to read uevent file '%s': %m", path
);
519 for (i
= 0; i
< uevent_len
; i
++)
522 if (!strchr(NEWLINE
, uevent
[i
])) {
530 if (uevent
[i
] == '=') {
534 } else if (strchr(NEWLINE
, uevent
[i
])) {
536 log_debug("sd-device: ignoring invalid uevent line '%s'", key
);
546 _fallthrough_
; /* to handle empty property */
548 if (strchr(NEWLINE
, uevent
[i
])) {
551 r
= handle_uevent_line(device
, key
, value
, &major
, &minor
);
553 log_debug_errno(r
, "sd-device: failed to handle uevent entry '%s=%s': %m", key
, value
);
560 assert_not_reached("invalid state when parsing uevent file");
564 r
= device_set_devnum(device
, major
, minor
);
566 log_debug_errno(r
, "sd-device: could not set 'MAJOR=%s' or 'MINOR=%s' from '%s': %m", major
, minor
, path
);
572 _public_
int sd_device_get_ifindex(sd_device
*device
, int *ifindex
) {
575 assert_return(device
, -EINVAL
);
576 assert_return(ifindex
, -EINVAL
);
578 r
= device_read_uevent_file(device
);
582 if (device
->ifindex
< 0)
585 *ifindex
= device
->ifindex
;
590 _public_
int sd_device_new_from_device_id(sd_device
**ret
, const char *id
) {
593 assert_return(ret
, -EINVAL
);
594 assert_return(id
, -EINVAL
);
603 r
= sscanf(id
, "%c%i:%i", &type
, &maj
, &min
);
607 return sd_device_new_from_devnum(ret
, type
, makedev(maj
, min
));
611 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
612 _cleanup_close_
int sk
= -1;
613 struct ifreq ifr
= {};
616 r
= parse_ifindex(&id
[1], &ifr
.ifr_ifindex
);
620 sk
= socket_ioctl_fd();
624 r
= ioctl(sk
, SIOCGIFNAME
, &ifr
);
628 r
= sd_device_new_from_subsystem_sysname(&device
, "net", ifr
.ifr_name
);
632 r
= sd_device_get_ifindex(device
, &ifindex
);
636 /* this is racey, so we might end up with the wrong device */
637 if (ifr
.ifr_ifindex
!= ifindex
)
640 *ret
= TAKE_PTR(device
);
646 char subsys
[PATH_MAX
];
649 (void) strscpy(subsys
, sizeof(subsys
), id
+ 1);
650 sysname
= strchr(subsys
, ':');
657 return sd_device_new_from_subsystem_sysname(ret
, subsys
, sysname
);
664 _public_
int sd_device_get_syspath(sd_device
*device
, const char **ret
) {
665 assert_return(device
, -EINVAL
);
666 assert_return(ret
, -EINVAL
);
668 assert(path_startswith(device
->syspath
, "/sys/"));
670 *ret
= device
->syspath
;
675 static int device_new_from_child(sd_device
**ret
, sd_device
*child
) {
676 _cleanup_free_
char *path
= NULL
;
677 const char *subdir
, *syspath
;
683 r
= sd_device_get_syspath(child
, &syspath
);
687 path
= strdup(syspath
);
690 subdir
= path
+ STRLEN("/sys");
695 pos
= strrchr(subdir
, '/');
696 if (!pos
|| pos
< subdir
+ 2)
701 r
= sd_device_new_from_syspath(ret
, path
);
711 _public_
int sd_device_get_parent(sd_device
*child
, sd_device
**ret
) {
713 assert_return(ret
, -EINVAL
);
714 assert_return(child
, -EINVAL
);
716 if (!child
->parent_set
) {
717 child
->parent_set
= true;
719 (void) device_new_from_child(&child
->parent
, child
);
725 *ret
= child
->parent
;
730 int device_set_subsystem(sd_device
*device
, const char *_subsystem
) {
731 _cleanup_free_
char *subsystem
= NULL
;
737 subsystem
= strdup(_subsystem
);
741 r
= device_add_property_internal(device
, "SUBSYSTEM", subsystem
);
745 free_and_replace(device
->subsystem
, subsystem
);
747 device
->subsystem_set
= true;
752 static int device_set_drivers_subsystem(sd_device
*device
, const char *_subsystem
) {
753 _cleanup_free_
char *subsystem
= NULL
;
760 subsystem
= strdup(_subsystem
);
764 r
= device_set_subsystem(device
, "drivers");
768 free_and_replace(device
->driver_subsystem
, subsystem
);
773 _public_
int sd_device_get_subsystem(sd_device
*device
, const char **ret
) {
774 const char *syspath
, *drivers
= NULL
;
777 assert_return(ret
, -EINVAL
);
778 assert_return(device
, -EINVAL
);
780 r
= sd_device_get_syspath(device
, &syspath
);
784 if (!device
->subsystem_set
) {
785 _cleanup_free_
char *subsystem
= NULL
;
788 /* read 'subsystem' link */
789 path
= strjoina(syspath
, "/subsystem");
790 r
= readlink_value(path
, &subsystem
);
792 r
= device_set_subsystem(device
, subsystem
);
793 /* use implicit names */
794 else if (path_startswith(device
->devpath
, "/module/"))
795 r
= device_set_subsystem(device
, "module");
796 else if (!(drivers
= strstr(syspath
, "/drivers/")) &&
797 (path_startswith(device
->devpath
, "/subsystem/") ||
798 path_startswith(device
->devpath
, "/class/") ||
799 path_startswith(device
->devpath
, "/bus/")))
800 r
= device_set_subsystem(device
, "subsystem");
801 if (r
< 0 && r
!= -ENOENT
)
802 return log_debug_errno(r
, "sd-device: could not set subsystem for %s: %m", device
->devpath
);
804 device
->subsystem_set
= true;
805 } else if (!device
->driver_subsystem_set
)
806 drivers
= strstr(syspath
, "/drivers/");
808 if (!device
->driver_subsystem_set
) {
810 _cleanup_free_
char *subpath
= NULL
;
812 subpath
= strndup(syspath
, drivers
- syspath
);
818 subsys
= strrchr(subpath
, '/');
822 r
= device_set_drivers_subsystem(device
, subsys
+ 1);
824 if (r
< 0 && r
!= -ENOENT
)
825 return log_debug_errno(r
, "sd-device: could not set subsystem for driver %s: %m", device
->devpath
);
828 device
->driver_subsystem_set
= true;
831 if (!device
->subsystem
)
834 *ret
= device
->subsystem
;
839 _public_
int sd_device_get_devtype(sd_device
*device
, const char **devtype
) {
845 r
= device_read_uevent_file(device
);
849 if (!device
->devtype
)
852 *devtype
= device
->devtype
;
857 _public_
int sd_device_get_parent_with_subsystem_devtype(sd_device
*child
, const char *subsystem
, const char *devtype
, sd_device
**ret
) {
858 sd_device
*parent
= NULL
;
861 assert_return(child
, -EINVAL
);
862 assert_return(subsystem
, -EINVAL
);
864 r
= sd_device_get_parent(child
, &parent
);
866 const char *parent_subsystem
= NULL
;
867 const char *parent_devtype
= NULL
;
869 (void) sd_device_get_subsystem(parent
, &parent_subsystem
);
870 if (streq_ptr(parent_subsystem
, subsystem
)) {
874 (void) sd_device_get_devtype(parent
, &parent_devtype
);
875 if (streq_ptr(parent_devtype
, devtype
))
878 r
= sd_device_get_parent(parent
, &parent
);
889 _public_
int sd_device_get_devnum(sd_device
*device
, dev_t
*devnum
) {
892 assert_return(device
, -EINVAL
);
893 assert_return(devnum
, -EINVAL
);
895 r
= device_read_uevent_file(device
);
899 if (major(device
->devnum
) <= 0)
902 *devnum
= device
->devnum
;
907 int device_set_driver(sd_device
*device
, const char *_driver
) {
908 _cleanup_free_
char *driver
= NULL
;
914 driver
= strdup(_driver
);
918 r
= device_add_property_internal(device
, "DRIVER", driver
);
922 free_and_replace(device
->driver
, driver
);
924 device
->driver_set
= true;
929 _public_
int sd_device_get_driver(sd_device
*device
, const char **ret
) {
930 assert_return(device
, -EINVAL
);
931 assert_return(ret
, -EINVAL
);
933 if (!device
->driver_set
) {
934 _cleanup_free_
char *driver
= NULL
;
939 r
= sd_device_get_syspath(device
, &syspath
);
943 path
= strjoina(syspath
, "/driver");
944 r
= readlink_value(path
, &driver
);
946 r
= device_set_driver(device
, driver
);
948 return log_debug_errno(r
, "sd-device: could not set driver for %s: %m", device
->devpath
);
949 } else if (r
== -ENOENT
)
950 device
->driver_set
= true;
952 return log_debug_errno(r
, "sd-device: could not set driver for %s: %m", device
->devpath
);
958 *ret
= device
->driver
;
963 _public_
int sd_device_get_devpath(sd_device
*device
, const char **devpath
) {
964 assert_return(device
, -EINVAL
);
965 assert_return(devpath
, -EINVAL
);
967 assert(device
->devpath
);
968 assert(device
->devpath
[0] == '/');
970 *devpath
= device
->devpath
;
975 _public_
int sd_device_get_devname(sd_device
*device
, const char **devname
) {
978 assert_return(device
, -EINVAL
);
979 assert_return(devname
, -EINVAL
);
981 r
= device_read_uevent_file(device
);
985 if (!device
->devname
)
988 assert(path_startswith(device
->devname
, "/dev/"));
990 *devname
= device
->devname
;
995 static int device_set_sysname(sd_device
*device
) {
996 _cleanup_free_
char *sysname
= NULL
;
997 const char *sysnum
= NULL
;
1001 pos
= strrchr(device
->devpath
, '/');
1006 /* devpath is not a root directory */
1007 if (*pos
== '\0' || pos
<= device
->devpath
)
1010 sysname
= strdup(pos
);
1014 /* some devices have '!' in their name, change that to '/' */
1015 while (sysname
[len
] != '\0') {
1016 if (sysname
[len
] == '!')
1022 /* trailing number */
1023 while (len
> 0 && isdigit(sysname
[--len
]))
1024 sysnum
= &sysname
[len
];
1029 free_and_replace(device
->sysname
, sysname
);
1031 device
->sysnum
= sysnum
;
1033 device
->sysname_set
= true;
1038 _public_
int sd_device_get_sysname(sd_device
*device
, const char **ret
) {
1041 assert_return(device
, -EINVAL
);
1042 assert_return(ret
, -EINVAL
);
1044 if (!device
->sysname_set
) {
1045 r
= device_set_sysname(device
);
1050 assert_return(device
->sysname
, -ENOENT
);
1052 *ret
= device
->sysname
;
1057 _public_
int sd_device_get_sysnum(sd_device
*device
, const char **ret
) {
1060 assert_return(device
, -EINVAL
);
1061 assert_return(ret
, -EINVAL
);
1063 if (!device
->sysname_set
) {
1064 r
= device_set_sysname(device
);
1069 if (!device
->sysnum
)
1072 *ret
= device
->sysnum
;
1077 static bool is_valid_tag(const char *tag
) {
1080 return !strchr(tag
, ':') && !strchr(tag
, ' ');
1083 int device_add_tag(sd_device
*device
, const char *tag
) {
1089 if (!is_valid_tag(tag
))
1092 r
= set_ensure_allocated(&device
->tags
, &string_hash_ops
);
1096 r
= set_put_strdup(device
->tags
, tag
);
1100 device
->tags_generation
++;
1101 device
->property_tags_outdated
= true;
1106 int device_add_devlink(sd_device
*device
, const char *devlink
) {
1112 r
= set_ensure_allocated(&device
->devlinks
, &string_hash_ops
);
1116 r
= set_put_strdup(device
->devlinks
, devlink
);
1120 device
->devlinks_generation
++;
1121 device
->property_devlinks_outdated
= true;
1126 static int device_add_property_internal_from_string(sd_device
*device
, const char *str
) {
1127 _cleanup_free_
char *key
= NULL
;
1137 value
= strchr(key
, '=');
1143 if (isempty(++value
))
1146 return device_add_property_internal(device
, key
, value
);
1149 int device_set_usec_initialized(sd_device
*device
, const char *initialized
) {
1150 uint64_t usec_initialized
;
1154 assert(initialized
);
1156 r
= safe_atou64(initialized
, &usec_initialized
);
1160 r
= device_add_property_internal(device
, "USEC_INITIALIZED", initialized
);
1164 device
->usec_initialized
= usec_initialized
;
1169 static int handle_db_line(sd_device
*device
, char key
, const char *value
) {
1178 r
= device_add_tag(device
, value
);
1184 path
= strjoina("/dev/", value
);
1185 r
= device_add_devlink(device
, path
);
1191 r
= device_add_property_internal_from_string(device
, value
);
1197 r
= device_set_usec_initialized(device
, value
);
1203 r
= safe_atoi(value
, &device
->devlink_priority
);
1209 r
= safe_atoi(value
, &device
->watch_handle
);
1215 log_debug("device db: unknown key '%c'", key
);
1221 int device_get_id_filename(sd_device
*device
, const char **ret
) {
1225 if (!device
->id_filename
) {
1226 _cleanup_free_
char *id
= NULL
;
1227 const char *subsystem
;
1231 r
= sd_device_get_subsystem(device
, &subsystem
);
1235 if (sd_device_get_devnum(device
, &devnum
) >= 0) {
1238 /* use dev_t — b259:131072, c254:0 */
1239 r
= asprintf(&id
, "%c%u:%u",
1240 streq(subsystem
, "block") ? 'b' : 'c',
1241 major(devnum
), minor(devnum
));
1244 } else if (sd_device_get_ifindex(device
, &ifindex
) >= 0 && ifindex
> 0) {
1245 /* use netdev ifindex — n3 */
1246 r
= asprintf(&id
, "n%u", ifindex
);
1250 /* use $subsys:$sysname — pci:0000:00:1f.2
1251 * sysname() has '!' translated, get it from devpath
1253 const char *sysname
;
1255 sysname
= basename(device
->devpath
);
1262 if (streq(subsystem
, "drivers")) {
1263 /* the 'drivers' pseudo-subsystem is special, and needs the real subsystem
1264 * encoded as well */
1265 r
= asprintf(&id
, "+drivers:%s:%s", device
->driver_subsystem
, sysname
);
1269 r
= asprintf(&id
, "+%s:%s", subsystem
, sysname
);
1275 device
->id_filename
= TAKE_PTR(id
);
1278 *ret
= device
->id_filename
;
1283 int device_read_db_aux(sd_device
*device
, bool force
) {
1284 _cleanup_free_
char *db
= NULL
;
1286 const char *id
, *value
;
1300 if (device
->db_loaded
|| (!force
&& device
->sealed
))
1303 device
->db_loaded
= true;
1305 r
= device_get_id_filename(device
, &id
);
1309 path
= strjoina("/run/udev/data/", id
);
1311 r
= read_full_file(path
, &db
, &db_len
);
1316 return log_debug_errno(r
, "sd-device: failed to read db '%s': %m", path
);
1319 /* devices with a database entry are initialized */
1320 device
->is_initialized
= true;
1322 for (i
= 0; i
< db_len
; i
++) {
1325 if (!strchr(NEWLINE
, db
[i
])) {
1334 log_debug("sd-device: ignoring invalid db entry with key '%c'", key
);
1336 state
= INVALID_LINE
;
1351 if (strchr(NEWLINE
, db
[i
]))
1356 if (strchr(NEWLINE
, db
[i
])) {
1358 r
= handle_db_line(device
, key
, value
);
1360 log_debug_errno(r
, "sd-device: failed to handle db entry '%c:%s': %m", key
, value
);
1367 assert_not_reached("invalid state when parsing db");
1374 static int device_read_db(sd_device
*device
) {
1375 return device_read_db_aux(device
, false);
1378 _public_
int sd_device_get_is_initialized(sd_device
*device
, int *initialized
) {
1381 assert_return(device
, -EINVAL
);
1382 assert_return(initialized
, -EINVAL
);
1384 r
= device_read_db(device
);
1388 *initialized
= device
->is_initialized
;
1393 _public_
int sd_device_get_usec_since_initialized(sd_device
*device
, uint64_t *usec
) {
1397 assert_return(device
, -EINVAL
);
1398 assert_return(usec
, -EINVAL
);
1400 r
= device_read_db(device
);
1404 if (!device
->is_initialized
)
1407 if (!device
->usec_initialized
)
1410 now_ts
= now(clock_boottime_or_monotonic());
1412 if (now_ts
< device
->usec_initialized
)
1415 *usec
= now_ts
- device
->usec_initialized
;
1420 _public_
const char *sd_device_get_tag_first(sd_device
*device
) {
1423 assert_return(device
, NULL
);
1425 (void) device_read_db(device
);
1427 device
->tags_iterator_generation
= device
->tags_generation
;
1428 device
->tags_iterator
= ITERATOR_FIRST
;
1430 (void) set_iterate(device
->tags
, &device
->tags_iterator
, &v
);
1434 _public_
const char *sd_device_get_tag_next(sd_device
*device
) {
1437 assert_return(device
, NULL
);
1439 (void) device_read_db(device
);
1441 if (device
->tags_iterator_generation
!= device
->tags_generation
)
1444 (void) set_iterate(device
->tags
, &device
->tags_iterator
, &v
);
1448 _public_
const char *sd_device_get_devlink_first(sd_device
*device
) {
1451 assert_return(device
, NULL
);
1453 (void) device_read_db(device
);
1455 device
->devlinks_iterator_generation
= device
->devlinks_generation
;
1456 device
->devlinks_iterator
= ITERATOR_FIRST
;
1458 (void) set_iterate(device
->devlinks
, &device
->devlinks_iterator
, &v
);
1462 _public_
const char *sd_device_get_devlink_next(sd_device
*device
) {
1465 assert_return(device
, NULL
);
1467 (void) device_read_db(device
);
1469 if (device
->devlinks_iterator_generation
!= device
->devlinks_generation
)
1472 (void) set_iterate(device
->devlinks
, &device
->devlinks_iterator
, &v
);
1476 static int device_properties_prepare(sd_device
*device
) {
1481 r
= device_read_uevent_file(device
);
1485 r
= device_read_db(device
);
1489 if (device
->property_devlinks_outdated
) {
1490 _cleanup_free_
char *devlinks
= NULL
;
1491 size_t devlinks_allocated
= 0, devlinks_len
= 0;
1492 const char *devlink
;
1494 for (devlink
= sd_device_get_devlink_first(device
); devlink
; devlink
= sd_device_get_devlink_next(device
)) {
1497 if (!GREEDY_REALLOC(devlinks
, devlinks_allocated
, devlinks_len
+ strlen(devlink
) + 2))
1499 if (devlinks_len
> 0)
1500 stpcpy(devlinks
+ devlinks_len
++, " ");
1501 e
= stpcpy(devlinks
+ devlinks_len
, devlink
);
1502 devlinks_len
= e
- devlinks
;
1505 r
= device_add_property_internal(device
, "DEVLINKS", devlinks
);
1509 device
->property_devlinks_outdated
= false;
1512 if (device
->property_tags_outdated
) {
1513 _cleanup_free_
char *tags
= NULL
;
1514 size_t tags_allocated
= 0, tags_len
= 0;
1517 if (!GREEDY_REALLOC(tags
, tags_allocated
, 2))
1522 for (tag
= sd_device_get_tag_first(device
); tag
; tag
= sd_device_get_tag_next(device
)) {
1525 if (!GREEDY_REALLOC(tags
, tags_allocated
, tags_len
+ strlen(tag
) + 2))
1527 e
= stpcpy(stpcpy(tags
+ tags_len
, tag
), ":");
1528 tags_len
= e
- tags
;
1531 r
= device_add_property_internal(device
, "TAGS", tags
);
1535 device
->property_tags_outdated
= false;
1541 _public_
const char *sd_device_get_property_first(sd_device
*device
, const char **_value
) {
1546 assert_return(device
, NULL
);
1548 r
= device_properties_prepare(device
);
1552 device
->properties_iterator_generation
= device
->properties_generation
;
1553 device
->properties_iterator
= ITERATOR_FIRST
;
1555 ordered_hashmap_iterate(device
->properties
, &device
->properties_iterator
, (void**)&value
, (const void**)&key
);
1563 _public_
const char *sd_device_get_property_next(sd_device
*device
, const char **_value
) {
1568 assert_return(device
, NULL
);
1570 r
= device_properties_prepare(device
);
1574 if (device
->properties_iterator_generation
!= device
->properties_generation
)
1577 ordered_hashmap_iterate(device
->properties
, &device
->properties_iterator
, (void**)&value
, (const void**)&key
);
1585 static int device_sysattrs_read_all(sd_device
*device
) {
1586 _cleanup_closedir_
DIR *dir
= NULL
;
1587 const char *syspath
;
1588 struct dirent
*dent
;
1593 if (device
->sysattrs_read
)
1596 r
= sd_device_get_syspath(device
, &syspath
);
1600 dir
= opendir(syspath
);
1604 r
= set_ensure_allocated(&device
->sysattrs
, &string_hash_ops
);
1608 FOREACH_DIRENT_ALL(dent
, dir
, return -errno
) {
1610 struct stat statbuf
;
1612 /* only handle symlinks and regular files */
1613 if (!IN_SET(dent
->d_type
, DT_LNK
, DT_REG
))
1616 path
= strjoina(syspath
, "/", dent
->d_name
);
1618 if (lstat(path
, &statbuf
) != 0)
1621 if (!(statbuf
.st_mode
& S_IRUSR
))
1624 r
= set_put_strdup(device
->sysattrs
, dent
->d_name
);
1629 device
->sysattrs_read
= true;
1634 _public_
const char *sd_device_get_sysattr_first(sd_device
*device
) {
1638 assert_return(device
, NULL
);
1640 if (!device
->sysattrs_read
) {
1641 r
= device_sysattrs_read_all(device
);
1648 device
->sysattrs_iterator
= ITERATOR_FIRST
;
1650 (void) set_iterate(device
->sysattrs
, &device
->sysattrs_iterator
, &v
);
1654 _public_
const char *sd_device_get_sysattr_next(sd_device
*device
) {
1657 assert_return(device
, NULL
);
1659 if (!device
->sysattrs_read
)
1662 (void) set_iterate(device
->sysattrs
, &device
->sysattrs_iterator
, &v
);
1666 _public_
int sd_device_has_tag(sd_device
*device
, const char *tag
) {
1667 assert_return(device
, -EINVAL
);
1668 assert_return(tag
, -EINVAL
);
1670 (void) device_read_db(device
);
1672 return !!set_contains(device
->tags
, tag
);
1675 _public_
int sd_device_get_property_value(sd_device
*device
, const char *key
, const char **_value
) {
1679 assert_return(device
, -EINVAL
);
1680 assert_return(key
, -EINVAL
);
1681 assert_return(_value
, -EINVAL
);
1683 r
= device_properties_prepare(device
);
1687 value
= ordered_hashmap_get(device
->properties
, key
);
1696 /* replaces the value if it already exists */
1697 static int device_add_sysattr_value(sd_device
*device
, const char *_key
, char *value
) {
1698 _cleanup_free_
char *key
= NULL
;
1699 _cleanup_free_
char *value_old
= NULL
;
1705 r
= hashmap_ensure_allocated(&device
->sysattr_values
, &string_hash_ops
);
1709 value_old
= hashmap_remove2(device
->sysattr_values
, _key
, (void **)&key
);
1716 r
= hashmap_put(device
->sysattr_values
, key
, value
);
1725 static int device_get_sysattr_value(sd_device
*device
, const char *_key
, const char **_value
) {
1726 const char *key
= NULL
, *value
;
1731 value
= hashmap_get2(device
->sysattr_values
, _key
, (void **) &key
);
1741 /* We cache all sysattr lookups. If an attribute does not exist, it is stored
1742 * with a NULL value in the cache, otherwise the returned string is stored */
1743 _public_
int sd_device_get_sysattr_value(sd_device
*device
, const char *sysattr
, const char **_value
) {
1744 _cleanup_free_
char *value
= NULL
;
1745 const char *syspath
, *cached_value
= NULL
;
1747 struct stat statbuf
;
1750 assert_return(device
, -EINVAL
);
1751 assert_return(sysattr
, -EINVAL
);
1753 /* look for possibly already cached result */
1754 r
= device_get_sysattr_value(device
, sysattr
, &cached_value
);
1760 /* we looked up the sysattr before and it did not exist */
1764 *_value
= cached_value
;
1769 r
= sd_device_get_syspath(device
, &syspath
);
1773 path
= strjoina(syspath
, "/", sysattr
);
1774 r
= lstat(path
, &statbuf
);
1776 /* remember that we could not access the sysattr */
1777 r
= device_add_sysattr_value(device
, sysattr
, NULL
);
1782 } else if (S_ISLNK(statbuf
.st_mode
)) {
1783 /* Some core links return only the last element of the target path,
1784 * these are just values, the paths should not be exposed. */
1785 if (STR_IN_SET(sysattr
, "driver", "subsystem", "module")) {
1786 r
= readlink_value(path
, &value
);
1791 } else if (S_ISDIR(statbuf
.st_mode
)) {
1792 /* skip directories */
1794 } else if (!(statbuf
.st_mode
& S_IRUSR
)) {
1795 /* skip non-readable files */
1800 /* read attribute value */
1801 r
= read_full_file(path
, &value
, &size
);
1805 /* drop trailing newlines */
1806 while (size
> 0 && value
[--size
] == '\n')
1810 r
= device_add_sysattr_value(device
, sysattr
, value
);
1814 *_value
= TAKE_PTR(value
);
1819 static void device_remove_sysattr_value(sd_device
*device
, const char *_key
) {
1820 _cleanup_free_
char *key
= NULL
;
1821 _cleanup_free_
char *value
= NULL
;
1826 value
= hashmap_remove2(device
->sysattr_values
, _key
, (void **) &key
);
1831 /* set the attribute and save it in the cache. If a NULL value is passed the
1832 * attribute is cleared from the cache */
1833 _public_
int sd_device_set_sysattr_value(sd_device
*device
, const char *sysattr
, const char *_value
) {
1834 _cleanup_close_
int fd
= -1;
1835 _cleanup_free_
char *value
= NULL
;
1836 const char *syspath
;
1842 assert_return(device
, -EINVAL
);
1843 assert_return(sysattr
, -EINVAL
);
1846 device_remove_sysattr_value(device
, sysattr
);
1851 r
= sd_device_get_syspath(device
, &syspath
);
1855 path
= strjoina(syspath
, "/", sysattr
);
1857 fd
= open(path
, O_WRONLY
| O_CLOEXEC
| O_NOFOLLOW
);
1861 if (errno
== EISDIR
)
1868 r
= device_add_sysattr_value(device
, sysattr
, value
);
1876 len
= strlen(_value
);
1878 /* drop trailing newlines */
1879 while (len
> 0 && _value
[len
- 1] == '\n')
1882 /* value length is limited to 4k */
1886 value
= strndup(_value
, len
);
1890 size
= write(fd
, value
, len
);
1894 if ((size_t)size
!= len
)
1897 r
= device_add_sysattr_value(device
, sysattr
, value
);