Pass the process to CharacterDevice::read/write.

This is much nicer than grabbing directly at 'current' inside a read().
This commit is contained in:
Andreas Kling 2019-01-16 00:20:38 +01:00
parent 08bfe518f9
commit bd3e77cc16
Notes: sideshowbarker 2024-07-19 16:01:51 +09:00
27 changed files with 63 additions and 62 deletions

View file

@ -28,14 +28,14 @@ bool Console::can_read(Process&) const
return false;
}
ssize_t Console::read(byte*, size_t)
ssize_t Console::read(Process&, byte*, size_t)
{
// FIXME: Implement reading from the console.
// Maybe we could use a ring buffer for this device?
return 0;
}
ssize_t Console::write(const byte* data, size_t size)
ssize_t Console::write(Process&, const byte* data, size_t size)
{
if (!size)
return 0;

View file

@ -20,8 +20,8 @@ public:
virtual bool can_read(Process&) const override;
virtual bool can_write(Process&) const override { return true; }
virtual ssize_t read(byte* buffer, size_t size) override;
virtual ssize_t write(const byte* data, size_t size) override;
virtual ssize_t read(Process&, byte* buffer, size_t size) override;
virtual ssize_t write(Process&, const byte* data, size_t size) override;
void setImplementation(ConsoleImplementation* implementation) { m_implementation = implementation; }

View file

@ -128,7 +128,7 @@ void load_ksyms()
if (!descriptor) {
kprintf("Failed to open /kernel.map\n");
} else {
auto buffer = descriptor->read_entire_file();
auto buffer = descriptor->read_entire_file(*current);
ASSERT(buffer);
load_ksyms_from_data(buffer);
}

View file

@ -119,7 +119,7 @@ bool Keyboard::can_read(Process&) const
return !m_queue.is_empty();
}
ssize_t Keyboard::read(byte* buffer, size_t size)
ssize_t Keyboard::read(Process&, byte* buffer, size_t size)
{
ssize_t nread = 0;
while ((size_t)nread < size) {
@ -130,7 +130,7 @@ ssize_t Keyboard::read(byte* buffer, size_t size)
return nread;
}
ssize_t Keyboard::write(const byte*, size_t)
ssize_t Keyboard::write(Process&, const byte*, size_t)
{
return 0;
}

View file

@ -37,8 +37,8 @@ private:
virtual void handle_irq() override;
// ^CharacterDevice
virtual ssize_t read(byte* buffer, size_t) override;
virtual ssize_t write(const byte* buffer, size_t) override;
virtual ssize_t read(Process&, byte* buffer, size_t) override;
virtual ssize_t write(Process&, const byte* buffer, size_t) override;
virtual bool can_read(Process&) const override;
virtual bool can_write(Process&) const override { return true; }

View file

@ -20,12 +20,12 @@ String MasterPTY::pts_name() const
return buffer;
}
ssize_t MasterPTY::read(byte* buffer, size_t size)
ssize_t MasterPTY::read(Process&, byte* buffer, size_t size)
{
return m_buffer.read(buffer, size);
}
ssize_t MasterPTY::write(const byte* buffer, size_t size)
ssize_t MasterPTY::write(Process&, const byte* buffer, size_t size)
{
m_slave->on_master_write(buffer, size);
return size;

View file

@ -11,8 +11,8 @@ public:
virtual ~MasterPTY() override;
void set_slave(SlavePTY& slave) { m_slave = &slave; }
virtual ssize_t read(byte*, size_t) override;
virtual ssize_t write(const byte*, size_t) override;
virtual ssize_t read(Process&, byte*, size_t) override;
virtual ssize_t write(Process&, const byte*, size_t) override;
virtual bool can_read(Process&) const override;
virtual bool can_write(Process&) const override;
virtual bool is_master_pty() const override { return true; }

View file

@ -121,12 +121,12 @@ bool PS2MouseDevice::can_read(Process&) const
return !m_buffer.is_empty();
}
ssize_t PS2MouseDevice::read(byte* buffer, size_t size)
ssize_t PS2MouseDevice::read(Process&, byte* buffer, size_t size)
{
return m_buffer.read(buffer, size);
}
ssize_t PS2MouseDevice::write(const byte*, size_t)
ssize_t PS2MouseDevice::write(Process&, const byte*, size_t)
{
return 0;
}

View file

