Add get_processes RPC call

This commit is contained in:
pjht 2024-11-10 10:23:23 -06:00
parent c876df4554
commit 806d079c7f
Signed by: pjht
GPG Key ID: 7B5F6AFBEC7EE78E
5 changed files with 57 additions and 3 deletions

3
Cargo.lock generated
View File

@ -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",
]

View File

@ -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" }

View File

@ -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<OsString>;
fn exit(&self, from: u64, code: u8);
fn wait(&self, from: u64, pid: u64, block: bool) -> Result<WaitResult, Errno>;
fn get_processes(&self) -> Vec<Process>;
}
#[derive(Copy, Clone)]
@ -74,6 +75,13 @@ impl Client {
)
.unwrap()
}
pub fn get_processes(&self) -> Vec<Process> {
postcard::from_bytes(
&rpc::send_call(self.0, PROTO, 8, &[]).get_return(),
)
.unwrap()
}
}
pub fn register_server(server: Box<dyn Server>) {
@ -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);
}
}

View File

@ -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<OsString>,
pub status: ProcessStatus,
pub parent: Option<u64>,
pub children: Vec<u64>,
}

View File

@ -64,7 +64,6 @@ impl proc_man_rpc::Server for Serv {
}
fn set_cli_args(&self, pid: u64, args: Vec<OsString>) -> 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<proc_man_structs::Process> {
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::<Vec<_>>();
procs.sort_by_key(|proc| proc.pid);
procs
}
}
fn main() {