From 336a70108d606e061f4f76083a13ca0280e16fdb Mon Sep 17 00:00:00 2001 From: Tie Liu Date: Thu, 13 Jul 2023 15:11:08 +0800 Subject: [PATCH 1/3] clean env --- bin/daemon.rs | 95 +++++++++++++++++++++++++++++++++++++++++++++++- sysboost.service | 1 + 2 files changed, 95 insertions(+), 1 deletion(-) diff --git a/bin/daemon.rs b/bin/daemon.rs index 29f829f..5ffd0de 100644 --- a/bin/daemon.rs +++ b/bin/daemon.rs @@ -48,6 +48,11 @@ pub struct RtoConfig { watch_paths: Vec, } +#[derive(Debug, Deserialize)] +pub struct LinkInfo { + pub elf_path: String, +} + impl FromStr for RtoConfig { type Err = toml::de::Error; fn from_str(s: &str) -> Result { @@ -55,6 +60,13 @@ impl FromStr for RtoConfig { } } +impl FromStr for LinkInfo { + type Err = toml::de::Error; + fn from_str(s: &str) -> Result { + toml::from_str(s) + } +} + fn is_symlink(path: &PathBuf) -> bool { let file_type = match fs::symlink_metadata(path) { Ok(metadata) => metadata.file_type(), @@ -585,10 +597,91 @@ fn insmod_ko(path: &String) { run_child("/usr/sbin/insmod", &args); } +fn clean_env() { + // There may be multiple directories in /var/lib/sysboost, corresponding to different sysboost modes + let mut elf_files = Vec::new(); + let dir_e = fs::read_dir(&Path::new(SYSBOOST_DB_PATH)); + let dir = match dir_e { + Ok(dir) => dir, + Err(e) => { + log::error!("{}", e); + return; + } + }; + + for entry in dir { + let entry = entry.ok().unwrap(); + let path = entry.path(); + + if path.is_file() { + continue; + } + + let link_dir = match fs::read_dir(&path) { + Ok(link_dir) => link_dir, + Err(e) => { + log::error!("{}", e); + return; + } + }; + + for link_entry in link_dir { + let link_entry = link_entry.ok().unwrap(); + let path = link_entry.path(); + if path.is_dir() { + continue; + } + if path.file_name() == None { + continue; + } + let contents = match fs::read_to_string(path) { + Ok(c) => c, + Err(e) => { + log::error!("reading file fail {}", e); + return; + } + }; + let info_e = contents.parse::(); + match info_e { + Ok(ref c) => c, + Err(_) => { + log::error!("parse config fail"); + return; + } + }; + + let info = info_e.unwrap(); + elf_files.push(info.elf_path); + // Clear all files in sysboost_dir. + match fs::remove_file(entry.path()) { + Ok(c) => c, + Err(e) => { + log::error!("remove file fail {}", e); + return; + } + }; + } + } + + // Restore the original file pointed to by elf_file. + for file_path in elf_files { + let file_path_buf = PathBuf::from(file_path); + let bak_file_path = file_path_buf.clone().with_extension("link"); + match fs::rename(&bak_file_path, &file_path_buf) { + Ok(c) => c, + Err(e) => { + log::error!("rename file fail {}", e); + return; + } + }; + } +} + pub fn daemon_loop() { insmod_ko(&KO_PATH.to_string()); - // TODO: clean env + // When rebooting, you should clean up the backup environment + // clean_env(); loop { start_service(); } diff --git a/sysboost.service b/sysboost.service index 7cb1d7a..e2d607e 100644 --- a/sysboost.service +++ b/sysboost.service @@ -6,6 +6,7 @@ After=sysboost.service [Service] Type=forking ExecStart=/usr/bin/sysboostd -daemon +Restart=always [Install] WantedBy=multi-user.target -- Gitee From ba8da7dd8003416ea1025935f1d58f7b8dbc0b93 Mon Sep 17 00:00:00 2001 From: Tie Liu Date: Thu, 13 Jul 2023 20:37:56 +0800 Subject: [PATCH 2/3] Restoring the sysboostd Environment --- tests/test_sysboostd.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/tests/test_sysboostd.rs b/tests/test_sysboostd.rs index edd8a97..ad518ff 100644 --- a/tests/test_sysboostd.rs +++ b/tests/test_sysboostd.rs @@ -222,5 +222,34 @@ mod tests { // Unnormal Scenarios // 1、When sysboostd break + fn test_restore_sysboostd_env() { +// Create libtinfo.toml file in /etc/sysboost.d directory + let toml_path = "/etc/sysboost.d/libtinfo.toml"; + let toml_content = "elf_path = '/usr/lib64/libtinfo.so' mode = 'bolt'"; + fs::write(toml_path, toml_content).unwrap(); + + // Sleep for 3 seconds + sleep(Duration::from_secs(3)); + + // Delete libtinfo.toml file + fs::remove_file(toml_path).unwrap(); + + // Restart sysboostd service using systemctl + let output = Command::new("systemctl") + .arg("restart") + .arg("sysboostd") + .output() + .unwrap(); + assert!(output.status.success()); + + // Check if /var/lib/sysboost directory exists and has files + let sysboost_dir = "/var/lib/sysboost"; + assert!(fs::metadata(sysboost_dir).unwrap().is_dir()); + assert!(fs::read_dir(sysboost_dir).unwrap().next().is_some()); + + // Check if /usr/lib64/libtinfo.so.bak file exists + let bak_path = "/usr/lib64/libtinfo.so.bak"; + assert!(fs::metadata(bak_path).unwrap().is_file()); + } } -- Gitee From 0aaf4c4995c912aadc66e09215ba8ac10ef17725 Mon Sep 17 00:00:00 2001 From: Tie Liu Date: Fri, 14 Jul 2023 10:21:02 +0800 Subject: [PATCH 3/3] Refactoring ad add link --- bin/daemon.rs | 104 ++-------------------------------------- tests/test_sysboostd.rs | 15 +++--- 2 files changed, 11 insertions(+), 108 deletions(-) diff --git a/bin/daemon.rs b/bin/daemon.rs index 5ffd0de..70eceb0 100644 --- a/bin/daemon.rs +++ b/bin/daemon.rs @@ -48,11 +48,6 @@ pub struct RtoConfig { watch_paths: Vec, } -#[derive(Debug, Deserialize)] -pub struct LinkInfo { - pub elf_path: String, -} - impl FromStr for RtoConfig { type Err = toml::de::Error; fn from_str(s: &str) -> Result { @@ -60,13 +55,6 @@ impl FromStr for RtoConfig { } } -impl FromStr for LinkInfo { - type Err = toml::de::Error; - fn from_str(s: &str) -> Result { - toml::from_str(s) - } -} - fn is_symlink(path: &PathBuf) -> bool { let file_type = match fs::symlink_metadata(path) { Ok(metadata) => metadata.file_type(), @@ -79,11 +67,11 @@ fn is_symlink(path: &PathBuf) -> bool { return file_type.is_symlink(); } -fn db_add_link(path: &String) -> i32 { +fn db_add_link(conf: &RtoConfig) -> i32 { // symlink app.link to app, different modes correspond to different directories - let file_name = Path::new(path).file_name().unwrap().to_str().unwrap(); + let file_name = Path::new(&conf.elf_path).file_name().unwrap().to_str().unwrap(); let link_path = format!("{}{}.link", SYSBOOST_DB_PATH, file_name); - let ret_e = UnixFs::symlink(&path, &link_path); + let ret_e = UnixFs::symlink(&conf.elf_path, &link_path); match ret_e { Ok(_) => {} Err(_) => { @@ -397,7 +385,7 @@ fn sysboost_core_process(conf: &RtoConfig) -> i32 { } } - let ret = db_add_link(&conf.elf_path); + let ret = db_add_link(&conf); if ret != 0 { log::error!("Error: db add link fault."); return ret; @@ -411,9 +399,6 @@ fn sysboost_core_process(conf: &RtoConfig) -> i32 { return ret; } -// TODO: exit() abnormal -// TODO: exit() normal - fn process_config(path: PathBuf) -> Option { let conf_e = read_config(&path); let mut conf = match conf_e { @@ -597,91 +582,10 @@ fn insmod_ko(path: &String) { run_child("/usr/sbin/insmod", &args); } -fn clean_env() { - // There may be multiple directories in /var/lib/sysboost, corresponding to different sysboost modes - let mut elf_files = Vec::new(); - let dir_e = fs::read_dir(&Path::new(SYSBOOST_DB_PATH)); - let dir = match dir_e { - Ok(dir) => dir, - Err(e) => { - log::error!("{}", e); - return; - } - }; - - for entry in dir { - let entry = entry.ok().unwrap(); - let path = entry.path(); - - if path.is_file() { - continue; - } - - let link_dir = match fs::read_dir(&path) { - Ok(link_dir) => link_dir, - Err(e) => { - log::error!("{}", e); - return; - } - }; - - for link_entry in link_dir { - let link_entry = link_entry.ok().unwrap(); - let path = link_entry.path(); - if path.is_dir() { - continue; - } - if path.file_name() == None { - continue; - } - let contents = match fs::read_to_string(path) { - Ok(c) => c, - Err(e) => { - log::error!("reading file fail {}", e); - return; - } - }; - let info_e = contents.parse::(); - match info_e { - Ok(ref c) => c, - Err(_) => { - log::error!("parse config fail"); - return; - } - }; - - let info = info_e.unwrap(); - elf_files.push(info.elf_path); - // Clear all files in sysboost_dir. - match fs::remove_file(entry.path()) { - Ok(c) => c, - Err(e) => { - log::error!("remove file fail {}", e); - return; - } - }; - } - } - - // Restore the original file pointed to by elf_file. - for file_path in elf_files { - let file_path_buf = PathBuf::from(file_path); - let bak_file_path = file_path_buf.clone().with_extension("link"); - match fs::rename(&bak_file_path, &file_path_buf) { - Ok(c) => c, - Err(e) => { - log::error!("rename file fail {}", e); - return; - } - }; - } -} - pub fn daemon_loop() { insmod_ko(&KO_PATH.to_string()); // When rebooting, you should clean up the backup environment - // clean_env(); loop { start_service(); } diff --git a/tests/test_sysboostd.rs b/tests/test_sysboostd.rs index ad518ff..ae19823 100644 --- a/tests/test_sysboostd.rs +++ b/tests/test_sysboostd.rs @@ -18,11 +18,10 @@ mod tests { use std::io::Read; use std::io::Write; use std::process::Command; - use std::path::Path; use std::io::{BufRead, BufReader}; - use std::fs::OpenOptions; use std::{thread, time}; - + use std::thread::sleep; + use std::time::Duration; // Normal Scenarios // 1. try to start sysboostd, if sysboostd @@ -160,7 +159,7 @@ mod tests { for line in lines{ if let Ok(data) = line { - if (data.contains(message)) { + if data.contains(message) { return true; } } @@ -180,7 +179,6 @@ mod tests { #[test] fn test_print_log_messages() { // clear /var/log/message - let file_path = "/var/log/messages"; clear_log_message(); // stop sysboost let output = Command::new("systemctl").args(&["is-active", "sysboost.service"]).output().expect("Failed to execute command"); @@ -216,14 +214,15 @@ mod tests { thread::sleep(sleep_millis); let has_message = is_contain_log_message("Started Run sysboost for Kunpeng CPU"); assert!(has_message, "log info is not print in message!"); - let has_Daemon_message = is_contain_log_message("On Daemon"); - assert!(has_Daemon_message, "log info is not print in message!"); + let has_daemon_message = is_contain_log_message("On Daemon"); + assert!(has_daemon_message, "log info is not print in message!"); } // Unnormal Scenarios // 1、When sysboostd break + #[test] fn test_restore_sysboostd_env() { -// Create libtinfo.toml file in /etc/sysboost.d directory + // Create libtinfo.toml file in /etc/sysboost.d directory let toml_path = "/etc/sysboost.d/libtinfo.toml"; let toml_content = "elf_path = '/usr/lib64/libtinfo.so' mode = 'bolt'"; fs::write(toml_path, toml_content).unwrap(); -- Gitee