#include #include #if HAVE_SETRLIMIT # include # include # include #endif #ifndef SIGSTKSZ # define SIGSTKSZ 16384 #endif static union { char buffer[2 * SIGSTKSZ]; long double ld; long u; void *p; } alternate_signal_stack; static void segv_handler (int signo) { _exit (0); } static int c_stack_action () { stack_t st; struct sigaction act; int r; st.ss_flags = 0; /* Use the midpoint to avoid Irix sigaltstack bug. */ st.ss_sp = alternate_signal_stack.buffer + SIGSTKSZ; st.ss_size = SIGSTKSZ; r = sigaltstack (&st, 0); if (r != 0) return 1; sigemptyset (&act.sa_mask); act.sa_flags = SA_NODEFER | SA_ONSTACK | SA_RESETHAND; act.sa_handler = segv_handler; #if FAULT_YIELDS_SIGBUS if (sigaction (SIGBUS, &act, 0) < 0) return 2; #endif if (sigaction (SIGSEGV, &act, 0) < 0) return 3; return 0; } static volatile int * recurse_1 (volatile int n, volatile int *p) { if (n >= 0) *recurse_1 (n + 1, p) += n; return p; } static int recurse (volatile int n) { int sum = 0; return *recurse_1 (n, &sum); } int main () { int result; #if 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 result = c_stack_action (); if (result != 0) return result; return recurse (0); }