Cannot catch SIGPIPE signal in Ubuntu

572 views Asked by At

I met a SIGPIPE crash issue and I would like to log it and try to exist.
But I could not catch SIGPIPE via follow code.
I try to use "kill -s signal process" to verify my code, it works with signal SIGINT, SIGSTOP, SIGSEGV and SIGALRM.
But failed on SIGPIPE.
Would you please advise if I miss anything here.

void handle_pipe(int sig)
{
    printf("SIG_PIPE happen, error code is %d", sig);
    exit(0);    
}   

int main(int argc, char **argv)
{
    struct sigaction action;
    sigemptyset(&action.sa_mask);
    action.sa_handler = handle_pipe;
    action.sa_flags = 0;
    //not work
    sigaction(SIGPIPE, &action, NULL);   //Not work with kill -13 process_id
    //works well
    sigaction(SIGINT, &action, NULL);    //work with kill -2 process_id
    sigaction(SIGSEGV, &action, NULL);   //work with kill -11 process_id
    sigaction(SIGALRM, &action, NULL);   //work with kill -14 process_id
    sigaction(SIGSTOP, &action, NULL);   //work with kill -17 process_id

    fooServer * pfooServer = new fooServer();
    while(1)
    {
        pfooServer->DoEvents();
    }
    delete pfooServer;
}

My environment is Ubuntu 12.04 LTS

1

There are 1 answers

1
Vorsprung On BEST ANSWER

This complete code example does work with a kill -13. I don't have ubuntu 12.04 LTS here to test it with, but it is fine on RHEL 6.5. Try this. If it works as expected then there must be something in your "fooServer" that is altering SIGPIPE behaviour

#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>



void handle_pipe(int sig)
{
    printf("SIG_PIPE happen, error code is %d", sig);
    exit(0);    
}   

int main(int argc, char **argv)
{
    struct sigaction action;
    sigemptyset(&action.sa_mask);
    action.sa_handler = handle_pipe;
    action.sa_flags = 0;
    //not work
    sigaction(SIGPIPE, &action, NULL);   //Not work with kill -13 process_id
    //works well
    sigaction(SIGINT, &action, NULL);    //work with kill -2 process_id
    sigaction(SIGSEGV, &action, NULL);   //work with kill -11 process_id
    sigaction(SIGALRM, &action, NULL);   //work with kill -14 process_id
    sigaction(SIGSTOP, &action, NULL);   //work with kill -17 process_id

    while(1)
    {
        sleep(1);
    }
}