1
0
Fork 0
mirror of https://gitlab.com/sortix/sortix.git synced 2023-02-13 20:55:38 -05:00
sortix--sortix/kernel/logterminal.cpp

893 lines
23 KiB
C++
Raw Normal View History

/*******************************************************************************
2016-01-23 14:56:07 -05:00
Copyright(C) Jonas 'Sortie' Termansen 2012, 2013, 2014, 2015, 2016.
This file is part of Sortix.
Sortix is free software: you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.
Sortix is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
details.
You should have received a copy of the GNU General Public License along with
Sortix. If not, see <http://www.gnu.org/licenses/>.
logterminal.cpp
A simple terminal that writes to the kernel log.
*******************************************************************************/
2013-05-17 09:30:13 -04:00
#include <sys/types.h>
#include <errno.h>
2016-01-23 14:56:07 -05:00
#include <signal.h>
2013-05-17 09:30:13 -04:00
#include <stdint.h>
2016-01-23 14:56:07 -05:00
#include <stdlib.h>
#include <string.h>
2014-01-03 20:45:01 -05:00
#include <wchar.h>
2012-12-29 17:09:24 -05:00
#include <sortix/fcntl.h>
#include <sortix/keycodes.h>
2013-05-17 09:30:13 -04:00
#include <sortix/poll.h>
Multithreaded kernel and improvement of signal handling. Pardon the big ass-commit, this took months to develop and debug and the refactoring got so far that a clean merge became impossible. The good news is that this commit does quite a bit of cleaning up and generally improves the kernel quality. This makes the kernel fully pre-emptive and multithreaded. This was done by rewriting the interrupt code, the scheduler, introducing new threading primitives, and rewriting large parts of the kernel. During the past few commits the kernel has had its device drivers thread secured; this commit thread secures large parts of the core kernel. There still remains some parts of the kernel that is _not_ thread secured, but this is not a problem at this point. Each user-space thread has an associated kernel stack that it uses when it goes into kernel mode. This stack is by default 8 KiB since that value works for me and is also used by Linux. Strange things tends to happen on x86 in case of a stack overflow - there is no ideal way to catch such a situation right now. The system call conventions were changed, too. The %edx register is now used to provide the errno value of the call, instead of the kernel writing it into a registered global variable. The system call code has also been updated to better reflect the native calling conventions: not all registers have to be preserved. This makes system calls faster and simplifies the assembly. In the kernel, there is no longer the event.h header or the hacky method of 'resuming system calls' that closely resembles cooperative multitasking. If a system call wants to block, it should just block. The signal handling was also improved significantly. At this point, signals cannot interrupt kernel threads (but can always interrupt user-space threads if enabled), which introduces some problems with how a SIGINT could interrupt a blocking read, for instance. This commit introduces and uses a number of new primitives such as kthread_lock_mutex_signal() that attempts to get the lock but fails if a signal is pending. In this manner, the kernel is safer as kernel threads cannot be shut down inconveniently, but in return for complexity as blocking operations must check they if they should fail. Process exiting has also been refactored significantly. The _exit(2) system call sets the exit code and sends SIGKILL to all the threads in the process. Once all the threads have cleaned themselves up and exited, a worker thread calls the process's LastPrayer() method that unmaps memory, deletes the address space, notifies the parent, etc. This provides a very robust way to terminate processes as even half-constructed processes (during a failing fork for instance) can be gracefully terminated. I have introduced a number of kernel threads to help avoid threading problems and simplify kernel design. For instance, there is now a functional generic kernel worker thread that any kernel thread can schedule jobs for. Interrupt handlers run with interrupts off (hence they cannot call kthread_ functions as it may deadlock the system if another thread holds the lock) therefore they cannot use the standard kernel worker threads. Instead, they use a special purpose interrupt worker thread that works much like the generic one expect that interrupt handlers can safely queue work with interrupts off. Note that this also means that interrupt handlers cannot allocate memory or print to the kernel log/screen as such mechanisms uses locks. I'll introduce a lock free algorithm for such cases later on. The boot process has also changed. The original kernel init thread in kernel.cpp creates a new bootstrap thread and becomes the system idle thread. Note that pid=0 now means the kernel, as there is no longer a system idle process. The bootstrap thread launches all the kernel worker threads and then creates a new process and loads /bin/init into it and then creates a thread in pid=1, which starts the system. The bootstrap thread then quietly waits for pid=1 to exit after which it shuts down/reboots/panics the system. In general, the introduction of race conditions and dead locks have forced me to revise a lot of the design and make sure it was thread secure. Since early parts of the kernel was quite hacky, I had to refactor such code. So it seems that the risk of dead locks forces me to write better code. Note that a real preemptive multithreaded kernel simplifies the construction of blocking system calls. My hope is that this will trigger a clean up of the filesystem code that current is almost beyond repair. Almost all of the kernel was modified during this refactoring. To the extent possible, these changes have been backported to older non-multithreaded kernel, but many changes were tightly coupled and went into this commit. Of interest is the implementation of the kthread_ api based on the design of pthreads; this library allows easy synchronization mechanisms and includes C++-style scoped locks. This commit also introduces new worker threads and tested mechanisms for interrupt handlers to schedule work in a kernel worker thread. A lot of code have been rewritten from scratch and has become a lot more stable and correct. Share and enjoy!
2012-08-01 11:30:34 -04:00
#include <sortix/signal.h>
#include <sortix/stat.h>
2013-05-17 09:30:13 -04:00
#include <sortix/termios.h>
#include <sortix/termmode.h>
2016-01-23 14:56:07 -05:00
#include <sortix/winsize.h>
2012-12-29 17:09:24 -05:00
2013-05-17 09:30:13 -04:00
#include <sortix/kernel/inode.h>
#include <sortix/kernel/interlock.h>
#include <sortix/kernel/ioctx.h>
2013-10-26 20:42:10 -04:00
#include <sortix/kernel/kernel.h>
2013-05-17 09:30:13 -04:00
#include <sortix/kernel/keyboard.h>
#include <sortix/kernel/kthread.h>
#include <sortix/kernel/poll.h>
#include <sortix/kernel/process.h>
#include <sortix/kernel/ptable.h>
2013-05-17 09:30:13 -04:00
#include <sortix/kernel/refcount.h>
#include <sortix/kernel/scheduler.h>
2016-01-23 14:56:07 -05:00
#include <sortix/kernel/thread.h>
2013-01-09 04:47:22 -05:00
#include "logterminal.h"
2016-01-23 14:56:07 -05:00
#define CONTROL(x) (((x) - 64) & 127)
#define M_CONTROL(x) (128 + CONTROL(x))
namespace Sortix {
2016-01-23 14:56:07 -05:00
static const unsigned int SUPPORTED_TERMMODES = TERMMODE_KBKEY
| TERMMODE_UNICODE
| TERMMODE_SIGNAL
| TERMMODE_UTF8
| TERMMODE_LINEBUFFER
| TERMMODE_ECHO
| TERMMODE_NONBLOCK
| TERMMODE_TERMIOS
| TERMMODE_DISABLE;
static const int MODIFIER_ALT = 1 << 0;
static const int MODIFIER_LSHIFT = 1 << 1;
static const int MODIFIER_RSHIFT = 1 << 2;
static const int MODIFIER_LCONTROL = 1 << 3;
static const int MODIFIER_RCONTROL = 1 << 4;
static const int SEQUENCE_1IFMOD = 1 << 0;
static const int SEQUENCE_OSHORT = 1 << 1;
struct kbkey_sequence
{
const char* sequence;
int kbkey;
int flags;
};
static const struct kbkey_sequence kbkey_sequences[] =
{
{ "\e[A", KBKEY_UP, SEQUENCE_1IFMOD },
{ "\e[B", KBKEY_DOWN, SEQUENCE_1IFMOD},
{ "\e[C", KBKEY_RIGHT, SEQUENCE_1IFMOD },
{ "\e[D", KBKEY_LEFT, SEQUENCE_1IFMOD },
{ "\e[F", KBKEY_END, SEQUENCE_1IFMOD },
{ "\e[H", KBKEY_HOME, SEQUENCE_1IFMOD },
{ "\e[2~", KBKEY_INSERT, 0 },
{ "\e[3~", KBKEY_DELETE, 0 },
{ "\e[5~", KBKEY_PGUP, 0 },
{ "\e[6~", KBKEY_PGDOWN, 0 },
{ "\e[1P", KBKEY_F1, SEQUENCE_OSHORT },
{ "\e[1Q", KBKEY_F2, SEQUENCE_OSHORT },
{ "\e[1R", KBKEY_F3, SEQUENCE_OSHORT },
{ "\e[1S", KBKEY_F4, SEQUENCE_OSHORT },
{ "\e[15~", KBKEY_F5, 0 },
{ "\e[17~", KBKEY_F6, 0 },
{ "\e[18~", KBKEY_F7, 0 },
{ "\e[19~", KBKEY_F8, 0 },
{ "\e[20~", KBKEY_F9, 0 },
{ "\e[21~", KBKEY_F10, 0 },
{ "\e[23~", KBKEY_F11, 0 },
{ "\e[24~", KBKEY_F12, 0 },
};
static inline bool IsByteUnescaped(unsigned char byte)
{
return (32 <= byte && byte != 127) ||
byte == '\t' || byte == '\n' || byte == '\r';
}
static inline bool IsUTF8Continuation(unsigned char byte)
{
return (byte & 0b11000000) == 0b10000000;
}
static inline const struct kbkey_sequence* LookupKeystrokeSequence(int kbkey)
{
size_t count = sizeof(kbkey_sequences) / sizeof(kbkey_sequences[0]);
for ( size_t i = 0; i < count; i++ )
if ( kbkey_sequences[i].kbkey == kbkey )
return &kbkey_sequences[i];
return NULL;
}
LogTerminal::LogTerminal(dev_t dev, mode_t mode, uid_t owner, gid_t group,
2014-04-22 08:02:04 -04:00
Keyboard* keyboard, KeyboardLayoutExecutor* kblayout)
{
inode_type = INODE_TYPE_TTY;
this->dev = dev;
this->ino = (ino_t) this;
this->type = S_IFCHR;
this->stat_mode = (mode & S_SETABLE) | this->type;
this->stat_uid = owner;
this->stat_gid = group;
this->keyboard = keyboard;
this->kblayout = kblayout;
2016-01-23 14:56:07 -05:00
this->modifiers = 0;
memset(&tio, 0, sizeof(tio));
tio.c_iflag = BRKINT | ICRNL | IXANY | IXON;
tio.c_oflag = OPOST;
tio.c_cflag = CS8 /*| CREAD*/ | HUPCL; // CREAD unset for boot security.
tio.c_lflag = ECHO | ECHOE | ECHOK | ICANON | IEXTEN | ISIG;
tio.c_cc[VEOF] = CONTROL('D');
tio.c_cc[VEOL] = M_CONTROL('?');
tio.c_cc[VERASE] = CONTROL('?');
tio.c_cc[VINTR] = CONTROL('C');
tio.c_cc[VKILL] = CONTROL('U');
tio.c_cc[VMIN] = 1;
tio.c_cc[VQUIT] = CONTROL('\\');
tio.c_cc[VSTART] = CONTROL('Q');
tio.c_cc[VSTOP] = CONTROL('S');
tio.c_cc[VSUSP] = CONTROL('Z');
tio.c_cc[VTIME] = 0;
tio.c_cc[VWERASE] = CONTROL('W');
tio.c_ispeed = B38400;
tio.c_ospeed = B38400;
this->termlock = KTHREAD_MUTEX_INITIALIZER;
this->datacond = KTHREAD_COND_INITIALIZER;
this->numeofs = 0;
2013-06-11 20:18:07 -04:00
this->foreground_pgid = 0;
keyboard->SetOwner(this, NULL);
}
LogTerminal::~LogTerminal()
{
delete keyboard;
delete kblayout;
}
2016-01-23 14:56:07 -05:00
int LogTerminal::settermmode(ioctx_t* /*ctx*/, unsigned int termmode)
{
ScopedLock lock(&termlock);
2016-01-23 14:56:07 -05:00
if ( !RequireForeground(SIGTTOU) )
return errno = EINTR, -1;
if ( termmode & ~SUPPORTED_TERMMODES )
return errno = EINVAL, -1;
tcflag_t old_cflag = tio.c_cflag;
tcflag_t new_cflag = old_cflag;
tcflag_t old_lflag = tio.c_lflag;
tcflag_t new_lflag = old_lflag;
if ( termmode & TERMMODE_KBKEY )
new_lflag |= ISORTIX_KBKEY;
else
new_lflag &= ~ISORTIX_KBKEY;
if ( !(termmode & TERMMODE_UNICODE) )
new_lflag |= ISORTIX_CHARS_DISABLE;
else
new_lflag &= ~ISORTIX_CHARS_DISABLE;
if ( termmode & TERMMODE_SIGNAL )
new_lflag |= ISIG;
else
new_lflag &= ~ISIG;
if ( !(termmode & TERMMODE_UTF8) )
new_lflag |= ISORTIX_32BIT;
else
new_lflag &= ~ISORTIX_32BIT;
if ( termmode & TERMMODE_LINEBUFFER )
new_lflag |= ICANON;
else
new_lflag &= ~ICANON;
if ( termmode & TERMMODE_ECHO )
new_lflag |= ECHO | ECHOE;
else
new_lflag &= ~(ECHO | ECHOE);
if ( termmode & TERMMODE_NONBLOCK )
new_lflag |= ISORTIX_NONBLOCK;
else
new_lflag &= ~ISORTIX_NONBLOCK;
if ( !(termmode & TERMMODE_TERMIOS) )
new_lflag |= ISORTIX_TERMMODE;
else
new_lflag &= ~ISORTIX_TERMMODE;
if ( !(termmode & TERMMODE_DISABLE) )
new_cflag |= CREAD;
else
new_cflag &= ~CREAD;
bool oldnoutf8 = old_lflag & ISORTIX_32BIT;
bool newnoutf8 = new_lflag & ISORTIX_32BIT;
if ( oldnoutf8 != newnoutf8 )
memset(&read_ps, 0, sizeof(read_ps));
tio.c_cflag = new_cflag;
tio.c_lflag = new_lflag;
if ( !(tio.c_lflag & ICANON) )
CommitLineBuffer();
return 0;
}
2016-01-23 14:56:07 -05:00
int LogTerminal::gettermmode(ioctx_t* ctx, unsigned int* mode)
{
ScopedLock lock(&termlock);
2016-01-23 14:56:07 -05:00
unsigned int termmode = 0;
if ( tio.c_lflag & ISORTIX_KBKEY )
termmode |= TERMMODE_KBKEY;
if ( !(tio.c_lflag & ISORTIX_CHARS_DISABLE) )
termmode |= TERMMODE_UNICODE;
if ( tio.c_lflag & ISIG )
termmode |= TERMMODE_SIGNAL;
if ( !(tio.c_lflag & ISORTIX_32BIT) )
termmode |= TERMMODE_UTF8;
if ( tio.c_lflag & ICANON )
termmode |= TERMMODE_LINEBUFFER;
if ( tio.c_lflag & (ECHO | ECHOE) )
termmode |= TERMMODE_ECHO;
if ( tio.c_lflag & ISORTIX_NONBLOCK )
termmode |= TERMMODE_NONBLOCK;
if ( !(tio.c_lflag & ISORTIX_TERMMODE) )
termmode |= TERMMODE_TERMIOS;
if ( !(tio.c_cflag & CREAD) )
termmode |= TERMMODE_DISABLE;
if ( !ctx->copy_to_dest(mode, &termmode, sizeof(termmode)) )
return -1;
return 0;
}
2013-12-20 15:55:05 -05:00
int LogTerminal::tcgetwincurpos(ioctx_t* ctx, struct wincurpos* wcp)
{
2016-01-23 14:56:07 -05:00
ScopedLock lock(&termlock);
2013-12-20 15:55:05 -05:00
struct wincurpos retwcp;
memset(&retwcp, 0, sizeof(retwcp));
size_t cursor_column, cursor_row;
Log::GetCursor(&cursor_column, &cursor_row);
retwcp.wcp_col = cursor_column;
retwcp.wcp_row = cursor_row;
if ( !ctx->copy_to_dest(wcp, &retwcp, sizeof(retwcp)) )
return -1;
return 0;
}
int LogTerminal::tcgetwinsize(ioctx_t* ctx, struct winsize* ws)
{
2016-01-23 14:56:07 -05:00
ScopedLock lock(&termlock);
struct winsize retws;
memset(&retws, 0, sizeof(retws));
retws.ws_col = Log::Width();
retws.ws_row = Log::Height();
if ( !ctx->copy_to_dest(ws, &retws, sizeof(retws)) )
return -1;
return 0;
}
2013-06-11 20:18:07 -04:00
int LogTerminal::tcsetpgrp(ioctx_t* /*ctx*/, pid_t pgid)
{
ScopedLock lock(&termlock);
2016-01-23 14:56:07 -05:00
if ( !RequireForeground(SIGTTOU) )
return errno = EINTR, -1;
if ( pgid <= 0 )
return errno = ESRCH, -1;
Process* process = CurrentProcess()->GetPTable()->Get(pgid);
2016-01-23 14:56:07 -05:00
if ( !process )
2013-06-11 20:18:07 -04:00
return errno = ESRCH, -1;
2016-01-23 14:56:07 -05:00
kthread_mutex_lock(&process->groupparentlock);
2013-06-11 20:18:07 -04:00
bool is_process_group = process->group == process;
2016-01-23 14:56:07 -05:00
kthread_mutex_unlock(&process->groupparentlock);
2013-06-11 20:18:07 -04:00
if ( !is_process_group )
return errno = EINVAL, -1;
foreground_pgid = pgid;
return 0;
}
pid_t LogTerminal::tcgetpgrp(ioctx_t* /*ctx*/)
{
ScopedLock lock(&termlock);
return foreground_pgid;
2013-06-11 20:18:07 -04:00
}
int LogTerminal::sync(ioctx_t* /*ctx*/)
{
2016-01-23 14:56:07 -05:00
ScopedLock lock(&termlock);
return Log::Sync() ? 0 : -1;
}
void LogTerminal::OnKeystroke(Keyboard* kb, void* /*user*/)
{
2016-01-23 14:56:07 -05:00
ScopedLock lock(&termlock);
while ( kb->HasPending() )
2016-01-23 14:56:07 -05:00
{
int kbkey = kb->Read();
if ( kbkey == KBKEY_LALT )
modifiers |= MODIFIER_ALT;
else if ( kbkey == -KBKEY_LALT )
modifiers &= ~MODIFIER_ALT;
else if ( kbkey == KBKEY_LSHIFT )
modifiers |= MODIFIER_LSHIFT;
else if ( kbkey == -KBKEY_LSHIFT )
modifiers &= ~MODIFIER_LSHIFT;
else if ( kbkey == KBKEY_RSHIFT )
modifiers |= MODIFIER_RSHIFT;
else if ( kbkey == -KBKEY_RSHIFT )
modifiers &= ~MODIFIER_RSHIFT;
else if ( kbkey == KBKEY_LCTRL )
modifiers |= MODIFIER_LCONTROL;
else if ( kbkey == -KBKEY_LCTRL )
modifiers &= ~MODIFIER_LCONTROL;
else if ( kbkey == KBKEY_RCTRL )
modifiers |= MODIFIER_RCONTROL;
else if ( kbkey == -KBKEY_RCTRL )
modifiers &= ~MODIFIER_RCONTROL;
uint32_t unicode = kblayout->Translate(kbkey);
if ( !(tio.c_cflag & CREAD) )
continue;
ProcessKeystroke(kbkey);
if ( !unicode )
continue;
if ( unicode == '\n' )
unicode = '\r';
bool control = modifiers & (MODIFIER_LCONTROL | MODIFIER_RCONTROL);
if ( !(tio.c_cflag & ISORTIX_TERMMODE) && unicode == '\b' )
unicode = 127;
if ( control && unicode == L' ' )
ProcessByte(0, unicode);
else if ( control && (L'`' <= unicode && unicode <= L'}') )
ProcessByte(unicode - L'`', unicode);
else if ( control && (L'@' <= unicode && unicode <= L'_') )
ProcessByte(unicode - L'@', unicode);
else if ( control && unicode == L'?' )
ProcessByte(127, unicode);
else
ProcessUnicode(unicode);
}
}
void LogTerminal::ProcessKeystroke(int kbkey)
{
2016-01-23 14:56:07 -05:00
if ( tio.c_lflag & ISORTIX_32BIT )
{
if ( tio.c_lflag & ISORTIX_KBKEY )
{
uint32_t unikbkey = KBKEY_ENCODE(kbkey);
if ( !linebuffer.Push(unikbkey) )
return;
if ( !(tio.c_lflag & ICANON) )
CommitLineBuffer();
}
return;
2016-01-23 14:56:07 -05:00
}
2016-01-23 14:56:07 -05:00
if ( kbkey < 0 )
return;
2016-01-23 14:56:07 -05:00
const struct kbkey_sequence* seq = LookupKeystrokeSequence(kbkey);
if ( !seq )
return;
const char* str = seq->sequence;
size_t len = strlen(str);
int mods = 0;
if ( modifiers & (MODIFIER_LSHIFT | MODIFIER_RSHIFT) )
mods |= 1;
if ( modifiers & MODIFIER_ALT )
mods |= 2;
if ( modifiers & (MODIFIER_LCONTROL | MODIFIER_RCONTROL) )
mods |= 4;
if ( (seq->flags & SEQUENCE_OSHORT) && mods == 0 )
{
ProcessByte('\e');
ProcessByte('O');
ProcessByte((unsigned char) str[len-1]);
return;
}
for ( size_t i = 0; i < len - 1; i++ )
ProcessByte((unsigned char) str[i]);
if ( seq->flags & SEQUENCE_1IFMOD && mods != 0 )
ProcessByte('1');
if ( mods )
{
ProcessByte(';');
ProcessByte('1' + mods);
}
ProcessByte(str[len-1]);
}
void LogTerminal::ProcessString(const char* string)
{
for ( size_t i = 0; string[i]; i++ )
ProcessByte((unsigned char) string[i]);
}
void LogTerminal::ProcessUnicode(uint32_t unicode)
{
mbstate_t ps;
memset(&ps, 0, sizeof(ps));
char mb[MB_CUR_MAX];
size_t amount = wcrtomb(mb, unicode, &ps);
for ( size_t i = 0; amount != (size_t) -1 && i < amount; i++ )
ProcessByte((unsigned char) mb[i]);
}
bool LogTerminal::CheckHandledByte(tcflag_t lflags,
unsigned char key,
unsigned char byte)
{
return (tio.c_lflag & lflags) == lflags && key && key == byte;
}
void LogTerminal::ProcessByte(unsigned char byte, uint32_t control_unicode)
{
if ( byte == '\r' && tio.c_iflag & IGNCR )
return;
if ( byte == '\r' && tio.c_iflag & ICRNL )
byte = '\n';
else if ( byte == '\n' && tio.c_iflag & INLCR )
byte = '\r';
// TODO: tio.c_cc[VEOL]
// TODO: tio.c_cc[VSTART]
// TODO: tio.c_cc[VSTOP]
// TODO: tio.c_cc[VSUSP]
if ( CheckHandledByte(ISIG, tio.c_cc[VQUIT], byte) )
{
while ( linebuffer.CanBackspace() )
linebuffer.Backspace();
if ( Process* process = CurrentProcess()->GetPTable()->Get(foreground_pgid) )
process->DeliverGroupSignal(SIGQUIT);
return;
}
if ( CheckHandledByte(ISIG, tio.c_cc[VINTR], byte) )
{
while ( linebuffer.CanBackspace() )
linebuffer.Backspace();
if ( Process* process = CurrentProcess()->GetPTable()->Get(foreground_pgid) )
process->DeliverGroupSignal(SIGINT);
return;
}
2016-01-23 14:56:07 -05:00
if ( CheckHandledByte(ISIG | ICANON, tio.c_cc[VEOF], byte) )
{
if ( !linebuffer.CanPop() )
{
numeofs++;
2016-01-23 14:56:07 -05:00
kthread_cond_broadcast(&datacond);
2012-12-29 17:09:24 -05:00
poll_channel.Signal(POLLIN | POLLRDNORM);
}
return;
}
2016-01-23 14:56:07 -05:00
if ( CheckHandledByte(ISIG, tio.c_cc[VQUIT], byte) )
{
while ( linebuffer.CanBackspace() )
linebuffer.Backspace();
if ( Process* process = CurrentProcess()->GetPTable()->Get(foreground_pgid) )
process->DeliverGroupSignal(SIGQUIT);
return;
}
if ( CheckHandledByte(ICANON, tio.c_cc[VERASE], byte) ||
CheckHandledByte(ICANON | ISORTIX_TERMMODE, '\b', byte) )
{
while ( linebuffer.CanBackspace() )
{
uint32_t delchar = linebuffer.Backspace();
if ( 256 <= delchar )
continue;
if ( IsUTF8Continuation(delchar) )
continue;
if ( tio.c_lflag & ECHOE )
{
// TODO: Handle tab specially. (Is that even possible without
// knowing cursor position?).
Log::Print("\b \b");
if ( !IsByteUnescaped(delchar) )
Log::Print("\b \b");
}
break;
}
return;
}
if ( CheckHandledByte(ICANON, tio.c_cc[VWERASE], byte) )
2013-05-17 09:30:13 -04:00
{
bool had_non_whitespace = false;
2016-01-23 14:56:07 -05:00
while ( linebuffer.CanBackspace() )
{
uint32_t delchar = linebuffer.WouldBackspace();
if ( 256 <= delchar )
continue;
if ( IsUTF8Continuation(delchar) )
continue;
if ( delchar == L' ' || delchar == L'\t' || delchar == L'\n' )
{
if ( had_non_whitespace )
break;
}
else
had_non_whitespace = true;
linebuffer.Backspace();
if ( tio.c_lflag & ECHOE )
{
Log::Print("\b \b");
2016-01-23 14:56:07 -05:00
if ( !IsByteUnescaped(delchar) )
Log::Print("\b \b");
}
}
return;
2013-05-17 09:30:13 -04:00
}
2016-01-23 14:56:07 -05:00
if ( CheckHandledByte(ICANON, tio.c_cc[VKILL], byte) )
2013-05-17 09:30:13 -04:00
{
while ( linebuffer.CanBackspace() )
{
uint32_t delchar = linebuffer.Backspace();
2016-01-23 14:56:07 -05:00
if ( 256 <= delchar )
continue;
if ( IsUTF8Continuation(delchar) )
continue;
if ( tio.c_lflag & ECHOE )
{
Log::Print("\b \b");
2016-01-23 14:56:07 -05:00
if ( !IsByteUnescaped(delchar) )
Log::Print("\b \b");
}
2013-05-17 09:30:13 -04:00
}
return;
}
2016-01-23 14:56:07 -05:00
if ( CheckHandledByte(ICANON | ISORTIX_TERMMODE, CONTROL('L'), byte) )
2013-05-28 18:35:29 -04:00
{
while ( linebuffer.CanBackspace() )
linebuffer.Backspace();
2016-01-23 14:56:07 -05:00
ProcessUnicode(KBKEY_ENCODE(KBKEY_ENTER));
ProcessByte('\n');
ProcessUnicode(KBKEY_ENCODE(-KBKEY_ENTER));
2013-05-28 18:35:29 -04:00
Log::PrintF("\e[H\e[2J");
return;
}
2016-01-23 14:56:07 -05:00
if ( tio.c_lflag & ISORTIX_CHARS_DISABLE )
return;
2016-01-23 14:56:07 -05:00
if ( control_unicode &&
!(tio.c_lflag & (ICANON | ISIG)) &&
tio.c_lflag & ISORTIX_KBKEY )
{
2016-01-23 14:56:07 -05:00
ProcessUnicode(control_unicode);
return;
}
2016-01-23 14:56:07 -05:00
if ( !linebuffer.Push(byte) )
return;
2016-01-23 14:56:07 -05:00
if ( tio.c_lflag & ECHO )
{
2016-01-23 14:56:07 -05:00
if ( IsByteUnescaped(byte) )
Log::PrintData(&byte, 1);
else
2016-01-23 14:56:07 -05:00
Log::PrintF("^%c", CONTROL(byte));
}
2016-01-23 14:56:07 -05:00
if ( !(tio.c_lflag & ICANON) || byte == '\n' )
CommitLineBuffer();
}
void LogTerminal::CommitLineBuffer()
{
linebuffer.Commit();
2016-01-23 14:56:07 -05:00
if ( linebuffer.CanPop() || numeofs )
{
kthread_cond_broadcast(&datacond);
2016-01-23 14:56:07 -05:00
poll_channel.Signal(POLLIN | POLLRDNORM);
}
}
ssize_t LogTerminal::read(ioctx_t* ctx, uint8_t* userbuf, size_t count)
{
ScopedLockSignal lock(&termlock);
2016-01-23 14:56:07 -05:00
if ( !lock.IsAcquired() )
return errno = EINTR, -1;
if ( !RequireForeground(SIGTTIN) )
return errno = EINTR, -1;
size_t sofar = 0;
size_t left = count;
2016-01-23 14:56:07 -05:00
bool nonblocking = tio.c_lflag & ISORTIX_NONBLOCK ||
ctx->dflags & O_NONBLOCK;
while ( left )
{
2016-01-23 14:56:07 -05:00
while ( !(linebuffer.CanPop() || numeofs) )
{
if ( sofar )
return sofar;
if ( nonblocking )
return errno = EWOULDBLOCK, -1;
if ( !kthread_cond_wait_signal(&datacond, &termlock) )
return errno = EINTR, -1;
if ( !RequireForeground(SIGTTIN) )
return errno = EINTR, -1;
}
if ( numeofs )
{
if ( sofar )
return sofar;
return numeofs--, 0;
}
uint32_t codepoint = linebuffer.Peek();
2016-01-23 14:56:07 -05:00
if ( tio.c_lflag & ISORTIX_32BIT )
{
2016-01-23 14:56:07 -05:00
if ( left < sizeof(codepoint) )
return sofar;
linebuffer.Pop();
if ( 256 <= codepoint && !(tio.c_lflag & ISORTIX_KBKEY) )
continue;
if ( codepoint < 256 && tio.c_lflag & ISORTIX_CHARS_DISABLE )
continue;
if ( codepoint < 256 )
{
2016-01-23 14:56:07 -05:00
char c = codepoint;
wchar_t wc;
size_t amount = mbrtowc(&wc, &c, 1, &read_ps);
if ( amount == (size_t) -2 )
continue;
if ( amount == (size_t) -1 )
{
memset(&read_ps, 0, sizeof(read_ps));
wc = 0xFFFD; /* REPLACEMENT CHARACTER */
}
codepoint = wc;
}
2016-01-23 14:56:07 -05:00
if ( !ctx->copy_to_dest(userbuf + sofar, &codepoint,
sizeof(codepoint)) )
return sofar ? sofar : -1;
left -= sizeof(codepoint);
sofar += sizeof(codepoint);
continue;
}
2016-01-23 14:56:07 -05:00
if ( 256 <= codepoint )
{
2016-01-23 14:56:07 -05:00
linebuffer.Pop();
continue;
}
2016-01-23 14:56:07 -05:00
unsigned char c = codepoint;
if ( !ctx->copy_to_dest(userbuf + sofar, &c, 1) )
return sofar ? sofar : -1;
linebuffer.Pop();
left -= 1;
sofar += 1;
if ( tio.c_lflag & ICANON && c == '\n' )
break;
}
return sofar;
}
ssize_t LogTerminal::write(ioctx_t* ctx, const uint8_t* io_buffer, size_t count)
{
2016-01-23 14:56:07 -05:00
ScopedLockSignal lock(&termlock);
if ( tio.c_lflag & TOSTOP && !RequireForeground(SIGTTOU) )
return errno = EINTR, -1;
// TODO: Add support for ioctx to the kernel log.
const size_t BUFFER_SIZE = 64UL;
char buffer[BUFFER_SIZE];
2016-01-23 14:56:07 -05:00
size_t sofar = 0;
while ( sofar < count )
{
size_t amount = count - sofar;
if ( BUFFER_SIZE < amount )
amount = BUFFER_SIZE;
if ( !ctx->copy_from_src(buffer, io_buffer + sofar, amount) )
return -1;
Log::PrintData(buffer, amount);
sofar += amount;
if ( sofar < count )
{
kthread_mutex_unlock(&termlock);
kthread_yield();
kthread_mutex_lock(&termlock);
if ( Signal::IsPending() )
return sofar;
}
}
return (ssize_t) sofar;
}
2012-12-29 17:09:24 -05:00
short LogTerminal::PollEventStatus()
{
short status = 0;
if ( linebuffer.CanPop() || numeofs )
status |= POLLIN | POLLRDNORM;
if ( true /* can always write */ )
status |= POLLOUT | POLLWRNORM;
return status;
}
int LogTerminal::poll(ioctx_t* /*ctx*/, PollNode* node)
{
ScopedLockSignal lock(&termlock);
short ret_status = PollEventStatus() & node->events;
if ( ret_status )
{
node->master->revents |= ret_status;
2012-12-29 17:09:24 -05:00
return 0;
}
poll_channel.Register(node);
return errno = EAGAIN, -1;
}
2014-05-05 15:36:40 -04:00
ssize_t LogTerminal::tcgetblob(ioctx_t* ctx, const char* name, void* buffer, size_t count)
{
if ( !name )
{
2014-04-22 08:02:04 -04:00
static const char index[] = "kblayout\0";
size_t index_size = sizeof(index) - 1;
if ( buffer && count < index_size )
2014-05-05 15:36:40 -04:00
return errno = ERANGE, -1;
2014-04-22 08:02:04 -04:00
if ( buffer && !ctx->copy_to_dest(buffer, &index, index_size) )
2014-05-05 15:36:40 -04:00
return -1;
2014-04-22 08:02:04 -04:00
return (ssize_t) index_size;
}
else if ( !strcmp(name, "kblayout") )
{
ScopedLockSignal lock(&termlock);
const uint8_t* data;
size_t size;
if ( !kblayout->Download(&data, &size) )
return -1;
if ( buffer && count < size )
return errno = ERANGE, -1;
if ( buffer && !ctx->copy_to_dest(buffer, data, size) )
return -1;
return (ssize_t) size;
2014-05-05 15:36:40 -04:00
}
else
return errno = ENOENT, -1;
}
ssize_t LogTerminal::tcsetblob(ioctx_t* ctx, const char* name, const void* buffer, size_t count)
{
if ( !name )
return errno = EPERM, -1;
2014-04-22 08:02:04 -04:00
else if ( !strcmp(name, "kblayout") )
{
uint8_t* data = new uint8_t[count];
if ( !data )
return -1;
if ( !ctx->copy_from_src(data, buffer, count) )
return -1;
ScopedLockSignal lock(&termlock);
if ( !kblayout->Upload(data, count) )
return -1;
delete[] data;
return (ssize_t) count;
}
2014-05-05 15:36:40 -04:00
else
return errno = ENOENT, -1;
}
2016-01-23 14:56:07 -05:00
int LogTerminal::tcdrain(ioctx_t* /*ctx*/)
{
ScopedLockSignal lock(&termlock);
if ( !RequireForeground(SIGTTOU) )
return errno = EINTR, -1;
return 0;
}
int LogTerminal::tcflow(ioctx_t* /*ctx*/, int action)
{
ScopedLockSignal lock(&termlock);
if ( !RequireForeground(SIGTTOU) )
return errno = EINTR, -1;
switch ( action )
{
case TCOOFF: break; // TODO: Suspend output.
case TCOON: break; // TODO: Resume suspended output.
case TCIOFF: break; // TODO: Transmit STOP character.
case TCION: break; // TODO: Transmit START character.
default: return errno = EINVAL -1;
}
return 0;
}
int LogTerminal::tcflush(ioctx_t* /*ctx*/, int queue_selector)
{
if ( queue_selector & ~TCIOFLUSH )
return errno = EINVAL, -1;
ScopedLockSignal lock(&termlock);
if ( !RequireForeground(SIGTTOU) )
return errno = EINTR, -1;
if ( queue_selector & TCIFLUSH )
linebuffer.Flush();
return 0;
}
int LogTerminal::tcgetattr(ioctx_t* ctx, struct termios* io_tio)
{
ScopedLockSignal lock(&termlock);
if ( !ctx->copy_to_dest(io_tio, &tio, sizeof(tio)) )
return -1;
return 0;
}
pid_t LogTerminal::tcgetsid(ioctx_t* /*ctx*/)
{
// TODO: Implement sessions.
return 1;
}
int LogTerminal::tcsendbreak(ioctx_t* /*ctx*/, int /*duration*/)
{
ScopedLockSignal lock(&termlock);
if ( !RequireForeground(SIGTTOU) )
return errno = EINTR, -1;
return 0;
}
int LogTerminal::tcsetattr(ioctx_t* ctx, int actions, const struct termios* io_tio)
{
ScopedLockSignal lock(&termlock);
if ( !RequireForeground(SIGTTOU) )
return errno = EINTR, -1;
switch ( actions )
{
case TCSANOW: break;
case TCSADRAIN: break;
case TCSAFLUSH: linebuffer.Flush(); break;
default: return errno = EINVAL, -1;
}
if ( !ctx->copy_from_src(&tio, io_tio, sizeof(tio)) )
return -1;
// TODO: Potentially take action here if something changed.
return 0;
}
bool LogTerminal::RequireForeground(int sig)
{
Thread* thread = CurrentThread();
Process* process = thread->process;
ScopedLock group_lock(&process->groupparentlock);
if ( process->group->pid == foreground_pgid )
return true;
if ( sigismember(&thread->signal_mask, sig) )
return true;
ScopedLock signal_lock(&process->signal_lock);
if ( process->signal_actions[sig].sa_handler == SIG_IGN )
return true;
signal_lock.Reset();
group_lock.Reset();
process->group->DeliverGroupSignal(sig);
return false;
}
} // namespace Sortix