From 202efc57018e38686e13a809215cd46f7f72b0a5 Mon Sep 17 00:00:00 2001 From: l00580197 Date: Fri, 14 Jul 2023 16:35:57 +0800 Subject: [PATCH] add coredump monitor for sysboost --- Cargo.toml | 7 ++--- bin/coredump_monitor.rs | 57 ++++++++++++++++++++++++++++++++++++----- bin/main.rs | 7 +++++ 3 files changed, 62 insertions(+), 9 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f4fe50e..097c7e8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,7 +6,6 @@ default-run = "sysboostd" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - [[bin]] name ="sysboostd" path = "bin/main.rs" @@ -14,6 +13,9 @@ path = "bin/main.rs" [target.'cfg(not(windows))'.dependencies] basic = { path = "libs/basic" } daemonize = "0.x" +lazy_static = "1.4.0" +procfs = "0.6.0" +cnproc = "0.2.1" serde = { version = "1.0.143", features = ["serde_derive"] } toml = "0.5.9" @@ -23,5 +25,4 @@ goblin = "0.7" [dev-dependencies.tempfile] version = "3.2.0" -cnproc = "0.2.1" -lazy_static = "1.4.0" + diff --git a/bin/coredump_monitor.rs b/bin/coredump_monitor.rs index 3346300..3b4b713 100644 --- a/bin/coredump_monitor.rs +++ b/bin/coredump_monitor.rs @@ -2,26 +2,70 @@ use lazy_static::lazy_static; use std::collections::HashMap; use cnproc::PidMonitor; use cnproc::PidEvent; +use std::sync::Mutex; +use std::fs; +use procfs::Process; lazy_static! { - static ref pid_maps: HashMap = HashMap::new(); + static ref MERGE_FILES: Mutex> = Mutex::new(Vec::new()); + static ref PID_INFOS: Mutex> = Mutex::new(HashMap::new()); +} + +// add merged file into vector, provide for testcase +fn add_merge_file(file_path: String) { + MERGE_FILES.lock().unwrap().push(file_path); +} + +fn init_merge_file_list() { + MERGE_FILES.lock().unwrap().push(String::from("/usr/bin/bash.rto")); } fn process_exec_event(pid: i32) { - // pid_maps.insert(pid, pname); + // 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 process_coredump_event(pid: i32) { - println!("lyt coredump pid: {}", 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) { + fs::remove_file(&file_path).expect("File delete failed!"); + } + } + + PID_INFOS.lock().unwrap().remove(&pid); } fn process_exit_event(pid: i32) { - // pid_maps.remove(pid) + PID_INFOS.lock().unwrap().remove(&pid); } -fn main_loop() { +pub fn coredump_monitor_loop() { + init_merge_file_list(); let mut monitor = PidMonitor::new().unwrap(); - loop { match monitor.recv() { None => {} @@ -36,3 +80,4 @@ fn main_loop() { } } } + diff --git a/bin/main.rs b/bin/main.rs index 9722ba1..2c0024f 100644 --- a/bin/main.rs +++ b/bin/main.rs @@ -10,13 +10,16 @@ // Create: 2023-4-20 mod daemon; +mod coredump_monitor; use crate::daemon::daemon_loop; +use crate::coredump_monitor::coredump_monitor_loop; use basic::logger::{self}; use daemonize::Daemonize; use log::{self}; use std::env; +use std::thread; const APP_NAME: &str = "sysboostd"; @@ -53,6 +56,10 @@ fn main() { } } } + // start up coredump monitor + let coredump_monitor_handle = thread::spawn(||{ + coredump_monitor_loop(); + }); // daemon service gen rto ELF with config daemon_loop(); -- Gitee