@ -13,8 +13,8 @@ public:
// ^CharacterDevice
virtual bool can_read(Process&) const override;
virtual ssize_t read(byte* buffer, size_t) override;
virtual ssize_t write(const byte* buffer, size_t) override;
virtual ssize_t read(Process&, byte* buffer, size_t) override;
virtual ssize_t write(Process&, const byte* buffer, size_t) override;
virtual bool can_write(Process&) const override { return true; }
private:

View file

@ -1031,7 +1031,7 @@ ssize_t Process::sys$write(int fd, const void* data, size_t size)
block(BlockedWrite);
Scheduler::yield();
}
ssize_t rc = descriptor->write((const byte*)data + nwritten, size - nwritten);
ssize_t rc = descriptor->write(*this, (const byte*)data + nwritten, size - nwritten);
#ifdef IO_DEBUG
dbgprintf(" -> write returned %d\n", rc);
#endif
@ -1051,7 +1051,7 @@ ssize_t Process::sys$write(int fd, const void* data, size_t size)
nwritten += rc;
}
} else {
nwritten = descriptor->write((const byte*)data, size);
nwritten = descriptor->write(*this, (const byte*)data, size);
}
if (has_unmasked_pending_signals()) {
block(BlockedSignal);
@ -1084,7 +1084,7 @@ ssize_t Process::sys$read(int fd, void* outbuf, size_t nread)
return -EINTR;
}
}
nread = descriptor->read((byte*)outbuf, nread);
nread = descriptor->read(*this, (byte*)outbuf, nread);
#ifdef DEBUG_IO
dbgprintf("%s(%u) Process::sys$read: nread=%u\n", name().characters(), pid(), nread);
#endif
@ -1232,7 +1232,7 @@ int Process::sys$readlink(const char* path, char* buffer, size_t size)
if (!descriptor->metadata().isSymbolicLink())
return -EINVAL;
auto contents = descriptor->read_entire_file();
auto contents = descriptor->read_entire_file(*this);
if (!contents)
return -EIO; // FIXME: Get a more detailed error from VFS.

View file

