From 69f9701097d2aaf7bfe2b643c49cb8d2655c190a Mon Sep 17 00:00:00 2001 From: apio Date: Tue, 11 Jul 2023 12:05:09 +0200 Subject: [PATCH] kernel+libc: Implement isatty() --- kernel/src/fs/VFS.h | 5 +++++ kernel/src/fs/devices/ConsoleDevice.cpp | 1 + kernel/src/fs/devices/ConsoleDevice.h | 5 +++++ kernel/src/fs/devices/Device.h | 5 +++++ kernel/src/fs/tmpfs/Inode.h | 5 +++++ kernel/src/sys/file.cpp | 10 ++++++++++ libc/include/unistd.h | 3 +++ libc/src/unistd.cpp | 11 +++++++++++ libluna/include/luna/Syscall.h | 2 +- 9 files changed, 46 insertions(+), 1 deletion(-) diff --git a/kernel/src/fs/VFS.h b/kernel/src/fs/VFS.h index 8abf7602..f89eaa4d 100644 --- a/kernel/src/fs/VFS.h +++ b/kernel/src/fs/VFS.h @@ -81,6 +81,11 @@ namespace VFS return err(ENOTTY); } + virtual Result isatty() const + { + return err(ENOTTY); + } + // Directory-specific methods virtual Result> find(const char* name) const = 0; diff --git a/kernel/src/fs/devices/ConsoleDevice.cpp b/kernel/src/fs/devices/ConsoleDevice.cpp index c9aad225..2038ec50 100644 --- a/kernel/src/fs/devices/ConsoleDevice.cpp +++ b/kernel/src/fs/devices/ConsoleDevice.cpp @@ -5,6 +5,7 @@ #include "memory/MemoryManager.h" #include "thread/Scheduler.h" #include "video/TextConsole.h" +#include #include #include #include diff --git a/kernel/src/fs/devices/ConsoleDevice.h b/kernel/src/fs/devices/ConsoleDevice.h index cf428ef2..d9dddd62 100644 --- a/kernel/src/fs/devices/ConsoleDevice.h +++ b/kernel/src/fs/devices/ConsoleDevice.h @@ -22,5 +22,10 @@ class ConsoleDevice : public Device return "console"; } + Result isatty() const override + { + return 1; + } + virtual ~ConsoleDevice() = default; }; diff --git a/kernel/src/fs/devices/Device.h b/kernel/src/fs/devices/Device.h index e7e44f27..daa43425 100644 --- a/kernel/src/fs/devices/Device.h +++ b/kernel/src/fs/devices/Device.h @@ -14,6 +14,11 @@ class Device return err(ENOTTY); } + virtual Result isatty() const + { + return err(ENOTTY); + } + virtual usize size() const { return 0; diff --git a/kernel/src/fs/tmpfs/Inode.h b/kernel/src/fs/tmpfs/Inode.h index 082f1a0c..e2815a91 100644 --- a/kernel/src/fs/tmpfs/Inode.h +++ b/kernel/src/fs/tmpfs/Inode.h @@ -276,6 +276,11 @@ namespace TmpFS return m_device->ioctl(request, arg); } + Result isatty() const override + { + return m_device->isatty(); + } + bool blocking() const override { return m_device->blocking(); diff --git a/kernel/src/sys/file.cpp b/kernel/src/sys/file.cpp index cc97aaf5..36e4153e 100644 --- a/kernel/src/sys/file.cpp +++ b/kernel/src/sys/file.cpp @@ -157,6 +157,16 @@ Result sys_ioctl(Registers*, SyscallArgs args) return descriptor.inode->ioctl(request, arg); } +Result sys_isatty(Registers*, SyscallArgs args) +{ + int fd = (int)args[0]; + + Thread* current = Scheduler::current(); + auto& descriptor = *TRY(current->resolve_fd(fd)); + + return descriptor.inode->isatty(); +} + Result sys_dup2(Registers*, SyscallArgs args) { int oldfd = (int)args[0]; diff --git a/libc/include/unistd.h b/libc/include/unistd.h index a4c56f14..b06dea68 100644 --- a/libc/include/unistd.h +++ b/libc/include/unistd.h @@ -168,6 +168,9 @@ extern "C" /* Check for a file's accessibility relative to a file descriptor. */ int faccessat(int dirfd, const char* path, int amode, int flags); + /* Check whether a file descriptor refers to a terminal device. */ + int isatty(int fd); + #ifdef __cplusplus } #endif diff --git a/libc/src/unistd.cpp b/libc/src/unistd.cpp index 9b6e7b60..75e8cedb 100644 --- a/libc/src/unistd.cpp +++ b/libc/src/unistd.cpp @@ -467,4 +467,15 @@ extern "C" long rc = syscall(SYS_faccessat, dirfd, path, amode, flags); __errno_return(rc, int); } + + int isatty(int fd) + { + long rc = syscall(SYS_isatty, fd); + if (rc < 0) + { + errno = -rc; + return 0; + } + return rc; + } } diff --git a/libluna/include/luna/Syscall.h b/libluna/include/luna/Syscall.h index ae216722..2577572c 100644 --- a/libluna/include/luna/Syscall.h +++ b/libluna/include/luna/Syscall.h @@ -6,7 +6,7 @@ _e(getgid) _e(getegid) _e(setuid) _e(setgid) _e(seteuid) _e(setegid) _e(fchmodat) _e(fchownat) _e(ioctl) \ _e(fstatat) _e(chdir) _e(getcwd) _e(unlinkat) _e(uname) _e(sethostname) _e(dup2) _e(pipe) _e(mount) \ _e(umount) _e(pstat) _e(getrusage) _e(symlinkat) _e(readlinkat) _e(umask) _e(linkat) _e(faccessat) \ - _e(pivot_root) _e(sigreturn) _e(sigaction) _e(kill) _e(sigprocmask) _e(setpgid) + _e(pivot_root) _e(sigreturn) _e(sigaction) _e(kill) _e(sigprocmask) _e(setpgid) _e(isatty) enum Syscalls {