source: vendor/glibc-tests/glibc/nptl/tst-cancel20.c

Last change on this file was 2036, checked in by bird, 20 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 4.9 KB
Line 
1/* Copyright (C) 2003, 2004 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
19
20#include <errno.h>
21#include <pthread.h>
22#include <signal.h>
23#include <stdio.h>
24#include <stdlib.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_cleanup_push (cl, (void *) 4L);
99 tf_body ();
100 pthread_cleanup_pop (0);
101 return NULL;
102}
103
104
105static int
106do_one_test (void)
107{
108 in_sh_body = 0;
109 cleanups = 0;
110 if (pipe (fd) != 0 || pipe (fd + 2) != 0)
111 {
112 puts ("pipe failed");
113 return 1;
114 }
115
116 pthread_t th;
117 if (pthread_create (&th, NULL, tf, NULL) != 0)
118 {
119 puts ("create failed");
120 return 1;
121 }
122
123 int r = pthread_barrier_wait (&b);
124 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
125 {
126 puts ("parent thread: barrier_wait failed");
127 return 1;
128 }
129
130 sleep (1);
131
132 r = pthread_kill (th, SIGHUP);
133 if (r)
134 {
135 errno = r;
136 printf ("pthread_kill failed %m\n");
137 return 1;
138 }
139
140 while (in_sh_body == 0)
141 sleep (1);
142
143 if (pthread_cancel (th) != 0)
144 {
145 puts ("cancel failed");
146 return 1;
147 }
148
149 /* This will cause the read in the child to return. */
150 close (fd[0]);
151 close (fd[1]);
152 close (fd[2]);
153 close (fd[3]);
154
155 void *ret;
156 if (pthread_join (th, &ret) != 0)
157 {
158 puts ("join failed");
159 return 1;
160 }
161
162 if (ret != PTHREAD_CANCELED)
163 {
164 puts ("result is wrong");
165 return 1;
166 }
167
168 if (cleanups != 0x1234L)
169 {
170 printf ("called cleanups %lx\n", cleanups);
171 return 1;
172 }
173
174 return 0;
175}
176
177
178static int
179do_test (void)
180{
181 stack_t ss;
182 ss.ss_sp = malloc (2 * SIGSTKSZ);
183 if (ss.ss_sp == NULL)
184 {
185 puts ("failed to allocate alternate stack");
186 return 1;
187 }
188 ss.ss_flags = 0;
189 ss.ss_size = 2 * SIGSTKSZ;
190 if (sigaltstack (&ss, NULL) < 0)
191 {
192 printf ("sigaltstack failed %m\n");
193 return 1;
194 }
195
196 if (pthread_barrier_init (&b, NULL, 2) != 0)
197 {
198 puts ("barrier_init failed");
199 return 1;
200 }
201
202 struct sigaction sa;
203 sa.sa_handler = sh;
204 sigemptyset (&sa.sa_mask);
205 sa.sa_flags = 0;
206
207 if (sigaction (SIGHUP, &sa, NULL) != 0)
208 {
209 puts ("sigaction failed");
210 return 1;
211 }
212
213 puts ("sa_flags = 0 test");
214 if (do_one_test ())
215 return 1;
216
217 sa.sa_handler = sh;
218 sigemptyset (&sa.sa_mask);
219 sa.sa_flags = SA_ONSTACK;
220
221 if (sigaction (SIGHUP, &sa, NULL) != 0)
222 {
223 puts ("sigaction failed");
224 return 1;
225 }
226
227 puts ("sa_flags = SA_ONSTACK test");
228 if (do_one_test ())
229 return 1;
230
231 sa.sa_sigaction = (void (*)(int, siginfo_t *, void *)) sh;
232 sigemptyset (&sa.sa_mask);
233 sa.sa_flags = SA_SIGINFO;
234
235 if (sigaction (SIGHUP, &sa, NULL) != 0)
236 {
237 puts ("sigaction failed");
238 return 1;
239 }
240
241 puts ("sa_flags = SA_SIGINFO test");
242 if (do_one_test ())
243 return 1;
244
245 sa.sa_sigaction = (void (*)(int, siginfo_t *, void *)) sh;
246 sigemptyset (&sa.sa_mask);
247 sa.sa_flags = SA_SIGINFO | SA_ONSTACK;
248
249 if (sigaction (SIGHUP, &sa, NULL) != 0)
250 {
251 puts ("sigaction failed");
252 return 1;
253 }
254
255 puts ("sa_flags = SA_SIGINFO|SA_ONSTACK test");
256 if (do_one_test ())
257 return 1;
258
259 return 0;
260}
261
262#define TIMEOUT 40
263#define TEST_FUNCTION do_test ()
264#include "../test-skeleton.c"
Note: See TracBrowser for help on using the repository browser.