diff options
author | Andreas Kling <kling@serenityos.org> | 2020-07-30 23:50:31 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-07-30 23:50:31 +0200 |
commit | 2e2de125e5134d0fdcb6d4e96a3a025be5630811 (patch) | |
tree | 91748a0cfba22a56acbe9d66eab14a813f4dcbdf /Kernel | |
parent | f2a152e9304ebd8c564cb730190c6aaf232252b5 (diff) | |
download | serenity-2e2de125e5134d0fdcb6d4e96a3a025be5630811.zip |
Kernel: Turn Process::FileDescriptionAndFlags into a proper class
Diffstat (limited to 'Kernel')
-rw-r--r-- | Kernel/Process.cpp | 14 | ||||
-rw-r--r-- | Kernel/Process.h | 20 | ||||
-rw-r--r-- | Kernel/Scheduler.cpp | 4 | ||||
-rw-r--r-- | Kernel/Syscalls/execve.cpp | 8 | ||||
-rw-r--r-- | Kernel/Syscalls/fcntl.cpp | 4 | ||||
-rw-r--r-- | Kernel/Syscalls/pipe.cpp | 4 | ||||
-rw-r--r-- | Kernel/Syscalls/socket.cpp | 2 | ||||
-rw-r--r-- | Kernel/Syscalls/watch_file.cpp | 2 |
8 files changed, 34 insertions, 24 deletions
diff --git a/Kernel/Process.cpp b/Kernel/Process.cpp index 87622d72e0..d9ef7ab873 100644 --- a/Kernel/Process.cpp +++ b/Kernel/Process.cpp @@ -488,7 +488,7 @@ RefPtr<FileDescription> Process::file_description(int fd) const if (fd < 0) return nullptr; if (static_cast<size_t>(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<size_t>(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<FileDescription>&& d, u32 f) +void Process::FileDescriptionAndFlags::set(NonnullRefPtr<FileDescription>&& 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 049dbc7df3..da3eee26d0 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<FileDescription>&& d, u32 f = 0); - RefPtr<FileDescription> description; - u32 flags { 0 }; + void set(NonnullRefPtr<FileDescription>&&, u32 flags = 0); + + private: + RefPtr<FileDescription> m_description; + u32 m_flags { 0 }; }; Vector<FileDescriptionAndFlags> m_fds; diff --git a/Kernel/Scheduler.cpp b/Kernel/Scheduler.cpp index a442c55931..c01caa1798 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 3be8dc69ae..5aecceb5b3 100644 --- a/Kernel/Syscalls/execve.cpp +++ b/Kernel/Syscalls/execve.cpp @@ -244,10 +244,10 @@ int Process::do_exec(NonnullRefPtr<FileDescription> 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 7c8298d761..c068d59abc 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 af58f27697..182c289857 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 feb5539a79..9204abcfc9 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 222bb268a9..ee87e32d08 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; } |