Skip to content

Commit

Permalink
Merge pull request #48 from luodeb/new_ioctl
Browse files Browse the repository at this point in the history
syscall: fix ioctl return 0 but isatty() needs non zero
  • Loading branch information
scPointer authored May 8, 2024
2 parents 94c65af + 1d3ffc4 commit d522a74
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 9 deletions.
6 changes: 5 additions & 1 deletion modules/axfs/src/api/port.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ pub trait FileIO: AsAny + Send + Sync {
}

/// To control the file descriptor
fn ioctl(&self, _request: usize, _arg1: usize) -> AxResult<()> {
fn ioctl(&self, _request: usize, _arg1: usize) -> AxResult<isize> {
Err(AxError::Unsupported)
}
}
Expand Down Expand Up @@ -405,6 +405,10 @@ pub const TIOCGPGRP: usize = 0x540F;
pub const TIOCSPGRP: usize = 0x5410;
#[allow(missing_docs)]
pub const TIOCGWINSZ: usize = 0x5413;
#[allow(missing_docs)]
pub const FIONBIO: usize = 0x5421;
#[allow(missing_docs)]
pub const FIOCLEX: usize = 0x5451;
#[repr(C)]
#[derive(Clone, Copy, Default)]
/// the size of the console window
Expand Down
67 changes: 61 additions & 6 deletions modules/axprocess/src/stdio.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use axerrno::{AxError, AxResult};
use axfs::api::port::{
ConsoleWinSize, FileExt, FileIO, FileIOType, OpenFlags, TCGETS, TIOCGPGRP, TIOCGWINSZ,
TIOCSPGRP,
ConsoleWinSize, FileExt, FileIO, FileIOType, OpenFlags, FIOCLEX, FIONBIO, TCGETS, TIOCGPGRP,
TIOCGWINSZ, TIOCSPGRP,
};
use axhal::console::{getchar, write_bytes};
use axio::{Read, Seek, SeekFrom, Write};
Expand Down Expand Up @@ -110,28 +110,29 @@ impl FileIO for Stdin {
false
}

fn ioctl(&self, request: usize, data: usize) -> AxResult<()> {
fn ioctl(&self, request: usize, data: usize) -> AxResult<isize> {
match request {
TIOCGWINSZ => {
let winsize = data as *mut ConsoleWinSize;
unsafe {
*winsize = ConsoleWinSize::default();
}
Ok(())
Ok(0)
}
TCGETS | TIOCSPGRP => {
warn!("stdin TCGETS | TIOCSPGRP, pretend to be tty.");
// pretend to be tty
Ok(())
Ok(0)
}

TIOCGPGRP => {
warn!("stdin TIOCGPGRP, pretend to be have a tty process group.");
unsafe {
*(data as *mut u32) = 0;
}
Ok(())
Ok(0)
}
FIOCLEX => Ok(0),
_ => Err(AxError::Unsupported),
}
}
Expand Down Expand Up @@ -250,6 +251,33 @@ impl FileIO for Stdout {
}
true
}

fn ioctl(&self, request: usize, data: usize) -> AxResult<isize> {
match request {
TIOCGWINSZ => {
let winsize = data as *mut ConsoleWinSize;
unsafe {
*winsize = ConsoleWinSize::default();
}
Ok(0)
}
TCGETS | TIOCSPGRP => {
warn!("stdout TCGETS | TIOCSPGRP, pretend to be tty.");
// pretend to be tty
Ok(0)
}

TIOCGPGRP => {
warn!("stdout TIOCGPGRP, pretend to be have a tty process group.");
unsafe {
*(data as *mut u32) = 0;
}
Ok(0)
}
FIOCLEX => Ok(0),
_ => Err(AxError::Unsupported),
}
}
}

impl Read for Stderr {
Expand Down Expand Up @@ -322,4 +350,31 @@ impl FileIO for Stderr {
fn ready_to_write(&self) -> bool {
true
}

fn ioctl(&self, request: usize, data: usize) -> AxResult<isize> {
match request {
TIOCGWINSZ => {
let winsize = data as *mut ConsoleWinSize;
unsafe {
*winsize = ConsoleWinSize::default();
}
Ok(0)
}
TCGETS | TIOCSPGRP => {
warn!("stderr TCGETS | TIOCSPGRP, pretend to be tty.");
// pretend to be tty
Ok(0)
}

TIOCGPGRP => {
warn!("stderr TIOCGPGRP, pretend to be have a tty process group.");
unsafe {
*(data as *mut u32) = 0;
}
Ok(0)
}
FIOCLEX => Ok(0),
_ => Err(AxError::Unsupported),
}
}
}
2 changes: 2 additions & 0 deletions ulib/axstarry/src/syscall_fs/fs_syscall_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub enum FsSyscallId {
FCNTL64 = 25,
IOCTL = 29,
MKDIRAT = 34,
SYMLINKAT = 36,
UNLINKAT = 35,
LINKAT = 37,
RENAMEAT = 38,
Expand Down Expand Up @@ -82,6 +83,7 @@ numeric_enum_macro::numeric_enum! {
FCNTL64 = 72,
IOCTL = 16,
MKDIRAT = 258,
SYMLINKAT = 266,
RENAME = 82,
MKDIR = 83,
RMDIR = 84,
Expand Down
6 changes: 4 additions & 2 deletions ulib/axstarry/src/syscall_fs/imp/ctl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,8 +449,10 @@ pub fn syscall_ioctl(args: [usize; 6]) -> SyscallResult {
}

let file = fd_table[fd].clone().unwrap();
let _ = file.ioctl(request, argp);
Ok(0)
match file.ioctl(request, argp) {
Ok(ret) => Ok(ret),
Err(_) => Ok(0),
}
}

/// 53
Expand Down
1 change: 1 addition & 0 deletions ulib/axstarry/src/syscall_fs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ pub fn fs_syscall(syscall_id: fs_syscall_id::FsSyscallId, args: [usize; 6]) -> S
COPYFILERANGE => syscall_copyfilerange(args),
LINKAT => sys_linkat(args),
UNLINKAT => syscall_unlinkat(args),
SYMLINKAT => Ok(0),
UTIMENSAT => syscall_utimensat(args),
EPOLL_CREATE => syscall_epoll_create1(args),
EPOLL_CTL => syscall_epoll_ctl(args),
Expand Down

0 comments on commit d522a74

Please sign in to comment.