Merge pull request 'implem_command' (#9) from implem_command into main

Reviewed-on: #9
This commit is contained in:
statzitz 2024-04-18 01:30:48 +02:00
commit 6aff5b2c87
12 changed files with 482 additions and 206 deletions

View file

@ -1,41 +1,17 @@
pub mod sent_signal; pub mod execute_service;
use crate::status_obj;
pub fn execute(svdir_var: String, svwait_var: i32, verbose: i8, command: &str, services: Vec<String>) -> i32{ pub fn execute(svdir_var: String, svwait_var: i32, verbose: i8, command: &str, services: Vec<String>) -> i32{
// split a service vector into different sv.
let mut exit_code: i32 = 0; let mut exit_code: i32 = 0;
for sv in services { for sv in services {
let ch1 = sv.chars().next().unwrap(); exit_code += execute_service::execute_service(svdir_var.clone(), svwait_var, verbose, command, sv);
let ch2_row = &sv.chars().collect::<Vec<_>>()[..2]; if exit_code > 99 {
let ch2: String = ch2_row.into_iter().collect(); exit_code = 99;
let path;
if ch1 == '/' || ch2 == "~/"{
// case absolute path for the service
//
path = sv.clone();
} else {
// case relative path for the service.
//
path = svdir_var.clone() + "/" + &sv;
} }
let status_option = status_obj::StatusObj::new(path, sv);
match status_option {
Some(status) => {
exit_code += sent_signal::sent_signal(svwait_var, verbose, command, status);
},
None => {
if exit_code < 99 {
exit_code += 1;
}
},
};
} }
return exit_code return exit_code
} }

View file

@ -0,0 +1,22 @@
mod sent_signal;
mod make_path;
use crate::status_obj;
pub fn execute_service(svdir_var: String, svwait_var: i32, verbose: i8, command: &str, sv: String) -> i32 {
// make a sv object from a string, svdir_var, and sent it the command signal.
let path = make_path::make_path(svdir_var, sv.clone());
let status_option = status_obj::StatusObj::new(path, sv);
match status_option {
Some(status) => {
return sent_signal::sent_signal(svwait_var, verbose, command, status);
},
None => {
return 1;
},
};
}

View file

@ -0,0 +1,17 @@
pub fn make_path(svdir_var: String, sv: String) -> String {
let ch1 = sv.chars().next().unwrap();
let ch2_row = &sv.chars().collect::<Vec<_>>()[..2];
let ch2: String = ch2_row.into_iter().collect();
let path;
if ch1 == '/' || ch2 == "~/"{
// case absolute path for the service
path = sv;
} else {
// case relative path for the service.
path = svdir_var + "/" + &sv;
}
return path
}

View file

@ -0,0 +1,68 @@
mod misc;
use crate::status_obj;
pub fn sent_signal( svwait_var: i32, verbose: i8, command: &str, mut sv: status_obj::StatusObj) -> i32 {
// Return 0 in case everything worked fine, return 1 if timeout or error.
match command {
"s" => {
// case command is status
return misc::print_status(sv);
},
"u" => {
// case where command is up
return misc::run_verbose(sv.sent_signal(b"u"), true, verbose, svwait_var, sv);
},
"d" => {
// case where command is up
return misc::run_verbose(sv.sent_signal(b"d"), false, verbose, svwait_var, sv);
},
"o" => {
// case where command is run once.
return misc::run_verbose(sv.sent_signal(b"o"), true, verbose, svwait_var, sv);
},
"p" => {
// case where it's the pause command which is entered.
return misc::pause_verbose(sv.sent_signal(b"p"), true, verbose, svwait_var, sv);
},
"c" => {
// case where it's the continue command which is entered.
return misc::pause_verbose(sv.sent_signal(b"c"), false, verbose, svwait_var, sv);
},
"h" => {
return misc::noexcept_verbose(sv.sent_signal(b"h"), verbose, sv);
},
"a" => {
return misc::noexcept_verbose(sv.sent_signal(b"a"), verbose, sv);
},
"i" => {
return misc::noexcept_verbose(sv.sent_signal(b"i"), verbose, sv);
},
"q" => {
return misc::noexcept_verbose(sv.sent_signal(b"q"), verbose, sv);
},
"1" => {
return misc::noexcept_verbose(sv.sent_signal(b"1"), verbose, sv);
},
"2" => {
return misc::noexcept_verbose(sv.sent_signal(b"2"), verbose, sv);
},
"t" => {
return misc::term_verbose(sv.sent_signal(b"t"), false, verbose, svwait_var, sv);
},
"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.
return 1;
}
};
}

