Skip to content

Instantly share code, notes, and snippets.

@devendranaga
Created December 27, 2015 13:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devendranaga/5c424f7278c0e45e9d67 to your computer and use it in GitHub Desktop.
Save devendranaga/5c424f7278c0e45e9d67 to your computer and use it in GitHub Desktop.
register more than one signal with signalfd for any event
#include <stdio.h>
#include <stdint.h>
#include <signal.h>
#include <unistd.h>
#include <sys/signalfd.h>
#include <sys/select.h>
fd_set allfd;
int maxfd;
int sfd_int;
int register_signal(sigset_t *sigmask, int sig, int *fd)
{
sigemptyset(sigmask);
sigaddset(sigmask, sig);
sigprocmask(SIG_BLOCK, sigmask, NULL);
*fd = signalfd(-1, sigmask, 0);
if (*fd == -1) {
printf("cannot get a signal fd\n");
return -1;
}
FD_SET(*fd, &allfd);
if (*fd > maxfd)
maxfd = *fd;
return 0;
}
int main()
{
sigset_t mask_int;
struct signalfd_siginfo siginfo;
int ret;
FD_ZERO(&allfd);
register_signal(&mask_int, SIGINT, &sfd_int);
while (1) {
fd_set fds = allfd;
ret = select(maxfd + 1, &allfd, NULL, NULL, 0);
if (ret > 0) {
struct signalfd_siginfo siginfo;
if (FD_ISSET(sfd_int, &allfd)) {
ret = read(sfd_int, &siginfo, sizeof(siginfo));
if (ret == sizeof(siginfo)) {
printf("Got %d = %d\n", siginfo.ssi_signo, SIGINT);
close(sfd_int);
FD_CLR(sfd_int, &allfd);
sfd_int = signalfd(-1, &mask_int, 0);
if (sfd_int > maxfd)
maxfd = sfd_int;
FD_SET(sfd_int, &allfd);
sigaddset(&mask_int, SIGQUIT);
sigprocmask(SIG_BLOCK, &mask_int, NULL);
}
}
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment