1 # sigaltstack-longjmp.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 longjmp out of a signal handler, in such a way that the
10 dnl alternate signal stack remains functional.
11 dnl SV_TRY_LEAVE_HANDLER_LONGJMP(KIND, CACHESYMBOL, KNOWN-SYSTEMS,
12 dnl INCLUDES, RESETCODE)
13 AC_DEFUN([SV_TRY_LEAVE_HANDLER_LONGJMP],
15 AC_REQUIRE([AC_PROG_CC])
16 AC_REQUIRE([AC_CANONICAL_HOST])
18 AC_CACHE_CHECK([whether a signal handler can be left through longjmp$1], [$2], [
26 # include <sys/types.h>
27 # include <sys/time.h>
28 # include <sys/resource.h>
31 # define SIGSTKSZ 16384
36 void stackoverflow_handler (int sig)
39 sigprocmask (SIG_SETMASK, &mainsigset, NULL);
41 longjmp (mainloop, pass);
43 volatile int * recurse_1 (volatile int n, volatile int *p)
46 *recurse_1 (n + 1, p) += n;
49 int recurse (volatile int n)
52 return *recurse_1 (n, &sum);
54 char mystack[2 * SIGSTKSZ];
58 struct sigaction action;
60 #if defined HAVE_SETRLIMIT && defined RLIMIT_STACK
61 /* Before starting the endless recursion, try to be friendly to the user's
62 machine. On some Linux 2.2.x systems, there is no stack limit for user
63 processes at all. We don't want to kill such systems. */
65 rl.rlim_cur = rl.rlim_max = 0x100000; /* 1 MB */
66 setrlimit (RLIMIT_STACK, &rl);
68 /* Install the alternate stack. Use the midpoint of mystack, to guard
69 against a buggy interpretation of ss_sp on IRIX. */
70 altstack.ss_sp = mystack + SIGSTKSZ;
71 altstack.ss_size = SIGSTKSZ;
72 altstack.ss_flags = 0; /* no SS_DISABLE */
73 if (sigaltstack (&altstack, NULL) < 0)
75 /* Install the SIGSEGV handler. */
76 sigemptyset (&action.sa_mask);
77 action.sa_handler = &stackoverflow_handler;
78 action.sa_flags = SA_ONSTACK;
79 sigaction (SIGSEGV, &action, (struct sigaction *) NULL);
80 sigaction (SIGBUS, &action, (struct sigaction *) NULL);
81 /* Save the current signal mask. */
82 sigemptyset (&emptyset);
83 sigprocmask (SIG_BLOCK, &emptyset, &mainsigset);
84 /* Provoke two stack overflows in a row. */
85 if (setjmp (mainloop) < 2)
95 m4_if([$3], [], [], [[$3]) $2=yes ;;])
96 *) $2="guessing no" ;;