Update.
[glibc.git] / nptl / tst-cancel4.c
blob87000fa6a4451ae3bc21506356baf4046f3b7543
1 /* Copyright (C) 2002, 2003 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 /* NOTE: this tests functionality beyond POSIX. POSIX does not allow
21 exit to be called more than once. */
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>
42 #include "pthreadP.h"
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()
51 lockf() and fcntl() are tested in tst-cancel16.
53 pthread_join() is tested in tst-join5.
55 pthread_testcancel()'s only purpose is to allow cancellation. This
56 is tested in several places.
58 sem_wait() and sem_timedwait() are checked in tst-cancel1[2345] tests.
60 mq_send(), mq_timedsend(), mq_receive() and mq_timedreceive() are checked
61 in tst-mqueue8{,x} tests.
63 aio_suspend() is tested in tst-cancel17.
65 clock_nanosleep() is tested in tst-cancel18.
68 /* Pipe descriptors. */
69 static int fds[2];
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;
79 /* Often used barrier for two threads. */
80 static pthread_barrier_t b2;
83 #ifndef IPC_ADDVAL
84 # define IPC_ADDVAL 0
85 #endif
87 /* Cleanup handling test. */
88 static int cl_called;
90 static void
91 cl (void *arg)
93 ++cl_called;
98 static void *
99 tf_read (void *arg)
101 int fd;
102 int r;
104 if (arg == NULL)
105 fd = fds[0];
106 else
108 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
109 tempfd = fd = mkstemp (fname);
110 if (fd == -1)
111 printf ("%s: mkstemp failed\n", __FUNCTION__);
112 unlink (fname);
114 r = pthread_barrier_wait (&b2);
115 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
117 printf ("%s: barrier_wait failed\n", __FUNCTION__);
118 exit (1);
122 r = pthread_barrier_wait (&b2);
123 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
125 printf ("%s: barrier_wait failed\n", __FUNCTION__);
126 exit (1);
129 ssize_t s;
130 pthread_cleanup_push (cl, NULL);
132 char buf[100];
133 s = read (fd, buf, sizeof (buf));
135 pthread_cleanup_pop (0);
137 printf ("%s: read returns with %zd\n", __FUNCTION__, s);
139 exit (1);
143 static void *
144 tf_readv (void *arg)
146 int fd;
147 int r;
149 if (arg == NULL)
150 fd = fds[0];
151 else
153 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
154 tempfd = fd = mkstemp (fname);
155 if (fd == -1)
156 printf ("%s: mkstemp failed\n", __FUNCTION__);
157 unlink (fname);
159 r = pthread_barrier_wait (&b2);
160 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
162 printf ("%s: barrier_wait failed\n", __FUNCTION__);
163 exit (1);
167 r = pthread_barrier_wait (&b2);
168 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
170 printf ("%s: barrier_wait failed\n", __FUNCTION__);
171 exit (1);
174 ssize_t s;
175 pthread_cleanup_push (cl, NULL);
177 char buf[100];
178 struct iovec iov[1] = { [0] = { .iov_base = buf, .iov_len = sizeof (buf) } };
179 s = readv (fd, iov, 1);
181 pthread_cleanup_pop (0);
183 printf ("%s: readv returns with %zd\n", __FUNCTION__, s);
185 exit (1);
189 static void *
190 tf_write (void *arg)
192 int fd;
193 int r;
195 if (arg == NULL)
196 fd = fds[1];
197 else
199 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
200 tempfd = fd = mkstemp (fname);
201 if (fd == -1)
202 printf ("%s: mkstemp failed\n", __FUNCTION__);
203 unlink (fname);
205 r = pthread_barrier_wait (&b2);
206 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
208 printf ("%s: barrier_wait failed\n", __FUNCTION__);
209 exit (1);
213 r = pthread_barrier_wait (&b2);
214 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
216 printf ("%s: barrier_wait failed\n", __FUNCTION__);
217 exit (1);
220 ssize_t s;
221 pthread_cleanup_push (cl, NULL);
223 char buf[100000];
224 memset (buf, '\0', sizeof (buf));
225 s = write (fd, buf, sizeof (buf));
227 pthread_cleanup_pop (0);
229 printf ("%s: write returns with %zd\n", __FUNCTION__, s);
231 exit (1);
235 static void *
236 tf_writev (void *arg)
238 int fd;
239 int r;
241 if (arg == NULL)
242 fd = fds[1];
243 else
245 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
246 tempfd = fd = mkstemp (fname);
247 if (fd == -1)
248 printf ("%s: mkstemp failed\n", __FUNCTION__);
249 unlink (fname);
251 r = pthread_barrier_wait (&b2);
252 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
254 printf ("%s: barrier_wait failed\n", __FUNCTION__);
255 exit (1);
259 r = pthread_barrier_wait (&b2);
260 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
262 printf ("%s: barrier_wait failed\n", __FUNCTION__);
263 exit (1);
266 ssize_t s;
267 pthread_cleanup_push (cl, NULL);
269 char buf[100000];
270 memset (buf, '\0', sizeof (buf));
271 struct iovec iov[1] = { [0] = { .iov_base = buf, .iov_len = sizeof (buf) } };
272 s = writev (fd, iov, 1);
274 pthread_cleanup_pop (0);
276 printf ("%s: writev returns with %zd\n", __FUNCTION__, s);
278 exit (1);
282 static void *
283 tf_sleep (void *arg)
285 int r = pthread_barrier_wait (&b2);
286 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
288 printf ("%s: barrier_wait failed\n", __FUNCTION__);
289 exit (1);
292 if (arg != NULL)
294 r = pthread_barrier_wait (&b2);
295 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
297 printf ("%s: barrier_wait failed\n", __FUNCTION__);
298 exit (1);
302 pthread_cleanup_push (cl, NULL);
304 sleep (arg == NULL ? 1000000 : 0);
306 pthread_cleanup_pop (0);
308 printf ("%s: sleep returns\n", __FUNCTION__);
310 exit (1);
314 static void *
315 tf_usleep (void *arg)
317 int r = pthread_barrier_wait (&b2);
318 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
320 printf ("%s: barrier_wait failed\n", __FUNCTION__);
321 exit (1);
324 if (arg != NULL)
326 r = pthread_barrier_wait (&b2);
327 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
329 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
330 exit (1);
334 pthread_cleanup_push (cl, NULL);
336 usleep (arg == NULL ? (useconds_t) ULONG_MAX : 0);
338 pthread_cleanup_pop (0);
340 printf ("%s: usleep returns\n", __FUNCTION__);
342 exit (1);
346 static void *
347 tf_nanosleep (void *arg)
349 int r = pthread_barrier_wait (&b2);
350 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
352 printf ("%s: barrier_wait failed\n", __FUNCTION__);
353 exit (1);
356 if (arg != NULL)
358 r = pthread_barrier_wait (&b2);
359 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
361 printf ("%s: barrier_wait failed\n", __FUNCTION__);
362 exit (1);
366 pthread_cleanup_push (cl, NULL);
368 struct timespec ts = { .tv_sec = arg == NULL ? 10000000 : 0, .tv_nsec = 0 };
369 TEMP_FAILURE_RETRY (nanosleep (&ts, &ts));
371 pthread_cleanup_pop (0);
373 printf ("%s: nanosleep returns\n", __FUNCTION__);
375 exit (1);
379 static void *
380 tf_select (void *arg)
382 int fd;
383 int r;
385 if (arg == NULL)
386 fd = fds[0];
387 else
389 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
390 tempfd = fd = mkstemp (fname);
391 if (fd == -1)
392 printf ("%s: mkstemp failed\n", __FUNCTION__);
393 unlink (fname);
395 r = pthread_barrier_wait (&b2);
396 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
398 printf ("%s: barrier_wait failed\n", __FUNCTION__);
399 exit (1);
403 r = pthread_barrier_wait (&b2);
404 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
406 printf ("%s: barrier_wait failed\n", __FUNCTION__);
407 exit (1);
410 fd_set rfs;
411 FD_ZERO (&rfs);
412 FD_SET (fd, &rfs);
414 int s;
415 pthread_cleanup_push (cl, NULL);
417 s = select (fd + 1, &rfs, NULL, NULL, NULL);
419 pthread_cleanup_pop (0);
421 printf ("%s: select returns with %d (%s)\n", __FUNCTION__, s,
422 strerror (errno));
424 exit (1);
428 static void *
429 tf_pselect (void *arg)
431 int fd;
432 int r;
434 if (arg == NULL)
435 fd = fds[0];
436 else
438 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
439 tempfd = fd = mkstemp (fname);
440 if (fd == -1)
441 printf ("%s: mkstemp failed\n", __FUNCTION__);
442 unlink (fname);
444 r = pthread_barrier_wait (&b2);
445 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
447 printf ("%s: barrier_wait failed\n", __FUNCTION__);
448 exit (1);
452 r = pthread_barrier_wait (&b2);
453 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
455 printf ("%s: barrier_wait failed\n", __FUNCTION__);
456 exit (1);
459 fd_set rfs;
460 FD_ZERO (&rfs);
461 FD_SET (fd, &rfs);
463 int s;
464 pthread_cleanup_push (cl, NULL);
466 s = pselect (fd + 1, &rfs, NULL, NULL, NULL, NULL);
468 pthread_cleanup_pop (0);
470 printf ("%s: pselect returns with %d (%s)\n", __FUNCTION__, s,
471 strerror (errno));
473 exit (1);
477 static void *
478 tf_poll (void *arg)
480 int fd;
481 int r;
483 if (arg == NULL)
484 fd = fds[0];
485 else
487 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
488 tempfd = fd = mkstemp (fname);
489 if (fd == -1)
490 printf ("%s: mkstemp failed\n", __FUNCTION__);
491 unlink (fname);
493 r = pthread_barrier_wait (&b2);
494 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
496 printf ("%s: barrier_wait failed\n", __FUNCTION__);
497 exit (1);
501 r = pthread_barrier_wait (&b2);
502 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
504 printf ("%s: barrier_wait failed\n", __FUNCTION__);
505 exit (1);
508 struct pollfd rfs[1] = { [0] = { .fd = fd, .events = POLLIN } };
510 int s;
511 pthread_cleanup_push (cl, NULL);
513 s = poll (rfs, 1, -1);
515 pthread_cleanup_pop (0);
517 printf ("%s: poll returns with %d (%s)\n", __FUNCTION__, s,
518 strerror (errno));
520 exit (1);
524 static void *
525 tf_wait (void *arg)
527 pid_t pid = fork ();
528 if (pid == -1)
530 puts ("fork failed");
531 exit (1);
534 if (pid == 0)
536 /* Make the program disappear after a while. */
537 if (arg == NULL)
538 sleep (10);
539 exit (0);
542 int r;
543 if (arg != NULL)
545 struct timespec ts = { .tv_sec = 0, .tv_nsec = 100000000 };
546 while (nanosleep (&ts, &ts) != 0)
547 continue;
549 r = pthread_barrier_wait (&b2);
550 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
552 printf ("%s: barrier_wait failed\n", __FUNCTION__);
553 exit (1);
557 r = pthread_barrier_wait (&b2);
558 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
560 printf ("%s: barrier_wait failed\n", __FUNCTION__);
561 exit (1);
564 int s;
565 pthread_cleanup_push (cl, NULL);
567 s = wait (NULL);
569 pthread_cleanup_pop (0);
571 printf ("%s: wait returns with %d (%s)\n", __FUNCTION__, s,
572 strerror (errno));
574 exit (1);
578 static void *
579 tf_waitpid (void *arg)
582 pid_t pid = fork ();
583 if (pid == -1)
585 puts ("fork failed");
586 exit (1);
589 if (pid == 0)
591 /* Make the program disappear after a while. */
592 if (arg == NULL)
593 sleep (10);
594 exit (0);
597 int r;
598 if (arg != NULL)
600 struct timespec ts = { .tv_sec = 0, .tv_nsec = 100000000 };
601 while (nanosleep (&ts, &ts) != 0)
602 continue;
604 r = pthread_barrier_wait (&b2);
605 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
607 printf ("%s: barrier_wait failed\n", __FUNCTION__);
608 exit (1);
612 r = pthread_barrier_wait (&b2);
613 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
615 printf ("%s: barrier_wait failed\n", __FUNCTION__);
616 exit (1);
619 int s;
620 pthread_cleanup_push (cl, NULL);
622 s = waitpid (-1, NULL, 0);
624 pthread_cleanup_pop (0);
626 printf ("%s: waitpid returns with %d (%s)\n", __FUNCTION__, s,
627 strerror (errno));
629 exit (1);
633 static void *
634 tf_waitid (void *arg)
636 pid_t pid = fork ();
637 if (pid == -1)
639 puts ("fork failed");
640 exit (1);
643 if (pid == 0)
645 /* Make the program disappear after a while. */
646 if (arg == NULL)
647 sleep (10);
648 exit (0);
651 int r;
652 if (arg != NULL)
654 struct timespec ts = { .tv_sec = 0, .tv_nsec = 100000000 };
655 while (nanosleep (&ts, &ts) != 0)
656 continue;
658 r = pthread_barrier_wait (&b2);
659 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
661 printf ("%s: barrier_wait failed\n", __FUNCTION__);
662 exit (1);
666 r = pthread_barrier_wait (&b2);
667 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
669 printf ("%s: barrier_wait failed\n", __FUNCTION__);
670 exit (1);
673 int s;
674 pthread_cleanup_push (cl, NULL);
676 siginfo_t si;
677 s = waitid (P_PID, pid, &si, 0);
679 pthread_cleanup_pop (0);
681 printf ("%s: waitid returns with %d (%s)\n", __FUNCTION__, s,
682 strerror (errno));
684 exit (1);
688 static void *
689 tf_sigpause (void *arg)
691 int r = pthread_barrier_wait (&b2);
692 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
694 printf ("%s: barrier_wait failed\n", __FUNCTION__);
695 exit (1);
698 if (arg != NULL)
700 r = pthread_barrier_wait (&b2);
701 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
703 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
704 exit (1);
708 pthread_cleanup_push (cl, NULL);
710 /* Just for fun block the cancellation signal. We need to use
711 __xpg_sigpause since otherwise we will get the BSD version. */
712 __xpg_sigpause (SIGCANCEL);
714 pthread_cleanup_pop (0);
716 printf ("%s: sigpause returned\n", __FUNCTION__);
718 exit (1);
722 static void *
723 tf_sigsuspend (void *arg)
725 int r = pthread_barrier_wait (&b2);
726 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
728 printf ("%s: barrier_wait failed\n", __FUNCTION__);
729 exit (1);
732 if (arg != NULL)
734 r = pthread_barrier_wait (&b2);
735 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
737 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
738 exit (1);
742 pthread_cleanup_push (cl, NULL);
744 /* Just for fun block all signals. */
745 sigset_t mask;
746 sigfillset (&mask);
747 sigsuspend (&mask);
749 pthread_cleanup_pop (0);
751 printf ("%s: sigsuspend returned\n", __FUNCTION__);
753 exit (1);
757 static void *
758 tf_sigwait (void *arg)
760 int r = pthread_barrier_wait (&b2);
761 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
763 printf ("%s: barrier_wait failed\n", __FUNCTION__);
764 exit (1);
767 if (arg != NULL)
769 r = pthread_barrier_wait (&b2);
770 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
772 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
773 exit (1);
777 /* Block SIGUSR1. */
778 sigset_t mask;
779 sigemptyset (&mask);
780 sigaddset (&mask, SIGUSR1);
781 if (pthread_sigmask (SIG_BLOCK, &mask, NULL) != 0)
783 printf ("%s: pthread_sigmask failed\n", __FUNCTION__);
784 exit (1);
787 int sig;
788 pthread_cleanup_push (cl, NULL);
790 /* Wait for SIGUSR1. */
791 sigwait (&mask, &sig);
793 pthread_cleanup_pop (0);
795 printf ("%s: sigwait returned with signal %d\n", __FUNCTION__, sig);
797 exit (1);
801 static void *
802 tf_sigwaitinfo (void *arg)
804 int r = pthread_barrier_wait (&b2);
805 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
807 printf ("%s: barrier_wait failed\n", __FUNCTION__);
808 exit (1);
811 if (arg != NULL)
813 r = pthread_barrier_wait (&b2);
814 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
816 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
817 exit (1);
821 /* Block SIGUSR1. */
822 sigset_t mask;
823 sigemptyset (&mask);
824 sigaddset (&mask, SIGUSR1);
825 if (pthread_sigmask (SIG_BLOCK, &mask, NULL) != 0)
827 printf ("%s: pthread_sigmask failed\n", __FUNCTION__);
828 exit (1);
831 siginfo_t info;
832 pthread_cleanup_push (cl, NULL);
834 /* Wait for SIGUSR1. */
835 sigwaitinfo (&mask, &info);
837 pthread_cleanup_pop (0);
839 printf ("%s: sigwaitinfo returned with signal %d\n", __FUNCTION__,
840 info.si_signo);
842 exit (1);
846 static void *
847 tf_sigtimedwait (void *arg)
849 int r = pthread_barrier_wait (&b2);
850 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
852 printf ("%s: barrier_wait failed\n", __FUNCTION__);
853 exit (1);
856 if (arg != NULL)
858 r = pthread_barrier_wait (&b2);
859 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
861 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
862 exit (1);
866 /* Block SIGUSR1. */
867 sigset_t mask;
868 sigemptyset (&mask);
869 sigaddset (&mask, SIGUSR1);
870 if (pthread_sigmask (SIG_BLOCK, &mask, NULL) != 0)
872 printf ("%s: pthread_sigmask failed\n", __FUNCTION__);
873 exit (1);
876 /* Wait for SIGUSR1. */
877 siginfo_t info;
878 struct timespec ts = { .tv_sec = 60, .tv_nsec = 0 };
879 pthread_cleanup_push (cl, NULL);
881 sigtimedwait (&mask, &info, &ts);
883 pthread_cleanup_pop (0);
885 printf ("%s: sigtimedwait returned with signal %d\n", __FUNCTION__,
886 info.si_signo);
888 exit (1);
892 static void *
893 tf_pause (void *arg)
895 int r = pthread_barrier_wait (&b2);
896 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
898 printf ("%s: barrier_wait failed\n", __FUNCTION__);
899 exit (1);
902 if (arg != NULL)
904 r = pthread_barrier_wait (&b2);
905 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
907 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
908 exit (1);
912 pthread_cleanup_push (cl, NULL);
914 pause ();
916 pthread_cleanup_pop (0);
918 printf ("%s: pause returned\n", __FUNCTION__);
920 exit (1);
924 static void *
925 tf_accept (void *arg)
927 struct sockaddr_un sun;
928 /* To test a non-blocking accept call we make the call file by using
929 a datagrame socket. */
930 int pf = arg == NULL ? SOCK_STREAM : SOCK_DGRAM;
932 tempfd = socket (AF_UNIX, pf, 0);
933 if (tempfd == -1)
935 printf ("%s: socket call failed\n", __FUNCTION__);
936 exit (1);
939 int tries = 0;
942 if (++tries > 10)
944 printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
947 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-1-XXXXXX");
948 if (mktemp (sun.sun_path) == NULL)
950 printf ("%s: cannot generate temp file name\n", __FUNCTION__);
951 exit (1);
954 sun.sun_family = AF_UNIX;
956 while (bind (tempfd, (struct sockaddr *) &sun,
957 offsetof (struct sockaddr_un, sun_path)
958 + strlen (sun.sun_path) + 1) != 0);
960 unlink (sun.sun_path);
962 listen (tempfd, 5);
964 socklen_t len = sizeof (sun);
966 int r = pthread_barrier_wait (&b2);
967 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
969 printf ("%s: barrier_wait failed\n", __FUNCTION__);
970 exit (1);
973 if (arg != NULL)
975 r = pthread_barrier_wait (&b2);
976 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
978 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
979 exit (1);
983 pthread_cleanup_push (cl, NULL);
985 accept (tempfd, (struct sockaddr *) &sun, &len);
987 pthread_cleanup_pop (0);
989 printf ("%s: accept returned\n", __FUNCTION__);
991 exit (1);
995 static void *
996 tf_send (void *arg)
998 struct sockaddr_un sun;
1000 tempfd = socket (AF_UNIX, SOCK_STREAM, 0);
1001 if (tempfd == -1)
1003 printf ("%s: first socket call failed\n", __FUNCTION__);
1004 exit (1);
1007 int tries = 0;
1010 if (++tries > 10)
1012 printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
1015 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-2-XXXXXX");
1016 if (mktemp (sun.sun_path) == NULL)
1018 printf ("%s: cannot generate temp file name\n", __FUNCTION__);
1019 exit (1);
1022 sun.sun_family = AF_UNIX;
1024 while (bind (tempfd, (struct sockaddr *) &sun,
1025 offsetof (struct sockaddr_un, sun_path)
1026 + strlen (sun.sun_path) + 1) != 0);
1028 listen (tempfd, 5);
1030 tempfd2 = socket (AF_UNIX, SOCK_STREAM, 0);
1031 if (tempfd2 == -1)
1033 printf ("%s: second socket call failed\n", __FUNCTION__);
1034 exit (1);
1037 if (connect (tempfd2, (struct sockaddr *) &sun, sizeof (sun)) != 0)
1039 printf ("%s: connect failed\n", __FUNCTION__);
1040 exit(1);
1043 unlink (sun.sun_path);
1045 int r = pthread_barrier_wait (&b2);
1046 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1048 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1049 exit (1);
1052 if (arg != NULL)
1054 r = pthread_barrier_wait (&b2);
1055 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1057 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1058 exit (1);
1062 pthread_cleanup_push (cl, NULL);
1064 /* Very large block, so that the send call blocks. */
1065 char mem[700000];
1067 send (tempfd2, mem, arg == NULL ? sizeof (mem) : 1, 0);
1069 pthread_cleanup_pop (0);
1071 printf ("%s: send returned\n", __FUNCTION__);
1073 exit (1);
1077 static void *
1078 tf_recv (void *arg)
1080 struct sockaddr_un sun;
1082 tempfd = socket (AF_UNIX, SOCK_STREAM, 0);
1083 if (tempfd == -1)
1085 printf ("%s: first socket call failed\n", __FUNCTION__);
1086 exit (1);
1089 int tries = 0;
1092 if (++tries > 10)
1094 printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
1097 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-3-XXXXXX");
1098 if (mktemp (sun.sun_path) == NULL)
1100 printf ("%s: cannot generate temp file name\n", __FUNCTION__);
1101 exit (1);
1104 sun.sun_family = AF_UNIX;
1106 while (bind (tempfd, (struct sockaddr *) &sun,
1107 offsetof (struct sockaddr_un, sun_path)
1108 + strlen (sun.sun_path) + 1) != 0);
1110 listen (tempfd, 5);
1112 tempfd2 = socket (AF_UNIX, SOCK_STREAM, 0);
1113 if (tempfd2 == -1)
1115 printf ("%s: second socket call failed\n", __FUNCTION__);
1116 exit (1);
1119 if (connect (tempfd2, (struct sockaddr *) &sun, sizeof (sun)) != 0)
1121 printf ("%s: connect failed\n", __FUNCTION__);
1122 exit(1);
1125 unlink (sun.sun_path);
1127 int r = pthread_barrier_wait (&b2);
1128 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1130 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1131 exit (1);
1134 if (arg != NULL)
1136 r = pthread_barrier_wait (&b2);
1137 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1139 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1140 exit (1);
1144 pthread_cleanup_push (cl, NULL);
1146 char mem[70];
1148 recv (tempfd2, mem, arg == NULL ? sizeof (mem) : 0, 0);
1150 pthread_cleanup_pop (0);
1152 printf ("%s: recv returned\n", __FUNCTION__);
1154 exit (1);
1158 static void *
1159 tf_recvfrom (void *arg)
1161 struct sockaddr_un sun;
1163 tempfd = socket (AF_UNIX, SOCK_DGRAM, 0);
1164 if (tempfd == -1)
1166 printf ("%s: first socket call failed\n", __FUNCTION__);
1167 exit (1);
1170 int tries = 0;
1173 if (++tries > 10)
1175 printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
1178 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-4-XXXXXX");
1179 if (mktemp (sun.sun_path) == NULL)
1181 printf ("%s: cannot generate temp file name\n", __FUNCTION__);
1182 exit (1);
1185 sun.sun_family = AF_UNIX;
1187 while (bind (tempfd, (struct sockaddr *) &sun,
1188 offsetof (struct sockaddr_un, sun_path)
1189 + strlen (sun.sun_path) + 1) != 0);
1191 tempfname = strdup (sun.sun_path);
1193 tempfd2 = socket (AF_UNIX, SOCK_DGRAM, 0);
1194 if (tempfd2 == -1)
1196 printf ("%s: second socket call failed\n", __FUNCTION__);
1197 exit (1);
1200 int r = pthread_barrier_wait (&b2);
1201 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1203 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1204 exit (1);
1207 if (arg != NULL)
1209 r = pthread_barrier_wait (&b2);
1210 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1212 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1213 exit (1);
1217 pthread_cleanup_push (cl, NULL);
1219 char mem[70];
1220 socklen_t len = sizeof (sun);
1222 recvfrom (tempfd2, mem, arg == NULL ? sizeof (mem) : 0, 0,
1223 (struct sockaddr *) &sun, &len);
1225 pthread_cleanup_pop (0);
1227 printf ("%s: recvfrom returned\n", __FUNCTION__);
1229 exit (1);
1233 static void *
1234 tf_recvmsg (void *arg)
1236 struct sockaddr_un sun;
1238 tempfd = socket (AF_UNIX, SOCK_DGRAM, 0);
1239 if (tempfd == -1)
1241 printf ("%s: first socket call failed\n", __FUNCTION__);
1242 exit (1);
1245 int tries = 0;
1248 if (++tries > 10)
1250 printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
1253 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-5-XXXXXX");
1254 if (mktemp (sun.sun_path) == NULL)
1256 printf ("%s: cannot generate temp file name\n", __FUNCTION__);
1257 exit (1);
1260 sun.sun_family = AF_UNIX;
1262 while (bind (tempfd, (struct sockaddr *) &sun,
1263 offsetof (struct sockaddr_un, sun_path)
1264 + strlen (sun.sun_path) + 1) != 0);
1266 tempfname = strdup (sun.sun_path);
1268 tempfd2 = socket (AF_UNIX, SOCK_DGRAM, 0);
1269 if (tempfd2 == -1)
1271 printf ("%s: second socket call failed\n", __FUNCTION__);
1272 exit (1);
1275 int r = pthread_barrier_wait (&b2);
1276 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1278 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1279 exit (1);
1282 if (arg != NULL)
1284 r = pthread_barrier_wait (&b2);
1285 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1287 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1288 exit (1);
1292 pthread_cleanup_push (cl, NULL);
1294 char mem[70];
1295 struct iovec iov[1];
1296 iov[0].iov_base = mem;
1297 iov[0].iov_len = arg == NULL ? sizeof (mem) : 0;
1299 struct msghdr m;
1300 m.msg_name = &sun;
1301 m.msg_namelen = sizeof (sun);
1302 m.msg_iov = iov;
1303 m.msg_iovlen = 1;
1304 m.msg_control = NULL;
1305 m.msg_controllen = 0;
1307 recvmsg (tempfd2, &m, 0);
1309 pthread_cleanup_pop (0);
1311 printf ("%s: recvmsg returned\n", __FUNCTION__);
1313 exit (1);
1317 static void *
1318 tf_open (void *arg)
1320 if (arg == NULL)
1321 // XXX If somebody can provide a portable test case in which open()
1322 // blocks we can enable this test to run in both rounds.
1323 abort ();
1325 int r = pthread_barrier_wait (&b2);
1326 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1328 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1329 exit (1);
1332 r = pthread_barrier_wait (&b2);
1333 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1335 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1336 exit (1);
1339 pthread_cleanup_push (cl, NULL);
1341 open ("Makefile", O_RDONLY);
1343 pthread_cleanup_pop (0);
1345 printf ("%s: open returned\n", __FUNCTION__);
1347 exit (1);
1351 static void *
1352 tf_close (void *arg)
1354 if (arg == NULL)
1355 // XXX If somebody can provide a portable test case in which close()
1356 // blocks we can enable this test to run in both rounds.
1357 abort ();
1359 char fname[] = "/tmp/tst-cancel-fd-XXXXXX";
1360 tempfd = mkstemp (fname);
1361 if (tempfd == -1)
1363 printf ("%s: mkstemp failed\n", __FUNCTION__);
1364 exit (1);
1366 unlink (fname);
1368 int r = pthread_barrier_wait (&b2);
1369 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1371 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1372 exit (1);
1375 r = pthread_barrier_wait (&b2);
1376 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1378 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1379 exit (1);
1382 pthread_cleanup_push (cl, NULL);
1384 close (tempfd);
1386 pthread_cleanup_pop (0);
1388 printf ("%s: close returned\n", __FUNCTION__);
1390 exit (1);
1394 static void *
1395 tf_pread (void *arg)
1397 if (arg == NULL)
1398 // XXX If somebody can provide a portable test case in which pread()
1399 // blocks we can enable this test to run in both rounds.
1400 abort ();
1402 tempfd = open ("Makefile", O_RDONLY);
1403 if (tempfd == -1)
1405 printf ("%s: cannot open Makefile\n", __FUNCTION__);
1406 exit (1);
1409 int r = pthread_barrier_wait (&b2);
1410 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1412 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1413 exit (1);
1416 r = pthread_barrier_wait (&b2);
1417 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1419 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1420 exit (1);
1423 pthread_cleanup_push (cl, NULL);
1425 char mem[10];
1426 pread (tempfd, mem, sizeof (mem), 0);
1428 pthread_cleanup_pop (0);
1430 printf ("%s: pread returned\n", __FUNCTION__);
1432 exit (1);
1436 static void *
1437 tf_pwrite (void *arg)
1439 if (arg == NULL)
1440 // XXX If somebody can provide a portable test case in which pwrite()
1441 // blocks we can enable this test to run in both rounds.
1442 abort ();
1444 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
1445 tempfd = mkstemp (fname);
1446 if (tempfd == -1)
1448 printf ("%s: mkstemp failed\n", __FUNCTION__);
1449 exit (1);
1451 unlink (fname);
1453 int r = pthread_barrier_wait (&b2);
1454 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1456 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1457 exit (1);
1460 r = pthread_barrier_wait (&b2);
1461 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1463 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1464 exit (1);
1467 pthread_cleanup_push (cl, NULL);
1469 char mem[10];
1470 pwrite (tempfd, mem, sizeof (mem), 0);
1472 pthread_cleanup_pop (0);
1474 printf ("%s: pwrite returned\n", __FUNCTION__);
1476 exit (1);
1480 static void *
1481 tf_fsync (void *arg)
1483 if (arg == NULL)
1484 // XXX If somebody can provide a portable test case in which fsync()
1485 // blocks we can enable this test to run in both rounds.
1486 abort ();
1488 tempfd = open ("Makefile", O_RDONLY);
1489 if (tempfd == -1)
1491 printf ("%s: cannot open Makefile\n", __FUNCTION__);
1492 exit (1);
1495 int r = pthread_barrier_wait (&b2);
1496 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1498 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1499 exit (1);
1502 r = pthread_barrier_wait (&b2);
1503 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1505 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1506 exit (1);
1509 pthread_cleanup_push (cl, NULL);
1511 fsync (tempfd);
1513 pthread_cleanup_pop (0);
1515 printf ("%s: fsync returned\n", __FUNCTION__);
1517 exit (1);
1521 static void *
1522 tf_msync (void *arg)
1524 if (arg == NULL)
1525 // XXX If somebody can provide a portable test case in which msync()
1526 // blocks we can enable this test to run in both rounds.
1527 abort ();
1529 tempfd = open ("Makefile", O_RDONLY);
1530 if (tempfd == -1)
1532 printf ("%s: cannot open Makefile\n", __FUNCTION__);
1533 exit (1);
1535 void *p = mmap (NULL, 10, PROT_READ, MAP_SHARED, tempfd, 0);
1536 if (p == MAP_FAILED)
1538 printf ("%s: mmap failed\n", __FUNCTION__);
1539 exit (1);
1542 int r = pthread_barrier_wait (&b2);
1543 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1545 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1546 exit (1);
1549 r = pthread_barrier_wait (&b2);
1550 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1552 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1553 exit (1);
1556 pthread_cleanup_push (cl, NULL);
1558 msync (p, 10, 0);
1560 pthread_cleanup_pop (0);
1562 printf ("%s: msync returned\n", __FUNCTION__);
1564 exit (1);
1568 static void *
1569 tf_sendto (void *arg)
1571 if (arg == NULL)
1572 // XXX If somebody can provide a portable test case in which sendto()
1573 // blocks we can enable this test to run in both rounds.
1574 abort ();
1576 struct sockaddr_un sun;
1578 tempfd = socket (AF_UNIX, SOCK_DGRAM, 0);
1579 if (tempfd == -1)
1581 printf ("%s: first socket call failed\n", __FUNCTION__);
1582 exit (1);
1585 int tries = 0;
1588 if (++tries > 10)
1590 printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
1593 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-6-XXXXXX");
1594 if (mktemp (sun.sun_path) == NULL)
1596 printf ("%s: cannot generate temp file name\n", __FUNCTION__);
1597 exit (1);
1600 sun.sun_family = AF_UNIX;
1602 while (bind (tempfd, (struct sockaddr *) &sun,
1603 offsetof (struct sockaddr_un, sun_path)
1604 + strlen (sun.sun_path) + 1) != 0);
1605 tempfname = strdup (sun.sun_path);
1607 tempfd2 = socket (AF_UNIX, SOCK_DGRAM, 0);
1608 if (tempfd2 == -1)
1610 printf ("%s: second socket call failed\n", __FUNCTION__);
1611 exit (1);
1614 int r = pthread_barrier_wait (&b2);
1615 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1617 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1618 exit (1);
1621 r = pthread_barrier_wait (&b2);
1622 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1624 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1625 exit (1);
1628 pthread_cleanup_push (cl, NULL);
1630 char mem[1];
1632 sendto (tempfd2, mem, arg == NULL ? sizeof (mem) : 1, 0,
1633 (struct sockaddr *) &sun,
1634 offsetof (struct sockaddr_un, sun_path) + strlen (sun.sun_path) + 1);
1636 pthread_cleanup_pop (0);
1638 printf ("%s: sendto returned\n", __FUNCTION__);
1640 exit (1);
1644 static void *
1645 tf_sendmsg (void *arg)
1647 if (arg == NULL)
1648 // XXX If somebody can provide a portable test case in which sendmsg()
1649 // blocks we can enable this test to run in both rounds.
1650 abort ();
1652 struct sockaddr_un sun;
1654 tempfd = socket (AF_UNIX, SOCK_DGRAM, 0);
1655 if (tempfd == -1)
1657 printf ("%s: first socket call failed\n", __FUNCTION__);
1658 exit (1);
1661 int tries = 0;
1664 if (++tries > 10)
1666 printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
1669 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-7-XXXXXX");
1670 if (mktemp (sun.sun_path) == NULL)
1672 printf ("%s: cannot generate temp file name\n", __FUNCTION__);
1673 exit (1);
1676 sun.sun_family = AF_UNIX;
1678 while (bind (tempfd, (struct sockaddr *) &sun,
1679 offsetof (struct sockaddr_un, sun_path)
1680 + strlen (sun.sun_path) + 1) != 0);
1681 tempfname = strdup (sun.sun_path);
1683 tempfd2 = socket (AF_UNIX, SOCK_DGRAM, 0);
1684 if (tempfd2 == -1)
1686 printf ("%s: second socket call failed\n", __FUNCTION__);
1687 exit (1);
1690 int r = pthread_barrier_wait (&b2);
1691 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1693 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1694 exit (1);
1697 r = pthread_barrier_wait (&b2);
1698 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1700 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1701 exit (1);
1704 pthread_cleanup_push (cl, NULL);
1706 char mem[1];
1707 struct iovec iov[1];
1708 iov[0].iov_base = mem;
1709 iov[0].iov_len = 1;
1711 struct msghdr m;
1712 m.msg_name = &sun;
1713 m.msg_namelen = (offsetof (struct sockaddr_un, sun_path)
1714 + strlen (sun.sun_path) + 1);
1715 m.msg_iov = iov;
1716 m.msg_iovlen = 1;
1717 m.msg_control = NULL;
1718 m.msg_controllen = 0;
1720 sendmsg (tempfd2, &m, 0);
1722 pthread_cleanup_pop (0);
1724 printf ("%s: sendmsg returned\n", __FUNCTION__);
1726 exit (1);
1730 static void *
1731 tf_creat (void *arg)
1733 if (arg == NULL)
1734 // XXX If somebody can provide a portable test case in which sendmsg()
1735 // blocks we can enable this test to run in both rounds.
1736 abort ();
1738 int r = pthread_barrier_wait (&b2);
1739 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1741 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1742 exit (1);
1745 r = pthread_barrier_wait (&b2);
1746 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1748 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1749 exit (1);
1752 pthread_cleanup_push (cl, NULL);
1754 creat ("tmp/tst-cancel-4-should-not-exist", 0666);
1756 pthread_cleanup_pop (0);
1758 printf ("%s: creat returned\n", __FUNCTION__);
1760 exit (1);
1764 static void *
1765 tf_connect (void *arg)
1767 if (arg == NULL)
1768 // XXX If somebody can provide a portable test case in which connect()
1769 // blocks we can enable this test to run in both rounds.
1770 abort ();
1772 struct sockaddr_un sun;
1774 tempfd = socket (AF_UNIX, SOCK_STREAM, 0);
1775 if (tempfd == -1)
1777 printf ("%s: first socket call failed\n", __FUNCTION__);
1778 exit (1);
1781 int tries = 0;
1784 if (++tries > 10)
1786 printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
1789 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-2-XXXXXX");
1790 if (mktemp (sun.sun_path) == NULL)
1792 printf ("%s: cannot generate temp file name\n", __FUNCTION__);
1793 exit (1);
1796 sun.sun_family = AF_UNIX;
1798 while (bind (tempfd, (struct sockaddr *) &sun,
1799 offsetof (struct sockaddr_un, sun_path)
1800 + strlen (sun.sun_path) + 1) != 0);
1801 tempfname = strdup (sun.sun_path);
1803 listen (tempfd, 5);
1805 tempfd2 = socket (AF_UNIX, SOCK_STREAM, 0);
1806 if (tempfd2 == -1)
1808 printf ("%s: second socket call failed\n", __FUNCTION__);
1809 exit (1);
1812 int r = pthread_barrier_wait (&b2);
1813 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1815 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1816 exit (1);
1819 if (arg != NULL)
1821 r = pthread_barrier_wait (&b2);
1822 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1824 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1825 exit (1);
1829 pthread_cleanup_push (cl, NULL);
1831 connect (tempfd2, (struct sockaddr *) &sun, sizeof (sun));
1833 pthread_cleanup_pop (0);
1835 printf ("%s: connect returned\n", __FUNCTION__);
1837 exit (1);
1841 static void *
1842 tf_tcdrain (void *arg)
1844 if (arg == NULL)
1845 // XXX If somebody can provide a portable test case in which tcdrain()
1846 // blocks we can enable this test to run in both rounds.
1847 abort ();
1849 int r = pthread_barrier_wait (&b2);
1850 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1852 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1853 exit (1);
1856 if (arg != NULL)
1858 r = pthread_barrier_wait (&b2);
1859 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1861 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1862 exit (1);
1866 pthread_cleanup_push (cl, NULL);
1868 /* Regardless of stderr being a terminal, the tcdrain call should be
1869 canceled. */
1870 tcdrain (STDERR_FILENO);
1872 pthread_cleanup_pop (0);
1874 printf ("%s: tcdrain returned\n", __FUNCTION__);
1876 exit (1);
1880 static void *
1881 tf_msgrcv (void *arg)
1883 tempmsg = msgget (IPC_PRIVATE, 0666 | IPC_CREAT);
1885 int r = pthread_barrier_wait (&b2);
1886 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1888 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1889 exit (1);
1892 if (arg != NULL)
1894 r = pthread_barrier_wait (&b2);
1895 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1897 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1898 exit (1);
1902 ssize_t s;
1904 pthread_cleanup_push (cl, NULL);
1906 struct
1908 long int type;
1909 char mem[10];
1910 } m;
1911 int randnr;
1912 /* We need a positive random number. */
1914 randnr = random () % 64000;
1915 while (randnr <= 0);
1918 errno = 0;
1919 s = msgrcv (tempmsg, (struct msgbuf *) &m, 10, randnr, 0);
1921 while (errno == EIDRM || errno == EINTR);
1923 pthread_cleanup_pop (0);
1925 printf ("%s: msgrcv returned %zd with errno = %m\n", __FUNCTION__, s);
1927 exit (1);
1931 static void *
1932 tf_msgsnd (void *arg)
1934 if (arg == NULL)
1935 // XXX If somebody can provide a portable test case in which msgsnd()
1936 // blocks we can enable this test to run in both rounds.
1937 abort ();
1939 tempmsg = msgget (IPC_PRIVATE, 0666 | IPC_CREAT);
1941 int r = pthread_barrier_wait (&b2);
1942 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1944 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1945 exit (1);
1948 r = pthread_barrier_wait (&b2);
1949 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1951 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1952 exit (1);
1955 pthread_cleanup_push (cl, NULL);
1957 struct
1959 long int type;
1960 char mem[1];
1961 } m;
1962 /* We need a positive random number. */
1964 m.type = random () % 64000;
1965 while (m.type <= 0);
1966 msgsnd (tempmsg, (struct msgbuf *) &m, sizeof (m.mem), 0);
1968 pthread_cleanup_pop (0);
1970 printf ("%s: msgsnd returned\n", __FUNCTION__);
1972 exit (1);
1976 static struct
1978 const char *name;
1979 void *(*tf) (void *);
1980 int nb;
1981 int only_early;
1982 } tests[] =
1984 #define ADD_TEST(name, nbar, early) { #name, tf_##name, nbar, early }
1985 ADD_TEST (read, 2, 0),
1986 ADD_TEST (readv, 2, 0),
1987 ADD_TEST (select, 2, 0),
1988 ADD_TEST (pselect, 2, 0),
1989 ADD_TEST (poll, 2, 0),
1990 ADD_TEST (write, 2, 0),
1991 ADD_TEST (writev, 2, 0),
1992 ADD_TEST (sleep, 2, 0),
1993 ADD_TEST (usleep, 2, 0),
1994 ADD_TEST (nanosleep, 2, 0),
1995 ADD_TEST (wait, 2, 0),
1996 ADD_TEST (waitid, 2, 0),
1997 ADD_TEST (waitpid, 2, 0),
1998 ADD_TEST (sigpause, 2, 0),
1999 ADD_TEST (sigsuspend, 2, 0),
2000 ADD_TEST (sigwait, 2, 0),
2001 ADD_TEST (sigwaitinfo, 2, 0),
2002 ADD_TEST (sigtimedwait, 2, 0),
2003 ADD_TEST (pause, 2, 0),
2004 ADD_TEST (accept, 2, 0),
2005 ADD_TEST (send, 2, 0),
2006 ADD_TEST (recv, 2, 0),
2007 ADD_TEST (recvfrom, 2, 0),
2008 ADD_TEST (recvmsg, 2, 0),
2009 ADD_TEST (open, 2, 1),
2010 ADD_TEST (close, 2, 1),
2011 ADD_TEST (pread, 2, 1),
2012 ADD_TEST (pwrite, 2, 1),
2013 ADD_TEST (fsync, 2, 1),
2014 ADD_TEST (msync, 2, 1),
2015 ADD_TEST (sendto, 2, 1),
2016 ADD_TEST (sendmsg, 2, 1),
2017 ADD_TEST (creat, 2, 1),
2018 ADD_TEST (connect, 2, 1),
2019 ADD_TEST (tcdrain, 2, 1),
2020 ADD_TEST (msgrcv, 2, 0),
2021 ADD_TEST (msgsnd, 2, 1),
2023 #define ntest_tf (sizeof (tests) / sizeof (tests[0]))
2026 static int
2027 do_test (void)
2029 if (pipe (fds) != 0)
2031 puts ("pipe failed");
2032 exit (1);
2035 int result = 0;
2036 size_t cnt;
2037 for (cnt = 0; cnt < ntest_tf; ++cnt)
2039 if (tests[cnt].only_early)
2040 continue;
2042 if (pthread_barrier_init (&b2, NULL, tests[cnt].nb) != 0)
2044 puts ("b2 init failed");
2045 exit (1);
2048 /* Reset the counter for the cleanup handler. */
2049 cl_called = 0;
2051 pthread_t th;
2052 if (pthread_create (&th, NULL, tests[cnt].tf, NULL) != 0)
2054 printf ("create for '%s' test failed\n", tests[cnt].name);
2055 result = 1;
2056 continue;
2059 int r = pthread_barrier_wait (&b2);
2060 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
2062 printf ("%s: barrier_wait failed\n", __FUNCTION__);
2063 result = 1;
2064 continue;
2067 struct timespec ts = { .tv_sec = 0, .tv_nsec = 100000000 };
2068 while (nanosleep (&ts, &ts) != 0)
2069 continue;
2071 if (pthread_cancel (th) != 0)
2073 printf ("cancel for '%s' failed\n", tests[cnt].name);
2074 result = 1;
2075 continue;
2078 void *status;
2079 if (pthread_join (th, &status) != 0)
2081 printf ("join for '%s' failed\n", tests[cnt].name);
2082 result = 1;
2083 continue;
2085 if (status != PTHREAD_CANCELED)
2087 printf ("thread for '%s' not canceled\n", tests[cnt].name);
2088 result = 1;
2089 continue;
2092 if (pthread_barrier_destroy (&b2) != 0)
2094 puts ("barrier_destroy failed");
2095 result = 1;
2096 continue;
2099 if (cl_called == 0)
2101 printf ("cleanup handler not called for '%s'\n", tests[cnt].name);
2102 result = 1;
2103 continue;
2105 if (cl_called > 1)
2107 printf ("cleanup handler called more than once for '%s'\n",
2108 tests[cnt].name);
2109 result = 1;
2110 continue;
2113 printf ("in-time cancel test of '%s' successful\n", tests[cnt].name);
2115 if (tempfd != -1)
2117 close (tempfd);
2118 tempfd = -1;
2120 if (tempfd2 != -1)
2122 close (tempfd2);
2123 tempfd2 = -1;
2125 if (tempfname != NULL)
2127 unlink (tempfname);
2128 free (tempfname);
2129 tempfname = NULL;
2131 if (tempmsg != -1)
2133 msgctl (tempmsg, IPC_RMID, NULL);
2134 tempmsg = -1;
2138 for (cnt = 0; cnt < ntest_tf; ++cnt)
2140 if (pthread_barrier_init (&b2, NULL, tests[cnt].nb) != 0)
2142 puts ("b2 init failed");
2143 exit (1);
2146 /* Reset the counter for the cleanup handler. */
2147 cl_called = 0;
2149 pthread_t th;
2150 if (pthread_create (&th, NULL, tests[cnt].tf, (void *) 1l) != 0)
2152 printf ("create for '%s' test failed\n", tests[cnt].name);
2153 result = 1;
2154 continue;
2157 int r = pthread_barrier_wait (&b2);
2158 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
2160 printf ("%s: barrier_wait failed\n", __FUNCTION__);
2161 result = 1;
2162 continue;
2165 if (pthread_cancel (th) != 0)
2167 printf ("cancel for '%s' failed\n", tests[cnt].name);
2168 result = 1;
2169 continue;
2172 r = pthread_barrier_wait (&b2);
2173 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
2175 printf ("%s: barrier_wait failed\n", __FUNCTION__);
2176 result = 1;
2177 continue;
2180 void *status;
2181 if (pthread_join (th, &status) != 0)
2183 printf ("join for '%s' failed\n", tests[cnt].name);
2184 result = 1;
2185 continue;
2187 if (status != PTHREAD_CANCELED)
2189 printf ("thread for '%s' not canceled\n", tests[cnt].name);
2190 result = 1;
2191 continue;
2194 if (pthread_barrier_destroy (&b2) != 0)
2196 puts ("barrier_destroy failed");
2197 result = 1;
2198 continue;
2201 if (cl_called == 0)
2203 printf ("cleanup handler not called for '%s'\n", tests[cnt].name);
2204 result = 1;
2205 continue;
2207 if (cl_called > 1)
2209 printf ("cleanup handler called more than once for '%s'\n",
2210 tests[cnt].name);
2211 result = 1;
2212 continue;
2215 printf ("early cancel test of '%s' successful\n", tests[cnt].name);
2217 if (tempfd != -1)
2219 close (tempfd);
2220 tempfd = -1;
2222 if (tempfd2 != -1)
2224 close (tempfd2);
2225 tempfd2 = -1;
2227 if (tempfname != NULL)
2229 unlink (tempfname);
2230 free (tempfname);
2231 tempfname = NULL;
2233 if (tempmsg != -1)
2235 msgctl (tempmsg, IPC_RMID, NULL);
2236 tempmsg = -1;
2240 return result;
2243 #define TIMEOUT 60
2244 #define TEST_FUNCTION do_test ()
2245 #include "../test-skeleton.c"