2023-03-18 09:10:33 +01:00
|
|
|
#include "fs/devices/ConsoleDevice.h"
|
2023-05-04 23:32:48 +02:00
|
|
|
#include "Log.h"
|
2023-03-24 17:21:21 +01:00
|
|
|
#include "arch/Keyboard.h"
|
2023-05-09 18:31:27 +02:00
|
|
|
#include "fs/devices/DeviceRegistry.h"
|
2023-04-09 11:23:57 +02:00
|
|
|
#include "memory/MemoryManager.h"
|
2023-05-04 23:32:48 +02:00
|
|
|
#include "thread/Scheduler.h"
|
2023-03-18 09:10:33 +01:00
|
|
|
#include "video/TextConsole.h"
|
2023-07-11 12:05:09 +02:00
|
|
|
#include <bits/ioctl-defs.h>
|
2023-04-09 11:23:57 +02:00
|
|
|
#include <bits/termios.h>
|
2023-03-19 19:15:19 +01:00
|
|
|
#include <luna/Buffer.h>
|
|
|
|
#include <luna/CString.h>
|
2023-05-04 23:32:48 +02:00
|
|
|
#include <luna/Units.h>
|
2023-03-23 21:22:12 +01:00
|
|
|
#include <luna/Vector.h>
|
2023-03-19 19:15:19 +01:00
|
|
|
|
|
|
|
static Buffer g_console_input;
|
2023-03-24 17:21:21 +01:00
|
|
|
static bool g_eof { false };
|
2023-04-09 11:23:57 +02:00
|
|
|
static bool g_echo { true };
|
2023-07-12 13:49:37 +02:00
|
|
|
static bool g_stop_background_output { false };
|
|
|
|
|
|
|
|
static Option<pid_t> g_foreground_process_group;
|
2023-03-18 09:10:33 +01:00
|
|
|
|
2023-05-09 18:31:27 +02:00
|
|
|
Result<void> ConsoleDevice::create()
|
2023-03-18 09:10:33 +01:00
|
|
|
{
|
2023-05-09 18:31:27 +02:00
|
|
|
auto device = (SharedPtr<Device>)TRY(make_shared<ConsoleDevice>());
|
2023-07-12 13:49:37 +02:00
|
|
|
g_foreground_process_group = {};
|
2023-06-16 21:46:51 +02:00
|
|
|
return DeviceRegistry::register_special_device(DeviceRegistry::Console, 0, device);
|
2023-03-18 09:10:33 +01:00
|
|
|
}
|
|
|
|
|
2023-07-12 13:49:37 +02:00
|
|
|
static Result<void> handle_background_process_group(bool can_succeed, int signo)
|
|
|
|
{
|
|
|
|
if (!g_foreground_process_group.has_value()) return {};
|
|
|
|
|
|
|
|
auto foreground_pgrp = g_foreground_process_group.value();
|
|
|
|
|
|
|
|
auto* current = Scheduler::current();
|
|
|
|
if ((pid_t)current->pgid == foreground_pgrp) return {};
|
|
|
|
|
|
|
|
if ((current->signal_mask & (1 << (signo - 1))) || (current->signal_handlers[signo - 1].sa_handler == SIG_IGN))
|
|
|
|
{
|
|
|
|
if (can_succeed) return {};
|
|
|
|
return err(EIO);
|
|
|
|
}
|
|
|
|
|
|
|
|
current->send_signal(signo);
|
|
|
|
|
|
|
|
if (can_succeed) return err(EINTR);
|
|
|
|
return err(EIO);
|
|
|
|
}
|
|
|
|
|
2023-03-19 19:15:19 +01:00
|
|
|
Result<usize> ConsoleDevice::read(u8* buf, usize, usize length) const
|
2023-03-18 09:10:33 +01:00
|
|
|
{
|
2023-07-12 13:49:37 +02:00
|
|
|
TRY(handle_background_process_group(false, SIGTTIN));
|
|
|
|
|
2023-03-19 19:15:19 +01:00
|
|
|
if (length > g_console_input.size()) length = g_console_input.size();
|
|
|
|
|
|
|
|
memcpy(buf, g_console_input.data(), length);
|
|
|
|
|
|
|
|
memmove(g_console_input.data(), g_console_input.data() + length, g_console_input.size() - length);
|
|
|
|
|
|
|
|
g_console_input.try_resize(g_console_input.size() - length).release_value();
|
|
|
|
|
2023-03-24 17:21:21 +01:00
|
|
|
if (!length && g_eof) g_eof = false;
|
|
|
|
|
2023-03-19 19:15:19 +01:00
|
|
|
return length;
|
2023-03-18 09:10:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result<usize> ConsoleDevice::write(const u8* buf, usize, usize length)
|
|
|
|
{
|
2023-07-12 13:49:37 +02:00
|
|
|
if (g_stop_background_output) TRY(handle_background_process_group(true, SIGTTOU));
|
|
|
|
|
2023-03-18 20:10:00 +01:00
|
|
|
TextConsole::write((const char*)buf, length);
|
2023-03-18 09:10:33 +01:00
|
|
|
return length;
|
|
|
|
}
|
2023-03-19 11:21:50 +01:00
|
|
|
|
|
|
|
bool ConsoleDevice::blocking() const
|
|
|
|
{
|
2023-03-24 17:21:21 +01:00
|
|
|
return g_eof ? false : g_console_input.size() == 0;
|
2023-03-19 19:15:19 +01:00
|
|
|
}
|
|
|
|
|
2023-03-23 21:22:12 +01:00
|
|
|
static Vector<u8> g_temp_input;
|
|
|
|
|
2023-03-19 19:15:19 +01:00
|
|
|
void ConsoleDevice::did_press_key(char key)
|
|
|
|
{
|
2023-03-23 21:22:12 +01:00
|
|
|
if (key == '\b')
|
|
|
|
{
|
2023-04-09 11:23:57 +02:00
|
|
|
if (g_temp_input.try_pop().has_value())
|
|
|
|
{
|
|
|
|
if (g_echo) TextConsole::putwchar(L'\b');
|
|
|
|
}
|
2023-03-23 21:22:12 +01:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-03-24 17:21:21 +01:00
|
|
|
// Ctrl+D
|
2023-07-12 13:49:37 +02:00
|
|
|
if (key == 'd' && (Keyboard::modifiers() == Keyboard::LeftControl))
|
2023-03-24 17:21:21 +01:00
|
|
|
{
|
2023-04-26 20:42:26 +02:00
|
|
|
if (g_temp_input.size() == 0) g_eof = true;
|
2023-03-24 17:21:21 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-07-12 13:49:37 +02:00
|
|
|
if (key == 'c' && (Keyboard::modifiers() == Keyboard::LeftControl))
|
|
|
|
{
|
|
|
|
g_temp_input.clear();
|
|
|
|
|
|
|
|
if (g_echo) TextConsole::wprintln(L"^C");
|
|
|
|
|
|
|
|
if (g_foreground_process_group.has_value())
|
|
|
|
{
|
|
|
|
Scheduler::for_each_in_process_group(g_foreground_process_group.value(), [](Thread* thread) {
|
|
|
|
thread->send_signal(SIGINT);
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (key == 'e' && (Keyboard::modifiers() == (Keyboard::LeftAlt | Keyboard::LeftControl)))
|
2023-05-04 23:32:48 +02:00
|
|
|
{
|
|
|
|
Scheduler::dump_state();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-07-12 13:49:37 +02:00
|
|
|
if (key == 'm' && (Keyboard::modifiers() == (Keyboard::LeftAlt | Keyboard::LeftControl)))
|
2023-05-04 23:32:48 +02:00
|
|
|
{
|
|
|
|
kinfoln("Total memory: %s", to_dynamic_unit(MemoryManager::total()).release_value().chars());
|
|
|
|
kinfoln("Free memory: %s", to_dynamic_unit(MemoryManager::free()).release_value().chars());
|
|
|
|
kinfoln("Used memory: %s", to_dynamic_unit(MemoryManager::used()).release_value().chars());
|
|
|
|
kinfoln("Reserved memory: %s", to_dynamic_unit(MemoryManager::reserved()).release_value().chars());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-07-12 13:49:37 +02:00
|
|
|
if (key == 'h' && (Keyboard::modifiers() == (Keyboard::LeftAlt | Keyboard::LeftControl)))
|
2023-05-04 23:32:48 +02:00
|
|
|
{
|
|
|
|
dump_heap_usage();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-06-17 19:43:25 +02:00
|
|
|
g_temp_input.try_append((u8)key);
|
2023-03-23 21:22:12 +01:00
|
|
|
|
|
|
|
if (key == '\n')
|
|
|
|
{
|
2023-06-17 19:43:25 +02:00
|
|
|
g_console_input.append_data(g_temp_input.data(), g_temp_input.size());
|
2023-03-23 21:22:12 +01:00
|
|
|
g_temp_input.clear();
|
|
|
|
}
|
|
|
|
|
2023-04-09 11:23:57 +02:00
|
|
|
if (!g_echo) return;
|
2023-03-23 21:22:12 +01:00
|
|
|
TextConsole::putchar(key);
|
2023-03-19 11:21:50 +01:00
|
|
|
}
|
2023-04-09 11:23:57 +02:00
|
|
|
|
|
|
|
Result<u64> ConsoleDevice::ioctl(int request, void* arg)
|
|
|
|
{
|
|
|
|
switch (request)
|
|
|
|
{
|
|
|
|
case TCGETS: {
|
|
|
|
struct termios tc
|
|
|
|
{
|
|
|
|
.c_lflag = 0
|
|
|
|
};
|
|
|
|
|
|
|
|
if (g_echo) tc.c_lflag |= ECHO;
|
2023-07-12 13:49:37 +02:00
|
|
|
if (g_stop_background_output) tc.c_lflag |= TOSTOP;
|
2023-04-09 11:23:57 +02:00
|
|
|
return MemoryManager::copy_to_user_typed((struct termios*)arg, &tc) ? 0 : err(EFAULT);
|
|
|
|
}
|
|
|
|
case TCSETS: {
|
2023-07-12 13:49:37 +02:00
|
|
|
TRY(handle_background_process_group(true, SIGTTOU));
|
|
|
|
|
2023-04-09 11:23:57 +02:00
|
|
|
struct termios tc;
|
|
|
|
if (!MemoryManager::copy_from_user_typed((const struct termios*)arg, &tc)) return err(EFAULT);
|
|
|
|
if (tc.c_lflag & ECHO) g_echo = true;
|
|
|
|
else
|
|
|
|
g_echo = false;
|
|
|
|
|
2023-07-12 13:49:37 +02:00
|
|
|
if (tc.c_lflag & TOSTOP) g_stop_background_output = true;
|
|
|
|
else
|
|
|
|
g_stop_background_output = false;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
case TIOCSPGRP: {
|
|
|
|
TRY(handle_background_process_group(true, SIGTTOU));
|
|
|
|
|
|
|
|
pid_t pgid;
|
|
|
|
if (!MemoryManager::copy_from_user_typed((const pid_t*)arg, &pgid)) return err(EFAULT);
|
|
|
|
|
|
|
|
bool pgid_exists = false;
|
|
|
|
Scheduler::for_each_in_process_group(pgid, [&pgid_exists](Thread*) {
|
|
|
|
pgid_exists = true;
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
if (!pgid_exists) return err(EPERM);
|
|
|
|
|
|
|
|
g_foreground_process_group = pgid;
|
2023-04-09 11:23:57 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2023-07-12 19:23:06 +02:00
|
|
|
case TIOCGPGRP: {
|
|
|
|
pid_t pgid = g_foreground_process_group.value_or((pid_t)next_thread_id());
|
|
|
|
if (!MemoryManager::copy_to_user_typed((pid_t*)arg, &pgid)) return err(EFAULT);
|
|
|
|
return 0;
|
|
|
|
}
|
2023-07-12 22:09:28 +02:00
|
|
|
case TIOCGWINSZ: {
|
|
|
|
struct winsize window;
|
|
|
|
window.ws_col = TextConsole::cols();
|
|
|
|
window.ws_row = TextConsole::rows();
|
|
|
|
if (!MemoryManager::copy_to_user_typed((struct winsize*)arg, &window)) return err(EFAULT);
|
|
|
|
return 0;
|
|
|
|
}
|
2023-04-09 11:23:57 +02:00
|
|
|
default: return err(EINVAL);
|
|
|
|
}
|
|
|
|
}
|