| 1 |
#include <stdio.h> |
| 2 |
#include <stdlib.h> |
| 3 |
#include <errno.h> |
| 4 |
#include <signal.h> |
| 5 |
|
| 6 |
extern void inf_loop(); |
| 7 |
void register_handlers(); |
| 8 |
void register_alt_stack(); |
| 9 |
|
| 10 |
int main(int argc, char** argv){ |
| 11 |
fprintf(stderr, "Entering ... \n"); |
| 12 |
register_handlers(); |
| 13 |
register_alt_stack(); |
| 14 |
inf_loop(); |
| 15 |
fprintf(stderr, "Done\n"); |
| 16 |
return 0; |
| 17 |
} |
| 18 |
|
| 19 |
#if 0 |
| 20 |
#include <signal.h> |
| 21 |
|
| 22 |
struct sigaction { |
| 23 |
union { |
| 24 |
void (*__sa_handler)(int); |
| 25 |
void (*__sa_sigaction)(int, struct __siginfo *, void *); |
| 26 |
} __sigaction_u; /* signal handler */ |
| 27 |
int sa_flags; /* see signal options below */ |
| 28 |
sigset_t sa_mask; /* signal mask to apply */ |
| 29 |
}; |
| 30 |
|
| 31 |
#define sa_handler __sigaction_u.__sa_handler |
| 32 |
#define sa_sigaction __sigaction_u.__sa_sigaction |
| 33 |
|
| 34 |
int |
| 35 |
sigaction(int sig, const struct sigaction * restrict act, |
| 36 |
struct sigaction * restrict oact); |
| 37 |
#endif |
| 38 |
|
| 39 |
void handler(int signo, struct __siginfo* info, ucontext_t* uap){ |
| 40 |
fprintf(stderr, "Handler Called!\n"); |
| 41 |
} |
| 42 |
|
| 43 |
void |
| 44 |
register_handlers(){ |
| 45 |
struct sigaction sa; |
| 46 |
sa.sa_sigaction = (void(*)(int,struct __siginfo*,void*)) handler; |
| 47 |
sa.sa_flags = SA_SIGINFO | SA_ONSTACK; |
| 48 |
sa.sa_mask = 0; |
| 49 |
int err = sigaction(SIGINT, &sa, 0); |
| 50 |
if(err){ |
| 51 |
fprintf(stderr, "Sigaction Failed: %s\n", strerror(errno)); |
| 52 |
exit(-1); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
#if 0 |
| 58 |
SYNOPSIS |
| 59 |
#include <sys/types.h> |
| 60 |
#include <signal.h> |
| 61 |
|
| 62 |
struct sigaltstack { |
| 63 |
char *ss_sp; |
| 64 |
int ss_size; |
| 65 |
int ss_flags; |
| 66 |
}; |
| 67 |
|
| 68 |
int |
| 69 |
sigaltstack(const struct sigaltstack *ss, struct sigaltstack *oss); |
| 70 |
#endif |
| 71 |
|
| 72 |
void |
| 73 |
register_alt_stack(){ |
| 74 |
char* stk = malloc(SIGSTKSZ); |
| 75 |
if(stk == 0){ |
| 76 |
fprintf(stderr, "Cannot maloc an alt stack\n"); |
| 77 |
exit(-1); |
| 78 |
} |
| 79 |
|
| 80 |
struct sigaltstack sa; |
| 81 |
sa.ss_sp = stk; |
| 82 |
sa.ss_size = SIGSTKSZ; |
| 83 |
sa.ss_flags = 0; |
| 84 |
int err = sigaltstack(&sa, 0); |
| 85 |
if(err){ |
| 86 |
fprintf(stderr, "Cannot set alt stack: %s\n", strerror(errno)); |
| 87 |
exit(-1); |
| 88 |
} |
| 89 |
fprintf(stderr, "alt-stack of size %d set\n", SIGSTKSZ); |
| 90 |
} |