Makeconfig: sandwich gnulib-tests between libc/ld linking of tests
[glibc.git] / nptl / tst-cancel4.c
blob652668537a51a7e0e2018566cd7baccf3e0c8e73
1 /* Copyright (C) 2002-2020 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 /* NOTE: this tests functionality beyond POSIX. POSIX does not allow
20 exit to be called more than once. */
22 #include <stddef.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/types.h>
27 #include <sys/socket.h>
28 #include <sys/un.h>
29 #include <sys/ipc.h>
30 #include <sys/msg.h>
31 #include <unistd.h>
32 #include <errno.h>
33 #include <limits.h>
34 #include <pthread.h>
35 #include <fcntl.h>
36 #include <termios.h>
37 #include <sys/mman.h>
38 #include <sys/poll.h>
39 #include <sys/wait.h>
40 #include <sys/stat.h>
41 #include <sys/uio.h>
44 /* Since STREAMS are not supported in the standard Linux kernel and
45 there we don't advertise STREAMS as supported is no need to test
46 the STREAMS related functions. This affects
47 getmsg() getpmsg() putmsg()
48 putpmsg()
50 lockf() and fcntl() are tested in tst-cancel16.
52 pthread_join() is tested in tst-join5.
54 pthread_testcancel()'s only purpose is to allow cancellation. This
55 is tested in several places.
57 sem_wait() and sem_timedwait() are checked in tst-cancel1[2345] tests.
59 mq_send(), mq_timedsend(), mq_receive() and mq_timedreceive() are checked
60 in tst-mqueue8{,x} tests.
62 aio_suspend() is tested in tst-cancel17.
64 clock_nanosleep() is tested in tst-cancel18.
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"
73 #ifndef IPC_ADDVAL
74 # define IPC_ADDVAL 0
75 #endif
78 static void *
79 tf_read (void *arg)
81 int fd;
83 if (arg == NULL)
84 fd = fds[0];
85 else
87 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
88 tempfd = fd = mkstemp (fname);
89 if (fd == -1)
90 FAIL_EXIT1 ("mkstemp failed: %m");
91 unlink (fname);
93 xpthread_barrier_wait (&b2);
96 xpthread_barrier_wait (&b2);
98 ssize_t s;
99 pthread_cleanup_push (cl, NULL);
101 char buf[100];
102 s = read (fd, buf, sizeof (buf));
104 pthread_cleanup_pop (0);
106 FAIL_EXIT1 ("read returns with %zd", s);
110 static void *
111 tf_readv (void *arg)
113 int fd;
115 if (arg == NULL)
116 fd = fds[0];
117 else
119 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
120 tempfd = fd = mkstemp (fname);
121 if (fd == -1)
122 FAIL_EXIT1 ("mkstemp failed: %m");
123 unlink (fname);
125 xpthread_barrier_wait (&b2);
128 xpthread_barrier_wait (&b2);
130 ssize_t s;
131 pthread_cleanup_push (cl, NULL);
133 char buf[100];
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);
143 static void *
144 tf_write (void *arg)
146 int fd;
148 if (arg == NULL)
149 fd = fds[1];
150 else
152 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
153 tempfd = fd = mkstemp (fname);
154 if (fd == -1)
155 FAIL_EXIT1 ("mkstemp failed: %m");
156 unlink (fname);
158 xpthread_barrier_wait (&b2);
161 xpthread_barrier_wait (&b2);
163 ssize_t s;
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
171 cancellation. */
172 pthread_testcancel ();
174 pthread_cleanup_pop (0);
176 FAIL_EXIT1 ("write returns with %zd", s);
180 static void *
181 tf_writev (void *arg)
183 int fd;
185 if (arg == NULL)
186 fd = fds[1];
187 else
189 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
190 tempfd = fd = mkstemp (fname);
191 if (fd == -1)
192 FAIL_EXIT1 ("mkstemp failed: %m");
193 unlink (fname);
195 xpthread_barrier_wait (&b2);
198 xpthread_barrier_wait (&b2);
200 ssize_t s;
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);
214 static void *
215 tf_sleep (void *arg)
217 xpthread_barrier_wait (&b2);
219 if (arg != NULL)
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");
232 static void *
233 tf_usleep (void *arg)
235 xpthread_barrier_wait (&b2);
237 if (arg != NULL)
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");
250 static void *
251 tf_nanosleep (void *arg)
253 xpthread_barrier_wait (&b2);
255 if (arg != NULL)
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");
269 static void *
270 tf_select (void *arg)
272 int fd;
274 if (arg == NULL)
275 fd = fds[0];
276 else
278 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
279 tempfd = fd = mkstemp (fname);
280 if (fd == -1)
281 FAIL_EXIT1 ("mkstemp failed: %m");
282 unlink (fname);
284 xpthread_barrier_wait (&b2);
287 xpthread_barrier_wait (&b2);
289 fd_set rfs;
290 FD_ZERO (&rfs);
291 FD_SET (fd, &rfs);
293 int s;
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);
304 static void *
305 tf_pselect (void *arg)
307 int fd;
309 if (arg == NULL)
310 fd = fds[0];
311 else
313 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
314 tempfd = fd = mkstemp (fname);
315 if (fd == -1)
316 FAIL_EXIT1 ("mkstemp failed: %m");
317 unlink (fname);
319 xpthread_barrier_wait (&b2);
322 xpthread_barrier_wait (&b2);
324 fd_set rfs;
325 FD_ZERO (&rfs);
326 FD_SET (fd, &rfs);
328 int s;
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);
339 static void *
340 tf_poll (void *arg)
342 int fd;
344 if (arg == NULL)
345 fd = fds[0];
346 else
348 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
349 tempfd = fd = mkstemp (fname);
350 if (fd == -1)
351 FAIL_EXIT1 ("mkstemp failed: %m");
352 unlink (fname);
354 xpthread_barrier_wait (&b2);
357 xpthread_barrier_wait (&b2);
359 struct pollfd rfs[1] = { [0] = { .fd = fd, .events = POLLIN } };
361 int s;
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);
372 static void *
373 tf_ppoll (void *arg)
375 int fd;
377 if (arg == NULL)
378 fd = fds[0];
379 else
381 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
382 tempfd = fd = mkstemp (fname);
383 if (fd == -1)
384 FAIL_EXIT1 ("mkstemp failed: %m");
385 unlink (fname);
387 xpthread_barrier_wait (&b2);
390 xpthread_barrier_wait (&b2);
392 struct pollfd rfs[1] = { [0] = { .fd = fd, .events = POLLIN } };
394 int s;
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);
405 static void *
406 tf_wait (void *arg)
408 pid_t pid = fork ();
409 if (pid == -1)
410 FAIL_EXIT1 ("fork: %m");
412 if (pid == 0)
414 /* Make the program disappear after a while. */
415 if (arg == NULL)
416 sleep (10);
417 exit (0);
420 if (arg != NULL)
422 struct timespec ts = { .tv_sec = 0, .tv_nsec = 100000000 };
423 while (nanosleep (&ts, &ts) != 0)
424 continue;
426 xpthread_barrier_wait (&b2);
429 xpthread_barrier_wait (&b2);
431 int s;
432 pthread_cleanup_push (cl, NULL);
434 s = wait (NULL);
436 pthread_cleanup_pop (0);
438 FAIL_EXIT1 ("wait returns with %d: %m", s);
442 static void *
443 tf_waitpid (void *arg)
445 pid_t pid = fork ();
446 if (pid == -1)
447 FAIL_EXIT1 ("fork: %m");
449 if (pid == 0)
451 /* Make the program disappear after a while. */
452 if (arg == NULL)
453 sleep (10);
454 exit (0);
457 if (arg != NULL)
459 struct timespec ts = { .tv_sec = 0, .tv_nsec = 100000000 };
460 while (nanosleep (&ts, &ts) != 0)
461 continue;
463 xpthread_barrier_wait (&b2);
466 xpthread_barrier_wait (&b2);
468 int s;
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);
479 static void *
480 tf_waitid (void *arg)
482 pid_t pid = fork ();
483 if (pid == -1)
484 FAIL_EXIT1 ("fork: %m");
486 if (pid == 0)
488 /* Make the program disappear after a while. */
489 if (arg == NULL)
490 sleep (10);
491 exit (0);
494 if (arg != NULL)
496 struct timespec ts = { .tv_sec = 0, .tv_nsec = 100000000 };
497 while (nanosleep (&ts, &ts) != 0)
498 continue;
500 xpthread_barrier_wait (&b2);
503 xpthread_barrier_wait (&b2);
505 int s;
506 pthread_cleanup_push (cl, NULL);
508 #ifndef WEXITED
509 # define WEXITED 0
510 #endif
511 siginfo_t si;
512 s = waitid (P_PID, pid, &si, WEXITED);
514 pthread_cleanup_pop (0);
516 FAIL_EXIT1 ("waitid returns with %d: %m", s);
520 static void *
521 tf_sigpause (void *arg)
523 xpthread_barrier_wait (&b2);
525 if (arg != NULL)
526 xpthread_barrier_wait (&b2);
528 pthread_cleanup_push (cl, NULL);
530 sigpause (sigmask (SIGINT));
532 pthread_cleanup_pop (0);
534 FAIL_EXIT1 ("sigpause returned");
538 static void *
539 tf_sigsuspend (void *arg)
541 xpthread_barrier_wait (&b2);
543 if (arg != NULL)
544 xpthread_barrier_wait (&b2);
546 pthread_cleanup_push (cl, NULL);
548 /* Just for fun block all signals. */
549 sigset_t mask;
550 sigfillset (&mask);
551 sigsuspend (&mask);
553 pthread_cleanup_pop (0);
555 FAIL_EXIT1 ("sigsuspend returned");
559 static void *
560 tf_sigwait (void *arg)
562 xpthread_barrier_wait (&b2);
564 if (arg != NULL)
565 xpthread_barrier_wait (&b2);
567 /* Block SIGUSR1. */
568 sigset_t mask;
569 sigemptyset (&mask);
570 sigaddset (&mask, SIGUSR1);
571 TEST_VERIFY_EXIT (pthread_sigmask (SIG_BLOCK, &mask, NULL) == 0);
573 int sig;
574 pthread_cleanup_push (cl, NULL);
576 /* Wait for SIGUSR1. */
577 sigwait (&mask, &sig);
579 pthread_cleanup_pop (0);
581 FAIL_EXIT1 ("sigwait returned with signal %d", sig);
585 static void *
586 tf_sigwaitinfo (void *arg)
588 xpthread_barrier_wait (&b2);
590 if (arg != NULL)
591 xpthread_barrier_wait (&b2);
593 /* Block SIGUSR1. */
594 sigset_t mask;
595 sigemptyset (&mask);
596 sigaddset (&mask, SIGUSR1);
597 TEST_VERIFY_EXIT (pthread_sigmask (SIG_BLOCK, &mask, NULL) == 0);
599 siginfo_t info;
600 pthread_cleanup_push (cl, NULL);
602 /* Wait for SIGUSR1. */
603 sigwaitinfo (&mask, &info);
605 pthread_cleanup_pop (0);
607 FAIL_EXIT1 ("sigwaitinfo returned with signal %d", info.si_signo);
611 static void *
612 tf_sigtimedwait (void *arg)
614 xpthread_barrier_wait (&b2);
616 if (arg != NULL)
617 xpthread_barrier_wait (&b2);
619 /* Block SIGUSR1. */
620 sigset_t mask;
621 sigemptyset (&mask);
622 sigaddset (&mask, SIGUSR1);
623 TEST_VERIFY_EXIT (pthread_sigmask (SIG_BLOCK, &mask, NULL) == 0);
625 /* Wait for SIGUSR1. */
626 siginfo_t info;
627 struct timespec ts = { .tv_sec = 60, .tv_nsec = 0 };
628 pthread_cleanup_push (cl, NULL);
630 sigtimedwait (&mask, &info, &ts);
632 pthread_cleanup_pop (0);
634 FAIL_EXIT1 ("sigtimedwait returned with signal %d", info.si_signo);
638 static void *
639 tf_pause (void *arg)
641 xpthread_barrier_wait (&b2);
643 if (arg != NULL)
644 xpthread_barrier_wait (&b2);
646 pthread_cleanup_push (cl, NULL);
648 pause ();
650 pthread_cleanup_pop (0);
652 FAIL_EXIT1 ("pause returned");
656 static void *
657 tf_accept (void *arg)
659 struct sockaddr_un sun;
660 /* To test a non-blocking accept call we make the call file by using
661 a datagrame socket. */
662 int pf = arg == NULL ? SOCK_STREAM : SOCK_DGRAM;
664 tempfd = socket (AF_UNIX, pf, 0);
665 if (tempfd == -1)
666 FAIL_EXIT1 ("socket (AF_UNIX, %s, 0): %m", arg == NULL ? "SOCK_STREAM"
667 : "SOCK_DGRAM");
669 int tries = 0;
672 TEST_VERIFY_EXIT (tries++ < 10);
674 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-1-XXXXXX");
675 TEST_VERIFY_EXIT (mktemp (sun.sun_path) != NULL);
677 sun.sun_family = AF_UNIX;
679 while (bind (tempfd, (struct sockaddr *) &sun,
680 offsetof (struct sockaddr_un, sun_path)
681 + strlen (sun.sun_path) + 1) != 0);
683 unlink (sun.sun_path);
685 listen (tempfd, 5);
687 socklen_t len = sizeof (sun);
689 xpthread_barrier_wait (&b2);
691 if (arg != NULL)
692 xpthread_barrier_wait (&b2);
694 pthread_cleanup_push (cl, NULL);
696 accept (tempfd, (struct sockaddr *) &sun, &len);
698 pthread_cleanup_pop (0);
700 FAIL_EXIT1 ("accept returned");
704 static void *
705 tf_send (void *arg)
707 struct sockaddr_un sun;
709 tempfd = socket (AF_UNIX, SOCK_STREAM, 0);
710 if (tempfd == -1)
711 FAIL_EXIT1 ("socket (AF_UNIX, SOCK_STREAM, 0): %m");
713 int tries = 0;
716 TEST_VERIFY_EXIT (tries++ < 10);
718 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-2-XXXXXX");
719 TEST_VERIFY_EXIT (mktemp (sun.sun_path) != NULL);
721 sun.sun_family = AF_UNIX;
723 while (bind (tempfd, (struct sockaddr *) &sun,
724 offsetof (struct sockaddr_un, sun_path)
725 + strlen (sun.sun_path) + 1) != 0);
727 listen (tempfd, 5);
729 tempfd2 = socket (AF_UNIX, SOCK_STREAM, 0);
730 if (tempfd2 == -1)
731 FAIL_EXIT1 ("socket (AF_UNIX, SOCK_STREAM, 0): %m");
733 set_socket_buffer (tempfd2);
735 if (connect (tempfd2, (struct sockaddr *) &sun, sizeof (sun)) != 0)
736 FAIL_EXIT1 ("connect: %m");
738 unlink (sun.sun_path);
740 xpthread_barrier_wait (&b2);
742 if (arg != NULL)
743 xpthread_barrier_wait (&b2);
745 pthread_cleanup_push (cl, NULL);
747 char mem[WRITE_BUFFER_SIZE];
749 send (tempfd2, mem, arg == NULL ? sizeof (mem) : 1, 0);
750 /* The send can return a value higher than 0 (meaning partial send)
751 due to the SIGCANCEL, but the thread may still be pending
752 cancellation. */
753 pthread_testcancel ();
755 pthread_cleanup_pop (0);
757 FAIL_EXIT1 ("send returned");
761 static void *
762 tf_recv (void *arg)
764 struct sockaddr_un sun;
766 tempfd = socket (AF_UNIX, SOCK_STREAM, 0);
767 if (tempfd == -1)
768 FAIL_EXIT1 ("socket (AF_UNIX, SOCK_STREAM, 0): %m");
770 int tries = 0;
773 TEST_VERIFY_EXIT (tries++ < 10);
775 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-3-XXXXXX");
776 TEST_VERIFY_EXIT (mktemp (sun.sun_path) != NULL);
778 sun.sun_family = AF_UNIX;
780 while (bind (tempfd, (struct sockaddr *) &sun,
781 offsetof (struct sockaddr_un, sun_path)
782 + strlen (sun.sun_path) + 1) != 0);
784 listen (tempfd, 5);
786 tempfd2 = socket (AF_UNIX, SOCK_STREAM, 0);
787 if (tempfd2 == -1)
788 FAIL_EXIT1 ("socket (AF_UNIX, SOCK_STREAM, 0): %m");
790 if (connect (tempfd2, (struct sockaddr *) &sun, sizeof (sun)) != 0)
791 FAIL_EXIT1 ("connect: %m");
793 unlink (sun.sun_path);
795 xpthread_barrier_wait (&b2);
797 if (arg != NULL)
798 xpthread_barrier_wait (&b2);
800 pthread_cleanup_push (cl, NULL);
802 char mem[70];
804 recv (tempfd2, mem, arg == NULL ? sizeof (mem) : 0, 0);
806 pthread_cleanup_pop (0);
808 FAIL_EXIT1 ("recv returned");
812 static void *
813 tf_recvfrom (void *arg)
815 struct sockaddr_un sun;
817 tempfd = socket (AF_UNIX, SOCK_DGRAM, 0);
818 if (tempfd == -1)
819 FAIL_EXIT1 ("socket (AF_UNIX, SOCK_DGRAM, 0): %m");
821 int tries = 0;
824 TEST_VERIFY_EXIT (tries++ < 10);
826 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-4-XXXXXX");
827 TEST_VERIFY_EXIT (mktemp (sun.sun_path) != NULL);
829 sun.sun_family = AF_UNIX;
831 while (bind (tempfd, (struct sockaddr *) &sun,
832 offsetof (struct sockaddr_un, sun_path)
833 + strlen (sun.sun_path) + 1) != 0);
835 tempfname = strdup (sun.sun_path);
837 tempfd2 = socket (AF_UNIX, SOCK_DGRAM, 0);
838 if (tempfd2 == -1)
839 FAIL_EXIT1 ("socket (AF_UNIX, SOCK_DGRAM, 0): %m");
841 xpthread_barrier_wait (&b2);
843 if (arg != NULL)
844 xpthread_barrier_wait (&b2);
846 pthread_cleanup_push (cl, NULL);
848 char mem[70];
849 socklen_t len = sizeof (sun);
851 recvfrom (tempfd2, mem, arg == NULL ? sizeof (mem) : 0, 0,
852 (struct sockaddr *) &sun, &len);
854 pthread_cleanup_pop (0);
856 FAIL_EXIT1 ("recvfrom returned");
860 static void *
861 tf_recvmsg (void *arg)
863 struct sockaddr_un sun;
865 tempfd = socket (AF_UNIX, SOCK_DGRAM, 0);
866 if (tempfd == -1)
867 FAIL_EXIT1 ("socket (AF_UNIX, SOCK_DGRAM, 0): %m");
869 int tries = 0;
872 TEST_VERIFY_EXIT (tries++ < 10);
874 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-5-XXXXXX");
875 TEST_VERIFY_EXIT (mktemp (sun.sun_path) != NULL);
877 sun.sun_family = AF_UNIX;
879 while (bind (tempfd, (struct sockaddr *) &sun,
880 offsetof (struct sockaddr_un, sun_path)
881 + strlen (sun.sun_path) + 1) != 0);
883 tempfname = strdup (sun.sun_path);
885 tempfd2 = socket (AF_UNIX, SOCK_DGRAM, 0);
886 if (tempfd2 == -1)
887 FAIL_EXIT1 ("socket (AF_UNIX, SOCK_DGRAM, 0): %m");
889 xpthread_barrier_wait (&b2);
891 if (arg != NULL)
892 xpthread_barrier_wait (&b2);
894 pthread_cleanup_push (cl, NULL);
896 char mem[70];
897 struct iovec iov[1];
898 iov[0].iov_base = mem;
899 iov[0].iov_len = arg == NULL ? sizeof (mem) : 0;
901 struct msghdr m;
902 m.msg_name = &sun;
903 m.msg_namelen = sizeof (sun);
904 m.msg_iov = iov;
905 m.msg_iovlen = 1;
906 m.msg_control = NULL;
907 m.msg_controllen = 0;
909 recvmsg (tempfd2, &m, 0);
911 pthread_cleanup_pop (0);
913 FAIL_EXIT1 ("recvmsg returned");
916 static void *
917 tf_open (void *arg)
919 if (arg == NULL)
921 fifofd = mkfifo (fifoname, S_IWUSR | S_IRUSR);
922 if (fifofd == -1)
923 FAIL_EXIT1 ("mkfifo: %m");
925 else
927 xpthread_barrier_wait (&b2);
930 xpthread_barrier_wait (&b2);
932 pthread_cleanup_push (cl_fifo, NULL);
934 open (arg ? "Makefile" : fifoname, O_RDONLY);
936 pthread_cleanup_pop (0);
938 FAIL_EXIT1 ("open returned");
942 static void *
943 tf_close (void *arg)
945 if (arg == NULL)
946 // XXX If somebody can provide a portable test case in which close()
947 // blocks we can enable this test to run in both rounds.
948 abort ();
950 char fname[] = "/tmp/tst-cancel-fd-XXXXXX";
951 tempfd = mkstemp (fname);
952 if (tempfd == -1)
953 FAIL_EXIT1 ("mkstemp failed: %m");
954 unlink (fname);
956 xpthread_barrier_wait (&b2);
958 xpthread_barrier_wait (&b2);
960 pthread_cleanup_push (cl, NULL);
962 close (tempfd);
964 pthread_cleanup_pop (0);
966 FAIL_EXIT1 ("close returned");
970 static void *
971 tf_pread (void *arg)
973 if (arg == NULL)
974 // XXX If somebody can provide a portable test case in which pread()
975 // blocks we can enable this test to run in both rounds.
976 abort ();
978 tempfd = open ("Makefile", O_RDONLY);
979 if (tempfd == -1)
980 FAIL_EXIT1 ("open (\"Makefile\", O_RDONLY): %m");
982 xpthread_barrier_wait (&b2);
984 xpthread_barrier_wait (&b2);
986 pthread_cleanup_push (cl, NULL);
988 char mem[10];
989 pread (tempfd, mem, sizeof (mem), 0);
991 pthread_cleanup_pop (0);
993 FAIL_EXIT1 ("pread returned");
997 static void *
998 tf_pwrite (void *arg)
1000 if (arg == NULL)
1001 // XXX If somebody can provide a portable test case in which pwrite()
1002 // blocks we can enable this test to run in both rounds.
1003 abort ();
1005 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
1006 tempfd = mkstemp (fname);
1007 if (tempfd == -1)
1008 FAIL_EXIT1 ("mkstemp failed: %m");
1009 unlink (fname);
1011 xpthread_barrier_wait (&b2);
1013 xpthread_barrier_wait (&b2);
1015 pthread_cleanup_push (cl, NULL);
1017 char mem[10];
1018 pwrite (tempfd, mem, sizeof (mem), 0);
1020 pthread_cleanup_pop (0);
1022 FAIL_EXIT1 ("pwrite returned");
1025 static void *
1026 tf_preadv (void *arg)
1028 int fd;
1030 if (arg == NULL)
1031 /* XXX If somebody can provide a portable test case in which preadv
1032 blocks we can enable this test to run in both rounds. */
1033 abort ();
1035 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
1036 tempfd = fd = mkstemp (fname);
1037 if (fd == -1)
1038 FAIL_EXIT1 ("mkstemp failed: %m");
1039 unlink (fname);
1041 xpthread_barrier_wait (&b2);
1043 xpthread_barrier_wait (&b2);
1045 ssize_t s;
1046 pthread_cleanup_push (cl, NULL);
1048 char buf[100];
1049 struct iovec iov[1] = { [0] = { .iov_base = buf, .iov_len = sizeof (buf) } };
1050 s = preadv (fd, iov, 1, 0);
1052 pthread_cleanup_pop (0);
1054 FAIL_EXIT1 ("preadv returns with %zd", s);
1057 static void *
1058 tf_pwritev (void *arg)
1060 int fd;
1062 if (arg == NULL)
1063 /* XXX If somebody can provide a portable test case in which pwritev
1064 blocks we can enable this test to run in both rounds. */
1065 abort ();
1067 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
1068 tempfd = fd = mkstemp (fname);
1069 if (fd == -1)
1070 FAIL_EXIT1 ("mkstemp failed: %m");
1071 unlink (fname);
1073 xpthread_barrier_wait (&b2);
1075 xpthread_barrier_wait (&b2);
1077 ssize_t s;
1078 pthread_cleanup_push (cl, NULL);
1080 char buf[WRITE_BUFFER_SIZE];
1081 memset (buf, '\0', sizeof (buf));
1082 struct iovec iov[1] = { [0] = { .iov_base = buf, .iov_len = sizeof (buf) } };
1083 s = pwritev (fd, iov, 1, 0);
1085 pthread_cleanup_pop (0);
1087 FAIL_EXIT1 ("pwritev returns with %zd", s);
1090 static void *
1091 tf_pwritev2 (void *arg)
1093 int fd;
1095 if (arg == NULL)
1096 /* XXX If somebody can provide a portable test case in which pwritev2
1097 blocks we can enable this test to run in both rounds. */
1098 abort ();
1100 errno = 0;
1102 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
1103 tempfd = fd = mkstemp (fname);
1104 if (fd == -1)
1105 FAIL_EXIT1 ("mkstemp: %m");
1106 unlink (fname);
1108 xpthread_barrier_wait (&b2);
1110 xpthread_barrier_wait (&b2);
1112 ssize_t s;
1113 pthread_cleanup_push (cl, NULL);
1115 char buf[WRITE_BUFFER_SIZE];
1116 memset (buf, '\0', sizeof (buf));
1117 struct iovec iov[1] = { [0] = { .iov_base = buf, .iov_len = sizeof (buf) } };
1118 s = pwritev2 (fd, iov, 1, 0, 0);
1120 pthread_cleanup_pop (0);
1122 FAIL_EXIT1 ("pwritev2 returns with %zd", s);
1125 static void *
1126 tf_preadv2 (void *arg)
1128 int fd;
1130 if (arg == NULL)
1131 /* XXX If somebody can provide a portable test case in which preadv2
1132 blocks we can enable this test to run in both rounds. */
1133 abort ();
1135 errno = 0;
1137 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
1138 tempfd = fd = mkstemp (fname);
1139 if (fd == -1)
1140 FAIL_EXIT1 ("mkstemp failed: %m");
1141 unlink (fname);
1143 xpthread_barrier_wait (&b2);
1145 xpthread_barrier_wait (&b2);
1147 ssize_t s;
1148 pthread_cleanup_push (cl, NULL);
1150 char buf[100];
1151 struct iovec iov[1] = { [0] = { .iov_base = buf, .iov_len = sizeof (buf) } };
1152 s = preadv2 (fd, iov, 1, 0, 0);
1154 pthread_cleanup_pop (0);
1156 FAIL_EXIT1 ("preadv2 returns with %zd", s);
1159 static void *
1160 tf_fsync (void *arg)
1162 if (arg == NULL)
1163 // XXX If somebody can provide a portable test case in which fsync()
1164 // blocks we can enable this test to run in both rounds.
1165 abort ();
1167 tempfd = open ("Makefile", O_RDONLY);
1168 if (tempfd == -1)
1169 FAIL_EXIT1 ("open (\"Makefile\", O_RDONLY): %m");
1171 xpthread_barrier_wait (&b2);
1173 xpthread_barrier_wait (&b2);
1175 pthread_cleanup_push (cl, NULL);
1177 fsync (tempfd);
1179 pthread_cleanup_pop (0);
1181 FAIL_EXIT1 ("fsync returned");
1185 static void *
1186 tf_fdatasync (void *arg)
1188 if (arg == NULL)
1189 // XXX If somebody can provide a portable test case in which fdatasync()
1190 // blocks we can enable this test to run in both rounds.
1191 abort ();
1193 tempfd = open ("Makefile", O_RDONLY);
1194 if (tempfd == -1)
1195 FAIL_EXIT1 ("open (\"Makefile\", O_RDONLY): %m");
1197 xpthread_barrier_wait (&b2);
1199 xpthread_barrier_wait (&b2);
1201 pthread_cleanup_push (cl, NULL);
1203 fdatasync (tempfd);
1205 pthread_cleanup_pop (0);
1207 FAIL_EXIT1 ("fdatasync returned");
1211 static void *
1212 tf_msync (void *arg)
1214 if (arg == NULL)
1215 // XXX If somebody can provide a portable test case in which msync()
1216 // blocks we can enable this test to run in both rounds.
1217 abort ();
1219 tempfd = open ("Makefile", O_RDONLY);
1220 if (tempfd == -1)
1221 FAIL_EXIT1 ("open (\"Makefile\", O_RDONLY): %m");
1223 void *p = xmmap (NULL, 10, PROT_READ, MAP_SHARED, tempfd);
1225 xpthread_barrier_wait (&b2);
1227 xpthread_barrier_wait (&b2);
1229 pthread_cleanup_push (cl, NULL);
1231 msync (p, 10, 0);
1233 pthread_cleanup_pop (0);
1235 FAIL_EXIT1 ("msync returned");
1239 static void *
1240 tf_sendto (void *arg)
1242 struct sockaddr_un sun;
1244 tempfd = socket (AF_UNIX, SOCK_STREAM, 0);
1245 if (tempfd == -1)
1246 FAIL_EXIT1 ("socket (AF_UNIX, SOCK_STREAM, 0): %m");
1248 int tries = 0;
1251 TEST_VERIFY_EXIT (tries++ < 10);
1253 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-6-XXXXXX");
1254 TEST_VERIFY_EXIT (mktemp (sun.sun_path) != NULL);
1256 sun.sun_family = AF_UNIX;
1258 while (bind (tempfd, (struct sockaddr *) &sun,
1259 offsetof (struct sockaddr_un, sun_path)
1260 + strlen (sun.sun_path) + 1) != 0);
1262 listen (tempfd, 5);
1264 tempfd2 = socket (AF_UNIX, SOCK_STREAM, 0);
1265 if (tempfd2 == -1)
1266 FAIL_EXIT1 ("socket (AF_UNIX, SOCK_STREAM, 0): %m");
1268 set_socket_buffer (tempfd2);
1270 if (connect (tempfd2, (struct sockaddr *) &sun, sizeof (sun)) != 0)
1271 FAIL_EXIT1 ("connect: %m");
1273 unlink (sun.sun_path);
1275 xpthread_barrier_wait (&b2);
1277 if (arg != NULL)
1278 xpthread_barrier_wait (&b2);
1280 pthread_cleanup_push (cl, NULL);
1282 char mem[WRITE_BUFFER_SIZE];
1284 sendto (tempfd2, mem, arg == NULL ? sizeof (mem) : 1, 0, NULL, 0);
1286 pthread_cleanup_pop (0);
1288 FAIL_EXIT1 ("sendto returned");
1292 static void *
1293 tf_sendmsg (void *arg)
1295 if (arg == NULL)
1296 // XXX If somebody can provide a portable test case in which sendmsg()
1297 // blocks we can enable this test to run in both rounds.
1298 abort ();
1300 struct sockaddr_un sun;
1302 tempfd = socket (AF_UNIX, SOCK_DGRAM, 0);
1303 if (tempfd == -1)
1304 FAIL_EXIT1 ("socket (AF_UNIX, SOCK_DGRAM, 0): %m");
1306 int tries = 0;
1309 TEST_VERIFY_EXIT (tries++ < 10);
1311 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-7-XXXXXX");
1312 TEST_VERIFY_EXIT (mktemp (sun.sun_path) != NULL);
1314 sun.sun_family = AF_UNIX;
1316 while (bind (tempfd, (struct sockaddr *) &sun,
1317 offsetof (struct sockaddr_un, sun_path)
1318 + strlen (sun.sun_path) + 1) != 0);
1319 tempfname = strdup (sun.sun_path);
1321 tempfd2 = socket (AF_UNIX, SOCK_DGRAM, 0);
1322 if (tempfd2 == -1)
1323 FAIL_EXIT1 ("socket (AF_UNIX, SOCK_DGRAM, 0): %m");
1325 xpthread_barrier_wait (&b2);
1327 xpthread_barrier_wait (&b2);
1329 pthread_cleanup_push (cl, NULL);
1331 char mem[1];
1332 struct iovec iov[1];
1333 iov[0].iov_base = mem;
1334 iov[0].iov_len = 1;
1336 struct msghdr m;
1337 m.msg_name = &sun;
1338 m.msg_namelen = (offsetof (struct sockaddr_un, sun_path)
1339 + strlen (sun.sun_path) + 1);
1340 m.msg_iov = iov;
1341 m.msg_iovlen = 1;
1342 m.msg_control = NULL;
1343 m.msg_controllen = 0;
1345 sendmsg (tempfd2, &m, 0);
1347 pthread_cleanup_pop (0);
1349 FAIL_EXIT1 ("sendmsg returned");
1353 static void *
1354 tf_creat (void *arg)
1356 if (arg == NULL)
1357 // XXX If somebody can provide a portable test case in which sendmsg()
1358 // blocks we can enable this test to run in both rounds.
1359 abort ();
1361 xpthread_barrier_wait (&b2);
1363 xpthread_barrier_wait (&b2);
1365 pthread_cleanup_push (cl, NULL);
1367 creat ("tmp/tst-cancel-4-should-not-exist", 0666);
1369 pthread_cleanup_pop (0);
1371 FAIL_EXIT1 ("creat returned");
1375 static void *
1376 tf_connect (void *arg)
1378 if (arg == NULL)
1379 // XXX If somebody can provide a portable test case in which connect()
1380 // blocks we can enable this test to run in both rounds.
1381 abort ();
1383 struct sockaddr_un sun;
1385 tempfd = socket (AF_UNIX, SOCK_STREAM, 0);
1386 if (tempfd == -1)
1387 FAIL_EXIT1 ("socket (AF_UNIX, SOCK_STREAM, 0): %m");
1389 int tries = 0;
1392 TEST_VERIFY_EXIT (tries++ < 10);
1394 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-2-XXXXXX");
1395 TEST_VERIFY_EXIT (mktemp (sun.sun_path) != NULL);
1397 sun.sun_family = AF_UNIX;
1399 while (bind (tempfd, (struct sockaddr *) &sun,
1400 offsetof (struct sockaddr_un, sun_path)
1401 + strlen (sun.sun_path) + 1) != 0);
1402 tempfname = strdup (sun.sun_path);
1404 listen (tempfd, 5);
1406 tempfd2 = socket (AF_UNIX, SOCK_STREAM, 0);
1407 if (tempfd2 == -1)
1408 FAIL_EXIT1 ("socket (AF_UNIX, SOCK_STREAM, 0): %m");
1410 xpthread_barrier_wait (&b2);
1412 if (arg != NULL)
1413 xpthread_barrier_wait (&b2);
1415 pthread_cleanup_push (cl, NULL);
1417 connect (tempfd2, (struct sockaddr *) &sun, sizeof (sun));
1419 pthread_cleanup_pop (0);
1421 FAIL_EXIT1 ("connect returned");
1425 static void *
1426 tf_tcdrain (void *arg)
1428 if (arg == NULL)
1429 // XXX If somebody can provide a portable test case in which tcdrain()
1430 // blocks we can enable this test to run in both rounds.
1431 abort ();
1433 xpthread_barrier_wait (&b2);
1435 if (arg != NULL)
1436 xpthread_barrier_wait (&b2);
1438 pthread_cleanup_push (cl, NULL);
1440 /* Regardless of stderr being a terminal, the tcdrain call should be
1441 canceled. */
1442 tcdrain (STDERR_FILENO);
1444 pthread_cleanup_pop (0);
1446 FAIL_EXIT1 ("tcdrain returned");
1450 static void *
1451 tf_msgrcv (void *arg)
1453 tempmsg = msgget (IPC_PRIVATE, 0666 | IPC_CREAT);
1454 if (tempmsg == -1)
1455 FAIL_EXIT1 ("msgget (IPC_PRIVATE, 0666 | IPC_CREAT): %m");
1457 xpthread_barrier_wait (&b2);
1459 if (arg != NULL)
1460 xpthread_barrier_wait (&b2);
1462 ssize_t s;
1464 pthread_cleanup_push (cl, NULL);
1466 struct
1468 long int type;
1469 char mem[10];
1470 } m;
1471 int randnr;
1472 /* We need a positive random number. */
1474 randnr = random () % 64000;
1475 while (randnr <= 0);
1478 errno = 0;
1479 s = msgrcv (tempmsg, (struct msgbuf *) &m, 10, randnr, 0);
1481 while (errno == EIDRM || errno == EINTR);
1483 pthread_cleanup_pop (0);
1485 msgctl (tempmsg, IPC_RMID, NULL);
1487 FAIL_EXIT1 ("msgrcv returned %zd", s);
1491 static void *
1492 tf_msgsnd (void *arg)
1494 if (arg == NULL)
1495 // XXX If somebody can provide a portable test case in which msgsnd()
1496 // blocks we can enable this test to run in both rounds.
1497 abort ();
1499 tempmsg = msgget (IPC_PRIVATE, 0666 | IPC_CREAT);
1500 if (tempmsg == -1)
1501 FAIL_EXIT1 ("msgget (IPC_PRIVATE, 0666 | IPC_CREAT): %m");
1503 xpthread_barrier_wait (&b2);
1505 xpthread_barrier_wait (&b2);
1507 pthread_cleanup_push (cl, NULL);
1509 struct
1511 long int type;
1512 char mem[1];
1513 } m;
1514 /* We need a positive random number. */
1516 m.type = random () % 64000;
1517 while (m.type <= 0);
1518 msgsnd (tempmsg, (struct msgbuf *) &m, sizeof (m.mem), 0);
1520 pthread_cleanup_pop (0);
1522 msgctl (tempmsg, IPC_RMID, NULL);
1524 FAIL_EXIT1 ("msgsnd returned");
1528 struct cancel_tests tests[] =
1530 ADD_TEST (read, 2, 0),
1531 ADD_TEST (readv, 2, 0),
1532 ADD_TEST (select, 2, 0),
1533 ADD_TEST (pselect, 2, 0),
1534 ADD_TEST (poll, 2, 0),
1535 ADD_TEST (ppoll, 2, 0),
1536 ADD_TEST (write, 2, 0),
1537 ADD_TEST (writev, 2, 0),
1538 ADD_TEST (sleep, 2, 0),
1539 ADD_TEST (usleep, 2, 0),
1540 ADD_TEST (nanosleep, 2, 0),
1541 ADD_TEST (wait, 2, 0),
1542 ADD_TEST (waitid, 2, 0),
1543 ADD_TEST (waitpid, 2, 0),
1544 ADD_TEST (sigpause, 2, 0),
1545 ADD_TEST (sigsuspend, 2, 0),
1546 ADD_TEST (sigwait, 2, 0),
1547 ADD_TEST (sigwaitinfo, 2, 0),
1548 ADD_TEST (sigtimedwait, 2, 0),
1549 ADD_TEST (pause, 2, 0),
1550 ADD_TEST (accept, 2, 0),
1551 ADD_TEST (send, 2, 0),
1552 ADD_TEST (recv, 2, 0),
1553 ADD_TEST (recvfrom, 2, 0),
1554 ADD_TEST (recvmsg, 2, 0),
1555 ADD_TEST (preadv, 2, 1),
1556 ADD_TEST (preadv2, 2, 1),
1557 ADD_TEST (pwritev, 2, 1),
1558 ADD_TEST (pwritev2, 2, 1),
1559 ADD_TEST (open, 2, 1),
1560 ADD_TEST (close, 2, 1),
1561 ADD_TEST (pread, 2, 1),
1562 ADD_TEST (pwrite, 2, 1),
1563 ADD_TEST (fsync, 2, 1),
1564 ADD_TEST (fdatasync, 2, 1),
1565 ADD_TEST (msync, 2, 1),
1566 ADD_TEST (sendto, 2, 1),
1567 ADD_TEST (sendmsg, 2, 1),
1568 ADD_TEST (creat, 2, 1),
1569 ADD_TEST (connect, 2, 1),
1570 ADD_TEST (tcdrain, 2, 1),
1571 ADD_TEST (msgrcv, 2, 0),
1572 ADD_TEST (msgsnd, 2, 1),
1574 #define ntest_tf (sizeof (tests) / sizeof (tests[0]))
1576 #include "tst-cancel4-common.c"