malloc: Ensure that the consolidated fast chunk has a sane size.
[glibc.git] / nptl / tst-cancel4.c
blob92a3d808bd05976aa395bcb91b318415d41b83e2
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 if (connect (tempfd2, (struct sockaddr *) &sun, sizeof (sun)) != 0)
730 FAIL_EXIT1 ("connect: %m");
732 unlink (sun.sun_path);
734 xpthread_barrier_wait (&b2);
736 if (arg != NULL)
737 xpthread_barrier_wait (&b2);
739 pthread_cleanup_push (cl, NULL);
741 /* Very large block, so that the send call blocks. */
742 char mem[700000];
744 send (tempfd2, mem, arg == NULL ? sizeof (mem) : 1, 0);
746 pthread_cleanup_pop (0);
748 FAIL_EXIT1 ("send returned");
752 static void *
753 tf_recv (void *arg)
755 struct sockaddr_un sun;
757 tempfd = socket (AF_UNIX, SOCK_STREAM, 0);
758 if (tempfd == -1)
759 FAIL_EXIT1 ("socket (AF_UNIX, SOCK_STREAM, 0): %m");
761 int tries = 0;
764 TEST_VERIFY_EXIT (tries++ < 10);
766 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-3-XXXXXX");
767 TEST_VERIFY_EXIT (mktemp (sun.sun_path) != NULL);
769 sun.sun_family = AF_UNIX;
771 while (bind (tempfd, (struct sockaddr *) &sun,
772 offsetof (struct sockaddr_un, sun_path)
773 + strlen (sun.sun_path) + 1) != 0);
775 listen (tempfd, 5);
777 tempfd2 = socket (AF_UNIX, SOCK_STREAM, 0);
778 if (tempfd2 == -1)
779 FAIL_EXIT1 ("socket (AF_UNIX, SOCK_STREAM, 0): %m");
781 if (connect (tempfd2, (struct sockaddr *) &sun, sizeof (sun)) != 0)
782 FAIL_EXIT1 ("connect: %m");
784 unlink (sun.sun_path);
786 xpthread_barrier_wait (&b2);
788 if (arg != NULL)
789 xpthread_barrier_wait (&b2);
791 pthread_cleanup_push (cl, NULL);
793 char mem[70];
795 recv (tempfd2, mem, arg == NULL ? sizeof (mem) : 0, 0);
797 pthread_cleanup_pop (0);
799 FAIL_EXIT1 ("recv returned");
803 static void *
804 tf_recvfrom (void *arg)
806 struct sockaddr_un sun;
808 tempfd = socket (AF_UNIX, SOCK_DGRAM, 0);
809 if (tempfd == -1)
810 FAIL_EXIT1 ("socket (AF_UNIX, SOCK_DGRAM, 0): %m");
812 int tries = 0;
815 TEST_VERIFY_EXIT (tries++ < 10);
817 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-4-XXXXXX");
818 TEST_VERIFY_EXIT (mktemp (sun.sun_path) != NULL);
820 sun.sun_family = AF_UNIX;
822 while (bind (tempfd, (struct sockaddr *) &sun,
823 offsetof (struct sockaddr_un, sun_path)
824 + strlen (sun.sun_path) + 1) != 0);
826 tempfname = strdup (sun.sun_path);
828 tempfd2 = socket (AF_UNIX, SOCK_DGRAM, 0);
829 if (tempfd2 == -1)
830 FAIL_EXIT1 ("socket (AF_UNIX, SOCK_DGRAM, 0): %m");
832 xpthread_barrier_wait (&b2);
834 if (arg != NULL)
835 xpthread_barrier_wait (&b2);
837 pthread_cleanup_push (cl, NULL);
839 char mem[70];
840 socklen_t len = sizeof (sun);
842 recvfrom (tempfd2, mem, arg == NULL ? sizeof (mem) : 0, 0,
843 (struct sockaddr *) &sun, &len);
845 pthread_cleanup_pop (0);
847 FAIL_EXIT1 ("recvfrom returned");
851 static void *
852 tf_recvmsg (void *arg)
854 struct sockaddr_un sun;
856 tempfd = socket (AF_UNIX, SOCK_DGRAM, 0);
857 if (tempfd == -1)
858 FAIL_EXIT1 ("socket (AF_UNIX, SOCK_DGRAM, 0): %m");
860 int tries = 0;
863 TEST_VERIFY_EXIT (tries++ < 10);
865 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-5-XXXXXX");
866 TEST_VERIFY_EXIT (mktemp (sun.sun_path) != NULL);
868 sun.sun_family = AF_UNIX;
870 while (bind (tempfd, (struct sockaddr *) &sun,
871 offsetof (struct sockaddr_un, sun_path)
872 + strlen (sun.sun_path) + 1) != 0);
874 tempfname = strdup (sun.sun_path);
876 tempfd2 = socket (AF_UNIX, SOCK_DGRAM, 0);
877 if (tempfd2 == -1)
878 FAIL_EXIT1 ("socket (AF_UNIX, SOCK_DGRAM, 0): %m");
880 xpthread_barrier_wait (&b2);
882 if (arg != NULL)
883 xpthread_barrier_wait (&b2);
885 pthread_cleanup_push (cl, NULL);
887 char mem[70];
888 struct iovec iov[1];
889 iov[0].iov_base = mem;
890 iov[0].iov_len = arg == NULL ? sizeof (mem) : 0;
892 struct msghdr m;
893 m.msg_name = &sun;
894 m.msg_namelen = sizeof (sun);
895 m.msg_iov = iov;
896 m.msg_iovlen = 1;
897 m.msg_control = NULL;
898 m.msg_controllen = 0;
900 recvmsg (tempfd2, &m, 0);
902 pthread_cleanup_pop (0);
904 FAIL_EXIT1 ("recvmsg returned");
907 static void *
908 tf_open (void *arg)
910 if (arg == NULL)
912 fifofd = mkfifo (fifoname, S_IWUSR | S_IRUSR);
913 if (fifofd == -1)
914 FAIL_EXIT1 ("mkfifo: %m");
916 else
918 xpthread_barrier_wait (&b2);
921 xpthread_barrier_wait (&b2);
923 pthread_cleanup_push (cl_fifo, NULL);
925 open (arg ? "Makefile" : fifoname, O_RDONLY);
927 pthread_cleanup_pop (0);
929 FAIL_EXIT1 ("open returned");
933 static void *
934 tf_close (void *arg)
936 if (arg == NULL)
937 // XXX If somebody can provide a portable test case in which close()
938 // blocks we can enable this test to run in both rounds.
939 abort ();
941 char fname[] = "/tmp/tst-cancel-fd-XXXXXX";
942 tempfd = mkstemp (fname);
943 if (tempfd == -1)
944 FAIL_EXIT1 ("mkstemp failed: %m");
945 unlink (fname);
947 xpthread_barrier_wait (&b2);
949 xpthread_barrier_wait (&b2);
951 pthread_cleanup_push (cl, NULL);
953 close (tempfd);
955 pthread_cleanup_pop (0);
957 FAIL_EXIT1 ("close returned");
961 static void *
962 tf_pread (void *arg)
964 if (arg == NULL)
965 // XXX If somebody can provide a portable test case in which pread()
966 // blocks we can enable this test to run in both rounds.
967 abort ();
969 tempfd = open ("Makefile", O_RDONLY);
970 if (tempfd == -1)
971 FAIL_EXIT1 ("open (\"Makefile\", O_RDONLY): %m");
973 xpthread_barrier_wait (&b2);
975 xpthread_barrier_wait (&b2);
977 pthread_cleanup_push (cl, NULL);
979 char mem[10];
980 pread (tempfd, mem, sizeof (mem), 0);
982 pthread_cleanup_pop (0);
984 FAIL_EXIT1 ("pread returned");
988 static void *
989 tf_pwrite (void *arg)
991 if (arg == NULL)
992 // XXX If somebody can provide a portable test case in which pwrite()
993 // blocks we can enable this test to run in both rounds.
994 abort ();
996 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
997 tempfd = mkstemp (fname);
998 if (tempfd == -1)
999 FAIL_EXIT1 ("mkstemp failed: %m");
1000 unlink (fname);
1002 xpthread_barrier_wait (&b2);
1004 xpthread_barrier_wait (&b2);
1006 pthread_cleanup_push (cl, NULL);
1008 char mem[10];
1009 pwrite (tempfd, mem, sizeof (mem), 0);
1011 pthread_cleanup_pop (0);
1013 FAIL_EXIT1 ("pwrite returned");
1016 static void *
1017 tf_preadv (void *arg)
1019 int fd;
1021 if (arg == NULL)
1022 /* XXX If somebody can provide a portable test case in which preadv
1023 blocks we can enable this test to run in both rounds. */
1024 abort ();
1026 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
1027 tempfd = fd = mkstemp (fname);
1028 if (fd == -1)
1029 FAIL_EXIT1 ("mkstemp failed: %m");
1030 unlink (fname);
1032 xpthread_barrier_wait (&b2);
1034 xpthread_barrier_wait (&b2);
1036 ssize_t s;
1037 pthread_cleanup_push (cl, NULL);
1039 char buf[100];
1040 struct iovec iov[1] = { [0] = { .iov_base = buf, .iov_len = sizeof (buf) } };
1041 s = preadv (fd, iov, 1, 0);
1043 pthread_cleanup_pop (0);
1045 FAIL_EXIT1 ("preadv returns with %zd", s);
1048 static void *
1049 tf_pwritev (void *arg)
1051 int fd;
1053 if (arg == NULL)
1054 /* XXX If somebody can provide a portable test case in which pwritev
1055 blocks we can enable this test to run in both rounds. */
1056 abort ();
1058 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
1059 tempfd = fd = mkstemp (fname);
1060 if (fd == -1)
1061 FAIL_EXIT1 ("mkstemp failed: %m");
1062 unlink (fname);
1064 xpthread_barrier_wait (&b2);
1066 xpthread_barrier_wait (&b2);
1068 ssize_t s;
1069 pthread_cleanup_push (cl, NULL);
1071 char buf[WRITE_BUFFER_SIZE];
1072 memset (buf, '\0', sizeof (buf));
1073 struct iovec iov[1] = { [0] = { .iov_base = buf, .iov_len = sizeof (buf) } };
1074 s = pwritev (fd, iov, 1, 0);
1076 pthread_cleanup_pop (0);
1078 FAIL_EXIT1 ("pwritev returns with %zd", s);
1081 static void *
1082 tf_pwritev2 (void *arg)
1084 int fd;
1086 if (arg == NULL)
1087 /* XXX If somebody can provide a portable test case in which pwritev2
1088 blocks we can enable this test to run in both rounds. */
1089 abort ();
1091 errno = 0;
1093 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
1094 tempfd = fd = mkstemp (fname);
1095 if (fd == -1)
1096 FAIL_EXIT1 ("mkstemp: %m");
1097 unlink (fname);
1099 xpthread_barrier_wait (&b2);
1101 xpthread_barrier_wait (&b2);
1103 ssize_t s;
1104 pthread_cleanup_push (cl, NULL);
1106 char buf[WRITE_BUFFER_SIZE];
1107 memset (buf, '\0', sizeof (buf));
1108 struct iovec iov[1] = { [0] = { .iov_base = buf, .iov_len = sizeof (buf) } };
1109 s = pwritev2 (fd, iov, 1, 0, 0);
1111 pthread_cleanup_pop (0);
1113 FAIL_EXIT1 ("pwritev2 returns with %zd", s);
1116 static void *
1117 tf_preadv2 (void *arg)
1119 int fd;
1121 if (arg == NULL)
1122 /* XXX If somebody can provide a portable test case in which preadv2
1123 blocks we can enable this test to run in both rounds. */
1124 abort ();
1126 errno = 0;
1128 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
1129 tempfd = fd = mkstemp (fname);
1130 if (fd == -1)
1131 FAIL_EXIT1 ("mkstemp failed: %m");
1132 unlink (fname);
1134 xpthread_barrier_wait (&b2);
1136 xpthread_barrier_wait (&b2);
1138 ssize_t s;
1139 pthread_cleanup_push (cl, NULL);
1141 char buf[100];
1142 struct iovec iov[1] = { [0] = { .iov_base = buf, .iov_len = sizeof (buf) } };
1143 s = preadv2 (fd, iov, 1, 0, 0);
1145 pthread_cleanup_pop (0);
1147 FAIL_EXIT1 ("preadv2 returns with %zd", s);
1150 static void *
1151 tf_fsync (void *arg)
1153 if (arg == NULL)
1154 // XXX If somebody can provide a portable test case in which fsync()
1155 // blocks we can enable this test to run in both rounds.
1156 abort ();
1158 tempfd = open ("Makefile", O_RDONLY);
1159 if (tempfd == -1)
1160 FAIL_EXIT1 ("open (\"Makefile\", O_RDONLY): %m");
1162 xpthread_barrier_wait (&b2);
1164 xpthread_barrier_wait (&b2);
1166 pthread_cleanup_push (cl, NULL);
1168 fsync (tempfd);
1170 pthread_cleanup_pop (0);
1172 FAIL_EXIT1 ("fsync returned");
1176 static void *
1177 tf_fdatasync (void *arg)
1179 if (arg == NULL)
1180 // XXX If somebody can provide a portable test case in which fdatasync()
1181 // blocks we can enable this test to run in both rounds.
1182 abort ();
1184 tempfd = open ("Makefile", O_RDONLY);
1185 if (tempfd == -1)
1186 FAIL_EXIT1 ("open (\"Makefile\", O_RDONLY): %m");
1188 xpthread_barrier_wait (&b2);
1190 xpthread_barrier_wait (&b2);
1192 pthread_cleanup_push (cl, NULL);
1194 fdatasync (tempfd);
1196 pthread_cleanup_pop (0);
1198 FAIL_EXIT1 ("fdatasync returned");
1202 static void *
1203 tf_msync (void *arg)
1205 if (arg == NULL)
1206 // XXX If somebody can provide a portable test case in which msync()
1207 // blocks we can enable this test to run in both rounds.
1208 abort ();
1210 tempfd = open ("Makefile", O_RDONLY);
1211 if (tempfd == -1)
1212 FAIL_EXIT1 ("open (\"Makefile\", O_RDONLY): %m");
1214 void *p = xmmap (NULL, 10, PROT_READ, MAP_SHARED, tempfd);
1216 xpthread_barrier_wait (&b2);
1218 xpthread_barrier_wait (&b2);
1220 pthread_cleanup_push (cl, NULL);
1222 msync (p, 10, 0);
1224 pthread_cleanup_pop (0);
1226 FAIL_EXIT1 ("msync returned");
1230 static void *
1231 tf_sendto (void *arg)
1233 if (arg == NULL)
1234 // XXX If somebody can provide a portable test case in which sendto()
1235 // blocks we can enable this test to run in both rounds.
1236 abort ();
1238 struct sockaddr_un sun;
1240 tempfd = socket (AF_UNIX, SOCK_DGRAM, 0);
1241 if (tempfd == -1)
1242 FAIL_EXIT1 ("socket (AF_UNIX, SOCK_DGRAM, 0): %m");
1244 int tries = 0;
1247 TEST_VERIFY_EXIT (tries++ < 10);
1249 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-6-XXXXXX");
1250 TEST_VERIFY_EXIT (mktemp (sun.sun_path) != NULL);
1252 sun.sun_family = AF_UNIX;
1254 while (bind (tempfd, (struct sockaddr *) &sun,
1255 offsetof (struct sockaddr_un, sun_path)
1256 + strlen (sun.sun_path) + 1) != 0);
1257 tempfname = strdup (sun.sun_path);
1259 tempfd2 = socket (AF_UNIX, SOCK_DGRAM, 0);
1260 if (tempfd2 == -1)
1261 FAIL_EXIT1 ("socket (AF_UNIX, SOCK_DGRAM, 0): %m");
1263 xpthread_barrier_wait (&b2);
1265 xpthread_barrier_wait (&b2);
1267 pthread_cleanup_push (cl, NULL);
1269 char mem[1];
1271 sendto (tempfd2, mem, arg == NULL ? sizeof (mem) : 1, 0,
1272 (struct sockaddr *) &sun,
1273 offsetof (struct sockaddr_un, sun_path) + strlen (sun.sun_path) + 1);
1275 pthread_cleanup_pop (0);
1277 FAIL_EXIT1 ("sendto returned");
1281 static void *
1282 tf_sendmsg (void *arg)
1284 if (arg == NULL)
1285 // XXX If somebody can provide a portable test case in which sendmsg()
1286 // blocks we can enable this test to run in both rounds.
1287 abort ();
1289 struct sockaddr_un sun;
1291 tempfd = socket (AF_UNIX, SOCK_DGRAM, 0);
1292 if (tempfd == -1)
1293 FAIL_EXIT1 ("socket (AF_UNIX, SOCK_DGRAM, 0): %m");
1295 int tries = 0;
1298 TEST_VERIFY_EXIT (tries++ < 10);
1300 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-7-XXXXXX");
1301 TEST_VERIFY_EXIT (mktemp (sun.sun_path) != NULL);
1303 sun.sun_family = AF_UNIX;
1305 while (bind (tempfd, (struct sockaddr *) &sun,
1306 offsetof (struct sockaddr_un, sun_path)
1307 + strlen (sun.sun_path) + 1) != 0);
1308 tempfname = strdup (sun.sun_path);
1310 tempfd2 = socket (AF_UNIX, SOCK_DGRAM, 0);
1311 if (tempfd2 == -1)
1312 FAIL_EXIT1 ("socket (AF_UNIX, SOCK_DGRAM, 0): %m");
1314 xpthread_barrier_wait (&b2);
1316 xpthread_barrier_wait (&b2);
1318 pthread_cleanup_push (cl, NULL);
1320 char mem[1];
1321 struct iovec iov[1];
1322 iov[0].iov_base = mem;
1323 iov[0].iov_len = 1;
1325 struct msghdr m;
1326 m.msg_name = &sun;
1327 m.msg_namelen = (offsetof (struct sockaddr_un, sun_path)
1328 + strlen (sun.sun_path) + 1);
1329 m.msg_iov = iov;
1330 m.msg_iovlen = 1;
1331 m.msg_control = NULL;
1332 m.msg_controllen = 0;
1334 sendmsg (tempfd2, &m, 0);
1336 pthread_cleanup_pop (0);
1338 FAIL_EXIT1 ("sendmsg returned");
1342 static void *
1343 tf_creat (void *arg)
1345 if (arg == NULL)
1346 // XXX If somebody can provide a portable test case in which sendmsg()
1347 // blocks we can enable this test to run in both rounds.
1348 abort ();
1350 xpthread_barrier_wait (&b2);
1352 xpthread_barrier_wait (&b2);
1354 pthread_cleanup_push (cl, NULL);
1356 creat ("tmp/tst-cancel-4-should-not-exist", 0666);
1358 pthread_cleanup_pop (0);
1360 FAIL_EXIT1 ("creat returned");
1364 static void *
1365 tf_connect (void *arg)
1367 if (arg == NULL)
1368 // XXX If somebody can provide a portable test case in which connect()
1369 // blocks we can enable this test to run in both rounds.
1370 abort ();
1372 struct sockaddr_un sun;
1374 tempfd = socket (AF_UNIX, SOCK_STREAM, 0);
1375 if (tempfd == -1)
1376 FAIL_EXIT1 ("socket (AF_UNIX, SOCK_STREAM, 0): %m");
1378 int tries = 0;
1381 TEST_VERIFY_EXIT (tries++ < 10);
1383 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-2-XXXXXX");
1384 TEST_VERIFY_EXIT (mktemp (sun.sun_path) != NULL);
1386 sun.sun_family = AF_UNIX;
1388 while (bind (tempfd, (struct sockaddr *) &sun,
1389 offsetof (struct sockaddr_un, sun_path)
1390 + strlen (sun.sun_path) + 1) != 0);
1391 tempfname = strdup (sun.sun_path);
1393 listen (tempfd, 5);
1395 tempfd2 = socket (AF_UNIX, SOCK_STREAM, 0);
1396 if (tempfd2 == -1)
1397 FAIL_EXIT1 ("socket (AF_UNIX, SOCK_STREAM, 0): %m");
1399 xpthread_barrier_wait (&b2);
1401 if (arg != NULL)
1402 xpthread_barrier_wait (&b2);
1404 pthread_cleanup_push (cl, NULL);
1406 connect (tempfd2, (struct sockaddr *) &sun, sizeof (sun));
1408 pthread_cleanup_pop (0);
1410 FAIL_EXIT1 ("connect returned");
1414 static void *
1415 tf_tcdrain (void *arg)
1417 if (arg == NULL)
1418 // XXX If somebody can provide a portable test case in which tcdrain()
1419 // blocks we can enable this test to run in both rounds.
1420 abort ();
1422 xpthread_barrier_wait (&b2);
1424 if (arg != NULL)
1425 xpthread_barrier_wait (&b2);
1427 pthread_cleanup_push (cl, NULL);
1429 /* Regardless of stderr being a terminal, the tcdrain call should be
1430 canceled. */
1431 tcdrain (STDERR_FILENO);
1433 pthread_cleanup_pop (0);
1435 FAIL_EXIT1 ("tcdrain returned");
1439 static void *
1440 tf_msgrcv (void *arg)
1442 tempmsg = msgget (IPC_PRIVATE, 0666 | IPC_CREAT);
1443 if (tempmsg == -1)
1444 FAIL_EXIT1 ("msgget (IPC_PRIVATE, 0666 | IPC_CREAT): %m");
1446 xpthread_barrier_wait (&b2);
1448 if (arg != NULL)
1449 xpthread_barrier_wait (&b2);
1451 ssize_t s;
1453 pthread_cleanup_push (cl, NULL);
1455 struct
1457 long int type;
1458 char mem[10];
1459 } m;
1460 int randnr;
1461 /* We need a positive random number. */
1463 randnr = random () % 64000;
1464 while (randnr <= 0);
1467 errno = 0;
1468 s = msgrcv (tempmsg, (struct msgbuf *) &m, 10, randnr, 0);
1470 while (errno == EIDRM || errno == EINTR);
1472 pthread_cleanup_pop (0);
1474 msgctl (tempmsg, IPC_RMID, NULL);
1476 FAIL_EXIT1 ("msgrcv returned %zd", s);
1480 static void *
1481 tf_msgsnd (void *arg)
1483 if (arg == NULL)
1484 // XXX If somebody can provide a portable test case in which msgsnd()
1485 // blocks we can enable this test to run in both rounds.
1486 abort ();
1488 tempmsg = msgget (IPC_PRIVATE, 0666 | IPC_CREAT);
1489 if (tempmsg == -1)
1490 FAIL_EXIT1 ("msgget (IPC_PRIVATE, 0666 | IPC_CREAT): %m");
1492 xpthread_barrier_wait (&b2);
1494 xpthread_barrier_wait (&b2);
1496 pthread_cleanup_push (cl, NULL);
1498 struct
1500 long int type;
1501 char mem[1];
1502 } m;
1503 /* We need a positive random number. */
1505 m.type = random () % 64000;
1506 while (m.type <= 0);
1507 msgsnd (tempmsg, (struct msgbuf *) &m, sizeof (m.mem), 0);
1509 pthread_cleanup_pop (0);
1511 msgctl (tempmsg, IPC_RMID, NULL);
1513 FAIL_EXIT1 ("msgsnd returned");
1517 struct cancel_tests tests[] =
1519 ADD_TEST (read, 2, 0),
1520 ADD_TEST (readv, 2, 0),
1521 ADD_TEST (select, 2, 0),
1522 ADD_TEST (pselect, 2, 0),
1523 ADD_TEST (poll, 2, 0),
1524 ADD_TEST (ppoll, 2, 0),
1525 ADD_TEST (write, 2, 0),
1526 ADD_TEST (writev, 2, 0),
1527 ADD_TEST (sleep, 2, 0),
1528 ADD_TEST (usleep, 2, 0),
1529 ADD_TEST (nanosleep, 2, 0),
1530 ADD_TEST (wait, 2, 0),
1531 ADD_TEST (waitid, 2, 0),
1532 ADD_TEST (waitpid, 2, 0),
1533 ADD_TEST (sigpause, 2, 0),
1534 ADD_TEST (sigsuspend, 2, 0),
1535 ADD_TEST (sigwait, 2, 0),
1536 ADD_TEST (sigwaitinfo, 2, 0),
1537 ADD_TEST (sigtimedwait, 2, 0),
1538 ADD_TEST (pause, 2, 0),
1539 ADD_TEST (accept, 2, 0),
1540 ADD_TEST (send, 2, 0),
1541 ADD_TEST (recv, 2, 0),
1542 ADD_TEST (recvfrom, 2, 0),
1543 ADD_TEST (recvmsg, 2, 0),
1544 ADD_TEST (preadv, 2, 1),
1545 ADD_TEST (preadv2, 2, 1),
1546 ADD_TEST (pwritev, 2, 1),
1547 ADD_TEST (pwritev2, 2, 1),
1548 ADD_TEST (open, 2, 1),
1549 ADD_TEST (close, 2, 1),
1550 ADD_TEST (pread, 2, 1),
1551 ADD_TEST (pwrite, 2, 1),
1552 ADD_TEST (fsync, 2, 1),
1553 ADD_TEST (fdatasync, 2, 1),
1554 ADD_TEST (msync, 2, 1),
1555 ADD_TEST (sendto, 2, 1),
1556 ADD_TEST (sendmsg, 2, 1),
1557 ADD_TEST (creat, 2, 1),
1558 ADD_TEST (connect, 2, 1),
1559 ADD_TEST (tcdrain, 2, 1),
1560 ADD_TEST (msgrcv, 2, 0),
1561 ADD_TEST (msgsnd, 2, 1),
1563 #define ntest_tf (sizeof (tests) / sizeof (tests[0]))
1565 #include "tst-cancel4-common.c"