Created
April 24, 2012 19:06
-
-
Save migueldeicaza/2482764 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <unistd.h> | |
| #include <signal.h> | |
| #include <pthread.h> | |
| #include <sys/wait.h> | |
| #if defined(__FreeBSD__) || defined(__NetBSD__) | |
| #define SA_STACK SA_ONSTACK | |
| #endif | |
| static void | |
| sigsegv_signal_handler (int _dummy, siginfo_t *info, void *context) | |
| { | |
| exit (0); | |
| } | |
| static void * | |
| loop (void *ignored) | |
| { | |
| char *ptr = NULL; | |
| *ptr = 0; | |
| return NULL; | |
| } | |
| static void | |
| child () | |
| { | |
| struct sigaction sa; | |
| #ifdef __APPLE__ | |
| stack_t sas; | |
| #else | |
| struct sigaltstack sas; | |
| #endif | |
| pthread_t id; | |
| pthread_attr_t attr; | |
| sa.sa_sigaction = sigsegv_signal_handler; | |
| sigemptyset (&sa.sa_mask); | |
| sa.sa_flags = SA_SIGINFO | SA_ONSTACK; | |
| if (sigaction (SIGSEGV, &sa, NULL) == -1) { | |
| perror ("sigaction"); | |
| return; | |
| } | |
| /* x86 darwin deliver segfaults using SIGBUS */ | |
| if (sigaction (SIGBUS, &sa, NULL) == -1) { | |
| perror ("sigaction"); | |
| return; | |
| } | |
| sas.ss_sp = malloc (SIGSTKSZ); | |
| sas.ss_size = SIGSTKSZ; | |
| sas.ss_flags = 0; | |
| if (sigaltstack (&sas, NULL) == -1) { | |
| perror ("sigaltstack"); | |
| return; | |
| } | |
| pthread_attr_init (&attr); | |
| if (pthread_create(&id, &attr, loop, &attr) != 0) { | |
| printf ("pthread_create\n"); | |
| return; | |
| } | |
| sleep (100); | |
| } | |
| int | |
| main () | |
| { | |
| pid_t son; | |
| int status; | |
| int i; | |
| son = fork (); | |
| if (son == -1) { | |
| return 1; | |
| } | |
| if (son == 0) { | |
| child (); | |
| return 0; | |
| } | |
| for (i = 0; i < 3; ++i) { | |
| sleep (1); | |
| waitpid (son, &status, WNOHANG); | |
| if (WIFEXITED (status) && WEXITSTATUS (status) == 0) | |
| return 0; | |
| } | |
| kill (son, SIGKILL); | |
| return 1; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment