Update libstd. Add CWD and associated syscalls. Remove debugging

This commit is contained in:
Jeremy Soller 2016-09-17 19:01:34 -06:00
parent 7561839cfc
commit 57f5699664
12 changed files with 113 additions and 12 deletions

View file

@ -19,6 +19,8 @@ pub enum Call {
WaitPid = 7,
/// Execute syscall
Exec = 11,
/// Change working directory
ChDir = 12,
/// Get process ID
GetPid = 20,
/// Duplicate file descriptor
@ -30,12 +32,15 @@ pub enum Call {
/// Clone process
Clone = 120,
/// Yield to scheduler
SchedYield = 158
SchedYield = 158,
/// Get process working directory
GetCwd = 183
}
/// Convert numbers to calls
/// See http://syscalls.kernelgrok.com/
impl Call {
//TODO: Return Option<Call>
pub fn from(number: usize) -> Result<Call> {
match number {
1 => Ok(Call::Exit),
@ -45,12 +50,14 @@ impl Call {
6 => Ok(Call::Close),
7 => Ok(Call::WaitPid),
11 => Ok(Call::Exec),
12 => Ok(Call::ChDir),
20 => Ok(Call::GetPid),
41 => Ok(Call::Dup),
45 => Ok(Call::Brk),
110 => Ok(Call::Iopl),
120 => Ok(Call::Clone),
158 => Ok(Call::SchedYield),
183 => Ok(Call::GetCwd),
_ => Err(Error::NoCall)
}
}

View file

@ -5,6 +5,28 @@ use scheme;
use super::{Error, Result};
pub fn chdir(path: &[u8]) -> Result<usize> {
let contexts = context::contexts();
let context_lock = contexts.current().ok_or(Error::NoProcess)?;
let context = context_lock.read();
let canonical = context.canonicalize(path);
*context.cwd.lock() = canonical;
Ok(0)
}
pub fn getcwd(buf: &mut [u8]) -> Result<usize> {
let contexts = context::contexts();
let context_lock = contexts.current().ok_or(Error::NoProcess)?;
let context = context_lock.read();
let cwd = context.cwd.lock();
let mut i = 0;
while i < buf.len() && i < cwd.len() {
buf[i] = cwd[i];
i += 1;
}
Ok(i)
}
/// Read syscall
pub fn read(fd: usize, buf: &mut [u8]) -> Result<usize> {
let file = {

View file

@ -34,12 +34,14 @@ pub extern fn syscall(a: usize, b: usize, c: usize, d: usize, e: usize, f: usize
Call::Close => close(b),
Call::WaitPid => waitpid(b, c, d),
Call::Exec => exec(validate_slice(b as *const u8, c)?, validate_slice(d as *const [usize; 2], e)?),
Call::ChDir => chdir(validate_slice(b as *const u8, c)?),
Call::GetPid => getpid(),
Call::Dup => dup(b),
Call::Brk => brk(b),
Call::Iopl => iopl(b),
Call::Clone => clone(b, stack),
Call::SchedYield => sched_yield()
Call::SchedYield => sched_yield(),
Call::GetCwd => getcwd(validate_slice_mut(b as *mut u8, c)?)
},
Err(err) => {
println!("Unknown syscall {}", a);

View file

@ -1,6 +1,7 @@
///! Process syscalls
use alloc::arc::Arc;
use collections::Vec;
use core::mem;
use core::str;
use spin::Mutex;
@ -53,7 +54,9 @@ pub const CLONE_FILES: usize = 0x400;
pub const CLONE_VFORK: usize = 0x4000;
pub fn clone(flags: usize, stack_base: usize) -> Result<usize> {
//TODO: Copy on write?
println!("Clone {:X}: {:X}", flags, stack_base);
// vfork not supported
assert!(flags & CLONE_VFORK == 0);
let ppid;
let pid;
@ -64,7 +67,8 @@ pub fn clone(flags: usize, stack_base: usize) -> Result<usize> {
let mut image = vec![];
let mut heap_option = None;
let mut stack_option = None;
let mut files = Arc::new(Mutex::new(vec![]));
let cwd;
let files;
// Copy from old process
{
@ -159,9 +163,16 @@ pub fn clone(flags: usize, stack_base: usize) -> Result<usize> {
stack_option = Some(new_stack);
}
if flags & CLONE_FS == CLONE_FS {
cwd = context.cwd.clone();
} else {
cwd = Arc::new(Mutex::new(context.cwd.lock().clone()));
}
if flags & CLONE_FILES == CLONE_FILES {
files = context.files.clone();
} else {
let mut files_vec = Vec::new();
for (fd, file_option) in context.files.lock().iter().enumerate() {
if let Some(file) = *file_option {
let result = {
@ -172,16 +183,17 @@ pub fn clone(flags: usize, stack_base: usize) -> Result<usize> {
};
match result {
Ok(new_number) => {
files.lock().push(Some(context::file::File { scheme: file.scheme, number: new_number }));
files_vec.push(Some(context::file::File { scheme: file.scheme, number: new_number }));
},
Err(err) => {
println!("clone: failed to dup {}: {:?}", fd, err);
}
}
} else {
files.lock().push(None);
files_vec.push(None);
}
}
files = Arc::new(Mutex::new(files_vec));
}
}
@ -289,6 +301,8 @@ pub fn clone(flags: usize, stack_base: usize) -> Result<usize> {
context.stack = Some(stack);
}
context.cwd = cwd;
context.files = files;
context.arch.set_page_table(unsafe { new_table.address() });
@ -322,7 +336,6 @@ pub fn exec(path: &[u8], _args: &[[usize; 2]]) -> Result<usize> {
//TODO: Use args
//TODO: Unmap previous mappings
//TODO: Drop data vec
println!("Exec {}", unsafe { str::from_utf8_unchecked(path) });
let file = syscall::open(path, 0)?;
let mut data = vec![];