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