Bug fixes for fcntl and o_cloexec
Add fcntl to schemes Fix debug: hang
This commit is contained in:
@@ -239,7 +239,7 @@ impl Context {
|
||||
pub fn get_file(&self, i: FileHandle) -> Option<File> {
|
||||
let files = self.files.lock();
|
||||
if i.into() < files.len() {
|
||||
files[i.into()]
|
||||
files[i.into()].clone()
|
||||
} else {
|
||||
None
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ use scheme::SchemeId;
|
||||
|
||||
/// A file
|
||||
//TODO: Close on exec
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct File {
|
||||
/// The scheme that this file refers to
|
||||
pub scheme: SchemeId,
|
||||
|
||||
@@ -5,7 +5,7 @@ use context;
|
||||
use device::serial::COM1;
|
||||
use scheme::*;
|
||||
use sync::WaitQueue;
|
||||
use syscall::flag::EVENT_READ;
|
||||
use syscall::flag::{EVENT_READ, F_GETFL, F_SETFL, O_ACCMODE, O_NONBLOCK};
|
||||
use syscall::scheme::Scheme;
|
||||
|
||||
pub static DEBUG_SCHEME_ID: AtomicSchemeId = ATOMIC_SCHEMEID_INIT;
|
||||
@@ -24,62 +24,127 @@ pub fn debug_input(b: u8) {
|
||||
context::event::trigger(DEBUG_SCHEME_ID.load(Ordering::SeqCst), 0, EVENT_READ, len);
|
||||
}
|
||||
|
||||
pub struct DebugScheme;
|
||||
pub struct DebugScheme {
|
||||
next_id: AtomicUsize,
|
||||
handles: RwLock<BTreeMap<usize, usize>>
|
||||
}
|
||||
|
||||
impl DebugScheme {
|
||||
pub fn new(scheme_id: SchemeId) -> DebugScheme {
|
||||
DEBUG_SCHEME_ID.store(scheme_id, Ordering::SeqCst);
|
||||
DebugScheme
|
||||
DebugScheme {
|
||||
next_id: AtomicUsize::new(0),
|
||||
handles: RwLock::new(BTreeMap::new())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Scheme for DebugScheme {
|
||||
fn open(&self, _path: &[u8], _flags: usize, _uid: u32, _gid: u32) -> Result<usize> {
|
||||
Ok(0)
|
||||
fn open(&self, _path: &[u8], flags: usize, _uid: u32, _gid: u32) -> Result<usize> {
|
||||
let id = self.next_id.fetch_add(1, Ordering::SeqCst);
|
||||
self.handles.write().insert(id, flags & ! O_ACCMODE);
|
||||
|
||||
Ok(id)
|
||||
}
|
||||
|
||||
fn dup(&self, _file: usize, _buf: &[u8]) -> Result<usize> {
|
||||
Ok(0)
|
||||
fn dup(&self, id: usize, _buf: &[u8]) -> Result<usize> {
|
||||
let flags = {
|
||||
let handles = self.handles.read();
|
||||
*handles.get(&id).ok_or(Error::new(EBADF))?
|
||||
};
|
||||
|
||||
let new_id = self.next_id.fetch_add(1, Ordering::SeqCst);
|
||||
self.handles.write().insert(new_id, flags);
|
||||
|
||||
Ok(new_id)
|
||||
}
|
||||
|
||||
/// Read the file `number` into the `buffer`
|
||||
///
|
||||
/// Returns the number of bytes read
|
||||
fn read(&self, _file: usize, buf: &mut [u8]) -> Result<usize> {
|
||||
Ok(INPUT.call_once(init_input).receive_into(buf, true))
|
||||
fn read(&self, id: usize, buf: &mut [u8]) -> Result<usize> {
|
||||
let flags = {
|
||||
let handles = self.handles.read();
|
||||
*handles.get(&id).ok_or(Error::new(EBADF))?
|
||||
};
|
||||
|
||||
Ok(INPUT.call_once(init_input).receive_into(buf, flags & O_NONBLOCK != O_NONBLOCK))
|
||||
}
|
||||
|
||||
/// Write the `buffer` to the `file`
|
||||
///
|
||||
/// Returns the number of bytes written
|
||||
fn write(&self, _file: usize, buffer: &[u8]) -> Result<usize> {
|
||||
fn write(&self, id: usize, buffer: &[u8]) -> Result<usize> {
|
||||
let _flags = {
|
||||
let handles = self.handles.read();
|
||||
*handles.get(&id).ok_or(Error::new(EBADF))?
|
||||
};
|
||||
|
||||
let mut com = COM1.lock();
|
||||
for &byte in buffer.iter() {
|
||||
com.send(byte);
|
||||
}
|
||||
|
||||
Ok(buffer.len())
|
||||
}
|
||||
|
||||
fn fevent(&self, _file: usize, _flags: usize) -> Result<usize> {
|
||||
fn fcntl(&self, id: usize, cmd: usize, arg: usize) -> Result<usize> {
|
||||
let mut handles = self.handles.write();
|
||||
if let Some(flags) = handles.get_mut(&id) {
|
||||
match cmd {
|
||||
F_GETFL => Ok(*flags),
|
||||
F_SETFL => {
|
||||
*flags = arg & ! O_ACCMODE;
|
||||
Ok(0)
|
||||
},
|
||||
_ => Err(Error::new(EINVAL))
|
||||
}
|
||||
} else {
|
||||
Err(Error::new(EBADF))
|
||||
}
|
||||
}
|
||||
|
||||
fn fevent(&self, id: usize, _flags: usize) -> Result<usize> {
|
||||
let _flags = {
|
||||
let handles = self.handles.read();
|
||||
*handles.get(&id).ok_or(Error::new(EBADF))?
|
||||
};
|
||||
|
||||
Ok(0)
|
||||
}
|
||||
|
||||
fn fpath(&self, _id: usize, buf: &mut [u8]) -> Result<usize> {
|
||||
fn fpath(&self, id: usize, buf: &mut [u8]) -> Result<usize> {
|
||||
let _flags = {
|
||||
let handles = self.handles.read();
|
||||
*handles.get(&id).ok_or(Error::new(EBADF))?
|
||||
};
|
||||
|
||||
let mut i = 0;
|
||||
let scheme_path = b"debug:";
|
||||
while i < buf.len() && i < scheme_path.len() {
|
||||
buf[i] = scheme_path[i];
|
||||
i += 1;
|
||||
}
|
||||
|
||||
Ok(i)
|
||||
}
|
||||
|
||||
fn fsync(&self, _file: usize) -> Result<usize> {
|
||||
fn fsync(&self, id: usize) -> Result<usize> {
|
||||
let _flags = {
|
||||
let handles = self.handles.read();
|
||||
*handles.get(&id).ok_or(Error::new(EBADF))?
|
||||
};
|
||||
|
||||
Ok(0)
|
||||
}
|
||||
|
||||
/// Close the file `number`
|
||||
fn close(&self, _file: usize) -> Result<usize> {
|
||||
fn close(&self, id: usize) -> Result<usize> {
|
||||
let _flags = {
|
||||
let mut handles = self.handles.write();
|
||||
handles.remove(&id).ok_or(Error::new(EBADF))?
|
||||
};
|
||||
|
||||
Ok(0)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -154,7 +154,17 @@ impl Scheme for EnvScheme {
|
||||
Ok(handle.seek)
|
||||
}
|
||||
|
||||
fn fcntl(&self, id: usize, _cmd: usize, _arg: usize) -> Result<usize> {
|
||||
let handles = self.handles.read();
|
||||
let _handle = handles.get(&id).ok_or(Error::new(EBADF))?;
|
||||
|
||||
Ok(0)
|
||||
}
|
||||
|
||||
fn fpath(&self, id: usize, buf: &mut [u8]) -> Result<usize> {
|
||||
let handles = self.handles.read();
|
||||
let _handle = handles.get(&id).ok_or(Error::new(EBADF))?;
|
||||
|
||||
let mut i = 0;
|
||||
//TODO: Get env name
|
||||
let scheme_path = b"env:";
|
||||
|
||||
@@ -62,6 +62,12 @@ impl Scheme for EventScheme {
|
||||
Ok(handle.receive_into(event_buf, true) * mem::size_of::<Event>())
|
||||
}
|
||||
|
||||
fn fcntl(&self, id: usize, _cmd: usize, _arg: usize) -> Result<usize> {
|
||||
let handles = self.handles.read();
|
||||
let handle_weak = handles.get(&id).ok_or(Error::new(EBADF))?;
|
||||
handle_weak.upgrade().ok_or(Error::new(EBADF)).and(Ok(0))
|
||||
}
|
||||
|
||||
fn fpath(&self, _id: usize, buf: &mut [u8]) -> Result<usize> {
|
||||
let mut i = 0;
|
||||
let scheme_path = b"event:";
|
||||
|
||||
@@ -5,7 +5,7 @@ use spin::RwLock;
|
||||
|
||||
use syscall::data::Stat;
|
||||
use syscall::error::*;
|
||||
use syscall::flag::{O_CLOEXEC, MODE_DIR, MODE_FILE, SEEK_SET, SEEK_CUR, SEEK_END};
|
||||
use syscall::flag::{MODE_DIR, MODE_FILE, SEEK_SET, SEEK_CUR, SEEK_END};
|
||||
use syscall::scheme::Scheme;
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -112,6 +112,13 @@ impl Scheme for InitFsScheme {
|
||||
Ok(handle.seek)
|
||||
}
|
||||
|
||||
fn fcntl(&self, id: usize, _cmd: usize, _arg: usize) -> Result<usize> {
|
||||
let handles = self.handles.read();
|
||||
let _handle = handles.get(&id).ok_or(Error::new(EBADF))?;
|
||||
|
||||
Ok(0)
|
||||
}
|
||||
|
||||
fn fpath(&self, id: usize, buf: &mut [u8]) -> Result<usize> {
|
||||
let handles = self.handles.read();
|
||||
let handle = handles.get(&id).ok_or(Error::new(EBADF))?;
|
||||
@@ -146,7 +153,9 @@ impl Scheme for InitFsScheme {
|
||||
Ok(0)
|
||||
}
|
||||
|
||||
fn fsync(&self, _id: usize) -> Result<usize> {
|
||||
fn fsync(&self, id: usize) -> Result<usize> {
|
||||
let handles = self.handles.read();
|
||||
let _handle = handles.get(&id).ok_or(Error::new(EBADF))?;
|
||||
Ok(0)
|
||||
}
|
||||
|
||||
|
||||
@@ -87,6 +87,10 @@ impl Scheme for IrqScheme {
|
||||
}
|
||||
}
|
||||
|
||||
fn fcntl(&self, _id: usize, _cmd: usize, _arg: usize) -> Result<usize> {
|
||||
Ok(0)
|
||||
}
|
||||
|
||||
fn fevent(&self, file: usize, _flags: usize) -> Result<usize> {
|
||||
Ok(file)
|
||||
}
|
||||
|
||||
@@ -112,6 +112,13 @@ impl Scheme for DiskScheme {
|
||||
Ok(handle.seek)
|
||||
}
|
||||
|
||||
fn fcntl(&self, _id: usize, _cmd: usize, _arg: usize) -> Result<usize> {
|
||||
let handles = self.handles.read();
|
||||
let _handle = handles.get(&id).ok_or(Error::new(EBADF))?;
|
||||
|
||||
Ok(0)
|
||||
}
|
||||
|
||||
fn fpath(&self, id: usize, buf: &mut [u8]) -> Result<usize> {
|
||||
let handles = self.handles.read();
|
||||
let handle = handles.get(&id).ok_or(Error::new(EBADF))?;
|
||||
|
||||
@@ -23,7 +23,11 @@ impl Scheme for MemoryScheme {
|
||||
Ok(0)
|
||||
}
|
||||
|
||||
fn fpath(&self, id: usize, buf: &mut [u8]) -> Result<usize> {
|
||||
fn fcntl(&self, _id: usize, _cmd: usize, _arg: usize) -> Result<usize> {
|
||||
Ok(0)
|
||||
}
|
||||
|
||||
fn fpath(&self, _id: usize, buf: &mut [u8]) -> Result<usize> {
|
||||
let mut i = 0;
|
||||
let scheme_path = b"memory:";
|
||||
while i < buf.len() && i < scheme_path.len() {
|
||||
|
||||
@@ -26,6 +26,10 @@ impl Scheme for NullScheme {
|
||||
Ok(buffer.len())
|
||||
}
|
||||
|
||||
fn fcntl(&self, _id: usize, _cmd: usize, _arg: usize) -> Result<usize> {
|
||||
Ok(0)
|
||||
}
|
||||
|
||||
fn fpath(&self, _id: usize, buf: &mut [u8]) -> Result<usize> {
|
||||
let mut i = 0;
|
||||
let scheme_path = b"null:";
|
||||
|
||||
@@ -112,7 +112,7 @@ impl Scheme for PipeScheme {
|
||||
Err(Error::new(EBADF))
|
||||
}
|
||||
|
||||
fn fpath(&self, id: usize, buf: &mut [u8]) -> Result<usize> {
|
||||
fn fpath(&self, _id: usize, buf: &mut [u8]) -> Result<usize> {
|
||||
let mut i = 0;
|
||||
let scheme_path = b"pipe:";
|
||||
while i < buf.len() && i < scheme_path.len() {
|
||||
|
||||
@@ -28,7 +28,7 @@ pub fn resource() -> Result<Vec<u8>> {
|
||||
for (fd, f) in row.2.iter().enumerate() {
|
||||
let file = match *f {
|
||||
None => continue,
|
||||
Some(file) => file
|
||||
Some(ref file) => file.clone()
|
||||
};
|
||||
|
||||
let scheme = {
|
||||
|
||||
@@ -14,6 +14,7 @@ mod cpu;
|
||||
mod exe;
|
||||
mod iostat;
|
||||
mod scheme;
|
||||
mod scheme_num;
|
||||
//mod interrupt;
|
||||
//mod log;
|
||||
//mod test;
|
||||
@@ -43,6 +44,7 @@ impl SysScheme {
|
||||
files.insert(b"exe", Box::new(move || exe::resource()));
|
||||
files.insert(b"iostat", Box::new(move || iostat::resource()));
|
||||
files.insert(b"scheme", Box::new(move || scheme::resource()));
|
||||
files.insert(b"scheme_num", Box::new(move || scheme_num::resource()));
|
||||
//files.insert(b"interrupt", Box::new(move || interrupt::resource()));
|
||||
//files.insert(b"log", Box::new(move || log::resource()));
|
||||
//files.insert(b"test", Box::new(move || test::resource()));
|
||||
|
||||
@@ -15,7 +15,7 @@ pub fn resource() -> Result<Vec<u8>> {
|
||||
let mut data = Vec::new();
|
||||
|
||||
let schemes = scheme::schemes();
|
||||
for (name, _scheme_lock) in schemes.iter_name(scheme_ns) {
|
||||
for (name, _scheme_id) in schemes.iter_name(scheme_ns) {
|
||||
data.extend_from_slice(name);
|
||||
data.push(b'\n');
|
||||
}
|
||||
|
||||
25
src/scheme/sys/scheme_num.rs
Normal file
25
src/scheme/sys/scheme_num.rs
Normal file
@@ -0,0 +1,25 @@
|
||||
use collections::Vec;
|
||||
|
||||
use context;
|
||||
use scheme;
|
||||
use syscall::error::{Error, ESRCH, Result};
|
||||
|
||||
pub fn resource() -> Result<Vec<u8>> {
|
||||
let scheme_ns = {
|
||||
let contexts = context::contexts();
|
||||
let context_lock = contexts.current().ok_or(Error::new(ESRCH))?;
|
||||
let context = context_lock.read();
|
||||
context.ens
|
||||
};
|
||||
|
||||
let mut data = Vec::new();
|
||||
|
||||
let schemes = scheme::schemes();
|
||||
for (name, &scheme_id) in schemes.iter_name(scheme_ns) {
|
||||
data.extend_from_slice(format!("{:>4}: ", scheme_id.into()).as_bytes());
|
||||
data.extend_from_slice(name);
|
||||
data.push(b'\n');
|
||||
}
|
||||
|
||||
Ok(data)
|
||||
}
|
||||
@@ -97,6 +97,10 @@ impl Scheme for TimeScheme {
|
||||
Ok(i * mem::size_of::<TimeSpec>())
|
||||
}
|
||||
|
||||
fn fcntl(&self, _id: usize, _cmd: usize, _arg: usize) -> Result<usize> {
|
||||
Ok(0)
|
||||
}
|
||||
|
||||
fn fevent(&self, id: usize, _flags: usize) -> Result<usize> {
|
||||
let handles = self.handles.read();
|
||||
handles.get(&id).ok_or(Error::new(EBADF)).and(Ok(id))
|
||||
|
||||
@@ -31,6 +31,10 @@ impl Scheme for ZeroScheme {
|
||||
Ok(buffer.len())
|
||||
}
|
||||
|
||||
fn fcntl(&self, _id: usize, _cmd: usize, _arg: usize) -> Result<usize> {
|
||||
Ok(0)
|
||||
}
|
||||
|
||||
fn fpath(&self, _id: usize, buf: &mut [u8]) -> Result<usize> {
|
||||
let mut i = 0;
|
||||
let scheme_path = b"zero:";
|
||||
|
||||
@@ -6,7 +6,7 @@ use scheme::{self, FileHandle};
|
||||
use syscall;
|
||||
use syscall::data::{Packet, Stat};
|
||||
use syscall::error::*;
|
||||
use syscall::flag::{F_SETFL, O_ACCMODE, O_RDONLY, O_WRONLY, MODE_DIR, MODE_FILE};
|
||||
use syscall::flag::{F_GETFL, F_SETFL, O_ACCMODE, O_RDONLY, O_WRONLY, MODE_DIR, MODE_FILE};
|
||||
|
||||
pub fn file_op(a: usize, fd: FileHandle, c: usize, d: usize) -> Result<usize> {
|
||||
let (file, pid, uid, gid) = {
|
||||
@@ -303,7 +303,7 @@ pub fn dup2(fd: FileHandle, new_fd: FileHandle, buf: &[u8]) -> Result<FileHandle
|
||||
}
|
||||
}
|
||||
|
||||
// File descriptor controls
|
||||
/// File descriptor controls
|
||||
pub fn fcntl(fd: FileHandle, cmd: usize, arg: usize) -> Result<usize> {
|
||||
let file = {
|
||||
let contexts = context::contexts();
|
||||
@@ -313,6 +313,7 @@ pub fn fcntl(fd: FileHandle, cmd: usize, arg: usize) -> Result<usize> {
|
||||
file
|
||||
};
|
||||
|
||||
// Communicate fcntl with scheme
|
||||
let res = {
|
||||
let scheme = {
|
||||
let schemes = scheme::schemes();
|
||||
@@ -322,17 +323,29 @@ pub fn fcntl(fd: FileHandle, cmd: usize, arg: usize) -> Result<usize> {
|
||||
scheme.fcntl(file.number, cmd, arg)?
|
||||
};
|
||||
|
||||
if cmd == F_SETFL {
|
||||
// Perform kernel operation if scheme agrees
|
||||
{
|
||||
let contexts = context::contexts();
|
||||
let context_lock = contexts.current().ok_or(Error::new(ESRCH))?;
|
||||
let context = context_lock.read();
|
||||
let mut files = context.files.lock();
|
||||
let mut file = files.get_mut(fd.into()).ok_or(Error::new(EBADF))?.ok_or(Error::new(EBADF))?;
|
||||
let accmode = file.flags & O_ACCMODE;
|
||||
file.flags = accmode | arg & !O_ACCMODE;
|
||||
match *files.get_mut(fd.into()).ok_or(Error::new(EBADF))? {
|
||||
Some(ref mut file) => match cmd {
|
||||
F_GETFL => {
|
||||
Ok(file.flags)
|
||||
},
|
||||
F_SETFL => {
|
||||
let new_flags = (file.flags & O_ACCMODE) | (arg & ! O_ACCMODE);
|
||||
file.flags = new_flags;
|
||||
Ok(0)
|
||||
},
|
||||
_ => {
|
||||
Err(Error::new(EINVAL))
|
||||
}
|
||||
},
|
||||
None => Err(Error::new(EBADF))
|
||||
}
|
||||
}
|
||||
|
||||
Ok(res)
|
||||
}
|
||||
|
||||
/// Register events for file
|
||||
@@ -342,12 +355,16 @@ pub fn fevent(fd: FileHandle, flags: usize) -> Result<usize> {
|
||||
let context_lock = contexts.current().ok_or(Error::new(ESRCH))?;
|
||||
let context = context_lock.read();
|
||||
let mut files = context.files.lock();
|
||||
let mut file = files.get_mut(fd.into()).ok_or(Error::new(EBADF))?.ok_or(Error::new(EBADF))?;
|
||||
if let Some(event_id) = file.event.take() {
|
||||
println!("{:?}: {:?}:{}: events already registered: {}", fd, file.scheme, file.number, event_id);
|
||||
context::event::unregister(fd, file.scheme, event_id);
|
||||
match *files.get_mut(fd.into()).ok_or(Error::new(EBADF))? {
|
||||
Some(ref mut file) => {
|
||||
if let Some(event_id) = file.event.take() {
|
||||
println!("{:?}: {:?}:{}: events already registered: {}", fd, file.scheme, file.number, event_id);
|
||||
context::event::unregister(fd, file.scheme, event_id);
|
||||
}
|
||||
file.clone()
|
||||
},
|
||||
None => return Err(Error::new(EBADF))
|
||||
}
|
||||
file.clone()
|
||||
};
|
||||
|
||||
let scheme = {
|
||||
@@ -361,8 +378,10 @@ pub fn fevent(fd: FileHandle, flags: usize) -> Result<usize> {
|
||||
let context_lock = contexts.current().ok_or(Error::new(ESRCH))?;
|
||||
let context = context_lock.read();
|
||||
let mut files = context.files.lock();
|
||||
let mut file = files.get_mut(fd.into()).ok_or(Error::new(EBADF))?.ok_or(Error::new(EBADF))?;
|
||||
file.event = Some(event_id);
|
||||
match *files.get_mut(fd.into()).ok_or(Error::new(EBADF))? {
|
||||
Some(ref mut file) => file.event = Some(event_id),
|
||||
None => return Err(Error::new(EBADF)),
|
||||
}
|
||||
}
|
||||
context::event::register(fd, file.scheme, event_id);
|
||||
Ok(0)
|
||||
|
||||
@@ -253,15 +253,14 @@ pub fn clone(flags: usize, stack_base: usize) -> Result<ContextId> {
|
||||
// This has to be done outside the context lock to prevent deadlocks
|
||||
if flags & CLONE_FILES == 0 {
|
||||
for (_fd, mut file_option) in files.lock().iter_mut().enumerate() {
|
||||
let new_file_option = if let Some(file) = *file_option {
|
||||
let new_file_option = if let Some(ref file) = *file_option {
|
||||
let result = {
|
||||
let scheme = {
|
||||
let schemes = scheme::schemes();
|
||||
let scheme = schemes.get(file.scheme).ok_or(Error::new(EBADF))?;
|
||||
scheme.clone()
|
||||
};
|
||||
let result = scheme.dup(file.number, b"clone");
|
||||
result
|
||||
scheme.dup(file.number, b"clone")
|
||||
};
|
||||
match result {
|
||||
Ok(new_number) => {
|
||||
@@ -731,7 +730,7 @@ pub fn exec(path: &[u8], arg_ptrs: &[[usize; 2]]) -> Result<usize> {
|
||||
|
||||
// Duplicate current files using b"exec", close previous
|
||||
for (fd, mut file_option) in files.lock().iter_mut().enumerate() {
|
||||
let new_file_option = if let Some(file) = *file_option {
|
||||
let new_file_option = if let Some(ref file) = *file_option {
|
||||
// Duplicate
|
||||
let result = {
|
||||
if file.flags & O_CLOEXEC == O_CLOEXEC {
|
||||
@@ -742,8 +741,7 @@ pub fn exec(path: &[u8], arg_ptrs: &[[usize; 2]]) -> Result<usize> {
|
||||
schemes.get(file.scheme).map(|scheme| scheme.clone())
|
||||
};
|
||||
if let Some(scheme) = scheme_option {
|
||||
let result = scheme.dup(file.number, b"exec");
|
||||
result
|
||||
scheme.dup(file.number, b"exec")
|
||||
} else {
|
||||
Err(Error::new(EBADF))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user