diff options
Diffstat (limited to 'src/facility/envsys.rs')
| -rw-r--r-- | src/facility/envsys.rs | 96 |
1 files changed, 61 insertions, 35 deletions
diff --git a/src/facility/envsys.rs b/src/facility/envsys.rs index dfecf94..ba2e53a 100644 --- a/src/facility/envsys.rs +++ b/src/facility/envsys.rs @@ -14,8 +14,10 @@ use rustix::mm::munmap; pub struct EnvSys { sysmon: File, + dictionary: Dictionary, } + #[repr(C)] #[derive(Debug)] struct PropertyListReference { @@ -34,14 +36,10 @@ impl CreatableFacility for EnvSys { const FACILITY_TYPE: FacilityType = FacilityType::ENVSYS; async fn new() -> Result<Self> { - println!("loading envsys"); let sysmon = File::open("/dev/sysmon").await?; + let dictionary = get_dictionary(&sysmon).await?; - let envsys = EnvSys { sysmon }; - - envsys.get_dictionary().await?; - - Ok(envsys) + Ok(EnvSys { sysmon, dictionary }) } } @@ -55,35 +53,63 @@ impl EnvSys { const ENVSYS_REMOVEPROPS: Opcode = read_write::<PropertyListReference>(b'E', 2); - async fn get_dictionary(&self) -> Result<Dictionary> { - let call = unsafe { - Getter::<{ Self::ENVSYS_GETDICTIONARY }, PropertyListReference>::new() - }; - - /* If the call fails, we can safely return because there's no resulting - * memory allocation. - */ - let result = unsafe { ioctl(&self.sysmon, call) }?; - - /* We're expecting the final byte to be a null, acting as a C string - * terminator. We ignore it. - */ - let xml: &[u8] = unsafe { from_raw_parts(result.data, result.size - 1) }; - - /* This returns a Result, but we want to clean up before we peek inside. - */ - let dictionary = parse_dictionary(xml); - - /* So. Fun story. Inspecting NetBSD src/common/lib/libprop/prop_kern.c, - * we find that the kernel creates a new memory mapping for the dictionary - * it returns. When we're done, we're expected to munmap() it. Wild. - * - * Anyway, if munmap() fails we have no way to clean up regardless, so we - * just return. - */ - unsafe { munmap(result.data as *mut c_void, result.size) }?; - - dictionary + pub async fn refresh_dictionary(&mut self) -> Result<()> { + self.dictionary = get_dictionary(&self.sysmon).await?; + + Ok(()) + } + + pub fn device_iter(&self) -> impl Iterator<Item=&str> { + let hash_iterator = self.dictionary.iter(); + + DeviceNames { hash_iterator } + } +} + + +struct DeviceNames<'a> { + hash_iterator: std::collections::hash_map::Iter<'a, String, Value>, +} + +impl<'a> Iterator for DeviceNames<'a> { + type Item = &'a str; + + fn next(&mut self) -> Option<Self::Item> { + if let Some((key, _)) = self.hash_iterator.next() { + Some(key) + } else { None } } } + +async fn get_dictionary(sysmon: &File) -> Result<Dictionary> { + let call = unsafe { + Getter::<{ EnvSys::ENVSYS_GETDICTIONARY }, PropertyListReference>::new() + }; + + /* If the call fails, we can safely return because there's no resulting + * memory allocation. + */ + let result = unsafe { ioctl(&sysmon, call) }?; + + /* We're expecting the final byte to be a null, acting as a C string + * terminator. We ignore it. + */ + let xml: &[u8] = unsafe { from_raw_parts(result.data, result.size - 1) }; + + /* This returns a Result, but we want to clean up before we peek inside. + */ + let dictionary = parse_dictionary(xml); + + /* So. Fun story. Inspecting NetBSD src/common/lib/libprop/prop_kern.c, + * we find that the kernel creates a new memory mapping for the dictionary + * it returns. When we're done, we're expected to munmap() it. Wild. + * + * Anyway, if munmap() fails we have no way to clean up regardless, so we + * just return. + */ + unsafe { munmap(result.data as *mut c_void, result.size) }?; + + dictionary +} + |