1 /* SPDX-License-Identifier: LGPL-2.1+ */
10 #include "alloc-util.h"
11 #include "device-internal.h"
12 #include "device-private.h"
13 #include "device-util.h"
14 #include "dirent-util.h"
20 #include "parse-util.h"
21 #include "path-util.h"
23 #include "socket-util.h"
24 #include "stat-util.h"
25 #include "stdio-util.h"
26 #include "string-util.h"
31 int device_new_aux(sd_device
**ret
) {
32 sd_device
*device
= NULL
;
36 device
= new(sd_device
, 1);
40 *device
= (sd_device
) {
43 .devmode
= (mode_t
) -1,
52 static sd_device
*device_free(sd_device
*device
) {
55 sd_device_unref(device
->parent
);
56 free(device
->syspath
);
57 free(device
->sysname
);
58 free(device
->devtype
);
59 free(device
->devname
);
60 free(device
->subsystem
);
61 free(device
->driver_subsystem
);
63 free(device
->id_filename
);
64 free(device
->properties_strv
);
65 free(device
->properties_nulstr
);
67 ordered_hashmap_free_free_free(device
->properties
);
68 ordered_hashmap_free_free_free(device
->properties_db
);
69 hashmap_free_free_free(device
->sysattr_values
);
70 set_free_free(device
->sysattrs
);
71 set_free_free(device
->tags
);
72 set_free_free(device
->devlinks
);
77 DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_device
, sd_device
, device_free
);
79 int device_add_property_aux(sd_device
*device
, const char *_key
, const char *_value
, bool db
) {
80 OrderedHashmap
**properties
;
86 properties
= &device
->properties_db
;
88 properties
= &device
->properties
;
91 _cleanup_free_
char *key
= NULL
, *value
= NULL
, *old_key
= NULL
, *old_value
= NULL
;
94 r
= ordered_hashmap_ensure_allocated(properties
, &string_hash_ops
);
102 value
= strdup(_value
);
106 old_value
= ordered_hashmap_get2(*properties
, key
, (void**) &old_key
);
108 r
= ordered_hashmap_replace(*properties
, key
, value
);
115 _cleanup_free_
char *key
= NULL
;
116 _cleanup_free_
char *value
= NULL
;
118 value
= ordered_hashmap_remove2(*properties
, _key
, (void**) &key
);
122 device
->properties_generation
++;
123 device
->properties_buf_outdated
= true;
129 int device_add_property_internal(sd_device
*device
, const char *key
, const char *value
) {
130 return device_add_property_aux(device
, key
, value
, false);
133 int device_set_syspath(sd_device
*device
, const char *_syspath
, bool verify
) {
134 _cleanup_free_
char *syspath
= NULL
;
141 /* must be a subdirectory of /sys */
142 if (!path_startswith(_syspath
, "/sys/"))
143 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL
),
144 "sd-device: Syspath '%s' is not a subdirectory of /sys",
148 r
= chase_symlinks(_syspath
, NULL
, 0, &syspath
);
150 return -ENODEV
; /* the device does not exist (any more?) */
152 return log_debug_errno(r
, "sd-device: Failed to get target of '%s': %m", _syspath
);
154 if (!path_startswith(syspath
, "/sys")) {
155 _cleanup_free_
char *real_sys
= NULL
, *new_syspath
= NULL
;
158 /* /sys is a symlink to somewhere sysfs is mounted on? In that case, we convert the path to real sysfs to "/sys". */
159 r
= chase_symlinks("/sys", NULL
, 0, &real_sys
);
161 return log_debug_errno(r
, "sd-device: Failed to chase symlink /sys: %m");
163 p
= path_startswith(syspath
, real_sys
);
165 return log_debug_errno(SYNTHETIC_ERRNO(ENODEV
),
166 "sd-device: Canonicalized path '%s' does not starts with sysfs mount point '%s'",
169 new_syspath
= strjoin("/sys/", p
);
173 free_and_replace(syspath
, new_syspath
);
174 path_simplify(syspath
, false);
177 if (path_startswith(syspath
, "/sys/devices/")) {
180 /* all 'devices' require an 'uevent' file */
181 path
= strjoina(syspath
, "/uevent");
182 r
= access(path
, F_OK
);
185 /* this is not a valid device */
188 return log_debug_errno(errno
, "sd-device: %s does not have an uevent file: %m", syspath
);
191 /* everything else just needs to be a directory */
192 if (!is_dir(syspath
, false))
196 syspath
= strdup(_syspath
);
201 devpath
= syspath
+ STRLEN("/sys");
203 r
= device_add_property_internal(device
, "DEVPATH", devpath
);
207 free_and_replace(device
->syspath
, syspath
);
209 device
->devpath
= devpath
;
214 _public_
int sd_device_new_from_syspath(sd_device
**ret
, const char *syspath
) {
215 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
218 assert_return(ret
, -EINVAL
);
219 assert_return(syspath
, -EINVAL
);
221 r
= device_new_aux(&device
);
225 r
= device_set_syspath(device
, syspath
, true);
229 *ret
= TAKE_PTR(device
);
234 _public_
int sd_device_new_from_devnum(sd_device
**ret
, char type
, dev_t devnum
) {
236 char id
[DECIMAL_STR_MAX(unsigned) * 2 + 1];
238 assert_return(ret
, -EINVAL
);
239 assert_return(IN_SET(type
, 'b', 'c'), -EINVAL
);
241 /* use /sys/dev/{block,char}/<maj>:<min> link */
242 snprintf(id
, sizeof(id
), "%u:%u", major(devnum
), minor(devnum
));
244 syspath
= strjoina("/sys/dev/", (type
== 'b' ? "block" : "char"), "/", id
);
246 return sd_device_new_from_syspath(ret
, syspath
);
249 _public_
int sd_device_new_from_subsystem_sysname(sd_device
**ret
, const char *subsystem
, const char *sysname
) {
250 char *name
, *syspath
;
253 assert_return(ret
, -EINVAL
);
254 assert_return(subsystem
, -EINVAL
);
255 assert_return(sysname
, -EINVAL
);
257 if (streq(subsystem
, "subsystem")) {
258 syspath
= strjoina("/sys/subsystem/", sysname
);
259 if (access(syspath
, F_OK
) >= 0)
260 return sd_device_new_from_syspath(ret
, syspath
);
262 syspath
= strjoina("/sys/bus/", sysname
);
263 if (access(syspath
, F_OK
) >= 0)
264 return sd_device_new_from_syspath(ret
, syspath
);
266 syspath
= strjoina("/sys/class/", sysname
);
267 if (access(syspath
, F_OK
) >= 0)
268 return sd_device_new_from_syspath(ret
, syspath
);
269 } else if (streq(subsystem
, "module")) {
270 syspath
= strjoina("/sys/module/", sysname
);
271 if (access(syspath
, F_OK
) >= 0)
272 return sd_device_new_from_syspath(ret
, syspath
);
273 } else if (streq(subsystem
, "drivers")) {
274 char subsys
[PATH_MAX
];
277 strscpy(subsys
, sizeof(subsys
), sysname
);
278 driver
= strchr(subsys
, ':');
283 syspath
= strjoina("/sys/subsystem/", subsys
, "/drivers/", driver
);
284 if (access(syspath
, F_OK
) >= 0)
285 return sd_device_new_from_syspath(ret
, syspath
);
287 syspath
= strjoina("/sys/bus/", subsys
, "/drivers/", driver
);
288 if (access(syspath
, F_OK
) >= 0)
289 return sd_device_new_from_syspath(ret
, syspath
);
293 /* translate sysname back to sysfs filename */
294 name
= strdupa(sysname
);
295 while (name
[len
] != '\0') {
296 if (name
[len
] == '/')
302 syspath
= strjoina("/sys/subsystem/", subsystem
, "/devices/", name
);
303 if (access(syspath
, F_OK
) >= 0)
304 return sd_device_new_from_syspath(ret
, syspath
);
306 syspath
= strjoina("/sys/bus/", subsystem
, "/devices/", name
);
307 if (access(syspath
, F_OK
) >= 0)
308 return sd_device_new_from_syspath(ret
, syspath
);
310 syspath
= strjoina("/sys/class/", subsystem
, "/", name
);
311 if (access(syspath
, F_OK
) >= 0)
312 return sd_device_new_from_syspath(ret
, syspath
);
314 syspath
= strjoina("/sys/firmware/", subsystem
, "/", sysname
);
315 if (access(syspath
, F_OK
) >= 0)
316 return sd_device_new_from_syspath(ret
, syspath
);
321 int device_set_devtype(sd_device
*device
, const char *_devtype
) {
322 _cleanup_free_
char *devtype
= NULL
;
328 devtype
= strdup(_devtype
);
332 r
= device_add_property_internal(device
, "DEVTYPE", devtype
);
336 free_and_replace(device
->devtype
, devtype
);
341 int device_set_ifindex(sd_device
*device
, const char *_ifindex
) {
347 r
= parse_ifindex(_ifindex
, &ifindex
);
351 r
= device_add_property_internal(device
, "IFINDEX", _ifindex
);
355 device
->ifindex
= ifindex
;
360 int device_set_devname(sd_device
*device
, const char *_devname
) {
361 _cleanup_free_
char *devname
= NULL
;
367 if (_devname
[0] != '/') {
368 r
= asprintf(&devname
, "/dev/%s", _devname
);
372 devname
= strdup(_devname
);
377 r
= device_add_property_internal(device
, "DEVNAME", devname
);
381 free_and_replace(device
->devname
, devname
);
386 int device_set_devmode(sd_device
*device
, const char *_devmode
) {
393 r
= safe_atou(_devmode
, &devmode
);
400 r
= device_add_property_internal(device
, "DEVMODE", _devmode
);
404 device
->devmode
= devmode
;
409 int device_set_devnum(sd_device
*device
, const char *major
, const char *minor
) {
410 unsigned maj
= 0, min
= 0;
416 r
= safe_atou(major
, &maj
);
423 r
= safe_atou(minor
, &min
);
428 r
= device_add_property_internal(device
, "MAJOR", major
);
433 r
= device_add_property_internal(device
, "MINOR", minor
);
438 device
->devnum
= makedev(maj
, min
);
443 static int handle_uevent_line(sd_device
*device
, const char *key
, const char *value
, const char **major
, const char **minor
) {
452 if (streq(key
, "DEVTYPE")) {
453 r
= device_set_devtype(device
, value
);
456 } else if (streq(key
, "IFINDEX")) {
457 r
= device_set_ifindex(device
, value
);
460 } else if (streq(key
, "DEVNAME")) {
461 r
= device_set_devname(device
, value
);
464 } else if (streq(key
, "DEVMODE")) {
465 r
= device_set_devmode(device
, value
);
468 } else if (streq(key
, "MAJOR"))
470 else if (streq(key
, "MINOR"))
473 r
= device_add_property_internal(device
, key
, value
);
481 int device_read_uevent_file(sd_device
*device
) {
482 _cleanup_free_
char *uevent
= NULL
;
483 const char *syspath
, *key
= NULL
, *value
= NULL
, *major
= NULL
, *minor
= NULL
;
499 if (device
->uevent_loaded
|| device
->sealed
)
502 r
= sd_device_get_syspath(device
, &syspath
);
506 path
= strjoina(syspath
, "/uevent");
508 r
= read_full_file(path
, &uevent
, &uevent_len
);
510 /* empty uevent files may be write-only */
511 device
->uevent_loaded
= true;
515 /* some devices may not have uevent files, see set_syspath() */
518 return log_device_debug_errno(device
, r
, "sd-device: Failed to read uevent file '%s': %m", path
);
520 device
->uevent_loaded
= true;
522 for (i
= 0; i
< uevent_len
; i
++)
525 if (!strchr(NEWLINE
, uevent
[i
])) {
533 if (uevent
[i
] == '=') {
537 } else if (strchr(NEWLINE
, uevent
[i
])) {
539 log_device_debug(device
, "sd-device: Invalid uevent line '%s', ignoring", key
);
549 _fallthrough_
; /* to handle empty property */
551 if (strchr(NEWLINE
, uevent
[i
])) {
554 r
= handle_uevent_line(device
, key
, value
, &major
, &minor
);
556 log_device_debug_errno(device
, r
, "sd-device: Failed to handle uevent entry '%s=%s', ignoring: %m", key
, value
);
563 assert_not_reached("Invalid state when parsing uevent file");
567 r
= device_set_devnum(device
, major
, minor
);
569 log_device_debug_errno(device
, r
, "sd-device: Failed to set 'MAJOR=%s' or 'MINOR=%s' from '%s', ignoring: %m", major
, minor
, path
);
575 _public_
int sd_device_get_ifindex(sd_device
*device
, int *ifindex
) {
578 assert_return(device
, -EINVAL
);
580 r
= device_read_uevent_file(device
);
584 if (device
->ifindex
<= 0)
588 *ifindex
= device
->ifindex
;
593 _public_
int sd_device_new_from_device_id(sd_device
**ret
, const char *id
) {
596 assert_return(ret
, -EINVAL
);
597 assert_return(id
, -EINVAL
);
607 r
= parse_dev(id
+ 1, &devt
);
611 return sd_device_new_from_devnum(ret
, id
[0], devt
);
615 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
616 _cleanup_close_
int sk
= -1;
617 struct ifreq ifr
= {};
620 r
= parse_ifindex(&id
[1], &ifr
.ifr_ifindex
);
624 sk
= socket_ioctl_fd();
628 r
= ioctl(sk
, SIOCGIFNAME
, &ifr
);
632 r
= sd_device_new_from_subsystem_sysname(&device
, "net", ifr
.ifr_name
);
636 r
= sd_device_get_ifindex(device
, &ifindex
);
640 /* this is racey, so we might end up with the wrong device */
641 if (ifr
.ifr_ifindex
!= ifindex
)
644 *ret
= TAKE_PTR(device
);
650 char subsys
[PATH_MAX
];
653 (void) strscpy(subsys
, sizeof(subsys
), id
+ 1);
654 sysname
= strchr(subsys
, ':');
661 return sd_device_new_from_subsystem_sysname(ret
, subsys
, sysname
);
668 _public_
int sd_device_get_syspath(sd_device
*device
, const char **ret
) {
669 assert_return(device
, -EINVAL
);
670 assert_return(ret
, -EINVAL
);
672 assert(path_startswith(device
->syspath
, "/sys/"));
674 *ret
= device
->syspath
;
679 static int device_new_from_child(sd_device
**ret
, sd_device
*child
) {
680 _cleanup_free_
char *path
= NULL
;
681 const char *subdir
, *syspath
;
687 r
= sd_device_get_syspath(child
, &syspath
);
691 path
= strdup(syspath
);
694 subdir
= path
+ STRLEN("/sys");
699 pos
= strrchr(subdir
, '/');
700 if (!pos
|| pos
< subdir
+ 2)
705 r
= sd_device_new_from_syspath(ret
, path
);
715 _public_
int sd_device_get_parent(sd_device
*child
, sd_device
**ret
) {
717 assert_return(ret
, -EINVAL
);
718 assert_return(child
, -EINVAL
);
720 if (!child
->parent_set
) {
721 child
->parent_set
= true;
723 (void) device_new_from_child(&child
->parent
, child
);
729 *ret
= child
->parent
;
734 int device_set_subsystem(sd_device
*device
, const char *_subsystem
) {
735 _cleanup_free_
char *subsystem
= NULL
;
741 subsystem
= strdup(_subsystem
);
745 r
= device_add_property_internal(device
, "SUBSYSTEM", subsystem
);
749 free_and_replace(device
->subsystem
, subsystem
);
751 device
->subsystem_set
= true;
756 static int device_set_drivers_subsystem(sd_device
*device
, const char *_subsystem
) {
757 _cleanup_free_
char *subsystem
= NULL
;
764 subsystem
= strdup(_subsystem
);
768 r
= device_set_subsystem(device
, "drivers");
772 free_and_replace(device
->driver_subsystem
, subsystem
);
777 _public_
int sd_device_get_subsystem(sd_device
*device
, const char **ret
) {
778 const char *syspath
, *drivers
= NULL
;
781 assert_return(ret
, -EINVAL
);
782 assert_return(device
, -EINVAL
);
784 r
= sd_device_get_syspath(device
, &syspath
);
788 if (!device
->subsystem_set
) {
789 _cleanup_free_
char *subsystem
= NULL
;
792 /* read 'subsystem' link */
793 path
= strjoina(syspath
, "/subsystem");
794 r
= readlink_value(path
, &subsystem
);
796 r
= device_set_subsystem(device
, subsystem
);
797 /* use implicit names */
798 else if (path_startswith(device
->devpath
, "/module/"))
799 r
= device_set_subsystem(device
, "module");
800 else if (!(drivers
= strstr(syspath
, "/drivers/")) &&
801 PATH_STARTSWITH_SET(device
->devpath
, "/subsystem/",
804 r
= device_set_subsystem(device
, "subsystem");
805 if (r
< 0 && r
!= -ENOENT
)
806 return log_device_debug_errno(device
, r
, "sd-device: Failed to set subsystem for %s: %m", device
->devpath
);
808 device
->subsystem_set
= true;
809 } else if (!device
->driver_subsystem_set
)
810 drivers
= strstr(syspath
, "/drivers/");
812 if (!device
->driver_subsystem_set
) {
814 _cleanup_free_
char *subpath
= NULL
;
816 subpath
= strndup(syspath
, drivers
- syspath
);
822 subsys
= strrchr(subpath
, '/');
826 r
= device_set_drivers_subsystem(device
, subsys
+ 1);
828 if (r
< 0 && r
!= -ENOENT
)
829 return log_device_debug_errno(device
, r
, "sd-device: Failed to set subsystem for driver %s: %m", device
->devpath
);
832 device
->driver_subsystem_set
= true;
835 if (!device
->subsystem
)
838 *ret
= device
->subsystem
;
843 _public_
int sd_device_get_devtype(sd_device
*device
, const char **devtype
) {
849 r
= device_read_uevent_file(device
);
853 if (!device
->devtype
)
856 *devtype
= device
->devtype
;
861 _public_
int sd_device_get_parent_with_subsystem_devtype(sd_device
*child
, const char *subsystem
, const char *devtype
, sd_device
**ret
) {
862 sd_device
*parent
= NULL
;
865 assert_return(child
, -EINVAL
);
866 assert_return(subsystem
, -EINVAL
);
868 r
= sd_device_get_parent(child
, &parent
);
870 const char *parent_subsystem
= NULL
;
871 const char *parent_devtype
= NULL
;
873 (void) sd_device_get_subsystem(parent
, &parent_subsystem
);
874 if (streq_ptr(parent_subsystem
, subsystem
)) {
878 (void) sd_device_get_devtype(parent
, &parent_devtype
);
879 if (streq_ptr(parent_devtype
, devtype
))
882 r
= sd_device_get_parent(parent
, &parent
);
893 _public_
int sd_device_get_devnum(sd_device
*device
, dev_t
*devnum
) {
896 assert_return(device
, -EINVAL
);
898 r
= device_read_uevent_file(device
);
902 if (major(device
->devnum
) <= 0)
906 *devnum
= device
->devnum
;
911 int device_set_driver(sd_device
*device
, const char *_driver
) {
912 _cleanup_free_
char *driver
= NULL
;
918 driver
= strdup(_driver
);
922 r
= device_add_property_internal(device
, "DRIVER", driver
);
926 free_and_replace(device
->driver
, driver
);
928 device
->driver_set
= true;
933 _public_
int sd_device_get_driver(sd_device
*device
, const char **ret
) {
934 assert_return(device
, -EINVAL
);
935 assert_return(ret
, -EINVAL
);
937 if (!device
->driver_set
) {
938 _cleanup_free_
char *driver
= NULL
;
943 r
= sd_device_get_syspath(device
, &syspath
);
947 path
= strjoina(syspath
, "/driver");
948 r
= readlink_value(path
, &driver
);
950 r
= device_set_driver(device
, driver
);
952 return log_device_debug_errno(device
, r
, "sd-device: Failed to set driver for %s: %m", device
->devpath
);
953 } else if (r
== -ENOENT
)
954 device
->driver_set
= true;
956 return log_device_debug_errno(device
, r
, "sd-device: Failed to set driver for %s: %m", device
->devpath
);
962 *ret
= device
->driver
;
967 _public_
int sd_device_get_devpath(sd_device
*device
, const char **devpath
) {
968 assert_return(device
, -EINVAL
);
969 assert_return(devpath
, -EINVAL
);
971 assert(device
->devpath
);
972 assert(device
->devpath
[0] == '/');
974 *devpath
= device
->devpath
;
979 _public_
int sd_device_get_devname(sd_device
*device
, const char **devname
) {
982 assert_return(device
, -EINVAL
);
983 assert_return(devname
, -EINVAL
);
985 r
= device_read_uevent_file(device
);
989 if (!device
->devname
)
992 assert(path_startswith(device
->devname
, "/dev/"));
994 *devname
= device
->devname
;
999 static int device_set_sysname(sd_device
*device
) {
1000 _cleanup_free_
char *sysname
= NULL
;
1001 const char *sysnum
= NULL
;
1005 if (!device
->devpath
)
1008 pos
= strrchr(device
->devpath
, '/');
1013 /* devpath is not a root directory */
1014 if (*pos
== '\0' || pos
<= device
->devpath
)
1017 sysname
= strdup(pos
);
1021 /* some devices have '!' in their name, change that to '/' */
1022 while (sysname
[len
] != '\0') {
1023 if (sysname
[len
] == '!')
1029 /* trailing number */
1030 while (len
> 0 && isdigit(sysname
[--len
]))
1031 sysnum
= &sysname
[len
];
1036 free_and_replace(device
->sysname
, sysname
);
1038 device
->sysnum
= sysnum
;
1040 device
->sysname_set
= true;
1045 _public_
int sd_device_get_sysname(sd_device
*device
, const char **ret
) {
1048 assert_return(device
, -EINVAL
);
1049 assert_return(ret
, -EINVAL
);
1051 if (!device
->sysname_set
) {
1052 r
= device_set_sysname(device
);
1057 assert_return(device
->sysname
, -ENOENT
);
1059 *ret
= device
->sysname
;
1064 _public_
int sd_device_get_sysnum(sd_device
*device
, const char **ret
) {
1067 assert_return(device
, -EINVAL
);
1068 assert_return(ret
, -EINVAL
);
1070 if (!device
->sysname_set
) {
1071 r
= device_set_sysname(device
);
1076 if (!device
->sysnum
)
1079 *ret
= device
->sysnum
;
1084 static bool is_valid_tag(const char *tag
) {
1087 return !strchr(tag
, ':') && !strchr(tag
, ' ');
1090 int device_add_tag(sd_device
*device
, const char *tag
) {
1096 if (!is_valid_tag(tag
))
1099 r
= set_ensure_allocated(&device
->tags
, &string_hash_ops
);
1103 r
= set_put_strdup(device
->tags
, tag
);
1107 device
->tags_generation
++;
1108 device
->property_tags_outdated
= true;
1113 int device_add_devlink(sd_device
*device
, const char *devlink
) {
1119 r
= set_ensure_allocated(&device
->devlinks
, &string_hash_ops
);
1123 r
= set_put_strdup(device
->devlinks
, devlink
);
1127 device
->devlinks_generation
++;
1128 device
->property_devlinks_outdated
= true;
1133 static int device_add_property_internal_from_string(sd_device
*device
, const char *str
) {
1134 _cleanup_free_
char *key
= NULL
;
1144 value
= strchr(key
, '=');
1150 if (isempty(++value
))
1153 return device_add_property_internal(device
, key
, value
);
1156 int device_set_usec_initialized(sd_device
*device
, usec_t when
) {
1157 char s
[DECIMAL_STR_MAX(usec_t
)];
1162 xsprintf(s
, USEC_FMT
, when
);
1164 r
= device_add_property_internal(device
, "USEC_INITIALIZED", s
);
1168 device
->usec_initialized
= when
;
1172 static int handle_db_line(sd_device
*device
, char key
, const char *value
) {
1181 r
= device_add_tag(device
, value
);
1187 path
= strjoina("/dev/", value
);
1188 r
= device_add_devlink(device
, path
);
1194 r
= device_add_property_internal_from_string(device
, value
);
1202 r
= safe_atou64(value
, &t
);
1206 r
= device_set_usec_initialized(device
, t
);
1213 r
= safe_atoi(value
, &device
->devlink_priority
);
1219 r
= safe_atoi(value
, &device
->watch_handle
);
1225 log_device_debug(device
, "sd-device: Unknown key '%c' in device db, ignoring", key
);
1231 int device_get_id_filename(sd_device
*device
, const char **ret
) {
1235 if (!device
->id_filename
) {
1236 _cleanup_free_
char *id
= NULL
;
1237 const char *subsystem
;
1241 r
= sd_device_get_subsystem(device
, &subsystem
);
1245 if (sd_device_get_devnum(device
, &devnum
) >= 0) {
1248 /* use dev_t — b259:131072, c254:0 */
1249 r
= asprintf(&id
, "%c%u:%u",
1250 streq(subsystem
, "block") ? 'b' : 'c',
1251 major(devnum
), minor(devnum
));
1254 } else if (sd_device_get_ifindex(device
, &ifindex
) >= 0) {
1255 /* use netdev ifindex — n3 */
1256 r
= asprintf(&id
, "n%u", (unsigned) ifindex
);
1260 /* use $subsys:$sysname — pci:0000:00:1f.2
1261 * sysname() has '!' translated, get it from devpath
1263 const char *sysname
;
1265 sysname
= basename(device
->devpath
);
1272 if (streq(subsystem
, "drivers")) {
1273 /* the 'drivers' pseudo-subsystem is special, and needs the real subsystem
1274 * encoded as well */
1275 r
= asprintf(&id
, "+drivers:%s:%s", device
->driver_subsystem
, sysname
);
1279 r
= asprintf(&id
, "+%s:%s", subsystem
, sysname
);
1285 device
->id_filename
= TAKE_PTR(id
);
1288 *ret
= device
->id_filename
;
1293 int device_read_db_internal(sd_device
*device
, bool force
) {
1294 _cleanup_free_
char *db
= NULL
;
1296 const char *id
, *value
;
1312 if (device
->db_loaded
|| (!force
&& device
->sealed
))
1315 r
= device_get_id_filename(device
, &id
);
1319 path
= strjoina("/run/udev/data/", id
);
1321 r
= read_full_file(path
, &db
, &db_len
);
1326 return log_device_debug_errno(device
, r
, "sd-device: Failed to read db '%s': %m", path
);
1329 /* devices with a database entry are initialized */
1330 device
->is_initialized
= true;
1332 device
->db_loaded
= true;
1334 for (i
= 0; i
< db_len
; i
++) {
1337 if (!strchr(NEWLINE
, db
[i
])) {
1346 log_device_debug(device
, "sd-device: Invalid db entry with key '%c', ignoring", key
);
1348 state
= INVALID_LINE
;
1363 if (strchr(NEWLINE
, db
[i
]))
1368 if (strchr(NEWLINE
, db
[i
])) {
1370 r
= handle_db_line(device
, key
, value
);
1372 log_device_debug_errno(device
, r
, "sd-device: Failed to handle db entry '%c:%s', ignoring: %m", key
, value
);
1379 assert_not_reached("Invalid state when parsing db");
1386 _public_
int sd_device_get_is_initialized(sd_device
*device
) {
1389 assert_return(device
, -EINVAL
);
1391 r
= device_read_db(device
);
1395 return device
->is_initialized
;
1398 _public_
int sd_device_get_usec_since_initialized(sd_device
*device
, uint64_t *usec
) {
1402 assert_return(device
, -EINVAL
);
1403 assert_return(usec
, -EINVAL
);
1405 r
= device_read_db(device
);
1409 if (!device
->is_initialized
)
1412 if (!device
->usec_initialized
)
1415 now_ts
= now(clock_boottime_or_monotonic());
1417 if (now_ts
< device
->usec_initialized
)
1420 *usec
= now_ts
- device
->usec_initialized
;
1425 _public_
const char *sd_device_get_tag_first(sd_device
*device
) {
1428 assert_return(device
, NULL
);
1430 (void) device_read_db(device
);
1432 device
->tags_iterator_generation
= device
->tags_generation
;
1433 device
->tags_iterator
= ITERATOR_FIRST
;
1435 (void) set_iterate(device
->tags
, &device
->tags_iterator
, &v
);
1439 _public_
const char *sd_device_get_tag_next(sd_device
*device
) {
1442 assert_return(device
, NULL
);
1444 (void) device_read_db(device
);
1446 if (device
->tags_iterator_generation
!= device
->tags_generation
)
1449 (void) set_iterate(device
->tags
, &device
->tags_iterator
, &v
);
1453 _public_
const char *sd_device_get_devlink_first(sd_device
*device
) {
1456 assert_return(device
, NULL
);
1458 (void) device_read_db(device
);
1460 device
->devlinks_iterator_generation
= device
->devlinks_generation
;
1461 device
->devlinks_iterator
= ITERATOR_FIRST
;
1463 (void) set_iterate(device
->devlinks
, &device
->devlinks_iterator
, &v
);
1467 _public_
const char *sd_device_get_devlink_next(sd_device
*device
) {
1470 assert_return(device
, NULL
);
1472 (void) device_read_db(device
);
1474 if (device
->devlinks_iterator_generation
!= device
->devlinks_generation
)
1477 (void) set_iterate(device
->devlinks
, &device
->devlinks_iterator
, &v
);
1481 static int device_properties_prepare(sd_device
*device
) {
1486 r
= device_read_uevent_file(device
);
1490 r
= device_read_db(device
);
1494 if (device
->property_devlinks_outdated
) {
1495 _cleanup_free_
char *devlinks
= NULL
;
1496 size_t devlinks_allocated
= 0, devlinks_len
= 0;
1497 const char *devlink
;
1499 for (devlink
= sd_device_get_devlink_first(device
); devlink
; devlink
= sd_device_get_devlink_next(device
)) {
1502 if (!GREEDY_REALLOC(devlinks
, devlinks_allocated
, devlinks_len
+ strlen(devlink
) + 2))
1504 if (devlinks_len
> 0)
1505 stpcpy(devlinks
+ devlinks_len
++, " ");
1506 e
= stpcpy(devlinks
+ devlinks_len
, devlink
);
1507 devlinks_len
= e
- devlinks
;
1510 r
= device_add_property_internal(device
, "DEVLINKS", devlinks
);
1514 device
->property_devlinks_outdated
= false;
1517 if (device
->property_tags_outdated
) {
1518 _cleanup_free_
char *tags
= NULL
;
1519 size_t tags_allocated
= 0, tags_len
= 0;
1522 if (!GREEDY_REALLOC(tags
, tags_allocated
, 2))
1527 for (tag
= sd_device_get_tag_first(device
); tag
; tag
= sd_device_get_tag_next(device
)) {
1530 if (!GREEDY_REALLOC(tags
, tags_allocated
, tags_len
+ strlen(tag
) + 2))
1532 e
= stpcpy(stpcpy(tags
+ tags_len
, tag
), ":");
1533 tags_len
= e
- tags
;
1536 r
= device_add_property_internal(device
, "TAGS", tags
);
1540 device
->property_tags_outdated
= false;
1546 _public_
const char *sd_device_get_property_first(sd_device
*device
, const char **_value
) {
1551 assert_return(device
, NULL
);
1553 r
= device_properties_prepare(device
);
1557 device
->properties_iterator_generation
= device
->properties_generation
;
1558 device
->properties_iterator
= ITERATOR_FIRST
;
1560 ordered_hashmap_iterate(device
->properties
, &device
->properties_iterator
, (void**)&value
, (const void**)&key
);
1568 _public_
const char *sd_device_get_property_next(sd_device
*device
, const char **_value
) {
1573 assert_return(device
, NULL
);
1575 r
= device_properties_prepare(device
);
1579 if (device
->properties_iterator_generation
!= device
->properties_generation
)
1582 ordered_hashmap_iterate(device
->properties
, &device
->properties_iterator
, (void**)&value
, (const void**)&key
);
1590 static int device_sysattrs_read_all(sd_device
*device
) {
1591 _cleanup_closedir_
DIR *dir
= NULL
;
1592 const char *syspath
;
1593 struct dirent
*dent
;
1598 if (device
->sysattrs_read
)
1601 r
= sd_device_get_syspath(device
, &syspath
);
1605 dir
= opendir(syspath
);
1609 r
= set_ensure_allocated(&device
->sysattrs
, &string_hash_ops
);
1613 FOREACH_DIRENT_ALL(dent
, dir
, return -errno
) {
1615 struct stat statbuf
;
1617 /* only handle symlinks and regular files */
1618 if (!IN_SET(dent
->d_type
, DT_LNK
, DT_REG
))
1621 path
= strjoina(syspath
, "/", dent
->d_name
);
1623 if (lstat(path
, &statbuf
) != 0)
1626 if (!(statbuf
.st_mode
& S_IRUSR
))
1629 r
= set_put_strdup(device
->sysattrs
, dent
->d_name
);
1634 device
->sysattrs_read
= true;
1639 _public_
const char *sd_device_get_sysattr_first(sd_device
*device
) {
1643 assert_return(device
, NULL
);
1645 if (!device
->sysattrs_read
) {
1646 r
= device_sysattrs_read_all(device
);
1653 device
->sysattrs_iterator
= ITERATOR_FIRST
;
1655 (void) set_iterate(device
->sysattrs
, &device
->sysattrs_iterator
, &v
);
1659 _public_
const char *sd_device_get_sysattr_next(sd_device
*device
) {
1662 assert_return(device
, NULL
);
1664 if (!device
->sysattrs_read
)
1667 (void) set_iterate(device
->sysattrs
, &device
->sysattrs_iterator
, &v
);
1671 _public_
int sd_device_has_tag(sd_device
*device
, const char *tag
) {
1672 assert_return(device
, -EINVAL
);
1673 assert_return(tag
, -EINVAL
);
1675 (void) device_read_db(device
);
1677 return !!set_contains(device
->tags
, tag
);
1680 _public_
int sd_device_get_property_value(sd_device
*device
, const char *key
, const char **_value
) {
1684 assert_return(device
, -EINVAL
);
1685 assert_return(key
, -EINVAL
);
1687 r
= device_properties_prepare(device
);
1691 value
= ordered_hashmap_get(device
->properties
, key
);
1701 /* replaces the value if it already exists */
1702 static int device_add_sysattr_value(sd_device
*device
, const char *_key
, char *value
) {
1703 _cleanup_free_
char *key
= NULL
;
1704 _cleanup_free_
char *value_old
= NULL
;
1710 r
= hashmap_ensure_allocated(&device
->sysattr_values
, &string_hash_ops
);
1714 value_old
= hashmap_remove2(device
->sysattr_values
, _key
, (void **)&key
);
1721 r
= hashmap_put(device
->sysattr_values
, key
, value
);
1730 static int device_get_sysattr_value(sd_device
*device
, const char *_key
, const char **_value
) {
1731 const char *key
= NULL
, *value
;
1736 value
= hashmap_get2(device
->sysattr_values
, _key
, (void **) &key
);
1746 /* We cache all sysattr lookups. If an attribute does not exist, it is stored
1747 * with a NULL value in the cache, otherwise the returned string is stored */
1748 _public_
int sd_device_get_sysattr_value(sd_device
*device
, const char *sysattr
, const char **_value
) {
1749 _cleanup_free_
char *value
= NULL
;
1750 const char *syspath
, *cached_value
= NULL
;
1752 struct stat statbuf
;
1755 assert_return(device
, -EINVAL
);
1756 assert_return(sysattr
, -EINVAL
);
1758 /* look for possibly already cached result */
1759 r
= device_get_sysattr_value(device
, sysattr
, &cached_value
);
1765 /* we looked up the sysattr before and it did not exist */
1769 *_value
= cached_value
;
1774 r
= sd_device_get_syspath(device
, &syspath
);
1778 path
= strjoina(syspath
, "/", sysattr
);
1779 r
= lstat(path
, &statbuf
);
1781 /* remember that we could not access the sysattr */
1782 r
= device_add_sysattr_value(device
, sysattr
, NULL
);
1787 } else if (S_ISLNK(statbuf
.st_mode
)) {
1788 /* Some core links return only the last element of the target path,
1789 * these are just values, the paths should not be exposed. */
1790 if (STR_IN_SET(sysattr
, "driver", "subsystem", "module")) {
1791 r
= readlink_value(path
, &value
);
1796 } else if (S_ISDIR(statbuf
.st_mode
)) {
1797 /* skip directories */
1799 } else if (!(statbuf
.st_mode
& S_IRUSR
)) {
1800 /* skip non-readable files */
1805 /* read attribute value */
1806 r
= read_full_file(path
, &value
, &size
);
1810 /* drop trailing newlines */
1811 while (size
> 0 && value
[--size
] == '\n')
1815 r
= device_add_sysattr_value(device
, sysattr
, value
);
1819 *_value
= TAKE_PTR(value
);
1824 static void device_remove_sysattr_value(sd_device
*device
, const char *_key
) {
1825 _cleanup_free_
char *key
= NULL
;
1826 _cleanup_free_
char *value
= NULL
;
1831 value
= hashmap_remove2(device
->sysattr_values
, _key
, (void **) &key
);
1836 /* set the attribute and save it in the cache. If a NULL value is passed the
1837 * attribute is cleared from the cache */
1838 _public_
int sd_device_set_sysattr_value(sd_device
*device
, const char *sysattr
, const char *_value
) {
1839 _cleanup_free_
char *value
= NULL
;
1840 const char *syspath
, *path
;
1844 assert_return(device
, -EINVAL
);
1845 assert_return(sysattr
, -EINVAL
);
1848 device_remove_sysattr_value(device
, sysattr
);
1853 r
= sd_device_get_syspath(device
, &syspath
);
1857 path
= strjoina(syspath
, "/", sysattr
);
1859 len
= strlen(_value
);
1861 /* drop trailing newlines */
1862 while (len
> 0 && _value
[len
- 1] == '\n')
1865 /* value length is limited to 4k */
1869 value
= strndup(_value
, len
);
1873 r
= write_string_file(path
, value
, WRITE_STRING_FILE_DISABLE_BUFFER
| WRITE_STRING_FILE_NOFOLLOW
);
1885 r
= device_add_sysattr_value(device
, sysattr
, value
);
1893 r
= device_add_sysattr_value(device
, sysattr
, value
);