Debian Codegraph @

src/lib.rs

raw symbols SCIP
1 //! Pure Rust library for interacting with the [udev](https://www.kernel.org/doc/ols/2003/ols2003-pages-249-257.pdf) userspace `devfs`.
2 //!
3 //! Uses the [`libc`](https://crates.io/crates/libc) and [`nix`](https://crates.io/crates/nix) crate to make syscalls to Linux.
4
5 use std::sync::Arc;
6
7 #[macro_use]
8 extern crate bitflags;
9
10 mod context;
11 mod device;
12 mod enumerate;
13 mod error;
14 mod file;
15 mod hwdb;
16 mod list;
17 mod log;
18 mod mode;
19 mod monitor;
20 mod murmur_hash;
21 mod queue;
22 mod socket;
23 mod util;
24
25 pub use context::*;
26 pub use device::*;
27 pub use enumerate::*;
28 pub use error::*;
29 pub use file::*;
30 pub use hwdb::*;
31 pub use list::*;
32 pub use log::*;
33 pub use mode::*;
34 pub use monitor::*;
35 pub use murmur_hash::*;
36 pub use queue::*;
37 pub use socket::*;
38 pub use util::*;
39
40 /// Creates a new [Udev] context.
41 pub fn udev_new() -> Arc<Udev> {
42 Arc::new(Udev::new())
43 }
44
45 /// Gets the [LogPriority] for the [Udev] context.
46 pub fn udev_get_log_priority(udev: &Udev) -> LogPriority {
47 udev.log_priority()
48 }
49
50 /// Sets the [LogPriority] for the [Udev] context.
51 pub fn udev_set_log_priority(udev: &mut Udev, val: LogPriority) {
52 udev.set_log_priority(val);
53 }
54
55 /// Gets a reference to the next entry in a [UdevList].
56 ///
57 /// Breaks with the original `libudev` API by requiring a reference to the list, instead of a list
58 /// entry. This is because the C version uses a linked-list composed of pointers, we don't.
59 pub fn udev_list_entry_get_next(list: &UdevList) -> Option<&UdevEntry> {
60 list.next_entry()
61 }
62
63 /// Gets a mutable reference to the next entry in a [UdevList].
64 ///
65 /// Breaks with the original `libudev` API by requiring a reference to the list, instead of a list
66 /// entry. This is because the C version uses a linked-list composed of pointers, we don't.
67 pub fn udev_list_entry_get_next_mut(list: &mut UdevList) -> Option<&mut UdevEntry> {
68 list.next_entry_mut()
69 }
70
71 /// Gets the name of the [UdevEntry].
72 pub fn udev_list_entry_get_name(entry: &UdevEntry) -> &str {
73 entry.name()
74 }
75
76 /// Gets the value of the [UdevEntry].
77 pub fn udev_list_entry_get_value(entry: &UdevEntry) -> &str {
78 entry.value()
79 }
80
81 /// Helper function that iterates over every [UdevEntry] in the list, applying the function to each
82 /// entry.
83 ///
84 /// Breaks with the original `libudev` API by requiring a reference to the list, instead of a list
85 /// entry. This is because the C version uses a linked-list composed of pointers, we don't.
86 pub fn udev_list_entry_foreach(list: &UdevList, f: fn(&UdevEntry) -> Result<()>) -> Result<()> {
87 for entry in list.iter() {
88 f(entry)?;
89 }
90 Ok(())
91 }
92
93 /// Helper function that iterates over every [UdevEntry] in the list, applying the function to each
94 /// entry.
95 ///
96 /// Breaks with the original `libudev` API by requiring a reference to the list, instead of a list
97 /// entry. This is because the C version uses a linked-list composed of pointers, we don't.
98 pub fn udev_list_entry_foreach_mut(
99 list: &mut UdevList,
100 f: fn(&mut UdevEntry) -> Result<()>,
101 ) -> Result<()> {
102 for entry in list.iter_mut() {
103 f(entry)?;
104 }
105 Ok(())
106 }
107
108 /// Creates a new [UdevDevice] from the provided [Udev] context.
109 pub fn udev_device_new(udev: Arc<Udev>) -> UdevDevice {
110 UdevDevice::new(udev)
111 }
112
113 /// Gets a reference to the [Udev] context from an [UdevDevice].
114 pub fn udev_device_get_udev(device: &UdevDevice) -> &Udev {
115 device.udev()
116 }
117
118 /// Gets a cloned reference to the [Udev] context from an [UdevDevice].
119 pub fn udev_device_get_udev_cloned(device: &UdevDevice) -> Arc<Udev> {
120 device.udev_cloned()
121 }
122
123 /// Creates new [UdevDevice], and fills in information from the sys
124 /// device and the udev database entry.
125 ///
126 /// The `syspath` is the absolute path to the device, including the sys mount point.
127 ///
128 /// The initial refcount is 1, and needs to be decremented to release the resources of the udev device.
129 ///
130 /// Returns: a new [UdevDevice], or `Error`, if it does not exist
131 pub fn udev_device_new_from_syspath(udev: Arc<Udev>, syspath: &str) -> Result<UdevDevice> {
132 UdevDevice::new_from_syspath(udev, syspath)
133 }
134
135 /// Creates new [UdevDevice].
136 ///
137 /// From the `libudev` documentation:
138 ///
139 /// ```no_build,no_run
140 /// Fills in information from the `sys` device and the udev database entry.
141 ///
142 /// The device is looked-up by its major/minor number and type. Character and block device
143 /// numbers are not unique across the two types.
144 /// ```
145 ///
146 /// Parameters:
147 ///
148 /// - `udev`: [Udev] library context
149 /// - `type`: `char` or `block` device
150 /// - `devnum`: device major/minor number
151 ///
152 /// Returns: a new [UdevDevice], or `Err`, if it does not exist
153 pub fn udev_device_new_from_devnum(
154 udev: Arc<Udev>,
155 devtype: &str,
156 devnum: libc::dev_t,
157 ) -> Result<UdevDevice> {
158 UdevDevice::new_from_devnum(udev, devtype, devnum)
159 }
160
161 /// Creates a new [UdevDevice] from the subsystem and sysname.
162 ///
163 /// From the `libudev` documentation:
164 ///
165 /// ```no_build,no_run
166 /// Fills in information from the sys device and the udev database entry.
167 ///
168 /// The device is looked up by the subsystem and name string of the device, like "mem" / "zero", or "block" / "sda".
169 /// ```
170 ///
171 /// Parameters:
172 ///
173 /// - `udev`: [Udev] library context
174 /// - `subsystem`: the subsystem of the device
175 /// - `sysname`: the name of the device
176 ///
177 /// Returns: a new [UdevDevice], or `Err`, if it does not exist
178 pub fn udev_device_new_from_subsystem_sysname(
179 udev: Arc<Udev>,
180 subsystem: &str,
181 sysname: &str,
182 ) -> Result<UdevDevice> {
183 UdevDevice::new_from_subsystem_sysname(udev, subsystem, sysname)
184 }
185
186 /// Create new [UdevDevice] from an ID string.
187 ///
188 /// From the `libudev` documentation:
189 ///
190 /// ```no_build,no_run
191 ///
192 /// Fill in information from the sys device and the udev database entry.
193 ///
194 /// The device is looked-up by a special string:
195 ///
196 /// b8:2 - block device major:minor
197 /// c128:1 - char device major:minor
198 /// n3 - network device ifindex
199 /// +sound:card29 - kernel driver core subsystem:device name
200 /// ```
201 ///
202 /// Parameters:
203 ///
204 /// - `udev`: udev library context
205 /// - `id`: text string identifying a kernel device
206 ///
207 /// Returns: a new [UdevDevice], or `Err`, if it does not exist
208 pub fn udev_device_new_from_device_id(udev: Arc<Udev>, id: &str) -> Result<UdevDevice> {
209 UdevDevice::new_from_device_id(udev, id)
210 }
211
212 /// Create new udev device from the environment information.
213 ///
214 /// From the original `libudev` documnentation:
215 ///
216 /// ```no_build,no_run
217 /// Fills in information from the current process environment.
218 /// This only works reliable if the process is called from a udev rule.
219 /// It is usually used for tools executed from IMPORT= rules.
220 /// ```
221 ///
222 /// Parameters:
223 ///
224 /// - `udev`: [Udev] library context
225 ///
226 /// Returns: a new [UdevDevice], or `Err`, if it does not exist
227 pub fn udev_device_new_from_environment(udev: Arc<Udev>) -> Result<UdevDevice> {
228 UdevDevice::new_from_environment(udev)
229 }
230
231 /// Gets the next parent [UdevDevice].
232 ///
233 /// From the `libudev` documentation:
234 ///
235 /// ```no_build,no_run
236 /// Find the next parent device, and fill in information from the sys
237 /// device and the udev database entry.
238 ///
239 /// @udev_device: the device to start searching from
240 ///
241 /// Returned device is not referenced. It is attached to the child
242 /// device, and will be cleaned up when the child device is cleaned up.
243 ///
244 /// It is not necessarily just the upper level directory, empty or not
245 /// recognized sys directories are ignored.
246 ///
247 /// It can be called as many times as needed, without caring about
248 /// references.
249 /// ```
250 ///
251 /// Returns: a new [UdevDevice], or `Err`, if it no parent exists.
252 pub fn udev_device_get_parent(dev: &mut UdevDevice) -> Result<Arc<UdevDevice>> {
253 dev.get_parent()
254 }
255
256 /// Gets the next parent [UdevDevice] based on `subsystem` and `devtype`.
257 ///
258 /// From the `libudev` documentation:
259 ///
260 /// ```no_build,no_run
261 /// Find the next parent device, with a matching subsystem and devtype
262 /// value, and fill in information from the sys device and the udev
263 /// database entry.
264 ///
265 /// If devtype is #NULL, only subsystem is checked, and any devtype will
266 /// match.
267 ///
268 /// Returned device is not referenced. It is attached to the child
269 /// device, and will be cleaned up when the child device is cleaned up.
270 ///
271 /// It can be called as many times as needed, without caring about
272 /// references.
273 /// ```
274 ///
275 /// Parameters:
276 ///
277 /// - `udev_device`: udev device to start searching from
278 /// - `subsystem`: the subsystem of the device
279 /// - `devtype`: the type (DEVTYPE) of the device
280 ///
281 /// Returns: a new [UdevDevice], or `Err` if no matching parent exists.
282 pub fn udev_device_get_parent_with_subsystem_devtype(
283 dev: &mut UdevDevice,
284 subsystem: &str,
285 devtype: &str,
286 ) -> Result<Arc<UdevDevice>> {
287 dev.get_parent_with_subsystem_devtype(subsystem, devtype)
288 }
289
290 /// Reads [UdevDevice] information from the persistent database file.
291 ///
292 /// Returns: `Ok(())` on success, `Err(Error)` otherwise
293 pub fn udev_device_read_db(dev: &mut UdevDevice) -> Result<()> {
294 dev.read_db()
295 }
296
297 /// Gets the [UdevDevice] device path.
298 ///
299 /// From the `libudev` documentation:
300 ///
301 /// ```no_build,no_run
302 /// Retrieve the kernel devpath value of the udev device. The path
303 /// does not contain the sys mount point, and starts with a '/'.
304 /// ```
305 pub fn udev_device_get_devpath(dev: &UdevDevice) -> &str {
306 dev.devpath()
307 }
308
309 /// Gets the [UdevDevice] `syspath`.
310 ///
311 /// From the `libudev` documentation:
312 ///
313 /// ```no_build,no_run
314 /// Retrieve the sys path of the udev device. The path is an
315 /// absolute path and starts with the sys mount point.
316 /// ```
317 pub fn udev_device_get_syspath(dev: &UdevDevice) -> &str {
318 dev.syspath()
319 }
320
321 /// Gets the [UdevDevice] `sysname`.
322 ///
323 /// From the `libudev` documentation:
324 ///
325 /// ```no_build,no_run
326 /// Get the kernel device name in /sys.
327 /// ```
328 pub fn udev_device_get_sysname(dev: &UdevDevice) -> &str {
329 dev.sysname()
330 }
331
332 /// Gets the [UdevDevice] `sysnum`.
333 ///
334 /// From the `libudev` documentation:
335 ///
336 /// ```no_build,no_run
337 /// Get the instance number of the device.
338 /// ```
339 pub fn udev_device_get_sysnum(dev: &UdevDevice) -> &str {
340 dev.sysnum()
341 }
342
343 /// Gets the [UdevDevice] `devnode`.
344 ///
345 /// From the `libudev` documentation:
346 ///
347 /// ```no_build,no_run
348 /// Retrieve the device node file name belonging to the udev device.
349 /// The path is an absolute path, and starts with the device directory.
350 /// ```
351 ///
352 /// Returns: the device node file name of the [UdevDevice], or an empty string if none exists.
353 pub fn udev_device_get_devnode(dev: &mut UdevDevice) -> &str {
354 dev.get_devnode()
355 }
356
357 /// Gets whether the [UdevDevice] is initialized.
358 ///
359 /// From the `libudev` documentation:
360 ///
361 /// ```no_build,no_run
362 /// Check if udev has already handled the device and has set up
363 /// device node permissions and context, or has renamed a network
364 /// device.
365 ///
366 /// This is only implemented for devices with a device node
367 /// or network interfaces. All other devices return 1 here.
368 /// ```
369 pub fn udev_device_get_is_initialized(dev: &mut UdevDevice) -> bool {
370 dev.get_is_initialized()
371 }
372
373 /// Gets the list of device links for the [UdevDevice].
374 ///
375 /// From the `libudev` documentation:
376 ///
377 /// ```no_build,no_run
378 /// Retrieve the list of device links pointing to the device file of
379 /// the udev device. The next list entry can be retrieved with
380 /// udev_list_entry_get_next(), which returns #NULL if no more entries exist.
381 ///
382 /// The devlink path can be retrieved from the list entry by
383 /// udev_list_entry_get_name(). The path is an absolute path, and starts with
384 /// the device directory.
385 /// ```
386 ///
387 /// Returns: the first entry of the device node link list
388 pub fn udev_device_get_devlinks_list_entry(dev: &mut UdevDevice) -> Option<&UdevEntry> {
389 dev.get_devlinks_list_entry()
390 }
391
392 /// Gets the first tags list entry in the [UdevDevice].
393 ///
394 /// From the `libudev` documentation:
395 ///
396 /// ```no_build,no_run
397 /// Retrieve the list of tags attached to the udev device. The next
398 /// list entry can be retrieved with udev_list_entry_get_next(),
399 /// which returns `None` if no more entries exist. The tag string
400 /// can be retrieved from the list entry by udev_list_entry_get_name().
401 /// ```
402 ///
403 /// Returns: the first entry of the tag list
404 pub fn udev_device_get_tags_list_entry(dev: &mut UdevDevice) -> Option<&UdevEntry> {
405 dev.get_tags_list_entry()
406 }
407
408 /// Gets the current tags list entry in the [UdevDevice].
409 ///
410 /// From the `libudev` documentation:
411 ///
412 /// ```no_build,no_run
413 /// Retrieve the list of tags attached to the udev device. The next
414 /// list entry can be retrieved with udev_list_entry_get_next(),
415 /// which returns `None` if no more entries exist. The tag string
416 /// can be retrieved from the list entry by udev_list_entry_get_name().
417 /// ```
418 ///
419 /// Returns: the current entry of the tag list
420 pub fn udev_device_get_current_tags_list_entry(dev: &mut UdevDevice) -> Option<&UdevEntry> {
421 dev.get_current_tags_list_entry()
422 }
423
424 /// Gets the first entry in the `sysattr` properties list.
425 ///
426 /// From the `libudev` documentation:
427 ///
428 /// ```no_build,no_run
429 /// Retrieve the list of available sysattrs, with value being empty;
430 /// This just return all available sysfs attributes for a particular
431 /// device without reading their values.
432 /// ```
433 ///
434 /// Returns: the first entry of the property list
435 pub fn udev_device_get_sysattr_list_entry(dev: &mut UdevDevice) -> Option<&UdevEntry> {
436 dev.get_sysattr_list_entry()
437 }
438
439 /// Gets the value of a given property.
440 pub fn udev_device_get_property_value<'d>(dev: &'d UdevDevice, key: &str) -> Option<&'d str> {
441 dev.get_property_value(key)
442 }
443
444 /// Gets the kernel driver name.
445 ///
446 /// Returns: the kernel driver name, or `None` if none is attached.
447 pub fn udev_device_get_driver(dev: &mut UdevDevice) -> Option<&str> {
448 dev.get_driver()
449 }
450
451 /// Gets the device major/minor number.
452 pub fn udev_device_get_devnum(dev: &mut UdevDevice) -> u64 {
453 dev.get_devnum()
454 }
455
456 /// Gets the device action.
457 ///
458 /// From the `libudev` documentation:
459 ///
460 /// ```no_build,no_run
461 /// This is only valid if the device was received through a monitor. Devices read from
462 /// sys do not have an action string. Usual actions are: add, remove, change, online,
463 /// offline.
464 /// ```
465 ///
466 /// Returns the kernel action value, or `None` if there is no action value available.
467 pub fn udev_device_get_action(dev: &UdevDevice) -> &str {
468 dev.action()
469 }
470
471 /// Gets the device event sequence number.
472 ///
473 /// From the `libudev` documentation:
474 ///
475 /// ```no_build,no_run
476 /// This is only valid if the device was received through a monitor. Devices read from
477 /// sys do not have a sequence number.
478 /// ```
479 ///
480 /// Returns the kernel event sequence number, or zero if none is available.
481 pub const fn udev_device_get_seqnum(dev: &UdevDevice) -> u64 {
482 dev.seqnum()
483 }
484
485 /// Gets the number of microseconds since the [UdevDevice] was initialized.
486 ///
487 /// From the `libudev` documentation:
488 ///
489 /// ```no_build,no_run
490 /// Return the number of microseconds passed since udev set up the
491 /// device for the first time.
492 ///
493 /// This is only implemented for devices with need to store properties
494 /// in the udev database. All other devices return 0 here.
495 /// ```
496 ///
497 /// Returns: the number of microseconds since the device was first seen.
498 pub fn udev_device_get_usec_since_initialized(dev: &mut UdevDevice) -> u64 {
499 dev.get_usec_since_initialized()
500 }
501
502 /// Gets the sys attribute file value.
503 ///
504 /// From the `libudev` documentation:
505 ///
506 /// ```no_build,no_run
507 /// The retrieved value is cached in the device. Repeated calls will return the same
508 /// value and not open the attribute again.
509 /// ```
510 ///
511 /// Returns: the content of a sys attribute file, or `None` if there is no sys attribute value.
512 pub fn udev_device_get_sysattr_value(dev: &mut UdevDevice, sysattr: &str) -> Option<String> {
513 dev.get_sysattr_value(sysattr)
514 }
515
516 /// Gets whether the [UdevDevice] has the provided `tag` associated.
517 ///
518 /// From the `libudev` documentation:
519 ///
520 /// ```no_build,no_run
521 /// Check if a given device has a certain tag associated.
522 /// ```
523 pub fn udev_device_has_tag(dev: &mut UdevDevice, tag: &str) -> bool {
524 dev.has_tag(tag)
525 }
526
527 /// Gets whether the [UdevDevice] has the provided current `tag` associated.
528 ///
529 /// From the `libudev` documentation:
530 ///
531 /// ```no_build,no_run
532 /// Check if a given device has a certain tag associated.
533 /// ```
534 ///
535 /// TODO: `eudev` does not database does not support current tags, implement in this library.
536 pub fn udev_device_has_current_tag(dev: &mut UdevDevice, tag: &str) -> bool {
537 dev.has_tag(tag)
538 }
539
540 /// Creates a new [UdevMonitor] from the provided parameters.
541 ///
542 /// Parameters:
543 ///
544 /// `udev`: udev library context
545 /// `name`: name of event source
546 ///
547 /// From the `libudev` documentation:
548 ///
549 /// ```no_build,no_run
550 /// Create new udev monitor and connect to a specified event
551 /// source. Valid sources identifiers are "udev" and "kernel".
552 ///
553 /// Applications should usually not connect directly to the
554 /// "kernel" events, because the devices might not be usable
555 /// at that time, before `udev` has configured them, and created
556 /// device nodes. Accessing devices at the same time as `udev`,
557 /// might result in unpredictable behavior. The "`udev`" events
558 /// are sent out after `udev` has finished its event processing,
559 /// all rules have been processed, and needed device nodes are
560 /// created.
561 /// ```
562 ///
563 /// Returns: a new [UdevMonitor], or [Error], in case of an error
564 pub fn udev_monitor_new_from_netlink(udev: Arc<Udev>, name: &str) -> Result<Arc<UdevMonitor>> {
565 Ok(Arc::new(UdevMonitor::new_from_netlink(udev, name)?))
566 }
567
568 /// Gets the [Udev] context of the [UdevMonitor].
569 pub fn udev_monitor_get_udev(monitor: &UdevMonitor) -> &Arc<Udev> {
570 monitor.udev()
571 }
572
573 /// Binds the [UdevMonitor] socket to the event source.
574 pub fn udev_monitor_enable_receiving(monitor: &mut UdevMonitor) -> Result<()> {
575 monitor.enable_receiving()
576 }
577
578 /// Sets the size of the kernel socket buffer.
579 ///
580 /// From the `libudev` documentation:
581 ///
582 /// ```no_build,no_run
583 /// Set the size of the kernel socket buffer. This call needs the
584 /// appropriate privileges to succeed.
585 /// ```
586 ///
587 /// Returns: `Ok(())` on success, `Err(Error)` otherwise.
588 pub fn udev_monitor_set_receive_buffer_size(monitor: &mut UdevMonitor, size: usize) -> Result<()> {
589 monitor.set_receive_buffer_size(size)
590 }
591
592 /// Gets the [UdevMonitor] socket file descriptor.
593 pub fn udev_monitor_get_fd(monitor: &UdevMonitor) -> i32 {
594 monitor.sock()
595 }
596
597 /// Receives data from the [UdevMonitor] socket.
598 ///
599 /// From the `libudev` documentation:
600 ///
601 /// ```no_build,no_run
602 /// Receive data from the udev monitor socket, allocate a new udev
603 /// device, fill in the received data, and return the device.
604 ///
605 /// Only socket connections with uid=0 are accepted.
606 ///
607 /// The monitor socket is by default set to NONBLOCK. A variant of poll() on
608 /// the file descriptor returned by udev_monitor_get_fd() should to be used to
609 /// wake up when new devices arrive, or alternatively the file descriptor
610 /// switched into blocking mode.
611 /// ```
612 ///
613 /// Returns: `Ok(UdevDevice)` on success, `Err(Error)` otherwise.
614 pub fn udev_monitor_receive_device(monitor: &mut UdevMonitor) -> Result<UdevDevice> {
615 monitor.receive_device()
616 }
617
618 /// Adds an [UdevEntry] into the filter subsystem list.
619 ///
620 /// From `libudev` documentation:
621 ///
622 /// Parameters:
623 ///
624 /// - `subsystem`: the subsystem value to match the incoming devices against
625 /// - must be non-empty
626 /// - `devtype`: the devtype value to match the incoming devices against
627 ///
628 /// ```no_build,no_run
629 /// This filter is efficiently executed inside the kernel, and libudev subscribers
630 /// will usually not be woken up for devices which do not match.
631 ///
632 /// The filter must be installed before the monitor is switched to listening mode.
633 /// ```
634 ///
635 /// Returns `Ok` on success, `Err` otherwise.
636 pub fn udev_monitor_filter_add_match_subsystem_devtype<'m>(
637 monitor: &'m mut UdevMonitor,
638 subsystem: &str,
639 devtype: &str,
640 ) -> Result<&'m UdevEntry> {
641 monitor.filter_add_match_subsystem_devtype(subsystem, devtype)
642 }
643
644 /// Adds an [UdevEntry] into the filter tag list.
645 ///
646 /// From `libudev` documentation:
647 ///
648 /// - `tag`: the name of a tag
649 /// - must be non-empty
650 ///
651 /// ```no_build,no_run
652 /// This filter is efficiently executed inside the kernel, and libudev subscribers
653 /// will usually not be woken up for devices which do not match.
654 ///
655 /// The filter must be installed before the monitor is switched to listening mode.
656 /// ```
657 ///
658 /// Returns `Ok` on success, `Err` otherwise.
659 pub fn udev_monitor_filter_add_match_tag<'m>(
660 monitor: &'m mut UdevMonitor,
661 tag: &str,
662 ) -> Result<&'m UdevEntry> {
663 monitor.filter_add_match_tag(tag)
664 }
665
666 /// Updates the monitor socket filter.
667 ///
668 /// From the `libudev` documentation:
669 ///
670 /// ```no_build,no_run
671 /// Update the installed socket filter. This is only needed,
672 /// if the filter was removed or changed.
673 /// ```
674 ///
675 /// Returns: `Ok(())` on success, `Err(Error)` otherwise.
676 pub fn udev_monitor_filter_update(monitor: &mut UdevMonitor) -> Result<()> {
677 monitor.filter_update()
678 }
679
680 /// Removes all filters from the [UdevMonitor].
681 ///
682 /// Returns `Ok(())` on success, `Err(Error)` otherwise.
683 pub fn udev_monitor_filter_remove(monitor: &mut UdevMonitor) -> Result<()> {
684 monitor.filter_remove()
685 }
686
687 /// Creates a new [UdevEnumerate].
688 pub fn udev_enumerate_new(udev: Arc<Udev>) -> Arc<UdevEnumerate> {
689 Arc::new(UdevEnumerate::new(udev))
690 }
691
692 /// Adds an entry to the match subsystem [UdevEntry] list.
693 ///
694 /// From the `libudev` documentation:
695 ///
696 /// ```no_build,no_run
697 /// Match only devices belonging to a certain kernel subsystem.
698 /// ```
699 ///
700 /// Returns `Ok(UdevEntry)` on success, `Err(Error)` otherwise.
701 pub fn udev_enumerate_add_match_subsystem<'e>(
702 enumerate: &'e mut UdevEnumerate,
703 subsystem: &str,
704 ) -> Result<&'e UdevEntry> {
705 enumerate.add_match_subsystem(subsystem)
706 }
707
708 /// Adds an entry to the no-match subsystem [UdevEntry] list.
709 ///
710 /// From the `libudev` documentation:
711 ///
712 /// ```no_build,no_run
713 /// Match only devices not belonging to a certain kernel subsystem.
714 /// ```
715 ///
716 /// Returns `Ok(UdevEntry)` on success, `Err(Error)` otherwise.
717 pub fn udev_enumerate_add_nomatch_subsystem<'e>(
718 enumerate: &'e mut UdevEnumerate,
719 subsystem: &str,
720 ) -> Result<&'e UdevEntry> {
721 enumerate.add_nomatch_subsystem(subsystem)
722 }
723
724 /// Adds an entry to the match sysattr [UdevEntry] list.
725 ///
726 /// From the `libudev` documentation:
727 ///
728 /// ```no_build,no_run
729 /// Match only devices with a given /sys device attribute.
730 /// ```
731 ///
732 /// Returns `Ok(UdevEntry)` on success, `Err(Error)` otherwise.
733 pub fn udev_enumerate_add_match_sysattr<'e>(
734 enumerate: &'e mut UdevEnumerate,
735 sysattr: &str,
736 ) -> Result<&'e UdevEntry> {
737 enumerate.add_match_sysattr(sysattr)
738 }
739
740 /// Adds an entry to the no-match sysattr [UdevEntry] list.
741 ///
742 /// From the `libudev` documentation:
743 ///
744 /// ```no_build,no_run
745 /// Match only devices without a given /sys device attribute.
746 /// ```
747 ///
748 /// Returns `Ok(UdevEntry)` on success, `Err(Error)` otherwise.
749 pub fn udev_enumerate_add_nomatch_sysattr<'e>(
750 enumerate: &'e mut UdevEnumerate,
751 sysattr: &str,
752 ) -> Result<&'e UdevEntry> {
753 enumerate.add_nomatch_sysattr(sysattr)
754 }
755
756 /// Adds an entry to the match properties [UdevEntry] list.
757 ///
758 /// From the `libudev` documentation:
759 ///
760 /// ```no_build,no_run
761 /// Match only devices with a certain property.
762 /// ```
763 ///
764 /// Returns `Ok(UdevEntry)` on success, `Err(Error)` otherwise.
765 pub fn udev_enumerate_add_match_property<'e>(
766 enumerate: &'e mut UdevEnumerate,
767 property: &str,
768 value: &str,
769 ) -> Result<&'e UdevEntry> {
770 enumerate.add_match_property(property, value)
771 }
772
773 /// Adds an entry to the match sysname [UdevEntry] list.
774 ///
775 /// From the `libudev` documentation:
776 ///
777 /// ```no_build,no_run
778 /// Match only devices with a given /sys device name.
779 /// ```
780 ///
781 /// Returns `Ok(UdevEntry)` on success, `Err(Error)` otherwise.
782 pub fn udev_enumerate_add_match_sysname<'e>(
783 enumerate: &'e mut UdevEnumerate,
784 sysname: &str,
785 ) -> Result<&'e UdevEntry> {
786 enumerate.add_match_sysname(sysname)
787 }
788
789 /// Adds an entry to the match tag [UdevEntry] list.
790 ///
791 /// From the `libudev` documentation:
792 ///
793 /// ```no_build,no_run
794 /// Match only devices with a certain tag.
795 /// ```
796 ///
797 /// Returns `Ok(UdevEntry)` on success, `Err(Error)` otherwise.
798 pub fn udev_enumerate_add_match_tag<'e>(
799 enumerate: &'e mut UdevEnumerate,
800 tag: &str,
801 ) -> Result<&'e UdevEntry> {
802 enumerate.add_match_tag(tag)
803 }
804
805 /// Sets the parent [UdevDevice] on a given device tree.
806 ///
807 /// From the `libudev` documentation:
808 ///
809 /// ```no_build,no_run
810 /// Return the devices on the subtree of one given device. The parent
811 /// itself is included in the list.
812 ///
813 /// A reference for the device is held until the udev_enumerate context
814 /// is cleaned up.
815 /// ```
816 ///
817 /// Returns: `Ok(())` on success, `Err(Error)` otherwise.
818 pub fn udev_enumerate_add_match_parent(
819 enumerate: &mut UdevEnumerate,
820 dev: Arc<UdevDevice>,
821 ) -> Result<()> {
822 enumerate.set_parent(dev);
823 Ok(())
824 }
825
826 /// Sets whether the match lists are initialized.
827 ///
828 /// From the `libudev` documentation:
829 ///
830 /// ```no_build,no_run
831 /// Match only devices which udev has set up already. This makes
832 /// sure, that the device node permissions and context are properly set
833 /// and that network devices are fully renamed.
834 ///
835 /// Usually, devices which are found in the kernel but not already
836 /// handled by udev, have still pending events. Services should subscribe
837 /// to monitor events and wait for these devices to become ready, instead
838 /// of using uninitialized devices.
839 ///
840 /// For now, this will not affect devices which do not have a device node
841 /// and are not network interfaces.
842 /// ```
843 ///
844 /// Returns: `Ok(())` on success, `Err(Error)` otherwise.
845 pub fn udev_enumerate_add_match_is_initialized(
846 enumerate: &mut UdevEnumerate,
847 val: bool,
848 ) -> Result<()> {
849 enumerate.set_match_is_initialized(val);
850 Ok(())
851 }
852
853 /// Adds a devices to the list of devices.
854 ///
855 /// From the `libudev` documentation:
856 ///
857 /// ```no_build,no_run
858 /// Add a device to the list of devices, to retrieve it back sorted in dependency order.
859 /// ```
860 ///
861 /// Returns: `Ok(())` on success, `Err(Error)` otherwise.
862 pub fn udev_enumerate_add_syspath(enumerate: &mut UdevEnumerate, syspath: &str) -> Result<()> {
863 enumerate.add_syspath(syspath)
864 }
865
866 /// Scan `/sys` for devices which match the given filters.
867 ///
868 /// From the `libudev` documentation:
869 ///
870 /// ```no_build,no_run
871 /// Scan /sys for all devices which match the given filters. No matches
872 /// will return all currently available devices.
873 /// ```
874 ///
875 /// Returns: `Ok(())` on success, `Err(Error)` otherwise.
876 pub fn udev_enumerate_scan_devices(enumerate: &mut UdevEnumerate) -> Result<()> {
877 enumerate.scan_devices()
878 }
879
880 /// Scans `/sys` for all kernel subsystems.
881 ///
882 /// From `libudev` documentation:
883 ///
884 /// ```no_build,no_run
885 /// Scan /sys for all kernel subsystems, including buses, classes, drivers.
886 /// ```
887 ///
888 /// Returns: `Ok(())` on success, `Err(Error)` otherwise.
889 pub fn udev_enumerate_scan_subsystems(enumerate: &mut UdevEnumerate) -> Result<()> {
890 enumerate.scan_subsystems()
891 }
892
893 /// Creates a new [UdevQueue].
894 pub fn udev_queue_new(udev: Arc<Udev>) -> Arc<UdevQueue> {
895 Arc::new(UdevQueue::new(udev))
896 }
897
898 /// Gets a reference to the [Udev] context.
899 pub fn udev_queue_get_udev(queue: &UdevQueue) -> &Arc<Udev> {
900 queue.udev()
901 }
902
903 /// Checks if [Udev] is active on the system.
904 pub fn udev_queue_get_udev_is_active(queue: &UdevQueue) -> bool {
905 queue.udev_is_active()
906 }
907
908 /// Gets whether [UdevQueue] is currently processing any events.
909 pub fn udev_queue_get_queue_is_empty(queue: &UdevQueue) -> bool {
910 queue.queue_is_empty()
911 }
912
913 /// Gets a file descriptor to watch for a queue to become empty.
914 pub fn udev_queue_get_fd(queue: &mut UdevQueue) -> Result<i32> {
915 queue.get_fd()
916 }
917
918 /// Clears the watched file descriptor for queue changes.
919 ///
920 /// # Safety
921 ///
922 /// Users must ensure that every [UdevQueue] has a unique file descriptor, if the descriptor is
923 /// non-negative.
924 ///
925 /// Returns: `Ok(())` on success, `Err(Error)` otherwise.
926 pub fn udev_queue_flush(queue: &mut UdevQueue) -> Result<()> {
927 queue.flush()
928 }
929
930 /// Creates a new [UdevHwdb].
931 ///
932 /// From the `libudev` documentation:
933 ///
934 /// ```no_build,no_run
935 /// Provides access to the static hardware properties database; the database to
936 /// use can be overriden by setting the UDEV_HWDB_BIN environment
937 /// variable to its file name.
938 /// ```
939 pub fn udev_hwdb_new(udev: Arc<Udev>) -> Result<Arc<UdevHwdb>> {
940 Ok(Arc::new(UdevHwdb::new(udev)?))
941 }
942
943 /// Looks up a matching device in the hardware database.
944 ///
945 /// Parameters:
946 ///
947 /// - `modalias`: modalias string
948 /// - `flags`: (unused), preserved for easier mapping to `libudev` C API
949 ///
950 /// From the `libudev` documentation:
951 ///
952 /// ```no_build,no_run
953 /// The lookup key is a `modalias` string, whose formats are defined for the Linux kernel modules.
954 /// Examples are: pci:v00008086d00001C2D*, usb:v04F2pB221*. The first entry
955 /// of a list of retrieved properties is returned.
956 /// ```
957 ///
958 /// Returns: an optional reference to an [UdevEntry].
959 pub fn udev_hwdb_get_properties_list_entry<'h>(
960 hwdb: &'h mut UdevHwdb,
961 modalias: &str,
962 flags: u32,
963 ) -> Option<&'h UdevEntry> {
964 hwdb.get_properties_list_entry(modalias, flags)
965 }
966
967 /// Looks up a matching device modalias in the hardware database and returns the list of properties.
968 pub fn udev_hwdb_query<'h>(hwdb: &'h mut UdevHwdb, modalias: &str) -> Option<&'h UdevList> {
969 // populate list if modalias is present and return
970 hwdb.query(modalias)
971 }
972
973 /// Looks up a specific matching property name (key) for device modalias
974 ///
975 /// ```no_run
976 /// use std::sync::Arc;
977 /// use udevrs::{Udev, UdevHwdb};
978 /// let udev = Arc::new(Udev::new());
979 ///
980 /// let mut hwdb = UdevHwdb::new(udev).unwrap();
981 /// let query = udevrs::udev_hwdb_query_one(&mut hwdb, "usb:v1D6Bp0001", "ID_VENDOR_FROM_DATABASE");
982 /// assert_eq!(query, Some("Linux Foundation"));
983 /// ```
984 pub fn udev_hwdb_query_one<'h>(
985 hwdb: &'h mut UdevHwdb,
986 modalias: &str,
987 name: &str,
988 ) -> Option<&'h str> {
989 hwdb.query(modalias)
990 .and_then(|list| list.iter().find(|e| e.name() == name).map(|e| e.value()))
991 }
992
993 /// Encodes provided string, removing potentially unsafe characters.
994 ///
995 /// From the `libudev` documentation:
996 ///
997 /// ```no_build,no_run
998 /// Encode all potentially unsafe characters of a string to the
999 /// corresponding 2 char hex value prefixed by '\x'.
1000 /// ```
1001 ///
1002 /// Returns: `Ok(String)` on success, `Err(Error)` otherwise
1003 pub fn udev_util_encode_string(arg: &str) -> Result<String> {
1004 util::encode_string(arg)
1005 }