Last active
August 29, 2015 14:06
-
-
Save NYKevin/f59fc15a6927a4f06408 to your computer and use it in GitHub Desktop.
What signal am I receiving?
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
#define _POSIX_C_SOURCE 1 | |
#include <signal.h> | |
#include <sys/signalfd.h> | |
#include <unistd.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
int main(void){ | |
sigset_t sig_mask; | |
sigfillset(&sig_mask); | |
if(sigprocmask(SIG_BLOCK, &sig_mask, NULL) == -1){ | |
perror("sigprocmask"); | |
exit(EXIT_FAILURE); | |
} | |
int sig_fd = signalfd(-1, &sig_mask, 0); | |
if(sig_fd == -1){ | |
perror("signalfd"); | |
exit(EXIT_FAILURE); | |
} | |
struct signalfd_siginfo buffer; | |
while(1){ | |
ssize_t rc = read(sig_fd, &buffer, sizeof(struct signalfd_siginfo)); | |
if(rc < 0){ | |
perror("read"); | |
exit(EXIT_FAILURE); | |
} | |
printf("Received signal #%d\n", buffer.ssi_signo); | |
if(buffer.ssi_signo == SIGQUIT || buffer.ssi_signo == SIGINT){ | |
printf("That's all, folks.\n"); | |
exit(EXIT_SUCCESS); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment