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
use std::fmt;
use std::ptr;
use std::ffi::OsStr;
use std::io::Result;
use std::ops::Deref;
use std::os::unix::io::{AsRawFd, RawFd};
#[cfg(feature = "mio")]
use mio::{event::Evented, unix::EventedFd, Poll, PollOpt, Ready, Token};
use Udev;
use {ffi, util};
use {AsRaw, AsRawWithContext, Device, FromRaw};
pub struct Builder {
udev: Udev,
monitor: *mut ffi::udev_monitor,
}
impl Clone for Builder {
fn clone(&self) -> Self {
Self {
udev: self.udev.clone(),
monitor: unsafe { ffi::udev_monitor_ref(self.monitor) },
}
}
}
impl Drop for Builder {
fn drop(&mut self) {
unsafe {
ffi::udev_monitor_unref(self.monitor);
}
}
}
as_ffi_with_context!(Builder, monitor, ffi::udev_monitor, ffi::udev_monitor_ref);
impl Builder {
pub fn new() -> Result<Self> {
Self::with_udev(Udev::new()?)
}
pub(crate) fn with_udev(udev: Udev) -> Result<Self> {
let name = b"udev\0".as_ptr() as *const libc::c_char;
let ptr = try_alloc!(unsafe { ffi::udev_monitor_new_from_netlink(udev.as_raw(), name) });
Ok(Self { udev, monitor: ptr })
}
pub fn match_subsystem<T: AsRef<OsStr>>(self, subsystem: T) -> Result<Self> {
let subsystem = util::os_str_to_cstring(subsystem)?;
util::errno_to_result(unsafe {
ffi::udev_monitor_filter_add_match_subsystem_devtype(
self.monitor,
subsystem.as_ptr(),
ptr::null(),
)
})
.and(Ok(self))
}
pub fn match_subsystem_devtype<T: AsRef<OsStr>, U: AsRef<OsStr>>(
self,
subsystem: T,
devtype: U,
) -> Result<Self> {
let subsystem = util::os_str_to_cstring(subsystem)?;
let devtype = util::os_str_to_cstring(devtype)?;
util::errno_to_result(unsafe {
ffi::udev_monitor_filter_add_match_subsystem_devtype(
self.monitor,
subsystem.as_ptr(),
devtype.as_ptr(),
)
})
.and(Ok(self))
}
pub fn match_tag<T: AsRef<OsStr>>(self, tag: T) -> Result<Self> {
let tag = util::os_str_to_cstring(tag)?;
util::errno_to_result(unsafe {
ffi::udev_monitor_filter_add_match_tag(self.monitor, tag.as_ptr())
})
.and(Ok(self))
}
pub fn clear_filters(self) -> Result<Self> {
util::errno_to_result(unsafe { ffi::udev_monitor_filter_remove(self.monitor) })
.and(Ok(self))
}
pub fn listen(self) -> Result<Socket> {
util::errno_to_result(unsafe { ffi::udev_monitor_enable_receiving(self.monitor) })?;
Ok(Socket { inner: self })
}
}
#[derive(Clone)]
pub struct Socket {
inner: Builder,
}
impl AsRaw<ffi::udev_monitor> for Socket {
fn as_raw(&self) -> *mut ffi::udev_monitor {
self.inner.monitor
}
fn into_raw(self) -> *mut ffi::udev_monitor {
self.inner.monitor
}
}
impl AsRawFd for Socket {
fn as_raw_fd(&self) -> RawFd {
unsafe { ffi::udev_monitor_get_fd(self.inner.monitor) }
}
}
impl Iterator for Socket {
type Item = Event;
fn next(&mut self) -> Option<Event> {
let ptr = unsafe { ffi::udev_monitor_receive_device(self.inner.monitor) };
if ptr.is_null() {
None
} else {
let device = Device::from_raw(self.inner.udev.clone(), ptr);
Some(Event { device })
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EventType {
Add,
Change,
Remove,
Bind,
Unbind,
Unknown,
}
impl Default for EventType {
fn default() -> Self {
EventType::Unknown
}
}
impl fmt::Display for EventType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(match *self {
EventType::Add => "add",
EventType::Change => "change",
EventType::Remove => "remove",
EventType::Bind => "bind",
EventType::Unbind => "unbind",
EventType::Unknown => "unknown",
})
}
}
pub struct Event {
device: Device,
}
impl Deref for Event {
type Target = Device;
fn deref(&self) -> &Device {
&self.device
}
}
impl Event {
pub fn event_type(&self) -> EventType {
let value = match self.device.property_value("ACTION") {
Some(s) => s.to_str(),
None => None,
};
match value {
Some("add") => EventType::Add,
Some("change") => EventType::Change,
Some("remove") => EventType::Remove,
Some("bind") => EventType::Bind,
Some("unbind") => EventType::Unbind,
_ => EventType::Unknown,
}
}
pub fn sequence_number(&self) -> u64 {
unsafe { ffi::udev_device_get_seqnum(self.device.as_raw()) as u64 }
}
pub fn device(&self) -> Device {
self.device.clone()
}
}
#[cfg(feature = "mio")]
impl Evented for Socket {
fn register(
&self,
poll: &Poll,
token: Token,
interest: Ready,
opts: PollOpt,
) -> std::io::Result<()> {
EventedFd(&self.as_raw_fd()).register(poll, token, interest, opts)
}
fn reregister(
&self,
poll: &Poll,
token: Token,
interest: Ready,
opts: PollOpt,
) -> std::io::Result<()> {
EventedFd(&self.as_raw_fd()).reregister(poll, token, interest, opts)
}
fn deregister(&self, poll: &Poll) -> std::io::Result<()> {
EventedFd(&self.as_raw_fd()).deregister(poll)
}
}