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,
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
);
208 device
->devpath
= devpath
;
212 _public_
int sd_device_new_from_syspath(sd_device
**ret
, const char *syspath
) {
213 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
216 assert_return(ret
, -EINVAL
);
217 assert_return(syspath
, -EINVAL
);
219 r
= device_new_aux(&device
);
223 r
= device_set_syspath(device
, syspath
, true);
227 *ret
= TAKE_PTR(device
);
231 _public_
int sd_device_new_from_devnum(sd_device
**ret
, char type
, dev_t devnum
) {
233 char id
[DECIMAL_STR_MAX(unsigned) * 2 + 1];
235 assert_return(ret
, -EINVAL
);
236 assert_return(IN_SET(type
, 'b', 'c'), -EINVAL
);
238 /* use /sys/dev/{block,char}/<maj>:<min> link */
239 snprintf(id
, sizeof(id
), "%u:%u", major(devnum
), minor(devnum
));
241 syspath
= strjoina("/sys/dev/", (type
== 'b' ? "block" : "char"), "/", id
);
243 return sd_device_new_from_syspath(ret
, syspath
);
246 _public_
int sd_device_new_from_subsystem_sysname(sd_device
**ret
, const char *subsystem
, const char *sysname
) {
247 char *name
, *syspath
;
250 assert_return(ret
, -EINVAL
);
251 assert_return(subsystem
, -EINVAL
);
252 assert_return(sysname
, -EINVAL
);
254 if (streq(subsystem
, "subsystem")) {
255 syspath
= strjoina("/sys/subsystem/", sysname
);
256 if (access(syspath
, F_OK
) >= 0)
257 return sd_device_new_from_syspath(ret
, syspath
);
259 syspath
= strjoina("/sys/bus/", sysname
);
260 if (access(syspath
, F_OK
) >= 0)
261 return sd_device_new_from_syspath(ret
, syspath
);
263 syspath
= strjoina("/sys/class/", sysname
);
264 if (access(syspath
, F_OK
) >= 0)
265 return sd_device_new_from_syspath(ret
, syspath
);
266 } else if (streq(subsystem
, "module")) {
267 syspath
= strjoina("/sys/module/", sysname
);
268 if (access(syspath
, F_OK
) >= 0)
269 return sd_device_new_from_syspath(ret
, syspath
);
270 } else if (streq(subsystem
, "drivers")) {
271 char subsys
[PATH_MAX
];
274 strscpy(subsys
, sizeof(subsys
), sysname
);
275 driver
= strchr(subsys
, ':');
280 syspath
= strjoina("/sys/subsystem/", subsys
, "/drivers/", driver
);
281 if (access(syspath
, F_OK
) >= 0)
282 return sd_device_new_from_syspath(ret
, syspath
);
284 syspath
= strjoina("/sys/bus/", subsys
, "/drivers/", driver
);
285 if (access(syspath
, F_OK
) >= 0)
286 return sd_device_new_from_syspath(ret
, syspath
);
290 /* translate sysname back to sysfs filename */
291 name
= strdupa(sysname
);
292 while (name
[len
] != '\0') {
293 if (name
[len
] == '/')
299 syspath
= strjoina("/sys/subsystem/", subsystem
, "/devices/", name
);
300 if (access(syspath
, F_OK
) >= 0)
301 return sd_device_new_from_syspath(ret
, syspath
);
303 syspath
= strjoina("/sys/bus/", subsystem
, "/devices/", name
);
304 if (access(syspath
, F_OK
) >= 0)
305 return sd_device_new_from_syspath(ret
, syspath
);
307 syspath
= strjoina("/sys/class/", subsystem
, "/", name
);
308 if (access(syspath
, F_OK
) >= 0)
309 return sd_device_new_from_syspath(ret
, syspath
);
311 syspath
= strjoina("/sys/firmware/", subsystem
, "/", sysname
);
312 if (access(syspath
, F_OK
) >= 0)
313 return sd_device_new_from_syspath(ret
, syspath
);
318 int device_set_devtype(sd_device
*device
, const char *_devtype
) {
319 _cleanup_free_
char *devtype
= NULL
;
325 devtype
= strdup(_devtype
);
329 r
= device_add_property_internal(device
, "DEVTYPE", devtype
);
333 free_and_replace(device
->devtype
, devtype
);
338 int device_set_ifindex(sd_device
*device
, const char *_ifindex
) {
344 r
= parse_ifindex(_ifindex
, &ifindex
);
348 r
= device_add_property_internal(device
, "IFINDEX", _ifindex
);
352 device
->ifindex
= ifindex
;
357 int device_set_devname(sd_device
*device
, const char *_devname
) {
358 _cleanup_free_
char *devname
= NULL
;
364 if (_devname
[0] != '/') {
365 r
= asprintf(&devname
, "/dev/%s", _devname
);
369 devname
= strdup(_devname
);
374 r
= device_add_property_internal(device
, "DEVNAME", devname
);
378 free_and_replace(device
->devname
, devname
);
383 int device_set_devmode(sd_device
*device
, const char *_devmode
) {
390 r
= safe_atou(_devmode
, &devmode
);
397 r
= device_add_property_internal(device
, "DEVMODE", _devmode
);
401 device
->devmode
= devmode
;
406 int device_set_devnum(sd_device
*device
, const char *major
, const char *minor
) {
407 unsigned maj
= 0, min
= 0;
413 r
= safe_atou(major
, &maj
);
420 r
= safe_atou(minor
, &min
);
425 r
= device_add_property_internal(device
, "MAJOR", major
);
430 r
= device_add_property_internal(device
, "MINOR", minor
);
435 device
->devnum
= makedev(maj
, min
);
440 static int handle_uevent_line(sd_device
*device
, const char *key
, const char *value
, const char **major
, const char **minor
) {
449 if (streq(key
, "DEVTYPE")) {
450 r
= device_set_devtype(device
, value
);
453 } else if (streq(key
, "IFINDEX")) {
454 r
= device_set_ifindex(device
, value
);
457 } else if (streq(key
, "DEVNAME")) {
458 r
= device_set_devname(device
, value
);
461 } else if (streq(key
, "DEVMODE")) {
462 r
= device_set_devmode(device
, value
);
465 } else if (streq(key
, "MAJOR"))
467 else if (streq(key
, "MINOR"))
470 r
= device_add_property_internal(device
, key
, value
);
478 int device_read_uevent_file(sd_device
*device
) {
479 _cleanup_free_
char *uevent
= NULL
;
480 const char *syspath
, *key
= NULL
, *value
= NULL
, *major
= NULL
, *minor
= NULL
;
496 if (device
->uevent_loaded
|| device
->sealed
)
499 r
= sd_device_get_syspath(device
, &syspath
);
503 path
= strjoina(syspath
, "/uevent");
505 r
= read_full_file(path
, &uevent
, &uevent_len
);
507 /* empty uevent files may be write-only */
508 device
->uevent_loaded
= true;
512 /* some devices may not have uevent files, see set_syspath() */
515 return log_device_debug_errno(device
, r
, "sd-device: Failed to read uevent file '%s': %m", path
);
517 device
->uevent_loaded
= true;
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_device_debug(device
, "sd-device: Invalid uevent line '%s', ignoring", 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_device_debug_errno(device
, r
, "sd-device: Failed to handle uevent entry '%s=%s', ignoring: %m", key
, value
);
560 assert_not_reached("Invalid state when parsing uevent file");
564 r
= device_set_devnum(device
, major
, minor
);
566 log_device_debug_errno(device
, r
, "sd-device: Failed to set 'MAJOR=%s' or 'MINOR=%s' from '%s', ignoring: %m", major
, minor
, path
);
572 _public_
int sd_device_get_ifindex(sd_device
*device
, int *ifindex
) {
575 assert_return(device
, -EINVAL
);
577 r
= device_read_uevent_file(device
);
581 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
);
604 r
= parse_dev(id
+ 1, &devt
);
608 return sd_device_new_from_devnum(ret
, id
[0], devt
);
612 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
613 _cleanup_close_
int sk
= -1;
614 struct ifreq ifr
= {};
617 r
= parse_ifindex(&id
[1], &ifr
.ifr_ifindex
);
621 sk
= socket_ioctl_fd();
625 r
= ioctl(sk
, SIOCGIFNAME
, &ifr
);
629 r
= sd_device_new_from_subsystem_sysname(&device
, "net", ifr
.ifr_name
);
633 r
= sd_device_get_ifindex(device
, &ifindex
);
637 /* this is racey, so we might end up with the wrong device */
638 if (ifr
.ifr_ifindex
!= ifindex
)
641 *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
);
665 _public_
int sd_device_get_syspath(sd_device
*device
, const char **ret
) {
666 assert_return(device
, -EINVAL
);
667 assert_return(ret
, -EINVAL
);
669 assert(path_startswith(device
->syspath
, "/sys/"));
671 *ret
= device
->syspath
;
676 static int device_new_from_child(sd_device
**ret
, sd_device
*child
) {
677 _cleanup_free_
char *path
= NULL
;
678 const char *subdir
, *syspath
;
684 r
= sd_device_get_syspath(child
, &syspath
);
688 path
= strdup(syspath
);
691 subdir
= path
+ STRLEN("/sys");
696 pos
= strrchr(subdir
, '/');
697 if (!pos
|| pos
< subdir
+ 2)
702 r
= sd_device_new_from_syspath(ret
, path
);
712 _public_
int sd_device_get_parent(sd_device
*child
, sd_device
**ret
) {
714 assert_return(ret
, -EINVAL
);
715 assert_return(child
, -EINVAL
);
717 if (!child
->parent_set
) {
718 child
->parent_set
= true;
720 (void) device_new_from_child(&child
->parent
, child
);
726 *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 device
->subsystem_set
= true;
746 return free_and_replace(device
->subsystem
, subsystem
);
749 static int device_set_drivers_subsystem(sd_device
*device
, const char *_subsystem
) {
750 _cleanup_free_
char *subsystem
= NULL
;
757 subsystem
= strdup(_subsystem
);
761 r
= device_set_subsystem(device
, "drivers");
765 return free_and_replace(device
->driver_subsystem
, subsystem
);
768 _public_
int sd_device_get_subsystem(sd_device
*device
, const char **ret
) {
769 const char *syspath
, *drivers
= NULL
;
772 assert_return(ret
, -EINVAL
);
773 assert_return(device
, -EINVAL
);
775 r
= sd_device_get_syspath(device
, &syspath
);
779 if (!device
->subsystem_set
) {
780 _cleanup_free_
char *subsystem
= NULL
;
783 /* read 'subsystem' link */
784 path
= strjoina(syspath
, "/subsystem");
785 r
= readlink_value(path
, &subsystem
);
787 r
= device_set_subsystem(device
, subsystem
);
788 /* use implicit names */
789 else if (path_startswith(device
->devpath
, "/module/"))
790 r
= device_set_subsystem(device
, "module");
791 else if (!(drivers
= strstr(syspath
, "/drivers/")) &&
792 PATH_STARTSWITH_SET(device
->devpath
, "/subsystem/",
795 r
= device_set_subsystem(device
, "subsystem");
796 if (r
< 0 && r
!= -ENOENT
)
797 return log_device_debug_errno(device
, r
, "sd-device: Failed to set subsystem for %s: %m", device
->devpath
);
799 device
->subsystem_set
= true;
800 } else if (!device
->driver_subsystem_set
)
801 drivers
= strstr(syspath
, "/drivers/");
803 if (!device
->driver_subsystem_set
) {
805 _cleanup_free_
char *subpath
= NULL
;
807 subpath
= strndup(syspath
, drivers
- syspath
);
813 subsys
= strrchr(subpath
, '/');
817 r
= device_set_drivers_subsystem(device
, subsys
+ 1);
819 if (r
< 0 && r
!= -ENOENT
)
820 return log_device_debug_errno(device
, r
, "sd-device: Failed to set subsystem for driver %s: %m", device
->devpath
);
823 device
->driver_subsystem_set
= true;
826 if (!device
->subsystem
)
829 *ret
= device
->subsystem
;
833 _public_
int sd_device_get_devtype(sd_device
*device
, const char **devtype
) {
839 r
= device_read_uevent_file(device
);
843 if (!device
->devtype
)
846 *devtype
= device
->devtype
;
851 _public_
int sd_device_get_parent_with_subsystem_devtype(sd_device
*child
, const char *subsystem
, const char *devtype
, sd_device
**ret
) {
852 sd_device
*parent
= NULL
;
855 assert_return(child
, -EINVAL
);
856 assert_return(subsystem
, -EINVAL
);
858 r
= sd_device_get_parent(child
, &parent
);
860 const char *parent_subsystem
= NULL
;
861 const char *parent_devtype
= NULL
;
863 (void) sd_device_get_subsystem(parent
, &parent_subsystem
);
864 if (streq_ptr(parent_subsystem
, subsystem
)) {
868 (void) sd_device_get_devtype(parent
, &parent_devtype
);
869 if (streq_ptr(parent_devtype
, devtype
))
872 r
= sd_device_get_parent(parent
, &parent
);
882 _public_
int sd_device_get_devnum(sd_device
*device
, dev_t
*devnum
) {
885 assert_return(device
, -EINVAL
);
887 r
= device_read_uevent_file(device
);
891 if (major(device
->devnum
) <= 0)
895 *devnum
= device
->devnum
;
900 int device_set_driver(sd_device
*device
, const char *_driver
) {
901 _cleanup_free_
char *driver
= NULL
;
907 driver
= strdup(_driver
);
911 r
= device_add_property_internal(device
, "DRIVER", driver
);
915 device
->driver_set
= true;
916 return free_and_replace(device
->driver
, driver
);
919 _public_
int sd_device_get_driver(sd_device
*device
, const char **ret
) {
920 assert_return(device
, -EINVAL
);
921 assert_return(ret
, -EINVAL
);
923 if (!device
->driver_set
) {
924 _cleanup_free_
char *driver
= NULL
;
929 r
= sd_device_get_syspath(device
, &syspath
);
933 path
= strjoina(syspath
, "/driver");
934 r
= readlink_value(path
, &driver
);
936 r
= device_set_driver(device
, driver
);
938 return log_device_debug_errno(device
, r
, "sd-device: Failed to set driver for %s: %m", device
->devpath
);
939 } else if (r
== -ENOENT
)
940 device
->driver_set
= true;
942 return log_device_debug_errno(device
, r
, "sd-device: Failed to set driver for %s: %m", device
->devpath
);
948 *ret
= device
->driver
;
952 _public_
int sd_device_get_devpath(sd_device
*device
, const char **devpath
) {
953 assert_return(device
, -EINVAL
);
954 assert_return(devpath
, -EINVAL
);
956 assert(device
->devpath
);
957 assert(device
->devpath
[0] == '/');
959 *devpath
= device
->devpath
;
963 _public_
int sd_device_get_devname(sd_device
*device
, const char **devname
) {
966 assert_return(device
, -EINVAL
);
967 assert_return(devname
, -EINVAL
);
969 r
= device_read_uevent_file(device
);
973 if (!device
->devname
)
976 assert(path_startswith(device
->devname
, "/dev/"));
978 *devname
= device
->devname
;
982 static int device_set_sysname(sd_device
*device
) {
983 _cleanup_free_
char *sysname
= NULL
;
984 const char *sysnum
= NULL
;
988 if (!device
->devpath
)
991 pos
= strrchr(device
->devpath
, '/');
996 /* devpath is not a root directory */
997 if (*pos
== '\0' || pos
<= device
->devpath
)
1000 sysname
= strdup(pos
);
1004 /* some devices have '!' in their name, change that to '/' */
1005 while (sysname
[len
] != '\0') {
1006 if (sysname
[len
] == '!')
1012 /* trailing number */
1013 while (len
> 0 && isdigit(sysname
[--len
]))
1014 sysnum
= &sysname
[len
];
1019 device
->sysname_set
= true;
1020 device
->sysnum
= sysnum
;
1021 return free_and_replace(device
->sysname
, sysname
);
1024 _public_
int sd_device_get_sysname(sd_device
*device
, const char **ret
) {
1027 assert_return(device
, -EINVAL
);
1028 assert_return(ret
, -EINVAL
);
1030 if (!device
->sysname_set
) {
1031 r
= device_set_sysname(device
);
1036 assert_return(device
->sysname
, -ENOENT
);
1038 *ret
= device
->sysname
;
1042 _public_
int sd_device_get_sysnum(sd_device
*device
, const char **ret
) {
1045 assert_return(device
, -EINVAL
);
1046 assert_return(ret
, -EINVAL
);
1048 if (!device
->sysname_set
) {
1049 r
= device_set_sysname(device
);
1054 if (!device
->sysnum
)
1057 *ret
= device
->sysnum
;
1061 static bool is_valid_tag(const char *tag
) {
1064 return !strchr(tag
, ':') && !strchr(tag
, ' ');
1067 int device_add_tag(sd_device
*device
, const char *tag
) {
1073 if (!is_valid_tag(tag
))
1076 r
= set_ensure_allocated(&device
->tags
, &string_hash_ops
);
1080 r
= set_put_strdup(device
->tags
, tag
);
1084 device
->tags_generation
++;
1085 device
->property_tags_outdated
= true;
1090 int device_add_devlink(sd_device
*device
, const char *devlink
) {
1096 r
= set_ensure_allocated(&device
->devlinks
, &string_hash_ops
);
1100 r
= set_put_strdup(device
->devlinks
, devlink
);
1104 device
->devlinks_generation
++;
1105 device
->property_devlinks_outdated
= true;
1110 static int device_add_property_internal_from_string(sd_device
*device
, const char *str
) {
1111 _cleanup_free_
char *key
= NULL
;
1122 value
= strchr(key
, '=');
1128 if (isempty(++value
))
1131 /* Add the property to both sd_device::properties and sd_device::properties_db,
1132 * as this is called by only handle_db_line(). */
1133 r
= device_add_property_aux(device
, key
, value
, false);
1137 return device_add_property_aux(device
, key
, value
, true);
1140 int device_set_usec_initialized(sd_device
*device
, usec_t when
) {
1141 char s
[DECIMAL_STR_MAX(usec_t
)];
1146 xsprintf(s
, USEC_FMT
, when
);
1148 r
= device_add_property_internal(device
, "USEC_INITIALIZED", s
);
1152 device
->usec_initialized
= when
;
1156 static int handle_db_line(sd_device
*device
, char key
, const char *value
) {
1165 r
= device_add_tag(device
, value
);
1171 path
= strjoina("/dev/", value
);
1172 r
= device_add_devlink(device
, path
);
1178 r
= device_add_property_internal_from_string(device
, value
);
1186 r
= safe_atou64(value
, &t
);
1190 r
= device_set_usec_initialized(device
, t
);
1197 r
= safe_atoi(value
, &device
->devlink_priority
);
1203 r
= safe_atoi(value
, &device
->watch_handle
);
1209 log_device_debug(device
, "sd-device: Unknown key '%c' in device db, ignoring", key
);
1215 int device_get_id_filename(sd_device
*device
, const char **ret
) {
1219 if (!device
->id_filename
) {
1220 _cleanup_free_
char *id
= NULL
;
1221 const char *subsystem
;
1225 r
= sd_device_get_subsystem(device
, &subsystem
);
1229 if (sd_device_get_devnum(device
, &devnum
) >= 0) {
1232 /* use dev_t — b259:131072, c254:0 */
1233 r
= asprintf(&id
, "%c%u:%u",
1234 streq(subsystem
, "block") ? 'b' : 'c',
1235 major(devnum
), minor(devnum
));
1238 } else if (sd_device_get_ifindex(device
, &ifindex
) >= 0) {
1239 /* use netdev ifindex — n3 */
1240 r
= asprintf(&id
, "n%u", (unsigned) ifindex
);
1244 /* use $subsys:$sysname — pci:0000:00:1f.2
1245 * sysname() has '!' translated, get it from devpath
1247 const char *sysname
;
1249 sysname
= basename(device
->devpath
);
1256 if (streq(subsystem
, "drivers")) {
1257 /* the 'drivers' pseudo-subsystem is special, and needs the real subsystem
1258 * encoded as well */
1259 r
= asprintf(&id
, "+drivers:%s:%s", device
->driver_subsystem
, sysname
);
1263 r
= asprintf(&id
, "+%s:%s", subsystem
, sysname
);
1269 device
->id_filename
= TAKE_PTR(id
);
1272 *ret
= device
->id_filename
;
1276 int device_read_db_internal_filename(sd_device
*device
, const char *filename
) {
1277 _cleanup_free_
char *db
= NULL
;
1294 r
= read_full_file(filename
, &db
, &db_len
);
1299 return log_device_debug_errno(device
, r
, "sd-device: Failed to read db '%s': %m", filename
);
1302 /* devices with a database entry are initialized */
1303 device
->is_initialized
= true;
1305 device
->db_loaded
= true;
1307 for (i
= 0; i
< db_len
; i
++) {
1310 if (!strchr(NEWLINE
, db
[i
])) {
1319 log_device_debug(device
, "sd-device: Invalid db entry with key '%c', ignoring", key
);
1321 state
= INVALID_LINE
;
1336 if (strchr(NEWLINE
, db
[i
]))
1341 if (strchr(NEWLINE
, db
[i
])) {
1343 r
= handle_db_line(device
, key
, value
);
1345 log_device_debug_errno(device
, r
, "sd-device: Failed to handle db entry '%c:%s', ignoring: %m", key
, value
);
1352 return log_device_debug_errno(device
, SYNTHETIC_ERRNO(EINVAL
), "sd-device: invalid db syntax.");
1359 int device_read_db_internal(sd_device
*device
, bool force
) {
1360 const char *id
, *path
;
1365 if (device
->db_loaded
|| (!force
&& device
->sealed
))
1368 r
= device_get_id_filename(device
, &id
);
1372 path
= strjoina("/run/udev/data/", id
);
1374 return device_read_db_internal_filename(device
, path
);
1377 _public_
int sd_device_get_is_initialized(sd_device
*device
) {
1380 assert_return(device
, -EINVAL
);
1382 r
= device_read_db(device
);
1386 return device
->is_initialized
;
1389 _public_
int sd_device_get_usec_since_initialized(sd_device
*device
, uint64_t *usec
) {
1393 assert_return(device
, -EINVAL
);
1394 assert_return(usec
, -EINVAL
);
1396 r
= device_read_db(device
);
1400 if (!device
->is_initialized
)
1403 if (!device
->usec_initialized
)
1406 now_ts
= now(clock_boottime_or_monotonic());
1408 if (now_ts
< device
->usec_initialized
)
1411 *usec
= now_ts
- device
->usec_initialized
;
1415 _public_
const char *sd_device_get_tag_first(sd_device
*device
) {
1418 assert_return(device
, NULL
);
1420 (void) device_read_db(device
);
1422 device
->tags_iterator_generation
= device
->tags_generation
;
1423 device
->tags_iterator
= ITERATOR_FIRST
;
1425 (void) set_iterate(device
->tags
, &device
->tags_iterator
, &v
);
1429 _public_
const char *sd_device_get_tag_next(sd_device
*device
) {
1432 assert_return(device
, NULL
);
1434 (void) device_read_db(device
);
1436 if (device
->tags_iterator_generation
!= device
->tags_generation
)
1439 (void) set_iterate(device
->tags
, &device
->tags_iterator
, &v
);
1443 _public_
const char *sd_device_get_devlink_first(sd_device
*device
) {
1446 assert_return(device
, NULL
);
1448 (void) device_read_db(device
);
1450 device
->devlinks_iterator_generation
= device
->devlinks_generation
;
1451 device
->devlinks_iterator
= ITERATOR_FIRST
;
1453 (void) set_iterate(device
->devlinks
, &device
->devlinks_iterator
, &v
);
1457 _public_
const char *sd_device_get_devlink_next(sd_device
*device
) {
1460 assert_return(device
, NULL
);
1462 (void) device_read_db(device
);
1464 if (device
->devlinks_iterator_generation
!= device
->devlinks_generation
)
1467 (void) set_iterate(device
->devlinks
, &device
->devlinks_iterator
, &v
);
1471 int device_properties_prepare(sd_device
*device
) {
1476 r
= device_read_uevent_file(device
);
1480 r
= device_read_db(device
);
1484 if (device
->property_devlinks_outdated
) {
1485 _cleanup_free_
char *devlinks
= NULL
;
1486 size_t devlinks_allocated
= 0, devlinks_len
= 0;
1487 const char *devlink
;
1489 for (devlink
= sd_device_get_devlink_first(device
); devlink
; devlink
= sd_device_get_devlink_next(device
)) {
1492 if (!GREEDY_REALLOC(devlinks
, devlinks_allocated
, devlinks_len
+ strlen(devlink
) + 2))
1494 if (devlinks_len
> 0)
1495 stpcpy(devlinks
+ devlinks_len
++, " ");
1496 e
= stpcpy(devlinks
+ devlinks_len
, devlink
);
1497 devlinks_len
= e
- devlinks
;
1500 r
= device_add_property_internal(device
, "DEVLINKS", devlinks
);
1504 device
->property_devlinks_outdated
= false;
1507 if (device
->property_tags_outdated
) {
1508 _cleanup_free_
char *tags
= NULL
;
1509 size_t tags_allocated
= 0, tags_len
= 0;
1512 if (!GREEDY_REALLOC(tags
, tags_allocated
, 2))
1517 for (tag
= sd_device_get_tag_first(device
); tag
; tag
= sd_device_get_tag_next(device
)) {
1520 if (!GREEDY_REALLOC(tags
, tags_allocated
, tags_len
+ strlen(tag
) + 2))
1522 e
= stpcpy(stpcpy(tags
+ tags_len
, tag
), ":");
1523 tags_len
= e
- tags
;
1526 r
= device_add_property_internal(device
, "TAGS", tags
);
1530 device
->property_tags_outdated
= false;
1536 _public_
const char *sd_device_get_property_first(sd_device
*device
, const char **_value
) {
1541 assert_return(device
, NULL
);
1543 r
= device_properties_prepare(device
);
1547 device
->properties_iterator_generation
= device
->properties_generation
;
1548 device
->properties_iterator
= ITERATOR_FIRST
;
1550 ordered_hashmap_iterate(device
->properties
, &device
->properties_iterator
, (void**)&value
, (const void**)&key
);
1557 _public_
const char *sd_device_get_property_next(sd_device
*device
, const char **_value
) {
1562 assert_return(device
, NULL
);
1564 r
= device_properties_prepare(device
);
1568 if (device
->properties_iterator_generation
!= device
->properties_generation
)
1571 ordered_hashmap_iterate(device
->properties
, &device
->properties_iterator
, (void**)&value
, (const void**)&key
);
1578 static int device_sysattrs_read_all(sd_device
*device
) {
1579 _cleanup_closedir_
DIR *dir
= NULL
;
1580 const char *syspath
;
1581 struct dirent
*dent
;
1586 if (device
->sysattrs_read
)
1589 r
= sd_device_get_syspath(device
, &syspath
);
1593 dir
= opendir(syspath
);
1597 r
= set_ensure_allocated(&device
->sysattrs
, &string_hash_ops
);
1601 FOREACH_DIRENT_ALL(dent
, dir
, return -errno
) {
1603 struct stat statbuf
;
1605 /* only handle symlinks and regular files */
1606 if (!IN_SET(dent
->d_type
, DT_LNK
, DT_REG
))
1609 path
= strjoina(syspath
, "/", dent
->d_name
);
1611 if (lstat(path
, &statbuf
) != 0)
1614 if (!(statbuf
.st_mode
& S_IRUSR
))
1617 r
= set_put_strdup(device
->sysattrs
, dent
->d_name
);
1622 device
->sysattrs_read
= true;
1627 _public_
const char *sd_device_get_sysattr_first(sd_device
*device
) {
1631 assert_return(device
, NULL
);
1633 if (!device
->sysattrs_read
) {
1634 r
= device_sysattrs_read_all(device
);
1641 device
->sysattrs_iterator
= ITERATOR_FIRST
;
1643 (void) set_iterate(device
->sysattrs
, &device
->sysattrs_iterator
, &v
);
1647 _public_
const char *sd_device_get_sysattr_next(sd_device
*device
) {
1650 assert_return(device
, NULL
);
1652 if (!device
->sysattrs_read
)
1655 (void) set_iterate(device
->sysattrs
, &device
->sysattrs_iterator
, &v
);
1659 _public_
int sd_device_has_tag(sd_device
*device
, const char *tag
) {
1660 assert_return(device
, -EINVAL
);
1661 assert_return(tag
, -EINVAL
);
1663 (void) device_read_db(device
);
1665 return !!set_contains(device
->tags
, tag
);
1668 _public_
int sd_device_get_property_value(sd_device
*device
, const char *key
, const char **_value
) {
1672 assert_return(device
, -EINVAL
);
1673 assert_return(key
, -EINVAL
);
1675 r
= device_properties_prepare(device
);
1679 value
= ordered_hashmap_get(device
->properties
, key
);
1688 /* replaces the value if it already exists */
1689 static int device_add_sysattr_value(sd_device
*device
, const char *_key
, char *value
) {
1690 _cleanup_free_
char *key
= NULL
;
1691 _cleanup_free_
char *value_old
= NULL
;
1697 r
= hashmap_ensure_allocated(&device
->sysattr_values
, &string_hash_ops
);
1701 value_old
= hashmap_remove2(device
->sysattr_values
, _key
, (void **)&key
);
1708 r
= hashmap_put(device
->sysattr_values
, key
, value
);
1716 static int device_get_sysattr_value(sd_device
*device
, const char *_key
, const char **_value
) {
1717 const char *key
= NULL
, *value
;
1722 value
= hashmap_get2(device
->sysattr_values
, _key
, (void **) &key
);
1731 /* We cache all sysattr lookups. If an attribute does not exist, it is stored
1732 * with a NULL value in the cache, otherwise the returned string is stored */
1733 _public_
int sd_device_get_sysattr_value(sd_device
*device
, const char *sysattr
, const char **_value
) {
1734 _cleanup_free_
char *value
= NULL
;
1735 const char *syspath
, *cached_value
= NULL
;
1737 struct stat statbuf
;
1740 assert_return(device
, -EINVAL
);
1741 assert_return(sysattr
, -EINVAL
);
1743 /* look for possibly already cached result */
1744 r
= device_get_sysattr_value(device
, sysattr
, &cached_value
);
1750 /* we looked up the sysattr before and it did not exist */
1754 *_value
= cached_value
;
1759 r
= sd_device_get_syspath(device
, &syspath
);
1763 path
= strjoina(syspath
, "/", sysattr
);
1764 r
= lstat(path
, &statbuf
);
1766 /* remember that we could not access the sysattr */
1767 r
= device_add_sysattr_value(device
, sysattr
, NULL
);
1772 } else if (S_ISLNK(statbuf
.st_mode
)) {
1773 /* Some core links return only the last element of the target path,
1774 * these are just values, the paths should not be exposed. */
1775 if (STR_IN_SET(sysattr
, "driver", "subsystem", "module")) {
1776 r
= readlink_value(path
, &value
);
1781 } else if (S_ISDIR(statbuf
.st_mode
)) {
1782 /* skip directories */
1784 } else if (!(statbuf
.st_mode
& S_IRUSR
)) {
1785 /* skip non-readable files */
1790 /* read attribute value */
1791 r
= read_full_file(path
, &value
, &size
);
1795 /* drop trailing newlines */
1796 while (size
> 0 && value
[--size
] == '\n')
1800 r
= device_add_sysattr_value(device
, sysattr
, value
);
1804 *_value
= TAKE_PTR(value
);
1809 static void device_remove_sysattr_value(sd_device
*device
, const char *_key
) {
1810 _cleanup_free_
char *key
= NULL
;
1815 free(hashmap_remove2(device
->sysattr_values
, _key
, (void **) &key
));
1818 /* set the attribute and save it in the cache. If a NULL value is passed the
1819 * attribute is cleared from the cache */
1820 _public_
int sd_device_set_sysattr_value(sd_device
*device
, const char *sysattr
, const char *_value
) {
1821 _cleanup_free_
char *value
= NULL
;
1822 const char *syspath
, *path
;
1826 assert_return(device
, -EINVAL
);
1827 assert_return(sysattr
, -EINVAL
);
1830 device_remove_sysattr_value(device
, sysattr
);
1834 r
= sd_device_get_syspath(device
, &syspath
);
1838 path
= strjoina(syspath
, "/", sysattr
);
1840 len
= strlen(_value
);
1842 /* drop trailing newlines */
1843 while (len
> 0 && _value
[len
- 1] == '\n')
1846 /* value length is limited to 4k */
1850 value
= strndup(_value
, len
);
1854 r
= write_string_file(path
, value
, WRITE_STRING_FILE_DISABLE_BUFFER
| WRITE_STRING_FILE_NOFOLLOW
);
1861 r
= free_and_strdup(&value
, "");
1865 r
= device_add_sysattr_value(device
, sysattr
, value
);
1873 r
= device_add_sysattr_value(device
, sysattr
, value
);