AquilaOS documentation
How it works, and its code
Loading...
Searching...
No Matches
sys_file.c File Reference

System calls on files: opening, reading and writing them, their names and attributes, directories, links, mounts and locks. More...

#include "syscalls.h"

Go to the source code of this file.

Macros

#define F_OK   0
#define X_OK   1
#define W_OK   2
#define R_OK   4

Functions

static int at_uio (int dirfd, const char *path, struct uio *uio)
 Sets up the uio to look up path from the directory that dirfd refers to, or from the working directory for AT_FDCWD.
static int at_path (int dirfd, const char *upath, char **path, struct uio *uio)
 Copies a path into the kernel (*path, which the caller frees) and sets up the uio to look it up from dirfd, for the calls that take two paths.
static int at_lookup (int dirfd, const char *upath, int flags, struct uio *uio, struct vnode **vnode)
 Looks up a path from user memory, from dirfd; with AT_SYMLINK_NOFOLLOW in flags, a final symbolic link isn't followed.
long sys_close (int fildes)
 Implements close(): frees a descriptor.
long sys_fstat (int fildes, struct aquila_stat *buf)
 Implements fstat(): an open file's attributes.
long sys_isatty (int fildes)
 Says whether a descriptor refers to a character device, the only kind of file that can be a terminal.
static int do_linkat (int olddirfd, const char *oldpath, int newdirfd, const char *newpath, int flags)
 Makes a link, for link() and linkat().
long sys_link (const char *oldpath, const char *newpath)
 Implements link(): a new name for a file.
long sys_linkat (int olddirfd, const char *oldpath, int newdirfd, const char *newpath, int flags)
 Implements linkat(): link(), with paths relative to directories.
long sys_lseek (int fildes, off_t offset, int whence)
 Implements lseek(): moves an open file's offset.
static int creat_path (const char *path, struct uio *uio, char **ref)
 Finds what O_CREAT should create for path: if path is a dangling symbolic link, the file it points to, through links to links.
static int do_openat (int dirfd, const char *upath, int oflags, mode_t mode)
 Opens (or creates) a file on a new descriptor, for open() and openat().
long sys_open (const char *path, int oflags, mode_t mode)
 Implements open(): opens a file on a new descriptor, creating it with O_CREAT.
long sys_openat (int dirfd, const char *path, int oflags, mode_t mode)
 Implements openat(): open(), with the path relative to a directory.
long sys_read (int fildes, void *buf, size_t nbytes)
 Implements read().
static int do_fstatat (int dirfd, const char *path, struct aquila_stat *buf, int flags)
 Gets a file's attributes, for the stat() calls.
long sys_stat (const char *path, struct aquila_stat *buf)
 Implements stat(): a file's attributes, following a symbolic link.
long sys_fstatat (int dirfd, const char *path, struct aquila_stat *buf, int flags)
 Implements fstatat(): stat(), with the path relative to a directory.
static int do_unlinkat (int dirfd, const char *upath, int flags)
 Removes a name, or a directory with AT_REMOVEDIR, for unlink(), unlinkat() and rmdir().
