# sigaltstack-longjmp.m4 serial 1 (libsigsegv-2.0) dnl Copyright (C) 2002 Bruno Haible dnl This file is free software, distributed under the terms of the GNU dnl General Public License. As a special exception to the GNU General dnl Public License, this file may be distributed as part of a program dnl that contains a configuration script generated by Autoconf, under dnl the same distribution terms as the rest of that program. dnl How to longjmp out of a signal handler, in such a way that the dnl alternate signal stack remains functional. dnl SV_TRY_LEAVE_HANDLER_LONGJMP(KIND, CACHESYMBOL, KNOWN-SYSTEMS, dnl INCLUDES, RESETCODE) AC_DEFUN([SV_TRY_LEAVE_HANDLER_LONGJMP], [ AC_REQUIRE([AC_PROG_CC]) AC_REQUIRE([AC_CANONICAL_HOST]) AC_CACHE_CHECK([whether a signal handler can be left through longjmp$1], [$2], [ AC_TRY_RUN([ #include #include #include $4 #if HAVE_SETRLIMIT # include # include # include #endif jmp_buf mainloop; sigset_t mainsigset; int pass = 0; void stackoverflow_handler (int sig) { pass++; sigprocmask (SIG_SETMASK, &mainsigset, NULL); { $5 } longjmp (mainloop, pass); } int recurse (int n) { if (n >= 0) return n + recurse (n + 1); else return 0; } int main () { char mystack[16384]; stack_t altstack; struct sigaction action; sigset_t emptyset; #if defined HAVE_SETRLIMIT && defined RLIMIT_STACK /* Before starting the endless recursion, try to be friendly to the user's machine. On some Linux 2.2.x systems, there is no stack limit for user processes at all. We don't want to kill such systems. */ struct rlimit rl; rl.rlim_cur = rl.rlim_max = 0x100000; /* 1 MB */ setrlimit (RLIMIT_STACK, &rl); #endif /* Install the alternate stack. */ altstack.ss_sp = mystack; altstack.ss_size = sizeof (mystack); altstack.ss_flags = 0; /* no SS_DISABLE */ if (sigaltstack (&altstack, NULL) < 0) exit (1); /* Install the SIGSEGV handler. */ sigemptyset (&action.sa_mask); action.sa_handler = &stackoverflow_handler; action.sa_flags = SA_ONSTACK; sigaction (SIGSEGV, &action, (struct sigaction *) NULL); sigaction (SIGBUS, &action, (struct sigaction *) NULL); /* Save the current signal mask. */ sigemptyset (&emptyset); sigprocmask (SIG_BLOCK, &emptyset, &mainsigset); /* Provoke two stack overflows in a row. */ if (setjmp (mainloop) < 2) { recurse (0); exit (2); } exit (0); }], [$2=yes], [$2=no], [case "$host" in ifelse([$3], [], [], [[$3]) $2=yes ;;]) *) $2="guessing no" ;; esac ]) ]) ])