udev: Add function to query the loaded driver of a device

This commit is contained in:
Victor Brekenfeld 2020-05-22 23:05:03 +02:00
parent 38d3db565a
commit 53f5753943
1 changed files with 21 additions and 0 deletions

View File

@ -256,3 +256,24 @@ pub fn all_gpus<S: AsRef<str>>(seat: S) -> IoResult<Vec<PathBuf>> {
.flat_map(|device| device.devnode().map(PathBuf::from)) .flat_map(|device| device.devnode().map(PathBuf::from))
.collect()) .collect())
} }
/// Returns the loaded driver for a device named by it's [`dev_t`](::nix::sys::stat::dev_t).
pub fn driver(dev: dev_t) -> IoResult<Option<OsString>> {
let mut enumerator = Enumerator::new()?;
enumerator.match_subsystem("drm")?;
enumerator.match_sysname("card[0-9]*")?;
Ok(enumerator
.scan_devices()?
.filter(|device| device.devnum() == Some(dev))
.flat_map(|dev| {
let mut device = Some(dev);
while let Some(dev) = device {
if dev.driver().is_some() {
return dev.driver().map(std::ffi::OsStr::to_os_string);
}
device = dev.parent();
}
None
})
.next())
}