mirror of
https://github.com/rcore-os/rCore.git
synced 2025-01-31 10:04:04 +04:00
run shell
This commit is contained in:
parent
47dade0c73
commit
d777f59118
6
kernel/Cargo.lock
generated
6
kernel/Cargo.lock
generated
@ -369,7 +369,7 @@ dependencies = [
|
||||
"rcore-fs-ramfs 0.1.0 (git+https://github.com/rcore-os/rcore-fs?rev=7f5eeac)",
|
||||
"rcore-fs-sfs 0.1.0 (git+https://github.com/rcore-os/rcore-fs?rev=7f5eeac)",
|
||||
"rcore-memory 0.1.0",
|
||||
"rcore-thread 0.1.0 (git+https://github.com/rcore-os/rcore-thread?rev=d727949b)",
|
||||
"rcore-thread 0.1.0 (git+https://github.com/function2-llx/rcore-thread?rev=8c1022d)",
|
||||
"riscv 0.5.0 (git+https://github.com/rcore-os/riscv)",
|
||||
"rlibc 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"smoltcp 0.5.0 (git+https://github.com/rcore-os/smoltcp?rev=5bd87c7c)",
|
||||
@ -453,7 +453,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "rcore-thread"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/rcore-os/rcore-thread?rev=d727949b#d727949b8ea20c2e3a94f8ba015652c9eff8b53c"
|
||||
source = "git+https://github.com/function2-llx/rcore-thread?rev=8c1022d#8c1022d95ce5868efa5d9fa93c8c098317df0714"
|
||||
dependencies = [
|
||||
"deque 0.3.2 (git+https://github.com/rcore-os/deque.git?branch=no_std)",
|
||||
"log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
@ -763,7 +763,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
"checksum rcore-fs-mountfs 0.1.0 (git+https://github.com/rcore-os/rcore-fs?rev=7f5eeac)" = "<none>"
|
||||
"checksum rcore-fs-ramfs 0.1.0 (git+https://github.com/rcore-os/rcore-fs?rev=7f5eeac)" = "<none>"
|
||||
"checksum rcore-fs-sfs 0.1.0 (git+https://github.com/rcore-os/rcore-fs?rev=7f5eeac)" = "<none>"
|
||||
"checksum rcore-thread 0.1.0 (git+https://github.com/rcore-os/rcore-thread?rev=d727949b)" = "<none>"
|
||||
"checksum rcore-thread 0.1.0 (git+https://github.com/function2-llx/rcore-thread?rev=8c1022d)" = "<none>"
|
||||
"checksum redox_syscall 0.1.56 (registry+https://github.com/rust-lang/crates.io-index)" = "2439c63f3f6139d1b57529d16bc3b8bb855230c8efcc5d3a896c8bea7c3b1e84"
|
||||
"checksum register 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "e10f31b6d2299e5620986ad9fcdd66463e125ad72af4f403f9aedf7592d5ccdb"
|
||||
"checksum riscv 0.5.0 (git+https://github.com/rcore-os/riscv)" = "<none>"
|
||||
|
@ -65,7 +65,7 @@ smoltcp = { git = "https://github.com/rcore-os/smoltcp", rev = "5bd87c7c", defau
|
||||
bitmap-allocator = { git = "https://github.com/rcore-os/bitmap-allocator" }
|
||||
rcore-console = { git = "https://github.com/rcore-os/rcore-console", rev = "b7bacf9", default-features = false }
|
||||
rcore-memory = { path = "../crate/memory" }
|
||||
rcore-thread = { git = "https://github.com/rcore-os/rcore-thread", rev = "d727949b" }
|
||||
rcore-thread = { git = "https://github.com/function2-llx/rcore-thread", rev = "8c1022d" }
|
||||
rcore-fs = { git = "https://github.com/rcore-os/rcore-fs", rev = "7f5eeac" }
|
||||
rcore-fs-sfs = { git = "https://github.com/rcore-os/rcore-fs", rev = "7f5eeac" }
|
||||
rcore-fs-ramfs = { git = "https://github.com/rcore-os/rcore-fs", rev = "7f5eeac" }
|
||||
|
@ -3,7 +3,9 @@
|
||||
mod fbdev;
|
||||
mod random;
|
||||
mod stdio;
|
||||
mod tty;
|
||||
|
||||
pub use fbdev::*;
|
||||
pub use random::*;
|
||||
pub use stdio::*;
|
||||
pub use tty::*;
|
||||
|
@ -1,7 +1,7 @@
|
||||
use rcore_fs::vfs::*;
|
||||
use core::any::Any;
|
||||
|
||||
pub use super::stdio::{STDIN, STDOUT};
|
||||
pub use super::{STDIN, STDOUT};
|
||||
|
||||
/// Ref: [https://linux.die.net/man/4/tty]
|
||||
#[derive(Default)]
|
15
kernel/src/fs/fcntl.rs
Normal file
15
kernel/src/fs/fcntl.rs
Normal file
@ -0,0 +1,15 @@
|
||||
// currently support x86_64 only
|
||||
// copy from fcntl.h
|
||||
|
||||
pub const F_DUPFD: usize = 0; /* dup */
|
||||
pub const F_GETFD: usize = 1; /* get close_on_exec */
|
||||
pub const F_SETFD: usize = 2; /* set/clear close_on_exec */
|
||||
pub const F_GETFL: usize = 3; /* get file->f_flags */
|
||||
pub const F_SETFL: usize = 4; /* set file->f_flags */
|
||||
|
||||
|
||||
const F_LINUX_SPECIFIC_BASE: usize = 1024;
|
||||
|
||||
pub const F_DUPFD_CLOEXEC: usize = F_LINUX_SPECIFIC_BASE + 6;
|
||||
|
||||
pub const O_CLOEXEC: usize = 02000000; /* set close_on_exec */
|
@ -2,20 +2,22 @@
|
||||
|
||||
use crate::memory::GlobalFrameAlloc;
|
||||
use crate::process::{current_thread, INodeForMap};
|
||||
use crate::syscall::MmapProt;
|
||||
use crate::thread;
|
||||
use crate::syscall::{MmapProt, SysResult};
|
||||
use crate::{thread, processor};
|
||||
use alloc::{string::String, sync::Arc};
|
||||
use core::fmt;
|
||||
|
||||
use rcore_fs::vfs::{FileType, FsError, INode, MMapArea, Metadata, PollStatus, Result};
|
||||
use rcore_memory::memory_set::handler::File;
|
||||
use rcore_fs::vfs::FsError::NotSupported;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct FileHandle {
|
||||
inode: Arc<dyn INode>,
|
||||
offset: u64,
|
||||
options: OpenOptions,
|
||||
pub options: OpenOptions,
|
||||
pub path: String,
|
||||
pub fd_cloexec: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
@ -41,6 +43,7 @@ impl FileHandle {
|
||||
offset: 0,
|
||||
options,
|
||||
path,
|
||||
fd_cloexec: false,
|
||||
};
|
||||
}
|
||||
|
||||
@ -173,12 +176,32 @@ impl FileHandle {
|
||||
self.inode.clone()
|
||||
}
|
||||
|
||||
pub fn fcntl(&mut self, cmd: usize, arg: usize) -> Result<()> {
|
||||
if arg & 0x800 > 0 && cmd == 4 {
|
||||
self.options.nonblock = true;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
// pub fn fcntl(&mut self, cmd: usize, arg: usize) -> SysResult {
|
||||
// use super::fcntl::*;
|
||||
// match cmd {
|
||||
// F_SETFD => {
|
||||
// self.fd_cloexec = (arg & 1) as bool;
|
||||
// Ok(0)
|
||||
// }
|
||||
// F_GETFD => {
|
||||
// Ok(self.fd_cloexec as usize)
|
||||
// }
|
||||
// F_SETFL => {
|
||||
// if arg & 0x800 > 0 {
|
||||
// self.options.nonblock = true;
|
||||
// }
|
||||
// Ok(0)
|
||||
// }
|
||||
// F_DUPFD_CLOEXEC => {
|
||||
// info!("dupfd_cloexec: arg: {:#x}", arg);
|
||||
// let proc = self
|
||||
// Ok(0)
|
||||
// }
|
||||
// _ => {
|
||||
// Ok(0)
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
impl fmt::Debug for FileHandle {
|
||||
|
@ -77,16 +77,19 @@ impl FileLike {
|
||||
};
|
||||
Ok(status)
|
||||
}
|
||||
pub fn fcntl(&mut self, cmd: usize, arg: usize) -> SysResult {
|
||||
match self {
|
||||
FileLike::File(file) => file.fcntl(cmd, arg)?,
|
||||
FileLike::Socket(_) => {
|
||||
//TODO
|
||||
}
|
||||
FileLike::EpollInstance(_) => {}
|
||||
}
|
||||
Ok(0)
|
||||
}
|
||||
// pub fn fcntl(&mut self, cmd: usize, arg: usize) -> SysResult {
|
||||
// use super::fcntl::*;
|
||||
// match self {
|
||||
// FileLike::File(file) => file.fcntl(cmd, arg),
|
||||
// FileLike::Socket(_) => {
|
||||
// Ok(0)
|
||||
// //TODO
|
||||
// }
|
||||
// FileLike::EpollInstance(_) => {
|
||||
// Ok(0)
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
impl fmt::Debug for FileLike {
|
||||
|
@ -11,15 +11,11 @@ use rcore_fs_sfs::SimpleFileSystem;
|
||||
|
||||
use self::devfs::{Fbdev, RandomINode};
|
||||
|
||||
pub use self::devfs::{STDIN, STDOUT};
|
||||
pub use self::devfs::{STDIN, STDOUT, TtyINode};
|
||||
pub use self::file::*;
|
||||
pub use self::file_like::*;
|
||||
pub use self::pipe::Pipe;
|
||||
pub use self::pseudo::*;
|
||||
pub use self::random::*;
|
||||
pub use self::stdio::{STDIN, STDOUT};
|
||||
pub use self::vga::*;
|
||||
pub use self::tty::TtyINode;
|
||||
|
||||
mod devfs;
|
||||
mod device;
|
||||
@ -27,12 +23,9 @@ pub mod epoll;
|
||||
mod file;
|
||||
mod file_like;
|
||||
mod ioctl;
|
||||
pub mod fcntl;
|
||||
mod pipe;
|
||||
mod pseudo;
|
||||
mod random;
|
||||
mod stdio;
|
||||
pub mod vga;
|
||||
pub mod tty;
|
||||
|
||||
// Hard link user programs
|
||||
#[cfg(feature = "link_user")]
|
||||
|
@ -399,6 +399,11 @@ impl Process {
|
||||
fn get_free_fd(&self) -> usize {
|
||||
(0..).find(|i| !self.files.contains_key(i)).unwrap()
|
||||
}
|
||||
|
||||
// get the lowest available fd great than or equal to arg
|
||||
pub fn get_free_fd_from(&self, arg: usize) -> usize {
|
||||
(arg..).find(|i| !self.files.contains_key(i)).unwrap()
|
||||
}
|
||||
/// Add a file to the process, return its fd.
|
||||
pub fn add_file(&mut self, file_like: FileLike) -> usize {
|
||||
let fd = self.get_free_fd();
|
||||
|
@ -20,6 +20,7 @@ use super::*;
|
||||
use crate::fs::epoll::EpollInstance;
|
||||
use crate::process::Process;
|
||||
use rcore_fs::vfs::PollStatus;
|
||||
use crate::fs::FileLike;
|
||||
|
||||
impl Syscall<'_> {
|
||||
pub fn sys_read(&mut self, fd: usize, base: *mut u8, len: usize) -> SysResult {
|
||||
@ -31,10 +32,10 @@ impl Syscall<'_> {
|
||||
let slice = unsafe { self.vm().check_write_array(base, len)? };
|
||||
let file_like = proc.get_file_like(fd)?;
|
||||
let len = file_like.read(slice)?;
|
||||
if !proc.pid.is_init() {
|
||||
// we trust pid 0 process
|
||||
info!("read result: {:?}", slice);
|
||||
}
|
||||
// if !proc.pid.is_init() {
|
||||
// // we trust pid 0 process
|
||||
// info!("read result: {:?}", slice);
|
||||
// }
|
||||
Ok(len)
|
||||
}
|
||||
|
||||
@ -777,11 +778,34 @@ impl Syscall<'_> {
|
||||
// close fd2 first if it is opened
|
||||
proc.files.remove(&fd2);
|
||||
|
||||
let file_like = proc.get_file_like(fd1)?.clone();
|
||||
let mut file_like = proc.get_file_like(fd1)?.clone();
|
||||
if let FileLike::File(file) = &mut file_like {
|
||||
// The two file descriptors do not share file descriptor flags (the
|
||||
// close-on-exec flag).
|
||||
file.fd_cloexec = false;
|
||||
}
|
||||
proc.files.insert(fd2, file_like);
|
||||
Ok(fd2)
|
||||
}
|
||||
|
||||
// TODO: handle `flags`
|
||||
pub fn sys_dup3(&mut self, fd1: usize, fd2: usize, flags: usize) -> SysResult {
|
||||
info!("dup3: from {} to {}", fd1, fd2);
|
||||
self.sys_dup2(fd1, fd2)
|
||||
// let mut proc = self.process();
|
||||
// // close fd2 first if it is opened
|
||||
// proc.files.remove(&fd2);
|
||||
//
|
||||
// let mut file_like = proc.get_file_like(fd1)?.clone();
|
||||
// if let FileLike::File(file) = &mut file_like {
|
||||
// // The two file descriptors do not share file descriptor flags (the
|
||||
// // close-on-exec flag).
|
||||
// file.fd_cloexec = false;
|
||||
// }
|
||||
// proc.files.insert(fd2, file_like);
|
||||
// Ok(fd2)
|
||||
}
|
||||
|
||||
pub fn sys_ioctl(
|
||||
&mut self,
|
||||
fd: usize,
|
||||
@ -1102,7 +1126,43 @@ impl Syscall<'_> {
|
||||
info!("fcntl: fd: {}, cmd: {:#x}, arg: {}", fd, cmd, arg);
|
||||
let mut proc = self.process();
|
||||
let file_like = proc.get_file_like(fd)?;
|
||||
file_like.fcntl(cmd, arg)
|
||||
match file_like {
|
||||
FileLike::File(file) => {
|
||||
use crate::fs::fcntl::*;
|
||||
match cmd {
|
||||
F_SETFD => {
|
||||
file.fd_cloexec = (arg & 1) != 0;
|
||||
Ok(0)
|
||||
}
|
||||
F_GETFD => {
|
||||
Ok(file.fd_cloexec as usize)
|
||||
}
|
||||
F_SETFL => {
|
||||
if arg & 0x800 > 0 {
|
||||
file.options.nonblock = true;
|
||||
}
|
||||
Ok(0)
|
||||
}
|
||||
F_DUPFD_CLOEXEC => {
|
||||
info!("dupfd_cloexec: arg: {:#x}", arg);
|
||||
// let file_like = proc.get_file_like(fd1)?.clone();
|
||||
let new_fd = proc.get_free_fd_from(arg);
|
||||
core::mem::drop(proc);
|
||||
self.sys_dup3(fd, new_fd, 1)
|
||||
}
|
||||
_ => {
|
||||
Ok(0)
|
||||
}
|
||||
}
|
||||
}
|
||||
FileLike::Socket(_) => {
|
||||
Ok(0)
|
||||
//TODO
|
||||
}
|
||||
FileLike::EpollInstance(_) => {
|
||||
Ok(0)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -142,7 +142,7 @@ impl Syscall<'_> {
|
||||
SYS_FCHOWN => self.unimplemented("fchown", Ok(0)),
|
||||
SYS_FCHOWNAT => self.unimplemented("fchownat", Ok(0)),
|
||||
SYS_FACCESSAT => self.sys_faccessat(args[0], args[1] as *const u8, args[2], args[3]),
|
||||
SYS_DUP3 => self.sys_dup2(args[0], args[1]), // TODO: handle `flags`
|
||||
SYS_DUP3 => self.sys_dup3(args[0], args[1], args[2]),
|
||||
SYS_PIPE2 => self.sys_pipe(args[0] as *mut u32), // TODO: handle `flags`
|
||||
SYS_UTIMENSAT => self.unimplemented("utimensat", Ok(0)),
|
||||
SYS_COPY_FILE_RANGE => self.sys_copy_file_range(
|
||||
|
Loading…
x
Reference in New Issue
Block a user