1

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

Vorsprung
  • 32,923
  • 5
  • 39
  • 63
Evan Lin
  • 1,272
  • 1
  • 12
  • 25

1 Answers1

1

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);
    }
}
Vorsprung
  • 32,923
  • 5
  • 39
  • 63
  • Hi Vorsprung, Thank you. It seems something happen in the fooServer. But since the fooServer is 3rd party library, do you have any suggestion I could catch this kind of signal outside of the fooServer? – Evan Lin Dec 23 '14 at 01:43