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