Kernel: Make FIFO inherit from File.
This commit is contained in:
parent
f254a84d17
commit
0a0d739e98
Notes:
sideshowbarker
2024-07-19 14:33:20 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/0a0d739e983
18 changed files with 96 additions and 72 deletions
|
@ -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)
|
||||
|
|
|
@ -34,6 +34,7 @@
|
|||
//
|
||||
|
||||
#include <Kernel/File.h>
|
||||
#include <Kernel/UnixTypes.h>
|
||||
|
||||
class Device : public File {
|
||||
public:
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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; }
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
Retained<FileDescriptor> open_direction(Direction);
|
||||
|
||||
ssize_t write(const byte*, ssize_t);
|
||||
ssize_t read(byte*, ssize_t);
|
||||
|
||||
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 };
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
|
|
|
@ -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 };
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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>
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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 };
|
||||
};
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
#include "PTYMultiplexer.h"
|
||||
#include "MasterPTY.h"
|
||||
#include <Kernel/FileSystem/FileDescriptor.h>
|
||||
#include <Kernel/Process.h>
|
||||
#include <LibC/errno_numbers.h>
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
#pragma once
|
||||
|
||||
#include <Kernel/FileSystem/InodeIdentifier.h>
|
||||
#include <Kernel/TTY/TTY.h>
|
||||
|
||||
class MasterPTY;
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Add table
Reference in a new issue