⚠️ VeridianOS Kernel Documentation - This is low-level kernel code. All functions are unsafe unless explicitly marked otherwise. no_std

sys_read

Function sys_read 

Source
pub fn sys_read(_fd: u64, _buf: &mut [u8]) -> SyscallResult<usize>
Expand description

Read data from an open file descriptor.

Reads up to buf.len() bytes from the file descriptor into the provided buffer, starting at the file’s current offset. The file offset is advanced by the number of bytes read. Partial reads are possible and normal: the kernel may return fewer bytes than requested if fewer are available (e.g., near end-of-file or for device files).

A return value of Ok(0) indicates end-of-file (EOF): the current offset is at or past the end of the file and no more data can be read.

For special file descriptors: fd 0 (stdin) reads from the process’s standard input source (terminal or pipe).

§Arguments

  • fd - File descriptor returned by a prior call to sys_open. Must be opened with at least O_RDONLY or O_RDWR access.
  • buf - Mutable buffer to read data into. The kernel reads at most buf.len() bytes. Must be non-empty.

§Returns

  • Ok(bytes_read) - The number of bytes actually read, which may be less than buf.len(). Returns Ok(0) at end-of-file.

§Errors

§Examples

use veridian_kernel::pkg::sdk::syscall_api::{sys_close, sys_open, sys_read};

let fd = sys_open("/etc/hostname", 0x000).expect("open failed");
let mut buf = [0u8; 256];
let bytes = sys_read(fd, &mut buf).expect("read failed");
if bytes == 0 {
    // End of file reached
}
let data = &buf[..bytes];
sys_close(fd).expect("close failed");