View file

@ -0,0 +1,196 @@
use crate::status_obj;
use std::thread::sleep;
use std::time::Duration;
pub fn print_status(mut sv: status_obj::StatusObj) -> i32 {
// The status code is the one who will be
let mut status_sv = sv.get_status_string();
let is_log = sv.get_log();
if is_log {
let log_option = sv.get_obj_log();
match log_option {
Some(mut log) => {
status_sv = status_sv + "; " + &log.get_status_string();
},
None => {
return 1
},
};
}
println!("{}", status_sv);
return 0;
}
pub fn run_verbose(sent_signal: bool, expected: bool, verbose: i8, svwait_var: i32, mut sv: status_obj::StatusObj) -> i32 {
// if the excepted is true, wait for the service to run.
// If the excepted is false, wait for the service to down.
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_up() == expected {
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 pause_verbose(sent_signal: bool, expected: bool, verbose: i8, svwait_var: i32, mut sv: status_obj::StatusObj) -> i32 {
// if the excepted is true, wait for the service to pause.
// if the excepted is false, wait for the service to continue.
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_paused() == expected {
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 term_verbose(sent_signal: bool, expected: 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_term() == expected {
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 kill_verbose(sent_signal: bool, verbose: i8, svwait_var: i32, mut sv: status_obj::StatusObj) -> i32 {
if sent_signal && verbose == 1 {
sleep(Duration::from_secs_f32(0.0005));
let mut time_wait = 0;
if ! sv.get_run_finish() {
println!("ok: {}", sv.get_status_string());
return 0;
}
let pid = sv.get_pid();
let time = sv.get_time();
loop {
let status = sv.update_status();
if status == 1 {
return 1;
}
if pid != sv.get_pid() || time > sv.get_time() {
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 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));
let status = sv.update_status();
if status == 1 {
return 1;
}
println!("ok: {}", sv.get_status_string());
return 0
} else if sent_signal && verbose == 0 {
return 0
} else {
// Case sent_signal != true.
return 1
}
}

View file

@ -1,85 +0,0 @@
mod misc;
use crate::status_obj;
use std::thread::sleep;
use std::time::Duration;
pub fn sent_signal( svwait_var: i32, verbose: i8, command: &str, mut sv: status_obj::StatusObj) -> i32 {
// Return 0 in case everything worked fine, return 1 if timeout or error.
let mut time_wait = 0;
// execute command.
if command == "s" {
// case command is status
return misc::print_status(sv);
}
if command == "u" {
// case where command is up
if sv.sent_signal(b"u") {
if verbose == 1 {
loop {
let status = sv.update_status();
if status == 1 {
return 1;
}
if sv.is_up() == true {
misc::print_status(sv);
return 0
}
if time_wait >= svwait_var {
println!("fail: {}: timeout", sv.get_name());
return 1
}
time_wait += 1;
sleep(Duration::from_secs(1));
}
} else {
return 0;
}
} else {
return 1;
}
}
if command == "d" {
// case where command is up
if sv.sent_signal(b"d") {
if verbose == 1 {
loop {
let status = sv.update_status();
if status == 1 {
return 1;
}
if sv.is_up() == false {
misc::print_status(sv);
return 0
}
if time_wait >= svwait_var {
println!("fail: {}: timeout", sv.get_name());
return 1
}
time_wait += 1;
sleep(Duration::from_secs(1));
}
} else {
return 0;
}
} else {
return 1;
}
}
// TODO
// sent the proper signal to supervise/control
//
// if verbose = 1, wait for the service to reach the proper state or timeout.
println!("Error, command not implemented.");
return 1;
}

View file

@ -1,21 +0,0 @@
use crate::status_obj;
pub fn print_status(mut sv: status_obj::StatusObj) -> i32 {
// The status code is the one who will be
let mut status_sv = sv.get_status_string();
let is_log = sv.get_log();
if is_log {
let log_option = sv.get_obj_log();
match log_option {
Some(mut log) => {
status_sv = status_sv + "; " + &log.get_status_string();
},
None => {
return 1
},
};
}
println!("{}", status_sv);
return 0;
}

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

@ -1,5 +1,7 @@
mod misc; mod misc;
mod sent_signal;
mod check_test;
use std::path::Path; use std::path::Path;
use std::env::set_current_dir; use std::env::set_current_dir;
@ -37,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 = misc::check_fifo(sv.clone(), "supervise/ok"); let control_exists = sent_signal::check_fifo("supervise/ok");
if control_exists == false { match control_exists {
return None; 1 => {
} println!("fail: {sv}: runsv not running");
let control_exists = misc::check_fifo(sv.clone(), "supervise/control"); return None;
if control_exists == false { },
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"); let status_option = fs::OpenOptions::new().read(true).open("supervise/status");
@ -166,6 +184,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;
@ -200,7 +222,7 @@ impl StatusObj {
ud_string = ""; ud_string = "";
} }
} }
if self.pause_run == true { if self.pause_run == true && self.run_finish {
pause_string = ", paused"; pause_string = ", paused";
} else { } else {
pause_string = ""; pause_string = "";
@ -211,6 +233,7 @@ impl StatusObj {
term_string = ""; term_string = "";
} }
let time_repaired: u64 = self.time - 4611686018427387914; let time_repaired: u64 = self.time - 4611686018427387914;
let sys_time_result = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH); let sys_time_result = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH);
let sys_time = match sys_time_result { let sys_time = match sys_time_result {
@ -226,11 +249,42 @@ impl StatusObj {
} }
pub fn sent_signal(&mut self, signal: &[u8]) -> bool { pub fn sent_signal(&mut self, signal: &[u8]) -> bool {
return misc::sent_signal(signal); return sent_signal::sent_signal(signal);
} }
pub fn is_up(&mut self) -> bool { pub fn is_up(&mut self) -> bool {
return self.run_finish; let check = fs::File::open("check");
match check {
Ok(file) => {
return check_test::check_test(file, self.run_finish)
},
Err(..) => return self.run_finish,
};
}
pub fn is_paused(&mut self) -> bool {
if self.pause_run == true {
return true
} else {
return false
}
}
pub fn is_term(&mut self) -> bool {
if self.term_sig == true {
return true
} else {
return false
}
}
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 {
@ -245,6 +299,18 @@ impl StatusObj {
return self.log; return self.log;
} }
pub fn get_pid(&mut self) -> u32 {
return self.pid;
}
pub fn get_time(&mut self) -> u64 {
return self.time;
}
pub fn get_run_finish(&mut self) -> bool {
return self.run_finish;
}
pub fn get_obj_log(&mut self) -> Option<StatusObj> { pub fn get_obj_log(&mut self) -> Option<StatusObj> {
return StatusObj::new(self.svpath.clone() + "/log", "log".to_string()); return StatusObj::new(self.svpath.clone() + "/log", "log".to_string());
} }

View file

@ -0,0 +1,33 @@
use std::fs::File;
use std::process::Command;
use std::os::unix::fs::PermissionsExt;
pub fn check_test(file: File, run_finish: bool) -> bool {
// Check the permission, and return the result of check
let meta = file.metadata();
match meta {
Ok(m) => {
let permissions = m.permissions();
//println!("permissions: {:o}", permissions.mode() & 0o100700);
if permissions.mode() & 0o100700 == 0o100700 {
// Execute script
let mut command = Command::new("sh");
let status = command.arg("check").status();
match status {
Ok(s) => {
if s.success() {
return true
} else {
return false
}
},
Err(..) => {}
};
}
},
Err(..) => {},
};
return run_finish;
}

View file

@ -1,7 +1,4 @@
use tokio::net::unix::pipe;
use tokio::runtime::Runtime;
use std::io::ErrorKind;
pub fn make_pause_run(pr_int: u8) -> bool { pub fn make_pause_run(pr_int: u8) -> bool {
if pr_int == 0 { if pr_int == 0 {
@ -76,58 +73,3 @@ pub fn return_u8_in_u64(table: Vec<u8>) -> u64 {
return r return r
} }
pub fn check_fifo(sv: String, path: &str) -> bool {
// Check if fifo is available. If yes, return true.
// If no, return false.
let rt = Runtime::new().unwrap();
let _guard = rt.enter();
let sender = pipe::OpenOptions::new().open_sender(path);
match sender {
Ok(..) => return true,
Err(e) if e.raw_os_error() == Some(libc::ENXIO) => {
println!("fail: {sv}: runsv not running");
return false;
},
Err(..) => {
println!("warning: {sv}: unable to open {path}: file does not exist");
return false;
},
};
}
pub fn sent_signal(signal: &[u8]) -> bool {
// sent the char to supervise/control.
// return true in case everything worked fine, false if there was any errors.
let rt = Runtime::new().unwrap();
let _guard = rt.enter();
let sender = pipe::OpenOptions::new().open_sender("supervise/control");
match sender {
Ok(tx) => {
loop {
match tx.try_write(signal) {
Ok(..) => {
return true;
},
Err(e) if e.kind() == ErrorKind::WouldBlock => {
continue;
},
Err(e) => {
dbg!(e);
println!("Impossible Error while writing on pipe.");
return false;
},
};
};
},
Err(e) if e.raw_os_error() == Some(libc::ENXIO) => {
println!("fail: sv: runsv not running");
return false;
},
Err(..) => {
println!("warning: sv: unable to open path: file does not exist");
return false;
},
};
}

View file

@ -0,0 +1,62 @@
use tokio::net::unix::pipe;
use tokio::runtime::Runtime;
use std::io::ErrorKind;
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();
let _guard = rt.enter();
let sender = pipe::OpenOptions::new().open_sender(path);
match sender {
Ok(..) => return 0,
Err(e) if e.raw_os_error() == Some(libc::ENXIO) => {
//println!("fail: {sv}: runsv not running");
return 1;
},
Err(..) => {
//println!("warning: {sv}: unable to open {path}: file does not exist");
return 2;
},
};
}
pub fn sent_signal(signal: &[u8]) -> bool {
// sent the char to supervise/control.
// return true in case everything worked fine, false if there was any errors.
let rt = Runtime::new().unwrap();
let _guard = rt.enter();
let sender = pipe::OpenOptions::new().open_sender("supervise/control");
match sender {
Ok(tx) => {
loop {
match tx.try_write(signal) {
Ok(..) => {
return true;
},
Err(e) if e.kind() == ErrorKind::WouldBlock => {
continue;
},
Err(e) => {
dbg!(e);
println!("Unable to write to supervise/control");
return false;
},
};
};
},
Err(e) if e.raw_os_error() == Some(libc::ENXIO) => {
println!("fail: sv: runsv not running");
return false;
},
Err(..) => {
println!("warning: sv: unable to open path: file does not exist");
return false;
},
};
}