From 26df7ce9aa9b2832a9db65532462c9469cabf520 Mon Sep 17 00:00:00 2001 From: Mark Date: Sun, 26 Jul 2020 21:47:38 +0300 Subject: [PATCH] open(2) superseded by openat(2) --- arch/amd64/syscall.c | 14 ++++++++++++-- fs/vfs_ops.c | 22 ++++++++++++++++------ include/fs/vfs.h | 6 +++++- include/sys/sys_file.h | 3 ++- include/user/fcntl.h | 1 + include/user/syscall.h | 4 +++- sys/execve.c | 2 +- sys/mod.c | 2 +- sys/panic.c | 2 ++ sys/sys_file.c | 16 ++++++++++++++-- 10 files changed, 57 insertions(+), 15 deletions(-) diff --git a/arch/amd64/syscall.c b/arch/amd64/syscall.c index b38a951..b4d807e 100644 --- a/arch/amd64/syscall.c +++ b/arch/amd64/syscall.c @@ -3,6 +3,7 @@ #include "user/syscall.h" #include "user/errno.h" #include "user/time.h" +#include "sys/sched.h" #include "arch/amd64/cpu.h" #include "sys/thread.h" #include "sys/debug.h" @@ -40,7 +41,9 @@ void *syscall_table[256] = { // I/O [SYSCALL_NR_READ] = sys_read, [SYSCALL_NR_WRITE] = sys_write, - [SYSCALL_NR_OPEN] = sys_open, + //[SYSCALL_NR_OPEN] = sys_open, + //SUPERSEDED BY: + [SYSCALL_NR_OPENAT] = sys_openat, [SYSCALL_NR_CLOSE] = sys_close, [SYSCALL_NR_STAT] = sys_stat, [SYSCALL_NR_FSTAT] = sys_fstat, @@ -112,7 +115,14 @@ void *syscall_table[256] = { }; int syscall_undefined(uint64_t rax) { - panic("TODO: exception signals\n"); + debugs(DEBUG_DEFAULT, "\033[41m"); + if (sched_ready) { + thread_dump(DEBUG_DEFAULT, thread_self); + } + debugf(DEBUG_DEFAULT, "Undefined system call: %d\n", rax); + debugs(DEBUG_DEFAULT, "\033[0m"); + thread_signal(thread_self, SIGSYS); + // TODO: more context? return -ENOSYS; } diff --git a/fs/vfs_ops.c b/fs/vfs_ops.c index 21bfb7d..7a3738a 100644 --- a/fs/vfs_ops.c +++ b/fs/vfs_ops.c @@ -420,7 +420,10 @@ int vfs_mknod(struct vfs_ioctx *ctx, const char *path, mode_t mode, struct vnode return 0; } -int vfs_creat(struct vfs_ioctx *ctx, const char *path, mode_t mode) { +int vfs_creatat(struct vfs_ioctx *ctx, + struct vnode *vn_at, + const char *path, + mode_t mode) { char parent_path[PATH_MAX]; const char *filename; int res; @@ -435,7 +438,7 @@ int vfs_creat(struct vfs_ioctx *ctx, const char *path, mode_t mode) { } // Find parent vnode - if ((res = vfs_find(ctx, ctx->cwd_vnode, parent_path, 0, &at)) != 0) { + if ((res = vfs_find(ctx, vn_at, parent_path, 0, &at)) != 0) { return res; } @@ -453,7 +456,14 @@ int vfs_creat(struct vfs_ioctx *ctx, const char *path, mode_t mode) { return at->op->creat(at, filename, ctx->uid, ctx->gid, mode); } -int vfs_open(struct vfs_ioctx *ctx, struct ofile *fd, const char *path, int opt, int mode) { +int vfs_openat(struct vfs_ioctx *ctx, + struct ofile *fd, + struct vnode *at, + const char *path, + int opt, int mode) { + if (!at) { + at = ctx->cwd_vnode; + } struct vnode *node; int res; @@ -461,19 +471,19 @@ int vfs_open(struct vfs_ioctx *ctx, struct ofile *fd, const char *path, int opt, _assert(fd); _assert(path); - if ((res = vfs_find(ctx, ctx->cwd_vnode, path, 0, &node)) != 0) { + if ((res = vfs_find(ctx, at, path, 0, &node)) != 0) { if (opt & O_CREAT) { if (opt & O_DIRECTORY) { return -EINVAL; } // Try to create a new file - if ((res = vfs_creat(ctx, path, mode)) != 0) { + if ((res = vfs_creatat(ctx, at, path, mode)) != 0) { return res; } // If creation succeeded, find file again - if ((res = vfs_find(ctx, ctx->cwd_vnode, path, 0, &node)) != 0) { + if ((res = vfs_find(ctx, at, path, 0, &node)) != 0) { return res; } } else { diff --git a/include/fs/vfs.h b/include/fs/vfs.h index da428f4..9b32551 100644 --- a/include/fs/vfs.h +++ b/include/fs/vfs.h @@ -41,7 +41,11 @@ int vfs_mount(struct vfs_ioctx *ctx, const char *at, void *blk, const char *fs, int vfs_umount(struct vfs_ioctx *ctx, const char *dir_name); int vfs_open_vnode(struct vfs_ioctx *ctx, struct ofile *fd, struct vnode *node, int opt); -int vfs_open(struct vfs_ioctx *ctx, struct ofile *fd, const char *path, int flags, int mode); +int vfs_openat(struct vfs_ioctx *ctx, + struct ofile *fd, + struct vnode *at, + const char *path, + int flags, int mode); void vfs_close(struct vfs_ioctx *ctx, struct ofile *fd); int vfs_readdir(struct vfs_ioctx *ctx, struct ofile *fd, struct dirent *ent); int vfs_unlink(struct vfs_ioctx *ctx, const char *path); diff --git a/include/sys/sys_file.h b/include/sys/sys_file.h index 350b56e..1a4554f 100644 --- a/include/sys/sys_file.h +++ b/include/sys/sys_file.h @@ -14,7 +14,8 @@ int sys_ioctl(int fd, unsigned int cmd, void *arg); // Kinda incompatible with linux, but who cares as long as it's // POSIX on the libc side int sys_getcwd(char *buf, size_t lim); -int sys_open(const char *filename, int flags, int mode); +// REMOVED: int sys_open(const char *filename, int flags, int mode); +int sys_openat(int dfd, const char *filename, int flags, int mode); void sys_close(int fd); int sys_stat(const char *filename, struct stat *st); int sys_fstat(int fd, struct stat *st); diff --git a/include/user/fcntl.h b/include/user/fcntl.h index 1a543aa..6e6512b 100644 --- a/include/user/fcntl.h +++ b/include/user/fcntl.h @@ -9,6 +9,7 @@ #define X_OK 1 #define F_OK 0 +#define AT_FDCWD (-100) // O_EXEC is a special one for opening a node for // execution diff --git a/include/user/syscall.h b/include/user/syscall.h index 393ca52..3ea12b7 100644 --- a/include/user/syscall.h +++ b/include/user/syscall.h @@ -2,7 +2,9 @@ #define SYSCALL_NR_READ 0 #define SYSCALL_NR_WRITE 1 -#define SYSCALL_NR_OPEN 2 +// #define SYSCALL_NR_OPEN 2 +// SUPERSEDED BY: +#define SYSCALL_NR_OPENAT 2 #define SYSCALL_NR_CLOSE 3 #define SYSCALL_NR_STAT 4 #define SYSCALL_NR_FSTAT 5 diff --git a/sys/execve.c b/sys/execve.c index ef8fd39..0ad4bd9 100644 --- a/sys/execve.c +++ b/sys/execve.c @@ -138,7 +138,7 @@ int sys_execve(const char *path, const char **argv, const char **envp) { return res; } - if ((res = vfs_open(&proc->ioctx, &fd, path, O_RDONLY, 0)) != 0) { + if ((res = vfs_openat(&proc->ioctx, &fd, NULL, path, O_RDONLY, 0)) != 0) { kerror("%s: %s\n", path, kstrerror(res)); return res; } diff --git a/sys/mod.c b/sys/mod.c index f513513..862502f 100644 --- a/sys/mod.c +++ b/sys/mod.c @@ -415,7 +415,7 @@ int sys_module_load(const char *path, const char *params) { kinfo("Load module %s\n", path); - if ((res = vfs_open(&kernel_ioctx, &fd, path, O_RDONLY, 0)) != 0) { + if ((res = vfs_openat(&kernel_ioctx, &fd, NULL, path, O_RDONLY, 0)) != 0) { kerror("%s: %s\n", path, kstrerror(res)); slab_free(module_cache, mod); return res; diff --git a/sys/panic.c b/sys/panic.c index 6095865..f16a324 100644 --- a/sys/panic.c +++ b/sys/panic.c @@ -17,7 +17,9 @@ void panicf(const char *fmt, ...) { kfatal("--- Panic ---\n"); if (sched_ready) { + debugs(DEBUG_FATAL, "\033[41m"); thread_dump(DEBUG_FATAL, thread_self); + debugs(DEBUG_FATAL, "\033[0m"); } va_start(args, fmt); diff --git a/sys/sys_file.c b/sys/sys_file.c index be91b3a..f31153d 100644 --- a/sys/sys_file.c +++ b/sys/sys_file.c @@ -3,6 +3,7 @@ #include "arch/amd64/hw/timer.h" #include "arch/amd64/cpu.h" #include "fs/ofile.h" +#include "user/fcntl.h" #include "sys/char/ring.h" #include "sys/char/pipe.h" #include "sys/char/chr.h" @@ -113,12 +114,23 @@ int sys_getcwd(char *buf, size_t lim) { } } -int sys_open(const char *filename, int flags, int mode) { +int sys_openat(int dfd, const char *filename, int flags, int mode) { userptr_check(filename); struct process *proc = thread_self->proc; + struct vnode *at; int fd = -1; int res; + if (dfd == AT_FDCWD) { + at = get_ioctx()->cwd_vnode; + } else { + struct ofile *of = get_fd(dfd); + if (of == NULL || !(of->flags & OF_DIRECTORY)) { + return -EBADF; + } + at = of->file.vnode; + } + // XXX: This should be atomic for (int i = 0; i < THREAD_MAX_FDS; ++i) { if (!proc->fds[i]) { @@ -132,7 +144,7 @@ int sys_open(const char *filename, int flags, int mode) { struct ofile *ofile = ofile_create(); - if ((res = vfs_open(&proc->ioctx, ofile, filename, flags, mode)) != 0) { + if ((res = vfs_openat(&proc->ioctx, ofile, at, filename, flags, mode)) != 0) { ofile_destroy(ofile); return res; }