Add sys_dup && support input/output redirection in user_shell
This commit is contained in:
parent
ff685b86c8
commit
685ca2c1ea
6 changed files with 93 additions and 13 deletions
|
@ -6,6 +6,7 @@ use crate::mm::{
|
|||
};
|
||||
use crate::task::{current_user_token, current_task};
|
||||
use crate::fs::{make_pipe, OpenFlags, open_file};
|
||||
use alloc::sync::Arc;
|
||||
|
||||
pub fn sys_write(fd: usize, buf: *const u8, len: usize) -> isize {
|
||||
let token = current_user_token();
|
||||
|
@ -93,4 +94,18 @@ pub fn sys_pipe(pipe: *mut usize) -> isize {
|
|||
*translated_refmut(token, pipe) = read_fd;
|
||||
*translated_refmut(token, unsafe { pipe.add(1) }) = write_fd;
|
||||
0
|
||||
}
|
||||
|
||||
pub fn sys_dup(fd: usize) -> isize {
|
||||
let task = current_task().unwrap();
|
||||
let mut inner = task.acquire_inner_lock();
|
||||
if fd >= inner.fd_table.len() {
|
||||
return -1;
|
||||
}
|
||||
if inner.fd_table[fd].is_none() {
|
||||
return -1;
|
||||
}
|
||||
let new_fd = inner.alloc_fd();
|
||||
inner.fd_table[new_fd] = Some(Arc::clone(inner.fd_table[fd].as_ref().unwrap()));
|
||||
new_fd as isize
|
||||
}
|
|
@ -1,3 +1,4 @@
|
|||
const SYSCALL_DUP: usize = 24;
|
||||
const SYSCALL_OPEN: usize = 56;
|
||||
const SYSCALL_CLOSE: usize = 57;
|
||||
const SYSCALL_PIPE: usize = 59;
|
||||
|
@ -19,6 +20,7 @@ use process::*;
|
|||
|
||||
pub fn syscall(syscall_id: usize, args: [usize; 3]) -> isize {
|
||||
match syscall_id {
|
||||
SYSCALL_DUP=> sys_dup(args[0]),
|
||||
SYSCALL_OPEN => sys_open(args[0] as *const u8, args[1] as u32),
|
||||
SYSCALL_CLOSE => sys_close(args[0]),
|
||||
SYSCALL_PIPE => sys_pipe(args[0] as *mut usize),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue