1 #![no_std] 2 #![no_main] 3 #![feature(slice_pattern)] 4 #![feature(fn_traits)] 5 6 #[cfg(target_os = "dragonos")] 7 extern crate drstd as std; 8 9 extern crate hashbrown; 10 11 mod contants; 12 mod error; 13 mod executor; 14 mod manager; 15 mod parse; 16 mod task; 17 mod time; 18 mod unit; 19 20 use std::fs; 21 use std::{eprint, eprintln, print, println}; 22 23 use std::string::ToString; 24 use std::vec::Vec; 25 26 use error::ErrorFormat; 27 28 pub struct FileDescriptor(usize); 29 30 const DRAGON_REACH_UNIT_DIR: &'static str = "/etc/reach/system/"; 31 32 #[cfg(target_os = "dragonos")] 33 #[no_mangle] 34 fn main() { 35 use manager::timer_manager::TimerManager; 36 use parse::UnitParser; 37 38 use crate::{executor::Executor, manager::Manager}; 39 40 let mut units_file_name = Vec::new(); 41 //读取目录里面的unit文件 42 if let Ok(entries) = fs::read_dir(DRAGON_REACH_UNIT_DIR) { 43 for entry in entries { 44 if let Ok(entry) = entry { 45 if let Ok(file_type) = entry.file_type() { 46 if file_type.is_file() { 47 let filename = entry.file_name(); 48 let filename = filename.to_str().unwrap(); 49 units_file_name.push(filename.to_string()); 50 } 51 } 52 } 53 } 54 } 55 56 //启动服务 57 for path in units_file_name { 58 let id = match UnitParser::from_path(&path) { 59 Ok(id) => id, 60 Err(e) => { 61 eprintln!("Err:{}", e.error_format()); 62 0 63 } 64 }; 65 66 if id != 0 { 67 if let Err(e) = Executor::exec(id) { 68 eprintln!("Err:{}", e.error_format()); 69 } else { 70 println!("Service {} startup success...", id); 71 } 72 } 73 } 74 75 // 启动完服务后进入主循环 76 loop { 77 // 检查各服务运行状态 78 Manager::check_running_status(); 79 // 检查cmd进程状态 80 Manager::check_cmd_proc(); 81 // 检查计时器任务 82 TimerManager::check_timer(); 83 } 84 } 85 86 #[cfg(not(target_os = "dragonos"))] 87 fn main() { 88 use parse::UnitParser; 89 90 use crate::{ 91 executor::Executor, 92 manager::{timer_manager::TimerManager, Manager, UnitManager}, 93 }; 94 95 let mut units_file_name = Vec::new(); 96 //读取目录里面的unit文件 97 if let Ok(entries) = fs::read_dir("/bin") { 98 for entry in entries { 99 if let Ok(entry) = entry { 100 if let Ok(file_type) = entry.file_type() { 101 if file_type.is_file() { 102 let filename = entry.file_name(); 103 let _filename = filename.to_str().unwrap(); 104 //units_file_name.push(filename.to_string()); 105 } 106 } 107 } 108 } 109 } 110 111 units_file_name.push("/home/heyicong/DragonReach/parse_test/test.service".to_string()); 112 113 // 完善unit之间依赖关系 114 UnitManager::init_units_dependencies(); 115 116 //启动服务 117 for path in units_file_name { 118 let id = match UnitParser::from_path(&path) { 119 Ok(id) => id, 120 Err(e) => { 121 eprintln!("Err:{}", e.error_format()); 122 0 123 } 124 }; 125 126 if id != 0 { 127 let _unit = UnitManager::get_unit_with_id(&id).unwrap(); 128 if let Err(e) = Executor::exec(id) { 129 eprintln!("Err:{}", e.error_format()); 130 } 131 } 132 } 133 134 // 启动完服务后进入主循环 135 loop { 136 // 检查各服务运行状态 137 Manager::check_running_status(); 138 139 Manager::check_cmd_proc(); 140 141 TimerManager::check_timer(); 142 } 143 } 144