sewardj | 2342c97 | 2002-05-22 23:34:20 +0000 | [diff] [blame] | 1 | |
| 2 | |
| 3 | #include <stdio.h> |
mueller | 535cc6e | 2004-01-03 14:18:02 +0000 | [diff] [blame] | 4 | #include <stdlib.h> |
sewardj | 2342c97 | 2002-05-22 23:34:20 +0000 | [diff] [blame] | 5 | #include <signal.h> |
fitzhardinge | f0f911c | 2003-11-09 09:51:33 +0000 | [diff] [blame] | 6 | #include <sys/mman.h> |
sewardj | 2342c97 | 2002-05-22 23:34:20 +0000 | [diff] [blame] | 7 | |
| 8 | void sig_handler(int sig){ |
| 9 | int var; |
| 10 | fprintf(stderr, "caught signal, local var is on %p\n", &var); |
| 11 | } |
| 12 | |
sewardj | 2342c97 | 2002-05-22 23:34:20 +0000 | [diff] [blame] | 13 | int main(int argv, char** argc) { |
| 14 | int res, i; |
| 15 | stack_t sigstk; |
| 16 | struct sigaction act; |
fitzhardinge | 8e53678 | 2003-11-21 09:22:06 +0000 | [diff] [blame] | 17 | static const int size = SIGSTKSZ*2; |
njn | 09ef408 | 2005-11-17 19:38:09 +0000 | [diff] [blame^] | 18 | // We give EXEC permissions because this won't work on ppc32 unless you |
| 19 | // ask for an alt stack with EXEC permissions. |
| 20 | since signal returning requires execution of code on the stack. |
sewardj | 89551b3 | 2005-11-17 13:04:46 +0000 | [diff] [blame] | 21 | char *stk = (char *)mmap(0, size, PROT_READ|PROT_WRITE|PROT_EXEC, |
| 22 | MAP_ANON|MAP_PRIVATE, -1, 0); |
fitzhardinge | f0f911c | 2003-11-09 09:51:33 +0000 | [diff] [blame] | 23 | sigstk.ss_sp = stk; |
sewardj | 2342c97 | 2002-05-22 23:34:20 +0000 | [diff] [blame] | 24 | |
fitzhardinge | f0f911c | 2003-11-09 09:51:33 +0000 | [diff] [blame] | 25 | sigstk.ss_size = size; |
sewardj | 2342c97 | 2002-05-22 23:34:20 +0000 | [diff] [blame] | 26 | sigstk.ss_flags = 0; |
| 27 | fprintf(stderr, "calling sigaltstack, stack base is %p\n", sigstk.ss_sp); |
| 28 | if (sigaltstack(&sigstk,0)<0) perror("sigaltstack"); |
| 29 | |
| 30 | fprintf(stderr,"setting sigaction\n"); |
| 31 | act.sa_flags=SA_ONSTACK; |
| 32 | act.sa_handler=&sig_handler; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 33 | sigemptyset(&act.sa_mask); |
sewardj | 2342c97 | 2002-05-22 23:34:20 +0000 | [diff] [blame] | 34 | res = sigaction(SIGUSR1,&act,0); |
| 35 | fprintf(stderr, "res = %d\n", res); |
| 36 | fprintf(stderr, "raising the signal\n"); |
| 37 | raise(SIGUSR1); |
| 38 | |
| 39 | /* Loop long enough so valgrind has a forced context switch and |
| 40 | actually delivers the signal before the thread exits. */ |
| 41 | for (i = 0; i < 1000000; i++) ; |
| 42 | |
| 43 | fprintf(stderr, "done\n"); |
| 44 | return 0; |
| 45 | } |