open(2) superseded by openat(2)
This commit is contained in:
+12
-2
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
+16
-6
@@ -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 {
|
||||
|
||||
+5
-1
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
+1
-1
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
+14
-2
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user