Improve NetBSD support: Allow VMA determination with fewer system calls.
[libsigsegv/ericb.git] / m4 / sigaltstack-siglongjmp.m4
blobc22061d3610856d5a25c511a917ffd63b87fec13
1 # sigaltstack-siglongjmp.m4 serial 6 (libsigsegv-2.7)
2 dnl Copyright (C) 2002-2003, 2006, 2008 Bruno Haible <bruno@clisp.org>
3 dnl This file is free software, distributed under the terms of the GNU
4 dnl General Public License.  As a special exception to the GNU General
5 dnl Public License, this file may be distributed as part of a program
6 dnl that contains a configuration script generated by Autoconf, under
7 dnl the same distribution terms as the rest of that program.
9 dnl How to siglongjmp out of a signal handler, in such a way that the
10 dnl alternate signal stack remains functional.
11 dnl SV_TRY_LEAVE_HANDLER_SIGLONGJMP(KIND, CACHESYMBOL, KNOWN-SYSTEMS,
12 dnl                                 INCLUDES, RESETCODE)
13 AC_DEFUN([SV_TRY_LEAVE_HANDLER_SIGLONGJMP],
15   AC_REQUIRE([AC_PROG_CC])
16   AC_REQUIRE([AC_CANONICAL_HOST])
18   AC_CACHE_CHECK([whether a signal handler can be left through siglongjmp$1], [$2], [
19     AC_RUN_IFELSE([
20       AC_LANG_SOURCE([[
21 #include <stdlib.h>
22 #include <signal.h>
23 #include <setjmp.h>
25 #if HAVE_SETRLIMIT
26 # include <sys/types.h>
27 # include <sys/time.h>
28 # include <sys/resource.h>
29 #endif
30 #ifndef SIGSTKSZ
31 # define SIGSTKSZ 16384
32 #endif
33 sigjmp_buf mainloop;
34 int pass = 0;
35 void stackoverflow_handler (int sig)
37   pass++;
38   { $5 }
39   siglongjmp (mainloop, pass);
41 volatile int * recurse_1 (volatile int n, volatile int *p)
43   if (n >= 0)
44     *recurse_1 (n + 1, p) += n;
45   return p;
47 int recurse (volatile int n)
49   int sum = 0;
50   return *recurse_1 (n, &sum);
52 char mystack[2 * SIGSTKSZ];
53 int main ()
55   stack_t altstack;
56   struct sigaction action;
57 #ifdef __BEOS__
58   /* On BeOS, this would hang, burning CPU time.  Better fail than hang.  */
59   exit (1);
60 #endif
61 #if defined HAVE_SETRLIMIT && defined RLIMIT_STACK
62   /* Before starting the endless recursion, try to be friendly to the user's
63      machine.  On some Linux 2.2.x systems, there is no stack limit for user
64      processes at all.  We don't want to kill such systems.  */
65   struct rlimit rl;
66   rl.rlim_cur = rl.rlim_max = 0x100000; /* 1 MB */
67   setrlimit (RLIMIT_STACK, &rl);
68 #endif
69   /* Install the alternate stack.  Use the midpoint of mystack, to guard
70      against a buggy interpretation of ss_sp on IRIX.  */
71   altstack.ss_sp = mystack + SIGSTKSZ;
72   altstack.ss_size = SIGSTKSZ;
73   altstack.ss_flags = 0; /* no SS_DISABLE */
74   if (sigaltstack (&altstack, NULL) < 0)
75     exit (1);
76   /* Install the SIGSEGV handler.  */
77   sigemptyset (&action.sa_mask);
78   action.sa_handler = &stackoverflow_handler;
79   action.sa_flags = SA_ONSTACK;
80   sigaction (SIGSEGV, &action, (struct sigaction *) NULL);
81   sigaction (SIGBUS, &action, (struct sigaction *) NULL);
82   /* Provoke two stack overflows in a row.  */
83   if (sigsetjmp (mainloop, 1) < 2)
84     {
85       recurse (0);
86       exit (2);
87     }
88   exit (0);
89 }]])],
90       [$2=yes],
91       [$2=no],
92       [case "$host" in
93          m4_if([$3], [], [], [[$3]) $2=yes ;;])
94          *) $2="guessing no" ;;
95        esac
96       ])
97   ])