diff --git a/src/sysboostd/config.rs b/src/sysboostd/config.rs index e2869c9267db8106ead610bc26b846578b9837e0..173dbc43bff1edafa4ddc18389209cf15c35d555 100644 --- a/src/sysboostd/config.rs +++ b/src/sysboostd/config.rs @@ -16,6 +16,8 @@ use serde::Deserialize; use ini::Ini; use lazy_static::lazy_static; use std::sync::RwLock; +use std::io::{BufRead, BufReader}; +use std::process::{Command, Stdio}; pub const SYSBOOST_CONFIG_PATH: &str = "/etc/sysboost.d/sysboost.ini"; // only 10 program can use boost @@ -68,6 +70,42 @@ lazy_static! { ); } +fn detect_so(path: &String, rtoconfig: &mut RtoConfig) { + let args: &Vec = &vec![path.to_string();1]; + let mut run_thread = match Command::new("/usr/bin/ldd").args(args).stdout(Stdio::piped()).spawn() { + Ok(run_thread) => run_thread, + Err(e) => { + log::error!("Failed to execute command: {}", e); + return; + } + }; + let stdout = match run_thread.stdout.take() { + Some(stdout) => stdout, + None => { + log::error!("Failed to capture stdout"); + return; + } + }; + let reader = BufReader::new(stdout); + for line in reader.lines() { + let line = line.unwrap_or_else(|_| "".to_owned()); + let start = match line.find('/') { + Some(s) => {s}, + None => continue + }; + let end = match line.find('(') { + Some(e) => {e}, + None => continue + }; + let lib_path = line[start..end].trim(); + if lib_path == "/usr/lib64/libc.so.6" || lib_path == "/lib/ld-linux-aarch64.so.1" { + continue; + } + rtoconfig.libs.push(lib_path.to_string()); + } + log::debug!("Automatically detect {} dependent so files: {:?}",rtoconfig.name, &rtoconfig.libs); +} + pub fn parse_sysinit_config() { let conf_file = match Ini::load_from_file(SYSBOOST_CONFIG_PATH){ Ok(c) => {c} @@ -136,11 +174,14 @@ fn parse_rto_config(sec: String, prop: &Properties) { path: prop.get("path").map(|s| s.to_string()), watch_paths: Vec::new(), }; - if rtoconf.elf_path == SYSBOOST_PATH || is_mode_invalid(rtoconf.mode.clone()){ - log::error!("invalid config in {}", sec); + log::error!("invalid config in {}", &sec); return; } + //如果用户没有配置libs, 则自动检测libs + if prop.get("libs") == None { + detect_so(&sec, &mut rtoconf); + } // add elf file to watch list rtoconf.watch_paths.push(rtoconf.elf_path.clone()); for lib in rtoconf.libs.iter() {