* configure.in: Avoid variable named BASH.
[glibc.git] / nptl / tst-cancel16.c
blob709902e97e4804e9d113905513169a8aea4f27fb
1 /* Copyright (C) 2003 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2003.
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.
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.
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. */
20 #include <errno.h>
21 #include <pthread.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <sys/mman.h>
27 #include <sys/wait.h>
30 static pthread_barrier_t b2;
31 static int fd;
32 static int called;
35 static void
36 cl (void *arg)
38 called = 1;
42 static void *
43 tf (void *arg)
45 int r = pthread_barrier_wait (&b2);
46 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
48 puts ("child thread: barrier_wait failed");
49 exit (1);
52 pthread_cleanup_push (cl, NULL);
54 /* This call should never return. */
55 (void) lockf (fd, F_LOCK, 0);
57 pthread_cleanup_pop (0);
59 return NULL;
63 static int
64 do_test (void)
66 char fname[] = "/tmp/cancel16XXXXXX";
67 fd = mkstemp (fname);
68 if (fd == -1)
70 puts ("mkstemp failed");
71 return 1;
73 unlink (fname);
75 char mem[sizeof (pthread_barrier_t)];
76 memset (mem, '\0', sizeof (mem));
77 if (TEMP_FAILURE_RETRY (pwrite (fd, mem, sizeof (mem), 0)) != sizeof (mem))
79 puts ("pwrite failed");
80 return 1;
83 void *p = mmap (NULL, sizeof (mem), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
84 if (p == MAP_FAILED)
86 puts ("mmap failed");
87 return 1;
89 pthread_barrier_t *b = (pthread_barrier_t *) p;
91 pthread_barrierattr_t ba;
92 if (pthread_barrierattr_init (&ba) != 0)
94 puts ("barrierattr_init failed");
95 return 1;
97 if (pthread_barrierattr_setpshared (&ba, 1) != 0)
99 puts ("barrierattr_setshared failed");
100 return 1;
103 if (pthread_barrier_init (b, &ba, 2) != 0)
105 puts ("1st barrier_init failed");
106 return 1;
108 if (pthread_barrierattr_destroy (&ba) != 0)
110 puts ("barrier_destroy failed");
111 return 1;
114 pid_t pid = fork ();
115 if (pid == 0)
117 /* Child. Lock the file and wait. */
118 if (lockf (fd, F_LOCK, 0) != 0)
120 puts ("child process: lockf failed");
121 _exit (1);
124 int r = pthread_barrier_wait (b);
125 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
127 puts ("child process: 1st barrier_wait failed");
128 _exit (1);
131 /* Make sure the process dies. */
132 alarm (5);
134 r = pthread_barrier_wait (b);
135 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
137 puts ("child process: 2nd barrier_wait failed");
138 _exit (1);
141 _exit (0);
143 if (pid == -1)
145 puts ("fork failed");
146 return 1;
149 int r = pthread_barrier_wait (b);
150 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
152 puts ("main: 1st barrier_wait failed");
153 _exit (1);
156 if (pthread_barrier_init (&b2, NULL, 2) != 0)
158 puts ("2nd barrier_init failed");
159 return 1;
162 pthread_t th;
163 if (pthread_create (&th, NULL, tf, NULL) != 0)
165 puts ("create failed");
166 return 1;
169 r = pthread_barrier_wait (&b2);
170 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
172 puts ("main: 2nd barrier_wait failed");
173 return 1;
176 /* Delay. */
177 sleep (1);
179 if (pthread_cancel (th) != 0)
181 puts ("cancel failed");
182 return 1;
185 void *result;
186 if (pthread_join (th, &result) != 0)
188 puts ("join failed");
189 return 1;
191 if (result != PTHREAD_CANCELED)
193 puts ("thread not canceled");
194 return 1;
196 if (called == 0)
198 puts ("cleanup handler not called");
199 return 1;
202 r = pthread_barrier_wait (b);
203 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
205 puts ("main: 3rd barrier_wait failed");
206 return 1;
209 int status;
210 if (TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)) != pid)
212 puts ("waitpid failed");
213 return 1;
215 if (WEXITSTATUS (status) != 0)
217 printf ("child process exits with %d\n", WEXITSTATUS (status));
218 return 1;
221 if (lockf (fd, F_LOCK, 0) != 0)
223 puts ("main: lockf failed");
224 return 1;
227 return 0;
230 #define TEST_FUNCTION do_test ()
231 #include "../test-skeleton.c"