2016-10-07 02:46:24 +02:00
|
|
|
use alloc::arc::{Arc, Weak};
|
Orbital (#16)
* Port previous ethernet scheme
* Add ipd
* Fix initfs rebuilds, use QEMU user networking addresses in ipd
* Add tcp/udp, netutils, dns, and network config
* Add fsync to network driver
* Add dns, router, subnet by default
* Fix e1000 driver. Make ethernet and IP non-blocking to avoid deadlocks
* Add orbital server, WIP
* Add futex
* Add orbutils and orbital
* Update libstd, orbutils, and orbital
Move ANSI key encoding to vesad
* Add orbital assets
* Update orbital
* Update to add login manager
* Add blocking primitives, block for most things except waitpid, update orbital
* Wait in waitpid and IRQ, improvements for other waits
* Fevent in root scheme
* WIP: Switch to using fevent
* Reorganize
* Event based e1000d driver
* Superuser-only access to some network schemes, display, and disk
* Superuser root and irq schemes
* Fix orbital
2016-10-14 01:21:42 +02:00
|
|
|
use collections::BTreeMap;
|
2016-10-07 02:46:24 +02:00
|
|
|
use core::sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT, Ordering};
|
Orbital (#16)
* Port previous ethernet scheme
* Add ipd
* Fix initfs rebuilds, use QEMU user networking addresses in ipd
* Add tcp/udp, netutils, dns, and network config
* Add fsync to network driver
* Add dns, router, subnet by default
* Fix e1000 driver. Make ethernet and IP non-blocking to avoid deadlocks
* Add orbital server, WIP
* Add futex
* Add orbutils and orbital
* Update libstd, orbutils, and orbital
Move ANSI key encoding to vesad
* Add orbital assets
* Update orbital
* Update to add login manager
* Add blocking primitives, block for most things except waitpid, update orbital
* Wait in waitpid and IRQ, improvements for other waits
* Fevent in root scheme
* WIP: Switch to using fevent
* Reorganize
* Event based e1000d driver
* Superuser-only access to some network schemes, display, and disk
* Superuser root and irq schemes
* Fix orbital
2016-10-14 01:21:42 +02:00
|
|
|
use spin::{Once, RwLock, RwLockReadGuard, RwLockWriteGuard};
|
2016-10-07 02:46:24 +02:00
|
|
|
|
Orbital (#16)
* Port previous ethernet scheme
* Add ipd
* Fix initfs rebuilds, use QEMU user networking addresses in ipd
* Add tcp/udp, netutils, dns, and network config
* Add fsync to network driver
* Add dns, router, subnet by default
* Fix e1000 driver. Make ethernet and IP non-blocking to avoid deadlocks
* Add orbital server, WIP
* Add futex
* Add orbutils and orbital
* Update libstd, orbutils, and orbital
Move ANSI key encoding to vesad
* Add orbital assets
* Update orbital
* Update to add login manager
* Add blocking primitives, block for most things except waitpid, update orbital
* Wait in waitpid and IRQ, improvements for other waits
* Fevent in root scheme
* WIP: Switch to using fevent
* Reorganize
* Event based e1000d driver
* Superuser-only access to some network schemes, display, and disk
* Superuser root and irq schemes
* Fix orbital
2016-10-14 01:21:42 +02:00
|
|
|
use sync::WaitQueue;
|
2016-10-07 02:46:24 +02:00
|
|
|
use syscall::error::{Error, Result, EBADF, EPIPE};
|
|
|
|
use syscall::scheme::Scheme;
|
|
|
|
|
|
|
|
/// Pipes list
|
|
|
|
pub static PIPE_SCHEME_ID: AtomicUsize = ATOMIC_USIZE_INIT;
|
|
|
|
static PIPE_NEXT_ID: AtomicUsize = ATOMIC_USIZE_INIT;
|
|
|
|
static PIPES: Once<RwLock<(BTreeMap<usize, PipeRead>, BTreeMap<usize, PipeWrite>)>> = Once::new();
|
|
|
|
|
|
|
|
/// Initialize pipes, called if needed
|
|
|
|
fn init_pipes() -> RwLock<(BTreeMap<usize, PipeRead>, BTreeMap<usize, PipeWrite>)> {
|
|
|
|
RwLock::new((BTreeMap::new(), BTreeMap::new()))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the global pipes list, const
|
|
|
|
fn pipes() -> RwLockReadGuard<'static, (BTreeMap<usize, PipeRead>, BTreeMap<usize, PipeWrite>)> {
|
|
|
|
PIPES.call_once(init_pipes).read()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the global schemes list, mutable
|
|
|
|
fn pipes_mut() -> RwLockWriteGuard<'static, (BTreeMap<usize, PipeRead>, BTreeMap<usize, PipeWrite>)> {
|
|
|
|
PIPES.call_once(init_pipes).write()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn pipe(_flags: usize) -> (usize, usize) {
|
|
|
|
let mut pipes = pipes_mut();
|
|
|
|
let read_id = PIPE_NEXT_ID.fetch_add(1, Ordering::SeqCst);
|
|
|
|
let read = PipeRead::new();
|
|
|
|
let write_id = PIPE_NEXT_ID.fetch_add(1, Ordering::SeqCst);
|
|
|
|
let write = PipeWrite::new(&read);
|
|
|
|
pipes.0.insert(read_id, read);
|
|
|
|
pipes.1.insert(write_id, write);
|
|
|
|
(read_id, write_id)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct PipeScheme;
|
|
|
|
|
|
|
|
impl Scheme for PipeScheme {
|
|
|
|
fn dup(&self, id: usize) -> Result<usize> {
|
|
|
|
let mut pipes = pipes_mut();
|
|
|
|
|
|
|
|
let read_option = pipes.0.get(&id).map(|pipe| pipe.clone());
|
|
|
|
if let Some(pipe) = read_option {
|
|
|
|
let pipe_id = PIPE_NEXT_ID.fetch_add(1, Ordering::SeqCst);
|
|
|
|
pipes.0.insert(pipe_id, pipe);
|
|
|
|
return Ok(pipe_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
let write_option = pipes.1.get(&id).map(|pipe| pipe.clone());
|
|
|
|
if let Some(pipe) = write_option {
|
|
|
|
let pipe_id = PIPE_NEXT_ID.fetch_add(1, Ordering::SeqCst);
|
|
|
|
pipes.1.insert(pipe_id, pipe);
|
|
|
|
return Ok(pipe_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
Err(Error::new(EBADF))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn read(&self, id: usize, buf: &mut [u8]) -> Result<usize> {
|
|
|
|
let pipe_option = {
|
|
|
|
let pipes = pipes();
|
|
|
|
pipes.0.get(&id).map(|pipe| pipe.clone())
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(pipe) = pipe_option {
|
|
|
|
pipe.read(buf)
|
|
|
|
} else {
|
|
|
|
Err(Error::new(EBADF))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn write(&self, id: usize, buf: &[u8]) -> Result<usize> {
|
|
|
|
let pipe_option = {
|
|
|
|
let pipes = pipes();
|
|
|
|
pipes.1.get(&id).map(|pipe| pipe.clone())
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(pipe) = pipe_option {
|
|
|
|
pipe.write(buf)
|
|
|
|
} else {
|
|
|
|
Err(Error::new(EBADF))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fsync(&self, _id: usize) -> Result<usize> {
|
|
|
|
Ok(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn close(&self, id: usize) -> Result<usize> {
|
|
|
|
let mut pipes = pipes_mut();
|
|
|
|
|
|
|
|
drop(pipes.0.remove(&id));
|
|
|
|
drop(pipes.1.remove(&id));
|
|
|
|
|
|
|
|
Ok(0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Read side of a pipe
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct PipeRead {
|
Orbital (#16)
* Port previous ethernet scheme
* Add ipd
* Fix initfs rebuilds, use QEMU user networking addresses in ipd
* Add tcp/udp, netutils, dns, and network config
* Add fsync to network driver
* Add dns, router, subnet by default
* Fix e1000 driver. Make ethernet and IP non-blocking to avoid deadlocks
* Add orbital server, WIP
* Add futex
* Add orbutils and orbital
* Update libstd, orbutils, and orbital
Move ANSI key encoding to vesad
* Add orbital assets
* Update orbital
* Update to add login manager
* Add blocking primitives, block for most things except waitpid, update orbital
* Wait in waitpid and IRQ, improvements for other waits
* Fevent in root scheme
* WIP: Switch to using fevent
* Reorganize
* Event based e1000d driver
* Superuser-only access to some network schemes, display, and disk
* Superuser root and irq schemes
* Fix orbital
2016-10-14 01:21:42 +02:00
|
|
|
vec: Arc<WaitQueue<u8>>
|
2016-10-07 02:46:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl PipeRead {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
PipeRead {
|
Orbital (#16)
* Port previous ethernet scheme
* Add ipd
* Fix initfs rebuilds, use QEMU user networking addresses in ipd
* Add tcp/udp, netutils, dns, and network config
* Add fsync to network driver
* Add dns, router, subnet by default
* Fix e1000 driver. Make ethernet and IP non-blocking to avoid deadlocks
* Add orbital server, WIP
* Add futex
* Add orbutils and orbital
* Update libstd, orbutils, and orbital
Move ANSI key encoding to vesad
* Add orbital assets
* Update orbital
* Update to add login manager
* Add blocking primitives, block for most things except waitpid, update orbital
* Wait in waitpid and IRQ, improvements for other waits
* Fevent in root scheme
* WIP: Switch to using fevent
* Reorganize
* Event based e1000d driver
* Superuser-only access to some network schemes, display, and disk
* Superuser root and irq schemes
* Fix orbital
2016-10-14 01:21:42 +02:00
|
|
|
vec: Arc::new(WaitQueue::new())
|
2016-10-07 02:46:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn read(&self, buf: &mut [u8]) -> Result<usize> {
|
Orbital (#16)
* Port previous ethernet scheme
* Add ipd
* Fix initfs rebuilds, use QEMU user networking addresses in ipd
* Add tcp/udp, netutils, dns, and network config
* Add fsync to network driver
* Add dns, router, subnet by default
* Fix e1000 driver. Make ethernet and IP non-blocking to avoid deadlocks
* Add orbital server, WIP
* Add futex
* Add orbutils and orbital
* Update libstd, orbutils, and orbital
Move ANSI key encoding to vesad
* Add orbital assets
* Update orbital
* Update to add login manager
* Add blocking primitives, block for most things except waitpid, update orbital
* Wait in waitpid and IRQ, improvements for other waits
* Fevent in root scheme
* WIP: Switch to using fevent
* Reorganize
* Event based e1000d driver
* Superuser-only access to some network schemes, display, and disk
* Superuser root and irq schemes
* Fix orbital
2016-10-14 01:21:42 +02:00
|
|
|
if buf.is_empty() || (Arc::weak_count(&self.vec) == 0 && self.vec.is_empty()) {
|
2016-10-07 02:46:24 +02:00
|
|
|
Ok(0)
|
|
|
|
} else {
|
Orbital (#16)
* Port previous ethernet scheme
* Add ipd
* Fix initfs rebuilds, use QEMU user networking addresses in ipd
* Add tcp/udp, netutils, dns, and network config
* Add fsync to network driver
* Add dns, router, subnet by default
* Fix e1000 driver. Make ethernet and IP non-blocking to avoid deadlocks
* Add orbital server, WIP
* Add futex
* Add orbutils and orbital
* Update libstd, orbutils, and orbital
Move ANSI key encoding to vesad
* Add orbital assets
* Update orbital
* Update to add login manager
* Add blocking primitives, block for most things except waitpid, update orbital
* Wait in waitpid and IRQ, improvements for other waits
* Fevent in root scheme
* WIP: Switch to using fevent
* Reorganize
* Event based e1000d driver
* Superuser-only access to some network schemes, display, and disk
* Superuser root and irq schemes
* Fix orbital
2016-10-14 01:21:42 +02:00
|
|
|
Ok(self.vec.receive_into(buf))
|
2016-10-07 02:46:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Read side of a pipe
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct PipeWrite {
|
Orbital (#16)
* Port previous ethernet scheme
* Add ipd
* Fix initfs rebuilds, use QEMU user networking addresses in ipd
* Add tcp/udp, netutils, dns, and network config
* Add fsync to network driver
* Add dns, router, subnet by default
* Fix e1000 driver. Make ethernet and IP non-blocking to avoid deadlocks
* Add orbital server, WIP
* Add futex
* Add orbutils and orbital
* Update libstd, orbutils, and orbital
Move ANSI key encoding to vesad
* Add orbital assets
* Update orbital
* Update to add login manager
* Add blocking primitives, block for most things except waitpid, update orbital
* Wait in waitpid and IRQ, improvements for other waits
* Fevent in root scheme
* WIP: Switch to using fevent
* Reorganize
* Event based e1000d driver
* Superuser-only access to some network schemes, display, and disk
* Superuser root and irq schemes
* Fix orbital
2016-10-14 01:21:42 +02:00
|
|
|
vec: Weak<WaitQueue<u8>>,
|
2016-10-07 02:46:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl PipeWrite {
|
|
|
|
pub fn new(read: &PipeRead) -> Self {
|
|
|
|
PipeWrite {
|
|
|
|
vec: Arc::downgrade(&read.vec),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn write(&self, buf: &[u8]) -> Result<usize> {
|
|
|
|
match self.vec.upgrade() {
|
|
|
|
Some(vec) => {
|
Orbital (#16)
* Port previous ethernet scheme
* Add ipd
* Fix initfs rebuilds, use QEMU user networking addresses in ipd
* Add tcp/udp, netutils, dns, and network config
* Add fsync to network driver
* Add dns, router, subnet by default
* Fix e1000 driver. Make ethernet and IP non-blocking to avoid deadlocks
* Add orbital server, WIP
* Add futex
* Add orbutils and orbital
* Update libstd, orbutils, and orbital
Move ANSI key encoding to vesad
* Add orbital assets
* Update orbital
* Update to add login manager
* Add blocking primitives, block for most things except waitpid, update orbital
* Wait in waitpid and IRQ, improvements for other waits
* Fevent in root scheme
* WIP: Switch to using fevent
* Reorganize
* Event based e1000d driver
* Superuser-only access to some network schemes, display, and disk
* Superuser root and irq schemes
* Fix orbital
2016-10-14 01:21:42 +02:00
|
|
|
vec.send_from(buf);
|
2016-10-07 02:46:24 +02:00
|
|
|
|
|
|
|
Ok(buf.len())
|
|
|
|
},
|
|
|
|
None => Err(Error::new(EPIPE))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|