diff options
author | Andreas Kling <awesomekling@gmail.com> | 2019-04-25 13:52:07 +0200 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2019-04-25 13:53:24 +0200 |
commit | d5578826afe5c974d63cf2ab6656e16260c3cc39 (patch) | |
tree | 1e92e46e9191e5c79a369e2df207aaf495489688 /Kernel | |
parent | eadcf720c085e2a51bc1c74b6fa59951cda08895 (diff) | |
download | serenity-d5578826afe5c974d63cf2ab6656e16260c3cc39.zip |
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.
Diffstat (limited to 'Kernel')
-rw-r--r-- | Kernel/FileSystem/FIFO.cpp | 34 | ||||
-rw-r--r-- | Kernel/FileSystem/FIFO.h | 11 | ||||
-rw-r--r-- | Kernel/FileSystem/FileDescriptor.cpp | 2 | ||||
-rw-r--r-- | Kernel/Process.cpp | 4 |
4 files changed, 43 insertions, 8 deletions
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 <Kernel/FileSystem/FIFO.h> +#include <Kernel/Lock.h> #include <AK/StdLibExtras.h> +#include <AK/HashTable.h> //#define FIFO_DEBUG -Retained<FIFO> FIFO::create() +Lockable<HashTable<FIFO*>>& all_fifos() { - return adopt(*new FIFO); + static Lockable<HashTable<FIFO*>>* s_table; + if (!s_table) + s_table = new Lockable<HashTable<FIFO*>>; + return *s_table; } -FIFO::FIFO() +RetainPtr<FIFO> FIFO::from_fifo_id(dword id) { + auto* ptr = reinterpret_cast<FIFO*>(id); + LOCKER(all_fifos().lock()); + if (auto it = all_fifos().resource().find(ptr); it == all_fifos().resource().end()) + return nullptr; + return ptr; +} + +Retained<FIFO> 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<FIFO> create(); + static RetainPtr<FIFO> from_fifo_id(dword); + + static Retained<FIFO> 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<String> 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<String> arguments, Vector<String> 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)); |