Skip to content

Instantly share code, notes, and snippets.

@NYKevin
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NYKevin/f59fc15a6927a4f06408 to your computer and use it in GitHub Desktop.
Save NYKevin/f59fc15a6927a4f06408 to your computer and use it in GitHub Desktop.
What signal am I receiving?
#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