Set GL(dl_nns) to 1 for vDSO in libc.a
[glibc.git] / nptl / tst-cancel4.c
blob37b29fc4f7adced165dd57c9e666281a0dc0d3d4
1 /* Copyright (C) 2002, 2003, 2004, 2006, 2007 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
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 #define WRITE_BUFFER_SIZE 4096
88 /* Cleanup handling test. */
89 static int cl_called;
91 static void
92 cl (void *arg)
94 ++cl_called;
99 static void *
100 tf_read (void *arg)
102 int fd;
103 int r;
105 if (arg == NULL)
106 fd = fds[0];
107 else
109 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
110 tempfd = fd = mkstemp (fname);
111 if (fd == -1)
112 printf ("%s: mkstemp failed\n", __FUNCTION__);
113 unlink (fname);
115 r = pthread_barrier_wait (&b2);
116 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
118 printf ("%s: barrier_wait failed\n", __FUNCTION__);
119 exit (1);
123 r = pthread_barrier_wait (&b2);
124 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
126 printf ("%s: barrier_wait failed\n", __FUNCTION__);
127 exit (1);
130 ssize_t s;
131 pthread_cleanup_push (cl, NULL);
133 char buf[100];
134 s = read (fd, buf, sizeof (buf));
136 pthread_cleanup_pop (0);
138 printf ("%s: read returns with %zd\n", __FUNCTION__, s);
140 exit (1);
144 static void *
145 tf_readv (void *arg)
147 int fd;
148 int r;
150 if (arg == NULL)
151 fd = fds[0];
152 else
154 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
155 tempfd = fd = mkstemp (fname);
156 if (fd == -1)
157 printf ("%s: mkstemp failed\n", __FUNCTION__);
158 unlink (fname);
160 r = pthread_barrier_wait (&b2);
161 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
163 printf ("%s: barrier_wait failed\n", __FUNCTION__);
164 exit (1);
168 r = pthread_barrier_wait (&b2);
169 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
171 printf ("%s: barrier_wait failed\n", __FUNCTION__);
172 exit (1);
175 ssize_t s;
176 pthread_cleanup_push (cl, NULL);
178 char buf[100];
179 struct iovec iov[1] = { [0] = { .iov_base = buf, .iov_len = sizeof (buf) } };
180 s = readv (fd, iov, 1);
182 pthread_cleanup_pop (0);
184 printf ("%s: readv returns with %zd\n", __FUNCTION__, s);
186 exit (1);
190 static void *
191 tf_write (void *arg)
193 int fd;
194 int r;
196 if (arg == NULL)
197 fd = fds[1];
198 else
200 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
201 tempfd = fd = mkstemp (fname);
202 if (fd == -1)
203 printf ("%s: mkstemp failed\n", __FUNCTION__);
204 unlink (fname);
206 r = pthread_barrier_wait (&b2);
207 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
209 printf ("%s: barrier_wait failed\n", __FUNCTION__);
210 exit (1);
214 r = pthread_barrier_wait (&b2);
215 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
217 printf ("%s: barrier_wait failed\n", __FUNCTION__);
218 exit (1);
221 ssize_t s;
222 pthread_cleanup_push (cl, NULL);
224 char buf[WRITE_BUFFER_SIZE];
225 memset (buf, '\0', sizeof (buf));
226 s = write (fd, buf, sizeof (buf));
228 pthread_cleanup_pop (0);
230 printf ("%s: write returns with %zd\n", __FUNCTION__, s);
232 exit (1);
236 static void *
237 tf_writev (void *arg)
239 int fd;
240 int r;
242 if (arg == NULL)
243 fd = fds[1];
244 else
246 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
247 tempfd = fd = mkstemp (fname);
248 if (fd == -1)
249 printf ("%s: mkstemp failed\n", __FUNCTION__);
250 unlink (fname);
252 r = pthread_barrier_wait (&b2);
253 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
255 printf ("%s: barrier_wait failed\n", __FUNCTION__);
256 exit (1);
260 r = pthread_barrier_wait (&b2);
261 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
263 printf ("%s: barrier_wait failed\n", __FUNCTION__);
264 exit (1);
267 ssize_t s;
268 pthread_cleanup_push (cl, NULL);
270 char buf[WRITE_BUFFER_SIZE];
271 memset (buf, '\0', sizeof (buf));
272 struct iovec iov[1] = { [0] = { .iov_base = buf, .iov_len = sizeof (buf) } };
273 s = writev (fd, iov, 1);
275 pthread_cleanup_pop (0);
277 printf ("%s: writev returns with %zd\n", __FUNCTION__, s);
279 exit (1);
283 static void *
284 tf_sleep (void *arg)
286 int r = pthread_barrier_wait (&b2);
287 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
289 printf ("%s: barrier_wait failed\n", __FUNCTION__);
290 exit (1);
293 if (arg != NULL)
295 r = pthread_barrier_wait (&b2);
296 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
298 printf ("%s: barrier_wait failed\n", __FUNCTION__);
299 exit (1);
303 pthread_cleanup_push (cl, NULL);
305 sleep (arg == NULL ? 1000000 : 0);
307 pthread_cleanup_pop (0);
309 printf ("%s: sleep returns\n", __FUNCTION__);
311 exit (1);
315 static void *
316 tf_usleep (void *arg)
318 int 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);
325 if (arg != NULL)
327 r = pthread_barrier_wait (&b2);
328 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
330 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
331 exit (1);
335 pthread_cleanup_push (cl, NULL);
337 usleep (arg == NULL ? (useconds_t) ULONG_MAX : 0);
339 pthread_cleanup_pop (0);
341 printf ("%s: usleep returns\n", __FUNCTION__);
343 exit (1);
347 static void *
348 tf_nanosleep (void *arg)
350 int r = pthread_barrier_wait (&b2);
351 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
353 printf ("%s: barrier_wait failed\n", __FUNCTION__);
354 exit (1);
357 if (arg != NULL)
359 r = pthread_barrier_wait (&b2);
360 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
362 printf ("%s: barrier_wait failed\n", __FUNCTION__);
363 exit (1);
367 pthread_cleanup_push (cl, NULL);
369 struct timespec ts = { .tv_sec = arg == NULL ? 10000000 : 0, .tv_nsec = 0 };
370 TEMP_FAILURE_RETRY (nanosleep (&ts, &ts));
372 pthread_cleanup_pop (0);
374 printf ("%s: nanosleep returns\n", __FUNCTION__);
376 exit (1);
380 static void *
381 tf_select (void *arg)
383 int fd;
384 int r;
386 if (arg == NULL)
387 fd = fds[0];
388 else
390 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
391 tempfd = fd = mkstemp (fname);
392 if (fd == -1)
393 printf ("%s: mkstemp failed\n", __FUNCTION__);
394 unlink (fname);
396 r = pthread_barrier_wait (&b2);
397 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
399 printf ("%s: barrier_wait failed\n", __FUNCTION__);
400 exit (1);
404 r = pthread_barrier_wait (&b2);
405 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
407 printf ("%s: barrier_wait failed\n", __FUNCTION__);
408 exit (1);
411 fd_set rfs;
412 FD_ZERO (&rfs);
413 FD_SET (fd, &rfs);
415 int s;
416 pthread_cleanup_push (cl, NULL);
418 s = select (fd + 1, &rfs, NULL, NULL, NULL);
420 pthread_cleanup_pop (0);
422 printf ("%s: select returns with %d (%s)\n", __FUNCTION__, s,
423 strerror (errno));
425 exit (1);
429 static void *
430 tf_pselect (void *arg)
432 int fd;
433 int r;
435 if (arg == NULL)
436 fd = fds[0];
437 else
439 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
440 tempfd = fd = mkstemp (fname);
441 if (fd == -1)
442 printf ("%s: mkstemp failed\n", __FUNCTION__);
443 unlink (fname);
445 r = pthread_barrier_wait (&b2);
446 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
448 printf ("%s: barrier_wait failed\n", __FUNCTION__);
449 exit (1);
453 r = pthread_barrier_wait (&b2);
454 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
456 printf ("%s: barrier_wait failed\n", __FUNCTION__);
457 exit (1);
460 fd_set rfs;
461 FD_ZERO (&rfs);
462 FD_SET (fd, &rfs);
464 int s;
465 pthread_cleanup_push (cl, NULL);
467 s = pselect (fd + 1, &rfs, NULL, NULL, NULL, NULL);
469 pthread_cleanup_pop (0);
471 printf ("%s: pselect returns with %d (%s)\n", __FUNCTION__, s,
472 strerror (errno));
474 exit (1);
478 static void *
479 tf_poll (void *arg)
481 int fd;
482 int r;
484 if (arg == NULL)
485 fd = fds[0];
486 else
488 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
489 tempfd = fd = mkstemp (fname);
490 if (fd == -1)
491 printf ("%s: mkstemp failed\n", __FUNCTION__);
492 unlink (fname);
494 r = pthread_barrier_wait (&b2);
495 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
497 printf ("%s: barrier_wait failed\n", __FUNCTION__);
498 exit (1);
502 r = pthread_barrier_wait (&b2);
503 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
505 printf ("%s: barrier_wait failed\n", __FUNCTION__);
506 exit (1);
509 struct pollfd rfs[1] = { [0] = { .fd = fd, .events = POLLIN } };
511 int s;
512 pthread_cleanup_push (cl, NULL);
514 s = poll (rfs, 1, -1);
516 pthread_cleanup_pop (0);
518 printf ("%s: poll returns with %d (%s)\n", __FUNCTION__, s,
519 strerror (errno));
521 exit (1);
525 static void *
526 tf_ppoll (void *arg)
528 int fd;
529 int r;
531 if (arg == NULL)
532 fd = fds[0];
533 else
535 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
536 tempfd = fd = mkstemp (fname);
537 if (fd == -1)
538 printf ("%s: mkstemp failed\n", __FUNCTION__);
539 unlink (fname);
541 r = pthread_barrier_wait (&b2);
542 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
544 printf ("%s: barrier_wait failed\n", __FUNCTION__);
545 exit (1);
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);
556 struct pollfd rfs[1] = { [0] = { .fd = fd, .events = POLLIN } };
558 int s;
559 pthread_cleanup_push (cl, NULL);
561 s = ppoll (rfs, 1, NULL, NULL);
563 pthread_cleanup_pop (0);
565 printf ("%s: ppoll returns with %d (%s)\n", __FUNCTION__, s,
566 strerror (errno));
568 exit (1);
572 static void *
573 tf_wait (void *arg)
575 pid_t pid = fork ();
576 if (pid == -1)
578 puts ("fork failed");
579 exit (1);
582 if (pid == 0)
584 /* Make the program disappear after a while. */
585 if (arg == NULL)
586 sleep (10);
587 exit (0);
590 int r;
591 if (arg != NULL)
593 struct timespec ts = { .tv_sec = 0, .tv_nsec = 100000000 };
594 while (nanosleep (&ts, &ts) != 0)
595 continue;
597 r = pthread_barrier_wait (&b2);
598 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
600 printf ("%s: barrier_wait failed\n", __FUNCTION__);
601 exit (1);
605 r = pthread_barrier_wait (&b2);
606 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
608 printf ("%s: barrier_wait failed\n", __FUNCTION__);
609 exit (1);
612 int s;
613 pthread_cleanup_push (cl, NULL);
615 s = wait (NULL);
617 pthread_cleanup_pop (0);
619 printf ("%s: wait returns with %d (%s)\n", __FUNCTION__, s,
620 strerror (errno));
622 exit (1);
626 static void *
627 tf_waitpid (void *arg)
630 pid_t pid = fork ();
631 if (pid == -1)
633 puts ("fork failed");
634 exit (1);
637 if (pid == 0)
639 /* Make the program disappear after a while. */
640 if (arg == NULL)
641 sleep (10);
642 exit (0);
645 int r;
646 if (arg != NULL)
648 struct timespec ts = { .tv_sec = 0, .tv_nsec = 100000000 };
649 while (nanosleep (&ts, &ts) != 0)
650 continue;
652 r = pthread_barrier_wait (&b2);
653 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
655 printf ("%s: barrier_wait failed\n", __FUNCTION__);
656 exit (1);
660 r = pthread_barrier_wait (&b2);
661 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
663 printf ("%s: barrier_wait failed\n", __FUNCTION__);
664 exit (1);
667 int s;
668 pthread_cleanup_push (cl, NULL);
670 s = waitpid (-1, NULL, 0);
672 pthread_cleanup_pop (0);
674 printf ("%s: waitpid returns with %d (%s)\n", __FUNCTION__, s,
675 strerror (errno));
677 exit (1);
681 static void *
682 tf_waitid (void *arg)
684 pid_t pid = fork ();
685 if (pid == -1)
687 puts ("fork failed");
688 exit (1);
691 if (pid == 0)
693 /* Make the program disappear after a while. */
694 if (arg == NULL)
695 sleep (10);
696 exit (0);
699 int r;
700 if (arg != NULL)
702 struct timespec ts = { .tv_sec = 0, .tv_nsec = 100000000 };
703 while (nanosleep (&ts, &ts) != 0)
704 continue;
706 r = pthread_barrier_wait (&b2);
707 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
709 printf ("%s: barrier_wait failed\n", __FUNCTION__);
710 exit (1);
714 r = pthread_barrier_wait (&b2);
715 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
717 printf ("%s: barrier_wait failed\n", __FUNCTION__);
718 exit (1);
721 int s;
722 pthread_cleanup_push (cl, NULL);
724 #ifndef WEXITED
725 # define WEXITED 0
726 #endif
727 siginfo_t si;
728 s = waitid (P_PID, pid, &si, WEXITED);
730 pthread_cleanup_pop (0);
732 printf ("%s: waitid returns with %d (%s)\n", __FUNCTION__, s,
733 strerror (errno));
735 exit (1);
739 static void *
740 tf_sigpause (void *arg)
742 int r = pthread_barrier_wait (&b2);
743 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
745 printf ("%s: barrier_wait failed\n", __FUNCTION__);
746 exit (1);
749 if (arg != NULL)
751 r = pthread_barrier_wait (&b2);
752 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
754 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
755 exit (1);
759 pthread_cleanup_push (cl, NULL);
761 /* Just for fun block the cancellation signal. We need to use
762 __xpg_sigpause since otherwise we will get the BSD version. */
763 __xpg_sigpause (SIGCANCEL);
765 pthread_cleanup_pop (0);
767 printf ("%s: sigpause returned\n", __FUNCTION__);
769 exit (1);
773 static void *
774 tf_sigsuspend (void *arg)
776 int r = pthread_barrier_wait (&b2);
777 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
779 printf ("%s: barrier_wait failed\n", __FUNCTION__);
780 exit (1);
783 if (arg != NULL)
785 r = pthread_barrier_wait (&b2);
786 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
788 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
789 exit (1);
793 pthread_cleanup_push (cl, NULL);
795 /* Just for fun block all signals. */
796 sigset_t mask;
797 sigfillset (&mask);
798 sigsuspend (&mask);
800 pthread_cleanup_pop (0);
802 printf ("%s: sigsuspend returned\n", __FUNCTION__);
804 exit (1);
808 static void *
809 tf_sigwait (void *arg)
811 int r = pthread_barrier_wait (&b2);
812 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
814 printf ("%s: barrier_wait failed\n", __FUNCTION__);
815 exit (1);
818 if (arg != NULL)
820 r = pthread_barrier_wait (&b2);
821 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
823 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
824 exit (1);
828 /* Block SIGUSR1. */
829 sigset_t mask;
830 sigemptyset (&mask);
831 sigaddset (&mask, SIGUSR1);
832 if (pthread_sigmask (SIG_BLOCK, &mask, NULL) != 0)
834 printf ("%s: pthread_sigmask failed\n", __FUNCTION__);
835 exit (1);
838 int sig;
839 pthread_cleanup_push (cl, NULL);
841 /* Wait for SIGUSR1. */
842 sigwait (&mask, &sig);
844 pthread_cleanup_pop (0);
846 printf ("%s: sigwait returned with signal %d\n", __FUNCTION__, sig);
848 exit (1);
852 static void *
853 tf_sigwaitinfo (void *arg)
855 int r = pthread_barrier_wait (&b2);
856 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
858 printf ("%s: barrier_wait failed\n", __FUNCTION__);
859 exit (1);
862 if (arg != NULL)
864 r = pthread_barrier_wait (&b2);
865 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
867 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
868 exit (1);
872 /* Block SIGUSR1. */
873 sigset_t mask;
874 sigemptyset (&mask);
875 sigaddset (&mask, SIGUSR1);
876 if (pthread_sigmask (SIG_BLOCK, &mask, NULL) != 0)
878 printf ("%s: pthread_sigmask failed\n", __FUNCTION__);
879 exit (1);
882 siginfo_t info;
883 pthread_cleanup_push (cl, NULL);
885 /* Wait for SIGUSR1. */
886 sigwaitinfo (&mask, &info);
888 pthread_cleanup_pop (0);
890 printf ("%s: sigwaitinfo returned with signal %d\n", __FUNCTION__,
891 info.si_signo);
893 exit (1);
897 static void *
898 tf_sigtimedwait (void *arg)
900 int r = pthread_barrier_wait (&b2);
901 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
903 printf ("%s: barrier_wait failed\n", __FUNCTION__);
904 exit (1);
907 if (arg != NULL)
909 r = pthread_barrier_wait (&b2);
910 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
912 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
913 exit (1);
917 /* Block SIGUSR1. */
918 sigset_t mask;
919 sigemptyset (&mask);
920 sigaddset (&mask, SIGUSR1);
921 if (pthread_sigmask (SIG_BLOCK, &mask, NULL) != 0)
923 printf ("%s: pthread_sigmask failed\n", __FUNCTION__);
924 exit (1);
927 /* Wait for SIGUSR1. */
928 siginfo_t info;
929 struct timespec ts = { .tv_sec = 60, .tv_nsec = 0 };
930 pthread_cleanup_push (cl, NULL);
932 sigtimedwait (&mask, &info, &ts);
934 pthread_cleanup_pop (0);
936 printf ("%s: sigtimedwait returned with signal %d\n", __FUNCTION__,
937 info.si_signo);
939 exit (1);
943 static void *
944 tf_pause (void *arg)
946 int r = pthread_barrier_wait (&b2);
947 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
949 printf ("%s: barrier_wait failed\n", __FUNCTION__);
950 exit (1);
953 if (arg != NULL)
955 r = pthread_barrier_wait (&b2);
956 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
958 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
959 exit (1);
963 pthread_cleanup_push (cl, NULL);
965 pause ();
967 pthread_cleanup_pop (0);
969 printf ("%s: pause returned\n", __FUNCTION__);
971 exit (1);
975 static void *
976 tf_accept (void *arg)
978 struct sockaddr_un sun;
979 /* To test a non-blocking accept call we make the call file by using
980 a datagrame socket. */
981 int pf = arg == NULL ? SOCK_STREAM : SOCK_DGRAM;
983 tempfd = socket (AF_UNIX, pf, 0);
984 if (tempfd == -1)
986 printf ("%s: socket call failed\n", __FUNCTION__);
987 exit (1);
990 int tries = 0;
993 if (++tries > 10)
995 printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
998 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-1-XXXXXX");
999 if (mktemp (sun.sun_path) == NULL)
1001 printf ("%s: cannot generate temp file name\n", __FUNCTION__);
1002 exit (1);
1005 sun.sun_family = AF_UNIX;
1007 while (bind (tempfd, (struct sockaddr *) &sun,
1008 offsetof (struct sockaddr_un, sun_path)
1009 + strlen (sun.sun_path) + 1) != 0);
1011 unlink (sun.sun_path);
1013 listen (tempfd, 5);
1015 socklen_t len = sizeof (sun);
1017 int r = pthread_barrier_wait (&b2);
1018 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1020 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1021 exit (1);
1024 if (arg != NULL)
1026 r = pthread_barrier_wait (&b2);
1027 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1029 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1030 exit (1);
1034 pthread_cleanup_push (cl, NULL);
1036 accept (tempfd, (struct sockaddr *) &sun, &len);
1038 pthread_cleanup_pop (0);
1040 printf ("%s: accept returned\n", __FUNCTION__);
1042 exit (1);
1046 static void *
1047 tf_send (void *arg)
1049 struct sockaddr_un sun;
1051 tempfd = socket (AF_UNIX, SOCK_STREAM, 0);
1052 if (tempfd == -1)
1054 printf ("%s: first socket call failed\n", __FUNCTION__);
1055 exit (1);
1058 int tries = 0;
1061 if (++tries > 10)
1063 printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
1066 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-2-XXXXXX");
1067 if (mktemp (sun.sun_path) == NULL)
1069 printf ("%s: cannot generate temp file name\n", __FUNCTION__);
1070 exit (1);
1073 sun.sun_family = AF_UNIX;
1075 while (bind (tempfd, (struct sockaddr *) &sun,
1076 offsetof (struct sockaddr_un, sun_path)
1077 + strlen (sun.sun_path) + 1) != 0);
1079 listen (tempfd, 5);
1081 tempfd2 = socket (AF_UNIX, SOCK_STREAM, 0);
1082 if (tempfd2 == -1)
1084 printf ("%s: second socket call failed\n", __FUNCTION__);
1085 exit (1);
1088 if (connect (tempfd2, (struct sockaddr *) &sun, sizeof (sun)) != 0)
1090 printf ("%s: connect failed\n", __FUNCTION__);
1091 exit(1);
1094 unlink (sun.sun_path);
1096 int r = pthread_barrier_wait (&b2);
1097 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1099 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1100 exit (1);
1103 if (arg != NULL)
1105 r = pthread_barrier_wait (&b2);
1106 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1108 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1109 exit (1);
1113 pthread_cleanup_push (cl, NULL);
1115 /* Very large block, so that the send call blocks. */
1116 char mem[700000];
1118 send (tempfd2, mem, arg == NULL ? sizeof (mem) : 1, 0);
1120 pthread_cleanup_pop (0);
1122 printf ("%s: send returned\n", __FUNCTION__);
1124 exit (1);
1128 static void *
1129 tf_recv (void *arg)
1131 struct sockaddr_un sun;
1133 tempfd = socket (AF_UNIX, SOCK_STREAM, 0);
1134 if (tempfd == -1)
1136 printf ("%s: first socket call failed\n", __FUNCTION__);
1137 exit (1);
1140 int tries = 0;
1143 if (++tries > 10)
1145 printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
1148 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-3-XXXXXX");
1149 if (mktemp (sun.sun_path) == NULL)
1151 printf ("%s: cannot generate temp file name\n", __FUNCTION__);
1152 exit (1);
1155 sun.sun_family = AF_UNIX;
1157 while (bind (tempfd, (struct sockaddr *) &sun,
1158 offsetof (struct sockaddr_un, sun_path)
1159 + strlen (sun.sun_path) + 1) != 0);
1161 listen (tempfd, 5);
1163 tempfd2 = socket (AF_UNIX, SOCK_STREAM, 0);
1164 if (tempfd2 == -1)
1166 printf ("%s: second socket call failed\n", __FUNCTION__);
1167 exit (1);
1170 if (connect (tempfd2, (struct sockaddr *) &sun, sizeof (sun)) != 0)
1172 printf ("%s: connect failed\n", __FUNCTION__);
1173 exit(1);
1176 unlink (sun.sun_path);
1178 int r = pthread_barrier_wait (&b2);
1179 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1181 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1182 exit (1);
1185 if (arg != NULL)
1187 r = pthread_barrier_wait (&b2);
1188 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1190 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1191 exit (1);
1195 pthread_cleanup_push (cl, NULL);
1197 char mem[70];
1199 recv (tempfd2, mem, arg == NULL ? sizeof (mem) : 0, 0);
1201 pthread_cleanup_pop (0);
1203 printf ("%s: recv returned\n", __FUNCTION__);
1205 exit (1);
1209 static void *
1210 tf_recvfrom (void *arg)
1212 struct sockaddr_un sun;
1214 tempfd = socket (AF_UNIX, SOCK_DGRAM, 0);
1215 if (tempfd == -1)
1217 printf ("%s: first socket call failed\n", __FUNCTION__);
1218 exit (1);
1221 int tries = 0;
1224 if (++tries > 10)
1226 printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
1229 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-4-XXXXXX");
1230 if (mktemp (sun.sun_path) == NULL)
1232 printf ("%s: cannot generate temp file name\n", __FUNCTION__);
1233 exit (1);
1236 sun.sun_family = AF_UNIX;
1238 while (bind (tempfd, (struct sockaddr *) &sun,
1239 offsetof (struct sockaddr_un, sun_path)
1240 + strlen (sun.sun_path) + 1) != 0);
1242 tempfname = strdup (sun.sun_path);
1244 tempfd2 = socket (AF_UNIX, SOCK_DGRAM, 0);
1245 if (tempfd2 == -1)
1247 printf ("%s: second socket call failed\n", __FUNCTION__);
1248 exit (1);
1251 int r = pthread_barrier_wait (&b2);
1252 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1254 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1255 exit (1);
1258 if (arg != NULL)
1260 r = pthread_barrier_wait (&b2);
1261 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1263 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1264 exit (1);
1268 pthread_cleanup_push (cl, NULL);
1270 char mem[70];
1271 socklen_t len = sizeof (sun);
1273 recvfrom (tempfd2, mem, arg == NULL ? sizeof (mem) : 0, 0,
1274 (struct sockaddr *) &sun, &len);
1276 pthread_cleanup_pop (0);
1278 printf ("%s: recvfrom returned\n", __FUNCTION__);
1280 exit (1);
1284 static void *
1285 tf_recvmsg (void *arg)
1287 struct sockaddr_un sun;
1289 tempfd = socket (AF_UNIX, SOCK_DGRAM, 0);
1290 if (tempfd == -1)
1292 printf ("%s: first socket call failed\n", __FUNCTION__);
1293 exit (1);
1296 int tries = 0;
1299 if (++tries > 10)
1301 printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
1304 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-5-XXXXXX");
1305 if (mktemp (sun.sun_path) == NULL)
1307 printf ("%s: cannot generate temp file name\n", __FUNCTION__);
1308 exit (1);
1311 sun.sun_family = AF_UNIX;
1313 while (bind (tempfd, (struct sockaddr *) &sun,
1314 offsetof (struct sockaddr_un, sun_path)
1315 + strlen (sun.sun_path) + 1) != 0);
1317 tempfname = strdup (sun.sun_path);
1319 tempfd2 = socket (AF_UNIX, SOCK_DGRAM, 0);
1320 if (tempfd2 == -1)
1322 printf ("%s: second socket call failed\n", __FUNCTION__);
1323 exit (1);
1326 int r = pthread_barrier_wait (&b2);
1327 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1329 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1330 exit (1);
1333 if (arg != NULL)
1335 r = pthread_barrier_wait (&b2);
1336 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1338 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1339 exit (1);
1343 pthread_cleanup_push (cl, NULL);
1345 char mem[70];
1346 struct iovec iov[1];
1347 iov[0].iov_base = mem;
1348 iov[0].iov_len = arg == NULL ? sizeof (mem) : 0;
1350 struct msghdr m;
1351 m.msg_name = &sun;
1352 m.msg_namelen = sizeof (sun);
1353 m.msg_iov = iov;
1354 m.msg_iovlen = 1;
1355 m.msg_control = NULL;
1356 m.msg_controllen = 0;
1358 recvmsg (tempfd2, &m, 0);
1360 pthread_cleanup_pop (0);
1362 printf ("%s: recvmsg returned\n", __FUNCTION__);
1364 exit (1);
1368 static void *
1369 tf_open (void *arg)
1371 if (arg == NULL)
1372 // XXX If somebody can provide a portable test case in which open()
1373 // blocks we can enable this test to run in both rounds.
1374 abort ();
1376 int r = pthread_barrier_wait (&b2);
1377 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1379 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1380 exit (1);
1383 r = pthread_barrier_wait (&b2);
1384 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1386 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1387 exit (1);
1390 pthread_cleanup_push (cl, NULL);
1392 open ("Makefile", O_RDONLY);
1394 pthread_cleanup_pop (0);
1396 printf ("%s: open returned\n", __FUNCTION__);
1398 exit (1);
1402 static void *
1403 tf_close (void *arg)
1405 if (arg == NULL)
1406 // XXX If somebody can provide a portable test case in which close()
1407 // blocks we can enable this test to run in both rounds.
1408 abort ();
1410 char fname[] = "/tmp/tst-cancel-fd-XXXXXX";
1411 tempfd = mkstemp (fname);
1412 if (tempfd == -1)
1414 printf ("%s: mkstemp failed\n", __FUNCTION__);
1415 exit (1);
1417 unlink (fname);
1419 int r = pthread_barrier_wait (&b2);
1420 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1422 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1423 exit (1);
1426 r = pthread_barrier_wait (&b2);
1427 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1429 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1430 exit (1);
1433 pthread_cleanup_push (cl, NULL);
1435 close (tempfd);
1437 pthread_cleanup_pop (0);
1439 printf ("%s: close returned\n", __FUNCTION__);
1441 exit (1);
1445 static void *
1446 tf_pread (void *arg)
1448 if (arg == NULL)
1449 // XXX If somebody can provide a portable test case in which pread()
1450 // blocks we can enable this test to run in both rounds.
1451 abort ();
1453 tempfd = open ("Makefile", O_RDONLY);
1454 if (tempfd == -1)
1456 printf ("%s: cannot open Makefile\n", __FUNCTION__);
1457 exit (1);
1460 int r = pthread_barrier_wait (&b2);
1461 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1463 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1464 exit (1);
1467 r = pthread_barrier_wait (&b2);
1468 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1470 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1471 exit (1);
1474 pthread_cleanup_push (cl, NULL);
1476 char mem[10];
1477 pread (tempfd, mem, sizeof (mem), 0);
1479 pthread_cleanup_pop (0);
1481 printf ("%s: pread returned\n", __FUNCTION__);
1483 exit (1);
1487 static void *
1488 tf_pwrite (void *arg)
1490 if (arg == NULL)
1491 // XXX If somebody can provide a portable test case in which pwrite()
1492 // blocks we can enable this test to run in both rounds.
1493 abort ();
1495 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
1496 tempfd = mkstemp (fname);
1497 if (tempfd == -1)
1499 printf ("%s: mkstemp failed\n", __FUNCTION__);
1500 exit (1);
1502 unlink (fname);
1504 int r = pthread_barrier_wait (&b2);
1505 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1507 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1508 exit (1);
1511 r = pthread_barrier_wait (&b2);
1512 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1514 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1515 exit (1);
1518 pthread_cleanup_push (cl, NULL);
1520 char mem[10];
1521 pwrite (tempfd, mem, sizeof (mem), 0);
1523 pthread_cleanup_pop (0);
1525 printf ("%s: pwrite returned\n", __FUNCTION__);
1527 exit (1);
1531 static void *
1532 tf_fsync (void *arg)
1534 if (arg == NULL)
1535 // XXX If somebody can provide a portable test case in which fsync()
1536 // blocks we can enable this test to run in both rounds.
1537 abort ();
1539 tempfd = open ("Makefile", O_RDONLY);
1540 if (tempfd == -1)
1542 printf ("%s: cannot open Makefile\n", __FUNCTION__);
1543 exit (1);
1546 int r = pthread_barrier_wait (&b2);
1547 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1549 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1550 exit (1);
1553 r = pthread_barrier_wait (&b2);
1554 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1556 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1557 exit (1);
1560 pthread_cleanup_push (cl, NULL);
1562 fsync (tempfd);
1564 pthread_cleanup_pop (0);
1566 printf ("%s: fsync returned\n", __FUNCTION__);
1568 exit (1);
1572 static void *
1573 tf_fdatasync (void *arg)
1575 if (arg == NULL)
1576 // XXX If somebody can provide a portable test case in which fdatasync()
1577 // blocks we can enable this test to run in both rounds.
1578 abort ();
1580 tempfd = open ("Makefile", O_RDONLY);
1581 if (tempfd == -1)
1583 printf ("%s: cannot open Makefile\n", __FUNCTION__);
1584 exit (1);
1587 int r = pthread_barrier_wait (&b2);
1588 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1590 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1591 exit (1);
1594 r = pthread_barrier_wait (&b2);
1595 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1597 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1598 exit (1);
1601 pthread_cleanup_push (cl, NULL);
1603 fdatasync (tempfd);
1605 pthread_cleanup_pop (0);
1607 printf ("%s: fdatasync returned\n", __FUNCTION__);
1609 exit (1);
1613 static void *
1614 tf_msync (void *arg)
1616 if (arg == NULL)
1617 // XXX If somebody can provide a portable test case in which msync()
1618 // blocks we can enable this test to run in both rounds.
1619 abort ();
1621 tempfd = open ("Makefile", O_RDONLY);
1622 if (tempfd == -1)
1624 printf ("%s: cannot open Makefile\n", __FUNCTION__);
1625 exit (1);
1627 void *p = mmap (NULL, 10, PROT_READ, MAP_SHARED, tempfd, 0);
1628 if (p == MAP_FAILED)
1630 printf ("%s: mmap failed\n", __FUNCTION__);
1631 exit (1);
1634 int r = pthread_barrier_wait (&b2);
1635 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1637 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1638 exit (1);
1641 r = pthread_barrier_wait (&b2);
1642 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1644 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1645 exit (1);
1648 pthread_cleanup_push (cl, NULL);
1650 msync (p, 10, 0);
1652 pthread_cleanup_pop (0);
1654 printf ("%s: msync returned\n", __FUNCTION__);
1656 exit (1);
1660 static void *
1661 tf_sendto (void *arg)
1663 if (arg == NULL)
1664 // XXX If somebody can provide a portable test case in which sendto()
1665 // blocks we can enable this test to run in both rounds.
1666 abort ();
1668 struct sockaddr_un sun;
1670 tempfd = socket (AF_UNIX, SOCK_DGRAM, 0);
1671 if (tempfd == -1)
1673 printf ("%s: first socket call failed\n", __FUNCTION__);
1674 exit (1);
1677 int tries = 0;
1680 if (++tries > 10)
1682 printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
1685 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-6-XXXXXX");
1686 if (mktemp (sun.sun_path) == NULL)
1688 printf ("%s: cannot generate temp file name\n", __FUNCTION__);
1689 exit (1);
1692 sun.sun_family = AF_UNIX;
1694 while (bind (tempfd, (struct sockaddr *) &sun,
1695 offsetof (struct sockaddr_un, sun_path)
1696 + strlen (sun.sun_path) + 1) != 0);
1697 tempfname = strdup (sun.sun_path);
1699 tempfd2 = socket (AF_UNIX, SOCK_DGRAM, 0);
1700 if (tempfd2 == -1)
1702 printf ("%s: second socket call failed\n", __FUNCTION__);
1703 exit (1);
1706 int r = pthread_barrier_wait (&b2);
1707 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1709 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1710 exit (1);
1713 r = pthread_barrier_wait (&b2);
1714 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1716 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1717 exit (1);
1720 pthread_cleanup_push (cl, NULL);
1722 char mem[1];
1724 sendto (tempfd2, mem, arg == NULL ? sizeof (mem) : 1, 0,
1725 (struct sockaddr *) &sun,
1726 offsetof (struct sockaddr_un, sun_path) + strlen (sun.sun_path) + 1);
1728 pthread_cleanup_pop (0);
1730 printf ("%s: sendto returned\n", __FUNCTION__);
1732 exit (1);
1736 static void *
1737 tf_sendmsg (void *arg)
1739 if (arg == NULL)
1740 // XXX If somebody can provide a portable test case in which sendmsg()
1741 // blocks we can enable this test to run in both rounds.
1742 abort ();
1744 struct sockaddr_un sun;
1746 tempfd = socket (AF_UNIX, SOCK_DGRAM, 0);
1747 if (tempfd == -1)
1749 printf ("%s: first socket call failed\n", __FUNCTION__);
1750 exit (1);
1753 int tries = 0;
1756 if (++tries > 10)
1758 printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
1761 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-7-XXXXXX");
1762 if (mktemp (sun.sun_path) == NULL)
1764 printf ("%s: cannot generate temp file name\n", __FUNCTION__);
1765 exit (1);
1768 sun.sun_family = AF_UNIX;
1770 while (bind (tempfd, (struct sockaddr *) &sun,
1771 offsetof (struct sockaddr_un, sun_path)
1772 + strlen (sun.sun_path) + 1) != 0);
1773 tempfname = strdup (sun.sun_path);
1775 tempfd2 = socket (AF_UNIX, SOCK_DGRAM, 0);
1776 if (tempfd2 == -1)
1778 printf ("%s: second socket call failed\n", __FUNCTION__);
1779 exit (1);
1782 int r = pthread_barrier_wait (&b2);
1783 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1785 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1786 exit (1);
1789 r = pthread_barrier_wait (&b2);
1790 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1792 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1793 exit (1);
1796 pthread_cleanup_push (cl, NULL);
1798 char mem[1];
1799 struct iovec iov[1];
1800 iov[0].iov_base = mem;
1801 iov[0].iov_len = 1;
1803 struct msghdr m;
1804 m.msg_name = &sun;
1805 m.msg_namelen = (offsetof (struct sockaddr_un, sun_path)
1806 + strlen (sun.sun_path) + 1);
1807 m.msg_iov = iov;
1808 m.msg_iovlen = 1;
1809 m.msg_control = NULL;
1810 m.msg_controllen = 0;
1812 sendmsg (tempfd2, &m, 0);
1814 pthread_cleanup_pop (0);
1816 printf ("%s: sendmsg returned\n", __FUNCTION__);
1818 exit (1);
1822 static void *
1823 tf_creat (void *arg)
1825 if (arg == NULL)
1826 // XXX If somebody can provide a portable test case in which sendmsg()
1827 // blocks we can enable this test to run in both rounds.
1828 abort ();
1830 int r = pthread_barrier_wait (&b2);
1831 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1833 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1834 exit (1);
1837 r = pthread_barrier_wait (&b2);
1838 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1840 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1841 exit (1);
1844 pthread_cleanup_push (cl, NULL);
1846 creat ("tmp/tst-cancel-4-should-not-exist", 0666);
1848 pthread_cleanup_pop (0);
1850 printf ("%s: creat returned\n", __FUNCTION__);
1852 exit (1);
1856 static void *
1857 tf_connect (void *arg)
1859 if (arg == NULL)
1860 // XXX If somebody can provide a portable test case in which connect()
1861 // blocks we can enable this test to run in both rounds.
1862 abort ();
1864 struct sockaddr_un sun;
1866 tempfd = socket (AF_UNIX, SOCK_STREAM, 0);
1867 if (tempfd == -1)
1869 printf ("%s: first socket call failed\n", __FUNCTION__);
1870 exit (1);
1873 int tries = 0;
1876 if (++tries > 10)
1878 printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
1881 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-2-XXXXXX");
1882 if (mktemp (sun.sun_path) == NULL)
1884 printf ("%s: cannot generate temp file name\n", __FUNCTION__);
1885 exit (1);
1888 sun.sun_family = AF_UNIX;
1890 while (bind (tempfd, (struct sockaddr *) &sun,
1891 offsetof (struct sockaddr_un, sun_path)
1892 + strlen (sun.sun_path) + 1) != 0);
1893 tempfname = strdup (sun.sun_path);
1895 listen (tempfd, 5);
1897 tempfd2 = socket (AF_UNIX, SOCK_STREAM, 0);
1898 if (tempfd2 == -1)
1900 printf ("%s: second socket call failed\n", __FUNCTION__);
1901 exit (1);
1904 int r = pthread_barrier_wait (&b2);
1905 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1907 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1908 exit (1);
1911 if (arg != NULL)
1913 r = pthread_barrier_wait (&b2);
1914 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1916 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1917 exit (1);
1921 pthread_cleanup_push (cl, NULL);
1923 connect (tempfd2, (struct sockaddr *) &sun, sizeof (sun));
1925 pthread_cleanup_pop (0);
1927 printf ("%s: connect returned\n", __FUNCTION__);
1929 exit (1);
1933 static void *
1934 tf_tcdrain (void *arg)
1936 if (arg == NULL)
1937 // XXX If somebody can provide a portable test case in which tcdrain()
1938 // blocks we can enable this test to run in both rounds.
1939 abort ();
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 if (arg != NULL)
1950 r = pthread_barrier_wait (&b2);
1951 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1953 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1954 exit (1);
1958 pthread_cleanup_push (cl, NULL);
1960 /* Regardless of stderr being a terminal, the tcdrain call should be
1961 canceled. */
1962 tcdrain (STDERR_FILENO);
1964 pthread_cleanup_pop (0);
1966 printf ("%s: tcdrain returned\n", __FUNCTION__);
1968 exit (1);
1972 static void *
1973 tf_msgrcv (void *arg)
1975 tempmsg = msgget (IPC_PRIVATE, 0666 | IPC_CREAT);
1976 if (tempmsg == -1)
1978 printf ("%s: msgget failed: %s\n", __FUNCTION__, strerror (errno));
1979 exit (1);
1982 int r = pthread_barrier_wait (&b2);
1983 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1985 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1986 exit (1);
1989 if (arg != NULL)
1991 r = pthread_barrier_wait (&b2);
1992 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1994 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1995 exit (1);
1999 ssize_t s;
2001 pthread_cleanup_push (cl, NULL);
2003 struct
2005 long int type;
2006 char mem[10];
2007 } m;
2008 int randnr;
2009 /* We need a positive random number. */
2011 randnr = random () % 64000;
2012 while (randnr <= 0);
2015 errno = 0;
2016 s = msgrcv (tempmsg, (struct msgbuf *) &m, 10, randnr, 0);
2018 while (errno == EIDRM || errno == EINTR);
2020 pthread_cleanup_pop (0);
2022 printf ("%s: msgrcv returned %zd with errno = %m\n", __FUNCTION__, s);
2024 msgctl (tempmsg, IPC_RMID, NULL);
2026 exit (1);
2030 static void *
2031 tf_msgsnd (void *arg)
2033 if (arg == NULL)
2034 // XXX If somebody can provide a portable test case in which msgsnd()
2035 // blocks we can enable this test to run in both rounds.
2036 abort ();
2038 tempmsg = msgget (IPC_PRIVATE, 0666 | IPC_CREAT);
2039 if (tempmsg == -1)
2041 printf ("%s: msgget failed: %s\n", __FUNCTION__, strerror (errno));
2042 exit (1);
2045 int r = pthread_barrier_wait (&b2);
2046 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
2048 printf ("%s: barrier_wait failed\n", __FUNCTION__);
2049 exit (1);
2052 r = pthread_barrier_wait (&b2);
2053 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
2055 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
2056 exit (1);
2059 pthread_cleanup_push (cl, NULL);
2061 struct
2063 long int type;
2064 char mem[1];
2065 } m;
2066 /* We need a positive random number. */
2068 m.type = random () % 64000;
2069 while (m.type <= 0);
2070 msgsnd (tempmsg, (struct msgbuf *) &m, sizeof (m.mem), 0);
2072 pthread_cleanup_pop (0);
2074 printf ("%s: msgsnd returned\n", __FUNCTION__);
2076 msgctl (tempmsg, IPC_RMID, NULL);
2078 exit (1);
2082 static struct
2084 const char *name;
2085 void *(*tf) (void *);
2086 int nb;
2087 int only_early;
2088 } tests[] =
2090 #define ADD_TEST(name, nbar, early) { #name, tf_##name, nbar, early }
2091 ADD_TEST (read, 2, 0),
2092 ADD_TEST (readv, 2, 0),
2093 ADD_TEST (select, 2, 0),
2094 ADD_TEST (pselect, 2, 0),
2095 ADD_TEST (poll, 2, 0),
2096 ADD_TEST (ppoll, 2, 0),
2097 ADD_TEST (write, 2, 0),
2098 ADD_TEST (writev, 2, 0),
2099 ADD_TEST (sleep, 2, 0),
2100 ADD_TEST (usleep, 2, 0),
2101 ADD_TEST (nanosleep, 2, 0),
2102 ADD_TEST (wait, 2, 0),
2103 ADD_TEST (waitid, 2, 0),
2104 ADD_TEST (waitpid, 2, 0),
2105 ADD_TEST (sigpause, 2, 0),
2106 ADD_TEST (sigsuspend, 2, 0),
2107 ADD_TEST (sigwait, 2, 0),
2108 ADD_TEST (sigwaitinfo, 2, 0),
2109 ADD_TEST (sigtimedwait, 2, 0),
2110 ADD_TEST (pause, 2, 0),
2111 ADD_TEST (accept, 2, 0),
2112 ADD_TEST (send, 2, 0),
2113 ADD_TEST (recv, 2, 0),
2114 ADD_TEST (recvfrom, 2, 0),
2115 ADD_TEST (recvmsg, 2, 0),
2116 ADD_TEST (open, 2, 1),
2117 ADD_TEST (close, 2, 1),
2118 ADD_TEST (pread, 2, 1),
2119 ADD_TEST (pwrite, 2, 1),
2120 ADD_TEST (fsync, 2, 1),
2121 ADD_TEST (fdatasync, 2, 1),
2122 ADD_TEST (msync, 2, 1),
2123 ADD_TEST (sendto, 2, 1),
2124 ADD_TEST (sendmsg, 2, 1),
2125 ADD_TEST (creat, 2, 1),
2126 ADD_TEST (connect, 2, 1),
2127 ADD_TEST (tcdrain, 2, 1),
2128 ADD_TEST (msgrcv, 2, 0),
2129 ADD_TEST (msgsnd, 2, 1),
2131 #define ntest_tf (sizeof (tests) / sizeof (tests[0]))
2134 static int
2135 do_test (void)
2137 int val;
2138 socklen_t len;
2140 if (socketpair (AF_UNIX, SOCK_STREAM, PF_UNIX, fds) != 0)
2142 perror ("socketpair");
2143 exit (1);
2146 val = 1;
2147 len = sizeof(val);
2148 setsockopt (fds[1], SOL_SOCKET, SO_SNDBUF, &val, sizeof(val));
2149 if (getsockopt (fds[1], SOL_SOCKET, SO_SNDBUF, &val, &len) < 0)
2151 perror ("getsockopt");
2152 exit (1);
2154 if (val >= WRITE_BUFFER_SIZE)
2156 puts ("minimum write buffer size too large");
2157 exit (1);
2159 setsockopt (fds[1], SOL_SOCKET, SO_SNDBUF, &val, sizeof(val));
2161 int result = 0;
2162 size_t cnt;
2163 for (cnt = 0; cnt < ntest_tf; ++cnt)
2165 if (tests[cnt].only_early)
2166 continue;
2168 if (pthread_barrier_init (&b2, NULL, tests[cnt].nb) != 0)
2170 puts ("b2 init failed");
2171 exit (1);
2174 /* Reset the counter for the cleanup handler. */
2175 cl_called = 0;
2177 pthread_t th;
2178 if (pthread_create (&th, NULL, tests[cnt].tf, NULL) != 0)
2180 printf ("create for '%s' test failed\n", tests[cnt].name);
2181 result = 1;
2182 continue;
2185 int r = pthread_barrier_wait (&b2);
2186 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
2188 printf ("%s: barrier_wait failed\n", __FUNCTION__);
2189 result = 1;
2190 continue;
2193 struct timespec ts = { .tv_sec = 0, .tv_nsec = 100000000 };
2194 while (nanosleep (&ts, &ts) != 0)
2195 continue;
2197 if (pthread_cancel (th) != 0)
2199 printf ("cancel for '%s' failed\n", tests[cnt].name);
2200 result = 1;
2201 continue;
2204 void *status;
2205 if (pthread_join (th, &status) != 0)
2207 printf ("join for '%s' failed\n", tests[cnt].name);
2208 result = 1;
2209 continue;
2211 if (status != PTHREAD_CANCELED)
2213 printf ("thread for '%s' not canceled\n", tests[cnt].name);
2214 result = 1;
2215 continue;
2218 if (pthread_barrier_destroy (&b2) != 0)
2220 puts ("barrier_destroy failed");
2221 result = 1;
2222 continue;
2225 if (cl_called == 0)
2227 printf ("cleanup handler not called for '%s'\n", tests[cnt].name);
2228 result = 1;
2229 continue;
2231 if (cl_called > 1)
2233 printf ("cleanup handler called more than once for '%s'\n",
2234 tests[cnt].name);
2235 result = 1;
2236 continue;
2239 printf ("in-time cancel test of '%s' successful\n", tests[cnt].name);
2241 if (tempfd != -1)
2243 close (tempfd);
2244 tempfd = -1;
2246 if (tempfd2 != -1)
2248 close (tempfd2);
2249 tempfd2 = -1;
2251 if (tempfname != NULL)
2253 unlink (tempfname);
2254 free (tempfname);
2255 tempfname = NULL;
2257 if (tempmsg != -1)
2259 msgctl (tempmsg, IPC_RMID, NULL);
2260 tempmsg = -1;
2264 for (cnt = 0; cnt < ntest_tf; ++cnt)
2266 if (pthread_barrier_init (&b2, NULL, tests[cnt].nb) != 0)
2268 puts ("b2 init failed");
2269 exit (1);
2272 /* Reset the counter for the cleanup handler. */
2273 cl_called = 0;
2275 pthread_t th;
2276 if (pthread_create (&th, NULL, tests[cnt].tf, (void *) 1l) != 0)
2278 printf ("create for '%s' test failed\n", tests[cnt].name);
2279 result = 1;
2280 continue;
2283 int r = pthread_barrier_wait (&b2);
2284 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
2286 printf ("%s: barrier_wait failed\n", __FUNCTION__);
2287 result = 1;
2288 continue;
2291 if (pthread_cancel (th) != 0)
2293 printf ("cancel for '%s' failed\n", tests[cnt].name);
2294 result = 1;
2295 continue;
2298 r = pthread_barrier_wait (&b2);
2299 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
2301 printf ("%s: barrier_wait failed\n", __FUNCTION__);
2302 result = 1;
2303 continue;
2306 void *status;
2307 if (pthread_join (th, &status) != 0)
2309 printf ("join for '%s' failed\n", tests[cnt].name);
2310 result = 1;
2311 continue;
2313 if (status != PTHREAD_CANCELED)
2315 printf ("thread for '%s' not canceled\n", tests[cnt].name);
2316 result = 1;
2317 continue;
2320 if (pthread_barrier_destroy (&b2) != 0)
2322 puts ("barrier_destroy failed");
2323 result = 1;
2324 continue;
2327 if (cl_called == 0)
2329 printf ("cleanup handler not called for '%s'\n", tests[cnt].name);
2330 result = 1;
2331 continue;
2333 if (cl_called > 1)
2335 printf ("cleanup handler called more than once for '%s'\n",
2336 tests[cnt].name);
2337 result = 1;
2338 continue;
2341 printf ("early cancel test of '%s' successful\n", tests[cnt].name);
2343 if (tempfd != -1)
2345 close (tempfd);
2346 tempfd = -1;
2348 if (tempfd2 != -1)
2350 close (tempfd2);
2351 tempfd2 = -1;
2353 if (tempfname != NULL)
2355 unlink (tempfname);
2356 free (tempfname);
2357 tempfname = NULL;
2359 if (tempmsg != -1)
2361 msgctl (tempmsg, IPC_RMID, NULL);
2362 tempmsg = -1;
2366 return result;
2369 #define TIMEOUT 60
2370 #define TEST_FUNCTION do_test ()
2371 #include "../test-skeleton.c"