1 /* Copyright (C) 2002-2023 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
18 /* NOTE: this tests functionality beyond POSIX. POSIX does not allow
19 exit to be called more than once. */
25 #include <sys/types.h>
26 #include <sys/socket.h>
41 #include <libc-diag.h>
44 /* Since STREAMS are not supported in the standard Linux kernel and
45 there we don't advertise STREAMS as supported is no need to test
46 the STREAMS related functions. This affects
47 getmsg() getpmsg() putmsg()
50 lockf() and fcntl() are tested in tst-cancel16.
52 pthread_join() is tested in tst-join5.
54 pthread_testcancel()'s only purpose is to allow cancellation. This
55 is tested in several places.
57 sem_wait() and sem_timedwait() are checked in tst-cancel1[2345] tests.
59 mq_send(), mq_timedsend(), mq_receive() and mq_timedreceive() are checked
60 in tst-mqueue8{,x} tests.
62 aio_suspend() is tested in tst-cancel17.
64 clock_nanosleep() is tested in tst-cancel18.
66 Linux sendmmsg and recvmmsg are checked in tst-cancel4_1.c and
67 tst-cancel4_2.c respectively.
70 #include "tst-cancel4-common.h"
87 char fname
[] = "/tmp/tst-cancel4-fd-XXXXXX";
88 tempfd
= fd
= mkstemp (fname
);
90 FAIL_EXIT1 ("mkstemp failed: %m");
93 xpthread_barrier_wait (&b2
);
96 xpthread_barrier_wait (&b2
);
99 pthread_cleanup_push (cl
, NULL
);
102 s
= read (fd
, buf
, sizeof (buf
));
104 pthread_cleanup_pop (0);
106 FAIL_EXIT1 ("read returns with %zd", s
);
119 char fname
[] = "/tmp/tst-cancel4-fd-XXXXXX";
120 tempfd
= fd
= mkstemp (fname
);
122 FAIL_EXIT1 ("mkstemp failed: %m");
125 xpthread_barrier_wait (&b2
);
128 xpthread_barrier_wait (&b2
);
131 pthread_cleanup_push (cl
, NULL
);
134 struct iovec iov
[1] = { [0] = { .iov_base
= buf
, .iov_len
= sizeof (buf
) } };
135 s
= readv (fd
, iov
, 1);
137 pthread_cleanup_pop (0);
139 FAIL_EXIT1 ("readv returns with %zd", s
);
152 char fname
[] = "/tmp/tst-cancel4-fd-XXXXXX";
153 tempfd
= fd
= mkstemp (fname
);
155 FAIL_EXIT1 ("mkstemp failed: %m");
158 xpthread_barrier_wait (&b2
);
161 xpthread_barrier_wait (&b2
);
164 pthread_cleanup_push (cl
, NULL
);
166 char buf
[WRITE_BUFFER_SIZE
];
167 memset (buf
, '\0', sizeof (buf
));
168 s
= write (fd
, buf
, sizeof (buf
));
169 /* The write can return a value higher than 0 (meaning partial write)
170 due to the SIGCANCEL, but the thread may still be pending
172 pthread_testcancel ();
174 pthread_cleanup_pop (0);
176 FAIL_EXIT1 ("write returns with %zd", s
);
181 tf_writev (void *arg
)
189 char fname
[] = "/tmp/tst-cancel4-fd-XXXXXX";
190 tempfd
= fd
= mkstemp (fname
);
192 FAIL_EXIT1 ("mkstemp failed: %m");
195 xpthread_barrier_wait (&b2
);
198 xpthread_barrier_wait (&b2
);
201 pthread_cleanup_push (cl
, NULL
);
203 char buf
[WRITE_BUFFER_SIZE
];
204 memset (buf
, '\0', sizeof (buf
));
205 struct iovec iov
[1] = { [0] = { .iov_base
= buf
, .iov_len
= sizeof (buf
) } };
206 s
= writev (fd
, iov
, 1);
208 pthread_cleanup_pop (0);
210 FAIL_EXIT1 ("writev returns with %zd", s
);
217 xpthread_barrier_wait (&b2
);
220 xpthread_barrier_wait (&b2
);
222 pthread_cleanup_push (cl
, NULL
);
224 sleep (arg
== NULL
? 1000000 : 0);
226 pthread_cleanup_pop (0);
228 FAIL_EXIT1 ("sleep returns");
233 tf_usleep (void *arg
)
235 xpthread_barrier_wait (&b2
);
238 xpthread_barrier_wait (&b2
);
240 pthread_cleanup_push (cl
, NULL
);
242 usleep (arg
== NULL
? (useconds_t
) ULONG_MAX
: 0);
244 pthread_cleanup_pop (0);
246 FAIL_EXIT1 ("usleep returns");
251 tf_nanosleep (void *arg
)
253 xpthread_barrier_wait (&b2
);
256 xpthread_barrier_wait (&b2
);
258 pthread_cleanup_push (cl
, NULL
);
260 struct timespec ts
= { .tv_sec
= arg
== NULL
? 10000000 : 0, .tv_nsec
= 0 };
261 TEMP_FAILURE_RETRY (nanosleep (&ts
, &ts
));
263 pthread_cleanup_pop (0);
265 FAIL_EXIT1 ("nanosleep returns");
270 tf_select (void *arg
)
278 char fname
[] = "/tmp/tst-cancel4-fd-XXXXXX";
279 tempfd
= fd
= mkstemp (fname
);
281 FAIL_EXIT1 ("mkstemp failed: %m");
284 xpthread_barrier_wait (&b2
);
287 xpthread_barrier_wait (&b2
);
294 pthread_cleanup_push (cl
, NULL
);
296 s
= select (fd
+ 1, &rfs
, NULL
, NULL
, NULL
);
298 pthread_cleanup_pop (0);
300 FAIL_EXIT1 ("select returns with %d: %m", s
);
305 tf_pselect (void *arg
)
313 char fname
[] = "/tmp/tst-cancel4-fd-XXXXXX";
314 tempfd
= fd
= mkstemp (fname
);
316 FAIL_EXIT1 ("mkstemp failed: %m");
319 xpthread_barrier_wait (&b2
);
322 xpthread_barrier_wait (&b2
);
329 pthread_cleanup_push (cl
, NULL
);
331 s
= pselect (fd
+ 1, &rfs
, NULL
, NULL
, NULL
, NULL
);
333 pthread_cleanup_pop (0);
335 FAIL_EXIT1 ("pselect returns with %d: %m", s
);
348 char fname
[] = "/tmp/tst-cancel4-fd-XXXXXX";
349 tempfd
= fd
= mkstemp (fname
);
351 FAIL_EXIT1 ("mkstemp failed: %m");
354 xpthread_barrier_wait (&b2
);
357 xpthread_barrier_wait (&b2
);
359 struct pollfd rfs
[1] = { [0] = { .fd
= fd
, .events
= POLLIN
} };
362 pthread_cleanup_push (cl
, NULL
);
364 s
= poll (rfs
, 1, -1);
366 pthread_cleanup_pop (0);
368 FAIL_EXIT1 ("poll returns with %d: %m", s
);
381 char fname
[] = "/tmp/tst-cancel4-fd-XXXXXX";
382 tempfd
= fd
= mkstemp (fname
);
384 FAIL_EXIT1 ("mkstemp failed: %m");
387 xpthread_barrier_wait (&b2
);
390 xpthread_barrier_wait (&b2
);
392 struct pollfd rfs
[1] = { [0] = { .fd
= fd
, .events
= POLLIN
} };
395 pthread_cleanup_push (cl
, NULL
);
397 s
= ppoll (rfs
, 1, NULL
, NULL
);
399 pthread_cleanup_pop (0);
401 FAIL_EXIT1 ("ppoll returns with %d: %m", s
);
410 FAIL_EXIT1 ("fork: %m");
414 /* Make the program disappear after a while. */
422 struct timespec ts
= { .tv_sec
= 0, .tv_nsec
= 100000000 };
423 while (nanosleep (&ts
, &ts
) != 0)
426 xpthread_barrier_wait (&b2
);
429 xpthread_barrier_wait (&b2
);
432 pthread_cleanup_push (cl
, NULL
);
436 pthread_cleanup_pop (0);
438 FAIL_EXIT1 ("wait returns with %d: %m", s
);
443 tf_waitpid (void *arg
)
447 FAIL_EXIT1 ("fork: %m");
451 /* Make the program disappear after a while. */
459 struct timespec ts
= { .tv_sec
= 0, .tv_nsec
= 100000000 };
460 while (nanosleep (&ts
, &ts
) != 0)
463 xpthread_barrier_wait (&b2
);
466 xpthread_barrier_wait (&b2
);
469 pthread_cleanup_push (cl
, NULL
);
471 s
= waitpid (-1, NULL
, 0);
473 pthread_cleanup_pop (0);
475 FAIL_EXIT1 ("waitpid returns with %d: %m", s
);
480 tf_waitid (void *arg
)
484 FAIL_EXIT1 ("fork: %m");
488 /* Make the program disappear after a while. */
496 struct timespec ts
= { .tv_sec
= 0, .tv_nsec
= 100000000 };
497 while (nanosleep (&ts
, &ts
) != 0)
500 xpthread_barrier_wait (&b2
);
503 xpthread_barrier_wait (&b2
);
506 pthread_cleanup_push (cl
, NULL
);
512 s
= waitid (P_PID
, pid
, &si
, WEXITED
);
514 pthread_cleanup_pop (0);
516 FAIL_EXIT1 ("waitid returns with %d: %m", s
);
521 tf_sigpause (void *arg
)
523 xpthread_barrier_wait (&b2
);
526 xpthread_barrier_wait (&b2
);
528 pthread_cleanup_push (cl
, NULL
);
530 /* This tests the deprecated sigpause and sigmask functions. The
531 file is compiled with -Wno-errno so that the sigmask deprecation
532 warning is not fatal. */
533 DIAG_PUSH_NEEDS_COMMENT
;
534 DIAG_IGNORE_NEEDS_COMMENT (4.9, "-Wdeprecated-declarations");
535 sigpause (sigmask (SIGINT
));
536 DIAG_POP_NEEDS_COMMENT
;
538 pthread_cleanup_pop (0);
540 FAIL_EXIT1 ("sigpause returned");
545 tf_sigsuspend (void *arg
)
547 xpthread_barrier_wait (&b2
);
550 xpthread_barrier_wait (&b2
);
552 pthread_cleanup_push (cl
, NULL
);
554 /* Just for fun block all signals. */
559 pthread_cleanup_pop (0);
561 FAIL_EXIT1 ("sigsuspend returned");
566 tf_sigwait (void *arg
)
568 xpthread_barrier_wait (&b2
);
571 xpthread_barrier_wait (&b2
);
576 sigaddset (&mask
, SIGUSR1
);
577 TEST_VERIFY_EXIT (pthread_sigmask (SIG_BLOCK
, &mask
, NULL
) == 0);
580 pthread_cleanup_push (cl
, NULL
);
582 /* Wait for SIGUSR1. */
583 sigwait (&mask
, &sig
);
585 pthread_cleanup_pop (0);
587 FAIL_EXIT1 ("sigwait returned with signal %d", sig
);
592 tf_sigwaitinfo (void *arg
)
594 xpthread_barrier_wait (&b2
);
597 xpthread_barrier_wait (&b2
);
602 sigaddset (&mask
, SIGUSR1
);
603 TEST_VERIFY_EXIT (pthread_sigmask (SIG_BLOCK
, &mask
, NULL
) == 0);
606 pthread_cleanup_push (cl
, NULL
);
608 /* Wait for SIGUSR1. */
610 ret
= sigwaitinfo (&mask
, &info
);
611 if (ret
== -1 && errno
== ENOSYS
)
615 printf ("sigwaitinfo not supported\n");
616 sigwait (&mask
, &sig
);
619 pthread_cleanup_pop (0);
621 FAIL_EXIT1 ("sigwaitinfo returned with signal %d", info
.si_signo
);
626 tf_sigtimedwait (void *arg
)
628 xpthread_barrier_wait (&b2
);
631 xpthread_barrier_wait (&b2
);
636 sigaddset (&mask
, SIGUSR1
);
637 TEST_VERIFY_EXIT (pthread_sigmask (SIG_BLOCK
, &mask
, NULL
) == 0);
639 /* Wait for SIGUSR1. */
641 struct timespec ts
= { .tv_sec
= 60, .tv_nsec
= 0 };
642 pthread_cleanup_push (cl
, NULL
);
645 ret
= sigtimedwait (&mask
, &info
, &ts
);
646 if (ret
== -1 && errno
== ENOSYS
)
649 printf ("sigtimedwait not supported\n");
651 sigwait (&mask
, &sig
);
654 pthread_cleanup_pop (0);
656 FAIL_EXIT1 ("sigtimedwait returned with signal %d", info
.si_signo
);
663 xpthread_barrier_wait (&b2
);
666 xpthread_barrier_wait (&b2
);
668 pthread_cleanup_push (cl
, NULL
);
672 pthread_cleanup_pop (0);
674 FAIL_EXIT1 ("pause returned");
679 tf_accept (void *arg
)
681 struct sockaddr_un sun
;
682 /* To test a non-blocking accept call we make the call file by using
683 a datagrame socket. */
684 int pf
= arg
== NULL
? SOCK_STREAM
: SOCK_DGRAM
;
686 tempfd
= socket (AF_UNIX
, pf
, 0);
688 FAIL_EXIT1 ("socket (AF_UNIX, %s, 0): %m", arg
== NULL
? "SOCK_STREAM"
694 TEST_VERIFY_EXIT (tries
++ < 10);
696 strcpy (sun
.sun_path
, "/tmp/tst-cancel4-socket-1-XXXXXX");
697 TEST_VERIFY_EXIT (mktemp (sun
.sun_path
) != NULL
);
699 sun
.sun_family
= AF_UNIX
;
701 while (bind (tempfd
, (struct sockaddr
*) &sun
,
702 offsetof (struct sockaddr_un
, sun_path
)
703 + strlen (sun
.sun_path
) + 1) != 0);
705 unlink (sun
.sun_path
);
709 socklen_t len
= sizeof (sun
);
711 xpthread_barrier_wait (&b2
);
714 xpthread_barrier_wait (&b2
);
716 pthread_cleanup_push (cl
, NULL
);
718 accept (tempfd
, (struct sockaddr
*) &sun
, &len
);
720 pthread_cleanup_pop (0);
722 FAIL_EXIT1 ("accept returned");
729 struct sockaddr_un sun
;
731 tempfd
= socket (AF_UNIX
, SOCK_STREAM
, 0);
733 FAIL_EXIT1 ("socket (AF_UNIX, SOCK_STREAM, 0): %m");
738 TEST_VERIFY_EXIT (tries
++ < 10);
740 strcpy (sun
.sun_path
, "/tmp/tst-cancel4-socket-2-XXXXXX");
741 TEST_VERIFY_EXIT (mktemp (sun
.sun_path
) != NULL
);
743 sun
.sun_family
= AF_UNIX
;
745 while (bind (tempfd
, (struct sockaddr
*) &sun
,
746 offsetof (struct sockaddr_un
, sun_path
)
747 + strlen (sun
.sun_path
) + 1) != 0);
751 tempfd2
= socket (AF_UNIX
, SOCK_STREAM
, 0);
753 FAIL_EXIT1 ("socket (AF_UNIX, SOCK_STREAM, 0): %m");
755 if (connect (tempfd2
, (struct sockaddr
*) &sun
, sizeof (sun
)) != 0)
756 FAIL_EXIT1 ("connect: %m");
758 unlink (sun
.sun_path
);
760 set_socket_buffer (tempfd2
);
762 xpthread_barrier_wait (&b2
);
765 xpthread_barrier_wait (&b2
);
767 pthread_cleanup_push (cl
, NULL
);
769 char mem
[WRITE_BUFFER_SIZE
];
771 size_t s
= send (tempfd2
, mem
, arg
== NULL
? sizeof (mem
) : 1, 0);
772 /* The send can return a value higher than 0 (meaning partial send)
773 due to the SIGCANCEL, but the thread may still be pending
775 pthread_testcancel ();
776 printf("send returned %zd\n", s
);
778 pthread_cleanup_pop (0);
780 FAIL_EXIT1 ("send returned");
787 struct sockaddr_un sun
;
789 tempfd
= socket (AF_UNIX
, SOCK_STREAM
, 0);
791 FAIL_EXIT1 ("socket (AF_UNIX, SOCK_STREAM, 0): %m");
796 TEST_VERIFY_EXIT (tries
++ < 10);
798 strcpy (sun
.sun_path
, "/tmp/tst-cancel4-socket-3-XXXXXX");
799 TEST_VERIFY_EXIT (mktemp (sun
.sun_path
) != NULL
);
801 sun
.sun_family
= AF_UNIX
;
803 while (bind (tempfd
, (struct sockaddr
*) &sun
,
804 offsetof (struct sockaddr_un
, sun_path
)
805 + strlen (sun
.sun_path
) + 1) != 0);
809 tempfd2
= socket (AF_UNIX
, SOCK_STREAM
, 0);
811 FAIL_EXIT1 ("socket (AF_UNIX, SOCK_STREAM, 0): %m");
813 if (connect (tempfd2
, (struct sockaddr
*) &sun
, sizeof (sun
)) != 0)
814 FAIL_EXIT1 ("connect: %m");
816 unlink (sun
.sun_path
);
818 xpthread_barrier_wait (&b2
);
821 xpthread_barrier_wait (&b2
);
823 pthread_cleanup_push (cl
, NULL
);
827 recv (tempfd2
, mem
, arg
== NULL
? sizeof (mem
) : 0, 0);
829 pthread_cleanup_pop (0);
831 FAIL_EXIT1 ("recv returned");
836 tf_recvfrom (void *arg
)
838 struct sockaddr_un sun
;
840 tempfd
= socket (AF_UNIX
, SOCK_DGRAM
, 0);
842 FAIL_EXIT1 ("socket (AF_UNIX, SOCK_DGRAM, 0): %m");
847 TEST_VERIFY_EXIT (tries
++ < 10);
849 strcpy (sun
.sun_path
, "/tmp/tst-cancel4-socket-4-XXXXXX");
850 TEST_VERIFY_EXIT (mktemp (sun
.sun_path
) != NULL
);
852 sun
.sun_family
= AF_UNIX
;
854 while (bind (tempfd
, (struct sockaddr
*) &sun
,
855 offsetof (struct sockaddr_un
, sun_path
)
856 + strlen (sun
.sun_path
) + 1) != 0);
858 tempfname
= strdup (sun
.sun_path
);
860 tempfd2
= socket (AF_UNIX
, SOCK_DGRAM
, 0);
862 FAIL_EXIT1 ("socket (AF_UNIX, SOCK_DGRAM, 0): %m");
864 xpthread_barrier_wait (&b2
);
867 xpthread_barrier_wait (&b2
);
869 pthread_cleanup_push (cl
, NULL
);
872 socklen_t len
= sizeof (sun
);
874 recvfrom (tempfd2
, mem
, arg
== NULL
? sizeof (mem
) : 0, 0,
875 (struct sockaddr
*) &sun
, &len
);
877 pthread_cleanup_pop (0);
879 FAIL_EXIT1 ("recvfrom returned");
884 tf_recvmsg (void *arg
)
886 struct sockaddr_un sun
;
888 tempfd
= socket (AF_UNIX
, SOCK_DGRAM
, 0);
890 FAIL_EXIT1 ("socket (AF_UNIX, SOCK_DGRAM, 0): %m");
895 TEST_VERIFY_EXIT (tries
++ < 10);
897 strcpy (sun
.sun_path
, "/tmp/tst-cancel4-socket-5-XXXXXX");
898 TEST_VERIFY_EXIT (mktemp (sun
.sun_path
) != NULL
);
900 sun
.sun_family
= AF_UNIX
;
902 while (bind (tempfd
, (struct sockaddr
*) &sun
,
903 offsetof (struct sockaddr_un
, sun_path
)
904 + strlen (sun
.sun_path
) + 1) != 0);
906 tempfname
= strdup (sun
.sun_path
);
908 tempfd2
= socket (AF_UNIX
, SOCK_DGRAM
, 0);
910 FAIL_EXIT1 ("socket (AF_UNIX, SOCK_DGRAM, 0): %m");
912 xpthread_barrier_wait (&b2
);
915 xpthread_barrier_wait (&b2
);
917 pthread_cleanup_push (cl
, NULL
);
921 iov
[0].iov_base
= mem
;
922 iov
[0].iov_len
= arg
== NULL
? sizeof (mem
) : 0;
926 m
.msg_namelen
= sizeof (sun
);
929 m
.msg_control
= NULL
;
930 m
.msg_controllen
= 0;
932 recvmsg (tempfd2
, &m
, 0);
934 pthread_cleanup_pop (0);
936 FAIL_EXIT1 ("recvmsg returned");
944 fifofd
= mkfifo (fifoname
, S_IWUSR
| S_IRUSR
);
946 FAIL_EXIT1 ("mkfifo: %m");
950 xpthread_barrier_wait (&b2
);
953 xpthread_barrier_wait (&b2
);
955 pthread_cleanup_push (cl_fifo
, NULL
);
957 open (arg
? "Makefile" : fifoname
, O_RDONLY
);
959 pthread_cleanup_pop (0);
961 FAIL_EXIT1 ("open returned");
969 // XXX If somebody can provide a portable test case in which close()
970 // blocks we can enable this test to run in both rounds.
973 char fname
[] = "/tmp/tst-cancel-fd-XXXXXX";
974 tempfd
= mkstemp (fname
);
976 FAIL_EXIT1 ("mkstemp failed: %m");
979 xpthread_barrier_wait (&b2
);
981 xpthread_barrier_wait (&b2
);
983 pthread_cleanup_push (cl
, NULL
);
987 pthread_cleanup_pop (0);
989 FAIL_EXIT1 ("close returned");
997 // XXX If somebody can provide a portable test case in which pread()
998 // blocks we can enable this test to run in both rounds.
1001 tempfd
= open ("Makefile", O_RDONLY
);
1003 FAIL_EXIT1 ("open (\"Makefile\", O_RDONLY): %m");
1005 xpthread_barrier_wait (&b2
);
1007 xpthread_barrier_wait (&b2
);
1009 pthread_cleanup_push (cl
, NULL
);
1012 if (pread (tempfd
, mem
, sizeof (mem
), 0) < 0)
1013 FAIL_EXIT1 ("pread failed: %m");
1015 pthread_cleanup_pop (0);
1017 FAIL_EXIT1 ("pread returned");
1022 tf_pwrite (void *arg
)
1025 // XXX If somebody can provide a portable test case in which pwrite()
1026 // blocks we can enable this test to run in both rounds.
1029 char fname
[] = "/tmp/tst-cancel4-fd-XXXXXX";
1030 tempfd
= mkstemp (fname
);
1032 FAIL_EXIT1 ("mkstemp failed: %m");
1035 xpthread_barrier_wait (&b2
);
1037 xpthread_barrier_wait (&b2
);
1039 pthread_cleanup_push (cl
, NULL
);
1042 if (pwrite (tempfd
, mem
, sizeof (mem
), 0) <0)
1043 FAIL_EXIT1 ("pwrite failed: %m");
1045 pthread_cleanup_pop (0);
1047 FAIL_EXIT1 ("pwrite returned");
1051 tf_preadv (void *arg
)
1056 /* XXX If somebody can provide a portable test case in which preadv
1057 blocks we can enable this test to run in both rounds. */
1060 char fname
[] = "/tmp/tst-cancel4-fd-XXXXXX";
1061 tempfd
= fd
= mkstemp (fname
);
1063 FAIL_EXIT1 ("mkstemp failed: %m");
1066 xpthread_barrier_wait (&b2
);
1068 xpthread_barrier_wait (&b2
);
1071 pthread_cleanup_push (cl
, NULL
);
1074 struct iovec iov
[1] = { [0] = { .iov_base
= buf
, .iov_len
= sizeof (buf
) } };
1075 s
= preadv (fd
, iov
, 1, 0);
1077 pthread_cleanup_pop (0);
1079 FAIL_EXIT1 ("preadv returns with %zd", s
);
1083 tf_pwritev (void *arg
)
1088 /* XXX If somebody can provide a portable test case in which pwritev
1089 blocks we can enable this test to run in both rounds. */
1092 char fname
[] = "/tmp/tst-cancel4-fd-XXXXXX";
1093 tempfd
= fd
= mkstemp (fname
);
1095 FAIL_EXIT1 ("mkstemp failed: %m");
1098 xpthread_barrier_wait (&b2
);
1100 xpthread_barrier_wait (&b2
);
1103 pthread_cleanup_push (cl
, NULL
);
1105 char buf
[WRITE_BUFFER_SIZE
];
1106 memset (buf
, '\0', sizeof (buf
));
1107 struct iovec iov
[1] = { [0] = { .iov_base
= buf
, .iov_len
= sizeof (buf
) } };
1108 s
= pwritev (fd
, iov
, 1, 0);
1110 pthread_cleanup_pop (0);
1112 FAIL_EXIT1 ("pwritev returns with %zd", s
);
1116 tf_pwritev2 (void *arg
)
1121 /* XXX If somebody can provide a portable test case in which pwritev2
1122 blocks we can enable this test to run in both rounds. */
1127 char fname
[] = "/tmp/tst-cancel4-fd-XXXXXX";
1128 tempfd
= fd
= mkstemp (fname
);
1130 FAIL_EXIT1 ("mkstemp: %m");
1133 xpthread_barrier_wait (&b2
);
1135 xpthread_barrier_wait (&b2
);
1138 pthread_cleanup_push (cl
, NULL
);
1140 char buf
[WRITE_BUFFER_SIZE
];
1141 memset (buf
, '\0', sizeof (buf
));
1142 struct iovec iov
[1] = { [0] = { .iov_base
= buf
, .iov_len
= sizeof (buf
) } };
1143 s
= pwritev2 (fd
, iov
, 1, 0, 0);
1145 pthread_cleanup_pop (0);
1147 FAIL_EXIT1 ("pwritev2 returns with %zd", s
);
1151 tf_preadv2 (void *arg
)
1156 /* XXX If somebody can provide a portable test case in which preadv2
1157 blocks we can enable this test to run in both rounds. */
1162 char fname
[] = "/tmp/tst-cancel4-fd-XXXXXX";
1163 tempfd
= fd
= mkstemp (fname
);
1165 FAIL_EXIT1 ("mkstemp failed: %m");
1168 xpthread_barrier_wait (&b2
);
1170 xpthread_barrier_wait (&b2
);
1173 pthread_cleanup_push (cl
, NULL
);
1176 struct iovec iov
[1] = { [0] = { .iov_base
= buf
, .iov_len
= sizeof (buf
) } };
1177 s
= preadv2 (fd
, iov
, 1, 0, 0);
1179 pthread_cleanup_pop (0);
1181 FAIL_EXIT1 ("preadv2 returns with %zd", s
);
1185 tf_fsync (void *arg
)
1188 // XXX If somebody can provide a portable test case in which fsync()
1189 // blocks we can enable this test to run in both rounds.
1192 tempfd
= open ("Makefile", O_RDONLY
);
1194 FAIL_EXIT1 ("open (\"Makefile\", O_RDONLY): %m");
1196 xpthread_barrier_wait (&b2
);
1198 xpthread_barrier_wait (&b2
);
1200 pthread_cleanup_push (cl
, NULL
);
1204 pthread_cleanup_pop (0);
1206 FAIL_EXIT1 ("fsync returned");
1211 tf_fdatasync (void *arg
)
1214 // XXX If somebody can provide a portable test case in which fdatasync()
1215 // blocks we can enable this test to run in both rounds.
1218 tempfd
= open ("Makefile", O_RDONLY
);
1220 FAIL_EXIT1 ("open (\"Makefile\", O_RDONLY): %m");
1222 xpthread_barrier_wait (&b2
);
1224 xpthread_barrier_wait (&b2
);
1226 pthread_cleanup_push (cl
, NULL
);
1230 pthread_cleanup_pop (0);
1232 FAIL_EXIT1 ("fdatasync returned");
1237 tf_msync (void *arg
)
1240 // XXX If somebody can provide a portable test case in which msync()
1241 // blocks we can enable this test to run in both rounds.
1244 tempfd
= open ("Makefile", O_RDONLY
);
1246 FAIL_EXIT1 ("open (\"Makefile\", O_RDONLY): %m");
1248 void *p
= xmmap (NULL
, 10, PROT_READ
, MAP_SHARED
, tempfd
);
1250 xpthread_barrier_wait (&b2
);
1252 xpthread_barrier_wait (&b2
);
1254 pthread_cleanup_push (cl
, NULL
);
1258 pthread_cleanup_pop (0);
1260 FAIL_EXIT1 ("msync returned");
1265 tf_sendto (void *arg
)
1267 struct sockaddr_un sun
;
1269 tempfd
= socket (AF_UNIX
, SOCK_STREAM
, 0);
1271 FAIL_EXIT1 ("socket (AF_UNIX, SOCK_STREAM, 0): %m");
1276 TEST_VERIFY_EXIT (tries
++ < 10);
1278 strcpy (sun
.sun_path
, "/tmp/tst-cancel4-socket-6-XXXXXX");
1279 TEST_VERIFY_EXIT (mktemp (sun
.sun_path
) != NULL
);
1281 sun
.sun_family
= AF_UNIX
;
1283 while (bind (tempfd
, (struct sockaddr
*) &sun
,
1284 offsetof (struct sockaddr_un
, sun_path
)
1285 + strlen (sun
.sun_path
) + 1) != 0);
1289 tempfd2
= socket (AF_UNIX
, SOCK_STREAM
, 0);
1291 FAIL_EXIT1 ("socket (AF_UNIX, SOCK_STREAM, 0): %m");
1293 if (connect (tempfd2
, (struct sockaddr
*) &sun
, sizeof (sun
)) != 0)
1294 FAIL_EXIT1 ("connect: %m");
1296 unlink (sun
.sun_path
);
1298 set_socket_buffer (tempfd2
);
1300 xpthread_barrier_wait (&b2
);
1303 xpthread_barrier_wait (&b2
);
1305 pthread_cleanup_push (cl
, NULL
);
1307 char mem
[WRITE_BUFFER_SIZE
];
1309 sendto (tempfd2
, mem
, arg
== NULL
? sizeof (mem
) : 1, 0, NULL
, 0);
1311 pthread_cleanup_pop (0);
1313 FAIL_EXIT1 ("sendto returned");
1318 tf_sendmsg (void *arg
)
1321 // XXX If somebody can provide a portable test case in which sendmsg()
1322 // blocks we can enable this test to run in both rounds.
1325 struct sockaddr_un sun
;
1327 tempfd
= socket (AF_UNIX
, SOCK_DGRAM
, 0);
1329 FAIL_EXIT1 ("socket (AF_UNIX, SOCK_DGRAM, 0): %m");
1334 TEST_VERIFY_EXIT (tries
++ < 10);
1336 strcpy (sun
.sun_path
, "/tmp/tst-cancel4-socket-7-XXXXXX");
1337 TEST_VERIFY_EXIT (mktemp (sun
.sun_path
) != NULL
);
1339 sun
.sun_family
= AF_UNIX
;
1341 while (bind (tempfd
, (struct sockaddr
*) &sun
,
1342 offsetof (struct sockaddr_un
, sun_path
)
1343 + strlen (sun
.sun_path
) + 1) != 0);
1344 tempfname
= strdup (sun
.sun_path
);
1346 tempfd2
= socket (AF_UNIX
, SOCK_DGRAM
, 0);
1348 FAIL_EXIT1 ("socket (AF_UNIX, SOCK_DGRAM, 0): %m");
1350 xpthread_barrier_wait (&b2
);
1352 xpthread_barrier_wait (&b2
);
1354 pthread_cleanup_push (cl
, NULL
);
1357 struct iovec iov
[1];
1358 iov
[0].iov_base
= mem
;
1363 m
.msg_namelen
= (offsetof (struct sockaddr_un
, sun_path
)
1364 + strlen (sun
.sun_path
) + 1);
1367 m
.msg_control
= NULL
;
1368 m
.msg_controllen
= 0;
1370 sendmsg (tempfd2
, &m
, 0);
1372 pthread_cleanup_pop (0);
1374 FAIL_EXIT1 ("sendmsg returned");
1379 tf_creat (void *arg
)
1382 // XXX If somebody can provide a portable test case in which sendmsg()
1383 // blocks we can enable this test to run in both rounds.
1386 xpthread_barrier_wait (&b2
);
1388 xpthread_barrier_wait (&b2
);
1390 pthread_cleanup_push (cl
, NULL
);
1392 creat ("tmp/tst-cancel-4-should-not-exist", 0666);
1394 pthread_cleanup_pop (0);
1396 FAIL_EXIT1 ("creat returned");
1401 tf_connect (void *arg
)
1404 // XXX If somebody can provide a portable test case in which connect()
1405 // blocks we can enable this test to run in both rounds.
1408 struct sockaddr_un sun
;
1410 tempfd
= socket (AF_UNIX
, SOCK_STREAM
, 0);
1412 FAIL_EXIT1 ("socket (AF_UNIX, SOCK_STREAM, 0): %m");
1417 TEST_VERIFY_EXIT (tries
++ < 10);
1419 strcpy (sun
.sun_path
, "/tmp/tst-cancel4-socket-2-XXXXXX");
1420 TEST_VERIFY_EXIT (mktemp (sun
.sun_path
) != NULL
);
1422 sun
.sun_family
= AF_UNIX
;
1424 while (bind (tempfd
, (struct sockaddr
*) &sun
,
1425 offsetof (struct sockaddr_un
, sun_path
)
1426 + strlen (sun
.sun_path
) + 1) != 0);
1427 tempfname
= strdup (sun
.sun_path
);
1431 tempfd2
= socket (AF_UNIX
, SOCK_STREAM
, 0);
1433 FAIL_EXIT1 ("socket (AF_UNIX, SOCK_STREAM, 0): %m");
1435 xpthread_barrier_wait (&b2
);
1438 xpthread_barrier_wait (&b2
);
1440 pthread_cleanup_push (cl
, NULL
);
1442 connect (tempfd2
, (struct sockaddr
*) &sun
, sizeof (sun
));
1444 pthread_cleanup_pop (0);
1446 FAIL_EXIT1 ("connect returned");
1451 tf_tcdrain (void *arg
)
1454 // XXX If somebody can provide a portable test case in which tcdrain()
1455 // blocks we can enable this test to run in both rounds.
1458 xpthread_barrier_wait (&b2
);
1461 xpthread_barrier_wait (&b2
);
1463 pthread_cleanup_push (cl
, NULL
);
1465 /* Regardless of stderr being a terminal, the tcdrain call should be
1467 tcdrain (STDERR_FILENO
);
1469 pthread_cleanup_pop (0);
1471 FAIL_EXIT1 ("tcdrain returned");
1476 tf_msgrcv (void *arg
)
1478 tempmsg
= msgget (IPC_PRIVATE
, 0666 | IPC_CREAT
);
1481 if (errno
== ENOSYS
)
1483 printf ("msgget not supported\n");
1485 pthread_exit (NULL
);
1488 FAIL_EXIT1 ("msgget (IPC_PRIVATE, 0666 | IPC_CREAT): %m");
1491 xpthread_barrier_wait (&b2
);
1494 xpthread_barrier_wait (&b2
);
1498 pthread_cleanup_push (cl
, NULL
);
1506 /* We need a positive random number. */
1508 randnr
= random () % 64000;
1509 while (randnr
<= 0);
1513 s
= msgrcv (tempmsg
, (struct msgbuf
*) &m
, 10, randnr
, 0);
1515 while (errno
== EIDRM
|| errno
== EINTR
);
1517 pthread_cleanup_pop (0);
1519 msgctl (tempmsg
, IPC_RMID
, NULL
);
1521 FAIL_EXIT1 ("msgrcv returned %zd", s
);
1526 tf_msgsnd (void *arg
)
1529 // XXX If somebody can provide a portable test case in which msgsnd()
1530 // blocks we can enable this test to run in both rounds.
1533 tempmsg
= msgget (IPC_PRIVATE
, 0666 | IPC_CREAT
);
1536 if (errno
== ENOSYS
)
1538 printf ("msgget not supported\n");
1540 pthread_exit (NULL
);
1543 FAIL_EXIT1 ("msgget (IPC_PRIVATE, 0666 | IPC_CREAT): %m");
1546 xpthread_barrier_wait (&b2
);
1548 xpthread_barrier_wait (&b2
);
1550 pthread_cleanup_push (cl
, NULL
);
1557 /* We need a positive random number. */
1559 m
.type
= random () % 64000;
1560 while (m
.type
<= 0);
1561 msgsnd (tempmsg
, (struct msgbuf
*) &m
, sizeof (m
.mem
), 0);
1563 pthread_cleanup_pop (0);
1565 msgctl (tempmsg
, IPC_RMID
, NULL
);
1567 FAIL_EXIT1 ("msgsnd returned");
1571 struct cancel_tests tests
[] =
1573 ADD_TEST (read
, 2, 0),
1574 ADD_TEST (readv
, 2, 0),
1575 ADD_TEST (select
, 2, 0),
1576 ADD_TEST (pselect
, 2, 0),
1577 ADD_TEST (poll
, 2, 0),
1578 ADD_TEST (ppoll
, 2, 0),
1579 ADD_TEST (write
, 2, 0),
1580 ADD_TEST (writev
, 2, 0),
1581 ADD_TEST (sleep
, 2, 0),
1582 ADD_TEST (usleep
, 2, 0),
1583 ADD_TEST (nanosleep
, 2, 0),
1584 ADD_TEST (wait
, 2, 0),
1585 ADD_TEST (waitid
, 2, 0),
1586 ADD_TEST (waitpid
, 2, 0),
1587 ADD_TEST (sigpause
, 2, 0),
1588 ADD_TEST (sigsuspend
, 2, 0),
1589 ADD_TEST (sigwait
, 2, 0),
1590 ADD_TEST (sigwaitinfo
, 2, 0),
1591 ADD_TEST (sigtimedwait
, 2, 0),
1592 ADD_TEST (pause
, 2, 0),
1593 ADD_TEST (accept
, 2, 0),
1594 ADD_TEST (send
, 2, 0),
1595 ADD_TEST (recv
, 2, 0),
1596 ADD_TEST (recvfrom
, 2, 0),
1597 ADD_TEST (recvmsg
, 2, 0),
1598 ADD_TEST (preadv
, 2, 1),
1599 ADD_TEST (preadv2
, 2, 1),
1600 ADD_TEST (pwritev
, 2, 1),
1601 ADD_TEST (pwritev2
, 2, 1),
1602 ADD_TEST (open
, 2, 1),
1603 ADD_TEST (close
, 2, 1),
1604 ADD_TEST (pread
, 2, 1),
1605 ADD_TEST (pwrite
, 2, 1),
1606 ADD_TEST (fsync
, 2, 1),
1607 ADD_TEST (fdatasync
, 2, 1),
1608 ADD_TEST (msync
, 2, 1),
1609 ADD_TEST (sendto
, 2, 1),
1610 ADD_TEST (sendmsg
, 2, 1),
1611 ADD_TEST (creat
, 2, 1),
1612 ADD_TEST (connect
, 2, 1),
1613 ADD_TEST (tcdrain
, 2, 1),
1614 ADD_TEST (msgrcv
, 2, 0),
1615 ADD_TEST (msgsnd
, 2, 1),
1617 #define ntest_tf (sizeof (tests) / sizeof (tests[0]))
1619 #include "tst-cancel4-common.c"