Compare commits

...

2 Commits

Author SHA1 Message Date
2304c939a1
Add get/set_cwd 2024-11-10 10:23:30 -06:00
806d079c7f
Add get_processes RPC call 2024-11-10 10:23:23 -06:00
5 changed files with 107 additions and 5 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

@ -1,11 +1,11 @@
#![allow(clippy::result_unit_err)]
use std::os::mikros::{
use std::{os::mikros::{
ipc::rpc::{self, IncomingCall},
Errno,
};
}, path::PathBuf};
pub use proc_man_structs::WaitResult;
pub use proc_man_structs::*;
use parking_lot::RwLock;
@ -23,6 +23,9 @@ 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>;
fn set_cwd(&self, pid: u64, path: PathBuf) -> Result<(), Errno>;
fn get_cwd(&self, from: u64) -> PathBuf;
}
#[derive(Copy, Clone)]
@ -74,6 +77,27 @@ impl Client {
)
.unwrap()
}
pub fn get_processes(&self) -> Vec<Process> {
postcard::from_bytes(
&rpc::send_call(self.0, PROTO, 8, &[]).get_return(),
)
.unwrap()
}
fn set_cwd(&self, pid: u64, path: PathBuf) -> Result<(), Errno> {
postcard::from_bytes(
&rpc::send_call(self.0, PROTO, 9, &postcard::to_stdvec(&(pid, path)).unwrap()).get_return(),
)
.unwrap()
}
fn get_cwd(&self) -> PathBuf {
postcard::from_bytes(
&rpc::send_call(self.0, PROTO, 10, &[]).get_return(),
)
.unwrap()
}
}
pub fn register_server(server: Box<dyn Server>) {
@ -120,5 +144,15 @@ 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);
} else if call.func == 9 {
let (pid, path)= postcard::from_bytes(call.args()).unwrap();
let ret = postcard::to_stdvec(&server.set_cwd(pid, path)).unwrap();
call.send_return(&ret);
} else if call.func == 10 {
let ret = postcard::to_stdvec(&server.get_cwd(call.from)).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

@ -2,6 +2,7 @@ use std::{
collections::HashMap,
ffi::OsString,
os::mikros::{self, ipc, syscalls, Errno},
path::PathBuf,
};
use parking_lot::RwLock;
@ -18,6 +19,7 @@ struct Process {
status: ProcessStatus,
parent: Option<u64>,
children: Vec<u64>,
cwd: PathBuf,
}
struct Serv {
@ -34,6 +36,7 @@ impl proc_man_rpc::Server for Serv {
status: ProcessStatus::Running,
parent,
children: Vec::new(),
cwd: PathBuf::from("/"),
},
);
if let Some(parent) = parent {
@ -64,7 +67,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 +146,52 @@ 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 set_cwd(&self, pid: u64, path: PathBuf) -> Result<(), Errno> {
if !path.is_absolute() {
return Err(Errno::EINVAL);
}
// Try to open the given directory, if it fails it's not a valid directory.
// Use Metadata instead once it's implemented.
std::fs::read_dir(&path)?;
self.processes
.write()
.get_mut(&pid)
.ok_or(Errno::ESRCH)?
.cwd = path;
Ok(())
}
fn get_cwd(&self, from: u64) -> PathBuf {
self.processes
.read()
.get(&from)
.map_or(PathBuf::from("/"), |proc| proc.cwd.clone())
}
}
fn main() {