split status_obj/misc.rs into three files.

This commit is contained in:
primardj 2024-04-14 21:20:47 +01:00
parent 486820f096
commit cb96b16438
4 changed files with 102 additions and 97 deletions

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;
@ -8,7 +10,6 @@ use std::io::prelude::*;
use std::time::SystemTime; use std::time::SystemTime;
#[derive(Debug)] #[derive(Debug)]
pub struct StatusObj { pub struct StatusObj {
svname: String, // Service name svname: String, // Service name
@ -38,11 +39,11 @@ 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(sv.clone(), "supervise/ok");
if control_exists == false { if control_exists == false {
return None; 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 { if control_exists == false {
return None; return None;
} }
@ -227,18 +228,17 @@ 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 {
let check = fs::File::open("check"); let check = fs::File::open("check");
match check { match check {
Ok(file) => { 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 { pub fn get_name(&mut self) -> 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,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 { pub fn make_pause_run(pr_int: u8) -> bool {
if pr_int == 0 { if pr_int == 0 {
@ -80,86 +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;
},
};
}
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;
}

View file

@ -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;
},
};
}