From 2e2de125e5134d0fdcb6d4e96a3a025be5630811 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Thu, 30 Jul 2020 23:50:31 +0200 Subject: [PATCH] Kernel: Turn Process::FileDescriptionAndFlags into a proper class --- Kernel/Process.cpp | 14 +++++++------- Kernel/Process.h | 20 +++++++++++++++----- Kernel/Scheduler.cpp | 4 ++-- Kernel/Syscalls/execve.cpp | 8 ++++---- Kernel/Syscalls/fcntl.cpp | 4 ++-- Kernel/Syscalls/pipe.cpp | 4 ++-- Kernel/Syscalls/socket.cpp | 2 +- Kernel/Syscalls/watch_file.cpp | 2 +- 8 files changed, 34 insertions(+), 24 deletions(-) diff --git a/Kernel/Process.cpp b/Kernel/Process.cpp index 87622d72e0f..d9ef7ab873f 100644 --- a/Kernel/Process.cpp +++ b/Kernel/Process.cpp @@ -488,7 +488,7 @@ RefPtr Process::file_description(int fd) const if (fd < 0) return nullptr; if (static_cast(fd) < m_fds.size()) - return m_fds[fd].description.ptr(); + return m_fds[fd].description(); return nullptr; } @@ -497,7 +497,7 @@ int Process::fd_flags(int fd) const if (fd < 0) return -1; if (static_cast(fd) < m_fds.size()) - return m_fds[fd].flags; + return m_fds[fd].flags(); return -1; } @@ -800,14 +800,14 @@ Thread* Process::create_kernel_thread(void (*entry)(), u32 priority, const Strin void Process::FileDescriptionAndFlags::clear() { - description = nullptr; - flags = 0; + m_description = nullptr; + m_flags = 0; } -void Process::FileDescriptionAndFlags::set(NonnullRefPtr&& d, u32 f) +void Process::FileDescriptionAndFlags::set(NonnullRefPtr&& description, u32 flags) { - description = move(d); - flags = f; + m_description = move(description); + m_flags = flags; } KBuffer Process::backtrace(ProcessInspectionHandle& handle) const diff --git a/Kernel/Process.h b/Kernel/Process.h index 049dbc7df35..da3eee26d0e 100644 --- a/Kernel/Process.h +++ b/Kernel/Process.h @@ -573,12 +573,22 @@ private: static const int m_max_open_file_descriptors { FD_SETSIZE }; - struct FileDescriptionAndFlags { - operator bool() const { return !!description; } + class FileDescriptionAndFlags { + public: + operator bool() const { return !!m_description; } + + FileDescription* description() { return m_description; } + const FileDescription* description() const { return m_description; } + + u32 flags() const { return m_flags; } + void set_flags(u32 flags) { m_flags = flags; } + void clear(); - void set(NonnullRefPtr&& d, u32 f = 0); - RefPtr description; - u32 flags { 0 }; + void set(NonnullRefPtr&&, u32 flags = 0); + + private: + RefPtr m_description; + u32 m_flags { 0 }; }; Vector m_fds; diff --git a/Kernel/Scheduler.cpp b/Kernel/Scheduler.cpp index a442c559317..c01caa1798b 100644 --- a/Kernel/Scheduler.cpp +++ b/Kernel/Scheduler.cpp @@ -221,13 +221,13 @@ bool Thread::SelectBlocker::should_unblock(Thread& thread, time_t now_sec, long for (int fd : m_select_read_fds) { if (!process.m_fds[fd]) continue; - if (process.m_fds[fd].description->can_read()) + if (process.m_fds[fd].description()->can_read()) return true; } for (int fd : m_select_write_fds) { if (!process.m_fds[fd]) continue; - if (process.m_fds[fd].description->can_write()) + if (process.m_fds[fd].description()->can_write()) return true; } diff --git a/Kernel/Syscalls/execve.cpp b/Kernel/Syscalls/execve.cpp index 3be8dc69ae7..5aecceb5b3b 100644 --- a/Kernel/Syscalls/execve.cpp +++ b/Kernel/Syscalls/execve.cpp @@ -244,10 +244,10 @@ int Process::do_exec(NonnullRefPtr main_program_description, Ve disown_all_shared_buffers(); for (size_t i = 0; i < m_fds.size(); ++i) { - auto& daf = m_fds[i]; - if (daf.description && daf.flags & FD_CLOEXEC) { - daf.description->close(); - daf = {}; + auto& description_and_flags = m_fds[i]; + if (description_and_flags.description() && description_and_flags.flags() & FD_CLOEXEC) { + description_and_flags.description()->close(); + description_and_flags = {}; } } diff --git a/Kernel/Syscalls/fcntl.cpp b/Kernel/Syscalls/fcntl.cpp index 7c8298d761f..c068d59abce 100644 --- a/Kernel/Syscalls/fcntl.cpp +++ b/Kernel/Syscalls/fcntl.cpp @@ -52,9 +52,9 @@ int Process::sys$fcntl(int fd, int cmd, u32 arg) return new_fd; } case F_GETFD: - return m_fds[fd].flags; + return m_fds[fd].flags(); case F_SETFD: - m_fds[fd].flags = arg; + m_fds[fd].set_flags(arg); break; case F_GETFL: return description->file_flags(); diff --git a/Kernel/Syscalls/pipe.cpp b/Kernel/Syscalls/pipe.cpp index af58f27697d..182c2898575 100644 --- a/Kernel/Syscalls/pipe.cpp +++ b/Kernel/Syscalls/pipe.cpp @@ -46,12 +46,12 @@ int Process::sys$pipe(int pipefd[2], int flags) int reader_fd = alloc_fd(); m_fds[reader_fd].set(fifo->open_direction(FIFO::Direction::Reader), fd_flags); - m_fds[reader_fd].description->set_readable(true); + m_fds[reader_fd].description()->set_readable(true); copy_to_user(&pipefd[0], &reader_fd); int writer_fd = alloc_fd(); m_fds[writer_fd].set(fifo->open_direction(FIFO::Direction::Writer), fd_flags); - m_fds[writer_fd].description->set_writable(true); + m_fds[writer_fd].description()->set_writable(true); copy_to_user(&pipefd[1], &writer_fd); return 0; diff --git a/Kernel/Syscalls/socket.cpp b/Kernel/Syscalls/socket.cpp index feb5539a799..9204abcfc96 100644 --- a/Kernel/Syscalls/socket.cpp +++ b/Kernel/Syscalls/socket.cpp @@ -115,7 +115,7 @@ int Process::sys$accept(int accepting_socket_fd, sockaddr* user_address, socklen // NOTE: The accepted socket inherits fd flags from the accepting socket. // I'm not sure if this matches other systems but it makes sense to me. accepted_socket_description->set_blocking(accepting_socket_description->is_blocking()); - m_fds[accepted_socket_fd].set(move(accepted_socket_description), m_fds[accepting_socket_fd].flags); + m_fds[accepted_socket_fd].set(move(accepted_socket_description), m_fds[accepting_socket_fd].flags()); // NOTE: Moving this state to Completed is what causes connect() to unblock on the client side. accepted_socket->set_setup_state(Socket::SetupState::Completed); diff --git a/Kernel/Syscalls/watch_file.cpp b/Kernel/Syscalls/watch_file.cpp index 222bb268a95..ee87e32d087 100644 --- a/Kernel/Syscalls/watch_file.cpp +++ b/Kernel/Syscalls/watch_file.cpp @@ -53,7 +53,7 @@ int Process::sys$watch_file(const char* user_path, size_t path_length) return fd; m_fds[fd].set(FileDescription::create(*InodeWatcher::create(inode))); - m_fds[fd].description->set_readable(true); + m_fds[fd].description()->set_readable(true); return fd; }