2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2022-02-09 18:33:39 +00:00
|
|
|
* Copyright (c) 2022, the SerenityOS developers.
|
2020-01-18 08:38:21 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2020-11-29 23:05:27 +00:00
|
|
|
#include <AK/ScopeGuard.h>
|
2021-05-16 17:00:48 +00:00
|
|
|
#include <AK/StringView.h>
|
2023-01-07 20:52:06 +00:00
|
|
|
#include <Kernel/API/Ioctl.h>
|
2021-09-12 11:29:28 +00:00
|
|
|
#include <Kernel/API/POSIX/errno.h>
|
2023-01-07 17:54:01 +00:00
|
|
|
#include <Kernel/API/POSIX/signal_numbers.h>
|
2023-02-26 16:56:25 +00:00
|
|
|
#include <Kernel/API/ttydefaults.h>
|
|
|
|
#include <Kernel/API/ttydefaultschars.h>
|
2021-01-25 15:07:10 +00:00
|
|
|
#include <Kernel/Debug.h>
|
2022-10-05 17:27:36 +00:00
|
|
|
#include <Kernel/InterruptDisabler.h>
|
2020-05-06 15:40:06 +00:00
|
|
|
#include <Kernel/Process.h>
|
2019-06-07 09:43:58 +00:00
|
|
|
#include <Kernel/TTY/TTY.h>
|
2023-02-26 16:56:25 +00:00
|
|
|
#include <Kernel/UnixTypes.h>
|
2018-10-30 12:59:29 +00:00
|
|
|
|
2020-02-16 00:27:42 +00:00
|
|
|
namespace Kernel {
|
|
|
|
|
2021-12-23 18:08:18 +00:00
|
|
|
TTY::TTY(MajorNumber major, MinorNumber minor)
|
2018-10-30 12:59:29 +00:00
|
|
|
: CharacterDevice(major, minor)
|
|
|
|
{
|
2018-12-07 00:19:02 +00:00
|
|
|
set_default_termios();
|
2018-10-30 12:59:29 +00:00
|
|
|
}
|
|
|
|
|
2022-03-16 19:15:15 +00:00
|
|
|
TTY::~TTY() = default;
|
2018-10-30 12:59:29 +00:00
|
|
|
|
2018-12-07 00:19:02 +00:00
|
|
|
void TTY::set_default_termios()
|
|
|
|
{
|
|
|
|
memset(&m_termios, 0, sizeof(m_termios));
|
2021-05-16 13:27:09 +00:00
|
|
|
m_termios.c_iflag = TTYDEF_IFLAG;
|
|
|
|
m_termios.c_oflag = TTYDEF_OFLAG;
|
|
|
|
m_termios.c_cflag = TTYDEF_CFLAG;
|
|
|
|
m_termios.c_lflag = TTYDEF_LFLAG;
|
|
|
|
m_termios.c_ispeed = TTYDEF_SPEED;
|
|
|
|
m_termios.c_ospeed = TTYDEF_SPEED;
|
|
|
|
memcpy(m_termios.c_cc, ttydefchars, sizeof(ttydefchars));
|
2018-12-07 00:19:02 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 23:51:39 +00:00
|
|
|
ErrorOr<size_t> TTY::read(OpenFileDescription&, u64, UserOrKernelBuffer& buffer, size_t size)
|
2018-10-30 12:59:29 +00:00
|
|
|
{
|
2021-08-19 19:45:07 +00:00
|
|
|
if (Process::current().pgid() != pgid()) {
|
2020-10-02 21:14:37 +00:00
|
|
|
// FIXME: Should we propagate this error path somehow?
|
2021-08-19 19:45:07 +00:00
|
|
|
[[maybe_unused]] auto rc = Process::current().send_signal(SIGTTIN, nullptr);
|
2021-01-20 22:11:17 +00:00
|
|
|
return EINTR;
|
2020-08-04 19:25:44 +00:00
|
|
|
}
|
2020-02-20 12:18:42 +00:00
|
|
|
if (m_input_buffer.size() < static_cast<size_t>(size))
|
2019-08-12 11:32:28 +00:00
|
|
|
size = m_input_buffer.size();
|
|
|
|
|
2020-11-29 23:05:27 +00:00
|
|
|
bool need_evaluate_block_conditions = false;
|
2021-09-01 06:44:55 +00:00
|
|
|
auto result = buffer.write_buffered<512>(size, [&](Bytes data) {
|
2021-05-23 21:16:00 +00:00
|
|
|
size_t bytes_written = 0;
|
2021-09-01 06:44:55 +00:00
|
|
|
for (; bytes_written < data.size(); ++bytes_written) {
|
2021-05-23 21:16:00 +00:00
|
|
|
auto bit_index = m_input_buffer.head_index();
|
|
|
|
bool is_special_character = m_special_character_bitmask[bit_index / 8] & (1 << (bit_index % 8));
|
|
|
|
if (in_canonical_mode() && is_special_character) {
|
|
|
|
u8 ch = m_input_buffer.dequeue();
|
2020-09-12 03:11:07 +00:00
|
|
|
if (ch == '\0') {
|
2021-05-23 21:16:00 +00:00
|
|
|
// EOF
|
2020-09-12 03:11:07 +00:00
|
|
|
m_available_lines--;
|
2020-11-29 23:05:27 +00:00
|
|
|
need_evaluate_block_conditions = true;
|
2020-09-12 03:11:07 +00:00
|
|
|
break;
|
2021-05-23 21:16:00 +00:00
|
|
|
} else {
|
|
|
|
// '\n' or EOL
|
|
|
|
data[bytes_written++] = ch;
|
2020-09-12 03:11:07 +00:00
|
|
|
m_available_lines--;
|
|
|
|
break;
|
|
|
|
}
|
2021-04-30 02:45:27 +00:00
|
|
|
}
|
2021-05-23 21:16:00 +00:00
|
|
|
data[bytes_written] = m_input_buffer.dequeue();
|
2021-05-16 17:00:48 +00:00
|
|
|
}
|
2021-05-23 21:16:00 +00:00
|
|
|
return bytes_written;
|
2021-05-16 17:00:48 +00:00
|
|
|
});
|
2021-05-13 07:08:44 +00:00
|
|
|
if ((!result.is_error() && result.value() > 0) || need_evaluate_block_conditions)
|
2020-11-29 23:05:27 +00:00
|
|
|
evaluate_block_conditions();
|
2021-05-13 07:08:44 +00:00
|
|
|
return result;
|
2018-10-30 12:59:29 +00:00
|
|
|
}
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
ErrorOr<size_t> TTY::write(OpenFileDescription&, u64, UserOrKernelBuffer const& buffer, size_t size)
|
2018-10-30 12:59:29 +00:00
|
|
|
{
|
2021-08-19 19:45:07 +00:00
|
|
|
if (m_termios.c_lflag & TOSTOP && Process::current().pgid() != pgid()) {
|
|
|
|
[[maybe_unused]] auto rc = Process::current().send_signal(SIGTTOU, nullptr);
|
2021-01-20 22:11:17 +00:00
|
|
|
return EINTR;
|
2020-08-04 19:25:44 +00:00
|
|
|
}
|
|
|
|
|
2021-05-13 07:08:44 +00:00
|
|
|
constexpr size_t num_chars = 256;
|
2021-11-07 23:51:39 +00:00
|
|
|
return buffer.read_buffered<num_chars>(size, [&](ReadonlyBytes bytes) -> ErrorOr<size_t> {
|
2021-04-30 02:45:27 +00:00
|
|
|
u8 modified_data[num_chars * 2];
|
2021-06-05 10:39:02 +00:00
|
|
|
size_t modified_data_size = 0;
|
2021-09-01 06:44:55 +00:00
|
|
|
for (const auto& byte : bytes) {
|
|
|
|
process_output(byte, [&modified_data, &modified_data_size](u8 out_ch) {
|
2021-06-05 10:39:02 +00:00
|
|
|
modified_data[modified_data_size++] = out_ch;
|
|
|
|
});
|
2021-04-30 02:45:27 +00:00
|
|
|
}
|
2021-06-16 13:20:35 +00:00
|
|
|
auto bytes_written_or_error = on_tty_write(UserOrKernelBuffer::for_kernel_buffer(modified_data), modified_data_size);
|
|
|
|
if (bytes_written_or_error.is_error() || !(m_termios.c_oflag & OPOST) || !(m_termios.c_oflag & ONLCR))
|
|
|
|
return bytes_written_or_error;
|
|
|
|
auto bytes_written = bytes_written_or_error.value();
|
|
|
|
if (bytes_written == modified_data_size)
|
2021-09-01 06:44:55 +00:00
|
|
|
return bytes.size();
|
2021-05-18 09:06:33 +00:00
|
|
|
|
|
|
|
// Degenerate case where we converted some newlines and encountered a partial write
|
|
|
|
|
|
|
|
// Calculate where in the input buffer the last character would have been
|
|
|
|
size_t pos_data = 0;
|
2021-06-16 13:20:35 +00:00
|
|
|
for (size_t pos_modified_data = 0; pos_modified_data < bytes_written; ++pos_data) {
|
2021-09-01 06:44:55 +00:00
|
|
|
if (bytes[pos_data] == '\n')
|
2021-05-18 09:06:33 +00:00
|
|
|
pos_modified_data += 2;
|
|
|
|
else
|
|
|
|
pos_modified_data += 1;
|
|
|
|
|
|
|
|
// Handle case where the '\r' got written but not the '\n'
|
|
|
|
// FIXME: Our strategy is to retry writing both. We should really be queuing a write for the corresponding '\n'
|
|
|
|
if (pos_modified_data > bytes_written)
|
|
|
|
--pos_data;
|
|
|
|
}
|
2021-06-16 13:20:35 +00:00
|
|
|
return pos_data;
|
2021-04-30 02:45:27 +00:00
|
|
|
});
|
2018-10-30 12:59:29 +00:00
|
|
|
}
|
|
|
|
|
2021-06-05 10:39:02 +00:00
|
|
|
void TTY::echo_with_processing(u8 ch)
|
|
|
|
{
|
|
|
|
process_output(ch, [this](u8 out_ch) { echo(out_ch); });
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Functor>
|
|
|
|
void TTY::process_output(u8 ch, Functor put_char)
|
|
|
|
{
|
|
|
|
if (m_termios.c_oflag & OPOST) {
|
|
|
|
if (ch == '\n' && (m_termios.c_oflag & ONLCR))
|
|
|
|
put_char('\r');
|
|
|
|
put_char(ch);
|
|
|
|
} else {
|
|
|
|
put_char(ch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
bool TTY::can_read(OpenFileDescription const&, u64) const
|
2018-10-30 12:59:29 +00:00
|
|
|
{
|
2019-10-20 06:12:00 +00:00
|
|
|
if (in_canonical_mode()) {
|
|
|
|
return m_available_lines > 0;
|
|
|
|
}
|
2019-08-12 11:32:28 +00:00
|
|
|
return !m_input_buffer.is_empty();
|
2018-10-30 14:33:37 +00:00
|
|
|
}
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
bool TTY::can_write(OpenFileDescription const&, u64) const
|
2019-01-15 08:17:22 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-10-20 06:12:00 +00:00
|
|
|
bool TTY::is_eol(u8 ch) const
|
|
|
|
{
|
|
|
|
return ch == m_termios.c_cc[VEOL];
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TTY::is_eof(u8 ch) const
|
|
|
|
{
|
|
|
|
return ch == m_termios.c_cc[VEOF];
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TTY::is_kill(u8 ch) const
|
|
|
|
{
|
|
|
|
return ch == m_termios.c_cc[VKILL];
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TTY::is_erase(u8 ch) const
|
|
|
|
{
|
|
|
|
return ch == m_termios.c_cc[VERASE];
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TTY::is_werase(u8 ch) const
|
|
|
|
{
|
|
|
|
return ch == m_termios.c_cc[VWERASE];
|
|
|
|
}
|
|
|
|
|
2020-11-29 23:05:27 +00:00
|
|
|
void TTY::emit(u8 ch, bool do_evaluate_block_conditions)
|
2018-10-30 14:33:37 +00:00
|
|
|
{
|
2021-05-16 17:00:48 +00:00
|
|
|
if (m_termios.c_iflag & ISTRIP)
|
|
|
|
ch &= 0x7F;
|
|
|
|
|
2018-11-16 19:18:58 +00:00
|
|
|
if (should_generate_signals()) {
|
2020-09-08 16:07:25 +00:00
|
|
|
if (ch == m_termios.c_cc[VINFO]) {
|
|
|
|
generate_signal(SIGINFO);
|
|
|
|
return;
|
|
|
|
}
|
2018-11-16 19:18:58 +00:00
|
|
|
if (ch == m_termios.c_cc[VINTR]) {
|
|
|
|
generate_signal(SIGINT);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (ch == m_termios.c_cc[VQUIT]) {
|
|
|
|
generate_signal(SIGQUIT);
|
|
|
|
return;
|
|
|
|
}
|
2019-06-06 18:31:14 +00:00
|
|
|
if (ch == m_termios.c_cc[VSUSP]) {
|
|
|
|
generate_signal(SIGTSTP);
|
2020-12-30 21:44:54 +00:00
|
|
|
if (auto original_process_parent = m_original_process_parent.strong_ref()) {
|
2020-12-20 23:09:48 +00:00
|
|
|
[[maybe_unused]] auto rc = original_process_parent->send_signal(SIGCHLD, nullptr);
|
2020-12-30 21:44:54 +00:00
|
|
|
}
|
2020-08-08 20:04:20 +00:00
|
|
|
// TODO: Else send it to the session leader maybe?
|
2019-06-06 18:31:14 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-11-16 19:18:58 +00:00
|
|
|
}
|
2019-10-20 06:12:00 +00:00
|
|
|
|
2020-11-29 23:05:27 +00:00
|
|
|
ScopeGuard guard([&]() {
|
|
|
|
if (do_evaluate_block_conditions)
|
|
|
|
evaluate_block_conditions();
|
|
|
|
});
|
|
|
|
|
2021-05-16 17:00:48 +00:00
|
|
|
if (ch == '\r' && (m_termios.c_iflag & ICRNL))
|
|
|
|
ch = '\n';
|
|
|
|
else if (ch == '\n' && (m_termios.c_iflag & INLCR))
|
|
|
|
ch = '\r';
|
|
|
|
|
2021-05-23 21:16:00 +00:00
|
|
|
auto current_char_head_index = (m_input_buffer.head_index() + m_input_buffer.size()) % TTY_BUFFER_SIZE;
|
|
|
|
m_special_character_bitmask[current_char_head_index / 8] &= ~(1u << (current_char_head_index % 8));
|
|
|
|
|
|
|
|
auto set_special_bit = [&] {
|
|
|
|
m_special_character_bitmask[current_char_head_index / 8] |= (1u << (current_char_head_index % 8));
|
|
|
|
};
|
|
|
|
|
2019-10-20 06:12:00 +00:00
|
|
|
if (in_canonical_mode()) {
|
|
|
|
if (is_eof(ch)) {
|
2021-05-23 21:16:00 +00:00
|
|
|
// Since EOF might change between when the data came in and when it is read,
|
|
|
|
// we use '\0' along with the bitmask to signal EOF. Any non-zero byte with
|
|
|
|
// the special bit set signals an end-of-line.
|
|
|
|
set_special_bit();
|
2019-10-20 06:12:00 +00:00
|
|
|
m_available_lines++;
|
|
|
|
m_input_buffer.enqueue('\0');
|
|
|
|
return;
|
|
|
|
}
|
2021-05-16 17:00:48 +00:00
|
|
|
if (is_kill(ch) && m_termios.c_lflag & ECHOK) {
|
2019-10-20 06:12:00 +00:00
|
|
|
kill_line();
|
|
|
|
return;
|
|
|
|
}
|
2021-05-16 17:00:48 +00:00
|
|
|
if (is_erase(ch) && m_termios.c_lflag & ECHOE) {
|
2019-10-20 06:12:00 +00:00
|
|
|
do_backspace();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (is_werase(ch)) {
|
|
|
|
erase_word();
|
|
|
|
return;
|
|
|
|
}
|
2021-05-16 17:00:48 +00:00
|
|
|
|
|
|
|
if (ch == '\n') {
|
|
|
|
if (m_termios.c_lflag & ECHO || m_termios.c_lflag & ECHONL)
|
2021-06-05 10:39:02 +00:00
|
|
|
echo_with_processing('\n');
|
|
|
|
|
2021-05-23 21:16:00 +00:00
|
|
|
set_special_bit();
|
2021-05-16 17:00:48 +00:00
|
|
|
m_input_buffer.enqueue('\n');
|
|
|
|
m_available_lines++;
|
|
|
|
return;
|
|
|
|
}
|
2021-05-23 21:16:00 +00:00
|
|
|
|
|
|
|
if (is_eol(ch)) {
|
|
|
|
set_special_bit();
|
|
|
|
m_available_lines++;
|
|
|
|
}
|
2019-10-20 06:12:00 +00:00
|
|
|
}
|
2021-05-16 17:00:48 +00:00
|
|
|
|
2019-08-12 11:32:28 +00:00
|
|
|
m_input_buffer.enqueue(ch);
|
2021-05-16 17:00:48 +00:00
|
|
|
if (m_termios.c_lflag & ECHO)
|
2021-06-05 10:39:02 +00:00
|
|
|
echo_with_processing(ch);
|
2019-10-20 06:12:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool TTY::can_do_backspace() const
|
|
|
|
{
|
2020-10-02 21:14:37 +00:00
|
|
|
// can't do back space if we're empty. Plus, we don't want to
|
|
|
|
// remove any lines "committed" by newlines or ^D.
|
2019-10-20 06:12:00 +00:00
|
|
|
if (!m_input_buffer.is_empty() && !is_eol(m_input_buffer.last()) && m_input_buffer.last() != '\0') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-10-06 06:58:09 +00:00
|
|
|
static size_t length_with_tabs(CircularDeque<u8, TTY_BUFFER_SIZE> const& line)
|
|
|
|
{
|
|
|
|
size_t length = 0;
|
|
|
|
for (auto& ch : line) {
|
|
|
|
length += (ch == '\t') ? 8 - (length % 8) : 1;
|
|
|
|
}
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
2019-10-20 06:12:00 +00:00
|
|
|
void TTY::do_backspace()
|
|
|
|
{
|
|
|
|
if (can_do_backspace()) {
|
2021-10-06 06:58:09 +00:00
|
|
|
auto ch = m_input_buffer.dequeue_end();
|
|
|
|
size_t to_delete = 1;
|
|
|
|
|
|
|
|
if (ch == '\t') {
|
|
|
|
auto length = length_with_tabs(m_input_buffer);
|
|
|
|
to_delete = 8 - (length % 8);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < to_delete; ++i) {
|
|
|
|
// We deliberately don't process the output here.
|
|
|
|
echo('\b');
|
|
|
|
echo(' ');
|
|
|
|
echo('\b');
|
|
|
|
}
|
2020-11-29 23:05:27 +00:00
|
|
|
|
|
|
|
evaluate_block_conditions();
|
2019-10-20 06:12:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Currently, both erase_word() and kill_line work by sending
|
|
|
|
// a lot of VERASE characters; this is done because Terminal.cpp
|
|
|
|
// doesn't currently support VWERASE and VKILL. When these are
|
|
|
|
// implemented we could just send a VKILL or VWERASE.
|
|
|
|
|
|
|
|
void TTY::erase_word()
|
|
|
|
{
|
2022-04-01 17:58:27 +00:00
|
|
|
// Note: if we have leading whitespace before the word
|
|
|
|
// we want to delete we have to also delete that.
|
2019-10-20 06:12:00 +00:00
|
|
|
bool first_char = false;
|
2020-11-29 23:05:27 +00:00
|
|
|
bool did_dequeue = false;
|
2019-10-20 06:12:00 +00:00
|
|
|
while (can_do_backspace()) {
|
|
|
|
u8 ch = m_input_buffer.last();
|
|
|
|
if (ch == ' ' && first_char)
|
|
|
|
break;
|
|
|
|
if (ch != ' ')
|
|
|
|
first_char = true;
|
|
|
|
m_input_buffer.dequeue_end();
|
2020-11-29 23:05:27 +00:00
|
|
|
did_dequeue = true;
|
2020-03-26 07:15:29 +00:00
|
|
|
erase_character();
|
2019-10-20 06:12:00 +00:00
|
|
|
}
|
2020-11-29 23:05:27 +00:00
|
|
|
if (did_dequeue)
|
|
|
|
evaluate_block_conditions();
|
2019-10-20 06:12:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TTY::kill_line()
|
|
|
|
{
|
2020-11-29 23:05:27 +00:00
|
|
|
bool did_dequeue = false;
|
2019-10-20 06:12:00 +00:00
|
|
|
while (can_do_backspace()) {
|
|
|
|
m_input_buffer.dequeue_end();
|
2020-11-29 23:05:27 +00:00
|
|
|
did_dequeue = true;
|
2020-03-26 07:15:29 +00:00
|
|
|
erase_character();
|
2019-10-20 06:12:00 +00:00
|
|
|
}
|
2020-11-29 23:05:27 +00:00
|
|
|
if (did_dequeue)
|
|
|
|
evaluate_block_conditions();
|
2018-10-30 12:59:29 +00:00
|
|
|
}
|
2018-11-02 13:06:48 +00:00
|
|
|
|
2020-03-26 07:15:29 +00:00
|
|
|
void TTY::erase_character()
|
|
|
|
{
|
2021-06-05 10:39:02 +00:00
|
|
|
// We deliberately don't process the output here.
|
2020-03-26 07:15:29 +00:00
|
|
|
echo(m_termios.c_cc[VERASE]);
|
|
|
|
echo(' ');
|
|
|
|
echo(m_termios.c_cc[VERASE]);
|
|
|
|
}
|
|
|
|
|
2018-11-16 19:18:58 +00:00
|
|
|
void TTY::generate_signal(int signal)
|
2018-11-02 13:06:48 +00:00
|
|
|
{
|
2018-11-16 19:18:58 +00:00
|
|
|
if (!pgid())
|
2018-11-11 14:36:40 +00:00
|
|
|
return;
|
2019-11-02 09:07:14 +00:00
|
|
|
if (should_flush_on_signal())
|
|
|
|
flush_input();
|
2022-02-15 19:41:41 +00:00
|
|
|
dbgln_if(TTY_DEBUG, "Send signal {} to everyone in pgrp {}", signal, pgid().value());
|
2018-11-16 19:18:58 +00:00
|
|
|
InterruptDisabler disabler; // FIXME: Iterate over a set of process handles instead?
|
2022-11-02 20:26:02 +00:00
|
|
|
MUST(Process::current().for_each_in_pgrp_in_same_jail(pgid(), [&](auto& process) -> ErrorOr<void> {
|
2022-02-15 19:41:41 +00:00
|
|
|
dbgln_if(TTY_DEBUG, "Send signal {} to {}", signal, process);
|
2020-08-05 09:13:30 +00:00
|
|
|
// FIXME: Should this error be propagated somehow?
|
2020-12-20 23:09:48 +00:00
|
|
|
[[maybe_unused]] auto rc = process.send_signal(signal, nullptr);
|
2022-11-02 20:26:02 +00:00
|
|
|
return {};
|
|
|
|
}));
|
2018-11-02 13:06:48 +00:00
|
|
|
}
|
2018-11-12 11:27:28 +00:00
|
|
|
|
2019-11-01 14:24:14 +00:00
|
|
|
void TTY::flush_input()
|
|
|
|
{
|
|
|
|
m_available_lines = 0;
|
|
|
|
m_input_buffer.clear();
|
2020-11-29 23:05:27 +00:00
|
|
|
evaluate_block_conditions();
|
2019-11-01 14:24:14 +00:00
|
|
|
}
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
ErrorOr<void> TTY::set_termios(termios const& t)
|
2018-11-12 11:27:28 +00:00
|
|
|
{
|
2021-11-07 23:51:39 +00:00
|
|
|
ErrorOr<void> rc;
|
2018-11-12 11:27:28 +00:00
|
|
|
m_termios = t;
|
2021-01-15 20:29:01 +00:00
|
|
|
|
2022-02-15 19:41:41 +00:00
|
|
|
dbgln_if(TTY_DEBUG, "set_termios: ECHO={}, ISIG={}, ICANON={}, ECHOE={}, ECHOK={}, ECHONL={}, ISTRIP={}, ICRNL={}, INLCR={}, IGNCR={}, OPOST={}, ONLCR={}",
|
2021-01-15 20:29:01 +00:00
|
|
|
should_echo_input(),
|
|
|
|
should_generate_signals(),
|
|
|
|
in_canonical_mode(),
|
|
|
|
((m_termios.c_lflag & ECHOE) != 0),
|
|
|
|
((m_termios.c_lflag & ECHOK) != 0),
|
|
|
|
((m_termios.c_lflag & ECHONL) != 0),
|
|
|
|
((m_termios.c_iflag & ISTRIP) != 0),
|
|
|
|
((m_termios.c_iflag & ICRNL) != 0),
|
|
|
|
((m_termios.c_iflag & INLCR) != 0),
|
2021-04-30 02:45:27 +00:00
|
|
|
((m_termios.c_iflag & IGNCR) != 0),
|
|
|
|
((m_termios.c_oflag & OPOST) != 0),
|
|
|
|
((m_termios.c_oflag & ONLCR) != 0));
|
2021-05-16 17:00:48 +00:00
|
|
|
|
|
|
|
struct FlagDescription {
|
|
|
|
tcflag_t value;
|
|
|
|
StringView name;
|
|
|
|
};
|
|
|
|
|
2022-02-09 18:33:39 +00:00
|
|
|
constexpr FlagDescription unimplemented_iflags[] = {
|
2022-07-11 17:32:29 +00:00
|
|
|
{ IGNBRK, "IGNBRK"sv },
|
|
|
|
{ BRKINT, "BRKINT"sv },
|
|
|
|
{ IGNPAR, "IGNPAR"sv },
|
|
|
|
{ PARMRK, "PARMRK"sv },
|
|
|
|
{ INPCK, "INPCK"sv },
|
|
|
|
{ IGNCR, "IGNCR"sv },
|
|
|
|
{ IUCLC, "IUCLC"sv },
|
|
|
|
{ IXON, "IXON"sv },
|
|
|
|
{ IXANY, "IXANY"sv },
|
|
|
|
{ IXOFF, "IXOFF"sv },
|
|
|
|
{ IMAXBEL, "IMAXBEL"sv },
|
|
|
|
{ IUTF8, "IUTF8"sv }
|
2021-05-16 17:00:48 +00:00
|
|
|
};
|
|
|
|
for (auto flag : unimplemented_iflags) {
|
|
|
|
if (m_termios.c_iflag & flag.value) {
|
|
|
|
dbgln("FIXME: iflag {} unimplemented", flag.name);
|
2021-07-26 10:47:25 +00:00
|
|
|
rc = ENOTIMPL;
|
2021-05-16 17:00:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-09 18:33:39 +00:00
|
|
|
constexpr FlagDescription unimplemented_oflags[] = {
|
2022-07-11 17:32:29 +00:00
|
|
|
{ OLCUC, "OLCUC"sv },
|
|
|
|
{ ONOCR, "ONOCR"sv },
|
|
|
|
{ ONLRET, "ONLRET"sv },
|
|
|
|
{ OFILL, "OFILL"sv },
|
|
|
|
{ OFDEL, "OFDEL"sv }
|
2021-05-16 17:00:48 +00:00
|
|
|
};
|
|
|
|
for (auto flag : unimplemented_oflags) {
|
|
|
|
if (m_termios.c_oflag & flag.value) {
|
|
|
|
dbgln("FIXME: oflag {} unimplemented", flag.name);
|
2021-07-26 10:47:25 +00:00
|
|
|
rc = ENOTIMPL;
|
2021-05-16 17:00:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-25 12:36:05 +00:00
|
|
|
if ((m_termios.c_cflag & CSIZE) != CS8) {
|
|
|
|
dbgln("FIXME: Character sizes other than 8 bits are not supported");
|
|
|
|
rc = ENOTIMPL;
|
|
|
|
}
|
|
|
|
|
2022-02-09 18:33:39 +00:00
|
|
|
constexpr FlagDescription unimplemented_cflags[] = {
|
2022-07-11 17:32:29 +00:00
|
|
|
{ CSTOPB, "CSTOPB"sv },
|
|
|
|
{ CREAD, "CREAD"sv },
|
|
|
|
{ PARENB, "PARENB"sv },
|
|
|
|
{ PARODD, "PARODD"sv },
|
|
|
|
{ HUPCL, "HUPCL"sv },
|
|
|
|
{ CLOCAL, "CLOCAL"sv }
|
2021-05-16 17:00:48 +00:00
|
|
|
};
|
|
|
|
for (auto flag : unimplemented_cflags) {
|
|
|
|
if (m_termios.c_cflag & flag.value) {
|
|
|
|
dbgln("FIXME: cflag {} unimplemented", flag.name);
|
2021-07-26 10:47:25 +00:00
|
|
|
rc = ENOTIMPL;
|
2021-05-16 17:00:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-09 18:33:39 +00:00
|
|
|
constexpr FlagDescription unimplemented_lflags[] = {
|
2022-07-11 17:32:29 +00:00
|
|
|
{ TOSTOP, "TOSTOP"sv },
|
|
|
|
{ IEXTEN, "IEXTEN"sv }
|
2021-05-16 17:00:48 +00:00
|
|
|
};
|
|
|
|
for (auto flag : unimplemented_lflags) {
|
|
|
|
if (m_termios.c_lflag & flag.value) {
|
|
|
|
dbgln("FIXME: lflag {} unimplemented", flag.name);
|
2021-07-26 10:47:25 +00:00
|
|
|
rc = ENOTIMPL;
|
2021-05-16 17:00:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
2018-11-12 11:27:28 +00:00
|
|
|
}
|
2018-11-16 12:11:21 +00:00
|
|
|
|
2021-11-07 23:51:39 +00:00
|
|
|
ErrorOr<void> TTY::ioctl(OpenFileDescription&, unsigned request, Userspace<void*> arg)
|
2018-11-16 12:11:21 +00:00
|
|
|
{
|
2021-08-19 19:45:07 +00:00
|
|
|
auto& current_process = Process::current();
|
2021-12-29 09:11:45 +00:00
|
|
|
TRY(current_process.require_promise(Pledge::tty));
|
2019-02-20 22:32:33 +00:00
|
|
|
#if 0
|
|
|
|
// FIXME: When should we block things?
|
|
|
|
// How do we make this work together with MasterPTY forwarding to us?
|
2020-06-28 21:34:31 +00:00
|
|
|
if (current_process.tty() && current_process.tty() != this) {
|
2021-08-05 22:35:27 +00:00
|
|
|
return ENOTTY;
|
2019-02-20 22:32:33 +00:00
|
|
|
}
|
|
|
|
#endif
|
2018-11-16 12:11:21 +00:00
|
|
|
switch (request) {
|
2021-07-26 10:42:16 +00:00
|
|
|
case TIOCGPGRP: {
|
|
|
|
auto user_pgid = static_ptr_cast<pid_t*>(arg);
|
|
|
|
auto pgid = this->pgid().value();
|
2021-09-05 15:38:37 +00:00
|
|
|
return copy_to_user(user_pgid, &pgid);
|
2021-07-26 10:42:16 +00:00
|
|
|
}
|
2020-08-08 20:04:20 +00:00
|
|
|
case TIOCSPGRP: {
|
2021-07-26 09:47:00 +00:00
|
|
|
ProcessGroupID pgid = static_cast<pid_t>(arg.ptr());
|
2020-02-26 20:33:14 +00:00
|
|
|
if (pgid <= 0)
|
2021-07-26 10:47:25 +00:00
|
|
|
return EINVAL;
|
2020-08-08 20:04:20 +00:00
|
|
|
InterruptDisabler disabler;
|
2020-08-15 19:13:19 +00:00
|
|
|
auto process_group = ProcessGroup::from_pgid(pgid);
|
|
|
|
// Disallow setting a nonexistent PGID.
|
|
|
|
if (!process_group)
|
2021-07-26 10:47:25 +00:00
|
|
|
return EINVAL;
|
2020-08-15 19:13:19 +00:00
|
|
|
|
2022-11-02 20:26:02 +00:00
|
|
|
auto process = Process::from_pid_in_same_jail(ProcessID(pgid.value()));
|
2020-08-08 20:04:20 +00:00
|
|
|
SessionID new_sid = process ? process->sid() : Process::get_sid_from_pgid(pgid);
|
|
|
|
if (!new_sid || new_sid != current_process.sid())
|
2021-07-26 10:47:25 +00:00
|
|
|
return EPERM;
|
2020-08-08 20:04:20 +00:00
|
|
|
if (process && pgid != process->pgid())
|
2021-07-26 10:47:25 +00:00
|
|
|
return EPERM;
|
2023-04-02 19:57:12 +00:00
|
|
|
m_pg = TRY(process_group->try_make_weak_ptr());
|
2020-08-15 19:13:19 +00:00
|
|
|
|
|
|
|
if (process) {
|
2022-11-02 20:26:02 +00:00
|
|
|
if (auto parent = Process::from_pid_ignoring_jails(process->ppid())) {
|
AK: Make RefPtr, NonnullRefPtr, WeakPtr thread safe
This makes most operations thread safe, especially so that they
can safely be used in the Kernel. This includes obtaining a strong
reference from a weak reference, which now requires an explicit
call to WeakPtr::strong_ref(). Another major change is that
Weakable::make_weak_ref() may require the explicit target type.
Previously we used reinterpret_cast in WeakPtr, assuming that it
can be properly converted. But WeakPtr does not necessarily have
the knowledge to be able to do this. Instead, we now ask the class
itself to deliver a WeakPtr to the type that we want.
Also, WeakLink is no longer specific to a target type. The reason
for this is that we want to be able to safely convert e.g. WeakPtr<T>
to WeakPtr<U>, and before this we just reinterpret_cast the internal
WeakLink<T> to WeakLink<U>, which is a bold assumption that it would
actually produce the correct code. Instead, WeakLink now operates
on just a raw pointer and we only make those constructors/operators
available if we can verify that it can be safely cast.
In order to guarantee thread safety, we now use the least significant
bit in the pointer for locking purposes. This also means that only
properly aligned pointers can be used.
2020-09-29 22:26:13 +00:00
|
|
|
m_original_process_parent = *parent;
|
2021-11-07 23:51:39 +00:00
|
|
|
return {};
|
2020-08-15 19:13:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m_original_process_parent = nullptr;
|
2021-11-07 23:51:39 +00:00
|
|
|
return {};
|
2020-08-08 20:04:20 +00:00
|
|
|
}
|
2020-07-30 22:17:25 +00:00
|
|
|
case TCGETS: {
|
2021-12-17 08:12:20 +00:00
|
|
|
auto user_termios = static_ptr_cast<termios*>(arg);
|
2021-09-05 15:38:37 +00:00
|
|
|
return copy_to_user(user_termios, &m_termios);
|
2020-07-30 22:17:25 +00:00
|
|
|
}
|
2018-11-16 14:41:48 +00:00
|
|
|
case TCSETS:
|
2018-11-16 16:56:18 +00:00
|
|
|
case TCSETSF:
|
2020-07-30 22:17:25 +00:00
|
|
|
case TCSETSW: {
|
2021-12-17 08:12:20 +00:00
|
|
|
auto user_termios = static_ptr_cast<termios const*>(arg);
|
|
|
|
auto termios = TRY(copy_typed_from_user(user_termios));
|
2021-07-26 10:47:25 +00:00
|
|
|
auto rc = set_termios(termios);
|
2020-05-09 10:30:51 +00:00
|
|
|
if (request == TCSETSF)
|
|
|
|
flush_input();
|
2021-05-16 17:00:48 +00:00
|
|
|
return rc;
|
2020-07-30 22:17:25 +00:00
|
|
|
}
|
2021-07-26 09:47:00 +00:00
|
|
|
case TCFLSH: {
|
2020-07-11 04:19:08 +00:00
|
|
|
// Serenity's TTY implementation does not use an output buffer, so ignore TCOFLUSH.
|
2021-07-26 09:47:00 +00:00
|
|
|
auto operation = static_cast<u8>(arg.ptr());
|
|
|
|
if (operation == TCIFLUSH || operation == TCIOFLUSH) {
|
2020-07-11 04:19:08 +00:00
|
|
|
flush_input();
|
2021-07-26 09:47:00 +00:00
|
|
|
} else if (operation != TCOFLUSH) {
|
2021-07-26 10:47:25 +00:00
|
|
|
return EINVAL;
|
2020-07-11 04:19:08 +00:00
|
|
|
}
|
2021-11-07 23:51:39 +00:00
|
|
|
return {};
|
2021-07-26 09:47:00 +00:00
|
|
|
}
|
2021-12-17 08:12:20 +00:00
|
|
|
case TIOCGWINSZ: {
|
|
|
|
auto user_winsize = static_ptr_cast<winsize*>(arg);
|
2021-12-29 10:59:44 +00:00
|
|
|
winsize ws {};
|
2020-07-30 22:17:25 +00:00
|
|
|
ws.ws_row = m_rows;
|
|
|
|
ws.ws_col = m_columns;
|
|
|
|
ws.ws_xpixel = 0;
|
|
|
|
ws.ws_ypixel = 0;
|
2021-09-05 15:38:37 +00:00
|
|
|
return copy_to_user(user_winsize, &ws);
|
2021-12-17 08:12:20 +00:00
|
|
|
}
|
2020-07-30 22:17:25 +00:00
|
|
|
case TIOCSWINSZ: {
|
2021-12-17 08:12:20 +00:00
|
|
|
auto user_winsize = static_ptr_cast<winsize const*>(arg);
|
|
|
|
auto ws = TRY(copy_typed_from_user(user_winsize));
|
2020-07-30 22:17:25 +00:00
|
|
|
if (ws.ws_col == m_columns && ws.ws_row == m_rows)
|
2021-11-07 23:51:39 +00:00
|
|
|
return {};
|
2020-07-30 22:17:25 +00:00
|
|
|
m_rows = ws.ws_row;
|
|
|
|
m_columns = ws.ws_col;
|
2019-02-20 22:32:33 +00:00
|
|
|
generate_signal(SIGWINCH);
|
2021-11-07 23:51:39 +00:00
|
|
|
return {};
|
2020-07-30 22:17:25 +00:00
|
|
|
}
|
2019-01-15 07:49:24 +00:00
|
|
|
case TIOCSCTTY:
|
2020-06-28 21:34:31 +00:00
|
|
|
current_process.set_tty(this);
|
2021-11-07 23:51:39 +00:00
|
|
|
return {};
|
2021-03-31 20:58:41 +00:00
|
|
|
case TIOCSTI:
|
2021-07-26 10:47:25 +00:00
|
|
|
return EIO;
|
2019-01-15 07:49:24 +00:00
|
|
|
case TIOCNOTTY:
|
2020-06-28 21:34:31 +00:00
|
|
|
current_process.set_tty(nullptr);
|
2021-11-07 23:51:39 +00:00
|
|
|
return {};
|
2022-04-28 07:17:32 +00:00
|
|
|
case KDSETMODE: {
|
|
|
|
auto mode = static_cast<unsigned int>(arg.ptr());
|
|
|
|
if (mode != KD_TEXT && mode != KD_GRAPHICS)
|
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
set_graphical(mode == KD_GRAPHICS);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
case KDGETMODE: {
|
|
|
|
auto mode_ptr = static_ptr_cast<int*>(arg);
|
|
|
|
int mode = (is_graphical()) ? KD_GRAPHICS : KD_TEXT;
|
|
|
|
return copy_to_user(mode_ptr, &mode);
|
|
|
|
}
|
2018-11-16 12:11:21 +00:00
|
|
|
}
|
2021-07-26 10:47:25 +00:00
|
|
|
return EINVAL;
|
2018-11-16 12:11:21 +00:00
|
|
|
}
|
2018-11-29 02:45:23 +00:00
|
|
|
|
|
|
|
void TTY::set_size(unsigned short columns, unsigned short rows)
|
|
|
|
{
|
|
|
|
m_rows = rows;
|
|
|
|
m_columns = columns;
|
|
|
|
}
|
2019-02-05 11:55:19 +00:00
|
|
|
|
|
|
|
void TTY::hang_up()
|
|
|
|
{
|
|
|
|
generate_signal(SIGHUP);
|
|
|
|
}
|
2020-02-16 00:27:42 +00:00
|
|
|
}
|