add command x and e

This commit is contained in:
primardj 2024-04-18 00:26:52 +01:00
parent 32acba1e7a
commit b73a0abee2
5 changed files with 77 additions and 15 deletions

View file

@ -55,6 +55,9 @@ pub fn sent_signal( svwait_var: i32, verbose: i8, command: &str, mut sv: status_
"k" => { "k" => {
return misc::kill_verbose(sv.sent_signal(b"k"), verbose, svwait_var, sv); 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 => { other => {
println!("Error, command {} not implemented.", other); // TODO : Put the real error println!("Error, command {} not implemented.", other); // TODO : Put the real error
// message. // message.

View file

@ -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 { pub fn noexcept_verbose(sent_signal: bool, verbose: i8, mut sv: status_obj::StatusObj) -> i32 {
if sent_signal && verbose == 1 { if sent_signal && verbose == 1 {
sleep(Duration::from_secs_f32(0.0005)); sleep(Duration::from_secs_f32(0.0005));

View file

@ -95,7 +95,7 @@ pub fn parse_args_1(arg: &String) -> String{
pub fn parse_args_2(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 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() { for var in POSSIBLE.iter() {
if arg == var { if arg == var {

View file

@ -39,14 +39,30 @@ impl StatusObj {
} }
// Check if supervise/control and supervise/ok exists, and if runsv running. // Check if supervise/control and supervise/ok exists, and if runsv running.
let control_exists = sent_signal::check_fifo(sv.clone(), "supervise/ok"); let control_exists = sent_signal::check_fifo("supervise/ok");
if control_exists == false { match control_exists {
1 => {
println!("fail: {sv}: runsv not running");
return None; return None;
} },
let control_exists = sent_signal::check_fifo(sv.clone(), "supervise/control"); 2 => {
if control_exists == false { 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; 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"); let status_option = fs::OpenOptions::new().read(true).open("supervise/status");
@ -169,6 +185,10 @@ impl StatusObj {
pub fn get_status_string(&mut self) -> String { pub fn get_status_string(&mut self) -> String {
// return the status for this service in particular. // return the status for this service in particular.
if self.is_exited() {
return self.svname.clone() + ": runsv not running";
}
let status_sv; let status_sv;
let pid_string: String; let pid_string: String;
let down_string; let down_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 { pub fn get_name(&mut self) -> String {
return self.svname.clone(); return self.svname.clone();
} }

View file

@ -4,7 +4,7 @@ use tokio::runtime::Runtime;
use std::io::ErrorKind; 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. // Check if fifo is available. If yes, return true.
// If no, return false. // If no, return false.
let rt = Runtime::new().unwrap(); 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); let sender = pipe::OpenOptions::new().open_sender(path);
match sender { match sender {
Ok(..) => return true, Ok(..) => return 0,
Err(e) if e.raw_os_error() == Some(libc::ENXIO) => { Err(e) if e.raw_os_error() == Some(libc::ENXIO) => {
println!("fail: {sv}: runsv not running"); //println!("fail: {sv}: runsv not running");
return false; return 1;
}, },
Err(..) => { Err(..) => {
println!("warning: {sv}: unable to open {path}: file does not exist"); //println!("warning: {sv}: unable to open {path}: file does not exist");
return false; return 2;
}, },
}; };
} }