syscalls: linkat

This commit is contained in:
Matheus
2023-07-06 14:01:12 -03:00
parent 4a9306191b
commit 9b945d2bc0
5 changed files with 127 additions and 9 deletions
+3 -1
View File
@@ -28,6 +28,7 @@ extern syscall_unlinkat
extern syscall_ioctl
extern syscall_mkdirat
extern syscall_clockget
extern syscall_linkat
syscalltab:
dq syscall_print
dq syscall_mmap
@@ -58,7 +59,8 @@ dq syscall_unlinkat
dq syscall_ioctl
dq syscall_mkdirat
dq syscall_clockget
syscallcount equ 29
dq syscall_linkat
syscallcount equ 30
section .text
global arch_syscall_entry
; on entry:
+5 -3
View File
@@ -4,7 +4,7 @@
#ifdef SYSCALL_LOGGING
#define SYSCALL_COUNT 29
#define SYSCALL_COUNT 30
#define LOGSTR(x) arch_e9_puts(x)
static char *name[] = {
@@ -36,7 +36,8 @@ static char *name[] = {
"unlinkat",
"ioctl",
"mkdirat",
"clockget"
"clockget",
"linkat"
};
static char *args[] = {
@@ -68,7 +69,8 @@ static char *args[] = {
"dirfd %d pathname %d flags %d", // unlinkat
"fd %d request %lu argument %d", // ioctl
"dirfd %d path %s mode %o", // mkdirat
"clockid %d timespec %p" // clockget
"clockid %d timespec %p", // clockget
"olddirfd %d oldpath %s newdirfd %d oldpath %s flags %d type %d" // linkat
};
#endif
+31
View File
@@ -3,10 +3,41 @@
#include <stdint.h>
#include <arch/context.h>
#include <kernel/vfs.h>
#include <kernel/file.h>
#include <kernel/scheduler.h>
typedef struct {
uint64_t ret;
uint64_t errno;
} syscallret_t;
static inline int dirfd_enter(char *path, int dirfd, file_t **file, vnode_t **dirnode) {
if (*path == '/') {
*dirnode = sched_getroot();
} else if (dirfd == AT_FDCWD) {
*dirnode = sched_getcwd();
} else {
*file = fd_get(dirfd);
if (file == NULL) {
return EBADF;
}
*dirnode = (*file)->vnode;
if ((*dirnode)->type != V_TYPE_DIR) {
fd_release(*file);
return ENOTDIR;
}
}
return 0;
}
static inline void dirfd_leave(vnode_t *dirnode, file_t *file) {
if (file)
fd_release(file);
else
VOP_RELEASE(dirnode);
}
#endif
+61
View File
@@ -0,0 +1,61 @@
#include <kernel/syscalls.h>
#include <kernel/alloc.h>
syscallret_t syscall_linkat(context_t *, int olddirfd, const char *uoldpath, int newdirfd, const char *unewpath, int flags, int type) {
syscallret_t ret = {
.ret = -1
};
char *oldpath = alloc(strlen(uoldpath) + 1);
if (oldpath == NULL) {
ret.errno = ENOMEM;
return ret;
}
char *newpath = alloc(strlen(unewpath) + 1);
if (newpath == NULL) {
free(oldpath);
ret.errno = ENOMEM;
return ret;
}
// TODO safe strcopy
strcpy(oldpath, uoldpath);
strcpy(newpath, unewpath);
vnode_t *olddirnode = NULL;
file_t *oldfile = NULL;
vnode_t *newdirnode = NULL;
file_t *newfile = NULL;
ret.errno = dirfd_enter(oldpath, olddirfd, &oldfile, &olddirnode);
if (ret.errno)
goto cleanup;
ret.errno = dirfd_enter(newpath, newdirfd, &newfile, &newdirnode);
if (ret.errno)
goto cleanup;
vattr_t attr = {
.gid = _cpu()->thread->proc->cred.gid,
.uid = _cpu()->thread->proc->cred.uid
};
ret.errno = vfs_link(olddirnode, oldpath, newdirnode, newpath, type == 0 ? V_TYPE_REGULAR : V_TYPE_LINK, &attr);
ret.ret = ret.errno ? -1 : 0;
cleanup:
if (olddirnode)
dirfd_leave(olddirnode, oldfile);
if (newdirnode)
dirfd_leave(newdirnode, newfile);
free(oldpath);
free(newpath);
return ret;
}