From cff9de29e12217a8a3a0bba84ef457ecb67c6923 Mon Sep 17 00:00:00 2001 From: Zhou Kang Date: Mon, 25 Sep 2023 11:05:33 +0000 Subject: [PATCH] format code --- bin/aot.rs | 12 +- bin/bolt.rs | 13 +- bin/common.rs | 2 +- bin/config.rs | 4 +- bin/coredump_monitor.rs | 538 ++++++++++++++++++++-------------------- bin/daemon.rs | 35 ++- bin/kmod_util.rs | 4 +- bin/lib/process_ext.rs | 2 +- bin/main.rs | 22 +- tests/test_sysboostd.rs | 45 ++-- 10 files changed, 323 insertions(+), 354 deletions(-) diff --git a/bin/aot.rs b/bin/aot.rs index 5a1d9b0..a8fa24b 100644 --- a/bin/aot.rs +++ b/bin/aot.rs @@ -9,17 +9,17 @@ // See the Mulan PSL v2 for more details. // Create: 2023-8-26 -use crate::lib::fs_ext; -use crate::lib::process_ext::run_child; use crate::common::SYSBOOST_PATH; use crate::config::RtoConfig; +use crate::lib::fs_ext; +use crate::lib::process_ext::run_child; -use std::{fs, env}; -use std::path::Path; use goblin::elf::Elf; +use std::path::Path; +use std::{env, fs}; // Obtain the full path from real path, environment variable PATH, current dir -fn get_lib_full_path(lib: &str, confpaths:Vec<&str>, rpaths: Vec<&str>, paths: Vec<&str>) -> Option { +fn get_lib_full_path(lib: &str, confpaths: Vec<&str>, rpaths: Vec<&str>, paths: Vec<&str>) -> Option { if !(confpaths.is_empty()) { for confpath in confpaths { let full_dir = fs_ext::find_file_in_dirs(&lib, &confpath); @@ -57,7 +57,7 @@ pub fn parse_elf_file(elf_path: &str) -> Option { Ok(elf_bytes) => elf_bytes, Err(_e) => { log::info!("Error: read elf file fault, please check config."); - return None + return None; } }; match Elf::parse(&elf_bytes) { diff --git a/bin/bolt.rs b/bin/bolt.rs index 3c4db86..5410f51 100644 --- a/bin/bolt.rs +++ b/bin/bolt.rs @@ -9,10 +9,10 @@ // See the Mulan PSL v2 for more details. // Create: 2023-8-28 -use crate::lib::process_ext::run_child; -use crate::common::ARCH; use crate::common::set_thp; +use crate::common::ARCH; use crate::config::RtoConfig; +use crate::lib::process_ext::run_child; use std::fs; use std::path::Path; @@ -29,8 +29,8 @@ fn get_profile_path(conf: &RtoConfig) -> String { match Path::new(&conf.elf_path).file_name() { Some(app_file_name) => { return format!("{}{}.profile.{}", SYSBOOST_BOLT_PROFILE, app_file_name.to_string_lossy(), ARCH); - }, - None => {}, + } + None => {} } } return "".to_string(); @@ -72,7 +72,6 @@ fn bolt_optimize_bin(conf: &RtoConfig) -> i32 { let real_profile_path = get_profile_path(conf); if real_profile_path != "" { args.push(format!("-data={}", real_profile_path)); - } let ret = run_child("/usr/bin/llvm-bolt", &args); @@ -123,8 +122,8 @@ fn is_mysqld(conf: &RtoConfig) -> bool { if app_file_name == "mysqld" { return true; } - }, - None => {}, + } + None => {} } return false; } diff --git a/bin/common.rs b/bin/common.rs index b0aeab5..12eae14 100644 --- a/bin/common.rs +++ b/bin/common.rs @@ -21,7 +21,7 @@ pub const ARCH: &str = "aarch64"; // echo always > /sys/kernel/mm/transparent_hugepage/enabled pub fn set_thp() -> i32 { - let args: Vec = Vec::new(); + let args: Vec = Vec::new(); let ret = run_child("echo always > /sys/kernel/mm/transparent_hugepage/enabled", &args); return ret; } diff --git a/bin/config.rs b/bin/config.rs index dd3b134..f5a4a7a 100644 --- a/bin/config.rs +++ b/bin/config.rs @@ -12,9 +12,9 @@ use crate::common::SYSBOOST_PATH; use serde::Deserialize; -use std::str::FromStr; -use std::path::PathBuf; use std::fs; +use std::path::PathBuf; +use std::str::FromStr; #[derive(Debug, Deserialize)] pub struct RtoConfig { diff --git a/bin/coredump_monitor.rs b/bin/coredump_monitor.rs index f415c50..eaacb90 100644 --- a/bin/coredump_monitor.rs +++ b/bin/coredump_monitor.rs @@ -12,15 +12,15 @@ use crate::aot::set_app_link_flag; use crate::daemon; -use log::{self}; +use cnproc::PidEvent; +use cnproc::PidMonitor; use lazy_static::lazy_static; +use log::{self}; +use procfs::Process; use std::collections::HashMap; -use cnproc::PidMonitor; -use cnproc::PidEvent; -use std::sync::Mutex; use std::fs; -use procfs::Process; use std::path::Path; +use std::sync::Mutex; const BASH_RTO_PATH: &str = "/usr/bin/bash.rto"; const BASH_PATH: &str = "/usr/bin/bash"; @@ -28,296 +28,290 @@ const BASH_TOML_PATH: &str = "/etc/sysboost.d/bash.toml"; const BASH_LINK_PATH: &str = "/var/lib/sysboost/bash"; lazy_static! { - static ref MERGE_FILES: Mutex> = Mutex::new(Vec::new()); - static ref PID_INFOS: Mutex> = Mutex::new(HashMap::new()); + static ref MERGE_FILES: Mutex> = Mutex::new(Vec::new()); + static ref PID_INFOS: Mutex> = Mutex::new(HashMap::new()); } fn init_merge_file_list() { - MERGE_FILES.lock().unwrap().push(BASH_PATH.to_string()); + MERGE_FILES.lock().unwrap().push(BASH_PATH.to_string()); } fn process_exec_event(pid: i32) { - // get execute file_path - let process = match Process::new(pid) { - Ok(process) => process, - Err(e) => { - log::error!("Failed to get execte process: {}", e); - return; - } - }; - let file_path = match process.exe() { - Ok(file_path) => file_path, - Err(e) => { - log::error!("Failed to get excute file path: {}", e); - return; - } - }; - if MERGE_FILES.lock().unwrap().contains(&file_path.as_path().display().to_string()) == false { - return; - } - PID_INFOS.lock().unwrap().insert(pid, String::from(file_path.to_str().unwrap())); + // get execute file_path + let process = match Process::new(pid) { + Ok(process) => process, + Err(e) => { + log::error!("Failed to get execte process: {}", e); + return; + } + }; + let file_path = match process.exe() { + Ok(file_path) => file_path, + Err(e) => { + log::error!("Failed to get excute file path: {}", e); + return; + } + }; + if MERGE_FILES.lock().unwrap().contains(&file_path.as_path().display().to_string()) == false { + return; + } + PID_INFOS.lock().unwrap().insert(pid, String::from(file_path.to_str().unwrap())); } fn do_bash_rollback() -> i32 { - // unset flag - let ret = set_app_link_flag(&BASH_PATH.to_string(), false); - if ret != 0 { - log::error!("Failed to unset link flag for bash!"); - return ret; - } - // remove link - daemon::db_remove_link(&BASH_LINK_PATH.to_string()); - // remove bash.rto - let bash_rto = Path::new(BASH_RTO_PATH); - match fs::remove_file(&bash_rto) { - Ok(_) => {} - Err(e) => { - log::error!("remove file failed: {}", e); - return -1; - } - } - - // rename bash.toml - let bash_toml = Path::new(BASH_TOML_PATH); - let bash_toml_err = bash_toml.with_extension("toml.err"); - match fs::rename(&bash_toml, &bash_toml_err) { - Ok(_) => {} - Err(e) => { - log::error!("Mv failed: {}", e); - return -1; - } - } - return 0; + // unset flag + let ret = set_app_link_flag(&BASH_PATH.to_string(), false); + if ret != 0 { + log::error!("Failed to unset link flag for bash!"); + return ret; + } + // remove link + daemon::db_remove_link(&BASH_LINK_PATH.to_string()); + // remove bash.rto + let bash_rto = Path::new(BASH_RTO_PATH); + match fs::remove_file(&bash_rto) { + Ok(_) => {} + Err(e) => { + log::error!("remove file failed: {}", e); + return -1; + } + } + + // rename bash.toml + let bash_toml = Path::new(BASH_TOML_PATH); + let bash_toml_err = bash_toml.with_extension("toml.err"); + match fs::rename(&bash_toml, &bash_toml_err) { + Ok(_) => {} + Err(e) => { + log::error!("Mv failed: {}", e); + return -1; + } + } + return 0; } fn do_common_rollback(file_path: &String) -> i32 { - let file_exist = Path::new(file_path).exists(); - if !file_exist { - log::error!("{} is not exist!", file_path); - } - match fs::remove_file(&file_path) { - Ok(_) => {} - Err(e) => { - log::error!("remove file failed: {}", e); - return -1; - } - } - return 0; + let file_exist = Path::new(file_path).exists(); + if !file_exist { + log::error!("{} is not exist!", file_path); + } + match fs::remove_file(&file_path) { + Ok(_) => {} + Err(e) => { + log::error!("remove file failed: {}", e); + return -1; + } + } + return 0; } fn process_coredump_event(pid: i32) { - // get file path by pid - if PID_INFOS.lock().unwrap().contains_key(&pid) == false { - log::info!("{} is not exist in PID_INFOS!", pid); - return; - } - - if let Some(file_path) = PID_INFOS.lock().unwrap().get(&pid) { - log::info!("{} has create a coredump!", file_path); - if MERGE_FILES.lock().unwrap().contains(&file_path) == false { - return; - } - - if file_path == BASH_PATH { - let ret = do_bash_rollback(); - if ret != 0 { - log::error!("rollback bash failed!"); - } - } else { - let ret = do_common_rollback(file_path); - if ret != 0 { - log::error!("rollback {} failed!", file_path); - } - } - } - - PID_INFOS.lock().unwrap().remove(&pid); + // get file path by pid + if PID_INFOS.lock().unwrap().contains_key(&pid) == false { + log::info!("{} is not exist in PID_INFOS!", pid); + return; + } + + if let Some(file_path) = PID_INFOS.lock().unwrap().get(&pid) { + log::info!("{} has create a coredump!", file_path); + if MERGE_FILES.lock().unwrap().contains(&file_path) == false { + return; + } + + if file_path == BASH_PATH { + let ret = do_bash_rollback(); + if ret != 0 { + log::error!("rollback bash failed!"); + } + } else { + let ret = do_common_rollback(file_path); + if ret != 0 { + log::error!("rollback {} failed!", file_path); + } + } + } + + PID_INFOS.lock().unwrap().remove(&pid); } fn process_exit_event(pid: i32) { - PID_INFOS.lock().unwrap().remove(&pid); + PID_INFOS.lock().unwrap().remove(&pid); } pub fn coredump_monitor_loop() { - init_merge_file_list(); - let mut monitor = match PidMonitor::new() { - Ok(p) => p, - Err(e) => panic!("create PidMonitor failed: {}", e) - }; - loop { - match monitor.recv() { - None => {} - Some(event) => { - match event { - PidEvent::Exec(pid) => process_exec_event(pid), - PidEvent::Coredump(pid) => process_coredump_event(pid), - PidEvent::Exit(pid) => process_exit_event(pid), - _ => {} - } - } - } - } + init_merge_file_list(); + let mut monitor = match PidMonitor::new() { + Ok(p) => p, + Err(e) => panic!("create PidMonitor failed: {}", e), + }; + loop { + match monitor.recv() { + None => {} + Some(event) => match event { + PidEvent::Exec(pid) => process_exec_event(pid), + PidEvent::Coredump(pid) => process_coredump_event(pid), + PidEvent::Exit(pid) => process_exit_event(pid), + _ => {} + }, + } + } } #[cfg(test)] mod tests { - use super::*; - use std::process::Command; - use std::fs::File; - use std::{thread, time}; - - const COREDUMP_TEST_PATH: &str = "tests/test_coredump/test.c"; - const EXCUTE_TEST_PATH: &str = "tests/test_coredump/test"; - - fn add_merge_file(file_path: String) { - MERGE_FILES.lock().unwrap().push(file_path); - } - - #[test] - fn test_coredump_monitor() { - // create excute file - let source_file = Path::new(COREDUMP_TEST_PATH); - let source_file = match fs::canonicalize(source_file) { - Ok(p) => p, - Err(e) => { - panic!("Failed to get realpath: {}", e); - } - }; - let source_file_exist = source_file.exists(); - assert!(source_file_exist == true, "coredump source file does not exist!"); - let excute_file = Path::new(EXCUTE_TEST_PATH); - - let output = Command::new("gcc").args(&["-o", &excute_file.to_str().unwrap(), &source_file.to_str().unwrap()]) - .output().expect("Faild to execute command!"); - if !output.status.success() { - panic!("Failed to create excute file!"); - } - let real_excute_file = match fs::canonicalize(excute_file) { - Ok(p) => p, - Err(e) => { - panic!("Failed to get realpath: {}", e); - } - }; - - let excute_file_exist = real_excute_file.exists(); - assert!(excute_file_exist == true, "excute file is not exist!"); - - add_merge_file(real_excute_file.to_str().unwrap().to_string()); - // do coredump monitor - let _coredump_monitor = thread::spawn(|| { - coredump_monitor_loop(); - }); - - - // excute a coredump action - let excute_command = String::from("./") + excute_file.to_str().unwrap(); - let output = Command::new(&excute_command).output() - .expect("Failed to excute command!"); - - if output.status.success() { - panic!("Coredump has not created!"); - } - let excute_file_exist = excute_file.exists(); - assert!(excute_file_exist == false, "excute file is not deleted!"); - } - - fn create_or_backup_file(src_path: &str, dest_path: &str) { - let file = Path::new(src_path); - let file_exist = file.exists(); - if file_exist { - match fs::copy(src_path, dest_path) { - Ok(_p) => {}, - Err(e) => { - panic!("Failed to rename file: {}", e); - } - } - } else { - match File::create(&src_path) { - Ok(_p) => (), - Err(e) => { - panic!("Failed to create file: {}", e); - } - } - } - } - - fn reset_file(bak_path: &str, src_path: &str) { - let file = Path::new(bak_path); - let file_exist = file.exists(); - if file_exist { - match fs::rename(bak_path, src_path) { - Ok(_p) => {}, - Err(e) => { - panic!("Failed to rename file: {}", e); - } - } - } - } - - fn reset_env() { - let bash_link_backup: &str = "/var/lib/sysboost/bash.bak"; - let bash_link_path: &str = "/var/lib/sysboost/bash"; - reset_file(bash_link_backup, bash_link_path); - let bash_toml_backup: &str = "/etc/sysboost.d/bash.tomlbak"; - let bash_toml_path: &str = "/etc/sysboost.d/bash.toml"; - reset_file(bash_toml_backup, bash_toml_path); - let bash_rto_backup: &str = "/usr/bin/bash.rtobak"; - let bash_rto_path: &str = "/usr/bin/bash.rto"; - reset_file(bash_rto_backup, bash_rto_path); - } - - #[test] - fn test_bash_coredump() { - // create link file - let bash_link_path: &str = "/var/lib/sysboost/bash"; - let bash_link_backup: &str = "/var/lib/sysboost/bash.bak"; - create_or_backup_file(bash_link_path, bash_link_backup); - // create toml file - let bash_toml_path: &str = "/etc/sysboost.d/bash.toml"; - let bash_toml_err_path: &str = "/etc/sysboost.d/bash.toml.err"; - let bash_toml_backup: &str = "/etc/sysboost.d/bash.tomlbak"; - create_or_backup_file(bash_toml_path, bash_toml_backup); - // create bash rto file - let bash_rto_path: &str = "/usr/bin/bash.rto"; - let bash_rto_backup: &str = "/usr/bin/bash.rtobak"; - create_or_backup_file(bash_rto_path, bash_rto_backup); - - // start sysboost - let output = Command::new("systemctl").args(&["start", "sysboost.service"]).output().expect("Failed to start sysboost"); - if !output.status.success() { - panic!("Failed to start sysboost service: {}", String::from_utf8_lossy(&output.stderr)); - } - - let sleep_millis = time::Duration::from_millis(1000); - thread::sleep(sleep_millis); - - // create a coredump for bash - let output = Command::new("bash") - .arg("-c") - .arg("kill -s SIGSEGV $$") - .output() - .expect("Failed to excute command!"); - - if output.status.success() { - panic!("Coredump has not created!"); - } - - let bash_link_file = Path::new(bash_link_path); - let bash_link_exist = bash_link_file.exists(); - assert_eq!(bash_link_exist, false); - - let bash_toml_file = Path::new(bash_toml_path); - let bash_toml_exist = bash_toml_file.exists(); - assert_eq!(bash_toml_exist, false); - - let bash_toml_err_file = Path::new(bash_toml_err_path); - let bash_toml_err_exist = bash_toml_err_file.exists(); - assert_eq!(bash_toml_err_exist, true); - - let bash_rto_file = Path::new(bash_rto_path); - let bash_rto_exist = bash_rto_file.exists(); - assert_eq!(bash_rto_exist, false); - - reset_env(); - } + use super::*; + use std::fs::File; + use std::process::Command; + use std::{thread, time}; + + const COREDUMP_TEST_PATH: &str = "tests/test_coredump/test.c"; + const EXCUTE_TEST_PATH: &str = "tests/test_coredump/test"; + + fn add_merge_file(file_path: String) { + MERGE_FILES.lock().unwrap().push(file_path); + } + + #[test] + fn test_coredump_monitor() { + // create excute file + let source_file = Path::new(COREDUMP_TEST_PATH); + let source_file = match fs::canonicalize(source_file) { + Ok(p) => p, + Err(e) => { + panic!("Failed to get realpath: {}", e); + } + }; + let source_file_exist = source_file.exists(); + assert!(source_file_exist == true, "coredump source file does not exist!"); + let excute_file = Path::new(EXCUTE_TEST_PATH); + + let output = Command::new("gcc") + .args(&["-o", &excute_file.to_str().unwrap(), &source_file.to_str().unwrap()]) + .output() + .expect("Faild to execute command!"); + if !output.status.success() { + panic!("Failed to create excute file!"); + } + let real_excute_file = match fs::canonicalize(excute_file) { + Ok(p) => p, + Err(e) => { + panic!("Failed to get realpath: {}", e); + } + }; + + let excute_file_exist = real_excute_file.exists(); + assert!(excute_file_exist == true, "excute file is not exist!"); + + add_merge_file(real_excute_file.to_str().unwrap().to_string()); + // do coredump monitor + let _coredump_monitor = thread::spawn(|| { + coredump_monitor_loop(); + }); + + // excute a coredump action + let excute_command = String::from("./") + excute_file.to_str().unwrap(); + let output = Command::new(&excute_command).output().expect("Failed to excute command!"); + + if output.status.success() { + panic!("Coredump has not created!"); + } + let excute_file_exist = excute_file.exists(); + assert!(excute_file_exist == false, "excute file is not deleted!"); + } + + fn create_or_backup_file(src_path: &str, dest_path: &str) { + let file = Path::new(src_path); + let file_exist = file.exists(); + if file_exist { + match fs::copy(src_path, dest_path) { + Ok(_p) => {} + Err(e) => { + panic!("Failed to rename file: {}", e); + } + } + } else { + match File::create(&src_path) { + Ok(_p) => (), + Err(e) => { + panic!("Failed to create file: {}", e); + } + } + } + } + + fn reset_file(bak_path: &str, src_path: &str) { + let file = Path::new(bak_path); + let file_exist = file.exists(); + if file_exist { + match fs::rename(bak_path, src_path) { + Ok(_p) => {} + Err(e) => { + panic!("Failed to rename file: {}", e); + } + } + } + } + + fn reset_env() { + let bash_link_backup: &str = "/var/lib/sysboost/bash.bak"; + let bash_link_path: &str = "/var/lib/sysboost/bash"; + reset_file(bash_link_backup, bash_link_path); + let bash_toml_backup: &str = "/etc/sysboost.d/bash.tomlbak"; + let bash_toml_path: &str = "/etc/sysboost.d/bash.toml"; + reset_file(bash_toml_backup, bash_toml_path); + let bash_rto_backup: &str = "/usr/bin/bash.rtobak"; + let bash_rto_path: &str = "/usr/bin/bash.rto"; + reset_file(bash_rto_backup, bash_rto_path); + } + + #[test] + fn test_bash_coredump() { + // create link file + let bash_link_path: &str = "/var/lib/sysboost/bash"; + let bash_link_backup: &str = "/var/lib/sysboost/bash.bak"; + create_or_backup_file(bash_link_path, bash_link_backup); + // create toml file + let bash_toml_path: &str = "/etc/sysboost.d/bash.toml"; + let bash_toml_err_path: &str = "/etc/sysboost.d/bash.toml.err"; + let bash_toml_backup: &str = "/etc/sysboost.d/bash.tomlbak"; + create_or_backup_file(bash_toml_path, bash_toml_backup); + // create bash rto file + let bash_rto_path: &str = "/usr/bin/bash.rto"; + let bash_rto_backup: &str = "/usr/bin/bash.rtobak"; + create_or_backup_file(bash_rto_path, bash_rto_backup); + + // start sysboost + let output = Command::new("systemctl").args(&["start", "sysboost.service"]).output().expect("Failed to start sysboost"); + if !output.status.success() { + panic!("Failed to start sysboost service: {}", String::from_utf8_lossy(&output.stderr)); + } + + let sleep_millis = time::Duration::from_millis(1000); + thread::sleep(sleep_millis); + + // create a coredump for bash + let output = Command::new("bash").arg("-c").arg("kill -s SIGSEGV $$").output().expect("Failed to excute command!"); + + if output.status.success() { + panic!("Coredump has not created!"); + } + + let bash_link_file = Path::new(bash_link_path); + let bash_link_exist = bash_link_file.exists(); + assert_eq!(bash_link_exist, false); + + let bash_toml_file = Path::new(bash_toml_path); + let bash_toml_exist = bash_toml_file.exists(); + assert_eq!(bash_toml_exist, false); + + let bash_toml_err_file = Path::new(bash_toml_err_path); + let bash_toml_err_exist = bash_toml_err_file.exists(); + assert_eq!(bash_toml_err_exist, true); + + let bash_rto_file = Path::new(bash_rto_path); + let bash_rto_exist = bash_rto_file.exists(); + assert_eq!(bash_rto_exist, false); + + reset_env(); + } } diff --git a/bin/daemon.rs b/bin/daemon.rs index 98d56d9..79ce9d0 100644 --- a/bin/daemon.rs +++ b/bin/daemon.rs @@ -9,18 +9,18 @@ // See the Mulan PSL v2 for more details. // Create: 2023-4-20 -use crate::lib::fs_ext; -use crate::kmod_util::set_ko_rto_flag; -use crate::kmod_util::set_hpage_rto_flag; -use crate::kmod_util::insmod_sysboost_ko; -use crate::kmod_util::test_kmod; -use crate::config::RtoConfig; -use crate::config::read_config; +use crate::aot::find_libs; use crate::aot::gen_app_rto; -use crate::aot::set_app_link_flag; use crate::aot::parse_elf_file; -use crate::aot::find_libs; +use crate::aot::set_app_link_flag; use crate::bolt::bolt_optimize; +use crate::config::read_config; +use crate::config::RtoConfig; +use crate::kmod_util::insmod_sysboost_ko; +use crate::kmod_util::set_hpage_rto_flag; +use crate::kmod_util::set_ko_rto_flag; +use crate::kmod_util::test_kmod; +use crate::lib::fs_ext; use inotify::{EventMask, Inotify, WatchMask}; use log::{self}; @@ -75,14 +75,14 @@ fn sysboost_core_process(conf: &RtoConfig) -> i32 { log::error!("Error: bolt mode start fault."); return ret; } - }, + } "static" | "static-nolibc" | "share" => { let ret = gen_app_rto(&conf); if ret != 0 { log::error!("Error: generate rto start fault."); return ret; } - }, + } _ => { log::info!("Warning: read elf file fault, please check config."); // handle other cases @@ -332,9 +332,9 @@ pub fn daemon_loop() { #[cfg(test)] mod tests { use super::*; + use crate::lib::process_ext::run_child; use basic::logger::{self}; use std::process::Command; - use crate::lib::process_ext::run_child; #[test] fn test_check_elf_files_modify_1() { @@ -429,13 +429,10 @@ mod tests { String::from("/lib/ld-linux-aarch64.so.1"), ]; - let bash_libs_nolibc = vec![ - String::from("/usr/lib64/libtinfo.so.6"), - ]; + let bash_libs_nolibc = vec![String::from("/usr/lib64/libtinfo.so.6")]; assert_eq!(libs, bash_libs); assert_eq!(libs_nolibc, bash_libs_nolibc); - } #[test] @@ -475,13 +472,9 @@ mod tests { String::from("/lib64/ld-linux-x86-64.so.2"), ]; - let bash_libs_nolibc = vec![ - String::from("/usr/lib64/libtinfo.so.6"), - ]; + let bash_libs_nolibc = vec![String::from("/usr/lib64/libtinfo.so.6")]; assert_eq!(libs, bash_libs); assert_eq!(libs_nolibc, bash_libs_nolibc); - } - } diff --git a/bin/kmod_util.rs b/bin/kmod_util.rs index 8d6ab98..b794da2 100644 --- a/bin/kmod_util.rs +++ b/bin/kmod_util.rs @@ -22,7 +22,7 @@ pub fn set_ko_rto_flag(is_set: bool) -> i32 { if is_set { args = "1".to_string(); } else { - args= "0".to_string(); + args = "0".to_string(); } match fs::write(KO_RTO_PARAM_PATH.to_string(), args) { Ok(_) => { @@ -42,7 +42,7 @@ pub fn set_hpage_rto_flag(is_set: bool) -> i32 { if is_set { args = "1".to_string(); } else { - args= "0".to_string(); + args = "0".to_string(); } match fs::write(HPAGE_RTO_PARAM_PATH.to_string(), args) { Ok(_) => { diff --git a/bin/lib/process_ext.rs b/bin/lib/process_ext.rs index 1804625..315ce05 100644 --- a/bin/lib/process_ext.rs +++ b/bin/lib/process_ext.rs @@ -9,8 +9,8 @@ // See the Mulan PSL v2 for more details. // Create: 2023-8-26 -use std::process::{Command, Stdio}; use std::io::{BufRead, BufReader}; +use std::process::{Command, Stdio}; pub fn run_child(cmd: &str, args: &Vec) -> i32 { log::info!("run child: {}, {}", cmd, args.join(" ").to_string()); diff --git a/bin/main.rs b/bin/main.rs index 126c3d3..df2bb3b 100644 --- a/bin/main.rs +++ b/bin/main.rs @@ -9,18 +9,18 @@ // See the Mulan PSL v2 for more details. // Create: 2023-4-20 -mod lib; -mod common; -mod config; -mod kmod_util; mod aot; mod bolt; -mod daemon; +mod common; +mod config; mod coredump_monitor; +mod daemon; +mod kmod_util; +mod lib; -use crate::kmod_util::test_kmod; -use crate::daemon::daemon_loop; use crate::coredump_monitor::coredump_monitor_loop; +use crate::daemon::daemon_loop; +use crate::kmod_util::test_kmod; use basic::logger::{self}; use daemonize::Daemonize; @@ -72,10 +72,10 @@ fn main() { } } - // start up coredump monitor - // let _coredump_monitor_handle = thread::spawn(||{ - // coredump_monitor_loop(); - // }); + // start up coredump monitor + // let _coredump_monitor_handle = thread::spawn(||{ + // coredump_monitor_loop(); + // }); // daemon service gen rto ELF with config daemon_loop(); diff --git a/tests/test_sysboostd.rs b/tests/test_sysboostd.rs index 3436fa0..a138224 100644 --- a/tests/test_sysboostd.rs +++ b/tests/test_sysboostd.rs @@ -9,7 +9,6 @@ // See the Mulan PSL v2 for more details. // Create: 2023-5-17 - // test all sysboostd modes and features #[cfg(test)] mod tests { @@ -17,11 +16,11 @@ mod tests { use std::fs::File; use std::io::Read; use std::io::Write; - use std::process::Command; use std::io::{BufRead, BufReader}; - use std::{thread, time}; + use std::process::Command; use std::thread::sleep; use std::time::Duration; + use std::{thread, time}; // Normal Scenarios // 1. try to start sysboostd, if sysboostd @@ -61,7 +60,7 @@ mod tests { let bash_rto_path = "/usr/bin/bash.rto"; if std::path::Path::new(bash_rto_path).exists() { match std::fs::remove_file(bash_rto_path) { - Ok(_) => {}, + Ok(_) => {} Err(e) => { panic!("Failed to remove old bash.rto file: {}", e); } @@ -130,34 +129,23 @@ mod tests { .expect("Failed to execute command"); // Check the status code of the command - assert!( - output.status.success(), - "Failed to replace bash.rto with Python: {}", - String::from_utf8_lossy(&output.stderr) - ); + assert!(output.status.success(), "Failed to replace bash.rto with Python: {}", String::from_utf8_lossy(&output.stderr)); // Restart Bash and check if it's running Python - let output = Command::new("bash") - .args(&["-c", "\"print('Python is running')\""]) - .output() - .expect("Failed to execute command"); + let output = Command::new("bash").args(&["-c", "\"print('Python is running')\""]).output().expect("Failed to execute command"); // Check the output of the Python program let stdout = String::from_utf8_lossy(&output.stdout); let stderr = String::from_utf8_lossy(&output.stderr); - assert!( - stdout.trim() == "Python is running", - "Bash is not running Python: {}", - stderr.trim() - ); + assert!(stdout.trim() == "Python is running", "Bash is not running Python: {}", stderr.trim()); } - fn is_contain_log_message(message : &str) -> bool { + fn is_contain_log_message(message: &str) -> bool { let file_name = "/var/log/messages"; let file = File::open(file_name).unwrap(); let lines = BufReader::new(file).lines(); - for line in lines{ + for line in lines { if let Ok(data) = line { if data.contains(message) { return true; @@ -170,7 +158,7 @@ mod tests { fn clear_log_message() { let cmd_string = "> /var/log/messages".to_string(); - let output = Command::new("bash").arg("-c").arg(cmd_string).output().expect("Failed to execute command"); + let output = Command::new("bash").arg("-c").arg(cmd_string).output().expect("Failed to execute command"); if !output.status.success() { panic!("Failed to clear file"); } @@ -212,10 +200,10 @@ mod tests { // check log message let sleep_millis = time::Duration::from_millis(1000); thread::sleep(sleep_millis); - let has_message = is_contain_log_message("Started Run sysboost for Kunpeng CPU"); + 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 @@ -234,11 +222,7 @@ mod tests { fs::remove_file(toml_path).unwrap(); // Restart sysboostd service using systemctl - let output = Command::new("systemctl") - .arg("restart") - .arg("sysboostd") - .output() - .unwrap(); + 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 @@ -254,7 +238,7 @@ mod tests { fn kill_pid(pid: u32) { match Command::new("kill").arg("-9").arg(pid.to_string()).output() { Ok(_) => return, - Err(_) => return + Err(_) => return, } } @@ -286,4 +270,3 @@ mod tests { kill_pid(c_pid); } } - -- Gitee