signals/catch_rtsigs.cThis is signals/catch_rtsigs.c (Listing 22-3, page 462), 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.
|
#define _GNU_SOURCE #include <string.h> #include <signal.h> #include "tlpi_hdr.h" static volatile int handlerSleepTime; static volatile int sigCnt = 0; /* Number of signals received */ static volatile sig_atomic_t allDone = 0;
static void /* Handler for signals established using SA_SIGINFO */ siginfoHandler(int sig, siginfo_t *si, void *ucontext) { /* UNSAFE: This handler uses non-async-signal-safe functions (printf()); see Section 21.1.2) */ /* SIGINT or SIGTERM can be used to terminate program */ if (sig == SIGINT || sig == SIGTERM) { allDone = 1; return; } sigCnt++; printf("caught signal %d\n", sig); printf(" si_signo=%d, si_code=%d (%s), ", si->si_signo, si->si_code, (si->si_code == SI_USER) ? "SI_USER" : (si->si_code == SI_QUEUE) ? "SI_QUEUE" : "other"); printf("si_value=%d\n", si->si_value.sival_int); printf(" si_pid=%ld, si_uid=%ld\n", (long) si->si_pid, (long) si->si_uid); sleep(handlerSleepTime); }
int main(int argc, char *argv[]) { struct sigaction sa; int sig; sigset_t prevMask, blockMask; if (argc > 1 && strcmp(argv[1], "--help") == 0) usageErr("%s [block-time [handler-sleep-time]]\n", argv[0]); printf("%s: PID is %ld\n", argv[0], (long) getpid()); handlerSleepTime = (argc > 2) ? getInt(argv[2], GN_NONNEG, "handler-sleep-time") : 1; /* Establish handler for most signals. During execution of the handler, mask all other signals to prevent handlers recursively interrupting each other (which would make the output hard to read). */ sa.sa_sigaction = siginfoHandler; sa.sa_flags = SA_SIGINFO; sigfillset(&sa.sa_mask); for (sig = 1; sig < NSIG; sig++) if (sig != SIGTSTP && sig != SIGQUIT) sigaction(sig, &sa, NULL); /* Optionally block signals and sleep, allowing signals to be sent to us before they are unblocked and handled */ if (argc > 1) { sigfillset(&blockMask); sigdelset(&blockMask, SIGINT); sigdelset(&blockMask, SIGTERM); if (sigprocmask(SIG_SETMASK, &blockMask, &prevMask) == -1) errExit("sigprocmask"); printf("%s: signals blocked - sleeping %s seconds\n", argv[0], argv[1]); sleep(getInt(argv[1], GN_GT_0, "block-time")); printf("%s: sleep complete\n", argv[0]); if (sigprocmask(SIG_SETMASK, &prevMask, NULL) == -1) errExit("sigprocmask"); } while (!allDone) /* Wait for incoming signals */ pause(); printf("Caught %d signals\n", sigCnt); exit(EXIT_SUCCESS); }
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.