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], [
26 # include <sys/types.h>
27 # include <sys/time.h>
28 # include <sys/resource.h>
31 # define SIGSTKSZ 16384
35 void stackoverflow_handler (int sig)
39 siglongjmp (mainloop, pass);
41 volatile int * recurse_1 (volatile int n, volatile int *p)
44 *recurse_1 (n + 1, p) += n;
47 int recurse (volatile int n)
50 return *recurse_1 (n, &sum);
52 char mystack[2 * SIGSTKSZ];
56 struct sigaction action;
58 /* On BeOS, this would hang, burning CPU time. Better fail than hang. */
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. */
66 rl.rlim_cur = rl.rlim_max = 0x100000; /* 1 MB */
67 setrlimit (RLIMIT_STACK, &rl);
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)
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)
93 m4_if([$3], [], [], [[$3]) $2=yes ;;])
94 *) $2="guessing no" ;;