Fix dbl-64 asin (sNaN) (bug 20213).
[glibc.git] / nptl / tst-cancel4.c
blob60f7eada908c9b6cc6d35622520cf4fd59503cf1
1 /* Copyright (C) 2002-2016 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, see
17 <http://www.gnu.org/licenses/>. */
19 /* NOTE: this tests functionality beyond POSIX. POSIX does not allow
20 exit to be called more than once. */
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <limits.h>
25 #include <pthread.h>
26 #include <stddef.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <termios.h>
31 #include <unistd.h>
32 #include <sys/mman.h>
33 #include <sys/msg.h>
34 #include <sys/poll.h>
35 #include <sys/select.h>
36 #include <sys/socket.h>
37 #include <sys/uio.h>
38 #include <sys/un.h>
39 #include <sys/wait.h>
41 #include "pthreadP.h"
44 /* Since STREAMS are not supported in the standard Linux kernel and
45 there we don't advertise STREAMS as supported is no need to test
46 the STREAMS related functions. This affects
47 getmsg() getpmsg() putmsg()
48 putpmsg()
50 lockf() and fcntl() are tested in tst-cancel16.
52 pthread_join() is tested in tst-join5.
54 pthread_testcancel()'s only purpose is to allow cancellation. This
55 is tested in several places.
57 sem_wait() and sem_timedwait() are checked in tst-cancel1[2345] tests.
59 mq_send(), mq_timedsend(), mq_receive() and mq_timedreceive() are checked
60 in tst-mqueue8{,x} tests.
62 aio_suspend() is tested in tst-cancel17.
64 clock_nanosleep() is tested in tst-cancel18.
67 /* Pipe descriptors. */
68 static int fds[2];
70 /* Temporary file descriptor, to be closed after each round. */
71 static int tempfd = -1;
72 static int tempfd2 = -1;
73 /* Name of temporary file to be removed after each round. */
74 static char *tempfname;
75 /* Temporary message queue. */
76 static int tempmsg = -1;
78 /* Often used barrier for two threads. */
79 static pthread_barrier_t b2;
82 #ifndef IPC_ADDVAL
83 # define IPC_ADDVAL 0
84 #endif
86 /* The WRITE_BUFFER_SIZE value needs to be chosen such that if we set
87 the socket send buffer size to '1', a write of this size on that
88 socket will block.
90 The Linux kernel imposes a minimum send socket buffer size which
91 has changed over the years. As of Linux 3.10 the value is:
93 2 * (2048 + SKB_DATA_ALIGN(sizeof(struct sk_buff)))
95 which is attempting to make sure that with standard MTUs,
96 TCP can always queue up at least 2 full sized packets.
98 Furthermore, there is logic in the socket send paths that
99 will allow one more packet (of any size) to be queued up as
100 long as some socket buffer space remains. Blocking only
101 occurs when we try to queue up a new packet and the send
102 buffer space has already been fully consumed.
104 Therefore we must set this value to the largest possible value of
105 the formula above (and since it depends upon the size of "struct
106 sk_buff", it is dependent upon machine word size etc.) plus some
107 slack space. */
109 #define WRITE_BUFFER_SIZE 16384
111 /* Cleanup handling test. */
112 static int cl_called;
114 static void
115 cl (void *arg)
117 ++cl_called;
122 static void *
123 tf_read (void *arg)
125 int fd;
126 int r;
128 if (arg == NULL)
129 fd = fds[0];
130 else
132 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
133 tempfd = fd = mkstemp (fname);
134 if (fd == -1)
135 printf ("%s: mkstemp failed\n", __FUNCTION__);
136 unlink (fname);
138 r = pthread_barrier_wait (&b2);
139 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
141 printf ("%s: barrier_wait failed\n", __FUNCTION__);
142 exit (1);
146 r = pthread_barrier_wait (&b2);
147 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
149 printf ("%s: barrier_wait failed\n", __FUNCTION__);
150 exit (1);
153 ssize_t s;
154 pthread_cleanup_push (cl, NULL);
156 char buf[100];
157 s = read (fd, buf, sizeof (buf));
159 pthread_cleanup_pop (0);
161 printf ("%s: read returns with %zd\n", __FUNCTION__, s);
163 exit (1);
167 static void *
168 tf_readv (void *arg)
170 int fd;
171 int r;
173 if (arg == NULL)
174 fd = fds[0];
175 else
177 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
178 tempfd = fd = mkstemp (fname);
179 if (fd == -1)
180 printf ("%s: mkstemp failed\n", __FUNCTION__);
181 unlink (fname);
183 r = pthread_barrier_wait (&b2);
184 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
186 printf ("%s: barrier_wait failed\n", __FUNCTION__);
187 exit (1);
191 r = pthread_barrier_wait (&b2);
192 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
194 printf ("%s: barrier_wait failed\n", __FUNCTION__);
195 exit (1);
198 ssize_t s;
199 pthread_cleanup_push (cl, NULL);
201 char buf[100];
202 struct iovec iov[1] = { [0] = { .iov_base = buf, .iov_len = sizeof (buf) } };
203 s = readv (fd, iov, 1);
205 pthread_cleanup_pop (0);
207 printf ("%s: readv returns with %zd\n", __FUNCTION__, s);
209 exit (1);
213 static void *
214 tf_write (void *arg)
216 int fd;
217 int r;
219 if (arg == NULL)
220 fd = fds[1];
221 else
223 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
224 tempfd = fd = mkstemp (fname);
225 if (fd == -1)
226 printf ("%s: mkstemp failed\n", __FUNCTION__);
227 unlink (fname);
229 r = pthread_barrier_wait (&b2);
230 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
232 printf ("%s: barrier_wait failed\n", __FUNCTION__);
233 exit (1);
237 r = pthread_barrier_wait (&b2);
238 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
240 printf ("%s: barrier_wait failed\n", __FUNCTION__);
241 exit (1);
244 ssize_t s;
245 pthread_cleanup_push (cl, NULL);
247 char buf[WRITE_BUFFER_SIZE];
248 memset (buf, '\0', sizeof (buf));
249 s = write (fd, buf, sizeof (buf));
251 pthread_cleanup_pop (0);
253 printf ("%s: write returns with %zd\n", __FUNCTION__, s);
255 exit (1);
259 static void *
260 tf_writev (void *arg)
262 int fd;
263 int r;
265 if (arg == NULL)
266 fd = fds[1];
267 else
269 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
270 tempfd = fd = mkstemp (fname);
271 if (fd == -1)
272 printf ("%s: mkstemp failed\n", __FUNCTION__);
273 unlink (fname);
275 r = pthread_barrier_wait (&b2);
276 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
278 printf ("%s: barrier_wait failed\n", __FUNCTION__);
279 exit (1);
283 r = pthread_barrier_wait (&b2);
284 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
286 printf ("%s: barrier_wait failed\n", __FUNCTION__);
287 exit (1);
290 ssize_t s;
291 pthread_cleanup_push (cl, NULL);
293 char buf[WRITE_BUFFER_SIZE];
294 memset (buf, '\0', sizeof (buf));
295 struct iovec iov[1] = { [0] = { .iov_base = buf, .iov_len = sizeof (buf) } };
296 s = writev (fd, iov, 1);
298 pthread_cleanup_pop (0);
300 printf ("%s: writev returns with %zd\n", __FUNCTION__, s);
302 exit (1);
306 static void *
307 tf_sleep (void *arg)
309 int r = pthread_barrier_wait (&b2);
310 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
312 printf ("%s: barrier_wait failed\n", __FUNCTION__);
313 exit (1);
316 if (arg != NULL)
318 r = pthread_barrier_wait (&b2);
319 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
321 printf ("%s: barrier_wait failed\n", __FUNCTION__);
322 exit (1);
326 pthread_cleanup_push (cl, NULL);
328 sleep (arg == NULL ? 1000000 : 0);
330 pthread_cleanup_pop (0);
332 printf ("%s: sleep returns\n", __FUNCTION__);
334 exit (1);
338 static void *
339 tf_usleep (void *arg)
341 int r = pthread_barrier_wait (&b2);
342 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
344 printf ("%s: barrier_wait failed\n", __FUNCTION__);
345 exit (1);
348 if (arg != NULL)
350 r = pthread_barrier_wait (&b2);
351 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
353 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
354 exit (1);
358 pthread_cleanup_push (cl, NULL);
360 usleep (arg == NULL ? (useconds_t) ULONG_MAX : 0);
362 pthread_cleanup_pop (0);
364 printf ("%s: usleep returns\n", __FUNCTION__);
366 exit (1);
370 static void *
371 tf_nanosleep (void *arg)
373 int r = pthread_barrier_wait (&b2);
374 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
376 printf ("%s: barrier_wait failed\n", __FUNCTION__);
377 exit (1);
380 if (arg != NULL)
382 r = pthread_barrier_wait (&b2);
383 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
385 printf ("%s: barrier_wait failed\n", __FUNCTION__);
386 exit (1);
390 pthread_cleanup_push (cl, NULL);
392 struct timespec ts = { .tv_sec = arg == NULL ? 10000000 : 0, .tv_nsec = 0 };
393 TEMP_FAILURE_RETRY (nanosleep (&ts, &ts));
395 pthread_cleanup_pop (0);
397 printf ("%s: nanosleep returns\n", __FUNCTION__);
399 exit (1);
403 static void *
404 tf_select (void *arg)
406 int fd;
407 int r;
409 if (arg == NULL)
410 fd = fds[0];
411 else
413 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
414 tempfd = fd = mkstemp (fname);
415 if (fd == -1)
416 printf ("%s: mkstemp failed\n", __FUNCTION__);
417 unlink (fname);
419 r = pthread_barrier_wait (&b2);
420 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
422 printf ("%s: barrier_wait failed\n", __FUNCTION__);
423 exit (1);
427 r = pthread_barrier_wait (&b2);
428 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
430 printf ("%s: barrier_wait failed\n", __FUNCTION__);
431 exit (1);
434 fd_set rfs;
435 FD_ZERO (&rfs);
436 FD_SET (fd, &rfs);
438 int s;
439 pthread_cleanup_push (cl, NULL);
441 s = select (fd + 1, &rfs, NULL, NULL, NULL);
443 pthread_cleanup_pop (0);
445 printf ("%s: select returns with %d (%s)\n", __FUNCTION__, s,
446 strerror (errno));
448 exit (1);
452 static void *
453 tf_pselect (void *arg)
455 int fd;
456 int r;
458 if (arg == NULL)
459 fd = fds[0];
460 else
462 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
463 tempfd = fd = mkstemp (fname);
464 if (fd == -1)
465 printf ("%s: mkstemp failed\n", __FUNCTION__);
466 unlink (fname);
468 r = pthread_barrier_wait (&b2);
469 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
471 printf ("%s: barrier_wait failed\n", __FUNCTION__);
472 exit (1);
476 r = pthread_barrier_wait (&b2);
477 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
479 printf ("%s: barrier_wait failed\n", __FUNCTION__);
480 exit (1);
483 fd_set rfs;
484 FD_ZERO (&rfs);
485 FD_SET (fd, &rfs);
487 int s;
488 pthread_cleanup_push (cl, NULL);
490 s = pselect (fd + 1, &rfs, NULL, NULL, NULL, NULL);
492 pthread_cleanup_pop (0);
494 printf ("%s: pselect returns with %d (%s)\n", __FUNCTION__, s,
495 strerror (errno));
497 exit (1);
501 static void *
502 tf_poll (void *arg)
504 int fd;
505 int r;
507 if (arg == NULL)
508 fd = fds[0];
509 else
511 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
512 tempfd = fd = mkstemp (fname);
513 if (fd == -1)
514 printf ("%s: mkstemp failed\n", __FUNCTION__);
515 unlink (fname);
517 r = pthread_barrier_wait (&b2);
518 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
520 printf ("%s: barrier_wait failed\n", __FUNCTION__);
521 exit (1);
525 r = pthread_barrier_wait (&b2);
526 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
528 printf ("%s: barrier_wait failed\n", __FUNCTION__);
529 exit (1);
532 struct pollfd rfs[1] = { [0] = { .fd = fd, .events = POLLIN } };
534 int s;
535 pthread_cleanup_push (cl, NULL);
537 s = poll (rfs, 1, -1);
539 pthread_cleanup_pop (0);
541 printf ("%s: poll returns with %d (%s)\n", __FUNCTION__, s,
542 strerror (errno));
544 exit (1);
548 static void *
549 tf_ppoll (void *arg)
551 int fd;
552 int r;
554 if (arg == NULL)
555 fd = fds[0];
556 else
558 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
559 tempfd = fd = mkstemp (fname);
560 if (fd == -1)
561 printf ("%s: mkstemp failed\n", __FUNCTION__);
562 unlink (fname);
564 r = pthread_barrier_wait (&b2);
565 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
567 printf ("%s: barrier_wait failed\n", __FUNCTION__);
568 exit (1);
572 r = pthread_barrier_wait (&b2);
573 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
575 printf ("%s: barrier_wait failed\n", __FUNCTION__);
576 exit (1);
579 struct pollfd rfs[1] = { [0] = { .fd = fd, .events = POLLIN } };
581 int s;
582 pthread_cleanup_push (cl, NULL);
584 s = ppoll (rfs, 1, NULL, NULL);
586 pthread_cleanup_pop (0);
588 printf ("%s: ppoll returns with %d (%s)\n", __FUNCTION__, s,
589 strerror (errno));
591 exit (1);
595 static void *
596 tf_wait (void *arg)
598 pid_t pid = fork ();
599 if (pid == -1)
601 puts ("fork failed");
602 exit (1);
605 if (pid == 0)
607 /* Make the program disappear after a while. */
608 if (arg == NULL)
609 sleep (10);
610 exit (0);
613 int r;
614 if (arg != NULL)
616 struct timespec ts = { .tv_sec = 0, .tv_nsec = 100000000 };
617 while (nanosleep (&ts, &ts) != 0)
618 continue;
620 r = pthread_barrier_wait (&b2);
621 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
623 printf ("%s: barrier_wait failed\n", __FUNCTION__);
624 exit (1);
628 r = pthread_barrier_wait (&b2);
629 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
631 printf ("%s: barrier_wait failed\n", __FUNCTION__);
632 exit (1);
635 int s;
636 pthread_cleanup_push (cl, NULL);
638 s = wait (NULL);
640 pthread_cleanup_pop (0);
642 printf ("%s: wait returns with %d (%s)\n", __FUNCTION__, s,
643 strerror (errno));
645 exit (1);
649 static void *
650 tf_waitpid (void *arg)
653 pid_t pid = fork ();
654 if (pid == -1)
656 puts ("fork failed");
657 exit (1);
660 if (pid == 0)
662 /* Make the program disappear after a while. */
663 if (arg == NULL)
664 sleep (10);
665 exit (0);
668 int r;
669 if (arg != NULL)
671 struct timespec ts = { .tv_sec = 0, .tv_nsec = 100000000 };
672 while (nanosleep (&ts, &ts) != 0)
673 continue;
675 r = pthread_barrier_wait (&b2);
676 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
678 printf ("%s: barrier_wait failed\n", __FUNCTION__);
679 exit (1);
683 r = pthread_barrier_wait (&b2);
684 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
686 printf ("%s: barrier_wait failed\n", __FUNCTION__);
687 exit (1);
690 int s;
691 pthread_cleanup_push (cl, NULL);
693 s = waitpid (-1, NULL, 0);
695 pthread_cleanup_pop (0);
697 printf ("%s: waitpid returns with %d (%s)\n", __FUNCTION__, s,
698 strerror (errno));
700 exit (1);
704 static void *
705 tf_waitid (void *arg)
707 pid_t pid = fork ();
708 if (pid == -1)
710 puts ("fork failed");
711 exit (1);
714 if (pid == 0)
716 /* Make the program disappear after a while. */
717 if (arg == NULL)
718 sleep (10);
719 exit (0);
722 int r;
723 if (arg != NULL)
725 struct timespec ts = { .tv_sec = 0, .tv_nsec = 100000000 };
726 while (nanosleep (&ts, &ts) != 0)
727 continue;
729 r = pthread_barrier_wait (&b2);
730 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
732 printf ("%s: barrier_wait failed\n", __FUNCTION__);
733 exit (1);
737 r = pthread_barrier_wait (&b2);
738 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
740 printf ("%s: barrier_wait failed\n", __FUNCTION__);
741 exit (1);
744 int s;
745 pthread_cleanup_push (cl, NULL);
747 #ifndef WEXITED
748 # define WEXITED 0
749 #endif
750 siginfo_t si;
751 s = waitid (P_PID, pid, &si, WEXITED);
753 pthread_cleanup_pop (0);
755 printf ("%s: waitid returns with %d (%s)\n", __FUNCTION__, s,
756 strerror (errno));
758 exit (1);
762 static void *
763 tf_sigpause (void *arg)
765 int r = pthread_barrier_wait (&b2);
766 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
768 printf ("%s: barrier_wait failed\n", __FUNCTION__);
769 exit (1);
772 if (arg != NULL)
774 r = pthread_barrier_wait (&b2);
775 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
777 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
778 exit (1);
782 pthread_cleanup_push (cl, NULL);
784 #ifdef SIGCANCEL
785 /* Just for fun block the cancellation signal. We need to use
786 __xpg_sigpause since otherwise we will get the BSD version. */
787 __xpg_sigpause (SIGCANCEL);
788 #else
789 pause ();
790 #endif
792 pthread_cleanup_pop (0);
794 printf ("%s: sigpause returned\n", __FUNCTION__);
796 exit (1);
800 static void *
801 tf_sigsuspend (void *arg)
803 int r = pthread_barrier_wait (&b2);
804 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
806 printf ("%s: barrier_wait failed\n", __FUNCTION__);
807 exit (1);
810 if (arg != NULL)
812 r = pthread_barrier_wait (&b2);
813 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
815 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
816 exit (1);
820 pthread_cleanup_push (cl, NULL);
822 /* Just for fun block all signals. */
823 sigset_t mask;
824 sigfillset (&mask);
825 sigsuspend (&mask);
827 pthread_cleanup_pop (0);
829 printf ("%s: sigsuspend returned\n", __FUNCTION__);
831 exit (1);
835 static void *
836 tf_sigwait (void *arg)
838 int r = pthread_barrier_wait (&b2);
839 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
841 printf ("%s: barrier_wait failed\n", __FUNCTION__);
842 exit (1);
845 if (arg != NULL)
847 r = pthread_barrier_wait (&b2);
848 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
850 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
851 exit (1);
855 /* Block SIGUSR1. */
856 sigset_t mask;
857 sigemptyset (&mask);
858 sigaddset (&mask, SIGUSR1);
859 if (pthread_sigmask (SIG_BLOCK, &mask, NULL) != 0)
861 printf ("%s: pthread_sigmask failed\n", __FUNCTION__);
862 exit (1);
865 int sig;
866 pthread_cleanup_push (cl, NULL);
868 /* Wait for SIGUSR1. */
869 sigwait (&mask, &sig);
871 pthread_cleanup_pop (0);
873 printf ("%s: sigwait returned with signal %d\n", __FUNCTION__, sig);
875 exit (1);
879 static void *
880 tf_sigwaitinfo (void *arg)
882 int r = pthread_barrier_wait (&b2);
883 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
885 printf ("%s: barrier_wait failed\n", __FUNCTION__);
886 exit (1);
889 if (arg != NULL)
891 r = pthread_barrier_wait (&b2);
892 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
894 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
895 exit (1);
899 /* Block SIGUSR1. */
900 sigset_t mask;
901 sigemptyset (&mask);
902 sigaddset (&mask, SIGUSR1);
903 if (pthread_sigmask (SIG_BLOCK, &mask, NULL) != 0)
905 printf ("%s: pthread_sigmask failed\n", __FUNCTION__);
906 exit (1);
909 siginfo_t info;
910 pthread_cleanup_push (cl, NULL);
912 /* Wait for SIGUSR1. */
913 sigwaitinfo (&mask, &info);
915 pthread_cleanup_pop (0);
917 printf ("%s: sigwaitinfo returned with signal %d\n", __FUNCTION__,
918 info.si_signo);
920 exit (1);
924 static void *
925 tf_sigtimedwait (void *arg)
927 int r = pthread_barrier_wait (&b2);
928 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
930 printf ("%s: barrier_wait failed\n", __FUNCTION__);
931 exit (1);
934 if (arg != NULL)
936 r = pthread_barrier_wait (&b2);
937 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
939 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
940 exit (1);
944 /* Block SIGUSR1. */
945 sigset_t mask;
946 sigemptyset (&mask);
947 sigaddset (&mask, SIGUSR1);
948 if (pthread_sigmask (SIG_BLOCK, &mask, NULL) != 0)
950 printf ("%s: pthread_sigmask failed\n", __FUNCTION__);
951 exit (1);
954 /* Wait for SIGUSR1. */
955 siginfo_t info;
956 struct timespec ts = { .tv_sec = 60, .tv_nsec = 0 };
957 pthread_cleanup_push (cl, NULL);
959 sigtimedwait (&mask, &info, &ts);
961 pthread_cleanup_pop (0);
963 printf ("%s: sigtimedwait returned with signal %d\n", __FUNCTION__,
964 info.si_signo);
966 exit (1);
970 static void *
971 tf_pause (void *arg)
973 int r = pthread_barrier_wait (&b2);
974 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
976 printf ("%s: barrier_wait failed\n", __FUNCTION__);
977 exit (1);
980 if (arg != NULL)
982 r = pthread_barrier_wait (&b2);
983 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
985 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
986 exit (1);
990 pthread_cleanup_push (cl, NULL);
992 pause ();
994 pthread_cleanup_pop (0);
996 printf ("%s: pause returned\n", __FUNCTION__);
998 exit (1);
1002 static void *
1003 tf_accept (void *arg)
1005 struct sockaddr_un sun;
1006 /* To test a non-blocking accept call we make the call file by using
1007 a datagrame socket. */
1008 int pf = arg == NULL ? SOCK_STREAM : SOCK_DGRAM;
1010 tempfd = socket (AF_UNIX, pf, 0);
1011 if (tempfd == -1)
1013 printf ("%s: socket call failed\n", __FUNCTION__);
1014 exit (1);
1017 int tries = 0;
1020 if (++tries > 10)
1022 printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
1025 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-1-XXXXXX");
1026 if (mktemp (sun.sun_path) == NULL)
1028 printf ("%s: cannot generate temp file name\n", __FUNCTION__);
1029 exit (1);
1032 sun.sun_family = AF_UNIX;
1034 while (bind (tempfd, (struct sockaddr *) &sun,
1035 offsetof (struct sockaddr_un, sun_path)
1036 + strlen (sun.sun_path) + 1) != 0);
1038 unlink (sun.sun_path);
1040 listen (tempfd, 5);
1042 socklen_t len = sizeof (sun);
1044 int r = pthread_barrier_wait (&b2);
1045 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1047 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1048 exit (1);
1051 if (arg != NULL)
1053 r = pthread_barrier_wait (&b2);
1054 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1056 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1057 exit (1);
1061 pthread_cleanup_push (cl, NULL);
1063 accept (tempfd, (struct sockaddr *) &sun, &len);
1065 pthread_cleanup_pop (0);
1067 printf ("%s: accept returned\n", __FUNCTION__);
1069 exit (1);
1073 static void *
1074 tf_send (void *arg)
1076 struct sockaddr_un sun;
1078 tempfd = socket (AF_UNIX, SOCK_STREAM, 0);
1079 if (tempfd == -1)
1081 printf ("%s: first socket call failed\n", __FUNCTION__);
1082 exit (1);
1085 int tries = 0;
1088 if (++tries > 10)
1090 printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
1093 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-2-XXXXXX");
1094 if (mktemp (sun.sun_path) == NULL)
1096 printf ("%s: cannot generate temp file name\n", __FUNCTION__);
1097 exit (1);
1100 sun.sun_family = AF_UNIX;
1102 while (bind (tempfd, (struct sockaddr *) &sun,
1103 offsetof (struct sockaddr_un, sun_path)
1104 + strlen (sun.sun_path) + 1) != 0);
1106 listen (tempfd, 5);
1108 tempfd2 = socket (AF_UNIX, SOCK_STREAM, 0);
1109 if (tempfd2 == -1)
1111 printf ("%s: second socket call failed\n", __FUNCTION__);
1112 exit (1);
1115 if (connect (tempfd2, (struct sockaddr *) &sun, sizeof (sun)) != 0)
1117 printf ("%s: connect failed\n", __FUNCTION__);
1118 exit(1);
1121 unlink (sun.sun_path);
1123 int r = pthread_barrier_wait (&b2);
1124 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1126 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1127 exit (1);
1130 if (arg != NULL)
1132 r = pthread_barrier_wait (&b2);
1133 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1135 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1136 exit (1);
1140 pthread_cleanup_push (cl, NULL);
1142 /* Very large block, so that the send call blocks. */
1143 char mem[700000];
1145 send (tempfd2, mem, arg == NULL ? sizeof (mem) : 1, 0);
1147 pthread_cleanup_pop (0);
1149 printf ("%s: send returned\n", __FUNCTION__);
1151 exit (1);
1155 static void *
1156 tf_recv (void *arg)
1158 struct sockaddr_un sun;
1160 tempfd = socket (AF_UNIX, SOCK_STREAM, 0);
1161 if (tempfd == -1)
1163 printf ("%s: first socket call failed\n", __FUNCTION__);
1164 exit (1);
1167 int tries = 0;
1170 if (++tries > 10)
1172 printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
1175 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-3-XXXXXX");
1176 if (mktemp (sun.sun_path) == NULL)
1178 printf ("%s: cannot generate temp file name\n", __FUNCTION__);
1179 exit (1);
1182 sun.sun_family = AF_UNIX;
1184 while (bind (tempfd, (struct sockaddr *) &sun,
1185 offsetof (struct sockaddr_un, sun_path)
1186 + strlen (sun.sun_path) + 1) != 0);
1188 listen (tempfd, 5);
1190 tempfd2 = socket (AF_UNIX, SOCK_STREAM, 0);
1191 if (tempfd2 == -1)
1193 printf ("%s: second socket call failed\n", __FUNCTION__);
1194 exit (1);
1197 if (connect (tempfd2, (struct sockaddr *) &sun, sizeof (sun)) != 0)
1199 printf ("%s: connect failed\n", __FUNCTION__);
1200 exit(1);
1203 unlink (sun.sun_path);
1205 int r = pthread_barrier_wait (&b2);
1206 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1208 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1209 exit (1);
1212 if (arg != NULL)
1214 r = pthread_barrier_wait (&b2);
1215 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1217 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1218 exit (1);
1222 pthread_cleanup_push (cl, NULL);
1224 char mem[70];
1226 recv (tempfd2, mem, arg == NULL ? sizeof (mem) : 0, 0);
1228 pthread_cleanup_pop (0);
1230 printf ("%s: recv returned\n", __FUNCTION__);
1232 exit (1);
1236 static void *
1237 tf_recvfrom (void *arg)
1239 struct sockaddr_un sun;
1241 tempfd = socket (AF_UNIX, SOCK_DGRAM, 0);
1242 if (tempfd == -1)
1244 printf ("%s: first socket call failed\n", __FUNCTION__);
1245 exit (1);
1248 int tries = 0;
1251 if (++tries > 10)
1253 printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
1256 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-4-XXXXXX");
1257 if (mktemp (sun.sun_path) == NULL)
1259 printf ("%s: cannot generate temp file name\n", __FUNCTION__);
1260 exit (1);
1263 sun.sun_family = AF_UNIX;
1265 while (bind (tempfd, (struct sockaddr *) &sun,
1266 offsetof (struct sockaddr_un, sun_path)
1267 + strlen (sun.sun_path) + 1) != 0);
1269 tempfname = strdup (sun.sun_path);
1271 tempfd2 = socket (AF_UNIX, SOCK_DGRAM, 0);
1272 if (tempfd2 == -1)
1274 printf ("%s: second socket call failed\n", __FUNCTION__);
1275 exit (1);
1278 int r = pthread_barrier_wait (&b2);
1279 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1281 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1282 exit (1);
1285 if (arg != NULL)
1287 r = pthread_barrier_wait (&b2);
1288 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1290 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1291 exit (1);
1295 pthread_cleanup_push (cl, NULL);
1297 char mem[70];
1298 socklen_t len = sizeof (sun);
1300 recvfrom (tempfd2, mem, arg == NULL ? sizeof (mem) : 0, 0,
1301 (struct sockaddr *) &sun, &len);
1303 pthread_cleanup_pop (0);
1305 printf ("%s: recvfrom returned\n", __FUNCTION__);
1307 exit (1);
1311 static void *
1312 tf_recvmsg (void *arg)
1314 struct sockaddr_un sun;
1316 tempfd = socket (AF_UNIX, SOCK_DGRAM, 0);
1317 if (tempfd == -1)
1319 printf ("%s: first socket call failed\n", __FUNCTION__);
1320 exit (1);
1323 int tries = 0;
1326 if (++tries > 10)
1328 printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
1331 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-5-XXXXXX");
1332 if (mktemp (sun.sun_path) == NULL)
1334 printf ("%s: cannot generate temp file name\n", __FUNCTION__);
1335 exit (1);
1338 sun.sun_family = AF_UNIX;
1340 while (bind (tempfd, (struct sockaddr *) &sun,
1341 offsetof (struct sockaddr_un, sun_path)
1342 + strlen (sun.sun_path) + 1) != 0);
1344 tempfname = strdup (sun.sun_path);
1346 tempfd2 = socket (AF_UNIX, SOCK_DGRAM, 0);
1347 if (tempfd2 == -1)
1349 printf ("%s: second socket call failed\n", __FUNCTION__);
1350 exit (1);
1353 int r = pthread_barrier_wait (&b2);
1354 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1356 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1357 exit (1);
1360 if (arg != NULL)
1362 r = pthread_barrier_wait (&b2);
1363 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1365 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1366 exit (1);
1370 pthread_cleanup_push (cl, NULL);
1372 char mem[70];
1373 struct iovec iov[1];
1374 iov[0].iov_base = mem;
1375 iov[0].iov_len = arg == NULL ? sizeof (mem) : 0;
1377 struct msghdr m;
1378 m.msg_name = &sun;
1379 m.msg_namelen = sizeof (sun);
1380 m.msg_iov = iov;
1381 m.msg_iovlen = 1;
1382 m.msg_control = NULL;
1383 m.msg_controllen = 0;
1385 recvmsg (tempfd2, &m, 0);
1387 pthread_cleanup_pop (0);
1389 printf ("%s: recvmsg returned\n", __FUNCTION__);
1391 exit (1);
1395 static void *
1396 tf_open (void *arg)
1398 if (arg == NULL)
1399 // XXX If somebody can provide a portable test case in which open()
1400 // blocks we can enable this test to run in both rounds.
1401 abort ();
1403 int r = pthread_barrier_wait (&b2);
1404 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1406 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1407 exit (1);
1410 r = pthread_barrier_wait (&b2);
1411 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1413 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1414 exit (1);
1417 pthread_cleanup_push (cl, NULL);
1419 open ("Makefile", O_RDONLY);
1421 pthread_cleanup_pop (0);
1423 printf ("%s: open returned\n", __FUNCTION__);
1425 exit (1);
1429 static void *
1430 tf_close (void *arg)
1432 if (arg == NULL)
1433 // XXX If somebody can provide a portable test case in which close()
1434 // blocks we can enable this test to run in both rounds.
1435 abort ();
1437 char fname[] = "/tmp/tst-cancel-fd-XXXXXX";
1438 tempfd = mkstemp (fname);
1439 if (tempfd == -1)
1441 printf ("%s: mkstemp failed\n", __FUNCTION__);
1442 exit (1);
1444 unlink (fname);
1446 int r = pthread_barrier_wait (&b2);
1447 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1449 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1450 exit (1);
1453 r = pthread_barrier_wait (&b2);
1454 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1456 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1457 exit (1);
1460 pthread_cleanup_push (cl, NULL);
1462 close (tempfd);
1464 pthread_cleanup_pop (0);
1466 printf ("%s: close returned\n", __FUNCTION__);
1468 exit (1);
1472 static void *
1473 tf_pread (void *arg)
1475 if (arg == NULL)
1476 // XXX If somebody can provide a portable test case in which pread()
1477 // blocks we can enable this test to run in both rounds.
1478 abort ();
1480 tempfd = open ("Makefile", O_RDONLY);
1481 if (tempfd == -1)
1483 printf ("%s: cannot open Makefile\n", __FUNCTION__);
1484 exit (1);
1487 int r = pthread_barrier_wait (&b2);
1488 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1490 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1491 exit (1);
1494 r = pthread_barrier_wait (&b2);
1495 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1497 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1498 exit (1);
1501 pthread_cleanup_push (cl, NULL);
1503 char mem[10];
1504 pread (tempfd, mem, sizeof (mem), 0);
1506 pthread_cleanup_pop (0);
1508 printf ("%s: pread returned\n", __FUNCTION__);
1510 exit (1);
1514 static void *
1515 tf_pwrite (void *arg)
1517 if (arg == NULL)
1518 // XXX If somebody can provide a portable test case in which pwrite()
1519 // blocks we can enable this test to run in both rounds.
1520 abort ();
1522 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
1523 tempfd = mkstemp (fname);
1524 if (tempfd == -1)
1526 printf ("%s: mkstemp failed\n", __FUNCTION__);
1527 exit (1);
1529 unlink (fname);
1531 int r = pthread_barrier_wait (&b2);
1532 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1534 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1535 exit (1);
1538 r = pthread_barrier_wait (&b2);
1539 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1541 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1542 exit (1);
1545 pthread_cleanup_push (cl, NULL);
1547 char mem[10];
1548 pwrite (tempfd, mem, sizeof (mem), 0);
1550 pthread_cleanup_pop (0);
1552 printf ("%s: pwrite returned\n", __FUNCTION__);
1554 exit (1);
1557 static void *
1558 tf_preadv (void *arg)
1560 int fd;
1561 int r;
1563 if (arg == NULL)
1564 /* XXX If somebody can provide a portable test case in which preadv
1565 blocks we can enable this test to run in both rounds. */
1566 abort ();
1568 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
1569 tempfd = fd = mkstemp (fname);
1570 if (fd == -1)
1571 printf ("%s: mkstemp failed\n", __FUNCTION__);
1572 unlink (fname);
1574 r = pthread_barrier_wait (&b2);
1575 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1577 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1578 exit (1);
1581 r = pthread_barrier_wait (&b2);
1582 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1584 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1585 exit (1);
1588 ssize_t s;
1589 pthread_cleanup_push (cl, NULL);
1591 char buf[100];
1592 struct iovec iov[1] = { [0] = { .iov_base = buf, .iov_len = sizeof (buf) } };
1593 s = preadv (fd, iov, 1, 0);
1595 pthread_cleanup_pop (0);
1597 printf ("%s: preadv returns with %zd\n", __FUNCTION__, s);
1599 exit (1);
1602 static void *
1603 tf_pwritev (void *arg)
1605 int fd;
1606 int r;
1608 if (arg == NULL)
1609 /* XXX If somebody can provide a portable test case in which pwritev
1610 blocks we can enable this test to run in both rounds. */
1611 abort ();
1613 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
1614 tempfd = fd = mkstemp (fname);
1615 if (fd == -1)
1616 printf ("%s: mkstemp failed\n", __FUNCTION__);
1617 unlink (fname);
1619 r = pthread_barrier_wait (&b2);
1620 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1622 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1623 exit (1);
1626 r = pthread_barrier_wait (&b2);
1627 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1629 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1630 exit (1);
1633 ssize_t s;
1634 pthread_cleanup_push (cl, NULL);
1636 char buf[WRITE_BUFFER_SIZE];
1637 memset (buf, '\0', sizeof (buf));
1638 struct iovec iov[1] = { [0] = { .iov_base = buf, .iov_len = sizeof (buf) } };
1639 s = pwritev (fd, iov, 1, 0);
1641 pthread_cleanup_pop (0);
1643 printf ("%s: pwritev returns with %zd\n", __FUNCTION__, s);
1645 exit (1);
1648 static void *
1649 tf_fsync (void *arg)
1651 if (arg == NULL)
1652 // XXX If somebody can provide a portable test case in which fsync()
1653 // blocks we can enable this test to run in both rounds.
1654 abort ();
1656 tempfd = open ("Makefile", O_RDONLY);
1657 if (tempfd == -1)
1659 printf ("%s: cannot open Makefile\n", __FUNCTION__);
1660 exit (1);
1663 int r = pthread_barrier_wait (&b2);
1664 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1666 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1667 exit (1);
1670 r = pthread_barrier_wait (&b2);
1671 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1673 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1674 exit (1);
1677 pthread_cleanup_push (cl, NULL);
1679 fsync (tempfd);
1681 pthread_cleanup_pop (0);
1683 printf ("%s: fsync returned\n", __FUNCTION__);
1685 exit (1);
1689 static void *
1690 tf_fdatasync (void *arg)
1692 if (arg == NULL)
1693 // XXX If somebody can provide a portable test case in which fdatasync()
1694 // blocks we can enable this test to run in both rounds.
1695 abort ();
1697 tempfd = open ("Makefile", O_RDONLY);
1698 if (tempfd == -1)
1700 printf ("%s: cannot open Makefile\n", __FUNCTION__);
1701 exit (1);
1704 int r = pthread_barrier_wait (&b2);
1705 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1707 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1708 exit (1);
1711 r = pthread_barrier_wait (&b2);
1712 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1714 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1715 exit (1);
1718 pthread_cleanup_push (cl, NULL);
1720 fdatasync (tempfd);
1722 pthread_cleanup_pop (0);
1724 printf ("%s: fdatasync returned\n", __FUNCTION__);
1726 exit (1);
1730 static void *
1731 tf_msync (void *arg)
1733 if (arg == NULL)
1734 // XXX If somebody can provide a portable test case in which msync()
1735 // blocks we can enable this test to run in both rounds.
1736 abort ();
1738 tempfd = open ("Makefile", O_RDONLY);
1739 if (tempfd == -1)
1741 printf ("%s: cannot open Makefile\n", __FUNCTION__);
1742 exit (1);
1744 void *p = mmap (NULL, 10, PROT_READ, MAP_SHARED, tempfd, 0);
1745 if (p == MAP_FAILED)
1747 printf ("%s: mmap failed\n", __FUNCTION__);
1748 exit (1);
1751 int r = pthread_barrier_wait (&b2);
1752 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1754 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1755 exit (1);
1758 r = pthread_barrier_wait (&b2);
1759 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1761 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1762 exit (1);
1765 pthread_cleanup_push (cl, NULL);
1767 msync (p, 10, 0);
1769 pthread_cleanup_pop (0);
1771 printf ("%s: msync returned\n", __FUNCTION__);
1773 exit (1);
1777 static void *
1778 tf_sendto (void *arg)
1780 if (arg == NULL)
1781 // XXX If somebody can provide a portable test case in which sendto()
1782 // blocks we can enable this test to run in both rounds.
1783 abort ();
1785 struct sockaddr_un sun;
1787 tempfd = socket (AF_UNIX, SOCK_DGRAM, 0);
1788 if (tempfd == -1)
1790 printf ("%s: first socket call failed\n", __FUNCTION__);
1791 exit (1);
1794 int tries = 0;
1797 if (++tries > 10)
1799 printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
1802 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-6-XXXXXX");
1803 if (mktemp (sun.sun_path) == NULL)
1805 printf ("%s: cannot generate temp file name\n", __FUNCTION__);
1806 exit (1);
1809 sun.sun_family = AF_UNIX;
1811 while (bind (tempfd, (struct sockaddr *) &sun,
1812 offsetof (struct sockaddr_un, sun_path)
1813 + strlen (sun.sun_path) + 1) != 0);
1814 tempfname = strdup (sun.sun_path);
1816 tempfd2 = socket (AF_UNIX, SOCK_DGRAM, 0);
1817 if (tempfd2 == -1)
1819 printf ("%s: second socket call failed\n", __FUNCTION__);
1820 exit (1);
1823 int r = pthread_barrier_wait (&b2);
1824 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1826 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1827 exit (1);
1830 r = pthread_barrier_wait (&b2);
1831 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1833 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1834 exit (1);
1837 pthread_cleanup_push (cl, NULL);
1839 char mem[1];
1841 sendto (tempfd2, mem, arg == NULL ? sizeof (mem) : 1, 0,
1842 (struct sockaddr *) &sun,
1843 offsetof (struct sockaddr_un, sun_path) + strlen (sun.sun_path) + 1);
1845 pthread_cleanup_pop (0);
1847 printf ("%s: sendto returned\n", __FUNCTION__);
1849 exit (1);
1853 static void *
1854 tf_sendmsg (void *arg)
1856 if (arg == NULL)
1857 // XXX If somebody can provide a portable test case in which sendmsg()
1858 // blocks we can enable this test to run in both rounds.
1859 abort ();
1861 struct sockaddr_un sun;
1863 tempfd = socket (AF_UNIX, SOCK_DGRAM, 0);
1864 if (tempfd == -1)
1866 printf ("%s: first socket call failed\n", __FUNCTION__);
1867 exit (1);
1870 int tries = 0;
1873 if (++tries > 10)
1875 printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
1878 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-7-XXXXXX");
1879 if (mktemp (sun.sun_path) == NULL)
1881 printf ("%s: cannot generate temp file name\n", __FUNCTION__);
1882 exit (1);
1885 sun.sun_family = AF_UNIX;
1887 while (bind (tempfd, (struct sockaddr *) &sun,
1888 offsetof (struct sockaddr_un, sun_path)
1889 + strlen (sun.sun_path) + 1) != 0);
1890 tempfname = strdup (sun.sun_path);
1892 tempfd2 = socket (AF_UNIX, SOCK_DGRAM, 0);
1893 if (tempfd2 == -1)
1895 printf ("%s: second socket call failed\n", __FUNCTION__);
1896 exit (1);
1899 int r = pthread_barrier_wait (&b2);
1900 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1902 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1903 exit (1);
1906 r = pthread_barrier_wait (&b2);
1907 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1909 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1910 exit (1);
1913 pthread_cleanup_push (cl, NULL);
1915 char mem[1];
1916 struct iovec iov[1];
1917 iov[0].iov_base = mem;
1918 iov[0].iov_len = 1;
1920 struct msghdr m;
1921 m.msg_name = &sun;
1922 m.msg_namelen = (offsetof (struct sockaddr_un, sun_path)
1923 + strlen (sun.sun_path) + 1);
1924 m.msg_iov = iov;
1925 m.msg_iovlen = 1;
1926 m.msg_control = NULL;
1927 m.msg_controllen = 0;
1929 sendmsg (tempfd2, &m, 0);
1931 pthread_cleanup_pop (0);
1933 printf ("%s: sendmsg returned\n", __FUNCTION__);
1935 exit (1);
1939 static void *
1940 tf_creat (void *arg)
1942 if (arg == NULL)
1943 // XXX If somebody can provide a portable test case in which sendmsg()
1944 // blocks we can enable this test to run in both rounds.
1945 abort ();
1947 int r = pthread_barrier_wait (&b2);
1948 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1950 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1951 exit (1);
1954 r = pthread_barrier_wait (&b2);
1955 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1957 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1958 exit (1);
1961 pthread_cleanup_push (cl, NULL);
1963 creat ("tmp/tst-cancel-4-should-not-exist", 0666);
1965 pthread_cleanup_pop (0);
1967 printf ("%s: creat returned\n", __FUNCTION__);
1969 exit (1);
1973 static void *
1974 tf_connect (void *arg)
1976 if (arg == NULL)
1977 // XXX If somebody can provide a portable test case in which connect()
1978 // blocks we can enable this test to run in both rounds.
1979 abort ();
1981 struct sockaddr_un sun;
1983 tempfd = socket (AF_UNIX, SOCK_STREAM, 0);
1984 if (tempfd == -1)
1986 printf ("%s: first socket call failed\n", __FUNCTION__);
1987 exit (1);
1990 int tries = 0;
1993 if (++tries > 10)
1995 printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
1998 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-2-XXXXXX");
1999 if (mktemp (sun.sun_path) == NULL)
2001 printf ("%s: cannot generate temp file name\n", __FUNCTION__);
2002 exit (1);
2005 sun.sun_family = AF_UNIX;
2007 while (bind (tempfd, (struct sockaddr *) &sun,
2008 offsetof (struct sockaddr_un, sun_path)
2009 + strlen (sun.sun_path) + 1) != 0);
2010 tempfname = strdup (sun.sun_path);
2012 listen (tempfd, 5);
2014 tempfd2 = socket (AF_UNIX, SOCK_STREAM, 0);
2015 if (tempfd2 == -1)
2017 printf ("%s: second socket call failed\n", __FUNCTION__);
2018 exit (1);
2021 int r = pthread_barrier_wait (&b2);
2022 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
2024 printf ("%s: barrier_wait failed\n", __FUNCTION__);
2025 exit (1);
2028 if (arg != NULL)
2030 r = pthread_barrier_wait (&b2);
2031 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
2033 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
2034 exit (1);
2038 pthread_cleanup_push (cl, NULL);
2040 connect (tempfd2, (struct sockaddr *) &sun, sizeof (sun));
2042 pthread_cleanup_pop (0);
2044 printf ("%s: connect returned\n", __FUNCTION__);
2046 exit (1);
2050 static void *
2051 tf_tcdrain (void *arg)
2053 if (arg == NULL)
2054 // XXX If somebody can provide a portable test case in which tcdrain()
2055 // blocks we can enable this test to run in both rounds.
2056 abort ();
2058 int r = pthread_barrier_wait (&b2);
2059 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
2061 printf ("%s: barrier_wait failed\n", __FUNCTION__);
2062 exit (1);
2065 if (arg != NULL)
2067 r = pthread_barrier_wait (&b2);
2068 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
2070 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
2071 exit (1);
2075 pthread_cleanup_push (cl, NULL);
2077 /* Regardless of stderr being a terminal, the tcdrain call should be
2078 canceled. */
2079 tcdrain (STDERR_FILENO);
2081 pthread_cleanup_pop (0);
2083 printf ("%s: tcdrain returned\n", __FUNCTION__);
2085 exit (1);
2089 static void *
2090 tf_msgrcv (void *arg)
2092 tempmsg = msgget (IPC_PRIVATE, 0666 | IPC_CREAT);
2093 if (tempmsg == -1)
2095 printf ("%s: msgget failed: %s\n", __FUNCTION__, strerror (errno));
2096 exit (1);
2099 int r = pthread_barrier_wait (&b2);
2100 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
2102 printf ("%s: barrier_wait failed\n", __FUNCTION__);
2103 exit (1);
2106 if (arg != NULL)
2108 r = pthread_barrier_wait (&b2);
2109 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
2111 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
2112 exit (1);
2116 ssize_t s;
2118 pthread_cleanup_push (cl, NULL);
2120 struct
2122 long int type;
2123 char mem[10];
2124 } m;
2125 int randnr;
2126 /* We need a positive random number. */
2128 randnr = random () % 64000;
2129 while (randnr <= 0);
2132 errno = 0;
2133 s = msgrcv (tempmsg, (struct msgbuf *) &m, 10, randnr, 0);
2135 while (errno == EIDRM || errno == EINTR);
2137 pthread_cleanup_pop (0);
2139 printf ("%s: msgrcv returned %zd with errno = %m\n", __FUNCTION__, s);
2141 msgctl (tempmsg, IPC_RMID, NULL);
2143 exit (1);
2147 static void *
2148 tf_msgsnd (void *arg)
2150 if (arg == NULL)
2151 // XXX If somebody can provide a portable test case in which msgsnd()
2152 // blocks we can enable this test to run in both rounds.
2153 abort ();
2155 tempmsg = msgget (IPC_PRIVATE, 0666 | IPC_CREAT);
2156 if (tempmsg == -1)
2158 printf ("%s: msgget failed: %s\n", __FUNCTION__, strerror (errno));
2159 exit (1);
2162 int r = pthread_barrier_wait (&b2);
2163 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
2165 printf ("%s: barrier_wait failed\n", __FUNCTION__);
2166 exit (1);
2169 r = pthread_barrier_wait (&b2);
2170 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
2172 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
2173 exit (1);
2176 pthread_cleanup_push (cl, NULL);
2178 struct
2180 long int type;
2181 char mem[1];
2182 } m;
2183 /* We need a positive random number. */
2185 m.type = random () % 64000;
2186 while (m.type <= 0);
2187 msgsnd (tempmsg, (struct msgbuf *) &m, sizeof (m.mem), 0);
2189 pthread_cleanup_pop (0);
2191 printf ("%s: msgsnd returned\n", __FUNCTION__);
2193 msgctl (tempmsg, IPC_RMID, NULL);
2195 exit (1);
2199 static struct
2201 const char *name;
2202 void *(*tf) (void *);
2203 int nb;
2204 int only_early;
2205 } tests[] =
2207 #define ADD_TEST(name, nbar, early) { #name, tf_##name, nbar, early }
2208 ADD_TEST (read, 2, 0),
2209 ADD_TEST (readv, 2, 0),
2210 ADD_TEST (select, 2, 0),
2211 ADD_TEST (pselect, 2, 0),
2212 ADD_TEST (poll, 2, 0),
2213 ADD_TEST (ppoll, 2, 0),
2214 ADD_TEST (write, 2, 0),
2215 ADD_TEST (writev, 2, 0),
2216 ADD_TEST (sleep, 2, 0),
2217 ADD_TEST (usleep, 2, 0),
2218 ADD_TEST (nanosleep, 2, 0),
2219 ADD_TEST (wait, 2, 0),
2220 ADD_TEST (waitid, 2, 0),
2221 ADD_TEST (waitpid, 2, 0),
2222 ADD_TEST (sigpause, 2, 0),
2223 ADD_TEST (sigsuspend, 2, 0),
2224 ADD_TEST (sigwait, 2, 0),
2225 ADD_TEST (sigwaitinfo, 2, 0),
2226 ADD_TEST (sigtimedwait, 2, 0),
2227 ADD_TEST (pause, 2, 0),
2228 ADD_TEST (accept, 2, 0),
2229 ADD_TEST (send, 2, 0),
2230 ADD_TEST (recv, 2, 0),
2231 ADD_TEST (recvfrom, 2, 0),
2232 ADD_TEST (recvmsg, 2, 0),
2233 ADD_TEST (preadv, 2, 1),
2234 ADD_TEST (pwritev, 2, 1),
2235 ADD_TEST (open, 2, 1),
2236 ADD_TEST (close, 2, 1),
2237 ADD_TEST (pread, 2, 1),
2238 ADD_TEST (pwrite, 2, 1),
2239 ADD_TEST (fsync, 2, 1),
2240 ADD_TEST (fdatasync, 2, 1),
2241 ADD_TEST (msync, 2, 1),
2242 ADD_TEST (sendto, 2, 1),
2243 ADD_TEST (sendmsg, 2, 1),
2244 ADD_TEST (creat, 2, 1),
2245 ADD_TEST (connect, 2, 1),
2246 ADD_TEST (tcdrain, 2, 1),
2247 ADD_TEST (msgrcv, 2, 0),
2248 ADD_TEST (msgsnd, 2, 1),
2250 #define ntest_tf (sizeof (tests) / sizeof (tests[0]))
2253 static int
2254 do_test (void)
2256 int val;
2257 socklen_t len;
2259 if (socketpair (AF_UNIX, SOCK_STREAM, PF_UNIX, fds) != 0)
2261 perror ("socketpair");
2262 exit (1);
2265 val = 1;
2266 len = sizeof(val);
2267 setsockopt (fds[1], SOL_SOCKET, SO_SNDBUF, &val, sizeof(val));
2268 if (getsockopt (fds[1], SOL_SOCKET, SO_SNDBUF, &val, &len) < 0)
2270 perror ("getsockopt");
2271 exit (1);
2273 if (val >= WRITE_BUFFER_SIZE)
2275 puts ("minimum write buffer size too large");
2276 exit (1);
2278 setsockopt (fds[1], SOL_SOCKET, SO_SNDBUF, &val, sizeof(val));
2280 int result = 0;
2281 size_t cnt;
2282 for (cnt = 0; cnt < ntest_tf; ++cnt)
2284 if (tests[cnt].only_early)
2285 continue;
2287 if (pthread_barrier_init (&b2, NULL, tests[cnt].nb) != 0)
2289 puts ("b2 init failed");
2290 exit (1);
2293 /* Reset the counter for the cleanup handler. */
2294 cl_called = 0;
2296 pthread_t th;
2297 if (pthread_create (&th, NULL, tests[cnt].tf, NULL) != 0)
2299 printf ("create for '%s' test failed\n", tests[cnt].name);
2300 result = 1;
2301 continue;
2304 int r = pthread_barrier_wait (&b2);
2305 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
2307 printf ("%s: barrier_wait failed\n", __FUNCTION__);
2308 result = 1;
2309 continue;
2312 struct timespec ts = { .tv_sec = 0, .tv_nsec = 100000000 };
2313 while (nanosleep (&ts, &ts) != 0)
2314 continue;
2316 if (pthread_cancel (th) != 0)
2318 printf ("cancel for '%s' failed\n", tests[cnt].name);
2319 result = 1;
2320 continue;
2323 void *status;
2324 if (pthread_join (th, &status) != 0)
2326 printf ("join for '%s' failed\n", tests[cnt].name);
2327 result = 1;
2328 continue;
2330 if (status != PTHREAD_CANCELED)
2332 printf ("thread for '%s' not canceled\n", tests[cnt].name);
2333 result = 1;
2334 continue;
2337 if (pthread_barrier_destroy (&b2) != 0)
2339 puts ("barrier_destroy failed");
2340 result = 1;
2341 continue;
2344 if (cl_called == 0)
2346 printf ("cleanup handler not called for '%s'\n", tests[cnt].name);
2347 result = 1;
2348 continue;
2350 if (cl_called > 1)
2352 printf ("cleanup handler called more than once for '%s'\n",
2353 tests[cnt].name);
2354 result = 1;
2355 continue;
2358 printf ("in-time cancel test of '%s' successful\n", tests[cnt].name);
2360 if (tempfd != -1)
2362 close (tempfd);
2363 tempfd = -1;
2365 if (tempfd2 != -1)
2367 close (tempfd2);
2368 tempfd2 = -1;
2370 if (tempfname != NULL)
2372 unlink (tempfname);
2373 free (tempfname);
2374 tempfname = NULL;
2376 if (tempmsg != -1)
2378 msgctl (tempmsg, IPC_RMID, NULL);
2379 tempmsg = -1;
2383 for (cnt = 0; cnt < ntest_tf; ++cnt)
2385 if (pthread_barrier_init (&b2, NULL, tests[cnt].nb) != 0)
2387 puts ("b2 init failed");
2388 exit (1);
2391 /* Reset the counter for the cleanup handler. */
2392 cl_called = 0;
2394 pthread_t th;
2395 if (pthread_create (&th, NULL, tests[cnt].tf, (void *) 1l) != 0)
2397 printf ("create for '%s' test failed\n", tests[cnt].name);
2398 result = 1;
2399 continue;
2402 int r = pthread_barrier_wait (&b2);
2403 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
2405 printf ("%s: barrier_wait failed\n", __FUNCTION__);
2406 result = 1;
2407 continue;
2410 if (pthread_cancel (th) != 0)
2412 printf ("cancel for '%s' failed\n", tests[cnt].name);
2413 result = 1;
2414 continue;
2417 r = pthread_barrier_wait (&b2);
2418 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
2420 printf ("%s: barrier_wait failed\n", __FUNCTION__);
2421 result = 1;
2422 continue;
2425 void *status;
2426 if (pthread_join (th, &status) != 0)
2428 printf ("join for '%s' failed\n", tests[cnt].name);
2429 result = 1;
2430 continue;
2432 if (status != PTHREAD_CANCELED)
2434 printf ("thread for '%s' not canceled\n", tests[cnt].name);
2435 result = 1;
2436 continue;
2439 if (pthread_barrier_destroy (&b2) != 0)
2441 puts ("barrier_destroy failed");
2442 result = 1;
2443 continue;
2446 if (cl_called == 0)
2448 printf ("cleanup handler not called for '%s'\n", tests[cnt].name);
2449 result = 1;
2450 continue;
2452 if (cl_called > 1)
2454 printf ("cleanup handler called more than once for '%s'\n",
2455 tests[cnt].name);
2456 result = 1;
2457 continue;
2460 printf ("early cancel test of '%s' successful\n", tests[cnt].name);
2462 if (tempfd != -1)
2464 close (tempfd);
2465 tempfd = -1;
2467 if (tempfd2 != -1)
2469 close (tempfd2);
2470 tempfd2 = -1;
2472 if (tempfname != NULL)
2474 unlink (tempfname);
2475 free (tempfname);
2476 tempfname = NULL;
2478 if (tempmsg != -1)
2480 msgctl (tempmsg, IPC_RMID, NULL);
2481 tempmsg = -1;
2485 return result;
2488 #define TIMEOUT 60
2489 #define TEST_FUNCTION do_test ()
2490 #include "../test-skeleton.c"