signals/signalfd_sigval.cThis is signals/signalfd_sigval.c (Listing 22-7, page 473), an example from the book, The Linux Programming Interface. The source code file is copyright 2024, Michael Kerrisk, and is licensed under the GNU General Public License, version 3. This page shows the "distribution" or "book" version of the file (why are there two versions?), or the differences between the two versions. You can switch between the views using the tabs below. In the listing below, the names of Linux system calls and C library functions are hyperlinked to manual pages from the Linux man-pages project, and the names of functions implemented in the book are hyperlinked to the implementations of those functions. |
+/* signalfd_sigval.c + + Usage: signalfd_sigval sig-num... + + Demonstrate the use of signalfd() to receive signals via a file descriptor. + + This program is Linux-specific. The signalfd API is supported since kernel + 2.6.22. +*/ #include <sys/signalfd.h> #include <signal.h> #include "tlpi_hdr.h" int main(int argc, char *argv[]) { sigset_t mask; int sfd, j; struct signalfd_siginfo fdsi; ssize_t s; if (argc < 2 || strcmp(argv[1], "--help") == 0) usageErr("%s sig-num...\n", argv[0]); printf("%s: PID = %ld\n", argv[0], (long) getpid()); sigemptyset(&mask); for (j = 1; j < argc; j++) sigaddset(&mask, atoi(argv[j])); if (sigprocmask(SIG_BLOCK, &mask, NULL) == -1) errExit("sigprocmask"); sfd = signalfd(-1, &mask, 0); if (sfd == -1) errExit("signalfd"); for (;;) { s = read(sfd, &fdsi, sizeof(struct signalfd_siginfo)); if (s != sizeof(struct signalfd_siginfo)) errExit("read"); printf("%s: got signal %d", argv[0], fdsi.ssi_signo); if (fdsi.ssi_code == SI_QUEUE) { printf("; ssi_pid = %d; ", fdsi.ssi_pid); printf("ssi_int = %d", fdsi.ssi_int); } printf("\n"); } }
Note that, in most cases, the programs rendered in these web pages are not free standing: you'll typically also need a few other source files (mostly in the lib/ subdirectory) as well. Generally, it's easier to just download the entire source tarball and build the programs with make(1). By hovering your mouse over the various hyperlinked include files and function calls above, you can see which other source files this file depends on.