mirror of
https://github.com/Mathewnd/Astral.git
synced 2026-10-28 13:18:22 +00:00
initial signal implementation
This commit is contained in:
@@ -284,3 +284,9 @@ void jobctl_procremove(proc_t *proc) {
|
||||
tty_t *tty = proc->session.controllingtty;
|
||||
VOP_RELEASE(tty->mastervnode);
|
||||
}
|
||||
|
||||
// proc can be null if the signal is being sent to the calling processes own process group
|
||||
// since that is only passed from a userland thread, we can assume _cpu()->thread->proc is not null
|
||||
int jobctl_signal(proc_t *proc, int signal) {
|
||||
|
||||
}
|
||||
|
||||
@@ -29,10 +29,21 @@ typedef struct {
|
||||
static rqueue_t runqueue[RUNQUEUE_COUNT];
|
||||
static uint64_t runqueuebitmap;
|
||||
static spinlock_t runqueuelock;
|
||||
static hashtable_t pidtable;
|
||||
// not defined as static because its acquired/released from a macro in kernel/scheduler.h
|
||||
mutex_t sched_pidtablemutex;
|
||||
|
||||
proc_t *sched_initproc;
|
||||
static pid_t currpid = 1;
|
||||
|
||||
proc_t *sched_getprocfrompid(int pid) {
|
||||
MUTEX_ACQUIRE(&sched_pidtablemutex, false);
|
||||
void *proc = NULL;
|
||||
hashtable_get(&pidtable, &proc, &pid, sizeof(pid));
|
||||
MUTEX_RELEASE(&sched_pidtablemutex);
|
||||
return proc;
|
||||
}
|
||||
|
||||
proc_t *sched_newproc() {
|
||||
proc_t *proc = slab_allocate(processcache);
|
||||
if (proc == NULL)
|
||||
@@ -50,7 +61,7 @@ proc_t *sched_newproc() {
|
||||
proc->state = SCHED_PROC_STATE_NORMAL;
|
||||
proc->threadtablesize = 1;
|
||||
proc->runningthreadcount = 1;
|
||||
SPINLOCK_INIT(proc->lock);
|
||||
MUTEX_INIT(&proc->mutex);
|
||||
SPINLOCK_INIT(proc->nodeslock);
|
||||
proc->fdcount = 3;
|
||||
proc->refcount = 1;
|
||||
@@ -59,6 +70,7 @@ proc_t *sched_newproc() {
|
||||
SEMAPHORE_INIT(&proc->waitsem, 0);
|
||||
SPINLOCK_INIT(proc->jobctllock);
|
||||
SPINLOCK_INIT(proc->pgrp.lock);
|
||||
SPINLOCK_INIT(proc->signals.lock);
|
||||
|
||||
proc->fd = alloc(sizeof(fd_t) * 3);
|
||||
if (proc->fd == NULL) {
|
||||
@@ -70,6 +82,17 @@ proc_t *sched_newproc() {
|
||||
proc->pid = __atomic_fetch_add(&currpid, 1, __ATOMIC_SEQ_CST);
|
||||
proc->umask = 022; // default umask
|
||||
|
||||
// add to pid table
|
||||
MUTEX_ACQUIRE(&sched_pidtablemutex, false);
|
||||
if (hashtable_set(&pidtable, proc, &proc->pid, sizeof(proc->pid), true)) {
|
||||
MUTEX_RELEASE(&sched_pidtablemutex);
|
||||
free(proc->fd);
|
||||
free(proc->threads);
|
||||
slab_free(processcache, proc);
|
||||
return NULL;
|
||||
}
|
||||
MUTEX_RELEASE(&sched_pidtablemutex);
|
||||
|
||||
return proc;
|
||||
}
|
||||
|
||||
@@ -105,6 +128,7 @@ thread_t *sched_newthread(void *ip, size_t kstacksize, int priority, proc_t *pro
|
||||
CTX_SP(&thread->context) = proc ? (ctxreg_t)ustack : (ctxreg_t)thread->kernelstacktop;
|
||||
CTX_IP(&thread->context) = (ctxreg_t)ip;
|
||||
SPINLOCK_INIT(thread->sleeplock);
|
||||
SPINLOCK_INIT(thread->signals.lock);
|
||||
|
||||
return thread;
|
||||
}
|
||||
@@ -241,6 +265,7 @@ __attribute__((noreturn)) void sched_stopcurrentthread() {
|
||||
// called when all threads in a process have exited
|
||||
void sched_procexit() {
|
||||
proc_t *proc = _cpu()->thread->proc;
|
||||
__assert(proc != sched_initproc);
|
||||
|
||||
// the process is going to be referenced by the child list, hold it
|
||||
// it only holds it now because it has to have running threads to be in the list
|
||||
@@ -254,7 +279,7 @@ void sched_procexit() {
|
||||
fd_close(fd);
|
||||
|
||||
// zombify the proc
|
||||
spinlock_acquire(&proc->lock);
|
||||
MUTEX_ACQUIRE(&proc->mutex, false);
|
||||
|
||||
VOP_RELEASE(proc->root);
|
||||
VOP_RELEASE(proc->cwd);
|
||||
@@ -263,7 +288,7 @@ void sched_procexit() {
|
||||
proc_t *lastchild = proc->child;
|
||||
|
||||
|
||||
spinlock_acquire(&sched_initproc->lock);
|
||||
MUTEX_ACQUIRE(&sched_initproc->mutex, false);
|
||||
|
||||
bool haszombie = false;
|
||||
|
||||
@@ -280,8 +305,8 @@ void sched_procexit() {
|
||||
sched_initproc->child = proc->child;
|
||||
}
|
||||
|
||||
spinlock_release(&sched_initproc->lock);
|
||||
spinlock_release(&proc->lock);
|
||||
MUTEX_RELEASE(&sched_initproc->mutex);
|
||||
MUTEX_RELEASE(&proc->mutex);
|
||||
|
||||
semaphore_signal(&proc->parent->waitsem);
|
||||
if (haszombie)
|
||||
@@ -290,9 +315,10 @@ void sched_procexit() {
|
||||
|
||||
void sched_inactiveproc(proc_t *proc) {
|
||||
// proc refcount 0, we can free the structure
|
||||
// TODO remove from the pid list
|
||||
// the pid table lock is already acquired.
|
||||
|
||||
hashtable_remove(&pidtable, &proc->pid, sizeof(proc->pid));
|
||||
|
||||
//printf("destroying proc!\n");
|
||||
sched_destroyproc(proc);
|
||||
}
|
||||
|
||||
@@ -346,12 +372,14 @@ __attribute__((no_caller_saved_registers)) void sched_userspacecheck(context_t *
|
||||
interrupt_set(true);
|
||||
sched_threadexit();
|
||||
}
|
||||
|
||||
signal_check(thread, context);
|
||||
}
|
||||
|
||||
void sched_stopotherthreads() {
|
||||
thread_t *thread = _cpu()->thread;
|
||||
proc_t *proc = thread->proc;
|
||||
spinlock_acquire(&proc->lock);
|
||||
MUTEX_ACQUIRE(&proc->mutex, false);
|
||||
proc->nomorethreads = true;
|
||||
|
||||
for (int i = 0; i < proc->threadtablesize; ++i) {
|
||||
@@ -363,7 +391,7 @@ void sched_stopotherthreads() {
|
||||
proc->threads[i] = NULL;
|
||||
}
|
||||
|
||||
spinlock_release(&proc->lock);
|
||||
MUTEX_RELEASE(&proc->mutex);
|
||||
|
||||
while (__atomic_load_n(&proc->runningthreadcount, __ATOMIC_SEQ_CST) > 1) sched_yield();
|
||||
|
||||
@@ -563,6 +591,9 @@ void sched_init() {
|
||||
processcache = slab_newcache(sizeof(proc_t), 0, NULL, NULL);
|
||||
__assert(processcache);
|
||||
|
||||
__assert(hashtable_init(&pidtable, 100) == 0);
|
||||
MUTEX_INIT(&sched_pidtablemutex);
|
||||
|
||||
_cpu()->schedulerstack = vmm_map(NULL, SCHEDULER_STACK_SIZE, VMM_FLAGS_ALLOCATE, ARCH_MMU_FLAGS_READ | ARCH_MMU_FLAGS_WRITE | ARCH_MMU_FLAGS_NOEXEC, NULL);
|
||||
__assert(_cpu()->schedulerstack);
|
||||
_cpu()->schedulerstack = (void *)((uintptr_t)_cpu()->schedulerstack + SCHEDULER_STACK_SIZE);
|
||||
@@ -658,6 +689,8 @@ void sched_runinit() {
|
||||
thread_t *uthread = sched_newthread(entry, PAGE_SIZE * 16, 1, proc, stack);
|
||||
__assert(uthread);
|
||||
|
||||
proc->threads[0] = uthread;
|
||||
|
||||
uthread->vmmctx = vmmctx;
|
||||
sched_queue(uthread);
|
||||
|
||||
|
||||
@@ -0,0 +1,311 @@
|
||||
#include <kernel/scheduler.h>
|
||||
#include <arch/signal.h>
|
||||
#include <logging.h>
|
||||
|
||||
#define PROCESS_ENTER(proc) \
|
||||
bool procintstatus = interrupt_set(false); \
|
||||
spinlock_acquire(&proc->signals.lock);
|
||||
|
||||
#define PROCESS_LEAVE(proc) \
|
||||
spinlock_release(&proc->signals.lock); \
|
||||
interrupt_set(procintstatus);
|
||||
|
||||
#define THREAD_ENTER(thread) \
|
||||
bool threadintstatus = interrupt_set(false); \
|
||||
spinlock_acquire(&thread->signals.lock);
|
||||
|
||||
#define THREAD_LEAVE(thread) \
|
||||
spinlock_release(&thread->signals.lock); \
|
||||
interrupt_set(threadintstatus);
|
||||
|
||||
#define SIGNAL_ASSERT(s) __assert((s) < NSIG && (s) >= 0)
|
||||
|
||||
void signal_action(struct proc_t *proc, int signal, sigaction_t *new, sigaction_t *old) {
|
||||
SIGNAL_ASSERT(signal);
|
||||
PROCESS_ENTER(proc);
|
||||
|
||||
if (old)
|
||||
*old = proc->signals.actions[signal];
|
||||
|
||||
if (new)
|
||||
proc->signals.actions[signal] = *new;
|
||||
|
||||
PROCESS_LEAVE(proc);
|
||||
}
|
||||
|
||||
void signal_altstack(struct thread_t *thread, stack_t *new, stack_t *old) {
|
||||
THREAD_ENTER(thread);
|
||||
|
||||
if (old)
|
||||
*old = thread->signals.stack;
|
||||
|
||||
if (new)
|
||||
thread->signals.stack = *new;
|
||||
|
||||
THREAD_LEAVE(thread);
|
||||
}
|
||||
|
||||
void signal_changemask(struct thread_t *thread, int how, sigset_t *new, sigset_t *old) {
|
||||
THREAD_ENTER(thread);
|
||||
|
||||
if (old)
|
||||
*old = thread->signals.mask;
|
||||
|
||||
if (new) {
|
||||
switch (how) {
|
||||
case SIG_BLOCK:
|
||||
case SIG_UNBLOCK: {
|
||||
for (int i = 0; i < NSIG; ++i) {
|
||||
if (SIGNAL_GET(new, i) == 0)
|
||||
continue;
|
||||
|
||||
if (how == SIG_BLOCK && SIGNAL_GET(&thread->signals.mask, i) == 0)
|
||||
SIGNAL_SETON(&thread->signals.mask, i);
|
||||
else if (how == SIG_UNBLOCK && SIGNAL_GET(&thread->signals.mask, i))
|
||||
SIGNAL_SETOFF(&thread->signals.mask, i);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case SIG_SETMASK: {
|
||||
thread->signals.mask = *new;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
__assert(!"bad how");
|
||||
}
|
||||
}
|
||||
|
||||
THREAD_LEAVE(thread);
|
||||
}
|
||||
|
||||
void signal_pending(struct thread_t *thread, sigset_t *sigset) {
|
||||
proc_t *proc = thread->proc;
|
||||
PROCESS_ENTER(proc);
|
||||
THREAD_ENTER(thread);
|
||||
|
||||
// urgent set doesn't get returned here as it will always be handled
|
||||
// before a return to userspace
|
||||
memset(sigset, 0, sizeof(sigset_t));
|
||||
for (int i = 0; i < NSIG; ++i) {
|
||||
if (SIGNAL_GET(&proc->signals.pending, i))
|
||||
SIGNAL_SETON(sigset, i);
|
||||
|
||||
if (SIGNAL_GET(&thread->signals.pending, i))
|
||||
SIGNAL_SETON(sigset, i);
|
||||
}
|
||||
|
||||
THREAD_LEAVE(thread);
|
||||
PROCESS_LEAVE(proc);
|
||||
}
|
||||
|
||||
void signal_signalthread(struct thread_t *thread, int signal, bool urgent) {
|
||||
THREAD_ENTER(thread);
|
||||
|
||||
sigset_t *sigset = urgent ? &thread->signals.urgent : &thread->signals.pending;
|
||||
SIGNAL_SETON(sigset, signal);
|
||||
if (signal == SIGKILL || signal == SIGSTOP || urgent || SIGNAL_GET(&thread->signals.mask, signal) == 0)
|
||||
sched_wakeup(thread, SCHED_WAKEUP_REASON_INTERRUPTED);
|
||||
|
||||
THREAD_LEAVE(thread);
|
||||
}
|
||||
|
||||
void signal_signalproc(struct proc_t *proc, int signal) {
|
||||
// first, check if any threads have the signal unmasked, and set as pending for the first one
|
||||
MUTEX_ACQUIRE(&proc->mutex, false);
|
||||
PROCESS_ENTER(proc);
|
||||
for (int i = 0; i < proc->threadtablesize; ++i) {
|
||||
if (proc->threads[i] == NULL || (proc->threads[i]->flags & SCHED_THREAD_FLAGS_DEAD))
|
||||
continue;
|
||||
|
||||
THREAD_ENTER(proc->threads[i]);
|
||||
|
||||
// sigcont will wake up all the threads or unset sigstop as pending
|
||||
if (signal == SIGCONT) {
|
||||
// TODO this
|
||||
} else if (SIGNAL_GET(&proc->threads[i]->signals.mask, signal) == 0 || signal == SIGKILL || signal == SIGSTOP) {
|
||||
SIGNAL_SETON(&proc->threads[i]->signals.pending, signal);
|
||||
// TODO ipi if running isn't self
|
||||
sched_wakeup(proc->threads[i], SCHED_WAKEUP_REASON_INTERRUPTED);
|
||||
THREAD_LEAVE(proc->threads[i]);
|
||||
PROCESS_LEAVE(proc);
|
||||
MUTEX_RELEASE(&proc->mutex);
|
||||
return;
|
||||
}
|
||||
|
||||
THREAD_LEAVE(proc->threads[i]);
|
||||
}
|
||||
MUTEX_RELEASE(&proc->mutex);
|
||||
|
||||
// there wasn't any thread with it unmasked, so set it as pending for the whole process
|
||||
|
||||
SIGNAL_SETON(&proc->signals.pending, signal);
|
||||
|
||||
PROCESS_LEAVE(proc);
|
||||
}
|
||||
|
||||
#define ACTION_TERM 0
|
||||
#define ACTION_IGN 1
|
||||
#define ACTION_CORE 2
|
||||
#define ACTION_STOP 3
|
||||
#define ACTION_CONT 4
|
||||
|
||||
static int defaultactions[NSIG] = {
|
||||
[SIGABRT] = ACTION_CORE,
|
||||
[SIGALRM] = ACTION_TERM,
|
||||
[SIGBUS] = ACTION_CORE,
|
||||
[SIGCHLD] = ACTION_IGN,
|
||||
[SIGCONT] = ACTION_CONT,
|
||||
[SIGFPE] = ACTION_CORE,
|
||||
[SIGHUP] = ACTION_TERM,
|
||||
[SIGILL] = ACTION_CORE,
|
||||
[SIGINT] = ACTION_TERM,
|
||||
[SIGIO] = ACTION_TERM,
|
||||
[SIGKILL] = ACTION_TERM,
|
||||
[SIGPIPE] = ACTION_TERM,
|
||||
[SIGPOLL] = ACTION_TERM,
|
||||
[SIGPROF] = ACTION_TERM,
|
||||
[SIGPWR] = ACTION_TERM,
|
||||
[SIGQUIT] = ACTION_CORE,
|
||||
[SIGSEGV] = ACTION_CORE,
|
||||
[SIGSTOP] = ACTION_STOP,
|
||||
[SIGTSTP] = ACTION_STOP,
|
||||
[SIGSYS] = ACTION_CORE,
|
||||
[SIGTERM] = ACTION_TERM,
|
||||
[SIGTRAP] = ACTION_CORE,
|
||||
[SIGTTIN] = ACTION_STOP,
|
||||
[SIGTTOU] = ACTION_STOP,
|
||||
[SIGURG] = ACTION_IGN,
|
||||
[SIGUSR1] = ACTION_TERM,
|
||||
[SIGUSR2] = ACTION_TERM,
|
||||
[SIGVTALRM] = ACTION_TERM,
|
||||
[SIGXCPU] = ACTION_CORE,
|
||||
[SIGXFSZ] = ACTION_CORE,
|
||||
[SIGWINCH] = ACTION_IGN
|
||||
};
|
||||
|
||||
void signal_check(struct thread_t *thread, context_t *context) {
|
||||
proc_t *proc = thread->proc;
|
||||
|
||||
PROCESS_ENTER(proc);
|
||||
THREAD_ENTER(thread);
|
||||
|
||||
if (SIGNAL_GET(&proc->signals.pending, SIGKILL) || SIGNAL_GET(&thread->signals.pending, SIGKILL)) {
|
||||
THREAD_LEAVE(thread);
|
||||
PROCESS_LEAVE(proc);
|
||||
proc->status = SIGKILL;
|
||||
interrupt_set(true);
|
||||
sched_threadexit();
|
||||
}
|
||||
|
||||
// find the first pending signal which is unmasked
|
||||
|
||||
int signal;
|
||||
sigset_t *sigset = NULL;
|
||||
|
||||
// check the urgent sigset first
|
||||
for (int i = 0; i < NSIG; ++i) {
|
||||
if (SIGNAL_GET(&thread->signals.urgent, i) == 0)
|
||||
continue;
|
||||
|
||||
signal = i;
|
||||
sigset = &thread->signals.urgent;
|
||||
}
|
||||
|
||||
for (int i = 0; i < NSIG && sigset == NULL; ++i) {
|
||||
if (SIGNAL_GET(&thread->signals.mask, i))
|
||||
continue;
|
||||
|
||||
if (SIGNAL_GET(&thread->signals.pending, i)) {
|
||||
signal = i;
|
||||
sigset = &thread->signals.pending;
|
||||
break;
|
||||
}
|
||||
|
||||
if (SIGNAL_GET(&proc->signals.pending, i)) {
|
||||
signal = i;
|
||||
sigset = &proc->signals.pending;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (sigset == NULL)
|
||||
goto leave;
|
||||
|
||||
sigaction_t *action = &proc->signals.actions[signal];
|
||||
SIGNAL_SETOFF(sigset, signal);
|
||||
|
||||
// urgent signals cannot be ignored, if they are the default action will be run
|
||||
if (action->address == SIG_IGN && sigset != &thread->signals.urgent) {
|
||||
goto leave;
|
||||
} else if ((action->address == SIG_DFL && (action->flags & SA_SIGINFO) == 0) || (action->address == SIG_IGN && sigset == &thread->signals.urgent)) {
|
||||
// default action
|
||||
int defaction = defaultactions[signal];
|
||||
switch (defaction) {
|
||||
case ACTION_IGN:
|
||||
goto leave;
|
||||
case ACTION_CORE:
|
||||
case ACTION_TERM:
|
||||
proc->status = signal;
|
||||
interrupt_set(true);
|
||||
sched_threadexit();
|
||||
default:
|
||||
__assert(!"unsupported signal action");
|
||||
}
|
||||
} else {
|
||||
// execute handler
|
||||
ARCH_CONTEXT_THREADSAVE(thread, context);
|
||||
|
||||
void *altstack = NULL;
|
||||
// figure out the stack we will be running the signal handler in
|
||||
if (action->flags & SA_ONSTACK && thread->signals.stack.size > 0)
|
||||
altstack = thread->signals.stack.base;
|
||||
|
||||
// get where in memory to put the frame
|
||||
void *stack = altstack ? altstack : (void *)CTX_SP(context);
|
||||
#if ARCH_SIGNAL_STACK_GROWS_DOWNWARDS == 1
|
||||
stack = (void *)(((uintptr_t)stack - ARCH_SIGNAL_REDZONE_SIZE - sizeof(sigframe_t)) & ~0xf);
|
||||
#else
|
||||
#error unsupported
|
||||
#endif
|
||||
|
||||
// configure stack frame
|
||||
sigframe_t *sigframe = stack;
|
||||
sigframe->restorer = action->restorer;
|
||||
if (altstack) {
|
||||
memcpy(&sigframe->oldstack, &thread->signals.stack, sizeof(stack_t));
|
||||
memset(&thread->signals.stack, 0, sizeof(stack_t));
|
||||
}
|
||||
memcpy(&sigframe->oldmask, &thread->signals.mask, sizeof(sigset_t));
|
||||
memcpy(&sigframe->context, context, sizeof(context_t));
|
||||
memcpy(&sigframe->extracontext, &thread->extracontext, sizeof(extracontext_t));
|
||||
// TODO siginfo
|
||||
|
||||
// configure new thread signal mask
|
||||
if ((action->flags & SA_NODEFER) == 0)
|
||||
SIGNAL_SETON(&thread->signals.mask, signal);
|
||||
|
||||
for (int i = 0; i < signal; ++i) {
|
||||
if (SIGNAL_GET(&action->mask, i) == 0)
|
||||
continue;
|
||||
|
||||
SIGNAL_SETON(&thread->signals.mask, i);
|
||||
}
|
||||
|
||||
// configure return context
|
||||
memset(context, 0, sizeof(context_t));
|
||||
CTX_INIT(context, true, true);
|
||||
CTX_IP(context) = (uint64_t)action->address;
|
||||
CTX_SP(context) = (uint64_t)stack;
|
||||
CTX_ARG0(context) = signal;
|
||||
CTX_ARG1(context) = 0; // TODO pass siginfo
|
||||
CTX_ARG2(context) = 0; // TODO ucontext
|
||||
|
||||
// reset handler if asked for
|
||||
if (action->flags & SA_RESETHAND)
|
||||
memset(&action, 0, sizeof(sigaction_t));
|
||||
}
|
||||
|
||||
leave:
|
||||
THREAD_LEAVE(thread);
|
||||
PROCESS_LEAVE(proc);
|
||||
}
|
||||
@@ -8,12 +8,12 @@
|
||||
__attribute__((noreturn)) void syscall_exit(context_t *context, int status) {
|
||||
thread_t *thread = _cpu()->thread;
|
||||
proc_t *proc = thread->proc;
|
||||
__assert(proc != sched_initproc);
|
||||
__assert(spinlock_try(&proc->exiting)); // only one thread can exit at the same time
|
||||
if (spinlock_try(&proc->exiting) == false)
|
||||
sched_threadexit();
|
||||
|
||||
sched_stopotherthreads();
|
||||
|
||||
proc->status = status;
|
||||
proc->status = status << 8;
|
||||
|
||||
sched_threadexit();
|
||||
}
|
||||
|
||||
@@ -35,15 +35,13 @@ syscallret_t syscall_fork(context_t *ctx) {
|
||||
if (ret.errno)
|
||||
goto cleanup;
|
||||
|
||||
bool intstate = interrupt_set(false);
|
||||
spinlock_acquire(&proc->lock);
|
||||
MUTEX_ACQUIRE(&proc->mutex, false);
|
||||
|
||||
nproc->parent = proc;
|
||||
nproc->sibling = proc->child;
|
||||
proc->child = nproc;
|
||||
|
||||
spinlock_release(&proc->lock);
|
||||
interrupt_set(intstate);
|
||||
MUTEX_RELEASE(&proc->mutex);
|
||||
|
||||
nproc->umask = _cpu()->thread->proc->umask;
|
||||
nproc->root = sched_getroot();
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
#include <kernel/syscalls.h>
|
||||
#include <arch/cpu.h>
|
||||
#include <logging.h>
|
||||
|
||||
syscallret_t syscall_kill(context_t *context, int pid, int signal) {
|
||||
syscallret_t ret = {
|
||||
.ret = -1
|
||||
};
|
||||
|
||||
int effectivepid = -1;
|
||||
|
||||
if (pid > 0) // specific process
|
||||
effectivepid = pid;
|
||||
else if (pid == 0) // own process group
|
||||
effectivepid = 0;
|
||||
else if (pid == -1) // every process, unsupported atm
|
||||
effectivepid = -1;
|
||||
else if (pid < -1)
|
||||
effectivepid = -pid; // target specific process group
|
||||
|
||||
if (effectivepid == -1) {
|
||||
ret.errno = ESRCH;
|
||||
return ret;
|
||||
}
|
||||
|
||||
proc_t *target = sched_getprocfrompid(pid);
|
||||
if (target == NULL && pid != 0) {
|
||||
ret.errno = ESRCH;
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (pid >= 1)
|
||||
signal_signalproc(target, signal);
|
||||
else
|
||||
ret.errno = jobctl_signal(target, signal);
|
||||
|
||||
ret.errno = 0;
|
||||
ret.ret = 0;
|
||||
return ret;
|
||||
}
|
||||
@@ -16,11 +16,11 @@ syscallret_t syscall_newthread(context_t *, void *entry, void *stack) {
|
||||
|
||||
proc_t *proc = _cpu()->thread->proc;
|
||||
|
||||
spinlock_acquire(&proc->lock);
|
||||
MUTEX_ACQUIRE(&proc->mutex, false);
|
||||
|
||||
// if we currently don't want any more threads to start
|
||||
if (proc->nomorethreads == true) {
|
||||
spinlock_release(&proc->lock);
|
||||
MUTEX_RELEASE(&proc->mutex);
|
||||
ret.errno = EAGAIN;
|
||||
return ret;
|
||||
}
|
||||
@@ -63,6 +63,6 @@ syscallret_t syscall_newthread(context_t *, void *entry, void *stack) {
|
||||
sched_queue(proc->threads[i]);
|
||||
|
||||
cleanup:
|
||||
spinlock_release(&proc->lock);
|
||||
MUTEX_RELEASE(&proc->mutex);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
#include <kernel/syscalls.h>
|
||||
#include <arch/cpu.h>
|
||||
#include <logging.h>
|
||||
|
||||
syscallret_t syscall_sigaction(context_t *, int sig, sigaction_t *new, sigaction_t *old) {
|
||||
syscallret_t ret = {
|
||||
.ret = -1
|
||||
};
|
||||
|
||||
if (sig > NSIG || sig == SIGKILL || sig == SIGSTOP) {
|
||||
ret.errno = EINVAL;
|
||||
return ret;
|
||||
}
|
||||
|
||||
sigaction_t newtmp, oldtmp;
|
||||
if (new) {
|
||||
newtmp = *new;
|
||||
__assert(newtmp.flags & SA_RESTORER);
|
||||
}
|
||||
|
||||
signal_action(_cpu()->thread->proc, sig, new ? &newtmp : NULL, old ? &oldtmp : NULL);
|
||||
|
||||
if (old)
|
||||
*old = oldtmp;
|
||||
|
||||
ret.errno = 0;
|
||||
ret.ret = 0;
|
||||
return ret;
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
#include <kernel/syscalls.h>
|
||||
#include <arch/cpu.h>
|
||||
|
||||
syscallret_t syscall_sigaltstack(context_t *, stack_t *new, stack_t *old) {
|
||||
syscallret_t ret = {
|
||||
.ret = -1
|
||||
};
|
||||
|
||||
stack_t newtmp, oldtmp;
|
||||
if (new)
|
||||
newtmp = *new;
|
||||
|
||||
signal_altstack(_cpu()->thread, new ? &newtmp : NULL, old ? &oldtmp : NULL);
|
||||
|
||||
if (old)
|
||||
*old = oldtmp;
|
||||
|
||||
ret.errno = 0;
|
||||
ret.ret = 0;
|
||||
return ret;
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
#include <kernel/syscalls.h>
|
||||
#include <arch/cpu.h>
|
||||
|
||||
#define SIG_BLOCK 0
|
||||
#define SIG_UNBLOCK 1
|
||||
#define SIG_SETMASK 2
|
||||
|
||||
syscallret_t syscall_sigprocmask(context_t *, int how, sigset_t *set, sigset_t *oldset) {
|
||||
syscallret_t ret = {
|
||||
.ret = -1
|
||||
};
|
||||
|
||||
sigset_t new, old;
|
||||
|
||||
if (set) {
|
||||
if (how != SIG_BLOCK && how != SIG_UNBLOCK && how != SIG_SETMASK) {
|
||||
ret.errno = EINVAL;
|
||||
return ret;
|
||||
}
|
||||
|
||||
new = *set;
|
||||
}
|
||||
|
||||
signal_changemask(_cpu()->thread, how, set ? &new : NULL, oldset ? &old : NULL);
|
||||
|
||||
if (oldset)
|
||||
*oldset = old;
|
||||
|
||||
ret.ret = 0;
|
||||
ret.errno = 0;
|
||||
return ret;
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
#include <kernel/syscalls.h>
|
||||
#include <arch/signal.h>
|
||||
#include <arch/cpu.h>
|
||||
#include <logging.h>
|
||||
|
||||
// this expects CTX_SP to be pointing to the signal frame
|
||||
__attribute__((noreturn)) void syscall_sigreturn(context_t *context) {
|
||||
// TODO check if the information is valid
|
||||
sigframe_t sigframe;
|
||||
memcpy(&sigframe, (void *)CTX_SP(context), sizeof(sigframe_t));
|
||||
|
||||
interrupt_set(false);
|
||||
signal_altstack(_cpu()->thread, &sigframe.oldstack, NULL);
|
||||
signal_changemask(_cpu()->thread, SIG_SETMASK, &sigframe.oldmask, NULL);
|
||||
|
||||
__assert(ARCH_CONTEXT_ISUSER(&sigframe.context));
|
||||
memcpy(&_cpu()->thread->extracontext, &sigframe.extracontext, sizeof(extracontext_t));
|
||||
ARCH_CONTEXT_THREADLOAD(_cpu()->thread, context);
|
||||
|
||||
arch_context_switch(&sigframe.context);
|
||||
|
||||
// if we were to return normally, CTX_RET and CTX_ERRNO would be corrupted,
|
||||
// so we will manually call arch_context_switch, which will jump to the right userland code
|
||||
// (and also check for pending signals again)
|
||||
__builtin_unreachable();
|
||||
}
|
||||
@@ -19,7 +19,7 @@ syscallret_t syscall_waitpid(context_t *context, pid_t pid, int *status, int opt
|
||||
|
||||
thread_t *thread = _cpu()->thread;
|
||||
proc_t *proc = thread->proc;
|
||||
spinlock_acquire(&proc->lock);
|
||||
MUTEX_ACQUIRE(&proc->mutex, false);
|
||||
|
||||
proc_t *prev = NULL;
|
||||
proc_t *iterator = proc->child;
|
||||
@@ -29,10 +29,10 @@ syscallret_t syscall_waitpid(context_t *context, pid_t pid, int *status, int opt
|
||||
if (iterator == NULL) {
|
||||
if (pid > 0 && desired == NULL) {
|
||||
ret.errno = ECHILD;
|
||||
spinlock_release(&proc->lock);
|
||||
MUTEX_RELEASE(&proc->mutex);
|
||||
return ret;
|
||||
}
|
||||
spinlock_release(&proc->lock);
|
||||
MUTEX_RELEASE(&proc->mutex);
|
||||
if ((options & WNOHANG) && semaphore_test(&proc->waitsem) == false) {
|
||||
ret.errno = 0;
|
||||
ret.ret = 0;
|
||||
@@ -41,7 +41,7 @@ syscallret_t syscall_waitpid(context_t *context, pid_t pid, int *status, int opt
|
||||
ret.errno = EINTR;
|
||||
return ret;
|
||||
}
|
||||
spinlock_acquire(&proc->lock);
|
||||
MUTEX_ACQUIRE(&proc->mutex, false);
|
||||
prev = NULL;
|
||||
iterator = proc->child;
|
||||
desired = NULL; // set as null to be certain another thread didn't get it
|
||||
@@ -65,12 +65,12 @@ syscallret_t syscall_waitpid(context_t *context, pid_t pid, int *status, int opt
|
||||
else
|
||||
proc->child = iterator->sibling;
|
||||
|
||||
spinlock_release(&proc->lock);
|
||||
MUTEX_RELEASE(&proc->mutex);
|
||||
|
||||
// TODO memory safe operation
|
||||
// TODO the signal number goes in the low byte of status
|
||||
if (status)
|
||||
*status = iterator->status << 8;
|
||||
if (status) {
|
||||
*status = iterator->status;
|
||||
}
|
||||
|
||||
for (int i = 0; i < iterator->threadtablesize; ++i) {
|
||||
if (iterator->threads[i] == NULL)
|
||||
|
||||
Reference in New Issue
Block a user