Debian Codegraph @

src/libsystemd/sd-device/device-enumerator.c

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