123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- mod mgrsh;
- mod prepare;
- use std::{thread, time};
- use easy_repl::{command, Repl, CommandStatus};
- use easy_repl::repl::BuilderError;
- use lazy_static::lazy_static;
- use nonblock::NonBlockingReader;
- use crate::{cfg, log};
- use crate::repl::mgrsh::mgr_shell;
- use crate::power;
- use std::sync::atomic::{AtomicBool, Ordering};
- use crate::swroot::switch_root;
- lazy_static!{
- static ref DO_SWITCH_ROOT_AFTER_QUIT: AtomicBool = AtomicBool::new(false);
- }
- pub fn build_repl_shell<'a>() -> Result<Repl<'a>, BuilderError> {
- Repl::builder()
- .description("rumia-cli operator view")
- .prompt("< rumia-cli > ")
- .add("print", command! {
- "print what input",
- (msg: String) => |msg:String| {
- println!("{}", msg.as_str());
- Ok(CommandStatus::Done)
- }
- })
- .add("reboot", command! {
- "reboot system",
- () => || {
- power::safe_reboot();
- }
- })
- .add("shutdown", command! {
- "shutdown system",
- () => || {
- power::safe_shutdown();
- }
- })
- .add("debug-on", command! {
- "turn on debug printing for rumia",
- () => || {
- log::set_debug_mode(true);
- Ok(CommandStatus::Done)
- }
- })
- .add("debug-off", command! {
- "turn off debug printing for rumia",
- () => || {
- log::set_debug_mode(false);
- Ok(CommandStatus::Done)
- }
- })
- .add("sys", command! {
- "login to system view",
- (password: String) => |password:String| {
- let bhash = cfg::CFG.get().get_rumia_con_cfg().get_password_bcrypt();
- let res = bcrypt::verify(password, &bhash);
- match res {
- Ok(b) => {
- if b {
- mgr_shell();
- }else{
- println!("incorrect password.")
- }
- }
- Err(err) => {
- println!("failed verify password: {:?}", err)
- }
- }
- if DO_SWITCH_ROOT_AFTER_QUIT.load(Ordering::Relaxed) {
- Ok(CommandStatus::Quit)
- }else{
- Ok(CommandStatus::Done)
- }
- }
- })
- .build()
- }
- pub fn fatal_to_shell() {
- DO_SWITCH_ROOT_AFTER_QUIT.store(false, Ordering::Relaxed);
- let repl_bresult= build_repl_shell();
- let mut repl = match repl_bresult {
- Ok(t) => t,
- Err(err) => {
- panic!("rumia shell init panic: {:?}", err);
- }
- };
- println!("Pre-init process aborted by fatal error.");
- println!("Now entering into the rumia shell...");
- loop {
- log::set_in_repl(true);
- let repl_run_result = repl.run();
- log::set_in_repl(false);
- match repl_run_result {
- Ok(_) => {
- if DO_SWITCH_ROOT_AFTER_QUIT.load(Ordering::Relaxed) {
- switch_root();
- }else{
- println!("[WARN] You shouldn't quit this shell now,\
- because of pre-init process aborted by fatal error. \
- You may need to fix some problems or you can just use \
- reboot or shutdown command to exit this shell.");
- println!("shell will restart");
- }
- }
- Err(err) => {
- println!("[ERROR] shell crash by error: {:?}", err);
- println!("shell will restart");
- }
- };
- }
- }
- pub fn user_pause_to_shell() {
- DO_SWITCH_ROOT_AFTER_QUIT.store(false, Ordering::Relaxed);
- let repl_bresult= build_repl_shell();
- let mut repl = match repl_bresult {
- Ok(t) => t,
- Err(err) => {
- panic!("rumia shell init panic: {:?}", err);
- }
- };
- println!("Pre-init process paused by user press <Enter>.");
- println!("Now entering into the rumia shell...");
- log::set_in_repl(true);
- let repl_run_result = repl.run();
- log::set_in_repl(false);
- match repl_run_result {
- Ok(_) => {
- if DO_SWITCH_ROOT_AFTER_QUIT.load(Ordering::Relaxed) {
- switch_root();
- }else {
- println!("continue pre-init process...");
- }
- }
- Err(err) => {
- println!("[ERROR] shell crash by error: {:?}", err);
- println!("system will restart");
- thread::sleep(time::Duration::from_secs(2));
- power::safe_reboot();
- }
- };
- }
- pub fn check_user_pause() -> bool {
- let nb_reader_res = NonBlockingReader::from_fd(std::io::stdin());
- let mut nb_reader = match nb_reader_res {
- Ok(r) => {r}
- Err(err) => {
- panic!("failed make stdin unblocking: {:?}", err)
- }
- };
- let mut rstr = String::new();
- let read_res = nb_reader.read_available_to_string(&mut rstr);
- nb_reader.into_blocking().expect("failed turn stdin into blocking mode.");
- match read_res {
- Ok(len) => {
- if len > 0 {
- true
- }else{
- false
- }
- }
- Err(_) => {
- false
- }
- }
- }
- pub fn check_enter_pause_to_shell() {
- let r = check_user_pause();
- if r {
- user_pause_to_shell();
- }
- }
- pub fn schedule_switch_root() {
- DO_SWITCH_ROOT_AFTER_QUIT.store(true, Ordering::Relaxed);
- }
|