From 806d079c7f080a51946404fedbbfa1dc6a68ce11 Mon Sep 17 00:00:00 2001 From: pjht Date: Sun, 10 Nov 2024 10:23:23 -0600 Subject: [PATCH] Add get_processes RPC call --- Cargo.lock | 3 ++- Cargo.toml | 1 + proc_man_rpc/src/lib.rs | 13 ++++++++++++- proc_man_structs/src/lib.rs | 18 ++++++++++++++++++ src/main.rs | 25 ++++++++++++++++++++++++- 5 files changed, 57 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0b3e83a..4601832 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,6 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "atomic-polyfill" @@ -149,6 +149,7 @@ version = "0.1.0" dependencies = [ "parking_lot", "proc_man_rpc", + "proc_man_structs", "syslog_rpc", ] diff --git a/Cargo.toml b/Cargo.toml index 7f08dc1..641ac52 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,6 +6,7 @@ edition = "2021" [dependencies] parking_lot = "0.12.3" proc_man_rpc = { version = "0.1.0", path = "proc_man_rpc" } +proc_man_structs = { version = "0.1.0", path = "proc_man_structs" } syslog_rpc = { version = "0.1.0", path = "../syslog/syslog_rpc" } diff --git a/proc_man_rpc/src/lib.rs b/proc_man_rpc/src/lib.rs index 28915ef..190d632 100644 --- a/proc_man_rpc/src/lib.rs +++ b/proc_man_rpc/src/lib.rs @@ -5,7 +5,7 @@ use std::os::mikros::{ Errno, }; -pub use proc_man_structs::WaitResult; +pub use proc_man_structs::*; use parking_lot::RwLock; @@ -23,6 +23,7 @@ pub trait Server: Send + Sync { fn get_cli_args(&self, from: u64) -> Vec; fn exit(&self, from: u64, code: u8); fn wait(&self, from: u64, pid: u64, block: bool) -> Result; + fn get_processes(&self) -> Vec; } #[derive(Copy, Clone)] @@ -74,6 +75,13 @@ impl Client { ) .unwrap() } + + pub fn get_processes(&self) -> Vec { + postcard::from_bytes( + &rpc::send_call(self.0, PROTO, 8, &[]).get_return(), + ) + .unwrap() + } } pub fn register_server(server: Box) { @@ -120,5 +128,8 @@ fn callback(call: IncomingCall) { let (pid, parent) = postcard::from_bytes(call.args()).unwrap(); let ret = postcard::to_stdvec(&server.new_proc(pid, parent)).unwrap(); call.send_return(&ret); + } else if call.func == 8 { + let ret = postcard::to_stdvec(&server.get_processes()).unwrap(); + call.send_return(&ret); } } diff --git a/proc_man_structs/src/lib.rs b/proc_man_structs/src/lib.rs index ce79948..fda6034 100644 --- a/proc_man_structs/src/lib.rs +++ b/proc_man_structs/src/lib.rs @@ -1,3 +1,5 @@ +use std::ffi::OsString; + use serde::{Deserialize, Serialize}; #[derive(Serialize, Deserialize)] @@ -5,3 +7,19 @@ pub struct WaitResult { pub pid: u64, pub exit_code: u8, } + +#[derive(Clone, Copy, Debug, Serialize, Deserialize)] +pub enum ProcessStatus { + Running, + Exited(u8), +} + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct Process { + pub pid: u64, + pub stdio: [Option<(u64, u64)>; 3], + pub cli_args: Vec, + pub status: ProcessStatus, + pub parent: Option, + pub children: Vec, +} diff --git a/src/main.rs b/src/main.rs index 289e2b3..4853a50 100644 --- a/src/main.rs +++ b/src/main.rs @@ -64,7 +64,6 @@ impl proc_man_rpc::Server for Serv { } fn set_cli_args(&self, pid: u64, args: Vec) -> Result<(), Errno> { - eprintln!("Set CLI args for PID {} to {:?}", pid, args); self.processes .write() .get_mut(&pid) @@ -144,6 +143,30 @@ impl proc_man_rpc::Server for Serv { } } } + + fn get_processes(&self) -> Vec { + let mut procs = self + .processes + .read() + .iter() + .map(|(&pid, proc)| { + let status = match proc.status { + ProcessStatus::Running => proc_man_structs::ProcessStatus::Running, + ProcessStatus::Exited(code) => proc_man_structs::ProcessStatus::Exited(code), + }; + proc_man_structs::Process { + pid, + status, + stdio: proc.stdio, + cli_args: proc.cli_args.clone(), + parent: proc.parent, + children: proc.children.clone(), + } + }) + .collect::>(); + procs.sort_by_key(|proc| proc.pid); + procs + } } fn main() {