mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-12-03 21:10:30 +00:00
Kernel: Turn Process::FileDescriptionAndFlags into a proper class
This commit is contained in:
parent
f2a152e930
commit
2e2de125e5
Notes:
sideshowbarker
2024-07-19 04:27:15 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/2e2de125e51
8 changed files with 34 additions and 24 deletions
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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 = {};
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue