Increase alternate stack size on ia64.
[libsigsegv/ericb.git] / m4 / sigaltstack.m4
blobf169782bb353a7461422cb71a20ac634a5cf0771
1 # sigaltstack.m4 serial 7 (libsigsegv-2.5)
2 dnl Copyright (C) 2002-2006 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 AC_DEFUN([SV_SIGALTSTACK],
11   AC_REQUIRE([AC_PROG_CC])
12   AC_REQUIRE([AC_CANONICAL_HOST])
14   AC_CHECK_FUNCS(sigaltstack)
16   if test "$ac_cv_func_sigaltstack" = yes; then
17     AC_CHECK_TYPE(stack_t, ,
18       [AC_DEFINE(stack_t, [struct sigaltstack],
19          [Define to 'struct sigaltstack' if that's the type of the argument to sigaltstack])
20       ],
21       [
22 #include <signal.h>
23 #if HAVE_SYS_SIGNAL_H
24 # include <sys/signal.h>
25 #endif
26       ])
27   fi
29   AC_CACHE_CHECK([for working sigaltstack], sv_cv_sigaltstack, [
30     if test "$ac_cv_func_sigaltstack" = yes; then
31       case "$host_os" in
32         macos* | darwin[[6-9]]* | darwin[[1-9]][[0-9]]*)
33           # On MacOS X 10.2 or newer, just assume that if it compiles, it will
34           # work. If we were to perform the real test, 1 Crash Report dialog
35           # window would pop up.
36           AC_LINK_IFELSE([
37             AC_LANG_PROGRAM([[#include <signal.h>]],
38               [[int x = SA_ONSTACK; stack_t ss; sigaltstack ((stack_t*)0, &ss);]])],
39             [sv_cv_sigaltstack="guessing yes"],
40             [sv_cv_sigaltstack=no])
41           ;;
42         *)
43           AC_RUN_IFELSE([
44             AC_LANG_SOURCE([[
45 #include <stdlib.h>
46 #include <signal.h>
47 #if HAVE_SYS_SIGNAL_H
48 # include <sys/signal.h>
49 #endif
50 #if HAVE_SETRLIMIT
51 # include <sys/types.h>
52 # include <sys/time.h>
53 # include <sys/resource.h>
54 #endif
55 #ifndef SIGSTKSZ
56 # define SIGSTKSZ 16384
57 #endif
58 void stackoverflow_handler (int sig)
60   /* If we get here, the stack overflow was caught.  */
61   exit (0);
63 volatile int * recurse_1 (volatile int n, volatile int *p)
65   if (n >= 0)
66     *recurse_1 (n + 1, p) += n;
67   return p;
69 volatile int recurse (volatile int n)
71   int sum = 0;
72   return *recurse_1 (n, &sum);
74 int main ()
76   char mystack[SIGSTKSZ];
77   stack_t altstack;
78   struct sigaction action;
79 #if defined HAVE_SETRLIMIT && defined RLIMIT_STACK
80   /* Before starting the endless recursion, try to be friendly to the user's
81      machine.  On some Linux 2.2.x systems, there is no stack limit for user
82      processes at all.  We don't want to kill such systems.  */
83   struct rlimit rl;
84   rl.rlim_cur = rl.rlim_max = 0x100000; /* 1 MB */
85   setrlimit (RLIMIT_STACK, &rl);
86 #endif
87   /* Install the alternate stack.  */
88   altstack.ss_sp = mystack;
89   altstack.ss_size = sizeof (mystack);
90   altstack.ss_flags = 0; /* no SS_DISABLE */
91   if (sigaltstack (&altstack, NULL) < 0)
92     exit (1);
93   /* Install the SIGSEGV handler.  */
94   sigemptyset (&action.sa_mask);
95   action.sa_handler = &stackoverflow_handler;
96   action.sa_flags = SA_ONSTACK;
97   sigaction (SIGSEGV, &action, (struct sigaction *) NULL);
98   sigaction (SIGBUS, &action, (struct sigaction *) NULL);
99   /* Provoke a stack overflow.  */
100   recurse (0);
101   exit (2);
102 }]])],
103             [sv_cv_sigaltstack=yes],
104             [sv_cv_sigaltstack=no],
105             [
106               dnl FIXME: Put in some more known values here.
107               case "$host_os" in
108                 *)
109                   AC_LINK_IFELSE([
110                     AC_LANG_PROGRAM([[#include <signal.h>]],
111                       [[int x = SA_ONSTACK; stack_t ss; sigaltstack ((stack_t*)0, &ss);]])],
112                     [sv_cv_sigaltstack="guessing yes"],
113                     [sv_cv_sigaltstack=no])
114                   ;;
115               esac
116             ])
117           ;;
118       esac
119     else
120       sv_cv_sigaltstack=no
121     fi
122   ])
123   if test "$sv_cv_sigaltstack" != no; then
124     AC_DEFINE(HAVE_WORKING_SIGALTSTACK, 1,
125       [Define if you have the sigaltstack() function and it works.])
126   fi