From 04a5aa4bc6ab91e50794ffc8d7eccf2bcbd44ea6 Mon Sep 17 00:00:00 2001 From: Matheus Date: Tue, 4 Jul 2023 20:19:15 -0300 Subject: [PATCH] syscalls: isatty --- kernel-src/arch/x86-64/syscall.asm | 4 +++- kernel-src/arch/x86-64/syscalllog.c | 6 ++++-- kernel-src/fs/devfs.c | 11 ++++++++++- kernel-src/include/kernel/devfs.h | 1 + kernel-src/include/kernel/vfs.h | 5 ++++- kernel-src/io/console.c | 7 ++++++- kernel-src/sys/syscalls/isatty.c | 25 +++++++++++++++++++++++++ patches/mlibc/jinx-working-patch.patch | 11 ++++++----- 8 files changed, 59 insertions(+), 11 deletions(-) create mode 100644 kernel-src/sys/syscalls/isatty.c diff --git a/kernel-src/arch/x86-64/syscall.asm b/kernel-src/arch/x86-64/syscall.asm index 6a6c4df9..b13707c8 100644 --- a/kernel-src/arch/x86-64/syscall.asm +++ b/kernel-src/arch/x86-64/syscall.asm @@ -22,6 +22,7 @@ extern syscall_dup3 extern syscall_fcntl extern syscall_chdir extern syscall_pipe2 +extern syscall_isatty syscalltab: dq syscall_print dq syscall_mmap @@ -46,7 +47,8 @@ dq syscall_dup3 dq syscall_fcntl dq syscall_chdir dq syscall_pipe2 -syscallcount equ 23 +dq syscall_isatty +syscallcount equ 24 section .text global arch_syscall_entry ; on entry: diff --git a/kernel-src/arch/x86-64/syscalllog.c b/kernel-src/arch/x86-64/syscalllog.c index 20c1dcc5..78fd2f9c 100644 --- a/kernel-src/arch/x86-64/syscalllog.c +++ b/kernel-src/arch/x86-64/syscalllog.c @@ -2,7 +2,7 @@ #include #include -#define SYSCALL_COUNT 23 +#define SYSCALL_COUNT 24 #define LOGSTR(x) arch_e9_puts(x) static char *name[] = { @@ -28,7 +28,8 @@ static char *name[] = { "dup3", "fcntl", "chdir", - "pipe2" + "pipe2", + "isatty" }; static char *args[] = { @@ -55,6 +56,7 @@ static char *args[] = { "fd %d cmd %d arg %lu", // fcntl "path %s", // chdir "flags %d", // pipe2 + "fd %d", // isatty }; __attribute__((no_caller_saved_registers)) void arch_syscall_log(int syscall, uint64_t a1, uint64_t a2, uint64_t a3, uint64_t a4, uint64_t a5, uint64_t a6) { diff --git a/kernel-src/fs/devfs.c b/kernel-src/fs/devfs.c index 4d8d17b0..82bccb5a 100644 --- a/kernel-src/fs/devfs.c +++ b/kernel-src/fs/devfs.c @@ -183,6 +183,14 @@ int devfs_access(vnode_t *node, mode_t mode, cred_t *cred) { return 0; } +int devfs_isatty(vnode_t *node) { + devnode_t *devnode = (devnode_t *)node; + if (devnode->master) + devnode = devnode->master; + + return devnode->devops->isatty ? devnode->devops->isatty(devnode->attr.rdevminor) : ENOTTY; +} + int devfs_inactive(vnode_t *node) { VOP_LOCK(node); devnode_t *devnode = (devnode_t *)node; @@ -304,7 +312,8 @@ static vops_t vnops = { .inactive = devfs_enodev, .mmap = devfs_mmap, .munmap = devfs_munmap, - .getdents = devfs_getdents + .getdents = devfs_getdents, + .isatty = devfs_isatty }; static void ctor(scache_t *cache, void *obj) { diff --git a/kernel-src/include/kernel/devfs.h b/kernel-src/include/kernel/devfs.h index 685c657d..ede5e7a7 100644 --- a/kernel-src/include/kernel/devfs.h +++ b/kernel-src/include/kernel/devfs.h @@ -16,6 +16,7 @@ typedef struct { int (*poll)(int minor, int events); int (*mmap)(int minor, void *addr, uintmax_t offset, int flags); int (*munmap)(int minor, void *addr, uintmax_t offset, int flags); + int (*isatty)(int minor); } devops_t; typedef struct devnode_t { diff --git a/kernel-src/include/kernel/vfs.h b/kernel-src/include/kernel/vfs.h index 89b2a3e9..0b7de81e 100644 --- a/kernel-src/include/kernel/vfs.h +++ b/kernel-src/include/kernel/vfs.h @@ -6,6 +6,7 @@ #include #include #include +#include typedef struct { uid_t uid; @@ -90,6 +91,7 @@ typedef struct vops_t { int (*mmap)(vnode_t *node, void *addr, uintmax_t offset, int flags, cred_t *cred); int (*munmap)(vnode_t *node, void *addr, uintmax_t offset, int flags, cred_t *cred); int (*getdents)(vnode_t *node, dent_t *buffer, size_t count, uintmax_t offset, size_t *readcount); + int (*isatty)(vnode_t *node); } vops_t; #define VFS_MOUNT(vfs, mp, b, d) (vfs)->ops->mount(vfs, mp, b, d) @@ -116,7 +118,8 @@ typedef struct vops_t { #define VOP_MMAP(v, a, o, f, c) (v)->ops->mmap(v, a, o, f, c) #define VOP_MUNMAP(v, a, o, f, c) (v)->ops->munmap(v, a, o, f, c) #define VOP_GETDENTS(v, b, c, o, rc) (v)->ops->getdents(v, b, c, o, rc) -#define VOP_POLL(v, p) (v)->ops->poll(v, p); +#define VOP_POLL(v, p) (v)->ops->poll(v, p) +#define VOP_ISATTY(v) ((v)->ops->isatty ? (v)->ops->isatty(v) : ENOTTY) #define VOP_HOLD(v) __atomic_add_fetch(&(v)->refcount, 1, __ATOMIC_SEQ_CST) #define VOP_RELEASE(v) {\ if (__atomic_sub_fetch(&(v)->refcount, 1, __ATOMIC_SEQ_CST) == 0) {\ diff --git a/kernel-src/io/console.c b/kernel-src/io/console.c index c957b5f5..f17284f2 100644 --- a/kernel-src/io/console.c +++ b/kernel-src/io/console.c @@ -153,9 +153,14 @@ static int write(int minor, void *buffer, size_t size, uintmax_t offset, int fla return 0; } +static int isatty(int minor) { + return 0; +} + static devops_t devops = { .write = write, - .read = read + .read = read, + .isatty = isatty }; void console_init() { diff --git a/kernel-src/sys/syscalls/isatty.c b/kernel-src/sys/syscalls/isatty.c new file mode 100644 index 00000000..3d4b39ad --- /dev/null +++ b/kernel-src/sys/syscalls/isatty.c @@ -0,0 +1,25 @@ +#include +#include +#include + +syscallret_t syscall_isatty(context_t *context, int fd) { + syscallret_t ret = { + .ret = -1 + }; + + file_t *file = fd_get(fd); + if (file == NULL) { + ret.errno = EBADF; + return ret; + } + + // mlibc expects isatty to return 1 if a terminal and 0 if not. + int e = VOP_ISATTY(file->vnode); + + ret.errno = e; + ret.ret = e ? 0 : 1; + + fd_release(file); + + return ret; +} diff --git a/patches/mlibc/jinx-working-patch.patch b/patches/mlibc/jinx-working-patch.patch index d952feda..af049d6c 100644 --- a/patches/mlibc/jinx-working-patch.patch +++ b/patches/mlibc/jinx-working-patch.patch @@ -133,7 +133,7 @@ index 0000000..e5b087a + diff --git mlibc-workdir/sysdeps/astral/generic/generic.cpp mlibc-workdir/sysdeps/astral/generic/generic.cpp new file mode 100644 -index 0000000..e76adfd +index 0000000..6b6d167 --- /dev/null +++ mlibc-workdir/sysdeps/astral/generic/generic.cpp @@ -0,0 +1,246 @@ @@ -372,8 +372,8 @@ index 0000000..e76adfd + } + + int sys_isatty(int fd) { -+ // TODO UNSTUB -+ return 0; ++ long ret; ++ return syscall(SYSCALL_ISATTY, &ret, fd); + } + + int sys_clock_get(int clock, time_t *secs, long *nanos) { @@ -402,10 +402,10 @@ index 0000000..990eca0 +#endif diff --git mlibc-workdir/sysdeps/astral/include/astral/syscall.h mlibc-workdir/sysdeps/astral/include/astral/syscall.h new file mode 100644 -index 0000000..e432092 +index 0000000..09c7dd4 --- /dev/null +++ mlibc-workdir/sysdeps/astral/include/astral/syscall.h -@@ -0,0 +1,46 @@ +@@ -0,0 +1,47 @@ +#ifndef _SYSCALL_H_INCLUDE +#define _SYSCALL_H_INCLUDE + @@ -432,6 +432,7 @@ index 0000000..e432092 +#define SYSCALL_FCNTL 20 +#define SYSCALL_CHDIR 21 +#define SYSCALL_PIPE2 22 ++#define SYSCALL_ISATTY 23 + +#include +#include