]> git.ipfire.org Git - thirdparty/glibc.git/blame - nptl/tst-cancel20.c
Update copyright notices with scripts/update-copyrights
[thirdparty/glibc.git] / nptl / tst-cancel20.c
CommitLineData
d4697bc9 1/* Copyright (C) 2003-2014 Free Software Foundation, Inc.
675620f7
UD
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
59ba27a6
PE
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
675620f7
UD
18
19#include <errno.h>
20#include <pthread.h>
21#include <signal.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <unistd.h>
25
26
27static int fd[4];
28static pthread_barrier_t b;
29volatile int in_sh_body;
30unsigned long cleanups;
31
32static void
33cl (void *arg)
34{
35 cleanups = (cleanups << 4) | (long) arg;
36}
37
38
39static void __attribute__((noinline))
40sh_body (void)
41{
42 char c;
43
44 pthread_cleanup_push (cl, (void *) 1L);
45
46 in_sh_body = 1;
47 if (read (fd[2], &c, 1) == 1)
48 {
49 puts ("read succeeded");
50 exit (1);
51 }
52
53 pthread_cleanup_pop (0);
54}
55
56
57static void
58sh (int sig)
59{
60 pthread_cleanup_push (cl, (void *) 2L);
61 sh_body ();
62 in_sh_body = 0;
63
64 pthread_cleanup_pop (0);
65}
66
67
68static void __attribute__((noinline))
69tf_body (void)
70{
71 char c;
72
73 pthread_cleanup_push (cl, (void *) 3L);
74
75 int r = pthread_barrier_wait (&b);
76 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
77 {
78 puts ("child thread: barrier_wait failed");
79 exit (1);
80 }
81
82 if (read (fd[0], &c, 1) == 1)
83 {
84 puts ("read succeeded");
85 exit (1);
86 }
87
88 read (fd[0], &c, 1);
89
90 pthread_cleanup_pop (0);
91}
92
93
94static void *
95tf (void *arg)
96{
97 pthread_cleanup_push (cl, (void *) 4L);
98 tf_body ();
99 pthread_cleanup_pop (0);
100 return NULL;
101}
102
103
104static int
105do_one_test (void)
106{
f4c024d1 107 in_sh_body = 0;
675620f7
UD
108 cleanups = 0;
109 if (pipe (fd) != 0 || pipe (fd + 2) != 0)
110 {
111 puts ("pipe failed");
112 return 1;
113 }
114
115 pthread_t th;
116 if (pthread_create (&th, NULL, tf, NULL) != 0)
117 {
118 puts ("create failed");
119 return 1;
120 }
121
122 int r = pthread_barrier_wait (&b);
123 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
124 {
125 puts ("parent thread: barrier_wait failed");
126 return 1;
127 }
128
129 sleep (1);
130
131 r = pthread_kill (th, SIGHUP);
132 if (r)
133 {
134 errno = r;
135 printf ("pthread_kill failed %m\n");
136 return 1;
137 }
138
139 while (in_sh_body == 0)
140 sleep (1);
141
142 if (pthread_cancel (th) != 0)
143 {
144 puts ("cancel failed");
145 return 1;
146 }
147
148 /* This will cause the read in the child to return. */
149 close (fd[0]);
150 close (fd[1]);
151 close (fd[2]);
152 close (fd[3]);
153
154 void *ret;
155 if (pthread_join (th, &ret) != 0)
156 {
157 puts ("join failed");
158 return 1;
159 }
160
161 if (ret != PTHREAD_CANCELED)
162 {
163 puts ("result is wrong");
164 return 1;
165 }
166
167 if (cleanups != 0x1234L)
168 {
169 printf ("called cleanups %lx\n", cleanups);
170 return 1;
171 }
172
173 return 0;
174}
175
176
177static int
178do_test (void)
179{
180 stack_t ss;
181 ss.ss_sp = malloc (2 * SIGSTKSZ);
182 if (ss.ss_sp == NULL)
183 {
184 puts ("failed to allocate alternate stack");
185 return 1;
186 }
187 ss.ss_flags = 0;
188 ss.ss_size = 2 * SIGSTKSZ;
189 if (sigaltstack (&ss, NULL) < 0)
190 {
191 printf ("sigaltstack failed %m\n");
192 return 1;
193 }
194
195 if (pthread_barrier_init (&b, NULL, 2) != 0)
196 {
197 puts ("barrier_init failed");
198 return 1;
199 }
200
201 struct sigaction sa;
202 sa.sa_handler = sh;
203 sigemptyset (&sa.sa_mask);
204 sa.sa_flags = 0;
205
206 if (sigaction (SIGHUP, &sa, NULL) != 0)
207 {
208 puts ("sigaction failed");
209 return 1;
210 }
211
212 puts ("sa_flags = 0 test");
213 if (do_one_test ())
214 return 1;
215
216 sa.sa_handler = sh;
217 sigemptyset (&sa.sa_mask);
218 sa.sa_flags = SA_ONSTACK;
219
220 if (sigaction (SIGHUP, &sa, NULL) != 0)
221 {
222 puts ("sigaction failed");
223 return 1;
224 }
225
226 puts ("sa_flags = SA_ONSTACK test");
227 if (do_one_test ())
228 return 1;
229
230 sa.sa_sigaction = (void (*)(int, siginfo_t *, void *)) sh;
231 sigemptyset (&sa.sa_mask);
232 sa.sa_flags = SA_SIGINFO;
233
234 if (sigaction (SIGHUP, &sa, NULL) != 0)
235 {
236 puts ("sigaction failed");
237 return 1;
238 }
239
240 puts ("sa_flags = SA_SIGINFO test");
241 if (do_one_test ())
242 return 1;
243
244 sa.sa_sigaction = (void (*)(int, siginfo_t *, void *)) sh;
245 sigemptyset (&sa.sa_mask);
246 sa.sa_flags = SA_SIGINFO | SA_ONSTACK;
247
248 if (sigaction (SIGHUP, &sa, NULL) != 0)
249 {
250 puts ("sigaction failed");
251 return 1;
252 }
253
254 puts ("sa_flags = SA_SIGINFO|SA_ONSTACK test");
255 if (do_one_test ())
256 return 1;
257
258 return 0;
259}
260
261#define TIMEOUT 40
262#define TEST_FUNCTION do_test ()
263#include "../test-skeleton.c"