Warning: That file was not part of the compilation database. It may have many parsing errors.

1/* Copyright (C) 2003-2019 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Jakub Jelinek <jakub@redhat.com>, 2003.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
18
19#include <errno.h>
20#include <pthread.h>
21#include <signal.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <sys/wait.h>
25#include <unistd.h>
26
27
28static int fd[4];
29static pthread_barrier_t b;
30volatile int in_sh_body;
31unsigned long cleanups;
32
33static void
34cl (void *arg)
35{
36 cleanups = (cleanups << 4) | (long) arg;
37}
38
39
40static void __attribute__((noinline))
41sh_body (void)
42{
43 char c;
44
45 pthread_cleanup_push (cl, (void *) 1L);
46
47 in_sh_body = 1;
48 if (read (fd[2], &c, 1) == 1)
49 {
50 puts ("read succeeded");
51 exit (1);
52 }
53
54 pthread_cleanup_pop (0);
55}
56
57
58static void
59sh (int sig)
60{
61 pthread_cleanup_push (cl, (void *) 2L);
62 sh_body ();
63 in_sh_body = 0;
64
65 pthread_cleanup_pop (0);
66}
67
68
69static void __attribute__((noinline))
70tf_body (void)
71{
72 char c;
73
74 pthread_cleanup_push (cl, (void *) 3L);
75
76 int r = pthread_barrier_wait (&b);
77 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
78 {
79 puts ("child thread: barrier_wait failed");
80 exit (1);
81 }
82
83 if (read (fd[0], &c, 1) == 1)
84 {
85 puts ("read succeeded");
86 exit (1);
87 }
88
89 read (fd[0], &c, 1);
90
91 pthread_cleanup_pop (0);
92}
93
94
95static void *
96tf (void *arg)
97{
98 pthread_t th = (pthread_t) arg;
99
100 int r = pthread_barrier_wait (&b);
101 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
102 {
103 puts ("parent thread: barrier_wait failed");
104 exit (1);
105 }
106
107 sleep (1);
108
109 r = pthread_kill (th, SIGHUP);
110 if (r)
111 {
112 errno = r;
113 printf ("pthread_kill failed %m\n");
114 exit (1);
115 }
116
117 while (in_sh_body == 0)
118 sleep (1);
119
120 if (pthread_cancel (th) != 0)
121 {
122 puts ("cancel failed");
123 exit (1);
124 }
125
126 void *ret;
127 if (pthread_join (th, &ret) != 0)
128 {
129 puts ("join failed");
130 exit (1);
131 }
132
133 if (ret != PTHREAD_CANCELED)
134 {
135 puts ("result is wrong");
136 exit (1);
137 }
138
139 if (cleanups != 0x1234L)
140 {
141 printf ("called cleanups %lx\n", cleanups);
142 exit (1);
143 }
144
145 if (pthread_barrier_destroy (&b))
146 {
147 puts ("barrier destroy failed");
148 exit (1);
149 }
150
151 /* The pipe closing must be issued after the cancellation handling to avoid
152 a race condition where the cancellation runs after both pipe ends are
153 closed. In this case the read syscall returns EOF and the cancellation
154 must not act. */
155 close (fd[0]);
156 close (fd[1]);
157 close (fd[2]);
158 close (fd[3]);
159
160 exit (0);
161}
162
163
164static int
165do_one_test (void)
166{
167 in_sh_body = 0;
168
169 pid_t pid = fork ();
170
171 if (pid == -1)
172 {
173 printf ("fork failed: %m\n");
174 return 1;
175 }
176
177 if (pid)
178 {
179 int status;
180 if (waitpid (pid, &status, 0) < 0)
181 {
182 printf ("waitpid failed %m\n");
183 return 1;
184 }
185
186 return !WIFEXITED (status) || WEXITSTATUS (status);
187 }
188
189 if (pthread_barrier_init (&b, NULL, 2) != 0)
190 {
191 puts ("barrier_init failed");
192 exit (1);
193 }
194
195 cleanups = 0;
196 if (pipe (fd) != 0 || pipe (fd + 2) != 0)
197 {
198 puts ("pipe failed");
199 exit (1);
200 }
201
202 pthread_t th;
203 if (pthread_create (&th, NULL, tf, (void *) pthread_self ()) != 0)
204 {
205 puts ("create failed");
206 exit (1);
207 }
208
209 pthread_cleanup_push (cl, (void *) 4L);
210 tf_body ();
211 pthread_cleanup_pop (0);
212 exit (1);
213}
214
215
216static int
217do_test (void)
218{
219 stack_t ss;
220 ss.ss_sp = malloc (2 * SIGSTKSZ);
221 if (ss.ss_sp == NULL)
222 {
223 puts ("failed to allocate alternate stack");
224 return 1;
225 }
226 ss.ss_flags = 0;
227 ss.ss_size = 2 * SIGSTKSZ;
228 if (sigaltstack (&ss, NULL) < 0)
229 {
230 printf ("sigaltstack failed %m\n");
231 return 1;
232 }
233
234 struct sigaction sa;
235 sa.sa_handler = sh;
236 sigemptyset (&sa.sa_mask);
237 sa.sa_flags = 0;
238
239 if (sigaction (SIGHUP, &sa, NULL) != 0)
240 {
241 puts ("sigaction failed");
242 return 1;
243 }
244
245 puts ("sa_flags = 0 test");
246 if (do_one_test ())
247 return 1;
248
249 sa.sa_handler = sh;
250 sigemptyset (&sa.sa_mask);
251 sa.sa_flags = SA_ONSTACK;
252
253 if (sigaction (SIGHUP, &sa, NULL) != 0)
254 {
255 puts ("sigaction failed");
256 return 1;
257 }
258
259 puts ("sa_flags = SA_ONSTACK test");
260 if (do_one_test ())
261 return 1;
262
263#ifdef SA_SIGINFO
264 sa.sa_sigaction = (void (*)(int, siginfo_t *, void *)) sh;
265 sigemptyset (&sa.sa_mask);
266 sa.sa_flags = SA_SIGINFO;
267
268 if (sigaction (SIGHUP, &sa, NULL) != 0)
269 {
270 puts ("sigaction failed");
271 return 1;
272 }
273
274 puts ("sa_flags = SA_SIGINFO test");
275 if (do_one_test ())
276 return 1;
277
278 sa.sa_sigaction = (void (*)(int, siginfo_t *, void *)) sh;
279 sigemptyset (&sa.sa_mask);
280 sa.sa_flags = SA_SIGINFO | SA_ONSTACK;
281
282 if (sigaction (SIGHUP, &sa, NULL) != 0)
283 {
284 puts ("sigaction failed");
285 return 1;
286 }
287
288 puts ("sa_flags = SA_SIGINFO|SA_ONSTACK test");
289 if (do_one_test ())
290 return 1;
291#endif
292
293 return 0;
294}
295
296#define TIMEOUT 40
297#define TEST_FUNCTION do_test ()
298#include "../test-skeleton.c"
299

Warning: That file was not part of the compilation database. It may have many parsing errors.