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