Implement signals, finally! #30

Merged
apio merged 14 commits from finally-signals into main 2023-07-10 20:16:00 +00:00
2 changed files with 37 additions and 6 deletions
Showing only changes of commit 60d68b74e1 - Show all commits

View File

@ -4,6 +4,7 @@
#include "thread/Scheduler.h" #include "thread/Scheduler.h"
#include <bits/atfile.h> #include <bits/atfile.h>
#include <bits/open-flags.h> #include <bits/open-flags.h>
#include <bits/signal.h>
#include <bits/waitpid.h> #include <bits/waitpid.h>
#include <luna/Alloc.h> #include <luna/Alloc.h>
#include <luna/Atomic.h> #include <luna/Atomic.h>
@ -106,26 +107,40 @@ enum class DefaultSignalAction
}; };
static constexpr DefaultSignalAction default_actions[] = { static constexpr DefaultSignalAction default_actions[] = {
DefaultSignalAction::Terminate // SIGABRT DefaultSignalAction::Terminate, // SIGHUP
DefaultSignalAction::Terminate, // SIGINT
DefaultSignalAction::Terminate, // SIGQUIT (dump core)
DefaultSignalAction::Terminate, // SIGILL (dump core)
DefaultSignalAction::Terminate, // SIGTRAP (dump core)
DefaultSignalAction::Terminate, // SIGABRT (dump core)
DefaultSignalAction::Ignore, // SIGCHLD
DefaultSignalAction::Terminate, // SIGFPE (dump core)
DefaultSignalAction::Terminate, // SIGKILL
DefaultSignalAction::Terminate, // SIGSTOP (FIXME: Support stopping and continuing)
DefaultSignalAction::Terminate, // SIGSEGV (dump core)
DefaultSignalAction::Ignore, // SIGCONT (FIXME: Support stopping and continuing)
DefaultSignalAction::Terminate, // SIGPIPE
DefaultSignalAction::Terminate, // SIGALRM
DefaultSignalAction::Terminate, // SIGTERM
}; };
void Thread::process_pending_signals(Registers* current_regs) void Thread::process_pending_signals(Registers* current_regs)
{ {
for (int i = 0; i < NSIG; i++) for (int i = 0; i < NSIG; i++)
{ {
if (signal_mask & (1 << i)) continue; int signo = i + 1;
if (signo != SIGKILL && signo != SIGSTOP && signal_mask & (1 << i)) continue;
if (pending_signals & (1 << i)) if (pending_signals & (1 << i))
{ {
int signo = i + 1;
pending_signals &= ~(1 << i); pending_signals &= ~(1 << i);
kinfoln("signal: executing signal %d for thread %ld", signo, id); kinfoln("signal: executing signal %d for thread %ld", signo, id);
auto handler = signal_handlers[i]; auto handler = signal_handlers[i];
if (handler.sa_handler == SIG_IGN) if (signo != SIGKILL && signo != SIGSTOP && handler.sa_handler == SIG_IGN)
{ {
kinfoln("signal: ignoring signal (handler=SIG_IGN)"); kinfoln("signal: ignoring signal (handler=SIG_IGN)");
return; return;
} }
if (handler.sa_handler == SIG_DFL) if (handler.sa_handler == SIG_DFL || signo == SIGKILL || signo == SIGSTOP)
{ {
default_signal: default_signal:
if (id == 1) if (id == 1)

View File

@ -18,9 +18,25 @@ struct sigaction
void* __sa_sigreturn = nullptr; void* __sa_sigreturn = nullptr;
}; };
// The signals with explicit numbers have portable signal numbers.
enum __signals enum __signals
{ {
SIGABRT = 1, SIGHUP = 1,
SIGINT = 2,
SIGQUIT = 3,
SIGILL = 4,
SIGTRAP = 5,
SIGABRT = 6,
SIGCHLD,
SIGFPE = 8,
SIGKILL = 9,
SIGSTOP,
SIGSEGV = 11,
SIGCONT,
SIGPIPE = 13,
SIGALRM = 14,
SIGTERM = 15,
// FIXME: Add the remaining signals.
__NSIG, __NSIG,
}; };