Revert:
[glibc.git] / nptl / tst-cancel4.c
blob05325385b180e936e1b63e49763947bdbf658970
1 /* Copyright (C) 2002-2018 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 /* NOTE: this tests functionality beyond POSIX. POSIX does not allow
20 exit to be called more than once. */
22 #include <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));
170 pthread_cleanup_pop (0);
172 FAIL_EXIT1 ("write returns with %zd", s);
176 static void *
177 tf_writev (void *arg)
179 int fd;
181 if (arg == NULL)
182 fd = fds[1];
183 else
185 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
186 tempfd = fd = mkstemp (fname);
187 if (fd == -1)
188 FAIL_EXIT1 ("mkstemp failed: %m");
189 unlink (fname);
191 xpthread_barrier_wait (&b2);
194 xpthread_barrier_wait (&b2);
196 ssize_t s;
197 pthread_cleanup_push (cl, NULL);
199 char buf[WRITE_BUFFER_SIZE];
200 memset (buf, '\0', sizeof (buf));
201 struct iovec iov[1] = { [0] = { .iov_base = buf, .iov_len = sizeof (buf) } };
202 s = writev (fd, iov, 1);
204 pthread_cleanup_pop (0);
206 FAIL_EXIT1 ("writev returns with %zd", s);
210 static void *
211 tf_sleep (void *arg)
213 xpthread_barrier_wait (&b2);
215 if (arg != NULL)
216 xpthread_barrier_wait (&b2);
218 pthread_cleanup_push (cl, NULL);
220 sleep (arg == NULL ? 1000000 : 0);
222 pthread_cleanup_pop (0);
224 FAIL_EXIT1 ("sleep returns");
228 static void *
229 tf_usleep (void *arg)
231 xpthread_barrier_wait (&b2);
233 if (arg != NULL)
234 xpthread_barrier_wait (&b2);
236 pthread_cleanup_push (cl, NULL);
238 usleep (arg == NULL ? (useconds_t) ULONG_MAX : 0);
240 pthread_cleanup_pop (0);
242 FAIL_EXIT1 ("usleep returns");
246 static void *
247 tf_nanosleep (void *arg)
249 xpthread_barrier_wait (&b2);
251 if (arg != NULL)
252 xpthread_barrier_wait (&b2);
254 pthread_cleanup_push (cl, NULL);
256 struct timespec ts = { .tv_sec = arg == NULL ? 10000000 : 0, .tv_nsec = 0 };
257 TEMP_FAILURE_RETRY (nanosleep (&ts, &ts));
259 pthread_cleanup_pop (0);
261 FAIL_EXIT1 ("nanosleep returns");
265 static void *
266 tf_select (void *arg)
268 int fd;
270 if (arg == NULL)
271 fd = fds[0];
272 else
274 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
275 tempfd = fd = mkstemp (fname);
276 if (fd == -1)
277 FAIL_EXIT1 ("mkstemp failed: %m");
278 unlink (fname);
280 xpthread_barrier_wait (&b2);
283 xpthread_barrier_wait (&b2);
285 fd_set rfs;
286 FD_ZERO (&rfs);
287 FD_SET (fd, &rfs);
289 int s;
290 pthread_cleanup_push (cl, NULL);
292 s = select (fd + 1, &rfs, NULL, NULL, NULL);
294 pthread_cleanup_pop (0);
296 FAIL_EXIT1 ("select returns with %d: %m", s);
300 static void *
301 tf_pselect (void *arg)
303 int fd;
305 if (arg == NULL)
306 fd = fds[0];
307 else
309 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
310 tempfd = fd = mkstemp (fname);
311 if (fd == -1)
312 FAIL_EXIT1 ("mkstemp failed: %m");
313 unlink (fname);
315 xpthread_barrier_wait (&b2);
318 xpthread_barrier_wait (&b2);
320 fd_set rfs;
321 FD_ZERO (&rfs);
322 FD_SET (fd, &rfs);
324 int s;
325 pthread_cleanup_push (cl, NULL);
327 s = pselect (fd + 1, &rfs, NULL, NULL, NULL, NULL);
329 pthread_cleanup_pop (0);
331 FAIL_EXIT1 ("pselect returns with %d: %m", s);
335 static void *
336 tf_poll (void *arg)
338 int fd;
340 if (arg == NULL)
341 fd = fds[0];
342 else
344 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
345 tempfd = fd = mkstemp (fname);
346 if (fd == -1)
347 FAIL_EXIT1 ("mkstemp failed: %m");
348 unlink (fname);
350 xpthread_barrier_wait (&b2);
353 xpthread_barrier_wait (&b2);
355 struct pollfd rfs[1] = { [0] = { .fd = fd, .events = POLLIN } };
357 int s;
358 pthread_cleanup_push (cl, NULL);
360 s = poll (rfs, 1, -1);
362 pthread_cleanup_pop (0);
364 FAIL_EXIT1 ("poll returns with %d: %m", s);
368 static void *
369 tf_ppoll (void *arg)
371 int fd;
373 if (arg == NULL)
374 fd = fds[0];
375 else
377 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
378 tempfd = fd = mkstemp (fname);
379 if (fd == -1)
380 FAIL_EXIT1 ("mkstemp failed: %m");
381 unlink (fname);
383 xpthread_barrier_wait (&b2);
386 xpthread_barrier_wait (&b2);
388 struct pollfd rfs[1] = { [0] = { .fd = fd, .events = POLLIN } };
390 int s;
391 pthread_cleanup_push (cl, NULL);
393 s = ppoll (rfs, 1, NULL, NULL);
395 pthread_cleanup_pop (0);
397 FAIL_EXIT1 ("ppoll returns with %d: %m", s);
401 static void *
402 tf_wait (void *arg)
404 pid_t pid = fork ();
405 if (pid == -1)
406 FAIL_EXIT1 ("fork: %m");
408 if (pid == 0)
410 /* Make the program disappear after a while. */
411 if (arg == NULL)
412 sleep (10);
413 exit (0);
416 if (arg != NULL)
418 struct timespec ts = { .tv_sec = 0, .tv_nsec = 100000000 };
419 while (nanosleep (&ts, &ts) != 0)
420 continue;
422 xpthread_barrier_wait (&b2);
425 xpthread_barrier_wait (&b2);
427 int s;
428 pthread_cleanup_push (cl, NULL);
430 s = wait (NULL);
432 pthread_cleanup_pop (0);
434 FAIL_EXIT1 ("wait returns with %d: %m", s);
438 static void *
439 tf_waitpid (void *arg)
441 pid_t pid = fork ();
442 if (pid == -1)
443 FAIL_EXIT1 ("fork: %m");
445 if (pid == 0)
447 /* Make the program disappear after a while. */
448 if (arg == NULL)
449 sleep (10);
450 exit (0);
453 if (arg != NULL)
455 struct timespec ts = { .tv_sec = 0, .tv_nsec = 100000000 };
456 while (nanosleep (&ts, &ts) != 0)
457 continue;
459 xpthread_barrier_wait (&b2);
462 xpthread_barrier_wait (&b2);
464 int s;
465 pthread_cleanup_push (cl, NULL);
467 s = waitpid (-1, NULL, 0);
469 pthread_cleanup_pop (0);
471 FAIL_EXIT1 ("waitpid returns with %d: %m", s);
475 static void *
476 tf_waitid (void *arg)
478 pid_t pid = fork ();
479 if (pid == -1)
480 FAIL_EXIT1 ("fork: %m");
482 if (pid == 0)
484 /* Make the program disappear after a while. */
485 if (arg == NULL)
486 sleep (10);
487 exit (0);
490 if (arg != NULL)
492 struct timespec ts = { .tv_sec = 0, .tv_nsec = 100000000 };
493 while (nanosleep (&ts, &ts) != 0)
494 continue;
496 xpthread_barrier_wait (&b2);
499 xpthread_barrier_wait (&b2);
501 int s;
502 pthread_cleanup_push (cl, NULL);
504 #ifndef WEXITED
505 # define WEXITED 0
506 #endif
507 siginfo_t si;
508 s = waitid (P_PID, pid, &si, WEXITED);
510 pthread_cleanup_pop (0);
512 FAIL_EXIT1 ("waitid returns with %d: %m", s);
516 static void *
517 tf_sigpause (void *arg)
519 xpthread_barrier_wait (&b2);
521 if (arg != NULL)
522 xpthread_barrier_wait (&b2);
524 pthread_cleanup_push (cl, NULL);
526 sigpause (sigmask (SIGINT));
528 pthread_cleanup_pop (0);
530 FAIL_EXIT1 ("sigpause returned");
534 static void *
535 tf_sigsuspend (void *arg)
537 xpthread_barrier_wait (&b2);
539 if (arg != NULL)
540 xpthread_barrier_wait (&b2);
542 pthread_cleanup_push (cl, NULL);
544 /* Just for fun block all signals. */
545 sigset_t mask;
546 sigfillset (&mask);
547 sigsuspend (&mask);
549 pthread_cleanup_pop (0);
551 FAIL_EXIT1 ("sigsuspend returned");
555 static void *
556 tf_sigwait (void *arg)
558 xpthread_barrier_wait (&b2);
560 if (arg != NULL)
561 xpthread_barrier_wait (&b2);
563 /* Block SIGUSR1. */
564 sigset_t mask;
565 sigemptyset (&mask);
566 sigaddset (&mask, SIGUSR1);
567 TEST_VERIFY_EXIT (pthread_sigmask (SIG_BLOCK, &mask, NULL) == 0);
569 int sig;
570 pthread_cleanup_push (cl, NULL);
572 /* Wait for SIGUSR1. */
573 sigwait (&mask, &sig);
575 pthread_cleanup_pop (0);
577 FAIL_EXIT1 ("sigwait returned with signal %d", sig);
581 static void *
582 tf_sigwaitinfo (void *arg)
584 xpthread_barrier_wait (&b2);
586 if (arg != NULL)
587 xpthread_barrier_wait (&b2);
589 /* Block SIGUSR1. */
590 sigset_t mask;
591 sigemptyset (&mask);
592 sigaddset (&mask, SIGUSR1);
593 TEST_VERIFY_EXIT (pthread_sigmask (SIG_BLOCK, &mask, NULL) == 0);
595 siginfo_t info;
596 pthread_cleanup_push (cl, NULL);
598 /* Wait for SIGUSR1. */
599 sigwaitinfo (&mask, &info);
601 pthread_cleanup_pop (0);
603 FAIL_EXIT1 ("sigwaitinfo returned with signal %d", info.si_signo);
607 static void *
608 tf_sigtimedwait (void *arg)
610 xpthread_barrier_wait (&b2);
612 if (arg != NULL)
613 xpthread_barrier_wait (&b2);
615 /* Block SIGUSR1. */
616 sigset_t mask;
617 sigemptyset (&mask);
618 sigaddset (&mask, SIGUSR1);
619 TEST_VERIFY_EXIT (pthread_sigmask (SIG_BLOCK, &mask, NULL) == 0);
621 /* Wait for SIGUSR1. */
622 siginfo_t info;
623 struct timespec ts = { .tv_sec = 60, .tv_nsec = 0 };
624 pthread_cleanup_push (cl, NULL);
626 sigtimedwait (&mask, &info, &ts);
628 pthread_cleanup_pop (0);
630 FAIL_EXIT1 ("sigtimedwait returned with signal %d", info.si_signo);
634 static void *
635 tf_pause (void *arg)
637 xpthread_barrier_wait (&b2);
639 if (arg != NULL)
640 xpthread_barrier_wait (&b2);
642 pthread_cleanup_push (cl, NULL);
644 pause ();
646 pthread_cleanup_pop (0);
648 FAIL_EXIT1 ("pause returned");
652 static void *
653 tf_accept (void *arg)
655 struct sockaddr_un sun;
656 /* To test a non-blocking accept call we make the call file by using
657 a datagrame socket. */
658 int pf = arg == NULL ? SOCK_STREAM : SOCK_DGRAM;
660 tempfd = socket (AF_UNIX, pf, 0);
661 if (tempfd == -1)
662 FAIL_EXIT1 ("socket (AF_UNIX, %s, 0): %m", arg == NULL ? "SOCK_STREAM"
663 : "SOCK_DGRAM");
665 int tries = 0;
668 TEST_VERIFY_EXIT (tries++ < 10);
670 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-1-XXXXXX");
671 TEST_VERIFY_EXIT (mktemp (sun.sun_path) != NULL);
673 sun.sun_family = AF_UNIX;
675 while (bind (tempfd, (struct sockaddr *) &sun,
676 offsetof (struct sockaddr_un, sun_path)
677 + strlen (sun.sun_path) + 1) != 0);
679 unlink (sun.sun_path);
681 listen (tempfd, 5);
683 socklen_t len = sizeof (sun);
685 xpthread_barrier_wait (&b2);
687 if (arg != NULL)
688 xpthread_barrier_wait (&b2);
690 pthread_cleanup_push (cl, NULL);
692 accept (tempfd, (struct sockaddr *) &sun, &len);
694 pthread_cleanup_pop (0);
696 FAIL_EXIT1 ("accept returned");
700 static void *
701 tf_send (void *arg)
703 struct sockaddr_un sun;
705 tempfd = socket (AF_UNIX, SOCK_STREAM, 0);
706 if (tempfd == -1)
707 FAIL_EXIT1 ("socket (AF_UNIX, SOCK_STREAM, 0): %m");
709 int tries = 0;
712 TEST_VERIFY_EXIT (tries++ < 10);
714 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-2-XXXXXX");
715 TEST_VERIFY_EXIT (mktemp (sun.sun_path) != NULL);
717 sun.sun_family = AF_UNIX;
719 while (bind (tempfd, (struct sockaddr *) &sun,
720 offsetof (struct sockaddr_un, sun_path)
721 + strlen (sun.sun_path) + 1) != 0);
723 listen (tempfd, 5);
725 tempfd2 = socket (AF_UNIX, SOCK_STREAM, 0);
726 if (tempfd2 == -1)
727 FAIL_EXIT1 ("socket (AF_UNIX, SOCK_STREAM, 0): %m");
729 set_socket_buffer (tempfd2);
731 if (connect (tempfd2, (struct sockaddr *) &sun, sizeof (sun)) != 0)
732 FAIL_EXIT1 ("connect: %m");
734 unlink (sun.sun_path);
736 xpthread_barrier_wait (&b2);
738 if (arg != NULL)
739 xpthread_barrier_wait (&b2);
741 pthread_cleanup_push (cl, NULL);
743 char mem[WRITE_BUFFER_SIZE];
745 send (tempfd2, mem, arg == NULL ? sizeof (mem) : 1, 0);
747 pthread_cleanup_pop (0);
749 FAIL_EXIT1 ("send returned");
753 static void *
754 tf_recv (void *arg)
756 struct sockaddr_un sun;
758 tempfd = socket (AF_UNIX, SOCK_STREAM, 0);
759 if (tempfd == -1)
760 FAIL_EXIT1 ("socket (AF_UNIX, SOCK_STREAM, 0): %m");
762 int tries = 0;
765 TEST_VERIFY_EXIT (tries++ < 10);
767 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-3-XXXXXX");
768 TEST_VERIFY_EXIT (mktemp (sun.sun_path) != NULL);
770 sun.sun_family = AF_UNIX;
772 while (bind (tempfd, (struct sockaddr *) &sun,
773 offsetof (struct sockaddr_un, sun_path)
774 + strlen (sun.sun_path) + 1) != 0);
776 listen (tempfd, 5);
778 tempfd2 = socket (AF_UNIX, SOCK_STREAM, 0);
779 if (tempfd2 == -1)
780 FAIL_EXIT1 ("socket (AF_UNIX, SOCK_STREAM, 0): %m");
782 if (connect (tempfd2, (struct sockaddr *) &sun, sizeof (sun)) != 0)
783 FAIL_EXIT1 ("connect: %m");
785 unlink (sun.sun_path);
787 xpthread_barrier_wait (&b2);
789 if (arg != NULL)
790 xpthread_barrier_wait (&b2);
792 pthread_cleanup_push (cl, NULL);
794 char mem[70];
796 recv (tempfd2, mem, arg == NULL ? sizeof (mem) : 0, 0);
798 pthread_cleanup_pop (0);
800 FAIL_EXIT1 ("recv returned");
804 static void *
805 tf_recvfrom (void *arg)
807 struct sockaddr_un sun;
809 tempfd = socket (AF_UNIX, SOCK_DGRAM, 0);
810 if (tempfd == -1)
811 FAIL_EXIT1 ("socket (AF_UNIX, SOCK_DGRAM, 0): %m");
813 int tries = 0;
816 TEST_VERIFY_EXIT (tries++ < 10);
818 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-4-XXXXXX");
819 TEST_VERIFY_EXIT (mktemp (sun.sun_path) != NULL);
821 sun.sun_family = AF_UNIX;
823 while (bind (tempfd, (struct sockaddr *) &sun,
824 offsetof (struct sockaddr_un, sun_path)
825 + strlen (sun.sun_path) + 1) != 0);
827 tempfname = strdup (sun.sun_path);
829 tempfd2 = socket (AF_UNIX, SOCK_DGRAM, 0);
830 if (tempfd2 == -1)
831 FAIL_EXIT1 ("socket (AF_UNIX, SOCK_DGRAM, 0): %m");
833 xpthread_barrier_wait (&b2);
835 if (arg != NULL)
836 xpthread_barrier_wait (&b2);
838 pthread_cleanup_push (cl, NULL);
840 char mem[70];
841 socklen_t len = sizeof (sun);
843 recvfrom (tempfd2, mem, arg == NULL ? sizeof (mem) : 0, 0,
844 (struct sockaddr *) &sun, &len);
846 pthread_cleanup_pop (0);
848 FAIL_EXIT1 ("recvfrom returned");
852 static void *
853 tf_recvmsg (void *arg)
855 struct sockaddr_un sun;
857 tempfd = socket (AF_UNIX, SOCK_DGRAM, 0);
858 if (tempfd == -1)
859 FAIL_EXIT1 ("socket (AF_UNIX, SOCK_DGRAM, 0): %m");
861 int tries = 0;
864 TEST_VERIFY_EXIT (tries++ < 10);
866 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-5-XXXXXX");
867 TEST_VERIFY_EXIT (mktemp (sun.sun_path) != NULL);
869 sun.sun_family = AF_UNIX;
871 while (bind (tempfd, (struct sockaddr *) &sun,
872 offsetof (struct sockaddr_un, sun_path)
873 + strlen (sun.sun_path) + 1) != 0);
875 tempfname = strdup (sun.sun_path);
877 tempfd2 = socket (AF_UNIX, SOCK_DGRAM, 0);
878 if (tempfd2 == -1)
879 FAIL_EXIT1 ("socket (AF_UNIX, SOCK_DGRAM, 0): %m");
881 xpthread_barrier_wait (&b2);
883 if (arg != NULL)
884 xpthread_barrier_wait (&b2);
886 pthread_cleanup_push (cl, NULL);
888 char mem[70];
889 struct iovec iov[1];
890 iov[0].iov_base = mem;
891 iov[0].iov_len = arg == NULL ? sizeof (mem) : 0;
893 struct msghdr m;
894 m.msg_name = &sun;
895 m.msg_namelen = sizeof (sun);
896 m.msg_iov = iov;
897 m.msg_iovlen = 1;
898 m.msg_control = NULL;
899 m.msg_controllen = 0;
901 recvmsg (tempfd2, &m, 0);
903 pthread_cleanup_pop (0);
905 FAIL_EXIT1 ("recvmsg returned");
908 static void *
909 tf_open (void *arg)
911 if (arg == NULL)
913 fifofd = mkfifo (fifoname, S_IWUSR | S_IRUSR);
914 if (fifofd == -1)
915 FAIL_EXIT1 ("mkfifo: %m");
917 else
919 xpthread_barrier_wait (&b2);
922 xpthread_barrier_wait (&b2);
924 pthread_cleanup_push (cl_fifo, NULL);
926 open (arg ? "Makefile" : fifoname, O_RDONLY);
928 pthread_cleanup_pop (0);
930 FAIL_EXIT1 ("open returned");
934 static void *
935 tf_close (void *arg)
937 if (arg == NULL)
938 // XXX If somebody can provide a portable test case in which close()
939 // blocks we can enable this test to run in both rounds.
940 abort ();
942 char fname[] = "/tmp/tst-cancel-fd-XXXXXX";
943 tempfd = mkstemp (fname);
944 if (tempfd == -1)
945 FAIL_EXIT1 ("mkstemp failed: %m");
946 unlink (fname);
948 xpthread_barrier_wait (&b2);
950 xpthread_barrier_wait (&b2);
952 pthread_cleanup_push (cl, NULL);
954 close (tempfd);
956 pthread_cleanup_pop (0);
958 FAIL_EXIT1 ("close returned");
962 static void *
963 tf_pread (void *arg)
965 if (arg == NULL)
966 // XXX If somebody can provide a portable test case in which pread()
967 // blocks we can enable this test to run in both rounds.
968 abort ();
970 tempfd = open ("Makefile", O_RDONLY);
971 if (tempfd == -1)
972 FAIL_EXIT1 ("open (\"Makefile\", O_RDONLY): %m");
974 xpthread_barrier_wait (&b2);
976 xpthread_barrier_wait (&b2);
978 pthread_cleanup_push (cl, NULL);
980 char mem[10];
981 pread (tempfd, mem, sizeof (mem), 0);
983 pthread_cleanup_pop (0);
985 FAIL_EXIT1 ("pread returned");
989 static void *
990 tf_pwrite (void *arg)
992 if (arg == NULL)
993 // XXX If somebody can provide a portable test case in which pwrite()
994 // blocks we can enable this test to run in both rounds.
995 abort ();
997 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
998 tempfd = mkstemp (fname);
999 if (tempfd == -1)
1000 FAIL_EXIT1 ("mkstemp failed: %m");
1001 unlink (fname);
1003 xpthread_barrier_wait (&b2);
1005 xpthread_barrier_wait (&b2);
1007 pthread_cleanup_push (cl, NULL);
1009 char mem[10];
1010 pwrite (tempfd, mem, sizeof (mem), 0);
1012 pthread_cleanup_pop (0);
1014 FAIL_EXIT1 ("pwrite returned");
1017 static void *
1018 tf_preadv (void *arg)
1020 int fd;
1022 if (arg == NULL)
1023 /* XXX If somebody can provide a portable test case in which preadv
1024 blocks we can enable this test to run in both rounds. */
1025 abort ();
1027 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
1028 tempfd = fd = mkstemp (fname);
1029 if (fd == -1)
1030 FAIL_EXIT1 ("mkstemp failed: %m");
1031 unlink (fname);
1033 xpthread_barrier_wait (&b2);
1035 xpthread_barrier_wait (&b2);
1037 ssize_t s;
1038 pthread_cleanup_push (cl, NULL);
1040 char buf[100];
1041 struct iovec iov[1] = { [0] = { .iov_base = buf, .iov_len = sizeof (buf) } };
1042 s = preadv (fd, iov, 1, 0);
1044 pthread_cleanup_pop (0);
1046 FAIL_EXIT1 ("preadv returns with %zd", s);
1049 static void *
1050 tf_pwritev (void *arg)
1052 int fd;
1054 if (arg == NULL)
1055 /* XXX If somebody can provide a portable test case in which pwritev
1056 blocks we can enable this test to run in both rounds. */
1057 abort ();
1059 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
1060 tempfd = fd = mkstemp (fname);
1061 if (fd == -1)
1062 FAIL_EXIT1 ("mkstemp failed: %m");
1063 unlink (fname);
1065 xpthread_barrier_wait (&b2);
1067 xpthread_barrier_wait (&b2);
1069 ssize_t s;
1070 pthread_cleanup_push (cl, NULL);
1072 char buf[WRITE_BUFFER_SIZE];
1073 memset (buf, '\0', sizeof (buf));
1074 struct iovec iov[1] = { [0] = { .iov_base = buf, .iov_len = sizeof (buf) } };
1075 s = pwritev (fd, iov, 1, 0);
1077 pthread_cleanup_pop (0);
1079 FAIL_EXIT1 ("pwritev returns with %zd", s);
1082 static void *
1083 tf_pwritev2 (void *arg)
1085 int fd;
1087 if (arg == NULL)
1088 /* XXX If somebody can provide a portable test case in which pwritev2
1089 blocks we can enable this test to run in both rounds. */
1090 abort ();
1092 errno = 0;
1094 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
1095 tempfd = fd = mkstemp (fname);
1096 if (fd == -1)
1097 FAIL_EXIT1 ("mkstemp: %m");
1098 unlink (fname);
1100 xpthread_barrier_wait (&b2);
1102 xpthread_barrier_wait (&b2);
1104 ssize_t s;
1105 pthread_cleanup_push (cl, NULL);
1107 char buf[WRITE_BUFFER_SIZE];
1108 memset (buf, '\0', sizeof (buf));
1109 struct iovec iov[1] = { [0] = { .iov_base = buf, .iov_len = sizeof (buf) } };
1110 s = pwritev2 (fd, iov, 1, 0, 0);
1112 pthread_cleanup_pop (0);
1114 FAIL_EXIT1 ("pwritev2 returns with %zd", s);
1117 static void *
1118 tf_preadv2 (void *arg)
1120 int fd;
1122 if (arg == NULL)
1123 /* XXX If somebody can provide a portable test case in which preadv2
1124 blocks we can enable this test to run in both rounds. */
1125 abort ();
1127 errno = 0;
1129 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
1130 tempfd = fd = mkstemp (fname);
1131 if (fd == -1)
1132 FAIL_EXIT1 ("mkstemp failed: %m");
1133 unlink (fname);
1135 xpthread_barrier_wait (&b2);
1137 xpthread_barrier_wait (&b2);
1139 ssize_t s;
1140 pthread_cleanup_push (cl, NULL);
1142 char buf[100];
1143 struct iovec iov[1] = { [0] = { .iov_base = buf, .iov_len = sizeof (buf) } };
1144 s = preadv2 (fd, iov, 1, 0, 0);
1146 pthread_cleanup_pop (0);
1148 FAIL_EXIT1 ("preadv2 returns with %zd", s);
1151 static void *
1152 tf_fsync (void *arg)
1154 if (arg == NULL)
1155 // XXX If somebody can provide a portable test case in which fsync()
1156 // blocks we can enable this test to run in both rounds.
1157 abort ();
1159 tempfd = open ("Makefile", O_RDONLY);
1160 if (tempfd == -1)
1161 FAIL_EXIT1 ("open (\"Makefile\", O_RDONLY): %m");
1163 xpthread_barrier_wait (&b2);
1165 xpthread_barrier_wait (&b2);
1167 pthread_cleanup_push (cl, NULL);
1169 fsync (tempfd);
1171 pthread_cleanup_pop (0);
1173 FAIL_EXIT1 ("fsync returned");
1177 static void *
1178 tf_fdatasync (void *arg)
1180 if (arg == NULL)
1181 // XXX If somebody can provide a portable test case in which fdatasync()
1182 // blocks we can enable this test to run in both rounds.
1183 abort ();
1185 tempfd = open ("Makefile", O_RDONLY);
1186 if (tempfd == -1)
1187 FAIL_EXIT1 ("open (\"Makefile\", O_RDONLY): %m");
1189 xpthread_barrier_wait (&b2);
1191 xpthread_barrier_wait (&b2);
1193 pthread_cleanup_push (cl, NULL);
1195 fdatasync (tempfd);
1197 pthread_cleanup_pop (0);
1199 FAIL_EXIT1 ("fdatasync returned");
1203 static void *
1204 tf_msync (void *arg)
1206 if (arg == NULL)
1207 // XXX If somebody can provide a portable test case in which msync()
1208 // blocks we can enable this test to run in both rounds.
1209 abort ();
1211 tempfd = open ("Makefile", O_RDONLY);
1212 if (tempfd == -1)
1213 FAIL_EXIT1 ("open (\"Makefile\", O_RDONLY): %m");
1215 void *p = xmmap (NULL, 10, PROT_READ, MAP_SHARED, tempfd);
1217 xpthread_barrier_wait (&b2);
1219 xpthread_barrier_wait (&b2);
1221 pthread_cleanup_push (cl, NULL);
1223 msync (p, 10, 0);
1225 pthread_cleanup_pop (0);
1227 FAIL_EXIT1 ("msync returned");
1231 static void *
1232 tf_sendto (void *arg)
1234 struct sockaddr_un sun;
1236 tempfd = socket (AF_UNIX, SOCK_STREAM, 0);
1237 if (tempfd == -1)
1238 FAIL_EXIT1 ("socket (AF_UNIX, SOCK_STREAM, 0): %m");
1240 int tries = 0;
1243 TEST_VERIFY_EXIT (tries++ < 10);
1245 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-6-XXXXXX");
1246 TEST_VERIFY_EXIT (mktemp (sun.sun_path) != NULL);
1248 sun.sun_family = AF_UNIX;
1250 while (bind (tempfd, (struct sockaddr *) &sun,
1251 offsetof (struct sockaddr_un, sun_path)
1252 + strlen (sun.sun_path) + 1) != 0);
1254 listen (tempfd, 5);
1256 tempfd2 = socket (AF_UNIX, SOCK_STREAM, 0);
1257 if (tempfd2 == -1)
1258 FAIL_EXIT1 ("socket (AF_UNIX, SOCK_STREAM, 0): %m");
1260 set_socket_buffer (tempfd2);
1262 if (connect (tempfd2, (struct sockaddr *) &sun, sizeof (sun)) != 0)
1263 FAIL_EXIT1 ("connect: %m");
1265 unlink (sun.sun_path);
1267 xpthread_barrier_wait (&b2);
1269 if (arg != NULL)
1270 xpthread_barrier_wait (&b2);
1272 pthread_cleanup_push (cl, NULL);
1274 char mem[WRITE_BUFFER_SIZE];
1276 sendto (tempfd2, mem, arg == NULL ? sizeof (mem) : 1, 0, NULL, 0);
1278 pthread_cleanup_pop (0);
1280 FAIL_EXIT1 ("sendto returned");
1284 static void *
1285 tf_sendmsg (void *arg)
1287 if (arg == NULL)
1288 // XXX If somebody can provide a portable test case in which sendmsg()
1289 // blocks we can enable this test to run in both rounds.
1290 abort ();
1292 struct sockaddr_un sun;
1294 tempfd = socket (AF_UNIX, SOCK_DGRAM, 0);
1295 if (tempfd == -1)
1296 FAIL_EXIT1 ("socket (AF_UNIX, SOCK_DGRAM, 0): %m");
1298 int tries = 0;
1301 TEST_VERIFY_EXIT (tries++ < 10);
1303 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-7-XXXXXX");
1304 TEST_VERIFY_EXIT (mktemp (sun.sun_path) != NULL);
1306 sun.sun_family = AF_UNIX;
1308 while (bind (tempfd, (struct sockaddr *) &sun,
1309 offsetof (struct sockaddr_un, sun_path)
1310 + strlen (sun.sun_path) + 1) != 0);
1311 tempfname = strdup (sun.sun_path);
1313 tempfd2 = socket (AF_UNIX, SOCK_DGRAM, 0);
1314 if (tempfd2 == -1)
1315 FAIL_EXIT1 ("socket (AF_UNIX, SOCK_DGRAM, 0): %m");
1317 xpthread_barrier_wait (&b2);
1319 xpthread_barrier_wait (&b2);
1321 pthread_cleanup_push (cl, NULL);
1323 char mem[1];
1324 struct iovec iov[1];
1325 iov[0].iov_base = mem;
1326 iov[0].iov_len = 1;
1328 struct msghdr m;
1329 m.msg_name = &sun;
1330 m.msg_namelen = (offsetof (struct sockaddr_un, sun_path)
1331 + strlen (sun.sun_path) + 1);
1332 m.msg_iov = iov;
1333 m.msg_iovlen = 1;
1334 m.msg_control = NULL;
1335 m.msg_controllen = 0;
1337 sendmsg (tempfd2, &m, 0);
1339 pthread_cleanup_pop (0);
1341 FAIL_EXIT1 ("sendmsg returned");
1345 static void *
1346 tf_creat (void *arg)
1348 if (arg == NULL)
1349 // XXX If somebody can provide a portable test case in which sendmsg()
1350 // blocks we can enable this test to run in both rounds.
1351 abort ();
1353 xpthread_barrier_wait (&b2);
1355 xpthread_barrier_wait (&b2);
1357 pthread_cleanup_push (cl, NULL);
1359 creat ("tmp/tst-cancel-4-should-not-exist", 0666);
1361 pthread_cleanup_pop (0);
1363 FAIL_EXIT1 ("creat returned");
1367 static void *
1368 tf_connect (void *arg)
1370 if (arg == NULL)
1371 // XXX If somebody can provide a portable test case in which connect()
1372 // blocks we can enable this test to run in both rounds.
1373 abort ();
1375 struct sockaddr_un sun;
1377 tempfd = socket (AF_UNIX, SOCK_STREAM, 0);
1378 if (tempfd == -1)
1379 FAIL_EXIT1 ("socket (AF_UNIX, SOCK_STREAM, 0): %m");
1381 int tries = 0;
1384 TEST_VERIFY_EXIT (tries++ < 10);
1386 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-2-XXXXXX");
1387 TEST_VERIFY_EXIT (mktemp (sun.sun_path) != NULL);
1389 sun.sun_family = AF_UNIX;
1391 while (bind (tempfd, (struct sockaddr *) &sun,
1392 offsetof (struct sockaddr_un, sun_path)
1393 + strlen (sun.sun_path) + 1) != 0);
1394 tempfname = strdup (sun.sun_path);
1396 listen (tempfd, 5);
1398 tempfd2 = socket (AF_UNIX, SOCK_STREAM, 0);
1399 if (tempfd2 == -1)
1400 FAIL_EXIT1 ("socket (AF_UNIX, SOCK_STREAM, 0): %m");
1402 xpthread_barrier_wait (&b2);
1404 if (arg != NULL)
1405 xpthread_barrier_wait (&b2);
1407 pthread_cleanup_push (cl, NULL);
1409 connect (tempfd2, (struct sockaddr *) &sun, sizeof (sun));
1411 pthread_cleanup_pop (0);
1413 FAIL_EXIT1 ("connect returned");
1417 static void *
1418 tf_tcdrain (void *arg)
1420 if (arg == NULL)
1421 // XXX If somebody can provide a portable test case in which tcdrain()
1422 // blocks we can enable this test to run in both rounds.
1423 abort ();
1425 xpthread_barrier_wait (&b2);
1427 if (arg != NULL)
1428 xpthread_barrier_wait (&b2);
1430 pthread_cleanup_push (cl, NULL);
1432 /* Regardless of stderr being a terminal, the tcdrain call should be
1433 canceled. */
1434 tcdrain (STDERR_FILENO);
1436 pthread_cleanup_pop (0);
1438 FAIL_EXIT1 ("tcdrain returned");
1442 static void *
1443 tf_msgrcv (void *arg)
1445 tempmsg = msgget (IPC_PRIVATE, 0666 | IPC_CREAT);
1446 if (tempmsg == -1)
1447 FAIL_EXIT1 ("msgget (IPC_PRIVATE, 0666 | IPC_CREAT): %m");
1449 xpthread_barrier_wait (&b2);
1451 if (arg != NULL)
1452 xpthread_barrier_wait (&b2);
1454 ssize_t s;
1456 pthread_cleanup_push (cl, NULL);
1458 struct
1460 long int type;
1461 char mem[10];
1462 } m;
1463 int randnr;
1464 /* We need a positive random number. */
1466 randnr = random () % 64000;
1467 while (randnr <= 0);
1470 errno = 0;
1471 s = msgrcv (tempmsg, (struct msgbuf *) &m, 10, randnr, 0);
1473 while (errno == EIDRM || errno == EINTR);
1475 pthread_cleanup_pop (0);
1477 msgctl (tempmsg, IPC_RMID, NULL);
1479 FAIL_EXIT1 ("msgrcv returned %zd", s);
1483 static void *
1484 tf_msgsnd (void *arg)
1486 if (arg == NULL)
1487 // XXX If somebody can provide a portable test case in which msgsnd()
1488 // blocks we can enable this test to run in both rounds.
1489 abort ();
1491 tempmsg = msgget (IPC_PRIVATE, 0666 | IPC_CREAT);
1492 if (tempmsg == -1)
1493 FAIL_EXIT1 ("msgget (IPC_PRIVATE, 0666 | IPC_CREAT): %m");
1495 xpthread_barrier_wait (&b2);
1497 xpthread_barrier_wait (&b2);
1499 pthread_cleanup_push (cl, NULL);
1501 struct
1503 long int type;
1504 char mem[1];
1505 } m;
1506 /* We need a positive random number. */
1508 m.type = random () % 64000;
1509 while (m.type <= 0);
1510 msgsnd (tempmsg, (struct msgbuf *) &m, sizeof (m.mem), 0);
1512 pthread_cleanup_pop (0);
1514 msgctl (tempmsg, IPC_RMID, NULL);
1516 FAIL_EXIT1 ("msgsnd returned");
1520 struct cancel_tests tests[] =
1522 ADD_TEST (read, 2, 0),
1523 ADD_TEST (readv, 2, 0),
1524 ADD_TEST (select, 2, 0),
1525 ADD_TEST (pselect, 2, 0),
1526 ADD_TEST (poll, 2, 0),
1527 ADD_TEST (ppoll, 2, 0),
1528 ADD_TEST (write, 2, 0),
1529 ADD_TEST (writev, 2, 0),
1530 ADD_TEST (sleep, 2, 0),
1531 ADD_TEST (usleep, 2, 0),
1532 ADD_TEST (nanosleep, 2, 0),
1533 ADD_TEST (wait, 2, 0),
1534 ADD_TEST (waitid, 2, 0),
1535 ADD_TEST (waitpid, 2, 0),
1536 ADD_TEST (sigpause, 2, 0),
1537 ADD_TEST (sigsuspend, 2, 0),
1538 ADD_TEST (sigwait, 2, 0),
1539 ADD_TEST (sigwaitinfo, 2, 0),
1540 ADD_TEST (sigtimedwait, 2, 0),
1541 ADD_TEST (pause, 2, 0),
1542 ADD_TEST (accept, 2, 0),
1543 ADD_TEST (send, 2, 0),
1544 ADD_TEST (recv, 2, 0),
1545 ADD_TEST (recvfrom, 2, 0),
1546 ADD_TEST (recvmsg, 2, 0),
1547 ADD_TEST (preadv, 2, 1),
1548 ADD_TEST (preadv2, 2, 1),
1549 ADD_TEST (pwritev, 2, 1),
1550 ADD_TEST (pwritev2, 2, 1),
1551 ADD_TEST (open, 2, 1),
1552 ADD_TEST (close, 2, 1),
1553 ADD_TEST (pread, 2, 1),
1554 ADD_TEST (pwrite, 2, 1),
1555 ADD_TEST (fsync, 2, 1),
1556 ADD_TEST (fdatasync, 2, 1),
1557 ADD_TEST (msync, 2, 1),
1558 ADD_TEST (sendto, 2, 1),
1559 ADD_TEST (sendmsg, 2, 1),
1560 ADD_TEST (creat, 2, 1),
1561 ADD_TEST (connect, 2, 1),
1562 ADD_TEST (tcdrain, 2, 1),
1563 ADD_TEST (msgrcv, 2, 0),
1564 ADD_TEST (msgsnd, 2, 1),
1566 #define ntest_tf (sizeof (tests) / sizeof (tests[0]))
1568 #include "tst-cancel4-common.c"