Created
June 20, 2015 06:06
-
-
Save devendranaga/acf9be54ff0a4297382f to your computer and use it in GitHub Desktop.
signalfd() syscall example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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