From 9893c90e74eb88ee532c013f2c04b871cf4255d2 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Fri, 17 Jan 2025 19:22:53 +0200 Subject: [PATCH] Kernel: Remove SYS_DUP and implement it using fcntl F_DUPFD --- kernel/include/kernel/OpenFileDescriptorSet.h | 1 - kernel/include/kernel/Process.h | 1 - kernel/kernel/OpenFileDescriptorSet.cpp | 37 +++++++++++-------- kernel/kernel/Process.cpp | 6 --- .../libraries/LibC/include/sys/syscall.h | 1 - userspace/libraries/LibC/unistd.cpp | 2 +- 6 files changed, 22 insertions(+), 26 deletions(-) diff --git a/kernel/include/kernel/OpenFileDescriptorSet.h b/kernel/include/kernel/OpenFileDescriptorSet.h index a36e4d80..b0882ac8 100644 --- a/kernel/include/kernel/OpenFileDescriptorSet.h +++ b/kernel/include/kernel/OpenFileDescriptorSet.h @@ -29,7 +29,6 @@ namespace Kernel BAN::ErrorOr pipe(int fds[2]); - BAN::ErrorOr dup(int); BAN::ErrorOr dup2(int, int); BAN::ErrorOr fcntl(int fd, int cmd, int extra); diff --git a/kernel/include/kernel/Process.h b/kernel/include/kernel/Process.h index 902f9669..0ba814cb 100644 --- a/kernel/include/kernel/Process.h +++ b/kernel/include/kernel/Process.h @@ -138,7 +138,6 @@ namespace Kernel BAN::ErrorOr sys_pselect(sys_pselect_t* arguments); BAN::ErrorOr sys_pipe(int fildes[2]); - BAN::ErrorOr sys_dup(int fildes); BAN::ErrorOr sys_dup2(int fildes, int fildes2); BAN::ErrorOr sys_fcntl(int fildes, int cmd, int extra); diff --git a/kernel/kernel/OpenFileDescriptorSet.cpp b/kernel/kernel/OpenFileDescriptorSet.cpp index 1f3907c0..b834b00a 100644 --- a/kernel/kernel/OpenFileDescriptorSet.cpp +++ b/kernel/kernel/OpenFileDescriptorSet.cpp @@ -159,22 +159,6 @@ namespace Kernel return {}; } - BAN::ErrorOr OpenFileDescriptorSet::dup(int fildes) - { - TRY(validate_fd(fildes)); - - int result = TRY(get_free_fd()); - m_open_files[result] = m_open_files[fildes]; - - if (m_open_files[result]->path() == ""_sv) - { - ASSERT(m_open_files[result]->inode()->is_pipe()); - static_cast(m_open_files[result]->inode().ptr())->clone_writing(); - } - - return result; - } - BAN::ErrorOr OpenFileDescriptorSet::dup2(int fildes, int fildes2) { if (fildes2 < 0 || fildes2 >= (int)m_open_files.size()) @@ -204,6 +188,26 @@ namespace Kernel switch (cmd) { + case F_DUPFD: + case F_DUPFD_CLOEXEC: + { + const int new_fd = TRY(get_free_fd()); + + auto new_file = TRY(BAN::RefPtr::create( + TRY(m_open_files[fd]->file.clone()), + m_open_files[fd]->offset, + m_open_files[fd]->flags | (cmd == F_DUPFD_CLOEXEC ? O_CLOEXEC : 0) + )); + + if (new_file->path() == ""_sv) + { + ASSERT(new_file->inode()->is_pipe()); + static_cast(new_file->inode().ptr())->clone_writing(); + } + + m_open_files[new_fd] = BAN::move(new_file); + return new_fd; + } case F_GETFD: return m_open_files[fd]->flags & FD_CLOEXEC; case F_SETFD: @@ -220,6 +224,7 @@ namespace Kernel break; } + dwarnln("TODO: fcntl command {}", cmd); return BAN::Error::from_errno(ENOTSUP); } diff --git a/kernel/kernel/Process.cpp b/kernel/kernel/Process.cpp index 763067db..a1c9d154 100644 --- a/kernel/kernel/Process.cpp +++ b/kernel/kernel/Process.cpp @@ -1462,12 +1462,6 @@ namespace Kernel return 0; } - BAN::ErrorOr Process::sys_dup(int fildes) - { - LockGuard _(m_process_lock); - return TRY(m_open_file_descriptors.dup(fildes)); - } - BAN::ErrorOr Process::sys_dup2(int fildes, int fildes2) { LockGuard _(m_process_lock); diff --git a/userspace/libraries/LibC/include/sys/syscall.h b/userspace/libraries/LibC/include/sys/syscall.h index 365f926e..e8a2bb2e 100644 --- a/userspace/libraries/LibC/include/sys/syscall.h +++ b/userspace/libraries/LibC/include/sys/syscall.h @@ -36,7 +36,6 @@ __BEGIN_DECLS O(SYS_SET_PWD, setpwd) \ O(SYS_CLOCK_GETTIME, clock_gettime) \ O(SYS_PIPE, pipe) \ - O(SYS_DUP, dup) \ O(SYS_DUP2, dup2) \ O(SYS_KILL, kill) \ O(SYS_TCGETPGRP, tcgetpgrp) \ diff --git a/userspace/libraries/LibC/unistd.cpp b/userspace/libraries/LibC/unistd.cpp index 3598421e..c90626e8 100644 --- a/userspace/libraries/LibC/unistd.cpp +++ b/userspace/libraries/LibC/unistd.cpp @@ -125,7 +125,7 @@ int fsync(int fildes) int dup(int fildes) { - return syscall(SYS_DUP, fildes); + return fcntl(fildes, F_DUPFD, 0); } int dup2(int fildes, int fildes2)