From 3da6d89d1f40d82fa0b6bdc3aec99a8919661797 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 17 Nov 2019 20:01:03 +0100 Subject: [PATCH] Kernel+LibC: Remove the isatty() syscall This can be implemented entirely in userspace by calling tcgetattr(). To avoid screwing up the syscall indexes, this patch also adds a mechanism for removing a syscall without shifting the index of other syscalls. Note that ports will still have to be rebuilt after this change, as their LibC code will try to make the isatty() syscall on startup. --- Kernel/Process.cpp | 10 ---------- Kernel/Process.h | 1 - Kernel/Syscall.cpp | 2 ++ Kernel/Syscall.h | 12 +++++++++++- Libraries/LibC/unistd.cpp | 5 +++-- Userland/syscall.cpp | 3 +++ 6 files changed, 19 insertions(+), 14 deletions(-) diff --git a/Kernel/Process.cpp b/Kernel/Process.cpp index 88b12750116..2abac191679 100644 --- a/Kernel/Process.cpp +++ b/Kernel/Process.cpp @@ -1522,16 +1522,6 @@ int Process::sys$uname(utsname* buf) return 0; } -int Process::sys$isatty(int fd) -{ - auto* description = file_description(fd); - if (!description) - return -EBADF; - if (!description->is_tty()) - return -ENOTTY; - return 1; -} - KResult Process::do_kill(Process& process, int signal) { // FIXME: Allow sending SIGCONT to everyone in the process group. diff --git a/Kernel/Process.h b/Kernel/Process.h index 58a2d39887b..035c48cee24 100644 --- a/Kernel/Process.h +++ b/Kernel/Process.h @@ -159,7 +159,6 @@ public: int sys$ptsname_r(int fd, char*, ssize_t); pid_t sys$fork(RegisterDump&); int sys$execve(const char* filename, const char** argv, const char** envp); - int sys$isatty(int fd); int sys$getdtablesize(); int sys$dup(int oldfd); int sys$dup2(int oldfd, int newfd); diff --git a/Kernel/Syscall.cpp b/Kernel/Syscall.cpp index a83c0936135..00a80343374 100644 --- a/Kernel/Syscall.cpp +++ b/Kernel/Syscall.cpp @@ -48,11 +48,13 @@ void initialize() #pragma GCC diagnostic ignored "-Wcast-function-type" typedef int (Process::*Handler)(u32, u32, u32); +#define __ENUMERATE_REMOVED_SYSCALL(x) nullptr, #define __ENUMERATE_SYSCALL(x) reinterpret_cast(&Process::sys$##x), static Handler s_syscall_table[] = { ENUMERATE_SYSCALLS }; #undef __ENUMERATE_SYSCALL +#undef __ENUMERATE_REMOVED_SYSCALL int handle(RegisterDump& regs, u32 function, u32 arg1, u32 arg2, u32 arg3) { diff --git a/Kernel/Syscall.h b/Kernel/Syscall.h index 3b90b2276be..6308dcdf949 100644 --- a/Kernel/Syscall.h +++ b/Kernel/Syscall.h @@ -50,7 +50,7 @@ typedef u32 socklen_t; __ENUMERATE_SYSCALL(execve) \ __ENUMERATE_SYSCALL(geteuid) \ __ENUMERATE_SYSCALL(getegid) \ - __ENUMERATE_SYSCALL(isatty) \ + __ENUMERATE_REMOVED_SYSCALL(isatty) \ __ENUMERATE_SYSCALL(getdtablesize) \ __ENUMERATE_SYSCALL(dup) \ __ENUMERATE_SYSCALL(dup2) \ @@ -144,9 +144,12 @@ namespace Syscall { enum Function { #undef __ENUMERATE_SYSCALL +#undef __ENUMERATE_REMOVED_SYSCALL +#define __ENUMERATE_REMOVED_SYSCALL(x) SC_##x, #define __ENUMERATE_SYSCALL(x) SC_##x, ENUMERATE_SYSCALLS #undef __ENUMERATE_SYSCALL +#undef __ENUMERATE_REMOVED_SYSCALL __Count }; @@ -154,11 +157,16 @@ inline constexpr const char* to_string(Function function) { switch (function) { #undef __ENUMERATE_SYSCALL +#undef __ENUMERATE_REMOVED_SYSCALL +#define __ENUMERATE_REMOVED_SYSCALL(x) \ + case SC_##x: \ + return #x " (removed)"; #define __ENUMERATE_SYSCALL(x) \ case SC_##x: \ return #x; ENUMERATE_SYSCALLS #undef __ENUMERATE_SYSCALL +#undef __ENUMERATE_REMOVED_SYSCALL default: break; } @@ -291,6 +299,8 @@ inline u32 invoke(Function function, T1 arg1, T2 arg2, T3 arg3) #undef __ENUMERATE_SYSCALL #define __ENUMERATE_SYSCALL(x) using Syscall::SC_##x; +#define __ENUMERATE_REMOVED_SYSCALL(x) ENUMERATE_SYSCALLS #undef __ENUMERATE_SYSCALL +#undef __ENUMERATE_REMOVED_SYSCALL #define syscall Syscall::invoke diff --git a/Libraries/LibC/unistd.cpp b/Libraries/LibC/unistd.cpp index 809d7051879..8fe148cd219 100644 --- a/Libraries/LibC/unistd.cpp +++ b/Libraries/LibC/unistd.cpp @@ -13,6 +13,7 @@ #include #include #include +#include #include extern "C" { @@ -329,8 +330,8 @@ int rmdir(const char* pathname) int isatty(int fd) { - int rc = syscall(SC_isatty, fd); - __RETURN_WITH_ERRNO(rc, 1, 0); + struct termios dummy; + return tcgetattr(fd, &dummy); } int getdtablesize() diff --git a/Userland/syscall.cpp b/Userland/syscall.cpp index 59f111ab965..d6ef41af401 100644 --- a/Userland/syscall.cpp +++ b/Userland/syscall.cpp @@ -10,6 +10,9 @@ #if !defined __ENUMERATE_SYSCALL # define __ENUMERATE_SYSCALL(x) SC_##x, #endif +#if !defined __ENUMERATE_REMOVED_SYSCALL +# define __ENUMERATE_REMOVED_SYSCALL(x) +#endif #define SC_NARG 4