@ -24,12 +24,12 @@ void TTY::set_default_termios()
memcpy(m_termios.c_cc, default_cc, sizeof(default_cc));
}
ssize_t TTY::read(byte* buffer, size_t size)
ssize_t TTY::read(Process&, byte* buffer, size_t size)
{
return m_buffer.read(buffer, size);
}
ssize_t TTY::write(const byte* buffer, size_t size)
ssize_t TTY::write(Process&, const byte* buffer, size_t size)
{
#ifdef TTY_DEBUG
dbgprintf("TTY::write {%u} ", size);

View file

@ -10,8 +10,8 @@ class TTY : public CharacterDevice {
public:
virtual ~TTY() override;
virtual ssize_t read(byte*, size_t) override;
virtual ssize_t write(const byte*, size_t) override;
virtual ssize_t read(Process&, byte*, size_t) override;
virtual ssize_t write(Process&, const byte*, size_t) override;
virtual bool can_read(Process&) const override;
virtual bool can_write(Process&) const override;
virtual int ioctl(Process&, unsigned request, unsigned arg) override final;

View file

@ -2,12 +2,13 @@
#include "Console.h"
#include "IO.h"
#include <stdarg.h>
#include "Process.h"
#include <AK/Types.h>
#include <AK/printf.cpp>
static void console_putch(char*&, char ch)
{
Console::the().write((byte*)&ch, 1);
Console::the().write(*current, (byte*)&ch, 1);
}
int kprintf(const char* fmt, ...)

View file

@ -15,8 +15,8 @@ public:
virtual bool can_read(Process&) const = 0;
virtual bool can_write(Process&) const = 0;
virtual ssize_t read(byte* buffer, size_t bufferSize) = 0;
virtual ssize_t write(const byte* buffer, size_t bufferSize) = 0;
virtual ssize_t read(Process&, byte* buffer, size_t bufferSize) = 0;
virtual ssize_t write(Process&, const byte* buffer, size_t bufferSize) = 0;
unsigned major() const { return m_major; }
unsigned minor() const { return m_minor; }

View file

@ -134,7 +134,7 @@ Unix::off_t FileDescriptor::seek(Unix::off_t offset, int whence)
return m_current_offset;
}
ssize_t FileDescriptor::read(byte* buffer, size_t count)
ssize_t FileDescriptor::read(Process& process, byte* buffer, size_t count)
{
if (is_fifo()) {
ASSERT(fifo_direction() == FIFO::Reader);
@ -142,7 +142,7 @@ ssize_t FileDescriptor::read(byte* buffer, size_t count)
}
if (m_vnode->isCharacterDevice()) {
// FIXME: What should happen to m_currentOffset?
return m_vnode->characterDevice()->read(buffer, count);
return m_vnode->characterDevice()->read(process, buffer, count);
}
ASSERT(inode());
ssize_t nread = inode()->read_bytes(m_current_offset, count, buffer, this);
@ -150,7 +150,7 @@ ssize_t FileDescriptor::read(byte* buffer, size_t count)
return nread;
}
ssize_t FileDescriptor::write(const byte* data, size_t size)
ssize_t FileDescriptor::write(Process& process, const byte* data, size_t size)
{
if (is_fifo()) {
ASSERT(fifo_direction() == FIFO::Writer);
@ -158,7 +158,7 @@ ssize_t FileDescriptor::write(const byte* data, size_t size)
}
if (m_vnode->isCharacterDevice()) {
// FIXME: What should happen to m_currentOffset?
return m_vnode->characterDevice()->write(data, size);
return m_vnode->characterDevice()->write(process, data, size);
}
// FIXME: Implement non-device writes.
ASSERT_NOT_REACHED();
@ -187,13 +187,13 @@ bool FileDescriptor::has_data_available_for_reading(Process& process)
return true;
}
ByteBuffer FileDescriptor::read_entire_file()
ByteBuffer FileDescriptor::read_entire_file(Process& process)
{
ASSERT(!is_fifo());
if (m_vnode->isCharacterDevice()) {
auto buffer = ByteBuffer::create_uninitialized(1024);
ssize_t nread = m_vnode->characterDevice()->read(buffer.pointer(), buffer.size());
ssize_t nread = m_vnode->characterDevice()->read(process, buffer.pointer(), buffer.size());
buffer.trim(nread);
return buffer;
}

View file

@ -25,8 +25,8 @@ public:
int close();
Unix::off_t seek(Unix::off_t, int whence);
ssize_t read(byte*, size_t);
ssize_t write(const byte* data, size_t);
ssize_t read(Process&, byte*, size_t);
ssize_t write(Process&, const byte* data, size_t);
int stat(Unix::stat*);
bool has_data_available_for_reading(Process&);
@ -34,7 +34,7 @@ public:
ssize_t get_dir_entries(byte* buffer, size_t);
ByteBuffer read_entire_file();
ByteBuffer read_entire_file(Process&);
String absolute_path();

View file

@ -18,14 +18,14 @@ bool FullDevice::can_read(Process&) const
return true;
}
ssize_t FullDevice::read(byte* buffer, size_t bufferSize)
ssize_t FullDevice::read(Process&, byte* buffer, size_t bufferSize)
{
size_t count = min(GoodBufferSize, bufferSize);
memset(buffer, 0, count);
return count;
}
ssize_t FullDevice::write(const byte*, size_t bufferSize)
ssize_t FullDevice::write(Process&, const byte*, size_t bufferSize)
{
if (bufferSize == 0)
return 0;

View file

@ -8,8 +8,8 @@ public:
FullDevice();
virtual ~FullDevice() override;
virtual ssize_t read(byte* buffer, size_t bufferSize) override;
virtual ssize_t write(const byte* buffer, size_t bufferSize) override;
virtual ssize_t read(Process&, byte* buffer, size_t bufferSize) override;
virtual ssize_t write(Process&, const byte* buffer, size_t bufferSize) override;
virtual bool can_read(Process&) const override;
virtual bool can_write(Process&) const override { return true; }
};

View file

@ -17,12 +17,12 @@ bool NullDevice::can_read(Process&) const
return true;
}
ssize_t NullDevice::read(byte*, size_t)
ssize_t NullDevice::read(Process&, byte*, size_t)
{
return 0;
}
ssize_t NullDevice::write(const byte*, size_t bufferSize)
ssize_t NullDevice::write(Process&, const byte*, size_t bufferSize)
{
return min(GoodBufferSize, bufferSize);
}

View file

@ -8,8 +8,8 @@ public:
NullDevice();
virtual ~NullDevice() override;
virtual ssize_t read(byte* buffer, size_t bufferSize) override;
virtual ssize_t write(const byte* buffer, size_t bufferSize) override;
virtual ssize_t read(Process&, byte* buffer, size_t bufferSize) override;
virtual ssize_t write(Process&, const byte* buffer, size_t bufferSize) override;
virtual bool can_write(Process&) const override { return true; }
virtual bool can_read(Process&) const override;
};

View file

