| 1 | use std::io::{self, BufRead, Read}; |
| 2 | use std::os::linux::fs::MetadataExt; |
| 3 | use std::sync::Arc; |
| 4 | use std::{cmp, env, ffi, fmt, fs, mem, time}; |
| 5 | |
| 6 | use super::{Error, Mode, Result, Udev, UdevEntry, UdevEntryList, UdevList}; |
| 7 | use crate::util; |
| 8 | |
| 9 | /// Maximum number of ENVP entries |
| 10 | pub const ENVP_LEN: usize = 128; |
| 11 | |
| 12 | /// Limits the number of characters for a UEVENT file. |
| 13 | /// |
| 14 | /// **NOTE** 4 KiB limit based on default Linux filesize. |
| 15 | pub const UEVENT_FILE_LIMIT: usize = 0x1000; |
| 16 | |
| 17 | /// Represents one kernel `sys` device. |
| 18 | #[repr(C)] |
| 19 | #[derive(Clone, Debug, PartialEq)] |
| 20 | pub struct UdevDevice { |
| 21 | udev: Arc<Udev>, |
| 22 | parent_device: Option<Arc<Self>>, |
| 23 | syspath: String, |
| 24 | devpath: String, |
| 25 | sysname: String, |
| 26 | sysnum: String, |
| 27 | devnode: String, |
| 28 | devnode_mode: Mode, |
| 29 | devnode_uid: u32, |
| 30 | devnode_gid: u32, |
| 31 | subsystem: String, |
| 32 | devtype: String, |
| 33 | driver: String, |
| 34 | action: String, |
| 35 | devpath_old: String, |
| 36 | id_filename: String, |
| 37 | envp: [String; ENVP_LEN], |
| 38 | monitor_buf: String, |
| 39 | devlinks_list: UdevList, |
| 40 | properties_list: UdevList, |
| 41 | sysattr_value_list: UdevList, |
| 42 | sysattr_list: UdevList, |
| 43 | tags_list: UdevList, |
| 44 | seqnum: u64, |
| 45 | usec_initialized: u64, |
| 46 | devlink_priority: i32, |
| 47 | devnum: u64, |
| 48 | ifindex: i32, |
| 49 | watch_handle: i32, |
| 50 | maj: i32, |
| 51 | min: i32, |
| 52 | devlinks_uptodate: bool, |
| 53 | envp_uptodate: bool, |
| 54 | tags_uptodate: bool, |
| 55 | info_loaded: bool, |
| 56 | db_loaded: bool, |
| 57 | uevent_loaded: bool, |
| 58 | is_initialized: bool, |
| 59 | sysattr_list_read: bool, |
| 60 | db_persist: bool, |
| 61 | } |
| 62 | |
| 63 | impl UdevDevice { |
| 64 | /// Creates a new [UdevDevice]. |
| 65 | pub fn new(udev: Arc<Udev>) -> Self { |
| 66 | let devlinks_list = UdevList::new(Arc::clone(&udev)); |
| 67 | let properties_list = UdevList::new(Arc::clone(&udev)); |
| 68 | let sysattr_value_list = UdevList::new(Arc::clone(&udev)); |
| 69 | let sysattr_list = UdevList::new(Arc::clone(&udev)); |
| 70 | let tags_list = UdevList::new(Arc::clone(&udev)); |
| 71 | |
| 72 | Self { |
| 73 | udev, |
| 74 | parent_device: None, |
| 75 | syspath: String::new(), |
| 76 | devpath: String::new(), |
| 77 | sysname: String::new(), |
| 78 | sysnum: String::new(), |
| 79 | devnode: String::new(), |
| 80 | devnode_mode: Mode::new(), |
| 81 | devnode_gid: 0, |
| 82 | devnode_uid: 0, |
| 83 | subsystem: String::new(), |
| 84 | devtype: String::new(), |
| 85 | driver: String::new(), |
| 86 | action: String::new(), |
| 87 | devpath_old: String::new(), |
| 88 | id_filename: String::new(), |
| 89 | envp: [""; ENVP_LEN].map(String::from), |
| 90 | monitor_buf: String::new(), |
| 91 | devlinks_list, |
| 92 | properties_list, |
| 93 | sysattr_value_list, |
| 94 | sysattr_list, |
| 95 | tags_list, |
| 96 | seqnum: 0, |
| 97 | usec_initialized: 0, |
| 98 | devlink_priority: 0, |
| 99 | devnum: 0, |
| 100 | ifindex: 0, |
| 101 | watch_handle: 0, |
| 102 | maj: 0, |
| 103 | min: 0, |
| 104 | devlinks_uptodate: false, |
| 105 | envp_uptodate: false, |
| 106 | tags_uptodate: false, |
| 107 | info_loaded: false, |
| 108 | db_loaded: false, |
| 109 | uevent_loaded: false, |
| 110 | is_initialized: false, |
| 111 | sysattr_list_read: false, |
| 112 | db_persist: false, |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | /// Gets a reference to the [Udev] context. |
| 117 | pub fn udev(&self) -> &Udev { |
| 118 | self.udev.as_ref() |
| 119 | } |
| 120 | |
| 121 | /// Gets a cloned reference to the [Udev] context. |
| 122 | pub fn udev_cloned(&self) -> Arc<Udev> { |
| 123 | Arc::clone(&self.udev) |
| 124 | } |
| 125 | |
| 126 | /// Creates a new [UdevDevice]. |
| 127 | pub fn new_from_nulstr(udev: Arc<Udev>, buf: &[u8]) -> Result<Self> { |
| 128 | let mut device = Self::new(udev); |
| 129 | |
| 130 | device.set_info_loaded(true); |
| 131 | |
| 132 | for key in buf.split(|&b| b == 0) { |
| 133 | if key.is_empty() { |
| 134 | break; |
| 135 | } |
| 136 | |
| 137 | device.add_property_from_string(std::str::from_utf8(key).unwrap_or("")); |
| 138 | } |
| 139 | |
| 140 | device.add_property_from_string_parse_finish()?; |
| 141 | |
| 142 | Ok(device) |
| 143 | } |
| 144 | |
| 145 | /// Creates new [UdevDevice], and fills in information from the sys |
| 146 | /// device and the udev database entry. |
| 147 | /// |
| 148 | /// The `syspath` is the absolute path to the device, including the sys mount point. |
| 149 | /// |
| 150 | /// The initial refcount is 1, and needs to be decremented to release the resources of the udev device. |
| 151 | /// |
| 152 | /// Returns: a new [UdevDevice], or `Error`, if it does not exist |
| 153 | pub fn new_from_syspath(udev: Arc<Udev>, syspath: &str) -> Result<Self> { |
| 154 | if syspath.is_empty() { |
| 155 | Err(Error::UdevDevice("empty syspath".into())) |
| 156 | } else if !syspath.starts_with("/sys") { |
| 157 | Err(Error::UdevDevice(format!("not in sys: {syspath}"))) |
| 158 | } else if let Some(path) = syspath.strip_prefix("/sys/") { |
| 159 | let fullpath = if path.starts_with("/devices/") { |
| 160 | let uevent_path = format!("{syspath}/uevent"); |
| 161 | let _ = fs::File::open(uevent_path.as_str()).map_err(|err| { |
| 162 | Error::UdevDevice(format!("unable to open syspath uevent file: {err}")) |
| 163 | })?; |
| 164 | uevent_path |
| 165 | } else { |
| 166 | syspath.into() |
| 167 | }; |
| 168 | |
| 169 | let dev = Self::new(udev).with_syspath(fullpath); |
| 170 | log::trace!("device {dev} has devpath: {}", dev.devpath()); |
| 171 | |
| 172 | Ok(dev) |
| 173 | } else { |
| 174 | Err(Error::UdevDevice("empty syspath subdir".into())) |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | /// Creates new [UdevDevice]. |
| 179 | /// |
| 180 | /// From the `libudev` documentation: |
| 181 | /// |
| 182 | /// ```no_build,no_run |
| 183 | /// Fills in information from the `sys` device and the udev database entry. |
| 184 | /// |
| 185 | /// The device is looked-up by its major/minor number and type. Character and block device |
| 186 | /// numbers are not unique across the two types. |
| 187 | /// ``` |
| 188 | /// |
| 189 | /// Parameters: |
| 190 | /// |
| 191 | /// - `udev`: [Udev] library context |
| 192 | /// - `type`: `char` or `block` device |
| 193 | /// - `devnum`: device major/minor number |
| 194 | /// |
| 195 | /// Returns: a new [UdevDevice], or `Err`, if it does not exist |
| 196 | pub fn new_from_devnum(udev: Arc<Udev>, devtype: &str, devnum: libc::dev_t) -> Result<Self> { |
| 197 | let type_str = match devtype { |
| 198 | t if t.starts_with('b') => Ok("block"), |
| 199 | t if t.starts_with('c') => Ok("char"), |
| 200 | _ => Err(Error::UdevDevice(format!("invalid device type: {devtype}"))), |
| 201 | }?; |
| 202 | |
| 203 | // use /sys/dev/{block,char}/<maj>:<min> link |
| 204 | let path = format!( |
| 205 | "/sys/dev/{type_str}/{}:{}", |
| 206 | util::major(devnum), |
| 207 | util::minor(devnum) |
| 208 | ); |
| 209 | Self::new_from_syspath(udev, &path) |
| 210 | } |
| 211 | |
| 212 | /// Creates a new [UdevDevice] from the subsystem and sysname. |
| 213 | /// |
| 214 | /// From the `libudev` documentation: |
| 215 | /// |
| 216 | /// ```no_build,no_run |
| 217 | /// Fills in information from the sys device and the udev database entry. |
| 218 | /// |
| 219 | /// The device is looked up by the subsystem and name string of the device, like "mem" / "zero", or "block" / "sda". |
| 220 | /// ``` |
| 221 | /// |
| 222 | /// Parameters: |
| 223 | /// |
| 224 | /// - `udev`: [Udev] library context |
| 225 | /// - `subsystem`: the subsystem of the device |
| 226 | /// - `sysname`: the name of the device |
| 227 | /// |
| 228 | /// Returns: a new [UdevDevice], or `Err`, if it does not exist |
| 229 | pub fn new_from_subsystem_sysname( |
| 230 | udev: Arc<Udev>, |
| 231 | subsystem: &str, |
| 232 | sysname: &str, |
| 233 | ) -> Result<Self> { |
| 234 | let path = if subsystem == "subsystem" { |
| 235 | let sub_path = format!("/sys/subsystem/{sysname}"); |
| 236 | let bus_path = format!("/sys/bus/{sysname}"); |
| 237 | let class_path = format!("/sys/class/{sysname}"); |
| 238 | |
| 239 | if fs::metadata(sub_path.as_str()).is_ok() { |
| 240 | Ok(sub_path) |
| 241 | } else if fs::metadata(bus_path.as_str()).is_ok() { |
| 242 | Ok(bus_path) |
| 243 | } else if fs::metadata(class_path.as_str()).is_ok() { |
| 244 | Ok(class_path) |
| 245 | } else { |
| 246 | Err(Error::UdevDevice(format!( |
| 247 | "no subsystem device found for: {sysname}" |
| 248 | ))) |
| 249 | } |
| 250 | } else if subsystem == "module" { |
| 251 | let path = format!("/sys/module/{sysname}"); |
| 252 | if fs::metadata(path.as_str()).is_ok() { |
| 253 | Ok(path) |
| 254 | } else { |
| 255 | Err(Error::UdevDevice(format!( |
| 256 | "no module device found for: {sysname}" |
| 257 | ))) |
| 258 | } |
| 259 | } else if subsystem == "drivers" { |
| 260 | if let Some(driver) = sysname.split(':').nth(2) { |
| 261 | let sub_path = format!("/sys/subsystem/{sysname}/drivers/{driver}"); |
| 262 | let bus_path = format!("/sys/bus/{sysname}/drivers/{driver}"); |
| 263 | |
| 264 | if fs::metadata(sub_path.as_str()).is_ok() { |
| 265 | Ok(sub_path) |
| 266 | } else if fs::metadata(bus_path.as_str()).is_ok() { |
| 267 | Ok(bus_path) |
| 268 | } else { |
| 269 | Err(Error::UdevDevice(format!( |
| 270 | "no driver device found for: {sysname}" |
| 271 | ))) |
| 272 | } |
| 273 | } else { |
| 274 | Err(Error::UdevDevice(format!( |
| 275 | "invalid driver subsystem: {sysname}" |
| 276 | ))) |
| 277 | } |
| 278 | } else { |
| 279 | let sub_path = format!("/sys/subsystem/{subsystem}/devices/{sysname}"); |
| 280 | let bus_path = format!("/sys/bus/{subsystem}/devices/{sysname}"); |
| 281 | let class_path = format!("/sys/class/{subsystem}/{sysname}"); |
| 282 | |
| 283 | if fs::metadata(sub_path.as_str()).is_ok() { |
| 284 | Ok(sub_path) |
| 285 | } else if fs::metadata(bus_path.as_str()).is_ok() { |
| 286 | Ok(bus_path) |
| 287 | } else if fs::metadata(class_path.as_str()).is_ok() { |
| 288 | Ok(class_path) |
| 289 | } else { |
| 290 | Err(Error::UdevDevice(format!("no device found for: {sysname}"))) |
| 291 | } |
| 292 | }?; |
| 293 | |
| 294 | Self::new_from_syspath(udev, &path) |
| 295 | } |
| 296 | |
| 297 | /// Create new [UdevDevice] from an ID string. |
| 298 | /// |
| 299 | /// From the `libudev` documentation: |
| 300 | /// |
| 301 | /// ```no_build,no_run |
| 302 | /// |
| 303 | /// Fill in information from the sys device and the udev database entry. |
| 304 | /// |
| 305 | /// The device is looked-up by a special string: |
| 306 | /// |
| 307 | /// b8:2 - block device major:minor |
| 308 | /// c128:1 - char device major:minor |
| 309 | /// n3 - network device ifindex |
| 310 | /// +sound:card29 - kernel driver core subsystem:device name |
| 311 | /// ``` |
| 312 | /// |
| 313 | /// Parameters: |
| 314 | /// |
| 315 | /// - `udev`: udev library context |
| 316 | /// - `id`: text string identifying a kernel device |
| 317 | /// |
| 318 | /// Returns: a new [UdevDevice], or `Err`, if it does not exist |
| 319 | pub fn new_from_device_id(udev: Arc<Udev>, id: &str) -> Result<Self> { |
| 320 | match id.chars().next() { |
| 321 | Some('b') | Some('c') => { |
| 322 | let devtype = &id[..1]; |
| 323 | |
| 324 | let mut maj_min_iter = id[1..].split(':'); |
| 325 | let maj = maj_min_iter |
| 326 | .next() |
| 327 | .unwrap_or("") |
| 328 | .parse::<u32>() |
| 329 | .unwrap_or(0); |
| 330 | let min = maj_min_iter |
| 331 | .next() |
| 332 | .unwrap_or("") |
| 333 | .parse::<u32>() |
| 334 | .unwrap_or(0); |
| 335 | |
| 336 | Self::new_from_devnum(udev, devtype, libc::makedev(maj, min)) |
| 337 | } |
| 338 | Some('n') => { |
| 339 | let ifindex = id[1..].parse::<i32>().map_err(|err| { |
| 340 | Error::UdevDevice(format!("invalid network device ID: {id}, error: {err}")) |
| 341 | })?; |
| 342 | // SAFETY: all arguments are valid, and properly initialized. |
| 343 | let sock = unsafe { libc::socket(libc::PF_INET, libc::SOCK_DGRAM, 0) }; |
| 344 | if sock < 0 { |
| 345 | let errno = io::Error::last_os_error(); |
| 346 | return Err(Error::UdevDevice(format!( |
| 347 | "unable to create a socket: {errno}" |
| 348 | ))); |
| 349 | } |
| 350 | |
| 351 | // SAFETY: zeroed memory initializes `libc::ifreq` to a valid state. |
| 352 | let mut ifr: libc::ifreq = unsafe { mem::zeroed() }; |
| 353 | ifr.ifr_ifru.ifru_ifindex = ifindex; |
| 354 | // SAFETY: all arguments are valid, pointers reference valid memory, and `SICGIFNAME` is a valid ioctl |
| 355 | if unsafe { libc::ioctl(sock, libc::SIOCGIFNAME, &mut ifr) } != 0 { |
| 356 | let errno = io::Error::last_os_error(); |
| 357 | // SAFETY: `sock` is a valid socket file descriptor returned by the kernel |
| 358 | unsafe { libc::close(sock) }; |
| 359 | return Err(Error::UdevDevice(format!( |
| 360 | "invalid interface name: {errno}" |
| 361 | ))); |
| 362 | } |
| 363 | // SAFETY: `sock` is a valid socket file descriptor returned by the kernel |
| 364 | unsafe { libc::close(sock) }; |
| 365 | |
| 366 | // SAFETY: `ifr_name` is a C-string returned by the kernel. |
| 367 | // Unless something went horribly wrong, it will be a valid, nul-terminated C-string. |
| 368 | let cifr_name = unsafe { ffi::CStr::from_ptr(ifr.ifr_name.as_ptr()) }; |
| 369 | let ifr_name = cifr_name.to_str().map_err(|err| { |
| 370 | Error::UdevDevice(format!("unable to convert ifr_name to Rust string: {err}")) |
| 371 | })?; |
| 372 | |
| 373 | let mut dev = Self::new_from_subsystem_sysname(udev, "net", ifr_name)?; |
| 374 | if dev.get_ifindex() == ifindex { |
| 375 | Ok(dev) |
| 376 | } else { |
| 377 | Err(Error::UdevDevice( |
| 378 | "unable to create device from ID string".into(), |
| 379 | )) |
| 380 | } |
| 381 | } |
| 382 | Some('+') => { |
| 383 | let mut subsys_iter = id[1..].split(':'); |
| 384 | let subsys = subsys_iter.next().ok_or(Error::UdevDevice(format!( |
| 385 | "invalid subsystem from ID: {id}" |
| 386 | )))?; |
| 387 | let sysname = subsys_iter.next().ok_or(Error::UdevDevice(format!( |
| 388 | "invalid system name from ID: {id}" |
| 389 | )))?; |
| 390 | |
| 391 | Self::new_from_subsystem_sysname(udev, subsys, sysname) |
| 392 | } |
| 393 | _ => Err(Error::UdevDevice(format!("invalid device ID: {id}"))), |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | /// Create new udev device from the environment information. |
| 398 | /// |
| 399 | /// From the original `libudev` documnentation: |
| 400 | /// |
| 401 | /// ```no_build,no_run |
| 402 | /// Fills in information from the current process environment. |
| 403 | /// This only works reliable if the process is called from a udev rule. |
| 404 | /// It is usually used for tools executed from IMPORT= rules. |
| 405 | /// ``` |
| 406 | /// |
| 407 | /// Parameters: |
| 408 | /// |
| 409 | /// - `udev`: [Udev] library context |
| 410 | /// |
| 411 | /// Returns: a new [UdevDevice], or `Err`, if it does not exist |
| 412 | pub fn new_from_environment(udev: Arc<Udev>) -> Result<Self> { |
| 413 | let mut dev = Self::new(udev); |
| 414 | dev.set_info_loaded(true); |
| 415 | |
| 416 | for (key, value) in env::vars() { |
| 417 | dev.add_property_from_string_parse(format!("{key}={value}").as_str())?; |
| 418 | } |
| 419 | |
| 420 | dev.add_property_from_string_parse_finish()?; |
| 421 | |
| 422 | Ok(dev) |
| 423 | } |
| 424 | |
| 425 | /// Creates a new [UdevDevice] from the next parent directory in the syspath. |
| 426 | /// |
| 427 | /// Returns an `Err` if no parent is found. |
| 428 | pub fn new_from_parent(&self) -> Result<Self> { |
| 429 | let path = self.syspath(); |
| 430 | let subdir = path |
| 431 | .strip_prefix("/sys/") |
| 432 | .ok_or(Error::UdevDevice(format!("invalid syspath: {path}")))?; |
| 433 | |
| 434 | let mut pos = subdir.len(); |
| 435 | let syslen = "/sys/".len(); |
| 436 | loop { |
| 437 | // do a reverse search for the next parent directory on the syspath |
| 438 | pos = subdir[..pos] |
| 439 | .rfind('/') |
| 440 | .ok_or(Error::UdevDevice("no syspath subdirectory".into()))?; |
| 441 | if pos < 2 { |
| 442 | break; |
| 443 | } |
| 444 | if let Ok(dev) = |
| 445 | Self::new_from_syspath(self.udev_cloned(), &path[..syslen.saturating_add(pos)]) |
| 446 | { |
| 447 | return Ok(dev); |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | Err(Error::UdevDevice(format!( |
| 452 | "no parent device found for syspath: {path}" |
| 453 | ))) |
| 454 | } |
| 455 | |
| 456 | /// Gets the next parent [UdevDevice]. |
| 457 | /// |
| 458 | /// From the `libudev` documentation: |
| 459 | /// |
| 460 | /// ```no_build,no_run |
| 461 | /// Find the next parent device, and fill in information from the sys |
| 462 | /// device and the udev database entry. |
| 463 | /// |
| 464 | /// @udev_device: the device to start searching from |
| 465 | /// |
| 466 | /// Returned device is not referenced. It is attached to the child |
| 467 | /// device, and will be cleaned up when the child device is cleaned up. |
| 468 | /// |
| 469 | /// It is not necessarily just the upper level directory, empty or not |
| 470 | /// recognized sys directories are ignored. |
| 471 | /// |
| 472 | /// It can be called as many times as needed, without caring about |
| 473 | /// references. |
| 474 | /// ``` |
| 475 | /// |
| 476 | /// Returns: a new [UdevDevice], or `Err`, if it no parent exists. |
| 477 | pub fn get_parent(&mut self) -> Result<Arc<Self>> { |
| 478 | match self.parent_device.as_ref() { |
| 479 | Some(dev) => Ok(Arc::clone(dev)), |
| 480 | None => { |
| 481 | let dev = Arc::new(self.new_from_parent()?); |
| 482 | self.parent_device = Some(Arc::clone(&dev)); |
| 483 | Ok(dev) |
| 484 | } |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | /// Gets the next parent [UdevDevice] based on `subsystem` and `devtype`. |
| 489 | /// |
| 490 | /// From the `libudev` documentation: |
| 491 | /// |
| 492 | /// ```no_build,no_run |
| 493 | /// Find the next parent device, with a matching subsystem and devtype |
| 494 | /// value, and fill in information from the sys device and the udev |
| 495 | /// database entry. |
| 496 | /// |
| 497 | /// If devtype is #NULL, only subsystem is checked, and any devtype will |
| 498 | /// match. |
| 499 | /// |
| 500 | /// Returned device is not referenced. It is attached to the child |
| 501 | /// device, and will be cleaned up when the child device is cleaned up. |
| 502 | /// |
| 503 | /// It can be called as many times as needed, without caring about |
| 504 | /// references. |
| 505 | /// ``` |
| 506 | /// |
| 507 | /// Parameters: |
| 508 | /// |
| 509 | /// - `udev_device`: udev device to start searching from |
| 510 | /// - `subsystem`: the subsystem of the device |
| 511 | /// - `devtype`: the type (DEVTYPE) of the device |
| 512 | /// |
| 513 | /// Returns: a new [UdevDevice], or `Err` if no matching parent exists. |
| 514 | pub fn get_parent_with_subsystem_devtype( |
| 515 | &mut self, |
| 516 | subsystem: &str, |
| 517 | devtype: &str, |
| 518 | ) -> Result<Arc<Self>> { |
| 519 | let mut ret = Err(Error::UdevDevice("no matching parent device found".into())); |
| 520 | let mut parent_res = self.new_from_parent(); |
| 521 | |
| 522 | while let Ok(mut parent) = parent_res { |
| 523 | let parent_subsystem = parent.get_subsystem().to_owned(); |
| 524 | let parent_devtype = parent.get_devtype().to_owned(); |
| 525 | |
| 526 | if !parent_subsystem.is_empty() |
| 527 | && parent_subsystem.as_str() == subsystem |
| 528 | && !parent_devtype.is_empty() |
| 529 | && parent_devtype.as_str() == devtype |
| 530 | { |
| 531 | let dev = Arc::new(parent); |
| 532 | self.parent_device = Some(Arc::clone(&dev)); |
| 533 | |
| 534 | // not a real error, but breaks the while loop next iteration |
| 535 | parent_res = Err(Error::UdevDevice("set parent device".into())); |
| 536 | // set a successful return value |
| 537 | ret = Ok(dev); |
| 538 | } else { |
| 539 | // try the next parent device |
| 540 | parent_res = parent.new_from_parent(); |
| 541 | } |
| 542 | } |
| 543 | |
| 544 | ret |
| 545 | } |
| 546 | |
| 547 | /// Gets whether a parent [UdevDevice] is set. |
| 548 | pub const fn parent_set(&self) -> bool { |
| 549 | self.parent_device.is_some() |
| 550 | } |
| 551 | |
| 552 | /// Gets the [UdevDevice] syspath. |
| 553 | pub fn syspath(&self) -> &str { |
| 554 | self.syspath.as_str() |
| 555 | } |
| 556 | |
| 557 | /// Sets the [UdevDevice] syspath. |
| 558 | pub fn set_syspath<P: Into<String>>(&mut self, syspath: P) { |
| 559 | self.syspath = syspath.into(); |
| 560 | } |
| 561 | |
| 562 | /// Builder function that sets the [UdevDevice] syspath. |
| 563 | pub fn with_syspath<P: Into<String>>(mut self, syspath: P) -> Self { |
| 564 | self.set_syspath(syspath); |
| 565 | self |
| 566 | } |
| 567 | |
| 568 | /// Gets the [UdevDevice] devpath. |
| 569 | pub fn devpath(&self) -> &str { |
| 570 | self.devpath.as_str() |
| 571 | } |
| 572 | |
| 573 | /// Sets the [UdevDevice] devpath. |
| 574 | pub fn set_devpath<P: Into<String>>(&mut self, devpath: P) { |
| 575 | self.devpath = devpath.into(); |
| 576 | } |
| 577 | |
| 578 | /// Builder function that sets the [UdevDevice] devpath. |
| 579 | pub fn with_devpath<P: Into<String>>(mut self, devpath: P) -> Self { |
| 580 | self.set_devpath(devpath); |
| 581 | self |
| 582 | } |
| 583 | |
| 584 | /// Gets the [UdevDevice] sysname. |
| 585 | pub fn sysname(&self) -> &str { |
| 586 | self.sysname.as_str() |
| 587 | } |
| 588 | |
| 589 | /// Sets the [UdevDevice] sysname. |
| 590 | pub fn set_sysname<P: Into<String>>(&mut self, sysname: P) { |
| 591 | self.sysname = sysname.into(); |
| 592 | } |
| 593 | |
| 594 | /// Builder function that sets the [UdevDevice] sysname. |
| 595 | pub fn with_sysname<P: Into<String>>(mut self, sysname: P) -> Self { |
| 596 | self.set_sysname(sysname); |
| 597 | self |
| 598 | } |
| 599 | |
| 600 | /// Gets the [UdevDevice] sysnum. |
| 601 | pub fn sysnum(&self) -> &str { |
| 602 | self.sysnum.as_str() |
| 603 | } |
| 604 | |
| 605 | /// Sets the [UdevDevice] sysnum. |
| 606 | pub fn set_sysnum<P: Into<String>>(&mut self, sysnum: P) { |
| 607 | self.sysnum = sysnum.into(); |
| 608 | } |
| 609 | |
| 610 | /// Builder function that sets the [UdevDevice] sysnum. |
| 611 | pub fn with_sysnum<P: Into<String>>(mut self, sysnum: P) -> Self { |
| 612 | self.set_sysnum(sysnum); |
| 613 | self |
| 614 | } |
| 615 | |
| 616 | /// Gets the [UdevDevice] devnode. |
| 617 | pub fn devnode(&self) -> &str { |
| 618 | self.devnode.as_str() |
| 619 | } |
| 620 | |
| 621 | /// Sets the [UdevDevice] devnode. |
| 622 | pub fn set_devnode<P: Into<String>>(&mut self, devnode: P) { |
| 623 | self.devnode = devnode.into(); |
| 624 | } |
| 625 | |
| 626 | /// Builder function that sets the [UdevDevice] devnode. |
| 627 | pub fn with_devnode<P: Into<String>>(mut self, devnode: P) -> Self { |
| 628 | self.set_devnode(devnode); |
| 629 | self |
| 630 | } |
| 631 | |
| 632 | /// Gets the [UdevDevice] `devnode`. |
| 633 | /// |
| 634 | /// From the `libudev` documentation: |
| 635 | /// |
| 636 | /// ```no_build,no_run |
| 637 | /// Retrieve the device node file name belonging to the udev device. |
| 638 | /// The path is an absolute path, and starts with the device directory. |
| 639 | /// ``` |
| 640 | /// |
| 641 | /// Returns: the device node file name of the [UdevDevice], or an empty string if none exists. |
| 642 | pub fn get_devnode(&mut self) -> &str { |
| 643 | if self.devnode.is_empty() && !self.info_loaded { |
| 644 | self.read_uevent_file().ok(); |
| 645 | } |
| 646 | self.devnode.as_str() |
| 647 | } |
| 648 | |
| 649 | /// Gets the [UdevDevice] devnode [Mode]. |
| 650 | pub const fn devnode_mode(&self) -> Mode { |
| 651 | self.devnode_mode |
| 652 | } |
| 653 | |
| 654 | /// Sets the [UdevDevice] devnode [Mode]. |
| 655 | pub fn set_devnode_mode(&mut self, devnode_mode: Mode) { |
| 656 | self.devnode_mode = devnode_mode; |
| 657 | } |
| 658 | |
| 659 | /// Builder function sets the [UdevDevice] devnode [Mode]. |
| 660 | pub fn with_devnode_mode(mut self, devnode_mode: Mode) -> Self { |
| 661 | self.set_devnode_mode(devnode_mode); |
| 662 | self |
| 663 | } |
| 664 | |
| 665 | /// Gets the [UdevDevice] devnode GID. |
| 666 | pub const fn devnode_gid(&self) -> u32 { |
| 667 | self.devnode_gid |
| 668 | } |
| 669 | |
| 670 | /// Sets the [UdevDevice] devnode GID. |
| 671 | pub fn set_devnode_gid(&mut self, gid: u32) { |
| 672 | self.devnode_gid = gid; |
| 673 | } |
| 674 | |
| 675 | /// Builder function that sets the [UdevDevice] devnode GID. |
| 676 | pub fn with_devnode_gid(mut self, gid: u32) -> Self { |
| 677 | self.set_devnode_gid(gid); |
| 678 | self |
| 679 | } |
| 680 | |
| 681 | /// Gets the [UdevDevice] devnode UID. |
| 682 | pub const fn devnode_uid(&self) -> u32 { |
| 683 | self.devnode_uid |
| 684 | } |
| 685 | |
| 686 | /// Sets the [UdevDevice] devnode UID. |
| 687 | pub fn set_devnode_uid(&mut self, uid: u32) { |
| 688 | self.devnode_uid = uid; |
| 689 | } |
| 690 | |
| 691 | /// Builder function that sets the [UdevDevice] devnode UID. |
| 692 | pub fn with_devnode_uid(mut self, uid: u32) -> Self { |
| 693 | self.set_devnode_uid(uid); |
| 694 | self |
| 695 | } |
| 696 | |
| 697 | /// Gets the [UdevDevice] subsystem. |
| 698 | pub fn subsystem(&self) -> &str { |
| 699 | self.subsystem.as_str() |
| 700 | } |
| 701 | |
| 702 | /// Sets the [UdevDevice] subsystem. |
| 703 | pub fn set_subsystem<P: Into<String>>(&mut self, subsystem: P) { |
| 704 | self.subsystem = subsystem.into(); |
| 705 | } |
| 706 | |
| 707 | /// Builder function that sets the [UdevDevice] subsystem. |
| 708 | pub fn with_subsystem<P: Into<String>>(mut self, subsystem: P) -> Self { |
| 709 | self.set_subsystem(subsystem); |
| 710 | self |
| 711 | } |
| 712 | |
| 713 | /// Retrieves the subsystem string of the `udev` device. |
| 714 | /// |
| 715 | /// The string does not contain any "`/`". |
| 716 | /// |
| 717 | /// Returns: |
| 718 | /// |
| 719 | /// - name of the subsystem of the `udev` device. |
| 720 | /// - empty string if the subsystem can not be determined. |
| 721 | pub fn get_subsystem(&mut self) -> &str { |
| 722 | if self.subsystem.is_empty() { |
| 723 | if let Ok(subsystem) = Udev::get_sys_core_link_value("subsystem", self.syspath()) { |
| 724 | self.set_subsystem(subsystem); |
| 725 | } else if self.devpath.starts_with("/module/") |
| 726 | || self.devpath.rfind("/drivers/").is_some() |
| 727 | { |
| 728 | self.set_subsystem("module"); |
| 729 | } else if self.devpath.starts_with("/subsystem/") |
| 730 | || self.devpath.starts_with("/class/") |
| 731 | || self.devpath.starts_with("/bus/") |
| 732 | { |
| 733 | self.set_subsystem("subsystem"); |
| 734 | } |
| 735 | } |
| 736 | |
| 737 | self.subsystem() |
| 738 | } |
| 739 | |
| 740 | /// Gets the [UdevDevice] devtype. |
| 741 | pub fn devtype(&self) -> &str { |
| 742 | self.devtype.as_str() |
| 743 | } |
| 744 | |
| 745 | /// Sets the [UdevDevice] devtype. |
| 746 | pub fn set_devtype<P: Into<String>>(&mut self, devtype: P) { |
| 747 | self.devtype = devtype.into(); |
| 748 | } |
| 749 | |
| 750 | /// Builder function that sets the [UdevDevice] devtype. |
| 751 | pub fn with_devtype<P: Into<String>>(mut self, devtype: P) -> Self { |
| 752 | self.set_devtype(devtype); |
| 753 | self |
| 754 | } |
| 755 | |
| 756 | /// Gets the devtypes string of the [UdevDevice]. |
| 757 | /// |
| 758 | /// If the devtype is unset, attempts to read properties from the syspath file. |
| 759 | pub fn get_devtype(&mut self) -> &str { |
| 760 | if self.devtype.is_empty() { |
| 761 | self.read_uevent_file().ok(); |
| 762 | } |
| 763 | self.devtype.as_str() |
| 764 | } |
| 765 | |
| 766 | /// Gets the [UdevDevice] driver. |
| 767 | pub fn driver(&self) -> &str { |
| 768 | self.driver.as_str() |
| 769 | } |
| 770 | |
| 771 | /// Sets the [UdevDevice] driver. |
| 772 | pub fn set_driver<P: Into<String>>(&mut self, driver: P) { |
| 773 | self.driver = driver.into(); |
| 774 | } |
| 775 | |
| 776 | /// Builder function that sets the [UdevDevice] driver. |
| 777 | pub fn with_driver<P: Into<String>>(mut self, driver: P) -> Self { |
| 778 | self.set_driver(driver); |
| 779 | self |
| 780 | } |
| 781 | |
| 782 | /// Gets the kernel driver name. |
| 783 | /// |
| 784 | /// Returns: the kernel driver name, or `None` if none is attached. |
| 785 | pub fn get_driver(&mut self) -> Option<&str> { |
| 786 | if self.driver.is_empty() { |
| 787 | self.driver = Udev::get_sys_core_link_value("driver", self.syspath.as_str()).ok()?; |
| 788 | } |
| 789 | Some(self.driver.as_str()) |
| 790 | } |
| 791 | |
| 792 | /// Gets the [UdevDevice] action. |
| 793 | pub fn action(&self) -> &str { |
| 794 | self.action.as_str() |
| 795 | } |
| 796 | |
| 797 | /// Sets the [UdevDevice] action. |
| 798 | pub fn set_action<P: Into<String>>(&mut self, action: P) { |
| 799 | self.action = action.into(); |
| 800 | } |
| 801 | |
| 802 | /// Builder function that sets the [UdevDevice] action. |
| 803 | pub fn with_action<P: Into<String>>(mut self, action: P) -> Self { |
| 804 | self.set_action(action); |
| 805 | self |
| 806 | } |
| 807 | |
| 808 | /// Gets the [UdevDevice] devpath_old. |
| 809 | pub fn devpath_old(&self) -> &str { |
| 810 | self.devpath_old.as_str() |
| 811 | } |
| 812 | |
| 813 | /// Sets the [UdevDevice] devpath_old. |
| 814 | pub fn set_devpath_old<P: Into<String>>(&mut self, devpath_old: P) { |
| 815 | self.devpath_old = devpath_old.into(); |
| 816 | } |
| 817 | |
| 818 | /// Builder function that sets the [UdevDevice] devpath_old. |
| 819 | pub fn with_devpath_old<P: Into<String>>(mut self, devpath_old: P) -> Self { |
| 820 | self.set_devpath_old(devpath_old); |
| 821 | self |
| 822 | } |
| 823 | |
| 824 | /// Gets the [UdevDevice] id_filename. |
| 825 | pub fn id_filename(&self) -> &str { |
| 826 | self.id_filename.as_str() |
| 827 | } |
| 828 | |
| 829 | /// Sets the [UdevDevice] id_filename. |
| 830 | pub fn set_id_filename<P: Into<String>>(&mut self, id_filename: P) { |
| 831 | self.id_filename = id_filename.into(); |
| 832 | } |
| 833 | |
| 834 | /// Builder function that sets the [UdevDevice] id_filename. |
| 835 | pub fn with_id_filename<P: Into<String>>(mut self, id_filename: P) -> Self { |
| 836 | self.set_id_filename(id_filename); |
| 837 | self |
| 838 | } |
| 839 | |
| 840 | /// Gets the ID filename. |
| 841 | /// |
| 842 | /// If the filename is empty, attempts to set it based on subsystem information. |
| 843 | /// |
| 844 | /// Returns: the ID filename, or an empty string if none can be constructed. |
| 845 | pub fn get_id_filename(&mut self) -> &str { |
| 846 | let id_filename = if self.id_filename.is_empty() { |
| 847 | if self.get_subsystem().is_empty() { |
| 848 | String::new() |
| 849 | } else if util::major(self.get_devnum()) > 0 { |
| 850 | // From `libudev` documentation: |
| 851 | // |
| 852 | // use devtype: <type><major>:<minor>, e.g. b259:131072, c254:0 |
| 853 | let devtype = if self.get_subsystem() == "block" { |
| 854 | 'b' |
| 855 | } else { |
| 856 | 'c' |
| 857 | }; |
| 858 | let devnum = self.devnum(); |
| 859 | let major = util::major(devnum); |
| 860 | let minor = util::minor(devnum); |
| 861 | format!("{devtype}{major}:{minor}") |
| 862 | } else if self.get_ifindex() > 0 { |
| 863 | // From `libudev` documentation: |
| 864 | // |
| 865 | // use netdev ifindex: <type><ifindex>, e.g. n3 |
| 866 | format!("n{}", self.ifindex) |
| 867 | } else if let Some(sysname) = self.devpath.clone().rsplit('/').next() { |
| 868 | // From `libudev` documentation: |
| 869 | // |
| 870 | // use $subsys:$sysname, e.g. pci:0000:00:1f.2 |
| 871 | // sysname() has '!' translated, get it from devpath |
| 872 | format!("+{}:{sysname}", self.get_subsystem()) |
| 873 | } else { |
| 874 | String::new() |
| 875 | } |
| 876 | } else { |
| 877 | String::new() |
| 878 | }; |
| 879 | |
| 880 | if id_filename.is_empty() { |
| 881 | "" |
| 882 | } else { |
| 883 | self.id_filename = id_filename; |
| 884 | self.id_filename() |
| 885 | } |
| 886 | } |
| 887 | |
| 888 | /// Gets a reference to the list of `envp` arguments. |
| 889 | pub fn envp(&self) -> &[String] { |
| 890 | let len = self.envp_len(); |
| 891 | self.envp[..len].as_ref() |
| 892 | } |
| 893 | |
| 894 | /// Gets the length of non-empty `envp` arguments. |
| 895 | pub fn envp_len(&self) -> usize { |
| 896 | self.envp.iter().take_while(|e| !e.is_empty()).count() |
| 897 | } |
| 898 | |
| 899 | /// Sets the list of `envp` arguments. |
| 900 | pub fn set_envp<P: Into<String> + Clone>(&mut self, envp: &[P]) { |
| 901 | let len = cmp::min(envp.len(), ENVP_LEN); |
| 902 | self.envp[..len] |
| 903 | .iter_mut() |
| 904 | .zip(envp.iter().cloned()) |
| 905 | .for_each(|(dst, src)| *dst = src.into()); |
| 906 | self.envp[len..].iter_mut().for_each(|s| s.clear()); |
| 907 | } |
| 908 | |
| 909 | /// Builder function that sets the list of `envp` arguments. |
| 910 | pub fn with_envp<P: Into<String> + Clone>(mut self, envp: &[P]) -> Self { |
| 911 | self.set_envp(envp); |
| 912 | self |
| 913 | } |
| 914 | |
| 915 | /// Gets whether the `envp` list is empty. |
| 916 | pub fn envp_is_empty(&self) -> bool { |
| 917 | self.envp.iter().filter(|e| !e.is_empty()).count() == 0 |
| 918 | } |
| 919 | |
| 920 | /// Clears the `envp` arguments. |
| 921 | pub fn clear_envp(&mut self) { |
| 922 | self.envp.iter_mut().for_each(|s| s.clear()); |
| 923 | } |
| 924 | |
| 925 | /// Gets the [UdevDevice] monitor_buf. |
| 926 | pub fn monitor_buf(&self) -> &str { |
| 927 | self.monitor_buf.as_str() |
| 928 | } |
| 929 | |
| 930 | /// Sets the [UdevDevice] monitor_buf. |
| 931 | pub fn set_monitor_buf<P: Into<String>>(&mut self, monitor_buf: P) { |
| 932 | self.monitor_buf = monitor_buf.into(); |
| 933 | } |
| 934 | |
| 935 | /// Builder function that sets the [UdevDevice] monitor_buf. |
| 936 | pub fn with_monitor_buf<P: Into<String>>(mut self, monitor_buf: P) -> Self { |
| 937 | self.set_monitor_buf(monitor_buf); |
| 938 | self |
| 939 | } |
| 940 | |
| 941 | /// Gets the environment properties monitor buffer. |
| 942 | /// |
| 943 | /// If the environment is not up to date, updates the monitor buffer. |
| 944 | pub fn get_properties_monitor_buf(&mut self) -> &str { |
| 945 | if !self.envp_uptodate { |
| 946 | self.update_envp_monitor_buf(); |
| 947 | } |
| 948 | self.monitor_buf() |
| 949 | } |
| 950 | |
| 951 | /// Updates the `envp` and monitor buffer from the properties list. |
| 952 | pub fn update_envp_monitor_buf(&mut self) { |
| 953 | self.monitor_buf.clear(); |
| 954 | self.clear_envp(); |
| 955 | |
| 956 | // add at most `ENVP_LEN` properties, skipping private entries |
| 957 | for (i, list_entry) in self |
| 958 | .properties_list |
| 959 | .iter() |
| 960 | .filter(|e| !e.name().starts_with('.')) |
| 961 | .enumerate() |
| 962 | .take(ENVP_LEN) |
| 963 | { |
| 964 | let key = list_entry.name(); |
| 965 | let value = list_entry.value(); |
| 966 | |
| 967 | // add string to envp |
| 968 | let envp_str = format!("{key}={value}"); |
| 969 | |
| 970 | self.monitor_buf += envp_str.as_str(); |
| 971 | self.monitor_buf += "\0"; |
| 972 | |
| 973 | self.envp[i] = envp_str; |
| 974 | } |
| 975 | |
| 976 | self.set_envp_uptodate(true); |
| 977 | } |
| 978 | |
| 979 | /// Gets a reference to the [UdevDevice] `devlinks_list` [UdevList]. |
| 980 | pub const fn devlinks_list(&self) -> &UdevList { |
| 981 | &self.devlinks_list |
| 982 | } |
| 983 | |
| 984 | /// Gets a mutable reference to the [UdevDevice] `devlinks_list` [UdevList]. |
| 985 | pub fn devlinks_list_mut(&mut self) -> &mut UdevList { |
| 986 | &mut self.devlinks_list |
| 987 | } |
| 988 | |
| 989 | /// Gets the next entry in the `devlinks` list. |
| 990 | pub fn devlinks_list_entry(&self) -> Option<&UdevEntry> { |
| 991 | self.devlinks_list.next_entry() |
| 992 | } |
| 993 | |
| 994 | /// Gets the list of device links for the [UdevDevice]. |
| 995 | /// |
| 996 | /// From the `libudev` documentation: |
| 997 | /// |
| 998 | /// ```no_build,no_run |
| 999 | /// Retrieve the list of device links pointing to the device file of |
| 1000 | /// the udev device. The next list entry can be retrieved with |
| 1001 | /// udev_list_entry_get_next(), which returns #NULL if no more entries exist. |
| 1002 | /// |
| 1003 | /// The devlink path can be retrieved from the list entry by |
| 1004 | /// udev_list_entry_get_name(). The path is an absolute path, and starts with |
| 1005 | /// the device directory. |
| 1006 | /// ``` |
| 1007 | /// |
| 1008 | /// Returns: the first entry of the device node link list |
| 1009 | pub fn get_devlinks_list_entry(&mut self) -> Option<&UdevEntry> { |
| 1010 | if !self.info_loaded { |
| 1011 | self.read_db().ok()?; |
| 1012 | } |
| 1013 | self.devlinks_list_entry() |
| 1014 | } |
| 1015 | |
| 1016 | /// Sets the [UdevDevice] `devlinks_list` [UdevList]. |
| 1017 | pub fn set_devlinks_list<U: Into<UdevEntryList>>(&mut self, devlinks_list: U) { |
| 1018 | self.devlinks_list.set_list(devlinks_list); |
| 1019 | } |
| 1020 | |
| 1021 | /// Builder function sets the [UdevDevice] [UdevList]. |
| 1022 | pub fn with_devlinks_list<U: Into<UdevEntryList>>(mut self, devlinks_list: U) -> Self { |
| 1023 | self.set_devlinks_list(devlinks_list); |
| 1024 | self |
| 1025 | } |
| 1026 | |
| 1027 | /// Adds an [UdevEntry] to the devlinks list. |
| 1028 | pub fn add_devlink(&mut self, devlink: &str) { |
| 1029 | self.set_devlinks_uptodate(false); |
| 1030 | self.devlinks_list.add_entry(devlink, ""); |
| 1031 | } |
| 1032 | |
| 1033 | /// Gets a reference to the [UdevDevice] `properties_list` [UdevList]. |
| 1034 | pub const fn properties_list(&self) -> &UdevList { |
| 1035 | &self.properties_list |
| 1036 | } |
| 1037 | |
| 1038 | /// Gets a mutable reference to the [UdevDevice] `properties_list` [UdevList]. |
| 1039 | pub fn properties_list_mut(&mut self) -> &mut UdevList { |
| 1040 | &mut self.properties_list |
| 1041 | } |
| 1042 | |
| 1043 | /// Sets the [UdevDevice] `properties_list` [UdevList]. |
| 1044 | pub fn set_properties_list<U: Into<UdevEntryList>>(&mut self, properties_list: U) { |
| 1045 | self.properties_list.set_list(properties_list); |
| 1046 | } |
| 1047 | |
| 1048 | /// Builder function sets the [UdevDevice] [UdevList]. |
| 1049 | pub fn with_properties_list<U: Into<UdevEntryList>>(mut self, properties_list: U) -> Self { |
| 1050 | self.set_properties_list(properties_list); |
| 1051 | self |
| 1052 | } |
| 1053 | |
| 1054 | /// Gets the value of a given property. |
| 1055 | pub fn get_property_value(&self, key: &str) -> Option<&str> { |
| 1056 | self.properties_list.entry_by_name(key).map(|e| e.value()) |
| 1057 | } |
| 1058 | |
| 1059 | /// Gets a reference to the [UdevDevice] `sysattr_value_list` [UdevList]. |
| 1060 | pub const fn sysattr_value_list(&self) -> &UdevList { |
| 1061 | &self.sysattr_value_list |
| 1062 | } |
| 1063 | |
| 1064 | /// Gets a mutable reference to the [UdevDevice] `sysattr_value_list` [UdevList]. |
| 1065 | pub fn sysattr_value_list_mut(&mut self) -> &mut UdevList { |
| 1066 | &mut self.sysattr_value_list |
| 1067 | } |
| 1068 | |
| 1069 | /// Sets the [UdevDevice] `sysattr_value_list` [UdevList]. |
| 1070 | pub fn set_sysattr_value_list<U: Into<UdevEntryList>>(&mut self, sysattr_value_list: U) { |
| 1071 | self.sysattr_value_list.set_list(sysattr_value_list); |
| 1072 | } |
| 1073 | |
| 1074 | /// Builder function sets the [UdevDevice] [UdevList]. |
| 1075 | pub fn with_sysattr_value_list<U: Into<UdevEntryList>>( |
| 1076 | mut self, |
| 1077 | sysattr_value_list: U, |
| 1078 | ) -> Self { |
| 1079 | self.set_sysattr_value_list(sysattr_value_list); |
| 1080 | self |
| 1081 | } |
| 1082 | |
| 1083 | /// Gets a reference to the [UdevDevice] `sysattr_list` [UdevList]. |
| 1084 | pub const fn sysattr_list(&self) -> &UdevList { |
| 1085 | &self.sysattr_list |
| 1086 | } |
| 1087 | |
| 1088 | /// Gets a mutable reference to the [UdevDevice] `sysattr_list` [UdevList]. |
| 1089 | pub fn sysattr_list_mut(&mut self) -> &mut UdevList { |
| 1090 | &mut self.sysattr_list |
| 1091 | } |
| 1092 | |
| 1093 | /// Sets the [UdevDevice] `sysattr_list` [UdevList]. |
| 1094 | pub fn set_sysattr_list<U: Into<UdevEntryList>>(&mut self, sysattr_list: U) { |
| 1095 | self.sysattr_list.set_list(sysattr_list); |
| 1096 | } |
| 1097 | |
| 1098 | /// Builder function sets the [UdevDevice] [UdevList]. |
| 1099 | pub fn with_sysattr_list<U: Into<UdevEntryList>>(mut self, sysattr_list: U) -> Self { |
| 1100 | self.set_sysattr_list(sysattr_list); |
| 1101 | self |
| 1102 | } |
| 1103 | |
| 1104 | /// Gets the first entry in the `sysattr` properties list. |
| 1105 | /// |
| 1106 | /// From the `libudev` documentation: |
| 1107 | /// |
| 1108 | /// ```no_build,no_run |
| 1109 | /// Retrieve the list of available sysattrs, with value being empty; |
| 1110 | /// This just return all available sysfs attributes for a particular |
| 1111 | /// device without reading their values. |
| 1112 | /// ``` |
| 1113 | /// |
| 1114 | /// Returns: the first entry of the property list |
| 1115 | pub fn get_sysattr_list_entry(&mut self) -> Option<&UdevEntry> { |
| 1116 | let num = if !self.sysattr_list_read { |
| 1117 | self.get_sysattr_list_read().ok()? |
| 1118 | } else { |
| 1119 | 0 |
| 1120 | }; |
| 1121 | if num > 0 { |
| 1122 | None |
| 1123 | } else { |
| 1124 | self.sysattr_list.entry() |
| 1125 | } |
| 1126 | } |
| 1127 | |
| 1128 | /// Gets the sys attribute file value. |
| 1129 | /// |
| 1130 | /// From the `libudev` documentation: |
| 1131 | /// |
| 1132 | /// ```no_build,no_run |
| 1133 | /// The retrieved value is cached in the device. Repeated calls will return the same |
| 1134 | /// value and not open the attribute again. |
| 1135 | /// ``` |
| 1136 | /// |
| 1137 | /// Returns: the content of a sys attribute file, or `None` if there is no sys attribute value. |
| 1138 | pub fn get_sysattr_value(&mut self, sysattr: &str) -> Option<String> { |
| 1139 | match self.sysattr_value_list.entry_by_name(sysattr).cloned() { |
| 1140 | Some(entry) => Some(entry.value().to_owned()), |
| 1141 | None => { |
| 1142 | let path = format!("{}/{sysattr}", self.syspath); |
| 1143 | match fs::metadata(path.as_str()) { |
| 1144 | Ok(metadata) => { |
| 1145 | if metadata.is_symlink() { |
| 1146 | if sysattr == "driver" || sysattr == "subsystem" || sysattr == "module" |
| 1147 | { |
| 1148 | let value = |
| 1149 | Udev::get_sys_core_link_value(sysattr, self.syspath.as_str()) |
| 1150 | .ok()?; |
| 1151 | Some( |
| 1152 | self.sysattr_value_list |
| 1153 | .add_entry(sysattr, value.as_str())? |
| 1154 | .value() |
| 1155 | .to_owned(), |
| 1156 | ) |
| 1157 | } else { |
| 1158 | None |
| 1159 | } |
| 1160 | } else if metadata.is_dir() |
| 1161 | || metadata.len() == 0 |
| 1162 | || metadata.st_mode() & libc::S_IRUSR == 0 |
| 1163 | { |
| 1164 | None |
| 1165 | } else { |
| 1166 | let mut file = fs::File::open(path.as_str()).ok()?; |
| 1167 | let mut value = [0u8; 4096]; |
| 1168 | let read = file.read(&mut value).ok()?; |
| 1169 | let value_str = |
| 1170 | std::str::from_utf8(value[..read].as_ref()).unwrap_or(""); |
| 1171 | let entry = self.sysattr_value_list.add_entry(sysattr, value_str)?; |
| 1172 | |
| 1173 | Some(entry.value().to_owned()) |
| 1174 | } |
| 1175 | } |
| 1176 | Err(_) => { |
| 1177 | self.sysattr_value_list.add_entry(sysattr, ""); |
| 1178 | None |
| 1179 | } |
| 1180 | } |
| 1181 | } |
| 1182 | } |
| 1183 | } |
| 1184 | |
| 1185 | /// Gets a reference to the [UdevDevice] `tags_list` [UdevList]. |
| 1186 | pub const fn tags_list(&self) -> &UdevList { |
| 1187 | &self.tags_list |
| 1188 | } |
| 1189 | |
| 1190 | /// Gets a mutable reference to the [UdevDevice] `tags_list` [UdevList]. |
| 1191 | pub fn tags_list_mut(&mut self) -> &mut UdevList { |
| 1192 | &mut self.tags_list |
| 1193 | } |
| 1194 | |
| 1195 | /// Sets the [UdevDevice] `tags_list` [UdevList]. |
| 1196 | pub fn set_tags_list<U: Into<UdevEntryList>>(&mut self, tags_list: U) { |
| 1197 | self.tags_list.set_list(tags_list); |
| 1198 | } |
| 1199 | |
| 1200 | /// Builder function sets the [UdevDevice] [UdevList]. |
| 1201 | pub fn with_tags_list<U: Into<UdevEntryList>>(mut self, tags_list: U) -> Self { |
| 1202 | self.set_tags_list(tags_list); |
| 1203 | self |
| 1204 | } |
| 1205 | |
| 1206 | /// Gets the first tags list entry in the [UdevDevice]. |
| 1207 | pub fn tags_list_entry(&self) -> Option<&UdevEntry> { |
| 1208 | self.tags_list.entry() |
| 1209 | } |
| 1210 | |
| 1211 | /// Gets the first tags list entry in the [UdevDevice]. |
| 1212 | /// |
| 1213 | /// From the `libudev` documentation: |
| 1214 | /// |
| 1215 | /// ```no_build,no_run |
| 1216 | /// Retrieve the list of tags attached to the udev device. The next |
| 1217 | /// list entry can be retrieved with udev_list_entry_get_next(), |
| 1218 | /// which returns `None` if no more entries exist. The tag string |
| 1219 | /// can be retrieved from the list entry by udev_list_entry_get_name(). |
| 1220 | /// ``` |
| 1221 | /// |
| 1222 | /// Returns: the first entry of the tag list |
| 1223 | pub fn get_tags_list_entry(&mut self) -> Option<&UdevEntry> { |
| 1224 | if !self.info_loaded { |
| 1225 | self.read_db().ok(); |
| 1226 | } |
| 1227 | self.tags_list_entry() |
| 1228 | } |
| 1229 | |
| 1230 | /// Gets the current tags list entry in the [UdevDevice]. |
| 1231 | /// |
| 1232 | /// From the `libudev` documentation: |
| 1233 | /// |
| 1234 | /// ```no_build,no_run |
| 1235 | /// Retrieve the list of tags attached to the udev device. The next |
| 1236 | /// list entry can be retrieved with udev_list_entry_get_next(), |
| 1237 | /// which returns `None` if no more entries exist. The tag string |
| 1238 | /// can be retrieved from the list entry by udev_list_entry_get_name(). |
| 1239 | /// ``` |
| 1240 | /// |
| 1241 | /// Returns: the current entry of the tag list |
| 1242 | pub fn get_current_tags_list_entry(&mut self) -> Option<&UdevEntry> { |
| 1243 | if !self.info_loaded { |
| 1244 | self.read_db().ok(); |
| 1245 | } |
| 1246 | self.tags_list.next_entry() |
| 1247 | } |
| 1248 | |
| 1249 | /// Adds an [UdevEntry] to the tags list. |
| 1250 | pub fn add_tag(&mut self, tag: &str) -> Result<()> { |
| 1251 | Self::is_valid_tag(tag)?; |
| 1252 | |
| 1253 | self.set_tags_uptodate(false); |
| 1254 | self.tags_list.add_entry(tag, ""); |
| 1255 | |
| 1256 | Ok(()) |
| 1257 | } |
| 1258 | |
| 1259 | fn is_valid_tag(tag: &str) -> Result<()> { |
| 1260 | if tag.contains(':') || tag.contains(' ') { |
| 1261 | Err(Error::Udev("device: invalid tag".into())) |
| 1262 | } else { |
| 1263 | Ok(()) |
| 1264 | } |
| 1265 | } |
| 1266 | |
| 1267 | /// Gets whether the [UdevDevice] has the provided `tag` associated. |
| 1268 | /// |
| 1269 | /// From the `libudev` documentation: |
| 1270 | /// |
| 1271 | /// ```no_build,no_run |
| 1272 | /// Check if a given device has a certain tag associated. |
| 1273 | /// ``` |
| 1274 | pub fn has_tag(&mut self, tag: &str) -> bool { |
| 1275 | if !self.info_loaded { |
| 1276 | self.read_db().ok(); |
| 1277 | } |
| 1278 | self.tags_list.entry_by_name(tag).is_some() |
| 1279 | } |
| 1280 | |
| 1281 | /// Gets the [UdevDevice] seqnum. |
| 1282 | pub const fn seqnum(&self) -> u64 { |
| 1283 | self.seqnum |
| 1284 | } |
| 1285 | |
| 1286 | /// Sets the [UdevDevice] seqnum. |
| 1287 | pub fn set_seqnum(&mut self, seqnum: u64) { |
| 1288 | self.seqnum = seqnum; |
| 1289 | } |
| 1290 | |
| 1291 | /// Builder function sets the [UdevDevice] seqnum. |
| 1292 | pub fn with_seqnum(mut self, seqnum: u64) -> Self { |
| 1293 | self.set_seqnum(seqnum); |
| 1294 | self |
| 1295 | } |
| 1296 | |
| 1297 | /// Gets the [UdevDevice] usec_initialized. |
| 1298 | pub const fn usec_initialized(&self) -> u64 { |
| 1299 | self.usec_initialized |
| 1300 | } |
| 1301 | |
| 1302 | /// Sets the [UdevDevice] usec_initialized. |
| 1303 | pub fn set_usec_initialized(&mut self, usec_initialized: u64) { |
| 1304 | self.usec_initialized = usec_initialized; |
| 1305 | } |
| 1306 | |
| 1307 | /// Builder function sets the [UdevDevice] usec_initialized. |
| 1308 | pub fn with_usec_initialized(mut self, usec_initialized: u64) -> Self { |
| 1309 | self.set_usec_initialized(usec_initialized); |
| 1310 | self |
| 1311 | } |
| 1312 | |
| 1313 | /// Gets the number of microseconds since the [UdevDevice] was initialized. |
| 1314 | /// |
| 1315 | /// From the `libudev` documentation: |
| 1316 | /// |
| 1317 | /// ```no_build,no_run |
| 1318 | /// Return the number of microseconds passed since udev set up the |
| 1319 | /// device for the first time. |
| 1320 | /// |
| 1321 | /// This is only implemented for devices with need to store properties |
| 1322 | /// in the udev database. All other devices return 0 here. |
| 1323 | /// ``` |
| 1324 | /// |
| 1325 | /// Returns: the number of microseconds since the device was first seen. |
| 1326 | pub fn get_usec_since_initialized(&mut self) -> u64 { |
| 1327 | if !self.info_loaded { |
| 1328 | self.read_db().ok(); |
| 1329 | } |
| 1330 | if self.usec_initialized == 0 { |
| 1331 | 0 |
| 1332 | } else { |
| 1333 | time::SystemTime::now() |
| 1334 | .duration_since(time::UNIX_EPOCH) |
| 1335 | .unwrap_or(time::Duration::from_micros(self.usec_initialized)) |
| 1336 | .as_micros() |
| 1337 | .saturating_sub(self.usec_initialized as u128) as u64 |
| 1338 | } |
| 1339 | } |
| 1340 | |
| 1341 | /// Gets the [UdevDevice] devlink priority. |
| 1342 | pub const fn devlink_priority(&self) -> i32 { |
| 1343 | self.devlink_priority |
| 1344 | } |
| 1345 | |
| 1346 | /// Sets the [UdevDevice] devlink priority. |
| 1347 | pub fn set_devlink_priority(&mut self, devlink_priority: i32) { |
| 1348 | self.devlink_priority = devlink_priority; |
| 1349 | } |
| 1350 | |
| 1351 | /// Builder function sets the [UdevDevice] devlink priority. |
| 1352 | pub fn with_devlink_priority(mut self, devlink_priority: i32) -> Self { |
| 1353 | self.set_devlink_priority(devlink_priority); |
| 1354 | self |
| 1355 | } |
| 1356 | |
| 1357 | /// Gets the [UdevDevice] devnum. |
| 1358 | pub const fn devnum(&self) -> u64 { |
| 1359 | self.devnum |
| 1360 | } |
| 1361 | |
| 1362 | /// Sets the [UdevDevice] devnum. |
| 1363 | pub fn set_devnum(&mut self, devnum: u64) { |
| 1364 | self.devnum = devnum; |
| 1365 | } |
| 1366 | |
| 1367 | /// Builder function sets the [UdevDevice] devnum. |
| 1368 | pub fn with_devnum(mut self, devnum: u64) -> Self { |
| 1369 | self.set_devnum(devnum); |
| 1370 | self |
| 1371 | } |
| 1372 | |
| 1373 | /// Gets the device major/minor number. |
| 1374 | pub fn get_devnum(&mut self) -> u64 { |
| 1375 | if !self.info_loaded { |
| 1376 | self.read_uevent_file().ok(); |
| 1377 | } |
| 1378 | self.devnum |
| 1379 | } |
| 1380 | |
| 1381 | /// Gets the [UdevDevice] ifindex. |
| 1382 | pub const fn ifindex(&self) -> i32 { |
| 1383 | self.ifindex |
| 1384 | } |
| 1385 | |
| 1386 | /// Sets the [UdevDevice] ifindex. |
| 1387 | pub fn set_ifindex(&mut self, ifindex: i32) { |
| 1388 | self.ifindex = ifindex; |
| 1389 | } |
| 1390 | |
| 1391 | /// Builder function sets the [UdevDevice] ifindex. |
| 1392 | pub fn with_ifindex(mut self, ifindex: i32) -> Self { |
| 1393 | self.set_ifindex(ifindex); |
| 1394 | self |
| 1395 | } |
| 1396 | |
| 1397 | /// Gets the [UdevDevice] ifindex. |
| 1398 | /// |
| 1399 | /// If the information file is not loaded, reads and parses the file from disk. |
| 1400 | pub fn get_ifindex(&mut self) -> i32 { |
| 1401 | if !self.info_loaded { |
| 1402 | self.read_uevent_file().ok(); |
| 1403 | } |
| 1404 | self.ifindex |
| 1405 | } |
| 1406 | |
| 1407 | /// Gets the [UdevDevice] watch handle. |
| 1408 | pub const fn watch_handle(&self) -> i32 { |
| 1409 | self.watch_handle |
| 1410 | } |
| 1411 | |
| 1412 | /// Sets the [UdevDevice] watch handle. |
| 1413 | pub fn set_watch_handle(&mut self, watch_handle: i32) { |
| 1414 | self.watch_handle = watch_handle; |
| 1415 | } |
| 1416 | |
| 1417 | /// Builder function sets the [UdevDevice] watch handle. |
| 1418 | pub fn with_watch_handle(mut self, watch_handle: i32) -> Self { |
| 1419 | self.set_watch_handle(watch_handle); |
| 1420 | self |
| 1421 | } |
| 1422 | |
| 1423 | /// Gets the [UdevDevice] major number. |
| 1424 | pub const fn maj(&self) -> i32 { |
| 1425 | self.maj |
| 1426 | } |
| 1427 | |
| 1428 | /// Sets the [UdevDevice] major number. |
| 1429 | pub fn set_maj(&mut self, maj: i32) { |
| 1430 | self.maj = maj; |
| 1431 | } |
| 1432 | |
| 1433 | /// Builder function sets the [UdevDevice] major number. |
| 1434 | pub fn with_maj(mut self, maj: i32) -> Self { |
| 1435 | self.set_maj(maj); |
| 1436 | self |
| 1437 | } |
| 1438 | |
| 1439 | /// Gets the [UdevDevice] minor number. |
| 1440 | pub const fn min(&self) -> i32 { |
| 1441 | self.min |
| 1442 | } |
| 1443 | |
| 1444 | /// Sets the [UdevDevice] minor number. |
| 1445 | pub fn set_min(&mut self, min: i32) { |
| 1446 | self.min = min; |
| 1447 | } |
| 1448 | |
| 1449 | /// Builder function sets the [UdevDevice] minor number. |
| 1450 | pub fn with_min(mut self, min: i32) -> Self { |
| 1451 | self.set_min(min); |
| 1452 | self |
| 1453 | } |
| 1454 | |
| 1455 | /// Gets the [UdevDevice] devlinks up-to-date. |
| 1456 | pub const fn devlinks_uptodate(&self) -> bool { |
| 1457 | self.devlinks_uptodate |
| 1458 | } |
| 1459 | |
| 1460 | /// Sets the [UdevDevice] devlinks up-to-date. |
| 1461 | pub fn set_devlinks_uptodate(&mut self, devlinks_uptodate: bool) { |
| 1462 | self.devlinks_uptodate = devlinks_uptodate; |
| 1463 | } |
| 1464 | |
| 1465 | /// Builder function sets the [UdevDevice] devlinks up-to-date. |
| 1466 | pub fn with_devlinks_uptodate(mut self, devlinks_uptodate: bool) -> Self { |
| 1467 | self.set_devlinks_uptodate(devlinks_uptodate); |
| 1468 | self |
| 1469 | } |
| 1470 | |
| 1471 | /// Gets the [UdevDevice] envp up-to-date. |
| 1472 | pub const fn envp_uptodate(&self) -> bool { |
| 1473 | self.envp_uptodate |
| 1474 | } |
| 1475 | |
| 1476 | /// Sets the [UdevDevice] envp up-to-date. |
| 1477 | pub fn set_envp_uptodate(&mut self, envp_uptodate: bool) { |
| 1478 | self.envp_uptodate = envp_uptodate; |
| 1479 | } |
| 1480 | |
| 1481 | /// Builder function sets the [UdevDevice] envp up-to-date. |
| 1482 | pub fn with_envp_uptodate(mut self, envp_uptodate: bool) -> Self { |
| 1483 | self.set_envp_uptodate(envp_uptodate); |
| 1484 | self |
| 1485 | } |
| 1486 | |
| 1487 | /// Gets the [UdevDevice] tags up-to-date. |
| 1488 | pub const fn tags_uptodate(&self) -> bool { |
| 1489 | self.tags_uptodate |
| 1490 | } |
| 1491 | |
| 1492 | /// Sets the [UdevDevice] tags up-to-date. |
| 1493 | pub fn set_tags_uptodate(&mut self, tags_uptodate: bool) { |
| 1494 | self.tags_uptodate = tags_uptodate; |
| 1495 | } |
| 1496 | |
| 1497 | /// Builder function sets the [UdevDevice] tags up-to-date. |
| 1498 | pub fn with_tags_uptodate(mut self, tags_uptodate: bool) -> Self { |
| 1499 | self.set_tags_uptodate(tags_uptodate); |
| 1500 | self |
| 1501 | } |
| 1502 | |
| 1503 | /// Gets the [UdevDevice] info loaded. |
| 1504 | pub const fn info_loaded(&self) -> bool { |
| 1505 | self.info_loaded |
| 1506 | } |
| 1507 | |
| 1508 | /// Sets the [UdevDevice] info loaded. |
| 1509 | pub fn set_info_loaded(&mut self, info_loaded: bool) { |
| 1510 | self.info_loaded = info_loaded; |
| 1511 | } |
| 1512 | |
| 1513 | /// Builder function sets the [UdevDevice] info loaded. |
| 1514 | pub fn with_info_loaded(mut self, info_loaded: bool) -> Self { |
| 1515 | self.set_info_loaded(info_loaded); |
| 1516 | self |
| 1517 | } |
| 1518 | |
| 1519 | /// Gets the [UdevDevice] db loaded. |
| 1520 | pub const fn db_loaded(&self) -> bool { |
| 1521 | self.db_loaded |
| 1522 | } |
| 1523 | |
| 1524 | /// Sets the [UdevDevice] db loaded. |
| 1525 | pub fn set_db_loaded(&mut self, db_loaded: bool) { |
| 1526 | self.db_loaded = db_loaded; |
| 1527 | } |
| 1528 | |
| 1529 | /// Builder function sets the [UdevDevice] db loaded. |
| 1530 | pub fn with_db_loaded(mut self, db_loaded: bool) -> Self { |
| 1531 | self.set_db_loaded(db_loaded); |
| 1532 | self |
| 1533 | } |
| 1534 | |
| 1535 | /// Gets the [UdevDevice] uevent loaded. |
| 1536 | pub const fn uevent_loaded(&self) -> bool { |
| 1537 | self.uevent_loaded |
| 1538 | } |
| 1539 | |
| 1540 | /// Sets the [UdevDevice] uevent loaded. |
| 1541 | pub fn set_uevent_loaded(&mut self, uevent_loaded: bool) { |
| 1542 | self.uevent_loaded = uevent_loaded; |
| 1543 | } |
| 1544 | |
| 1545 | /// Builder function sets the [UdevDevice] uevent loaded. |
| 1546 | pub fn with_uevent_loaded(mut self, uevent_loaded: bool) -> Self { |
| 1547 | self.set_uevent_loaded(uevent_loaded); |
| 1548 | self |
| 1549 | } |
| 1550 | |
| 1551 | /// Gets the [UdevDevice] is initialized. |
| 1552 | pub const fn is_initialized(&self) -> bool { |
| 1553 | self.is_initialized |
| 1554 | } |
| 1555 | |
| 1556 | /// Sets the [UdevDevice] is initialized. |
| 1557 | pub fn set_is_initialized(&mut self, is_initialized: bool) { |
| 1558 | self.is_initialized = is_initialized; |
| 1559 | } |
| 1560 | |
| 1561 | /// Builder function sets the [UdevDevice] is initialized. |
| 1562 | pub fn with_is_initialized(mut self, is_initialized: bool) -> Self { |
| 1563 | self.set_is_initialized(is_initialized); |
| 1564 | self |
| 1565 | } |
| 1566 | |
| 1567 | /// Gets whether the [UdevDevice] is initialized. |
| 1568 | /// |
| 1569 | /// From the `libudev` documentation: |
| 1570 | /// |
| 1571 | /// ```no_build,no_run |
| 1572 | /// Check if udev has already handled the device and has set up |
| 1573 | /// device node permissions and context, or has renamed a network |
| 1574 | /// device. |
| 1575 | /// |
| 1576 | /// This is only implemented for devices with a device node |
| 1577 | /// or network interfaces. All other devices return 1 here. |
| 1578 | /// ``` |
| 1579 | pub fn get_is_initialized(&mut self) -> bool { |
| 1580 | if !self.info_loaded { |
| 1581 | self.read_db().ok(); |
| 1582 | } |
| 1583 | self.is_initialized |
| 1584 | } |
| 1585 | |
| 1586 | /// Gets the [UdevDevice] sysattr list read. |
| 1587 | pub const fn sysattr_list_read(&self) -> bool { |
| 1588 | self.sysattr_list_read |
| 1589 | } |
| 1590 | |
| 1591 | /// Sets the [UdevDevice] sysattr list read. |
| 1592 | pub fn set_sysattr_list_read(&mut self, sysattr_list_read: bool) { |
| 1593 | self.sysattr_list_read = sysattr_list_read; |
| 1594 | } |
| 1595 | |
| 1596 | /// Builder function sets the [UdevDevice] sysattr list read. |
| 1597 | pub fn with_sysattr_list_read(mut self, sysattr_list_read: bool) -> Self { |
| 1598 | self.set_sysattr_list_read(sysattr_list_read); |
| 1599 | self |
| 1600 | } |
| 1601 | |
| 1602 | pub fn get_sysattr_list_read(&mut self) -> Result<usize> { |
| 1603 | let mut num = 0usize; |
| 1604 | if self.sysattr_list_read { |
| 1605 | Ok(num) |
| 1606 | } else { |
| 1607 | let syspath = self.syspath().to_owned(); |
| 1608 | |
| 1609 | for dir_entry in fs::read_dir(syspath.as_str()) |
| 1610 | .map_err(|err| Error::UdevDevice(format!("unable to open device syspath: {err}")))? |
| 1611 | { |
| 1612 | let entry = match dir_entry { |
| 1613 | Ok(e) => e, |
| 1614 | Err(_) => continue, |
| 1615 | }; |
| 1616 | |
| 1617 | let file_type = entry.file_type().map_err(|err| { |
| 1618 | Error::UdevDevice(format!("unable to get syspath entry file type: {err}")) |
| 1619 | })?; |
| 1620 | |
| 1621 | if !file_type.is_symlink() && !file_type.is_file() { |
| 1622 | continue; |
| 1623 | } |
| 1624 | |
| 1625 | let name = match entry.file_name().into_string() { |
| 1626 | Ok(n) => n, |
| 1627 | Err(_) => continue, |
| 1628 | }; |
| 1629 | |
| 1630 | let path = format!("{}/{name}", syspath); |
| 1631 | let metadata = match fs::metadata(path.as_str()) { |
| 1632 | Ok(m) => m, |
| 1633 | Err(_) => continue, |
| 1634 | }; |
| 1635 | if metadata.st_mode() & libc::S_IRUSR != 0 { |
| 1636 | continue; |
| 1637 | } |
| 1638 | |
| 1639 | self.sysattr_list.add_entry(name.as_str(), ""); |
| 1640 | num = num.saturating_add(1); |
| 1641 | } |
| 1642 | |
| 1643 | self.sysattr_list_read = true; |
| 1644 | |
| 1645 | Ok(num) |
| 1646 | } |
| 1647 | } |
| 1648 | |
| 1649 | /// Gets the [UdevDevice] database persist. |
| 1650 | pub const fn db_persist(&self) -> bool { |
| 1651 | self.db_persist |
| 1652 | } |
| 1653 | |
| 1654 | /// Sets the [UdevDevice] database persist. |
| 1655 | pub fn set_db_persist(&mut self, db_persist: bool) { |
| 1656 | self.db_persist = db_persist; |
| 1657 | } |
| 1658 | |
| 1659 | /// Builder function sets the [UdevDevice] database persist. |
| 1660 | pub fn with_db_persist(mut self, db_persist: bool) -> Self { |
| 1661 | self.set_db_persist(db_persist); |
| 1662 | self |
| 1663 | } |
| 1664 | |
| 1665 | /// Reads [UdevDevice] information from the persistent database file. |
| 1666 | /// |
| 1667 | /// Returns: `Ok(())` on success, `Err(Error)` otherwise |
| 1668 | pub fn read_db(&mut self) -> Result<()> { |
| 1669 | let id = self.get_id_filename().to_owned(); |
| 1670 | if self.db_loaded() { |
| 1671 | Ok(()) |
| 1672 | } else if id.is_empty() { |
| 1673 | Err(Error::UdevDevice("unable to retrieve ID filename".into())) |
| 1674 | } else { |
| 1675 | let filename = format!("/udev/data/{id}"); |
| 1676 | let file = fs::File::open(filename.as_str()).map_err(|err| { |
| 1677 | Error::UdevDevice(format!("unable to open DB file: {filename}, error: {err}")) |
| 1678 | })?; |
| 1679 | |
| 1680 | // devices with a database entry are initialized |
| 1681 | self.is_initialized = true; |
| 1682 | |
| 1683 | let mut line = String::with_capacity(util::LINE_SIZE); |
| 1684 | let mut reader = io::BufReader::new(file); |
| 1685 | |
| 1686 | while let Ok(len) = reader.read_line(&mut line) { |
| 1687 | if len < 4 { |
| 1688 | break; |
| 1689 | } |
| 1690 | let val = &line[2..]; |
| 1691 | match val.chars().next() { |
| 1692 | Some('S') => { |
| 1693 | self.add_devlink(format!("/dev/{val}").as_str()); |
| 1694 | break; |
| 1695 | } |
| 1696 | Some('L') => { |
| 1697 | self.set_devlink_priority(val.parse::<i32>().unwrap_or(0)); |
| 1698 | break; |
| 1699 | } |
| 1700 | Some('E') => { |
| 1701 | if self.add_property_from_string(val).is_some() { |
| 1702 | if let Some(entry) = self.properties_list.list_mut().back_mut() { |
| 1703 | entry.set_num(1); |
| 1704 | } |
| 1705 | } |
| 1706 | break; |
| 1707 | } |
| 1708 | Some('G') => { |
| 1709 | self.add_tag(val)?; |
| 1710 | break; |
| 1711 | } |
| 1712 | Some('W') => { |
| 1713 | self.set_watch_handle(val.parse::<i32>().unwrap_or(0)); |
| 1714 | break; |
| 1715 | } |
| 1716 | Some('I') => { |
| 1717 | self.set_usec_initialized(val.parse::<u64>().unwrap_or(0)); |
| 1718 | break; |
| 1719 | } |
| 1720 | _ => (), |
| 1721 | } |
| 1722 | } |
| 1723 | |
| 1724 | log::trace!("device {self} filled with DB file data"); |
| 1725 | |
| 1726 | Ok(()) |
| 1727 | } |
| 1728 | } |
| 1729 | |
| 1730 | /// Reads properties from the `uevent` file. |
| 1731 | pub fn read_uevent_file(&mut self) -> Result<()> { |
| 1732 | if !self.uevent_loaded { |
| 1733 | Ok(()) |
| 1734 | } else { |
| 1735 | let filename = format!("{}/uevent", self.syspath()); |
| 1736 | let f = fs::OpenOptions::new().read(true).open(filename)?; |
| 1737 | |
| 1738 | self.uevent_loaded = true; |
| 1739 | |
| 1740 | let mut reader = io::BufReader::new(f.take(UEVENT_FILE_LIMIT as u64)); |
| 1741 | let mut line = String::new(); |
| 1742 | |
| 1743 | let (mut maj, mut min) = (0u32, 0u32); |
| 1744 | |
| 1745 | while let Ok(read) = reader.read_line(&mut line) { |
| 1746 | let tline = line.trim_end_matches('\n'); |
| 1747 | |
| 1748 | if let Some(devtype) = tline.strip_prefix("DEVTYPE=") { |
| 1749 | self.set_devtype(devtype); |
| 1750 | continue; |
| 1751 | } |
| 1752 | if let Some(ifindex) = tline.strip_prefix("IFINDEX=") { |
| 1753 | self.set_ifindex(ifindex.parse::<i32>().unwrap_or(0)); |
| 1754 | continue; |
| 1755 | } |
| 1756 | if let Some(devname) = tline.strip_prefix("DEVNAME=") { |
| 1757 | self.set_devnode(devname); |
| 1758 | continue; |
| 1759 | } |
| 1760 | |
| 1761 | if let Some(major) = tline.strip_prefix("MAJOR=") { |
| 1762 | maj = major.parse::<u32>().unwrap_or(0); |
| 1763 | } else if let Some(minor) = tline.strip_prefix("MINOR=") { |
| 1764 | min = minor.parse::<u32>().unwrap_or(0); |
| 1765 | } else if let Some(devmode) = tline.strip_prefix("DEVMODE=") { |
| 1766 | self.set_devnode_mode(u32::from_str_radix(devmode, 8).unwrap_or(0).into()); |
| 1767 | } |
| 1768 | |
| 1769 | self.add_property_from_string(tline); |
| 1770 | |
| 1771 | if read == 0 { |
| 1772 | break; |
| 1773 | } |
| 1774 | } |
| 1775 | |
| 1776 | self.set_devnum(libc::makedev(maj, min)); |
| 1777 | |
| 1778 | Ok(()) |
| 1779 | } |
| 1780 | } |
| 1781 | |
| 1782 | /// Parses the `property` string, and adds an [UdevEntry] to the properties list. |
| 1783 | pub fn add_property_from_string(&mut self, property: &str) -> Option<&UdevEntry> { |
| 1784 | let mut pit = property.split('=').take(2); |
| 1785 | |
| 1786 | let name = pit.next().unwrap_or(""); |
| 1787 | let value = pit.next().unwrap_or(""); |
| 1788 | |
| 1789 | self.add_property_internal(name, value) |
| 1790 | } |
| 1791 | |
| 1792 | fn add_property_internal(&mut self, key: &str, value: &str) -> Option<&UdevEntry> { |
| 1793 | if key.is_empty() { |
| 1794 | None |
| 1795 | } else { |
| 1796 | self.set_envp_uptodate(false); |
| 1797 | if value.is_empty() { |
| 1798 | // remove the matching property if it already exists |
| 1799 | self.properties_list_mut().remove_entry(key); |
| 1800 | None |
| 1801 | } else { |
| 1802 | self.properties_list_mut().add_entry(key, value) |
| 1803 | } |
| 1804 | } |
| 1805 | } |
| 1806 | |
| 1807 | /// Parses property string, and if needed, updates internal values accordingly. |
| 1808 | /// |
| 1809 | /// From `libudev` documentation: |
| 1810 | /// |
| 1811 | /// [add_property_from_string_parse_finish()](Self::add_property_from_string_parse_finish) needs to be |
| 1812 | /// called after adding properties, and its return value checked. |
| 1813 | /// |
| 1814 | /// [set_info_loaded()](Self::set_info_loaded) needs to be set, to avoid trying |
| 1815 | /// to use a device without a `DEVPATH` set. |
| 1816 | pub fn add_property_from_string_parse(&mut self, property: &str) -> Result<()> { |
| 1817 | if let Some(path) = property.strip_prefix("DEVPATH=") { |
| 1818 | self.set_syspath(path); |
| 1819 | } else if let Some(path) = property.strip_prefix("SUBSYSTEM=") { |
| 1820 | self.set_subsystem(path); |
| 1821 | } else if let Some(devtype) = property.strip_prefix("DEVTYPE=") { |
| 1822 | self.set_devtype(devtype); |
| 1823 | } else if let Some(devname) = property.strip_prefix("DEVNAME=") { |
| 1824 | self.set_devnode(devname); |
| 1825 | } else if let Some(devlinks) = property.strip_prefix("DEVLINKS=") { |
| 1826 | for link in devlinks.split(' ') { |
| 1827 | if !link.is_empty() && !link.starts_with('\0') { |
| 1828 | self.add_devlink(link); |
| 1829 | } |
| 1830 | } |
| 1831 | } else if let Some(tags) = property.strip_prefix("TAGS=") { |
| 1832 | for tag in tags.split(':') { |
| 1833 | if !tag.is_empty() && !tag.starts_with('\0') { |
| 1834 | self.add_tag(tag)?; |
| 1835 | } |
| 1836 | } |
| 1837 | } else if let Some(usec_init) = property.strip_prefix("USEC_INITIALIZED=") { |
| 1838 | self.set_usec_initialized(usec_init.parse::<u64>().unwrap_or(0)); |
| 1839 | } else if let Some(driver) = property.strip_prefix("DRIVER=") { |
| 1840 | self.set_driver(driver); |
| 1841 | } else if let Some(action) = property.strip_prefix("ACTION=") { |
| 1842 | self.set_action(action); |
| 1843 | } else if let Some(major) = property.strip_prefix("MAJOR=") { |
| 1844 | self.set_maj(major.parse::<i32>().unwrap_or(0)); |
| 1845 | } else if let Some(minor) = property.strip_prefix("MINOR=") { |
| 1846 | self.set_min(minor.parse::<i32>().unwrap_or(0)); |
| 1847 | } else if let Some(devpath_old) = property.strip_prefix("DEVPATH_OLD=") { |
| 1848 | self.set_devpath_old(devpath_old); |
| 1849 | } else if let Some(seqnum) = property.strip_prefix("SEQNUM=") { |
| 1850 | self.set_seqnum(seqnum.parse::<u64>().unwrap_or(0)); |
| 1851 | } else if let Some(ifindex) = property.strip_prefix("IFINDEX=") { |
| 1852 | self.set_ifindex(ifindex.parse::<i32>().unwrap_or(0)); |
| 1853 | } else if let Some(devmode) = property.strip_prefix("DEVMODE=") { |
| 1854 | self.set_devnode_mode(devmode.parse::<u32>().unwrap_or(0).into()); |
| 1855 | } else if let Some(devuid) = property.strip_prefix("DEVUID=") { |
| 1856 | self.set_devnode_uid(devuid.parse::<u32>().unwrap_or(0)); |
| 1857 | } else if let Some(devgid) = property.strip_prefix("DEVGID=") { |
| 1858 | self.set_devnode_gid(devgid.parse::<u32>().unwrap_or(0)); |
| 1859 | } else { |
| 1860 | self.add_property_from_string(property); |
| 1861 | } |
| 1862 | |
| 1863 | Ok(()) |
| 1864 | } |
| 1865 | |
| 1866 | /// Finishes adding property information after parsing configuration string. |
| 1867 | /// |
| 1868 | /// **NOTE** users should call this function after the final call to |
| 1869 | /// (add_property_from_string_parse)[Self::add_property_from_string_parse]. |
| 1870 | pub fn add_property_from_string_parse_finish(&mut self) -> Result<()> { |
| 1871 | if self.maj() > 0 { |
| 1872 | self.set_devnum(libc::makedev(self.maj() as u32, self.min() as u32)); |
| 1873 | } |
| 1874 | |
| 1875 | self.set_maj(0); |
| 1876 | self.set_min(0); |
| 1877 | |
| 1878 | if self.devpath().is_empty() || self.subsystem().is_empty() { |
| 1879 | log::debug!("device: empty devpath and/or subsystem"); |
| 1880 | } |
| 1881 | |
| 1882 | Ok(()) |
| 1883 | } |
| 1884 | } |
| 1885 | |
| 1886 | impl Default for UdevDevice { |
| 1887 | fn default() -> Self { |
| 1888 | Self::new(Arc::new(Udev::new())) |
| 1889 | } |
| 1890 | } |
| 1891 | |
| 1892 | impl fmt::Display for UdevDevice { |
| 1893 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 1894 | write!(f, "{{")?; |
| 1895 | write!(f, r#""syspath": {},"#, self.syspath)?; |
| 1896 | write!(f, r#""devpath": {},"#, self.devpath)?; |
| 1897 | write!(f, r#""sysname": {},"#, self.sysname)?; |
| 1898 | write!(f, r#""sysnum": {},"#, self.sysnum)?; |
| 1899 | write!(f, r#""devnode": {},"#, self.devnode)?; |
| 1900 | write!(f, r#""devnode_mode": {},"#, self.devnode_mode)?; |
| 1901 | write!(f, r#""devnode_uid": {},"#, self.devnode_uid)?; |
| 1902 | write!(f, r#""devnode_gid": {},"#, self.devnode_gid)?; |
| 1903 | write!(f, r#""subsystem": {},"#, self.subsystem)?; |
| 1904 | write!(f, r#""devtype": {},"#, self.devtype)?; |
| 1905 | write!(f, r#""driver": {},"#, self.driver)?; |
| 1906 | write!(f, r#""action": {},"#, self.action)?; |
| 1907 | write!(f, r#""devpath_old": {},"#, self.devpath_old)?; |
| 1908 | write!(f, r#""id_filename": {},"#, self.id_filename)?; |
| 1909 | write!(f, r#""monitor_buf": {},"#, self.monitor_buf)?; |
| 1910 | write!(f, r#""seqnum": {},"#, self.seqnum)?; |
| 1911 | write!(f, r#""usec_initialized": {},"#, self.usec_initialized)?; |
| 1912 | write!(f, r#""devlink_priority": {},"#, self.devlink_priority)?; |
| 1913 | write!(f, r#""devnum": {},"#, self.devnum)?; |
| 1914 | write!(f, r#""ifindex": {},"#, self.ifindex)?; |
| 1915 | write!(f, r#""watch_handle": {},"#, self.watch_handle)?; |
| 1916 | write!(f, r#""maj": {},"#, self.maj)?; |
| 1917 | write!(f, r#""min": {}"#, self.min)?; |
| 1918 | write!(f, "}}") |
| 1919 | } |
| 1920 | } |
| 1921 | |
| 1922 | #[cfg(test)] |
| 1923 | mod tests { |
| 1924 | use super::*; |
| 1925 | use crate::UdevEntry; |
| 1926 | |
| 1927 | #[test] |
| 1928 | fn test_udev_device() { |
| 1929 | let udev = Arc::new(Udev::new()); |
| 1930 | let mut null_dev = UdevDevice::new(Arc::clone(&udev)); |
| 1931 | |
| 1932 | let exp_syspath = "test_syspath"; |
| 1933 | let exp_devpath = "test_devpath"; |
| 1934 | let exp_sysname = "test_sysname"; |
| 1935 | let exp_sysnum = "test_sysnum"; |
| 1936 | let exp_devnode = "test_devnode"; |
| 1937 | let exp_devnode_mode = Mode::EXEC_OTHER; |
| 1938 | let exp_subsystem = "test_subsystem"; |
| 1939 | let exp_devtype = "test_devtype"; |
| 1940 | let exp_driver = "test_driver"; |
| 1941 | let exp_action = "test_action"; |
| 1942 | let exp_devpath_old = "test_devpath_old"; |
| 1943 | let exp_id_filename = "test_id_filename"; |
| 1944 | let exp_envp = ["test_envp"]; |
| 1945 | let exp_monitor_buf = "test_monitor_buf"; |
| 1946 | |
| 1947 | let exp_devlinks = [UdevEntry::new().with_name("test_devlinks")]; |
| 1948 | let exp_properties = [UdevEntry::new().with_name("test_properties")]; |
| 1949 | let exp_sysattr_value = [UdevEntry::new().with_name("test_sysattr_value")]; |
| 1950 | let exp_sysattr = [UdevEntry::new().with_name("test_sysattr")]; |
| 1951 | let exp_tags = [UdevEntry::new().with_name("test_tags")]; |
| 1952 | |
| 1953 | let exp_seqnum = 1; |
| 1954 | let exp_usec_initialized = 2; |
| 1955 | let exp_devlink_priority = 3; |
| 1956 | let exp_devnum = 4; |
| 1957 | let exp_ifindex = 5; |
| 1958 | let exp_watch_handle = 6; |
| 1959 | let exp_maj = 7; |
| 1960 | let exp_min = 8; |
| 1961 | |
| 1962 | let exp_devlinks_uptodate = true; |
| 1963 | let exp_envp_uptodate = true; |
| 1964 | let exp_tags_uptodate = true; |
| 1965 | let exp_info_loaded = true; |
| 1966 | let exp_db_loaded = true; |
| 1967 | let exp_uevent_loaded = true; |
| 1968 | let exp_is_initialized = true; |
| 1969 | let exp_sysattr_list_read = true; |
| 1970 | let exp_db_persist = true; |
| 1971 | |
| 1972 | let exp_dev = UdevDevice::new(udev) |
| 1973 | .with_syspath(exp_syspath) |
| 1974 | .with_devpath(exp_devpath) |
| 1975 | .with_sysname(exp_sysname) |
| 1976 | .with_sysnum(exp_sysnum) |
| 1977 | .with_devnode(exp_devnode) |
| 1978 | .with_devnode_mode(exp_devnode_mode) |
| 1979 | .with_subsystem(exp_subsystem) |
| 1980 | .with_devtype(exp_devtype) |
| 1981 | .with_driver(exp_driver) |
| 1982 | .with_action(exp_action) |
| 1983 | .with_devpath_old(exp_devpath_old) |
| 1984 | .with_id_filename(exp_id_filename) |
| 1985 | .with_envp(&exp_envp) |
| 1986 | .with_monitor_buf(exp_monitor_buf) |
| 1987 | .with_devlinks_list(exp_devlinks.clone()) |
| 1988 | .with_properties_list(exp_properties.clone()) |
| 1989 | .with_sysattr_value_list(exp_sysattr_value.clone()) |
| 1990 | .with_sysattr_list(exp_sysattr.clone()) |
| 1991 | .with_tags_list(exp_tags.clone()) |
| 1992 | .with_seqnum(exp_seqnum) |
| 1993 | .with_usec_initialized(exp_usec_initialized) |
| 1994 | .with_devlink_priority(exp_devlink_priority) |
| 1995 | .with_devnum(exp_devnum) |
| 1996 | .with_ifindex(exp_ifindex) |
| 1997 | .with_watch_handle(exp_watch_handle) |
| 1998 | .with_maj(exp_maj) |
| 1999 | .with_min(exp_min) |
| 2000 | .with_devlinks_uptodate(exp_devlinks_uptodate) |
| 2001 | .with_envp_uptodate(exp_envp_uptodate) |
| 2002 | .with_tags_uptodate(exp_tags_uptodate) |
| 2003 | .with_info_loaded(exp_info_loaded) |
| 2004 | .with_db_loaded(exp_db_loaded) |
| 2005 | .with_uevent_loaded(exp_uevent_loaded) |
| 2006 | .with_is_initialized(exp_is_initialized) |
| 2007 | .with_sysattr_list_read(exp_sysattr_list_read) |
| 2008 | .with_db_persist(exp_db_persist); |
| 2009 | |
| 2010 | assert_eq!(null_dev.syspath(), ""); |
| 2011 | assert_eq!(null_dev.devpath(), ""); |
| 2012 | assert_eq!(null_dev.sysname(), ""); |
| 2013 | assert_eq!(null_dev.sysnum(), ""); |
| 2014 | assert_eq!(null_dev.devnode(), ""); |
| 2015 | assert_eq!(null_dev.devnode_mode(), Mode::NONE); |
| 2016 | assert_eq!(null_dev.subsystem(), ""); |
| 2017 | assert_eq!(null_dev.devtype(), ""); |
| 2018 | assert_eq!(null_dev.driver(), ""); |
| 2019 | assert_eq!(null_dev.action(), ""); |
| 2020 | assert_eq!(null_dev.devpath_old(), ""); |
| 2021 | assert_eq!(null_dev.id_filename(), ""); |
| 2022 | assert!(null_dev.envp_is_empty()); |
| 2023 | assert_eq!(null_dev.monitor_buf(), ""); |
| 2024 | assert!(null_dev.devlinks_list().is_empty()); |
| 2025 | assert!(null_dev.properties_list().is_empty()); |
| 2026 | assert!(null_dev.sysattr_value_list().is_empty()); |
| 2027 | assert!(null_dev.sysattr_list().is_empty()); |
| 2028 | assert!(null_dev.tags_list().is_empty()); |
| 2029 | assert_eq!(null_dev.seqnum(), 0); |
| 2030 | assert_eq!(null_dev.usec_initialized(), 0); |
| 2031 | assert_eq!(null_dev.devlink_priority(), 0); |
| 2032 | assert_eq!(null_dev.devnum(), 0); |
| 2033 | assert_eq!(null_dev.ifindex(), 0); |
| 2034 | assert_eq!(null_dev.watch_handle(), 0); |
| 2035 | assert_eq!(null_dev.maj(), 0); |
| 2036 | assert_eq!(null_dev.min(), 0); |
| 2037 | assert!(!null_dev.devlinks_uptodate()); |
| 2038 | assert!(!null_dev.envp_uptodate()); |
| 2039 | assert!(!null_dev.tags_uptodate()); |
| 2040 | assert!(!null_dev.info_loaded()); |
| 2041 | assert!(!null_dev.db_loaded()); |
| 2042 | assert!(!null_dev.uevent_loaded()); |
| 2043 | assert!(!null_dev.is_initialized()); |
| 2044 | assert!(!null_dev.sysattr_list_read()); |
| 2045 | assert!(!null_dev.db_persist()); |
| 2046 | |
| 2047 | assert_eq!(exp_dev.syspath(), exp_syspath); |
| 2048 | assert_eq!(exp_dev.devpath(), exp_devpath); |
| 2049 | assert_eq!(exp_dev.sysname(), exp_sysname); |
| 2050 | assert_eq!(exp_dev.sysnum(), exp_sysnum); |
| 2051 | assert_eq!(exp_dev.devnode(), exp_devnode); |
| 2052 | assert_eq!(exp_dev.devnode_mode(), exp_devnode_mode); |
| 2053 | assert_eq!(exp_dev.subsystem(), exp_subsystem); |
| 2054 | assert_eq!(exp_dev.devtype(), exp_devtype); |
| 2055 | assert_eq!(exp_dev.driver(), exp_driver); |
| 2056 | assert_eq!(exp_dev.action(), exp_action); |
| 2057 | assert_eq!(exp_dev.devpath_old(), exp_devpath_old); |
| 2058 | assert_eq!(exp_dev.id_filename(), exp_id_filename); |
| 2059 | assert_eq!(exp_dev.envp(), exp_envp); |
| 2060 | assert_eq!(exp_dev.monitor_buf(), exp_monitor_buf); |
| 2061 | |
| 2062 | for (item, exp_item) in exp_dev.devlinks_list().iter().zip(exp_devlinks.iter()) { |
| 2063 | assert_eq!(item, exp_item); |
| 2064 | } |
| 2065 | |
| 2066 | for (item, exp_item) in exp_dev.properties_list().iter().zip(exp_properties.iter()) { |
| 2067 | assert_eq!(item, exp_item); |
| 2068 | } |
| 2069 | |
| 2070 | for (item, exp_item) in exp_dev |
| 2071 | .sysattr_value_list() |
| 2072 | .iter() |
| 2073 | .zip(exp_sysattr_value.iter()) |
| 2074 | { |
| 2075 | assert_eq!(item, exp_item); |
| 2076 | } |
| 2077 | |
| 2078 | for (item, exp_item) in exp_dev.sysattr_list().iter().zip(exp_sysattr.iter()) { |
| 2079 | assert_eq!(item, exp_item); |
| 2080 | } |
| 2081 | |
| 2082 | for (item, exp_item) in exp_dev.tags_list().iter().zip(exp_tags.iter()) { |
| 2083 | assert_eq!(item, exp_item); |
| 2084 | } |
| 2085 | |
| 2086 | assert_eq!(exp_dev.seqnum(), exp_seqnum); |
| 2087 | assert_eq!(exp_dev.usec_initialized(), exp_usec_initialized); |
| 2088 | assert_eq!(exp_dev.devlink_priority(), exp_devlink_priority); |
| 2089 | assert_eq!(exp_dev.devnum(), exp_devnum); |
| 2090 | assert_eq!(exp_dev.ifindex(), exp_ifindex); |
| 2091 | assert_eq!(exp_dev.watch_handle(), exp_watch_handle); |
| 2092 | assert_eq!(exp_dev.maj(), exp_maj); |
| 2093 | assert_eq!(exp_dev.min(), exp_min); |
| 2094 | assert_eq!(exp_dev.devlinks_uptodate(), exp_devlinks_uptodate); |
| 2095 | assert_eq!(exp_dev.envp_uptodate(), exp_envp_uptodate); |
| 2096 | assert_eq!(exp_dev.tags_uptodate(), exp_tags_uptodate); |
| 2097 | assert_eq!(exp_dev.info_loaded(), exp_info_loaded); |
| 2098 | assert_eq!(exp_dev.db_loaded(), exp_db_loaded); |
| 2099 | assert_eq!(exp_dev.uevent_loaded(), exp_uevent_loaded); |
| 2100 | assert_eq!(exp_dev.is_initialized(), exp_is_initialized); |
| 2101 | assert_eq!(exp_dev.sysattr_list_read(), exp_sysattr_list_read); |
| 2102 | assert_eq!(exp_dev.db_persist(), exp_db_persist); |
| 2103 | |
| 2104 | null_dev.set_syspath(exp_syspath); |
| 2105 | null_dev.set_devpath(exp_devpath); |
| 2106 | null_dev.set_sysname(exp_sysname); |
| 2107 | null_dev.set_sysnum(exp_sysnum); |
| 2108 | null_dev.set_devnode(exp_devnode); |
| 2109 | null_dev.set_devnode_mode(exp_devnode_mode); |
| 2110 | null_dev.set_subsystem(exp_subsystem); |
| 2111 | null_dev.set_devtype(exp_devtype); |
| 2112 | null_dev.set_driver(exp_driver); |
| 2113 | null_dev.set_action(exp_action); |
| 2114 | null_dev.set_devpath_old(exp_devpath_old); |
| 2115 | null_dev.set_id_filename(exp_id_filename); |
| 2116 | null_dev.set_envp(&exp_envp); |
| 2117 | null_dev.set_monitor_buf(exp_monitor_buf); |
| 2118 | null_dev.set_devlinks_list(exp_devlinks.clone()); |
| 2119 | null_dev.set_properties_list(exp_properties.clone()); |
| 2120 | null_dev.set_sysattr_value_list(exp_sysattr_value.clone()); |
| 2121 | null_dev.set_sysattr_list(exp_sysattr.clone()); |
| 2122 | null_dev.set_tags_list(exp_tags.clone()); |
| 2123 | null_dev.set_seqnum(exp_seqnum); |
| 2124 | null_dev.set_usec_initialized(exp_usec_initialized); |
| 2125 | null_dev.set_devlink_priority(exp_devlink_priority); |
| 2126 | null_dev.set_devnum(exp_devnum); |
| 2127 | null_dev.set_ifindex(exp_ifindex); |
| 2128 | null_dev.set_watch_handle(exp_watch_handle); |
| 2129 | null_dev.set_maj(exp_maj); |
| 2130 | null_dev.set_min(exp_min); |
| 2131 | null_dev.set_devlinks_uptodate(exp_devlinks_uptodate); |
| 2132 | null_dev.set_envp_uptodate(exp_envp_uptodate); |
| 2133 | null_dev.set_tags_uptodate(exp_tags_uptodate); |
| 2134 | null_dev.set_info_loaded(exp_info_loaded); |
| 2135 | null_dev.set_db_loaded(exp_db_loaded); |
| 2136 | null_dev.set_uevent_loaded(exp_uevent_loaded); |
| 2137 | null_dev.set_is_initialized(exp_is_initialized); |
| 2138 | null_dev.set_sysattr_list_read(exp_sysattr_list_read); |
| 2139 | null_dev.set_db_persist(exp_db_persist); |
| 2140 | |
| 2141 | assert_eq!(null_dev, exp_dev); |
| 2142 | } |
| 2143 | } |