Kernel: Fix pseudo terminal writability

This commit is contained in:
2025-06-06 11:09:50 +03:00
parent a7e20d6e85
commit b668173cba
8 changed files with 63 additions and 30 deletions

View File

@@ -360,7 +360,7 @@ namespace Kernel
{
LockGuard _(inode->m_mutex);
if (is_nonblock && !inode->can_read())
return 0;
return BAN::Error::from_errno(EAGAIN);
nread = TRY(inode->read(offset, buffer));
}
@@ -395,7 +395,7 @@ namespace Kernel
{
LockGuard _(inode->m_mutex);
if (is_nonblock && !inode->can_write())
return BAN::Error::from_errno(EWOULDBLOCK);
return BAN::Error::from_errno(EAGAIN);
nwrite = TRY(inode->write(offset, buffer));
}

View File

@@ -130,6 +130,9 @@ namespace Kernel
m_buffer_size -= to_copy;
m_buffer_tail = (m_buffer_tail + to_copy) % m_buffer->size();
if (auto slave = m_slave.lock())
slave->m_write_blocker.unblock();
epoll_notify(EPOLLOUT);
return to_copy;
@@ -139,12 +142,18 @@ namespace Kernel
{
auto slave = m_slave.lock();
if (!slave)
return BAN::Error::from_errno(ENODEV);
return BAN::Error::from_errno(EIO);
for (size_t i = 0; i < buffer.size(); i++)
slave->handle_input_byte(buffer[i]);
return buffer.size();
}
void PseudoTerminalMaster::on_close(int)
{
if (auto slave = m_slave.lock())
slave->m_write_blocker.unblock();
}
BAN::ErrorOr<long> PseudoTerminalMaster::ioctl_impl(int request, void* argument)
{
auto slave = m_slave.lock();
@@ -186,6 +195,15 @@ namespace Kernel
return false;
}
bool PseudoTerminalSlave::can_write_impl() const
{
auto master = m_master.lock();
if (!master)
return false;
SpinLockGuard _(master->m_buffer_lock);
return master->m_buffer_size < master->m_buffer->size();
}
BAN::ErrorOr<long> PseudoTerminalSlave::ioctl_impl(int request, void* argument)
{
switch (request)

View File

@@ -219,7 +219,7 @@ namespace Kernel
auto* ptr = reinterpret_cast<const uint8_t*>(ansi_c_str);
while (*ptr)
handle_input_byte(*ptr++);
update_cursor();
after_write();
}
}
@@ -297,12 +297,8 @@ namespace Kernel
if (should_append)
{
// FIXME: don't ignore these bytes
if (m_output.bytes >= m_output.buffer.size())
{
dwarnln("TTY input full");
return;
}
m_output.buffer[m_output.bytes++] = ch;
}
@@ -405,6 +401,8 @@ namespace Kernel
if (m_output.bytes == 0)
{
if (master_has_closed())
return 0;
m_output.flush = false;
return 0;
}
@@ -431,6 +429,13 @@ namespace Kernel
BAN::ErrorOr<size_t> TTY::write_impl(off_t, BAN::ConstByteSpan buffer)
{
while (!can_write_impl())
{
if (master_has_closed())
return BAN::Error::from_errno(EIO);
TRY(Thread::current().block_or_eintr_indefinite(m_write_blocker, &m_mutex));
}
size_t written = 0;
{
@@ -438,7 +443,7 @@ namespace Kernel
for (; written < buffer.size(); written++)
if (!putchar(buffer[written]))
break;
update_cursor();
after_write();
}
if (can_write_impl())
@@ -452,7 +457,7 @@ namespace Kernel
ASSERT(s_tty);
SpinLockGuard _(s_tty->m_write_lock);
s_tty->putchar(ch);
s_tty->update_cursor();
s_tty->after_write();
}
bool TTY::is_initialized()

View File

@@ -577,7 +577,7 @@ namespace Kernel
return true;
}
void VirtualTTY::update_cursor()
void VirtualTTY::after_write()
{
if (m_cursor_shown != m_last_cursor_shown)
m_terminal_driver->set_cursor_shown(m_cursor_shown);