From 5f0830cd41acca287f24e609a38cddf9985d518d Mon Sep 17 00:00:00 2001 From: apio Date: Sat, 14 Oct 2023 19:00:10 +0200 Subject: [PATCH] kernel: Add /dev/tty --- kernel/CMakeLists.txt | 1 + kernel/src/fs/devices/DeviceRegistry.cpp | 7 +++ kernel/src/fs/devices/MasterPTY.cpp | 2 +- kernel/src/fs/devices/TTYLink.cpp | 15 +++++ kernel/src/fs/devices/TTYLink.h | 75 ++++++++++++++++++++++++ kernel/src/thread/Thread.h | 2 + 6 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 kernel/src/fs/devices/TTYLink.cpp create mode 100644 kernel/src/fs/devices/TTYLink.h diff --git a/kernel/CMakeLists.txt b/kernel/CMakeLists.txt index 5ceff595..a300480e 100644 --- a/kernel/CMakeLists.txt +++ b/kernel/CMakeLists.txt @@ -76,6 +76,7 @@ set(SOURCES src/fs/devices/PTYMultiplexer.cpp src/fs/devices/MasterPTY.cpp src/fs/devices/SlavePTY.cpp + src/fs/devices/TTYLink.cpp src/fs/InitRD.cpp src/binfmt/ELF.cpp src/binfmt/BinaryFormat.cpp diff --git a/kernel/src/fs/devices/DeviceRegistry.cpp b/kernel/src/fs/devices/DeviceRegistry.cpp index a46b1d10..0a6d8ad9 100644 --- a/kernel/src/fs/devices/DeviceRegistry.cpp +++ b/kernel/src/fs/devices/DeviceRegistry.cpp @@ -8,6 +8,7 @@ #include "fs/devices/MouseDevice.h" #include "fs/devices/NullDevice.h" #include "fs/devices/PTYMultiplexer.h" +#include "fs/devices/TTYLink.h" #include "fs/devices/UARTDevice.h" #include "fs/devices/ZeroDevice.h" #include "fs/tmpfs/FileSystem.h" @@ -99,6 +100,12 @@ namespace DeviceRegistry TRY(fs->root_inode()->add_entry(multiplexer, "ptmx")); + auto ttylink = TRY(make_shared()); + ttylink->set_fs(*fs); + ttylink->set_inode_number(TRY(fs->allocate_inode_number())); + + TRY(fs->root_inode()->add_entry(ttylink, "tty")); + return fs; } } diff --git a/kernel/src/fs/devices/MasterPTY.cpp b/kernel/src/fs/devices/MasterPTY.cpp index 7128e6d6..150e1090 100644 --- a/kernel/src/fs/devices/MasterPTY.cpp +++ b/kernel/src/fs/devices/MasterPTY.cpp @@ -29,7 +29,7 @@ Result> MasterPTY::create_pair(int index) master->m_window.ws_row = 25; slave->m_master = master.ptr(); - slave->m_metadata.devid = luna_dev_makedev(DeviceRegistry::Terminal, index + 1); + slave->m_metadata.devid = luna_dev_makedev(DeviceRegistry::Terminal, index + 2); slave->m_metadata.uid = Scheduler::current()->auth.euid; slave->m_metadata.gid = Scheduler::current()->auth.egid; slave->m_metadata.mode = 0620; diff --git a/kernel/src/fs/devices/TTYLink.cpp b/kernel/src/fs/devices/TTYLink.cpp new file mode 100644 index 00000000..8dd3c1e4 --- /dev/null +++ b/kernel/src/fs/devices/TTYLink.cpp @@ -0,0 +1,15 @@ +#include "fs/devices/TTYLink.h" +#include "thread/Scheduler.h" + +TTYLink::TTYLink() +{ + m_metadata.devid = luna_dev_makedev(DeviceRegistry::Terminal, 1); + m_metadata.mode = 0666; +} + +Result> TTYLink::open() +{ + if (!Scheduler::current()->controlling_terminal) return err(ENXIO); + + return Scheduler::current()->controlling_terminal; +} diff --git a/kernel/src/fs/devices/TTYLink.h b/kernel/src/fs/devices/TTYLink.h new file mode 100644 index 00000000..0b64c5c6 --- /dev/null +++ b/kernel/src/fs/devices/TTYLink.h @@ -0,0 +1,75 @@ +#pragma once +#include "fs/VFS.h" +#include "fs/devices/DeviceRegistry.h" +#include "fs/devices/MasterPTY.h" +#include + +/* /dev/tty implementation. */ +class TTYLink : public VFS::DeviceInode +{ + public: + TTYLink(); + + VFS::InodeType type() const override + { + return VFS::InodeType::CharacterDevice; + } + + void set_fs(VFS::FileSystem& fs) + { + m_fs = &fs; + } + + void set_inode_number(usize inum) + { + m_metadata.inum = inum; + } + + Result query_shared_memory(off_t, usize) override + { + unreachable(); + } + + Result> open() override; + + VFS::FileSystem* fs() const override + { + return m_fs; + } + + Result read(u8*, usize, usize) const override + { + unreachable(); + } + + Result write(const u8*, usize, usize) override + { + unreachable(); + } + + Result truncate(usize) override + { + // POSIX says truncate is for regular files, but doesn't tell us what error to return for non-regular files. + return err(EINVAL); + } + + bool will_block_if_read() const override + { + unreachable(); + } + + void did_link() override + { + m_metadata.nlinks++; + } + + void did_unlink() override + { + m_metadata.nlinks--; + } + + virtual ~TTYLink() = default; + + private: + VFS::FileSystem* m_fs; +}; diff --git a/kernel/src/thread/Thread.h b/kernel/src/thread/Thread.h index 0ad2e929..bcb35697 100644 --- a/kernel/src/thread/Thread.h +++ b/kernel/src/thread/Thread.h @@ -110,6 +110,8 @@ struct Thread : public LinkedListNode Bitset pending_signals { 0 }; bool interrupted { false }; + SharedPtr controlling_terminal; + bool unrestricted_task { false }; FPData fp_data;