xref: /DragonReach/src/main.rs (revision 17ae4661898f9f5bdb95f865ac453f4508b9e876)
1e5a8055aSGnoCiYeH // #![no_std]
2e5a8055aSGnoCiYeH // #![no_main]
3e5a8055aSGnoCiYeH #![feature(slice_pattern)]
4e5a8055aSGnoCiYeH 
5f206f17aSGnoCiYeH use cfg_if::cfg_if;
621fc724cSGnoCiYeH 
7f206f17aSGnoCiYeH cfg_if! {
8f206f17aSGnoCiYeH     if #[cfg(target_os = "dragonos")]{
9f206f17aSGnoCiYeH         extern crate drstd;
10f206f17aSGnoCiYeH         use drstd as std;
11f206f17aSGnoCiYeH     }
12f206f17aSGnoCiYeH }
13f206f17aSGnoCiYeH 
14f206f17aSGnoCiYeH extern crate hashbrown;
154e851715SGnoCiYeH 
1621fc724cSGnoCiYeH mod contants;
174e851715SGnoCiYeH mod error;
18*17ae4661SGnoCiYeH mod executor;
19*17ae4661SGnoCiYeH mod manager;
204e851715SGnoCiYeH mod parse;
214e851715SGnoCiYeH mod task;
2221fc724cSGnoCiYeH mod unit;
2321fc724cSGnoCiYeH 
244e851715SGnoCiYeH use crate::unit::service;
25*17ae4661SGnoCiYeH use std::eprint;
26*17ae4661SGnoCiYeH use std::eprintln;
27*17ae4661SGnoCiYeH use std::fs;
28*17ae4661SGnoCiYeH use std::print;
29*17ae4661SGnoCiYeH use std::println;
30*17ae4661SGnoCiYeH use std::string::{String, ToString};
31*17ae4661SGnoCiYeH use std::vec::Vec;
32*17ae4661SGnoCiYeH use unit::service::ServiceUnit;
3321fc724cSGnoCiYeH 
344e851715SGnoCiYeH use self::unit::Unit;
35*17ae4661SGnoCiYeH use error::ErrorFormat;
364e851715SGnoCiYeH 
374e851715SGnoCiYeH pub struct FileDescriptor(usize);
384e851715SGnoCiYeH 
39*17ae4661SGnoCiYeH const DRAGON_REACH_UNIT_DIR: &'static str = "/etc/reach/system/";
40*17ae4661SGnoCiYeH 
41f206f17aSGnoCiYeH #[cfg(target_os = "dragonos")]
42f206f17aSGnoCiYeH #[no_mangle]
434e851715SGnoCiYeH fn main() {
44*17ae4661SGnoCiYeH     // use parse::UnitParser;
45f206f17aSGnoCiYeH 
46*17ae4661SGnoCiYeH     // use crate::{
47*17ae4661SGnoCiYeH     //     executor::Executor,
48*17ae4661SGnoCiYeH     //     manager::{Manager, UnitManager},
49*17ae4661SGnoCiYeH     //     parse::parse_util::UnitParseUtil,
50*17ae4661SGnoCiYeH     // };
51*17ae4661SGnoCiYeH     // let id = ServiceUnit::from_path("/etc/reach/system/shell.service").unwrap();
52*17ae4661SGnoCiYeH 
53*17ae4661SGnoCiYeH     // if id != 0 {
54*17ae4661SGnoCiYeH     //     let unit = UnitManager::get_unit_with_id(&id).unwrap();
55*17ae4661SGnoCiYeH     //     if let Err(e) = Executor::exec(&unit) {
56*17ae4661SGnoCiYeH     //         eprintln!("Err:{}", e.error_format());
57*17ae4661SGnoCiYeH     //     }
58*17ae4661SGnoCiYeH     // }
59*17ae4661SGnoCiYeH 
60*17ae4661SGnoCiYeH     // loop{
61*17ae4661SGnoCiYeH 
62*17ae4661SGnoCiYeH     // }
63*17ae4661SGnoCiYeH 
64*17ae4661SGnoCiYeH     //================================
65*17ae4661SGnoCiYeH     // use std::process::Command;
66*17ae4661SGnoCiYeH     // use std::process::Stdio;
67*17ae4661SGnoCiYeH     // let proc = unsafe {
68*17ae4661SGnoCiYeH     //     Command::new("/bin/shell.elf")
69*17ae4661SGnoCiYeH     //         .stderr(Stdio::inherit())
70*17ae4661SGnoCiYeH     //         .stdout(Stdio::inherit())
71*17ae4661SGnoCiYeH     //         .stdin(Stdio::inherit())
72*17ae4661SGnoCiYeH     //         .spawn()
73*17ae4661SGnoCiYeH     // };
74*17ae4661SGnoCiYeH 
75*17ae4661SGnoCiYeH     // match proc {
76*17ae4661SGnoCiYeH     //     Ok(p) => {
77*17ae4661SGnoCiYeH     //         println!("Service running...");
78*17ae4661SGnoCiYeH     //     }
79*17ae4661SGnoCiYeH     //     Err(err) => {
80*17ae4661SGnoCiYeH     //         eprintln!(": Service startup failed: {}",err);
81*17ae4661SGnoCiYeH     //     }
82*17ae4661SGnoCiYeH     // }
83*17ae4661SGnoCiYeH 
84*17ae4661SGnoCiYeH     // loop {
85*17ae4661SGnoCiYeH 
86*17ae4661SGnoCiYeH     // }
87*17ae4661SGnoCiYeH 
88*17ae4661SGnoCiYeH     //========================================
89*17ae4661SGnoCiYeH 
90*17ae4661SGnoCiYeH     use parse::UnitParser;
91*17ae4661SGnoCiYeH 
92*17ae4661SGnoCiYeH     use crate::{
93*17ae4661SGnoCiYeH         executor::Executor,
94*17ae4661SGnoCiYeH         manager::{Manager, UnitManager},
95*17ae4661SGnoCiYeH         parse::parse_util::UnitParseUtil,
96*17ae4661SGnoCiYeH     };
97*17ae4661SGnoCiYeH 
98*17ae4661SGnoCiYeH     let mut units_file_name = Vec::new();
99*17ae4661SGnoCiYeH     //读取目录里面的unit文件
100*17ae4661SGnoCiYeH     if let Ok(entries) = fs::read_dir(DRAGON_REACH_UNIT_DIR) {
101*17ae4661SGnoCiYeH         for entry in entries {
102*17ae4661SGnoCiYeH             if let Ok(entry) = entry {
103*17ae4661SGnoCiYeH                 let filename = entry.file_name();
104*17ae4661SGnoCiYeH                 let filename = filename.to_str().unwrap();
105*17ae4661SGnoCiYeH                 units_file_name.push(filename.to_string());
106*17ae4661SGnoCiYeH             }
107*17ae4661SGnoCiYeH         }
108*17ae4661SGnoCiYeH     }
109*17ae4661SGnoCiYeH 
110*17ae4661SGnoCiYeH     units_file_name.push(String::from("shell.service"));
111*17ae4661SGnoCiYeH 
112*17ae4661SGnoCiYeH     println!("files: {:?}", units_file_name);
113*17ae4661SGnoCiYeH 
114*17ae4661SGnoCiYeH     //启动服务
115*17ae4661SGnoCiYeH     for path in units_file_name {
116*17ae4661SGnoCiYeH         let id = match UnitParser::from_path(&path) {
117*17ae4661SGnoCiYeH             Ok(id) => id,
118f206f17aSGnoCiYeH             Err(e) => {
119*17ae4661SGnoCiYeH                 eprintln!("Err:{}", e.error_format());
120*17ae4661SGnoCiYeH                 0
1214e851715SGnoCiYeH             }
122f206f17aSGnoCiYeH         };
123*17ae4661SGnoCiYeH 
124*17ae4661SGnoCiYeH         if id != 0 {
125*17ae4661SGnoCiYeH             let unit = UnitManager::get_unit_with_id(&id).unwrap();
126*17ae4661SGnoCiYeH             if let Err(e) = Executor::exec(&unit) {
127*17ae4661SGnoCiYeH                 eprintln!("Err:{}", e.error_format());
128*17ae4661SGnoCiYeH             } else {
129*17ae4661SGnoCiYeH                 println!("Service {} startup success...", id);
130*17ae4661SGnoCiYeH             }
131*17ae4661SGnoCiYeH         }
132*17ae4661SGnoCiYeH     }
133*17ae4661SGnoCiYeH 
134*17ae4661SGnoCiYeH     // 启动完服务后进入主循环
135*17ae4661SGnoCiYeH     loop {
136*17ae4661SGnoCiYeH         // 检查各服务运行状态
137*17ae4661SGnoCiYeH         //eprintln!(".");
138*17ae4661SGnoCiYeH         Manager::check_running_status();
139*17ae4661SGnoCiYeH     }
140f206f17aSGnoCiYeH }
141f206f17aSGnoCiYeH 
142f206f17aSGnoCiYeH #[cfg(not(target_os = "dragonos"))]
143f206f17aSGnoCiYeH fn main() {
144*17ae4661SGnoCiYeH     use parse::UnitParser;
145e5a8055aSGnoCiYeH 
146*17ae4661SGnoCiYeH     use crate::{
147*17ae4661SGnoCiYeH         executor::Executor,
148*17ae4661SGnoCiYeH         manager::{Manager, UnitManager},
149*17ae4661SGnoCiYeH         parse::parse_util::UnitParseUtil,
150*17ae4661SGnoCiYeH     };
151f206f17aSGnoCiYeH 
152*17ae4661SGnoCiYeH     let mut units_file_name = Vec::new();
153*17ae4661SGnoCiYeH     // //读取目录里面的unit文件
154*17ae4661SGnoCiYeH     // if let Ok(entries) = fs::read_dir(DRAGON_REACH_UNIT_DIR) {
155*17ae4661SGnoCiYeH     //     for entry in entries {
156*17ae4661SGnoCiYeH     //         if let Ok(entry) = entry {
157*17ae4661SGnoCiYeH     //             if let Ok(file_type) = entry.file_type() {
158*17ae4661SGnoCiYeH     //                 if file_type.is_file() {
159*17ae4661SGnoCiYeH     //                     let filename = entry.file_name();
160*17ae4661SGnoCiYeH     //                     let filename = filename.to_str().unwrap();
161*17ae4661SGnoCiYeH     //                     units_file_name.push(filename.to_string());
162*17ae4661SGnoCiYeH     //                 }
163*17ae4661SGnoCiYeH     //             }
164*17ae4661SGnoCiYeH     //         }
165*17ae4661SGnoCiYeH     //     }
166*17ae4661SGnoCiYeH     // }
167e5a8055aSGnoCiYeH 
168*17ae4661SGnoCiYeH     units_file_name.push("/home/heyicong/DragonReach/parse_test/test.service");
169*17ae4661SGnoCiYeH 
170*17ae4661SGnoCiYeH     //启动服务
171*17ae4661SGnoCiYeH     for path in units_file_name {
172*17ae4661SGnoCiYeH         let id = match UnitParser::from_path(&path) {
173*17ae4661SGnoCiYeH             Ok(id) => id,
174f206f17aSGnoCiYeH             Err(e) => {
175*17ae4661SGnoCiYeH                 eprintln!("Err:{}", e.error_format());
176*17ae4661SGnoCiYeH                 0
177f206f17aSGnoCiYeH             }
178f206f17aSGnoCiYeH         };
179f206f17aSGnoCiYeH 
180*17ae4661SGnoCiYeH         if id != 0 {
181*17ae4661SGnoCiYeH             let unit = UnitManager::get_unit_with_id(&id).unwrap();
182e5a8055aSGnoCiYeH             if let Err(e) = Executor::exec(&unit) {
183*17ae4661SGnoCiYeH                 eprintln!("Err:{}", e.error_format());
184*17ae4661SGnoCiYeH             }
185*17ae4661SGnoCiYeH         }
186*17ae4661SGnoCiYeH     }
187*17ae4661SGnoCiYeH 
188*17ae4661SGnoCiYeH     // 启动完服务后进入主循环
189*17ae4661SGnoCiYeH     loop {
190*17ae4661SGnoCiYeH         // 检查各服务运行状态
191*17ae4661SGnoCiYeH         Manager::check_running_status();
192*17ae4661SGnoCiYeH         //println!(".");
193e5a8055aSGnoCiYeH     }
19421fc724cSGnoCiYeH }
195