]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
9380d34c | 2 | |
b9d82090 | 3 | #include <ctype.h> |
9534d6c9 | 4 | #include <fcntl.h> |
284ef480 | 5 | #include <unistd.h> |
9534d6c9 | 6 | |
9380d34c | 7 | #include "device-enumerator-private.h" |
e6f88287 | 8 | #include "device-internal.h" |
9380d34c YW |
9 | #include "device-private.h" |
10 | #include "device-util.h" | |
acfc2a1d | 11 | #include "errno-util.h" |
9534d6c9 | 12 | #include "fd-util.h" |
68db4a0c | 13 | #include "hashmap.h" |
e6f88287 | 14 | #include "nulstr-util.h" |
209294ad | 15 | #include "path-util.h" |
284ef480 | 16 | #include "rm-rf.h" |
f79fdea6 | 17 | #include "stat-util.h" |
9380d34c YW |
18 | #include "string-util.h" |
19 | #include "tests.h" | |
ca78ad1d | 20 | #include "time-util.h" |
284ef480 | 21 | #include "tmpfile-util.h" |
5342810b | 22 | #include "udev-util.h" |
9380d34c | 23 | |
af18705f | 24 | static void test_sd_device_one(sd_device *d) { |
209294ad | 25 | _cleanup_(sd_device_unrefp) sd_device *dev = NULL; |
901fa9a4 | 26 | const char *syspath, *sysname, *subsystem = NULL, *devname, *val; |
209294ad | 27 | bool is_block = false; |
af18705f YW |
28 | dev_t devnum; |
29 | usec_t usec; | |
c7749367 | 30 | int ifindex, r; |
9380d34c | 31 | |
af18705f | 32 | assert_se(sd_device_get_syspath(d, &syspath) >= 0); |
209294ad | 33 | assert_se(path_startswith(syspath, "/sys")); |
c7749367 | 34 | assert_se(sd_device_get_sysname(d, &sysname) >= 0); |
04d1fbd5 YW |
35 | |
36 | log_info("%s(%s)", __func__, syspath); | |
37 | ||
209294ad YW |
38 | assert_se(sd_device_new_from_syspath(&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); | |
ecbe9873 | 42 | |
209294ad YW |
43 | assert_se(sd_device_new_from_path(&dev, syspath) >= 0); |
44 | assert_se(sd_device_get_syspath(dev, &val) >= 0); | |
45 | assert_se(streq(syspath, val)); | |
46 | dev = sd_device_unref(dev); | |
9380d34c | 47 | |
c7749367 YW |
48 | r = sd_device_get_ifindex(d, &ifindex); |
49 | if (r >= 0) { | |
50 | assert_se(ifindex > 0); | |
51 | ||
52 | r = sd_device_new_from_ifindex(&dev, ifindex); | |
53 | if (r == -ENODEV) | |
54 | log_device_warning_errno(d, r, | |
55 | "Failed to create sd-device object from ifindex %i. " | |
56 | "Maybe running on a non-host network namespace.", ifindex); | |
57 | else { | |
58 | assert_se(r >= 0); | |
59 | assert_se(sd_device_get_syspath(dev, &val) >= 0); | |
60 | assert_se(streq(syspath, val)); | |
61 | dev = sd_device_unref(dev); | |
62 | } | |
63 | ||
64 | /* This does not require the interface really exists on the network namespace. | |
65 | * Hence, this should always succeed. */ | |
66 | assert_se(sd_device_new_from_ifname(&dev, sysname) >= 0); | |
67 | assert_se(sd_device_get_syspath(dev, &val) >= 0); | |
68 | assert_se(streq(syspath, val)); | |
69 | dev = sd_device_unref(dev); | |
70 | } else | |
71 | assert_se(r == -ENOENT); | |
72 | ||
209294ad | 73 | r = sd_device_get_subsystem(d, &subsystem); |
fc0cbed2 YW |
74 | if (r < 0) |
75 | assert_se(r == -ENOENT); | |
76 | else if (!streq(subsystem, "gpio")) { /* Unfortunately, there exist /sys/class/gpio and /sys/bus/gpio. | |
77 | * Hence, sd_device_new_from_subsystem_sysname() and | |
78 | * sd_device_new_from_device_id() may not work as expected. */ | |
901fa9a4 | 79 | const char *name, *id; |
209294ad YW |
80 | |
81 | if (streq(subsystem, "drivers")) | |
82 | name = strjoina(d->driver_subsystem, ":", sysname); | |
83 | else | |
84 | name = sysname; | |
85 | assert_se(sd_device_new_from_subsystem_sysname(&dev, subsystem, name) >= 0); | |
86 | assert_se(sd_device_get_syspath(dev, &val) >= 0); | |
87 | assert_se(streq(syspath, val)); | |
88 | dev = sd_device_unref(dev); | |
901fa9a4 YW |
89 | |
90 | /* The device ID depends on subsystem. */ | |
91 | assert_se(device_get_device_id(d, &id) >= 0); | |
c7749367 YW |
92 | r = sd_device_new_from_device_id(&dev, id); |
93 | if (r == -ENODEV && ifindex > 0) | |
94 | log_device_warning_errno(d, r, | |
95 | "Failed to create sd-device object from device ID \"%s\". " | |
96 | "Maybe running on a non-host network namespace.", id); | |
97 | else { | |
98 | assert_se(r >= 0); | |
99 | assert_se(sd_device_get_syspath(dev, &val) >= 0); | |
100 | assert_se(streq(syspath, val)); | |
101 | dev = sd_device_unref(dev); | |
102 | } | |
901fa9a4 YW |
103 | |
104 | /* These require udev database, and reading database requires device ID. */ | |
105 | r = sd_device_get_is_initialized(d); | |
106 | if (r > 0) { | |
107 | r = sd_device_get_usec_since_initialized(d, &usec); | |
108 | assert_se((r >= 0 && usec > 0) || r == -ENODATA); | |
109 | } else | |
110 | assert(r == 0); | |
111 | ||
112 | r = sd_device_get_property_value(d, "ID_NET_DRIVER", &val); | |
113 | assert_se(r >= 0 || r == -ENOENT); | |
fc0cbed2 | 114 | } |
209294ad YW |
115 | |
116 | is_block = streq_ptr(subsystem, "block"); | |
117 | ||
118 | r = sd_device_get_devname(d, &devname); | |
119 | if (r >= 0) { | |
120 | r = sd_device_new_from_devname(&dev, devname); | |
121 | if (r >= 0) { | |
122 | assert_se(sd_device_get_syspath(dev, &val) >= 0); | |
123 | assert_se(streq(syspath, val)); | |
124 | dev = sd_device_unref(dev); | |
125 | } else | |
126 | assert_se(r == -ENODEV || ERRNO_IS_PRIVILEGE(r)); | |
127 | ||
128 | r = sd_device_new_from_path(&dev, devname); | |
129 | if (r >= 0) { | |
130 | assert_se(sd_device_get_syspath(dev, &val) >= 0); | |
131 | assert_se(streq(syspath, val)); | |
132 | dev = sd_device_unref(dev); | |
9534d6c9 | 133 | |
254d1313 | 134 | _cleanup_close_ int fd = -EBADF; |
9534d6c9 YW |
135 | fd = sd_device_open(d, O_CLOEXEC| O_NONBLOCK | (is_block ? O_RDONLY : O_NOCTTY | O_PATH)); |
136 | assert_se(fd >= 0 || ERRNO_IS_PRIVILEGE(fd)); | |
209294ad YW |
137 | } else |
138 | assert_se(r == -ENODEV || ERRNO_IS_PRIVILEGE(r)); | |
139 | } else | |
140 | assert_se(r == -ENOENT); | |
9380d34c | 141 | |
af18705f | 142 | r = sd_device_get_devnum(d, &devnum); |
209294ad YW |
143 | if (r >= 0) { |
144 | _cleanup_free_ char *p = NULL; | |
145 | ||
146 | assert_se(major(devnum) > 0); | |
147 | ||
148 | assert_se(sd_device_new_from_devnum(&dev, is_block ? 'b' : 'c', devnum) >= 0); | |
149 | assert_se(sd_device_get_syspath(dev, &val) >= 0); | |
150 | assert_se(streq(syspath, val)); | |
151 | dev = sd_device_unref(dev); | |
152 | ||
153 | assert_se(asprintf(&p, "/dev/%s/%u:%u", is_block ? "block" : "char", major(devnum), minor(devnum)) >= 0); | |
154 | assert_se(sd_device_new_from_devname(&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); | |
158 | ||
159 | assert_se(sd_device_new_from_path(&dev, p) >= 0); | |
160 | assert_se(sd_device_get_syspath(dev, &val) >= 0); | |
161 | assert_se(streq(syspath, val)); | |
162 | dev = sd_device_unref(dev); | |
163 | } else | |
164 | assert_se(r == -ENOENT); | |
9380d34c | 165 | |
af18705f | 166 | assert_se(sd_device_get_devpath(d, &val) >= 0); |
9380d34c | 167 | |
209294ad | 168 | r = sd_device_get_devtype(d, &val); |
af18705f | 169 | assert_se(r >= 0 || r == -ENOENT); |
9380d34c | 170 | |
209294ad YW |
171 | r = sd_device_get_driver(d, &val); |
172 | assert_se(r >= 0 || r == -ENOENT); | |
9380d34c | 173 | |
af18705f | 174 | r = sd_device_get_sysnum(d, &val); |
b9d82090 YW |
175 | if (r >= 0) { |
176 | assert_se(val > sysname); | |
177 | assert_se(val < sysname + strlen(sysname)); | |
178 | assert_se(in_charset(val, DIGITS)); | |
ff25d338 | 179 | assert_se(!ascii_isdigit(val[-1])); |
b9d82090 YW |
180 | } else |
181 | assert_se(r == -ENOENT); | |
9380d34c | 182 | |
733529d8 YW |
183 | r = sd_device_get_sysattr_value(d, "nsid", NULL); |
184 | if (r >= 0) { | |
48a511cf ZJS |
185 | unsigned x; |
186 | ||
733529d8 YW |
187 | assert_se(device_get_sysattr_unsigned(d, "nsid", NULL) >= 0); |
188 | r = device_get_sysattr_unsigned(d, "nsid", &x); | |
189 | assert_se(r >= 0); | |
190 | assert_se((x > 0) == (r > 0)); | |
191 | } else | |
192 | assert_se(ERRNO_IS_PRIVILEGE(r) || IN_SET(r, -ENOENT, -EINVAL)); | |
af18705f | 193 | } |
9380d34c | 194 | |
68da8adf | 195 | TEST(sd_device_enumerator_devices) { |
af18705f | 196 | _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; |
9380d34c | 197 | |
af18705f YW |
198 | assert_se(sd_device_enumerator_new(&e) >= 0); |
199 | assert_se(sd_device_enumerator_allow_uninitialized(e) >= 0); | |
ff56124b YW |
200 | /* On some CI environments, it seems some loop block devices and corresponding bdi devices sometimes |
201 | * disappear during running this test. Let's exclude them here for stability. */ | |
202 | assert_se(sd_device_enumerator_add_match_subsystem(e, "bdi", false) >= 0); | |
203 | assert_se(sd_device_enumerator_add_nomatch_sysname(e, "loop*") >= 0); | |
3284dd91 YW |
204 | /* On CentOS CI, systemd-networkd-tests.py may be running when this test is invoked. The networkd |
205 | * test creates and removes many network interfaces, and may interfere with this test. */ | |
206 | assert_se(sd_device_enumerator_add_match_subsystem(e, "net", false) >= 0); | |
af18705f YW |
207 | FOREACH_DEVICE(e, d) |
208 | test_sd_device_one(d); | |
209 | } | |
210 | ||
68da8adf | 211 | TEST(sd_device_enumerator_subsystems) { |
af18705f | 212 | _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; |
af18705f | 213 | |
af18705f YW |
214 | assert_se(sd_device_enumerator_new(&e) >= 0); |
215 | assert_se(sd_device_enumerator_allow_uninitialized(e) >= 0); | |
216 | FOREACH_SUBSYSTEM(e, d) | |
217 | test_sd_device_one(d); | |
9380d34c YW |
218 | } |
219 | ||
b1d72322 YW |
220 | static void test_sd_device_enumerator_filter_subsystem_one( |
221 | const char *subsystem, | |
222 | Hashmap *h, | |
223 | unsigned *ret_n_new_dev, | |
224 | unsigned *ret_n_removed_dev) { | |
225 | ||
68db4a0c | 226 | _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; |
b1d72322 | 227 | unsigned n_new_dev = 0, n_removed_dev = 0; |
a1af8372 | 228 | sd_device *dev; |
68db4a0c YW |
229 | |
230 | assert_se(sd_device_enumerator_new(&e) >= 0); | |
231 | assert_se(sd_device_enumerator_add_match_subsystem(e, subsystem, true) >= 0); | |
f9e090b1 | 232 | assert_se(sd_device_enumerator_add_nomatch_sysname(e, "loop*") >= 0); |
68db4a0c YW |
233 | |
234 | FOREACH_DEVICE(e, d) { | |
235 | const char *syspath; | |
b1d72322 | 236 | sd_device *t; |
68db4a0c YW |
237 | |
238 | assert_se(sd_device_get_syspath(d, &syspath) >= 0); | |
8ee08dc5 | 239 | t = hashmap_remove(h, syspath); |
68db4a0c | 240 | |
b1d72322 | 241 | if (!t) { |
8ee08dc5 YW |
242 | log_warning("New device found: subsystem:%s syspath:%s", subsystem, syspath); |
243 | n_new_dev++; | |
244 | } | |
b1d72322 YW |
245 | |
246 | assert_se(!sd_device_unref(t)); | |
68db4a0c YW |
247 | } |
248 | ||
a1af8372 | 249 | HASHMAP_FOREACH(dev, h) { |
b1d72322 | 250 | const char *syspath; |
8ee08dc5 | 251 | |
a1af8372 | 252 | assert_se(sd_device_get_syspath(dev, &syspath) >= 0); |
b1d72322 YW |
253 | log_warning("Device removed: subsystem:%s syspath:%s", subsystem, syspath); |
254 | n_removed_dev++; | |
255 | ||
a1af8372 | 256 | assert_se(!sd_device_unref(dev)); |
b1d72322 YW |
257 | } |
258 | ||
259 | hashmap_free(h); | |
260 | ||
261 | *ret_n_new_dev = n_new_dev; | |
262 | *ret_n_removed_dev = n_removed_dev; | |
68db4a0c YW |
263 | } |
264 | ||
5342810b | 265 | static bool test_sd_device_enumerator_filter_subsystem_trial(void) { |
68db4a0c | 266 | _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; |
5d2a48da | 267 | _cleanup_hashmap_free_ Hashmap *subsystems = NULL; |
b1d72322 | 268 | unsigned n_new_dev = 0, n_removed_dev = 0; |
68db4a0c YW |
269 | Hashmap *h; |
270 | char *s; | |
271 | ||
272 | assert_se(subsystems = hashmap_new(&string_hash_ops)); | |
273 | assert_se(sd_device_enumerator_new(&e) >= 0); | |
ff56124b YW |
274 | /* See comments in TEST(sd_device_enumerator_devices). */ |
275 | assert_se(sd_device_enumerator_add_match_subsystem(e, "bdi", false) >= 0); | |
276 | assert_se(sd_device_enumerator_add_nomatch_sysname(e, "loop*") >= 0); | |
3284dd91 | 277 | assert_se(sd_device_enumerator_add_match_subsystem(e, "net", false) >= 0); |
68db4a0c YW |
278 | |
279 | FOREACH_DEVICE(e, d) { | |
280 | const char *syspath, *subsystem; | |
281 | int r; | |
282 | ||
283 | assert_se(sd_device_get_syspath(d, &syspath) >= 0); | |
284 | ||
285 | r = sd_device_get_subsystem(d, &subsystem); | |
286 | assert_se(r >= 0 || r == -ENOENT); | |
287 | if (r < 0) | |
288 | continue; | |
289 | ||
290 | h = hashmap_get(subsystems, subsystem); | |
291 | if (!h) { | |
292 | char *str; | |
293 | assert_se(str = strdup(subsystem)); | |
294 | assert_se(h = hashmap_new(&string_hash_ops)); | |
295 | assert_se(hashmap_put(subsystems, str, h) >= 0); | |
296 | } | |
297 | ||
b5f24a38 | 298 | assert_se(hashmap_put(h, syspath, d) >= 0); |
68db4a0c YW |
299 | assert_se(sd_device_ref(d)); |
300 | ||
301 | log_debug("Added subsystem:%s syspath:%s", subsystem, syspath); | |
302 | } | |
303 | ||
304 | while ((h = hashmap_steal_first_key_and_value(subsystems, (void**) &s))) { | |
b1d72322 YW |
305 | unsigned n, m; |
306 | ||
307 | test_sd_device_enumerator_filter_subsystem_one(s, TAKE_PTR(h), &n, &m); | |
68db4a0c | 308 | free(s); |
b1d72322 YW |
309 | |
310 | n_new_dev += n; | |
311 | n_removed_dev += m; | |
68db4a0c | 312 | } |
8ee08dc5 YW |
313 | |
314 | if (n_new_dev > 0) | |
b1d72322 YW |
315 | log_warning("%u new devices are found in re-scan", n_new_dev); |
316 | if (n_removed_dev > 0) | |
317 | log_warning("%u devices removed in re-scan", n_removed_dev); | |
8ee08dc5 | 318 | |
5342810b YW |
319 | return n_new_dev + n_removed_dev == 0; |
320 | } | |
321 | ||
322 | static bool test_sd_device_enumerator_filter_subsystem_trial_many(void) { | |
323 | for (unsigned i = 0; i < 20; i++) { | |
324 | log_debug("%s(): trial %u", __func__, i); | |
325 | if (test_sd_device_enumerator_filter_subsystem_trial()) | |
326 | return true; | |
327 | } | |
328 | ||
329 | return false; | |
330 | } | |
331 | ||
332 | static int on_inotify(sd_event_source *s, const struct inotify_event *event, void *userdata) { | |
333 | if (test_sd_device_enumerator_filter_subsystem_trial_many()) | |
334 | return sd_event_exit(sd_event_source_get_event(s), 0); | |
335 | ||
336 | return sd_event_exit(sd_event_source_get_event(s), -EBUSY); | |
337 | } | |
338 | ||
339 | TEST(sd_device_enumerator_filter_subsystem) { | |
340 | /* The test test_sd_device_enumerator_filter_subsystem_trial() is quite racy. Let's run the function | |
341 | * several times after the udev queue becomes empty. */ | |
342 | ||
6cae57d9 | 343 | if (!udev_available() || (access("/run/udev", F_OK) < 0 && errno == ENOENT)) { |
5342810b YW |
344 | assert_se(test_sd_device_enumerator_filter_subsystem_trial_many()); |
345 | return; | |
346 | } | |
347 | ||
348 | _cleanup_(sd_event_unrefp) sd_event *event = NULL; | |
349 | assert_se(sd_event_default(&event) >= 0); | |
350 | assert_se(sd_event_add_inotify(event, NULL, "/run/udev" , IN_DELETE, on_inotify, NULL) >= 0); | |
351 | ||
352 | if (udev_queue_is_empty() == 0) { | |
353 | log_debug("udev queue is not empty, waiting for all queued events to be processed."); | |
354 | assert_se(sd_event_loop(event) >= 0); | |
355 | } else | |
356 | assert_se(test_sd_device_enumerator_filter_subsystem_trial_many()); | |
68db4a0c YW |
357 | } |
358 | ||
50f9c507 YW |
359 | TEST(sd_device_enumerator_add_match_sysattr) { |
360 | _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; | |
361 | sd_device *dev; | |
362 | int ifindex; | |
363 | ||
364 | assert_se(sd_device_enumerator_new(&e) >= 0); | |
365 | assert_se(sd_device_enumerator_allow_uninitialized(e) >= 0); | |
366 | assert_se(sd_device_enumerator_add_match_subsystem(e, "net", true) >= 0); | |
367 | assert_se(sd_device_enumerator_add_match_sysattr(e, "ifindex", "1", true) >= 0); | |
368 | assert_se(sd_device_enumerator_add_match_sysattr(e, "ifindex", "hoge", true) >= 0); | |
369 | assert_se(sd_device_enumerator_add_match_sysattr(e, "ifindex", "foo", true) >= 0); | |
370 | assert_se(sd_device_enumerator_add_match_sysattr(e, "ifindex", "bar", false) >= 0); | |
371 | assert_se(sd_device_enumerator_add_match_sysattr(e, "ifindex", "baz", false) >= 0); | |
372 | ||
373 | dev = sd_device_enumerator_get_device_first(e); | |
374 | assert_se(dev); | |
375 | assert_se(sd_device_get_ifindex(dev, &ifindex) >= 0); | |
376 | assert_se(ifindex == 1); | |
377 | ||
378 | assert_se(!sd_device_enumerator_get_device_next(e)); | |
379 | } | |
380 | ||
381 | TEST(sd_device_enumerator_add_match_property) { | |
382 | _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; | |
383 | sd_device *dev; | |
384 | int ifindex; | |
385 | ||
386 | assert_se(sd_device_enumerator_new(&e) >= 0); | |
387 | assert_se(sd_device_enumerator_allow_uninitialized(e) >= 0); | |
388 | assert_se(sd_device_enumerator_add_match_subsystem(e, "net", true) >= 0); | |
389 | assert_se(sd_device_enumerator_add_match_sysattr(e, "ifindex", "1", true) >= 0); | |
390 | assert_se(sd_device_enumerator_add_match_property(e, "IFINDE*", "1*") >= 0); | |
391 | assert_se(sd_device_enumerator_add_match_property(e, "IFINDE*", "hoge") >= 0); | |
392 | assert_se(sd_device_enumerator_add_match_property(e, "IFINDE*", NULL) >= 0); | |
393 | assert_se(sd_device_enumerator_add_match_property(e, "AAAAA", "BBBB") >= 0); | |
394 | assert_se(sd_device_enumerator_add_match_property(e, "FOOOO", NULL) >= 0); | |
395 | ||
396 | dev = sd_device_enumerator_get_device_first(e); | |
397 | assert_se(dev); | |
398 | assert_se(sd_device_get_ifindex(dev, &ifindex) >= 0); | |
399 | assert_se(ifindex == 1); | |
400 | } | |
401 | ||
2d9c58d7 YW |
402 | static void check_parent_match(sd_device_enumerator *e, sd_device *dev) { |
403 | const char *syspath; | |
404 | bool found = false; | |
2d9c58d7 YW |
405 | |
406 | assert_se(sd_device_get_syspath(dev, &syspath) >= 0); | |
407 | ||
408 | FOREACH_DEVICE(e, d) { | |
409 | const char *s; | |
410 | ||
411 | assert_se(sd_device_get_syspath(d, &s) >= 0); | |
412 | if (streq(s, syspath)) { | |
413 | found = true; | |
414 | break; | |
415 | } | |
416 | } | |
417 | ||
418 | if (!found) { | |
419 | log_device_debug(dev, "not enumerated, already removed??"); | |
420 | /* If the original device not found, then the device should be already removed. */ | |
421 | assert_se(access(syspath, F_OK) < 0); | |
422 | assert_se(errno == ENOENT); | |
423 | } | |
424 | } | |
425 | ||
426 | TEST(sd_device_enumerator_add_match_parent) { | |
427 | _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; | |
2d9c58d7 YW |
428 | int r; |
429 | ||
430 | assert_se(sd_device_enumerator_new(&e) >= 0); | |
431 | assert_se(sd_device_enumerator_allow_uninitialized(e) >= 0); | |
432 | /* See comments in TEST(sd_device_enumerator_devices). */ | |
433 | assert_se(sd_device_enumerator_add_match_subsystem(e, "bdi", false) >= 0); | |
434 | assert_se(sd_device_enumerator_add_nomatch_sysname(e, "loop*") >= 0); | |
435 | assert_se(sd_device_enumerator_add_match_subsystem(e, "net", false) >= 0); | |
436 | ||
437 | if (!slow_tests_enabled()) | |
438 | assert_se(sd_device_enumerator_add_match_subsystem(e, "block", true) >= 0); | |
439 | ||
440 | FOREACH_DEVICE(e, dev) { | |
441 | _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *p = NULL; | |
442 | const char *syspath; | |
443 | sd_device *parent; | |
444 | ||
445 | assert_se(sd_device_get_syspath(dev, &syspath) >= 0); | |
446 | ||
447 | r = sd_device_get_parent(dev, &parent); | |
448 | if (r < 0) { | |
449 | assert_se(ERRNO_IS_DEVICE_ABSENT(r)); | |
450 | continue; | |
451 | } | |
452 | ||
453 | log_debug("> %s", syspath); | |
454 | ||
455 | assert_se(sd_device_enumerator_new(&p) >= 0); | |
456 | assert_se(sd_device_enumerator_allow_uninitialized(p) >= 0); | |
457 | assert_se(sd_device_enumerator_add_match_parent(p, parent) >= 0); | |
458 | ||
459 | check_parent_match(p, dev); | |
460 | ||
461 | /* If the device does not have subsystem, then it is not enumerated. */ | |
462 | r = sd_device_get_subsystem(parent, NULL); | |
463 | if (r < 0) { | |
464 | assert_se(r == -ENOENT); | |
465 | continue; | |
466 | } | |
467 | check_parent_match(p, parent); | |
468 | } | |
469 | } | |
470 | ||
fadc8c48 YW |
471 | TEST(sd_device_get_child) { |
472 | _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; | |
fadc8c48 YW |
473 | int r; |
474 | ||
475 | assert_se(sd_device_enumerator_new(&e) >= 0); | |
476 | assert_se(sd_device_enumerator_allow_uninitialized(e) >= 0); | |
477 | /* See comments in TEST(sd_device_enumerator_devices). */ | |
478 | assert_se(sd_device_enumerator_add_match_subsystem(e, "bdi", false) >= 0); | |
479 | assert_se(sd_device_enumerator_add_nomatch_sysname(e, "loop*") >= 0); | |
480 | assert_se(sd_device_enumerator_add_match_subsystem(e, "net", false) >= 0); | |
481 | ||
482 | if (!slow_tests_enabled()) | |
483 | assert_se(sd_device_enumerator_add_match_subsystem(e, "block", true) >= 0); | |
484 | ||
485 | FOREACH_DEVICE(e, dev) { | |
486 | const char *syspath, *parent_syspath, *expected_suffix, *suffix; | |
a1af8372 | 487 | sd_device *parent; |
fadc8c48 YW |
488 | bool found = false; |
489 | ||
490 | assert_se(sd_device_get_syspath(dev, &syspath) >= 0); | |
491 | ||
492 | r = sd_device_get_parent(dev, &parent); | |
493 | if (r < 0) { | |
494 | assert_se(ERRNO_IS_DEVICE_ABSENT(r)); | |
495 | continue; | |
496 | } | |
497 | ||
498 | assert_se(sd_device_get_syspath(parent, &parent_syspath) >= 0); | |
499 | assert_se(expected_suffix = path_startswith(syspath, parent_syspath)); | |
500 | ||
501 | log_debug("> %s", syspath); | |
502 | ||
503 | FOREACH_DEVICE_CHILD_WITH_SUFFIX(parent, child, suffix) { | |
504 | const char *s; | |
505 | ||
506 | assert_se(child); | |
507 | assert_se(suffix); | |
508 | ||
509 | if (!streq(suffix, expected_suffix)) | |
510 | continue; | |
511 | ||
512 | assert_se(sd_device_get_syspath(child, &s) >= 0); | |
513 | assert_se(streq(s, syspath)); | |
514 | found = true; | |
515 | break; | |
516 | } | |
517 | assert_se(found); | |
518 | } | |
519 | } | |
520 | ||
68da8adf | 521 | TEST(sd_device_new_from_nulstr) { |
e6f88287 YW |
522 | const char *devlinks = |
523 | "/dev/disk/by-partuuid/1290d63a-42cc-4c71-b87c-xxxxxxxxxxxx\0" | |
524 | "/dev/disk/by-path/pci-0000:00:0f.0-scsi-0:0:0:0-part3\0" | |
525 | "/dev/disk/by-label/Arch\\x20Linux\0" | |
526 | "/dev/disk/by-uuid/a07b87e5-4af5-4a59-bde9-yyyyyyyyyyyy\0" | |
527 | "/dev/disk/by-partlabel/Arch\\x20Linux\0" | |
528 | "\0"; | |
529 | ||
530 | _cleanup_(sd_device_unrefp) sd_device *device = NULL, *from_nulstr = NULL; | |
cff31876 | 531 | _cleanup_free_ char *nulstr_copy = NULL; |
12e2b70f | 532 | const char *nulstr; |
e6f88287 YW |
533 | size_t len; |
534 | ||
e6f88287 YW |
535 | assert_se(sd_device_new_from_syspath(&device, "/sys/class/net/lo") >= 0); |
536 | ||
537 | /* Yeah, of course, setting devlink to the loopback interface is nonsense. But this is just a | |
538 | * test for generating and parsing nulstr. For issue #17772. */ | |
539 | NULSTR_FOREACH(devlink, devlinks) { | |
540 | log_device_info(device, "setting devlink: %s", devlink); | |
541 | assert_se(device_add_devlink(device, devlink) >= 0); | |
542 | assert_se(set_contains(device->devlinks, devlink)); | |
543 | } | |
544 | ||
4bc4040b YW |
545 | /* For issue #23799 */ |
546 | assert_se(device_add_tag(device, "tag1", false) >= 0); | |
547 | assert_se(device_add_tag(device, "tag2", false) >= 0); | |
548 | assert_se(device_add_tag(device, "current-tag1", true) >= 0); | |
549 | assert_se(device_add_tag(device, "current-tag2", true) >= 0); | |
550 | ||
e6f88287 YW |
551 | /* These properties are necessary for device_new_from_nulstr(). See device_verify(). */ |
552 | assert_se(device_add_property_internal(device, "SEQNUM", "1") >= 0); | |
553 | assert_se(device_add_property_internal(device, "ACTION", "change") >= 0); | |
554 | ||
555 | assert_se(device_get_properties_nulstr(device, &nulstr, &len) >= 0); | |
cff31876 | 556 | assert_se(nulstr_copy = newdup(char, nulstr, len)); |
e6f88287 YW |
557 | assert_se(device_new_from_nulstr(&from_nulstr, nulstr_copy, len) >= 0); |
558 | ||
4bc4040b YW |
559 | assert_se(sd_device_has_tag(from_nulstr, "tag1") == 1); |
560 | assert_se(sd_device_has_tag(from_nulstr, "tag2") == 1); | |
561 | assert_se(sd_device_has_tag(from_nulstr, "current-tag1") == 1); | |
562 | assert_se(sd_device_has_tag(from_nulstr, "current-tag2") == 1); | |
563 | assert_se(sd_device_has_current_tag(from_nulstr, "tag1") == 0); | |
564 | assert_se(sd_device_has_current_tag(from_nulstr, "tag2") == 0); | |
565 | assert_se(sd_device_has_current_tag(from_nulstr, "current-tag1") == 1); | |
566 | assert_se(sd_device_has_current_tag(from_nulstr, "current-tag2") == 1); | |
567 | ||
e6f88287 YW |
568 | NULSTR_FOREACH(devlink, devlinks) { |
569 | log_device_info(from_nulstr, "checking devlink: %s", devlink); | |
570 | assert_se(set_contains(from_nulstr->devlinks, devlink)); | |
571 | } | |
572 | } | |
573 | ||
284ef480 YW |
574 | TEST(sd_device_new_from_path) { |
575 | _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; | |
576 | _cleanup_(rm_rf_physical_and_freep) char *tmpdir = NULL; | |
284ef480 YW |
577 | int r; |
578 | ||
579 | assert_se(mkdtemp_malloc("/tmp/test-sd-device.XXXXXXX", &tmpdir) >= 0); | |
580 | ||
581 | assert_se(sd_device_enumerator_new(&e) >= 0); | |
582 | assert_se(sd_device_enumerator_allow_uninitialized(e) >= 0); | |
583 | assert_se(sd_device_enumerator_add_match_subsystem(e, "block", true) >= 0); | |
584 | assert_se(sd_device_enumerator_add_nomatch_sysname(e, "loop*") >= 0); | |
585 | assert_se(sd_device_enumerator_add_match_property(e, "DEVNAME", "*") >= 0); | |
586 | ||
587 | FOREACH_DEVICE(e, dev) { | |
588 | _cleanup_(sd_device_unrefp) sd_device *d = NULL; | |
589 | const char *syspath, *devpath, *sysname, *s; | |
590 | _cleanup_free_ char *path = NULL; | |
591 | ||
592 | assert_se(sd_device_get_sysname(dev, &sysname) >= 0); | |
593 | ||
594 | log_debug("%s(%s)", __func__, sysname); | |
595 | ||
596 | assert_se(sd_device_get_syspath(dev, &syspath) >= 0); | |
597 | assert_se(sd_device_new_from_path(&d, syspath) >= 0); | |
598 | assert_se(sd_device_get_syspath(d, &s) >= 0); | |
599 | assert_se(streq(s, syspath)); | |
600 | d = sd_device_unref(d); | |
601 | ||
602 | assert_se(sd_device_get_devname(dev, &devpath) >= 0); | |
603 | r = sd_device_new_from_path(&d, devpath); | |
604 | if (r >= 0) { | |
605 | assert_se(sd_device_get_syspath(d, &s) >= 0); | |
606 | assert_se(streq(s, syspath)); | |
607 | d = sd_device_unref(d); | |
608 | } else | |
609 | assert_se(r == -ENODEV || ERRNO_IS_PRIVILEGE(r)); | |
610 | ||
611 | assert_se(path = path_join(tmpdir, sysname)); | |
612 | assert_se(symlink(syspath, path) >= 0); | |
613 | assert_se(sd_device_new_from_path(&d, path) >= 0); | |
614 | assert_se(sd_device_get_syspath(d, &s) >= 0); | |
615 | assert_se(streq(s, syspath)); | |
616 | } | |
617 | } | |
618 | ||
4fe46c34 | 619 | static void test_devname_from_devnum_one(const char *path) { |
f79fdea6 YW |
620 | _cleanup_free_ char *resolved = NULL; |
621 | struct stat st; | |
622 | ||
623 | log_debug("> %s", path); | |
624 | ||
625 | if (stat(path, &st) < 0) { | |
626 | assert_se(errno == ENOENT); | |
627 | log_notice("Path %s not found, skipping test", path); | |
628 | return; | |
629 | } | |
630 | ||
4fe46c34 | 631 | assert_se(devname_from_devnum(st.st_mode, st.st_rdev, &resolved) >= 0); |
f79fdea6 | 632 | assert_se(path_equal(path, resolved)); |
778912a6 YW |
633 | resolved = mfree(resolved); |
634 | assert_se(devname_from_stat_rdev(&st, &resolved) >= 0); | |
635 | assert_se(path_equal(path, resolved)); | |
f79fdea6 YW |
636 | } |
637 | ||
4fe46c34 YW |
638 | TEST(devname_from_devnum) { |
639 | test_devname_from_devnum_one("/dev/null"); | |
640 | test_devname_from_devnum_one("/dev/zero"); | |
641 | test_devname_from_devnum_one("/dev/full"); | |
642 | test_devname_from_devnum_one("/dev/random"); | |
643 | test_devname_from_devnum_one("/dev/urandom"); | |
644 | test_devname_from_devnum_one("/dev/tty"); | |
f79fdea6 YW |
645 | |
646 | if (is_device_node("/run/systemd/inaccessible/blk") > 0) { | |
4fe46c34 YW |
647 | test_devname_from_devnum_one("/run/systemd/inaccessible/chr"); |
648 | test_devname_from_devnum_one("/run/systemd/inaccessible/blk"); | |
f79fdea6 YW |
649 | } |
650 | } | |
651 | ||
68da8adf | 652 | DEFINE_TEST_MAIN(LOG_INFO); |