Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions api/src/imp/fs/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,37 @@ pub fn sys_read(fd: i32, buf: UserPtr<u8>, len: usize) -> LinuxResult<isize> {
Ok(get_file_like(fd)?.read(buf)? as _)
}

pub fn sys_readv(fd: i32, iov: UserPtr<iovec>, iocnt: usize) -> LinuxResult<isize> {
if !(0..=1024).contains(&iocnt) {
return Err(LinuxError::EINVAL);
}

let iovs = iov.get_as_mut_slice(iocnt)?;
let mut ret = 0;
for iov in iovs {
if iov.iov_len == 0 {
continue;
}
let buf = UserPtr::<u8>::from(iov.iov_base as usize);
let buf = buf.get_as_mut_slice(iov.iov_len as _)?;
debug!(
"sys_readv <= fd: {}, buf: {:p}, len: {}",
fd,
buf.as_ptr(),
buf.len()
);

let read = get_file_like(fd)?.read(buf)?;
ret += read as isize;

if read < buf.len() {
break;
}
}

Ok(ret)
}

/// Write data to the file indicated by `fd`.
///
/// Return the written size if success.
Expand Down
14 changes: 13 additions & 1 deletion core/src/mm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use core::ffi::CStr;

use alloc::{string::String, vec};
use alloc::{borrow::ToOwned, string::String, vec, vec::Vec};
use axerrno::{AxError, AxResult};
use axhal::{mem::virt_to_phys, paging::MappingFlags};
use axmm::{AddrSpace, kernel_aspace};
Expand Down Expand Up @@ -111,6 +111,18 @@ pub fn load_user_app(
return Err(AxError::InvalidInput);
}
let file_data = axfs::api::read(args[0].as_str())?;
if file_data.starts_with(b"#!") {
let head = &file_data[2..file_data.len().min(256)];
let pos = head.iter().position(|c| *c == b'\n').unwrap_or(head.len());
let line = core::str::from_utf8(&head[..pos]).map_err(|_| AxError::InvalidData)?;

let new_args: Vec<String> = line
.splitn(2, |c: char| c.is_ascii_whitespace())
.map(|s| s.trim_ascii().to_owned())
.chain(args.iter().cloned())
.collect();
return load_user_app(uspace, &new_args, envs);
}
let elf = ElfFile::new(&file_data).map_err(|_| AxError::InvalidData)?;

if let Some(interp) = elf
Expand Down
1 change: 1 addition & 0 deletions src/syscall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ fn handle_syscall(tf: &mut TrapFrame, syscall_num: usize) -> isize {

// io
Sysno::read => sys_read(tf.arg0() as _, tf.arg1().into(), tf.arg2() as _),
Sysno::readv => sys_readv(tf.arg0() as _, tf.arg1().into(), tf.arg2() as _),
Sysno::write => sys_write(tf.arg0() as _, tf.arg1().into(), tf.arg2() as _),
Sysno::writev => sys_writev(tf.arg0() as _, tf.arg1().into(), tf.arg2() as _),
Sysno::lseek => sys_lseek(tf.arg0() as _, tf.arg1() as _, tf.arg2() as _),
Expand Down
Loading