redox/kernel/context/context.rs

171 lines
5 KiB
Rust
Raw Normal View History

2016-09-17 02:27:54 +02:00
use alloc::arc::Arc;
2016-09-16 18:44:52 +02:00
use alloc::boxed::Box;
use collections::{BTreeMap, Vec, VecDeque};
2016-09-17 02:27:54 +02:00
use spin::Mutex;
2016-09-16 18:44:52 +02:00
use arch;
use syscall::data::Event;
2016-09-16 18:44:52 +02:00
use super::file::File;
use super::memory::{Grant, Memory, SharedMemory};
2016-09-16 18:44:52 +02:00
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum Status {
Runnable,
Blocked,
2016-09-17 17:23:36 +02:00
Exited(usize)
}
2016-09-16 18:44:52 +02:00
/// A context, which identifies either a process or a thread
#[derive(Debug)]
pub struct Context {
/// The ID of this context
pub id: usize,
2016-09-29 05:33:54 +02:00
/// The ID of the parent context
pub ppid: usize,
/// The real user id
pub ruid: u32,
/// The real group id
pub rgid: u32,
/// The effective user id
pub euid: u32,
/// The effective group id
pub egid: u32,
/// Status of context
pub status: Status,
/// Context running or not
2016-09-16 18:44:52 +02:00
pub running: bool,
2016-09-29 05:33:54 +02:00
/// Context is halting parent
pub vfork: bool,
2016-09-16 18:44:52 +02:00
/// The architecture specific context
pub arch: arch::context::Context,
2016-09-23 00:14:45 +02:00
/// Kernel FX
pub kfx: Option<Box<[u8]>>,
2016-09-16 18:44:52 +02:00
/// Kernel stack
pub kstack: Option<Box<[u8]>>,
/// Executable image
2016-09-17 01:51:27 +02:00
pub image: Vec<SharedMemory>,
2016-09-16 18:44:52 +02:00
/// User heap
2016-09-17 01:51:27 +02:00
pub heap: Option<SharedMemory>,
2016-09-16 18:44:52 +02:00
/// User stack
pub stack: Option<Memory>,
/// User grants
pub grants: Arc<Mutex<Vec<Grant>>>,
/// The current working directory
pub cwd: Arc<Mutex<Vec<u8>>>,
/// Kernel events
pub events: Arc<Mutex<VecDeque<Event>>>,
2016-09-23 19:01:53 +02:00
/// The process environment
pub env: Arc<Mutex<BTreeMap<Box<[u8]>, Arc<Mutex<Vec<u8>>>>>>,
2016-09-16 18:44:52 +02:00
/// The open files in the scheme
2016-09-17 02:27:54 +02:00
pub files: Arc<Mutex<Vec<Option<File>>>>
2016-09-16 18:44:52 +02:00
}
impl Context {
/// Create a new context
pub fn new(id: usize) -> Context {
Context {
id: id,
2016-09-29 05:33:54 +02:00
ppid: 0,
ruid: 0,
rgid: 0,
euid: 0,
egid: 0,
status: Status::Blocked,
2016-09-16 18:44:52 +02:00
running: false,
2016-09-29 05:33:54 +02:00
vfork: false,
2016-09-16 18:44:52 +02:00
arch: arch::context::Context::new(),
2016-09-23 00:14:45 +02:00
kfx: None,
2016-09-16 18:44:52 +02:00
kstack: None,
image: Vec::new(),
heap: None,
stack: None,
grants: Arc::new(Mutex::new(Vec::new())),
cwd: Arc::new(Mutex::new(Vec::new())),
events: Arc::new(Mutex::new(VecDeque::new())),
2016-09-23 19:01:53 +02:00
env: Arc::new(Mutex::new(BTreeMap::new())),
2016-09-17 02:27:54 +02:00
files: Arc::new(Mutex::new(Vec::new()))
2016-09-16 18:44:52 +02:00
}
}
pub fn canonicalize(&self, path: &[u8]) -> Vec<u8> {
if path.iter().position(|&b| b == b':').is_none() {
let cwd = self.cwd.lock();
if path == b"." {
cwd.clone()
} else if path == b".." {
cwd[..cwd[..cwd.len() - 1]
2016-09-23 05:27:33 +02:00
.iter().rposition(|&b| b == b'/' || b == b':')
.map_or(cwd.len(), |i| i + 1)]
.to_vec()
} else if path.starts_with(b"./") {
let mut canon = cwd.clone();
if ! canon.ends_with(b"/") {
canon.push(b'/');
}
canon.extend_from_slice(&path[2..]);
canon
} else if path.starts_with(b"../") {
let mut canon = cwd[..cwd[..cwd.len() - 1]
2016-09-23 05:27:33 +02:00
.iter().rposition(|&b| b == b'/' || b == b':')
.map_or(cwd.len(), |i| i + 1)]
.to_vec();
canon.extend_from_slice(&path[3..]);
canon
} else if path.starts_with(b"/") {
let mut canon = cwd[..cwd.iter().position(|&b| b == b':').map_or(1, |i| i + 1)].to_vec();
canon.extend_from_slice(&path);
canon
} else {
let mut canon = cwd.clone();
2016-09-28 19:52:29 +02:00
if ! canon.ends_with(b"/") {
canon.push(b'/');
}
canon.extend_from_slice(&path);
canon
}
} else {
path.to_vec()
}
}
2016-09-16 18:44:52 +02:00
/// Add a file to the lowest available slot.
/// Return the file descriptor number or None if no slot was found
2016-09-18 20:54:10 +02:00
pub fn add_file(&self, file: File) -> Option<usize> {
2016-09-17 02:27:54 +02:00
let mut files = self.files.lock();
for (i, mut file_option) in files.iter_mut().enumerate() {
2016-09-16 18:44:52 +02:00
if file_option.is_none() {
*file_option = Some(file);
return Some(i);
}
}
2016-09-17 02:27:54 +02:00
let len = files.len();
2016-09-16 18:44:52 +02:00
if len < super::CONTEXT_MAX_FILES {
2016-09-17 02:27:54 +02:00
files.push(Some(file));
2016-09-16 18:44:52 +02:00
Some(len)
} else {
None
}
}
/// Get a file
pub fn get_file(&self, i: usize) -> Option<File> {
2016-09-17 02:27:54 +02:00
let files = self.files.lock();
if i < files.len() {
files[i]
2016-09-16 18:44:52 +02:00
} else {
None
}
}
/// Remove a file
// TODO: adjust files vector to smaller size if possible
2016-09-18 20:54:10 +02:00
pub fn remove_file(&self, i: usize) -> Option<File> {
2016-09-17 02:27:54 +02:00
let mut files = self.files.lock();
if i < files.len() {
files[i].take()
2016-09-16 18:44:52 +02:00
} else {
None
}
}
}