Add pipe2

This commit is contained in:
Jeremy Soller 2016-10-06 18:46:24 -06:00
parent 9bb2d007c6
commit f28a128198
6 changed files with 254 additions and 24 deletions

View file

@ -1,4 +1,5 @@
//! Filesystem syscalls
use core::sync::atomic::Ordering;
use context;
use scheme;
@ -101,6 +102,34 @@ pub fn open(path: &[u8], flags: usize) -> Result<usize> {
}).ok_or(Error::new(EMFILE))
}
pub fn pipe2(fds: &mut [usize], flags: usize) -> Result<usize> {
if fds.len() >= 2 {
let scheme_id = ::scheme::pipe::PIPE_SCHEME_ID.load(Ordering::SeqCst);
let (read_id, write_id) = ::scheme::pipe::pipe(flags);
let contexts = context::contexts();
let context_lock = contexts.current().ok_or(Error::new(ESRCH))?;
let context = context_lock.read();
let read_fd = context.add_file(::context::file::File {
scheme: scheme_id,
number: read_id
}).ok_or(Error::new(EMFILE))?;
let write_fd = context.add_file(::context::file::File {
scheme: scheme_id,
number: write_id
}).ok_or(Error::new(EMFILE))?;
fds[0] = read_fd;
fds[1] = write_fd;
Ok(0)
} else {
Err(Error::new(EFAULT))
}
}
/// mkdir syscall
pub fn mkdir(path: &[u8], mode: u16) -> Result<usize> {
let (path_canon, uid, gid) = {

View file

@ -59,6 +59,7 @@ pub extern fn syscall(a: usize, b: usize, c: usize, d: usize, e: usize, f: usize
SYS_GETEGID => getegid(),
SYS_SETUID => setuid(b as u32),
SYS_SETGID => setgid(b as u32),
SYS_PIPE2 => pipe2(validate_slice_mut(b as *mut usize, 2)?, c),
SYS_PHYSALLOC => physalloc(b),
SYS_PHYSFREE => physfree(b, c),
SYS_PHYSMAP => physmap(b, c, d),
@ -72,7 +73,7 @@ pub extern fn syscall(a: usize, b: usize, c: usize, d: usize, e: usize, f: usize
let result = inner(a, b, c, d, e, f, stack);
if let Err(ref err) = result {
println!("{}, {}, {}, {}: {}", a, b, c, d, err);
println!("{}, {}, {}, {}: {}", a & 0xFFFF, b, c, d, err);
}
Error::mux(result)

View file

@ -3,6 +3,7 @@ use alloc::arc::Arc;
use alloc::boxed::Box;
use collections::{BTreeMap, Vec};
use core::mem;
use core::ops::DerefMut;
use core::str;
use spin::Mutex;
@ -397,31 +398,51 @@ pub fn clone(flags: usize, stack_base: usize) -> Result<usize> {
pub fn exit(status: usize) -> ! {
{
let contexts = context::contexts();
let (vfork, ppid) = {
let context_lock = contexts.current().expect("tried to exit without context");
let mut context = context_lock.write();
context.image.clear();
drop(context.heap.take());
drop(context.stack.take());
context.grants = Arc::new(Mutex::new(Vec::new()));
context.files = Arc::new(Mutex::new(Vec::new()));
context.status = context::Status::Exited(status);
let vfork = context.vfork;
context.vfork = false;
(vfork, context.ppid)
};
if vfork {
if let Some(context_lock) = contexts.get(ppid) {
let mut close_files = Vec::new();
{
let contexts = context::contexts();
let (vfork, ppid) = {
let context_lock = contexts.current().expect("tried to exit without context");
let mut context = context_lock.write();
if context.status == context::Status::Blocked {
context.status = context::Status::Runnable;
} else {
println!("{} not blocked for exit vfork unblock", ppid);
context.image.clear();
drop(context.heap.take());
drop(context.stack.take());
context.grants = Arc::new(Mutex::new(Vec::new()));
if Arc::strong_count(&context.files) == 1 {
mem::swap(context.files.lock().deref_mut(), &mut close_files);
}
context.files = Arc::new(Mutex::new(Vec::new()));
context.status = context::Status::Exited(status);
let vfork = context.vfork;
context.vfork = false;
(vfork, context.ppid)
};
if vfork {
if let Some(context_lock) = contexts.get(ppid) {
let mut context = context_lock.write();
if context.status == context::Status::Blocked {
context.status = context::Status::Runnable;
} else {
println!("{} not blocked for exit vfork unblock", ppid);
}
} else {
println!("{} not found for exit vfork unblock", ppid);
}
}
}
for (fd, file_option) in close_files.drain(..).enumerate() {
if let Some(file) = file_option {
context::event::unregister(fd, file.scheme, file.number);
let scheme_option = {
let schemes = scheme::schemes();
schemes.get(file.scheme).map(|scheme| scheme.clone())
};
if let Some(scheme) = scheme_option {
let _ = scheme.close(file.number);
}
} else {
println!("{} not found for exit vfork unblock", ppid);
}
}
}