file tst-timer.c was initially added on branch fedora-branch.
[glibc/history.git] / signal / tst-sigset2.c
blobf65332332152e6f7974879486b839325269a9459
1 /* sigset_SIG_HOLD_bug.c [BZ #1951] */
2 #include <errno.h>
3 #include <error.h>
4 #include <inttypes.h>
5 #include <signal.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <unistd.h>
10 #include <sys/types.h>
11 #include <sys/wait.h>
13 #define TEST_SIG SIGINT
16 /* Print mask of blocked signals for this process */
17 static void
18 printSigMask (const char *msg)
20 sigset_t currMask;
21 int sig;
22 int cnt;
24 if (msg != NULL)
25 printf ("%s", msg);
27 if (sigprocmask (SIG_BLOCK, NULL, &currMask) == -1)
28 error (1, errno, "sigaction");
30 cnt = 0;
31 for (sig = 1; sig < NSIG; sig++)
33 if (sigismember (&currMask, sig))
35 cnt++;
36 printf ("\t\t%d (%s)\n", sig, strsignal (sig));
40 if (cnt == 0)
41 printf ("\t\t<empty signal set>\n");
42 } /* printSigMask */
44 static void
45 handler (int sig)
47 printf ("Caught signal %d\n", sig);
48 printSigMask ("Signal mask in handler\n");
49 printf ("Handler returning\n");
50 _exit (1);
51 } /* handler */
53 static void
54 printDisposition (sighandler_t disp)
56 if (disp == SIG_HOLD)
57 printf ("SIG_HOLD");
58 else if (disp == SIG_DFL)
59 printf ("SIG_DFL");
60 else if (disp == SIG_IGN)
61 printf ("SIG_IGN");
62 else
63 printf ("handled at %" PRIxPTR, (uintptr_t) disp);
64 } /* printDisposition */
66 static int
67 returnTest1 (void)
69 sighandler_t prev;
71 printf ("===== TEST 1 =====\n");
72 printf ("Blocking signal with sighold()\n");
73 if (sighold (TEST_SIG) == -1)
74 error (1, errno, "sighold");
75 printSigMask ("Signal mask after sighold()\n");
77 printf ("About to use sigset() to establish handler\n");
78 prev = sigset (TEST_SIG, handler);
79 if (prev == SIG_ERR)
80 error(1, errno, "sigset");
82 printf ("Previous disposition: ");
83 printDisposition (prev);
84 printf (" (should be SIG_HOLD)\n");
85 if (prev != SIG_HOLD)
87 printf("TEST FAILED!!!\n");
88 return 1;
90 return 0;
91 } /* returnTest1 */
93 static int
94 returnTest2 (void)
96 sighandler_t prev;
98 printf ("\n===== TEST 2 =====\n");
100 printf ("About to use sigset() to set SIG_HOLD\n");
101 prev = sigset (TEST_SIG, SIG_HOLD);
102 if (prev == SIG_ERR)
103 error (1, errno, "sigset");
105 printf ("Previous disposition: ");
106 printDisposition (prev);
107 printf (" (should be SIG_DFL)\n");
108 if (prev != SIG_DFL)
110 printf("TEST FAILED!!!\n");
111 return 1;
113 return 0;
114 } /* returnTest2 */
116 static int
117 returnTest3 (void)
119 sighandler_t prev;
121 printf ("\n===== TEST 3 =====\n");
123 printf ("About to use sigset() to set SIG_HOLD\n");
124 prev = sigset (TEST_SIG, SIG_HOLD);
125 if (prev == SIG_ERR)
126 error (1, errno, "sigset");
128 printf ("About to use sigset() to set SIG_HOLD (again)\n");
129 prev = sigset (TEST_SIG, SIG_HOLD);
130 if (prev == SIG_ERR)
131 error (1, errno, "sigset");
133 printf ("Previous disposition: ");
134 printDisposition (prev);
135 printf (" (should be SIG_HOLD)\n");
136 if (prev != SIG_HOLD)
138 printf("TEST FAILED!!!\n");
139 return 1;
141 return 0;
142 } /* returnTest3 */
145 main (int argc, char *argv[])
147 pid_t childPid;
149 childPid = fork();
150 if (childPid == -1)
151 error (1, errno, "fork");
153 if (childPid == 0)
154 exit (returnTest1 ());
156 int status;
157 if (TEMP_FAILURE_RETRY (waitpid (childPid, &status, 0)) != childPid)
158 error (1, errno, "waitpid");
159 int result = !WIFEXITED (status) || WEXITSTATUS (status) != 0;
161 childPid = fork();
162 if (childPid == -1)
163 error (1, errno, "fork");
165 if (childPid == 0)
166 exit (returnTest2 ());
168 if (TEMP_FAILURE_RETRY (waitpid (childPid, &status, 0)) != childPid)
169 error (1, errno, "waitpid");
170 result |= !WIFEXITED (status) || WEXITSTATUS (status) != 0;
172 childPid = fork();
173 if (childPid == -1)
174 error (1, errno, "fork");
176 if (childPid == 0)
177 exit (returnTest3 ());
179 if (TEMP_FAILURE_RETRY (waitpid (childPid, &status, 0)) != childPid)
180 error (1, errno, "waitpid");
181 result |= !WIFEXITED (status) || WEXITSTATUS (status) != 0;
183 return result;
184 } /* main */