Kernel: Add /dev/tty symlink that targets the current tty

This commit is contained in:
Bananymous 2023-09-05 00:59:29 +03:00
parent f0772e385c
commit ba7e1b9ca5
9 changed files with 35 additions and 12 deletions

View File

@ -14,7 +14,7 @@ namespace Kernel
void initialize_device_updater();
void add_device(BAN::StringView path, BAN::RefPtr<Device>);
void add_device(BAN::StringView path, BAN::RefPtr<RamInode>);
dev_t get_next_dev();

View File

@ -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:

View File

@ -47,6 +47,8 @@ namespace Kernel
: CharacterDevice(mode, uid, gid)
{ }
virtual BAN::StringView name() const = 0;
private:
void do_backspace();

View File

@ -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 { };

View File

@ -52,7 +52,7 @@ namespace Kernel
);
}
void DevFileSystem::add_device(BAN::StringView path, BAN::RefPtr<Device> device)
void DevFileSystem::add_device(BAN::StringView path, BAN::RefPtr<RamInode> device)
{
ASSERT(!path.contains('/'));
MUST(reinterpret_cast<RamDirectoryInode*>(root_inode().ptr())->add_inode(path, device));

View File

@ -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<BAN::RefPtr<SerialTTY>> 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<SerialTTY>::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)

View File

@ -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()

View File

@ -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<VirtualTTY>::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();

View File

@ -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);