The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
sys_io.c File Reference
#include "syscall_internal.h"
#include "syscall.h"
#include "fd_table.h"
#include "kernel/vfs/file.h"
#include "kernel/vfs/flock.h"
#include "kernel/vfs/fifo.h"
#include "kernel/vfs/poll.h"
#include "kernel/vfs/permission.h"
#include "kernel/time/timer.h"
#include "kernel/process/process.h"

Macros

#define OPEN_STATUS_FLAGS   (O_ACCMODE | O_APPEND | O_NONBLOCK)
#define F_DUPFD   0
#define F_GETFD   1
#define F_SETFD   2
#define F_GETFL   3
#define F_SETFL   4
#define F_DUPFD_CLOEXEC   1030
#define SETFL_FLAGS   (O_APPEND | O_NONBLOCK)
#define POLL_FDS_MAX   64

Functions

syscall_ret_t sys_write (struct registers *regs)
 SYS_write: write bytes to a file descriptor.
syscall_ret_t sys_read (struct registers *regs)
 SYS_read: read bytes from a file descriptor.
syscall_ret_t sys_open (struct registers *regs)
syscall_ret_t sys_openat (struct registers *regs)
 SYS_openat: open relative to a directory.
syscall_ret_t sys_close (struct registers *regs)
 SYS_close: close an open file descriptor.
syscall_ret_t sys_lseek (struct registers *regs)
 SYS_lseek: set the file offset.
syscall_ret_t sys_ftruncate (struct registers *regs)
 SYS_ftruncate: cut or grow an open file to a length.
syscall_ret_t sys_dup2 (struct registers *regs)
 SYS_dup2: make new_fd another descriptor for old_fd's open file.
syscall_ret_t sys_pipe (struct registers *regs)
 SYS_pipe: create a pipe (pair of connected file descriptors).
syscall_ret_t sys_fstat (struct registers *regs)
 SYS_fstat: get the metadata of an open file.
syscall_ret_t sys_fcntl (struct registers *regs)
 SYS_fcntl: control an open file descriptor.
syscall_ret_t sys_pread (struct registers *regs)
 SYS_pread: read at an offset without moving the file offset.
syscall_ret_t sys_pwrite (struct registers *regs)
 SYS_pwrite: write at an offset without moving the file offset.
syscall_ret_t sys_fsync (struct registers *regs)
 SYS_fsync: write an open file's data to disk.
syscall_ret_t sys_poll (struct registers *regs)
 SYS_poll: wait until one of a set of descriptors is ready.
syscall_ret_t sys_flock (struct registers *regs)
 SYS_flock: an advisory lock on a whole file (kernel/vfs/flock.h).

Macro Definition Documentation

◆ OPEN_STATUS_FLAGS

#define OPEN_STATUS_FLAGS   (O_ACCMODE | O_APPEND | O_NONBLOCK)

◆ F_DUPFD

#define F_DUPFD   0

◆ F_GETFD

#define F_GETFD   1

◆ F_SETFD

#define F_SETFD   2

◆ F_GETFL

#define F_GETFL   3

◆ F_SETFL

#define F_SETFL   4

◆ F_DUPFD_CLOEXEC

#define F_DUPFD_CLOEXEC   1030

◆ SETFL_FLAGS

#define SETFL_FLAGS   (O_APPEND | O_NONBLOCK)

◆ POLL_FDS_MAX

#define POLL_FDS_MAX   64

Function Documentation

◆ sys_write()

syscall_ret_t sys_write ( struct registers * regs)

SYS_write: write bytes to a file descriptor.

args: EBX = fd, ECX = buffer pointer, EDX = count. returns number of bytes written, or negative errno on error.

◆ sys_read()

syscall_ret_t sys_read ( struct registers * regs)

SYS_read: read bytes from a file descriptor.

args: EBX = fd, ECX = buffer pointer, EDX = count. returns number of bytes read (0 at end of file), or negative errno.

◆ sys_open()

syscall_ret_t sys_open ( struct registers * regs)

◆ sys_openat()

syscall_ret_t sys_openat ( struct registers * regs)

