From ba7e1b9ca59079187bfb3e7da96f953d86b2429a Mon Sep 17 00:00:00 2001 From: Bananymous Date: Tue, 5 Sep 2023 00:59:29 +0300 Subject: [PATCH] Kernel: Add /dev/tty symlink that targets the current tty --- kernel/include/kernel/FS/DevFS/FileSystem.h | 2 +- kernel/include/kernel/Terminal/Serial.h | 4 ++++ kernel/include/kernel/Terminal/TTY.h | 2 ++ kernel/include/kernel/Terminal/VirtualTTY.h | 5 +++++ kernel/kernel/FS/DevFS/FileSystem.cpp | 2 +- kernel/kernel/Terminal/Serial.cpp | 9 ++++----- kernel/kernel/Terminal/TTY.cpp | 14 ++++++++++++++ kernel/kernel/Terminal/VirtualTTY.cpp | 7 +++---- userspace/init/main.cpp | 2 +- 9 files changed, 35 insertions(+), 12 deletions(-) diff --git a/kernel/include/kernel/FS/DevFS/FileSystem.h b/kernel/include/kernel/FS/DevFS/FileSystem.h index f6c6955da6..d8584195c1 100644 --- a/kernel/include/kernel/FS/DevFS/FileSystem.h +++ b/kernel/include/kernel/FS/DevFS/FileSystem.h @@ -14,7 +14,7 @@ namespace Kernel void initialize_device_updater(); - void add_device(BAN::StringView path, BAN::RefPtr); + void add_device(BAN::StringView path, BAN::RefPtr); dev_t get_next_dev(); diff --git a/kernel/include/kernel/Terminal/Serial.h b/kernel/include/kernel/Terminal/Serial.h index 4ddd28fb86..acda40e40e 100644 --- a/kernel/include/kernel/Terminal/Serial.h +++ b/kernel/include/kernel/Terminal/Serial.h @@ -45,11 +45,15 @@ namespace Kernel virtual void update() override; + protected: + virtual BAN::StringView name() const override { return m_name; } + private: SerialTTY(Serial); bool initialize(); private: + BAN::String m_name; Serial m_serial; public: diff --git a/kernel/include/kernel/Terminal/TTY.h b/kernel/include/kernel/Terminal/TTY.h index f32619535a..f67186c294 100644 --- a/kernel/include/kernel/Terminal/TTY.h +++ b/kernel/include/kernel/Terminal/TTY.h @@ -47,6 +47,8 @@ namespace Kernel : CharacterDevice(mode, uid, gid) { } + virtual BAN::StringView name() const = 0; + private: void do_backspace(); diff --git a/kernel/include/kernel/Terminal/VirtualTTY.h b/kernel/include/kernel/Terminal/VirtualTTY.h index 9d391f9491..17c24dbdb1 100644 --- a/kernel/include/kernel/Terminal/VirtualTTY.h +++ b/kernel/include/kernel/Terminal/VirtualTTY.h @@ -23,6 +23,9 @@ namespace Kernel virtual uint32_t width() const override { return m_width; } virtual void putchar(uint8_t ch) override; + protected: + virtual BAN::StringView name() const override { return m_name; } + private: VirtualTTY(TerminalDriver*); @@ -64,6 +67,8 @@ namespace Kernel }; private: + BAN::String m_name; + State m_state { State::Normal }; AnsiState m_ansi_state { }; UTF8State m_utf8_state { }; diff --git a/kernel/kernel/FS/DevFS/FileSystem.cpp b/kernel/kernel/FS/DevFS/FileSystem.cpp index 9d12384693..e3a06dcf0b 100644 --- a/kernel/kernel/FS/DevFS/FileSystem.cpp +++ b/kernel/kernel/FS/DevFS/FileSystem.cpp @@ -52,7 +52,7 @@ namespace Kernel ); } - void DevFileSystem::add_device(BAN::StringView path, BAN::RefPtr device) + void DevFileSystem::add_device(BAN::StringView path, BAN::RefPtr device) { ASSERT(!path.contains('/')); MUST(reinterpret_cast(root_inode().ptr())->add_inode(path, device)); diff --git a/kernel/kernel/Terminal/Serial.cpp b/kernel/kernel/Terminal/Serial.cpp index c97132f04f..0274247f9a 100644 --- a/kernel/kernel/Terminal/Serial.cpp +++ b/kernel/kernel/Terminal/Serial.cpp @@ -207,7 +207,9 @@ namespace Kernel : TTY(0660, 0, 0) , m_serial(serial) , m_rdev(next_rdev()) - {} + { + m_name = BAN::String::formatted("ttyS{}", minor(rdev())); + } BAN::ErrorOr> SerialTTY::create(Serial serial) { @@ -228,11 +230,8 @@ namespace Kernel IDT::register_irq_handler(COM2_IRQ, irq3_handler); } - ASSERT(minor(tty->rdev()) < 10); - char name[] = { 't', 't', 'y', 'S', (char)('0' + minor(tty->rdev())), '\0' }; - auto ref_ptr = BAN::RefPtr::adopt(tty); - DevFileSystem::get().add_device(name, ref_ptr); + DevFileSystem::get().add_device(ref_ptr->name(), ref_ptr); if (serial.port() == COM1_PORT) s_com1 = ref_ptr; if (serial.port() == COM2_PORT) diff --git a/kernel/kernel/Terminal/TTY.cpp b/kernel/kernel/Terminal/TTY.cpp index 28fd2d8d7a..5f996931e3 100644 --- a/kernel/kernel/Terminal/TTY.cpp +++ b/kernel/kernel/Terminal/TTY.cpp @@ -25,6 +25,20 @@ namespace Kernel void TTY::set_as_current() { s_tty = this; + + auto inode_or_error = DevFileSystem::get().root_inode()->directory_find_inode("tty"); + if (inode_or_error.is_error()) + { + if (inode_or_error.error().get_error_code() == ENOENT) + DevFileSystem::get().add_device("tty"sv, MUST(RamSymlinkInode::create(DevFileSystem::get(), s_tty->name(), S_IFLNK | 0666, 0, 0))); + else + dwarnln("{}", inode_or_error.error()); + return; + } + + auto inode = inode_or_error.release_value(); + if (inode->mode().iflnk()) + MUST(((RamSymlinkInode*)inode.ptr())->set_link_target(name())); } void TTY::initialize_devices() diff --git a/kernel/kernel/Terminal/VirtualTTY.cpp b/kernel/kernel/Terminal/VirtualTTY.cpp index 4bed338468..d8896c5407 100644 --- a/kernel/kernel/Terminal/VirtualTTY.cpp +++ b/kernel/kernel/Terminal/VirtualTTY.cpp @@ -36,11 +36,8 @@ namespace Kernel auto* tty = new VirtualTTY(driver); ASSERT(tty); - ASSERT(minor(tty->rdev()) < 10); - char name[5] { 't', 't', 'y', (char)('0' + minor(tty->rdev())), '\0' }; - auto ref_ptr = BAN::RefPtr::adopt(tty); - DevFileSystem::get().add_device(name, ref_ptr); + DevFileSystem::get().add_device(ref_ptr->name(), ref_ptr); return ref_ptr; } @@ -49,6 +46,8 @@ namespace Kernel , m_terminal_driver(driver) , m_rdev(next_rdev()) { + m_name = BAN::String::formatted("tty{}", minor(rdev())); + m_width = m_terminal_driver->width(); m_height = m_terminal_driver->height(); diff --git a/userspace/init/main.cpp b/userspace/init/main.cpp index 92f0f29bf2..1337cd42c8 100644 --- a/userspace/init/main.cpp +++ b/userspace/init/main.cpp @@ -11,7 +11,7 @@ void initialize_stdio() { - const char* tty = "/dev/tty0"; + const char* tty = "/dev/tty"; if (open(tty, O_RDONLY | O_TTY_INIT) != 0) _exit(1); if (open(tty, O_WRONLY) != 1) _exit(1); if (open(tty, O_WRONLY) != 2) _exit(1);