From e5bb84305902c29bf23fe7af1b2367b97cb8bd68 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Tue, 30 Jul 2024 11:41:18 +0300 Subject: [PATCH] LibC/Kernel: Implement ttyname --- kernel/include/kernel/Process.h | 1 + kernel/kernel/Process.cpp | 14 ++++++++++++++ userspace/libraries/LibC/include/limits.h | 1 + userspace/libraries/LibC/include/sys/syscall.h | 1 + userspace/libraries/LibC/unistd.cpp | 8 ++++++++ 5 files changed, 25 insertions(+) diff --git a/kernel/include/kernel/Process.h b/kernel/include/kernel/Process.h index 009c79d115..5387d4f017 100644 --- a/kernel/include/kernel/Process.h +++ b/kernel/include/kernel/Process.h @@ -168,6 +168,7 @@ namespace Kernel BAN::ErrorOr sys_smo_delete(SharedMemoryObjectManager::Key); BAN::ErrorOr sys_smo_map(SharedMemoryObjectManager::Key); + BAN::ErrorOr sys_ttyname(int fildes, char* storage); BAN::ErrorOr sys_isatty(int fildes); BAN::ErrorOr sys_tty_ctrl(int fildes, int command, int flags); diff --git a/kernel/kernel/Process.cpp b/kernel/kernel/Process.cpp index 0601eee0e8..54c24a01b6 100644 --- a/kernel/kernel/Process.cpp +++ b/kernel/kernel/Process.cpp @@ -1497,6 +1497,20 @@ namespace Kernel return m_mapped_regions.back()->vaddr(); } + BAN::ErrorOr Process::sys_ttyname(int fildes, char* storage) + { + LockGuard _(m_process_lock); + TRY(validate_pointer_access(storage, TTY_NAME_MAX)); + auto inode = TRY(m_open_file_descriptors.inode_of(fildes)); + if (!inode->is_tty()) + return BAN::Error::from_errno(ENOTTY); + auto path = TRY(m_open_file_descriptors.path_of(fildes)); + ASSERT(path.size() < TTY_NAME_MAX); + strncpy(storage, path.data(), path.size()); + storage[path.size()] = '\0'; + return 0; + } + BAN::ErrorOr Process::sys_isatty(int fildes) { LockGuard _(m_process_lock); diff --git a/userspace/libraries/LibC/include/limits.h b/userspace/libraries/LibC/include/limits.h index 872d27a36b..791b9d5ae2 100644 --- a/userspace/libraries/LibC/include/limits.h +++ b/userspace/libraries/LibC/include/limits.h @@ -62,6 +62,7 @@ __BEGIN_DECLS #define PATH_MAX 256 #define LOGIN_NAME_MAX 256 #define HOST_NAME_MAX 255 +#define TTY_NAME_MAX PATH_MAX __END_DECLS diff --git a/userspace/libraries/LibC/include/sys/syscall.h b/userspace/libraries/LibC/include/sys/syscall.h index e88954ffa4..f8366c72a2 100644 --- a/userspace/libraries/LibC/include/sys/syscall.h +++ b/userspace/libraries/LibC/include/sys/syscall.h @@ -82,6 +82,7 @@ __BEGIN_DECLS O(SYS_GETSOCKOPT, getsockopt) \ O(SYS_SETSOCKOPT, setsockopt) \ O(SYS_REALPATH, realpath) \ + O(SYS_TTYNAME, ttyname) \ enum Syscall { diff --git a/userspace/libraries/LibC/unistd.cpp b/userspace/libraries/LibC/unistd.cpp index e7405bc99f..c7277d95a3 100644 --- a/userspace/libraries/LibC/unistd.cpp +++ b/userspace/libraries/LibC/unistd.cpp @@ -488,3 +488,11 @@ char* getlogin(void) endpwent(); return buffer; } + +char* ttyname(int fildes) +{ + static char storage[_POSIX_TTY_NAME_MAX]; + if (syscall(SYS_TTYNAME, fildes, storage) == -1) + return nullptr; + return storage; +}