Skip to content

Commit

Permalink
ipc: implemented sigaction()
Browse files Browse the repository at this point in the history
  • Loading branch information
jewelcodes committed Nov 20, 2024
1 parent d9b90f0 commit 2db1134
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/ipc/signal.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <kernel/sched.h>
#include <kernel/logger.h>
#include <platform/lock.h>
#include <platform/mmap.h>
#include <platform/platform.h>

/* Implementation of ISO C and POSIX Signals */
Expand Down Expand Up @@ -287,4 +288,40 @@ void signalHandle(Thread *t) {
KERROR("TODO: execute custom signal handler: handler = %X, def = %d\n", handler, def);
for(;;);
}
}

/* sigaction(): manipulate a signal handler
* params: t - calling thread
* params: sig - signal number
* params: act - new signal handler, NULL to query the current signal handler
* params: oact - old signal handler, NULL if not requested
* returns: zero on success, negative errno on fail
*/

int sigaction(Thread *t, int sig, const struct sigaction *act, struct sigaction *oact) {
if(sig <= 0 || sig > MAX_SIGNAL) return -EINVAL;

struct sigaction *handlers = (struct sigaction *) t->signals;

if(!act) {
// query signal handler
if(!oact) return 0;
memcpy(oact, &handlers[sig-1], sizeof(struct sigaction));
return 0;
}

uintptr_t handler = (uintptr_t) act->sa_handler;
if(handler != (uintptr_t) SIG_DFL && handler != (uintptr_t) SIG_IGN &&
handler < USER_BASE_ADDRESS)
return -EINVAL;

acquireLockBlocking(&t->lock);

// save the old signal handler if necessary
if(oact)
memcpy(oact, &handlers[sig-1], sizeof(struct sigaction));

memcpy(&handlers[sig-1], act, sizeof(struct sigaction));
releaseLock(&t->lock);
return 0;
}

0 comments on commit 2db1134

Please sign in to comment.