Compare commits
2 Commits
c6c2136a9f
...
83d19db638
Author | SHA1 | Date | |
---|---|---|---|
83d19db638 | |||
4336130353 |
85
src/main.rs
85
src/main.rs
@ -1,4 +1,7 @@
|
||||
use std::{collections::HashMap, os::mikros::{self, ipc, syscalls, Errno}};
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
os::mikros::{self, ipc, syscalls, Errno},
|
||||
};
|
||||
|
||||
use parking_lot::RwLock;
|
||||
|
||||
@ -22,38 +25,57 @@ struct Serv {
|
||||
|
||||
impl proc_man_rpc::Server for Serv {
|
||||
fn new_proc(&self, pid: u64, parent: Option<u64>) -> Result<(), Errno> {
|
||||
let syslog_pid = syscalls::try_get_registered(2).unwrap();
|
||||
let syslog_client = syslog_rpc::Client::new(syslog_pid);
|
||||
syslog_client.send_text_message("proc_man", format!("Process {pid} created with parent {parent:?}")).unwrap();
|
||||
self.processes.write().insert(pid, Process {
|
||||
stdio: [None; 3],
|
||||
cli_args: Vec::new(),
|
||||
status: ProcessStatus::Running,
|
||||
parent,
|
||||
children: Vec::new(),
|
||||
});
|
||||
self.processes.write().insert(
|
||||
pid,
|
||||
Process {
|
||||
stdio: [None; 3],
|
||||
cli_args: Vec::new(),
|
||||
status: ProcessStatus::Running,
|
||||
parent,
|
||||
children: Vec::new(),
|
||||
},
|
||||
);
|
||||
if let Some(parent) = parent {
|
||||
self.processes.write().get_mut(&parent).unwrap().children.push(pid);
|
||||
self.processes
|
||||
.write()
|
||||
.get_mut(&parent)
|
||||
.unwrap()
|
||||
.children
|
||||
.push(pid);
|
||||
};
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn set_stdio(&self, pid: u64, stdio: [Option<(u64, u64)>; 3]) -> Result<(), Errno> {
|
||||
self.processes.write().get_mut(&pid).ok_or(Errno::ESRCH)?.stdio = stdio;
|
||||
self.processes
|
||||
.write()
|
||||
.get_mut(&pid)
|
||||
.ok_or(Errno::ESRCH)?
|
||||
.stdio = stdio;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn get_stdio(&self, from: u64) -> [Option<(u64, u64)>; 3] {
|
||||
self.processes.read().get(&from).map_or([None; 3], |proc| proc.stdio)
|
||||
self.processes
|
||||
.read()
|
||||
.get(&from)
|
||||
.map_or([None; 3], |proc| proc.stdio)
|
||||
}
|
||||
|
||||
fn set_cli_args(&self, pid: u64, args: Vec<String>) -> Result<(), Errno> {
|
||||
self.processes.write().get_mut(&pid).ok_or(Errno::ESRCH)?.cli_args = args;
|
||||
self.processes
|
||||
.write()
|
||||
.get_mut(&pid)
|
||||
.ok_or(Errno::ESRCH)?
|
||||
.cli_args = args;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn get_cli_args(&self, from: u64) -> Vec<String> {
|
||||
self.processes.read().get(&from).map_or(Vec::new(), |proc| proc.cli_args.clone())
|
||||
self.processes
|
||||
.read()
|
||||
.get(&from)
|
||||
.map_or(Vec::new(), |proc| proc.cli_args.clone())
|
||||
}
|
||||
|
||||
fn exit(&self, from: u64, code: u8) {
|
||||
@ -78,8 +100,16 @@ impl proc_man_rpc::Server for Serv {
|
||||
if let ProcessStatus::Exited(code) = proc_status {
|
||||
self.processes.write().remove(&pid);
|
||||
syscalls::clear_exited(pid).unwrap();
|
||||
self.processes.write().get_mut(&from).unwrap().children.retain(|&child| child != pid);
|
||||
return Ok(proc_man_rpc::WaitResult { pid, exit_code: code });
|
||||
self.processes
|
||||
.write()
|
||||
.get_mut(&from)
|
||||
.unwrap()
|
||||
.children
|
||||
.retain(|&child| child != pid);
|
||||
return Ok(proc_man_rpc::WaitResult {
|
||||
pid,
|
||||
exit_code: code,
|
||||
});
|
||||
};
|
||||
}
|
||||
std::mem::drop(processes);
|
||||
@ -91,11 +121,19 @@ impl proc_man_rpc::Server for Serv {
|
||||
} else {
|
||||
loop {
|
||||
let proc_status = self.processes.read().get(&pid).unwrap().status;
|
||||
if let ProcessStatus::Exited(code) = proc_status {
|
||||
if let ProcessStatus::Exited(code) = proc_status {
|
||||
self.processes.write().remove(&pid);
|
||||
syscalls::clear_exited(pid).unwrap();
|
||||
self.processes.write().get_mut(&from).unwrap().children.retain(|&child| child != pid);
|
||||
return Ok(proc_man_rpc::WaitResult { pid, exit_code: code });
|
||||
self.processes
|
||||
.write()
|
||||
.get_mut(&from)
|
||||
.unwrap()
|
||||
.children
|
||||
.retain(|&child| child != pid);
|
||||
return Ok(proc_man_rpc::WaitResult {
|
||||
pid,
|
||||
exit_code: code,
|
||||
});
|
||||
};
|
||||
if !block {
|
||||
return Err(Errno::EAGAIN);
|
||||
@ -104,7 +142,6 @@ impl proc_man_rpc::Server for Serv {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fn main() {
|
||||
@ -119,7 +156,9 @@ fn main() {
|
||||
};
|
||||
proc_man_rpc::register_server(Box::new(serv));
|
||||
syscalls::register(3);
|
||||
syslog_client.send_text_message("proc_man", "Process manager initialized").unwrap();
|
||||
syslog_client
|
||||
.send_text_message("proc_man", "Process manager initialized")
|
||||
.unwrap();
|
||||
loop {
|
||||
ipc::process_messages();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user