OSDN Git Service

test: get out of the endless while loop, when bind failed
[uclinux-h8/uClibc.git] / test / nptl / tst-cancel4.c
1 /* Copyright (C) 2002, 2003, 2004, 2006, 2007 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3    Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
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 /* NOTE: this tests functionality beyond POSIX.  POSIX does not allow
20    exit to be called more than once.  */
21
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <limits.h>
25 #include <pthread.h>
26 #include <stddef.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <termios.h>
31 #include <unistd.h>
32 #include <sys/mman.h>
33 #include <sys/msg.h>
34 #include <sys/poll.h>
35 #include <sys/select.h>
36 #include <sys/socket.h>
37 #include <sys/uio.h>
38 #include <sys/un.h>
39 #include <sys/wait.h>
40
41 #include "pthreadP.h"
42
43
44 /* Since STREAMS are not supported in the standard Linux kernel and
45    there we don't advertise STREAMS as supported is no need to test
46    the STREAMS related functions.  This affects
47      getmsg()              getpmsg()          putmsg()
48      putpmsg()
49
50    lockf() and fcntl() are tested in tst-cancel16.
51
52    pthread_join() is tested in tst-join5.
53
54    pthread_testcancel()'s only purpose is to allow cancellation.  This
55    is tested in several places.
56
57    sem_wait() and sem_timedwait() are checked in tst-cancel1[2345] tests.
58
59    mq_send(), mq_timedsend(), mq_receive() and mq_timedreceive() are checked
60    in tst-mqueue8{,x} tests.
61
62    aio_suspend() is tested in tst-cancel17.
63
64    clock_nanosleep() is tested in tst-cancel18.
65 */
66
67 /* Pipe descriptors.  */
68 static int fds[2];
69
70 /* Temporary file descriptor, to be closed after each round.  */
71 static int tempfd = -1;
72 static int tempfd2 = -1;
73 /* Name of temporary file to be removed after each round.  */
74 static char *tempfname;
75 /* Temporary message queue.  */
76 static int tempmsg = -1;
77
78 /* Often used barrier for two threads.  */
79 static pthread_barrier_t b2;
80
81
82 #ifndef IPC_ADDVAL
83 # define IPC_ADDVAL 0
84 #endif
85
86 /* The WRITE_BUFFER_SIZE value needs to be chosen such that if we set
87    the socket send buffer size to '1', a write of this size on that
88    socket will block.
89
90    The Linux kernel imposes a minimum send socket buffer size which
91    has changed over the years.  As of Linux 3.10 the value is:
92
93      2 * (2048 + SKB_DATA_ALIGN(sizeof(struct sk_buff)))
94
95    which is attempting to make sure that with standard MTUs,
96    TCP can always queue up at least 2 full sized packets.
97
98    Furthermore, there is logic in the socket send paths that
99    will allow one more packet (of any size) to be queued up as
100    long as some socket buffer space remains.   Blocking only
101    occurs when we try to queue up a new packet and the send
102    buffer space has already been fully consumed.
103
104    Therefore we must set this value to the largest possible value of
105    the formula above (and since it depends upon the size of "struct
106    sk_buff", it is dependent upon machine word size etc.) plus some
107    slack space.  */
108
109 #define WRITE_BUFFER_SIZE 16384
110
111 /* Cleanup handling test.  */
112 static int cl_called;
113
114 static void
115 cl (void *arg)
116 {
117   ++cl_called;
118 }
119
120
121
122 static void *
123 tf_read  (void *arg)
124 {
125   int fd;
126   int r;
127
128   if (arg == NULL)
129     fd = fds[0];
130   else
131     {
132       char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
133       tempfd = fd = mkstemp (fname);
134       if (fd == -1)
135         printf ("%s: mkstemp failed\n", __FUNCTION__);
136       unlink (fname);
137
138       r = pthread_barrier_wait (&b2);
139       if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
140         {
141           printf ("%s: barrier_wait failed\n", __FUNCTION__);
142           exit (1);
143         }
144     }
145
146   r = pthread_barrier_wait (&b2);
147   if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
148     {
149       printf ("%s: barrier_wait failed\n", __FUNCTION__);
150       exit (1);
151     }
152
153   ssize_t s;
154   pthread_cleanup_push (cl, NULL);
155
156   char buf[100];
157   s = read (fd, buf, sizeof (buf));
158
159   pthread_cleanup_pop (0);
160
161   printf ("%s: read returns with %zd\n", __FUNCTION__, s);
162
163   exit (1);
164 }
165
166
167 static void *
168 tf_readv  (void *arg)
169 {
170   int fd;
171   int r;
172
173   if (arg == NULL)
174     fd = fds[0];
175   else
176     {
177       char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
178       tempfd = fd = mkstemp (fname);
179       if (fd == -1)
180         printf ("%s: mkstemp failed\n", __FUNCTION__);
181       unlink (fname);
182
183       r = pthread_barrier_wait (&b2);
184       if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
185         {
186           printf ("%s: barrier_wait failed\n", __FUNCTION__);
187           exit (1);
188         }
189     }
190
191   r = pthread_barrier_wait (&b2);
192   if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
193     {
194       printf ("%s: barrier_wait failed\n", __FUNCTION__);
195       exit (1);
196     }
197
198   ssize_t s;
199   pthread_cleanup_push (cl, NULL);
200
201   char buf[100];
202   struct iovec iov[1] = { [0] = { .iov_base = buf, .iov_len = sizeof (buf) } };
203   s = readv (fd, iov, 1);
204
205   pthread_cleanup_pop (0);
206
207   printf ("%s: readv returns with %zd\n", __FUNCTION__, s);
208
209   exit (1);
210 }
211
212
213 static void *
214 tf_write  (void *arg)
215 {
216   int fd;
217   int r;
218
219   if (arg == NULL)
220     fd = fds[1];
221   else
222     {
223       char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
224       tempfd = fd = mkstemp (fname);
225       if (fd == -1)
226         printf ("%s: mkstemp failed\n", __FUNCTION__);
227       unlink (fname);
228
229       r = pthread_barrier_wait (&b2);
230       if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
231         {
232           printf ("%s: barrier_wait failed\n", __FUNCTION__);
233           exit (1);
234         }
235     }
236
237   r = pthread_barrier_wait (&b2);
238   if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
239     {
240       printf ("%s: barrier_wait failed\n", __FUNCTION__);
241       exit (1);
242     }
243
244   ssize_t s;
245   pthread_cleanup_push (cl, NULL);
246
247   char buf[WRITE_BUFFER_SIZE];
248   memset (buf, '\0', sizeof (buf));
249   s = write (fd, buf, sizeof (buf));
250
251   pthread_cleanup_pop (0);
252
253   printf ("%s: write returns with %zd\n", __FUNCTION__, s);
254
255   exit (1);
256 }
257
258
259 static void *
260 tf_writev  (void *arg)
261 {
262   int fd;
263   int r;
264
265   if (arg == NULL)
266     fd = fds[1];
267   else
268     {
269       char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
270       tempfd = fd = mkstemp (fname);
271       if (fd == -1)
272         printf ("%s: mkstemp failed\n", __FUNCTION__);
273       unlink (fname);
274
275       r = pthread_barrier_wait (&b2);
276       if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
277         {
278           printf ("%s: barrier_wait failed\n", __FUNCTION__);
279           exit (1);
280         }
281     }
282
283   r = pthread_barrier_wait (&b2);
284   if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
285     {
286       printf ("%s: barrier_wait failed\n", __FUNCTION__);
287       exit (1);
288     }
289
290   ssize_t s;
291   pthread_cleanup_push (cl, NULL);
292
293   char buf[WRITE_BUFFER_SIZE];
294   memset (buf, '\0', sizeof (buf));
295   struct iovec iov[1] = { [0] = { .iov_base = buf, .iov_len = sizeof (buf) } };
296   s = writev (fd, iov, 1);
297
298   pthread_cleanup_pop (0);
299
300   printf ("%s: writev returns with %zd\n", __FUNCTION__, s);
301
302   exit (1);
303 }
304
305
306 static void *
307 tf_sleep (void *arg)
308 {
309   int r = pthread_barrier_wait (&b2);
310   if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
311     {
312       printf ("%s: barrier_wait failed\n", __FUNCTION__);
313       exit (1);
314     }
315
316   if (arg != NULL)
317     {
318       r = pthread_barrier_wait (&b2);
319       if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
320         {
321           printf ("%s: barrier_wait failed\n", __FUNCTION__);
322           exit (1);
323         }
324     }
325
326   pthread_cleanup_push (cl, NULL);
327
328   sleep (arg == NULL ? 1000000 : 0);
329
330   pthread_cleanup_pop (0);
331
332   printf ("%s: sleep returns\n", __FUNCTION__);
333
334   exit (1);
335 }
336
337
338 static void *
339 tf_usleep (void *arg)
340 {
341   int r = pthread_barrier_wait (&b2);
342   if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
343     {
344       printf ("%s: barrier_wait failed\n", __FUNCTION__);
345       exit (1);
346     }
347
348   if (arg != NULL)
349     {
350       r = pthread_barrier_wait (&b2);
351       if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
352         {
353           printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
354           exit (1);
355         }
356     }
357
358   pthread_cleanup_push (cl, NULL);
359
360   usleep (arg == NULL ? (useconds_t) ULONG_MAX : 0);
361
362   pthread_cleanup_pop (0);
363
364   printf ("%s: usleep returns\n", __FUNCTION__);
365
366   exit (1);
367 }
368
369
370 static void *
371 tf_nanosleep (void *arg)
372 {
373   int r = pthread_barrier_wait (&b2);
374   if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
375     {
376       printf ("%s: barrier_wait failed\n", __FUNCTION__);
377       exit (1);
378     }
379
380   if (arg != NULL)
381     {
382       r = pthread_barrier_wait (&b2);
383       if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
384         {
385           printf ("%s: barrier_wait failed\n", __FUNCTION__);
386           exit (1);
387         }
388     }
389
390   pthread_cleanup_push (cl, NULL);
391
392   struct timespec ts = { .tv_sec = arg == NULL ? 10000000 : 0, .tv_nsec = 0 };
393   TEMP_FAILURE_RETRY (nanosleep (&ts, &ts));
394
395   pthread_cleanup_pop (0);
396
397   printf ("%s: nanosleep returns\n", __FUNCTION__);
398
399   exit (1);
400 }
401
402
403 static void *
404 tf_select (void *arg)
405 {
406   int fd;
407   int r;
408
409   if (arg == NULL)
410     fd = fds[0];
411   else
412     {
413       char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
414       tempfd = fd = mkstemp (fname);
415       if (fd == -1)
416         printf ("%s: mkstemp failed\n", __FUNCTION__);
417       unlink (fname);
418
419       r = pthread_barrier_wait (&b2);
420       if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
421         {
422           printf ("%s: barrier_wait failed\n", __FUNCTION__);
423           exit (1);
424         }
425     }
426
427   r = pthread_barrier_wait (&b2);
428   if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
429     {
430       printf ("%s: barrier_wait failed\n", __FUNCTION__);
431       exit (1);
432     }
433
434   fd_set rfs;
435   FD_ZERO (&rfs);
436   FD_SET (fd, &rfs);
437
438   int s;
439   pthread_cleanup_push (cl, NULL);
440
441   s = select (fd + 1, &rfs, NULL, NULL, NULL);
442
443   pthread_cleanup_pop (0);
444
445   printf ("%s: select returns with %d (%s)\n", __FUNCTION__, s,
446           strerror (errno));
447
448   exit (1);
449 }
450
451
452 static void *
453 tf_pselect (void *arg)
454 {
455   int fd;
456   int r;
457
458   if (arg == NULL)
459     fd = fds[0];
460   else
461     {
462       char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
463       tempfd = fd = mkstemp (fname);
464       if (fd == -1)
465         printf ("%s: mkstemp failed\n", __FUNCTION__);
466       unlink (fname);
467
468       r = pthread_barrier_wait (&b2);
469       if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
470         {
471           printf ("%s: barrier_wait failed\n", __FUNCTION__);
472           exit (1);
473         }
474     }
475
476   r = pthread_barrier_wait (&b2);
477   if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
478     {
479       printf ("%s: barrier_wait failed\n", __FUNCTION__);
480       exit (1);
481     }
482
483   fd_set rfs;
484   FD_ZERO (&rfs);
485   FD_SET (fd, &rfs);
486
487   int s;
488   pthread_cleanup_push (cl, NULL);
489
490   s = pselect (fd + 1, &rfs, NULL, NULL, NULL, NULL);
491
492   pthread_cleanup_pop (0);
493
494   printf ("%s: pselect returns with %d (%s)\n", __FUNCTION__, s,
495           strerror (errno));
496
497   exit (1);
498 }
499
500
501 static void *
502 tf_poll (void *arg)
503 {
504   int fd;
505   int r;
506
507   if (arg == NULL)
508     fd = fds[0];
509   else
510     {
511       char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
512       tempfd = fd = mkstemp (fname);
513       if (fd == -1)
514         printf ("%s: mkstemp failed\n", __FUNCTION__);
515       unlink (fname);
516
517       r = pthread_barrier_wait (&b2);
518       if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
519         {
520           printf ("%s: barrier_wait failed\n", __FUNCTION__);
521           exit (1);
522         }
523     }
524
525   r = pthread_barrier_wait (&b2);
526   if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
527     {
528       printf ("%s: barrier_wait failed\n", __FUNCTION__);
529       exit (1);
530     }
531
532   struct pollfd rfs[1] = { [0] = { .fd = fd, .events = POLLIN } };
533
534   int s;
535   pthread_cleanup_push (cl, NULL);
536
537   s = poll (rfs, 1, -1);
538
539   pthread_cleanup_pop (0);
540
541   printf ("%s: poll returns with %d (%s)\n", __FUNCTION__, s,
542           strerror (errno));
543
544   exit (1);
545 }
546
547
548 static void *
549 tf_ppoll (void *arg)
550 {
551   int fd;
552   int r;
553
554   if (arg == NULL)
555     fd = fds[0];
556   else
557     {
558       char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
559       tempfd = fd = mkstemp (fname);
560       if (fd == -1)
561         printf ("%s: mkstemp failed\n", __FUNCTION__);
562       unlink (fname);
563
564       r = pthread_barrier_wait (&b2);
565       if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
566         {
567           printf ("%s: barrier_wait failed\n", __FUNCTION__);
568           exit (1);
569         }
570     }
571
572   r = pthread_barrier_wait (&b2);
573   if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
574     {
575       printf ("%s: barrier_wait failed\n", __FUNCTION__);
576       exit (1);
577     }
578
579   struct pollfd rfs[1] = { [0] = { .fd = fd, .events = POLLIN } };
580
581   int s;
582   pthread_cleanup_push (cl, NULL);
583
584   s = ppoll (rfs, 1, NULL, NULL);
585
586   pthread_cleanup_pop (0);
587
588   printf ("%s: ppoll returns with %d (%s)\n", __FUNCTION__, s,
589           strerror (errno));
590
591   exit (1);
592 }
593
594
595 static void *
596 tf_wait (void *arg)
597 {
598   pid_t pid = fork ();
599   if (pid == -1)
600     {
601       puts ("fork failed");
602       exit (1);
603     }
604
605   if (pid == 0)
606     {
607       /* Make the program disappear after a while.  */
608       if (arg == NULL)
609         sleep (10);
610       exit (0);
611     }
612
613   int r;
614   if (arg != NULL)
615     {
616       struct timespec  ts = { .tv_sec = 0, .tv_nsec = 100000000 };
617       while (nanosleep (&ts, &ts) != 0)
618         continue;
619
620       r = pthread_barrier_wait (&b2);
621       if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
622         {
623           printf ("%s: barrier_wait failed\n", __FUNCTION__);
624           exit (1);
625         }
626     }
627
628   r = pthread_barrier_wait (&b2);
629   if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
630     {
631       printf ("%s: barrier_wait failed\n", __FUNCTION__);
632       exit (1);
633     }
634
635   int s;
636   pthread_cleanup_push (cl, NULL);
637
638   s = wait (NULL);
639
640   pthread_cleanup_pop (0);
641
642   printf ("%s: wait returns with %d (%s)\n", __FUNCTION__, s,
643           strerror (errno));
644
645   exit (1);
646 }
647
648
649 static void *
650 tf_waitpid (void *arg)
651 {
652
653   pid_t pid = fork ();
654   if (pid == -1)
655     {
656       puts ("fork failed");
657       exit (1);
658     }
659
660   if (pid == 0)
661     {
662       /* Make the program disappear after a while.  */
663       if (arg == NULL)
664         sleep (10);
665       exit (0);
666     }
667
668   int r;
669   if (arg != NULL)
670     {
671       struct timespec  ts = { .tv_sec = 0, .tv_nsec = 100000000 };
672       while (nanosleep (&ts, &ts) != 0)
673         continue;
674
675       r = pthread_barrier_wait (&b2);
676       if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
677         {
678           printf ("%s: barrier_wait failed\n", __FUNCTION__);
679           exit (1);
680         }
681     }
682
683   r = pthread_barrier_wait (&b2);
684   if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
685     {
686       printf ("%s: barrier_wait failed\n", __FUNCTION__);
687       exit (1);
688     }
689
690   int s;
691  pthread_cleanup_push (cl, NULL);
692
693   s = waitpid (-1, NULL, 0);
694
695   pthread_cleanup_pop (0);
696
697   printf ("%s: waitpid returns with %d (%s)\n", __FUNCTION__, s,
698           strerror (errno));
699
700   exit (1);
701 }
702
703
704 static void *
705 tf_waitid (void *arg)
706 {
707   pid_t pid = fork ();
708   if (pid == -1)
709     {
710       puts ("fork failed");
711       exit (1);
712     }
713
714   if (pid == 0)
715     {
716       /* Make the program disappear after a while.  */
717       if (arg == NULL)
718         sleep (10);
719       exit (0);
720     }
721
722   int r;
723   if (arg != NULL)
724     {
725       struct timespec  ts = { .tv_sec = 0, .tv_nsec = 100000000 };
726       while (nanosleep (&ts, &ts) != 0)
727         continue;
728
729       r = pthread_barrier_wait (&b2);
730       if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
731         {
732           printf ("%s: barrier_wait failed\n", __FUNCTION__);
733           exit (1);
734         }
735     }
736
737   r = pthread_barrier_wait (&b2);
738   if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
739     {
740       printf ("%s: barrier_wait failed\n", __FUNCTION__);
741       exit (1);
742     }
743
744   int s;
745   pthread_cleanup_push (cl, NULL);
746
747 #ifndef WEXITED
748 # define WEXITED 0
749 #endif
750   siginfo_t si;
751   s = waitid (P_PID, pid, &si, WEXITED);
752
753   pthread_cleanup_pop (0);
754
755   printf ("%s: waitid returns with %d (%s)\n", __FUNCTION__, s,
756           strerror (errno));
757
758   exit (1);
759 }
760
761
762 static void *
763 tf_sigpause (void *arg)
764 {
765   int r = pthread_barrier_wait (&b2);
766   if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
767     {
768       printf ("%s: barrier_wait failed\n", __FUNCTION__);
769       exit (1);
770     }
771
772   if (arg != NULL)
773     {
774       r = pthread_barrier_wait (&b2);
775       if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
776         {
777           printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
778           exit (1);
779         }
780     }
781
782   pthread_cleanup_push (cl, NULL);
783
784   sigpause (SIGCANCEL);
785
786   pthread_cleanup_pop (0);
787
788   printf ("%s: sigpause returned\n", __FUNCTION__);
789
790   exit (1);
791 }
792
793
794 static void *
795 tf_sigsuspend (void *arg)
796 {
797   int r = pthread_barrier_wait (&b2);
798   if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
799     {
800       printf ("%s: barrier_wait failed\n", __FUNCTION__);
801       exit (1);
802     }
803
804   if (arg != NULL)
805     {
806       r = pthread_barrier_wait (&b2);
807       if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
808         {
809           printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
810           exit (1);
811         }
812     }
813
814   pthread_cleanup_push (cl, NULL);
815
816   /* Just for fun block all signals.  */
817   sigset_t mask;
818   sigfillset (&mask);
819   sigsuspend (&mask);
820
821   pthread_cleanup_pop (0);
822
823   printf ("%s: sigsuspend returned\n", __FUNCTION__);
824
825   exit (1);
826 }
827
828
829 static void *
830 tf_sigwait (void *arg)
831 {
832   int r = pthread_barrier_wait (&b2);
833   if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
834     {
835       printf ("%s: barrier_wait failed\n", __FUNCTION__);
836       exit (1);
837     }
838
839   if (arg != NULL)
840     {
841       r = pthread_barrier_wait (&b2);
842       if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
843         {
844           printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
845           exit (1);
846         }
847     }
848
849   /* Block SIGUSR1.  */
850   sigset_t mask;
851   sigemptyset (&mask);
852   sigaddset (&mask, SIGUSR1);
853   if (pthread_sigmask (SIG_BLOCK, &mask, NULL) != 0)
854     {
855       printf ("%s: pthread_sigmask failed\n", __FUNCTION__);
856       exit (1);
857     }
858
859   int sig;
860   pthread_cleanup_push (cl, NULL);
861
862   /* Wait for SIGUSR1.  */
863   sigwait (&mask, &sig);
864
865   pthread_cleanup_pop (0);
866
867   printf ("%s: sigwait returned with signal %d\n", __FUNCTION__, sig);
868
869   exit (1);
870 }
871
872
873 static void *
874 tf_sigwaitinfo (void *arg)
875 {
876   int r = pthread_barrier_wait (&b2);
877   if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
878     {
879       printf ("%s: barrier_wait failed\n", __FUNCTION__);
880       exit (1);
881     }
882
883   if (arg != NULL)
884     {
885       r = pthread_barrier_wait (&b2);
886       if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
887         {
888           printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
889           exit (1);
890         }
891     }
892
893   /* Block SIGUSR1.  */
894   sigset_t mask;
895   sigemptyset (&mask);
896   sigaddset (&mask, SIGUSR1);
897   if (pthread_sigmask (SIG_BLOCK, &mask, NULL) != 0)
898     {
899       printf ("%s: pthread_sigmask failed\n", __FUNCTION__);
900       exit (1);
901     }
902
903   siginfo_t info;
904   pthread_cleanup_push (cl, NULL);
905
906   /* Wait for SIGUSR1.  */
907   sigwaitinfo (&mask, &info);
908
909   pthread_cleanup_pop (0);
910
911   printf ("%s: sigwaitinfo returned with signal %d\n", __FUNCTION__,
912           info.si_signo);
913
914   exit (1);
915 }
916
917
918 static void *
919 tf_sigtimedwait (void *arg)
920 {
921   int r = pthread_barrier_wait (&b2);
922   if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
923     {
924       printf ("%s: barrier_wait failed\n", __FUNCTION__);
925       exit (1);
926     }
927
928   if (arg != NULL)
929     {
930       r = pthread_barrier_wait (&b2);
931       if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
932         {
933           printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
934           exit (1);
935         }
936     }
937
938   /* Block SIGUSR1.  */
939   sigset_t mask;
940   sigemptyset (&mask);
941   sigaddset (&mask, SIGUSR1);
942   if (pthread_sigmask (SIG_BLOCK, &mask, NULL) != 0)
943     {
944       printf ("%s: pthread_sigmask failed\n", __FUNCTION__);
945       exit (1);
946     }
947
948   /* Wait for SIGUSR1.  */
949   siginfo_t info;
950   struct timespec ts = { .tv_sec = 60, .tv_nsec = 0 };
951   pthread_cleanup_push (cl, NULL);
952
953   sigtimedwait (&mask, &info, &ts);
954
955   pthread_cleanup_pop (0);
956
957   printf ("%s: sigtimedwait returned with signal %d\n", __FUNCTION__,
958           info.si_signo);
959
960   exit (1);
961 }
962
963
964 static void *
965 tf_pause (void *arg)
966 {
967   int r = pthread_barrier_wait (&b2);
968   if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
969     {
970       printf ("%s: barrier_wait failed\n", __FUNCTION__);
971       exit (1);
972     }
973
974   if (arg != NULL)
975     {
976       r = pthread_barrier_wait (&b2);
977       if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
978         {
979           printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
980           exit (1);
981         }
982     }
983
984   pthread_cleanup_push (cl, NULL);
985
986   pause ();
987
988   pthread_cleanup_pop (0);
989
990   printf ("%s: pause returned\n", __FUNCTION__);
991
992   exit (1);
993 }
994
995
996 static void *
997 tf_accept (void *arg)
998 {
999   int tfd;
1000   struct sockaddr_un sun;
1001   /* To test a non-blocking accept call we make the call file by using
1002      a datagrame socket.  */
1003   int pf = arg == NULL ? SOCK_STREAM : SOCK_DGRAM;
1004
1005   tempfd = socket (AF_UNIX, pf, 0);
1006   if (tempfd == -1)
1007     {
1008       printf ("%s: socket call failed\n", __FUNCTION__);
1009       exit (1);
1010     }
1011
1012   int tries = 0;
1013   do
1014     {
1015       if (++tries > 10)
1016         {
1017           printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
1018           /* prevent endless loop, when bind fails forever */
1019           exit (1);
1020         }
1021
1022       strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-1-XXXXXX");
1023       tfd = mkstemp(sun.sun_path);
1024       if (tfd < 0)
1025         {
1026           printf ("%s: cannot generate temp file name\n", __FUNCTION__);
1027           exit (1);
1028         }
1029       close(tfd);
1030       sun.sun_family = AF_UNIX;
1031     }
1032   while (bind (tempfd, (struct sockaddr *) &sun,
1033                offsetof (struct sockaddr_un, sun_path)
1034                + strlen (sun.sun_path) + 1) != 0);
1035
1036   unlink (sun.sun_path);
1037
1038   listen (tempfd, 5);
1039
1040   socklen_t len = sizeof (sun);
1041
1042   int r = pthread_barrier_wait (&b2);
1043   if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1044     {
1045       printf ("%s: barrier_wait failed\n", __FUNCTION__);
1046       exit (1);
1047     }
1048
1049   if (arg != NULL)
1050     {
1051       r = pthread_barrier_wait (&b2);
1052       if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1053         {
1054           printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1055           exit (1);
1056         }
1057     }
1058
1059   pthread_cleanup_push (cl, NULL);
1060
1061   accept (tempfd, (struct sockaddr *) &sun, &len);
1062
1063   pthread_cleanup_pop (0);
1064
1065   printf ("%s: accept returned\n", __FUNCTION__);
1066
1067   exit (1);
1068 }
1069
1070
1071 static void *
1072 tf_send (void *arg)
1073 {
1074   int tfd;
1075   struct sockaddr_un sun;
1076
1077   tempfd = socket (AF_UNIX, SOCK_STREAM, 0);
1078   if (tempfd == -1)
1079     {
1080       printf ("%s: first socket call failed\n", __FUNCTION__);
1081       exit (1);
1082     }
1083
1084   int tries = 0;
1085   do
1086     {
1087       if (++tries > 10)
1088         {
1089           printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
1090         }
1091
1092       strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-2-XXXXXX");
1093       tfd = mkstemp(sun.sun_path);
1094       if (tfd < 0)
1095         {
1096           printf ("%s: cannot generate temp file name\n", __FUNCTION__);
1097           exit (1);
1098         }
1099       close(tfd);
1100       sun.sun_family = AF_UNIX;
1101     }
1102   while (bind (tempfd, (struct sockaddr *) &sun,
1103                offsetof (struct sockaddr_un, sun_path)
1104                + strlen (sun.sun_path) + 1) != 0);
1105
1106   listen (tempfd, 5);
1107
1108   tempfd2 = socket (AF_UNIX, SOCK_STREAM, 0);
1109   if (tempfd2 == -1)
1110     {
1111       printf ("%s: second socket call failed\n", __FUNCTION__);
1112       exit (1);
1113     }
1114
1115   if (connect (tempfd2, (struct sockaddr *) &sun, sizeof (sun)) != 0)
1116     {
1117       printf ("%s: connect failed\n", __FUNCTION__);
1118       exit(1);
1119     }
1120
1121   unlink (sun.sun_path);
1122
1123   int r = pthread_barrier_wait (&b2);
1124   if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1125     {
1126       printf ("%s: barrier_wait failed\n", __FUNCTION__);
1127       exit (1);
1128     }
1129
1130   if (arg != NULL)
1131     {
1132       r = pthread_barrier_wait (&b2);
1133       if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1134         {
1135           printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1136           exit (1);
1137         }
1138     }
1139
1140   pthread_cleanup_push (cl, NULL);
1141
1142   /* Very large block, so that the send call blocks.  */
1143   char mem[700000];
1144
1145   send (tempfd2, mem, arg == NULL ? sizeof (mem) : 1, 0);
1146
1147   pthread_cleanup_pop (0);
1148
1149   printf ("%s: send returned\n", __FUNCTION__);
1150
1151   exit (1);
1152 }
1153
1154
1155 static void *
1156 tf_recv (void *arg)
1157 {
1158   int tfd;
1159   struct sockaddr_un sun;
1160
1161   tempfd = socket (AF_UNIX, SOCK_STREAM, 0);
1162   if (tempfd == -1)
1163     {
1164       printf ("%s: first socket call failed\n", __FUNCTION__);
1165       exit (1);
1166     }
1167
1168   int tries = 0;
1169   do
1170     {
1171       if (++tries > 10)
1172         {
1173           printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
1174         }
1175
1176       strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-3-XXXXXX");
1177       tfd = mkstemp(sun.sun_path);
1178       if (tfd < 0)
1179         {
1180           printf ("%s: cannot generate temp file name\n", __FUNCTION__);
1181           exit (1);
1182         }
1183       close(tfd);
1184       sun.sun_family = AF_UNIX;
1185     }
1186   while (bind (tempfd, (struct sockaddr *) &sun,
1187                offsetof (struct sockaddr_un, sun_path)
1188                + strlen (sun.sun_path) + 1) != 0);
1189
1190   listen (tempfd, 5);
1191
1192   tempfd2 = socket (AF_UNIX, SOCK_STREAM, 0);
1193   if (tempfd2 == -1)
1194     {
1195       printf ("%s: second socket call failed\n", __FUNCTION__);
1196       exit (1);
1197     }
1198
1199   if (connect (tempfd2, (struct sockaddr *) &sun, sizeof (sun)) != 0)
1200     {
1201       printf ("%s: connect failed\n", __FUNCTION__);
1202       exit(1);
1203     }
1204
1205   unlink (sun.sun_path);
1206
1207   int r = pthread_barrier_wait (&b2);
1208   if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1209     {
1210       printf ("%s: barrier_wait failed\n", __FUNCTION__);
1211       exit (1);
1212     }
1213
1214   if (arg != NULL)
1215     {
1216       r = pthread_barrier_wait (&b2);
1217       if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1218         {
1219           printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1220           exit (1);
1221         }
1222     }
1223
1224   pthread_cleanup_push (cl, NULL);
1225
1226   char mem[70];
1227
1228   recv (tempfd2, mem, arg == NULL ? sizeof (mem) : 0, 0);
1229
1230   pthread_cleanup_pop (0);
1231
1232   printf ("%s: recv returned\n", __FUNCTION__);
1233
1234   exit (1);
1235 }
1236
1237
1238 static void *
1239 tf_recvfrom (void *arg)
1240 {
1241   int tfd;
1242   struct sockaddr_un sun;
1243
1244   tempfd = socket (AF_UNIX, SOCK_DGRAM, 0);
1245   if (tempfd == -1)
1246     {
1247       printf ("%s: first socket call failed\n", __FUNCTION__);
1248       exit (1);
1249     }
1250
1251   int tries = 0;
1252   do
1253     {
1254       if (++tries > 10)
1255         {
1256           printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
1257         }
1258
1259       strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-4-XXXXXX");
1260       tfd = mkstemp(sun.sun_path);
1261       if (tfd < 0)
1262         {
1263           printf ("%s: cannot generate temp file name\n", __FUNCTION__);
1264           exit (1);
1265         }
1266       close(tfd);
1267       sun.sun_family = AF_UNIX;
1268     }
1269   while (bind (tempfd, (struct sockaddr *) &sun,
1270                offsetof (struct sockaddr_un, sun_path)
1271                + strlen (sun.sun_path) + 1) != 0);
1272
1273   tempfname = strdup (sun.sun_path);
1274
1275   tempfd2 = socket (AF_UNIX, SOCK_DGRAM, 0);
1276   if (tempfd2 == -1)
1277     {
1278       printf ("%s: second socket call failed\n", __FUNCTION__);
1279       exit (1);
1280     }
1281
1282   int r = pthread_barrier_wait (&b2);
1283   if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1284     {
1285       printf ("%s: barrier_wait failed\n", __FUNCTION__);
1286       exit (1);
1287     }
1288
1289   if (arg != NULL)
1290     {
1291       r = pthread_barrier_wait (&b2);
1292       if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1293         {
1294           printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1295           exit (1);
1296         }
1297     }
1298
1299   pthread_cleanup_push (cl, NULL);
1300
1301   char mem[70];
1302   socklen_t len = sizeof (sun);
1303
1304   recvfrom (tempfd2, mem, arg == NULL ? sizeof (mem) : 0, 0,
1305             (struct sockaddr *) &sun, &len);
1306
1307   pthread_cleanup_pop (0);
1308
1309   printf ("%s: recvfrom returned\n", __FUNCTION__);
1310
1311   exit (1);
1312 }
1313
1314
1315 static void *
1316 tf_recvmsg (void *arg)
1317 {
1318   int tfd;
1319   struct sockaddr_un sun;
1320
1321   tempfd = socket (AF_UNIX, SOCK_DGRAM, 0);
1322   if (tempfd == -1)
1323     {
1324       printf ("%s: first socket call failed\n", __FUNCTION__);
1325       exit (1);
1326     }
1327
1328   int tries = 0;
1329   do
1330     {
1331       if (++tries > 10)
1332         {
1333           printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
1334         }
1335
1336       strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-5-XXXXXX");
1337       tfd = mkstemp(sun.sun_path);
1338       if (tfd < 0)
1339         {
1340           printf ("%s: cannot generate temp file name\n", __FUNCTION__);
1341           exit (1);
1342         }
1343       close(tfd);
1344       sun.sun_family = AF_UNIX;
1345     }
1346   while (bind (tempfd, (struct sockaddr *) &sun,
1347                offsetof (struct sockaddr_un, sun_path)
1348                + strlen (sun.sun_path) + 1) != 0);
1349
1350   tempfname = strdup (sun.sun_path);
1351
1352   tempfd2 = socket (AF_UNIX, SOCK_DGRAM, 0);
1353   if (tempfd2 == -1)
1354     {
1355       printf ("%s: second socket call failed\n", __FUNCTION__);
1356       exit (1);
1357     }
1358
1359   int r = pthread_barrier_wait (&b2);
1360   if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1361     {
1362       printf ("%s: barrier_wait failed\n", __FUNCTION__);
1363       exit (1);
1364     }
1365
1366   if (arg != NULL)
1367     {
1368       r = pthread_barrier_wait (&b2);
1369       if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1370         {
1371           printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1372           exit (1);
1373         }
1374     }
1375
1376   pthread_cleanup_push (cl, NULL);
1377
1378   char mem[70];
1379   struct iovec iov[1];
1380   iov[0].iov_base = mem;
1381   iov[0].iov_len = arg == NULL ? sizeof (mem) : 0;
1382
1383   struct msghdr m;
1384   m.msg_name = &sun;
1385   m.msg_namelen = sizeof (sun);
1386   m.msg_iov = iov;
1387   m.msg_iovlen = 1;
1388   m.msg_control = NULL;
1389   m.msg_controllen = 0;
1390
1391   recvmsg (tempfd2, &m, 0);
1392
1393   pthread_cleanup_pop (0);
1394
1395   printf ("%s: recvmsg returned\n", __FUNCTION__);
1396
1397   exit (1);
1398 }
1399
1400
1401 static void *
1402 tf_open (void *arg)
1403 {
1404   if (arg == NULL)
1405     // XXX If somebody can provide a portable test case in which open()
1406     // blocks we can enable this test to run in both rounds.
1407     abort ();
1408
1409   int r = pthread_barrier_wait (&b2);
1410   if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1411     {
1412       printf ("%s: barrier_wait failed\n", __FUNCTION__);
1413       exit (1);
1414     }
1415
1416   r = pthread_barrier_wait (&b2);
1417   if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1418     {
1419       printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1420       exit (1);
1421     }
1422
1423   pthread_cleanup_push (cl, NULL);
1424
1425   open ("Makefile", O_RDONLY);
1426
1427   pthread_cleanup_pop (0);
1428
1429   printf ("%s: open returned\n", __FUNCTION__);
1430
1431   exit (1);
1432 }
1433
1434
1435 static void *
1436 tf_close (void *arg)
1437 {
1438   if (arg == NULL)
1439     // XXX If somebody can provide a portable test case in which close()
1440     // blocks we can enable this test to run in both rounds.
1441     abort ();
1442
1443   char fname[] = "/tmp/tst-cancel-fd-XXXXXX";
1444   tempfd = mkstemp (fname);
1445   if (tempfd == -1)
1446     {
1447       printf ("%s: mkstemp failed\n", __FUNCTION__);
1448       exit (1);
1449     }
1450   unlink (fname);
1451
1452   int r = pthread_barrier_wait (&b2);
1453   if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1454     {
1455       printf ("%s: barrier_wait failed\n", __FUNCTION__);
1456       exit (1);
1457     }
1458
1459   r = pthread_barrier_wait (&b2);
1460   if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1461     {
1462       printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1463       exit (1);
1464     }
1465
1466   pthread_cleanup_push (cl, NULL);
1467
1468   close (tempfd);
1469
1470   pthread_cleanup_pop (0);
1471
1472   printf ("%s: close returned\n", __FUNCTION__);
1473
1474   exit (1);
1475 }
1476
1477
1478 static void *
1479 tf_pread (void *arg)
1480 {
1481   if (arg == NULL)
1482     // XXX If somebody can provide a portable test case in which pread()
1483     // blocks we can enable this test to run in both rounds.
1484     abort ();
1485
1486   tempfd = open ("Makefile", O_RDONLY);
1487   if (tempfd == -1)
1488     {
1489       printf ("%s: cannot open Makefile\n", __FUNCTION__);
1490       exit (1);
1491     }
1492
1493   int r = pthread_barrier_wait (&b2);
1494   if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1495     {
1496       printf ("%s: barrier_wait failed\n", __FUNCTION__);
1497       exit (1);
1498     }
1499
1500   r = pthread_barrier_wait (&b2);
1501   if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1502     {
1503       printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1504       exit (1);
1505     }
1506
1507   pthread_cleanup_push (cl, NULL);
1508
1509   char mem[10];
1510   pread (tempfd, mem, sizeof (mem), 0);
1511
1512   pthread_cleanup_pop (0);
1513
1514   printf ("%s: pread returned\n", __FUNCTION__);
1515
1516   exit (1);
1517 }
1518
1519
1520 static void *
1521 tf_pwrite (void *arg)
1522 {
1523   if (arg == NULL)
1524     // XXX If somebody can provide a portable test case in which pwrite()
1525     // blocks we can enable this test to run in both rounds.
1526     abort ();
1527
1528   char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
1529   tempfd = mkstemp (fname);
1530   if (tempfd == -1)
1531     {
1532       printf ("%s: mkstemp failed\n", __FUNCTION__);
1533       exit (1);
1534     }
1535   unlink (fname);
1536
1537   int r = pthread_barrier_wait (&b2);
1538   if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1539     {
1540       printf ("%s: barrier_wait failed\n", __FUNCTION__);
1541       exit (1);
1542     }
1543
1544   r = pthread_barrier_wait (&b2);
1545   if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1546     {
1547       printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1548       exit (1);
1549     }
1550
1551   pthread_cleanup_push (cl, NULL);
1552
1553   char mem[10];
1554   pwrite (tempfd, mem, sizeof (mem), 0);
1555
1556   pthread_cleanup_pop (0);
1557
1558   printf ("%s: pwrite returned\n", __FUNCTION__);
1559
1560   exit (1);
1561 }
1562
1563
1564 static void *
1565 tf_fsync (void *arg)
1566 {
1567   if (arg == NULL)
1568     // XXX If somebody can provide a portable test case in which fsync()
1569     // blocks we can enable this test to run in both rounds.
1570     abort ();
1571
1572   tempfd = open ("Makefile", O_RDONLY);
1573   if (tempfd == -1)
1574     {
1575       printf ("%s: cannot open Makefile\n", __FUNCTION__);
1576       exit (1);
1577     }
1578
1579   int r = pthread_barrier_wait (&b2);
1580   if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1581     {
1582       printf ("%s: barrier_wait failed\n", __FUNCTION__);
1583       exit (1);
1584     }
1585
1586   r = pthread_barrier_wait (&b2);
1587   if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1588     {
1589       printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1590       exit (1);
1591     }
1592
1593   pthread_cleanup_push (cl, NULL);
1594
1595   fsync (tempfd);
1596
1597   pthread_cleanup_pop (0);
1598
1599   printf ("%s: fsync returned\n", __FUNCTION__);
1600
1601   exit (1);
1602 }
1603
1604
1605 static void *
1606 tf_fdatasync (void *arg)
1607 {
1608   if (arg == NULL)
1609     // XXX If somebody can provide a portable test case in which fdatasync()
1610     // blocks we can enable this test to run in both rounds.
1611     abort ();
1612
1613   tempfd = open ("Makefile", O_RDONLY);
1614   if (tempfd == -1)
1615     {
1616       printf ("%s: cannot open Makefile\n", __FUNCTION__);
1617       exit (1);
1618     }
1619
1620   int r = pthread_barrier_wait (&b2);
1621   if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1622     {
1623       printf ("%s: barrier_wait failed\n", __FUNCTION__);
1624       exit (1);
1625     }
1626
1627   r = pthread_barrier_wait (&b2);
1628   if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1629     {
1630       printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1631       exit (1);
1632     }
1633
1634   pthread_cleanup_push (cl, NULL);
1635
1636   fdatasync (tempfd);
1637
1638   pthread_cleanup_pop (0);
1639
1640   printf ("%s: fdatasync returned\n", __FUNCTION__);
1641
1642   exit (1);
1643 }
1644
1645
1646 static void *
1647 tf_msync (void *arg)
1648 {
1649   if (arg == NULL)
1650     // XXX If somebody can provide a portable test case in which msync()
1651     // blocks we can enable this test to run in both rounds.
1652     abort ();
1653
1654   tempfd = open ("Makefile", O_RDONLY);
1655   if (tempfd == -1)
1656     {
1657       printf ("%s: cannot open Makefile\n", __FUNCTION__);
1658       exit (1);
1659     }
1660   void *p = mmap (NULL, 10, PROT_READ, MAP_SHARED, tempfd, 0);
1661   if (p == MAP_FAILED)
1662     {
1663       printf ("%s: mmap failed\n", __FUNCTION__);
1664       exit (1);
1665     }
1666
1667   int r = pthread_barrier_wait (&b2);
1668   if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1669     {
1670       printf ("%s: barrier_wait failed\n", __FUNCTION__);
1671       exit (1);
1672     }
1673
1674   r = pthread_barrier_wait (&b2);
1675   if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1676     {
1677       printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1678       exit (1);
1679     }
1680
1681   pthread_cleanup_push (cl, NULL);
1682
1683   msync (p, 10, 0);
1684
1685   pthread_cleanup_pop (0);
1686
1687   printf ("%s: msync returned\n", __FUNCTION__);
1688
1689   exit (1);
1690 }
1691
1692
1693 static void *
1694 tf_sendto (void *arg)
1695 {
1696   int tfd;
1697   if (arg == NULL)
1698     // XXX If somebody can provide a portable test case in which sendto()
1699     // blocks we can enable this test to run in both rounds.
1700     abort ();
1701
1702   struct sockaddr_un sun;
1703
1704   tempfd = socket (AF_UNIX, SOCK_DGRAM, 0);
1705   if (tempfd == -1)
1706     {
1707       printf ("%s: first socket call failed\n", __FUNCTION__);
1708       exit (1);
1709     }
1710
1711   int tries = 0;
1712   do
1713     {
1714       if (++tries > 10)
1715         {
1716           printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
1717         }
1718
1719       strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-6-XXXXXX");
1720       tfd = mkstemp(sun.sun_path);
1721       if (tfd < 0)
1722         {
1723           printf ("%s: cannot generate temp file name\n", __FUNCTION__);
1724           exit (1);
1725         }
1726       close(tfd);
1727       sun.sun_family = AF_UNIX;
1728     }
1729   while (bind (tempfd, (struct sockaddr *) &sun,
1730                offsetof (struct sockaddr_un, sun_path)
1731                + strlen (sun.sun_path) + 1) != 0);
1732   tempfname = strdup (sun.sun_path);
1733
1734   tempfd2 = socket (AF_UNIX, SOCK_DGRAM, 0);
1735   if (tempfd2 == -1)
1736     {
1737       printf ("%s: second socket call failed\n", __FUNCTION__);
1738       exit (1);
1739     }
1740
1741   int r = pthread_barrier_wait (&b2);
1742   if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1743     {
1744       printf ("%s: barrier_wait failed\n", __FUNCTION__);
1745       exit (1);
1746     }
1747
1748   r = pthread_barrier_wait (&b2);
1749   if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1750     {
1751       printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1752       exit (1);
1753     }
1754
1755   pthread_cleanup_push (cl, NULL);
1756
1757   char mem[1];
1758
1759   sendto (tempfd2, mem, arg == NULL ? sizeof (mem) : 1, 0,
1760           (struct sockaddr *) &sun,
1761           offsetof (struct sockaddr_un, sun_path) + strlen (sun.sun_path) + 1);
1762
1763   pthread_cleanup_pop (0);
1764
1765   printf ("%s: sendto returned\n", __FUNCTION__);
1766
1767   exit (1);
1768 }
1769
1770
1771 static void *
1772 tf_sendmsg (void *arg)
1773 {
1774   int tfd;
1775   if (arg == NULL)
1776     // XXX If somebody can provide a portable test case in which sendmsg()
1777     // blocks we can enable this test to run in both rounds.
1778     abort ();
1779
1780   struct sockaddr_un sun;
1781
1782   tempfd = socket (AF_UNIX, SOCK_DGRAM, 0);
1783   if (tempfd == -1)
1784     {
1785       printf ("%s: first socket call failed\n", __FUNCTION__);
1786       exit (1);
1787     }
1788
1789   int tries = 0;
1790   do
1791     {
1792       if (++tries > 10)
1793         {
1794           printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
1795         }
1796
1797       strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-7-XXXXXX");
1798       tfd = mkstemp(sun.sun_path);
1799       if (tfd < 0)
1800         {
1801           printf ("%s: cannot generate temp file name\n", __FUNCTION__);
1802           exit (1);
1803         }
1804       close(tfd);
1805       sun.sun_family = AF_UNIX;
1806     }
1807   while (bind (tempfd, (struct sockaddr *) &sun,
1808                offsetof (struct sockaddr_un, sun_path)
1809                + strlen (sun.sun_path) + 1) != 0);
1810   tempfname = strdup (sun.sun_path);
1811
1812   tempfd2 = socket (AF_UNIX, SOCK_DGRAM, 0);
1813   if (tempfd2 == -1)
1814     {
1815       printf ("%s: second socket call failed\n", __FUNCTION__);
1816       exit (1);
1817     }
1818
1819   int r = pthread_barrier_wait (&b2);
1820   if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1821     {
1822       printf ("%s: barrier_wait failed\n", __FUNCTION__);
1823       exit (1);
1824     }
1825
1826   r = pthread_barrier_wait (&b2);
1827   if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1828     {
1829       printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1830       exit (1);
1831     }
1832
1833   pthread_cleanup_push (cl, NULL);
1834
1835   char mem[1];
1836   struct iovec iov[1];
1837   iov[0].iov_base = mem;
1838   iov[0].iov_len = 1;
1839
1840   struct msghdr m;
1841   m.msg_name = &sun;
1842   m.msg_namelen = (offsetof (struct sockaddr_un, sun_path)
1843                    + strlen (sun.sun_path) + 1);
1844   m.msg_iov = iov;
1845   m.msg_iovlen = 1;
1846   m.msg_control = NULL;
1847   m.msg_controllen = 0;
1848
1849   sendmsg (tempfd2, &m, 0);
1850
1851   pthread_cleanup_pop (0);
1852
1853   printf ("%s: sendmsg returned\n", __FUNCTION__);
1854
1855   exit (1);
1856 }
1857
1858
1859 static void *
1860 tf_creat (void *arg)
1861 {
1862   if (arg == NULL)
1863     // XXX If somebody can provide a portable test case in which sendmsg()
1864     // blocks we can enable this test to run in both rounds.
1865     abort ();
1866
1867   int r = pthread_barrier_wait (&b2);
1868   if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1869     {
1870       printf ("%s: barrier_wait failed\n", __FUNCTION__);
1871       exit (1);
1872     }
1873
1874   r = pthread_barrier_wait (&b2);
1875   if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1876     {
1877       printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1878       exit (1);
1879     }
1880
1881   pthread_cleanup_push (cl, NULL);
1882
1883   creat ("tmp/tst-cancel-4-should-not-exist", 0666);
1884
1885   pthread_cleanup_pop (0);
1886
1887   printf ("%s: creat returned\n", __FUNCTION__);
1888
1889   exit (1);
1890 }
1891
1892
1893 static void *
1894 tf_connect (void *arg)
1895 {
1896   int tfd;
1897   if (arg == NULL)
1898     // XXX If somebody can provide a portable test case in which connect()
1899     // blocks we can enable this test to run in both rounds.
1900     abort ();
1901
1902   struct sockaddr_un sun;
1903
1904   tempfd = socket (AF_UNIX, SOCK_STREAM, 0);
1905   if (tempfd == -1)
1906     {
1907       printf ("%s: first socket call failed\n", __FUNCTION__);
1908       exit (1);
1909     }
1910
1911   int tries = 0;
1912   do
1913     {
1914       if (++tries > 10)
1915         {
1916           printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
1917         }
1918
1919       strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-2-XXXXXX");
1920       tfd = mkstemp(sun.sun_path);
1921       if (tfd < 0)
1922         {
1923           printf ("%s: cannot generate temp file name\n", __FUNCTION__);
1924           exit (1);
1925         }
1926       close(tfd);
1927       sun.sun_family = AF_UNIX;
1928     }
1929   while (bind (tempfd, (struct sockaddr *) &sun,
1930                offsetof (struct sockaddr_un, sun_path)
1931                + strlen (sun.sun_path) + 1) != 0);
1932   tempfname = strdup (sun.sun_path);
1933
1934   listen (tempfd, 5);
1935
1936   tempfd2 = socket (AF_UNIX, SOCK_STREAM, 0);
1937   if (tempfd2 == -1)
1938     {
1939       printf ("%s: second socket call failed\n", __FUNCTION__);
1940       exit (1);
1941     }
1942
1943   int r = pthread_barrier_wait (&b2);
1944   if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1945     {
1946       printf ("%s: barrier_wait failed\n", __FUNCTION__);
1947       exit (1);
1948     }
1949
1950   if (arg != NULL)
1951     {
1952       r = pthread_barrier_wait (&b2);
1953       if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1954         {
1955           printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1956           exit (1);
1957         }
1958     }
1959
1960   pthread_cleanup_push (cl, NULL);
1961
1962   connect (tempfd2, (struct sockaddr *) &sun, sizeof (sun));
1963
1964   pthread_cleanup_pop (0);
1965
1966   printf ("%s: connect returned\n", __FUNCTION__);
1967
1968   exit (1);
1969 }
1970
1971
1972 static void *
1973 tf_tcdrain (void *arg)
1974 {
1975   if (arg == NULL)
1976     // XXX If somebody can provide a portable test case in which tcdrain()
1977     // blocks we can enable this test to run in both rounds.
1978     abort ();
1979
1980   int r = pthread_barrier_wait (&b2);
1981   if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1982     {
1983       printf ("%s: barrier_wait failed\n", __FUNCTION__);
1984       exit (1);
1985     }
1986
1987   if (arg != NULL)
1988     {
1989       r = pthread_barrier_wait (&b2);
1990       if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1991         {
1992           printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1993           exit (1);
1994         }
1995     }
1996
1997   pthread_cleanup_push (cl, NULL);
1998
1999   /* Regardless of stderr being a terminal, the tcdrain call should be
2000      canceled.  */
2001   tcdrain (STDERR_FILENO);
2002
2003   pthread_cleanup_pop (0);
2004
2005   printf ("%s: tcdrain returned\n", __FUNCTION__);
2006
2007   exit (1);
2008 }
2009
2010
2011 static void *
2012 tf_msgrcv (void *arg)
2013 {
2014   tempmsg = msgget (IPC_PRIVATE, 0666 | IPC_CREAT);
2015   if (tempmsg == -1)
2016     {
2017       printf ("%s: msgget failed: %s\n", __FUNCTION__, strerror (errno));
2018       exit (1);
2019     }
2020
2021   int r = pthread_barrier_wait (&b2);
2022   if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
2023     {
2024       printf ("%s: barrier_wait failed\n", __FUNCTION__);
2025       exit (1);
2026     }
2027
2028   if (arg != NULL)
2029     {
2030       r = pthread_barrier_wait (&b2);
2031       if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
2032         {
2033           printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
2034           exit (1);
2035         }
2036     }
2037
2038   ssize_t s;
2039
2040   pthread_cleanup_push (cl, NULL);
2041
2042   struct
2043   {
2044     long int type;
2045     char mem[10];
2046   } m;
2047   int randnr;
2048   /* We need a positive random number.  */
2049   do
2050     randnr = random () % 64000;
2051   while (randnr <= 0);
2052   do
2053     {
2054       errno = 0;
2055       s = msgrcv (tempmsg, (struct msgbuf *) &m, 10, randnr, 0);
2056     }
2057   while (errno == EIDRM || errno == EINTR);
2058
2059   pthread_cleanup_pop (0);
2060
2061   printf ("%s: msgrcv returned %zd with errno = %m\n", __FUNCTION__, s);
2062
2063   msgctl (tempmsg, IPC_RMID, NULL);
2064
2065   exit (1);
2066 }
2067
2068
2069 static void *
2070 tf_msgsnd (void *arg)
2071 {
2072   if (arg == NULL)
2073     // XXX If somebody can provide a portable test case in which msgsnd()
2074     // blocks we can enable this test to run in both rounds.
2075     abort ();
2076
2077   tempmsg = msgget (IPC_PRIVATE, 0666 | IPC_CREAT);
2078   if (tempmsg == -1)
2079     {
2080       printf ("%s: msgget failed: %s\n", __FUNCTION__, strerror (errno));
2081       exit (1);
2082     }
2083
2084   int r = pthread_barrier_wait (&b2);
2085   if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
2086     {
2087       printf ("%s: barrier_wait failed\n", __FUNCTION__);
2088       exit (1);
2089     }
2090
2091   r = pthread_barrier_wait (&b2);
2092   if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
2093     {
2094       printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
2095       exit (1);
2096     }
2097
2098   pthread_cleanup_push (cl, NULL);
2099
2100   struct
2101   {
2102     long int type;
2103     char mem[1];
2104   } m;
2105   /* We need a positive random number.  */
2106   do
2107     m.type = random () % 64000;
2108   while (m.type <= 0);
2109   msgsnd (tempmsg, (struct msgbuf *) &m, sizeof (m.mem), 0);
2110
2111   pthread_cleanup_pop (0);
2112
2113   printf ("%s: msgsnd returned\n", __FUNCTION__);
2114
2115   msgctl (tempmsg, IPC_RMID, NULL);
2116
2117   exit (1);
2118 }
2119
2120
2121 static struct
2122 {
2123   const char *name;
2124   void *(*tf) (void *);
2125   int nb;
2126   int only_early;
2127 } tests[] =
2128 {
2129 #define ADD_TEST(name, nbar, early) { #name, tf_##name, nbar, early }
2130   ADD_TEST (read, 2, 0),
2131   ADD_TEST (readv, 2, 0),
2132   ADD_TEST (select, 2, 0),
2133   ADD_TEST (pselect, 2, 0),
2134   ADD_TEST (poll, 2, 0),
2135   ADD_TEST (ppoll, 2, 0),
2136   ADD_TEST (write, 2, 0),
2137   ADD_TEST (writev, 2, 0),
2138   ADD_TEST (sleep, 2, 0),
2139   ADD_TEST (usleep, 2, 0),
2140   ADD_TEST (nanosleep, 2, 0),
2141   ADD_TEST (wait, 2, 0),
2142   ADD_TEST (waitid, 2, 0),
2143   ADD_TEST (waitpid, 2, 0),
2144   ADD_TEST (sigpause, 2, 0),
2145   ADD_TEST (sigsuspend, 2, 0),
2146   ADD_TEST (sigwait, 2, 0),
2147   ADD_TEST (sigwaitinfo, 2, 0),
2148   ADD_TEST (sigtimedwait, 2, 0),
2149   ADD_TEST (pause, 2, 0),
2150   ADD_TEST (accept, 2, 0),
2151   ADD_TEST (send, 2, 0),
2152   ADD_TEST (recv, 2, 0),
2153   ADD_TEST (recvfrom, 2, 0),
2154   ADD_TEST (recvmsg, 2, 0),
2155   ADD_TEST (open, 2, 1),
2156   ADD_TEST (close, 2, 1),
2157   ADD_TEST (pread, 2, 1),
2158   ADD_TEST (pwrite, 2, 1),
2159   ADD_TEST (fsync, 2, 1),
2160   ADD_TEST (fdatasync, 2, 1),
2161   ADD_TEST (msync, 2, 1),
2162   ADD_TEST (sendto, 2, 1),
2163   ADD_TEST (sendmsg, 2, 1),
2164   ADD_TEST (creat, 2, 1),
2165   ADD_TEST (connect, 2, 1),
2166   ADD_TEST (tcdrain, 2, 1),
2167   ADD_TEST (msgrcv, 2, 0),
2168   ADD_TEST (msgsnd, 2, 1),
2169 };
2170 #define ntest_tf (sizeof (tests) / sizeof (tests[0]))
2171
2172
2173 static int
2174 do_test (void)
2175 {
2176   int val;
2177   socklen_t len;
2178
2179   if (socketpair (AF_UNIX, SOCK_STREAM, PF_UNIX, fds) != 0)
2180     {
2181       perror ("socketpair");
2182       exit (1);
2183     }
2184
2185   val = 1;
2186   len = sizeof(val);
2187   setsockopt (fds[1], SOL_SOCKET, SO_SNDBUF, &val, sizeof(val));
2188   if (getsockopt (fds[1], SOL_SOCKET, SO_SNDBUF, &val, &len) < 0)
2189     {
2190       perror ("getsockopt");
2191       exit (1);
2192     }
2193   if (val >= WRITE_BUFFER_SIZE)
2194     {
2195       puts ("minimum write buffer size too large");
2196       exit (1);
2197     }
2198   setsockopt (fds[1], SOL_SOCKET, SO_SNDBUF, &val, sizeof(val));
2199
2200   int result = 0;
2201   size_t cnt;
2202   for (cnt = 0; cnt < ntest_tf; ++cnt)
2203     {
2204       if (tests[cnt].only_early)
2205         continue;
2206
2207       if (pthread_barrier_init (&b2, NULL, tests[cnt].nb) != 0)
2208         {
2209           puts ("b2 init failed");
2210           exit (1);
2211         }
2212
2213       /* Reset the counter for the cleanup handler.  */
2214       cl_called = 0;
2215
2216       pthread_t th;
2217       if (pthread_create (&th, NULL, tests[cnt].tf, NULL) != 0)
2218         {
2219           printf ("create for '%s' test failed\n", tests[cnt].name);
2220           result = 1;
2221           continue;
2222         }
2223
2224       int r = pthread_barrier_wait (&b2);
2225       if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
2226         {
2227           printf ("%s: barrier_wait failed\n", __FUNCTION__);
2228           result = 1;
2229           continue;
2230         }
2231
2232       struct timespec  ts = { .tv_sec = 0, .tv_nsec = 100000000 };
2233       while (nanosleep (&ts, &ts) != 0)
2234         continue;
2235
2236       if (pthread_cancel (th) != 0)
2237         {
2238           printf ("cancel for '%s' failed\n", tests[cnt].name);
2239           result = 1;
2240           continue;
2241         }
2242
2243       void *status;
2244       if (pthread_join (th, &status) != 0)
2245         {
2246           printf ("join for '%s' failed\n", tests[cnt].name);
2247           result = 1;
2248           continue;
2249         }
2250       if (status != PTHREAD_CANCELED)
2251         {
2252           printf ("thread for '%s' not canceled\n", tests[cnt].name);
2253           result = 1;
2254           continue;
2255         }
2256
2257       if (pthread_barrier_destroy (&b2) != 0)
2258         {
2259           puts ("barrier_destroy failed");
2260           result = 1;
2261           continue;
2262         }
2263
2264       if (cl_called == 0)
2265         {
2266           printf ("cleanup handler not called for '%s'\n", tests[cnt].name);
2267           result = 1;
2268           continue;
2269         }
2270       if (cl_called > 1)
2271         {
2272           printf ("cleanup handler called more than once for '%s'\n",
2273                   tests[cnt].name);
2274           result = 1;
2275           continue;
2276         }
2277
2278       printf ("in-time cancel test of '%s' successful\n", tests[cnt].name);
2279
2280       if (tempfd != -1)
2281         {
2282           close (tempfd);
2283           tempfd = -1;
2284         }
2285       if (tempfd2 != -1)
2286         {
2287           close (tempfd2);
2288           tempfd2 = -1;
2289         }
2290       if (tempfname != NULL)
2291         {
2292           unlink (tempfname);
2293           free (tempfname);
2294           tempfname = NULL;
2295         }
2296       if (tempmsg != -1)
2297         {
2298           msgctl (tempmsg, IPC_RMID, NULL);
2299           tempmsg = -1;
2300         }
2301     }
2302
2303   for (cnt = 0; cnt < ntest_tf; ++cnt)
2304     {
2305       if (pthread_barrier_init (&b2, NULL, tests[cnt].nb) != 0)
2306         {
2307           puts ("b2 init failed");
2308           exit (1);
2309         }
2310
2311       /* Reset the counter for the cleanup handler.  */
2312       cl_called = 0;
2313
2314       pthread_t th;
2315       if (pthread_create (&th, NULL, tests[cnt].tf, (void *) 1l) != 0)
2316         {
2317           printf ("create for '%s' test failed\n", tests[cnt].name);
2318           result = 1;
2319           continue;
2320         }
2321
2322       int r = pthread_barrier_wait (&b2);
2323       if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
2324         {
2325           printf ("%s: barrier_wait failed\n", __FUNCTION__);
2326           result = 1;
2327           continue;
2328         }
2329
2330       if (pthread_cancel (th) != 0)
2331         {
2332           printf ("cancel for '%s' failed\n", tests[cnt].name);
2333           result = 1;
2334           continue;
2335         }
2336
2337       r = pthread_barrier_wait (&b2);
2338       if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
2339         {
2340           printf ("%s: barrier_wait failed\n", __FUNCTION__);
2341           result = 1;
2342           continue;
2343         }
2344
2345       void *status;
2346       if (pthread_join (th, &status) != 0)
2347         {
2348           printf ("join for '%s' failed\n", tests[cnt].name);
2349           result = 1;
2350           continue;
2351         }
2352       if (status != PTHREAD_CANCELED)
2353         {
2354           printf ("thread for '%s' not canceled\n", tests[cnt].name);
2355           result = 1;
2356           continue;
2357         }
2358
2359       if (pthread_barrier_destroy (&b2) != 0)
2360         {
2361           puts ("barrier_destroy failed");
2362           result = 1;
2363           continue;
2364         }
2365
2366       if (cl_called == 0)
2367         {
2368           printf ("cleanup handler not called for '%s'\n", tests[cnt].name);
2369           result = 1;
2370           continue;
2371         }
2372       if (cl_called > 1)
2373         {
2374           printf ("cleanup handler called more than once for '%s'\n",
2375                   tests[cnt].name);
2376           result = 1;
2377           continue;
2378         }
2379
2380       printf ("early cancel test of '%s' successful\n", tests[cnt].name);
2381
2382       if (tempfd != -1)
2383         {
2384           close (tempfd);
2385           tempfd = -1;
2386         }
2387       if (tempfd2 != -1)
2388         {
2389           close (tempfd2);
2390           tempfd2 = -1;
2391         }
2392       if (tempfname != NULL)
2393         {
2394           unlink (tempfname);
2395           free (tempfname);
2396           tempfname = NULL;
2397         }
2398       if (tempmsg != -1)
2399         {
2400           msgctl (tempmsg, IPC_RMID, NULL);
2401           tempmsg = -1;
2402         }
2403     }
2404
2405   return result;
2406 }
2407
2408 #define TIMEOUT 60
2409 #define TEST_FUNCTION do_test ()
2410 #include "../test-skeleton.c"