From 4c4366ccb02bc005dee78e379000bb09447b24ca Mon Sep 17 00:00:00 2001 From: Jianfeng Jiang Date: Fri, 2 Jun 2023 16:10:18 +0800 Subject: [PATCH] Open stdio by opening /dev/tty --- services/libs/jinux-std/src/fs/file_table.rs | 27 ++- services/libs/jinux-std/src/fs/fs_resolver.rs | 1 + services/libs/jinux-std/src/fs/mod.rs | 1 - services/libs/jinux-std/src/fs/stdio.rs | 189 ------------------ services/libs/jinux-std/src/fs/utils/inode.rs | 22 ++ services/libs/jinux-std/src/fs/utils/vnode.rs | 12 +- 6 files changed, 49 insertions(+), 203 deletions(-) delete mode 100644 services/libs/jinux-std/src/fs/stdio.rs diff --git a/services/libs/jinux-std/src/fs/file_table.rs b/services/libs/jinux-std/src/fs/file_table.rs index 8c63d4e73..28f9fcbbb 100644 --- a/services/libs/jinux-std/src/fs/file_table.rs +++ b/services/libs/jinux-std/src/fs/file_table.rs @@ -5,10 +5,9 @@ use crate::prelude::*; use core::cell::Cell; use jinux_util::slot_vec::SlotVec; -use super::{ - file_handle::FileLike, - stdio::{Stderr, Stdin, Stdout}, -}; +use super::file_handle::FileLike; +use super::fs_resolver::{FsPath, FsResolver, AT_FDCWD}; +use super::utils::{AccessMode, InodeMode}; pub type FileDescripter = i32; @@ -27,9 +26,23 @@ impl FileTable { pub fn new_with_stdio() -> Self { let mut table = SlotVec::new(); - let stdin = Stdin::new_with_default_console(); - let stdout = Stdout::new_with_default_console(); - let stderr = Stderr::new_with_default_console(); + let fs_resolver = FsResolver::new(); + let tty_path = FsPath::new(AT_FDCWD, "/dev/tty").expect("cannot find tty"); + let stdin = { + let flags = AccessMode::O_RDONLY as u32; + let mode = InodeMode::S_IRUSR; + fs_resolver.open(&tty_path, flags, mode.bits()).unwrap() + }; + let stdout = { + let flags = AccessMode::O_WRONLY as u32; + let mode = InodeMode::S_IWUSR; + fs_resolver.open(&tty_path, flags, mode.bits()).unwrap() + }; + let stderr = { + let flags = AccessMode::O_WRONLY as u32; + let mode = InodeMode::S_IWUSR; + fs_resolver.open(&tty_path, flags, mode.bits()).unwrap() + }; table.put(FileTableEntry::new(Arc::new(stdin), false)); table.put(FileTableEntry::new(Arc::new(stdout), false)); table.put(FileTableEntry::new(Arc::new(stderr), false)); diff --git a/services/libs/jinux-std/src/fs/fs_resolver.rs b/services/libs/jinux-std/src/fs/fs_resolver.rs index e89f04ae7..59f1c43e6 100644 --- a/services/libs/jinux-std/src/fs/fs_resolver.rs +++ b/services/libs/jinux-std/src/fs/fs_resolver.rs @@ -341,6 +341,7 @@ impl FsResolver { pub const AT_FDCWD: FileDescripter = -100; +#[derive(Debug)] pub struct FsPath<'a> { inner: FsPathInner<'a>, } diff --git a/services/libs/jinux-std/src/fs/mod.rs b/services/libs/jinux-std/src/fs/mod.rs index 6d119ab41..e8605659d 100644 --- a/services/libs/jinux-std/src/fs/mod.rs +++ b/services/libs/jinux-std/src/fs/mod.rs @@ -8,5 +8,4 @@ pub mod inode_handle; pub mod pipe; pub mod procfs; pub mod ramfs; -pub mod stdio; pub mod utils; diff --git a/services/libs/jinux-std/src/fs/stdio.rs b/services/libs/jinux-std/src/fs/stdio.rs deleted file mode 100644 index 792bd12ce..000000000 --- a/services/libs/jinux-std/src/fs/stdio.rs +++ /dev/null @@ -1,189 +0,0 @@ -use crate::device::tty::{get_n_tty, Tty}; -use crate::prelude::*; - -use super::device::Device; -use super::file_handle::FileLike; -use super::file_table::FileDescripter; -use super::utils::{InodeMode, InodeType, IoEvents, Metadata, Poller, SeekFrom}; - -pub const FD_STDIN: FileDescripter = 0; -pub const FD_STDOUT: FileDescripter = 1; -pub const FD_STDERR: FileDescripter = 2; - -pub struct Stdin { - console: Option>, -} - -pub struct Stdout { - console: Option>, -} - -pub struct Stderr { - console: Option>, -} - -impl FileLike for Stdin { - fn poll(&self, mask: IoEvents, poller: Option<&Poller>) -> IoEvents { - if let Some(console) = self.console.as_ref() { - console.poll(mask, poller) - } else { - todo!() - } - } - - fn read(&self, buf: &mut [u8]) -> Result { - if let Some(console) = self.console.as_ref() { - console.read(buf) - } else { - todo!() - } - } - - fn ioctl(&self, cmd: super::utils::IoctlCmd, arg: usize) -> Result { - if let Some(console) = self.console.as_ref() { - console.ioctl(cmd, arg) - } else { - todo!() - } - } - - fn seek(&self, seek_from: SeekFrom) -> Result { - // TODO: do real seek - Ok(0) - } - - fn metadata(&self) -> Metadata { - Metadata { - dev: 0, - ino: 0, - size: 0, - blk_size: 1024, - blocks: 0, - atime: Default::default(), - mtime: Default::default(), - ctime: Default::default(), - type_: InodeType::CharDevice, - mode: InodeMode::from_bits_truncate(0o620), - nlinks: 1, - uid: 0, - gid: 0, - rdev: 0, - } - } -} -impl FileLike for Stdout { - fn ioctl(&self, cmd: super::utils::IoctlCmd, arg: usize) -> Result { - if let Some(console) = self.console.as_ref() { - console.ioctl(cmd, arg) - } else { - todo!() - } - } - - fn write(&self, buf: &[u8]) -> Result { - if let Some(console) = self.console.as_ref() { - console.write(buf) - } else { - todo!() - } - } - - fn seek(&self, seek_from: SeekFrom) -> Result { - // TODO: do real seek - Ok(0) - } - - fn metadata(&self) -> Metadata { - Metadata { - dev: 0, - ino: 0, - size: 0, - blk_size: 1024, - blocks: 0, - atime: Default::default(), - mtime: Default::default(), - ctime: Default::default(), - type_: InodeType::CharDevice, - mode: InodeMode::from_bits_truncate(0o620), - nlinks: 1, - uid: 0, - gid: 0, - rdev: 0, - } - } -} - -impl FileLike for Stderr { - fn ioctl(&self, cmd: super::utils::IoctlCmd, arg: usize) -> Result { - if let Some(console) = self.console.as_ref() { - console.ioctl(cmd, arg) - } else { - todo!() - } - } - - fn write(&self, buf: &[u8]) -> Result { - if let Some(console) = self.console.as_ref() { - console.write(buf) - } else { - todo!() - } - } - - fn seek(&self, seek_from: SeekFrom) -> Result { - // TODO: do real seek - Ok(0) - } - - fn metadata(&self) -> Metadata { - Metadata { - dev: 0, - ino: 0, - size: 0, - blk_size: 1024, - blocks: 0, - atime: Default::default(), - mtime: Default::default(), - ctime: Default::default(), - type_: InodeType::CharDevice, - mode: InodeMode::from_bits_truncate(0o620), - nlinks: 1, - uid: 0, - gid: 0, - rdev: 0, - } - } -} - -impl Stdin { - /// FIXME: console should be file under devfs. - /// reimplement the function when devfs is enabled. - pub fn new_with_default_console() -> Self { - let console = get_n_tty(); - Self { - console: Some(console.clone()), - } - } -} - -impl Stdout { - /// FIXME: console should be file under devfs. - /// reimplement the function when devfs is enabled. - pub fn new_with_default_console() -> Self { - let console = get_n_tty(); - Self { - console: Some(console.clone()), - } - } -} - -impl Stderr { - /// FIXME: console should be file under devfs. - /// reimplement the function when devfs is enabled. - pub fn new_with_default_console() -> Self { - let console = get_n_tty(); - Self { - console: Some(console.clone()), - } - } -} diff --git a/services/libs/jinux-std/src/fs/utils/inode.rs b/services/libs/jinux-std/src/fs/utils/inode.rs index eef2abce0..343af3c2f 100644 --- a/services/libs/jinux-std/src/fs/utils/inode.rs +++ b/services/libs/jinux-std/src/fs/utils/inode.rs @@ -21,6 +21,28 @@ pub enum InodeType { Socket = 0o140000, } +impl InodeType { + pub fn support_read(&self) -> bool { + match self { + InodeType::File + | InodeType::Socket + | InodeType::CharDevice + | InodeType::BlockDevice => true, + _ => false, + } + } + + pub fn support_write(&self) -> bool { + match self { + InodeType::File + | InodeType::Socket + | InodeType::CharDevice + | InodeType::BlockDevice => true, + _ => false, + } + } +} + impl From for InodeType { fn from(type_: DeviceType) -> InodeType { match type_ { diff --git a/services/libs/jinux-std/src/fs/utils/vnode.rs b/services/libs/jinux-std/src/fs/utils/vnode.rs index f54060425..8a75d8c10 100644 --- a/services/libs/jinux-std/src/fs/utils/vnode.rs +++ b/services/libs/jinux-std/src/fs/utils/vnode.rs @@ -45,7 +45,7 @@ impl Vnode { pub fn write_at(&self, offset: usize, buf: &[u8]) -> Result { let type_ = self.inode_type(); - if type_ != InodeType::File && type_ != InodeType::Socket { + if !type_.support_write() { return_errno!(Errno::EINVAL); } let inner = self.inner.write(); @@ -68,7 +68,7 @@ impl Vnode { pub fn write_direct_at(&self, offset: usize, buf: &[u8]) -> Result { let type_ = self.inode_type(); - if type_ != InodeType::File && type_ != InodeType::Socket { + if !type_.support_write() { return_errno!(Errno::EINVAL); } let inner = self.inner.write(); @@ -80,7 +80,7 @@ impl Vnode { pub fn read_at(&self, offset: usize, buf: &mut [u8]) -> Result { let type_ = self.inode_type(); - if type_ != InodeType::File && type_ != InodeType::Socket { + if !type_.support_read() { return_errno!(Errno::EISDIR); } let inner = self.inner.read(); @@ -103,7 +103,7 @@ impl Vnode { pub fn read_direct_at(&self, offset: usize, buf: &mut [u8]) -> Result { let type_ = self.inode_type(); - if type_ != InodeType::File && type_ != InodeType::Socket { + if !type_.support_read() { return_errno!(Errno::EISDIR); } let inner = self.inner.read(); @@ -115,7 +115,7 @@ impl Vnode { pub fn read_to_end(&self, buf: &mut Vec) -> Result { let type_ = self.inode_type(); - if type_ != InodeType::File && type_ != InodeType::Socket { + if !type_.support_read() { return_errno!(Errno::EISDIR); } let inner = self.inner.read(); @@ -134,7 +134,7 @@ impl Vnode { pub fn read_direct_to_end(&self, buf: &mut Vec) -> Result { let type_ = self.inode_type(); - if type_ != InodeType::File && type_ != InodeType::Socket { + if !type_.support_read() { return_errno!(Errno::EISDIR); } let inner = self.inner.read();