2018-10-30 15:59:29 +03:00
|
|
|
#include "TTY.h"
|
2018-11-02 16:06:48 +03:00
|
|
|
#include "Process.h"
|
2018-11-16 15:11:21 +03:00
|
|
|
#include <LibC/errno_numbers.h>
|
2018-11-06 17:45:16 +03:00
|
|
|
#include <LibC/signal_numbers.h>
|
2018-11-16 15:11:21 +03:00
|
|
|
#include <LibC/sys/ioctl_numbers.h>
|
2018-10-30 15:59:29 +03:00
|
|
|
|
|
|
|
TTY::TTY(unsigned major, unsigned minor)
|
|
|
|
: CharacterDevice(major, minor)
|
|
|
|
{
|
2018-11-11 17:36:40 +03:00
|
|
|
memset(&m_termios, 0, sizeof(m_termios));
|
|
|
|
m_termios.c_lflag |= ISIG | ECHO;
|
2018-10-30 15:59:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
TTY::~TTY()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t TTY::read(byte* buffer, size_t size)
|
|
|
|
{
|
2018-10-30 17:33:37 +03:00
|
|
|
ssize_t nread = min(m_buffer.size(), size);
|
|
|
|
memcpy(buffer, m_buffer.data(), nread);
|
2018-11-09 12:03:21 +03:00
|
|
|
if (nread == (ssize_t)m_buffer.size())
|
2018-10-30 17:33:37 +03:00
|
|
|
m_buffer.clear();
|
|
|
|
else {
|
2018-11-11 22:42:41 +03:00
|
|
|
kprintf("had %u, read %u\n", m_buffer.size(), nread);
|
2018-10-30 17:33:37 +03:00
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
return nread;
|
2018-10-30 15:59:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t TTY::write(const byte* buffer, size_t size)
|
|
|
|
{
|
2018-11-08 03:23:23 +03:00
|
|
|
onTTYWrite(buffer, size);
|
2018-10-30 15:59:29 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TTY::hasDataAvailableForRead() const
|
|
|
|
{
|
2018-10-30 17:33:37 +03:00
|
|
|
return !m_buffer.isEmpty();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TTY::emit(byte ch)
|
|
|
|
{
|
|
|
|
m_buffer.append(ch);
|
2018-10-30 15:59:29 +03:00
|
|
|
}
|
2018-11-02 16:06:48 +03:00
|
|
|
|
|
|
|
void TTY::interrupt()
|
|
|
|
{
|
2018-11-11 17:36:40 +03:00
|
|
|
if (!should_generate_signals())
|
|
|
|
return;
|
2018-11-02 16:06:48 +03:00
|
|
|
dbgprintf("%s: Interrupt ^C pressed!\n", ttyName().characters());
|
|
|
|
if (pgid()) {
|
|
|
|
dbgprintf("%s: Send SIGINT to everyone in pgrp %d\n", ttyName().characters(), pgid());
|
|
|
|
InterruptDisabler disabler;
|
|
|
|
Process::for_each_in_pgrp(pgid(), [this] (auto& process) {
|
|
|
|
dbgprintf("%s: Send SIGINT to %d\n", ttyName().characters(), process.pid());
|
|
|
|
process.send_signal(SIGINT, nullptr);
|
2018-11-07 23:38:18 +03:00
|
|
|
return true;
|
2018-11-02 16:06:48 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2018-11-12 14:27:28 +03:00
|
|
|
|
|
|
|
void TTY::set_termios(const Unix::termios& t)
|
|
|
|
{
|
|
|
|
m_termios = t;
|
|
|
|
dbgprintf("%s set_termios: IECHO? %u, ISIG? %u\n",
|
|
|
|
ttyName().characters(),
|
|
|
|
should_echo_input(),
|
|
|
|
should_generate_signals());
|
|
|
|
}
|
2018-11-16 15:11:21 +03:00
|
|
|
|
|
|
|
int TTY::ioctl(Process& process, unsigned request, unsigned arg)
|
|
|
|
{
|
2018-11-16 17:41:48 +03:00
|
|
|
pid_t pgid;
|
|
|
|
Unix::termios* tp;
|
|
|
|
|
2018-11-16 15:11:21 +03:00
|
|
|
if (process.tty() != this)
|
|
|
|
return -ENOTTY;
|
|
|
|
switch (request) {
|
|
|
|
case TIOCGPGRP:
|
2018-11-16 17:41:48 +03:00
|
|
|
return m_pgid;
|
|
|
|
case TIOCSPGRP:
|
2018-11-16 15:11:21 +03:00
|
|
|
// FIXME: Validate pgid fully.
|
2018-11-16 17:41:48 +03:00
|
|
|
pgid = static_cast<pid_t>(arg);
|
2018-11-16 15:11:21 +03:00
|
|
|
if (pgid < 0)
|
|
|
|
return -EINVAL;
|
2018-11-16 17:41:48 +03:00
|
|
|
m_pgid = pgid;
|
|
|
|
return 0;
|
|
|
|
case TCGETS:
|
|
|
|
tp = reinterpret_cast<Unix::termios*>(arg);
|
|
|
|
if (!process.validate_write(tp, sizeof(Unix::termios)))
|
|
|
|
return -EFAULT;
|
|
|
|
*tp = m_termios;
|
|
|
|
return 0;
|
|
|
|
case TCSETS:
|
|
|
|
tp = reinterpret_cast<Unix::termios*>(arg);
|
|
|
|
if (!process.validate_read(tp, sizeof(Unix::termios)))
|
|
|
|
return -EFAULT;
|
|
|
|
set_termios(*tp);
|
2018-11-16 15:11:21 +03:00
|
|
|
return 0;
|
|
|
|
}
|
2018-11-16 17:41:48 +03:00
|
|
|
ASSERT_NOT_REACHED();
|
2018-11-16 15:11:21 +03:00
|
|
|
return -EINVAL;
|
|
|
|
}
|