|
|
@ -10,6 +10,8 @@ use crate::fs::*;
|
|
|
|
use crate::memory::MemorySet;
|
|
|
|
use crate::memory::MemorySet;
|
|
|
|
use crate::sync::Condvar;
|
|
|
|
use crate::sync::Condvar;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use bitvec::{BitSlice, BitVec, LittleEndian};
|
|
|
|
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
|
|
|
|
pub fn sys_read(fd: usize, base: *mut u8, len: usize) -> SysResult {
|
|
|
|
pub fn sys_read(fd: usize, base: *mut u8, len: usize) -> SysResult {
|
|
|
@ -160,30 +162,30 @@ pub fn sys_select(
|
|
|
|
loop {
|
|
|
|
loop {
|
|
|
|
let proc = process();
|
|
|
|
let proc = process();
|
|
|
|
let mut events = 0;
|
|
|
|
let mut events = 0;
|
|
|
|
for (fd, file_like) in proc.files.iter() {
|
|
|
|
for (&fd, file_like) in proc.files.iter() {
|
|
|
|
if *fd < nfds {
|
|
|
|
if fd < nfds {
|
|
|
|
match file_like {
|
|
|
|
match file_like {
|
|
|
|
FileLike::File(_) => {
|
|
|
|
FileLike::File(_) => {
|
|
|
|
// FIXME: assume it is stdin for now
|
|
|
|
// FIXME: assume it is stdin for now
|
|
|
|
if STDIN.can_read() {
|
|
|
|
if STDIN.can_read() {
|
|
|
|
if read_fds.is_set(*fd) {
|
|
|
|
if read_fds.contains(fd) {
|
|
|
|
read_fds.set(*fd);
|
|
|
|
read_fds.set(fd);
|
|
|
|
events = events + 1;
|
|
|
|
events = events + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
FileLike::Socket(socket) => {
|
|
|
|
FileLike::Socket(socket) => {
|
|
|
|
let (input, output, err) = socket.poll();
|
|
|
|
let (input, output, err) = socket.poll();
|
|
|
|
if err && err_fds.is_set(*fd) {
|
|
|
|
if err && err_fds.contains(fd) {
|
|
|
|
err_fds.set(*fd);
|
|
|
|
err_fds.set(fd);
|
|
|
|
events = events + 1;
|
|
|
|
events = events + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if input && read_fds.is_set(*fd) {
|
|
|
|
if input && read_fds.contains(fd) {
|
|
|
|
read_fds.set(*fd);
|
|
|
|
read_fds.set(fd);
|
|
|
|
events = events + 1;
|
|
|
|
events = events + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if output && write_fds.is_set(*fd) {
|
|
|
|
if output && write_fds.contains(fd) {
|
|
|
|
write_fds.set(*fd);
|
|
|
|
write_fds.set(fd);
|
|
|
|
events = events + 1;
|
|
|
|
events = events + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -1172,54 +1174,51 @@ const FD_PER_ITEM: usize = 8 * size_of::<u32>();
|
|
|
|
const MAX_FDSET_SIZE: usize = 1024 / FD_PER_ITEM;
|
|
|
|
const MAX_FDSET_SIZE: usize = 1024 / FD_PER_ITEM;
|
|
|
|
|
|
|
|
|
|
|
|
struct FdSet {
|
|
|
|
struct FdSet {
|
|
|
|
addr: *mut u32,
|
|
|
|
bitset: &'static mut BitSlice<LittleEndian, u32>,
|
|
|
|
nfds: usize,
|
|
|
|
origin: BitVec<LittleEndian, u32>,
|
|
|
|
saved: [u32; MAX_FDSET_SIZE],
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl FdSet {
|
|
|
|
impl FdSet {
|
|
|
|
/// Initialize a `FdSet` from pointer and number of fds
|
|
|
|
/// Initialize a `FdSet` from pointer and number of fds
|
|
|
|
/// Check if the array is large enough
|
|
|
|
/// Check if the array is large enough
|
|
|
|
fn new(vm: &MemorySet, addr: *mut u32, nfds: usize) -> Result<FdSet, SysError> {
|
|
|
|
fn new(vm: &MemorySet, addr: *mut u32, nfds: usize) -> Result<FdSet, SysError> {
|
|
|
|
let mut saved = [0u32; MAX_FDSET_SIZE];
|
|
|
|
if addr.is_null() {
|
|
|
|
if addr as usize != 0 {
|
|
|
|
Ok(FdSet {
|
|
|
|
|
|
|
|
bitset: BitSlice::empty_mut(),
|
|
|
|
|
|
|
|
origin: BitVec::new(),
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
} else {
|
|
|
|
let len = (nfds + FD_PER_ITEM - 1) / FD_PER_ITEM;
|
|
|
|
let len = (nfds + FD_PER_ITEM - 1) / FD_PER_ITEM;
|
|
|
|
vm.check_write_array(addr, len)?;
|
|
|
|
vm.check_write_array(addr, len)?;
|
|
|
|
if len > MAX_FDSET_SIZE {
|
|
|
|
if len > MAX_FDSET_SIZE {
|
|
|
|
return Err(SysError::EINVAL);
|
|
|
|
return Err(SysError::EINVAL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let slice = unsafe { slice::from_raw_parts_mut(addr, len) };
|
|
|
|
let slice = unsafe { slice::from_raw_parts_mut(addr, len) };
|
|
|
|
|
|
|
|
let bitset: &'static mut BitSlice<LittleEndian, u32> = slice.into();
|
|
|
|
|
|
|
|
|
|
|
|
// save the fdset, and clear it
|
|
|
|
// save the fdset, and clear it
|
|
|
|
for i in 0..len {
|
|
|
|
use alloc::prelude::ToOwned;
|
|
|
|
saved[i] = slice[i];
|
|
|
|
let origin = bitset.to_owned();
|
|
|
|
slice[i] = 0;
|
|
|
|
bitset.set_all(false);
|
|
|
|
}
|
|
|
|
Ok(FdSet { bitset, origin })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Ok(FdSet { addr, nfds, saved })
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Try to set fd in `FdSet`
|
|
|
|
/// Try to set fd in `FdSet`
|
|
|
|
/// Return true when `FdSet` is valid, and false when `FdSet` is bad (i.e. null pointer)
|
|
|
|
/// Return true when `FdSet` is valid, and false when `FdSet` is bad (i.e. null pointer)
|
|
|
|
/// Fd should be less than nfds
|
|
|
|
/// Fd should be less than nfds
|
|
|
|
fn set(&mut self, fd: usize) -> bool {
|
|
|
|
fn set(&mut self, fd: usize) -> bool {
|
|
|
|
if self.addr as usize != 0 {
|
|
|
|
if self.bitset.is_empty() {
|
|
|
|
assert!(fd < self.nfds);
|
|
|
|
return false;
|
|
|
|
unsafe {
|
|
|
|
|
|
|
|
*self.addr.add(fd / 8 / size_of::<u32>()) |= 1 << (fd % (8 * size_of::<u32>()));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
true
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
false
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.bitset.set(fd, true);
|
|
|
|
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Check to see fd is see in original `FdSet`
|
|
|
|
/// Check to see whether `fd` is in original `FdSet`
|
|
|
|
/// Fd should be less than nfds
|
|
|
|
/// Fd should be less than nfds
|
|
|
|
fn is_set(&mut self, fd: usize) -> bool {
|
|
|
|
fn contains(&self, fd: usize) -> bool {
|
|
|
|
assert!(fd < self.nfds);
|
|
|
|
self.origin[fd]
|
|
|
|
self.saved[fd / 8 / size_of::<u32>()] & (1 << (fd % (8 * size_of::<u32>()))) != 0
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|