/
enumerate.rs
1017 lines (897 loc) · 35.9 KB
/
enumerate.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! Lookup and sort `sys` devices.
//!
//! Lookup devices in the `sys` filesystem, filter devices by properties,
//! and return a sorted list of devices.
//!
//! From [`libudev-enumerate`](https://github.com/eudev-project/eudev/blob/master/src/libudev/libudev-enumerate.c) documentation.
use std::{fs, sync::Arc};
use crate::util;
use crate::UDEV_ROOT_RUN;
use crate::{Error, Result, Udev, UdevDevice, UdevEntry, UdevEntryList, UdevList};
const LOG_PREFIX: &str = "udev enumerate:";
/// Represents the file path in the `sys` filesystem.
#[repr(C)]
#[derive(Clone, Debug, Default, PartialEq)]
pub struct Syspath {
syspath: String,
}
impl Syspath {
/// Creates a new [Syspath].
pub const fn new() -> Self {
Self {
syspath: String::new(),
}
}
/// Gets a reference to the syspath string.
pub fn syspath(&self) -> &str {
self.syspath.as_str()
}
/// Sets the syspath.
pub fn set_syspath<S: Into<String>>(&mut self, syspath: S) {
self.syspath = syspath.into();
}
/// Builder function that sets the syspath.
pub fn with_syspath<S: Into<String>>(mut self, syspath: S) -> Self {
self.set_syspath(syspath);
self
}
}
/// Represents one device lookup/sort context..
#[repr(C)]
#[derive(Clone, Debug, Default, PartialEq)]
pub struct UdevEnumerate {
udev: Arc<Udev>,
sysattr_match_list: UdevList,
sysattr_nomatch_list: UdevList,
subsystem_match_list: UdevList,
subsystem_nomatch_list: UdevList,
sysname_match_list: UdevList,
properties_match_list: UdevList,
tags_match_list: UdevList,
devices_list: UdevList,
parent: Option<Arc<UdevDevice>>,
devices: Vec<Syspath>,
devices_cur: usize,
devices_max: usize,
devices_uptodate: bool,
match_is_initialized: bool,
}
impl UdevEnumerate {
/// Creates a new [UdevEnumerate].
pub fn new(udev: Arc<Udev>) -> Self {
let sysattr_match_list = UdevList::new(Arc::clone(&udev));
let sysattr_nomatch_list = UdevList::new(Arc::clone(&udev));
let subsystem_match_list = UdevList::new(Arc::clone(&udev));
let subsystem_nomatch_list = UdevList::new(Arc::clone(&udev));
let sysname_match_list = UdevList::new(Arc::clone(&udev));
let properties_match_list = UdevList::new(Arc::clone(&udev));
let tags_match_list = UdevList::new(Arc::clone(&udev));
let devices_list = UdevList::new(Arc::clone(&udev));
Self {
udev,
sysattr_match_list,
sysattr_nomatch_list,
subsystem_match_list,
subsystem_nomatch_list,
sysname_match_list,
properties_match_list,
tags_match_list,
devices_list,
parent: None,
devices: Vec::new(),
devices_cur: 0,
devices_max: 0,
devices_uptodate: false,
match_is_initialized: false,
}
}
/// Gets a reference to the [Udev] object.
pub fn udev(&self) -> &Arc<Udev> {
&self.udev
}
/// Gets a reference to the sysattr match list [UdevList].
pub const fn sysattr_match_list(&self) -> &UdevList {
&self.sysattr_match_list
}
/// Gets a mutable reference to the sysattr match list [UdevList].
pub fn sysattr_match_list_mut(&mut self) -> &mut UdevList {
&mut self.sysattr_match_list
}
/// Sets the sysattr match list [UdevList].
pub fn set_sysattr_match_list<L: Into<UdevEntryList>>(&mut self, list: L) {
self.sysattr_match_list.set_list(list);
}
/// Builder function that sets the sysattr match list [UdevList].
pub fn with_sysattr_match_list<L: Into<UdevEntryList>>(mut self, list: L) -> Self {
self.set_sysattr_match_list(list);
self
}
/// Adds an entry to the match sysattr [UdevEntry] list.
///
/// From the `libudev` documentation:
///
/// ```no_build,no_run
/// Match only devices with a given /sys device attribute.
/// ```
///
/// Returns `Ok(UdevEntry)` on success, `Err(Error)` otherwise.
pub fn add_match_sysattr(&mut self, sysattr: &str) -> Result<&UdevEntry> {
if sysattr.is_empty() {
Err(Error::UdevEnumerate("sysattr is null".into()))
} else {
self.sysattr_match_list
.add_entry(sysattr, "")
.ok_or(Error::UdevEnumerate(
"unable to add match sysattr entry".into(),
))
}
}
/// Gets a reference to the sysattr nomatch list [UdevList].
pub const fn sysattr_nomatch_list(&self) -> &UdevList {
&self.sysattr_nomatch_list
}
/// Gets a mutable reference to the sysattr nomatch list [UdevList].
pub fn sysattr_nomatch_list_mut(&mut self) -> &mut UdevList {
&mut self.sysattr_nomatch_list
}
/// Sets the sysattr nomatch list [UdevList].
pub fn set_sysattr_nomatch_list<L: Into<UdevEntryList>>(&mut self, list: L) {
self.sysattr_nomatch_list.set_list(list);
}
/// Builder function that sets the sysattr nomatch list [UdevList].
pub fn with_sysattr_nomatch_list<L: Into<UdevEntryList>>(mut self, list: L) -> Self {
self.set_sysattr_nomatch_list(list);
self
}
/// Adds an entry to the no-match sysattr [UdevEntry] list.
///
/// From the `libudev` documentation:
///
/// ```no_build,no_run
/// Match only devices without a given /sys device attribute.
/// ```
///
/// Returns `Ok(UdevEntry)` on success, `Err(Error)` otherwise.
pub fn add_nomatch_sysattr(&mut self, sysattr: &str) -> Result<&UdevEntry> {
if sysattr.is_empty() {
Err(Error::UdevEnumerate("sysattr is null".into()))
} else {
self.sysattr_nomatch_list
.add_entry(sysattr, "")
.ok_or(Error::UdevEnumerate(
"unable to add no-match sysattr entry".into(),
))
}
}
/// Gets a reference to the subsystem match list [UdevList].
pub const fn subsystem_match_list(&self) -> &UdevList {
&self.subsystem_match_list
}
/// Gets a mutable reference to the subsystem match list [UdevList].
pub fn subsystem_match_list_mut(&mut self) -> &mut UdevList {
&mut self.subsystem_match_list
}
/// Sets the subsystem match list [UdevList].
pub fn set_subsystem_match_list<L: Into<UdevEntryList>>(&mut self, list: L) {
self.subsystem_match_list.set_list(list);
}
/// Builder function that sets the subsystem match list [UdevList].
pub fn with_subsystem_match_list<L: Into<UdevEntryList>>(mut self, list: L) -> Self {
self.set_subsystem_match_list(list);
self
}
/// Adds an entry to the match subsystem [UdevEntry] list.
///
/// From the `libudev` documentation:
///
/// ```no_build,no_run
/// Match only devices belonging to a certain kernel subsystem.
/// ```
///
/// Returns `Ok(UdevEntry)` on success, `Err(Error)` otherwise.
pub fn add_match_subsystem(&mut self, subsystem: &str) -> Result<&UdevEntry> {
if subsystem.is_empty() {
Err(Error::UdevEnumerate("subsystem is null".into()))
} else {
self.subsystem_match_list
.add_entry(subsystem, "")
.ok_or(Error::UdevEnumerate(
"unable to add match subsystem entry".into(),
))
}
}
/// Gets a reference to the subsystem nomatch list [UdevList].
pub const fn subsystem_nomatch_list(&self) -> &UdevList {
&self.subsystem_nomatch_list
}
/// Gets a mutable reference to the subsystem nomatch list [UdevList].
pub fn subsystem_nomatch_list_mut(&mut self) -> &mut UdevList {
&mut self.subsystem_nomatch_list
}
/// Sets the subsystem nomatch list [UdevList].
pub fn set_subsystem_nomatch_list<L: Into<UdevEntryList>>(&mut self, list: L) {
self.subsystem_nomatch_list.set_list(list);
}
/// Builder function that sets the subsystem nomatch list [UdevList].
pub fn with_subsystem_nomatch_list<L: Into<UdevEntryList>>(mut self, list: L) -> Self {
self.set_subsystem_nomatch_list(list);
self
}
/// Adds an entry to the no-match subsystem [UdevEntry] list.
///
/// From the `libudev` documentation:
///
/// ```no_build,no_run
/// Match only devices not belonging to a certain kernel subsystem.
/// ```
///
/// Returns `Ok(UdevEntry)` on success, `Err(Error)` otherwise.
pub fn add_nomatch_subsystem(&mut self, subsystem: &str) -> Result<&UdevEntry> {
if subsystem.is_empty() {
Err(Error::UdevEnumerate("subsystem is null".into()))
} else {
self.subsystem_nomatch_list
.add_entry(subsystem, "")
.ok_or(Error::UdevEnumerate(
"unable to add no-match subsystem entry".into(),
))
}
}
/// Gets a reference to the sysname match list [UdevList].
pub const fn sysname_match_list(&self) -> &UdevList {
&self.sysname_match_list
}
/// Gets a mutable reference to the sysname match list [UdevList].
pub fn sysname_match_list_mut(&mut self) -> &mut UdevList {
&mut self.sysname_match_list
}
/// Sets the sysname match list [UdevList].
pub fn set_sysname_match_list<L: Into<UdevEntryList>>(&mut self, list: L) {
self.sysname_match_list.set_list(list);
}
/// Builder function that sets the sysname match list [UdevList].
pub fn with_sysname_match_list<L: Into<UdevEntryList>>(mut self, list: L) -> Self {
self.set_sysname_match_list(list);
self
}
/// Adds an entry to the match sysname [UdevEntry] list.
///
/// From the `libudev` documentation:
///
/// ```no_build,no_run
/// Match only devices with a given /sys device name.
/// ```
///
/// Returns `Ok(UdevEntry)` on success, `Err(Error)` otherwise.
pub fn add_match_sysname(&mut self, sysname: &str) -> Result<&UdevEntry> {
if sysname.is_empty() {
Err(Error::UdevEnumerate("sysname is null".into()))
} else {
self.sysname_match_list
.add_entry(sysname, "")
.ok_or(Error::UdevEnumerate(
"unable to add match sysname entry".into(),
))
}
}
/// Gets a reference to the properties match list [UdevList].
pub const fn properties_match_list(&self) -> &UdevList {
&self.properties_match_list
}
/// Gets a mutable reference to the properties match list [UdevList].
pub fn properties_match_list_mut(&mut self) -> &mut UdevList {
&mut self.properties_match_list
}
/// Sets the properties match list [UdevList].
pub fn set_properties_match_list<L: Into<UdevEntryList>>(&mut self, list: L) {
self.properties_match_list.set_list(list);
}
/// Builder function that sets the properties match list [UdevList].
pub fn with_properties_match_list<L: Into<UdevEntryList>>(mut self, list: L) -> Self {
self.set_properties_match_list(list);
self
}
/// Adds an entry to the match properties [UdevEntry] list.
///
/// From the `libudev` documentation:
///
/// ```no_build,no_run
/// Match only devices with a certain property.
/// ```
///
/// Returns `Ok(UdevEntry)` on success, `Err(Error)` otherwise.
pub fn add_match_property(&mut self, property: &str, value: &str) -> Result<&UdevEntry> {
if property.is_empty() {
Err(Error::UdevEnumerate("property is null".into()))
} else {
self.properties_match_list
.add_entry(property, value)
.ok_or(Error::UdevEnumerate(
"unable to add match property entry".into(),
))
}
}
/// Gets a reference to the tags match list [UdevList].
pub const fn tags_match_list(&self) -> &UdevList {
&self.tags_match_list
}
/// Gets a mutable reference to the tags match list [UdevList].
pub fn tags_match_list_mut(&mut self) -> &mut UdevList {
&mut self.tags_match_list
}
/// Sets the tags match list [UdevList].
pub fn set_tags_match_list<L: Into<UdevEntryList>>(&mut self, list: L) {
self.tags_match_list.set_list(list);
}
/// Builder function that sets the tags match list [UdevList].
pub fn with_tags_match_list<L: Into<UdevEntryList>>(mut self, list: L) -> Self {
self.set_tags_match_list(list);
self
}
/// Adds an entry to the match tags [UdevEntry] list.
///
/// From the `libudev` documentation:
///
/// ```no_build,no_run
/// Match only devices with a certain tag.
/// ```
///
/// Returns `Ok(UdevEntry)` on success, `Err(Error)` otherwise.
pub fn add_match_tag(&mut self, tag: &str) -> Result<&UdevEntry> {
if tag.is_empty() {
Err(Error::UdevEnumerate("tag is null".into()))
} else {
self.tags_match_list
.add_entry(tag, "")
.ok_or(Error::UdevEnumerate("unable to add match tag entry".into()))
}
}
/// Gets a reference to the devices list [UdevList].
pub const fn devices_list(&self) -> &UdevList {
&self.devices_list
}
/// Gets a mutable reference to the devices list [UdevList].
pub fn devices_list_mut(&mut self) -> &mut UdevList {
&mut self.devices_list
}
/// Sets the devices list [UdevList].
pub fn set_devices_list<L: Into<UdevEntryList>>(&mut self, list: L) {
self.devices_list.set_list(list);
}
/// Builder function that sets the devices list [UdevList].
pub fn with_devices_list<L: Into<UdevEntryList>>(mut self, list: L) -> Self {
self.set_devices_list(list);
self
}
/// Gets an optional reference to the parent [UdevDevice].
pub fn parent(&self) -> Option<&Arc<UdevDevice>> {
self.parent.as_ref()
}
/// Sets the parent [UdevDevice].
pub fn set_parent(&mut self, parent: Arc<UdevDevice>) {
self.parent.replace(parent);
}
/// Builder function that sets the parent [UdevDevice].
pub fn with_parent(mut self, parent: Arc<UdevDevice>) -> Self {
self.set_parent(parent);
self
}
/// Gets a reference to the [Syspath] devices list.
pub fn devices(&self) -> &[Syspath] {
self.devices.as_ref()
}
/// Sets the [Syspath] devices list.
pub fn set_devices<S: IntoIterator<Item = Syspath>>(&mut self, devices: S) {
self.devices = devices.into_iter().collect();
}
/// Builder function that sets the [Syspath] devices list.
pub fn with_devices<S: IntoIterator<Item = Syspath>>(mut self, devices: S) -> Self {
self.set_devices(devices);
self
}
/// Gets the current index into the [Syspath] devices list.
pub const fn devices_cur(&self) -> usize {
self.devices_cur
}
/// Sets the current index into the [Syspath] devices list.
///
/// **NOTE**: `cur` must be in-bounds to set the current index, otherwise this function is a
/// no-op.
pub fn set_devices_cur(&mut self, cur: usize) {
if cur < self.devices.len() {
self.devices_cur = cur;
}
}
/// Builder function that sets the current index into the [Syspath] devices list.
///
/// **NOTE**: `cur` must be in-bounds to set the current index, otherwise this function is a
/// no-op.
pub fn with_devices_cur(mut self, cur: usize) -> Self {
self.set_devices_cur(cur);
self
}
/// Gets the maximum number of [Syspath] devices.
pub const fn devices_max(&self) -> usize {
self.devices_max
}
/// Sets the maximum number of [Syspath] devices.
///
/// **NOTE** if `max` is greater than the current [Syspath] devices list capacity,
/// additional space will be reserved to avoid frequent reallocations.
pub fn set_devices_max(&mut self, max: usize) {
self.devices_max = max;
let cap = self.devices.capacity();
self.devices.reserve(self.devices_max.saturating_sub(cap));
}
/// Builder function that sets the maximum number of [Syspath] devices.
///
/// **NOTE** if `max` is greater than the current [Syspath] devices list capacity,
/// additional space will be reserved to avoid frequent reallocations.
pub fn with_devices_max(mut self, max: usize) -> Self {
self.set_devices_max(max);
self
}
/// Gets whether the [Syspath] devices list is up-to-date.
pub const fn devices_uptodate(&self) -> bool {
self.devices_uptodate
}
/// Sets whether the [Syspath] devices list is up-to-date.
pub fn set_devices_uptodate(&mut self, val: bool) {
self.devices_uptodate = val;
}
/// Builder function that sets whether the [Syspath] devices list is up-to-date.
pub fn with_devices_uptodate(mut self, val: bool) -> Self {
self.set_devices_uptodate(val);
self
}
/// Gets whether the match lists are initialized.
pub const fn match_is_initialized(&self) -> bool {
self.match_is_initialized
}
/// Sets whether the match lists are initialized.
pub fn set_match_is_initialized(&mut self, val: bool) {
self.match_is_initialized = val;
}
/// Builder function that sets whether the match lists are initialized.
pub fn with_match_is_initialized(mut self, val: bool) -> Self {
self.set_match_is_initialized(val);
self
}
/// Adds a devices to the list of devices.
///
/// From the `libudev` documentation:
///
/// ```no_build,no_run
/// Add a device to the list of devices, to retrieve it back sorted in dependency order.
/// ```
///
/// Returns: `Ok(())` on success, `Err(Error)` otherwise.
pub fn add_syspath(&mut self, syspath: &str) -> Result<()> {
if syspath.is_empty() {
Err(Error::UdevEnumerate("empty syspath".into()))
} else {
let dev = UdevDevice::new_from_syspath(Arc::clone(&self.udev), syspath)?;
self.syspath_add(dev.syspath())
}
}
fn syspath_add(&mut self, syspath: &str) -> Result<()> {
if self.devices_cur >= self.devices_max {
self.devices_max = self.devices_max.saturating_add(1024);
self.devices.reserve(1024);
}
self.devices.push(Syspath::new().with_syspath(syspath));
self.devices_cur = self.devices_cur.saturating_add(1);
self.devices_uptodate = false;
Ok(())
}
/// Scan `/sys` for devices which match the given filters.
///
/// From the `libudev` documentation:
///
/// ```no_build,no_run
/// Scan /sys for all devices which match the given filters. No matches
/// will return all currently available devices.
/// ```
///
/// Returns: `Ok(())` on success, `Err(Error)` otherwise.
pub fn scan_devices(&mut self) -> Result<()> {
if self.tags_match_list.entry().is_some() {
self.scan_devices_tags()
} else if self.parent.is_some() {
self.scan_devices_children()
} else {
self.scan_devices_all()
}
}
fn scan_devices_tags(&mut self) -> Result<()> {
// scan only tagged devices.
// use tags reverse-index, instead of searching all deivces in /sys
let mut add_syspaths: Vec<String> = Vec::new();
for list_entry in self.tags_match_list.iter() {
let tag_name = list_entry.name();
let path = format!("{UDEV_ROOT_RUN}/udev/tags/{tag_name}");
for dir_entry in fs::read_dir(path.as_str())
.map_err(|err| Error::UdevEnumerate(format!("unable to open tags path: {err}")))?
.filter(|e| e.is_ok())
{
let d_name = dir_entry?
.file_name()
.into_string()
.unwrap_or(String::new());
if d_name.is_empty() {
log::trace!("{LOG_PREFIX} empty/invalid entry");
} else if d_name.starts_with('.') {
log::trace!("{LOG_PREFIX} private entry");
} else {
let mut dev =
UdevDevice::new_from_device_id(Arc::clone(&self.udev), d_name.as_str())?;
let dev_syspath = dev.syspath().to_owned();
if !self.match_subsystem(dev.subsystem()) {
log::trace!("{LOG_PREFIX} no subsystem match");
} else if !self.match_sysname(dev.sysname()) {
log::trace!("{LOG_PREFIX} no sysname match");
} else if !self.match_parent(&dev) {
log::trace!("{LOG_PREFIX} no parent match");
} else if !self.match_property(&dev) {
log::trace!("{LOG_PREFIX} no property match");
} else if !self.match_sysattr(&mut dev) {
log::trace!("{LOG_PREFIX} no sys attribute match");
} else {
add_syspaths.push(dev_syspath);
}
}
}
}
for path in add_syspaths.iter() {
self.syspath_add(path)?;
}
Ok(())
}
fn match_subsystem(&self, subsystem: &str) -> bool {
!subsystem.is_empty()
&& !self
.subsystem_nomatch_list
.iter()
.any(|f| f.name() == subsystem)
&& (self.subsystem_match_list.is_empty()
|| self
.subsystem_match_list
.iter()
.any(|f| f.name() == subsystem))
}
fn match_sysname(&self, sysname: &str) -> bool {
!sysname.is_empty()
&& (self.sysname_match_list.is_empty()
|| self.sysname_match_list.iter().any(|f| f.name() == sysname))
}
fn match_parent(&self, dev: &UdevDevice) -> bool {
match self.parent.as_ref() {
Some(parent) => dev.devpath().starts_with(parent.devpath()),
None => true,
}
}
fn match_tag(&self, dev: &mut UdevDevice) -> bool {
// no match always matches
self.tags_match_list.is_empty() ||
// loop over matches
// if any tag is a mismatch, return false
self.tags_match_list.iter().filter(|f| !dev.has_tag(f.name())).count() == 0
}
fn match_property(&self, dev: &UdevDevice) -> bool {
if self.properties_match_list.is_empty() {
true
} else {
let mut ret = false;
for list_entry in self.properties_match_list.iter() {
let match_key = list_entry.name();
let match_value = list_entry.value();
for property_entry in dev.properties_list().iter() {
let dev_key = property_entry.name();
let dev_value = property_entry.value();
if let Ok(key_pattern) = glob::Pattern::new(match_key) {
if !key_pattern.matches(dev_key) {
log::trace!(
"no key match found, entry key: {match_key}, device key {dev_key}"
);
} else if match_value.is_empty() && dev_value.is_empty() {
ret = true;
break;
} else if let Ok(val_pattern) = glob::Pattern::new(match_value) {
if val_pattern.matches(dev_value) {
ret = true;
break;
}
} else {
log::trace!("no value match found, entry value: {match_value}, device value: {dev_value}");
}
}
}
if ret {
break;
}
}
ret
}
}
fn match_sysattr(&self, dev: &mut UdevDevice) -> bool {
!self
.sysattr_nomatch_list
.iter()
.any(|f| dev.match_sysattr_value(f.name(), f.value()))
&& self
.sysattr_match_list
.iter()
.filter(|f| !dev.match_sysattr_value(f.name(), f.value()))
.count()
== 0
&& self
.sysattr_match_list
.iter()
.any(|f| dev.match_sysattr_value(f.name(), f.value()))
}
fn scan_devices_children(&mut self) -> Result<()> {
Err(Error::UdevEnumerate("unimplemented".into()))
}
fn scan_devices_all(&mut self) -> Result<()> {
Err(Error::UdevEnumerate("unimplemented".into()))
}
/// Scans `/sys` for all kernel subsystems.
///
/// From `libudev` documentation:
///
/// ```no_build,no_run
/// Scan /sys for all kernel subsystems, including buses, classes, drivers.
/// ```
///
/// Returns: `Ok(())` on success, `Err(Error)` otherwise.
pub fn scan_subsystems(&mut self) -> Result<()> {
// all kernel modules
if self.match_subsystem("module") {
self.scan_dir_and_add_devices("module", "", "")?;
}
let subsysdir = if fs::metadata("/sys/subsystem").is_ok() {
"subsystem"
} else {
"bus"
};
// all subsystems (only buses support coldplug)
if self.match_subsystem("subsystem") {
self.scan_dir_and_add_devices(subsysdir, "", "")?;
}
// all subsystem drivers
if self.match_subsystem("drivers") {
self.scan_dir(subsysdir, "drivers", "drivers")?;
}
Ok(())
}
fn scan_dir_and_add_devices(
&mut self,
basedir: &str,
subdir1: &str,
subdir2: &str,
) -> Result<()> {
let path = if !subdir1.is_empty() && !subdir2.is_empty() {
format!("/sys/{basedir}/{subdir1}/{subdir2}")
} else if !subdir1.is_empty() {
format!("/sys/{basedir}/{subdir1}")
} else if !subdir2.is_empty() {
format!("/sys/{basedir}/{subdir2}")
} else {
format!("/sys/{basedir}")
};
let mut add_syspaths: Vec<String> = Vec::new();
for dir_entry in fs::read_dir(path.as_str())
.map_err(|err| Error::UdevEnumerate(format!("unable to open {path} path: {err}")))?
{
let d_name = dir_entry?
.file_name()
.into_string()
.unwrap_or(String::new());
if d_name.is_empty() {
log::trace!("{LOG_PREFIX} empty/invalid entry");
} else if d_name.starts_with('.') {
log::trace!("{LOG_PREFIX} private entry");
} else if !self.match_sysname(d_name.as_str()) {
log::trace!("{LOG_PREFIX} no /sys name match");
} else {
let syspath = format!("{path}/{d_name}");
if let Ok(mut dev) =
UdevDevice::new_from_syspath(Arc::clone(&self.udev), syspath.as_str())
{
if self.match_is_initialized {
// From `libudev` documentation:
//
// ```
// All devices with a device node or network interfaces
// possibly need udev to adjust the device node permission
// or context, or rename the interface before it can be
// reliably used from other processes.
//
// For now, we can only check these types of devices, we
// might not store a database, and have no way to find out
// for all other types of devices.
// ```
if dev.get_is_initialized()
&& (util::major(dev.devnum()) > 0 || dev.get_ifindex() > 0)
{
break;
}
}
let dev_syspath = dev.syspath().to_owned();
if !self.match_parent(&dev) {
log::trace!("{LOG_PREFIX} no parent match");
break;
} else if !self.match_tag(&mut dev) {
log::trace!("{LOG_PREFIX} no tag match");
break;
} else if !self.match_property(&dev) {
log::trace!("{LOG_PREFIX} no property match");
break;
} else if !self.match_sysattr(&mut dev) {
log::trace!("{LOG_PREFIX} no /sys attribute match");
break;
} else {
add_syspaths.push(dev_syspath);
}
}
}
}
for syspath in add_syspaths.iter() {
self.add_syspath(syspath)?;
}
Ok(())
}
fn scan_dir(&mut self, basedir: &str, subdir: &str, subsystem: &str) -> Result<()> {
let path = format!("/sys/{basedir}");
for dir_entry in fs::read_dir(path.as_str())
.map_err(|err| Error::UdevEnumerate(format!("unable to open {path} path: {err}")))?
{
let d_name = dir_entry?
.file_name()
.into_string()
.unwrap_or(String::new());
if d_name.is_empty() {
log::trace!("{LOG_PREFIX} empty/invalid entry");
} else if d_name.starts_with('.') {
log::trace!("{LOG_PREFIX} private entry");
} else if !self.match_sysname(if subsystem.is_empty() {
d_name.as_str()
} else {
subsystem
}) {
log::trace!("{LOG_PREFIX} no /sys subsystem name match");
} else {
self.scan_dir_and_add_devices(basedir, d_name.as_str(), subdir)?;
}
}
Ok(())
}
}
impl UdevDevice {
pub(crate) fn match_sysattr_value(&mut self, sysattr: &str, match_val: &str) -> bool {
match self.get_sysattr_value(sysattr) {
Some(val) => {
if match_val.is_empty() {
true
} else if let Ok(pattern) = glob::Pattern::new(match_val) {
pattern.matches(val.as_str())
} else {
false
}
}
None => false,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::UdevEntry;
#[test]
fn test_udev_enumerate() {
let udev = Arc::new(Udev::new());
let mut null_enum = UdevEnumerate::new(Arc::clone(&udev));
assert_eq!(null_enum.udev(), &udev);
assert!(null_enum.sysattr_match_list().is_empty());
assert!(null_enum.sysattr_nomatch_list().is_empty());
assert!(null_enum.subsystem_match_list().is_empty());
assert!(null_enum.subsystem_nomatch_list().is_empty());
assert!(null_enum.sysname_match_list().is_empty());
assert!(null_enum.properties_match_list().is_empty());
assert!(null_enum.tags_match_list().is_empty());
assert!(null_enum.devices_list().is_empty());
assert!(null_enum.parent().is_none());
assert!(null_enum.devices().is_empty());
assert_eq!(null_enum.devices_cur(), 0);
assert_eq!(null_enum.devices_max(), 0);
assert!(!null_enum.devices_uptodate());
assert!(!null_enum.match_is_initialized());
let exp_sysattr_match_list = [UdevEntry::new().with_name("test_sysattr_match_list")];
let exp_sysattr_nomatch_list = [UdevEntry::new().with_name("test_sysattr_nomatch_list")];
let exp_subsystem_match_list = [UdevEntry::new().with_name("test_subsystem_match_list")];
let exp_subsystem_nomatch_list =
[UdevEntry::new().with_name("test_subsystem_nomatch_list")];
let exp_sysname_match_list = [UdevEntry::new().with_name("test_sysname_match_list")];
let exp_properties_match_list = [UdevEntry::new().with_name("test_properties_match_list")];
let exp_tags_match_list = [UdevEntry::new().with_name("test_tags_match_list")];
let exp_devices_list = [UdevEntry::new().with_name("test_devices_list")];
let exp_parent = Arc::new(UdevDevice::new(Arc::clone(&udev)));
let exp_devices = [Syspath::new(), Syspath::new()];
let exp_devices_cur = 1;
let exp_devices_max = 4;
let exp_devices_uptodate = true;
let exp_match_is_initialized = true;
let exp_enum = UdevEnumerate::new(Arc::clone(&udev))
.with_sysattr_match_list(exp_sysattr_match_list.clone())
.with_sysattr_nomatch_list(exp_sysattr_nomatch_list.clone())
.with_subsystem_match_list(exp_subsystem_match_list.clone())
.with_subsystem_nomatch_list(exp_subsystem_nomatch_list.clone())
.with_sysname_match_list(exp_sysname_match_list.clone())
.with_properties_match_list(exp_properties_match_list.clone())
.with_tags_match_list(exp_tags_match_list.clone())
.with_devices_list(exp_devices_list.clone())
.with_parent(exp_parent.clone())
.with_devices(exp_devices.clone())
.with_devices_cur(exp_devices_cur)
.with_devices_max(exp_devices_max)
.with_devices_uptodate(exp_devices_uptodate)
.with_match_is_initialized(exp_match_is_initialized);
assert_eq!(exp_enum.udev(), &udev);
for iter in [
exp_enum
.sysattr_match_list()
.iter()
.zip(exp_sysattr_match_list.iter()),
exp_enum
.sysattr_nomatch_list()
.iter()
.zip(exp_sysattr_nomatch_list.iter()),
exp_enum
.subsystem_match_list()
.iter()
.zip(exp_subsystem_match_list.iter()),
exp_enum
.subsystem_nomatch_list()
.iter()
.zip(exp_subsystem_nomatch_list.iter()),
exp_enum
.sysname_match_list()
.iter()
.zip(exp_sysname_match_list.iter()),
exp_enum
.properties_match_list()
.iter()
.zip(exp_properties_match_list.iter()),
exp_enum
.tags_match_list()
.iter()
.zip(exp_tags_match_list.iter()),
exp_enum.devices_list().iter().zip(exp_devices_list.iter()),
] {
for (entry, exp_entry) in iter {
assert_eq!(entry, exp_entry);
}
}
assert_eq!(exp_enum.parent(), Some(&exp_parent));
assert_eq!(exp_enum.devices(), exp_devices.as_ref());
assert_eq!(exp_enum.devices_cur(), exp_devices_cur);
assert_eq!(exp_enum.devices_max(), exp_devices_max);
assert_eq!(exp_enum.devices_uptodate(), exp_devices_uptodate);
assert_eq!(exp_enum.match_is_initialized(), exp_match_is_initialized);
null_enum.set_sysattr_match_list(exp_sysattr_match_list.clone());