SYS_openat: open relative to a directory.

args: dirfd (AT_FDCWD: the working directory), path, flags, mode.

◆ sys_close()

syscall_ret_t sys_close ( struct registers * regs)

SYS_close: close an open file descriptor.

args: EBX = fd. returns 0 on success, negative errno on failure.

◆ sys_lseek()

syscall_ret_t sys_lseek ( struct registers * regs)

SYS_lseek: set the file offset.

args: fd, offset (low, high: syscall_arg_offset), whence (0=SET, 1=CUR, 2=END), and a pointer to an int64_t that receives the new offset (on i686 the return value is too small for it). returns 0, or negative errno (ESPIPE for a pipe or the console, EINVAL for a bad whence or a negative result).

◆ sys_ftruncate()

syscall_ret_t sys_ftruncate ( struct registers * regs)

SYS_ftruncate: cut or grow an open file to a length.

args: fd, length (low, high: syscall_arg_offset; the new bytes read as zeros). the offset stays. returns 0, or a negative errno (EINVAL: a negative length, or a file not open for writing; EFBIG: longer than the filesystem allows).

◆ sys_dup2()

syscall_ret_t sys_dup2 ( struct registers * regs)

SYS_dup2: make new_fd another descriptor for old_fd's open file.

args: EBX = old_fd, ECX = new_fd. if new_fd is already open, it is closed first. the new descriptor does not have FD_CLOEXEC. returns new_fd on success, negative errno on failure.

◆ sys_pipe()

syscall_ret_t sys_pipe ( struct registers * regs)

SYS_pipe: create a pipe (pair of connected file descriptors).

args: EBX = pointer to int[2] array (receives [read_fd, write_fd]). returns 0 on success, negative errno on failure.

◆ sys_fstat()

syscall_ret_t sys_fstat ( struct registers * regs)

SYS_fstat: get the metadata of an open file.

args: EBX = fd, ECX = struct stat pointer. returns 0 on success, negative errno on failure.

◆ sys_fcntl()

syscall_ret_t sys_fcntl ( struct registers * regs)

SYS_fcntl: control an open file descriptor.

args: EBX = fd, ECX = command, EDX = argument. F_DUPFD / F_DUPFD_CLOEXEC: duplicate to the lowest free fd >= argument F_GETFD / F_SETFD: the descriptor flags (FD_CLOEXEC) F_GETFL / F_SETFL: the description's status flags; only O_APPEND and O_NONBLOCK can be changed returns the command's result, or a negative errno.

◆ sys_pread()

syscall_ret_t sys_pread ( struct registers * regs)

SYS_pread: read at an offset without moving the file offset.

args: fd, buffer, count, offset (low, high: syscall_arg_offset). returns bytes read, or negative errno (ESPIPE for a pipe or the console).

◆ sys_pwrite()

syscall_ret_t sys_pwrite ( struct registers * regs)

SYS_pwrite: write at an offset without moving the file offset.

args: fd, buffer, count, offset (low, high: syscall_arg_offset). returns bytes written, or negative errno (EINVAL with O_APPEND).

◆ sys_fsync()

syscall_ret_t sys_fsync ( struct registers * regs)

SYS_fsync: write an open file's data to disk.

args: EBX = fd. ext2 writes through to the disk at once (there is no cache), so this only checks the descriptor. returns 0 or -EBADF.

◆ sys_poll()

syscall_ret_t sys_poll ( struct registers * regs)

SYS_poll: wait until one of a set of descriptors is ready.

args: EBX = struct pollfd array, ECX = count (at most 64), EDX = timeout in milliseconds (negative: no limit, 0: do not wait). returns the number of descriptors with events, 0 on timeout, or a negative errno.

◆ sys_flock()

syscall_ret_t sys_flock ( struct registers * regs)

SYS_flock: an advisory lock on a whole file (kernel/vfs/flock.h).

args: fd, operation (LOCK_SH or LOCK_EX, with LOCK_NB; or LOCK_UN). returns 0, or a negative errno (EWOULDBLOCK, EINTR, EBADF, EINVAL).