Skip to content

signalfd API Reference

#include <sys/signalfd.h>
#include <signal.h>

Core APIs

int signalfd(int fd, const sigset_t *mask, int flags);
int sigprocmask(int how, const sigset_t *set, sigset_t *oldset);

Setup Pattern

sigset_t mask;
sigemptyset(&mask);
sigaddset(&mask, SIGINT);
sigaddset(&mask, SIGTERM);

sigprocmask(SIG_BLOCK, &mask, NULL);

int fd = signalfd(-1, &mask, SFD_NONBLOCK | SFD_CLOEXEC);

Read Format

struct signalfd_siginfo si;
read(fd, &si, sizeof(si));

Common Pitfalls

  • Block the signal before creating or using signalfd.
  • Read struct signalfd_siginfo to consume the event.
  • Avoid mixing traditional signal handlers and signalfd without a clear design.