1 /* SPDX-License-Identifier: LGPL-2.1+ */
9 #include "alloc-util.h"
10 #include "device-internal.h"
11 #include "device-private.h"
12 #include "device-util.h"
13 #include "dirent-util.h"
19 #include "parse-util.h"
20 #include "path-util.h"
22 #include "socket-util.h"
23 #include "stat-util.h"
24 #include "string-util.h"
29 int device_new_aux(sd_device
**ret
) {
30 sd_device
*device
= NULL
;
34 device
= new0(sd_device
, 1);
39 device
->watch_handle
= -1;
46 _public_ sd_device
*sd_device_ref(sd_device
*device
) {
48 assert_se(++ device
->n_ref
>= 2);
53 _public_ sd_device
*sd_device_unref(sd_device
*device
) {
54 if (device
&& -- device
->n_ref
== 0) {
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
);
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 log_debug("sd-device: syspath '%s' is not a subdirectory of /sys", _syspath
);
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: could not 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: could not chase symlink /sys: %m");
164 p
= path_startswith(syspath
, real_sys
);
166 log_debug("sd-device: canonicalized path '%s' does not starts with sysfs mount point '%s'", syspath
, real_sys
);
170 new_syspath
= strjoin("/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
);
210 device
->devpath
= devpath
;
215 _public_
int sd_device_new_from_syspath(sd_device
**ret
, const char *syspath
) {
216 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
219 assert_return(ret
, -EINVAL
);
220 assert_return(syspath
, -EINVAL
);
222 r
= device_new_aux(&device
);
226 r
= device_set_syspath(device
, syspath
, true);
230 *ret
= TAKE_PTR(device
);
235 _public_
int sd_device_new_from_devnum(sd_device
**ret
, char type
, dev_t devnum
) {
237 char id
[DECIMAL_STR_MAX(unsigned) * 2 + 1];
239 assert_return(ret
, -EINVAL
);
240 assert_return(IN_SET(type
, 'b', 'c'), -EINVAL
);
242 /* use /sys/dev/{block,char}/<maj>:<min> link */
243 snprintf(id
, sizeof(id
), "%u:%u", major(devnum
), minor(devnum
));
245 syspath
= strjoina("/sys/dev/", (type
== 'b' ? "block" : "char"), "/", id
);
247 return sd_device_new_from_syspath(ret
, syspath
);
250 _public_
int sd_device_new_from_subsystem_sysname(sd_device
**ret
, const char *subsystem
, const char *sysname
) {
251 char *name
, *syspath
;
254 assert_return(ret
, -EINVAL
);
255 assert_return(subsystem
, -EINVAL
);
256 assert_return(sysname
, -EINVAL
);
258 if (streq(subsystem
, "subsystem")) {
259 syspath
= strjoina("/sys/subsystem/", sysname
);
260 if (access(syspath
, F_OK
) >= 0)
261 return sd_device_new_from_syspath(ret
, syspath
);
263 syspath
= strjoina("/sys/bus/", sysname
);
264 if (access(syspath
, F_OK
) >= 0)
265 return sd_device_new_from_syspath(ret
, syspath
);
267 syspath
= strjoina("/sys/class/", sysname
);
268 if (access(syspath
, F_OK
) >= 0)
269 return sd_device_new_from_syspath(ret
, syspath
);
270 } else if (streq(subsystem
, "module")) {
271 syspath
= strjoina("/sys/module/", sysname
);
272 if (access(syspath
, F_OK
) >= 0)
273 return sd_device_new_from_syspath(ret
, syspath
);
274 } else if (streq(subsystem
, "drivers")) {
275 char subsys
[PATH_MAX
];
278 strscpy(subsys
, sizeof(subsys
), sysname
);
279 driver
= strchr(subsys
, ':');
284 syspath
= strjoina("/sys/subsystem/", subsys
, "/drivers/", driver
);
285 if (access(syspath
, F_OK
) >= 0)
286 return sd_device_new_from_syspath(ret
, syspath
);
288 syspath
= strjoina("/sys/bus/", subsys
, "/drivers/", driver
);
289 if (access(syspath
, F_OK
) >= 0)
290 return sd_device_new_from_syspath(ret
, syspath
);
294 /* translate sysname back to sysfs filename */
295 name
= strdupa(sysname
);
296 while (name
[len
] != '\0') {
297 if (name
[len
] == '/')
303 syspath
= strjoina("/sys/subsystem/", subsystem
, "/devices/", name
);
304 if (access(syspath
, F_OK
) >= 0)
305 return sd_device_new_from_syspath(ret
, syspath
);
307 syspath
= strjoina("/sys/bus/", subsystem
, "/devices/", name
);
308 if (access(syspath
, F_OK
) >= 0)
309 return sd_device_new_from_syspath(ret
, syspath
);
311 syspath
= strjoina("/sys/class/", subsystem
, "/", name
);
312 if (access(syspath
, F_OK
) >= 0)
313 return sd_device_new_from_syspath(ret
, syspath
);
315 syspath
= strjoina("/sys/firmware/", subsystem
, "/", sysname
);
316 if (access(syspath
, F_OK
) >= 0)
317 return sd_device_new_from_syspath(ret
, syspath
);
322 int device_set_devtype(sd_device
*device
, const char *_devtype
) {
323 _cleanup_free_
char *devtype
= NULL
;
329 devtype
= strdup(_devtype
);
333 r
= device_add_property_internal(device
, "DEVTYPE", devtype
);
337 free_and_replace(device
->devtype
, devtype
);
342 int device_set_ifindex(sd_device
*device
, const char *_ifindex
) {
348 r
= parse_ifindex(_ifindex
, &ifindex
);
352 r
= device_add_property_internal(device
, "IFINDEX", _ifindex
);
356 device
->ifindex
= ifindex
;
361 int device_set_devname(sd_device
*device
, const char *_devname
) {
362 _cleanup_free_
char *devname
= NULL
;
368 if (_devname
[0] != '/') {
369 r
= asprintf(&devname
, "/dev/%s", _devname
);
373 devname
= strdup(_devname
);
378 r
= device_add_property_internal(device
, "DEVNAME", devname
);
382 free_and_replace(device
->devname
, devname
);
387 int device_set_devmode(sd_device
*device
, const char *_devmode
) {
394 r
= safe_atou(_devmode
, &devmode
);
401 r
= device_add_property_internal(device
, "DEVMODE", _devmode
);
405 device
->devmode
= devmode
;
410 int device_set_devnum(sd_device
*device
, const char *major
, const char *minor
) {
411 unsigned maj
= 0, min
= 0;
417 r
= safe_atou(major
, &maj
);
424 r
= safe_atou(minor
, &min
);
429 r
= device_add_property_internal(device
, "MAJOR", major
);
434 r
= device_add_property_internal(device
, "MINOR", minor
);
439 device
->devnum
= makedev(maj
, min
);
444 static int handle_uevent_line(sd_device
*device
, const char *key
, const char *value
, const char **major
, const char **minor
) {
453 if (streq(key
, "DEVTYPE")) {
454 r
= device_set_devtype(device
, value
);
457 } else if (streq(key
, "IFINDEX")) {
458 r
= device_set_ifindex(device
, value
);
461 } else if (streq(key
, "DEVNAME")) {
462 r
= device_set_devname(device
, value
);
465 } else if (streq(key
, "DEVMODE")) {
466 r
= device_set_devmode(device
, value
);
469 } else if (streq(key
, "MAJOR"))
471 else if (streq(key
, "MINOR"))
474 r
= device_add_property_internal(device
, key
, value
);
482 int device_read_uevent_file(sd_device
*device
) {
483 _cleanup_free_
char *uevent
= NULL
;
484 const char *syspath
, *key
= NULL
, *value
= NULL
, *major
= NULL
, *minor
= NULL
;
500 if (device
->uevent_loaded
|| device
->sealed
)
503 device
->uevent_loaded
= true;
505 r
= sd_device_get_syspath(device
, &syspath
);
509 path
= strjoina(syspath
, "/uevent");
511 r
= read_full_file(path
, &uevent
, &uevent_len
);
513 /* empty uevent files may be write-only */
515 else if (r
== -ENOENT
)
516 /* some devices may not have uevent files, see set_syspath() */
519 log_debug_errno(r
, "sd-device: failed to read uevent file '%s': %m", path
);
523 for (i
= 0; i
< uevent_len
; i
++)
526 if (!strchr(NEWLINE
, uevent
[i
])) {
534 if (uevent
[i
] == '=') {
538 } else if (strchr(NEWLINE
, uevent
[i
])) {
540 log_debug("sd-device: ignoring invalid uevent line '%s'", key
);
550 _fallthrough_
; /* to handle empty property */
552 if (strchr(NEWLINE
, uevent
[i
])) {
555 r
= handle_uevent_line(device
, key
, value
, &major
, &minor
);
557 log_debug_errno(r
, "sd-device: failed to handle uevent entry '%s=%s': %m", key
, value
);
564 assert_not_reached("invalid state when parsing uevent file");
568 r
= device_set_devnum(device
, major
, minor
);
570 log_debug_errno(r
, "sd-device: could not set 'MAJOR=%s' or 'MINOR=%s' from '%s': %m", major
, minor
, path
);
576 _public_
int sd_device_get_ifindex(sd_device
*device
, int *ifindex
) {
579 assert_return(device
, -EINVAL
);
580 assert_return(ifindex
, -EINVAL
);
582 r
= device_read_uevent_file(device
);
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
);
604 r
= sscanf(id
, "%c%i:%i", &type
, &maj
, &min
);
608 return sd_device_new_from_devnum(ret
, type
, makedev(maj
, min
));
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
);
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
);
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
;
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 free_and_replace(device
->subsystem
, subsystem
);
748 device
->subsystem_set
= true;
753 static int device_set_drivers_subsystem(sd_device
*device
, const char *_subsystem
) {
754 _cleanup_free_
char *subsystem
= NULL
;
761 subsystem
= strdup(_subsystem
);
765 r
= device_set_subsystem(device
, "drivers");
769 free_and_replace(device
->driver_subsystem
, subsystem
);
774 _public_
int sd_device_get_subsystem(sd_device
*device
, const char **ret
) {
775 const char *syspath
, *drivers
= NULL
;
778 assert_return(ret
, -EINVAL
);
779 assert_return(device
, -EINVAL
);
781 r
= sd_device_get_syspath(device
, &syspath
);
785 if (!device
->subsystem_set
) {
786 _cleanup_free_
char *subsystem
= NULL
;
789 /* read 'subsystem' link */
790 path
= strjoina(syspath
, "/subsystem");
791 r
= readlink_value(path
, &subsystem
);
793 r
= device_set_subsystem(device
, subsystem
);
794 /* use implicit names */
795 else if (path_startswith(device
->devpath
, "/module/"))
796 r
= device_set_subsystem(device
, "module");
797 else if (!(drivers
= strstr(syspath
, "/drivers/")) &&
798 (path_startswith(device
->devpath
, "/subsystem/") ||
799 path_startswith(device
->devpath
, "/class/") ||
800 path_startswith(device
->devpath
, "/bus/")))
801 r
= device_set_subsystem(device
, "subsystem");
802 if (r
< 0 && r
!= -ENOENT
)
803 return log_debug_errno(r
, "sd-device: could not set subsystem for %s: %m", device
->devpath
);
805 device
->subsystem_set
= true;
806 } else if (!device
->driver_subsystem_set
)
807 drivers
= strstr(syspath
, "/drivers/");
809 if (!device
->driver_subsystem_set
) {
811 _cleanup_free_
char *subpath
= NULL
;
813 subpath
= strndup(syspath
, drivers
- syspath
);
819 subsys
= strrchr(subpath
, '/');
823 r
= device_set_drivers_subsystem(device
, subsys
+ 1);
825 if (r
< 0 && r
!= -ENOENT
)
826 return log_debug_errno(r
, "sd-device: could not set subsystem for driver %s: %m", device
->devpath
);
829 device
->driver_subsystem_set
= true;
832 if (!device
->subsystem
)
835 *ret
= device
->subsystem
;
840 _public_
int sd_device_get_devtype(sd_device
*device
, const char **devtype
) {
846 r
= device_read_uevent_file(device
);
850 *devtype
= device
->devtype
;
855 _public_
int sd_device_get_parent_with_subsystem_devtype(sd_device
*child
, const char *subsystem
, const char *devtype
, sd_device
**ret
) {
856 sd_device
*parent
= NULL
;
859 assert_return(child
, -EINVAL
);
860 assert_return(subsystem
, -EINVAL
);
862 r
= sd_device_get_parent(child
, &parent
);
864 const char *parent_subsystem
= NULL
;
865 const char *parent_devtype
= NULL
;
867 (void) sd_device_get_subsystem(parent
, &parent_subsystem
);
868 if (streq_ptr(parent_subsystem
, subsystem
)) {
872 (void) sd_device_get_devtype(parent
, &parent_devtype
);
873 if (streq_ptr(parent_devtype
, devtype
))
876 r
= sd_device_get_parent(parent
, &parent
);
887 _public_
int sd_device_get_devnum(sd_device
*device
, dev_t
*devnum
) {
890 assert_return(device
, -EINVAL
);
891 assert_return(devnum
, -EINVAL
);
893 r
= device_read_uevent_file(device
);
897 *devnum
= device
->devnum
;
902 int device_set_driver(sd_device
*device
, const char *_driver
) {
903 _cleanup_free_
char *driver
= NULL
;
909 driver
= strdup(_driver
);
913 r
= device_add_property_internal(device
, "DRIVER", driver
);
917 free_and_replace(device
->driver
, driver
);
919 device
->driver_set
= true;
924 _public_
int sd_device_get_driver(sd_device
*device
, const char **ret
) {
925 assert_return(device
, -EINVAL
);
926 assert_return(ret
, -EINVAL
);
928 if (!device
->driver_set
) {
929 _cleanup_free_
char *driver
= NULL
;
934 r
= sd_device_get_syspath(device
, &syspath
);
938 path
= strjoina(syspath
, "/driver");
939 r
= readlink_value(path
, &driver
);
941 r
= device_set_driver(device
, driver
);
943 return log_debug_errno(r
, "sd-device: could not set driver for %s: %m", device
->devpath
);
944 } else if (r
== -ENOENT
)
945 device
->driver_set
= true;
947 return log_debug_errno(r
, "sd-device: could not set driver for %s: %m", device
->devpath
);
953 *ret
= device
->driver
;
958 _public_
int sd_device_get_devpath(sd_device
*device
, const char **devpath
) {
959 assert_return(device
, -EINVAL
);
960 assert_return(devpath
, -EINVAL
);
962 assert(device
->devpath
);
963 assert(device
->devpath
[0] == '/');
965 *devpath
= device
->devpath
;
970 _public_
int sd_device_get_devname(sd_device
*device
, const char **devname
) {
973 assert_return(device
, -EINVAL
);
974 assert_return(devname
, -EINVAL
);
976 r
= device_read_uevent_file(device
);
980 if (!device
->devname
)
983 assert(path_startswith(device
->devname
, "/dev/"));
985 *devname
= device
->devname
;
990 static int device_set_sysname(sd_device
*device
) {
991 _cleanup_free_
char *sysname
= NULL
;
992 const char *sysnum
= NULL
;
996 pos
= strrchr(device
->devpath
, '/');
1001 /* devpath is not a root directory */
1002 if (*pos
== '\0' || pos
<= device
->devpath
)
1005 sysname
= strdup(pos
);
1009 /* some devices have '!' in their name, change that to '/' */
1010 while (sysname
[len
] != '\0') {
1011 if (sysname
[len
] == '!')
1017 /* trailing number */
1018 while (len
> 0 && isdigit(sysname
[--len
]))
1019 sysnum
= &sysname
[len
];
1024 free_and_replace(device
->sysname
, sysname
);
1026 device
->sysnum
= sysnum
;
1028 device
->sysname_set
= true;
1033 _public_
int sd_device_get_sysname(sd_device
*device
, const char **ret
) {
1036 assert_return(device
, -EINVAL
);
1037 assert_return(ret
, -EINVAL
);
1039 if (!device
->sysname_set
) {
1040 r
= device_set_sysname(device
);
1045 assert_return(device
->sysname
, -ENOENT
);
1047 *ret
= device
->sysname
;
1052 _public_
int sd_device_get_sysnum(sd_device
*device
, const char **ret
) {
1055 assert_return(device
, -EINVAL
);
1056 assert_return(ret
, -EINVAL
);
1058 if (!device
->sysname_set
) {
1059 r
= device_set_sysname(device
);
1064 *ret
= device
->sysnum
;
1069 static bool is_valid_tag(const char *tag
) {
1072 return !strchr(tag
, ':') && !strchr(tag
, ' ');
1075 int device_add_tag(sd_device
*device
, const char *tag
) {
1081 if (!is_valid_tag(tag
))
1084 r
= set_ensure_allocated(&device
->tags
, &string_hash_ops
);
1088 r
= set_put_strdup(device
->tags
, tag
);
1092 device
->tags_generation
++;
1093 device
->property_tags_outdated
= true;
1098 int device_add_devlink(sd_device
*device
, const char *devlink
) {
1104 r
= set_ensure_allocated(&device
->devlinks
, &string_hash_ops
);
1108 r
= set_put_strdup(device
->devlinks
, devlink
);
1112 device
->devlinks_generation
++;
1113 device
->property_devlinks_outdated
= true;
1118 static int device_add_property_internal_from_string(sd_device
*device
, const char *str
) {
1119 _cleanup_free_
char *key
= NULL
;
1129 value
= strchr(key
, '=');
1135 if (isempty(++value
))
1138 return device_add_property_internal(device
, key
, value
);
1141 int device_set_usec_initialized(sd_device
*device
, const char *initialized
) {
1142 uint64_t usec_initialized
;
1146 assert(initialized
);
1148 r
= safe_atou64(initialized
, &usec_initialized
);
1152 r
= device_add_property_internal(device
, "USEC_INITIALIZED", initialized
);
1156 device
->usec_initialized
= usec_initialized
;
1161 static int handle_db_line(sd_device
*device
, char key
, const char *value
) {
1170 r
= device_add_tag(device
, value
);
1176 path
= strjoina("/dev/", value
);
1177 r
= device_add_devlink(device
, path
);
1183 r
= device_add_property_internal_from_string(device
, value
);
1189 r
= device_set_usec_initialized(device
, value
);
1195 r
= safe_atoi(value
, &device
->devlink_priority
);
1201 r
= safe_atoi(value
, &device
->watch_handle
);
1207 log_debug("device db: unknown key '%c'", key
);
1213 int device_get_id_filename(sd_device
*device
, const char **ret
) {
1217 if (!device
->id_filename
) {
1218 _cleanup_free_
char *id
= NULL
;
1219 const char *subsystem
;
1223 r
= sd_device_get_subsystem(device
, &subsystem
);
1227 r
= sd_device_get_devnum(device
, &devnum
);
1231 r
= sd_device_get_ifindex(device
, &ifindex
);
1235 if (major(devnum
) > 0) {
1238 /* use dev_t — b259:131072, c254:0 */
1239 r
= asprintf(&id
, "%c%u:%u",
1240 streq(subsystem
, "block") ? 'b' : 'c',
1241 major(devnum
), minor(devnum
));
1244 } else if (ifindex
> 0) {
1245 /* use netdev ifindex — n3 */
1246 r
= asprintf(&id
, "n%u", ifindex
);
1250 /* use $subsys:$sysname — pci:0000:00:1f.2
1251 * sysname() has '!' translated, get it from devpath
1253 const char *sysname
;
1255 sysname
= basename(device
->devpath
);
1262 if (streq(subsystem
, "drivers")) {
1263 /* the 'drivers' pseudo-subsystem is special, and needs the real subsystem
1264 * encoded as well */
1265 r
= asprintf(&id
, "+drivers:%s:%s", device
->driver_subsystem
, sysname
);
1269 r
= asprintf(&id
, "+%s:%s", subsystem
, sysname
);
1275 device
->id_filename
= TAKE_PTR(id
);
1278 *ret
= device
->id_filename
;
1283 int device_read_db_aux(sd_device
*device
, bool force
) {
1284 _cleanup_free_
char *db
= NULL
;
1286 const char *id
, *value
;
1300 if (device
->db_loaded
|| (!force
&& device
->sealed
))
1303 device
->db_loaded
= true;
1305 r
= device_get_id_filename(device
, &id
);
1309 path
= strjoina("/run/udev/data/", id
);
1311 r
= read_full_file(path
, &db
, &db_len
);
1316 return log_debug_errno(r
, "sd-device: failed to read db '%s': %m", path
);
1319 /* devices with a database entry are initialized */
1320 device
->is_initialized
= true;
1322 for (i
= 0; i
< db_len
; i
++) {
1325 if (!strchr(NEWLINE
, db
[i
])) {
1334 log_debug("sd-device: ignoring invalid db entry with key '%c'", key
);
1336 state
= INVALID_LINE
;
1351 if (strchr(NEWLINE
, db
[i
]))
1356 if (strchr(NEWLINE
, db
[i
])) {
1358 r
= handle_db_line(device
, key
, value
);
1360 log_debug_errno(r
, "sd-device: failed to handle db entry '%c:%s': %m", key
, value
);
1367 assert_not_reached("invalid state when parsing db");
1374 static int device_read_db(sd_device
*device
) {
1375 return device_read_db_aux(device
, false);
1378 _public_
int sd_device_get_is_initialized(sd_device
*device
, int *initialized
) {
1381 assert_return(device
, -EINVAL
);
1382 assert_return(initialized
, -EINVAL
);
1384 r
= device_read_db(device
);
1388 *initialized
= device
->is_initialized
;
1393 _public_
int sd_device_get_usec_since_initialized(sd_device
*device
, uint64_t *usec
) {
1397 assert_return(device
, -EINVAL
);
1398 assert_return(usec
, -EINVAL
);
1400 r
= device_read_db(device
);
1404 if (!device
->is_initialized
)
1407 if (!device
->usec_initialized
)
1410 now_ts
= now(clock_boottime_or_monotonic());
1412 if (now_ts
< device
->usec_initialized
)
1415 *usec
= now_ts
- device
->usec_initialized
;
1420 _public_
const char *sd_device_get_tag_first(sd_device
*device
) {
1423 assert_return(device
, NULL
);
1425 (void) device_read_db(device
);
1427 device
->tags_iterator_generation
= device
->tags_generation
;
1428 device
->tags_iterator
= ITERATOR_FIRST
;
1430 (void) set_iterate(device
->tags
, &device
->tags_iterator
, &v
);
1434 _public_
const char *sd_device_get_tag_next(sd_device
*device
) {
1437 assert_return(device
, NULL
);
1439 (void) device_read_db(device
);
1441 if (device
->tags_iterator_generation
!= device
->tags_generation
)
1444 (void) set_iterate(device
->tags
, &device
->tags_iterator
, &v
);
1448 _public_
const char *sd_device_get_devlink_first(sd_device
*device
) {
1451 assert_return(device
, NULL
);
1453 (void) device_read_db(device
);
1455 device
->devlinks_iterator_generation
= device
->devlinks_generation
;
1456 device
->devlinks_iterator
= ITERATOR_FIRST
;
1458 (void) set_iterate(device
->devlinks
, &device
->devlinks_iterator
, &v
);
1462 _public_
const char *sd_device_get_devlink_next(sd_device
*device
) {
1465 assert_return(device
, NULL
);
1467 (void) device_read_db(device
);
1469 if (device
->devlinks_iterator_generation
!= device
->devlinks_generation
)
1472 (void) set_iterate(device
->devlinks
, &device
->devlinks_iterator
, &v
);
1476 static int device_properties_prepare(sd_device
*device
) {
1481 r
= device_read_uevent_file(device
);
1485 r
= device_read_db(device
);
1489 if (device
->property_devlinks_outdated
) {
1490 _cleanup_free_
char *devlinks
= NULL
;
1491 size_t devlinks_allocated
= 0, devlinks_len
= 0;
1492 const char *devlink
;
1494 for (devlink
= sd_device_get_devlink_first(device
); devlink
; devlink
= sd_device_get_devlink_next(device
)) {
1497 if (!GREEDY_REALLOC(devlinks
, devlinks_allocated
, devlinks_len
+ strlen(devlink
) + 2))
1499 if (devlinks_len
> 0)
1500 stpcpy(devlinks
+ devlinks_len
++, " ");
1501 e
= stpcpy(devlinks
+ devlinks_len
, devlink
);
1502 devlinks_len
= e
- devlinks
;
1505 r
= device_add_property_internal(device
, "DEVLINKS", devlinks
);
1509 device
->property_devlinks_outdated
= false;
1512 if (device
->property_tags_outdated
) {
1513 _cleanup_free_
char *tags
= NULL
;
1514 size_t tags_allocated
= 0, tags_len
= 0;
1517 if (!GREEDY_REALLOC(tags
, tags_allocated
, 2))
1522 for (tag
= sd_device_get_tag_first(device
); tag
; tag
= sd_device_get_tag_next(device
)) {
1525 if (!GREEDY_REALLOC(tags
, tags_allocated
, tags_len
+ strlen(tag
) + 2))
1527 e
= stpcpy(stpcpy(tags
+ tags_len
, tag
), ":");
1528 tags_len
= e
- tags
;
1531 r
= device_add_property_internal(device
, "TAGS", tags
);
1535 device
->property_tags_outdated
= false;
1541 _public_
const char *sd_device_get_property_first(sd_device
*device
, const char **_value
) {
1546 assert_return(device
, NULL
);
1548 r
= device_properties_prepare(device
);
1552 device
->properties_iterator_generation
= device
->properties_generation
;
1553 device
->properties_iterator
= ITERATOR_FIRST
;
1555 ordered_hashmap_iterate(device
->properties
, &device
->properties_iterator
, (void**)&value
, (const void**)&key
);
1563 _public_
const char *sd_device_get_property_next(sd_device
*device
, const char **_value
) {
1568 assert_return(device
, NULL
);
1570 r
= device_properties_prepare(device
);
1574 if (device
->properties_iterator_generation
!= device
->properties_generation
)
1577 ordered_hashmap_iterate(device
->properties
, &device
->properties_iterator
, (void**)&value
, (const void**)&key
);
1585 static int device_sysattrs_read_all(sd_device
*device
) {
1586 _cleanup_closedir_
DIR *dir
= NULL
;
1587 const char *syspath
;
1588 struct dirent
*dent
;
1593 if (device
->sysattrs_read
)
1596 r
= sd_device_get_syspath(device
, &syspath
);
1600 dir
= opendir(syspath
);
1604 r
= set_ensure_allocated(&device
->sysattrs
, &string_hash_ops
);
1608 FOREACH_DIRENT_ALL(dent
, dir
, return -errno
) {
1610 struct stat statbuf
;
1612 /* only handle symlinks and regular files */
1613 if (!IN_SET(dent
->d_type
, DT_LNK
, DT_REG
))
1616 path
= strjoina(syspath
, "/", dent
->d_name
);
1618 if (lstat(path
, &statbuf
) != 0)
1621 if (!(statbuf
.st_mode
& S_IRUSR
))
1624 r
= set_put_strdup(device
->sysattrs
, dent
->d_name
);
1629 device
->sysattrs_read
= true;
1634 _public_
const char *sd_device_get_sysattr_first(sd_device
*device
) {
1638 assert_return(device
, NULL
);
1640 if (!device
->sysattrs_read
) {
1641 r
= device_sysattrs_read_all(device
);
1648 device
->sysattrs_iterator
= ITERATOR_FIRST
;
1650 (void) set_iterate(device
->sysattrs
, &device
->sysattrs_iterator
, &v
);
1654 _public_
const char *sd_device_get_sysattr_next(sd_device
*device
) {
1657 assert_return(device
, NULL
);
1659 if (!device
->sysattrs_read
)
1662 (void) set_iterate(device
->sysattrs
, &device
->sysattrs_iterator
, &v
);
1666 _public_
int sd_device_has_tag(sd_device
*device
, const char *tag
) {
1667 assert_return(device
, -EINVAL
);
1668 assert_return(tag
, -EINVAL
);
1670 (void) device_read_db(device
);
1672 return !!set_contains(device
->tags
, tag
);
1675 _public_
int sd_device_get_property_value(sd_device
*device
, const char *key
, const char **_value
) {
1679 assert_return(device
, -EINVAL
);
1680 assert_return(key
, -EINVAL
);
1681 assert_return(_value
, -EINVAL
);
1683 r
= device_properties_prepare(device
);
1687 value
= ordered_hashmap_get(device
->properties
, key
);
1696 /* replaces the value if it already exists */
1697 static int device_add_sysattr_value(sd_device
*device
, const char *_key
, char *value
) {
1698 _cleanup_free_
char *key
= NULL
;
1699 _cleanup_free_
char *value_old
= NULL
;
1705 r
= hashmap_ensure_allocated(&device
->sysattr_values
, &string_hash_ops
);
1709 value_old
= hashmap_remove2(device
->sysattr_values
, _key
, (void **)&key
);
1716 r
= hashmap_put(device
->sysattr_values
, key
, value
);
1725 static int device_get_sysattr_value(sd_device
*device
, const char *_key
, const char **_value
) {
1726 const char *key
= NULL
, *value
;
1731 value
= hashmap_get2(device
->sysattr_values
, _key
, (void **) &key
);
1741 /* We cache all sysattr lookups. If an attribute does not exist, it is stored
1742 * with a NULL value in the cache, otherwise the returned string is stored */
1743 _public_
int sd_device_get_sysattr_value(sd_device
*device
, const char *sysattr
, const char **_value
) {
1744 _cleanup_free_
char *value
= NULL
;
1745 const char *syspath
, *cached_value
= NULL
;
1747 struct stat statbuf
;
1750 assert_return(device
, -EINVAL
);
1751 assert_return(sysattr
, -EINVAL
);
1753 /* look for possibly already cached result */
1754 r
= device_get_sysattr_value(device
, sysattr
, &cached_value
);
1760 /* we looked up the sysattr before and it did not exist */
1764 *_value
= cached_value
;
1769 r
= sd_device_get_syspath(device
, &syspath
);
1773 path
= strjoina(syspath
, "/", sysattr
);
1774 r
= lstat(path
, &statbuf
);
1776 /* remember that we could not access the sysattr */
1777 r
= device_add_sysattr_value(device
, sysattr
, NULL
);
1782 } else if (S_ISLNK(statbuf
.st_mode
)) {
1783 /* Some core links return only the last element of the target path,
1784 * these are just values, the paths should not be exposed. */
1785 if (STR_IN_SET(sysattr
, "driver", "subsystem", "module")) {
1786 r
= readlink_value(path
, &value
);
1791 } else if (S_ISDIR(statbuf
.st_mode
)) {
1792 /* skip directories */
1794 } else if (!(statbuf
.st_mode
& S_IRUSR
)) {
1795 /* skip non-readable files */
1800 /* read attribute value */
1801 r
= read_full_file(path
, &value
, &size
);
1805 /* drop trailing newlines */
1806 while (size
> 0 && value
[--size
] == '\n')
1810 r
= device_add_sysattr_value(device
, sysattr
, value
);
1814 *_value
= TAKE_PTR(value
);
1819 static void device_remove_sysattr_value(sd_device
*device
, const char *_key
) {
1820 _cleanup_free_
char *key
= NULL
;
1821 _cleanup_free_
char *value
= NULL
;
1826 value
= hashmap_remove2(device
->sysattr_values
, _key
, (void **) &key
);
1831 /* set the attribute and save it in the cache. If a NULL value is passed the
1832 * attribute is cleared from the cache */
1833 _public_
int sd_device_set_sysattr_value(sd_device
*device
, const char *sysattr
, char *_value
) {
1834 _cleanup_close_
int fd
= -1;
1835 _cleanup_free_
char *value
= NULL
;
1836 const char *syspath
;
1842 assert_return(device
, -EINVAL
);
1843 assert_return(sysattr
, -EINVAL
);
1846 device_remove_sysattr_value(device
, sysattr
);
1851 r
= sd_device_get_syspath(device
, &syspath
);
1855 path
= strjoina(syspath
, "/", sysattr
);
1857 fd
= open(path
, O_WRONLY
| O_CLOEXEC
| O_NOFOLLOW
);
1861 if (errno
== EISDIR
)
1868 r
= device_add_sysattr_value(device
, sysattr
, value
);
1876 len
= strlen(_value
);
1878 /* drop trailing newlines */
1879 while (len
> 0 && _value
[len
- 1] == '\n')
1882 /* value length is limited to 4k */
1886 value
= strndup(_value
, len
);
1890 size
= write(fd
, value
, len
);
1894 if ((size_t)size
!= len
)
1897 r
= device_add_sysattr_value(device
, sysattr
, value
);