libvirt-hotplug-usb 0.1.0

A daemon that watches Linux USB hot-plug events and passes matching devices through to a libvirt guest.
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
// SPDX-License-Identifier: GPL-3.0-or-later

// Copyright (C) 2026  Forest Crossman <cyrozap@gmail.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

//! Matching rules and their CLI grammar.
//!
//! Grammar:
//! ```text
//! rule      := criterion (',' criterion)*
//! criterion := 'id=' (HEX ':' | ':' HEX | HEX ':' HEX)
//!            | 'serial=' STR
//!            | 'bus=' (DEC | PATH)
//!            | 'port=' (DEC | DEC ('.' DEC)*)
//!            | 'hub(' rule ')'
//! ```
//! Criteria within a rule are ANDed; top-level rules are ORed.

use std::path::{Path, PathBuf};

use crate::error::{Error, Result};
use crate::usb::{DeviceSource, UsbDevice};

/// How a bus criterion identifies a bus.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum BusSpec {
    Number(u32),
    /// A sysfs path to a bus/root hub, resolved to a number before matching.
    Path(PathBuf),
}

/// How a port criterion identifies a port.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum PortSpec {
    /// A top-level port number; matches its whole subtree.
    Number(u32),
    /// An explicit port chain; matches that subtree.
    Path(Vec<u32>),
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Criterion {
    Id { vid: Option<u16>, pid: Option<u16> },
    Serial(String),
    Bus(BusSpec),
    Port(PortSpec),
    Hub(Box<Rule>),
}

#[derive(Clone, Debug, PartialEq, Eq, Default)]
pub struct Rule {
    pub criteria: Vec<Criterion>,
}

impl Rule {
    /// True if the device satisfies every criterion.
    pub fn matches(&self, dev: &UsbDevice, src: &dyn DeviceSource) -> bool {
        self.criteria.iter().all(|c| c.matches(dev, src))
    }
}

impl Criterion {
    fn matches(&self, dev: &UsbDevice, src: &dyn DeviceSource) -> bool {
        match self {
            Criterion::Id { vid, pid } => {
                vid.is_none_or(|v| v == dev.vid) && pid.is_none_or(|p| p == dev.pid)
            }
            Criterion::Serial(s) => dev.serial.as_deref() == Some(s.as_str()),
            Criterion::Bus(BusSpec::Number(n)) => dev.bus == *n,
            // Unresolved paths never match; `resolve_bus_paths` should have run.
            Criterion::Bus(BusSpec::Path(_)) => false,
            Criterion::Port(PortSpec::Number(n)) => dev.ports.first() == Some(n),
            Criterion::Port(PortSpec::Path(p)) => !p.is_empty() && dev.ports.starts_with(p),
            Criterion::Hub(inner) => dev
                .ancestors()
                .iter()
                .filter_map(|name| src.lookup(name))
                .any(|ancestor| ancestor.is_hub() && inner.matches(&ancestor, src)),
        }
    }
}

/// Resolve every `bus=PATH` criterion to a bus number.
///
/// Called once at startup; afterwards the matcher only sees `BusSpec::Number`.
pub fn resolve_bus_paths(rules: &mut [Rule]) -> Result<()> {
    for rule in rules {
        resolve_rule(rule)?;
    }
    Ok(())
}

fn resolve_rule(rule: &mut Rule) -> Result<()> {
    for criterion in &mut rule.criteria {
        match criterion {
            Criterion::Bus(BusSpec::Path(path)) => {
                let bus = resolve_bus_path(path)?;
                *criterion = Criterion::Bus(BusSpec::Number(bus));
            }
            Criterion::Hub(inner) => resolve_rule(inner)?,
            _ => {}
        }
    }
    Ok(())
}

fn resolve_bus_path(path: &Path) -> Result<u32> {
    let canon = std::fs::canonicalize(path)
        .map_err(|e| Error::usb(format!("cannot resolve bus path {}: {e}", path.display())))?;
    if let Ok(text) = std::fs::read_to_string(canon.join("busnum"))
        && let Ok(n) = text.trim().parse::<u32>()
    {
        return Ok(n);
    }
    // Fall back to a `usbN` path component.
    for component in canon.components() {
        if let std::path::Component::Normal(os) = component
            && let Some(n) = os
                .to_str()
                .and_then(|s| s.strip_prefix("usb"))
                .and_then(|s| s.parse::<u32>().ok())
        {
            return Ok(n);
        }
    }
    Err(Error::usb(format!(
        "no bus number found at {}",
        canon.display()
    )))
}

