(_IO_new_file_fopen): If ,ccs= is given, also set vtable to the wide vtable.
[glibc.git] / nptl / tst-cancel4.c
blobcb7619688e26a8e414f0e04309e910cad95d4d39
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 #define WRITE_BUFFER_SIZE 4096
89 /* Cleanup handling test. */
90 static int cl_called;
92 static void
93 cl (void *arg)
95 ++cl_called;
100 static void *
101 tf_read (void *arg)
103 int fd;
104 int r;
106 if (arg == NULL)
107 fd = fds[0];
108 else
110 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
111 tempfd = fd = mkstemp (fname);
112 if (fd == -1)
113 printf ("%s: mkstemp failed\n", __FUNCTION__);
114 unlink (fname);
116 r = pthread_barrier_wait (&b2);
117 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
119 printf ("%s: barrier_wait failed\n", __FUNCTION__);
120 exit (1);
124 r = pthread_barrier_wait (&b2);
125 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
127 printf ("%s: barrier_wait failed\n", __FUNCTION__);
128 exit (1);
131 ssize_t s;
132 pthread_cleanup_push (cl, NULL);
134 char buf[100];
135 s = read (fd, buf, sizeof (buf));
137 pthread_cleanup_pop (0);
139 printf ("%s: read returns with %zd\n", __FUNCTION__, s);
141 exit (1);
145 static void *
146 tf_readv (void *arg)
148 int fd;
149 int r;
151 if (arg == NULL)
152 fd = fds[0];
153 else
155 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
156 tempfd = fd = mkstemp (fname);
157 if (fd == -1)
158 printf ("%s: mkstemp failed\n", __FUNCTION__);
159 unlink (fname);
161 r = pthread_barrier_wait (&b2);
162 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
164 printf ("%s: barrier_wait failed\n", __FUNCTION__);
165 exit (1);
169 r = pthread_barrier_wait (&b2);
170 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
172 printf ("%s: barrier_wait failed\n", __FUNCTION__);
173 exit (1);
176 ssize_t s;
177 pthread_cleanup_push (cl, NULL);
179 char buf[100];
180 struct iovec iov[1] = { [0] = { .iov_base = buf, .iov_len = sizeof (buf) } };
181 s = readv (fd, iov, 1);
183 pthread_cleanup_pop (0);
185 printf ("%s: readv returns with %zd\n", __FUNCTION__, s);
187 exit (1);
191 static void *
192 tf_write (void *arg)
194 int fd;
195 int r;
197 if (arg == NULL)
198 fd = fds[1];
199 else
201 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
202 tempfd = fd = mkstemp (fname);
203 if (fd == -1)
204 printf ("%s: mkstemp failed\n", __FUNCTION__);
205 unlink (fname);
207 r = pthread_barrier_wait (&b2);
208 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
210 printf ("%s: barrier_wait failed\n", __FUNCTION__);
211 exit (1);
215 r = pthread_barrier_wait (&b2);
216 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
218 printf ("%s: barrier_wait failed\n", __FUNCTION__);
219 exit (1);
222 ssize_t s;
223 pthread_cleanup_push (cl, NULL);
225 char buf[WRITE_BUFFER_SIZE];
226 memset (buf, '\0', sizeof (buf));
227 s = write (fd, buf, sizeof (buf));
229 pthread_cleanup_pop (0);
231 printf ("%s: write returns with %zd\n", __FUNCTION__, s);
233 exit (1);
237 static void *
238 tf_writev (void *arg)
240 int fd;
241 int r;
243 if (arg == NULL)
244 fd = fds[1];
245 else
247 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
248 tempfd = fd = mkstemp (fname);
249 if (fd == -1)
250 printf ("%s: mkstemp failed\n", __FUNCTION__);
251 unlink (fname);
253 r = pthread_barrier_wait (&b2);
254 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
256 printf ("%s: barrier_wait failed\n", __FUNCTION__);
257 exit (1);
261 r = pthread_barrier_wait (&b2);
262 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
264 printf ("%s: barrier_wait failed\n", __FUNCTION__);
265 exit (1);
268 ssize_t s;
269 pthread_cleanup_push (cl, NULL);
271 char buf[WRITE_BUFFER_SIZE];
272 memset (buf, '\0', sizeof (buf));
273 struct iovec iov[1] = { [0] = { .iov_base = buf, .iov_len = sizeof (buf) } };
274 s = writev (fd, iov, 1);
276 pthread_cleanup_pop (0);
278 printf ("%s: writev returns with %zd\n", __FUNCTION__, s);
280 exit (1);
284 static void *
285 tf_sleep (void *arg)
287 int r = pthread_barrier_wait (&b2);
288 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
290 printf ("%s: barrier_wait failed\n", __FUNCTION__);
291 exit (1);
294 if (arg != NULL)
296 r = pthread_barrier_wait (&b2);
297 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
299 printf ("%s: barrier_wait failed\n", __FUNCTION__);
300 exit (1);
304 pthread_cleanup_push (cl, NULL);
306 sleep (arg == NULL ? 1000000 : 0);
308 pthread_cleanup_pop (0);
310 printf ("%s: sleep returns\n", __FUNCTION__);
312 exit (1);
316 static void *
317 tf_usleep (void *arg)
319 int r = pthread_barrier_wait (&b2);
320 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
322 printf ("%s: barrier_wait failed\n", __FUNCTION__);
323 exit (1);
326 if (arg != NULL)
328 r = pthread_barrier_wait (&b2);
329 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
331 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
332 exit (1);
336 pthread_cleanup_push (cl, NULL);
338 usleep (arg == NULL ? (useconds_t) ULONG_MAX : 0);
340 pthread_cleanup_pop (0);
342 printf ("%s: usleep returns\n", __FUNCTION__);
344 exit (1);
348 static void *
349 tf_nanosleep (void *arg)
351 int r = pthread_barrier_wait (&b2);
352 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
354 printf ("%s: barrier_wait failed\n", __FUNCTION__);
355 exit (1);
358 if (arg != NULL)
360 r = pthread_barrier_wait (&b2);
361 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
363 printf ("%s: barrier_wait failed\n", __FUNCTION__);
364 exit (1);
368 pthread_cleanup_push (cl, NULL);
370 struct timespec ts = { .tv_sec = arg == NULL ? 10000000 : 0, .tv_nsec = 0 };
371 TEMP_FAILURE_RETRY (nanosleep (&ts, &ts));
373 pthread_cleanup_pop (0);
375 printf ("%s: nanosleep returns\n", __FUNCTION__);
377 exit (1);
381 static void *
382 tf_select (void *arg)
384 int fd;
385 int r;
387 if (arg == NULL)
388 fd = fds[0];
389 else
391 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
392 tempfd = fd = mkstemp (fname);
393 if (fd == -1)
394 printf ("%s: mkstemp failed\n", __FUNCTION__);
395 unlink (fname);
397 r = pthread_barrier_wait (&b2);
398 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
400 printf ("%s: barrier_wait failed\n", __FUNCTION__);
401 exit (1);
405 r = pthread_barrier_wait (&b2);
406 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
408 printf ("%s: barrier_wait failed\n", __FUNCTION__);
409 exit (1);
412 fd_set rfs;
413 FD_ZERO (&rfs);
414 FD_SET (fd, &rfs);
416 int s;
417 pthread_cleanup_push (cl, NULL);
419 s = select (fd + 1, &rfs, NULL, NULL, NULL);
421 pthread_cleanup_pop (0);
423 printf ("%s: select returns with %d (%s)\n", __FUNCTION__, s,
424 strerror (errno));
426 exit (1);
430 static void *
431 tf_pselect (void *arg)
433 int fd;
434 int r;
436 if (arg == NULL)
437 fd = fds[0];
438 else
440 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
441 tempfd = fd = mkstemp (fname);
442 if (fd == -1)
443 printf ("%s: mkstemp failed\n", __FUNCTION__);
444 unlink (fname);
446 r = pthread_barrier_wait (&b2);
447 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
449 printf ("%s: barrier_wait failed\n", __FUNCTION__);
450 exit (1);
454 r = pthread_barrier_wait (&b2);
455 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
457 printf ("%s: barrier_wait failed\n", __FUNCTION__);
458 exit (1);
461 fd_set rfs;
462 FD_ZERO (&rfs);
463 FD_SET (fd, &rfs);
465 int s;
466 pthread_cleanup_push (cl, NULL);
468 s = pselect (fd + 1, &rfs, NULL, NULL, NULL, NULL);
470 pthread_cleanup_pop (0);
472 printf ("%s: pselect returns with %d (%s)\n", __FUNCTION__, s,
473 strerror (errno));
475 exit (1);
479 static void *
480 tf_poll (void *arg)
482 int fd;
483 int r;
485 if (arg == NULL)
486 fd = fds[0];
487 else
489 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
490 tempfd = fd = mkstemp (fname);
491 if (fd == -1)
492 printf ("%s: mkstemp failed\n", __FUNCTION__);
493 unlink (fname);
495 r = pthread_barrier_wait (&b2);
496 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
498 printf ("%s: barrier_wait failed\n", __FUNCTION__);
499 exit (1);
503 r = pthread_barrier_wait (&b2);
504 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
506 printf ("%s: barrier_wait failed\n", __FUNCTION__);
507 exit (1);
510 struct pollfd rfs[1] = { [0] = { .fd = fd, .events = POLLIN } };
512 int s;
513 pthread_cleanup_push (cl, NULL);
515 s = poll (rfs, 1, -1);
517 pthread_cleanup_pop (0);
519 printf ("%s: poll returns with %d (%s)\n", __FUNCTION__, s,
520 strerror (errno));
522 exit (1);
526 static void *
527 tf_wait (void *arg)
529 pid_t pid = fork ();
530 if (pid == -1)
532 puts ("fork failed");
533 exit (1);
536 if (pid == 0)
538 /* Make the program disappear after a while. */
539 if (arg == NULL)
540 sleep (10);
541 exit (0);
544 int r;
545 if (arg != NULL)
547 struct timespec ts = { .tv_sec = 0, .tv_nsec = 100000000 };
548 while (nanosleep (&ts, &ts) != 0)
549 continue;
551 r = pthread_barrier_wait (&b2);
552 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
554 printf ("%s: barrier_wait failed\n", __FUNCTION__);
555 exit (1);
559 r = pthread_barrier_wait (&b2);
560 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
562 printf ("%s: barrier_wait failed\n", __FUNCTION__);
563 exit (1);
566 int s;
567 pthread_cleanup_push (cl, NULL);
569 s = wait (NULL);
571 pthread_cleanup_pop (0);
573 printf ("%s: wait returns with %d (%s)\n", __FUNCTION__, s,
574 strerror (errno));
576 exit (1);
580 static void *
581 tf_waitpid (void *arg)
584 pid_t pid = fork ();
585 if (pid == -1)
587 puts ("fork failed");
588 exit (1);
591 if (pid == 0)
593 /* Make the program disappear after a while. */
594 if (arg == NULL)
595 sleep (10);
596 exit (0);
599 int r;
600 if (arg != NULL)
602 struct timespec ts = { .tv_sec = 0, .tv_nsec = 100000000 };
603 while (nanosleep (&ts, &ts) != 0)
604 continue;
606 r = pthread_barrier_wait (&b2);
607 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
609 printf ("%s: barrier_wait failed\n", __FUNCTION__);
610 exit (1);
614 r = pthread_barrier_wait (&b2);
615 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
617 printf ("%s: barrier_wait failed\n", __FUNCTION__);
618 exit (1);
621 int s;
622 pthread_cleanup_push (cl, NULL);
624 s = waitpid (-1, NULL, 0);
626 pthread_cleanup_pop (0);
628 printf ("%s: waitpid returns with %d (%s)\n", __FUNCTION__, s,
629 strerror (errno));
631 exit (1);
635 static void *
636 tf_waitid (void *arg)
638 pid_t pid = fork ();
639 if (pid == -1)
641 puts ("fork failed");
642 exit (1);
645 if (pid == 0)
647 /* Make the program disappear after a while. */
648 if (arg == NULL)
649 sleep (10);
650 exit (0);
653 int r;
654 if (arg != NULL)
656 struct timespec ts = { .tv_sec = 0, .tv_nsec = 100000000 };
657 while (nanosleep (&ts, &ts) != 0)
658 continue;
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);
668 r = pthread_barrier_wait (&b2);
669 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
671 printf ("%s: barrier_wait failed\n", __FUNCTION__);
672 exit (1);
675 int s;
676 pthread_cleanup_push (cl, NULL);
678 #ifndef WEXITED
679 # define WEXITED 0
680 #endif
681 siginfo_t si;
682 s = waitid (P_PID, pid, &si, WEXITED);
684 pthread_cleanup_pop (0);
686 printf ("%s: waitid returns with %d (%s)\n", __FUNCTION__, s,
687 strerror (errno));
689 exit (1);
693 static void *
694 tf_sigpause (void *arg)
696 int r = pthread_barrier_wait (&b2);
697 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
699 printf ("%s: barrier_wait failed\n", __FUNCTION__);
700 exit (1);
703 if (arg != NULL)
705 r = pthread_barrier_wait (&b2);
706 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
708 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
709 exit (1);
713 pthread_cleanup_push (cl, NULL);
715 /* Just for fun block the cancellation signal. We need to use
716 __xpg_sigpause since otherwise we will get the BSD version. */
717 __xpg_sigpause (SIGCANCEL);
719 pthread_cleanup_pop (0);
721 printf ("%s: sigpause returned\n", __FUNCTION__);
723 exit (1);
727 static void *
728 tf_sigsuspend (void *arg)
730 int r = pthread_barrier_wait (&b2);
731 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
733 printf ("%s: barrier_wait failed\n", __FUNCTION__);
734 exit (1);
737 if (arg != NULL)
739 r = pthread_barrier_wait (&b2);
740 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
742 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
743 exit (1);
747 pthread_cleanup_push (cl, NULL);
749 /* Just for fun block all signals. */
750 sigset_t mask;
751 sigfillset (&mask);
752 sigsuspend (&mask);
754 pthread_cleanup_pop (0);
756 printf ("%s: sigsuspend returned\n", __FUNCTION__);
758 exit (1);
762 static void *
763 tf_sigwait (void *arg)
765 int r = pthread_barrier_wait (&b2);
766 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
768 printf ("%s: barrier_wait failed\n", __FUNCTION__);
769 exit (1);
772 if (arg != NULL)
774 r = pthread_barrier_wait (&b2);
775 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
777 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
778 exit (1);
782 /* Block SIGUSR1. */
783 sigset_t mask;
784 sigemptyset (&mask);
785 sigaddset (&mask, SIGUSR1);
786 if (pthread_sigmask (SIG_BLOCK, &mask, NULL) != 0)
788 printf ("%s: pthread_sigmask failed\n", __FUNCTION__);
789 exit (1);
792 int sig;
793 pthread_cleanup_push (cl, NULL);
795 /* Wait for SIGUSR1. */
796 sigwait (&mask, &sig);
798 pthread_cleanup_pop (0);
800 printf ("%s: sigwait returned with signal %d\n", __FUNCTION__, sig);
802 exit (1);
806 static void *
807 tf_sigwaitinfo (void *arg)
809 int r = pthread_barrier_wait (&b2);
810 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
812 printf ("%s: barrier_wait failed\n", __FUNCTION__);
813 exit (1);
816 if (arg != NULL)
818 r = pthread_barrier_wait (&b2);
819 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
821 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
822 exit (1);
826 /* Block SIGUSR1. */
827 sigset_t mask;
828 sigemptyset (&mask);
829 sigaddset (&mask, SIGUSR1);
830 if (pthread_sigmask (SIG_BLOCK, &mask, NULL) != 0)
832 printf ("%s: pthread_sigmask failed\n", __FUNCTION__);
833 exit (1);
836 siginfo_t info;
837 pthread_cleanup_push (cl, NULL);
839 /* Wait for SIGUSR1. */
840 sigwaitinfo (&mask, &info);
842 pthread_cleanup_pop (0);
844 printf ("%s: sigwaitinfo returned with signal %d\n", __FUNCTION__,
845 info.si_signo);
847 exit (1);
851 static void *
852 tf_sigtimedwait (void *arg)
854 int r = pthread_barrier_wait (&b2);
855 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
857 printf ("%s: barrier_wait failed\n", __FUNCTION__);
858 exit (1);
861 if (arg != NULL)
863 r = pthread_barrier_wait (&b2);
864 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
866 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
867 exit (1);
871 /* Block SIGUSR1. */
872 sigset_t mask;
873 sigemptyset (&mask);
874 sigaddset (&mask, SIGUSR1);
875 if (pthread_sigmask (SIG_BLOCK, &mask, NULL) != 0)
877 printf ("%s: pthread_sigmask failed\n", __FUNCTION__);
878 exit (1);
881 /* Wait for SIGUSR1. */
882 siginfo_t info;
883 struct timespec ts = { .tv_sec = 60, .tv_nsec = 0 };
884 pthread_cleanup_push (cl, NULL);
886 sigtimedwait (&mask, &info, &ts);
888 pthread_cleanup_pop (0);
890 printf ("%s: sigtimedwait returned with signal %d\n", __FUNCTION__,
891 info.si_signo);
893 exit (1);
897 static void *
898 tf_pause (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 pthread_cleanup_push (cl, NULL);
919 pause ();
921 pthread_cleanup_pop (0);
923 printf ("%s: pause returned\n", __FUNCTION__);
925 exit (1);
929 static void *
930 tf_accept (void *arg)
932 struct sockaddr_un sun;
933 /* To test a non-blocking accept call we make the call file by using
934 a datagrame socket. */
935 int pf = arg == NULL ? SOCK_STREAM : SOCK_DGRAM;
937 tempfd = socket (AF_UNIX, pf, 0);
938 if (tempfd == -1)
940 printf ("%s: socket call failed\n", __FUNCTION__);
941 exit (1);
944 int tries = 0;
947 if (++tries > 10)
949 printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
952 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-1-XXXXXX");
953 if (mktemp (sun.sun_path) == NULL)
955 printf ("%s: cannot generate temp file name\n", __FUNCTION__);
956 exit (1);
959 sun.sun_family = AF_UNIX;
961 while (bind (tempfd, (struct sockaddr *) &sun,
962 offsetof (struct sockaddr_un, sun_path)
963 + strlen (sun.sun_path) + 1) != 0);
965 unlink (sun.sun_path);
967 listen (tempfd, 5);
969 socklen_t len = sizeof (sun);
971 int r = pthread_barrier_wait (&b2);
972 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
974 printf ("%s: barrier_wait failed\n", __FUNCTION__);
975 exit (1);
978 if (arg != NULL)
980 r = pthread_barrier_wait (&b2);
981 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
983 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
984 exit (1);
988 pthread_cleanup_push (cl, NULL);
990 accept (tempfd, (struct sockaddr *) &sun, &len);
992 pthread_cleanup_pop (0);
994 printf ("%s: accept returned\n", __FUNCTION__);
996 exit (1);
1000 static void *
1001 tf_send (void *arg)
1003 struct sockaddr_un sun;
1005 tempfd = socket (AF_UNIX, SOCK_STREAM, 0);
1006 if (tempfd == -1)
1008 printf ("%s: first socket call failed\n", __FUNCTION__);
1009 exit (1);
1012 int tries = 0;
1015 if (++tries > 10)
1017 printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
1020 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-2-XXXXXX");
1021 if (mktemp (sun.sun_path) == NULL)
1023 printf ("%s: cannot generate temp file name\n", __FUNCTION__);
1024 exit (1);
1027 sun.sun_family = AF_UNIX;
1029 while (bind (tempfd, (struct sockaddr *) &sun,
1030 offsetof (struct sockaddr_un, sun_path)
1031 + strlen (sun.sun_path) + 1) != 0);
1033 listen (tempfd, 5);
1035 tempfd2 = socket (AF_UNIX, SOCK_STREAM, 0);
1036 if (tempfd2 == -1)
1038 printf ("%s: second socket call failed\n", __FUNCTION__);
1039 exit (1);
1042 if (connect (tempfd2, (struct sockaddr *) &sun, sizeof (sun)) != 0)
1044 printf ("%s: connect failed\n", __FUNCTION__);
1045 exit(1);
1048 unlink (sun.sun_path);
1050 int r = pthread_barrier_wait (&b2);
1051 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1053 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1054 exit (1);
1057 if (arg != NULL)
1059 r = pthread_barrier_wait (&b2);
1060 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1062 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1063 exit (1);
1067 pthread_cleanup_push (cl, NULL);
1069 /* Very large block, so that the send call blocks. */
1070 char mem[700000];
1072 send (tempfd2, mem, arg == NULL ? sizeof (mem) : 1, 0);
1074 pthread_cleanup_pop (0);
1076 printf ("%s: send returned\n", __FUNCTION__);
1078 exit (1);
1082 static void *
1083 tf_recv (void *arg)
1085 struct sockaddr_un sun;
1087 tempfd = socket (AF_UNIX, SOCK_STREAM, 0);
1088 if (tempfd == -1)
1090 printf ("%s: first socket call failed\n", __FUNCTION__);
1091 exit (1);
1094 int tries = 0;
1097 if (++tries > 10)
1099 printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
1102 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-3-XXXXXX");
1103 if (mktemp (sun.sun_path) == NULL)
1105 printf ("%s: cannot generate temp file name\n", __FUNCTION__);
1106 exit (1);
1109 sun.sun_family = AF_UNIX;
1111 while (bind (tempfd, (struct sockaddr *) &sun,
1112 offsetof (struct sockaddr_un, sun_path)
1113 + strlen (sun.sun_path) + 1) != 0);
1115 listen (tempfd, 5);
1117 tempfd2 = socket (AF_UNIX, SOCK_STREAM, 0);
1118 if (tempfd2 == -1)
1120 printf ("%s: second socket call failed\n", __FUNCTION__);
1121 exit (1);
1124 if (connect (tempfd2, (struct sockaddr *) &sun, sizeof (sun)) != 0)
1126 printf ("%s: connect failed\n", __FUNCTION__);
1127 exit(1);
1130 unlink (sun.sun_path);
1132 int r = pthread_barrier_wait (&b2);
1133 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1135 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1136 exit (1);
1139 if (arg != NULL)
1141 r = pthread_barrier_wait (&b2);
1142 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1144 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1145 exit (1);
1149 pthread_cleanup_push (cl, NULL);
1151 char mem[70];
1153 recv (tempfd2, mem, arg == NULL ? sizeof (mem) : 0, 0);
1155 pthread_cleanup_pop (0);
1157 printf ("%s: recv returned\n", __FUNCTION__);
1159 exit (1);
1163 static void *
1164 tf_recvfrom (void *arg)
1166 struct sockaddr_un sun;
1168 tempfd = socket (AF_UNIX, SOCK_DGRAM, 0);
1169 if (tempfd == -1)
1171 printf ("%s: first socket call failed\n", __FUNCTION__);
1172 exit (1);
1175 int tries = 0;
1178 if (++tries > 10)
1180 printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
1183 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-4-XXXXXX");
1184 if (mktemp (sun.sun_path) == NULL)
1186 printf ("%s: cannot generate temp file name\n", __FUNCTION__);
1187 exit (1);
1190 sun.sun_family = AF_UNIX;
1192 while (bind (tempfd, (struct sockaddr *) &sun,
1193 offsetof (struct sockaddr_un, sun_path)
1194 + strlen (sun.sun_path) + 1) != 0);
1196 tempfname = strdup (sun.sun_path);
1198 tempfd2 = socket (AF_UNIX, SOCK_DGRAM, 0);
1199 if (tempfd2 == -1)
1201 printf ("%s: second socket call failed\n", __FUNCTION__);
1202 exit (1);
1205 int r = pthread_barrier_wait (&b2);
1206 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1208 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1209 exit (1);
1212 if (arg != NULL)
1214 r = pthread_barrier_wait (&b2);
1215 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1217 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1218 exit (1);
1222 pthread_cleanup_push (cl, NULL);
1224 char mem[70];
1225 socklen_t len = sizeof (sun);
1227 recvfrom (tempfd2, mem, arg == NULL ? sizeof (mem) : 0, 0,
1228 (struct sockaddr *) &sun, &len);
1230 pthread_cleanup_pop (0);
1232 printf ("%s: recvfrom returned\n", __FUNCTION__);
1234 exit (1);
1238 static void *
1239 tf_recvmsg (void *arg)
1241 struct sockaddr_un sun;
1243 tempfd = socket (AF_UNIX, SOCK_DGRAM, 0);
1244 if (tempfd == -1)
1246 printf ("%s: first socket call failed\n", __FUNCTION__);
1247 exit (1);
1250 int tries = 0;
1253 if (++tries > 10)
1255 printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
1258 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-5-XXXXXX");
1259 if (mktemp (sun.sun_path) == NULL)
1261 printf ("%s: cannot generate temp file name\n", __FUNCTION__);
1262 exit (1);
1265 sun.sun_family = AF_UNIX;
1267 while (bind (tempfd, (struct sockaddr *) &sun,
1268 offsetof (struct sockaddr_un, sun_path)
1269 + strlen (sun.sun_path) + 1) != 0);
1271 tempfname = strdup (sun.sun_path);
1273 tempfd2 = socket (AF_UNIX, SOCK_DGRAM, 0);
1274 if (tempfd2 == -1)
1276 printf ("%s: second socket call failed\n", __FUNCTION__);
1277 exit (1);
1280 int r = pthread_barrier_wait (&b2);
1281 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1283 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1284 exit (1);
1287 if (arg != NULL)
1289 r = pthread_barrier_wait (&b2);
1290 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1292 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1293 exit (1);
1297 pthread_cleanup_push (cl, NULL);
1299 char mem[70];
1300 struct iovec iov[1];
1301 iov[0].iov_base = mem;
1302 iov[0].iov_len = arg == NULL ? sizeof (mem) : 0;
1304 struct msghdr m;
1305 m.msg_name = &sun;
1306 m.msg_namelen = sizeof (sun);
1307 m.msg_iov = iov;
1308 m.msg_iovlen = 1;
1309 m.msg_control = NULL;
1310 m.msg_controllen = 0;
1312 recvmsg (tempfd2, &m, 0);
1314 pthread_cleanup_pop (0);
1316 printf ("%s: recvmsg returned\n", __FUNCTION__);
1318 exit (1);
1322 static void *
1323 tf_open (void *arg)
1325 if (arg == NULL)
1326 // XXX If somebody can provide a portable test case in which open()
1327 // blocks we can enable this test to run in both rounds.
1328 abort ();
1330 int r = pthread_barrier_wait (&b2);
1331 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1333 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1334 exit (1);
1337 r = pthread_barrier_wait (&b2);
1338 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1340 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1341 exit (1);
1344 pthread_cleanup_push (cl, NULL);
1346 open ("Makefile", O_RDONLY);
1348 pthread_cleanup_pop (0);
1350 printf ("%s: open returned\n", __FUNCTION__);
1352 exit (1);
1356 static void *
1357 tf_close (void *arg)
1359 if (arg == NULL)
1360 // XXX If somebody can provide a portable test case in which close()
1361 // blocks we can enable this test to run in both rounds.
1362 abort ();
1364 char fname[] = "/tmp/tst-cancel-fd-XXXXXX";
1365 tempfd = mkstemp (fname);
1366 if (tempfd == -1)
1368 printf ("%s: mkstemp failed\n", __FUNCTION__);
1369 exit (1);
1371 unlink (fname);
1373 int r = pthread_barrier_wait (&b2);
1374 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1376 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1377 exit (1);
1380 r = pthread_barrier_wait (&b2);
1381 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1383 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1384 exit (1);
1387 pthread_cleanup_push (cl, NULL);
1389 close (tempfd);
1391 pthread_cleanup_pop (0);
1393 printf ("%s: close returned\n", __FUNCTION__);
1395 exit (1);
1399 static void *
1400 tf_pread (void *arg)
1402 if (arg == NULL)
1403 // XXX If somebody can provide a portable test case in which pread()
1404 // blocks we can enable this test to run in both rounds.
1405 abort ();
1407 tempfd = open ("Makefile", O_RDONLY);
1408 if (tempfd == -1)
1410 printf ("%s: cannot open Makefile\n", __FUNCTION__);
1411 exit (1);
1414 int r = pthread_barrier_wait (&b2);
1415 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1417 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1418 exit (1);
1421 r = pthread_barrier_wait (&b2);
1422 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1424 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1425 exit (1);
1428 pthread_cleanup_push (cl, NULL);
1430 char mem[10];
1431 pread (tempfd, mem, sizeof (mem), 0);
1433 pthread_cleanup_pop (0);
1435 printf ("%s: pread returned\n", __FUNCTION__);
1437 exit (1);
1441 static void *
1442 tf_pwrite (void *arg)
1444 if (arg == NULL)
1445 // XXX If somebody can provide a portable test case in which pwrite()
1446 // blocks we can enable this test to run in both rounds.
1447 abort ();
1449 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
1450 tempfd = mkstemp (fname);
1451 if (tempfd == -1)
1453 printf ("%s: mkstemp failed\n", __FUNCTION__);
1454 exit (1);
1456 unlink (fname);
1458 int r = pthread_barrier_wait (&b2);
1459 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1461 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1462 exit (1);
1465 r = pthread_barrier_wait (&b2);
1466 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1468 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1469 exit (1);
1472 pthread_cleanup_push (cl, NULL);
1474 char mem[10];
1475 pwrite (tempfd, mem, sizeof (mem), 0);
1477 pthread_cleanup_pop (0);
1479 printf ("%s: pwrite returned\n", __FUNCTION__);
1481 exit (1);
1485 static void *
1486 tf_fsync (void *arg)
1488 if (arg == NULL)
1489 // XXX If somebody can provide a portable test case in which fsync()
1490 // blocks we can enable this test to run in both rounds.
1491 abort ();
1493 tempfd = open ("Makefile", O_RDONLY);
1494 if (tempfd == -1)
1496 printf ("%s: cannot open Makefile\n", __FUNCTION__);
1497 exit (1);
1500 int r = pthread_barrier_wait (&b2);
1501 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1503 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1504 exit (1);
1507 r = pthread_barrier_wait (&b2);
1508 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1510 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1511 exit (1);
1514 pthread_cleanup_push (cl, NULL);
1516 fsync (tempfd);
1518 pthread_cleanup_pop (0);
1520 printf ("%s: fsync returned\n", __FUNCTION__);
1522 exit (1);
1526 static void *
1527 tf_msync (void *arg)
1529 if (arg == NULL)
1530 // XXX If somebody can provide a portable test case in which msync()
1531 // blocks we can enable this test to run in both rounds.
1532 abort ();
1534 tempfd = open ("Makefile", O_RDONLY);
1535 if (tempfd == -1)
1537 printf ("%s: cannot open Makefile\n", __FUNCTION__);
1538 exit (1);
1540 void *p = mmap (NULL, 10, PROT_READ, MAP_SHARED, tempfd, 0);
1541 if (p == MAP_FAILED)
1543 printf ("%s: mmap failed\n", __FUNCTION__);
1544 exit (1);
1547 int r = pthread_barrier_wait (&b2);
1548 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1550 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1551 exit (1);
1554 r = pthread_barrier_wait (&b2);
1555 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1557 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1558 exit (1);
1561 pthread_cleanup_push (cl, NULL);
1563 msync (p, 10, 0);
1565 pthread_cleanup_pop (0);
1567 printf ("%s: msync returned\n", __FUNCTION__);
1569 exit (1);
1573 static void *
1574 tf_sendto (void *arg)
1576 if (arg == NULL)
1577 // XXX If somebody can provide a portable test case in which sendto()
1578 // blocks we can enable this test to run in both rounds.
1579 abort ();
1581 struct sockaddr_un sun;
1583 tempfd = socket (AF_UNIX, SOCK_DGRAM, 0);
1584 if (tempfd == -1)
1586 printf ("%s: first socket call failed\n", __FUNCTION__);
1587 exit (1);
1590 int tries = 0;
1593 if (++tries > 10)
1595 printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
1598 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-6-XXXXXX");
1599 if (mktemp (sun.sun_path) == NULL)
1601 printf ("%s: cannot generate temp file name\n", __FUNCTION__);
1602 exit (1);
1605 sun.sun_family = AF_UNIX;
1607 while (bind (tempfd, (struct sockaddr *) &sun,
1608 offsetof (struct sockaddr_un, sun_path)
1609 + strlen (sun.sun_path) + 1) != 0);
1610 tempfname = strdup (sun.sun_path);
1612 tempfd2 = socket (AF_UNIX, SOCK_DGRAM, 0);
1613 if (tempfd2 == -1)
1615 printf ("%s: second socket call failed\n", __FUNCTION__);
1616 exit (1);
1619 int r = pthread_barrier_wait (&b2);
1620 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1622 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1623 exit (1);
1626 r = pthread_barrier_wait (&b2);
1627 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1629 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1630 exit (1);
1633 pthread_cleanup_push (cl, NULL);
1635 char mem[1];
1637 sendto (tempfd2, mem, arg == NULL ? sizeof (mem) : 1, 0,
1638 (struct sockaddr *) &sun,
1639 offsetof (struct sockaddr_un, sun_path) + strlen (sun.sun_path) + 1);
1641 pthread_cleanup_pop (0);
1643 printf ("%s: sendto returned\n", __FUNCTION__);
1645 exit (1);
1649 static void *
1650 tf_sendmsg (void *arg)
1652 if (arg == NULL)
1653 // XXX If somebody can provide a portable test case in which sendmsg()
1654 // blocks we can enable this test to run in both rounds.
1655 abort ();
1657 struct sockaddr_un sun;
1659 tempfd = socket (AF_UNIX, SOCK_DGRAM, 0);
1660 if (tempfd == -1)
1662 printf ("%s: first socket call failed\n", __FUNCTION__);
1663 exit (1);
1666 int tries = 0;
1669 if (++tries > 10)
1671 printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
1674 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-7-XXXXXX");
1675 if (mktemp (sun.sun_path) == NULL)
1677 printf ("%s: cannot generate temp file name\n", __FUNCTION__);
1678 exit (1);
1681 sun.sun_family = AF_UNIX;
1683 while (bind (tempfd, (struct sockaddr *) &sun,
1684 offsetof (struct sockaddr_un, sun_path)
1685 + strlen (sun.sun_path) + 1) != 0);
1686 tempfname = strdup (sun.sun_path);
1688 tempfd2 = socket (AF_UNIX, SOCK_DGRAM, 0);
1689 if (tempfd2 == -1)
1691 printf ("%s: second socket call failed\n", __FUNCTION__);
1692 exit (1);
1695 int r = pthread_barrier_wait (&b2);
1696 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1698 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1699 exit (1);
1702 r = pthread_barrier_wait (&b2);
1703 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1705 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1706 exit (1);
1709 pthread_cleanup_push (cl, NULL);
1711 char mem[1];
1712 struct iovec iov[1];
1713 iov[0].iov_base = mem;
1714 iov[0].iov_len = 1;
1716 struct msghdr m;
1717 m.msg_name = &sun;
1718 m.msg_namelen = (offsetof (struct sockaddr_un, sun_path)
1719 + strlen (sun.sun_path) + 1);
1720 m.msg_iov = iov;
1721 m.msg_iovlen = 1;
1722 m.msg_control = NULL;
1723 m.msg_controllen = 0;
1725 sendmsg (tempfd2, &m, 0);
1727 pthread_cleanup_pop (0);
1729 printf ("%s: sendmsg returned\n", __FUNCTION__);
1731 exit (1);
1735 static void *
1736 tf_creat (void *arg)
1738 if (arg == NULL)
1739 // XXX If somebody can provide a portable test case in which sendmsg()
1740 // blocks we can enable this test to run in both rounds.
1741 abort ();
1743 int r = pthread_barrier_wait (&b2);
1744 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1746 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1747 exit (1);
1750 r = pthread_barrier_wait (&b2);
1751 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1753 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1754 exit (1);
1757 pthread_cleanup_push (cl, NULL);
1759 creat ("tmp/tst-cancel-4-should-not-exist", 0666);
1761 pthread_cleanup_pop (0);
1763 printf ("%s: creat returned\n", __FUNCTION__);
1765 exit (1);
1769 static void *
1770 tf_connect (void *arg)
1772 if (arg == NULL)
1773 // XXX If somebody can provide a portable test case in which connect()
1774 // blocks we can enable this test to run in both rounds.
1775 abort ();
1777 struct sockaddr_un sun;
1779 tempfd = socket (AF_UNIX, SOCK_STREAM, 0);
1780 if (tempfd == -1)
1782 printf ("%s: first socket call failed\n", __FUNCTION__);
1783 exit (1);
1786 int tries = 0;
1789 if (++tries > 10)
1791 printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
1794 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-2-XXXXXX");
1795 if (mktemp (sun.sun_path) == NULL)
1797 printf ("%s: cannot generate temp file name\n", __FUNCTION__);
1798 exit (1);
1801 sun.sun_family = AF_UNIX;
1803 while (bind (tempfd, (struct sockaddr *) &sun,
1804 offsetof (struct sockaddr_un, sun_path)
1805 + strlen (sun.sun_path) + 1) != 0);
1806 tempfname = strdup (sun.sun_path);
1808 listen (tempfd, 5);
1810 tempfd2 = socket (AF_UNIX, SOCK_STREAM, 0);
1811 if (tempfd2 == -1)
1813 printf ("%s: second socket call failed\n", __FUNCTION__);
1814 exit (1);
1817 int r = pthread_barrier_wait (&b2);
1818 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1820 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1821 exit (1);
1824 if (arg != NULL)
1826 r = pthread_barrier_wait (&b2);
1827 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1829 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1830 exit (1);
1834 pthread_cleanup_push (cl, NULL);
1836 connect (tempfd2, (struct sockaddr *) &sun, sizeof (sun));
1838 pthread_cleanup_pop (0);
1840 printf ("%s: connect returned\n", __FUNCTION__);
1842 exit (1);
1846 static void *
1847 tf_tcdrain (void *arg)
1849 if (arg == NULL)
1850 // XXX If somebody can provide a portable test case in which tcdrain()
1851 // blocks we can enable this test to run in both rounds.
1852 abort ();
1854 int r = pthread_barrier_wait (&b2);
1855 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1857 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1858 exit (1);
1861 if (arg != NULL)
1863 r = pthread_barrier_wait (&b2);
1864 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1866 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1867 exit (1);
1871 pthread_cleanup_push (cl, NULL);
1873 /* Regardless of stderr being a terminal, the tcdrain call should be
1874 canceled. */
1875 tcdrain (STDERR_FILENO);
1877 pthread_cleanup_pop (0);
1879 printf ("%s: tcdrain returned\n", __FUNCTION__);
1881 exit (1);
1885 static void *
1886 tf_msgrcv (void *arg)
1888 tempmsg = msgget (IPC_PRIVATE, 0666 | IPC_CREAT);
1889 if (tempmsg == -1)
1891 printf ("%s: msgget failed: %s\n", __FUNCTION__, strerror (errno));
1892 exit (1);
1895 int r = pthread_barrier_wait (&b2);
1896 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1898 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1899 exit (1);
1902 if (arg != NULL)
1904 r = pthread_barrier_wait (&b2);
1905 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1907 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1908 exit (1);
1912 ssize_t s;
1914 pthread_cleanup_push (cl, NULL);
1916 struct
1918 long int type;
1919 char mem[10];
1920 } m;
1921 int randnr;
1922 /* We need a positive random number. */
1924 randnr = random () % 64000;
1925 while (randnr <= 0);
1928 errno = 0;
1929 s = msgrcv (tempmsg, (struct msgbuf *) &m, 10, randnr, 0);
1931 while (errno == EIDRM || errno == EINTR);
1933 pthread_cleanup_pop (0);
1935 printf ("%s: msgrcv returned %zd with errno = %m\n", __FUNCTION__, s);
1937 msgctl (tempmsg, IPC_RMID, NULL);
1939 exit (1);
1943 static void *
1944 tf_msgsnd (void *arg)
1946 if (arg == NULL)
1947 // XXX If somebody can provide a portable test case in which msgsnd()
1948 // blocks we can enable this test to run in both rounds.
1949 abort ();
1951 tempmsg = msgget (IPC_PRIVATE, 0666 | IPC_CREAT);
1952 if (tempmsg == -1)
1954 printf ("%s: msgget failed: %s\n", __FUNCTION__, strerror (errno));
1955 exit (1);
1958 int r = pthread_barrier_wait (&b2);
1959 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1961 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1962 exit (1);
1965 r = pthread_barrier_wait (&b2);
1966 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1968 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1969 exit (1);
1972 pthread_cleanup_push (cl, NULL);
1974 struct
1976 long int type;
1977 char mem[1];
1978 } m;
1979 /* We need a positive random number. */
1981 m.type = random () % 64000;
1982 while (m.type <= 0);
1983 msgsnd (tempmsg, (struct msgbuf *) &m, sizeof (m.mem), 0);
1985 pthread_cleanup_pop (0);
1987 printf ("%s: msgsnd returned\n", __FUNCTION__);
1989 msgctl (tempmsg, IPC_RMID, NULL);
1991 exit (1);
1995 static struct
1997 const char *name;
1998 void *(*tf) (void *);
1999 int nb;
2000 int only_early;
2001 } tests[] =
2003 #define ADD_TEST(name, nbar, early) { #name, tf_##name, nbar, early }
2004 ADD_TEST (read, 2, 0),
2005 ADD_TEST (readv, 2, 0),
2006 ADD_TEST (select, 2, 0),
2007 ADD_TEST (pselect, 2, 0),
2008 ADD_TEST (poll, 2, 0),
2009 ADD_TEST (write, 2, 0),
2010 ADD_TEST (writev, 2, 0),
2011 ADD_TEST (sleep, 2, 0),
2012 ADD_TEST (usleep, 2, 0),
2013 ADD_TEST (nanosleep, 2, 0),
2014 ADD_TEST (wait, 2, 0),
2015 ADD_TEST (waitid, 2, 0),
2016 ADD_TEST (waitpid, 2, 0),
2017 ADD_TEST (sigpause, 2, 0),
2018 ADD_TEST (sigsuspend, 2, 0),
2019 ADD_TEST (sigwait, 2, 0),
2020 ADD_TEST (sigwaitinfo, 2, 0),
2021 ADD_TEST (sigtimedwait, 2, 0),
2022 ADD_TEST (pause, 2, 0),
2023 ADD_TEST (accept, 2, 0),
2024 ADD_TEST (send, 2, 0),
2025 ADD_TEST (recv, 2, 0),
2026 ADD_TEST (recvfrom, 2, 0),
2027 ADD_TEST (recvmsg, 2, 0),
2028 ADD_TEST (open, 2, 1),
2029 ADD_TEST (close, 2, 1),
2030 ADD_TEST (pread, 2, 1),
2031 ADD_TEST (pwrite, 2, 1),
2032 ADD_TEST (fsync, 2, 1),
2033 ADD_TEST (msync, 2, 1),
2034 ADD_TEST (sendto, 2, 1),
2035 ADD_TEST (sendmsg, 2, 1),
2036 ADD_TEST (creat, 2, 1),
2037 ADD_TEST (connect, 2, 1),
2038 ADD_TEST (tcdrain, 2, 1),
2039 ADD_TEST (msgrcv, 2, 0),
2040 ADD_TEST (msgsnd, 2, 1),
2042 #define ntest_tf (sizeof (tests) / sizeof (tests[0]))
2045 static int
2046 do_test (void)
2048 int val;
2049 socklen_t len;
2051 if (socketpair (AF_UNIX, SOCK_STREAM, PF_UNIX, fds) != 0)
2053 perror ("socketpair");
2054 exit (1);
2057 val = 1;
2058 len = sizeof(val);
2059 setsockopt (fds[1], SOL_SOCKET, SO_SNDBUF, &val, sizeof(val));
2060 if (getsockopt (fds[1], SOL_SOCKET, SO_SNDBUF, &val, &len) < 0)
2062 perror ("getsockopt");
2063 exit (1);
2065 if (val >= WRITE_BUFFER_SIZE)
2067 puts ("minimum write buffer size too large");
2068 exit (1);
2070 setsockopt (fds[1], SOL_SOCKET, SO_SNDBUF, &val, sizeof(val));
2072 int result = 0;
2073 size_t cnt;
2074 for (cnt = 0; cnt < ntest_tf; ++cnt)
2076 if (tests[cnt].only_early)
2077 continue;
2079 if (pthread_barrier_init (&b2, NULL, tests[cnt].nb) != 0)
2081 puts ("b2 init failed");
2082 exit (1);
2085 /* Reset the counter for the cleanup handler. */
2086 cl_called = 0;
2088 pthread_t th;
2089 if (pthread_create (&th, NULL, tests[cnt].tf, NULL) != 0)
2091 printf ("create for '%s' test failed\n", tests[cnt].name);
2092 result = 1;
2093 continue;
2096 int r = pthread_barrier_wait (&b2);
2097 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
2099 printf ("%s: barrier_wait failed\n", __FUNCTION__);
2100 result = 1;
2101 continue;
2104 struct timespec ts = { .tv_sec = 0, .tv_nsec = 100000000 };
2105 while (nanosleep (&ts, &ts) != 0)
2106 continue;
2108 if (pthread_cancel (th) != 0)
2110 printf ("cancel for '%s' failed\n", tests[cnt].name);
2111 result = 1;
2112 continue;
2115 void *status;
2116 if (pthread_join (th, &status) != 0)
2118 printf ("join for '%s' failed\n", tests[cnt].name);
2119 result = 1;
2120 continue;
2122 if (status != PTHREAD_CANCELED)
2124 printf ("thread for '%s' not canceled\n", tests[cnt].name);
2125 result = 1;
2126 continue;
2129 if (pthread_barrier_destroy (&b2) != 0)
2131 puts ("barrier_destroy failed");
2132 result = 1;
2133 continue;
2136 if (cl_called == 0)
2138 printf ("cleanup handler not called for '%s'\n", tests[cnt].name);
2139 result = 1;
2140 continue;
2142 if (cl_called > 1)
2144 printf ("cleanup handler called more than once for '%s'\n",
2145 tests[cnt].name);
2146 result = 1;
2147 continue;
2150 printf ("in-time cancel test of '%s' successful\n", tests[cnt].name);
2152 if (tempfd != -1)
2154 close (tempfd);
2155 tempfd = -1;
2157 if (tempfd2 != -1)
2159 close (tempfd2);
2160 tempfd2 = -1;
2162 if (tempfname != NULL)
2164 unlink (tempfname);
2165 free (tempfname);
2166 tempfname = NULL;
2168 if (tempmsg != -1)
2170 msgctl (tempmsg, IPC_RMID, NULL);
2171 tempmsg = -1;
2175 for (cnt = 0; cnt < ntest_tf; ++cnt)
2177 if (pthread_barrier_init (&b2, NULL, tests[cnt].nb) != 0)
2179 puts ("b2 init failed");
2180 exit (1);
2183 /* Reset the counter for the cleanup handler. */
2184 cl_called = 0;
2186 pthread_t th;
2187 if (pthread_create (&th, NULL, tests[cnt].tf, (void *) 1l) != 0)
2189 printf ("create for '%s' test failed\n", tests[cnt].name);
2190 result = 1;
2191 continue;
2194 int r = pthread_barrier_wait (&b2);
2195 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
2197 printf ("%s: barrier_wait failed\n", __FUNCTION__);
2198 result = 1;
2199 continue;
2202 if (pthread_cancel (th) != 0)
2204 printf ("cancel for '%s' failed\n", tests[cnt].name);
2205 result = 1;
2206 continue;
2209 r = pthread_barrier_wait (&b2);
2210 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
2212 printf ("%s: barrier_wait failed\n", __FUNCTION__);
2213 result = 1;
2214 continue;
2217 void *status;
2218 if (pthread_join (th, &status) != 0)
2220 printf ("join for '%s' failed\n", tests[cnt].name);
2221 result = 1;
2222 continue;
2224 if (status != PTHREAD_CANCELED)
2226 printf ("thread for '%s' not canceled\n", tests[cnt].name);
2227 result = 1;
2228 continue;
2231 if (pthread_barrier_destroy (&b2) != 0)
2233 puts ("barrier_destroy failed");
2234 result = 1;
2235 continue;
2238 if (cl_called == 0)
2240 printf ("cleanup handler not called for '%s'\n", tests[cnt].name);
2241 result = 1;
2242 continue;
2244 if (cl_called > 1)
2246 printf ("cleanup handler called more than once for '%s'\n",
2247 tests[cnt].name);
2248 result = 1;
2249 continue;
2252 printf ("early cancel test of '%s' successful\n", tests[cnt].name);
2254 if (tempfd != -1)
2256 close (tempfd);
2257 tempfd = -1;
2259 if (tempfd2 != -1)
2261 close (tempfd2);
2262 tempfd2 = -1;
2264 if (tempfname != NULL)
2266 unlink (tempfname);
2267 free (tempfname);
2268 tempfname = NULL;
2270 if (tempmsg != -1)
2272 msgctl (tempmsg, IPC_RMID, NULL);
2273 tempmsg = -1;
2277 return result;
2280 #define TIMEOUT 60
2281 #define TEST_FUNCTION do_test ()
2282 #include "../test-skeleton.c"