Introduce old redir program
[lcapit-junk-code.git] / books / apue / signal.c
blobe248341dc2ea01f41f8d28ba96d3613d70651bd4
1 /*
2 * 10.11: Demonstrates the use of sigpending() and sigismember()
3 * by installing a handler for SIGQUIT, blocking it and checking
4 * if it was sent.
5 */
6 #include <stdio.h>
7 #include <unistd.h>
8 #include <stdlib.h>
9 #include <signal.h>
11 static void sig_quit(int signo)
13 fprintf(stderr, "caught SIGQUIT\n");
15 if (signal(SIGQUIT, SIG_DFL) == SIG_ERR) {
16 fprintf(stderr, "can't reset SIGQUIT\n");
17 exit(1);
21 int main(void)
23 sigset_t newmask, oldmask, pendmask;
25 /* Installs sig_quit() as our handler for SIGQUIT */
26 if (signal(SIGQUIT, sig_quit) == SIG_ERR) {
27 fprintf(stderr, "can't catch SIGQUIT\n");
28 exit(1);
31 /* Blocks SIGQUIT */
32 sigemptyset(&newmask);
33 sigaddset(&newmask, SIGQUIT);
35 if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) {
36 fprintf(stderr, "SIG_BLOCK error\n");
37 exit(1);
40 /*
41 * Sleeps for five seconds. In this period we should
42 * receive a SIGQUIT, but as it's blocked we continue
43 * to sleep
45 sleep(5);
47 if (sigpending(&pendmask) < 0) {
48 fprintf(stderr, "sigpending error\n");
49 exit(1);
52 if (sigismember(&pendmask, SIGQUIT))
53 printf("SIGQUIT pending\n");
55 if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) {
56 fprintf(stderr, "SIG_SETMASK error\n");
57 exit(1);
59 printf("SIGQUIT unblocked\n");
61 exit(0);