support: Add envp argument to support_capture_subprogram
[glibc.git] / signal / tst-sigset2.c
blobc1d125e4cbed1f0404252d7136c54672ca4fb668
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>
12 #include <libc-diag.h>
14 #include <support/xsignal.h>
16 /* The sigset function is deprecated. */
17 DIAG_IGNORE_NEEDS_COMMENT (4.9, "-Wdeprecated-declarations");
19 #define TEST_SIG SIGINT
22 /* Print mask of blocked signals for this process */
23 static void
24 printSigMask (const char *msg)
26 sigset_t currMask;
27 int sig;
28 int cnt;
30 if (msg != NULL)
31 printf ("%s", msg);
33 if (sigprocmask (SIG_BLOCK, NULL, &currMask) == -1)
34 error (1, errno, "sigaction");
36 cnt = 0;
37 for (sig = 1; sig < NSIG; sig++)
39 if (sigismember (&currMask, sig))
41 cnt++;
42 printf ("\t\t%d (%s)\n", sig, strsignal (sig));
46 if (cnt == 0)
47 printf ("\t\t<empty signal set>\n");
48 } /* printSigMask */
50 static void
51 handler (int sig)
53 printf ("Caught signal %d\n", sig);
54 printSigMask ("Signal mask in handler\n");
55 printf ("Handler returning\n");
56 _exit (1);
57 } /* handler */
59 static void
60 printDisposition (sighandler_t disp)
62 if (disp == SIG_HOLD)
63 printf ("SIG_HOLD");
64 else if (disp == SIG_DFL)
65 printf ("SIG_DFL");
66 else if (disp == SIG_IGN)
67 printf ("SIG_IGN");
68 else
69 printf ("handled at %" PRIxPTR, (uintptr_t) disp);
70 } /* printDisposition */
72 static int
73 returnTest1 (void)
75 sighandler_t prev;
77 printf ("===== TEST 1 =====\n");
78 printf ("Blocking signal with sighold()\n");
79 if (sighold (TEST_SIG) == -1)
80 error (1, errno, "sighold");
81 printSigMask ("Signal mask after sighold()\n");
83 printf ("About to use sigset() to establish handler\n");
84 prev = sigset (TEST_SIG, handler);
85 if (prev == SIG_ERR)
86 error(1, errno, "sigset");
88 printf ("Previous disposition: ");
89 printDisposition (prev);
90 printf (" (should be SIG_HOLD)\n");
91 if (prev != SIG_HOLD)
93 printf("TEST FAILED!!!\n");
94 return 1;
96 return 0;
97 } /* returnTest1 */
99 static int
100 returnTest2 (void)
102 sighandler_t prev;
104 printf ("\n===== TEST 2 =====\n");
106 printf ("About to use sigset() to set SIG_HOLD\n");
107 prev = sigset (TEST_SIG, SIG_HOLD);
108 if (prev == SIG_ERR)
109 error (1, errno, "sigset");
111 printf ("Previous disposition: ");
112 printDisposition (prev);
113 printf (" (should be SIG_DFL)\n");
114 if (prev != SIG_DFL)
116 printf("TEST FAILED!!!\n");
117 return 1;
119 return 0;
120 } /* returnTest2 */
122 static int
123 returnTest3 (void)
125 sighandler_t prev;
127 printf ("\n===== TEST 3 =====\n");
129 printf ("About to use sigset() to set SIG_HOLD\n");
130 prev = sigset (TEST_SIG, SIG_HOLD);
131 if (prev == SIG_ERR)
132 error (1, errno, "sigset");
134 printf ("About to use sigset() to set SIG_HOLD (again)\n");
135 prev = sigset (TEST_SIG, SIG_HOLD);
136 if (prev == SIG_ERR)
137 error (1, errno, "sigset");
139 printf ("Previous disposition: ");
140 printDisposition (prev);
141 printf (" (should be SIG_HOLD)\n");
142 if (prev != SIG_HOLD)
144 printf("TEST FAILED!!!\n");
145 return 1;
147 return 0;
148 } /* returnTest3 */
151 main (int argc, char *argv[])
153 xsignal (TEST_SIG, SIG_DFL);
155 pid_t childPid;
157 childPid = fork();
158 if (childPid == -1)
159 error (1, errno, "fork");
161 if (childPid == 0)
162 exit (returnTest1 ());
164 int status;
165 if (TEMP_FAILURE_RETRY (waitpid (childPid, &status, 0)) != childPid)
166 error (1, errno, "waitpid");
167 int result = !WIFEXITED (status) || WEXITSTATUS (status) != 0;
169 childPid = fork();
170 if (childPid == -1)
171 error (1, errno, "fork");
173 if (childPid == 0)
174 exit (returnTest2 ());
176 if (TEMP_FAILURE_RETRY (waitpid (childPid, &status, 0)) != childPid)
177 error (1, errno, "waitpid");
178 result |= !WIFEXITED (status) || WEXITSTATUS (status) != 0;
180 childPid = fork();
181 if (childPid == -1)
182 error (1, errno, "fork");
184 if (childPid == 0)
185 exit (returnTest3 ());
187 if (TEMP_FAILURE_RETRY (waitpid (childPid, &status, 0)) != childPid)
188 error (1, errno, "waitpid");
189 result |= !WIFEXITED (status) || WEXITSTATUS (status) != 0;
191 return result;
192 } /* main */