From d5578826afe5c974d63cf2ab6656e16260c3cc39 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Thu, 25 Apr 2019 13:52:07 +0200 Subject: Kernel: Make it possible to look up FIFO's by ID. This will be useful when implementing the /proc/PID/fd/N links where N is a file descriptor referring to a FIFO. --- Kernel/FileSystem/FIFO.cpp | 34 +++++++++++++++++++++++++++++++--- Kernel/FileSystem/FIFO.h | 11 +++++++++-- Kernel/FileSystem/FileDescriptor.cpp | 2 +- Kernel/Process.cpp | 4 ++-- 4 files changed, 43 insertions(+), 8 deletions(-) (limited to 'Kernel') diff --git a/Kernel/FileSystem/FIFO.cpp b/Kernel/FileSystem/FIFO.cpp index f742df4449..b229c75b76 100644 --- a/Kernel/FileSystem/FIFO.cpp +++ b/Kernel/FileSystem/FIFO.cpp @@ -1,15 +1,43 @@ #include +#include #include +#include //#define FIFO_DEBUG -Retained FIFO::create() +Lockable>& all_fifos() { - return adopt(*new FIFO); + static Lockable>* s_table; + if (!s_table) + s_table = new Lockable>; + return *s_table; } -FIFO::FIFO() +RetainPtr FIFO::from_fifo_id(dword id) { + auto* ptr = reinterpret_cast(id); + LOCKER(all_fifos().lock()); + if (auto it = all_fifos().resource().find(ptr); it == all_fifos().resource().end()) + return nullptr; + return ptr; +} + +Retained FIFO::create(uid_t uid) +{ + return adopt(*new FIFO(uid)); +} + +FIFO::FIFO(uid_t uid) + : m_uid(uid) +{ + LOCKER(all_fifos().lock()); + all_fifos().resource().set(this); +} + +FIFO::~FIFO() +{ + LOCKER(all_fifos().lock()); + all_fifos().resource().remove(this); } void FIFO::open(Direction direction) diff --git a/Kernel/FileSystem/FIFO.h b/Kernel/FileSystem/FIFO.h index cef704a397..a16814a52b 100644 --- a/Kernel/FileSystem/FIFO.h +++ b/Kernel/FileSystem/FIFO.h @@ -11,7 +11,12 @@ public: Neither, Reader, Writer }; - static Retained create(); + static RetainPtr from_fifo_id(dword); + + static Retained create(uid_t); + ~FIFO(); + + uid_t uid() const { return m_uid; } void open(Direction); void close(Direction); @@ -23,9 +28,11 @@ public: bool can_write() const; private: - FIFO(); + explicit FIFO(uid_t); unsigned m_writers { 0 }; unsigned m_readers { 0 }; DoubleBuffer m_buffer; + + uid_t m_uid { 0 }; }; diff --git a/Kernel/FileSystem/FileDescriptor.cpp b/Kernel/FileSystem/FileDescriptor.cpp index 5b482affc8..b037e1ee03 100644 --- a/Kernel/FileSystem/FileDescriptor.cpp +++ b/Kernel/FileSystem/FileDescriptor.cpp @@ -402,7 +402,7 @@ KResultOr FileDescriptor::absolute_path() if (is_tty()) return tty()->tty_name(); if (is_fifo()) - return String::format("fifo:%x", m_fifo.ptr()); + return String::format("fifo:%u", m_fifo.ptr()); if (is_device()) return String::format("device:%u,%u (%s)", m_device->major(), m_device->minor(), m_device->class_name()); if (is_socket()) diff --git a/Kernel/Process.cpp b/Kernel/Process.cpp index bdfa930d99..5189fd41ca 100644 --- a/Kernel/Process.cpp +++ b/Kernel/Process.cpp @@ -276,7 +276,7 @@ int Process::do_exec(String path, Vector arguments, Vector envir { ASSERT(is_ring3()); - dbgprintf("%s(%d) do_exec: thread_count() = %d\n", m_name.characters(), m_pid, thread_count()); + dbgprintf("%s(%d) do_exec(%s): thread_count() = %d\n", m_name.characters(), m_pid, path.characters(), thread_count()); // FIXME(Thread): Kill any threads the moment we commit to the exec(). if (thread_count() != 1) { dbgprintf("Gonna die because I have many threads! These are the threads:\n"); @@ -1097,7 +1097,7 @@ int Process::sys$pipe(int pipefd[2]) return -EFAULT; if (number_of_open_file_descriptors() + 2 > max_open_file_descriptors()) return -EMFILE; - auto fifo = FIFO::create(); + auto fifo = FIFO::create(m_uid); int reader_fd = alloc_fd(); m_fds[reader_fd].set(FileDescriptor::create_pipe_reader(*fifo)); -- cgit v1.2.3