Keep track of process stdio FDs

This commit is contained in:
pjht 2024-09-22 11:52:14 -05:00
parent 134bc41511
commit 35b8f3b441
Signed by: pjht
GPG Key ID: 7B5F6AFBEC7EE78E
2 changed files with 47 additions and 0 deletions

View File

@ -10,6 +10,7 @@ use parking_lot::RwLock;
struct Serv {
fs_map: RwLock<HashMap<String, u64>>,
mount_list: RwLock<HashMap<PathBuf, (u64, u64)>>,
process_stdio: RwLock<HashMap<u64, [Option<(u64, u64)>; 3]>>,
}
impl vfs_rpc::Server for Serv {
@ -61,12 +62,23 @@ impl vfs_rpc::Server for Serv {
Ok(())
}
}
fn set_stdio(&self, pid: u64, stdio: [Option<(u64, u64)>; 3]) -> Result<(), ()> {
self.process_stdio.write().insert(pid, stdio);
Ok(())
}
fn get_stdio(&self, from: u64) -> [Option<(u64, u64)>; 3] {
let stdio = self.process_stdio.read().get(&from).copied().unwrap_or([None; 3]);
stdio
}
}
fn main() {
vfs_rpc::register_server(Box::new(Serv {
fs_map: RwLock::new(HashMap::new()),
mount_list: RwLock::new(HashMap::new()),
process_stdio: RwLock::new(HashMap::new()),
}));
syscalls::register(0);
loop {

View File

@ -16,6 +16,8 @@ pub trait Server: Send + Sync {
fn mount(&self, dev: &Path, kind: &str, path: &Path) -> Result<(), ()>;
fn open(&self, path: &Path) -> Result<(u64, u64), ()>;
fn unmount(&self, path: &Path) -> Result<(), ()>;
fn set_stdio(&self, pid: u64, stdio: [Option<(u64, u64)>; 3]) -> Result<(), ()>;
fn get_stdio(&self, from: u64) -> [Option<(u64, u64)>; 3];
}
#[derive(Copy, Clone)]
@ -65,6 +67,32 @@ impl Client {
)
.unwrap()
}
pub fn set_stdio(&self, pid: u64, stdio: [Option<(u64, u64)>; 3]) -> Result<(), ()> {
postcard::from_bytes(
&rpc::send_call(
self.0,
PROTO,
4,
&postcard::to_stdvec(&(pid, stdio)).unwrap(),
)
.get_return(),
)
.unwrap()
}
pub fn get_stdio(&self) -> [Option<(u64, u64)>; 3] {
postcard::from_bytes(
&rpc::send_call(
self.0,
PROTO,
5,
&[],
)
.get_return(),
)
.unwrap()
}
}
pub fn register_server(server: Box<dyn Server>) {
@ -96,5 +124,12 @@ fn callback(call: IncomingCall) {
let path = postcard::from_bytes(call.args()).unwrap();
let ret = postcard::to_stdvec(&server.unmount(path)).unwrap();
call.send_return(&ret);
} else if call.func == 4 {
let (pid, stdio) = postcard::from_bytes(call.args()).unwrap();
let ret = postcard::to_stdvec(&server.set_stdio(pid, stdio)).unwrap();
call.send_return(&ret);
} else if call.func == 5 {
let ret = postcard::to_stdvec(&server.get_stdio(call.from)).unwrap();
call.send_return(&ret);
}
}