(CFLAGS-tst-align.c): Add -mpreferred-stack-boundary=4.
[glibc.git] / nptl / tst-cancel4.c
blobc3e527fd1cd40c9cfd7b503db4657a00055be134
1 /* Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 /* NOTE: this tests functionality beyond POSIX. POSIX does not allow
21 exit to be called more than once. */
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <limits.h>
26 #include <pthread.h>
27 #include <stddef.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <termios.h>
32 #include <unistd.h>
33 #include <sys/mman.h>
34 #include <sys/msg.h>
35 #include <sys/poll.h>
36 #include <sys/select.h>
37 #include <sys/socket.h>
38 #include <sys/uio.h>
39 #include <sys/un.h>
40 #include <sys/wait.h>
42 #include "pthreadP.h"
45 /* Since STREAMS are not supported in the standard Linux kernel and
46 there we don't advertise STREAMS as supported is no need to test
47 the STREAMS related functions. This affects
48 getmsg() getpmsg() putmsg()
49 putpmsg()
51 lockf() and fcntl() are tested in tst-cancel16.
53 pthread_join() is tested in tst-join5.
55 pthread_testcancel()'s only purpose is to allow cancellation. This
56 is tested in several places.
58 sem_wait() and sem_timedwait() are checked in tst-cancel1[2345] tests.
60 mq_send(), mq_timedsend(), mq_receive() and mq_timedreceive() are checked
61 in tst-mqueue8{,x} tests.
63 aio_suspend() is tested in tst-cancel17.
65 clock_nanosleep() is tested in tst-cancel18.
68 /* Pipe descriptors. */
69 static int fds[2];
71 /* Temporary file descriptor, to be closed after each round. */
72 static int tempfd = -1;
73 static int tempfd2 = -1;
74 /* Name of temporary file to be removed after each round. */
75 static char *tempfname;
76 /* Temporary message queue. */
77 static int tempmsg = -1;
79 /* Often used barrier for two threads. */
80 static pthread_barrier_t b2;
83 #ifndef IPC_ADDVAL
84 # define IPC_ADDVAL 0
85 #endif
87 /* Cleanup handling test. */
88 static int cl_called;
90 static void
91 cl (void *arg)
93 ++cl_called;
98 static void *
99 tf_read (void *arg)
101 int fd;
102 int r;
104 if (arg == NULL)
105 fd = fds[0];
106 else
108 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
109 tempfd = fd = mkstemp (fname);
110 if (fd == -1)
111 printf ("%s: mkstemp failed\n", __FUNCTION__);
112 unlink (fname);
114 r = pthread_barrier_wait (&b2);
115 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
117 printf ("%s: barrier_wait failed\n", __FUNCTION__);
118 exit (1);
122 r = pthread_barrier_wait (&b2);
123 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
125 printf ("%s: barrier_wait failed\n", __FUNCTION__);
126 exit (1);
129 ssize_t s;
130 pthread_cleanup_push (cl, NULL);
132 char buf[100];
133 s = read (fd, buf, sizeof (buf));
135 pthread_cleanup_pop (0);
137 printf ("%s: read returns with %zd\n", __FUNCTION__, s);
139 exit (1);
143 static void *
144 tf_readv (void *arg)
146 int fd;
147 int r;
149 if (arg == NULL)
150 fd = fds[0];
151 else
153 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
154 tempfd = fd = mkstemp (fname);
155 if (fd == -1)
156 printf ("%s: mkstemp failed\n", __FUNCTION__);
157 unlink (fname);
159 r = pthread_barrier_wait (&b2);
160 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
162 printf ("%s: barrier_wait failed\n", __FUNCTION__);
163 exit (1);
167 r = pthread_barrier_wait (&b2);
168 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
170 printf ("%s: barrier_wait failed\n", __FUNCTION__);
171 exit (1);
174 ssize_t s;
175 pthread_cleanup_push (cl, NULL);
177 char buf[100];
178 struct iovec iov[1] = { [0] = { .iov_base = buf, .iov_len = sizeof (buf) } };
179 s = readv (fd, iov, 1);
181 pthread_cleanup_pop (0);
183 printf ("%s: readv returns with %zd\n", __FUNCTION__, s);
185 exit (1);
189 static void *
190 tf_write (void *arg)
192 int fd;
193 int r;
195 if (arg == NULL)
196 fd = fds[1];
197 else
199 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
200 tempfd = fd = mkstemp (fname);
201 if (fd == -1)
202 printf ("%s: mkstemp failed\n", __FUNCTION__);
203 unlink (fname);
205 r = pthread_barrier_wait (&b2);
206 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
208 printf ("%s: barrier_wait failed\n", __FUNCTION__);
209 exit (1);
213 r = pthread_barrier_wait (&b2);
214 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
216 printf ("%s: barrier_wait failed\n", __FUNCTION__);
217 exit (1);
220 ssize_t s;
221 pthread_cleanup_push (cl, NULL);
223 char buf[100000];
224 memset (buf, '\0', sizeof (buf));
225 s = write (fd, buf, sizeof (buf));
227 pthread_cleanup_pop (0);
229 printf ("%s: write returns with %zd\n", __FUNCTION__, s);
231 exit (1);
235 static void *
236 tf_writev (void *arg)
238 int fd;
239 int r;
241 if (arg == NULL)
242 fd = fds[1];
243 else
245 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
246 tempfd = fd = mkstemp (fname);
247 if (fd == -1)
248 printf ("%s: mkstemp failed\n", __FUNCTION__);
249 unlink (fname);
251 r = pthread_barrier_wait (&b2);
252 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
254 printf ("%s: barrier_wait failed\n", __FUNCTION__);
255 exit (1);
259 r = pthread_barrier_wait (&b2);
260 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
262 printf ("%s: barrier_wait failed\n", __FUNCTION__);
263 exit (1);
266 ssize_t s;
267 pthread_cleanup_push (cl, NULL);
269 char buf[100000];
270 memset (buf, '\0', sizeof (buf));
271 struct iovec iov[1] = { [0] = { .iov_base = buf, .iov_len = sizeof (buf) } };
272 s = writev (fd, iov, 1);
274 pthread_cleanup_pop (0);
276 printf ("%s: writev returns with %zd\n", __FUNCTION__, s);
278 exit (1);
282 static void *
283 tf_sleep (void *arg)
285 int r = pthread_barrier_wait (&b2);
286 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
288 printf ("%s: barrier_wait failed\n", __FUNCTION__);
289 exit (1);
292 if (arg != NULL)
294 r = pthread_barrier_wait (&b2);
295 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
297 printf ("%s: barrier_wait failed\n", __FUNCTION__);
298 exit (1);
302 pthread_cleanup_push (cl, NULL);
304 sleep (arg == NULL ? 1000000 : 0);
306 pthread_cleanup_pop (0);
308 printf ("%s: sleep returns\n", __FUNCTION__);
310 exit (1);
314 static void *
315 tf_usleep (void *arg)
317 int r = pthread_barrier_wait (&b2);
318 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
320 printf ("%s: barrier_wait failed\n", __FUNCTION__);
321 exit (1);
324 if (arg != NULL)
326 r = pthread_barrier_wait (&b2);
327 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
329 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
330 exit (1);
334 pthread_cleanup_push (cl, NULL);
336 usleep (arg == NULL ? (useconds_t) ULONG_MAX : 0);
338 pthread_cleanup_pop (0);
340 printf ("%s: usleep returns\n", __FUNCTION__);
342 exit (1);
346 static void *
347 tf_nanosleep (void *arg)
349 int r = pthread_barrier_wait (&b2);
350 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
352 printf ("%s: barrier_wait failed\n", __FUNCTION__);
353 exit (1);
356 if (arg != NULL)
358 r = pthread_barrier_wait (&b2);
359 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
361 printf ("%s: barrier_wait failed\n", __FUNCTION__);
362 exit (1);
366 pthread_cleanup_push (cl, NULL);
368 struct timespec ts = { .tv_sec = arg == NULL ? 10000000 : 0, .tv_nsec = 0 };
369 TEMP_FAILURE_RETRY (nanosleep (&ts, &ts));
371 pthread_cleanup_pop (0);
373 printf ("%s: nanosleep returns\n", __FUNCTION__);
375 exit (1);
379 static void *
380 tf_select (void *arg)
382 int fd;
383 int r;
385 if (arg == NULL)
386 fd = fds[0];
387 else
389 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
390 tempfd = fd = mkstemp (fname);
391 if (fd == -1)
392 printf ("%s: mkstemp failed\n", __FUNCTION__);
393 unlink (fname);
395 r = pthread_barrier_wait (&b2);
396 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
398 printf ("%s: barrier_wait failed\n", __FUNCTION__);
399 exit (1);
403 r = pthread_barrier_wait (&b2);
404 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
406 printf ("%s: barrier_wait failed\n", __FUNCTION__);
407 exit (1);
410 fd_set rfs;
411 FD_ZERO (&rfs);
412 FD_SET (fd, &rfs);
414 int s;
415 pthread_cleanup_push (cl, NULL);
417 s = select (fd + 1, &rfs, NULL, NULL, NULL);
419 pthread_cleanup_pop (0);
421 printf ("%s: select returns with %d (%s)\n", __FUNCTION__, s,
422 strerror (errno));
424 exit (1);
428 static void *
429 tf_pselect (void *arg)
431 int fd;
432 int r;
434 if (arg == NULL)
435 fd = fds[0];
436 else
438 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
439 tempfd = fd = mkstemp (fname);
440 if (fd == -1)
441 printf ("%s: mkstemp failed\n", __FUNCTION__);
442 unlink (fname);
444 r = pthread_barrier_wait (&b2);
445 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
447 printf ("%s: barrier_wait failed\n", __FUNCTION__);
448 exit (1);
452 r = pthread_barrier_wait (&b2);
453 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
455 printf ("%s: barrier_wait failed\n", __FUNCTION__);
456 exit (1);
459 fd_set rfs;
460 FD_ZERO (&rfs);
461 FD_SET (fd, &rfs);
463 int s;
464 pthread_cleanup_push (cl, NULL);
466 s = pselect (fd + 1, &rfs, NULL, NULL, NULL, NULL);
468 pthread_cleanup_pop (0);
470 printf ("%s: pselect returns with %d (%s)\n", __FUNCTION__, s,
471 strerror (errno));
473 exit (1);
477 static void *
478 tf_poll (void *arg)
480 int fd;
481 int r;
483 if (arg == NULL)
484 fd = fds[0];
485 else
487 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
488 tempfd = fd = mkstemp (fname);
489 if (fd == -1)
490 printf ("%s: mkstemp failed\n", __FUNCTION__);
491 unlink (fname);
493 r = pthread_barrier_wait (&b2);
494 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
496 printf ("%s: barrier_wait failed\n", __FUNCTION__);
497 exit (1);
501 r = pthread_barrier_wait (&b2);
502 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
504 printf ("%s: barrier_wait failed\n", __FUNCTION__);
505 exit (1);
508 struct pollfd rfs[1] = { [0] = { .fd = fd, .events = POLLIN } };
510 int s;
511 pthread_cleanup_push (cl, NULL);
513 s = poll (rfs, 1, -1);
515 pthread_cleanup_pop (0);
517 printf ("%s: poll returns with %d (%s)\n", __FUNCTION__, s,
518 strerror (errno));
520 exit (1);
524 static void *
525 tf_wait (void *arg)
527 pid_t pid = fork ();
528 if (pid == -1)
530 puts ("fork failed");
531 exit (1);
534 if (pid == 0)
536 /* Make the program disappear after a while. */
537 if (arg == NULL)
538 sleep (10);
539 exit (0);
542 int r;
543 if (arg != NULL)
545 struct timespec ts = { .tv_sec = 0, .tv_nsec = 100000000 };
546 while (nanosleep (&ts, &ts) != 0)
547 continue;
549 r = pthread_barrier_wait (&b2);
550 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
552 printf ("%s: barrier_wait failed\n", __FUNCTION__);
553 exit (1);
557 r = pthread_barrier_wait (&b2);
558 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
560 printf ("%s: barrier_wait failed\n", __FUNCTION__);
561 exit (1);
564 int s;
565 pthread_cleanup_push (cl, NULL);
567 s = wait (NULL);
569 pthread_cleanup_pop (0);
571 printf ("%s: wait returns with %d (%s)\n", __FUNCTION__, s,
572 strerror (errno));
574 exit (1);
578 static void *
579 tf_waitpid (void *arg)
582 pid_t pid = fork ();
583 if (pid == -1)
585 puts ("fork failed");
586 exit (1);
589 if (pid == 0)
591 /* Make the program disappear after a while. */
592 if (arg == NULL)
593 sleep (10);
594 exit (0);
597 int r;
598 if (arg != NULL)
600 struct timespec ts = { .tv_sec = 0, .tv_nsec = 100000000 };
601 while (nanosleep (&ts, &ts) != 0)
602 continue;
604 r = pthread_barrier_wait (&b2);
605 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
607 printf ("%s: barrier_wait failed\n", __FUNCTION__);
608 exit (1);
612 r = pthread_barrier_wait (&b2);
613 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
615 printf ("%s: barrier_wait failed\n", __FUNCTION__);
616 exit (1);
619 int s;
620 pthread_cleanup_push (cl, NULL);
622 s = waitpid (-1, NULL, 0);
624 pthread_cleanup_pop (0);
626 printf ("%s: waitpid returns with %d (%s)\n", __FUNCTION__, s,
627 strerror (errno));
629 exit (1);
633 static void *
634 tf_waitid (void *arg)
636 pid_t pid = fork ();
637 if (pid == -1)
639 puts ("fork failed");
640 exit (1);
643 if (pid == 0)
645 /* Make the program disappear after a while. */
646 if (arg == NULL)
647 sleep (10);
648 exit (0);
651 int r;
652 if (arg != NULL)
654 struct timespec ts = { .tv_sec = 0, .tv_nsec = 100000000 };
655 while (nanosleep (&ts, &ts) != 0)
656 continue;
658 r = pthread_barrier_wait (&b2);
659 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
661 printf ("%s: barrier_wait failed\n", __FUNCTION__);
662 exit (1);
666 r = pthread_barrier_wait (&b2);
667 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
669 printf ("%s: barrier_wait failed\n", __FUNCTION__);
670 exit (1);
673 int s;
674 pthread_cleanup_push (cl, NULL);
676 #ifndef WEXITED
677 # define WEXITED 0
678 #endif
679 siginfo_t si;
680 s = waitid (P_PID, pid, &si, WEXITED);
682 pthread_cleanup_pop (0);
684 printf ("%s: waitid returns with %d (%s)\n", __FUNCTION__, s,
685 strerror (errno));
687 exit (1);
691 static void *
692 tf_sigpause (void *arg)
694 int r = pthread_barrier_wait (&b2);
695 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
697 printf ("%s: barrier_wait failed\n", __FUNCTION__);
698 exit (1);
701 if (arg != NULL)
703 r = pthread_barrier_wait (&b2);
704 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
706 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
707 exit (1);
711 pthread_cleanup_push (cl, NULL);
713 /* Just for fun block the cancellation signal. We need to use
714 __xpg_sigpause since otherwise we will get the BSD version. */
715 __xpg_sigpause (SIGCANCEL);
717 pthread_cleanup_pop (0);
719 printf ("%s: sigpause returned\n", __FUNCTION__);
721 exit (1);
725 static void *
726 tf_sigsuspend (void *arg)
728 int r = pthread_barrier_wait (&b2);
729 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
731 printf ("%s: barrier_wait failed\n", __FUNCTION__);
732 exit (1);
735 if (arg != NULL)
737 r = pthread_barrier_wait (&b2);
738 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
740 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
741 exit (1);
745 pthread_cleanup_push (cl, NULL);
747 /* Just for fun block all signals. */
748 sigset_t mask;
749 sigfillset (&mask);
750 sigsuspend (&mask);
752 pthread_cleanup_pop (0);
754 printf ("%s: sigsuspend returned\n", __FUNCTION__);
756 exit (1);
760 static void *
761 tf_sigwait (void *arg)
763 int r = pthread_barrier_wait (&b2);
764 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
766 printf ("%s: barrier_wait failed\n", __FUNCTION__);
767 exit (1);
770 if (arg != NULL)
772 r = pthread_barrier_wait (&b2);
773 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
775 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
776 exit (1);
780 /* Block SIGUSR1. */
781 sigset_t mask;
782 sigemptyset (&mask);
783 sigaddset (&mask, SIGUSR1);
784 if (pthread_sigmask (SIG_BLOCK, &mask, NULL) != 0)
786 printf ("%s: pthread_sigmask failed\n", __FUNCTION__);
787 exit (1);
790 int sig;
791 pthread_cleanup_push (cl, NULL);
793 /* Wait for SIGUSR1. */
794 sigwait (&mask, &sig);
796 pthread_cleanup_pop (0);
798 printf ("%s: sigwait returned with signal %d\n", __FUNCTION__, sig);
800 exit (1);
804 static void *
805 tf_sigwaitinfo (void *arg)
807 int r = pthread_barrier_wait (&b2);
808 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
810 printf ("%s: barrier_wait failed\n", __FUNCTION__);
811 exit (1);
814 if (arg != NULL)
816 r = pthread_barrier_wait (&b2);
817 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
819 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
820 exit (1);
824 /* Block SIGUSR1. */
825 sigset_t mask;
826 sigemptyset (&mask);
827 sigaddset (&mask, SIGUSR1);
828 if (pthread_sigmask (SIG_BLOCK, &mask, NULL) != 0)
830 printf ("%s: pthread_sigmask failed\n", __FUNCTION__);
831 exit (1);
834 siginfo_t info;
835 pthread_cleanup_push (cl, NULL);
837 /* Wait for SIGUSR1. */
838 sigwaitinfo (&mask, &info);
840 pthread_cleanup_pop (0);
842 printf ("%s: sigwaitinfo returned with signal %d\n", __FUNCTION__,
843 info.si_signo);
845 exit (1);
849 static void *
850 tf_sigtimedwait (void *arg)
852 int r = pthread_barrier_wait (&b2);
853 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
855 printf ("%s: barrier_wait failed\n", __FUNCTION__);
856 exit (1);
859 if (arg != NULL)
861 r = pthread_barrier_wait (&b2);
862 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
864 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
865 exit (1);
869 /* Block SIGUSR1. */
870 sigset_t mask;
871 sigemptyset (&mask);
872 sigaddset (&mask, SIGUSR1);
873 if (pthread_sigmask (SIG_BLOCK, &mask, NULL) != 0)
875 printf ("%s: pthread_sigmask failed\n", __FUNCTION__);
876 exit (1);
879 /* Wait for SIGUSR1. */
880 siginfo_t info;
881 struct timespec ts = { .tv_sec = 60, .tv_nsec = 0 };
882 pthread_cleanup_push (cl, NULL);
884 sigtimedwait (&mask, &info, &ts);
886 pthread_cleanup_pop (0);
888 printf ("%s: sigtimedwait returned with signal %d\n", __FUNCTION__,
889 info.si_signo);
891 exit (1);
895 static void *
896 tf_pause (void *arg)
898 int r = pthread_barrier_wait (&b2);
899 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
901 printf ("%s: barrier_wait failed\n", __FUNCTION__);
902 exit (1);
905 if (arg != NULL)
907 r = pthread_barrier_wait (&b2);
908 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
910 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
911 exit (1);
915 pthread_cleanup_push (cl, NULL);
917 pause ();
919 pthread_cleanup_pop (0);
921 printf ("%s: pause returned\n", __FUNCTION__);
923 exit (1);
927 static void *
928 tf_accept (void *arg)
930 struct sockaddr_un sun;
931 /* To test a non-blocking accept call we make the call file by using
932 a datagrame socket. */
933 int pf = arg == NULL ? SOCK_STREAM : SOCK_DGRAM;
935 tempfd = socket (AF_UNIX, pf, 0);
936 if (tempfd == -1)
938 printf ("%s: socket call failed\n", __FUNCTION__);
939 exit (1);
942 int tries = 0;
945 if (++tries > 10)
947 printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
950 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-1-XXXXXX");
951 if (mktemp (sun.sun_path) == NULL)
953 printf ("%s: cannot generate temp file name\n", __FUNCTION__);
954 exit (1);
957 sun.sun_family = AF_UNIX;
959 while (bind (tempfd, (struct sockaddr *) &sun,
960 offsetof (struct sockaddr_un, sun_path)
961 + strlen (sun.sun_path) + 1) != 0);
963 unlink (sun.sun_path);
965 listen (tempfd, 5);
967 socklen_t len = sizeof (sun);
969 int r = pthread_barrier_wait (&b2);
970 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
972 printf ("%s: barrier_wait failed\n", __FUNCTION__);
973 exit (1);
976 if (arg != NULL)
978 r = pthread_barrier_wait (&b2);
979 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
981 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
982 exit (1);
986 pthread_cleanup_push (cl, NULL);
988 accept (tempfd, (struct sockaddr *) &sun, &len);
990 pthread_cleanup_pop (0);
992 printf ("%s: accept returned\n", __FUNCTION__);
994 exit (1);
998 static void *
999 tf_send (void *arg)
1001 struct sockaddr_un sun;
1003 tempfd = socket (AF_UNIX, SOCK_STREAM, 0);
1004 if (tempfd == -1)
1006 printf ("%s: first socket call failed\n", __FUNCTION__);
1007 exit (1);
1010 int tries = 0;
1013 if (++tries > 10)
1015 printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
1018 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-2-XXXXXX");
1019 if (mktemp (sun.sun_path) == NULL)
1021 printf ("%s: cannot generate temp file name\n", __FUNCTION__);
1022 exit (1);
1025 sun.sun_family = AF_UNIX;
1027 while (bind (tempfd, (struct sockaddr *) &sun,
1028 offsetof (struct sockaddr_un, sun_path)
1029 + strlen (sun.sun_path) + 1) != 0);
1031 listen (tempfd, 5);
1033 tempfd2 = socket (AF_UNIX, SOCK_STREAM, 0);
1034 if (tempfd2 == -1)
1036 printf ("%s: second socket call failed\n", __FUNCTION__);
1037 exit (1);
1040 if (connect (tempfd2, (struct sockaddr *) &sun, sizeof (sun)) != 0)
1042 printf ("%s: connect failed\n", __FUNCTION__);
1043 exit(1);
1046 unlink (sun.sun_path);
1048 int r = pthread_barrier_wait (&b2);
1049 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1051 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1052 exit (1);
1055 if (arg != NULL)
1057 r = pthread_barrier_wait (&b2);
1058 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1060 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1061 exit (1);
1065 pthread_cleanup_push (cl, NULL);
1067 /* Very large block, so that the send call blocks. */
1068 char mem[700000];
1070 send (tempfd2, mem, arg == NULL ? sizeof (mem) : 1, 0);
1072 pthread_cleanup_pop (0);
1074 printf ("%s: send returned\n", __FUNCTION__);
1076 exit (1);
1080 static void *
1081 tf_recv (void *arg)
1083 struct sockaddr_un sun;
1085 tempfd = socket (AF_UNIX, SOCK_STREAM, 0);
1086 if (tempfd == -1)
1088 printf ("%s: first socket call failed\n", __FUNCTION__);
1089 exit (1);
1092 int tries = 0;
1095 if (++tries > 10)
1097 printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
1100 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-3-XXXXXX");
1101 if (mktemp (sun.sun_path) == NULL)
1103 printf ("%s: cannot generate temp file name\n", __FUNCTION__);
1104 exit (1);
1107 sun.sun_family = AF_UNIX;
1109 while (bind (tempfd, (struct sockaddr *) &sun,
1110 offsetof (struct sockaddr_un, sun_path)
1111 + strlen (sun.sun_path) + 1) != 0);
1113 listen (tempfd, 5);
1115 tempfd2 = socket (AF_UNIX, SOCK_STREAM, 0);
1116 if (tempfd2 == -1)
1118 printf ("%s: second socket call failed\n", __FUNCTION__);
1119 exit (1);
1122 if (connect (tempfd2, (struct sockaddr *) &sun, sizeof (sun)) != 0)
1124 printf ("%s: connect failed\n", __FUNCTION__);
1125 exit(1);
1128 unlink (sun.sun_path);
1130 int r = pthread_barrier_wait (&b2);
1131 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1133 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1134 exit (1);
1137 if (arg != NULL)
1139 r = pthread_barrier_wait (&b2);
1140 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1142 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1143 exit (1);
1147 pthread_cleanup_push (cl, NULL);
1149 char mem[70];
1151 recv (tempfd2, mem, arg == NULL ? sizeof (mem) : 0, 0);
1153 pthread_cleanup_pop (0);
1155 printf ("%s: recv returned\n", __FUNCTION__);
1157 exit (1);
1161 static void *
1162 tf_recvfrom (void *arg)
1164 struct sockaddr_un sun;
1166 tempfd = socket (AF_UNIX, SOCK_DGRAM, 0);
1167 if (tempfd == -1)
1169 printf ("%s: first socket call failed\n", __FUNCTION__);
1170 exit (1);
1173 int tries = 0;
1176 if (++tries > 10)
1178 printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
1181 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-4-XXXXXX");
1182 if (mktemp (sun.sun_path) == NULL)
1184 printf ("%s: cannot generate temp file name\n", __FUNCTION__);
1185 exit (1);
1188 sun.sun_family = AF_UNIX;
1190 while (bind (tempfd, (struct sockaddr *) &sun,
1191 offsetof (struct sockaddr_un, sun_path)
1192 + strlen (sun.sun_path) + 1) != 0);
1194 tempfname = strdup (sun.sun_path);
1196 tempfd2 = socket (AF_UNIX, SOCK_DGRAM, 0);
1197 if (tempfd2 == -1)
1199 printf ("%s: second socket call failed\n", __FUNCTION__);
1200 exit (1);
1203 int r = pthread_barrier_wait (&b2);
1204 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1206 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1207 exit (1);
1210 if (arg != NULL)
1212 r = pthread_barrier_wait (&b2);
1213 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1215 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1216 exit (1);
1220 pthread_cleanup_push (cl, NULL);
1222 char mem[70];
1223 socklen_t len = sizeof (sun);
1225 recvfrom (tempfd2, mem, arg == NULL ? sizeof (mem) : 0, 0,
1226 (struct sockaddr *) &sun, &len);
1228 pthread_cleanup_pop (0);
1230 printf ("%s: recvfrom returned\n", __FUNCTION__);
1232 exit (1);
1236 static void *
1237 tf_recvmsg (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-5-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 struct iovec iov[1];
1299 iov[0].iov_base = mem;
1300 iov[0].iov_len = arg == NULL ? sizeof (mem) : 0;
1302 struct msghdr m;
1303 m.msg_name = &sun;
1304 m.msg_namelen = sizeof (sun);
1305 m.msg_iov = iov;
1306 m.msg_iovlen = 1;
1307 m.msg_control = NULL;
1308 m.msg_controllen = 0;
1310 recvmsg (tempfd2, &m, 0);
1312 pthread_cleanup_pop (0);
1314 printf ("%s: recvmsg returned\n", __FUNCTION__);
1316 exit (1);
1320 static void *
1321 tf_open (void *arg)
1323 if (arg == NULL)
1324 // XXX If somebody can provide a portable test case in which open()
1325 // blocks we can enable this test to run in both rounds.
1326 abort ();
1328 int r = pthread_barrier_wait (&b2);
1329 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1331 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1332 exit (1);
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);
1342 pthread_cleanup_push (cl, NULL);
1344 open ("Makefile", O_RDONLY);
1346 pthread_cleanup_pop (0);
1348 printf ("%s: open returned\n", __FUNCTION__);
1350 exit (1);
1354 static void *
1355 tf_close (void *arg)
1357 if (arg == NULL)
1358 // XXX If somebody can provide a portable test case in which close()
1359 // blocks we can enable this test to run in both rounds.
1360 abort ();
1362 char fname[] = "/tmp/tst-cancel-fd-XXXXXX";
1363 tempfd = mkstemp (fname);
1364 if (tempfd == -1)
1366 printf ("%s: mkstemp failed\n", __FUNCTION__);
1367 exit (1);
1369 unlink (fname);
1371 int r = pthread_barrier_wait (&b2);
1372 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1374 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1375 exit (1);
1378 r = pthread_barrier_wait (&b2);
1379 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1381 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1382 exit (1);
1385 pthread_cleanup_push (cl, NULL);
1387 close (tempfd);
1389 pthread_cleanup_pop (0);
1391 printf ("%s: close returned\n", __FUNCTION__);
1393 exit (1);
1397 static void *
1398 tf_pread (void *arg)
1400 if (arg == NULL)
1401 // XXX If somebody can provide a portable test case in which pread()
1402 // blocks we can enable this test to run in both rounds.
1403 abort ();
1405 tempfd = open ("Makefile", O_RDONLY);
1406 if (tempfd == -1)
1408 printf ("%s: cannot open Makefile\n", __FUNCTION__);
1409 exit (1);
1412 int r = pthread_barrier_wait (&b2);
1413 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1415 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1416 exit (1);
1419 r = pthread_barrier_wait (&b2);
1420 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1422 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1423 exit (1);
1426 pthread_cleanup_push (cl, NULL);
1428 char mem[10];
1429 pread (tempfd, mem, sizeof (mem), 0);
1431 pthread_cleanup_pop (0);
1433 printf ("%s: pread returned\n", __FUNCTION__);
1435 exit (1);
1439 static void *
1440 tf_pwrite (void *arg)
1442 if (arg == NULL)
1443 // XXX If somebody can provide a portable test case in which pwrite()
1444 // blocks we can enable this test to run in both rounds.
1445 abort ();
1447 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
1448 tempfd = mkstemp (fname);
1449 if (tempfd == -1)
1451 printf ("%s: mkstemp failed\n", __FUNCTION__);
1452 exit (1);
1454 unlink (fname);
1456 int r = pthread_barrier_wait (&b2);
1457 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1459 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1460 exit (1);
1463 r = pthread_barrier_wait (&b2);
1464 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1466 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1467 exit (1);
1470 pthread_cleanup_push (cl, NULL);
1472 char mem[10];
1473 pwrite (tempfd, mem, sizeof (mem), 0);
1475 pthread_cleanup_pop (0);
1477 printf ("%s: pwrite returned\n", __FUNCTION__);
1479 exit (1);
1483 static void *
1484 tf_fsync (void *arg)
1486 if (arg == NULL)
1487 // XXX If somebody can provide a portable test case in which fsync()
1488 // blocks we can enable this test to run in both rounds.
1489 abort ();
1491 tempfd = open ("Makefile", O_RDONLY);
1492 if (tempfd == -1)
1494 printf ("%s: cannot open Makefile\n", __FUNCTION__);
1495 exit (1);
1498 int r = pthread_barrier_wait (&b2);
1499 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1501 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1502 exit (1);
1505 r = pthread_barrier_wait (&b2);
1506 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1508 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1509 exit (1);
1512 pthread_cleanup_push (cl, NULL);
1514 fsync (tempfd);
1516 pthread_cleanup_pop (0);
1518 printf ("%s: fsync returned\n", __FUNCTION__);
1520 exit (1);
1524 static void *
1525 tf_msync (void *arg)
1527 if (arg == NULL)
1528 // XXX If somebody can provide a portable test case in which msync()
1529 // blocks we can enable this test to run in both rounds.
1530 abort ();
1532 tempfd = open ("Makefile", O_RDONLY);
1533 if (tempfd == -1)
1535 printf ("%s: cannot open Makefile\n", __FUNCTION__);
1536 exit (1);
1538 void *p = mmap (NULL, 10, PROT_READ, MAP_SHARED, tempfd, 0);
1539 if (p == MAP_FAILED)
1541 printf ("%s: mmap failed\n", __FUNCTION__);
1542 exit (1);
1545 int r = pthread_barrier_wait (&b2);
1546 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1548 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1549 exit (1);
1552 r = pthread_barrier_wait (&b2);
1553 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1555 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1556 exit (1);
1559 pthread_cleanup_push (cl, NULL);
1561 msync (p, 10, 0);
1563 pthread_cleanup_pop (0);
1565 printf ("%s: msync returned\n", __FUNCTION__);
1567 exit (1);
1571 static void *
1572 tf_sendto (void *arg)
1574 if (arg == NULL)
1575 // XXX If somebody can provide a portable test case in which sendto()
1576 // blocks we can enable this test to run in both rounds.
1577 abort ();
1579 struct sockaddr_un sun;
1581 tempfd = socket (AF_UNIX, SOCK_DGRAM, 0);
1582 if (tempfd == -1)
1584 printf ("%s: first socket call failed\n", __FUNCTION__);
1585 exit (1);
1588 int tries = 0;
1591 if (++tries > 10)
1593 printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
1596 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-6-XXXXXX");
1597 if (mktemp (sun.sun_path) == NULL)
1599 printf ("%s: cannot generate temp file name\n", __FUNCTION__);
1600 exit (1);
1603 sun.sun_family = AF_UNIX;
1605 while (bind (tempfd, (struct sockaddr *) &sun,
1606 offsetof (struct sockaddr_un, sun_path)
1607 + strlen (sun.sun_path) + 1) != 0);
1608 tempfname = strdup (sun.sun_path);
1610 tempfd2 = socket (AF_UNIX, SOCK_DGRAM, 0);
1611 if (tempfd2 == -1)
1613 printf ("%s: second socket call failed\n", __FUNCTION__);
1614 exit (1);
1617 int r = pthread_barrier_wait (&b2);
1618 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1620 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1621 exit (1);
1624 r = pthread_barrier_wait (&b2);
1625 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1627 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1628 exit (1);
1631 pthread_cleanup_push (cl, NULL);
1633 char mem[1];
1635 sendto (tempfd2, mem, arg == NULL ? sizeof (mem) : 1, 0,
1636 (struct sockaddr *) &sun,
1637 offsetof (struct sockaddr_un, sun_path) + strlen (sun.sun_path) + 1);
1639 pthread_cleanup_pop (0);
1641 printf ("%s: sendto returned\n", __FUNCTION__);
1643 exit (1);
1647 static void *
1648 tf_sendmsg (void *arg)
1650 if (arg == NULL)
1651 // XXX If somebody can provide a portable test case in which sendmsg()
1652 // blocks we can enable this test to run in both rounds.
1653 abort ();
1655 struct sockaddr_un sun;
1657 tempfd = socket (AF_UNIX, SOCK_DGRAM, 0);
1658 if (tempfd == -1)
1660 printf ("%s: first socket call failed\n", __FUNCTION__);
1661 exit (1);
1664 int tries = 0;
1667 if (++tries > 10)
1669 printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
1672 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-7-XXXXXX");
1673 if (mktemp (sun.sun_path) == NULL)
1675 printf ("%s: cannot generate temp file name\n", __FUNCTION__);
1676 exit (1);
1679 sun.sun_family = AF_UNIX;
1681 while (bind (tempfd, (struct sockaddr *) &sun,
1682 offsetof (struct sockaddr_un, sun_path)
1683 + strlen (sun.sun_path) + 1) != 0);
1684 tempfname = strdup (sun.sun_path);
1686 tempfd2 = socket (AF_UNIX, SOCK_DGRAM, 0);
1687 if (tempfd2 == -1)
1689 printf ("%s: second socket call failed\n", __FUNCTION__);
1690 exit (1);
1693 int r = pthread_barrier_wait (&b2);
1694 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1696 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1697 exit (1);
1700 r = pthread_barrier_wait (&b2);
1701 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1703 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1704 exit (1);
1707 pthread_cleanup_push (cl, NULL);
1709 char mem[1];
1710 struct iovec iov[1];
1711 iov[0].iov_base = mem;
1712 iov[0].iov_len = 1;
1714 struct msghdr m;
1715 m.msg_name = &sun;
1716 m.msg_namelen = (offsetof (struct sockaddr_un, sun_path)
1717 + strlen (sun.sun_path) + 1);
1718 m.msg_iov = iov;
1719 m.msg_iovlen = 1;
1720 m.msg_control = NULL;
1721 m.msg_controllen = 0;
1723 sendmsg (tempfd2, &m, 0);
1725 pthread_cleanup_pop (0);
1727 printf ("%s: sendmsg returned\n", __FUNCTION__);
1729 exit (1);
1733 static void *
1734 tf_creat (void *arg)
1736 if (arg == NULL)
1737 // XXX If somebody can provide a portable test case in which sendmsg()
1738 // blocks we can enable this test to run in both rounds.
1739 abort ();
1741 int r = pthread_barrier_wait (&b2);
1742 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1744 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1745 exit (1);
1748 r = pthread_barrier_wait (&b2);
1749 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1751 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1752 exit (1);
1755 pthread_cleanup_push (cl, NULL);
1757 creat ("tmp/tst-cancel-4-should-not-exist", 0666);
1759 pthread_cleanup_pop (0);
1761 printf ("%s: creat returned\n", __FUNCTION__);
1763 exit (1);
1767 static void *
1768 tf_connect (void *arg)
1770 if (arg == NULL)
1771 // XXX If somebody can provide a portable test case in which connect()
1772 // blocks we can enable this test to run in both rounds.
1773 abort ();
1775 struct sockaddr_un sun;
1777 tempfd = socket (AF_UNIX, SOCK_STREAM, 0);
1778 if (tempfd == -1)
1780 printf ("%s: first socket call failed\n", __FUNCTION__);
1781 exit (1);
1784 int tries = 0;
1787 if (++tries > 10)
1789 printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
1792 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-2-XXXXXX");
1793 if (mktemp (sun.sun_path) == NULL)
1795 printf ("%s: cannot generate temp file name\n", __FUNCTION__);
1796 exit (1);
1799 sun.sun_family = AF_UNIX;
1801 while (bind (tempfd, (struct sockaddr *) &sun,
1802 offsetof (struct sockaddr_un, sun_path)
1803 + strlen (sun.sun_path) + 1) != 0);
1804 tempfname = strdup (sun.sun_path);
1806 listen (tempfd, 5);
1808 tempfd2 = socket (AF_UNIX, SOCK_STREAM, 0);
1809 if (tempfd2 == -1)
1811 printf ("%s: second socket call failed\n", __FUNCTION__);
1812 exit (1);
1815 int r = pthread_barrier_wait (&b2);
1816 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1818 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1819 exit (1);
1822 if (arg != NULL)
1824 r = pthread_barrier_wait (&b2);
1825 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1827 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1828 exit (1);
1832 pthread_cleanup_push (cl, NULL);
1834 connect (tempfd2, (struct sockaddr *) &sun, sizeof (sun));
1836 pthread_cleanup_pop (0);
1838 printf ("%s: connect returned\n", __FUNCTION__);
1840 exit (1);
1844 static void *
1845 tf_tcdrain (void *arg)
1847 if (arg == NULL)
1848 // XXX If somebody can provide a portable test case in which tcdrain()
1849 // blocks we can enable this test to run in both rounds.
1850 abort ();
1852 int r = pthread_barrier_wait (&b2);
1853 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1855 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1856 exit (1);
1859 if (arg != NULL)
1861 r = pthread_barrier_wait (&b2);
1862 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1864 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1865 exit (1);
1869 pthread_cleanup_push (cl, NULL);
1871 /* Regardless of stderr being a terminal, the tcdrain call should be
1872 canceled. */
1873 tcdrain (STDERR_FILENO);
1875 pthread_cleanup_pop (0);
1877 printf ("%s: tcdrain returned\n", __FUNCTION__);
1879 exit (1);
1883 static void *
1884 tf_msgrcv (void *arg)
1886 tempmsg = msgget (IPC_PRIVATE, 0666 | IPC_CREAT);
1887 if (tempmsg == -1)
1889 printf ("%s: msgget failed: %s\n", __FUNCTION__, strerror (errno));
1890 exit (1);
1893 int r = pthread_barrier_wait (&b2);
1894 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1896 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1897 exit (1);
1900 if (arg != NULL)
1902 r = pthread_barrier_wait (&b2);
1903 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1905 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1906 exit (1);
1910 ssize_t s;
1912 pthread_cleanup_push (cl, NULL);
1914 struct
1916 long int type;
1917 char mem[10];
1918 } m;
1919 int randnr;
1920 /* We need a positive random number. */
1922 randnr = random () % 64000;
1923 while (randnr <= 0);
1926 errno = 0;
1927 s = msgrcv (tempmsg, (struct msgbuf *) &m, 10, randnr, 0);
1929 while (errno == EIDRM || errno == EINTR);
1931 pthread_cleanup_pop (0);
1933 printf ("%s: msgrcv returned %zd with errno = %m\n", __FUNCTION__, s);
1935 msgctl (tempmsg, IPC_RMID, NULL);
1937 exit (1);
1941 static void *
1942 tf_msgsnd (void *arg)
1944 if (arg == NULL)
1945 // XXX If somebody can provide a portable test case in which msgsnd()
1946 // blocks we can enable this test to run in both rounds.
1947 abort ();
1949 tempmsg = msgget (IPC_PRIVATE, 0666 | IPC_CREAT);
1950 if (tempmsg == -1)
1952 printf ("%s: msgget failed: %s\n", __FUNCTION__, strerror (errno));
1953 exit (1);
1956 int r = pthread_barrier_wait (&b2);
1957 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1959 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1960 exit (1);
1963 r = pthread_barrier_wait (&b2);
1964 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1966 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1967 exit (1);
1970 pthread_cleanup_push (cl, NULL);
1972 struct
1974 long int type;
1975 char mem[1];
1976 } m;
1977 /* We need a positive random number. */
1979 m.type = random () % 64000;
1980 while (m.type <= 0);
1981 msgsnd (tempmsg, (struct msgbuf *) &m, sizeof (m.mem), 0);
1983 pthread_cleanup_pop (0);
1985 printf ("%s: msgsnd returned\n", __FUNCTION__);
1987 msgctl (tempmsg, IPC_RMID, NULL);
1989 exit (1);
1993 static struct
1995 const char *name;
1996 void *(*tf) (void *);
1997 int nb;
1998 int only_early;
1999 } tests[] =
2001 #define ADD_TEST(name, nbar, early) { #name, tf_##name, nbar, early }
2002 ADD_TEST (read, 2, 0),
2003 ADD_TEST (readv, 2, 0),
2004 ADD_TEST (select, 2, 0),
2005 ADD_TEST (pselect, 2, 0),
2006 ADD_TEST (poll, 2, 0),
2007 ADD_TEST (write, 2, 0),
2008 ADD_TEST (writev, 2, 0),
2009 ADD_TEST (sleep, 2, 0),
2010 ADD_TEST (usleep, 2, 0),
2011 ADD_TEST (nanosleep, 2, 0),
2012 ADD_TEST (wait, 2, 0),
2013 ADD_TEST (waitid, 2, 0),
2014 ADD_TEST (waitpid, 2, 0),
2015 ADD_TEST (sigpause, 2, 0),
2016 ADD_TEST (sigsuspend, 2, 0),
2017 ADD_TEST (sigwait, 2, 0),
2018 ADD_TEST (sigwaitinfo, 2, 0),
2019 ADD_TEST (sigtimedwait, 2, 0),
2020 ADD_TEST (pause, 2, 0),
2021 ADD_TEST (accept, 2, 0),
2022 ADD_TEST (send, 2, 0),
2023 ADD_TEST (recv, 2, 0),
2024 ADD_TEST (recvfrom, 2, 0),
2025 ADD_TEST (recvmsg, 2, 0),
2026 ADD_TEST (open, 2, 1),
2027 ADD_TEST (close, 2, 1),
2028 ADD_TEST (pread, 2, 1),
2029 ADD_TEST (pwrite, 2, 1),
2030 ADD_TEST (fsync, 2, 1),
2031 ADD_TEST (msync, 2, 1),
2032 ADD_TEST (sendto, 2, 1),
2033 ADD_TEST (sendmsg, 2, 1),
2034 ADD_TEST (creat, 2, 1),
2035 ADD_TEST (connect, 2, 1),
2036 ADD_TEST (tcdrain, 2, 1),
2037 ADD_TEST (msgrcv, 2, 0),
2038 ADD_TEST (msgsnd, 2, 1),
2040 #define ntest_tf (sizeof (tests) / sizeof (tests[0]))
2043 static int
2044 do_test (void)
2046 if (pipe (fds) != 0)
2048 puts ("pipe failed");
2049 exit (1);
2052 int result = 0;
2053 size_t cnt;
2054 for (cnt = 0; cnt < ntest_tf; ++cnt)
2056 if (tests[cnt].only_early)
2057 continue;
2059 if (pthread_barrier_init (&b2, NULL, tests[cnt].nb) != 0)
2061 puts ("b2 init failed");
2062 exit (1);
2065 /* Reset the counter for the cleanup handler. */
2066 cl_called = 0;
2068 pthread_t th;
2069 if (pthread_create (&th, NULL, tests[cnt].tf, NULL) != 0)
2071 printf ("create for '%s' test failed\n", tests[cnt].name);
2072 result = 1;
2073 continue;
2076 int r = pthread_barrier_wait (&b2);
2077 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
2079 printf ("%s: barrier_wait failed\n", __FUNCTION__);
2080 result = 1;
2081 continue;
2084 struct timespec ts = { .tv_sec = 0, .tv_nsec = 100000000 };
2085 while (nanosleep (&ts, &ts) != 0)
2086 continue;
2088 if (pthread_cancel (th) != 0)
2090 printf ("cancel for '%s' failed\n", tests[cnt].name);
2091 result = 1;
2092 continue;
2095 void *status;
2096 if (pthread_join (th, &status) != 0)
2098 printf ("join for '%s' failed\n", tests[cnt].name);
2099 result = 1;
2100 continue;
2102 if (status != PTHREAD_CANCELED)
2104 printf ("thread for '%s' not canceled\n", tests[cnt].name);
2105 result = 1;
2106 continue;
2109 if (pthread_barrier_destroy (&b2) != 0)
2111 puts ("barrier_destroy failed");
2112 result = 1;
2113 continue;
2116 if (cl_called == 0)
2118 printf ("cleanup handler not called for '%s'\n", tests[cnt].name);
2119 result = 1;
2120 continue;
2122 if (cl_called > 1)
2124 printf ("cleanup handler called more than once for '%s'\n",
2125 tests[cnt].name);
2126 result = 1;
2127 continue;
2130 printf ("in-time cancel test of '%s' successful\n", tests[cnt].name);
2132 if (tempfd != -1)
2134 close (tempfd);
2135 tempfd = -1;
2137 if (tempfd2 != -1)
2139 close (tempfd2);
2140 tempfd2 = -1;
2142 if (tempfname != NULL)
2144 unlink (tempfname);
2145 free (tempfname);
2146 tempfname = NULL;
2148 if (tempmsg != -1)
2150 msgctl (tempmsg, IPC_RMID, NULL);
2151 tempmsg = -1;
2155 for (cnt = 0; cnt < ntest_tf; ++cnt)
2157 if (pthread_barrier_init (&b2, NULL, tests[cnt].nb) != 0)
2159 puts ("b2 init failed");
2160 exit (1);
2163 /* Reset the counter for the cleanup handler. */
2164 cl_called = 0;
2166 pthread_t th;
2167 if (pthread_create (&th, NULL, tests[cnt].tf, (void *) 1l) != 0)
2169 printf ("create for '%s' test failed\n", tests[cnt].name);
2170 result = 1;
2171 continue;
2174 int r = pthread_barrier_wait (&b2);
2175 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
2177 printf ("%s: barrier_wait failed\n", __FUNCTION__);
2178 result = 1;
2179 continue;
2182 if (pthread_cancel (th) != 0)
2184 printf ("cancel for '%s' failed\n", tests[cnt].name);
2185 result = 1;
2186 continue;
2189 r = pthread_barrier_wait (&b2);
2190 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
2192 printf ("%s: barrier_wait failed\n", __FUNCTION__);
2193 result = 1;
2194 continue;
2197 void *status;
2198 if (pthread_join (th, &status) != 0)
2200 printf ("join for '%s' failed\n", tests[cnt].name);
2201 result = 1;
2202 continue;
2204 if (status != PTHREAD_CANCELED)
2206 printf ("thread for '%s' not canceled\n", tests[cnt].name);
2207 result = 1;
2208 continue;
2211 if (pthread_barrier_destroy (&b2) != 0)
2213 puts ("barrier_destroy failed");
2214 result = 1;
2215 continue;
2218 if (cl_called == 0)
2220 printf ("cleanup handler not called for '%s'\n", tests[cnt].name);
2221 result = 1;
2222 continue;
2224 if (cl_called > 1)
2226 printf ("cleanup handler called more than once for '%s'\n",
2227 tests[cnt].name);
2228 result = 1;
2229 continue;
2232 printf ("early cancel test of '%s' successful\n", tests[cnt].name);
2234 if (tempfd != -1)
2236 close (tempfd);
2237 tempfd = -1;
2239 if (tempfd2 != -1)
2241 close (tempfd2);
2242 tempfd2 = -1;
2244 if (tempfname != NULL)
2246 unlink (tempfname);
2247 free (tempfname);
2248 tempfname = NULL;
2250 if (tempmsg != -1)
2252 msgctl (tempmsg, IPC_RMID, NULL);
2253 tempmsg = -1;
2257 return result;
2260 #define TIMEOUT 60
2261 #define TEST_FUNCTION do_test ()
2262 #include "../test-skeleton.c"