Reliable management of signal SIGPIPE and SIGTERM

I' m note very expert in the reliable manage of signal... but in my server I must manage SIGPIPE for the socket and SIGTERM...
I've wrote this but there is something wrong... Can someone explain me with some example the reliable management of signal??

This is what I've wrote in the server

 struct sigaction sig, osig; 
   sigset_t sigmask, oldmask, zeromask;

  sig.sa_handler= catcher;
  sigemptyset( &sig.sa_mask);
  sig.sa_flags= 0;
  sigemptyset( &sigmask);
  sigaddset(&sigmask, SIGPIPE);
  sigaddset(&sigmask, SIGTERM);
  sigprocmask(SIG_BLOCK, &sigmask, &oldmask);
  sigaction(SIGPIPE, &sig, &osig);
  sigaction(SIGTERM, &sig, &osig);

......
......
......
while(1){
.....
.....
......
.....
.....
sigsuspend(&zeromask);
}

....
.....
......
}

You should proceed as follows:

  • for SIGPIPE: you usually want to set to ignore SIGPIPE. Advantage: you will be synchronously informed in the send() function if a SIGPIPE is caught (return -1, and errno is set to EPIPE). No need to mess up with a signal handler.
  • for SIGTERM, set simply a signal handler with sigaction()

This simply looks like this:

struct sigaction act;

// ignore SIGPIPE
act.sa_handler=SIG_IGN;
sigemptyset(&act.sa_mask);
act.sa_flags=0;
sigaction(SIGPIPE, &act, NULL); 

// set my handler for SIGTERM
act.sa_handler=catcher;
sigemptyset(&act.sa_mask);
act.sa_flags=0;
sigaction(SIGTERM, &act, NULL);

In particular, don't use sigprocmask. You would block the signal SIGTERM and SIGPIPE, preventing effectively your handlers to run!

HTH,
Lo�c.

Loic thank you very much!!Now it's perfetc!! ;-)!