1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
6 #include "device-enumerator-private.h"
7 #include "device-internal.h"
8 #include "device-private.h"
9 #include "device-util.h"
10 #include "errno-util.h"
13 #include "nulstr-util.h"
14 #include "path-util.h"
15 #include "string-util.h"
17 #include "time-util.h"
19 static void test_sd_device_one(sd_device
*d
) {
20 _cleanup_(sd_device_unrefp
) sd_device
*dev
= NULL
;
21 const char *syspath
, *sysname
, *subsystem
= NULL
, *devname
, *val
;
22 bool is_block
= false;
27 assert_se(sd_device_get_syspath(d
, &syspath
) >= 0);
28 assert_se(path_startswith(syspath
, "/sys"));
29 assert_se(sd_device_get_sysname(d
, &sysname
) >= 0);
31 log_info("%s(%s)", __func__
, syspath
);
33 assert_se(sd_device_new_from_syspath(&dev
, syspath
) >= 0);
34 assert_se(sd_device_get_syspath(dev
, &val
) >= 0);
35 assert_se(streq(syspath
, val
));
36 dev
= sd_device_unref(dev
);
38 assert_se(sd_device_new_from_path(&dev
, syspath
) >= 0);
39 assert_se(sd_device_get_syspath(dev
, &val
) >= 0);
40 assert_se(streq(syspath
, val
));
41 dev
= sd_device_unref(dev
);
43 r
= sd_device_get_ifindex(d
, &ifindex
);
45 assert_se(ifindex
> 0);
47 r
= sd_device_new_from_ifindex(&dev
, ifindex
);
49 log_device_warning_errno(d
, r
,
50 "Failed to create sd-device object from ifindex %i. "
51 "Maybe running on a non-host network namespace.", ifindex
);
54 assert_se(sd_device_get_syspath(dev
, &val
) >= 0);
55 assert_se(streq(syspath
, val
));
56 dev
= sd_device_unref(dev
);
59 /* This does not require the interface really exists on the network namespace.
60 * Hence, this should always succeed. */
61 assert_se(sd_device_new_from_ifname(&dev
, sysname
) >= 0);
62 assert_se(sd_device_get_syspath(dev
, &val
) >= 0);
63 assert_se(streq(syspath
, val
));
64 dev
= sd_device_unref(dev
);
66 assert_se(r
== -ENOENT
);
68 r
= sd_device_get_subsystem(d
, &subsystem
);
70 assert_se(r
== -ENOENT
);
71 else if (!streq(subsystem
, "gpio")) { /* Unfortunately, there exist /sys/class/gpio and /sys/bus/gpio.
72 * Hence, sd_device_new_from_subsystem_sysname() and
73 * sd_device_new_from_device_id() may not work as expected. */
74 const char *name
, *id
;
76 if (streq(subsystem
, "drivers"))
77 name
= strjoina(d
->driver_subsystem
, ":", sysname
);
80 assert_se(sd_device_new_from_subsystem_sysname(&dev
, subsystem
, name
) >= 0);
81 assert_se(sd_device_get_syspath(dev
, &val
) >= 0);
82 assert_se(streq(syspath
, val
));
83 dev
= sd_device_unref(dev
);
85 /* The device ID depends on subsystem. */
86 assert_se(device_get_device_id(d
, &id
) >= 0);
87 r
= sd_device_new_from_device_id(&dev
, id
);
88 if (r
== -ENODEV
&& ifindex
> 0)
89 log_device_warning_errno(d
, r
,
90 "Failed to create sd-device object from device ID \"%s\". "
91 "Maybe running on a non-host network namespace.", id
);
94 assert_se(sd_device_get_syspath(dev
, &val
) >= 0);
95 assert_se(streq(syspath
, val
));
96 dev
= sd_device_unref(dev
);
99 /* These require udev database, and reading database requires device ID. */
100 r
= sd_device_get_is_initialized(d
);
102 r
= sd_device_get_usec_since_initialized(d
, &usec
);
103 assert_se((r
>= 0 && usec
> 0) || r
== -ENODATA
);
107 r
= sd_device_get_property_value(d
, "ID_NET_DRIVER", &val
);
108 assert_se(r
>= 0 || r
== -ENOENT
);
111 is_block
= streq_ptr(subsystem
, "block");
113 r
= sd_device_get_devname(d
, &devname
);
115 r
= sd_device_new_from_devname(&dev
, devname
);
117 assert_se(sd_device_get_syspath(dev
, &val
) >= 0);
118 assert_se(streq(syspath
, val
));
119 dev
= sd_device_unref(dev
);
121 assert_se(r
== -ENODEV
|| ERRNO_IS_PRIVILEGE(r
));
123 r
= sd_device_new_from_path(&dev
, devname
);
125 assert_se(sd_device_get_syspath(dev
, &val
) >= 0);
126 assert_se(streq(syspath
, val
));
127 dev
= sd_device_unref(dev
);
129 _cleanup_close_
int fd
= -1;
130 fd
= sd_device_open(d
, O_CLOEXEC
| O_NONBLOCK
| (is_block
? O_RDONLY
: O_NOCTTY
| O_PATH
));
131 assert_se(fd
>= 0 || ERRNO_IS_PRIVILEGE(fd
));
133 assert_se(r
== -ENODEV
|| ERRNO_IS_PRIVILEGE(r
));
135 assert_se(r
== -ENOENT
);
137 r
= sd_device_get_devnum(d
, &devnum
);
139 _cleanup_free_
char *p
= NULL
;
141 assert_se(major(devnum
) > 0);
143 assert_se(sd_device_new_from_devnum(&dev
, is_block
? 'b' : 'c', devnum
) >= 0);
144 assert_se(sd_device_get_syspath(dev
, &val
) >= 0);
145 assert_se(streq(syspath
, val
));
146 dev
= sd_device_unref(dev
);
148 assert_se(asprintf(&p
, "/dev/%s/%u:%u", is_block
? "block" : "char", major(devnum
), minor(devnum
)) >= 0);
149 assert_se(sd_device_new_from_devname(&dev
, p
) >= 0);
150 assert_se(sd_device_get_syspath(dev
, &val
) >= 0);
151 assert_se(streq(syspath
, val
));
152 dev
= sd_device_unref(dev
);
154 assert_se(sd_device_new_from_path(&dev
, p
) >= 0);
155 assert_se(sd_device_get_syspath(dev
, &val
) >= 0);
156 assert_se(streq(syspath
, val
));
157 dev
= sd_device_unref(dev
);
159 assert_se(r
== -ENOENT
);
161 assert_se(sd_device_get_devpath(d
, &val
) >= 0);
163 r
= sd_device_get_devtype(d
, &val
);
164 assert_se(r
>= 0 || r
== -ENOENT
);
166 r
= sd_device_get_driver(d
, &val
);
167 assert_se(r
>= 0 || r
== -ENOENT
);
169 r
= sd_device_get_sysnum(d
, &val
);
171 assert_se(val
> sysname
);
172 assert_se(val
< sysname
+ strlen(sysname
));
173 assert_se(in_charset(val
, DIGITS
));
174 assert_se(!ascii_isdigit(val
[-1]));
176 assert_se(r
== -ENOENT
);
178 r
= sd_device_get_sysattr_value(d
, "name_assign_type", &val
);
179 assert_se(r
>= 0 || ERRNO_IS_PRIVILEGE(r
) || IN_SET(r
, -ENOENT
, -EINVAL
));
182 TEST(sd_device_enumerator_devices
) {
183 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
186 assert_se(sd_device_enumerator_new(&e
) >= 0);
187 assert_se(sd_device_enumerator_allow_uninitialized(e
) >= 0);
188 /* On some CI environments, it seems some loop block devices and corresponding bdi devices sometimes
189 * disappear during running this test. Let's exclude them here for stability. */
190 assert_se(sd_device_enumerator_add_match_subsystem(e
, "bdi", false) >= 0);
191 assert_se(sd_device_enumerator_add_nomatch_sysname(e
, "loop*") >= 0);
192 /* On CentOS CI, systemd-networkd-tests.py may be running when this test is invoked. The networkd
193 * test creates and removes many network interfaces, and may interfere with this test. */
194 assert_se(sd_device_enumerator_add_match_subsystem(e
, "net", false) >= 0);
196 test_sd_device_one(d
);
199 TEST(sd_device_enumerator_subsystems
) {
200 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
203 assert_se(sd_device_enumerator_new(&e
) >= 0);
204 assert_se(sd_device_enumerator_allow_uninitialized(e
) >= 0);
205 FOREACH_SUBSYSTEM(e
, d
)
206 test_sd_device_one(d
);
209 static void test_sd_device_enumerator_filter_subsystem_one(
210 const char *subsystem
,
212 unsigned *ret_n_new_dev
,
213 unsigned *ret_n_removed_dev
) {
215 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
216 unsigned n_new_dev
= 0, n_removed_dev
= 0;
219 assert_se(sd_device_enumerator_new(&e
) >= 0);
220 assert_se(sd_device_enumerator_add_match_subsystem(e
, subsystem
, true) >= 0);
221 assert_se(sd_device_enumerator_add_nomatch_sysname(e
, "loop*") >= 0);
223 FOREACH_DEVICE(e
, d
) {
227 assert_se(sd_device_get_syspath(d
, &syspath
) >= 0);
228 t
= hashmap_remove(h
, syspath
);
231 log_warning("New device found: subsystem:%s syspath:%s", subsystem
, syspath
);
235 assert_se(!sd_device_unref(t
));
238 HASHMAP_FOREACH(d
, h
) {
241 assert_se(sd_device_get_syspath(d
, &syspath
) >= 0);
242 log_warning("Device removed: subsystem:%s syspath:%s", subsystem
, syspath
);
245 assert_se(!sd_device_unref(d
));
250 *ret_n_new_dev
= n_new_dev
;
251 *ret_n_removed_dev
= n_removed_dev
;
254 TEST(sd_device_enumerator_filter_subsystem
) {
255 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
256 _cleanup_(hashmap_freep
) Hashmap
*subsystems
;
257 unsigned n_new_dev
= 0, n_removed_dev
= 0;
262 assert_se(subsystems
= hashmap_new(&string_hash_ops
));
263 assert_se(sd_device_enumerator_new(&e
) >= 0);
264 /* See comments in TEST(sd_device_enumerator_devices). */
265 assert_se(sd_device_enumerator_add_match_subsystem(e
, "bdi", false) >= 0);
266 assert_se(sd_device_enumerator_add_nomatch_sysname(e
, "loop*") >= 0);
267 assert_se(sd_device_enumerator_add_match_subsystem(e
, "net", false) >= 0);
269 FOREACH_DEVICE(e
, d
) {
270 const char *syspath
, *subsystem
;
273 assert_se(sd_device_get_syspath(d
, &syspath
) >= 0);
275 r
= sd_device_get_subsystem(d
, &subsystem
);
276 assert_se(r
>= 0 || r
== -ENOENT
);
280 h
= hashmap_get(subsystems
, subsystem
);
283 assert_se(str
= strdup(subsystem
));
284 assert_se(h
= hashmap_new(&string_hash_ops
));
285 assert_se(hashmap_put(subsystems
, str
, h
) >= 0);
288 assert_se(hashmap_put(h
, syspath
, d
) >= 0);
289 assert_se(sd_device_ref(d
));
291 log_debug("Added subsystem:%s syspath:%s", subsystem
, syspath
);
294 while ((h
= hashmap_steal_first_key_and_value(subsystems
, (void**) &s
))) {
297 test_sd_device_enumerator_filter_subsystem_one(s
, TAKE_PTR(h
), &n
, &m
);
305 log_warning("%u new devices are found in re-scan", n_new_dev
);
306 if (n_removed_dev
> 0)
307 log_warning("%u devices removed in re-scan", n_removed_dev
);
309 /* Assume that not so many devices are plugged or unplugged. */
310 assert_se(n_new_dev
+ n_removed_dev
<= 10);
313 TEST(sd_device_new_from_nulstr
) {
314 const char *devlinks
=
315 "/dev/disk/by-partuuid/1290d63a-42cc-4c71-b87c-xxxxxxxxxxxx\0"
316 "/dev/disk/by-path/pci-0000:00:0f.0-scsi-0:0:0:0-part3\0"
317 "/dev/disk/by-label/Arch\\x20Linux\0"
318 "/dev/disk/by-uuid/a07b87e5-4af5-4a59-bde9-yyyyyyyyyyyy\0"
319 "/dev/disk/by-partlabel/Arch\\x20Linux\0"
322 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
, *from_nulstr
= NULL
;
323 _cleanup_free_
uint8_t *nulstr_copy
= NULL
;
325 const uint8_t *nulstr
;
328 assert_se(sd_device_new_from_syspath(&device
, "/sys/class/net/lo") >= 0);
330 /* Yeah, of course, setting devlink to the loopback interface is nonsense. But this is just a
331 * test for generating and parsing nulstr. For issue #17772. */
332 NULSTR_FOREACH(devlink
, devlinks
) {
333 log_device_info(device
, "setting devlink: %s", devlink
);
334 assert_se(device_add_devlink(device
, devlink
) >= 0);
335 assert_se(set_contains(device
->devlinks
, devlink
));
338 /* These properties are necessary for device_new_from_nulstr(). See device_verify(). */
339 assert_se(device_add_property_internal(device
, "SEQNUM", "1") >= 0);
340 assert_se(device_add_property_internal(device
, "ACTION", "change") >= 0);
342 assert_se(device_get_properties_nulstr(device
, &nulstr
, &len
) >= 0);
343 assert_se(nulstr_copy
= newdup(uint8_t, nulstr
, len
));
344 assert_se(device_new_from_nulstr(&from_nulstr
, nulstr_copy
, len
) >= 0);
346 NULSTR_FOREACH(devlink
, devlinks
) {
347 log_device_info(from_nulstr
, "checking devlink: %s", devlink
);
348 assert_se(set_contains(from_nulstr
->devlinks
, devlink
));
352 DEFINE_TEST_MAIN(LOG_INFO
);