]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/libsystemd/sd-device/device-enumerator.c
basic/include: replace _Static_assert() with static_assert()
[thirdparty/systemd.git] / src / libsystemd / sd-device / device-enumerator.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
96df036f 2
6553db60 3#include <fnmatch.h>
ca78ad1d 4
96df036f
TG
5#include "sd-device.h"
6
b5efdb8a 7#include "alloc-util.h"
96df036f 8#include "device-enumerator-private.h"
a2ded81f 9#include "device-filter.h"
07630cea 10#include "device-util.h"
a0956174 11#include "dirent-util.h"
3ffd4af2 12#include "fd-util.h"
5cdf13c7
DDM
13#include "log.h"
14#include "path-util.h"
07630cea 15#include "set.h"
760877e9 16#include "sort-util.h"
07630cea
LP
17#include "string-util.h"
18#include "strv.h"
96df036f 19
96df036f
TG
20typedef enum DeviceEnumerationType {
21 DEVICE_ENUMERATION_TYPE_DEVICES,
22 DEVICE_ENUMERATION_TYPE_SUBSYSTEMS,
3c5cc23a 23 DEVICE_ENUMERATION_TYPE_ALL,
96df036f 24 _DEVICE_ENUMERATION_TYPE_MAX,
2d93c20e 25 _DEVICE_ENUMERATION_TYPE_INVALID = -EINVAL,
96df036f
TG
26} DeviceEnumerationType;
27
7fd45eec
FG
28typedef enum MatchFlag {
29 MATCH_NONE = 0,
30 MATCH_BASIC = 1u << 0,
31 MATCH_SYSNAME = 1u << 1,
32 MATCH_SUBSYSTEM = 1u << 2,
33 MATCH_PARENT = 1u << 3,
34 MATCH_TAG = 1u << 4,
35
36 MATCH_ALL = (1u << 5) - 1,
37} MatchFlag;
38
96df036f
TG
39struct sd_device_enumerator {
40 unsigned n_ref;
41
42 DeviceEnumerationType type;
87c3a0f9 43 Hashmap *devices_by_syspath;
0a166589 44 sd_device **devices;
319a4f4b 45 size_t n_devices, current_device_index;
96df036f 46 bool scan_uptodate;
87c3a0f9 47 bool sorted;
96df036f 48
95a6f969 49 char **prioritized_subsystems;
96df036f
TG
50 Set *match_subsystem;
51 Set *nomatch_subsystem;
52 Hashmap *match_sysattr;
53 Hashmap *nomatch_sysattr;
54 Hashmap *match_property;
fe2a6dce 55 Hashmap *match_property_required;
96df036f 56 Set *match_sysname;
94097100 57 Set *nomatch_sysname;
96df036f 58 Set *match_tag;
e022bf66 59 Set *match_parent;
d8b50e5d 60 MatchInitializedType match_initialized;
7fd45eec 61 MatchFlag parent_match_flags;
96df036f
TG
62};
63
64_public_ int sd_device_enumerator_new(sd_device_enumerator **ret) {
4afd3348 65 _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *enumerator = NULL;
96df036f
TG
66
67 assert(ret);
68
6116d2b2 69 enumerator = new(sd_device_enumerator, 1);
96df036f
TG
70 if (!enumerator)
71 return -ENOMEM;
72
6116d2b2
YW
73 *enumerator = (sd_device_enumerator) {
74 .n_ref = 1,
75 .type = _DEVICE_ENUMERATION_TYPE_INVALID,
d8b50e5d 76 .match_initialized = MATCH_INITIALIZED_COMPAT,
7fd45eec 77 .parent_match_flags = MATCH_ALL,
6116d2b2 78 };
96df036f 79
1cc6c93a 80 *ret = TAKE_PTR(enumerator);
96df036f
TG
81
82 return 0;
83}
84
deac0c9c
YW
85static void device_unref_many(sd_device **devices, size_t n) {
86 assert(devices || n == 0);
87
88 for (size_t i = 0; i < n; i++)
89 sd_device_unref(devices[i]);
90}
91
92static void device_enumerator_unref_devices(sd_device_enumerator *enumerator) {
93 assert(enumerator);
94
70669fa2 95 hashmap_clear(enumerator->devices_by_syspath);
deac0c9c 96 device_unref_many(enumerator->devices, enumerator->n_devices);
87c3a0f9 97 enumerator->devices = mfree(enumerator->devices);
deac0c9c
YW
98 enumerator->n_devices = 0;
99}
100
8301aa0b 101static sd_device_enumerator *device_enumerator_free(sd_device_enumerator *enumerator) {
8301aa0b 102 assert(enumerator);
96df036f 103
deac0c9c 104 device_enumerator_unref_devices(enumerator);
96df036f 105
87c3a0f9 106 hashmap_free(enumerator->devices_by_syspath);
95a6f969 107 strv_free(enumerator->prioritized_subsystems);
c73bb513
ZJS
108 set_free(enumerator->match_subsystem);
109 set_free(enumerator->nomatch_subsystem);
eb1c1dc0
ZJS
110 hashmap_free(enumerator->match_sysattr);
111 hashmap_free(enumerator->nomatch_sysattr);
112 hashmap_free(enumerator->match_property);
fe2a6dce 113 hashmap_free(enumerator->match_property_required);
c73bb513 114 set_free(enumerator->match_sysname);
94097100 115 set_free(enumerator->nomatch_sysname);
c73bb513
ZJS
116 set_free(enumerator->match_tag);
117 set_free(enumerator->match_parent);
96df036f 118
8301aa0b 119 return mfree(enumerator);
96df036f
TG
120}
121
8301aa0b
YW
122DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_device_enumerator, sd_device_enumerator, device_enumerator_free);
123
95a6f969
YW
124int device_enumerator_add_prioritized_subsystem(sd_device_enumerator *enumerator, const char *subsystem) {
125 int r;
126
127 assert(enumerator);
128 assert(subsystem);
129
130 if (strv_contains(enumerator->prioritized_subsystems, subsystem))
131 return 0;
132
133 r = strv_extend(&enumerator->prioritized_subsystems, subsystem);
134 if (r < 0)
135 return r;
136
137 enumerator->scan_uptodate = false;
138
139 return 1;
140}
141
96df036f
TG
142_public_ int sd_device_enumerator_add_match_subsystem(sd_device_enumerator *enumerator, const char *subsystem, int match) {
143 Set **set;
144 int r;
145
146 assert_return(enumerator, -EINVAL);
147 assert_return(subsystem, -EINVAL);
148
149 if (match)
150 set = &enumerator->match_subsystem;
151 else
152 set = &enumerator->nomatch_subsystem;
153
be327321 154 r = set_put_strdup(set, subsystem);
2204f018 155 if (r <= 0)
96df036f
TG
156 return r;
157
158 enumerator->scan_uptodate = false;
159
2204f018 160 return 1;
96df036f
TG
161}
162
eb1c1dc0 163_public_ int sd_device_enumerator_add_match_sysattr(sd_device_enumerator *enumerator, const char *sysattr, const char *value, int match) {
96df036f
TG
164 Hashmap **hashmap;
165 int r;
166
167 assert_return(enumerator, -EINVAL);
eb1c1dc0 168 assert_return(sysattr, -EINVAL);
96df036f
TG
169
170 if (match)
171 hashmap = &enumerator->match_sysattr;
172 else
173 hashmap = &enumerator->nomatch_sysattr;
174
b07131fa 175 r = update_match_strv(hashmap, sysattr, value, /* clear_on_null = */ true);
2204f018 176 if (r <= 0)
96df036f
TG
177 return r;
178
96df036f
TG
179 enumerator->scan_uptodate = false;
180
2204f018 181 return 1;
96df036f
TG
182}
183
eb1c1dc0 184_public_ int sd_device_enumerator_add_match_property(sd_device_enumerator *enumerator, const char *property, const char *value) {
96df036f
TG
185 int r;
186
187 assert_return(enumerator, -EINVAL);
eb1c1dc0 188 assert_return(property, -EINVAL);
96df036f 189
b07131fa 190 r = update_match_strv(&enumerator->match_property, property, value, /* clear_on_null = */ false);
2204f018 191 if (r <= 0)
96df036f
TG
192 return r;
193
96df036f
TG
194 enumerator->scan_uptodate = false;
195
2204f018 196 return 1;
96df036f
TG
197}
198
fe2a6dce
DDM
199_public_ int sd_device_enumerator_add_match_property_required(sd_device_enumerator *enumerator, const char *property, const char *value) {
200 int r;
201
202 assert_return(enumerator, -EINVAL);
203 assert_return(property, -EINVAL);
204
205 r = update_match_strv(&enumerator->match_property_required, property, value, /* clear_on_null = */ false);
206 if (r <= 0)
207 return r;
208
209 enumerator->scan_uptodate = false;
210
211 return 1;
212}
213
94097100 214static int device_enumerator_add_match_sysname(sd_device_enumerator *enumerator, const char *sysname, bool match) {
96df036f
TG
215 int r;
216
217 assert_return(enumerator, -EINVAL);
218 assert_return(sysname, -EINVAL);
219
94097100 220 r = set_put_strdup(match ? &enumerator->match_sysname : &enumerator->nomatch_sysname, sysname);
2204f018 221 if (r <= 0)
96df036f
TG
222 return r;
223
224 enumerator->scan_uptodate = false;
225
2204f018 226 return 1;
96df036f
TG
227}
228
94097100
YW
229_public_ int sd_device_enumerator_add_match_sysname(sd_device_enumerator *enumerator, const char *sysname) {
230 return device_enumerator_add_match_sysname(enumerator, sysname, true);
231}
232
233_public_ int sd_device_enumerator_add_nomatch_sysname(sd_device_enumerator *enumerator, const char *sysname) {
234 return device_enumerator_add_match_sysname(enumerator, sysname, false);
235}
236
96df036f
TG
237_public_ int sd_device_enumerator_add_match_tag(sd_device_enumerator *enumerator, const char *tag) {
238 int r;
239
240 assert_return(enumerator, -EINVAL);
241 assert_return(tag, -EINVAL);
242
be327321 243 r = set_put_strdup(&enumerator->match_tag, tag);
2204f018 244 if (r <= 0)
96df036f
TG
245 return r;
246
247 enumerator->scan_uptodate = false;
248
2204f018 249 return 1;
96df036f
TG
250}
251
e022bf66
YW
252int device_enumerator_add_match_parent_incremental(sd_device_enumerator *enumerator, sd_device *parent) {
253 const char *path;
254 int r;
255
476a63e9
ZJS
256 assert(enumerator);
257 assert(parent);
96df036f 258
e022bf66
YW
259 r = sd_device_get_syspath(parent, &path);
260 if (r < 0)
261 return r;
262
be327321 263 r = set_put_strdup(&enumerator->match_parent, path);
2204f018 264 if (r <= 0)
e022bf66 265 return r;
96df036f
TG
266
267 enumerator->scan_uptodate = false;
268
2204f018 269 return 1;
96df036f
TG
270}
271
e022bf66 272_public_ int sd_device_enumerator_add_match_parent(sd_device_enumerator *enumerator, sd_device *parent) {
476a63e9
ZJS
273 assert_return(enumerator, -EINVAL);
274 assert_return(parent, -EINVAL);
275
276 set_clear(enumerator->match_parent);
277
e022bf66
YW
278 return device_enumerator_add_match_parent_incremental(enumerator, parent);
279}
280
dee5e0b6 281_public_ int sd_device_enumerator_allow_uninitialized(sd_device_enumerator *enumerator) {
96df036f
TG
282 assert_return(enumerator, -EINVAL);
283
d8b50e5d 284 enumerator->match_initialized = MATCH_INITIALIZED_ALL;
dee5e0b6
TG
285
286 enumerator->scan_uptodate = false;
287
2204f018 288 return 1;
dee5e0b6 289}
7fd45eec
FG
290_public_ int sd_device_enumerator_add_all_parents(sd_device_enumerator *enumerator) {
291 assert_return(enumerator, -EINVAL);
292
293 enumerator->parent_match_flags = MATCH_NONE;
294
295 enumerator->scan_uptodate = false;
296
297 return 1;
298}
dee5e0b6 299
d8b50e5d 300int device_enumerator_add_match_is_initialized(sd_device_enumerator *enumerator, MatchInitializedType type) {
dee5e0b6 301 assert_return(enumerator, -EINVAL);
d8b50e5d 302 assert_return(type >= 0 && type < _MATCH_INITIALIZED_MAX, -EINVAL);
dee5e0b6 303
d8b50e5d 304 enumerator->match_initialized = type;
96df036f
TG
305
306 enumerator->scan_uptodate = false;
307
2204f018 308 return 1;
96df036f
TG
309}
310
9e871965
YW
311static int sound_device_compare(const char *devpath_a, const char *devpath_b) {
312 const char *sound_a, *sound_b;
313 size_t prefix_len;
314
315 assert(devpath_a);
316 assert(devpath_b);
96df036f 317
9e871965
YW
318 /* For sound cards the control device must be enumerated last to make sure it's the final
319 * device node that gets ACLs applied. Applications rely on this fact and use ACL changes on
320 * the control node as an indicator that the ACL change of the entire sound card completed. The
321 * kernel makes this guarantee when creating those devices, and hence we should too when
322 * enumerating them. */
96df036f 323
d791013f 324 sound_a = strstrafter(devpath_a, "/sound/card");
9e871965
YW
325 if (!sound_a)
326 return 0;
327
9e871965
YW
328 sound_a = strchr(devpath_a, '/');
329 if (!sound_a)
330 return 0;
331
332 prefix_len = sound_a - devpath_a;
333
334 if (!strneq(devpath_a, devpath_b, prefix_len))
335 return 0;
336
337 sound_b = devpath_b + prefix_len;
338
339 return CMP(!!startswith(sound_a, "/controlC"),
340 !!startswith(sound_b, "/controlC"));
341}
342
343static bool devpath_is_late_block(const char *devpath) {
344 assert(devpath);
345
346 return strstr(devpath, "/block/md") || strstr(devpath, "/block/dm-");
347}
348
349static int device_compare(sd_device * const *a, sd_device * const *b) {
350 const char *devpath_a, *devpath_b;
351 int r;
352
353 assert(a);
354 assert(b);
355 assert(*a);
356 assert(*b);
357
358 assert_se(sd_device_get_devpath(*(sd_device**) a, &devpath_a) >= 0);
359 assert_se(sd_device_get_devpath(*(sd_device**) b, &devpath_b) >= 0);
360
361 r = sound_device_compare(devpath_a, devpath_b);
362 if (r != 0)
363 return r;
96df036f
TG
364
365 /* md and dm devices are enumerated after all other devices */
9e871965 366 r = CMP(devpath_is_late_block(devpath_a), devpath_is_late_block(devpath_b));
93bab288
YW
367 if (r != 0)
368 return r;
96df036f 369
63aac21c 370 return path_compare(devpath_a, devpath_b);
96df036f
TG
371}
372
87c3a0f9 373static int enumerator_sort_devices(sd_device_enumerator *enumerator) {
95a6f969 374 size_t n_sorted = 0, n = 0;
87c3a0f9
YW
375 sd_device **devices;
376 sd_device *device;
95a6f969 377 int r;
87c3a0f9
YW
378
379 assert(enumerator);
380
381 if (enumerator->sorted)
382 return 0;
383
384 devices = new(sd_device*, hashmap_size(enumerator->devices_by_syspath));
385 if (!devices)
386 return -ENOMEM;
387
95a6f969
YW
388 STRV_FOREACH(prioritized_subsystem, enumerator->prioritized_subsystems) {
389
390 for (;;) {
391 const char *syspath;
392 size_t m = n;
393
394 HASHMAP_FOREACH_KEY(device, syspath, enumerator->devices_by_syspath) {
395 _cleanup_free_ char *p = NULL;
95a6f969 396
ab1bd9da
YW
397 r = device_in_subsystem(device, *prioritized_subsystem);
398 if (r < 0)
399 return r;
400 if (r == 0)
95a6f969
YW
401 continue;
402
403 devices[n++] = sd_device_ref(device);
404
405 for (;;) {
406 _cleanup_free_ char *q = NULL;
407
408 r = path_extract_directory(p ?: syspath, &q);
409 if (r == -EADDRNOTAVAIL)
410 break;
411 if (r < 0)
412 goto failed;
413
414 device = hashmap_get(enumerator->devices_by_syspath, q);
415 if (device)
416 devices[n++] = sd_device_ref(device);
417
418 free_and_replace(p, q);
419 }
420
421 break;
422 }
423
424 /* We cannot remove multiple entries in the loop HASHMAP_FOREACH_KEY() above. */
425 for (size_t i = m; i < n; i++) {
426 r = sd_device_get_syspath(devices[i], &syspath);
427 if (r < 0)
428 goto failed;
429
430 assert_se(hashmap_remove(enumerator->devices_by_syspath, syspath) == devices[i]);
431 sd_device_unref(devices[i]);
432 }
433
434 if (m == n)
435 break;
436 }
437
438 typesafe_qsort(devices + n_sorted, n - n_sorted, device_compare);
439 n_sorted = n;
440 }
441
87c3a0f9
YW
442 HASHMAP_FOREACH(device, enumerator->devices_by_syspath)
443 devices[n++] = sd_device_ref(device);
444
95a6f969
YW
445 /* Move all devices back to the hashmap. Otherwise, devices added by
446 * udev_enumerate_add_syspath() -> device_enumerator_add_device() may not be listed. */
447 for (size_t i = 0; i < n_sorted; i++) {
448 const char *syspath;
449
450 r = sd_device_get_syspath(devices[i], &syspath);
451 if (r < 0)
452 goto failed;
453
454 r = hashmap_put(enumerator->devices_by_syspath, syspath, devices[i]);
455 if (r < 0)
456 goto failed;
457 assert(r > 0);
458
459 sd_device_ref(devices[i]);
460 }
461
462 typesafe_qsort(devices + n_sorted, n - n_sorted, device_compare);
87c3a0f9
YW
463
464 device_unref_many(enumerator->devices, enumerator->n_devices);
465
466 enumerator->n_devices = n;
467 free_and_replace(enumerator->devices, devices);
468
469 enumerator->sorted = true;
470 return 0;
95a6f969
YW
471
472failed:
473 device_unref_many(devices, n);
474 free(devices);
475 return r;
87c3a0f9
YW
476}
477
70669fa2
YW
478DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
479 device_hash_ops_by_syspath,
480 char, path_hash_func, path_compare,
481 sd_device, sd_device_unref);
482
19c9df44 483int device_enumerator_add_device(sd_device_enumerator *enumerator, sd_device *device) {
87c3a0f9
YW
484 const char *syspath;
485 int r;
486
96df036f
TG
487 assert_return(enumerator, -EINVAL);
488 assert_return(device, -EINVAL);
489
87c3a0f9
YW
490 r = sd_device_get_syspath(device, &syspath);
491 if (r < 0)
492 return r;
96df036f 493
70669fa2 494 r = hashmap_ensure_put(&enumerator->devices_by_syspath, &device_hash_ops_by_syspath, syspath, device);
87c3a0f9
YW
495 if (IN_SET(r, -EEXIST, 0))
496 return 0;
497 if (r < 0)
498 return r;
96df036f 499
87c3a0f9
YW
500 sd_device_ref(device);
501
502 enumerator->sorted = false;
503 return 1;
96df036f
TG
504}
505
fe2a6dce 506static bool match_property(Hashmap *properties, sd_device *device, bool match_all) {
b07131fa
YW
507 const char *property_pattern;
508 char * const *value_patterns;
96df036f 509
96df036f
TG
510 assert(device);
511
b07131fa
YW
512 /* Unlike device_match_sysattr(), this accepts device that has at least one matching property. */
513
fe2a6dce 514 if (hashmap_isempty(properties))
96df036f
TG
515 return true;
516
fe2a6dce
DDM
517 HASHMAP_FOREACH_KEY(value_patterns, property_pattern, properties) {
518 bool match = false;
519
b07131fa
YW
520 FOREACH_DEVICE_PROPERTY(device, property, value) {
521 if (fnmatch(property_pattern, property, 0) != 0)
96df036f
TG
522 continue;
523
fe2a6dce
DDM
524 match = strv_fnmatch(value_patterns, value);
525 if (match) {
526 if (!match_all)
527 return true;
528
529 break;
530 }
96df036f 531 }
96df036f 532
fe2a6dce
DDM
533 if (!match && match_all)
534 return false;
535 }
536
537 return match_all;
96df036f
TG
538}
539
540static bool match_tag(sd_device_enumerator *enumerator, sd_device *device) {
541 const char *tag;
96df036f
TG
542
543 assert(enumerator);
544 assert(device);
545
90e74a66 546 SET_FOREACH(tag, enumerator->match_tag)
96df036f
TG
547 if (!sd_device_has_tag(device, tag))
548 return false;
549
550 return true;
551}
552
96df036f 553static bool match_sysname(sd_device_enumerator *enumerator, const char *sysname) {
96df036f
TG
554 assert(enumerator);
555 assert(sysname);
556
94097100 557 return set_fnmatch(enumerator->match_sysname, enumerator->nomatch_sysname, sysname);
96df036f
TG
558}
559
d8b50e5d
YW
560static int match_initialized(sd_device_enumerator *enumerator, sd_device *device) {
561 int r;
562
563 assert(enumerator);
564 assert(device);
565
566 if (enumerator->match_initialized == MATCH_INITIALIZED_ALL)
567 return true;
568
569 r = sd_device_get_is_initialized(device);
570 if (r == -ENOENT) /* this is necessarily racey, so ignore missing devices */
571 return false;
572 if (r < 0)
573 return r;
574
575 if (enumerator->match_initialized == MATCH_INITIALIZED_COMPAT) {
576 /* only devices that have no devnode/ifindex or have a db entry are accepted. */
577 if (r > 0)
578 return true;
579
580 if (sd_device_get_devnum(device, NULL) >= 0)
b692ad36 581 return false;
d8b50e5d
YW
582
583 if (sd_device_get_ifindex(device, NULL) >= 0)
b692ad36 584 return false;
d8b50e5d 585
b692ad36 586 return true;
d8b50e5d
YW
587 }
588
589 return (enumerator->match_initialized == MATCH_INITIALIZED_NO) == (r == 0);
590}
591
cdffb95c
YW
592static bool match_subsystem(sd_device_enumerator *enumerator, const char *subsystem) {
593 assert(enumerator);
594
595 if (!subsystem)
596 return false;
597
598 return set_fnmatch(enumerator->match_subsystem, enumerator->nomatch_subsystem, subsystem);
599}
600
e7c7d79d
LP
601static int test_matches(
602 sd_device_enumerator *enumerator,
bd4297e7 603 sd_device *device,
cdffb95c 604 MatchFlag flags) {
e7c7d79d
LP
605
606 int r;
607
608 assert(enumerator);
609 assert(device);
610
cdffb95c
YW
611 if (FLAGS_SET(flags, MATCH_SYSNAME)) {
612 const char *sysname;
e7c7d79d 613
cdffb95c
YW
614 r = sd_device_get_sysname(device, &sysname);
615 if (r < 0)
616 return r;
617
618 if (!match_sysname(enumerator, sysname))
619 return false;
620 }
e7c7d79d 621
cdffb95c
YW
622 if (FLAGS_SET(flags, MATCH_SUBSYSTEM)) {
623 const char *subsystem;
624
625 r = sd_device_get_subsystem(device, &subsystem);
626 if (r == -ENOENT)
627 return false;
628 if (r < 0)
629 return r;
630
631 if (!match_subsystem(enumerator, subsystem))
632 return false;
633 }
634
635 if (FLAGS_SET(flags, MATCH_PARENT) &&
bd4297e7 636 !device_match_parent(device, enumerator->match_parent, NULL))
e7c7d79d
LP
637 return false;
638
cdffb95c
YW
639 if (FLAGS_SET(flags, MATCH_TAG) &&
640 !match_tag(enumerator, device))
e7c7d79d
LP
641 return false;
642
7fd45eec
FG
643 if (FLAGS_SET(flags, MATCH_BASIC)) {
644 r = match_initialized(enumerator, device);
645 if (r <= 0)
646 return r;
cdffb95c 647
7fd45eec
FG
648 if (!match_property(enumerator->match_property, device, /* match_all = */ false))
649 return false;
fe2a6dce 650
7fd45eec
FG
651 if (!match_property(enumerator->match_property_required, device, /* match_all = */ true))
652 return false;
e7c7d79d 653
7fd45eec
FG
654 if (!device_match_sysattr(device, enumerator->match_sysattr, enumerator->nomatch_sysattr))
655 return false;
656 }
e7c7d79d
LP
657
658 return true;
659}
660
bd4297e7
YW
661static int enumerator_add_parent_devices(
662 sd_device_enumerator *enumerator,
663 sd_device *device,
cdffb95c 664 MatchFlag flags) {
bd4297e7 665
09ace4c7 666 int r;
bd4297e7
YW
667
668 assert(enumerator);
669 assert(device);
670
671 for (;;) {
09ace4c7
ZJS
672 r = sd_device_get_parent(device, &device);
673 if (r == -ENOENT) /* Reached the top? */
674 return 0;
675 if (r < 0)
676 return r;
bd4297e7 677
09ace4c7
ZJS
678 r = test_matches(enumerator, device, flags);
679 if (r < 0)
680 return r;
681 if (r == 0)
bd4297e7
YW
682 continue;
683
09ace4c7 684 r = device_enumerator_add_device(enumerator, device);
54cbad30 685 if (r <= 0) /* r == 0 means the device already exists, then no need to go further up. */
09ace4c7 686 return r;
bd4297e7 687 }
bd4297e7
YW
688}
689
690int device_enumerator_add_parent_devices(sd_device_enumerator *enumerator, sd_device *device) {
cdffb95c 691 return enumerator_add_parent_devices(enumerator, device, MATCH_ALL & (~MATCH_PARENT));
bd4297e7
YW
692}
693
29a5428f
LP
694static bool relevant_sysfs_subdir(const struct dirent *de) {
695 assert(de);
696
697 if (de->d_name[0] == '.')
698 return false;
699
700 /* Also filter out regular files and such, i.e. stuff that definitely isn't a kobject path. (Note
701 * that we rely on the fact that sysfs fills in d_type here, i.e. doesn't do DT_UNKNOWN) */
702 return IN_SET(de->d_type, DT_DIR, DT_LNK);
703}
704
e7c7d79d
LP
705static int enumerator_scan_dir_and_add_devices(
706 sd_device_enumerator *enumerator,
707 const char *basedir,
708 const char *subdir1,
709 const char *subdir2) {
710
96df036f
TG
711 _cleanup_closedir_ DIR *dir = NULL;
712 char *path;
d8b50e5d 713 int k, r = 0;
96df036f
TG
714
715 assert(enumerator);
716 assert(basedir);
717
718 path = strjoina("/sys/", basedir, "/");
719
720 if (subdir1)
721 path = strjoina(path, subdir1, "/");
722
723 if (subdir2)
724 path = strjoina(path, subdir2, "/");
725
726 dir = opendir(path);
89ad6048 727 if (!dir) {
a68c97a5
LP
728 /* This is necessarily racey, so ignore missing directories */
729 if (errno == ENOENT)
730 return 0;
89ad6048 731
a68c97a5 732 return log_debug_errno(errno, "sd-device-enumerator: Failed to open directory '%s': %m", path);
89ad6048 733 }
96df036f 734
c7f0d9e5 735 FOREACH_DIRENT_ALL(de, dir, return -errno) {
4afd3348 736 _cleanup_(sd_device_unrefp) sd_device *device = NULL;
c7f0d9e5 737 char syspath[strlen(path) + 1 + strlen(de->d_name) + 1];
96df036f 738
29a5428f 739 if (!relevant_sysfs_subdir(de))
96df036f
TG
740 continue;
741
c7f0d9e5 742 if (!match_sysname(enumerator, de->d_name))
96df036f
TG
743 continue;
744
c7f0d9e5 745 (void) sprintf(syspath, "%s%s", path, de->d_name);
96df036f
TG
746
747 k = sd_device_new_from_syspath(&device, syspath);
748 if (k < 0) {
08232a02
TG
749 if (k != -ENODEV)
750 /* this is necessarily racey, so ignore missing devices */
751 r = k;
752
96df036f
TG
753 continue;
754 }
755
cdffb95c 756 k = test_matches(enumerator, device, MATCH_ALL & (~MATCH_SYSNAME)); /* sysname is already tested. */
d8b50e5d
YW
757 if (k <= 0) {
758 if (k < 0)
759 r = k;
96df036f
TG
760 continue;
761 }
762
19c9df44 763 k = device_enumerator_add_device(enumerator, device);
96df036f
TG
764 if (k < 0)
765 r = k;
96df036f 766
00dfbf35
LP
767 /* Also include all potentially matching parent devices in the enumeration. These are things
768 * like root busses — e.g. /sys/devices/pci0000:00/ or /sys/devices/pnp0/, which ar not
769 * linked from /sys/class/ or /sys/bus/, hence pick them up explicitly here. */
7fd45eec 770 k = enumerator_add_parent_devices(enumerator, device, enumerator->parent_match_flags);
bd4297e7
YW
771 if (k < 0)
772 r = k;
00dfbf35
LP
773 }
774
775 return r;
96df036f
TG
776}
777
09c24f76
LP
778static int enumerator_scan_dir(
779 sd_device_enumerator *enumerator,
780 const char *basedir,
781 const char *subdir,
782 const char *subsystem) {
783
96df036f
TG
784 _cleanup_closedir_ DIR *dir = NULL;
785 char *path;
96df036f
TG
786 int r = 0;
787
788 path = strjoina("/sys/", basedir);
789
790 dir = opendir(path);
89ad6048 791 if (!dir) {
a68c97a5
LP
792 if (errno == ENOENT)
793 return 0;
89ad6048 794
a68c97a5 795 return log_debug_errno(errno, "sd-device-enumerator: Failed to open directory '%s': %m", path);
89ad6048 796 }
96df036f 797
c7f0d9e5 798 FOREACH_DIRENT_ALL(de, dir, return -errno) {
96df036f
TG
799 int k;
800
29a5428f 801 if (!relevant_sysfs_subdir(de))
96df036f
TG
802 continue;
803
1da3cb81 804 if (!match_subsystem(enumerator, subsystem ?: de->d_name))
96df036f
TG
805 continue;
806
c7f0d9e5 807 k = enumerator_scan_dir_and_add_devices(enumerator, basedir, de->d_name, subdir);
96df036f
TG
808 if (k < 0)
809 r = k;
810 }
811
812 return r;
813}
814
815static int enumerator_scan_devices_tag(sd_device_enumerator *enumerator, const char *tag) {
816 _cleanup_closedir_ DIR *dir = NULL;
817 char *path;
96df036f
TG
818 int r = 0;
819
820 assert(enumerator);
821 assert(tag);
822
823 path = strjoina("/run/udev/tags/", tag);
824
825 dir = opendir(path);
826 if (!dir) {
a68c97a5
LP
827 if (errno == ENOENT)
828 return 0;
89ad6048 829
a68c97a5 830 return log_debug_errno(errno, "sd-device-enumerator: Failed to open directory '%s': %m", path);
96df036f
TG
831 }
832
833 /* TODO: filter away subsystems? */
834
c7f0d9e5 835 FOREACH_DIRENT_ALL(de, dir, return -errno) {
4afd3348 836 _cleanup_(sd_device_unrefp) sd_device *device = NULL;
96df036f
TG
837 int k;
838
c7f0d9e5 839 if (de->d_name[0] == '.')
96df036f
TG
840 continue;
841
c7f0d9e5 842 k = sd_device_new_from_device_id(&device, de->d_name);
96df036f 843 if (k < 0) {
08232a02
TG
844 if (k != -ENODEV)
845 /* this is necessarily racy, so ignore missing devices */
846 r = k;
847
96df036f
TG
848 continue;
849 }
850
cdffb95c
YW
851 /* Generated from tag, hence not necessary to check tag again. */
852 k = test_matches(enumerator, device, MATCH_ALL & (~MATCH_TAG));
853 if (k < 0)
96df036f 854 r = k;
cdffb95c 855 if (k <= 0)
96df036f
TG
856 continue;
857
19c9df44 858 k = device_enumerator_add_device(enumerator, device);
96df036f
TG
859 if (k < 0) {
860 r = k;
861 continue;
862 }
863 }
864
865 return r;
866}
867
868static int enumerator_scan_devices_tags(sd_device_enumerator *enumerator) {
869 const char *tag;
3172836b 870 int r = 0;
96df036f
TG
871
872 assert(enumerator);
873
90e74a66 874 SET_FOREACH(tag, enumerator->match_tag) {
3172836b
TG
875 int k;
876
877 k = enumerator_scan_devices_tag(enumerator, tag);
878 if (k < 0)
879 r = k;
96df036f
TG
880 }
881
3172836b 882 return r;
96df036f
TG
883}
884
0f519b9c 885static int parent_add_child(sd_device_enumerator *enumerator, const char *path, MatchFlag flags) {
4afd3348 886 _cleanup_(sd_device_unrefp) sd_device *device = NULL;
96df036f
TG
887 int r;
888
889 r = sd_device_new_from_syspath(&device, path);
08232a02
TG
890 if (r == -ENODEV)
891 /* this is necessarily racy, so ignore missing devices */
96df036f
TG
892 return 0;
893 else if (r < 0)
894 return r;
895
0f519b9c 896 r = test_matches(enumerator, device, flags);
cdffb95c 897 if (r <= 0)
96df036f
TG
898 return r;
899
cdffb95c 900 return device_enumerator_add_device(enumerator, device);
96df036f
TG
901}
902
6c876f0c 903static int parent_crawl_children(sd_device_enumerator *enumerator, const char *path, Set **stack) {
96df036f 904 _cleanup_closedir_ DIR *dir = NULL;
96df036f
TG
905 int r = 0;
906
6c876f0c
YW
907 assert(enumerator);
908 assert(path);
909 assert(stack);
910
96df036f 911 dir = opendir(path);
bae47767 912 if (!dir) {
a68c97a5
LP
913 if (errno == ENOENT)
914 return 0;
89ad6048 915
a68c97a5 916 return log_debug_errno(errno, "sd-device-enumerator: Failed to open directory '%s': %m", path);
bae47767 917 }
96df036f 918
c7f0d9e5 919 FOREACH_DIRENT_ALL(de, dir, return -errno) {
96df036f
TG
920 _cleanup_free_ char *child = NULL;
921 int k;
922
c7f0d9e5 923 if (de->d_name[0] == '.')
96df036f
TG
924 continue;
925
c7f0d9e5 926 if (de->d_type != DT_DIR)
96df036f
TG
927 continue;
928
c7f0d9e5 929 child = path_join(path, de->d_name);
53fae771
ZJS
930 if (!child)
931 return -ENOMEM;
96df036f 932
0f519b9c
YW
933 /* Let's check sysname filter earlier. The other tests require the sd-device object created
934 * from the path, thus much costly. */
935 if (match_sysname(enumerator, de->d_name)) {
936 k = parent_add_child(enumerator, child, MATCH_ALL & (~(MATCH_SYSNAME|MATCH_PARENT)));
937 if (k < 0)
938 r = k;
939 }
96df036f 940
6c876f0c
YW
941 k = set_ensure_consume(stack, &path_hash_ops_free, TAKE_PTR(child));
942 if (k < 0)
943 r = k;
96df036f
TG
944 }
945
946 return r;
947}
948
949static int enumerator_scan_devices_children(sd_device_enumerator *enumerator) {
6c876f0c 950 _cleanup_set_free_ Set *stack = NULL;
96df036f
TG
951 const char *path;
952 int r = 0, k;
953
6c876f0c
YW
954 assert(enumerator);
955
90e74a66 956 SET_FOREACH(path, enumerator->match_parent) {
0f519b9c 957 k = parent_add_child(enumerator, path, MATCH_ALL & (~MATCH_PARENT));
e022bf66
YW
958 if (k < 0)
959 r = k;
96df036f 960
6c876f0c
YW
961 k = parent_crawl_children(enumerator, path, &stack);
962 if (k < 0)
963 r = k;
964 }
965
88b5080e
YW
966 for (;;) {
967 _cleanup_free_ char *p = NULL;
968
969 p = set_steal_first(stack);
970 if (!p)
971 return r;
972
6c876f0c 973 k = parent_crawl_children(enumerator, p, &stack);
e022bf66
YW
974 if (k < 0)
975 r = k;
976 }
96df036f
TG
977}
978
979static int enumerator_scan_devices_all(sd_device_enumerator *enumerator) {
37cf83d9 980 int k, r = 0;
96df036f 981
37cf83d9
YW
982 k = enumerator_scan_dir(enumerator, "bus", "devices", NULL);
983 if (k < 0)
984 r = log_debug_errno(k, "sd-device-enumerator: Failed to scan /sys/bus: %m");
96df036f 985
37cf83d9
YW
986 k = enumerator_scan_dir(enumerator, "class", NULL, NULL);
987 if (k < 0)
988 r = log_debug_errno(k, "sd-device-enumerator: Failed to scan /sys/class: %m");
96df036f
TG
989
990 return r;
991}
992
993int device_enumerator_scan_devices(sd_device_enumerator *enumerator) {
3172836b 994 int r = 0, k;
96df036f
TG
995
996 assert(enumerator);
997
998 if (enumerator->scan_uptodate &&
999 enumerator->type == DEVICE_ENUMERATION_TYPE_DEVICES)
1000 return 0;
1001
deac0c9c 1002 device_enumerator_unref_devices(enumerator);
96df036f
TG
1003
1004 if (!set_isempty(enumerator->match_tag)) {
3172836b
TG
1005 k = enumerator_scan_devices_tags(enumerator);
1006 if (k < 0)
1007 r = k;
96df036f 1008 } else if (enumerator->match_parent) {
3172836b
TG
1009 k = enumerator_scan_devices_children(enumerator);
1010 if (k < 0)
1011 r = k;
96df036f 1012 } else {
3172836b
TG
1013 k = enumerator_scan_devices_all(enumerator);
1014 if (k < 0)
1015 r = k;
96df036f
TG
1016 }
1017
1018 enumerator->scan_uptodate = true;
0a166589 1019 enumerator->type = DEVICE_ENUMERATION_TYPE_DEVICES;
96df036f 1020
3172836b 1021 return r;
96df036f
TG
1022}
1023
1024_public_ sd_device *sd_device_enumerator_get_device_first(sd_device_enumerator *enumerator) {
96df036f
TG
1025 assert_return(enumerator, NULL);
1026
87c3a0f9
YW
1027 if (device_enumerator_scan_devices(enumerator) < 0)
1028 return NULL;
1029
1030 if (enumerator_sort_devices(enumerator) < 0)
96df036f
TG
1031 return NULL;
1032
0a166589
YW
1033 enumerator->current_device_index = 0;
1034
1035 if (enumerator->n_devices == 0)
1036 return NULL;
96df036f 1037
0a166589 1038 return enumerator->devices[0];
96df036f
TG
1039}
1040
1041_public_ sd_device *sd_device_enumerator_get_device_next(sd_device_enumerator *enumerator) {
1042 assert_return(enumerator, NULL);
1043
1044 if (!enumerator->scan_uptodate ||
87c3a0f9 1045 !enumerator->sorted ||
0a166589
YW
1046 enumerator->type != DEVICE_ENUMERATION_TYPE_DEVICES ||
1047 enumerator->current_device_index + 1 >= enumerator->n_devices)
96df036f
TG
1048 return NULL;
1049
0a166589 1050 return enumerator->devices[++enumerator->current_device_index];
96df036f
TG
1051}
1052
1053int device_enumerator_scan_subsystems(sd_device_enumerator *enumerator) {
96df036f
TG
1054 int r = 0, k;
1055
1056 assert(enumerator);
1057
1058 if (enumerator->scan_uptodate &&
1059 enumerator->type == DEVICE_ENUMERATION_TYPE_SUBSYSTEMS)
1060 return 0;
1061
deac0c9c 1062 device_enumerator_unref_devices(enumerator);
96df036f
TG
1063
1064 /* modules */
1065 if (match_subsystem(enumerator, "module")) {
1066 k = enumerator_scan_dir_and_add_devices(enumerator, "module", NULL, NULL);
fed66db0
YW
1067 if (k < 0)
1068 r = log_debug_errno(k, "sd-device-enumerator: Failed to scan modules: %m");
96df036f
TG
1069 }
1070
96df036f
TG
1071 /* subsystems (only buses support coldplug) */
1072 if (match_subsystem(enumerator, "subsystem")) {
37cf83d9 1073 k = enumerator_scan_dir_and_add_devices(enumerator, "bus", NULL, NULL);
fed66db0
YW
1074 if (k < 0)
1075 r = log_debug_errno(k, "sd-device-enumerator: Failed to scan subsystems: %m");
96df036f
TG
1076 }
1077
1078 /* subsystem drivers */
1079 if (match_subsystem(enumerator, "drivers")) {
37cf83d9 1080 k = enumerator_scan_dir(enumerator, "bus", "drivers", "drivers");
fed66db0
YW
1081 if (k < 0)
1082 r = log_debug_errno(k, "sd-device-enumerator: Failed to scan drivers: %m");
96df036f
TG
1083 }
1084
1085 enumerator->scan_uptodate = true;
0a166589 1086 enumerator->type = DEVICE_ENUMERATION_TYPE_SUBSYSTEMS;
96df036f
TG
1087
1088 return r;
1089}
1090
1091_public_ sd_device *sd_device_enumerator_get_subsystem_first(sd_device_enumerator *enumerator) {
96df036f
TG
1092 assert_return(enumerator, NULL);
1093
87c3a0f9
YW
1094 if (device_enumerator_scan_subsystems(enumerator) < 0)
1095 return NULL;
1096
1097 if (enumerator_sort_devices(enumerator) < 0)
96df036f
TG
1098 return NULL;
1099
0a166589
YW
1100 enumerator->current_device_index = 0;
1101
1102 if (enumerator->n_devices == 0)
1103 return NULL;
96df036f 1104
0a166589 1105 return enumerator->devices[0];
96df036f
TG
1106}
1107
1108_public_ sd_device *sd_device_enumerator_get_subsystem_next(sd_device_enumerator *enumerator) {
1109 assert_return(enumerator, NULL);
1110
4005d321 1111 if (!enumerator->scan_uptodate ||
87c3a0f9 1112 !enumerator->sorted ||
0a166589
YW
1113 enumerator->type != DEVICE_ENUMERATION_TYPE_SUBSYSTEMS ||
1114 enumerator->current_device_index + 1 >= enumerator->n_devices)
96df036f
TG
1115 return NULL;
1116
0a166589 1117 return enumerator->devices[++enumerator->current_device_index];
96df036f
TG
1118}
1119
3c5cc23a 1120int device_enumerator_scan_devices_and_subsystems(sd_device_enumerator *enumerator) {
09c24f76 1121 int r;
3c5cc23a
YW
1122
1123 assert(enumerator);
1124
1125 if (enumerator->scan_uptodate &&
1126 enumerator->type == DEVICE_ENUMERATION_TYPE_ALL)
1127 return 0;
1128
1129 device_enumerator_unref_devices(enumerator);
1130
09c24f76
LP
1131 if (!set_isempty(enumerator->match_tag))
1132 r = enumerator_scan_devices_tags(enumerator);
1133 else if (enumerator->match_parent)
1134 r = enumerator_scan_devices_children(enumerator);
1135 else {
1136 int k;
3c5cc23a 1137
09c24f76 1138 r = enumerator_scan_devices_all(enumerator);
3c5cc23a
YW
1139
1140 if (match_subsystem(enumerator, "module")) {
1141 k = enumerator_scan_dir_and_add_devices(enumerator, "module", NULL, NULL);
1142 if (k < 0)
1143 r = log_debug_errno(k, "sd-device-enumerator: Failed to scan modules: %m");
1144 }
1145 if (match_subsystem(enumerator, "subsystem")) {
1146 k = enumerator_scan_dir_and_add_devices(enumerator, "bus", NULL, NULL);
1147 if (k < 0)
1148 r = log_debug_errno(k, "sd-device-enumerator: Failed to scan subsystems: %m");
1149 }
1150
1151 if (match_subsystem(enumerator, "drivers")) {
1152 k = enumerator_scan_dir(enumerator, "bus", "drivers", "drivers");
1153 if (k < 0)
1154 r = log_debug_errno(k, "sd-device-enumerator: Failed to scan drivers: %m");
1155 }
1156 }
1157
1158 enumerator->scan_uptodate = true;
1159 enumerator->type = DEVICE_ENUMERATION_TYPE_ALL;
1160
1161 return r;
1162}
1163
96df036f
TG
1164sd_device *device_enumerator_get_first(sd_device_enumerator *enumerator) {
1165 assert_return(enumerator, NULL);
1166
0a166589
YW
1167 if (!enumerator->scan_uptodate)
1168 return NULL;
1169
87c3a0f9
YW
1170 if (enumerator_sort_devices(enumerator) < 0)
1171 return NULL;
1172
0a166589
YW
1173 enumerator->current_device_index = 0;
1174
1175 if (enumerator->n_devices == 0)
1176 return NULL;
1177
1178 return enumerator->devices[0];
96df036f
TG
1179}
1180
1181sd_device *device_enumerator_get_next(sd_device_enumerator *enumerator) {
1182 assert_return(enumerator, NULL);
1183
0a166589 1184 if (!enumerator->scan_uptodate ||
87c3a0f9 1185 !enumerator->sorted ||
0a166589
YW
1186 enumerator->current_device_index + 1 >= enumerator->n_devices)
1187 return NULL;
96df036f 1188
0a166589 1189 return enumerator->devices[++enumerator->current_device_index];
96df036f 1190}
708474c5
YW
1191
1192sd_device **device_enumerator_get_devices(sd_device_enumerator *enumerator, size_t *ret_n_devices) {
1193 assert(enumerator);
1194 assert(ret_n_devices);
1195
1196 if (!enumerator->scan_uptodate)
1197 return NULL;
1198
87c3a0f9
YW
1199 if (enumerator_sort_devices(enumerator) < 0)
1200 return NULL;
1201
708474c5
YW
1202 *ret_n_devices = enumerator->n_devices;
1203 return enumerator->devices;
1204}