From a3362d8f882a7ec86972591a661f1765cf5a8d2c Mon Sep 17 00:00:00 2001 From: Zhou Kang Date: Mon, 25 Sep 2023 07:43:59 +0000 Subject: [PATCH] fix set link flag --- bin/aot.rs | 69 ++++++++++++++--------------------------- bin/coredump_monitor.rs | 28 ++++++++--------- bin/daemon.rs | 14 ++++----- bin/lib/fs_ext.rs | 4 +-- 4 files changed, 47 insertions(+), 68 deletions(-) diff --git a/bin/aot.rs b/bin/aot.rs index 2172dc1..5a1d9b0 100644 --- a/bin/aot.rs +++ b/bin/aot.rs @@ -90,44 +90,27 @@ pub fn find_libs(conf: &RtoConfig, elf: &Elf) -> Vec { } } -// pub fn set_app_aot_flag(old_path: &String, is_set: bool) -> i32 { -// let mut args: Vec = Vec::new(); -// let setfattr = "setfattr".to_string(); -// args.push("-n".to_string()); -// args.push("trusted.sysboost_flags".to_string()); -// args.push("-v".to_string()); -// if is_set { -// args.push("true".to_string()); -// } else { -// args.push("false".to_string()); -// } -// let old_path = Path::new(old_path); -// let old_path = match fs::canonicalize(old_path) { -// Ok(p) => p, -// Err(e) => { -// log::error!("get realpath failed: {}", e); -// return -1; -// } -// }; -// let new_path = old_path.with_extension("bak"); -// match fs::copy(&old_path, &new_path) { -// Ok(_) => {} -// Err(e) => { -// log::error!("Copy failed: {}", e); -// return -1; -// } -// } -// args.push(new_path.to_str().unwrap().to_string()); -// let ret = run_child(&setfattr, &args); -// match fs::rename(&new_path, &old_path) { -// Ok(_) => {} -// Err(e) => { -// log::error!("Mv failed: {}", e); -// return -1; -// } -// } -// return ret; -// } +pub fn set_app_link_flag(path: &String, is_set: bool) -> i32 { + let mut args: Vec = Vec::new(); + if is_set { + args.push("--set".to_string()); + } else { + args.push("--unset".to_string()); + } + + // 回滚场景, 路径是软链接要转换为真实路径 + let real_path = match fs::canonicalize(path) { + Ok(p) => p, + Err(e) => { + log::error!("get realpath failed: {}", e); + return -1; + } + }; + + args.push(format!("{}", real_path.to_string_lossy())); + let ret = run_child(SYSBOOST_PATH, &args); + return ret; +} // 生成rto文件 // rto文件先生成到临时文件, 然后mv到最终路径, 避免并发操作文件问题 @@ -165,16 +148,12 @@ pub fn gen_app_rto(conf: &RtoConfig) -> i32 { set_mod.push("755".to_string()); set_mod.push(format!("{}.rto", conf.elf_path)); ret = run_child("/usr/bin/chmod", &set_mod); - if ret != 0 { - return ret; - } - let mut set_bash: Vec = Vec::new(); - set_bash.push("--set".to_string()); - set_bash.push(format!("{}", conf.elf_path)); - ret = run_child(SYSBOOST_PATH, &set_bash); if ret != 0 { return ret; } + // 设置链接标志位 + ret = set_app_link_flag(&conf.elf_path, true); + return ret; } diff --git a/bin/coredump_monitor.rs b/bin/coredump_monitor.rs index af8fcf2..f415c50 100644 --- a/bin/coredump_monitor.rs +++ b/bin/coredump_monitor.rs @@ -9,7 +9,7 @@ // See the Mulan PSL v2 for more details. // Create: 2023-7-13 -//use crate::aot::set_app_aot_flag; +use crate::aot::set_app_link_flag; use crate::daemon; use log::{self}; @@ -60,11 +60,11 @@ fn process_exec_event(pid: i32) { fn do_bash_rollback() -> i32 { // unset flag - // let ret = set_app_aot_flag(&BASH_PATH.to_string(), false); - // if ret != 0 { - // log::error!("Failed to unset flag for bash!"); - // return ret; - // } + 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 @@ -111,13 +111,13 @@ fn process_coredump_event(pid: i32) { 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 { @@ -186,7 +186,7 @@ mod tests { 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() { @@ -198,14 +198,14 @@ mod tests { 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(); + coredump_monitor_loop(); }); @@ -281,7 +281,7 @@ mod tests { 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() { @@ -301,7 +301,7 @@ mod tests { 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); diff --git a/bin/daemon.rs b/bin/daemon.rs index 57c1244..98d56d9 100644 --- a/bin/daemon.rs +++ b/bin/daemon.rs @@ -17,7 +17,7 @@ use crate::kmod_util::test_kmod; use crate::config::RtoConfig; use crate::config::read_config; use crate::aot::gen_app_rto; -//use crate::aot::set_app_aot_flag; +use crate::aot::set_app_link_flag; use crate::aot::parse_elf_file; use crate::aot::find_libs; use crate::bolt::bolt_optimize; @@ -95,11 +95,11 @@ fn sysboost_core_process(conf: &RtoConfig) -> i32 { return ret; } - // let ret = set_app_aot_flag(&conf.elf_path, true); - // if ret != 0 { - // log::error!("Error: set app aot flag fault."); - // return ret; - // } + let ret = set_app_link_flag(&conf.elf_path, true); + if ret != 0 { + log::error!("Error: set app link flag fail."); + return ret; + } return ret; } @@ -213,7 +213,7 @@ fn clean_last_rto() { } let file_name = path.file_name().unwrap(); let p = format!("{}{}", SYSBOOST_DB_PATH, file_name.to_string_lossy()); - //set_app_aot_flag(&p, false); + set_app_link_flag(&p, false); db_remove_link(&p); } let ret = fs::remove_file("/usr/bin/bash.rto"); diff --git a/bin/lib/fs_ext.rs b/bin/lib/fs_ext.rs index f1b36b5..787c420 100644 --- a/bin/lib/fs_ext.rs +++ b/bin/lib/fs_ext.rs @@ -13,8 +13,8 @@ use log::{self}; use std::fs; use std::path::PathBuf; -pub fn move_file(new_path: &String, old_path: &String) -> i32 { - match fs::rename(&new_path, &old_path) { +pub fn move_file(from: &String, to: &String) -> i32 { + match fs::rename(&from, &to) { Ok(_) => {} Err(e) => { log::error!("move file failed: {}", e); -- Gitee