diff options
-rw-r--r-- | Kernel/Devices/Device.cpp | 3 | ||||
-rw-r--r-- | Kernel/Devices/Device.h | 1 | ||||
-rw-r--r-- | Kernel/Devices/PS2MouseDevice.h | 1 | ||||
-rw-r--r-- | Kernel/File.h | 8 | ||||
-rw-r--r-- | Kernel/FileSystem/FIFO.cpp | 26 | ||||
-rw-r--r-- | Kernel/FileSystem/FIFO.h | 30 | ||||
-rw-r--r-- | Kernel/FileSystem/FileDescriptor.cpp | 58 | ||||
-rw-r--r-- | Kernel/FileSystem/FileDescriptor.h | 7 | ||||
-rw-r--r-- | Kernel/FileSystem/ProcFS.cpp | 1 | ||||
-rw-r--r-- | Kernel/KSyms.cpp | 1 | ||||
-rw-r--r-- | Kernel/Net/LocalSocket.cpp | 1 | ||||
-rw-r--r-- | Kernel/Process.cpp | 16 | ||||
-rw-r--r-- | Kernel/Process.h | 4 | ||||
-rw-r--r-- | Kernel/Scheduler.cpp | 1 | ||||
-rw-r--r-- | Kernel/TTY/PTYMultiplexer.cpp | 1 | ||||
-rw-r--r-- | Kernel/TTY/SlavePTY.h | 1 | ||||
-rw-r--r-- | Kernel/Thread.cpp | 6 | ||||
-rw-r--r-- | Kernel/Thread.h | 2 |
18 files changed, 96 insertions, 72 deletions
diff --git a/Kernel/Devices/Device.cpp b/Kernel/Devices/Device.cpp index 8319fc08c4..4fbf493ee5 100644 --- a/Kernel/Devices/Device.cpp +++ b/Kernel/Devices/Device.cpp @@ -1,4 +1,5 @@ -#include "CharacterDevice.h" +#include <Kernel/Devices/Device.h> +#include <Kernel/FileSystem/VirtualFileSystem.h> #include <LibC/errno_numbers.h> Device::Device(unsigned major, unsigned minor) diff --git a/Kernel/Devices/Device.h b/Kernel/Devices/Device.h index 14ca312234..cc8d70066e 100644 --- a/Kernel/Devices/Device.h +++ b/Kernel/Devices/Device.h @@ -34,6 +34,7 @@ // #include <Kernel/File.h> +#include <Kernel/UnixTypes.h> class Device : public File { public: diff --git a/Kernel/Devices/PS2MouseDevice.h b/Kernel/Devices/PS2MouseDevice.h index 2410a344f3..3ca6832797 100644 --- a/Kernel/Devices/PS2MouseDevice.h +++ b/Kernel/Devices/PS2MouseDevice.h @@ -3,6 +3,7 @@ #include <Kernel/Devices/CharacterDevice.h> #include <Kernel/MousePacket.h> #include <Kernel/IRQHandler.h> +#include <AK/CircularQueue.h> class PS2MouseDevice final : public IRQHandler, public CharacterDevice { public: diff --git a/Kernel/File.h b/Kernel/File.h index 0733d0a8a5..fcc0db246d 100644 --- a/Kernel/File.h +++ b/Kernel/File.h @@ -1,10 +1,15 @@ #pragma once +#include <AK/AKString.h> #include <AK/Retainable.h> +#include <AK/Retained.h> #include <AK/Types.h> -#include <Kernel/FileSystem/FileDescriptor.h> +#include <Kernel/KResult.h> +#include <Kernel/LinearAddress.h> +class FileDescriptor; class Process; +class Region; class File : public Retainable<File> { public: @@ -28,6 +33,7 @@ public: virtual bool is_seekable() const { return false; } virtual bool is_shared_memory() const { return false; } + virtual bool is_fifo() const { return false; } virtual bool is_device() const { return false; } virtual bool is_tty() const { return false; } virtual bool is_master_pty() const { return false; } diff --git a/Kernel/FileSystem/FIFO.cpp b/Kernel/FileSystem/FIFO.cpp index b229c75b76..1bc4a550ac 100644 --- a/Kernel/FileSystem/FIFO.cpp +++ b/Kernel/FileSystem/FIFO.cpp @@ -1,4 +1,5 @@ #include <Kernel/FileSystem/FIFO.h> +#include <Kernel/FileSystem/FileDescriptor.h> #include <Kernel/Lock.h> #include <AK/StdLibExtras.h> #include <AK/HashTable.h> @@ -27,6 +28,14 @@ Retained<FIFO> FIFO::create(uid_t uid) return adopt(*new FIFO(uid)); } +Retained<FileDescriptor> FIFO::open_direction(FIFO::Direction direction) +{ + auto descriptor = FileDescriptor::create(this); + attach(direction); + descriptor->set_fifo_direction({ }, direction); + return descriptor; +} + FIFO::FIFO(uid_t uid) : m_uid(uid) { @@ -40,7 +49,7 @@ FIFO::~FIFO() all_fifos().resource().remove(this); } -void FIFO::open(Direction direction) +void FIFO::attach(Direction direction) { if (direction == Reader) { ++m_readers; @@ -55,7 +64,7 @@ void FIFO::open(Direction direction) } } -void FIFO::close(Direction direction) +void FIFO::detach(Direction direction) { if (direction == Reader) { #ifdef FIFO_DEBUG @@ -72,17 +81,17 @@ void FIFO::close(Direction direction) } } -bool FIFO::can_read() const +bool FIFO::can_read(Process&) const { return !m_buffer.is_empty() || !m_writers; } -bool FIFO::can_write() const +bool FIFO::can_write(Process&) const { return m_buffer.bytes_in_write_buffer() < 4096; } -ssize_t FIFO::read(byte* buffer, ssize_t size) +ssize_t FIFO::read(Process&, byte* buffer, ssize_t size) { if (!m_writers && m_buffer.is_empty()) return 0; @@ -96,7 +105,7 @@ ssize_t FIFO::read(byte* buffer, ssize_t size) return nread; } -ssize_t FIFO::write(const byte* buffer, ssize_t size) +ssize_t FIFO::write(Process&, const byte* buffer, ssize_t size) { if (!m_readers) return 0; @@ -105,3 +114,8 @@ ssize_t FIFO::write(const byte* buffer, ssize_t size) #endif return m_buffer.write(buffer, size); } + +String FIFO::absolute_path() const +{ + return String::format("fifo:%u", this); +} diff --git a/Kernel/FileSystem/FIFO.h b/Kernel/FileSystem/FIFO.h index a16814a52b..4e72032c28 100644 --- a/Kernel/FileSystem/FIFO.h +++ b/Kernel/FileSystem/FIFO.h @@ -1,11 +1,12 @@ #pragma once -#include "DoubleBuffer.h" -#include <AK/Retainable.h> -#include <AK/RetainPtr.h> +#include <Kernel/DoubleBuffer.h> #include <Kernel/UnixTypes.h> +#include <Kernel/File.h> -class FIFO : public Retainable<FIFO> { +class FileDescriptor; + +class FIFO final : public File { public: enum Direction { Neither, Reader, Writer @@ -14,20 +15,25 @@ public: static RetainPtr<FIFO> from_fifo_id(dword); static Retained<FIFO> create(uid_t); - ~FIFO(); + virtual ~FIFO() override; uid_t uid() const { return m_uid; } - void open(Direction); - void close(Direction); - - ssize_t write(const byte*, ssize_t); - ssize_t read(byte*, ssize_t); + Retained<FileDescriptor> open_direction(Direction); - bool can_read() const; - bool can_write() const; + void attach(Direction); + void detach(Direction); private: + // ^File + virtual ssize_t write(Process&, const byte*, ssize_t) override; + virtual ssize_t read(Process&, byte*, ssize_t) override; + virtual bool can_read(Process&) const override; + virtual bool can_write(Process&) const override; + virtual String absolute_path() const override; + virtual const char* class_name() const override { return "FIFO"; } + virtual bool is_fifo() const override { return true; } + explicit FIFO(uid_t); unsigned m_writers { 0 }; diff --git a/Kernel/FileSystem/FileDescriptor.cpp b/Kernel/FileSystem/FileDescriptor.cpp index 91f59b8ffc..b185ed5585 100644 --- a/Kernel/FileSystem/FileDescriptor.cpp +++ b/Kernel/FileSystem/FileDescriptor.cpp @@ -28,16 +28,6 @@ Retained<FileDescriptor> FileDescriptor::create(RetainPtr<Socket>&& socket, Sock return adopt(*new FileDescriptor(move(socket), role)); } -Retained<FileDescriptor> FileDescriptor::create_pipe_writer(FIFO& fifo) -{ - return adopt(*new FileDescriptor(fifo, FIFO::Writer)); -} - -Retained<FileDescriptor> FileDescriptor::create_pipe_reader(FIFO& fifo) -{ - return adopt(*new FileDescriptor(fifo, FIFO::Reader)); -} - FileDescriptor::FileDescriptor(RetainPtr<Inode>&& inode) : m_inode(move(inode)) { @@ -60,14 +50,12 @@ FileDescriptor::~FileDescriptor() m_socket->detach_fd(m_socket_role); m_socket = nullptr; } + if (is_fifo()) + static_cast<FIFO*>(m_file.ptr())->detach(m_fifo_direction); if (m_file) { m_file->close(); m_file = nullptr; } - if (m_fifo) { - m_fifo->close(fifo_direction()); - m_fifo = nullptr; - } m_inode = nullptr; } @@ -88,9 +76,7 @@ Retained<FileDescriptor> FileDescriptor::clone() { RetainPtr<FileDescriptor> descriptor; if (is_fifo()) { - descriptor = fifo_direction() == FIFO::Reader - ? FileDescriptor::create_pipe_reader(*m_fifo) - : FileDescriptor::create_pipe_writer(*m_fifo); + descriptor = fifo()->open_direction(m_fifo_direction); } else { if (m_file) { descriptor = FileDescriptor::create(m_file.copy_ref()); @@ -189,10 +175,6 @@ off_t FileDescriptor::seek(off_t offset, int whence) ssize_t FileDescriptor::read(Process& process, byte* buffer, ssize_t count) { - if (is_fifo()) { - ASSERT(fifo_direction() == FIFO::Reader); - return m_fifo->read(buffer, count); - } if (m_file) { int nread = m_file->read(process, buffer, count); if (!m_file->is_seekable()) @@ -209,10 +191,6 @@ ssize_t FileDescriptor::read(Process& process, byte* buffer, ssize_t count) ssize_t FileDescriptor::write(Process& process, const byte* data, ssize_t size) { - if (is_fifo()) { - ASSERT(fifo_direction() == FIFO::Writer); - return m_fifo->write(data, size); - } if (m_file) { int nwritten = m_file->write(process, data, size); if (m_file->is_seekable()) @@ -229,10 +207,6 @@ ssize_t FileDescriptor::write(Process& process, const byte* data, ssize_t size) bool FileDescriptor::can_write(Process& process) { - if (is_fifo()) { - ASSERT(fifo_direction() == FIFO::Writer); - return m_fifo->can_write(); - } if (m_file) return m_file->can_write(process); if (m_socket) @@ -242,10 +216,6 @@ bool FileDescriptor::can_write(Process& process) bool FileDescriptor::can_read(Process& process) { - if (is_fifo()) { - ASSERT(fifo_direction() == FIFO::Reader); - return m_fifo->can_read(); - } if (m_file) return m_file->can_read(process); if (m_socket) @@ -374,8 +344,6 @@ bool FileDescriptor::is_fsfile() const KResultOr<String> FileDescriptor::absolute_path() { - if (is_fifo()) - return String::format("fifo:%u", m_fifo.ptr()); if (m_file) return m_file->absolute_path(); if (is_socket()) @@ -384,14 +352,6 @@ KResultOr<String> FileDescriptor::absolute_path() return VFS::the().absolute_path(*m_inode); } -FileDescriptor::FileDescriptor(FIFO& fifo, FIFO::Direction direction) - : m_is_blocking(true) - , m_fifo(fifo) - , m_fifo_direction(direction) -{ - m_fifo->open(direction); -} - InodeMetadata FileDescriptor::metadata() const { if (m_inode) @@ -457,3 +417,15 @@ const SharedMemory* FileDescriptor::shared_memory() const return nullptr; return static_cast<const SharedMemory*>(m_file.ptr()); } + +bool FileDescriptor::is_fifo() const +{ + return m_file && m_file->is_fifo(); +} + +FIFO* FileDescriptor::fifo() +{ + if (!is_fifo()) + return nullptr; + return static_cast<FIFO*>(m_file.ptr()); +} diff --git a/Kernel/FileSystem/FileDescriptor.h b/Kernel/FileSystem/FileDescriptor.h index d99eecf1f6..33b4ee1062 100644 --- a/Kernel/FileSystem/FileDescriptor.h +++ b/Kernel/FileSystem/FileDescriptor.h @@ -23,8 +23,6 @@ public: static Retained<FileDescriptor> create(RetainPtr<Socket>&&, SocketRole = SocketRole::None); static Retained<FileDescriptor> create(RetainPtr<Inode>&&); static Retained<FileDescriptor> create(RetainPtr<File>&&); - static Retained<FileDescriptor> create_pipe_writer(FIFO&); - static Retained<FileDescriptor> create_pipe_reader(FIFO&); ~FileDescriptor(); Retained<FileDescriptor> clone(); @@ -80,8 +78,10 @@ public: Socket* socket() { return m_socket.ptr(); } const Socket* socket() const { return m_socket.ptr(); } - bool is_fifo() const { return m_fifo; } + bool is_fifo() const; + FIFO* fifo(); FIFO::Direction fifo_direction() { return m_fifo_direction; } + void set_fifo_direction(Badge<FIFO>, FIFO::Direction direction) { m_fifo_direction = direction; } bool is_fsfile() const; bool is_shared_memory() const; @@ -117,7 +117,6 @@ private: RetainPtr<Socket> m_socket; SocketRole m_socket_role { SocketRole::None }; - RetainPtr<FIFO> m_fifo; FIFO::Direction m_fifo_direction { FIFO::Neither }; bool m_closed { false }; diff --git a/Kernel/FileSystem/ProcFS.cpp b/Kernel/FileSystem/ProcFS.cpp index f401fe2e7a..5ca1d4dc8a 100644 --- a/Kernel/FileSystem/ProcFS.cpp +++ b/Kernel/FileSystem/ProcFS.cpp @@ -1,5 +1,6 @@ #include "ProcFS.h" #include "Process.h" +#include <Kernel/FileSystem/FileDescriptor.h> #include <Kernel/FileSystem/VirtualFileSystem.h> #include <Kernel/VM/MemoryManager.h> #include "StdLib.h" diff --git a/Kernel/KSyms.cpp b/Kernel/KSyms.cpp index b397aa7adf..e07c86089b 100644 --- a/Kernel/KSyms.cpp +++ b/Kernel/KSyms.cpp @@ -1,6 +1,7 @@ #include "KSyms.h" #include "Process.h" #include "Scheduler.h" +#include <Kernel/FileSystem/FileDescriptor.h> static KSym* s_ksyms; dword ksym_lowest_address; diff --git a/Kernel/Net/LocalSocket.cpp b/Kernel/Net/LocalSocket.cpp index 2754644ff5..5be29e9c24 100644 --- a/Kernel/Net/LocalSocket.cpp +++ b/Kernel/Net/LocalSocket.cpp @@ -1,6 +1,7 @@ #include <Kernel/Net/LocalSocket.h> #include <Kernel/UnixTypes.h> #include <Kernel/Process.h> +#include <Kernel/FileSystem/FileDescriptor.h> #include <Kernel/FileSystem/VirtualFileSystem.h> #include <LibC/errno_numbers.h> diff --git a/Kernel/Process.cpp b/Kernel/Process.cpp index c9e11927e9..7810a39e53 100644 --- a/Kernel/Process.cpp +++ b/Kernel/Process.cpp @@ -1105,11 +1105,11 @@ int Process::sys$pipe(int pipefd[2]) auto fifo = FIFO::create(m_uid); int reader_fd = alloc_fd(); - m_fds[reader_fd].set(FileDescriptor::create_pipe_reader(*fifo)); + m_fds[reader_fd].set(fifo->open_direction(FIFO::Reader)); pipefd[0] = reader_fd; int writer_fd = alloc_fd(); - m_fds[writer_fd].set(FileDescriptor::create_pipe_writer(*fifo)); + m_fds[writer_fd].set(fifo->open_direction(FIFO::Writer)); pipefd[1] = writer_fd; return 0; @@ -2531,3 +2531,15 @@ ProcessTracer& Process::ensure_tracer() m_tracer = ProcessTracer::create(m_pid); return *m_tracer; } + +void Process::FileDescriptorAndFlags::clear() +{ + descriptor = nullptr; + flags = 0; +} + +void Process::FileDescriptorAndFlags::set(Retained<FileDescriptor>&& d, dword f) +{ + descriptor = move(d); + flags = f; +} diff --git a/Kernel/Process.h b/Kernel/Process.h index 9ade3ef64b..7f2f25f63b 100644 --- a/Kernel/Process.h +++ b/Kernel/Process.h @@ -281,8 +281,8 @@ private: struct FileDescriptorAndFlags { operator bool() const { return !!descriptor; } - void clear() { descriptor = nullptr; flags = 0; } - void set(Retained<FileDescriptor>&& d, dword f = 0) { descriptor = move(d); flags = f; } + void clear(); + void set(Retained<FileDescriptor>&& d, dword f = 0); RetainPtr<FileDescriptor> descriptor; dword flags { 0 }; }; diff --git a/Kernel/Scheduler.cpp b/Kernel/Scheduler.cpp index 9b545bdcee..14507c4bfb 100644 --- a/Kernel/Scheduler.cpp +++ b/Kernel/Scheduler.cpp @@ -4,6 +4,7 @@ #include "i8253.h" #include <AK/TemporaryChange.h> #include <Kernel/Alarm.h> +#include <Kernel/FileSystem/FileDescriptor.h> //#define LOG_EVERY_CONTEXT_SWITCH //#define SCHEDULER_DEBUG diff --git a/Kernel/TTY/PTYMultiplexer.cpp b/Kernel/TTY/PTYMultiplexer.cpp index 9d519d74e5..1ce67f10ed 100644 --- a/Kernel/TTY/PTYMultiplexer.cpp +++ b/Kernel/TTY/PTYMultiplexer.cpp @@ -1,5 +1,6 @@ #include "PTYMultiplexer.h" #include "MasterPTY.h" +#include <Kernel/FileSystem/FileDescriptor.h> #include <Kernel/Process.h> #include <LibC/errno_numbers.h> diff --git a/Kernel/TTY/SlavePTY.h b/Kernel/TTY/SlavePTY.h index 36ec094ce6..56ecb33c61 100644 --- a/Kernel/TTY/SlavePTY.h +++ b/Kernel/TTY/SlavePTY.h @@ -1,5 +1,6 @@ #pragma once +#include <Kernel/FileSystem/InodeIdentifier.h> #include <Kernel/TTY/TTY.h> class MasterPTY; diff --git a/Kernel/Thread.cpp b/Kernel/Thread.cpp index 6919749135..ec8fe247d4 100644 --- a/Kernel/Thread.cpp +++ b/Kernel/Thread.cpp @@ -1,6 +1,7 @@ #include <Kernel/Thread.h> #include <Kernel/Scheduler.h> #include <Kernel/Process.h> +#include <Kernel/Net/Socket.h> #include <Kernel/VM/MemoryManager.h> #include <LibC/signal_numbers.h> @@ -532,3 +533,8 @@ bool Thread::is_thread(void* ptr) } return false; } + +void Thread::set_blocked_socket(Socket* socket) +{ + m_blocked_socket = socket; +} diff --git a/Kernel/Thread.h b/Kernel/Thread.h index 641f1443d5..5c9780de75 100644 --- a/Kernel/Thread.h +++ b/Kernel/Thread.h @@ -116,7 +116,7 @@ public: bool has_used_fpu() const { return m_has_used_fpu; } void set_has_used_fpu(bool b) { m_has_used_fpu = b; } - void set_blocked_socket(Socket* socket) { m_blocked_socket = socket; } + void set_blocked_socket(Socket*); void set_default_signal_dispositions(); void push_value_on_stack(dword); |