1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 08:48:11 +00:00

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.
This commit is contained in:
Andreas Kling 2019-04-25 13:52:07 +02:00
parent eadcf720c0
commit d5578826af
4 changed files with 43 additions and 8 deletions

View file

@ -1,15 +1,43 @@
#include <Kernel/FileSystem/FIFO.h> #include <Kernel/FileSystem/FIFO.h>
#include <Kernel/Lock.h>
#include <AK/StdLibExtras.h> #include <AK/StdLibExtras.h>
#include <AK/HashTable.h>
//#define FIFO_DEBUG //#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) void FIFO::open(Direction direction)

View file

@ -11,7 +11,12 @@ public:
Neither, Reader, Writer 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 open(Direction);
void close(Direction); void close(Direction);
@ -23,9 +28,11 @@ public:
bool can_write() const; bool can_write() const;
private: private:
FIFO(); explicit FIFO(uid_t);
unsigned m_writers { 0 }; unsigned m_writers { 0 };
unsigned m_readers { 0 }; unsigned m_readers { 0 };
DoubleBuffer m_buffer; DoubleBuffer m_buffer;
uid_t m_uid { 0 };
}; };

View file

@ -402,7 +402,7 @@ KResultOr<String> FileDescriptor::absolute_path()
if (is_tty()) if (is_tty())
return tty()->tty_name(); return tty()->tty_name();
if (is_fifo()) if (is_fifo())
return String::format("fifo:%x", m_fifo.ptr()); return String::format("fifo:%u", m_fifo.ptr());
if (is_device()) if (is_device())
return String::format("device:%u,%u (%s)", m_device->major(), m_device->minor(), m_device->class_name()); return String::format("device:%u,%u (%s)", m_device->major(), m_device->minor(), m_device->class_name());
if (is_socket()) if (is_socket())

View file

@ -276,7 +276,7 @@ int Process::do_exec(String path, Vector<String> arguments, Vector<String> envir
{ {
ASSERT(is_ring3()); 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(). // FIXME(Thread): Kill any threads the moment we commit to the exec().
if (thread_count() != 1) { if (thread_count() != 1) {
dbgprintf("Gonna die because I have many threads! These are the threads:\n"); 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; return -EFAULT;
if (number_of_open_file_descriptors() + 2 > max_open_file_descriptors()) if (number_of_open_file_descriptors() + 2 > max_open_file_descriptors())
return -EMFILE; return -EMFILE;
auto fifo = FIFO::create(); auto fifo = FIFO::create(m_uid);
int reader_fd = alloc_fd(); int reader_fd = alloc_fd();
m_fds[reader_fd].set(FileDescriptor::create_pipe_reader(*fifo)); m_fds[reader_fd].set(FileDescriptor::create_pipe_reader(*fifo));