From 4a5947e10ec17586ddc06ce54c3e4c98a4ca300a Mon Sep 17 00:00:00 2001 From: apio Date: Mon, 10 Jul 2023 21:17:25 +0200 Subject: [PATCH] libc: Implement signal() --- libc/include/signal.h | 5 +++++ libc/src/signal.cpp | 12 ++++++++++++ 2 files changed, 17 insertions(+) diff --git a/libc/include/signal.h b/libc/include/signal.h index 3cfa36cf..814ede80 100644 --- a/libc/include/signal.h +++ b/libc/include/signal.h @@ -8,6 +8,8 @@ typedef int sig_atomic_t; +#define SIG_ERR (__simple_sighandler_t)(-3) + #ifdef __cplusplus extern "C" { @@ -19,6 +21,9 @@ extern "C" int sigaction(int signo, const struct sigaction* act, struct sigaction* oldact); #pragma GCC pop_options + /* Change the current thread's signal disposition for a specific signal. */ + __simple_sighandler_t signal(int signo, __simple_sighandler_t handler); + /* Send a signal to a specific process. */ int kill(pid_t pid, int signo); diff --git a/libc/src/signal.cpp b/libc/src/signal.cpp index 9b55e78b..64849364 100644 --- a/libc/src/signal.cpp +++ b/libc/src/signal.cpp @@ -13,6 +13,18 @@ extern "C" __errno_return(rc, int); } + __simple_sighandler_t signal(int signo, __simple_sighandler_t handler) + { + struct sigaction act, oldact; + act.sa_handler = handler; + sigemptyset(&act.sa_mask); + act.sa_flags = 0; + + if (sigaction(signo, &act, &oldact) < 0) return SIG_ERR; + + return oldact.sa_handler; + } + int kill(pid_t pid, int signo) { long rc = syscall(SYS_kill, pid, signo);