diff --git a/daemon/src/device.rs b/daemon/src/device.rs index fd0a7ac..8f636cf 100644 --- a/daemon/src/device.rs +++ b/daemon/src/device.rs @@ -241,7 +241,9 @@ pub async fn get_device(name: &str) -> DbSection { } pub fn sync_db() { - DB.save(); + if let Err(err) = DB.save() { + tracing::error!("db sync failure: {err}"); + } } fn dev_root() -> PathBuf { diff --git a/libcompat/udev/src/lib.rs b/libcompat/udev/src/lib.rs index cdbdfbf..f98f2f1 100644 --- a/libcompat/udev/src/lib.rs +++ b/libcompat/udev/src/lib.rs @@ -118,9 +118,9 @@ pub struct UdevDevice { udev: *mut Udev, properties: UdevListEntry, syspath: Option<*const libc::c_char>, - devlinks: UdevListEntry, - tags: UdevListEntry, - sysattr: UdevListEntry, + devlinks: Option, + tags: Option, + sysattr: Option, seqnum: libc::c_ulonglong, strings: FfiStringPool, } @@ -160,9 +160,9 @@ impl UdevDevice { udev: unsafe { udev_ref(udev) }, properties, syspath: syspath.map(|x| strings.insert_rust(x).as_ptr()), - devlinks: *devlinks, - tags: *tags, - sysattr: *sysattr, + devlinks: devlinks.map(|x| *x), + tags: tags.map(|x| *x), + sysattr: sysattr.map(|x| *x), seqnum: 0, strings, } @@ -367,7 +367,13 @@ pub unsafe extern "C" fn udev_device_get_is_initialized(_: *const UdevDevice) -> pub unsafe extern "C" fn udev_device_get_devlinks_list_entry( dev: *const UdevDevice, ) -> *const UdevListEntry { - unsafe { &raw const (*dev).devlinks } + unsafe { + (*dev) + .devlinks + .as_ref() + .map(|x| x as *const _) + .unwrap_or_default() + } } #[unsafe(no_mangle)] @@ -381,21 +387,39 @@ pub unsafe extern "C" fn udev_device_get_properties_list_entry( pub unsafe extern "C" fn udev_device_get_tags_list_entry( dev: *const UdevDevice, ) -> *const UdevListEntry { - unsafe { &raw const (*dev).tags } + unsafe { + (*dev) + .tags + .as_ref() + .map(|x| x as *const _) + .unwrap_or_default() + } } #[unsafe(no_mangle)] pub unsafe extern "C" fn udev_device_get_current_tags_list_entry( dev: *const UdevDevice, ) -> *const UdevListEntry { - unsafe { &raw const (*dev).tags } + unsafe { + (*dev) + .tags + .as_ref() + .map(|x| x as *const _) + .unwrap_or_default() + } } #[unsafe(no_mangle)] pub unsafe extern "C" fn udev_device_get_sysattr_list_entry( dev: *const UdevDevice, ) -> *const UdevListEntry { - unsafe { &raw const (*dev).sysattr } + unsafe { + (*dev) + .sysattr + .as_ref() + .map(|x| x as *const _) + .unwrap_or_default() + } } #[unsafe(no_mangle)] @@ -872,7 +896,7 @@ pub unsafe extern "C" fn udev_enumerate_scan_devices( for i in &mut result { i.insert_str(0, "/sys"); } - (*udev_enumerate).result = Some(*vec_to_list(&mut (*udev_enumerate).strings, &result)); + (*udev_enumerate).result = vec_to_list(&mut (*udev_enumerate).strings, &result).map(|x| *x); 0 } } @@ -1023,25 +1047,19 @@ fn hashmap_to_list( })) } -fn vec_to_list(strings: &mut FfiStringPool, vec: &Vec) -> Box { - let mut head = None; +fn vec_to_list(strings: &mut FfiStringPool, vec: &[String]) -> Option> { + let mut head: Option> = None; let mut tail = &mut head; for s in vec { let entry = Box::new(UdevListEntry { key: strings.insert_rust(s.clone()).as_ptr(), - value: strings.insert_rust("".into()).as_ptr(), + value: strings.insert_rust(String::new()).as_ptr(), next: None, }); *tail = Some(entry); tail = &mut tail.as_mut().unwrap().next; } - head.unwrap_or_else(|| { - Box::new(UdevListEntry { - key: std::ptr::null(), - value: std::ptr::null(), - next: None, - }) - }) + head } diff --git a/libs/db/src/lib.rs b/libs/db/src/lib.rs index 66a8f4e..b497ed9 100644 --- a/libs/db/src/lib.rs +++ b/libs/db/src/lib.rs @@ -36,11 +36,16 @@ impl Db { } } - pub fn save(&self) { + pub fn save(&self) -> std::io::Result<()> { if let Some(path) = &self.path { - let text = serde_json::to_string(&*self.map.read().unwrap()).unwrap(); - _ = std::fs::write(&path, text); + let mut fd = std::fs::File::options() + .truncate(true) + .create(true) + .write(true) + .open(path)?; + serde_json::to_writer(&mut fd, &*self.map.read().unwrap())?; } + Ok(()) } } diff --git a/libs/rule/Cargo.toml b/libs/rule/Cargo.toml index 0cec742..a37fcea 100644 --- a/libs/rule/Cargo.toml +++ b/libs/rule/Cargo.toml @@ -5,6 +5,7 @@ edition = "2024" [dependencies] libc = "0.2" +moka = { version = "0.12", features = ["sync"] } regex = "1" ron = "0.12" rustc-hash = "2" diff --git a/libs/rule/src/engine.rs b/libs/rule/src/engine.rs index 86838b8..8dabe16 100644 --- a/libs/rule/src/engine.rs +++ b/libs/rule/src/engine.rs @@ -3,11 +3,13 @@ use crate::{ parser::{Action, OnError, Rule, RuleFile, Str, When}, }; use db::DbSection; +use moka::sync::Cache; use regex::Regex; use std::{ + borrow::Cow, path::Path, process::{ExitStatus, Stdio}, - sync::Arc, + sync::{Arc, LazyLock}, time::Duration, }; use tokio::{io::AsyncReadExt, process::Child}; @@ -135,20 +137,18 @@ impl Engine { let key = self.bake_str(key)?; let regex = self.bake_str(regex)?; let regex = format!("^{regex}$"); - let regex = Regex::new(®ex).map_err(Error::Regex)?; let Some(val) = self.properties.get(&key) else { return Ok(false); }; - Ok(regex.is_match(&val)) + regex_match(®ex, &val).map_err(Error::Regex) } When::PartMatch(key, regex) => { let key = self.bake_str(key)?; let regex = self.bake_str(regex)?; - let regex = Regex::new(®ex).map_err(Error::Regex)?; let Some(val) = self.properties.get(&key) else { return Ok(false); }; - Ok(regex.is_match(&val)) + regex_match(®ex, &val).map_err(Error::Regex) } } } @@ -158,7 +158,7 @@ impl Engine { Action::Set(key, val) => { let key = self.bake_str(key)?; let val = self.bake_str(val)?; - self.properties.insert(&key, val); + self.properties.insert(&key, val.into()); } Action::Add(key, val) => { let key = self.bake_str(key)?; @@ -182,7 +182,7 @@ impl Engine { Ok(()) } - fn bake_str(&self, s: &Str) -> Result { + fn bake_str<'s>(&self, s: &'s Str) -> Result, Error> { match s { Str::Direct(x) => Ok(x.into()), Str::Format(template) => { @@ -222,7 +222,7 @@ impl Engine { if current_property.is_some() || right_bracket { return Err(Error::InvalidStringTemplate); } - Ok(result) + Ok(result.into()) } } } @@ -340,3 +340,14 @@ impl CommandTimeoutBehavior { Self::default() } } + +fn regex_match(re: &str, s: &str) -> Result { + static REGEX_CACHE: LazyLock> = LazyLock::new(|| Cache::new(256)); + + let it = REGEX_CACHE + .entry(re.into()) + .or_try_insert_with(|| Regex::new(re)) + .map_err(|err| (*err).clone())?; + let re = it.value(); + Ok(re.is_match(s)) +} diff --git a/misc/airup/lxdeviced.airs b/misc/airup/lxdeviced.airs index f519ca9..6795780 100644 --- a/misc/airup/lxdeviced.airs +++ b/misc/airup/lxdeviced.airs @@ -13,7 +13,7 @@ clear_vars = true max-attempts = -1 [env.vars] -PATH = "/usr/bin" +PATH = "/bin:/usr/bin:/sbin:/usr/sbin" LXDEVICED_HAS_DEVTMPFS = "1" LXDEVICED_DEV_ROOT = "/dev" LXDEVICED_SYSFS_ROOT = "/sys"