/// Parse a single rule expression.
pub fn parse_rule(input: &str) -> Result<Rule> {
    let mut parser = RuleParser {
        bytes: input.as_bytes(),
        pos: 0,
    };
    let rule = parser.parse_rule()?;
    parser.skip_ws();
    if parser.pos != parser.bytes.len() {
        return Err(Error::parse(format!(
            "unexpected trailing input at position {} in {input:?}",
            parser.pos
        )));
    }
    Ok(rule)
}

struct RuleParser<'a> {
    bytes: &'a [u8],
    pos: usize,
}

impl RuleParser<'_> {
    fn peek(&self) -> Option<u8> {
        self.bytes.get(self.pos).copied()
    }

    fn skip_ws(&mut self) {
        while matches!(self.peek(), Some(b' ') | Some(b'\t')) {
            self.pos += 1;
        }
    }

    fn starts_with(&self, pat: &str) -> bool {
        self.bytes[self.pos..].starts_with(pat.as_bytes())
    }

    fn parse_rule(&mut self) -> Result<Rule> {
        let mut criteria = vec![self.parse_criterion()?];
        loop {
            self.skip_ws();
            if self.peek() == Some(b',') {
                self.pos += 1;
                criteria.push(self.parse_criterion()?);
            } else {
                break;
            }
        }
        Ok(Rule { criteria })
    }

    fn parse_criterion(&mut self) -> Result<Criterion> {
        self.skip_ws();
        if self.starts_with("hub(") {
            self.pos += 4;
            let inner = self.parse_rule()?;
            self.skip_ws();
            if self.peek() != Some(b')') {
                return Err(Error::parse("expected ')' to close hub(...)"));
            }
            self.pos += 1;
            return Ok(Criterion::Hub(Box::new(inner)));
        }
        if self.starts_with("id=") {
            self.pos += 3;
            return self.parse_id();
        }
        if self.starts_with("serial=") {
            self.pos += 7;
            let value = self.parse_value();
            if value.is_empty() {
                return Err(Error::parse("serial= requires a value"));
            }
            return Ok(Criterion::Serial(value));
        }
        if self.starts_with("bus=") {
            self.pos += 4;
            let value = self.parse_value();
            return parse_bus(&value);
        }
        if self.starts_with("port=") {
            self.pos += 5;
            let value = self.parse_value();
            return parse_port(&value);
        }
        Err(Error::parse(format!(
            "expected a criterion (id=, serial=, bus=, port=, hub(...)) at position {}",
            self.pos
        )))
    }

    /// Read until `,`, `)` or end of input.
    fn parse_value(&mut self) -> String {
        let start = self.pos;
        while let Some(c) = self.peek() {
            if c == b',' || c == b')' {
                break;
            }
            self.pos += 1;
        }
        String::from_utf8_lossy(&self.bytes[start..self.pos])
            .trim()
            .to_string()
    }

    fn parse_id(&mut self) -> Result<Criterion> {
        let value = self.parse_value();
        let (vid_s, pid_s) = value
            .split_once(':')
            .ok_or_else(|| Error::parse(format!("id= requires a ':' (got {value:?})")))?;
        if pid_s.contains(':') {
            return Err(Error::parse(format!(
                "id= has too many ':' (got {value:?})"
            )));
        }
        let vid = if vid_s.is_empty() {
            None
        } else {
            Some(parse_hex(vid_s, "VID")?)
        };
        let pid = if pid_s.is_empty() {
            None
        } else {
            Some(parse_hex(pid_s, "PID")?)
        };
        if vid.is_none() && pid.is_none() {
            return Err(Error::parse("id=: matches neither VID nor PID"));
        }
        Ok(Criterion::Id { vid, pid })
    }
}

fn parse_hex(value: &str, what: &str) -> Result<u16> {
    let t = value
        .strip_prefix("0x")
        .or_else(|| value.strip_prefix("0X"))
        .unwrap_or(value);
    if t.is_empty() {
        return Err(Error::parse(format!("{what} is empty")));
    }
    u16::from_str_radix(t, 16).map_err(|e| Error::parse(format!("invalid {what} {value:?}: {e}")))
}