long sys_unlink (const char *path)
 Implements unlink(): removes a name of a file (not a directory's).
long sys_unlinkat (int dirfd, const char *path, int flags)
 Implements unlinkat(): unlink(), or rmdir() with AT_REMOVEDIR, with the path relative to a directory.
long sys_write (int fd, void *buf, size_t nbytes)
 Implements write().
long sys_ioctl (int fd, int request, void *argp)
 Implements ioctl(): a device's, a socket's, or a terminal's request.
long sys_readdir (int fd, struct dirent *dirent)
 Reads a directory's next entry, for the C library's readdir().
long sys_mount (struct aquila_mount_args *args)
 Implements mount(): mounts a file system on a directory.
static int do_mknodat (int dirfd, const char *upath, mode_t mode, dev_t dev)
 Makes a file of any type, for mknod(), mknodat() and mkdir().
long sys_mkdir (const char *path, mode_t mode)
 Implements mkdir(): makes a directory.
long sys_pipe (int fd[2])
 Implements pipe(): makes a pipe, its read end and its write end open on two new descriptors.
long sys_fcntl (int fd, int cmd, uintptr_t arg)
 Implements fcntl(): duplicates a descriptor, or reads and sets the descriptor's flags (FD_CLOEXEC) or the open file's status flags.
static int cwd_set (struct vnode *vnode)
 Makes vnode the working directory, if it is a directory the process may search.
long sys_chdir (const char *upath)
 Implements chdir(): changes the working directory.
long sys_getcwd (char *buf, size_t size)
 Implements getcwd(): the working directory's path.
long sys_mknod (const char *path, uint32_t mode, uint32_t dev)
 Implements mknod(): makes a file, a directory, a FIFO, or (only root) a device file.
long sys_mknodat (int dirfd, const char *path, uint32_t mode, uint32_t dev)
 Implements mknodat(): mknod(), with the path relative to a directory.
long sys_lstat (const char *path, struct aquila_stat *buf)
 Implements lstat(): a file's attributes, a symbolic link's own.
long sys_ftruncate (int fildes, off_t len)
 Implements ftruncate(): sets an open file's size.
long sys_umask (mode_t mask)
 Implements umask(): sets the permissions that new files don't get.
static int do_fchmodat (int dirfd, const char *path, mode_t mode, int flags)
 Changes a file's permissions, for chmod() and fchmodat().
long sys_chmod (const char *path, mode_t mode)
 Implements chmod(): changes a file's permissions.
long sys_fchmodat (int dirfd, const char *path, mode_t mode, int flags)
 Implements fchmodat(): chmod(), with the path relative to a directory.
static struct vnode * fd_vnode (int fd, int *err)
 Returns the vnode of an open file, or NULL with *err set (-EBADF, or -EINVAL for a socket).
long sys_fchmod (int fd, mode_t mode)
 Implements fchmod(): changes an open file's permissions.
static int do_faccessat (int dirfd, const char *path, int mode, int flags)
 Checks a file's permissions, for access() and faccessat().
long sys_access (const char *path, int mode)
 Implements access(): says whether the process may read, write or execute a file.
long sys_faccessat (int dirfd, const char *path, int mode, int flags)
 Implements faccessat(): access(), with the path relative to a directory.
static int do_fchownat (int dirfd, const char *path, uid_t owner, gid_t group, int flags)
 Changes a file's owner and group, for the chown() calls.
long sys_chown (const char *path, uid_t owner, gid_t group)
 Implements chown(): changes a file's owner and group.
long sys_fchownat (int dirfd, const char *path, uid_t owner, gid_t group, int flags)
 Implements fchownat(): chown(), with the path relative to a directory.
long sys_fchown (int fd, uid_t owner, gid_t group)
 Implements fchown(): changes an open file's owner and group.
long sys_lchown (const char *path, uid_t owner, gid_t group)
 Implements lchown(): chown() of a symbolic link itself.
long sys_utimensat (int dirfd, const char *path, const struct aquila_timespec times[2], int flags)
 Implements utimensat() (and futimens()): sets a file's access and modification times.
long sys_rmdir (const char *path)
 Implements rmdir(): removes an empty directory.
long sys_symlinkat (const char *utarget, int dirfd, const char *upath)
 Implements symlinkat(): makes a symbolic link.
long sys_readlinkat (int dirfd, const char *upath, char *buf, size_t size)
 Implements readlinkat(): what a symbolic link points to (not NUL-terminated).
long sys_renameat (int olddirfd, const char *oldpath, int newdirfd, const char *newpath)
 Implements renameat() (and rename()): moves a file to another name, replacing what was there.
long sys_flock (int fd, int op)
 Implements flock(): takes or releases an advisory lock on an open file.
long sys_sync (void)
 Implements sync(): writes out what the devices' caches hold.
long sys_fchdir (int fd)
 Implements fchdir(): changes the working directory to an open directory.

Detailed Description

System calls on files: opening, reading and writing them, their names and attributes, directories, links, mounts and locks.

The *at() calls take paths relative to a directory descriptor, and the older calls are the same with AT_FDCWD, the working directory. Paths are copied into the kernel first (upath_get()); the work is the VFS's (fs/).

Definition in file sys_file.c.

Macro Definition Documentation

◆ F_OK

#define F_OK   0

Definition at line 1185 of file sys_file.c.

◆ R_OK

#define R_OK   4

Definition at line 1188 of file sys_file.c.

Referenced by do_faccessat().

◆ W_OK

#define W_OK   2

Definition at line 1187 of file sys_file.c.

Referenced by do_faccessat().

◆ X_OK

#define X_OK   1

Definition at line 1186 of file sys_file.c.

Referenced by do_faccessat().

Function Documentation

◆ at_lookup()

int at_lookup ( int dirfd,
const char * upath,
int flags,
struct uio * uio,
struct vnode ** vnode )
static

Looks up a path from user memory, from dirfd; with AT_SYMLINK_NOFOLLOW in flags, a final symbolic link isn't followed.

Returns 0 or a negative errno.

Definition at line 60 of file sys_file.c.

References at_lookup(), at_uio(), file::flags, uio::flags, kfree(), upath_get(), and vfs_lookup().

Referenced by at_lookup(), do_faccessat(), do_fchmodat(), do_fchownat(), do_fstatat(), and sys_utimensat().

◆ at_path()

int at_path ( int dirfd,
const char * upath,
char ** path,
struct uio * uio )
static

Copies a path into the kernel (*path, which the caller frees) and sets up the uio to look it up from dirfd, for the calls that take two paths.

Returns 0 or a negative errno.

Definition at line 42 of file sys_file.c.

References at_path(), at_uio(), kfree(), NULL, and upath_get().

Referenced by at_path(), do_linkat(), and sys_renameat().

◆ at_uio()

int at_uio ( int dirfd,
const char * path,
struct uio * uio )
static

Sets up the uio to look up path from the directory that dirfd refers to, or from the working directory for AT_FDCWD.

An absolute path ignores dirfd. Returns 0, -EBADF, or -ENOTDIR.

Definition at line 18 of file sys_file.c.

References at_uio(), curproc, uio::cwd, fd_file(), FILE_SOCKET, file::flags, and PROC_UIO.

Referenced by at_lookup(), at_path(), at_uio(), do_mknodat(), do_openat(), do_unlinkat(), sys_readlinkat(), and sys_symlinkat().

◆ creat_path()

int creat_path ( const char * path,
struct uio * uio,
char ** ref )
static

Finds what O_CREAT should create for path: if path is a dangling symbolic link, the file it points to, through links to links.

Sets *ref to that path (which the caller frees), or to NULL for path itself. Returns 0, -EEXIST if path exists, -ELOOP, or another negative errno.

Definition at line 248 of file sys_file.c.

References creat_path(), uio::flags, kfree(), kmalloc(), memcpy(), vnode::mode, NULL, vnode::p, vnode::ref, vfs_close(), vfs_lookup(), and vfs_readlink().

Referenced by creat_path(), and do_openat().

◆ cwd_set()

int cwd_set ( struct vnode * vnode)
static

Makes vnode the working directory, if it is a directory the process may search.

On success, the caller's reference to it becomes the process's. Returns 0, -ENOTDIR or -EACCES.

Definition at line 933 of file sys_file.c.

References curproc, cwd_set(), vnode::mode, PROC_UIO, vfs_access(), vfs_close(), and VFS_X.

Referenced by cwd_set(), sys_chdir(), and sys_fchdir().

◆ do_faccessat()

int do_faccessat ( int dirfd,
const char * path,
int mode,
int flags )
static

Checks a file's permissions, for access() and faccessat().

Returns 0 or a negative errno.

Definition at line 1192 of file sys_file.c.

References at_lookup(), do_faccessat(), uio::flags, NULL, R_OK, vfs_access(), vfs_close(), VFS_R, VFS_W, VFS_X, W_OK, and X_OK.

Referenced by do_faccessat(), sys_access(), and sys_faccessat().

◆ do_fchmodat()

int do_fchmodat ( int dirfd,
const char * path,
mode_t mode,
int flags )
static

Changes a file's permissions, for chmod() and fchmodat().

Returns 0 or a negative errno.

Definition at line 1101 of file sys_file.c.

References at_lookup(), do_fchmodat(), uio::flags, NULL, vfs_chmod(), and vfs_close().

Referenced by do_fchmodat(), sys_chmod(), and sys_fchmodat().

◆ do_fchownat()

int do_fchownat ( int dirfd,
const char * path,
uid_t owner,
gid_t group,
int flags )
static

Changes a file's owner and group, for the chown() calls.

Returns 0 or a negative errno.

Definition at line 1244 of file sys_file.c.

References at_lookup(), do_fchownat(), uio::flags, NULL, vfs_chown(), and vfs_close().

Referenced by do_fchownat(), sys_chown(), sys_fchownat(), and sys_lchown().

◆ do_fstatat()

int do_fstatat ( int dirfd,
const char * path,
struct aquila_stat * buf,
int flags )
static

Gets a file's attributes, for the stat() calls.

Returns 0 or a negative errno.

Definition at line 477 of file sys_file.c.

References at_lookup(), do_fstatat(), uio::flags, memset(), NULL, vfs_close(), and vfs_stat().

Referenced by do_fstatat(), sys_fstatat(), sys_lstat(), and sys_stat().

◆ do_linkat()

int do_linkat ( int olddirfd,
const char * oldpath,
int newdirfd,
const char * newpath,
int flags )
static

Makes a link, for link() and linkat().

Returns 0 or a negative errno.

Definition at line 157 of file sys_file.c.

References at_path(), do_linkat(), uio::flags, kfree(), NULL, and vfs_link().

Referenced by do_linkat(), sys_link(), and sys_linkat().

◆ do_mknodat()

int do_mknodat ( int dirfd,
const char * upath,
mode_t mode,
dev_t dev )
static

Makes a file of any type, for mknod(), mknodat() and mkdir().

Returns 0 or a negative errno.

Definition at line 754 of file sys_file.c.

References at_uio(), curproc, do_mknodat(), kfree(), NULL, upath_get(), and vfs_mknod().

Referenced by do_mknodat(), sys_mkdir(), sys_mknod(), and sys_mknodat().

◆ do_openat()

int do_openat ( int dirfd,
const char * upath,
int oflags,
mode_t mode )
static

Opens (or creates) a file on a new descriptor, for open() and openat().

Returns the descriptor or a negative errno.

Definition at line 311 of file sys_file.c.

References at_uio(), creat_path(), curproc, do_openat(), file_new(), file::flags, uio::flags, kfree(), vnode::mode, NULL, proc_fd_get(), proc_fd_install(), proc_fd_release(), upath_get(), vfs_close(), vfs_creat(), vfs_file_open(), vfs_lookup(), and vfs_perms_check().

Referenced by do_openat(), sys_open(), and sys_openat().

◆ do_unlinkat()

int do_unlinkat ( int dirfd,
const char * upath,
int flags )
static

Removes a name, or a directory with AT_REMOVEDIR, for unlink(), unlinkat() and rmdir().

Returns 0 or a negative errno.

Definition at line 528 of file sys_file.c.

References at_uio(), do_unlinkat(), uio::flags, kfree(), upath_get(), vfs_rmdir(), and vfs_unlink().

Referenced by do_unlinkat(), sys_rmdir(), sys_unlink(), and sys_unlinkat().

◆ fd_vnode()

struct vnode * fd_vnode ( int fd,
int * err )
static

Returns the vnode of an open file, or NULL with *err set (-EBADF, or -EINVAL for a socket).

Definition at line 1153 of file sys_file.c.

References fd_file(), fd_vnode(), FILE_SOCKET, file::flags, and NULL.

Referenced by fd_vnode(), sys_fchmod(), sys_fchown(), and sys_utimensat().

◆ sys_access()

long sys_access ( const char * path,
int mode )

Implements access(): says whether the process may read, write or execute a file.

Parameters
paththe file's path
modeF_OK, or R_OK, W_OK and X_OK together
Returns
0 if it may, or a negative errno: -EACCES, -ENOENT, ...

Definition at line 1220 of file sys_file.c.

References do_faccessat(), LOG_DEBUG, and sys_access().

Referenced by sys_access().

◆ sys_chdir()

long sys_chdir ( const char * upath)

Implements chdir(): changes the working directory.

Parameters
upaththe directory's path
Returns
0, or a negative errno: -ENOENT, -ENOTDIR, -EACCES, ...

Definition at line 956 of file sys_file.c.

References curproc, cwd_set(), kfree(), LOG_DEBUG, NULL, PROC_UIO, sys_chdir(), upath_get(), vfs_close(), and vfs_lookup().

Referenced by sys_chdir().

◆ sys_chmod()

long sys_chmod ( const char * path,
mode_t mode )

Implements chmod(): changes a file's permissions.

Parameters
paththe file's path
modethe new permissions
Returns
0, or a negative errno: -ENOENT, -EPERM, ...

Definition at line 1129 of file sys_file.c.

References do_fchmodat(), LOG_DEBUG, and sys_chmod().

Referenced by sys_chmod().

◆ sys_chown()

long sys_chown ( const char * path,
uid_t owner,
gid_t group )

Implements chown(): changes a file's owner and group.

Parameters
paththe file's path
ownerthe new owner, or -1 to keep it
groupthe new group, or -1 to keep it
Returns
0, or a negative errno: -EPERM, -ENOENT, ...

Definition at line 1269 of file sys_file.c.

References do_fchownat(), LOG_DEBUG, and sys_chown().

Referenced by sys_chown().

◆ sys_close()

long sys_close ( int fildes)

Implements close(): frees a descriptor.

The open file is closed once nothing else refers to it (another descriptor, or a call using it).

Parameters
fildesthe descriptor
Returns
0, -EBADF, or an error from closing the file

Definition at line 86 of file sys_file.c.

References curproc, LOG_DEBUG, proc_fd_close(), and sys_close().

Referenced by sys_close().

◆ sys_faccessat()

long sys_faccessat ( int dirfd,
const char * path,
int mode,
int flags )

Implements faccessat(): access(), with the path relative to a directory.

Parameters
dirfdthe directory, or AT_FDCWD
paththe file's path
modeF_OK, or R_OK, W_OK and X_OK together
flagsAT_EACCESS (which changes nothing), AT_SYMLINK_NOFOLLOW
Returns
0 if it may, or a negative errno

Definition at line 1236 of file sys_file.c.

References do_faccessat(), uio::flags, LOG_DEBUG, and sys_faccessat().

Referenced by sys_faccessat().

◆ sys_fchdir()

long sys_fchdir ( int fd)

Implements fchdir(): changes the working directory to an open directory.

Parameters
fdthe directory's descriptor
Returns
0, or a negative errno: -EBADF, -ENOTDIR, -EACCES

Definition at line 1523 of file sys_file.c.

References cwd_set(), fd_file(), FILE_SOCKET, file::flags, LOG_DEBUG, file::ref, sys_fchdir(), and vfs_close().

Referenced by sys_fchdir().

◆ sys_fchmod()

long sys_fchmod ( int fd,
mode_t mode )

Implements fchmod(): changes an open file's permissions.

Parameters
fdthe descriptor
modethe new permissions
Returns
0, or a negative errno: -EBADF, -EINVAL, -EPERM

Definition at line 1172 of file sys_file.c.

References curproc, fd_vnode(), LOG_DEBUG, vnode::mode, PROC_UIO, sys_fchmod(), and vfs_chmod().

Referenced by sys_fchmod().

◆ sys_fchmodat()

long sys_fchmodat ( int dirfd,
const char * path,
mode_t mode,
int flags )

Implements fchmodat(): chmod(), with the path relative to a directory.

Parameters
dirfdthe directory, or AT_FDCWD
paththe file's path
modethe new permissions
flags0 (AT_SYMLINK_NOFOLLOW isn't supported)
Returns
0, or a negative errno

Definition at line 1145 of file sys_file.c.

References do_fchmodat(), uio::flags, LOG_DEBUG, and sys_fchmodat().

Referenced by sys_fchmodat().

◆ sys_fchown()

long sys_fchown ( int fd,
uid_t owner,
gid_t group )

Implements fchown(): changes an open file's owner and group.

Parameters
fdthe descriptor
ownerthe new owner, or -1
groupthe new group, or -1
Returns
0, or a negative errno: -EBADF, -EINVAL, -EPERM

Definition at line 1301 of file sys_file.c.

References curproc, fd_vnode(), LOG_DEBUG, PROC_UIO, sys_fchown(), and vfs_chown().

Referenced by sys_fchown().

◆ sys_fchownat()

long sys_fchownat ( int dirfd,
const char * path,
uid_t owner,
gid_t group,
int flags )

Implements fchownat(): chown(), with the path relative to a directory.

Parameters
dirfdthe directory, or AT_FDCWD
paththe file's path
ownerthe new owner, or -1
groupthe new group, or -1
flagsAT_SYMLINK_NOFOLLOW for a symbolic link's own
Returns
0, or a negative errno

Definition at line 1286 of file sys_file.c.

References do_fchownat(), uio::flags, LOG_DEBUG, and sys_fchownat().

Referenced by sys_fchownat().

◆ sys_fcntl()

long sys_fcntl ( int fd,
int cmd,
uintptr_t arg )

Implements fcntl(): duplicates a descriptor, or reads and sets the descriptor's flags (FD_CLOEXEC) or the open file's status flags.

Parameters
fdthe descriptor
cmdF_DUPFD, F_DUPFD_CLOEXEC, F_GETFD, F_SETFD, F_GETFL or F_SETFL
argthe command's argument
Returns
what the command returns, or a negative errno: -EBADF, -EINVAL, -EMFILE

Definition at line 876 of file sys_file.c.

References curproc, fd_file(), FDS_COUNT, file_ref(), FILE_SOCKET, file::flags, LOG_DEBUG, proc_fd_install(), and sys_fcntl().

Referenced by sys_fcntl().

◆ sys_flock()

long sys_flock ( int fd,
int op )

Implements flock(): takes or releases an advisory lock on an open file.

Parameters
fdthe descriptor
opLOCK_SH, LOCK_EX or LOCK_UN, with LOCK_NB to not wait
Returns
0, or a negative errno: -EBADF, -EWOULDBLOCK, -EINTR, ...

Definition at line 1490 of file sys_file.c.

References fd_file(), LOG_DEBUG, sys_flock(), and vfs_file_flock().

Referenced by sys_flock().

◆ sys_fstat()

long sys_fstat ( int fildes,
struct aquila_stat * buf )

Implements fstat(): an open file's attributes.

A socket only reports its type.

Parameters
fildesthe descriptor
bufwhere to put them
Returns
0, or a negative errno: -EBADF, -EFAULT

Definition at line 103 of file sys_file.c.

References fd_file(), FILE_SOCKET, file::flags, LOG_DEBUG, memset(), sys_fstat(), and vfs_stat().

Referenced by sys_fstat().

◆ sys_fstatat()

long sys_fstatat ( int dirfd,
const char * path,
struct aquila_stat * buf,
int flags )

Implements fstatat(): stat(), with the path relative to a directory.

Parameters
dirfdthe directory, or AT_FDCWD
paththe file's path
bufwhere to put its attributes
flagsAT_SYMLINK_NOFOLLOW for a symbolic link's own
Returns
0, or a negative errno

Definition at line 520 of file sys_file.c.

References do_fstatat(), LOG_DEBUG, and sys_fstatat().

Referenced by sys_fstatat().

◆ sys_ftruncate()

long sys_ftruncate ( int fildes,
off_t len )

Implements ftruncate(): sets an open file's size.

Parameters
fildesthe descriptor, open for writing
lenthe new size
Returns
0, or a negative errno: -EBADF, -EINVAL, ...

Definition at line 1065 of file sys_file.c.

References fd_file(), file::flags, LOG_DEBUG, sys_ftruncate(), and vfs_file_trunc().

Referenced by sys_ftruncate().

◆ sys_getcwd()

long sys_getcwd ( char * buf,
size_t size )

Implements getcwd(): the working directory's path.

Parameters
bufwhere to put it
sizethe room at buf
Returns
0, or a negative errno: -EINVAL, -ERANGE if it doesn't fit, -ENOMEM, -EFAULT

Definition at line 985 of file sys_file.c.

References curproc, kfree(), kmalloc(), LOG_DEBUG, MIN, NULL, vnode::size, sys_getcwd(), UPATH_MAX, and vfs_vnode_path().

Referenced by sys_getcwd().

◆ sys_ioctl()

long sys_ioctl ( int fd,
int request,
void * argp )

Implements ioctl(): a device's, a socket's, or a terminal's request.

FIONBIO, which sets the descriptor's O_NONBLOCK, works on any file.

Parameters
fdthe descriptor
requestthe request
argpits argument, in user memory
Returns
what the request returns, or a negative errno: -EBADF, -ENOTTY for a request the file doesn't know, -EFAULT

Definition at line 622 of file sys_file.c.

References fd_file(), FILE_SOCKET, file::flags, LOG_DEBUG, socket_ioctl(), sys_ioctl(), and vfs_file_ioctl().

Referenced by sys_ioctl().

◆ sys_isatty()

long sys_isatty ( int fildes)

Says whether a descriptor refers to a character device, the only kind of file that can be a terminal.

(The C library's isatty() asks the device with TCGETS instead.)

Parameters
fildesthe descriptor
Returns
1, -ENOTTY, or -EBADF

Definition at line 137 of file sys_file.c.

References fd_file(), FILE_SOCKET, file::flags, LOG_DEBUG, and sys_isatty().

Referenced by sys_isatty().

◆ sys_lchown()

long sys_lchown ( const char * path,
uid_t owner,
gid_t group )

Implements lchown(): chown() of a symbolic link itself.

Parameters
paththe link's path
ownerthe new owner, or -1
groupthe new group, or -1
Returns
0, or a negative errno

Definition at line 1322 of file sys_file.c.

References do_fchownat(), LOG_DEBUG, and sys_lchown().

Referenced by sys_lchown().

◆ sys_link()

long sys_link ( const char * oldpath,
const char * newpath )

Implements link(): a new name for a file.

Parameters
oldpaththe file's path
newpaththe new name
Returns
0, or a negative errno: -ENOENT, -EEXIST, -EPERM, -EXDEV, -EACCES, ...

Definition at line 188 of file sys_file.c.

References do_linkat(), LOG_DEBUG, and sys_link().

Referenced by sys_link().

◆ sys_linkat()

long sys_linkat ( int olddirfd,
const char * oldpath,
int newdirfd,
const char * newpath,
int flags )

Implements linkat(): link(), with paths relative to directories.

Parameters
olddirfdthe directory oldpath is relative to, or AT_FDCWD
oldpaththe file's path
newdirfdthe directory newpath is relative to, or AT_FDCWD
newpaththe new name
flagsAT_SYMLINK_FOLLOW to link what a symbolic link points to
Returns
0, or a negative errno

Definition at line 205 of file sys_file.c.

References do_linkat(), uio::flags, LOG_DEBUG, and sys_linkat().

Referenced by sys_linkat().

◆ sys_lseek()

long sys_lseek ( int fildes,
off_t offset,
int whence )

Implements lseek(): moves an open file's offset.

Parameters
fildesthe descriptor
offsetthe offset
whenceSEEK_SET, SEEK_CUR or SEEK_END
Returns
the new offset, or a negative errno: -EBADF, -ESPIPE (a socket, a pipe, a terminal), -EINVAL

Definition at line 221 of file sys_file.c.

References fd_file(), FILE_SOCKET, file::flags, LOG_DEBUG, file::offset, sys_lseek(), and vfs_file_lseek().

Referenced by sys_lseek().

◆ sys_lstat()

long sys_lstat ( const char * path,
struct aquila_stat * buf )

Implements lstat(): a file's attributes, a symbolic link's own.

Parameters
paththe file's path
bufwhere to put them
Returns
0, or a negative errno

Definition at line 1052 of file sys_file.c.

References do_fstatat(), LOG_DEBUG, and sys_lstat().

Referenced by sys_lstat().

◆ sys_mkdir()

long sys_mkdir ( const char * path,
mode_t mode )

Implements mkdir(): makes a directory.

Parameters
pathits path
modeits permissions (less the umask)
Returns
0, or a negative errno: -EEXIST, -ENOENT, -EACCES, ...

Definition at line 797 of file sys_file.c.

References do_mknodat(), LOG_DEBUG, and sys_mkdir().

Referenced by sys_mkdir().

◆ sys_mknod()

long sys_mknod ( const char * path,
uint32_t mode,
uint32_t dev )

Implements mknod(): makes a file, a directory, a FIFO, or (only root) a device file.

Parameters
pathits path
modeits type and permissions
deva device file's device number
Returns
0, or a negative errno: -EEXIST, -EPERM, -EINVAL, ...

Definition at line 1023 of file sys_file.c.

References do_mknodat(), LOG_DEBUG, vnode::mode, and sys_mknod().

Referenced by sys_mknod().

◆ sys_mknodat()

long sys_mknodat ( int dirfd,
const char * path,
uint32_t mode,
uint32_t dev )

Implements mknodat(): mknod(), with the path relative to a directory.

Parameters
dirfdthe directory, or AT_FDCWD
paththe new file's path
modeits type and permissions
deva device file's device number
Returns
0, or a negative errno

Definition at line 1039 of file sys_file.c.

References do_mknodat(), LOG_DEBUG, vnode::mode, and sys_mknodat().

Referenced by sys_mknodat().

◆ sys_mount()

long sys_mount ( struct aquila_mount_args * args)

Implements mount(): mounts a file system on a directory.

Only root may.

Parameters
argsthe arguments, in user memory: the file system's type, the directory, flags, and the data (the device and the options)
Returns
0, or a negative errno: -EPERM (not root), -ENOENT, -ENODEV, -EFAULT, ...

Definition at line 706 of file sys_file.c.

References curproc, kfree(), LOG_DEBUG, NULL, PROC_UIO, sys_mount(), upath_get(), and vfs_mount().

Referenced by sys_mount().

◆ sys_open()

long sys_open ( const char * path,
int oflags,
mode_t mode )

Implements open(): opens a file on a new descriptor, creating it with O_CREAT.

Parameters
paththe file's path
oflagsO_* flags
modethe new file's permissions, with O_CREAT (less the umask)
Returns
the descriptor, or a negative errno: -ENOENT, -EACCES, -EEXIST, -EMFILE, -ENOTDIR, -ELOOP, ...

Definition at line 424 of file sys_file.c.

References do_openat(), LOG_DEBUG, and sys_open().

Referenced by sys_open().

◆ sys_openat()

long sys_openat ( int dirfd,
const char * path,
int oflags,
mode_t mode )

Implements openat(): open(), with the path relative to a directory.

Parameters
dirfdthe directory, or AT_FDCWD
paththe file's path
oflagsO_* flags
modethe new file's permissions, with O_CREAT
Returns
the descriptor, or a negative errno

Definition at line 439 of file sys_file.c.

References do_openat(), LOG_DEBUG, and sys_openat().

Referenced by sys_openat().

◆ sys_pipe()

long sys_pipe ( int fd[2])

Implements pipe(): makes a pipe, its read end and its write end open on two new descriptors.

Parameters
fdwhere to put the two descriptors
Returns
0, or a negative errno: -EMFILE, -ENOMEM, -EFAULT

Definition at line 810 of file sys_file.c.

References curproc, file_new(), kfree(), LOG_DEBUG, NULL, pipefs_pipe(), proc_fd_close(), proc_fd_get(), proc_fd_install(), proc_fd_release(), sys_pipe(), and uaccess_check().

Referenced by sys_pipe().

◆ sys_read()

long sys_read ( int fildes,
void * buf,
size_t nbytes )

Implements read().

Parameters
fildesthe descriptor
bufwhere to put the data
nbyteshow much to read, at most
Returns
how many bytes were read (0 at the end), or a negative errno: -EBADF, -EFAULT, -EAGAIN, -EINTR, ...

Definition at line 454 of file sys_file.c.

References fd_file(), iobuf_init(), LOG_DEBUG, sys_read(), uaccess_check(), and vfs_file_read().

Referenced by sys_read().

◆ sys_readdir()

long sys_readdir ( int fd,
struct dirent * dirent )

Reads a directory's next entry, for the C library's readdir().

Parameters
fdthe directory's descriptor
direntwhere to put the entry
Returns
a positive number if there was an entry, 0 at the end, or a negative errno: -EBADF, -ENOTDIR, -EFAULT

Definition at line 667 of file sys_file.c.

References fd_file(), FILE_SOCKET, file::flags, LOG_DEBUG, memset(), sys_readdir(), and vfs_file_readdir().

Referenced by sys_readdir().

◆ sys_readlinkat()

long sys_readlinkat ( int dirfd,
const char * upath,
char * buf,
size_t size )

Implements readlinkat(): what a symbolic link points to (not NUL-terminated).

Parameters
dirfdthe directory upath is relative to, or AT_FDCWD
upaththe link's path
bufwhere to put the target
sizethe room at buf; a longer target is cut
Returns
the target's length, or a negative errno: -EINVAL (not a link), -ENOENT, -EFAULT, ...

Definition at line 1426 of file sys_file.c.

References at_uio(), kfree(), kmalloc(), LOG_DEBUG, MIN, NULL, sys_readlinkat(), uaccess_check(), upath_get(), UPATH_MAX, and vfs_readlink().

Referenced by sys_readlinkat().

◆ sys_renameat()

long sys_renameat ( int olddirfd,
const char * oldpath,
int newdirfd,
const char * newpath )

Implements renameat() (and rename()): moves a file to another name, replacing what was there.

Parameters
olddirfdthe directory oldpath is relative to, or AT_FDCWD
oldpaththe file's path
newdirfdthe directory newpath is relative to, or AT_FDCWD
newpathits new path
Returns
0, or a negative errno: -ENOENT, -EXDEV, -EISDIR, -ENOTEMPTY, -EINVAL, ...

Definition at line 1464 of file sys_file.c.

References at_path(), kfree(), LOG_DEBUG, NULL, sys_renameat(), and vfs_rename().

Referenced by sys_renameat().

◆ sys_rmdir()

long sys_rmdir ( const char * path)

Implements rmdir(): removes an empty directory.

Parameters
paththe directory's path
Returns
0, or a negative errno: -ENOTEMPTY, -ENOTDIR, -EBUSY, ...

Definition at line 1383 of file sys_file.c.

References do_unlinkat(), LOG_DEBUG, and sys_rmdir().

Referenced by sys_rmdir().

◆ sys_stat()

long sys_stat ( const char * path,
struct aquila_stat * buf )

Implements stat(): a file's attributes, following a symbolic link.

Parameters
paththe file's path
bufwhere to put them
Returns
0, or a negative errno: -ENOENT, -EACCES, -EFAULT, ...

Definition at line 504 of file sys_file.c.

References do_fstatat(), LOG_DEBUG, and sys_stat().

Referenced by sys_stat().

◆ sys_symlinkat()

long sys_symlinkat ( const char * utarget,
int dirfd,
const char * upath )

Implements symlinkat(): makes a symbolic link.

Parameters
utargetwhat it points to
dirfdthe directory upath is relative to, or AT_FDCWD
upaththe link's path
Returns
0, or a negative errno: -EEXIST, -ENOENT, -EACCES, ...

Definition at line 1397 of file sys_file.c.

References at_uio(), kfree(), LOG_DEBUG, NULL, sys_symlinkat(), upath_get(), and vfs_symlink().

Referenced by sys_symlinkat().

◆ sys_sync()

long sys_sync ( void )

Implements sync(): writes out what the devices' caches hold.

Returns
0, or a negative errno from a device

Definition at line 1508 of file sys_file.c.

References kdev_sync(), LOG_DEBUG, and sys_sync().

Referenced by sys_sync().

◆ sys_umask()

long sys_umask ( mode_t mask)

Implements umask(): sets the permissions that new files don't get.

Parameters
maskthe new mask
Returns
the old mask

Definition at line 1089 of file sys_file.c.

References curproc, LOG_DEBUG, and sys_umask().

Referenced by sys_umask().

◆ sys_unlink()

long sys_unlink ( const char * path)

Implements unlink(): removes a name of a file (not a directory's).

Parameters
paththe name
Returns
0, or a negative errno: -ENOENT, -EISDIR, -EACCES, ...

Definition at line 553 of file sys_file.c.

References do_unlinkat(), LOG_DEBUG, and sys_unlink().

Referenced by sys_unlink().

◆ sys_unlinkat()

long sys_unlinkat ( int dirfd,
const char * path,
int flags )

Implements unlinkat(): unlink(), or rmdir() with AT_REMOVEDIR, with the path relative to a directory.

Parameters
dirfdthe directory, or AT_FDCWD
paththe name
flagsAT_REMOVEDIR to remove a directory
Returns
0, or a negative errno

Definition at line 568 of file sys_file.c.

References do_unlinkat(), uio::flags, LOG_DEBUG, and sys_unlinkat().

Referenced by sys_unlinkat().

◆ sys_utimensat()

long sys_utimensat ( int dirfd,
const char * path,
const struct aquila_timespec times[2],
int flags )

Implements utimensat() (and futimens()): sets a file's access and modification times.

Parameters
dirfdthe directory path is relative to, or AT_FDCWD; with a NULL path, the file itself (futimens())
paththe file's path, or NULL
timesthe two times, or NULL for now
flagsAT_SYMLINK_NOFOLLOW for a symbolic link's own
Returns
0, or a negative errno: -EINVAL, -EFAULT, -ENOENT, -EPERM, ...

Definition at line 1340 of file sys_file.c.

References at_lookup(), curproc, fd_vnode(), uio::flags, LOG_DEBUG, NULL, PROC_UIO, sys_utimensat(), vfs_close(), and vfs_utimens().

Referenced by sys_utimensat().

◆ sys_write()

long sys_write ( int fd,
void * buf,
size_t nbytes )

Implements write().

Writing to a pipe that no one reads also raises SIGPIPE.

Parameters
fdthe descriptor
bufthe data
nbyteshow much to write
Returns
how many bytes were written, or a negative errno: -EBADF, -EFAULT, -EPIPE, -EAGAIN, -ENOSPC, ...

Definition at line 584 of file sys_file.c.

References curproc, fd_file(), iobuf_init(), LOG_DEBUG, signal_proc_send(), sys_write(), uaccess_check(), and vfs_file_write().

Referenced by sys_write().