[ create a new paste ] login | about

Link: http://codepad.org/ifh53S3R    [ raw code | output | fork ]

cky - C, pasted on Jun 21:
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#define PUTS(x) write(1, (x), sizeof (x) - 1)

extern char *__progname;

static void
sighandler1(int signal)
{
    PUTS("First-try signal handled, re-execing\n");
    execl(__progname, (char *) 0);
    PUTS("Exec unsuccessful, dying\n");
    _exit(2);
}

static void
sighandler2(int signal)
{
    PUTS("Second-try signal handled, dying\n");
    _exit(3);
}

int
main(int argc, char **argv)
{
    struct sigaction sa;
    sa.sa_handler = argc ? &sighandler1 : &sighandler2;
    sigemptyset(&sa.sa_mask);
    sigaddset(&sa.sa_mask, SIGSEGV);
    sa.sa_flags = 0;
    if (sigaction(SIGSEGV, &sa, 0) == -1) {
        fprintf(stderr, "Cannot install signal handler: %s", strerror(errno));
        return 1;
    }
    return *(int *) 0;
}


Output:
1
Segmentation fault


Create a new paste based on this one


Comments: