diff options
author | dzwdz | 2021-09-04 15:25:40 +0200 |
---|---|---|
committer | dzwdz | 2021-09-04 15:25:40 +0200 |
commit | 57ce367309de9ac1b1938202156eac554420bb2a (patch) | |
tree | 7be24257975d47a7db6e4c5a999b6e1951142ff3 | |
parent | a5bd09d5a995400c4f4ec1270e1ad380d238783c (diff) |
rename file descriptors to handles
-rw-r--r-- | src/init/syscalls.c | 18 | ||||
-rw-r--r-- | src/kernel/fd.h | 47 | ||||
-rw-r--r-- | src/kernel/handle.c (renamed from src/kernel/fd.c) | 32 | ||||
-rw-r--r-- | src/kernel/handle.h | 47 | ||||
-rw-r--r-- | src/kernel/proc.c | 4 | ||||
-rw-r--r-- | src/kernel/proc.h | 4 | ||||
-rw-r--r-- | src/kernel/syscalls.c | 64 | ||||
-rw-r--r-- | src/kernel/vfs/mount.c | 4 | ||||
-rw-r--r-- | src/kernel/vfs/mount.h | 4 | ||||
-rw-r--r-- | src/shared/syscalls.h | 13 |
10 files changed, 118 insertions, 119 deletions
diff --git a/src/init/syscalls.c b/src/init/syscalls.c index 7963da2..ebf174e 100644 --- a/src/init/syscalls.c +++ b/src/init/syscalls.c @@ -17,22 +17,22 @@ int _syscall_await(user_ptr buf, int len) { return _syscall(_SYSCALL_AWAIT, (int)buf, (int)len, 0); } -fd_t _syscall_fs_open(const user_ptr path, int len) { +handle_t _syscall_fs_open(const user_ptr path, int len) { return _syscall(_SYSCALL_FS_OPEN, (int)path, len, 0); } -int _syscall_fd_mount(fd_t fd, const user_ptr path, int len) { - return _syscall(_SYSCALL_FD_MOUNT, fd, (int)path, len); +int _syscall_fd_mount(handle_t handle, const user_ptr path, int len) { + return _syscall(_SYSCALL_FD_MOUNT, handle, (int)path, len); } -int _syscall_fd_read(fd_t fd, user_ptr buf, int len) { - return _syscall(_SYSCALL_FD_READ, fd, (int)buf, len); +int _syscall_fd_read(handle_t handle, user_ptr buf, int len) { + return _syscall(_SYSCALL_FD_READ, handle, (int)buf, len); } -int _syscall_fd_write(fd_t fd, user_ptr buf, int len) { - return _syscall(_SYSCALL_FD_WRITE, fd, (int)buf, len); +int _syscall_fd_write(handle_t handle, user_ptr buf, int len) { + return _syscall(_SYSCALL_FD_WRITE, handle, (int)buf, len); } -int _syscall_fd_close(fd_t fd) { - return _syscall(_SYSCALL_FD_CLOSE, fd, 0, 0); +int _syscall_fd_close(handle_t handle) { + return _syscall(_SYSCALL_FD_CLOSE, handle, 0, 0); } diff --git a/src/kernel/fd.h b/src/kernel/fd.h deleted file mode 100644 index c7c5182..0000000 --- a/src/kernel/fd.h +++ /dev/null @@ -1,47 +0,0 @@ -#pragma once -#include <kernel/types.h> -#include <stddef.h> - -#define FD_MAX 16 - -typedef int fd_t; // TODO duplicated in syscalls.h - -enum fd_type { - FD_EMPTY, - FD_SPECIAL_TTY, -}; - -struct fd { - enum fd_type type; -}; - - -enum fdop { // describes the operations which can be done on file descriptors - FDOP_MOUNT, // also closes the original fd - FDOP_OPEN, // when the file descriptor is mounted, open a file relative to it - - FDOP_READ, - FDOP_WRITE, - FDOP_CLOSE, -}; - -struct fdop_args { - enum fdop type; - struct fd *fd; - union { - struct { // FDOP_MOUNT - struct mount *target; - } mnt; - struct { // FDOP_OPEN - struct fd *target; - const char *path; // relative to the mount point - size_t len; - } open; - struct { // FDOP_READ, FDOP_WRITE - user_ptr ptr; - size_t len; - } rw; - }; -}; - -int fdop_dispatch(struct fdop_args args); diff --git a/src/kernel/fd.c b/src/kernel/handle.c index e875061..6e25b74 100644 --- a/src/kernel/fd.c +++ b/src/kernel/handle.c @@ -1,43 +1,43 @@ -#include <kernel/fd.h> +#include <kernel/handle.h> #include <kernel/mem.h> #include <kernel/panic.h> #include <kernel/proc.h> -static int fdop_special_tty(struct fdop_args *args); +static int handleop_special_tty(struct handleop_args *args); -int fdop_dispatch(struct fdop_args args) { - switch(args.fd->type) { - case FD_EMPTY: { - if (args.type == FDOP_MOUNT) // mounting an empty fd is allowed +int handleop_dispatch(struct handleop_args args) { + switch(args.handle->type) { + case HANDLE_EMPTY: { + if (args.type == HANDLEOP_MOUNT) // mounting an empty handle is allowed return 0; return -1; } - case FD_SPECIAL_TTY: - return fdop_special_tty(&args); + case HANDLE_SPECIAL_TTY: + return handleop_special_tty(&args); default: panic(); } } -static int fdop_special_tty(struct fdop_args *args) { +static int handleop_special_tty(struct handleop_args *args) { switch(args->type) { - case FDOP_MOUNT: + case HANDLEOP_MOUNT: return 0; // no special action needed - case FDOP_OPEN: + case HANDLEOP_OPEN: /* don't allow anything after the mount point * this is a file, not a directory * for example: open("/tty") is allowed. open("/tty/smth") isn't */ if (args->open.len == 0) { - args->open.target->type = FD_SPECIAL_TTY; + args->open.target->type = HANDLE_SPECIAL_TTY; return 0; } return -1; - case FDOP_READ: + case HANDLEOP_READ: return -1; // input not implemented yet - case FDOP_WRITE: { + case HANDLEOP_WRITE: { struct virt_iter iter; virt_iter_new(&iter, args->rw.ptr, args->rw.len, process_current->pages, true, false); @@ -46,8 +46,8 @@ static int fdop_special_tty(struct fdop_args *args) { return iter.prior; } - case FDOP_CLOSE: - args->fd->type = FD_EMPTY; + case HANDLEOP_CLOSE: + args->handle->type = HANDLE_EMPTY; return 0; default: panic(); diff --git a/src/kernel/handle.h b/src/kernel/handle.h new file mode 100644 index 0000000..4b066ad --- /dev/null +++ b/src/kernel/handle.h @@ -0,0 +1,47 @@ +#pragma once +#include <kernel/types.h> +#include <stddef.h> + +#define HANDLE_MAX 16 + +typedef int handle_t; // TODO duplicated in syscalls.h + +enum handle_type { + HANDLE_EMPTY, + HANDLE_SPECIAL_TTY, +}; + +struct handle { + enum handle_type type; +}; + + +enum handleop { // describes the operations which can be done on handles + HANDLEOP_MOUNT, // also closes the original handle + HANDLEOP_OPEN, + + HANDLEOP_READ, + HANDLEOP_WRITE, + HANDLEOP_CLOSE, +}; + +struct handleop_args { + enum handleop type; + struct handle *handle; + union { + struct { // HANDLEOP_MOUNT + struct mount *target; + } mnt; + struct { // HANDLEOP_OPEN + struct handle *target; + const char *path; // relative to the mount point + size_t len; + } open; + struct { // HANDLEOP_READ, HANDLEOP_WRITE + user_ptr ptr; + size_t len; + } rw; + }; +}; + +int handleop_dispatch(struct handleop_args args); diff --git a/src/kernel/proc.c b/src/kernel/proc.c index f45ff2b..19b09a8 100644 --- a/src/kernel/proc.c +++ b/src/kernel/proc.c @@ -22,8 +22,8 @@ struct process *process_seed(void) { process_first = proc; - for (int i = 0; i < FD_MAX; i++) - proc->fds[i].type = FD_EMPTY; + for (int i = 0; i < HANDLE_MAX; i++) + proc->handles[i].type = HANDLE_EMPTY; // map the stack to the last page in memory pagedir_map(proc->pages, ~PAGE_MASK, page_alloc(1), true, true); diff --git a/src/kernel/proc.h b/src/kernel/proc.h index 6de270b..ed7c656 100644 --- a/src/kernel/proc.h +++ b/src/kernel/proc.h @@ -1,6 +1,6 @@ #pragma once #include <kernel/arch/generic.h> -#include <kernel/fd.h> +#include <kernel/handle.h> #include <kernel/vfs/mount.h> enum process_state { @@ -30,7 +30,7 @@ struct process { struct vfs_mount *mount; - struct fd fds[FD_MAX]; + struct handle handles[HANDLE_MAX]; }; extern struct process *process_first; diff --git a/src/kernel/syscalls.c b/src/kernel/syscalls.c index 19499d1..a869af5 100644 --- a/src/kernel/syscalls.c +++ b/src/kernel/syscalls.c @@ -66,20 +66,20 @@ int _syscall_fork(void) { return 1; } -fd_t _syscall_fs_open(const user_ptr path, int len) { +handle_t _syscall_fs_open(const user_ptr path, int len) { struct virt_iter iter; struct vfs_mount *mount; static char buffer[PATH_MAX]; // holds the path - int fd, res; + int handle, res; if (len > PATH_MAX) return -1; - // find the first free fd - for (fd = 0; fd < FD_MAX; fd++) { - if (process_current->fds[fd].type == FD_EMPTY) + // find the first free handle + for (handle = 0; handle < HANDLE_MAX; handle++) { + if (process_current->handles[handle].type == HANDLE_EMPTY) break; } - if (fd == FD_MAX) return -1; + if (handle == HANDLE_MAX) return -1; // copy the path to the kernel virt_iter_new(&iter, path, len, process_current->pages, true, false); @@ -93,11 +93,11 @@ fd_t _syscall_fs_open(const user_ptr path, int len) { mount = vfs_mount_resolve(process_current->mount, buffer, len); if (!mount) return -1; - res = fdop_dispatch((struct fdop_args){ - .type = FDOP_OPEN, - .fd = &mount->fd, + res = handleop_dispatch((struct handleop_args){ + .type = HANDLEOP_OPEN, + .handle = &mount->handle, .open = { - .target = &process_current->fds[fd], + .target = &process_current->handles[handle], .path = &buffer[mount->prefix_len], .len = len - mount->prefix_len, } @@ -105,17 +105,17 @@ fd_t _syscall_fs_open(const user_ptr path, int len) { if (res < 0) return res; else - return fd; + return handle; } -int _syscall_fd_mount(fd_t fd, const user_ptr path, int len) { +int _syscall_fd_mount(handle_t handle, const user_ptr path, int len) { struct virt_iter iter; struct vfs_mount *mount; char *path_buf; int res; if (len > PATH_MAX) return -1; - if (fd < 0 || fd >= FD_MAX) return -1; + if (handle < 0 || handle >= HANDLE_MAX) return -1; // copy the path to the kernel path_buf = kmalloc(len); @@ -134,11 +134,11 @@ int _syscall_fd_mount(fd_t fd, const user_ptr path, int len) { mount->prev = process_current->mount; mount->prefix = path_buf; mount->prefix_len = len; - memcpy(&mount->fd, &process_current->fds[fd], sizeof(struct fd)); + memcpy(&mount->handle, &process_current->handles[handle], sizeof(struct handle)); - res = fdop_dispatch((struct fdop_args){ - .type = FDOP_MOUNT, - .fd = &process_current->fds[fd], + res = handleop_dispatch((struct handleop_args){ + .type = HANDLEOP_MOUNT, + .handle = &process_current->handles[handle], .mnt = {mount}, }); if (res < 0) goto fail; @@ -150,29 +150,29 @@ fail: return -1; } -int _syscall_fd_read(fd_t fd, user_ptr buf, int len) { - if (fd < 0 || fd >= FD_MAX) return -1; - return fdop_dispatch((struct fdop_args){ - .type = FDOP_READ, - .fd = &process_current->fds[fd], +int _syscall_fd_read(handle_t handle, user_ptr buf, int len) { + if (handle < 0 || handle >= HANDLE_MAX) return -1; + return handleop_dispatch((struct handleop_args){ + .type = HANDLEOP_READ, + .handle = &process_current->handles[handle], .rw = {buf, len} }); } -int _syscall_fd_write(fd_t fd, user_ptr buf, int len) { - if (fd < 0 || fd >= FD_MAX) return -1; - return fdop_dispatch((struct fdop_args){ - .type = FDOP_WRITE, - .fd = &process_current->fds[fd], +int _syscall_fd_write(handle_t handle, user_ptr buf, int len) { + if (handle < 0 || handle >= HANDLE_MAX) return -1; + return handleop_dispatch((struct handleop_args){ + .type = HANDLEOP_WRITE, + .handle = &process_current->handles[handle], .rw = {buf, len} }); } -int _syscall_fd_close(fd_t fd) { - if (fd < 0 || fd >= FD_MAX) return -1; - return fdop_dispatch((struct fdop_args){ - .type = FDOP_CLOSE, - .fd = &process_current->fds[fd], +int _syscall_fd_close(handle_t handle) { + if (handle < 0 || handle >= HANDLE_MAX) return -1; + return handleop_dispatch((struct handleop_args){ + .type = HANDLEOP_CLOSE, + .handle = &process_current->handles[handle], }); } diff --git a/src/kernel/vfs/mount.c b/src/kernel/vfs/mount.c index bd06370..d9d9d6d 100644 --- a/src/kernel/vfs/mount.c +++ b/src/kernel/vfs/mount.c @@ -8,8 +8,8 @@ struct vfs_mount *vfs_mount_seed(void) { .prev = NULL, .prefix = "/tty", .prefix_len = 4, - .fd = { - .type = FD_SPECIAL_TTY, + .handle = { + .type = HANDLE_SPECIAL_TTY, }, }; return mount; diff --git a/src/kernel/vfs/mount.h b/src/kernel/vfs/mount.h index 51990a1..d70d30a 100644 --- a/src/kernel/vfs/mount.h +++ b/src/kernel/vfs/mount.h @@ -1,11 +1,11 @@ #pragma once -#include <kernel/fd.h> +#include <kernel/handle.h> struct vfs_mount { struct vfs_mount *prev; char *prefix; size_t prefix_len; - struct fd fd; + struct handle handle; }; // prepares init's filesystem view diff --git a/src/shared/syscalls.h b/src/shared/syscalls.h index a4957a9..d65723d 100644 --- a/src/shared/syscalls.h +++ b/src/shared/syscalls.h @@ -3,8 +3,7 @@ #pragma once #include <stddef.h> -typedef int fd_t; -typedef int fs_handle_t; +typedef int handle_t; enum { // idc about stable syscall numbers just yet @@ -36,9 +35,9 @@ int _syscall_await(user_ptr buf, int len); */ int _syscall_fork(void); -fd_t _syscall_fs_open(const user_ptr path, int len); +handle_t _syscall_fs_open(const user_ptr path, int len); -int _syscall_fd_mount(fd_t fd, const user_ptr path, int len); -int _syscall_fd_read(fd_t fd, user_ptr buf, int len); -int _syscall_fd_write(fd_t fd, user_ptr buf, int len); -int _syscall_fd_close(fd_t fd); +int _syscall_fd_mount(handle_t, const user_ptr path, int len); +int _syscall_fd_read(handle_t, user_ptr buf, int len); +int _syscall_fd_write(handle_t, user_ptr buf, int len); +int _syscall_fd_close(handle_t); |