0

I have to manage the follows signals: SIGHUP, SIGINT, SIGQUIT, SIGTERM The code below is an example with SIGHUP:

int sock_ds, acc_sock_ds; //TCP sockets

void shutdown(int sig){
    close(sock_ds);
    close(acc_sock_ds);
    raise(sig);
}

/*Sets the disposition of a signal
 * Returns 0 on success, -1 on error */
int sig_disp(int sig, void (* handler)(int), int flag){
    struct sigaction sa;
    memset((char *)&sa, 0, sizeof(sa));
    sa.sa_handler = handler;
    sa.sa_flags = flag;
    sigemptyset(&sa.sa_mask);
    return sigaction(sig, &sa, NULL);
}
int main(){

    /*...*/

    /*--- Signal management ---*/
    sigset_t set;
    /*sigset filling with all signal*/
    if(sigfillset(&set) == -1){
        fprintf(stderr, "Signal set fill error\n");
        exit(EXIT_FAILURE);
    }

    /*...*/ 

    if(sigdelset(&set, SIGHUP) == -1){
        fprintf(stderr, "Signal set action error\n");
        exit(EXIT_FAILURE);
    }

    /*...*/

   /*Changing signals disposition*/
   if(sig_disp(SIGHUP, shutdown, SA_RESETHAND));

   /*...*/

   /*Remaining signals are inserted into process signal mask (blocked) */
   if(sigprocmask(SIG_BLOCK, &set, NULL) == -1){
       fprintf(stderr, "Process signal mask action error\n");
       exit(EXIT_FAILURE);
   }

   /*...*/
}

My idea is to reactivate the signal's default handling, which is to terminate the process and re-raise the same signal after cleanup operations.

How can i pass sig to shutdown function with sa.sa_handler = handler;?

Furthermore what are the cleaning actions commonly taken in these cases? My scenario is mono-threaded server with TCP socket and some opened files.

Fabio Carello
  • 1,062
  • 3
  • 12
  • 30

2 Answers2

1

There is no need to close sockets, files, whatever or free memory on exit. The OS will do that anyway, you are just wasting time doing something twice.

To terminate just use

exit(0);

To reset a signal handler to the original, instead of setting to NULL the last parameter to sigaction you must pass a pointer that will be set with the address of the previous handler.

When you change the handler using signal, it will return the pointer of the previous handler

LtWorf
  • 7,286
  • 6
  • 31
  • 45
0

When you say 'manage the following signals', what are you trying to achieve?

If it is just to do some custom cleanup upon receipt of signals that would normally terminate a process, you could use a call to atexit to register a cleanup function.

kevinm
  • 151
  • 1
  • 1
  • 9
  • Yes i have only to do cleanup and termination but that must be done from the signal handler – Fabio Carello Mar 11 '13 at 20:37
  • Because this kind of termination must occur only with a termination signal. – Fabio Carello Mar 11 '13 at 21:14
  • Fabio, what kind of termination are you referring to? The default handler for signal such as `INT` and `TERM` are to gracefully shutdown the process. This means to close all file descriptors and exit properly. Signals are still involved, you just don't have to deal with them for a straight-forward situation. – kevinm Mar 11 '13 at 22:57
  • I don't know. that is a didactic implementation of a TCP mono–threaded server in which I have to manage termination signals. At this point I can't imagine why. – Fabio Carello Mar 12 '13 at 15:17
  • The default behavior of signals is to cause ["Abnormal termination of the process"](http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/signal.h.html) and therefore the handlers registered via `atexit` are *not* invoked. – Rufflewind Nov 18 '15 at 11:22