@ -34,7 +34,7 @@ bool RandomDevice::can_read(Process&) const
return true;
}
ssize_t RandomDevice::read(byte* buffer, size_t bufferSize)
ssize_t RandomDevice::read(Process&, byte* buffer, size_t bufferSize)
{
const int range = 'z' - 'a';
ssize_t nread = min(bufferSize, GoodBufferSize);
@ -45,7 +45,7 @@ ssize_t RandomDevice::read(byte* buffer, size_t bufferSize)
return nread;
}
ssize_t RandomDevice::write(const byte*, size_t bufferSize)
ssize_t RandomDevice::write(Process&, const byte*, size_t bufferSize)
{
// FIXME: Use input for entropy? I guess that could be a neat feature?
return min(GoodBufferSize, bufferSize);

View file

@ -8,8 +8,8 @@ public:
RandomDevice();
virtual ~RandomDevice() override;
virtual ssize_t read(byte* buffer, size_t bufferSize) override;
virtual ssize_t write(const byte* buffer, size_t bufferSize) override;
virtual ssize_t read(Process&, byte* buffer, size_t bufferSize) override;
virtual ssize_t write(Process&, const byte* buffer, size_t bufferSize) override;
virtual bool can_read(Process&) const override;
virtual bool can_write(Process&) const override { return true; }
};

View file

@ -17,14 +17,14 @@ bool ZeroDevice::can_read(Process&) const
return true;
}
ssize_t ZeroDevice::read(byte* buffer, size_t bufferSize)
ssize_t ZeroDevice::read(Process&, byte* buffer, size_t bufferSize)
{
size_t count = min(GoodBufferSize, bufferSize);
memset(buffer, 0, count);
return count;
}
ssize_t ZeroDevice::write(const byte*, size_t bufferSize)
ssize_t ZeroDevice::write(Process&, const byte*, size_t bufferSize)
{
return min(GoodBufferSize, bufferSize);
}

View file

@ -8,8 +8,8 @@ public:
ZeroDevice();
virtual ~ZeroDevice() override;
virtual ssize_t read(byte* buffer, size_t bufferSize) override;
virtual ssize_t write(const byte* buffer, size_t bufferSize) override;
virtual ssize_t read(Process&, byte* buffer, size_t bufferSize) override;
virtual ssize_t write(Process&, const byte* buffer, size_t bufferSize) override;
virtual bool can_read(Process&) const override;
virtual bool can_write(Process&) const override { return true; }
};

View file

@ -78,7 +78,7 @@ void EventLoop::waitForEvent()
int dy = 0;
while (mouse.can_read(*m_server_process)) {
signed_byte data[3];
ssize_t nread = mouse.read((byte*)data, 3);
ssize_t nread = mouse.read(*m_server_process, (byte*)data, 3);
ASSERT(nread == 3);
bool left_button = data[0] & 1;
bool right_button = data[0] & 2;

View file

@ -19,20 +19,20 @@ bool GUIEventDevice::can_read(Process& process) const
return !process.gui_events().is_empty();
}
ssize_t GUIEventDevice::read(byte* buffer, size_t size)
ssize_t GUIEventDevice::read(Process& process, byte* buffer, size_t size)
{
#ifdef GUIEVENTDEVICE_DEBUG
dbgprintf("GUIEventDevice::read(): %s<%u>, size=%u, sizeof(GUI_Event)=%u\n", current->name().characters(), current->pid(), size, sizeof(GUI_Event));
dbgprintf("GUIEventDevice::read(): %s<%u>, size=%u, sizeof(GUI_Event)=%u\n", process.name().characters(), process.pid(), size, sizeof(GUI_Event));
#endif
if (current->gui_events().is_empty())
if (process.gui_events().is_empty())
return 0;
LOCKER(current->gui_events_lock());
LOCKER(process.gui_events_lock());
ASSERT(size == sizeof(GUI_Event));
*reinterpret_cast<GUI_Event*>(buffer) = current->gui_events().take_first();
*reinterpret_cast<GUI_Event*>(buffer) = process.gui_events().take_first();
return size;
}
ssize_t GUIEventDevice::write(const byte*, size_t)
ssize_t GUIEventDevice::write(Process&, const byte*, size_t)
{
return -EINVAL;
}

View file

@ -9,8 +9,8 @@ public:
private:
// ^CharacterDevice
virtual ssize_t read(byte* buffer, size_t bufferSize) override;
virtual ssize_t write(const byte* buffer, size_t bufferSize) override;
virtual ssize_t read(Process&, byte* buffer, size_t bufferSize) override;
virtual ssize_t write(Process&, const byte* buffer, size_t bufferSize) override;
virtual bool can_read(Process&) const override;
virtual bool can_write(Process&) const override { return true; }
};