From 28aaae53b99b36ad4fbf5c27d52659c1474bda85 Mon Sep 17 00:00:00 2001 From: Jiajie Chen Date: Fri, 3 May 2019 11:33:56 +0800 Subject: [PATCH] Refactor ioctl numbers and add FIOCLEX --- kernel/src/fs/file_like.rs | 17 ++++++++++++----- kernel/src/fs/ioctl.rs | 36 ++++++++++++++++++++++++++++++++++++ kernel/src/fs/mod.rs | 5 +++-- kernel/src/fs/stdio.rs | 29 ++--------------------------- 4 files changed, 53 insertions(+), 34 deletions(-) create mode 100644 kernel/src/fs/ioctl.rs diff --git a/kernel/src/fs/file_like.rs b/kernel/src/fs/file_like.rs index 9c4913f..dbbdbe4 100644 --- a/kernel/src/fs/file_like.rs +++ b/kernel/src/fs/file_like.rs @@ -5,6 +5,7 @@ use crate::net::Socket; use crate::syscall::{SysError, SysResult}; use alloc::boxed::Box; use rcore_fs::vfs::PollStatus; +use super::ioctl::*; // TODO: merge FileLike to FileHandle ? // TODO: fix dup and remove Clone @@ -30,13 +31,19 @@ impl FileLike { Ok(len) } pub fn ioctl(&mut self, request: usize, arg1: usize, arg2: usize, arg3: usize) -> SysResult { - match self { - FileLike::File(file) => file.io_control(request as u32, arg1)?, - FileLike::Socket(socket) => { - socket.ioctl(request, arg1, arg2, arg3)?; + match request { + // TODO: place flags & path in FileLike in stead of FileHandle/Socket + FIOCLEX => Ok(0), + _ => { + match self { + FileLike::File(file) => file.io_control(request as u32, arg1)?, + FileLike::Socket(socket) => { + socket.ioctl(request, arg1, arg2, arg3)?; + } + } + Ok(0) } } - Ok(0) } pub fn poll(&self) -> Result { let status = match self { diff --git a/kernel/src/fs/ioctl.rs b/kernel/src/fs/ioctl.rs new file mode 100644 index 0000000..e9fa9ca --- /dev/null +++ b/kernel/src/fs/ioctl.rs @@ -0,0 +1,36 @@ +// for IOR and IOW: +// 32bits total, command in lower 16bits, size of the parameter structure in the lower 14 bits of the upper 16 bits +// higher 2 bits: 01 = write, 10 = read + +#[cfg(not(target_arch = "mips"))] +pub const TCGETS: usize = 0x5401; +#[cfg(target_arch = "mips")] +pub const TCGETS: usize = 0x540D; + +#[cfg(not(target_arch = "mips"))] +pub const TIOCGPGRP: usize = 0x540F; +// _IOR('t', 119, int) +#[cfg(target_arch = "mips")] +pub const TIOCGPGRP: usize = 0x4_004_74_77; + +#[cfg(not(target_arch = "mips"))] +pub const TIOCSPGRP: usize = 0x5410; +// _IOW('t', 118, int) +#[cfg(target_arch = "mips")] +pub const TIOCSPGRP: usize = 0x8_004_74_76; + +#[cfg(not(target_arch = "mips"))] +pub const TIOCGWINSZ: usize = 0x5413; +// _IOR('t', 104, struct winsize) +#[cfg(target_arch = "mips")] +pub const TIOCGWINSZ: usize = 0x4_008_74_68; + +#[cfg(not(target_arch = "mips"))] +pub const FIONCLEX: usize = 0x5450; +#[cfg(target_arch = "mips")] +pub const FIOCLEX: usize = 0x6602; + +#[cfg(not(target_arch = "mips"))] +pub const FIOCLEX: usize = 0x5451; +#[cfg(target_arch = "mips")] +pub const FIOCLEX: usize = 0x6601; \ No newline at end of file diff --git a/kernel/src/fs/mod.rs b/kernel/src/fs/mod.rs index 4a378ad..a5e56a1 100644 --- a/kernel/src/fs/mod.rs +++ b/kernel/src/fs/mod.rs @@ -18,6 +18,7 @@ mod file_like; mod pipe; mod pseudo; mod stdio; +mod ioctl; /// Hard link user programs #[cfg(feature = "link_user")] @@ -48,8 +49,8 @@ lazy_static! { .clone() ); // enable block cache - // Arc::new(BlockCache::new(driver, 0x100)) - Arc::new(driver) + Arc::new(BlockCache::new(driver, 0x100)) + // Arc::new(driver) } #[cfg(target_arch = "aarch64")] { diff --git a/kernel/src/fs/stdio.rs b/kernel/src/fs/stdio.rs index e373a1d..11a342e 100644 --- a/kernel/src/fs/stdio.rs +++ b/kernel/src/fs/stdio.rs @@ -7,6 +7,7 @@ use rcore_fs::vfs::*; use crate::sync::Condvar; use crate::sync::SpinNoIrqLock as Mutex; +use super::ioctl::*; #[derive(Default)] pub struct Stdin { @@ -52,32 +53,6 @@ lazy_static! { pub static ref STDOUT: Arc = Arc::new(Stdout::default()); } -// 32bits total, command in lower 16bits, size of the parameter structure in the lower 14 bits of the upper 16 bits -// higher 2 bits: 01 = write, 10 = read - -#[cfg(not(target_arch = "mips"))] -const TCGETS: u32 = 0x5401; -#[cfg(target_arch = "mips")] -const TCGETS: u32 = 0x540D; - -#[cfg(not(target_arch = "mips"))] -const TIOCGPGRP: u32 = 0x540F; -// _IOR('t', 119, int) -#[cfg(target_arch = "mips")] -const TIOCGPGRP: u32 = 0x4_004_74_77; - -#[cfg(not(target_arch = "mips"))] -const TIOCSPGRP: u32 = 0x5410; -// _IOW('t', 118, int) -#[cfg(target_arch = "mips")] -const TIOCSPGRP: u32 = 0x8_004_74_76; - -#[cfg(not(target_arch = "mips"))] -const TIOCGWINSZ: u32 = 0x5413; -// _IOR('t', 104, struct winsize) -#[cfg(target_arch = "mips")] -const TIOCGWINSZ: u32 = 0x4_008_74_68; - // TODO: better way to provide default impl? macro_rules! impl_inode { () => { @@ -93,7 +68,7 @@ macro_rules! impl_inode { fn find(&self, _name: &str) -> Result> { Err(FsError::NotDir) } fn get_entry(&self, _id: usize) -> Result { Err(FsError::NotDir) } fn io_control(&self, cmd: u32, data: usize) -> Result<()> { - match cmd { + match cmd as usize { TCGETS | TIOCGWINSZ | TIOCSPGRP => { // pretend to be tty Ok(())