Add get/set_cwd

This commit is contained in:
pjht 2024-11-10 10:23:30 -06:00
parent 806d079c7f
commit 2304c939a1
Signed by: pjht
GPG Key ID: 7B5F6AFBEC7EE78E
2 changed files with 50 additions and 2 deletions

View File

@ -1,9 +1,9 @@
#![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::*;
@ -24,6 +24,8 @@ pub trait Server: Send + Sync {
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)]
@ -82,6 +84,20 @@ impl Client {
)
.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>) {
@ -131,5 +147,12 @@ fn callback(call: IncomingCall) {
} 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

@ -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 {
@ -167,6 +170,28 @@ impl proc_man_rpc::Server for Serv {
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() {