1 use std::fs::{self, File}; 2 use std::io::Read; 3 use std::os::fd::FromRawFd; 4 use std::sync::{Arc, Mutex}; 5 6 use lazy_static::lazy_static; 7 8 use crate::error::ErrorFormat; 9 use crate::manager::ctl_manager::CtlManager; 10 11 use super::ctl_parser::{CommandOperation, CtlParser, Pattern}; 12 use super::{ctl_path, DRAGON_REACH_CTL_PIPE}; 13 14 lazy_static! { 15 static ref CTL_READER: Mutex<Arc<File>> = { 16 let file = Systemctl::init_listener(); 17 Mutex::new(Arc::new(file)) 18 }; 19 } 20 21 pub struct Command { 22 pub(crate) operation: CommandOperation, 23 pub(crate) args: Option<Vec<String>>, 24 pub(crate) patterns: Vec<Pattern>, 25 } 26 27 impl Command { 28 #[allow(dead_code)] 29 fn new(op: CommandOperation, patterns: Vec<Pattern>, args: Option<Vec<String>>) -> Self { 30 Command { 31 operation: op, 32 args, 33 patterns: patterns, 34 } 35 } 36 } 37 38 impl Default for Command { 39 fn default() -> Self { 40 Command { 41 operation: CommandOperation::None, 42 args: None, 43 patterns: Vec::new(), 44 } 45 } 46 } 47 48 pub struct Systemctl; 49 50 impl Systemctl { 51 pub fn init() { 52 Self::init_ctl_pipe(); 53 } 54 55 pub fn init_listener() -> File { 56 let fd = unsafe { libc::open(ctl_path().as_ptr(), libc::O_RDONLY | libc::O_NONBLOCK) }; 57 if fd < 0 { 58 panic!("open ctl pipe error"); 59 } 60 unsafe { File::from_raw_fd(fd) } 61 } 62 63 pub fn ctl_listen() { 64 let mut guard = CTL_READER.lock().unwrap(); 65 let mut s = String::new(); 66 if let Ok(size) = guard.read_to_string(&mut s) { 67 if size == 0 { 68 return; 69 } 70 match CtlParser::parse_ctl(&s) { 71 Ok(cmd) => { 72 let _ = CtlManager::exec_ctl(cmd); 73 } 74 Err(err) => { 75 eprintln!("parse tcl command error: {}", err.error_format()); 76 } 77 } 78 } 79 } 80 81 fn is_ctl_exists() -> bool { 82 if let Ok(metadata) = fs::metadata(DRAGON_REACH_CTL_PIPE) { 83 metadata.is_file() 84 } else { 85 false 86 } 87 } 88 89 fn init_ctl_pipe() { 90 if !Self::is_ctl_exists() { 91 let path = ctl_path(); 92 let ret = unsafe { libc::mkfifo(path.as_ptr(), 0o666) }; 93 if ret != 0 { 94 // 创建管道失败打日志 95 panic!("create ctl pipe failed"); 96 } 97 } 98 } 99 } 100