Solve the freeze in case no runsv to listen to fifo.

This commit is contained in:
primardj 2024-04-13 00:05:41 +01:00
parent 3b44aa3a07
commit b971ae1c6c
3 changed files with 39 additions and 9 deletions

View file

@ -1,8 +1,9 @@
[package] [package]
name = "sv" name = "sv"
version = "0.1.0" version = "0.2.0"
edition = "2021" edition = "2024"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
tokio = { version = "1.37.0", features = ["full"] }

View file

@ -8,7 +8,6 @@ use std::io::prelude::*;
use std::time::SystemTime; use std::time::SystemTime;
pub struct StatusObj { pub struct StatusObj {
svname: String, // Service name svname: String, // Service name
svpath: String, // Service path svpath: String, // Service path
@ -35,15 +34,18 @@ impl StatusObj {
println!("fail: {}: unable to change to service directory: file does not exist", sv); println!("fail: {}: unable to change to service directory: file does not exist", sv);
return None return None
} }
let ok_option = Path::new("supervise/ok");
if ! ok_option.exists() { // Check if supervise/control and supervise/ok exists, and if runsv running.
println!("warning: {}: unable to open supervise/ok: file does not exist", sv); let control_exists = misc::check_fifo(sv.clone(), "supervise/ok");
if control_exists == false {
return None;
}
let control_exists = misc::check_fifo(sv.clone(), "supervise/control");
if control_exists == false {
return None; return None;
} }
// TODO: check if there is a process reading the pipes. supervise/control and supervise/ok
// piste : https://docs.rs/tokio/latest/tokio/net/unix/pipe/struct.OpenOptions.html
let status_option = fs::OpenOptions::new().read(true).open("supervise/status"); let status_option = fs::OpenOptions::new().read(true).open("supervise/status");
let mut status = match status_option { let mut status = match status_option {
Ok(file) => file, Ok(file) => file,
@ -180,4 +182,3 @@ impl StatusObj {
} }
} }

View file

@ -1,4 +1,9 @@
use std::io::ErrorKind;
use tokio::net::unix::pipe;
use tokio::runtime::Runtime;
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 {
return false // run return false // run
@ -72,3 +77,26 @@ 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(..) => {},
Err(e) => match e.kind() {
ErrorKind::NotFound => {
println!("warning: {sv}: unable to open {path}: file does not exist");
return false;
},
_ => {
println!("fail: {sv}: runsv not running");
return false;
}
},
};
return true
}