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
) {
36 device
= new(sd_device
, 1);
40 *device
= (sd_device
) {
43 .devmode
= (mode_t
) -1,
46 .action
= _DEVICE_ACTION_INVALID
,
53 static sd_device
*device_free(sd_device
*device
) {
56 sd_device_unref(device
->parent
);
57 free(device
->syspath
);
58 free(device
->sysname
);
59 free(device
->devtype
);
60 free(device
->devname
);
61 free(device
->subsystem
);
62 free(device
->driver_subsystem
);
64 free(device
->id_filename
);
65 free(device
->properties_strv
);
66 free(device
->properties_nulstr
);
68 ordered_hashmap_free_free_free(device
->properties
);
69 ordered_hashmap_free_free_free(device
->properties_db
);
70 hashmap_free_free_free(device
->sysattr_values
);
71 set_free_free(device
->sysattrs
);
72 set_free_free(device
->tags
);
73 set_free_free(device
->devlinks
);
78 DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_device
, sd_device
, device_free
);
80 int device_add_property_aux(sd_device
*device
, const char *_key
, const char *_value
, bool db
) {
81 OrderedHashmap
**properties
;
87 properties
= &device
->properties_db
;
89 properties
= &device
->properties
;
92 _cleanup_free_
char *key
= NULL
, *value
= NULL
, *old_key
= NULL
, *old_value
= NULL
;
95 r
= ordered_hashmap_ensure_allocated(properties
, &string_hash_ops
);
103 value
= strdup(_value
);
107 old_value
= ordered_hashmap_get2(*properties
, key
, (void**) &old_key
);
109 r
= ordered_hashmap_replace(*properties
, key
, value
);
116 _cleanup_free_
char *key
= NULL
;
117 _cleanup_free_
char *value
= NULL
;
119 value
= ordered_hashmap_remove2(*properties
, _key
, (void**) &key
);
123 device
->properties_generation
++;
124 device
->properties_buf_outdated
= true;
130 int device_add_property_internal(sd_device
*device
, const char *key
, const char *value
) {
131 return device_add_property_aux(device
, key
, value
, false);
134 int device_set_syspath(sd_device
*device
, const char *_syspath
, bool verify
) {
135 _cleanup_free_
char *syspath
= NULL
;
142 /* must be a subdirectory of /sys */
143 if (!path_startswith(_syspath
, "/sys/"))
144 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL
),
145 "sd-device: Syspath '%s' is not a subdirectory of /sys",
149 r
= chase_symlinks(_syspath
, NULL
, 0, &syspath
);
151 return -ENODEV
; /* the device does not exist (any more?) */
153 return log_debug_errno(r
, "sd-device: Failed to get target of '%s': %m", _syspath
);
155 if (!path_startswith(syspath
, "/sys")) {
156 _cleanup_free_
char *real_sys
= NULL
, *new_syspath
= NULL
;
159 /* /sys is a symlink to somewhere sysfs is mounted on? In that case, we convert the path to real sysfs to "/sys". */
160 r
= chase_symlinks("/sys", NULL
, 0, &real_sys
);
162 return log_debug_errno(r
, "sd-device: Failed to chase symlink /sys: %m");
164 p
= path_startswith(syspath
, real_sys
);
166 return log_debug_errno(SYNTHETIC_ERRNO(ENODEV
),
167 "sd-device: Canonicalized path '%s' does not starts with sysfs mount point '%s'",
170 new_syspath
= path_join("/sys", p
);
174 free_and_replace(syspath
, new_syspath
);
175 path_simplify(syspath
, false);
178 if (path_startswith(syspath
, "/sys/devices/")) {
181 /* all 'devices' require an 'uevent' file */
182 path
= strjoina(syspath
, "/uevent");
183 r
= access(path
, F_OK
);
186 /* this is not a valid device */
189 return log_debug_errno(errno
, "sd-device: %s does not have an uevent file: %m", syspath
);
192 /* everything else just needs to be a directory */
193 if (!is_dir(syspath
, false))
197 syspath
= strdup(_syspath
);
202 devpath
= syspath
+ STRLEN("/sys");
204 r
= device_add_property_internal(device
, "DEVPATH", devpath
);
208 free_and_replace(device
->syspath
, syspath
);
209 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
);
232 _public_
int sd_device_new_from_devnum(sd_device
**ret
, char type
, dev_t devnum
) {
234 char id
[DECIMAL_STR_MAX(unsigned) * 2 + 1];
236 assert_return(ret
, -EINVAL
);
237 assert_return(IN_SET(type
, 'b', 'c'), -EINVAL
);
239 /* use /sys/dev/{block,char}/<maj>:<min> link */
240 xsprintf(id
, "%u:%u", major(devnum
), minor(devnum
));
242 syspath
= strjoina("/sys/dev/", (type
== 'b' ? "block" : "char"), "/", id
);
244 return sd_device_new_from_syspath(ret
, syspath
);
247 _public_
int sd_device_new_from_subsystem_sysname(sd_device
**ret
, const char *subsystem
, const char *sysname
) {
248 char *name
, *syspath
;
251 assert_return(ret
, -EINVAL
);
252 assert_return(subsystem
, -EINVAL
);
253 assert_return(sysname
, -EINVAL
);
255 if (streq(subsystem
, "subsystem")) {
256 syspath
= strjoina("/sys/subsystem/", sysname
);
257 if (access(syspath
, F_OK
) >= 0)
258 return sd_device_new_from_syspath(ret
, syspath
);
260 syspath
= strjoina("/sys/bus/", sysname
);
261 if (access(syspath
, F_OK
) >= 0)
262 return sd_device_new_from_syspath(ret
, syspath
);
264 syspath
= strjoina("/sys/class/", sysname
);
265 if (access(syspath
, F_OK
) >= 0)
266 return sd_device_new_from_syspath(ret
, syspath
);
267 } else if (streq(subsystem
, "module")) {
268 syspath
= strjoina("/sys/module/", sysname
);
269 if (access(syspath
, F_OK
) >= 0)
270 return sd_device_new_from_syspath(ret
, syspath
);
271 } else if (streq(subsystem
, "drivers")) {
272 char subsys
[PATH_MAX
];
275 strscpy(subsys
, sizeof(subsys
), sysname
);
276 driver
= strchr(subsys
, ':');
281 syspath
= strjoina("/sys/subsystem/", subsys
, "/drivers/", driver
);
282 if (access(syspath
, F_OK
) >= 0)
283 return sd_device_new_from_syspath(ret
, syspath
);
285 syspath
= strjoina("/sys/bus/", subsys
, "/drivers/", driver
);
286 if (access(syspath
, F_OK
) >= 0)
287 return sd_device_new_from_syspath(ret
, syspath
);
291 /* translate sysname back to sysfs filename */
292 name
= strdupa(sysname
);
293 while (name
[len
] != '\0') {
294 if (name
[len
] == '/')
300 syspath
= strjoina("/sys/subsystem/", subsystem
, "/devices/", name
);
301 if (access(syspath
, F_OK
) >= 0)
302 return sd_device_new_from_syspath(ret
, syspath
);
304 syspath
= strjoina("/sys/bus/", subsystem
, "/devices/", name
);
305 if (access(syspath
, F_OK
) >= 0)
306 return sd_device_new_from_syspath(ret
, syspath
);
308 syspath
= strjoina("/sys/class/", subsystem
, "/", name
);
309 if (access(syspath
, F_OK
) >= 0)
310 return sd_device_new_from_syspath(ret
, syspath
);
312 syspath
= strjoina("/sys/firmware/", subsystem
, "/", sysname
);
313 if (access(syspath
, F_OK
) >= 0)
314 return sd_device_new_from_syspath(ret
, syspath
);
319 int device_set_devtype(sd_device
*device
, const char *_devtype
) {
320 _cleanup_free_
char *devtype
= NULL
;
326 devtype
= strdup(_devtype
);
330 r
= device_add_property_internal(device
, "DEVTYPE", devtype
);
334 free_and_replace(device
->devtype
, devtype
);
339 int device_set_ifindex(sd_device
*device
, const char *_ifindex
) {
345 r
= parse_ifindex(_ifindex
, &ifindex
);
349 r
= device_add_property_internal(device
, "IFINDEX", _ifindex
);
353 device
->ifindex
= ifindex
;
358 int device_set_devname(sd_device
*device
, const char *_devname
) {
359 _cleanup_free_
char *devname
= NULL
;
365 if (_devname
[0] != '/') {
366 r
= asprintf(&devname
, "/dev/%s", _devname
);
370 devname
= strdup(_devname
);
375 r
= device_add_property_internal(device
, "DEVNAME", devname
);
379 free_and_replace(device
->devname
, devname
);
384 int device_set_devmode(sd_device
*device
, const char *_devmode
) {
391 r
= safe_atou(_devmode
, &devmode
);
398 r
= device_add_property_internal(device
, "DEVMODE", _devmode
);
402 device
->devmode
= devmode
;
407 int device_set_devnum(sd_device
*device
, const char *major
, const char *minor
) {
408 unsigned maj
= 0, min
= 0;
414 r
= safe_atou(major
, &maj
);
421 r
= safe_atou(minor
, &min
);
426 r
= device_add_property_internal(device
, "MAJOR", major
);
431 r
= device_add_property_internal(device
, "MINOR", minor
);
436 device
->devnum
= makedev(maj
, min
);
441 static int handle_uevent_line(sd_device
*device
, const char *key
, const char *value
, const char **major
, const char **minor
) {
450 if (streq(key
, "DEVTYPE")) {
451 r
= device_set_devtype(device
, value
);
454 } else if (streq(key
, "IFINDEX")) {
455 r
= device_set_ifindex(device
, value
);
458 } else if (streq(key
, "DEVNAME")) {
459 r
= device_set_devname(device
, value
);
462 } else if (streq(key
, "DEVMODE")) {
463 r
= device_set_devmode(device
, value
);
466 } else if (streq(key
, "MAJOR"))
468 else if (streq(key
, "MINOR"))
471 r
= device_add_property_internal(device
, key
, value
);
479 int device_read_uevent_file(sd_device
*device
) {
480 _cleanup_free_
char *uevent
= NULL
;
481 const char *syspath
, *key
= NULL
, *value
= NULL
, *major
= NULL
, *minor
= NULL
;
497 if (device
->uevent_loaded
|| device
->sealed
)
500 r
= sd_device_get_syspath(device
, &syspath
);
504 path
= strjoina(syspath
, "/uevent");
506 r
= read_full_file(path
, &uevent
, &uevent_len
);
508 /* empty uevent files may be write-only */
509 device
->uevent_loaded
= true;
513 /* some devices may not have uevent files, see set_syspath() */
516 return log_device_debug_errno(device
, r
, "sd-device: Failed to read uevent file '%s': %m", path
);
518 device
->uevent_loaded
= true;
520 for (i
= 0; i
< uevent_len
; i
++)
523 if (!strchr(NEWLINE
, uevent
[i
])) {
531 if (uevent
[i
] == '=') {
535 } else if (strchr(NEWLINE
, uevent
[i
])) {
537 log_device_debug(device
, "sd-device: Invalid uevent line '%s', ignoring", key
);
547 _fallthrough_
; /* to handle empty property */
549 if (strchr(NEWLINE
, uevent
[i
])) {
552 r
= handle_uevent_line(device
, key
, value
, &major
, &minor
);
554 log_device_debug_errno(device
, r
, "sd-device: Failed to handle uevent entry '%s=%s', ignoring: %m", key
, value
);
561 assert_not_reached("Invalid state when parsing uevent file");
565 r
= device_set_devnum(device
, major
, minor
);
567 log_device_debug_errno(device
, r
, "sd-device: Failed to set 'MAJOR=%s' or 'MINOR=%s' from '%s', ignoring: %m", major
, minor
, path
);
573 _public_
int sd_device_get_ifindex(sd_device
*device
, int *ifindex
) {
576 assert_return(device
, -EINVAL
);
578 r
= device_read_uevent_file(device
);
582 if (device
->ifindex
<= 0)
586 *ifindex
= device
->ifindex
;
591 _public_
int sd_device_new_from_device_id(sd_device
**ret
, const char *id
) {
594 assert_return(ret
, -EINVAL
);
595 assert_return(id
, -EINVAL
);
605 r
= parse_dev(id
+ 1, &devt
);
609 return sd_device_new_from_devnum(ret
, id
[0], devt
);
613 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
614 _cleanup_close_
int sk
= -1;
615 struct ifreq ifr
= {};
618 r
= parse_ifindex(&id
[1], &ifr
.ifr_ifindex
);
622 sk
= socket_ioctl_fd();
626 r
= ioctl(sk
, SIOCGIFNAME
, &ifr
);
630 r
= sd_device_new_from_subsystem_sysname(&device
, "net", ifr
.ifr_name
);
634 r
= sd_device_get_ifindex(device
, &ifindex
);
638 /* this is racey, so we might end up with the wrong device */
639 if (ifr
.ifr_ifindex
!= ifindex
)
642 *ret
= TAKE_PTR(device
);
647 char subsys
[PATH_MAX
];
650 (void) strscpy(subsys
, sizeof(subsys
), id
+ 1);
651 sysname
= strchr(subsys
, ':');
658 return sd_device_new_from_subsystem_sysname(ret
, subsys
, sysname
);
666 _public_
int sd_device_get_syspath(sd_device
*device
, const char **ret
) {
667 assert_return(device
, -EINVAL
);
668 assert_return(ret
, -EINVAL
);
670 assert(path_startswith(device
->syspath
, "/sys/"));
672 *ret
= device
->syspath
;
677 static int device_new_from_child(sd_device
**ret
, sd_device
*child
) {
678 _cleanup_free_
char *path
= NULL
;
679 const char *subdir
, *syspath
;
685 r
= sd_device_get_syspath(child
, &syspath
);
689 path
= strdup(syspath
);
692 subdir
= path
+ STRLEN("/sys");
697 pos
= strrchr(subdir
, '/');
698 if (!pos
|| pos
< subdir
+ 2)
703 r
= sd_device_new_from_syspath(ret
, path
);
713 _public_
int sd_device_get_parent(sd_device
*child
, sd_device
**ret
) {
715 assert_return(ret
, -EINVAL
);
716 assert_return(child
, -EINVAL
);
718 if (!child
->parent_set
) {
719 child
->parent_set
= true;
721 (void) device_new_from_child(&child
->parent
, child
);
727 *ret
= child
->parent
;
731 int device_set_subsystem(sd_device
*device
, const char *_subsystem
) {
732 _cleanup_free_
char *subsystem
= NULL
;
738 subsystem
= strdup(_subsystem
);
742 r
= device_add_property_internal(device
, "SUBSYSTEM", subsystem
);
746 device
->subsystem_set
= true;
747 return free_and_replace(device
->subsystem
, subsystem
);
750 static int device_set_drivers_subsystem(sd_device
*device
, const char *_subsystem
) {
751 _cleanup_free_
char *subsystem
= NULL
;
758 subsystem
= strdup(_subsystem
);
762 r
= device_set_subsystem(device
, "drivers");
766 return free_and_replace(device
->driver_subsystem
, subsystem
);
769 _public_
int sd_device_get_subsystem(sd_device
*device
, const char **ret
) {
770 const char *syspath
, *drivers
= NULL
;
773 assert_return(ret
, -EINVAL
);
774 assert_return(device
, -EINVAL
);
776 r
= sd_device_get_syspath(device
, &syspath
);
780 if (!device
->subsystem_set
) {
781 _cleanup_free_
char *subsystem
= NULL
;
784 /* read 'subsystem' link */
785 path
= strjoina(syspath
, "/subsystem");
786 r
= readlink_value(path
, &subsystem
);
788 r
= device_set_subsystem(device
, subsystem
);
789 /* use implicit names */
790 else if (path_startswith(device
->devpath
, "/module/"))
791 r
= device_set_subsystem(device
, "module");
792 else if (!(drivers
= strstr(syspath
, "/drivers/")) &&
793 PATH_STARTSWITH_SET(device
->devpath
, "/subsystem/",
796 r
= device_set_subsystem(device
, "subsystem");
797 if (r
< 0 && r
!= -ENOENT
)
798 return log_device_debug_errno(device
, r
, "sd-device: Failed to set subsystem for %s: %m", device
->devpath
);
800 device
->subsystem_set
= true;
801 } else if (!device
->driver_subsystem_set
)
802 drivers
= strstr(syspath
, "/drivers/");
804 if (!device
->driver_subsystem_set
) {
806 _cleanup_free_
char *subpath
= NULL
;
808 subpath
= strndup(syspath
, drivers
- syspath
);
814 subsys
= strrchr(subpath
, '/');
818 r
= device_set_drivers_subsystem(device
, subsys
+ 1);
820 if (r
< 0 && r
!= -ENOENT
)
821 return log_device_debug_errno(device
, r
, "sd-device: Failed to set subsystem for driver %s: %m", device
->devpath
);
824 device
->driver_subsystem_set
= true;
827 if (!device
->subsystem
)
830 *ret
= device
->subsystem
;
834 _public_
int sd_device_get_devtype(sd_device
*device
, const char **devtype
) {
840 r
= device_read_uevent_file(device
);
844 if (!device
->devtype
)
847 *devtype
= device
->devtype
;
852 _public_
int sd_device_get_parent_with_subsystem_devtype(sd_device
*child
, const char *subsystem
, const char *devtype
, sd_device
**ret
) {
853 sd_device
*parent
= NULL
;
856 assert_return(child
, -EINVAL
);
857 assert_return(subsystem
, -EINVAL
);
859 r
= sd_device_get_parent(child
, &parent
);
861 const char *parent_subsystem
= NULL
;
862 const char *parent_devtype
= NULL
;
864 (void) sd_device_get_subsystem(parent
, &parent_subsystem
);
865 if (streq_ptr(parent_subsystem
, subsystem
)) {
869 (void) sd_device_get_devtype(parent
, &parent_devtype
);
870 if (streq_ptr(parent_devtype
, devtype
))
873 r
= sd_device_get_parent(parent
, &parent
);
883 _public_
int sd_device_get_devnum(sd_device
*device
, dev_t
*devnum
) {
886 assert_return(device
, -EINVAL
);
888 r
= device_read_uevent_file(device
);
892 if (major(device
->devnum
) <= 0)
896 *devnum
= device
->devnum
;
901 int device_set_driver(sd_device
*device
, const char *_driver
) {
902 _cleanup_free_
char *driver
= NULL
;
908 driver
= strdup(_driver
);
912 r
= device_add_property_internal(device
, "DRIVER", driver
);
916 device
->driver_set
= true;
917 return free_and_replace(device
->driver
, driver
);
920 _public_
int sd_device_get_driver(sd_device
*device
, const char **ret
) {
921 assert_return(device
, -EINVAL
);
922 assert_return(ret
, -EINVAL
);
924 if (!device
->driver_set
) {
925 _cleanup_free_
char *driver
= NULL
;
930 r
= sd_device_get_syspath(device
, &syspath
);
934 path
= strjoina(syspath
, "/driver");
935 r
= readlink_value(path
, &driver
);
937 r
= device_set_driver(device
, driver
);
939 return log_device_debug_errno(device
, r
, "sd-device: Failed to set driver for %s: %m", device
->devpath
);
940 } else if (r
== -ENOENT
)
941 device
->driver_set
= true;
943 return log_device_debug_errno(device
, r
, "sd-device: Failed to set driver for %s: %m", device
->devpath
);
949 *ret
= device
->driver
;
953 _public_
int sd_device_get_devpath(sd_device
*device
, const char **devpath
) {
954 assert_return(device
, -EINVAL
);
955 assert_return(devpath
, -EINVAL
);
957 assert(device
->devpath
);
958 assert(device
->devpath
[0] == '/');
960 *devpath
= device
->devpath
;
964 _public_
int sd_device_get_devname(sd_device
*device
, const char **devname
) {
967 assert_return(device
, -EINVAL
);
968 assert_return(devname
, -EINVAL
);
970 r
= device_read_uevent_file(device
);
974 if (!device
->devname
)
977 assert(path_startswith(device
->devname
, "/dev/"));
979 *devname
= device
->devname
;
983 static int device_set_sysname(sd_device
*device
) {
984 _cleanup_free_
char *sysname
= NULL
;
985 const char *sysnum
= NULL
;
989 if (!device
->devpath
)
992 pos
= strrchr(device
->devpath
, '/');
997 /* devpath is not a root directory */
998 if (*pos
== '\0' || pos
<= device
->devpath
)
1001 sysname
= strdup(pos
);
1005 /* some devices have '!' in their name, change that to '/' */
1006 while (sysname
[len
] != '\0') {
1007 if (sysname
[len
] == '!')
1013 /* trailing number */
1014 while (len
> 0 && isdigit(sysname
[--len
]))
1015 sysnum
= &sysname
[len
];
1020 device
->sysname_set
= true;
1021 device
->sysnum
= sysnum
;
1022 return free_and_replace(device
->sysname
, sysname
);
1025 _public_
int sd_device_get_sysname(sd_device
*device
, const char **ret
) {
1028 assert_return(device
, -EINVAL
);
1029 assert_return(ret
, -EINVAL
);
1031 if (!device
->sysname_set
) {
1032 r
= device_set_sysname(device
);
1037 assert_return(device
->sysname
, -ENOENT
);
1039 *ret
= device
->sysname
;
1043 _public_
int sd_device_get_sysnum(sd_device
*device
, const char **ret
) {
1046 assert_return(device
, -EINVAL
);
1047 assert_return(ret
, -EINVAL
);
1049 if (!device
->sysname_set
) {
1050 r
= device_set_sysname(device
);
1055 if (!device
->sysnum
)
1058 *ret
= device
->sysnum
;
1062 static bool is_valid_tag(const char *tag
) {
1065 return !strchr(tag
, ':') && !strchr(tag
, ' ');
1068 int device_add_tag(sd_device
*device
, const char *tag
) {
1074 if (!is_valid_tag(tag
))
1077 r
= set_ensure_allocated(&device
->tags
, &string_hash_ops
);
1081 r
= set_put_strdup(device
->tags
, tag
);
1085 device
->tags_generation
++;
1086 device
->property_tags_outdated
= true;
1091 int device_add_devlink(sd_device
*device
, const char *devlink
) {
1097 r
= set_ensure_allocated(&device
->devlinks
, &string_hash_ops
);
1101 r
= set_put_strdup(device
->devlinks
, devlink
);
1105 device
->devlinks_generation
++;
1106 device
->property_devlinks_outdated
= true;
1111 static int device_add_property_internal_from_string(sd_device
*device
, const char *str
) {
1112 _cleanup_free_
char *key
= NULL
;
1123 value
= strchr(key
, '=');
1129 if (isempty(++value
))
1132 /* Add the property to both sd_device::properties and sd_device::properties_db,
1133 * as this is called by only handle_db_line(). */
1134 r
= device_add_property_aux(device
, key
, value
, false);
1138 return device_add_property_aux(device
, key
, value
, true);
1141 int device_set_usec_initialized(sd_device
*device
, usec_t when
) {
1142 char s
[DECIMAL_STR_MAX(usec_t
)];
1147 xsprintf(s
, USEC_FMT
, when
);
1149 r
= device_add_property_internal(device
, "USEC_INITIALIZED", s
);
1153 device
->usec_initialized
= when
;
1157 static int handle_db_line(sd_device
*device
, char key
, const char *value
) {
1166 r
= device_add_tag(device
, value
);
1172 path
= strjoina("/dev/", value
);
1173 r
= device_add_devlink(device
, path
);
1179 r
= device_add_property_internal_from_string(device
, value
);
1187 r
= safe_atou64(value
, &t
);
1191 r
= device_set_usec_initialized(device
, t
);
1198 r
= safe_atoi(value
, &device
->devlink_priority
);
1204 r
= safe_atoi(value
, &device
->watch_handle
);
1210 log_device_debug(device
, "sd-device: Unknown key '%c' in device db, ignoring", key
);
1216 int device_get_id_filename(sd_device
*device
, const char **ret
) {
1220 if (!device
->id_filename
) {
1221 _cleanup_free_
char *id
= NULL
;
1222 const char *subsystem
;
1226 r
= sd_device_get_subsystem(device
, &subsystem
);
1230 if (sd_device_get_devnum(device
, &devnum
) >= 0) {
1233 /* use dev_t — b259:131072, c254:0 */
1234 r
= asprintf(&id
, "%c%u:%u",
1235 streq(subsystem
, "block") ? 'b' : 'c',
1236 major(devnum
), minor(devnum
));
1239 } else if (sd_device_get_ifindex(device
, &ifindex
) >= 0) {
1240 /* use netdev ifindex — n3 */
1241 r
= asprintf(&id
, "n%u", (unsigned) ifindex
);
1245 /* use $subsys:$sysname — pci:0000:00:1f.2
1246 * sysname() has '!' translated, get it from devpath
1248 const char *sysname
;
1250 sysname
= basename(device
->devpath
);
1257 if (streq(subsystem
, "drivers")) {
1258 /* the 'drivers' pseudo-subsystem is special, and needs the real subsystem
1259 * encoded as well */
1260 r
= asprintf(&id
, "+drivers:%s:%s", device
->driver_subsystem
, sysname
);
1264 r
= asprintf(&id
, "+%s:%s", subsystem
, sysname
);
1270 device
->id_filename
= TAKE_PTR(id
);
1273 *ret
= device
->id_filename
;
1277 int device_read_db_internal_filename(sd_device
*device
, const char *filename
) {
1278 _cleanup_free_
char *db
= NULL
;
1295 r
= read_full_file(filename
, &db
, &db_len
);
1300 return log_device_debug_errno(device
, r
, "sd-device: Failed to read db '%s': %m", filename
);
1303 /* devices with a database entry are initialized */
1304 device
->is_initialized
= true;
1306 device
->db_loaded
= true;
1308 for (i
= 0; i
< db_len
; i
++) {
1311 if (!strchr(NEWLINE
, db
[i
])) {
1320 log_device_debug(device
, "sd-device: Invalid db entry with key '%c', ignoring", key
);
1322 state
= INVALID_LINE
;
1337 if (strchr(NEWLINE
, db
[i
]))
1342 if (strchr(NEWLINE
, db
[i
])) {
1344 r
= handle_db_line(device
, key
, value
);
1346 log_device_debug_errno(device
, r
, "sd-device: Failed to handle db entry '%c:%s', ignoring: %m", key
, value
);
1353 return log_device_debug_errno(device
, SYNTHETIC_ERRNO(EINVAL
), "sd-device: invalid db syntax.");
1360 int device_read_db_internal(sd_device
*device
, bool force
) {
1361 const char *id
, *path
;
1366 if (device
->db_loaded
|| (!force
&& device
->sealed
))
1369 r
= device_get_id_filename(device
, &id
);
1373 path
= strjoina("/run/udev/data/", id
);
1375 return device_read_db_internal_filename(device
, path
);
1378 _public_
int sd_device_get_is_initialized(sd_device
*device
) {
1381 assert_return(device
, -EINVAL
);
1383 r
= device_read_db(device
);
1387 return device
->is_initialized
;
1390 _public_
int sd_device_get_usec_since_initialized(sd_device
*device
, uint64_t *usec
) {
1394 assert_return(device
, -EINVAL
);
1395 assert_return(usec
, -EINVAL
);
1397 r
= device_read_db(device
);
1401 if (!device
->is_initialized
)
1404 if (!device
->usec_initialized
)
1407 now_ts
= now(clock_boottime_or_monotonic());
1409 if (now_ts
< device
->usec_initialized
)
1412 *usec
= now_ts
- device
->usec_initialized
;
1416 _public_
const char *sd_device_get_tag_first(sd_device
*device
) {
1419 assert_return(device
, NULL
);
1421 (void) device_read_db(device
);
1423 device
->tags_iterator_generation
= device
->tags_generation
;
1424 device
->tags_iterator
= ITERATOR_FIRST
;
1426 (void) set_iterate(device
->tags
, &device
->tags_iterator
, &v
);
1430 _public_
const char *sd_device_get_tag_next(sd_device
*device
) {
1433 assert_return(device
, NULL
);
1435 (void) device_read_db(device
);
1437 if (device
->tags_iterator_generation
!= device
->tags_generation
)
1440 (void) set_iterate(device
->tags
, &device
->tags_iterator
, &v
);
1444 _public_
const char *sd_device_get_devlink_first(sd_device
*device
) {
1447 assert_return(device
, NULL
);
1449 (void) device_read_db(device
);
1451 device
->devlinks_iterator_generation
= device
->devlinks_generation
;
1452 device
->devlinks_iterator
= ITERATOR_FIRST
;
1454 (void) set_iterate(device
->devlinks
, &device
->devlinks_iterator
, &v
);
1458 _public_
const char *sd_device_get_devlink_next(sd_device
*device
) {
1461 assert_return(device
, NULL
);
1463 (void) device_read_db(device
);
1465 if (device
->devlinks_iterator_generation
!= device
->devlinks_generation
)
1468 (void) set_iterate(device
->devlinks
, &device
->devlinks_iterator
, &v
);
1472 int device_properties_prepare(sd_device
*device
) {
1477 r
= device_read_uevent_file(device
);
1481 r
= device_read_db(device
);
1485 if (device
->property_devlinks_outdated
) {
1486 _cleanup_free_
char *devlinks
= NULL
;
1487 size_t devlinks_allocated
= 0, devlinks_len
= 0;
1488 const char *devlink
;
1490 for (devlink
= sd_device_get_devlink_first(device
); devlink
; devlink
= sd_device_get_devlink_next(device
)) {
1493 if (!GREEDY_REALLOC(devlinks
, devlinks_allocated
, devlinks_len
+ strlen(devlink
) + 2))
1495 if (devlinks_len
> 0)
1496 stpcpy(devlinks
+ devlinks_len
++, " ");
1497 e
= stpcpy(devlinks
+ devlinks_len
, devlink
);
1498 devlinks_len
= e
- devlinks
;
1501 r
= device_add_property_internal(device
, "DEVLINKS", devlinks
);
1505 device
->property_devlinks_outdated
= false;
1508 if (device
->property_tags_outdated
) {
1509 _cleanup_free_
char *tags
= NULL
;
1510 size_t tags_allocated
= 0, tags_len
= 0;
1513 if (!GREEDY_REALLOC(tags
, tags_allocated
, 2))
1518 for (tag
= sd_device_get_tag_first(device
); tag
; tag
= sd_device_get_tag_next(device
)) {
1521 if (!GREEDY_REALLOC(tags
, tags_allocated
, tags_len
+ strlen(tag
) + 2))
1523 e
= stpcpy(stpcpy(tags
+ tags_len
, tag
), ":");
1524 tags_len
= e
- tags
;
1527 r
= device_add_property_internal(device
, "TAGS", tags
);
1531 device
->property_tags_outdated
= false;
1537 _public_
const char *sd_device_get_property_first(sd_device
*device
, const char **_value
) {
1542 assert_return(device
, NULL
);
1544 r
= device_properties_prepare(device
);
1548 device
->properties_iterator_generation
= device
->properties_generation
;
1549 device
->properties_iterator
= ITERATOR_FIRST
;
1551 ordered_hashmap_iterate(device
->properties
, &device
->properties_iterator
, (void**)&value
, (const void**)&key
);
1558 _public_
const char *sd_device_get_property_next(sd_device
*device
, const char **_value
) {
1563 assert_return(device
, NULL
);
1565 r
= device_properties_prepare(device
);
1569 if (device
->properties_iterator_generation
!= device
->properties_generation
)
1572 ordered_hashmap_iterate(device
->properties
, &device
->properties_iterator
, (void**)&value
, (const void**)&key
);
1579 static int device_sysattrs_read_all(sd_device
*device
) {
1580 _cleanup_closedir_
DIR *dir
= NULL
;
1581 const char *syspath
;
1582 struct dirent
*dent
;
1587 if (device
->sysattrs_read
)
1590 r
= sd_device_get_syspath(device
, &syspath
);
1594 dir
= opendir(syspath
);
1598 r
= set_ensure_allocated(&device
->sysattrs
, &string_hash_ops
);
1602 FOREACH_DIRENT_ALL(dent
, dir
, return -errno
) {
1604 struct stat statbuf
;
1606 /* only handle symlinks and regular files */
1607 if (!IN_SET(dent
->d_type
, DT_LNK
, DT_REG
))
1610 path
= strjoina(syspath
, "/", dent
->d_name
);
1612 if (lstat(path
, &statbuf
) != 0)
1615 if (!(statbuf
.st_mode
& S_IRUSR
))
1618 r
= set_put_strdup(device
->sysattrs
, dent
->d_name
);
1623 device
->sysattrs_read
= true;
1628 _public_
const char *sd_device_get_sysattr_first(sd_device
*device
) {
1632 assert_return(device
, NULL
);
1634 if (!device
->sysattrs_read
) {
1635 r
= device_sysattrs_read_all(device
);
1642 device
->sysattrs_iterator
= ITERATOR_FIRST
;
1644 (void) set_iterate(device
->sysattrs
, &device
->sysattrs_iterator
, &v
);
1648 _public_
const char *sd_device_get_sysattr_next(sd_device
*device
) {
1651 assert_return(device
, NULL
);
1653 if (!device
->sysattrs_read
)
1656 (void) set_iterate(device
->sysattrs
, &device
->sysattrs_iterator
, &v
);
1660 _public_
int sd_device_has_tag(sd_device
*device
, const char *tag
) {
1661 assert_return(device
, -EINVAL
);
1662 assert_return(tag
, -EINVAL
);
1664 (void) device_read_db(device
);
1666 return !!set_contains(device
->tags
, tag
);
1669 _public_
int sd_device_get_property_value(sd_device
*device
, const char *key
, const char **_value
) {
1673 assert_return(device
, -EINVAL
);
1674 assert_return(key
, -EINVAL
);
1676 r
= device_properties_prepare(device
);
1680 value
= ordered_hashmap_get(device
->properties
, key
);
1689 /* replaces the value if it already exists */
1690 static int device_add_sysattr_value(sd_device
*device
, const char *_key
, char *value
) {
1691 _cleanup_free_
char *key
= NULL
;
1692 _cleanup_free_
char *value_old
= NULL
;
1698 r
= hashmap_ensure_allocated(&device
->sysattr_values
, &string_hash_ops
);
1702 value_old
= hashmap_remove2(device
->sysattr_values
, _key
, (void **)&key
);
1709 r
= hashmap_put(device
->sysattr_values
, key
, value
);
1717 static int device_get_sysattr_value(sd_device
*device
, const char *_key
, const char **_value
) {
1718 const char *key
= NULL
, *value
;
1723 value
= hashmap_get2(device
->sysattr_values
, _key
, (void **) &key
);
1732 /* We cache all sysattr lookups. If an attribute does not exist, it is stored
1733 * with a NULL value in the cache, otherwise the returned string is stored */
1734 _public_
int sd_device_get_sysattr_value(sd_device
*device
, const char *sysattr
, const char **_value
) {
1735 _cleanup_free_
char *value
= NULL
;
1736 const char *syspath
, *cached_value
= NULL
;
1738 struct stat statbuf
;
1741 assert_return(device
, -EINVAL
);
1742 assert_return(sysattr
, -EINVAL
);
1744 /* look for possibly already cached result */
1745 r
= device_get_sysattr_value(device
, sysattr
, &cached_value
);
1751 /* we looked up the sysattr before and it did not exist */
1755 *_value
= cached_value
;
1760 r
= sd_device_get_syspath(device
, &syspath
);
1764 path
= strjoina(syspath
, "/", sysattr
);
1765 r
= lstat(path
, &statbuf
);
1767 /* remember that we could not access the sysattr */
1768 r
= device_add_sysattr_value(device
, sysattr
, NULL
);
1773 } else if (S_ISLNK(statbuf
.st_mode
)) {
1774 /* Some core links return only the last element of the target path,
1775 * these are just values, the paths should not be exposed. */
1776 if (STR_IN_SET(sysattr
, "driver", "subsystem", "module")) {
1777 r
= readlink_value(path
, &value
);
1782 } else if (S_ISDIR(statbuf
.st_mode
)) {
1783 /* skip directories */
1785 } else if (!(statbuf
.st_mode
& S_IRUSR
)) {
1786 /* skip non-readable files */
1791 /* read attribute value */
1792 r
= read_full_file(path
, &value
, &size
);
1796 /* drop trailing newlines */
1797 while (size
> 0 && value
[--size
] == '\n')
1801 r
= device_add_sysattr_value(device
, sysattr
, value
);
1805 *_value
= TAKE_PTR(value
);
1810 static void device_remove_sysattr_value(sd_device
*device
, const char *_key
) {
1811 _cleanup_free_
char *key
= NULL
;
1816 free(hashmap_remove2(device
->sysattr_values
, _key
, (void **) &key
));
1819 /* set the attribute and save it in the cache. If a NULL value is passed the
1820 * attribute is cleared from the cache */
1821 _public_
int sd_device_set_sysattr_value(sd_device
*device
, const char *sysattr
, const char *_value
) {
1822 _cleanup_free_
char *value
= NULL
;
1823 const char *syspath
, *path
;
1827 assert_return(device
, -EINVAL
);
1828 assert_return(sysattr
, -EINVAL
);
1831 device_remove_sysattr_value(device
, sysattr
);
1835 r
= sd_device_get_syspath(device
, &syspath
);
1839 path
= strjoina(syspath
, "/", sysattr
);
1841 len
= strlen(_value
);
1843 /* drop trailing newlines */
1844 while (len
> 0 && _value
[len
- 1] == '\n')
1847 /* value length is limited to 4k */
1851 value
= strndup(_value
, len
);
1855 r
= write_string_file(path
, value
, WRITE_STRING_FILE_DISABLE_BUFFER
| WRITE_STRING_FILE_NOFOLLOW
);
1862 r
= free_and_strdup(&value
, "");
1866 r
= device_add_sysattr_value(device
, sysattr
, value
);
1874 r
= device_add_sysattr_value(device
, sysattr
, value
);