Signals: Advanced Features 463
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();
}
–––––––––––––––––––––––––––––––––––––––––––––––––––– signals/catch_rtsigs.c