OSDN Git Service

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