mod.rs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. mod mgrsh;
  2. use std::io;
  3. use easy_repl::{command, CommandStatus, Repl};
  4. use crate::{cfg, logger, syscall};
  5. use crate::reaper;
  6. pub fn shell_wait_enter() {
  7. let stdin = io::stdin();
  8. let mut sbuf = String::new();
  9. let _ = stdin.read_line(&mut sbuf);
  10. }
  11. pub fn shell_repl() {
  12. println!("---- yukari maintenance shell ----");
  13. let repl_bresult = Repl::builder()
  14. .description(" -- yukari maintenance shell --")
  15. .prompt("< Maintenance Shell > ")
  16. .add("print", command! {
  17. "print what input",
  18. (msg: String) => |msg:String| {
  19. println!("{}", msg.as_str());
  20. Ok(CommandStatus::Done)
  21. }
  22. })
  23. .add("reboot", command! {
  24. "reboot system",
  25. () => || {
  26. syscall::power::safe_reboot();
  27. }
  28. })
  29. .add("shutdown", command! {
  30. "shutdown system",
  31. () => || {
  32. syscall::power::safe_shutdown();
  33. }
  34. })
  35. .add("debug-on", command! {
  36. "turn on debug printing for yukari",
  37. () => || {
  38. logger::set_debug_mode(true);
  39. Ok(CommandStatus::Done)
  40. }
  41. })
  42. .add("debug-off", command! {
  43. "turn off debug printing for yukari",
  44. () => || {
  45. logger::set_debug_mode(false);
  46. Ok(CommandStatus::Done)
  47. }
  48. })
  49. .add("get-reaper-counter", command! {
  50. "print the zombie child reaper working counter",
  51. () => || {
  52. println!("{} zombie child reaped.", reaper::get_count());
  53. Ok(CommandStatus::Done)
  54. }
  55. })
  56. .add("login", command! {
  57. "login to admin shell",
  58. (password: String) => |password:String| {
  59. let bhash = cfg::CFG.blocking_get().get_etc_cfg().secure.bcrypt_password.clone();
  60. let res = bcrypt::verify(password, &bhash);
  61. match res {
  62. Ok(b) => {
  63. if b {
  64. mgrsh::mgr_shell();
  65. }else{
  66. println!("incorrect password.")
  67. }
  68. }
  69. Err(err) => {
  70. println!("failed verify password: {:?}", err)
  71. }
  72. }
  73. Ok(CommandStatus::Done)
  74. }
  75. })
  76. .build();
  77. let mut repl = match repl_bresult {
  78. Ok(t) => t,
  79. Err(err) => {
  80. println!("Maintenance Shell Init Fatal Error: {:?}", err);
  81. return;
  82. }
  83. };
  84. loop {
  85. let repl_run_result = repl.run();
  86. match repl_run_result {
  87. Ok(_) => {
  88. println!("[WARN] shouldn't quit this shell");
  89. println!("shell will restart");
  90. }
  91. Err(err) => {
  92. println!("[ERROR] shell crash by error: {:?}", err);
  93. println!("shell will restart");
  94. }
  95. };
  96. }
  97. }