From b73a0abee240413fe682d5c256f2d32cf3fd6008 Mon Sep 17 00:00:00 2001 From: primardj Date: Thu, 18 Apr 2024 00:26:52 +0100 Subject: [PATCH] add command x and e --- .../executor/execute_service/sent_signal.rs | 3 ++ .../execute_service/sent_signal/misc.rs | 30 +++++++++++++ 01_phase_rust_src/sv-rustit/src/parser.rs | 2 +- 01_phase_rust_src/sv-rustit/src/status_obj.rs | 45 +++++++++++++++---- .../sv-rustit/src/status_obj/sent_signal.rs | 12 ++--- 5 files changed, 77 insertions(+), 15 deletions(-) diff --git a/01_phase_rust_src/sv-rustit/src/executor/execute_service/sent_signal.rs b/01_phase_rust_src/sv-rustit/src/executor/execute_service/sent_signal.rs index f95e9a9..4004b85 100644 --- a/01_phase_rust_src/sv-rustit/src/executor/execute_service/sent_signal.rs +++ b/01_phase_rust_src/sv-rustit/src/executor/execute_service/sent_signal.rs @@ -55,6 +55,9 @@ pub fn sent_signal( svwait_var: i32, verbose: i8, command: &str, mut sv: status_ "k" => { return misc::kill_verbose(sv.sent_signal(b"k"), verbose, svwait_var, sv); }, + "e" | "x" => { + return misc::exit_verbose(sv.sent_signal(b"x"), verbose, svwait_var, sv); + }, other => { println!("Error, command {} not implemented.", other); // TODO : Put the real error // message. diff --git a/01_phase_rust_src/sv-rustit/src/executor/execute_service/sent_signal/misc.rs b/01_phase_rust_src/sv-rustit/src/executor/execute_service/sent_signal/misc.rs index 98a7222..f93636f 100644 --- a/01_phase_rust_src/sv-rustit/src/executor/execute_service/sent_signal/misc.rs +++ b/01_phase_rust_src/sv-rustit/src/executor/execute_service/sent_signal/misc.rs @@ -148,6 +148,36 @@ pub fn kill_verbose(sent_signal: bool, verbose: i8, svwait_var: i32, mut sv: sta } } +pub fn exit_verbose(sent_signal: bool, verbose: i8, svwait_var: i32, mut sv: status_obj::StatusObj) -> i32 { + // if the excepted is true, wait for the service to get term signal. + // if the excepted is false, wait for the service to interpret the term signal. + if sent_signal && verbose == 1 { + sleep(Duration::from_secs_f32(0.0005)); + let mut time_wait = 0; + loop { + let status = sv.update_status(); + if status == 1 { + return 1; + } + if sv.is_exited() == true { + println!("ok: {}", sv.get_status_string()); + return 0 + } + if time_wait >= svwait_var*2 { + println!("timeout: {}", sv.get_status_string()); + return 1 + } + time_wait += 1; + sleep(Duration::from_secs_f32(0.5)); + } + } else if sent_signal && verbose == 0 { + return 0 + } else { + // Case sent_signal != true. + return 1 + } +} + pub fn noexcept_verbose(sent_signal: bool, verbose: i8, mut sv: status_obj::StatusObj) -> i32 { if sent_signal && verbose == 1 { sleep(Duration::from_secs_f32(0.0005)); diff --git a/01_phase_rust_src/sv-rustit/src/parser.rs b/01_phase_rust_src/sv-rustit/src/parser.rs index cb09545..b2cf51b 100644 --- a/01_phase_rust_src/sv-rustit/src/parser.rs +++ b/01_phase_rust_src/sv-rustit/src/parser.rs @@ -95,7 +95,7 @@ pub fn parse_args_1(arg: &String) -> String{ pub fn parse_args_2(arg: &String) -> String{ const POSSIBLE: &'static [&'static str] = &["start", "stop", "reload", "restart", "shutdown", "force-stop", "force-reload", "force-restart", "force-shutdown", "try-restart"]; - const CPOSSIBLE: &'static [char] = &['s', 'u', 'd', 'o', 'p', 'c', 'h', 'a', 'i', 'q', '1', '2', 't', 'k']; + const CPOSSIBLE: &'static [char] = &['s', 'u', 'd', 'o', 'p', 'c', 'h', 'a', 'i', 'q', '1', '2', 't', 'k', 'e', 'x']; for var in POSSIBLE.iter() { if arg == var { diff --git a/01_phase_rust_src/sv-rustit/src/status_obj.rs b/01_phase_rust_src/sv-rustit/src/status_obj.rs index 2d7328f..a37fa86 100644 --- a/01_phase_rust_src/sv-rustit/src/status_obj.rs +++ b/01_phase_rust_src/sv-rustit/src/status_obj.rs @@ -39,14 +39,30 @@ impl StatusObj { } // Check if supervise/control and supervise/ok exists, and if runsv running. - let control_exists = sent_signal::check_fifo(sv.clone(), "supervise/ok"); - if control_exists == false { - return None; - } - let control_exists = sent_signal::check_fifo(sv.clone(), "supervise/control"); - if control_exists == false { - return None; - } + let control_exists = sent_signal::check_fifo("supervise/ok"); + match control_exists { + 1 => { + println!("fail: {sv}: runsv not running"); + return None; + }, + 2 => { + println!("warning: {sv}: unable to open {path_string}: file does not exist"); + return None + }, + _other => {}, + }; + let control_exists = sent_signal::check_fifo("supervise/control"); + match control_exists { + 1 => { + println!("fail: {sv}: runsv not running"); + return None; + }, + 2 => { + println!("warning: {sv}: unable to open {path_string}: file does not exist"); + return None + }, + _other => {}, + }; let status_option = fs::OpenOptions::new().read(true).open("supervise/status"); @@ -168,6 +184,10 @@ impl StatusObj { pub fn get_status_string(&mut self) -> String { // return the status for this service in particular. + + if self.is_exited() { + return self.svname.clone() + ": runsv not running"; + } let status_sv; let pid_string: String; @@ -258,6 +278,15 @@ impl StatusObj { } } + pub fn is_exited(&mut self) -> bool { + let control_exists = sent_signal::check_fifo("supervise/ok"); + if control_exists == 1 || control_exists == 2 { + return true; + } + return false; + + } + pub fn get_name(&mut self) -> String { return self.svname.clone(); } diff --git a/01_phase_rust_src/sv-rustit/src/status_obj/sent_signal.rs b/01_phase_rust_src/sv-rustit/src/status_obj/sent_signal.rs index 9e75d80..76e42fb 100644 --- a/01_phase_rust_src/sv-rustit/src/status_obj/sent_signal.rs +++ b/01_phase_rust_src/sv-rustit/src/status_obj/sent_signal.rs @@ -4,7 +4,7 @@ use tokio::runtime::Runtime; use std::io::ErrorKind; -pub fn check_fifo(sv: String, path: &str) -> bool { +pub fn check_fifo( path: &str) -> u32 { // Check if fifo is available. If yes, return true. // If no, return false. let rt = Runtime::new().unwrap(); @@ -12,14 +12,14 @@ pub fn check_fifo(sv: String, path: &str) -> bool { let sender = pipe::OpenOptions::new().open_sender(path); match sender { - Ok(..) => return true, + Ok(..) => return 0, Err(e) if e.raw_os_error() == Some(libc::ENXIO) => { - println!("fail: {sv}: runsv not running"); - return false; + //println!("fail: {sv}: runsv not running"); + return 1; }, Err(..) => { - println!("warning: {sv}: unable to open {path}: file does not exist"); - return false; + //println!("warning: {sv}: unable to open {path}: file does not exist"); + return 2; }, }; }