]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd/sd-device/device-enumerator.c
1b2b534530a55468c0bd8ead919f895d25d91e57
[thirdparty/systemd.git] / src / libsystemd / sd-device / device-enumerator.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <fnmatch.h>
4
5 #include "sd-device.h"
6
7 #include "alloc-util.h"
8 #include "device-enumerator-private.h"
9 #include "device-filter.h"
10 #include "device-util.h"
11 #include "dirent-util.h"
12 #include "fd-util.h"
13 #include "log.h"
14 #include "path-util.h"
15 #include "set.h"
16 #include "sort-util.h"
17 #include "string-util.h"
18 #include "strv.h"
19
20 typedef enum DeviceEnumerationType {
21 DEVICE_ENUMERATION_TYPE_DEVICES,
22 DEVICE_ENUMERATION_TYPE_SUBSYSTEMS,
23 DEVICE_ENUMERATION_TYPE_ALL,
24 _DEVICE_ENUMERATION_TYPE_MAX,
25 _DEVICE_ENUMERATION_TYPE_INVALID = -EINVAL,
26 } DeviceEnumerationType;
27
28 typedef 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
39 struct sd_device_enumerator {
40 unsigned n_ref;
41
42 DeviceEnumerationType type;
43 Hashmap *devices_by_syspath;
44 sd_device **devices;
45 size_t n_devices, current_device_index;
46 bool scan_uptodate;
47 bool sorted;
48
49 char **prioritized_subsystems;
50 Set *match_subsystem;
51 Set *nomatch_subsystem;
52 Hashmap *match_sysattr;
53 Hashmap *nomatch_sysattr;
54 Hashmap *match_property;
55 Hashmap *match_property_required;
56 Set *match_sysname;
57 Set *nomatch_sysname;
58 Set *match_tag;
59 Set *match_parent;
60 MatchInitializedType match_initialized;
61 MatchFlag parent_match_flags;
62 };
63
64 _public_ int sd_device_enumerator_new(sd_device_enumerator **ret) {
65 _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *enumerator = NULL;
66
67 assert(ret);
68
69 enumerator = new(sd_device_enumerator, 1);
70 if (!enumerator)
71 return -ENOMEM;
72
73 *enumerator = (sd_device_enumerator) {
74 .n_ref = 1,
75 .type = _DEVICE_ENUMERATION_TYPE_INVALID,
76 .match_initialized = MATCH_INITIALIZED_COMPAT,
77 .parent_match_flags = MATCH_ALL,
78 };
79
80 *ret = TAKE_PTR(enumerator);
81
82 return 0;
83 }
84
85 static 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
92 static void device_enumerator_unref_devices(sd_device_enumerator *enumerator) {
93 assert(enumerator);
94
95 hashmap_clear(enumerator->devices_by_syspath);
96 device_unref_many(enumerator->devices, enumerator->n_devices);
97 enumerator->devices = mfree(enumerator->devices);
98 enumerator->n_devices = 0;
99 }
100
101 static sd_device_enumerator *device_enumerator_free(sd_device_enumerator *enumerator) {
102 assert(enumerator);
103
104 device_enumerator_unref_devices(enumerator);
105
106 hashmap_free(enumerator->devices_by_syspath);
107 strv_free(enumerator->prioritized_subsystems);
108 set_free(enumerator->match_subsystem);
109 set_free(enumerator->nomatch_subsystem);
110 hashmap_free(enumerator->match_sysattr);
111 hashmap_free(enumerator->nomatch_sysattr);
112 hashmap_free(enumerator->match_property);
113 hashmap_free(enumerator->match_property_required);
114 set_free(enumerator->match_sysname);
115 set_free(enumerator->nomatch_sysname);
116 set_free(enumerator->match_tag);
117 set_free(enumerator->match_parent);
118
119 return mfree(enumerator);
120 }
121
122 DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_device_enumerator, sd_device_enumerator, device_enumerator_free);
123
124 int 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
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
154 r = set_put_strdup(set, subsystem);
155 if (r <= 0)
156 return r;
157
158 enumerator->scan_uptodate = false;
159
160 return 1;
161 }
162
163 _public_ int sd_device_enumerator_add_match_sysattr(sd_device_enumerator *enumerator, const char *sysattr, const char *value, int match) {
164 Hashmap **hashmap;
165 int r;
166
167 assert_return(enumerator, -EINVAL);
168 assert_return(sysattr, -EINVAL);
169
170 if (match)
171 hashmap = &enumerator->match_sysattr;
172 else
173 hashmap = &enumerator->nomatch_sysattr;
174
175 r = update_match_strv(hashmap, sysattr, value, /* clear_on_null = */ true);
176 if (r <= 0)
177 return r;
178
179 enumerator->scan_uptodate = false;
180
181 return 1;
182 }
183
184 _public_ int sd_device_enumerator_add_match_property(sd_device_enumerator *enumerator, const char *property, const char *value) {
185 int r;
186
187 assert_return(enumerator, -EINVAL);
188 assert_return(property, -EINVAL);
189
190 r = update_match_strv(&enumerator->match_property, property, value, /* clear_on_null = */ false);
191 if (r <= 0)
192 return r;
193
194 enumerator->scan_uptodate = false;
195
196 return 1;
197 }
198
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
214 static int device_enumerator_add_match_sysname(sd_device_enumerator *enumerator, const char *sysname, bool match) {
215 int r;
216
217 assert_return(enumerator, -EINVAL);
218 assert_return(sysname, -EINVAL);
219
220 r = set_put_strdup(match ? &enumerator->match_sysname : &enumerator->nomatch_sysname, sysname);
221 if (r <= 0)
222 return r;
223
224 enumerator->scan_uptodate = false;
225
226 return 1;
227 }
228
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
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
243 r = set_put_strdup(&enumerator->match_tag, tag);
244 if (r <= 0)
245 return r;
246
247 enumerator->scan_uptodate = false;
248
249 return 1;
250 }
251
252 int device_enumerator_add_match_parent_incremental(sd_device_enumerator *enumerator, sd_device *parent) {
253 const char *path;
254 int r;
255
256 assert(enumerator);
257 assert(parent);
258
259 r = sd_device_get_syspath(parent, &path);
260 if (r < 0)
261 return r;
262
263 r = set_put_strdup(&enumerator->match_parent, path);
264 if (r <= 0)
265 return r;
266
267 enumerator->scan_uptodate = false;
268
269 return 1;
270 }
271
272 _public_ int sd_device_enumerator_add_match_parent(sd_device_enumerator *enumerator, sd_device *parent) {
273 assert_return(enumerator, -EINVAL);
274 assert_return(parent, -EINVAL);
275
276 set_clear(enumerator->match_parent);
277
278 return device_enumerator_add_match_parent_incremental(enumerator, parent);
279 }
280
281 _public_ int sd_device_enumerator_allow_uninitialized(sd_device_enumerator *enumerator) {
282 assert_return(enumerator, -EINVAL);
283
284 enumerator->match_initialized = MATCH_INITIALIZED_ALL;
285
286 enumerator->scan_uptodate = false;
287
288 return 1;
289 }
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 }
299
300 int device_enumerator_add_match_is_initialized(sd_device_enumerator *enumerator, MatchInitializedType type) {
301 assert_return(enumerator, -EINVAL);
302 assert_return(type >= 0 && type < _MATCH_INITIALIZED_MAX, -EINVAL);
303
304 enumerator->match_initialized = type;
305
306 enumerator->scan_uptodate = false;
307
308 return 1;
309 }
310
311 static 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);
317
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. */
323
324 sound_a = strstrafter(devpath_a, "/sound/card");
325 if (!sound_a)
326 return 0;
327
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
343 static bool devpath_is_late_block(const char *devpath) {
344 assert(devpath);
345
346 return strstr(devpath, "/block/md") || strstr(devpath, "/block/dm-");
347 }
348
349 static 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;
364
365 /* md and dm devices are enumerated after all other devices */
366 r = CMP(devpath_is_late_block(devpath_a), devpath_is_late_block(devpath_b));
367 if (r != 0)
368 return r;
369
370 return path_compare(devpath_a, devpath_b);
371 }
372
373 static int enumerator_sort_devices(sd_device_enumerator *enumerator) {
374 size_t n_sorted = 0, n = 0;
375 sd_device **devices;
376 sd_device *device;
377 int r;
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
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;
396
397 r = device_in_subsystem(device, *prioritized_subsystem);
398 if (r < 0)
399 return r;
400 if (r == 0)
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
442 HASHMAP_FOREACH(device, enumerator->devices_by_syspath)
443 devices[n++] = sd_device_ref(device);
444
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);
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;
471
472 failed:
473 device_unref_many(devices, n);
474 free(devices);
475 return r;
476 }
477
478 DEFINE_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
483 int device_enumerator_add_device(sd_device_enumerator *enumerator, sd_device *device) {
484 const char *syspath;
485 int r;
486
487 assert_return(enumerator, -EINVAL);
488 assert_return(device, -EINVAL);
489
490 r = sd_device_get_syspath(device, &syspath);
491 if (r < 0)
492 return r;
493
494 r = hashmap_ensure_put(&enumerator->devices_by_syspath, &device_hash_ops_by_syspath, syspath, device);
495 if (IN_SET(r, -EEXIST, 0))
496 return 0;
497 if (r < 0)
498 return r;
499
500 sd_device_ref(device);
501
502 enumerator->sorted = false;
503 return 1;
504 }
505
506 static bool match_property(Hashmap *properties, sd_device *device, bool match_all) {
507 const char *property_pattern;
508 char * const *value_patterns;
509
510 assert(device);
511
512 /* Unlike device_match_sysattr(), this accepts device that has at least one matching property. */
513
514 if (hashmap_isempty(properties))
515 return true;
516
517 HASHMAP_FOREACH_KEY(value_patterns, property_pattern, properties) {
518 bool match = false;
519
520 FOREACH_DEVICE_PROPERTY(device, property, value) {
521 if (fnmatch(property_pattern, property, 0) != 0)
522 continue;
523
524 match = strv_fnmatch(value_patterns, value);
525 if (match) {
526 if (!match_all)
527 return true;
528
529 break;
530 }
531 }
532
533 if (!match && match_all)
534 return false;
535 }
536
537 return match_all;
538 }
539
540 static bool match_tag(sd_device_enumerator *enumerator, sd_device *device) {
541 const char *tag;
542
543 assert(enumerator);
544 assert(device);
545
546 SET_FOREACH(tag, enumerator->match_tag)
547 if (!sd_device_has_tag(device, tag))
548 return false;
549
550 return true;
551 }
552
553 static bool match_sysname(sd_device_enumerator *enumerator, const char *sysname) {
554 assert(enumerator);
555 assert(sysname);
556
557 return set_fnmatch(enumerator->match_sysname, enumerator->nomatch_sysname, sysname);
558 }
559
560 static 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)
581 return false;
582
583 if (sd_device_get_ifindex(device, NULL) >= 0)
584 return false;
585
586 return true;
587 }
588
589 return (enumerator->match_initialized == MATCH_INITIALIZED_NO) == (r == 0);
590 }
591
592 static 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
601 static int test_matches(
602 sd_device_enumerator *enumerator,
603 sd_device *device,
604 MatchFlag flags) {
605
606 int r;
607
608 assert(enumerator);
609 assert(device);
610
611 if (FLAGS_SET(flags, MATCH_SYSNAME)) {
612 const char *sysname;
613
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 }
621
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) &&
636 !device_match_parent(device, enumerator->match_parent, NULL))
637 return false;
638
639 if (FLAGS_SET(flags, MATCH_TAG) &&
640 !match_tag(enumerator, device))
641 return false;
642
643 if (FLAGS_SET(flags, MATCH_BASIC)) {
644 r = match_initialized(enumerator, device);
645 if (r <= 0)
646 return r;
647
648 if (!match_property(enumerator->match_property, device, /* match_all = */ false))
649 return false;
650
651 if (!match_property(enumerator->match_property_required, device, /* match_all = */ true))
652 return false;
653
654 if (!device_match_sysattr(device, enumerator->match_sysattr, enumerator->nomatch_sysattr))
655 return false;
656 }
657
658 return true;
659 }
660
661 static int enumerator_add_parent_devices(
662 sd_device_enumerator *enumerator,
663 sd_device *device,
664 MatchFlag flags) {
665
666 int r;
667
668 assert(enumerator);
669 assert(device);
670
671 for (;;) {
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;
677
678 r = test_matches(enumerator, device, flags);
679 if (r < 0)
680 return r;
681 if (r == 0)
682 continue;
683
684 r = device_enumerator_add_device(enumerator, device);
685 if (r <= 0) /* r == 0 means the device already exists, then no need to go further up. */
686 return r;
687 }
688 }
689
690 int device_enumerator_add_parent_devices(sd_device_enumerator *enumerator, sd_device *device) {
691 return enumerator_add_parent_devices(enumerator, device, MATCH_ALL & (~MATCH_PARENT));
692 }
693
694 static 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
705 static 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
711 _cleanup_closedir_ DIR *dir = NULL;
712 char *path;
713 int k, r = 0;
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);
727 if (!dir) {
728 /* This is necessarily racey, so ignore missing directories */
729 if (errno == ENOENT)
730 return 0;
731
732 return log_debug_errno(errno, "sd-device-enumerator: Failed to open directory '%s': %m", path);
733 }
734
735 FOREACH_DIRENT_ALL(de, dir, return -errno) {
736 _cleanup_(sd_device_unrefp) sd_device *device = NULL;
737 char syspath[strlen(path) + 1 + strlen(de->d_name) + 1];
738
739 if (!relevant_sysfs_subdir(de))
740 continue;
741
742 if (!match_sysname(enumerator, de->d_name))
743 continue;
744
745 (void) sprintf(syspath, "%s%s", path, de->d_name);
746
747 k = sd_device_new_from_syspath(&device, syspath);
748 if (k < 0) {
749 if (k != -ENODEV)
750 /* this is necessarily racey, so ignore missing devices */
751 r = k;
752
753 continue;
754 }
755
756 k = test_matches(enumerator, device, MATCH_ALL & (~MATCH_SYSNAME)); /* sysname is already tested. */
757 if (k <= 0) {
758 if (k < 0)
759 r = k;
760 continue;
761 }
762
763 k = device_enumerator_add_device(enumerator, device);
764 if (k < 0)
765 r = k;
766
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. */
770 k = enumerator_add_parent_devices(enumerator, device, enumerator->parent_match_flags);
771 if (k < 0)
772 r = k;
773 }
774
775 return r;
776 }
777
778 static int enumerator_scan_dir(
779 sd_device_enumerator *enumerator,
780 const char *basedir,
781 const char *subdir,
782 const char *subsystem) {
783
784 _cleanup_closedir_ DIR *dir = NULL;
785 char *path;
786 int r = 0;
787
788 path = strjoina("/sys/", basedir);
789
790 dir = opendir(path);
791 if (!dir) {
792 if (errno == ENOENT)
793 return 0;
794
795 return log_debug_errno(errno, "sd-device-enumerator: Failed to open directory '%s': %m", path);
796 }
797
798 FOREACH_DIRENT_ALL(de, dir, return -errno) {
799 int k;
800
801 if (!relevant_sysfs_subdir(de))
802 continue;
803
804 if (!match_subsystem(enumerator, subsystem ?: de->d_name))
805 continue;
806
807 k = enumerator_scan_dir_and_add_devices(enumerator, basedir, de->d_name, subdir);
808 if (k < 0)
809 r = k;
810 }
811
812 return r;
813 }
814
815 static int enumerator_scan_devices_tag(sd_device_enumerator *enumerator, const char *tag) {
816 _cleanup_closedir_ DIR *dir = NULL;
817 char *path;
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) {
827 if (errno == ENOENT)
828 return 0;
829
830 return log_debug_errno(errno, "sd-device-enumerator: Failed to open directory '%s': %m", path);
831 }
832
833 /* TODO: filter away subsystems? */
834
835 FOREACH_DIRENT_ALL(de, dir, return -errno) {
836 _cleanup_(sd_device_unrefp) sd_device *device = NULL;
837 int k;
838
839 if (de->d_name[0] == '.')
840 continue;
841
842 k = sd_device_new_from_device_id(&device, de->d_name);
843 if (k < 0) {
844 if (k != -ENODEV)
845 /* this is necessarily racy, so ignore missing devices */
846 r = k;
847
848 continue;
849 }
850
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)
854 r = k;
855 if (k <= 0)
856 continue;
857
858 k = device_enumerator_add_device(enumerator, device);
859 if (k < 0) {
860 r = k;
861 continue;
862 }
863 }
864
865 return r;
866 }
867
868 static int enumerator_scan_devices_tags(sd_device_enumerator *enumerator) {
869 const char *tag;
870 int r = 0;
871
872 assert(enumerator);
873
874 SET_FOREACH(tag, enumerator->match_tag) {
875 int k;
876
877 k = enumerator_scan_devices_tag(enumerator, tag);
878 if (k < 0)
879 r = k;
880 }
881
882 return r;
883 }
884
885 static int parent_add_child(sd_device_enumerator *enumerator, const char *path, MatchFlag flags) {
886 _cleanup_(sd_device_unrefp) sd_device *device = NULL;
887 int r;
888
889 r = sd_device_new_from_syspath(&device, path);
890 if (r == -ENODEV)
891 /* this is necessarily racy, so ignore missing devices */
892 return 0;
893 else if (r < 0)
894 return r;
895
896 r = test_matches(enumerator, device, flags);
897 if (r <= 0)
898 return r;
899
900 return device_enumerator_add_device(enumerator, device);
901 }
902
903 static int parent_crawl_children(sd_device_enumerator *enumerator, const char *path, Set **stack) {
904 _cleanup_closedir_ DIR *dir = NULL;
905 int r = 0;
906
907 assert(enumerator);
908 assert(path);
909 assert(stack);
910
911 dir = opendir(path);
912 if (!dir) {
913 if (errno == ENOENT)
914 return 0;
915
916 return log_debug_errno(errno, "sd-device-enumerator: Failed to open directory '%s': %m", path);
917 }
918
919 FOREACH_DIRENT_ALL(de, dir, return -errno) {
920 _cleanup_free_ char *child = NULL;
921 int k;
922
923 if (de->d_name[0] == '.')
924 continue;
925
926 if (de->d_type != DT_DIR)
927 continue;
928
929 child = path_join(path, de->d_name);
930 if (!child)
931 return -ENOMEM;
932
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 }
940
941 k = set_ensure_consume(stack, &path_hash_ops_free, TAKE_PTR(child));
942 if (k < 0)
943 r = k;
944 }
945
946 return r;
947 }
948
949 static int enumerator_scan_devices_children(sd_device_enumerator *enumerator) {
950 _cleanup_set_free_ Set *stack = NULL;
951 const char *path;
952 int r = 0, k;
953
954 assert(enumerator);
955
956 SET_FOREACH(path, enumerator->match_parent) {
957 k = parent_add_child(enumerator, path, MATCH_ALL & (~MATCH_PARENT));
958 if (k < 0)
959 r = k;
960
961 k = parent_crawl_children(enumerator, path, &stack);
962 if (k < 0)
963 r = k;
964 }
965
966 for (;;) {
967 _cleanup_free_ char *p = NULL;
968
969 p = set_steal_first(stack);
970 if (!p)
971 return r;
972
973 k = parent_crawl_children(enumerator, p, &stack);
974 if (k < 0)
975 r = k;
976 }
977 }
978
979 static int enumerator_scan_devices_all(sd_device_enumerator *enumerator) {
980 int k, r = 0;
981
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");
985
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");
989
990 return r;
991 }
992
993 int device_enumerator_scan_devices(sd_device_enumerator *enumerator) {
994 int r = 0, k;
995
996 assert(enumerator);
997
998 if (enumerator->scan_uptodate &&
999 enumerator->type == DEVICE_ENUMERATION_TYPE_DEVICES)
1000 return 0;
1001
1002 device_enumerator_unref_devices(enumerator);
1003
1004 if (!set_isempty(enumerator->match_tag)) {
1005 k = enumerator_scan_devices_tags(enumerator);
1006 if (k < 0)
1007 r = k;
1008 } else if (enumerator->match_parent) {
1009 k = enumerator_scan_devices_children(enumerator);
1010 if (k < 0)
1011 r = k;
1012 } else {
1013 k = enumerator_scan_devices_all(enumerator);
1014 if (k < 0)
1015 r = k;
1016 }
1017
1018 enumerator->scan_uptodate = true;
1019 enumerator->type = DEVICE_ENUMERATION_TYPE_DEVICES;
1020
1021 return r;
1022 }
1023
1024 _public_ sd_device *sd_device_enumerator_get_device_first(sd_device_enumerator *enumerator) {
1025 assert_return(enumerator, NULL);
1026
1027 if (device_enumerator_scan_devices(enumerator) < 0)
1028 return NULL;
1029
1030 if (enumerator_sort_devices(enumerator) < 0)
1031 return NULL;
1032
1033 enumerator->current_device_index = 0;
1034
1035 if (enumerator->n_devices == 0)
1036 return NULL;
1037
1038 return enumerator->devices[0];
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 ||
1045 !enumerator->sorted ||
1046 enumerator->type != DEVICE_ENUMERATION_TYPE_DEVICES ||
1047 enumerator->current_device_index + 1 >= enumerator->n_devices)
1048 return NULL;
1049
1050 return enumerator->devices[++enumerator->current_device_index];
1051 }
1052
1053 int device_enumerator_scan_subsystems(sd_device_enumerator *enumerator) {
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
1062 device_enumerator_unref_devices(enumerator);
1063
1064 /* modules */
1065 if (match_subsystem(enumerator, "module")) {
1066 k = enumerator_scan_dir_and_add_devices(enumerator, "module", NULL, NULL);
1067 if (k < 0)
1068 r = log_debug_errno(k, "sd-device-enumerator: Failed to scan modules: %m");
1069 }
1070
1071 /* subsystems (only buses support coldplug) */
1072 if (match_subsystem(enumerator, "subsystem")) {
1073 k = enumerator_scan_dir_and_add_devices(enumerator, "bus", NULL, NULL);
1074 if (k < 0)
1075 r = log_debug_errno(k, "sd-device-enumerator: Failed to scan subsystems: %m");
1076 }
1077
1078 /* subsystem drivers */
1079 if (match_subsystem(enumerator, "drivers")) {
1080 k = enumerator_scan_dir(enumerator, "bus", "drivers", "drivers");
1081 if (k < 0)
1082 r = log_debug_errno(k, "sd-device-enumerator: Failed to scan drivers: %m");
1083 }
1084
1085 enumerator->scan_uptodate = true;
1086 enumerator->type = DEVICE_ENUMERATION_TYPE_SUBSYSTEMS;
1087
1088 return r;
1089 }
1090
1091 _public_ sd_device *sd_device_enumerator_get_subsystem_first(sd_device_enumerator *enumerator) {
1092 assert_return(enumerator, NULL);
1093
1094 if (device_enumerator_scan_subsystems(enumerator) < 0)
1095 return NULL;
1096
1097 if (enumerator_sort_devices(enumerator) < 0)
1098 return NULL;
1099
1100 enumerator->current_device_index = 0;
1101
1102 if (enumerator->n_devices == 0)
1103 return NULL;
1104
1105 return enumerator->devices[0];
1106 }
1107
1108 _public_ sd_device *sd_device_enumerator_get_subsystem_next(sd_device_enumerator *enumerator) {
1109 assert_return(enumerator, NULL);
1110
1111 if (!enumerator->scan_uptodate ||
1112 !enumerator->sorted ||
1113 enumerator->type != DEVICE_ENUMERATION_TYPE_SUBSYSTEMS ||
1114 enumerator->current_device_index + 1 >= enumerator->n_devices)
1115 return NULL;
1116
1117 return enumerator->devices[++enumerator->current_device_index];
1118 }
1119
1120 int device_enumerator_scan_devices_and_subsystems(sd_device_enumerator *enumerator) {
1121 int r;
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
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;
1137
1138 r = enumerator_scan_devices_all(enumerator);
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
1164 sd_device *device_enumerator_get_first(sd_device_enumerator *enumerator) {
1165 assert_return(enumerator, NULL);
1166
1167 if (!enumerator->scan_uptodate)
1168 return NULL;
1169
1170 if (enumerator_sort_devices(enumerator) < 0)
1171 return NULL;
1172
1173 enumerator->current_device_index = 0;
1174
1175 if (enumerator->n_devices == 0)
1176 return NULL;
1177
1178 return enumerator->devices[0];
1179 }
1180
1181 sd_device *device_enumerator_get_next(sd_device_enumerator *enumerator) {
1182 assert_return(enumerator, NULL);
1183
1184 if (!enumerator->scan_uptodate ||
1185 !enumerator->sorted ||
1186 enumerator->current_device_index + 1 >= enumerator->n_devices)
1187 return NULL;
1188
1189 return enumerator->devices[++enumerator->current_device_index];
1190 }
1191
1192 sd_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
1199 if (enumerator_sort_devices(enumerator) < 0)
1200 return NULL;
1201
1202 *ret_n_devices = enumerator->n_devices;
1203 return enumerator->devices;
1204 }