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.