From c6f1a399afc367271ada1d58f09fc00e3d298ce8 Mon Sep 17 00:00:00 2001 From: pommicket Date: Wed, 16 Feb 2022 13:33:35 -0500 Subject: signal.h --- 05/main.c | 8 +------- 05/signal.h | 27 ++++++++++++++++----------- 05/stdc_common.h | 1 + 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/05/main.c b/05/main.c index 1420330..59d6ccd 100644 --- a/05/main.c +++ b/05/main.c @@ -3,14 +3,8 @@ #include #include -void test_signal_handler(int x) { - printf("interompu\n"); - _Exit(0); -} - int main(int argc, char **argv) { - signal(SIGINT, test_signal_handler); - while (1){} + raise(SIGKILL); return 0; } diff --git a/05/signal.h b/05/signal.h index dc10e41..f885782 100644 --- a/05/signal.h +++ b/05/signal.h @@ -6,9 +6,9 @@ typedef long sig_atomic_t; // there are no "asynchronous interrupts" -#define SIG_DFL 0 +#define SIG_DFL ((void *)0) #define SIG_IGN _sig_ign -#define SIG_ERR (-1) +#define SIG_ERR ((void *)-1) typedef void (*_Sighandler)(int); @@ -30,6 +30,7 @@ unsigned char _signal_restorer[] = { // we need to do this weird indirection because linux has a different // calling convention from us. + unsigned char _signal_handler[] = { // signal # passed in rdi 0x48,0x89,0xf8, // mov rax, rdi (signal #) @@ -61,25 +62,29 @@ void _sig_ign(int signal) { static unsigned long _sig_mask = 0; _Sighandler signal(int sig, _Sighandler func) { - if (func == SIG_DFL) { - // @TODO - return 0; - } + void **handlers = _SIGNAL_HANDLERS; + _Sighandler ret = handlers[sig]; if (func == SIG_IGN) { func = _sig_ign; } - - void **handlers = _SIGNAL_HANDLERS; handlers[sig] = func; - _sig_mask |= 1ul << (sig-1); + if (func == SIG_DFL) { + _sig_mask &= ~(1ul << (sig-1)); + } else { + _sig_mask |= 1ul << (sig-1); + } struct sigaction act = {0}; - act.handler = _signal_handler; + act.handler = func == SIG_DFL ? SIG_DFL : (void*)_signal_handler; act.mask = _sig_mask; act.flags = _SA_RESTORER; act.restorer = _signal_restorer; __sigaction(sig, &act, NULL); - return 0;//@TODO + return ret; +} + +int raise(int signal) { + return kill(getpid(), signal); } #endif // _SIGNAL_H diff --git a/05/stdc_common.h b/05/stdc_common.h index af30b06..10144a8 100644 --- a/05/stdc_common.h +++ b/05/stdc_common.h @@ -110,6 +110,7 @@ int getpid(void) { #define SIGABRT 6 #define SIGFPE 8 +#define SIGKILL 9 #define SIGILL 4 #define SIGINT 2 #define SIGSEGV 11 -- cgit v1.2.3