signal.h
This commit is contained in:
parent
408e9b0e81
commit
c6f1a399af
3 changed files with 18 additions and 18 deletions
|
@ -3,14 +3,8 @@
|
|||
#include <stdio.h>
|
||||
#include <signal.h>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
|
25
05/signal.h
25
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;
|
||||
|
||||
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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue