| 1 | //! Access to currently active events. |
| 2 | //! |
| 3 | //! The udev daemon processes events asynchronously. All events which do not have |
| 4 | //! interdependencies run in parallel. This exports the current state of the |
| 5 | //! event processing queue, and the current event sequence numbers from the kernel |
| 6 | //! and the udev daemon. |
| 7 | //! |
| 8 | //! From `libudev-queue` documentation. |
| 9 | |
| 10 | use std::io::{self, Write}; |
| 11 | use std::os::fd::FromRawFd; |
| 12 | use std::{ffi, fs, sync::Arc}; |
| 13 | |
| 14 | use crate::UDEV_ROOT_RUN; |
| 15 | use crate::{Error, Result, Udev, UdevEntryList, UdevList}; |
| 16 | |
| 17 | /// Represents the current event queue in the udev daemon. |
| 18 | #[repr(C)] |
| 19 | #[derive(Clone, Debug, Default, PartialEq)] |
| 20 | pub struct UdevQueue { |
| 21 | udev: Arc<Udev>, |
| 22 | queue_list: UdevList, |
| 23 | fd: i32, |
| 24 | } |
| 25 | |
| 26 | impl UdevQueue { |
| 27 | /// Creates a new [UdevQueue]. |
| 28 | pub fn new(udev: Arc<Udev>) -> Self { |
| 29 | let udev_arc = Arc::clone(&udev); |
| 30 | Self { |
| 31 | udev, |
| 32 | queue_list: UdevList::new(udev_arc), |
| 33 | fd: -1, |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | /// Creates a new [UdevQueue] from the provided parameters. |
| 38 | pub fn create<Q: Into<UdevEntryList>>(udev: Arc<Udev>, queue_list: Q, fd: i32) -> Self { |
| 39 | let udev_arc = Arc::clone(&udev); |
| 40 | Self { |
| 41 | udev, |
| 42 | queue_list: UdevList::create(udev_arc, queue_list.into()), |
| 43 | fd, |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | /// Gets a reference to the [Udev] context. |
| 48 | pub const fn udev(&self) -> &Arc<Udev> { |
| 49 | &self.udev |
| 50 | } |
| 51 | |
| 52 | /// Gets a reference to the queue list [UdevList]. |
| 53 | pub const fn queue_list(&self) -> &UdevList { |
| 54 | &self.queue_list |
| 55 | } |
| 56 | |
| 57 | /// Gets a mutable reference to the queue list [UdevList]. |
| 58 | pub fn queue_list_mut(&mut self) -> &mut UdevList { |
| 59 | &mut self.queue_list |
| 60 | } |
| 61 | |
| 62 | /// Sets the queue list [UdevEntryList]. |
| 63 | pub fn set_queue_list<Q: Into<UdevEntryList>>(&mut self, queue_list: Q) { |
| 64 | self.queue_list.set_list(queue_list); |
| 65 | } |
| 66 | |
| 67 | /// Builder function that sets the queue list [UdevEntryList]. |
| 68 | pub fn with_queue_list<Q: Into<UdevEntryList>>(mut self, queue_list: Q) -> Self { |
| 69 | self.set_queue_list(queue_list); |
| 70 | self |
| 71 | } |
| 72 | |
| 73 | /// Gets the length of the [UdevQueue]. |
| 74 | pub fn len(&self) -> usize { |
| 75 | self.queue_list.len() |
| 76 | } |
| 77 | |
| 78 | /// Gets whether the [UdevQueue] is empty. |
| 79 | pub fn is_empty(&self) -> bool { |
| 80 | self.queue_list.is_empty() |
| 81 | } |
| 82 | |
| 83 | /// Gets the [UdevQueue] file descriptor. |
| 84 | pub const fn fd(&self) -> i32 { |
| 85 | self.fd |
| 86 | } |
| 87 | |
| 88 | /// Sets the [UdevQueue] file descriptor. |
| 89 | pub fn set_fd(&mut self, val: i32) { |
| 90 | self.fd = val; |
| 91 | } |
| 92 | |
| 93 | /// Builder function that sets the [UdevQueue] file descriptor. |
| 94 | pub fn with_fd(mut self, val: i32) -> Self { |
| 95 | self.set_fd(val); |
| 96 | self |
| 97 | } |
| 98 | |
| 99 | /// Gets a file descriptor to watch for a queue to become empty. |
| 100 | pub fn get_fd(&mut self) -> Result<i32> { |
| 101 | if self.fd < 0 { |
| 102 | // SAFETY: the argument is valid, and the return value is checked before use. |
| 103 | let fd = unsafe { libc::inotify_init1(libc::IN_CLOEXEC) }; |
| 104 | if fd < 0 { |
| 105 | let errno = io::Error::last_os_error(); |
| 106 | let err_msg = |
| 107 | format!("unable to init inotify monitor, error: {fd}, errno: {errno}"); |
| 108 | log::error!("{err_msg}"); |
| 109 | Err(Error::UdevQueue(err_msg)) |
| 110 | } else { |
| 111 | let udev_path = ffi::CString::new(format!("{UDEV_ROOT_RUN}/udev"))?; |
| 112 | // SAFETY: arguments are valid, and pointers reference valid memory. |
| 113 | let r = unsafe { |
| 114 | libc::inotify_add_watch(fd, udev_path.as_ptr() as *const _, libc::IN_DELETE) |
| 115 | }; |
| 116 | if r < 0 { |
| 117 | let errno = io::Error::last_os_error(); |
| 118 | let err_msg = |
| 119 | format!("unable to add inotify watch event, error: {r}, errno: {errno}"); |
| 120 | log::error!("{err_msg}"); |
| 121 | // SAFETY: argument is valid |
| 122 | unsafe { libc::close(fd) }; |
| 123 | Err(Error::UdevQueue(err_msg)) |
| 124 | } else { |
| 125 | self.fd = fd; |
| 126 | Ok(fd) |
| 127 | } |
| 128 | } |
| 129 | } else { |
| 130 | Ok(self.fd) |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | /// Clears the watched file descriptor for queue changes. |
| 135 | /// |
| 136 | /// # Safety |
| 137 | /// |
| 138 | /// Users must ensure that every [UdevQueue] has a unique file descriptor, if the descriptor is |
| 139 | /// non-negative. |
| 140 | /// |
| 141 | /// Returns: `Ok(())` on success, `Err(Error)` otherwise. |
| 142 | pub fn flush(&mut self) -> Result<()> { |
| 143 | let fd = self.fd; |
| 144 | if fd < 0 { |
| 145 | let err = libc::EINVAL; |
| 146 | Err(Error::UdevQueue(format!( |
| 147 | "invalid file descriptor, fd: {fd}, error: {err}" |
| 148 | ))) |
| 149 | } else { |
| 150 | // SAFETY: argument is valid, and only one valid mutable reference to this UdevQueue |
| 151 | // can be held without further `unsafe` code. |
| 152 | // |
| 153 | // Users must ensure that every UdevQueue has a unique file descriptor. |
| 154 | let mut file = unsafe { fs::File::from_raw_fd(fd) }; |
| 155 | file.flush().map_err(|err| { |
| 156 | let errno = io::Error::last_os_error(); |
| 157 | let err_msg = |
| 158 | format!("unable to flush queue file descriptor, error: {err}, errno: {errno}"); |
| 159 | log::error!("{err_msg}"); |
| 160 | Error::UdevQueue(err_msg) |
| 161 | }) |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | /// Checks if [Udev] is active on the system. |
| 166 | pub fn udev_is_active(&self) -> bool { |
| 167 | fs::OpenOptions::new() |
| 168 | .read(true) |
| 169 | .open(format!("{UDEV_ROOT_RUN}/udev/control")) |
| 170 | .is_ok() |
| 171 | } |
| 172 | |
| 173 | /// Gets whether [UdevQueue] is currently processing any events. |
| 174 | pub fn queue_is_empty(&self) -> bool { |
| 175 | fs::OpenOptions::new() |
| 176 | .read(true) |
| 177 | .open(format!("{UDEV_ROOT_RUN}/udev/queue")) |
| 178 | .is_ok() |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | #[cfg(test)] |
| 183 | mod tests { |
| 184 | use super::*; |
| 185 | use crate::UdevEntry; |
| 186 | |
| 187 | #[test] |
| 188 | fn test_udev_queue() { |
| 189 | let udev = Arc::new(Udev::new()); |
| 190 | let mut null_queue = UdevQueue::new(Arc::clone(&udev)); |
| 191 | |
| 192 | let exp_list = [UdevEntry::new().with_name("test_list_entry")]; |
| 193 | let exp_queue = UdevQueue::create(Arc::clone(&udev), exp_list.clone(), -1); |
| 194 | |
| 195 | assert!(null_queue.queue_list().is_empty()); |
| 196 | |
| 197 | for (entry, exp_entry) in exp_queue.queue_list().iter().zip(exp_list.iter()) { |
| 198 | assert_eq!(entry, exp_entry); |
| 199 | } |
| 200 | |
| 201 | null_queue.set_queue_list(exp_list); |
| 202 | |
| 203 | assert_eq!(null_queue, exp_queue); |
| 204 | } |
| 205 | } |