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 <stdio.h>
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
|
|
||||||
void test_signal_handler(int x) {
|
|
||||||
printf("interompu\n");
|
|
||||||
_Exit(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
signal(SIGINT, test_signal_handler);
|
raise(SIGKILL);
|
||||||
while (1){}
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
25
05/signal.h
25
05/signal.h
|
@ -6,9 +6,9 @@
|
||||||
|
|
||||||
typedef long sig_atomic_t; // there are no "asynchronous interrupts"
|
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_IGN _sig_ign
|
||||||
#define SIG_ERR (-1)
|
#define SIG_ERR ((void *)-1)
|
||||||
|
|
||||||
typedef void (*_Sighandler)(int);
|
typedef void (*_Sighandler)(int);
|
||||||
|
|
||||||
|
@ -30,6 +30,7 @@ unsigned char _signal_restorer[] = {
|
||||||
|
|
||||||
// we need to do this weird indirection because linux has a different
|
// we need to do this weird indirection because linux has a different
|
||||||
// calling convention from us.
|
// calling convention from us.
|
||||||
|
|
||||||
unsigned char _signal_handler[] = {
|
unsigned char _signal_handler[] = {
|
||||||
// signal # passed in rdi
|
// signal # passed in rdi
|
||||||
0x48,0x89,0xf8, // mov rax, rdi (signal #)
|
0x48,0x89,0xf8, // mov rax, rdi (signal #)
|
||||||
|
@ -61,25 +62,29 @@ void _sig_ign(int signal) {
|
||||||
static unsigned long _sig_mask = 0;
|
static unsigned long _sig_mask = 0;
|
||||||
|
|
||||||
_Sighandler signal(int sig, _Sighandler func) {
|
_Sighandler signal(int sig, _Sighandler func) {
|
||||||
if (func == SIG_DFL) {
|
void **handlers = _SIGNAL_HANDLERS;
|
||||||
// @TODO
|
_Sighandler ret = handlers[sig];
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
if (func == SIG_IGN) {
|
if (func == SIG_IGN) {
|
||||||
func = _sig_ign;
|
func = _sig_ign;
|
||||||
}
|
}
|
||||||
|
|
||||||
void **handlers = _SIGNAL_HANDLERS;
|
|
||||||
handlers[sig] = func;
|
handlers[sig] = func;
|
||||||
|
|
||||||
|
if (func == SIG_DFL) {
|
||||||
|
_sig_mask &= ~(1ul << (sig-1));
|
||||||
|
} else {
|
||||||
_sig_mask |= 1ul << (sig-1);
|
_sig_mask |= 1ul << (sig-1);
|
||||||
|
}
|
||||||
struct sigaction act = {0};
|
struct sigaction act = {0};
|
||||||
act.handler = _signal_handler;
|
act.handler = func == SIG_DFL ? SIG_DFL : (void*)_signal_handler;
|
||||||
act.mask = _sig_mask;
|
act.mask = _sig_mask;
|
||||||
act.flags = _SA_RESTORER;
|
act.flags = _SA_RESTORER;
|
||||||
act.restorer = _signal_restorer;
|
act.restorer = _signal_restorer;
|
||||||
__sigaction(sig, &act, NULL);
|
__sigaction(sig, &act, NULL);
|
||||||
return 0;//@TODO
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
int raise(int signal) {
|
||||||
|
return kill(getpid(), signal);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // _SIGNAL_H
|
#endif // _SIGNAL_H
|
||||||
|
|
|
@ -110,6 +110,7 @@ int getpid(void) {
|
||||||
|
|
||||||
#define SIGABRT 6
|
#define SIGABRT 6
|
||||||
#define SIGFPE 8
|
#define SIGFPE 8
|
||||||
|
#define SIGKILL 9
|
||||||
#define SIGILL 4
|
#define SIGILL 4
|
||||||
#define SIGINT 2
|
#define SIGINT 2
|
||||||
#define SIGSEGV 11
|
#define SIGSEGV 11
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue