diff --git a/01_phase_rust_src/sv/src/executor.rs b/01_phase_rust_src/sv/src/executor.rs index 91ad357..ae04033 100644 --- a/01_phase_rust_src/sv/src/executor.rs +++ b/01_phase_rust_src/sv/src/executor.rs @@ -3,6 +3,86 @@ use std::fs; use std::path::Path; use std::env::set_current_dir; +use std::io::prelude::*; + +fn make_exit_code(exit_code: i32) -> i32 { + if exit_code < 99 { + return exit_code + 1; + } else { + return exit_code; + } +} + +fn return_u8_in_u32(mut table: Vec) -> u32 { + // Table make 4 vector u8 in one u32 + if table.len() != 4 { + return 0 + } + let mut r: u32 = 0; + table.reverse(); + for i in table.iter() { + println!("{}",i); + r = r * 256; + r += >::into(*i); + } + return r +} + +fn exec_status(exit_code: i32, sv: String) -> i32 { + let status_option = fs::OpenOptions::new().read(true).open("supervise/status"); + + // works this way : + // 20 chars, + // TODO 0 -> 11; time since I don't know when. It is time_sv = 4611686018427387914 + time_unix + // 12 -> 15; PID + // 16; PAUSE/RUN + // 17; UP/DOWN + // 18; TERM SIG + // 19; RUN/FINISH + // + // In this version, don't take in account the time. (char 0 -> 11) + + if ! status_option.is_ok() { + println!("ERROR, Unable to get supervise/status"); + return make_exit_code(exit_code); + } + + let mut status = match status_option { + Ok(file) => file, + Err(..) => { + println!("warning: {}: unable to open supervise/status: file does not exist", sv.clone()); + return make_exit_code(exit_code); + }, + }; + + let mut contents: Vec = Vec::new(); + let option_size = status.read_to_end(&mut contents); + + let size = match option_size { + Ok(size) => size, + Err(..) => { + println!("warning: {}: couldn't read supervise/status", sv.clone()); + return make_exit_code(exit_code); + }, + }; + + let time_buf: Vec = contents[0..12].to_vec(); + let pid_buf: Vec = contents[12..16].to_vec(); + + let pid: u32 = return_u8_in_u32(pid_buf.clone()); // PID OK + + let pause_run: u8 = contents[16]; // TODO function truc(contents) -> String + let up_down: u8 = contents[17]; // TODO function truc(contents) -> String + let term_sig: u8 = contents[18]; // TODO function truc(contents) -> String + let run_finish: u8 = contents[19]; // TODO function truc(contents) -> String + dbg!(contents); + dbg!(time_buf); + dbg!(pid_buf); + dbg!(pid); + + return exit_code; + +} fn sent_signal(exit_code: i32, svwait_var: i32, verbose: i8, command: &str, sv: String) -> i32 { @@ -10,12 +90,27 @@ fn sent_signal(exit_code: i32, svwait_var: i32, verbose: i8, command: &str, sv: let supervise_ok = Path::new("supervise/ok"); if ! supervise_ok.exists() { println!("warning: {}: unable to open supervise/ok: file does not exist", sv.clone()); - if exit_code < 99 { - return exit_code + 1; - } else { - return exit_code; - } + return make_exit_code(exit_code); } + + // execute command. + if command == "s" { + // case command is status + return exec_status(exit_code, sv); + } + + //let service = fs::OpenOptions::new().append(true).open("supervise/control"); + + //if ! service.is_ok() { + // println!("warning: {}: unable to append to supervise/control: file does not exist", sv.clone()); + // return make_exit_code(exit_code); + //} + //let mut control = match service { + // Ok(file) => file, + // Err(..) => panic!("Shouln't goes there, the programmer is trash."), + //}; + + // TODO // sent the proper signal to supervise/control //