In C, I want to catch the SIGINT
signal and print out a message like
"SIGINT received" by using sigaction and passing a new handler to it via
sa.sa_sigaction = handler;
I don't want to terminate the program.
If I run my program through the shell and generate the signal with Ctrl+c , the signal handler will catch the signal and print out my message.
Afterwards, it will peform the default action which is terminating the process.
What am I doing wrong?
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <string.h>
#include <signal.h>
static void handler(int sig, siginfo_t* si, void *unused){
if(sig == SIGINT){
printf("Signal %i received\n",si->si_signo);
}
}
int main(int argc, char* argv[]){
char s [256];
struct sigaction sa;
sigemptyset(&sa.sa_mask);
sigaddset(&sa.sa_mask, SIGINT);
sa.sa_flags = SA_SIGINFO;
sa.sa_sigaction = handler;
if(sigaction(SIGINT, &sa, NULL) < 0 ){
perror("sigaction");
}
fgets(s,sizeof(s), stdin);
printf("%s", s);
return 0;
}