1 #![allow(non_snake_case)] 2 3 mod contants; 4 mod error; 5 mod executor; 6 mod manager; 7 mod parse; 8 mod systemctl; 9 mod task; 10 mod time; 11 mod unit; 12 13 use error::ErrorFormat; 14 use executor::Executor; 15 use manager::{timer_manager::TimerManager, Manager}; 16 use parse::UnitParser; 17 use systemctl::listener::Systemctl; 18 19 pub struct FileDescriptor(usize); 20 21 const DRAGON_REACH_UNIT_DIR: &'static str = "/etc/reach/system/"; 22 23 fn main() { 24 // 初始化 25 Systemctl::init(); 26 27 let mut units_file_name = Vec::new(); 28 //读取目录里面的unit文件 29 if let Ok(entries) = std::fs::read_dir(DRAGON_REACH_UNIT_DIR) { 30 for entry in entries { 31 if let Ok(entry) = entry { 32 if let Ok(file_type) = entry.file_type() { 33 if file_type.is_file() { 34 let filename = entry.file_name().to_str().unwrap().to_string(); 35 units_file_name.push(filename); 36 } 37 } 38 } 39 } 40 } 41 42 //启动服务 43 for path in units_file_name { 44 let id = match UnitParser::from_path(&path) { 45 Ok(id) => id, 46 Err(e) => { 47 eprintln!("Err:{}", e.error_format()); 48 0 49 } 50 }; 51 52 if id != 0 { 53 if let Err(e) = Executor::exec(id) { 54 eprintln!("Err:{}", e.error_format()); 55 } 56 } 57 } 58 59 // 启动完服务后进入主循环 60 loop { 61 // 检查各服务运行状态 62 Manager::check_running_status(); 63 // 检查cmd进程状态 64 Manager::check_cmd_proc(); 65 // 检查计时器任务 66 TimerManager::check_timer(); 67 // 监听systemctl 68 Systemctl::ctl_listen(); 69 } 70 } 71