diff --git a/bin/daemon.rs b/bin/daemon.rs index 29f829fa3280ec57aba649d4f258024a5bd762c1..70eceb01d43af5ff44dbc9b5495eca3a719f57d4 100644 --- a/bin/daemon.rs +++ b/bin/daemon.rs @@ -67,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(_) => { @@ -385,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; @@ -399,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 { @@ -588,7 +585,7 @@ fn insmod_ko(path: &String) { pub fn daemon_loop() { insmod_ko(&KO_PATH.to_string()); - // TODO: clean env + // When rebooting, you should clean up the backup environment loop { start_service(); } diff --git a/sysboost.service b/sysboost.service index 7cb1d7a43b49da5dbbb94ee943039eac634cfbfb..e2d607ed0eefc88c2101e19fd12a30d29a14ee54 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 diff --git a/tests/test_sysboostd.rs b/tests/test_sysboostd.rs index edd8a979addf934edfdc938ca082b7f9d477e1c3..ae198239c130b9b2a50bacd215509c23cc7d0021 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,11 +214,41 @@ 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 + 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()); + } }