From 310a5f41997492d203fed6dd7b4ff77ce80f82ff Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Wed, 16 Jan 2019 02:11:50 +0100 Subject: Let each MasterPTY create its slave. --- Kernel/MasterPTY.cpp | 6 +++--- Kernel/MasterPTY.h | 4 ++-- Kernel/SlavePTY.cpp | 7 ++++--- Kernel/SlavePTY.h | 8 +++++--- Kernel/init.cpp | 23 ++++------------------- 5 files changed, 18 insertions(+), 30 deletions(-) (limited to 'Kernel') diff --git a/Kernel/MasterPTY.cpp b/Kernel/MasterPTY.cpp index 138d9bc5b0..7cb6f8b687 100644 --- a/Kernel/MasterPTY.cpp +++ b/Kernel/MasterPTY.cpp @@ -3,9 +3,10 @@ MasterPTY::MasterPTY(unsigned index) : CharacterDevice(10, index) + , m_slave(*new SlavePTY(*this, index)) , m_index(index) { - + VFS::the().register_character_device(m_slave); } MasterPTY::~MasterPTY() @@ -14,7 +15,6 @@ MasterPTY::~MasterPTY() String MasterPTY::pts_name() const { - dbgprintf("MasterPTY::pts_name requested for index %u!\n", m_index); char buffer[32]; ksprintf(buffer, "/dev/pts%u", m_index); return buffer; @@ -27,7 +27,7 @@ ssize_t MasterPTY::read(Process&, byte* buffer, size_t size) ssize_t MasterPTY::write(Process&, const byte* buffer, size_t size) { - m_slave->on_master_write(buffer, size); + m_slave.on_master_write(buffer, size); return size; } diff --git a/Kernel/MasterPTY.h b/Kernel/MasterPTY.h index 822764f57a..0837e250a4 100644 --- a/Kernel/MasterPTY.h +++ b/Kernel/MasterPTY.h @@ -6,10 +6,10 @@ class SlavePTY; class MasterPTY final : public CharacterDevice { + AK_MAKE_ETERNAL public: explicit MasterPTY(unsigned index); virtual ~MasterPTY() override; - void set_slave(SlavePTY& slave) { m_slave = &slave; } virtual ssize_t read(Process&, byte*, size_t) override; virtual ssize_t write(Process&, const byte*, size_t) override; @@ -21,7 +21,7 @@ public: void on_slave_write(const byte*, size_t); private: + SlavePTY& m_slave; unsigned m_index; - SlavePTY* m_slave { nullptr }; DoubleBuffer m_buffer; }; diff --git a/Kernel/SlavePTY.cpp b/Kernel/SlavePTY.cpp index 3c0898e28b..c8fa110746 100644 --- a/Kernel/SlavePTY.cpp +++ b/Kernel/SlavePTY.cpp @@ -1,8 +1,9 @@ #include "SlavePTY.h" #include "MasterPTY.h" -SlavePTY::SlavePTY(unsigned index) +SlavePTY::SlavePTY(MasterPTY& master, unsigned index) : TTY(11, index) + , m_master(master) , m_index(index) { set_size(80, 25); @@ -27,10 +28,10 @@ void SlavePTY::on_master_write(const byte* buffer, size_t size) void SlavePTY::on_tty_write(const byte* data, size_t size) { - m_master->on_slave_write(data, size); + m_master.on_slave_write(data, size); } bool SlavePTY::can_write(Process& process) const { - return m_master->can_write(process); + return m_master.can_write(process); } diff --git a/Kernel/SlavePTY.h b/Kernel/SlavePTY.h index 312efa740b..773dcee6b4 100644 --- a/Kernel/SlavePTY.h +++ b/Kernel/SlavePTY.h @@ -5,10 +5,9 @@ class MasterPTY; class SlavePTY final : public TTY { + AK_MAKE_ETERNAL public: - explicit SlavePTY(unsigned index); virtual ~SlavePTY() override; - void set_master(MasterPTY& master) { m_master = &master; } virtual String tty_name() const override; @@ -19,7 +18,10 @@ protected: virtual bool can_write(Process&) const override; private: + friend class MasterPTY; + SlavePTY(MasterPTY&, unsigned index); + + MasterPTY& m_master; unsigned m_index; - MasterPTY* m_master { nullptr }; }; diff --git a/Kernel/init.cpp b/Kernel/init.cpp index d7c42823f0..2348d53b88 100644 --- a/Kernel/init.cpp +++ b/Kernel/init.cpp @@ -23,7 +23,6 @@ #include "Scheduler.h" #include "PS2MouseDevice.h" #include "MasterPTY.h" -#include "SlavePTY.h" #define SPAWN_GUI_TEST_APP //#define SPAWN_MULTIPLE_SHELLS @@ -42,10 +41,6 @@ MasterPTY* ptm0; MasterPTY* ptm1; MasterPTY* ptm2; MasterPTY* ptm3; -SlavePTY* pts0; -SlavePTY* pts1; -SlavePTY* pts2; -SlavePTY* pts3; #ifdef STRESS_TEST_SPAWNING static void spawn_stress() NORETURN; @@ -66,16 +61,6 @@ static void spawn_stress() } #endif -static void make_pty_pair(unsigned index) -{ - auto* master = new MasterPTY(index); - auto* slave = new SlavePTY(index); - master->set_slave(*slave); - slave->set_master(*master); - VFS::the().register_character_device(*master); - VFS::the().register_character_device(*slave); -} - static void init_stage2() NORETURN; static void init_stage2() { @@ -95,10 +80,10 @@ static void init_stage2() auto dev_random = make(); vfs->register_character_device(*dev_random); - make_pty_pair(0); - make_pty_pair(1); - make_pty_pair(2); - make_pty_pair(3); + VFS::the().register_character_device(*new MasterPTY(0)); + VFS::the().register_character_device(*new MasterPTY(1)); + VFS::the().register_character_device(*new MasterPTY(2)); + VFS::the().register_character_device(*new MasterPTY(3)); vfs->register_character_device(*keyboard); vfs->register_character_device(*ps2mouse); -- cgit v1.2.3