Home | History | Annotate | Download | only in tests
      1 #include <inttypes.h>
      2 #include <stdio.h>
      3 #include <stdlib.h>
      4 #include <signal.h>
      5 #include "tests/sys_mman.h"
      6 
      7 void sig_handler(int sig){
      8   int var;
      9   fprintf(stderr, "caught signal, local var is on %#" PRIxPTR "\n",
     10           (uintptr_t)&var);
     11 }
     12 
     13 int main(int argv, char** argc) {
     14   int res, i;
     15   stack_t sigstk;
     16   struct sigaction act;
     17   static const int size = SIGSTKSZ*2;
     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.
     21   char *stk = (char *)mmap(0, size, PROT_READ|PROT_WRITE|PROT_EXEC,
     22                                     MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
     23   sigstk.ss_sp = stk;
     24 
     25   sigstk.ss_size = size;
     26   sigstk.ss_flags = 0;
     27   fprintf(stderr, "calling sigaltstack, stack base is %#" PRIxPTR "\n",
     28           (uintptr_t)sigstk.ss_sp);
     29   if (sigaltstack(&sigstk,0)<0) perror("sigaltstack");
     30 
     31   fprintf(stderr,"setting sigaction\n");
     32   act.sa_flags=SA_ONSTACK;
     33   act.sa_handler=&sig_handler;
     34   sigemptyset(&act.sa_mask);
     35   res = sigaction(SIGUSR1,&act,0);
     36   fprintf(stderr, "res = %d\n", res);
     37   fprintf(stderr, "raising the signal\n");
     38   raise(SIGUSR1);
     39 
     40   /* Loop long enough so valgrind has a forced context switch and
     41      actually delivers the signal before the thread exits. */
     42   for (i = 0; i < 1000000; i++) ;
     43 
     44   fprintf(stderr, "done\n");
     45   return 0;
     46 }
     47