mirror of
https://github.com/RGBCube/serenity
synced 2025-05-25 21:05:07 +00:00
Kernel: Don't copy a Vector<FileDescriptionAndFlags>
Instead of copying a Vector everytime we need to enumerate a Process' file descriptions, we can just temporarily lock so it won't change.
This commit is contained in:
parent
12b6e69150
commit
7c87891c06
28 changed files with 198 additions and 128 deletions
|
@ -159,7 +159,7 @@ RefPtr<Process> Process::create_user_process(RefPtr<Thread>& first_thread, const
|
||||||
auto process = Process::create(first_thread, parts.take_last(), uid, gid, parent_pid, false, move(cwd), nullptr, tty);
|
auto process = Process::create(first_thread, parts.take_last(), uid, gid, parent_pid, false, move(cwd), nullptr, tty);
|
||||||
if (!first_thread)
|
if (!first_thread)
|
||||||
return {};
|
return {};
|
||||||
if (!process->m_fds.try_resize(m_max_open_file_descriptors)) {
|
if (!process->m_fds.try_resize(process->m_fds.max_open())) {
|
||||||
first_thread = nullptr;
|
first_thread = nullptr;
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
@ -393,42 +393,68 @@ RefPtr<Process> Process::from_pid(ProcessID pid)
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
RefPtr<FileDescription> Process::file_description(int fd) const
|
const Process::FileDescriptionAndFlags& Process::FileDescriptions::at(size_t i) const
|
||||||
|
{
|
||||||
|
ScopedSpinLock lock(m_fds_lock);
|
||||||
|
return m_fds_metadatas[i];
|
||||||
|
}
|
||||||
|
Process::FileDescriptionAndFlags& Process::FileDescriptions::at(size_t i)
|
||||||
|
{
|
||||||
|
ScopedSpinLock lock(m_fds_lock);
|
||||||
|
return m_fds_metadatas[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
RefPtr<FileDescription> Process::FileDescriptions::file_description(int fd) const
|
||||||
{
|
{
|
||||||
ScopedSpinLock lock(m_fds_lock);
|
ScopedSpinLock lock(m_fds_lock);
|
||||||
if (fd < 0)
|
if (fd < 0)
|
||||||
return nullptr;
|
return nullptr;
|
||||||
if (static_cast<size_t>(fd) < m_fds.size())
|
if (static_cast<size_t>(fd) < m_fds_metadatas.size())
|
||||||
return m_fds[fd].description();
|
return m_fds_metadatas[fd].description();
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
int Process::fd_flags(int fd) const
|
int Process::FileDescriptions::fd_flags(int fd) const
|
||||||
{
|
{
|
||||||
ScopedSpinLock lock(m_fds_lock);
|
ScopedSpinLock lock(m_fds_lock);
|
||||||
if (fd < 0)
|
if (fd < 0)
|
||||||
return -1;
|
return -1;
|
||||||
if (static_cast<size_t>(fd) < m_fds.size())
|
if (static_cast<size_t>(fd) < m_fds_metadatas.size())
|
||||||
return m_fds[fd].flags();
|
return m_fds_metadatas[fd].flags();
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int Process::number_of_open_file_descriptors() const
|
void Process::FileDescriptions::enumerate(Function<void(const FileDescriptionAndFlags&)> callback) const
|
||||||
{
|
{
|
||||||
ScopedSpinLock lock(m_fds_lock);
|
ScopedSpinLock lock(m_fds_lock);
|
||||||
int count = 0;
|
for (auto& file_description_metadata : m_fds_metadatas) {
|
||||||
for (size_t index = 0; index < m_fds.size(); index++) {
|
callback(file_description_metadata);
|
||||||
if (m_fds[index].is_valid())
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Process::FileDescriptions::change_each(Function<void(FileDescriptionAndFlags&)> callback)
|
||||||
|
{
|
||||||
|
ScopedSpinLock lock(m_fds_lock);
|
||||||
|
for (auto& file_description_metadata : m_fds_metadatas) {
|
||||||
|
callback(file_description_metadata);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t Process::FileDescriptions::open_count() const
|
||||||
|
{
|
||||||
|
size_t count = 0;
|
||||||
|
enumerate([&](auto& file_description_metadata) {
|
||||||
|
if (file_description_metadata.is_valid())
|
||||||
++count;
|
++count;
|
||||||
}
|
});
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
int Process::alloc_fd(int first_candidate_fd)
|
int Process::FileDescriptions::allocate(int first_candidate_fd)
|
||||||
{
|
{
|
||||||
ScopedSpinLock lock(m_fds_lock);
|
ScopedSpinLock lock(m_fds_lock);
|
||||||
for (int i = first_candidate_fd; i < (int)m_max_open_file_descriptors; ++i) {
|
for (size_t i = first_candidate_fd; i < max_open(); ++i) {
|
||||||
if (!m_fds[i])
|
if (!m_fds_metadatas[i])
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
return -EMFILE;
|
return -EMFILE;
|
||||||
|
|
|
@ -228,9 +228,6 @@ public:
|
||||||
|
|
||||||
bool in_group(gid_t) const;
|
bool in_group(gid_t) const;
|
||||||
|
|
||||||
RefPtr<FileDescription> file_description(int fd) const;
|
|
||||||
int fd_flags(int fd) const;
|
|
||||||
|
|
||||||
// Breakable iteration functions
|
// Breakable iteration functions
|
||||||
template<IteratorFunction<Process&> Callback>
|
template<IteratorFunction<Process&> Callback>
|
||||||
static void for_each(Callback);
|
static void for_each(Callback);
|
||||||
|
@ -443,12 +440,6 @@ public:
|
||||||
const Vector<String>& arguments() const { return m_arguments; };
|
const Vector<String>& arguments() const { return m_arguments; };
|
||||||
const Vector<String>& environment() const { return m_environment; };
|
const Vector<String>& environment() const { return m_environment; };
|
||||||
|
|
||||||
int number_of_open_file_descriptors() const;
|
|
||||||
int max_open_file_descriptors() const
|
|
||||||
{
|
|
||||||
return m_max_open_file_descriptors;
|
|
||||||
}
|
|
||||||
|
|
||||||
KResult exec(String path, Vector<String> arguments, Vector<String> environment, int recusion_depth = 0);
|
KResult exec(String path, Vector<String> arguments, Vector<String> environment, int recusion_depth = 0);
|
||||||
|
|
||||||
KResultOr<LoadResult> load(NonnullRefPtr<FileDescription> main_program_description, RefPtr<FileDescription> interpreter_description, const ElfW(Ehdr) & main_program_header);
|
KResultOr<LoadResult> load(NonnullRefPtr<FileDescription> main_program_description, RefPtr<FileDescription> interpreter_description, const ElfW(Ehdr) & main_program_header);
|
||||||
|
@ -541,8 +532,6 @@ private:
|
||||||
|
|
||||||
KResultOr<RefPtr<FileDescription>> find_elf_interpreter_for_executable(const String& path, const ElfW(Ehdr) & elf_header, int nread, size_t file_size);
|
KResultOr<RefPtr<FileDescription>> find_elf_interpreter_for_executable(const String& path, const ElfW(Ehdr) & elf_header, int nread, size_t file_size);
|
||||||
|
|
||||||
int alloc_fd(int first_candidate_fd = 0);
|
|
||||||
|
|
||||||
KResult do_kill(Process&, int signal);
|
KResult do_kill(Process&, int signal);
|
||||||
KResult do_killpg(ProcessGroupID pgrp, int signal);
|
KResult do_killpg(ProcessGroupID pgrp, int signal);
|
||||||
KResult do_killall(int signal);
|
KResult do_killall(int signal);
|
||||||
|
@ -586,8 +575,6 @@ private:
|
||||||
|
|
||||||
OwnPtr<ThreadTracer> m_tracer;
|
OwnPtr<ThreadTracer> m_tracer;
|
||||||
|
|
||||||
static constexpr int m_max_open_file_descriptors { FD_SETSIZE };
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
class FileDescriptionAndFlags {
|
class FileDescriptionAndFlags {
|
||||||
friend class FileDescriptionRegistrar;
|
friend class FileDescriptionRegistrar;
|
||||||
|
@ -616,14 +603,62 @@ public:
|
||||||
InodeIndex m_global_procfs_inode_index;
|
InodeIndex m_global_procfs_inode_index;
|
||||||
};
|
};
|
||||||
|
|
||||||
// FIXME: We create a copy when trying to iterate the Vector because
|
class FileDescriptions {
|
||||||
// we don't want to put lots of locking when accessing this Vector.
|
friend class Process;
|
||||||
// Maybe try to encapsulate the Vector inside a protective class with locking
|
|
||||||
// mechanism.
|
public:
|
||||||
Vector<FileDescriptionAndFlags> fds() const { return m_fds; }
|
ALWAYS_INLINE const FileDescriptionAndFlags& operator[](size_t i) const { return at(i); }
|
||||||
|
ALWAYS_INLINE FileDescriptionAndFlags& operator[](size_t i) { return at(i); }
|
||||||
|
|
||||||
|
FileDescriptions& operator=(const Kernel::Process::FileDescriptions& other)
|
||||||
|
{
|
||||||
|
ScopedSpinLock lock(m_fds_lock);
|
||||||
|
ScopedSpinLock lock_other(other.m_fds_lock);
|
||||||
|
m_fds_metadatas = other.m_fds_metadatas;
|
||||||
|
for (auto& file_description_metadata : m_fds_metadatas) {
|
||||||
|
file_description_metadata.refresh_inode_index();
|
||||||
|
}
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
const FileDescriptionAndFlags& at(size_t i) const;
|
||||||
|
FileDescriptionAndFlags& at(size_t i);
|
||||||
|
|
||||||
|
void enumerate(Function<void(const FileDescriptionAndFlags&)>) const;
|
||||||
|
void change_each(Function<void(FileDescriptionAndFlags&)>);
|
||||||
|
|
||||||
|
int allocate(int first_candidate_fd = 0);
|
||||||
|
size_t open_count() const;
|
||||||
|
|
||||||
|
bool try_resize(size_t size) { return m_fds_metadatas.try_resize(size); }
|
||||||
|
|
||||||
|
size_t max_open() const
|
||||||
|
{
|
||||||
|
return m_max_open_file_descriptors;
|
||||||
|
}
|
||||||
|
|
||||||
|
void clear()
|
||||||
|
{
|
||||||
|
ScopedSpinLock lock(m_fds_lock);
|
||||||
|
m_fds_metadatas.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
// FIXME: Consider to remove this somehow
|
||||||
|
RefPtr<FileDescription> file_description(int fd) const;
|
||||||
|
int fd_flags(int fd) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Vector<FileDescriptionAndFlags> m_fds;
|
FileDescriptions() = default;
|
||||||
|
static constexpr size_t m_max_open_file_descriptors { FD_SETSIZE };
|
||||||
|
mutable SpinLock<u8> m_fds_lock;
|
||||||
|
Vector<FileDescriptionAndFlags> m_fds_metadatas;
|
||||||
|
};
|
||||||
|
|
||||||
|
FileDescriptions& fds() { return m_fds; }
|
||||||
|
const FileDescriptions& fds() const { return m_fds; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
FileDescriptions m_fds;
|
||||||
|
|
||||||
mutable RecursiveSpinLock m_thread_list_lock;
|
mutable RecursiveSpinLock m_thread_list_lock;
|
||||||
|
|
||||||
|
@ -659,7 +694,6 @@ private:
|
||||||
|
|
||||||
FutexQueues m_futex_queues;
|
FutexQueues m_futex_queues;
|
||||||
SpinLock<u8> m_futex_lock;
|
SpinLock<u8> m_futex_lock;
|
||||||
mutable SpinLock<u8> m_fds_lock;
|
|
||||||
|
|
||||||
// This member is used in the implementation of ptrace's PT_TRACEME flag.
|
// This member is used in the implementation of ptrace's PT_TRACEME flag.
|
||||||
// If it is set to true, the process will stop at the next execve syscall
|
// If it is set to true, the process will stop at the next execve syscall
|
||||||
|
|
|
@ -454,7 +454,7 @@ private:
|
||||||
process_object.add("uid", process.uid());
|
process_object.add("uid", process.uid());
|
||||||
process_object.add("gid", process.gid());
|
process_object.add("gid", process.gid());
|
||||||
process_object.add("ppid", process.ppid().value());
|
process_object.add("ppid", process.ppid().value());
|
||||||
process_object.add("nfds", process.number_of_open_file_descriptors());
|
process_object.add("nfds", process.fds().open_count());
|
||||||
process_object.add("name", process.name());
|
process_object.add("name", process.name());
|
||||||
process_object.add("executable", process.executable() ? process.executable()->absolute_path() : "");
|
process_object.add("executable", process.executable() ? process.executable()->absolute_path() : "");
|
||||||
process_object.add("tty", process.tty() ? process.tty()->tty_name() : "notty");
|
process_object.add("tty", process.tty() ? process.tty()->tty_name() : "notty");
|
||||||
|
@ -1239,7 +1239,7 @@ private:
|
||||||
KResultOr<size_t> ProcFSProcessFileDescriptions::entries_count() const
|
KResultOr<size_t> ProcFSProcessFileDescriptions::entries_count() const
|
||||||
{
|
{
|
||||||
Locker locker(m_lock);
|
Locker locker(m_lock);
|
||||||
return m_process_folder->m_associated_process->number_of_open_file_descriptors();
|
return m_process_folder->m_associated_process->fds().open_count();
|
||||||
}
|
}
|
||||||
KResult ProcFSProcessFileDescriptions::traverse_as_directory(unsigned fsid, Function<bool(const FS::DirectoryEntryView&)> callback) const
|
KResult ProcFSProcessFileDescriptions::traverse_as_directory(unsigned fsid, Function<bool(const FS::DirectoryEntryView&)> callback) const
|
||||||
{
|
{
|
||||||
|
@ -1248,29 +1248,36 @@ KResult ProcFSProcessFileDescriptions::traverse_as_directory(unsigned fsid, Func
|
||||||
callback({ "..", { fsid, m_parent_folder->component_index() }, 0 });
|
callback({ "..", { fsid, m_parent_folder->component_index() }, 0 });
|
||||||
|
|
||||||
auto process = m_process_folder->m_associated_process;
|
auto process = m_process_folder->m_associated_process;
|
||||||
for (int i = 0; i < process->max_open_file_descriptors(); ++i) {
|
size_t count = 0;
|
||||||
auto description_metadata = process->fds()[i];
|
m_process_folder->m_associated_process->fds().enumerate([&](auto& file_description_metadata) {
|
||||||
if (!description_metadata.is_valid())
|
if (!file_description_metadata.is_valid()) {
|
||||||
continue;
|
count++;
|
||||||
InodeIdentifier identifier = { fsid, description_metadata.global_procfs_inode_index() };
|
return;
|
||||||
callback({ String::number(i), identifier, 0 });
|
|
||||||
}
|
}
|
||||||
|
InodeIdentifier identifier = { fsid, file_description_metadata.global_procfs_inode_index() };
|
||||||
|
callback({ String::number(count), identifier, 0 });
|
||||||
|
count++;
|
||||||
|
});
|
||||||
return KSuccess;
|
return KSuccess;
|
||||||
}
|
}
|
||||||
RefPtr<ProcFSExposedComponent> ProcFSProcessFileDescriptions::lookup(StringView name)
|
RefPtr<ProcFSExposedComponent> ProcFSProcessFileDescriptions::lookup(StringView name)
|
||||||
{
|
{
|
||||||
Locker locker(m_lock);
|
Locker locker(m_lock);
|
||||||
auto process = m_process_folder->m_associated_process;
|
auto process = m_process_folder->m_associated_process;
|
||||||
ScopedSpinLock lock(process->m_fds_lock);
|
RefPtr<ProcFSProcessFileDescription> procfd_fd;
|
||||||
for (int i = 0; i < process->max_open_file_descriptors(); ++i) {
|
// FIXME: Try to exit the loop earlier
|
||||||
auto description_metadata = process->fds()[i];
|
size_t count = 0;
|
||||||
if (!description_metadata.is_valid())
|
m_process_folder->m_associated_process->fds().enumerate([&](auto& file_description_metadata) {
|
||||||
continue;
|
if (!file_description_metadata.is_valid()) {
|
||||||
if (name == String::number(i)) {
|
count++;
|
||||||
return ProcFSProcessFileDescription::create(i, *description_metadata.description(), description_metadata.global_procfs_inode_index(), *this);
|
return;
|
||||||
}
|
}
|
||||||
|
if (name == String::number(count)) {
|
||||||
|
procfd_fd = ProcFSProcessFileDescription::create(count, *file_description_metadata.description(), file_description_metadata.global_procfs_inode_index(), *this);
|
||||||
}
|
}
|
||||||
return nullptr;
|
count++;
|
||||||
|
});
|
||||||
|
return procfd_fd;
|
||||||
}
|
}
|
||||||
|
|
||||||
class ProcFSProcessUnveil final : public ProcFSProcessInformation {
|
class ProcFSProcessUnveil final : public ProcFSProcessInformation {
|
||||||
|
@ -1350,19 +1357,21 @@ private:
|
||||||
{
|
{
|
||||||
JsonArraySerializer array { builder };
|
JsonArraySerializer array { builder };
|
||||||
auto process = m_parent_folder->m_associated_process;
|
auto process = m_parent_folder->m_associated_process;
|
||||||
if (process->number_of_open_file_descriptors() == 0) {
|
if (process->fds().open_count() == 0) {
|
||||||
array.finish();
|
array.finish();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < process->max_open_file_descriptors(); ++i) {
|
size_t count = 0;
|
||||||
auto description = process->file_description(i);
|
process->fds().enumerate([&](auto& file_description_metadata) {
|
||||||
if (!description)
|
if (!file_description_metadata.is_valid()) {
|
||||||
continue;
|
count++;
|
||||||
bool cloexec = process->fd_flags(i) & FD_CLOEXEC;
|
return;
|
||||||
|
}
|
||||||
|
bool cloexec = file_description_metadata.flags() & FD_CLOEXEC;
|
||||||
|
RefPtr<FileDescription> description = file_description_metadata.description();
|
||||||
auto description_object = array.add_object();
|
auto description_object = array.add_object();
|
||||||
description_object.add("fd", i);
|
description_object.add("fd", count);
|
||||||
description_object.add("absolute_path", description->absolute_path());
|
description_object.add("absolute_path", description->absolute_path());
|
||||||
description_object.add("seekable", description->file().is_seekable());
|
description_object.add("seekable", description->file().is_seekable());
|
||||||
description_object.add("class", description->file().class_name());
|
description_object.add("class", description->file().class_name());
|
||||||
|
@ -1371,7 +1380,9 @@ private:
|
||||||
description_object.add("blocking", description->is_blocking());
|
description_object.add("blocking", description->is_blocking());
|
||||||
description_object.add("can_read", description->can_read());
|
description_object.add("can_read", description->can_read());
|
||||||
description_object.add("can_write", description->can_write());
|
description_object.add("can_write", description->can_write());
|
||||||
}
|
count++;
|
||||||
|
});
|
||||||
|
|
||||||
array.finish();
|
array.finish();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,7 +24,7 @@ KResultOr<FlatPtr> Process::sys$anon_create(size_t size, int options)
|
||||||
if (size > NumericLimits<ssize_t>::max())
|
if (size > NumericLimits<ssize_t>::max())
|
||||||
return EINVAL;
|
return EINVAL;
|
||||||
|
|
||||||
int new_fd = alloc_fd();
|
int new_fd = m_fds.allocate();
|
||||||
if (new_fd < 0)
|
if (new_fd < 0)
|
||||||
return new_fd;
|
return new_fd;
|
||||||
|
|
||||||
|
|
|
@ -27,7 +27,7 @@ KResultOr<FlatPtr> Process::sys$chdir(Userspace<const char*> user_path, size_t p
|
||||||
KResultOr<FlatPtr> Process::sys$fchdir(int fd)
|
KResultOr<FlatPtr> Process::sys$fchdir(int fd)
|
||||||
{
|
{
|
||||||
REQUIRE_PROMISE(stdio);
|
REQUIRE_PROMISE(stdio);
|
||||||
auto description = file_description(fd);
|
auto description = fds().file_description(fd);
|
||||||
if (!description)
|
if (!description)
|
||||||
return EBADF;
|
return EBADF;
|
||||||
|
|
||||||
|
|
|
@ -23,7 +23,7 @@ KResultOr<FlatPtr> Process::sys$chmod(Userspace<const char*> user_path, size_t p
|
||||||
KResultOr<FlatPtr> Process::sys$fchmod(int fd, mode_t mode)
|
KResultOr<FlatPtr> Process::sys$fchmod(int fd, mode_t mode)
|
||||||
{
|
{
|
||||||
REQUIRE_PROMISE(fattr);
|
REQUIRE_PROMISE(fattr);
|
||||||
auto description = file_description(fd);
|
auto description = fds().file_description(fd);
|
||||||
if (!description)
|
if (!description)
|
||||||
return EBADF;
|
return EBADF;
|
||||||
return description->chmod(mode);
|
return description->chmod(mode);
|
||||||
|
|
|
@ -12,7 +12,7 @@ namespace Kernel {
|
||||||
KResultOr<FlatPtr> Process::sys$fchown(int fd, uid_t uid, gid_t gid)
|
KResultOr<FlatPtr> Process::sys$fchown(int fd, uid_t uid, gid_t gid)
|
||||||
{
|
{
|
||||||
REQUIRE_PROMISE(chown);
|
REQUIRE_PROMISE(chown);
|
||||||
auto description = file_description(fd);
|
auto description = fds().file_description(fd);
|
||||||
if (!description)
|
if (!description)
|
||||||
return EBADF;
|
return EBADF;
|
||||||
return description->chown(uid, gid);
|
return description->chown(uid, gid);
|
||||||
|
|
|
@ -12,12 +12,12 @@ namespace Kernel {
|
||||||
KResultOr<FlatPtr> Process::sys$dup2(int old_fd, int new_fd)
|
KResultOr<FlatPtr> Process::sys$dup2(int old_fd, int new_fd)
|
||||||
{
|
{
|
||||||
REQUIRE_PROMISE(stdio);
|
REQUIRE_PROMISE(stdio);
|
||||||
auto description = file_description(old_fd);
|
auto description = fds().file_description(old_fd);
|
||||||
if (!description)
|
if (!description)
|
||||||
return EBADF;
|
return EBADF;
|
||||||
if (old_fd == new_fd)
|
if (old_fd == new_fd)
|
||||||
return new_fd;
|
return new_fd;
|
||||||
if (new_fd < 0 || new_fd >= m_max_open_file_descriptors)
|
if (new_fd < 0 || static_cast<size_t>(new_fd) >= fds().max_open())
|
||||||
return EINVAL;
|
return EINVAL;
|
||||||
m_fds[new_fd].set(*description);
|
m_fds[new_fd].set(*description);
|
||||||
return new_fd;
|
return new_fd;
|
||||||
|
|
|
@ -573,15 +573,14 @@ KResult Process::do_exec(NonnullRefPtr<FileDescription> main_program_description
|
||||||
|
|
||||||
clear_futex_queues_on_exec();
|
clear_futex_queues_on_exec();
|
||||||
|
|
||||||
for (size_t i = 0; i < m_fds.size(); ++i) {
|
fds().change_each([&](auto& file_description_metadata) {
|
||||||
auto& description_and_flags = m_fds[i];
|
if (file_description_metadata.is_valid() && file_description_metadata.flags() & FD_CLOEXEC)
|
||||||
if (description_and_flags.description() && description_and_flags.flags() & FD_CLOEXEC)
|
file_description_metadata = {};
|
||||||
description_and_flags = {};
|
});
|
||||||
}
|
|
||||||
|
|
||||||
int main_program_fd = -1;
|
int main_program_fd = -1;
|
||||||
if (interpreter_description) {
|
if (interpreter_description) {
|
||||||
main_program_fd = alloc_fd();
|
main_program_fd = m_fds.allocate();
|
||||||
VERIFY(main_program_fd >= 0);
|
VERIFY(main_program_fd >= 0);
|
||||||
auto seek_result = main_program_description->seek(0, SEEK_SET);
|
auto seek_result = main_program_description->seek(0, SEEK_SET);
|
||||||
VERIFY(!seek_result.is_error());
|
VERIFY(!seek_result.is_error());
|
||||||
|
|
|
@ -14,7 +14,7 @@ KResultOr<FlatPtr> Process::sys$fcntl(int fd, int cmd, u32 arg)
|
||||||
{
|
{
|
||||||
REQUIRE_PROMISE(stdio);
|
REQUIRE_PROMISE(stdio);
|
||||||
dbgln_if(IO_DEBUG, "sys$fcntl: fd={}, cmd={}, arg={}", fd, cmd, arg);
|
dbgln_if(IO_DEBUG, "sys$fcntl: fd={}, cmd={}, arg={}", fd, cmd, arg);
|
||||||
auto description = file_description(fd);
|
auto description = fds().file_description(fd);
|
||||||
if (!description)
|
if (!description)
|
||||||
return EBADF;
|
return EBADF;
|
||||||
// NOTE: The FD flags are not shared between FileDescription objects.
|
// NOTE: The FD flags are not shared between FileDescription objects.
|
||||||
|
@ -24,7 +24,7 @@ KResultOr<FlatPtr> Process::sys$fcntl(int fd, int cmd, u32 arg)
|
||||||
int arg_fd = (int)arg;
|
int arg_fd = (int)arg;
|
||||||
if (arg_fd < 0)
|
if (arg_fd < 0)
|
||||||
return EINVAL;
|
return EINVAL;
|
||||||
int new_fd = alloc_fd(arg_fd);
|
int new_fd = fds().allocate(arg_fd);
|
||||||
if (new_fd < 0)
|
if (new_fd < 0)
|
||||||
return new_fd;
|
return new_fd;
|
||||||
m_fds[new_fd].set(*description);
|
m_fds[new_fd].set(*description);
|
||||||
|
|
|
@ -17,7 +17,7 @@ KResultOr<FlatPtr> Process::sys$ftruncate(int fd, Userspace<off_t*> userspace_le
|
||||||
return EFAULT;
|
return EFAULT;
|
||||||
if (length < 0)
|
if (length < 0)
|
||||||
return EINVAL;
|
return EINVAL;
|
||||||
auto description = file_description(fd);
|
auto description = fds().file_description(fd);
|
||||||
if (!description)
|
if (!description)
|
||||||
return EBADF;
|
return EBADF;
|
||||||
if (!description->is_writable())
|
if (!description->is_writable())
|
||||||
|
|
|
@ -14,7 +14,7 @@ KResultOr<FlatPtr> Process::sys$get_dir_entries(int fd, Userspace<void*> user_bu
|
||||||
REQUIRE_PROMISE(stdio);
|
REQUIRE_PROMISE(stdio);
|
||||||
if (user_size > NumericLimits<ssize_t>::max())
|
if (user_size > NumericLimits<ssize_t>::max())
|
||||||
return EINVAL;
|
return EINVAL;
|
||||||
auto description = file_description(fd);
|
auto description = fds().file_description(fd);
|
||||||
if (!description)
|
if (!description)
|
||||||
return EBADF;
|
return EBADF;
|
||||||
auto buffer = UserOrKernelBuffer::for_user_buffer(user_buffer, static_cast<size_t>(user_size));
|
auto buffer = UserOrKernelBuffer::for_user_buffer(user_buffer, static_cast<size_t>(user_size));
|
||||||
|
|
|
@ -17,7 +17,7 @@ KResultOr<FlatPtr> Process::sys$create_inode_watcher(u32 flags)
|
||||||
{
|
{
|
||||||
REQUIRE_PROMISE(rpath);
|
REQUIRE_PROMISE(rpath);
|
||||||
|
|
||||||
int fd = alloc_fd();
|
int fd = m_fds.allocate();
|
||||||
if (fd < 0)
|
if (fd < 0)
|
||||||
return fd;
|
return fd;
|
||||||
|
|
||||||
|
@ -48,7 +48,7 @@ KResultOr<FlatPtr> Process::sys$inode_watcher_add_watch(Userspace<const Syscall:
|
||||||
if (!copy_from_user(¶ms, user_params))
|
if (!copy_from_user(¶ms, user_params))
|
||||||
return EFAULT;
|
return EFAULT;
|
||||||
|
|
||||||
auto description = file_description(params.fd);
|
auto description = fds().file_description(params.fd);
|
||||||
if (!description)
|
if (!description)
|
||||||
return EBADF;
|
return EBADF;
|
||||||
if (!description->is_inode_watcher())
|
if (!description->is_inode_watcher())
|
||||||
|
@ -75,7 +75,7 @@ KResultOr<FlatPtr> Process::sys$inode_watcher_add_watch(Userspace<const Syscall:
|
||||||
|
|
||||||
KResultOr<FlatPtr> Process::sys$inode_watcher_remove_watch(int fd, int wd)
|
KResultOr<FlatPtr> Process::sys$inode_watcher_remove_watch(int fd, int wd)
|
||||||
{
|
{
|
||||||
auto description = file_description(fd);
|
auto description = fds().file_description(fd);
|
||||||
if (!description)
|
if (!description)
|
||||||
return EBADF;
|
return EBADF;
|
||||||
if (!description->is_inode_watcher())
|
if (!description->is_inode_watcher())
|
||||||
|
|
|
@ -12,7 +12,7 @@ namespace Kernel {
|
||||||
|
|
||||||
KResultOr<FlatPtr> Process::sys$ioctl(int fd, unsigned request, FlatPtr arg)
|
KResultOr<FlatPtr> Process::sys$ioctl(int fd, unsigned request, FlatPtr arg)
|
||||||
{
|
{
|
||||||
auto description = file_description(fd);
|
auto description = fds().file_description(fd);
|
||||||
if (!description)
|
if (!description)
|
||||||
return EBADF;
|
return EBADF;
|
||||||
if (request == FIONBIO) {
|
if (request == FIONBIO) {
|
||||||
|
|
|
@ -12,7 +12,7 @@ namespace Kernel {
|
||||||
KResultOr<FlatPtr> Process::sys$lseek(int fd, Userspace<off_t*> userspace_offset, int whence)
|
KResultOr<FlatPtr> Process::sys$lseek(int fd, Userspace<off_t*> userspace_offset, int whence)
|
||||||
{
|
{
|
||||||
REQUIRE_PROMISE(stdio);
|
REQUIRE_PROMISE(stdio);
|
||||||
auto description = file_description(fd);
|
auto description = fds().file_description(fd);
|
||||||
if (!description)
|
if (!description)
|
||||||
return EBADF;
|
return EBADF;
|
||||||
off_t offset;
|
off_t offset;
|
||||||
|
|
|
@ -224,7 +224,7 @@ KResultOr<FlatPtr> Process::sys$mmap(Userspace<const Syscall::SC_mmap_params*> u
|
||||||
return EINVAL;
|
return EINVAL;
|
||||||
if (static_cast<size_t>(offset) & ~PAGE_MASK)
|
if (static_cast<size_t>(offset) & ~PAGE_MASK)
|
||||||
return EINVAL;
|
return EINVAL;
|
||||||
auto description = file_description(fd);
|
auto description = fds().file_description(fd);
|
||||||
if (!description)
|
if (!description)
|
||||||
return EBADF;
|
return EBADF;
|
||||||
if (description->is_directory())
|
if (description->is_directory())
|
||||||
|
|
|
@ -36,7 +36,7 @@ KResultOr<FlatPtr> Process::sys$mount(Userspace<const Syscall::SC_mount_params*>
|
||||||
if (fs_type.is_null())
|
if (fs_type.is_null())
|
||||||
return EFAULT;
|
return EFAULT;
|
||||||
|
|
||||||
auto description = file_description(source_fd);
|
auto description = fds().file_description(source_fd);
|
||||||
if (!description.is_null())
|
if (!description.is_null())
|
||||||
dbgln("mount {}: source fd {} @ {}", fs_type, source_fd, target);
|
dbgln("mount {}: source fd {} @ {}", fs_type, source_fd, target);
|
||||||
else
|
else
|
||||||
|
|
|
@ -44,7 +44,7 @@ KResultOr<FlatPtr> Process::sys$open(Userspace<const Syscall::SC_open_params*> u
|
||||||
return path.error();
|
return path.error();
|
||||||
|
|
||||||
dbgln_if(IO_DEBUG, "sys$open(dirfd={}, path='{}', options={}, mode={})", dirfd, path.value()->view(), options, mode);
|
dbgln_if(IO_DEBUG, "sys$open(dirfd={}, path='{}', options={}, mode={})", dirfd, path.value()->view(), options, mode);
|
||||||
int fd = alloc_fd();
|
int fd = m_fds.allocate();
|
||||||
if (fd < 0)
|
if (fd < 0)
|
||||||
return fd;
|
return fd;
|
||||||
|
|
||||||
|
@ -52,7 +52,7 @@ KResultOr<FlatPtr> Process::sys$open(Userspace<const Syscall::SC_open_params*> u
|
||||||
if (dirfd == AT_FDCWD) {
|
if (dirfd == AT_FDCWD) {
|
||||||
base = current_directory();
|
base = current_directory();
|
||||||
} else {
|
} else {
|
||||||
auto base_description = file_description(dirfd);
|
auto base_description = fds().file_description(dirfd);
|
||||||
if (!base_description)
|
if (!base_description)
|
||||||
return EBADF;
|
return EBADF;
|
||||||
if (!base_description->is_directory())
|
if (!base_description->is_directory())
|
||||||
|
@ -78,7 +78,7 @@ KResultOr<FlatPtr> Process::sys$open(Userspace<const Syscall::SC_open_params*> u
|
||||||
KResultOr<FlatPtr> Process::sys$close(int fd)
|
KResultOr<FlatPtr> Process::sys$close(int fd)
|
||||||
{
|
{
|
||||||
REQUIRE_PROMISE(stdio);
|
REQUIRE_PROMISE(stdio);
|
||||||
auto description = file_description(fd);
|
auto description = fds().file_description(fd);
|
||||||
dbgln_if(IO_DEBUG, "sys$close({}) {}", fd, description.ptr());
|
dbgln_if(IO_DEBUG, "sys$close({}) {}", fd, description.ptr());
|
||||||
if (!description)
|
if (!description)
|
||||||
return EBADF;
|
return EBADF;
|
||||||
|
|
|
@ -13,7 +13,7 @@ namespace Kernel {
|
||||||
KResultOr<FlatPtr> Process::sys$pipe(int pipefd[2], int flags)
|
KResultOr<FlatPtr> Process::sys$pipe(int pipefd[2], int flags)
|
||||||
{
|
{
|
||||||
REQUIRE_PROMISE(stdio);
|
REQUIRE_PROMISE(stdio);
|
||||||
if (number_of_open_file_descriptors() + 2 > max_open_file_descriptors())
|
if (fds().open_count() + 2 > fds().max_open())
|
||||||
return EMFILE;
|
return EMFILE;
|
||||||
// Reject flags other than O_CLOEXEC.
|
// Reject flags other than O_CLOEXEC.
|
||||||
if ((flags & O_CLOEXEC) != flags)
|
if ((flags & O_CLOEXEC) != flags)
|
||||||
|
@ -29,13 +29,13 @@ KResultOr<FlatPtr> Process::sys$pipe(int pipefd[2], int flags)
|
||||||
if (open_writer_result.is_error())
|
if (open_writer_result.is_error())
|
||||||
return open_writer_result.error();
|
return open_writer_result.error();
|
||||||
|
|
||||||
int reader_fd = alloc_fd();
|
int reader_fd = m_fds.allocate();
|
||||||
m_fds[reader_fd].set(open_reader_result.release_value(), fd_flags);
|
m_fds[reader_fd].set(open_reader_result.release_value(), fd_flags);
|
||||||
m_fds[reader_fd].description()->set_readable(true);
|
m_fds[reader_fd].description()->set_readable(true);
|
||||||
if (!copy_to_user(&pipefd[0], &reader_fd))
|
if (!copy_to_user(&pipefd[0], &reader_fd))
|
||||||
return EFAULT;
|
return EFAULT;
|
||||||
|
|
||||||
int writer_fd = alloc_fd();
|
int writer_fd = m_fds.allocate();
|
||||||
m_fds[writer_fd].set(open_writer_result.release_value(), fd_flags);
|
m_fds[writer_fd].set(open_writer_result.release_value(), fd_flags);
|
||||||
m_fds[writer_fd].description()->set_writable(true);
|
m_fds[writer_fd].description()->set_writable(true);
|
||||||
if (!copy_to_user(&pipefd[1], &writer_fd))
|
if (!copy_to_user(&pipefd[1], &writer_fd))
|
||||||
|
|
|
@ -34,7 +34,7 @@ KResultOr<FlatPtr> Process::sys$readv(int fd, Userspace<const struct iovec*> iov
|
||||||
return EINVAL;
|
return EINVAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto description = file_description(fd);
|
auto description = fds().file_description(fd);
|
||||||
if (!description)
|
if (!description)
|
||||||
return EBADF;
|
return EBADF;
|
||||||
|
|
||||||
|
@ -76,7 +76,7 @@ KResultOr<FlatPtr> Process::sys$read(int fd, Userspace<u8*> buffer, size_t size)
|
||||||
if (size > NumericLimits<ssize_t>::max())
|
if (size > NumericLimits<ssize_t>::max())
|
||||||
return EINVAL;
|
return EINVAL;
|
||||||
dbgln_if(IO_DEBUG, "sys$read({}, {}, {})", fd, buffer.ptr(), size);
|
dbgln_if(IO_DEBUG, "sys$read({}, {}, {})", fd, buffer.ptr(), size);
|
||||||
auto description = file_description(fd);
|
auto description = fds().file_description(fd);
|
||||||
if (!description)
|
if (!description)
|
||||||
return EBADF;
|
return EBADF;
|
||||||
if (!description->is_readable())
|
if (!description->is_readable())
|
||||||
|
|
|
@ -61,7 +61,7 @@ KResultOr<FlatPtr> Process::sys$select(Userspace<const Syscall::SC_select_params
|
||||||
return EFAULT;
|
return EFAULT;
|
||||||
|
|
||||||
Thread::SelectBlocker::FDVector fds_info;
|
Thread::SelectBlocker::FDVector fds_info;
|
||||||
Vector<int, FD_SETSIZE> fds;
|
Vector<int, FD_SETSIZE> selected_fds;
|
||||||
for (int fd = 0; fd < params.nfds; fd++) {
|
for (int fd = 0; fd < params.nfds; fd++) {
|
||||||
auto block_flags = BlockFlags::None;
|
auto block_flags = BlockFlags::None;
|
||||||
if (params.readfds && FD_ISSET(fd, &fds_read))
|
if (params.readfds && FD_ISSET(fd, &fds_read))
|
||||||
|
@ -73,14 +73,14 @@ KResultOr<FlatPtr> Process::sys$select(Userspace<const Syscall::SC_select_params
|
||||||
if (block_flags == BlockFlags::None)
|
if (block_flags == BlockFlags::None)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
auto description = file_description(fd);
|
auto description = fds().file_description(fd);
|
||||||
if (!description) {
|
if (!description) {
|
||||||
dbgln("sys$select: Bad fd number {}", fd);
|
dbgln("sys$select: Bad fd number {}", fd);
|
||||||
return EBADF;
|
return EBADF;
|
||||||
}
|
}
|
||||||
if (!fds_info.try_append({ description.release_nonnull(), block_flags }))
|
if (!fds_info.try_append({ description.release_nonnull(), block_flags }))
|
||||||
return ENOMEM;
|
return ENOMEM;
|
||||||
if (!fds.try_append(fd))
|
if (!selected_fds.try_append(fd))
|
||||||
return ENOMEM;
|
return ENOMEM;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -105,15 +105,15 @@ KResultOr<FlatPtr> Process::sys$select(Userspace<const Syscall::SC_select_params
|
||||||
if (fd_entry.unblocked_flags == BlockFlags::None)
|
if (fd_entry.unblocked_flags == BlockFlags::None)
|
||||||
continue;
|
continue;
|
||||||
if (params.readfds && has_flag(fd_entry.unblocked_flags, BlockFlags::Read)) {
|
if (params.readfds && has_flag(fd_entry.unblocked_flags, BlockFlags::Read)) {
|
||||||
FD_SET(fds[i], &fds_read);
|
FD_SET(selected_fds[i], &fds_read);
|
||||||
marked_fd_count++;
|
marked_fd_count++;
|
||||||
}
|
}
|
||||||
if (params.writefds && has_flag(fd_entry.unblocked_flags, BlockFlags::Write)) {
|
if (params.writefds && has_flag(fd_entry.unblocked_flags, BlockFlags::Write)) {
|
||||||
FD_SET(fds[i], &fds_write);
|
FD_SET(selected_fds[i], &fds_write);
|
||||||
marked_fd_count++;
|
marked_fd_count++;
|
||||||
}
|
}
|
||||||
if (params.exceptfds && has_flag(fd_entry.unblocked_flags, BlockFlags::Exception)) {
|
if (params.exceptfds && has_flag(fd_entry.unblocked_flags, BlockFlags::Exception)) {
|
||||||
FD_SET(fds[i], &fds_except);
|
FD_SET(selected_fds[i], &fds_except);
|
||||||
marked_fd_count++;
|
marked_fd_count++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -135,7 +135,7 @@ KResultOr<FlatPtr> Process::sys$poll(Userspace<const Syscall::SC_poll_params*> u
|
||||||
if (!copy_from_user(¶ms, user_params))
|
if (!copy_from_user(¶ms, user_params))
|
||||||
return EFAULT;
|
return EFAULT;
|
||||||
|
|
||||||
if (params.nfds >= m_max_open_file_descriptors)
|
if (params.nfds >= fds().max_open())
|
||||||
return ENOBUFS;
|
return ENOBUFS;
|
||||||
|
|
||||||
Thread::BlockTimeout timeout;
|
Thread::BlockTimeout timeout;
|
||||||
|
@ -165,7 +165,7 @@ KResultOr<FlatPtr> Process::sys$poll(Userspace<const Syscall::SC_poll_params*> u
|
||||||
Thread::SelectBlocker::FDVector fds_info;
|
Thread::SelectBlocker::FDVector fds_info;
|
||||||
for (size_t i = 0; i < params.nfds; i++) {
|
for (size_t i = 0; i < params.nfds; i++) {
|
||||||
auto& pfd = fds_copy[i];
|
auto& pfd = fds_copy[i];
|
||||||
auto description = file_description(pfd.fd);
|
auto description = fds().file_description(pfd.fd);
|
||||||
if (!description) {
|
if (!description) {
|
||||||
dbgln("sys$poll: Bad fd number {}", pfd.fd);
|
dbgln("sys$poll: Bad fd number {}", pfd.fd);
|
||||||
return EBADF;
|
return EBADF;
|
||||||
|
|
|
@ -13,7 +13,7 @@ namespace Kernel {
|
||||||
KResultOr<FlatPtr> Process::sys$sendfd(int sockfd, int fd)
|
KResultOr<FlatPtr> Process::sys$sendfd(int sockfd, int fd)
|
||||||
{
|
{
|
||||||
REQUIRE_PROMISE(sendfd);
|
REQUIRE_PROMISE(sendfd);
|
||||||
auto socket_description = file_description(sockfd);
|
auto socket_description = fds().file_description(sockfd);
|
||||||
if (!socket_description)
|
if (!socket_description)
|
||||||
return EBADF;
|
return EBADF;
|
||||||
if (!socket_description->is_socket())
|
if (!socket_description->is_socket())
|
||||||
|
@ -24,7 +24,7 @@ KResultOr<FlatPtr> Process::sys$sendfd(int sockfd, int fd)
|
||||||
if (!socket.is_connected())
|
if (!socket.is_connected())
|
||||||
return ENOTCONN;
|
return ENOTCONN;
|
||||||
|
|
||||||
auto passing_descriptor = file_description(fd);
|
auto passing_descriptor = fds().file_description(fd);
|
||||||
if (!passing_descriptor)
|
if (!passing_descriptor)
|
||||||
return EBADF;
|
return EBADF;
|
||||||
|
|
||||||
|
@ -35,7 +35,7 @@ KResultOr<FlatPtr> Process::sys$sendfd(int sockfd, int fd)
|
||||||
KResultOr<FlatPtr> Process::sys$recvfd(int sockfd, int options)
|
KResultOr<FlatPtr> Process::sys$recvfd(int sockfd, int options)
|
||||||
{
|
{
|
||||||
REQUIRE_PROMISE(recvfd);
|
REQUIRE_PROMISE(recvfd);
|
||||||
auto socket_description = file_description(sockfd);
|
auto socket_description = fds().file_description(sockfd);
|
||||||
if (!socket_description)
|
if (!socket_description)
|
||||||
return EBADF;
|
return EBADF;
|
||||||
if (!socket_description->is_socket())
|
if (!socket_description->is_socket())
|
||||||
|
@ -44,7 +44,7 @@ KResultOr<FlatPtr> Process::sys$recvfd(int sockfd, int options)
|
||||||
if (!socket.is_local())
|
if (!socket.is_local())
|
||||||
return EAFNOSUPPORT;
|
return EAFNOSUPPORT;
|
||||||
|
|
||||||
int new_fd = alloc_fd();
|
int new_fd = m_fds.allocate();
|
||||||
if (new_fd < 0)
|
if (new_fd < 0)
|
||||||
return new_fd;
|
return new_fd;
|
||||||
|
|
||||||
|
|
|
@ -38,7 +38,7 @@ KResultOr<FlatPtr> Process::sys$socket(int domain, int type, int protocol)
|
||||||
|
|
||||||
if ((type & SOCK_TYPE_MASK) == SOCK_RAW && !is_superuser())
|
if ((type & SOCK_TYPE_MASK) == SOCK_RAW && !is_superuser())
|
||||||
return EACCES;
|
return EACCES;
|
||||||
int fd = alloc_fd();
|
int fd = m_fds.allocate();
|
||||||
if (fd < 0)
|
if (fd < 0)
|
||||||
return fd;
|
return fd;
|
||||||
auto result = Socket::create(domain, type, protocol);
|
auto result = Socket::create(domain, type, protocol);
|
||||||
|
@ -53,7 +53,7 @@ KResultOr<FlatPtr> Process::sys$socket(int domain, int type, int protocol)
|
||||||
|
|
||||||
KResultOr<FlatPtr> Process::sys$bind(int sockfd, Userspace<const sockaddr*> address, socklen_t address_length)
|
KResultOr<FlatPtr> Process::sys$bind(int sockfd, Userspace<const sockaddr*> address, socklen_t address_length)
|
||||||
{
|
{
|
||||||
auto description = file_description(sockfd);
|
auto description = fds().file_description(sockfd);
|
||||||
if (!description)
|
if (!description)
|
||||||
return EBADF;
|
return EBADF;
|
||||||
if (!description->is_socket())
|
if (!description->is_socket())
|
||||||
|
@ -67,7 +67,7 @@ KResultOr<FlatPtr> Process::sys$listen(int sockfd, int backlog)
|
||||||
{
|
{
|
||||||
if (backlog < 0)
|
if (backlog < 0)
|
||||||
return EINVAL;
|
return EINVAL;
|
||||||
auto description = file_description(sockfd);
|
auto description = fds().file_description(sockfd);
|
||||||
if (!description)
|
if (!description)
|
||||||
return EBADF;
|
return EBADF;
|
||||||
if (!description->is_socket())
|
if (!description->is_socket())
|
||||||
|
@ -96,10 +96,10 @@ KResultOr<FlatPtr> Process::sys$accept4(Userspace<const Syscall::SC_accept4_para
|
||||||
if (user_address && !copy_from_user(&address_size, static_ptr_cast<const socklen_t*>(user_address_size)))
|
if (user_address && !copy_from_user(&address_size, static_ptr_cast<const socklen_t*>(user_address_size)))
|
||||||
return EFAULT;
|
return EFAULT;
|
||||||
|
|
||||||
int accepted_socket_fd = alloc_fd();
|
int accepted_socket_fd = m_fds.allocate();
|
||||||
if (accepted_socket_fd < 0)
|
if (accepted_socket_fd < 0)
|
||||||
return accepted_socket_fd;
|
return accepted_socket_fd;
|
||||||
auto accepting_socket_description = file_description(accepting_socket_fd);
|
auto accepting_socket_description = fds().file_description(accepting_socket_fd);
|
||||||
if (!accepting_socket_description)
|
if (!accepting_socket_description)
|
||||||
return EBADF;
|
return EBADF;
|
||||||
if (!accepting_socket_description->is_socket())
|
if (!accepting_socket_description->is_socket())
|
||||||
|
@ -148,10 +148,10 @@ KResultOr<FlatPtr> Process::sys$accept4(Userspace<const Syscall::SC_accept4_para
|
||||||
|
|
||||||
KResultOr<FlatPtr> Process::sys$connect(int sockfd, Userspace<const sockaddr*> user_address, socklen_t user_address_size)
|
KResultOr<FlatPtr> Process::sys$connect(int sockfd, Userspace<const sockaddr*> user_address, socklen_t user_address_size)
|
||||||
{
|
{
|
||||||
int fd = alloc_fd();
|
int fd = m_fds.allocate();
|
||||||
if (fd < 0)
|
if (fd < 0)
|
||||||
return fd;
|
return fd;
|
||||||
auto description = file_description(sockfd);
|
auto description = fds().file_description(sockfd);
|
||||||
if (!description)
|
if (!description)
|
||||||
return EBADF;
|
return EBADF;
|
||||||
if (!description->is_socket())
|
if (!description->is_socket())
|
||||||
|
@ -168,7 +168,7 @@ KResultOr<FlatPtr> Process::sys$shutdown(int sockfd, int how)
|
||||||
REQUIRE_PROMISE(stdio);
|
REQUIRE_PROMISE(stdio);
|
||||||
if (how & ~SHUT_RDWR)
|
if (how & ~SHUT_RDWR)
|
||||||
return EINVAL;
|
return EINVAL;
|
||||||
auto description = file_description(sockfd);
|
auto description = fds().file_description(sockfd);
|
||||||
if (!description)
|
if (!description)
|
||||||
return EBADF;
|
return EBADF;
|
||||||
if (!description->is_socket())
|
if (!description->is_socket())
|
||||||
|
@ -199,7 +199,7 @@ KResultOr<FlatPtr> Process::sys$sendmsg(int sockfd, Userspace<const struct msghd
|
||||||
Userspace<const sockaddr*> user_addr((FlatPtr)msg.msg_name);
|
Userspace<const sockaddr*> user_addr((FlatPtr)msg.msg_name);
|
||||||
socklen_t addr_length = msg.msg_namelen;
|
socklen_t addr_length = msg.msg_namelen;
|
||||||
|
|
||||||
auto description = file_description(sockfd);
|
auto description = fds().file_description(sockfd);
|
||||||
if (!description)
|
if (!description)
|
||||||
return EBADF;
|
return EBADF;
|
||||||
if (!description->is_socket())
|
if (!description->is_socket())
|
||||||
|
@ -236,7 +236,7 @@ KResultOr<FlatPtr> Process::sys$recvmsg(int sockfd, Userspace<struct msghdr*> us
|
||||||
Userspace<sockaddr*> user_addr((FlatPtr)msg.msg_name);
|
Userspace<sockaddr*> user_addr((FlatPtr)msg.msg_name);
|
||||||
Userspace<socklen_t*> user_addr_length(msg.msg_name ? (FlatPtr)&user_msg.unsafe_userspace_ptr()->msg_namelen : 0);
|
Userspace<socklen_t*> user_addr_length(msg.msg_name ? (FlatPtr)&user_msg.unsafe_userspace_ptr()->msg_namelen : 0);
|
||||||
|
|
||||||
auto description = file_description(sockfd);
|
auto description = fds().file_description(sockfd);
|
||||||
if (!description)
|
if (!description)
|
||||||
return EBADF;
|
return EBADF;
|
||||||
if (!description->is_socket())
|
if (!description->is_socket())
|
||||||
|
@ -301,7 +301,7 @@ int Process::get_sock_or_peer_name(const Params& params)
|
||||||
if (addrlen_value <= 0)
|
if (addrlen_value <= 0)
|
||||||
return EINVAL;
|
return EINVAL;
|
||||||
|
|
||||||
auto description = file_description(params.sockfd);
|
auto description = fds().file_description(params.sockfd);
|
||||||
if (!description)
|
if (!description)
|
||||||
return EBADF;
|
return EBADF;
|
||||||
|
|
||||||
|
@ -356,7 +356,7 @@ KResultOr<FlatPtr> Process::sys$getsockopt(Userspace<const Syscall::SC_getsockop
|
||||||
if (!copy_from_user(&value_size, params.value_size, sizeof(socklen_t)))
|
if (!copy_from_user(&value_size, params.value_size, sizeof(socklen_t)))
|
||||||
return EFAULT;
|
return EFAULT;
|
||||||
|
|
||||||
auto description = file_description(sockfd);
|
auto description = fds().file_description(sockfd);
|
||||||
if (!description)
|
if (!description)
|
||||||
return EBADF;
|
return EBADF;
|
||||||
if (!description->is_socket())
|
if (!description->is_socket())
|
||||||
|
@ -373,7 +373,7 @@ KResultOr<FlatPtr> Process::sys$setsockopt(Userspace<const Syscall::SC_setsockop
|
||||||
if (!copy_from_user(¶ms, user_params))
|
if (!copy_from_user(¶ms, user_params))
|
||||||
return EFAULT;
|
return EFAULT;
|
||||||
Userspace<const void*> user_value((FlatPtr)params.value);
|
Userspace<const void*> user_value((FlatPtr)params.value);
|
||||||
auto description = file_description(params.sockfd);
|
auto description = fds().file_description(params.sockfd);
|
||||||
if (!description)
|
if (!description)
|
||||||
return EBADF;
|
return EBADF;
|
||||||
if (!description->is_socket())
|
if (!description->is_socket())
|
||||||
|
@ -401,12 +401,12 @@ KResultOr<FlatPtr> Process::sys$socketpair(Userspace<const Syscall::SC_socketpai
|
||||||
auto pair = result.value();
|
auto pair = result.value();
|
||||||
|
|
||||||
int fds[2];
|
int fds[2];
|
||||||
fds[0] = alloc_fd();
|
fds[0] = m_fds.allocate();
|
||||||
if (fds[0] < 0)
|
if (fds[0] < 0)
|
||||||
return ENFILE;
|
return ENFILE;
|
||||||
setup_socket_fd(fds[0], pair.description1, params.type);
|
setup_socket_fd(fds[0], pair.description1, params.type);
|
||||||
|
|
||||||
fds[1] = alloc_fd();
|
fds[1] = m_fds.allocate();
|
||||||
if (fds[1] < 0) {
|
if (fds[1] < 0) {
|
||||||
// FIXME: This leaks fds[0]
|
// FIXME: This leaks fds[0]
|
||||||
return ENFILE;
|
return ENFILE;
|
||||||
|
|
|
@ -15,7 +15,7 @@ namespace Kernel {
|
||||||
KResultOr<FlatPtr> Process::sys$fstat(int fd, Userspace<stat*> user_statbuf)
|
KResultOr<FlatPtr> Process::sys$fstat(int fd, Userspace<stat*> user_statbuf)
|
||||||
{
|
{
|
||||||
REQUIRE_PROMISE(stdio);
|
REQUIRE_PROMISE(stdio);
|
||||||
auto description = file_description(fd);
|
auto description = fds().file_description(fd);
|
||||||
if (!description)
|
if (!description)
|
||||||
return EBADF;
|
return EBADF;
|
||||||
stat buffer = {};
|
stat buffer = {};
|
||||||
|
@ -38,7 +38,7 @@ KResultOr<FlatPtr> Process::sys$stat(Userspace<const Syscall::SC_stat_params*> u
|
||||||
if (params.dirfd == AT_FDCWD) {
|
if (params.dirfd == AT_FDCWD) {
|
||||||
base = current_directory();
|
base = current_directory();
|
||||||
} else {
|
} else {
|
||||||
auto base_description = file_description(params.dirfd);
|
auto base_description = fds().file_description(params.dirfd);
|
||||||
if (!base_description)
|
if (!base_description)
|
||||||
return EBADF;
|
return EBADF;
|
||||||
if (!base_description->is_directory())
|
if (!base_description->is_directory())
|
||||||
|
|
|
@ -87,7 +87,7 @@ KResultOr<FlatPtr> Process::sys$fstatvfs(int fd, statvfs* buf)
|
||||||
{
|
{
|
||||||
REQUIRE_PROMISE(stdio);
|
REQUIRE_PROMISE(stdio);
|
||||||
|
|
||||||
auto description = file_description(fd);
|
auto description = fds().file_description(fd);
|
||||||
if (!description)
|
if (!description)
|
||||||
return EBADF;
|
return EBADF;
|
||||||
|
|
||||||
|
|
|
@ -18,7 +18,7 @@ KResultOr<FlatPtr> Process::sys$sysconf(int name)
|
||||||
case _SC_NPROCESSORS_ONLN:
|
case _SC_NPROCESSORS_ONLN:
|
||||||
return Processor::processor_count();
|
return Processor::processor_count();
|
||||||
case _SC_OPEN_MAX:
|
case _SC_OPEN_MAX:
|
||||||
return max_open_file_descriptors();
|
return fds().max_open();
|
||||||
case _SC_PAGESIZE:
|
case _SC_PAGESIZE:
|
||||||
return PAGE_SIZE;
|
return PAGE_SIZE;
|
||||||
case _SC_TTY_NAME_MAX:
|
case _SC_TTY_NAME_MAX:
|
||||||
|
|
|
@ -14,7 +14,7 @@ namespace Kernel {
|
||||||
KResultOr<FlatPtr> Process::sys$ttyname(int fd, Userspace<char*> buffer, size_t size)
|
KResultOr<FlatPtr> Process::sys$ttyname(int fd, Userspace<char*> buffer, size_t size)
|
||||||
{
|
{
|
||||||
REQUIRE_PROMISE(tty);
|
REQUIRE_PROMISE(tty);
|
||||||
auto description = file_description(fd);
|
auto description = fds().file_description(fd);
|
||||||
if (!description)
|
if (!description)
|
||||||
return EBADF;
|
return EBADF;
|
||||||
if (!description->is_tty())
|
if (!description->is_tty())
|
||||||
|
@ -30,7 +30,7 @@ KResultOr<FlatPtr> Process::sys$ttyname(int fd, Userspace<char*> buffer, size_t
|
||||||
KResultOr<FlatPtr> Process::sys$ptsname(int fd, Userspace<char*> buffer, size_t size)
|
KResultOr<FlatPtr> Process::sys$ptsname(int fd, Userspace<char*> buffer, size_t size)
|
||||||
{
|
{
|
||||||
REQUIRE_PROMISE(tty);
|
REQUIRE_PROMISE(tty);
|
||||||
auto description = file_description(fd);
|
auto description = fds().file_description(fd);
|
||||||
if (!description)
|
if (!description)
|
||||||
return EBADF;
|
return EBADF;
|
||||||
auto* master_pty = description->master_pty();
|
auto* master_pty = description->master_pty();
|
||||||
|
|
|
@ -33,7 +33,7 @@ KResultOr<FlatPtr> Process::sys$writev(int fd, Userspace<const struct iovec*> io
|
||||||
return EINVAL;
|
return EINVAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto description = file_description(fd);
|
auto description = fds().file_description(fd);
|
||||||
if (!description)
|
if (!description)
|
||||||
return EBADF;
|
return EBADF;
|
||||||
|
|
||||||
|
@ -105,7 +105,7 @@ KResultOr<FlatPtr> Process::sys$write(int fd, Userspace<const u8*> data, size_t
|
||||||
return EINVAL;
|
return EINVAL;
|
||||||
|
|
||||||
dbgln_if(IO_DEBUG, "sys$write({}, {}, {})", fd, data.ptr(), size);
|
dbgln_if(IO_DEBUG, "sys$write({}, {}, {})", fd, data.ptr(), size);
|
||||||
auto description = file_description(fd);
|
auto description = fds().file_description(fd);
|
||||||
if (!description)
|
if (!description)
|
||||||
return EBADF;
|
return EBADF;
|
||||||
if (!description->is_writable())
|
if (!description->is_writable())
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue