From 3b44aa3a07d009184b11a3968ef636be1bbab4a4 Mon Sep 17 00:00:00 2001 From: primardj Date: Fri, 12 Apr 2024 00:27:29 +0100 Subject: [PATCH 1/2] remove some useless comments. --- 01_phase_rust_src/sv/src/status_obj.rs | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/01_phase_rust_src/sv/src/status_obj.rs b/01_phase_rust_src/sv/src/status_obj.rs index a87ff49..14a10ae 100644 --- a/01_phase_rust_src/sv/src/status_obj.rs +++ b/01_phase_rust_src/sv/src/status_obj.rs @@ -42,17 +42,8 @@ impl StatusObj { } - // TODO: check if there is a process reading the pipe. + // 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 mut control_option = fs::File::open("supervise/control"); - //let status_option = fs::OpenOptions::new().read(true).open("supervise/control"); - //match control_option { - // Ok(_file) => {}, - // Err(ee) => { - // println!("fail: {}: runsv not running {}", sv, ee); - // return None; - // }, - //}; let status_option = fs::OpenOptions::new().read(true).open("supervise/status"); let mut status = match status_option { Ok(file) => file, @@ -73,8 +64,6 @@ impl StatusObj { }; if size != 20 { - dbg!(size); - dbg!(contents); println!("warning: {}: bad supervise/status format", sv); return None } @@ -120,7 +109,9 @@ impl StatusObj { log: log_exists, }) } + pub fn get_status_string(&mut self) -> String { + // return the status for this service in particular. let status_sv; let pid_string: String; From b971ae1c6c0b4aae35d317b01da26f8a4ffbf428 Mon Sep 17 00:00:00 2001 From: primardj Date: Sat, 13 Apr 2024 00:05:41 +0100 Subject: [PATCH 2/2] Solve the freeze in case no runsv to listen to fifo. --- 01_phase_rust_src/sv/Cargo.toml | 5 ++-- 01_phase_rust_src/sv/src/status_obj.rs | 15 +++++------ 01_phase_rust_src/sv/src/status_obj/misc.rs | 28 +++++++++++++++++++++ 3 files changed, 39 insertions(+), 9 deletions(-) diff --git a/01_phase_rust_src/sv/Cargo.toml b/01_phase_rust_src/sv/Cargo.toml index 950a8e0..43f8a44 100644 --- a/01_phase_rust_src/sv/Cargo.toml +++ b/01_phase_rust_src/sv/Cargo.toml @@ -1,8 +1,9 @@ [package] name = "sv" -version = "0.1.0" -edition = "2021" +version = "0.2.0" +edition = "2024" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +tokio = { version = "1.37.0", features = ["full"] } diff --git a/01_phase_rust_src/sv/src/status_obj.rs b/01_phase_rust_src/sv/src/status_obj.rs index 14a10ae..8ca8c69 100644 --- a/01_phase_rust_src/sv/src/status_obj.rs +++ b/01_phase_rust_src/sv/src/status_obj.rs @@ -8,7 +8,6 @@ use std::io::prelude::*; use std::time::SystemTime; - pub struct StatusObj { svname: String, // Service name svpath: String, // Service path @@ -35,15 +34,18 @@ impl StatusObj { println!("fail: {}: unable to change to service directory: file does not exist", sv); return None } - let ok_option = Path::new("supervise/ok"); - if ! ok_option.exists() { - println!("warning: {}: unable to open supervise/ok: file does not exist", sv); + + // Check if supervise/control and supervise/ok exists, and if runsv running. + 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; } - // 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 mut status = match status_option { Ok(file) => file, @@ -180,4 +182,3 @@ impl StatusObj { } } - diff --git a/01_phase_rust_src/sv/src/status_obj/misc.rs b/01_phase_rust_src/sv/src/status_obj/misc.rs index 896e2d5..85dbd6c 100644 --- a/01_phase_rust_src/sv/src/status_obj/misc.rs +++ b/01_phase_rust_src/sv/src/status_obj/misc.rs @@ -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 { if pr_int == 0 { return false // run @@ -72,3 +77,26 @@ 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(..) => {}, + 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 +}