2012-08-01 15:40:14 -04:00
|
|
|
/*******************************************************************************
|
2012-02-09 17:55:03 -05:00
|
|
|
|
2013-03-31 11:28:29 -04:00
|
|
|
Copyright(C) Jonas 'Sortie' Termansen 2012, 2013.
|
2012-02-09 17:55:03 -05:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
This file is part of Sortix.
|
2012-02-09 17:55:03 -05:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
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.
|
2012-02-09 17:55:03 -05:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
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.
|
2012-02-09 17:55:03 -05:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
You should have received a copy of the GNU General Public License along with
|
|
|
|
Sortix. If not, see <http://www.gnu.org/licenses/>.
|
2012-02-09 17:55:03 -05:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
logterminal.cpp
|
|
|
|
A simple terminal that writes to the kernel log.
|
2012-02-09 17:55:03 -05:00
|
|
|
|
2012-08-01 15:40:14 -04:00
|
|
|
*******************************************************************************/
|
2012-02-09 17:55:03 -05:00
|
|
|
|
2013-05-17 09:30:13 -04:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
2014-01-03 20:45:01 -05:00
|
|
|
#include <wchar.h>
|
2012-12-29 17:09:24 -05:00
|
|
|
|
2013-03-31 11:28:29 -04:00
|
|
|
#include <sortix/fcntl.h>
|
2012-08-01 15:40:14 -04:00
|
|
|
#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>
|
2012-08-07 18:19:44 -04:00
|
|
|
#include <sortix/stat.h>
|
2013-05-17 09:30:13 -04:00
|
|
|
#include <sortix/termios.h>
|
|
|
|
#include <sortix/termmode.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>
|
2014-03-19 18:34:09 -04:00
|
|
|
#include <sortix/kernel/ptable.h>
|
2013-05-17 09:30:13 -04:00
|
|
|
#include <sortix/kernel/refcount.h>
|
|
|
|
#include <sortix/kernel/scheduler.h>
|
2013-01-09 04:47:22 -05:00
|
|
|
|
2012-02-09 17:55:03 -05:00
|
|
|
#include "logterminal.h"
|
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
namespace Sortix {
|
|
|
|
|
|
|
|
const unsigned SUPPORTED_MODES = TERMMODE_KBKEY
|
|
|
|
| TERMMODE_UNICODE
|
|
|
|
| TERMMODE_SIGNAL
|
|
|
|
| TERMMODE_UTF8
|
|
|
|
| TERMMODE_LINEBUFFER
|
|
|
|
| TERMMODE_ECHO
|
|
|
|
| TERMMODE_NONBLOCK;
|
|
|
|
|
|
|
|
LogTerminal::LogTerminal(dev_t dev, mode_t mode, uid_t owner, gid_t group,
|
|
|
|
Keyboard* keyboard, KeyboardLayout* kblayout)
|
2012-02-09 17:55:03 -05:00
|
|
|
{
|
2012-08-07 18:19:44 -04:00
|
|
|
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;
|
|
|
|
this->partiallywritten = 0;
|
|
|
|
this->control = false;
|
|
|
|
this->termmode = TERMMODE_UNICODE
|
|
|
|
| TERMMODE_SIGNAL
|
|
|
|
| TERMMODE_UTF8
|
|
|
|
| TERMMODE_LINEBUFFER
|
|
|
|
| TERMMODE_ECHO;
|
|
|
|
this->termlock = KTHREAD_MUTEX_INITIALIZER;
|
|
|
|
this->datacond = KTHREAD_COND_INITIALIZER;
|
|
|
|
this->numwaiting = 0;
|
|
|
|
this->numeofs = 0;
|
2013-06-11 20:18:07 -04:00
|
|
|
this->foreground_pgid = 0;
|
2012-08-07 18:19:44 -04:00
|
|
|
keyboard->SetOwner(this, NULL);
|
|
|
|
}
|
2012-02-09 17:55:03 -05:00
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
LogTerminal::~LogTerminal()
|
|
|
|
{
|
|
|
|
delete keyboard;
|
|
|
|
delete kblayout;
|
|
|
|
}
|
2012-02-09 17:55:03 -05:00
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
int LogTerminal::settermmode(ioctx_t* /*ctx*/, unsigned newtermmode)
|
|
|
|
{
|
|
|
|
if ( newtermmode & ~SUPPORTED_MODES ) { errno = EINVAL; return -1; }
|
|
|
|
ScopedLock lock(&termlock);
|
|
|
|
unsigned oldtermmode = termmode;
|
|
|
|
bool oldutf8 = oldtermmode & TERMMODE_UTF8;
|
|
|
|
bool newutf8 = newtermmode & TERMMODE_UTF8;
|
|
|
|
if ( oldutf8 != newutf8 )
|
2012-02-09 17:55:03 -05:00
|
|
|
partiallywritten = 0;
|
2012-08-07 18:19:44 -04:00
|
|
|
termmode = newtermmode;
|
|
|
|
if ( (!newtermmode & TERMMODE_LINEBUFFER) )
|
|
|
|
CommitLineBuffer();
|
|
|
|
partiallywritten = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
2012-02-09 17:55:03 -05:00
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
int LogTerminal::gettermmode(ioctx_t* ctx, unsigned* mode)
|
|
|
|
{
|
|
|
|
ScopedLock lock(&termlock);
|
|
|
|
if ( !ctx->copy_to_dest(mode, &termmode, sizeof(termmode)) )
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
2012-02-09 17:55:03 -05:00
|
|
|
|
2013-12-20 15:55:05 -05:00
|
|
|
int LogTerminal::tcgetwincurpos(ioctx_t* ctx, struct wincurpos* wcp)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
int LogTerminal::tcgetwinsize(ioctx_t* ctx, struct winsize* ws)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
2012-02-09 17:55:03 -05:00
|
|
|
|
2013-06-11 20:18:07 -04:00
|
|
|
int LogTerminal::tcsetpgrp(ioctx_t* /*ctx*/, pid_t pgid)
|
|
|
|
{
|
|
|
|
ScopedLock lock(&termlock);
|
2014-03-19 18:34:09 -04:00
|
|
|
Process* process = CurrentProcess()->GetPTable()->Get(pgid);
|
2013-06-11 20:18:07 -04:00
|
|
|
if ( !pgid || !process )
|
|
|
|
return errno = ESRCH, -1;
|
|
|
|
kthread_mutex_lock(&process->groupchildlock);
|
|
|
|
bool is_process_group = process->group == process;
|
|
|
|
kthread_mutex_unlock(&process->groupchildlock);
|
|
|
|
if ( !is_process_group )
|
|
|
|
return errno = EINVAL, -1;
|
|
|
|
foreground_pgid = pgid;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
pid_t LogTerminal::tcgetpgrp(ioctx_t* /*ctx*/)
|
|
|
|
{
|
|
|
|
ScopedLock lock(&termlock);
|
2014-03-05 17:25:05 -05:00
|
|
|
return foreground_pgid;
|
2013-06-11 20:18:07 -04:00
|
|
|
}
|
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
int LogTerminal::sync(ioctx_t* /*ctx*/)
|
|
|
|
{
|
2012-12-23 15:00:17 -05:00
|
|
|
return Log::Sync() ? 0 : -1;
|
2012-08-07 18:19:44 -04:00
|
|
|
}
|
2012-02-09 17:55:03 -05:00
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
void LogTerminal::OnKeystroke(Keyboard* kb, void* /*user*/)
|
|
|
|
{
|
|
|
|
while ( kb->HasPending() )
|
|
|
|
ProcessKeystroke(kb->Read());
|
|
|
|
}
|
|
|
|
|
|
|
|
void LogTerminal::ProcessKeystroke(int kbkey)
|
|
|
|
{
|
|
|
|
if ( !kbkey )
|
|
|
|
return;
|
|
|
|
|
|
|
|
ScopedLock lock(&termlock);
|
2012-02-09 17:55:03 -05:00
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
if ( kbkey == KBKEY_LCTRL ) { control = true; }
|
|
|
|
if ( kbkey == -KBKEY_LCTRL ) { control = false; }
|
|
|
|
if ( termmode & TERMMODE_SIGNAL && control && kbkey == KBKEY_C )
|
2012-02-09 17:55:03 -05:00
|
|
|
{
|
2012-08-07 18:19:44 -04:00
|
|
|
while ( linebuffer.CanBackspace() )
|
|
|
|
linebuffer.Backspace();
|
2014-03-19 18:34:09 -04:00
|
|
|
if ( Process* process = CurrentProcess()->GetPTable()->Get(foreground_pgid) )
|
2013-08-04 14:24:59 -04:00
|
|
|
process->DeliverGroupSignal(SIGINT);
|
2012-08-07 18:19:44 -04:00
|
|
|
return;
|
2012-02-09 17:55:03 -05:00
|
|
|
}
|
2012-08-07 18:19:44 -04:00
|
|
|
if ( termmode & TERMMODE_SIGNAL && control && kbkey == KBKEY_D )
|
2012-02-09 17:55:03 -05:00
|
|
|
{
|
2012-08-07 18:19:44 -04:00
|
|
|
if ( !linebuffer.CanPop() )
|
2012-02-09 17:55:03 -05:00
|
|
|
{
|
2012-08-07 18:19:44 -04:00
|
|
|
numeofs++;
|
|
|
|
if ( numwaiting )
|
|
|
|
kthread_cond_broadcast(&datacond);
|
2012-12-29 17:09:24 -05:00
|
|
|
poll_channel.Signal(POLLIN | POLLRDNORM);
|
2012-02-09 17:55:03 -05:00
|
|
|
}
|
2012-08-07 18:19:44 -04:00
|
|
|
return;
|
2012-02-09 17:55:03 -05:00
|
|
|
}
|
|
|
|
|
2013-05-17 09:30:13 -04:00
|
|
|
if ( termmode & TERMMODE_LINEBUFFER && control && kbkey == KBKEY_W )
|
|
|
|
{
|
|
|
|
bool had_non_whitespace = false;
|
|
|
|
c_w_delete_more:
|
|
|
|
if ( !linebuffer.CanBackspace() ) { return; }
|
|
|
|
uint32_t delchar = linebuffer.WouldBackspace();
|
|
|
|
bool waskbkey = KBKEY_DECODE(delchar);
|
|
|
|
bool wasuni = !waskbkey;
|
|
|
|
bool whitespace =
|
|
|
|
(wasuni && (delchar == ' ' ||
|
|
|
|
delchar == '\t' ||
|
|
|
|
delchar == '\n')) ||
|
|
|
|
(waskbkey && (abs(KBKEY_DECODE(delchar)) == -KBKEY_SPACE ||
|
|
|
|
abs(KBKEY_DECODE(delchar)) == -KBKEY_TAB ||
|
|
|
|
abs(KBKEY_DECODE(delchar)) == -KBKEY_ENTER));
|
|
|
|
if ( wasuni && whitespace && had_non_whitespace )
|
|
|
|
return;
|
|
|
|
if ( wasuni && !whitespace )
|
|
|
|
had_non_whitespace = true;
|
|
|
|
linebuffer.Backspace();
|
|
|
|
if ( (!waskbkey || termmode & TERMMODE_KBKEY) &&
|
|
|
|
(!wasuni || termmode & TERMMODE_UNICODE) &&
|
|
|
|
termmode & TERMMODE_ECHO &&
|
|
|
|
wasuni )
|
|
|
|
Log::Print("\b");
|
|
|
|
goto c_w_delete_more;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( termmode & TERMMODE_LINEBUFFER && control && kbkey == KBKEY_U )
|
|
|
|
{
|
|
|
|
while ( linebuffer.CanBackspace() )
|
|
|
|
{
|
|
|
|
uint32_t delchar = linebuffer.Backspace();
|
|
|
|
bool waskbkey = KBKEY_DECODE(delchar);
|
|
|
|
bool wasuni = !waskbkey;
|
|
|
|
if ( (!waskbkey || termmode & TERMMODE_KBKEY) &&
|
|
|
|
(!wasuni || termmode & TERMMODE_UNICODE) &&
|
|
|
|
termmode & TERMMODE_ECHO &&
|
|
|
|
wasuni )
|
|
|
|
Log::Print("\b");
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-05-28 18:35:29 -04:00
|
|
|
if ( termmode & TERMMODE_LINEBUFFER && control && kbkey == KBKEY_L )
|
|
|
|
{
|
|
|
|
while ( linebuffer.CanBackspace() )
|
|
|
|
linebuffer.Backspace();
|
|
|
|
QueueUnicode(KBKEY_ENCODE(KBKEY_ENTER));
|
|
|
|
QueueUnicode('\n');
|
|
|
|
QueueUnicode(KBKEY_ENCODE(-KBKEY_ENTER));
|
|
|
|
Log::PrintF("\e[H\e[2J");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
uint32_t unikbkey = KBKEY_ENCODE(kbkey);
|
|
|
|
QueueUnicode(unikbkey);
|
|
|
|
uint32_t unicode = kblayout->Translate(kbkey);
|
|
|
|
if ( 0 < kbkey )
|
|
|
|
QueueUnicode(unicode);
|
|
|
|
}
|
2012-08-01 15:40:14 -04:00
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
void LogTerminal::QueueUnicode(uint32_t unicode)
|
|
|
|
{
|
|
|
|
if ( !unicode )
|
|
|
|
return;
|
|
|
|
|
|
|
|
int kbkey = KBKEY_DECODE(unicode);
|
|
|
|
int abskbkey = (kbkey < 0) ? -kbkey : kbkey;
|
|
|
|
bool wasenter = kbkey == KBKEY_ENTER || unicode == '\n';
|
|
|
|
bool kbkeymode = termmode & TERMMODE_KBKEY;
|
|
|
|
bool unicodemode = termmode && TERMMODE_UNICODE;
|
|
|
|
bool linemode = termmode & TERMMODE_LINEBUFFER;
|
|
|
|
bool echomode = termmode & TERMMODE_ECHO;
|
|
|
|
|
|
|
|
if ( linemode && abskbkey == KBKEY_BKSPC ) { return; }
|
|
|
|
while ( linemode && unicode == '\b' )
|
|
|
|
{
|
|
|
|
if ( !linebuffer.CanBackspace() ) { return; }
|
|
|
|
uint32_t delchar = linebuffer.Backspace();
|
|
|
|
bool waskbkey = KBKEY_DECODE(delchar);
|
|
|
|
bool wasuni = !waskbkey;
|
|
|
|
if ( waskbkey && !kbkeymode ) { continue; }
|
|
|
|
if ( wasuni && !unicodemode ) { continue; }
|
|
|
|
if ( !echomode ) { return; }
|
|
|
|
if ( wasuni ) { Log::Print("\b"); }
|
|
|
|
return;
|
|
|
|
}
|
2012-02-09 17:55:03 -05:00
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
if ( !linebuffer.Push(unicode) )
|
|
|
|
{
|
|
|
|
Log::PrintF("Warning: LogTerminal driver dropping keystroke due "
|
|
|
|
"to insufficient buffer space\n");
|
|
|
|
return;
|
2012-02-09 17:55:03 -05:00
|
|
|
}
|
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
// TODO: Could it be the right thing to print the unicode character even
|
|
|
|
// if it is unprintable (it's an encoded keystroke)?
|
|
|
|
if ( !KBKEY_DECODE(unicode) && echomode )
|
2012-02-09 17:55:03 -05:00
|
|
|
{
|
2014-01-03 20:45:01 -05:00
|
|
|
mbstate_t ps;
|
|
|
|
memset(&ps, 0, sizeof(ps));
|
|
|
|
char utf8buf[MB_CUR_MAX];
|
|
|
|
size_t num_bytes = wcrtomb(utf8buf, (wchar_t) unicode, &ps);
|
|
|
|
Log::PrintData(utf8buf, num_bytes);
|
2012-08-07 18:19:44 -04:00
|
|
|
}
|
2012-02-09 17:55:03 -05:00
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
bool commit = !linemode || wasenter;
|
|
|
|
if ( commit )
|
|
|
|
CommitLineBuffer();
|
|
|
|
}
|
2012-02-09 17:55:03 -05:00
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
void LogTerminal::CommitLineBuffer()
|
|
|
|
{
|
|
|
|
linebuffer.Commit();
|
|
|
|
if ( numwaiting )
|
|
|
|
kthread_cond_broadcast(&datacond);
|
2012-12-29 17:09:24 -05:00
|
|
|
poll_channel.Signal(POLLIN | POLLRDNORM);
|
2012-08-07 18:19:44 -04:00
|
|
|
}
|
2012-02-09 17:55:03 -05:00
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
ssize_t LogTerminal::read(ioctx_t* ctx, uint8_t* userbuf, size_t count)
|
|
|
|
{
|
|
|
|
ScopedLockSignal lock(&termlock);
|
|
|
|
if ( !lock.IsAcquired() ) { errno = EINTR; return -1; }
|
|
|
|
size_t sofar = 0;
|
|
|
|
size_t left = count;
|
|
|
|
bool blocking = !(termmode & TERMMODE_NONBLOCK);
|
|
|
|
while ( left && !linebuffer.CanPop() && blocking && !numeofs )
|
|
|
|
{
|
2013-03-31 11:28:29 -04:00
|
|
|
if ( ctx->dflags & O_NONBLOCK )
|
|
|
|
return errno = EWOULDBLOCK, -1;
|
2012-08-07 18:19:44 -04:00
|
|
|
numwaiting++;
|
|
|
|
bool abort = !kthread_cond_wait_signal(&datacond, &termlock);
|
|
|
|
numwaiting--;
|
|
|
|
if ( abort ) { errno = EINTR; return -1; }
|
2012-02-09 17:55:03 -05:00
|
|
|
}
|
2012-08-07 18:19:44 -04:00
|
|
|
if ( left && !linebuffer.CanPop() && !blocking && !numeofs )
|
2012-02-09 17:55:03 -05:00
|
|
|
{
|
2012-08-07 18:19:44 -04:00
|
|
|
errno = EWOULDBLOCK;
|
|
|
|
return -1;
|
2012-02-09 17:55:03 -05:00
|
|
|
}
|
2012-08-07 18:19:44 -04:00
|
|
|
if ( numeofs )
|
2012-02-09 17:55:03 -05:00
|
|
|
{
|
2012-08-07 18:19:44 -04:00
|
|
|
numeofs--;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
while ( left && linebuffer.CanPop() )
|
|
|
|
{
|
|
|
|
uint32_t codepoint = linebuffer.Peek();
|
|
|
|
int kbkey = KBKEY_DECODE(codepoint);
|
2012-02-09 17:55:03 -05:00
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
bool ignore = false;
|
|
|
|
if ( !(termmode & TERMMODE_KBKEY) && kbkey ) { ignore = true; }
|
|
|
|
if ( !(termmode & TERMMODE_UNICODE) && !kbkey ) { ignore = true; }
|
|
|
|
if ( ignore ) { linebuffer.Pop(); continue; }
|
2012-02-09 17:55:03 -05:00
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
uint8_t* buf;
|
|
|
|
size_t bufsize;
|
|
|
|
uint8_t codepointbuf[4];
|
2014-01-03 20:45:01 -05:00
|
|
|
char utf8buf[MB_CUR_MAX];
|
2012-02-09 17:55:03 -05:00
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
if ( termmode & TERMMODE_UTF8 )
|
|
|
|
{
|
2014-01-03 20:45:01 -05:00
|
|
|
mbstate_t ps;
|
|
|
|
memset(&ps, 0, sizeof(ps));
|
|
|
|
size_t num_bytes = wcrtomb(utf8buf, (wchar_t) codepoint, &ps);
|
|
|
|
if ( num_bytes == (size_t) -1)
|
2012-02-09 17:55:03 -05:00
|
|
|
{
|
2012-08-07 18:19:44 -04:00
|
|
|
Log::PrintF("Warning: logterminal driver dropping invalid "
|
|
|
|
"codepoint 0x%x\n", codepoint);
|
2014-01-03 20:45:01 -05:00
|
|
|
num_bytes = 0;
|
2012-02-09 17:55:03 -05:00
|
|
|
}
|
2012-08-07 18:19:44 -04:00
|
|
|
buf = (uint8_t*) utf8buf;
|
2014-01-03 20:45:01 -05:00
|
|
|
bufsize = num_bytes;
|
2012-02-09 17:55:03 -05:00
|
|
|
}
|
2012-08-07 18:19:44 -04:00
|
|
|
else
|
2012-02-09 17:55:03 -05:00
|
|
|
{
|
2012-08-07 18:19:44 -04:00
|
|
|
codepointbuf[0] = (codepoint >> 24U) & 0xFFU;
|
|
|
|
codepointbuf[1] = (codepoint >> 16U) & 0xFFU;
|
|
|
|
codepointbuf[2] = (codepoint >> 8U) & 0xFFU;
|
|
|
|
codepointbuf[3] = (codepoint >> 0U) & 0xFFU;
|
|
|
|
buf = (uint8_t*) &codepointbuf;
|
|
|
|
bufsize = sizeof(codepointbuf);
|
|
|
|
// TODO: Whoops, the above is big-endian and the user programs
|
|
|
|
// expect the data to be in the host endian. That's bad. For now
|
|
|
|
// just send the data in host endian, but this will break when
|
|
|
|
// terminals are accessed over the network.
|
|
|
|
buf = (uint8_t*) &codepoint;
|
2012-02-09 17:55:03 -05:00
|
|
|
}
|
2012-08-07 18:19:44 -04:00
|
|
|
if ( bufsize < partiallywritten ) { partiallywritten = bufsize; }
|
|
|
|
buf += partiallywritten;
|
|
|
|
bufsize -= partiallywritten;
|
|
|
|
if ( sofar && left < bufsize ) { return sofar; }
|
|
|
|
size_t amount = left < bufsize ? left : bufsize;
|
|
|
|
ctx->copy_to_dest(userbuf + sofar, buf, amount);
|
|
|
|
partiallywritten = (amount < bufsize) ? partiallywritten + amount : 0;
|
|
|
|
left -= amount;
|
|
|
|
sofar += amount;
|
|
|
|
if ( !partiallywritten ) { linebuffer.Pop(); }
|
2012-02-09 17:55:03 -05:00
|
|
|
}
|
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
return sofar;
|
|
|
|
}
|
2012-02-09 17:55:03 -05:00
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
ssize_t LogTerminal::write(ioctx_t* ctx, const uint8_t* buf, size_t count)
|
|
|
|
{
|
|
|
|
// TODO: Add support for ioctx to the kernel log.
|
|
|
|
const size_t BUFFER_SIZE = 64UL;
|
|
|
|
if ( BUFFER_SIZE < count )
|
|
|
|
count = BUFFER_SIZE;
|
|
|
|
char buffer[BUFFER_SIZE];
|
|
|
|
if ( !ctx->copy_from_src(buffer, buf, count) )
|
|
|
|
return -1;
|
|
|
|
Log::PrintData(buf, count);
|
|
|
|
return count;
|
2012-02-09 17:55:03 -05:00
|
|
|
}
|
2012-08-07 18:19:44 -04:00
|
|
|
|
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 )
|
|
|
|
{
|
2014-04-29 16:16:07 -04:00
|
|
|
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 )
|
|
|
|
{
|
|
|
|
char index[] = "";
|
|
|
|
if ( buffer && count < sizeof(index) )
|
|
|
|
return errno = ERANGE, -1;
|
|
|
|
if ( buffer && !ctx->copy_to_dest(buffer, &index, sizeof(index)) )
|
|
|
|
return -1;
|
|
|
|
return (ssize_t) sizeof(index);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return errno = ENOENT, -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t LogTerminal::tcsetblob(ioctx_t* ctx, const char* name, const void* buffer, size_t count)
|
|
|
|
{
|
|
|
|
(void) ctx;
|
|
|
|
(void) buffer;
|
|
|
|
(void) count;
|
|
|
|
if ( !name )
|
|
|
|
return errno = EPERM, -1;
|
|
|
|
else
|
|
|
|
return errno = ENOENT, -1;
|
|
|
|
}
|
2012-08-07 18:19:44 -04:00
|
|
|
} // namespace Sortix
|