From cb96b16438264ca860df0d64f6a2593d322483ad Mon Sep 17 00:00:00 2001 From: primardj Date: Sun, 14 Apr 2024 21:20:47 +0100 Subject: [PATCH] split status_obj/misc.rs into three files. --- 01_phase_rust_src/sv-rustit/src/status_obj.rs | 14 +-- .../sv-rustit/src/status_obj/check_test.rs | 33 +++++++ .../sv-rustit/src/status_obj/misc.rs | 90 ------------------- .../sv-rustit/src/status_obj/sent_signal.rs | 62 +++++++++++++ 4 files changed, 102 insertions(+), 97 deletions(-) create mode 100644 01_phase_rust_src/sv-rustit/src/status_obj/check_test.rs create mode 100644 01_phase_rust_src/sv-rustit/src/status_obj/sent_signal.rs 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 777d261..e34b7f5 100644 --- a/01_phase_rust_src/sv-rustit/src/status_obj.rs +++ b/01_phase_rust_src/sv-rustit/src/status_obj.rs @@ -1,5 +1,7 @@ mod misc; +mod sent_signal; +mod check_test; use std::path::Path; use std::env::set_current_dir; @@ -8,7 +10,6 @@ use std::io::prelude::*; use std::time::SystemTime; - #[derive(Debug)] pub struct StatusObj { svname: String, // Service name @@ -38,11 +39,11 @@ impl StatusObj { } // 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(sv.clone(), "supervise/ok"); if control_exists == false { return None; } - let control_exists = misc::check_fifo(sv.clone(), "supervise/control"); + let control_exists = sent_signal::check_fifo(sv.clone(), "supervise/control"); if control_exists == false { return None; } @@ -227,18 +228,17 @@ impl StatusObj { } 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 { let check = fs::File::open("check"); match check { Ok(file) => { - return misc::test_check(file, self.run_finish) + return check_test::check_test(file, self.run_finish) }, - Err(..) => {}, + Err(..) => return self.run_finish, }; - return self.run_finish; } pub fn get_name(&mut self) -> String { diff --git a/01_phase_rust_src/sv-rustit/src/status_obj/check_test.rs b/01_phase_rust_src/sv-rustit/src/status_obj/check_test.rs new file mode 100644 index 0000000..1bddbd6 --- /dev/null +++ b/01_phase_rust_src/sv-rustit/src/status_obj/check_test.rs @@ -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; +} diff --git a/01_phase_rust_src/sv-rustit/src/status_obj/misc.rs b/01_phase_rust_src/sv-rustit/src/status_obj/misc.rs index b53feb2..45a20ba 100644 --- a/01_phase_rust_src/sv-rustit/src/status_obj/misc.rs +++ b/01_phase_rust_src/sv-rustit/src/status_obj/misc.rs @@ -1,11 +1,4 @@ -use tokio::net::unix::pipe; -use tokio::runtime::Runtime; -use std::io::ErrorKind; - -use std::fs::File; -use std::process::Command; -use std::os::unix::fs::PermissionsExt; pub fn make_pause_run(pr_int: u8) -> bool { if pr_int == 0 { @@ -80,86 +73,3 @@ pub fn return_u8_in_u64(table: Vec) -> u64 { 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; - }, - }; -} - -pub fn test_check(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; -} 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 new file mode 100644 index 0000000..9e75d80 --- /dev/null +++ b/01_phase_rust_src/sv-rustit/src/status_obj/sent_signal.rs @@ -0,0 +1,62 @@ + +use tokio::net::unix::pipe; +use tokio::runtime::Runtime; +use std::io::ErrorKind; + + +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!("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; + }, + }; +} +