fn parse_bus(value: &str) -> Result<Criterion> {
    if value.is_empty() {
        return Err(Error::parse("bus= requires a value"));
    }
    if value.starts_with('/') {
        return Ok(Criterion::Bus(BusSpec::Path(PathBuf::from(value))));
    }
    let n: u32 = value
        .parse()
        .map_err(|_| Error::parse(format!("invalid bus number {value:?}")))?;
    Ok(Criterion::Bus(BusSpec::Number(n)))
}

fn parse_port(value: &str) -> Result<Criterion> {
    if value.is_empty() {
        return Err(Error::parse("port= requires a value"));
    }
    if value.contains('.') {
        let mut ports = Vec::new();
        for part in value.split('.') {
            if part.is_empty() {
                return Err(Error::parse(format!("invalid port path {value:?}")));
            }
            let n: u32 = part
                .parse()
                .map_err(|_| Error::parse(format!("invalid port path {value:?}")))?;
            ports.push(n);
        }
        Ok(Criterion::Port(PortSpec::Path(ports)))
    } else {
        let n: u32 = value
            .parse()
            .map_err(|_| Error::parse(format!("invalid port number {value:?}")))?;
        Ok(Criterion::Port(PortSpec::Number(n)))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::usb::test_support::{MapSource, device};

    fn parse_ok(input: &str) -> Rule {
        parse_rule(input).expect("rule should parse")
    }

    #[test]
    fn parses_id_forms() {
        assert_eq!(
            parse_ok("id=1234:"),
            Rule {
                criteria: vec![Criterion::Id {
                    vid: Some(0x1234),
                    pid: None
                }]
            }
        );
        assert_eq!(
            parse_ok("id=:abcd"),
            Rule {
                criteria: vec![Criterion::Id {
                    vid: None,
                    pid: Some(0xabcd)
                }]
            }
        );
        assert_eq!(
            parse_ok("id=1234:abcd"),
            Rule {
                criteria: vec![Criterion::Id {
                    vid: Some(0x1234),
                    pid: Some(0xabcd)
                }]
            }
        );
        assert_eq!(
            parse_ok("id=0x1234:0XABCD"),
            Rule {
                criteria: vec![Criterion::Id {
                    vid: Some(0x1234),
                    pid: Some(0xabcd)
                }]
            }
        );
    }

    #[test]
    fn parses_bus_and_port() {
        assert_eq!(
            parse_ok("bus=3"),
            Rule {
                criteria: vec![Criterion::Bus(BusSpec::Number(3))]
            }
        );
        assert_eq!(
            parse_ok("bus=/sys/bus/usb/devices/usb3"),
            Rule {
                criteria: vec![Criterion::Bus(BusSpec::Path(PathBuf::from(
                    "/sys/bus/usb/devices/usb3"
                )))]
            }
        );
        assert_eq!(
            parse_ok("port=1"),
            Rule {
                criteria: vec![Criterion::Port(PortSpec::Number(1))]
            }
        );
        assert_eq!(
            parse_ok("port=1.4"),
            Rule {
                criteria: vec![Criterion::Port(PortSpec::Path(vec![1, 4]))]
            }
        );
    }

    #[test]
    fn parses_nested_hub_and_combinations() {
        let rule = parse_ok("hub(id=1234:abcd,bus=2),serial=12345678");
        assert_eq!(rule.criteria.len(), 2);
        match &rule.criteria[0] {
            Criterion::Hub(inner) => {
                assert_eq!(inner.criteria.len(), 2);
                assert!(matches!(
                    inner.criteria[0],
                    Criterion::Id {
                        vid: Some(0x1234),
                        pid: Some(0xabcd)
                    }
                ));
                assert!(matches!(
                    inner.criteria[1],
                    Criterion::Bus(BusSpec::Number(2))
                ));
            }
            other => panic!("expected hub, got {other:?}"),
        }
        assert!(matches!(rule.criteria[1], Criterion::Serial(ref s) if s == "12345678"));

        let nested = parse_ok("hub(id=1:,hub(id=:2))");
        assert!(matches!(nested.criteria[0], Criterion::Hub(_)));
    }

    #[test]
    fn rejects_bad_rules() {
        for bad in [
            "",
            "id=1234",
            "id=:",
            "id=zzzz:",
            "id=1234:zz",
            "id=1234:5:6",
            "bus=",
            "bus=abc",
            "port=",
            "port=1..2",
            "port=a.b",
            "serial=",
            "hub()",
            "hub(id=1234:",
            "id=1234:,junk",
            "nonsense=1",
        ] {
            assert!(parse_rule(bad).is_err(), "expected {bad:?} to be rejected");
        }
    }

    #[test]
    fn id_matching() {
        let mut dev = device("1-1", 0x1234, 0xabcd, 0);
        dev.serial = Some("SER".into());
        let src = MapSource::default();
        assert!(parse_ok("id=1234:").matches(&dev, &src));
        assert!(parse_ok("id=:abcd").matches(&dev, &src));
        assert!(parse_ok("id=1234:abcd").matches(&dev, &src));
        assert!(!parse_ok("id=9999:").matches(&dev, &src));
        assert!(!parse_ok("id=:9999").matches(&dev, &src));
        assert!(!parse_ok("id=1234:9999").matches(&dev, &src));
        assert!(parse_ok("serial=SER").matches(&dev, &src));
        assert!(!parse_ok("serial=OTHER").matches(&dev, &src));
    }

    #[test]
    fn bus_and_port_matching() {
        let src = MapSource::default();
        let direct = device("1-3", 1, 2, 0); // bus 1, port 3
        let deep = device("1-3.2", 1, 2, 0); // bus 1, port 3.2
        let other = device("1-4", 1, 2, 0); // bus 1, port 4
        let bus2 = device("2-1", 1, 2, 0); // bus 2

        assert!(parse_ok("bus=1").matches(&direct, &src));
        assert!(!parse_ok("bus=1").matches(&bus2, &src));
        // port=N matches the whole subtree under that top-level port
        assert!(parse_ok("port=3").matches(&direct, &src));
        assert!(parse_ok("port=3").matches(&deep, &src));
        assert!(!parse_ok("port=3").matches(&other, &src));
        // port=1.4 matches the exact port path and its subtree
        assert!(parse_ok("port=3.2").matches(&deep, &src));
        assert!(!parse_ok("port=3.2").matches(&direct, &src));
        // AND within a rule
        assert!(parse_ok("bus=1,port=3").matches(&deep, &src));
        assert!(!parse_ok("bus=2,port=3").matches(&deep, &src));
    }

    #[test]
    fn hub_matching_is_transitive() {
        let root = device("usb1", 0x1d6b, 0x0002, 0x09);
        let mut hub = device("1-1", 0x1234, 0xabcd, 0x09);
        hub.serial = Some("HUBSER".into());
        let mut hub2 = device("1-1.4", 0x5678, 0x0001, 0x09);
        hub2.serial = Some("HUB2".into());
        let leaf = device("1-1.4.2", 0xdead, 0xbeef, 0);
        let direct = device("1-7", 0xdead, 0xbeef, 0);

        let src = MapSource::new(vec![root, hub, hub2, leaf.clone(), direct.clone()]);

        // leaf is behind 1-1.4 which is behind 1-1 (transitive)
        assert!(parse_ok("hub(id=1234:abcd)").matches(&leaf, &src));
        assert!(parse_ok("hub(serial=HUBSER)").matches(&leaf, &src));
        assert!(parse_ok("hub(serial=HUB2)").matches(&leaf, &src));
        // nested hub: the matching hub must itself be behind a matching hub
        // (leaf -> 1-1.4 [HUB2] -> 1-1 [HUBSER] -> usb1)
        assert!(parse_ok("hub(serial=HUB2,hub(serial=HUBSER))").matches(&leaf, &src));
        assert!(!parse_ok("hub(serial=HUBSER,hub(serial=HUB2))").matches(&leaf, &src));
        assert!(!parse_ok("hub(id=1234:abcd)").matches(&direct, &src));
        // multiple criteria on the leaf
        assert!(parse_ok("id=dead:beef,hub(serial=HUB2)").matches(&leaf, &src));
        // hubs themselves can match too (caller decides not to attach them)
        assert!(parse_ok("id=1234:abcd").matches(&src.lookup("1-1").unwrap(), &src));
    }

    #[test]
    fn identical_devices_both_match() {
        let a = device("1-1", 0x1234, 0xabcd, 0);
        let b = device("1-2", 0x1234, 0xabcd, 0);
        let src = MapSource::default();
        let rule = parse_ok("id=1234:abcd");
        assert!(rule.matches(&a, &src));
        assert!(rule.matches(&b, &src));
    }
}