Skip to content

Instantly share code, notes, and snippets.

@devendranaga
Created June 20, 2015 06:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save devendranaga/acf9be54ff0a4297382f to your computer and use it in GitHub Desktop.
Save devendranaga/acf9be54ff0a4297382f to your computer and use it in GitHub Desktop.
signalfd() syscall example
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/signalfd.h>
int segv_handle()
{
printf("segv\n");
exit(0);
}
int main(void)
{
int fd;
sigset_t mask;
sigemptyset(&mask);
sigaddset(&mask, SIGQUIT);
sigaddset(&mask, SIGINT);
sigaddset(&mask, SIGSEGV);
if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0) {
printf("Failed to signalmask\n");
return -1;
}
fd = signalfd(-1, &mask, 0);
if (fd < 0)
return -1;
while (1) {
struct signalfd_siginfo si;
int ret;
ret = read(fd, &si, sizeof(si));
if (ret < 0)
return -1;
if (ret != sizeof(si))
return -1;
if (si.ssi_signo == SIGQUIT)
printf("receive ctrl + \\\n");
else if (si.ssi_signo == SIGINT)
printf("receive ctrl + c\n");
else if (si.ssi_signo == SIGSEGV)
segv_handle();
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment