Debian Codegraph @

third_party/rust/authenticator/src/transport/netbsd/monitor.rs

raw
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 use crate::transport::device_selector::DeviceSelectorEvent;
6 use crate::transport::platform::fd::Fd;
7 use runloop::RunLoop;
8 use std::collections::HashMap;
9 use std::error::Error;
10 use std::ffi::OsString;
11 use std::sync::{mpsc::Sender, Arc};
12 use std::thread;
13 use std::time::Duration;
14
15 // XXX Should use drvctl, but it doesn't do pubsub properly yet so
16 // DRVGETEVENT requires write access to /dev/drvctl. Instead, for now,
17 // just poll every 500ms.
18 const POLL_TIMEOUT: u64 = 500;
19
20 #[derive(Debug)]
21 pub struct WrappedOpenDevice {
22 pub fd: Fd,
23 pub os_path: OsString,
24 }
25
26 pub struct Monitor<F>
27 where
28 F: Fn(
29 WrappedOpenDevice,
30 Sender<DeviceSelectorEvent>,
31 Sender<crate::StatusUpdate>,
32 &dyn Fn() -> bool,
33 ) + Sync,
34 {
35 runloops: HashMap<OsString, RunLoop>,
36 new_device_cb: Arc<F>,
37 selector_sender: Sender<DeviceSelectorEvent>,
38 status_sender: Sender<crate::StatusUpdate>,
39 }
40
41 impl<F> Monitor<F>
42 where
43 F: Fn(
44 WrappedOpenDevice,
45 Sender<DeviceSelectorEvent>,
46 Sender<crate::StatusUpdate>,
47 &dyn Fn() -> bool,
48 ) + Send
49 + Sync
50 + 'static,
51 {
52 pub fn new(
53 new_device_cb: F,
54 selector_sender: Sender<DeviceSelectorEvent>,
55 status_sender: Sender<crate::StatusUpdate>,
56 ) -> Self {
57 Self {
58 runloops: HashMap::new(),
59 new_device_cb: Arc::new(new_device_cb),
60 selector_sender,
61 status_sender,
62 }
63 }
64
65 pub fn run(&mut self, alive: &dyn Fn() -> bool) -> Result<(), Box<dyn Error>> {
66 // Loop until we're stopped by the controlling thread, or fail.
67 while alive() {
68 for n in 0..100 {
69 let uhidpath = OsString::from(format!("/dev/uhid{n}"));
70 match Fd::open(&uhidpath, libc::O_RDWR | libc::O_CLOEXEC) {
71 Ok(uhid) => {
72 // The device is available if it can be opened.
73 let _ = self
74 .selector_sender
75 .send(DeviceSelectorEvent::DevicesAdded(vec![uhidpath.clone()]));
76 self.add_device(WrappedOpenDevice {
77 fd: uhid,
78 os_path: uhidpath,
79 });
80 }
81 Err(ref err) => match err.raw_os_error() {
82 Some(libc::EBUSY) => continue,
83 Some(libc::ENOENT) => break,
84 _ => self.remove_device(uhidpath),
85 },
86 }
87 }
88 thread::sleep(Duration::from_millis(POLL_TIMEOUT));
89 }
90
91 // Remove all tracked devices.
92 self.remove_all_devices();
93
94 Ok(())
95 }
96
97 fn add_device(&mut self, fido: WrappedOpenDevice) {
98 let f = self.new_device_cb.clone();
99 let selector_sender = self.selector_sender.clone();
100 let status_sender = self.status_sender.clone();
101 let key = fido.os_path.clone();
102 debug!("Adding device {}", key.to_string_lossy());
103
104 let runloop = RunLoop::new(move |alive| {
105 if alive() {
106 f(fido, selector_sender, status_sender, alive);
107 }
108 });
109
110 if let Ok(runloop) = runloop {
111 self.runloops.insert(key, runloop);
112 }
113 }
114
115 fn remove_device(&mut self, path: OsString) {
116 let _ = self
117 .selector_sender
118 .send(DeviceSelectorEvent::DeviceRemoved(path.clone()));
119
120 debug!("Removing device {}", path.to_string_lossy());
121 if let Some(runloop) = self.runloops.remove(&path) {
122 runloop.cancel();
123 }
124 }
125
126 fn remove_all_devices(&mut self) {
127 while !self.runloops.is_empty() {
128 let path = self.runloops.keys().next().unwrap().clone();
129 self.remove_device(path);
130 }
131 }
132 }