1 /* Copyright (C) 2002, 2003 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 /* NOTE: this tests functionality beyond POSIX. POSIX does not allow
21 exit to be called more than once. */
36 #include <sys/select.h>
37 #include <sys/socket.h>
45 /* Since STREAMS are not supported in the standard Linux kernel and
46 there we don't advertise STREAMS as supported is no need to test
47 the STREAMS related functions. This affects
48 getmsg() getpmsg() putmsg()
51 lockf() and fcntl() are tested in tst-cancel16.
53 pthread_join() is tested in tst-join5.
55 pthread_testcancel()'s only purpose is to allow cancellation. This
56 is tested in several places.
58 sem_wait() and sem_timedwait() are checked in tst-cancel1[2345] tests.
60 POSIX message queues aren't implemented yet. This affects
61 mq_receive() mq_send() mq_timedreceive() mq_timedsend()
63 aio_suspend() is tested in tst-cancel17.
65 clock_nanosleep() is tested in tst-cancel18.
68 /* Pipe descriptors. */
71 /* Temporary file descriptor, to be closed after each round. */
72 static int tempfd = -1;
73 static int tempfd2 = -1;
74 /* Name of temporary file to be removed after each round. */
75 static char *tempfname;
76 /* Temporary message queue. */
77 static int tempmsg = -1;
79 /* Often used barrier for two threads. */
80 static pthread_barrier_t b2;
83 /* Cleanup handling test. */
103 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
104 tempfd = fd = mkstemp (fname);
106 printf ("%s: mkstemp failed\n", __FUNCTION__);
109 r = pthread_barrier_wait (&b2);
110 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
112 printf ("%s: barrier_wait failed\n", __FUNCTION__);
117 r = pthread_barrier_wait (&b2);
118 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
120 printf ("%s: barrier_wait failed\n", __FUNCTION__);
125 pthread_cleanup_push (cl, NULL);
128 s = read (fd, buf, sizeof (buf));
130 pthread_cleanup_pop (0);
132 printf ("%s: read returns with %zd\n", __FUNCTION__, s);
148 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
149 tempfd = fd = mkstemp (fname);
151 printf ("%s: mkstemp failed\n", __FUNCTION__);
154 r = pthread_barrier_wait (&b2);
155 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
157 printf ("%s: barrier_wait failed\n", __FUNCTION__);
162 r = pthread_barrier_wait (&b2);
163 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
165 printf ("%s: barrier_wait failed\n", __FUNCTION__);
170 pthread_cleanup_push (cl, NULL);
173 struct iovec iov[1] = { [0] = { .iov_base = buf, .iov_len = sizeof (buf) } };
174 s = readv (fd, iov, 1);
176 pthread_cleanup_pop (0);
178 printf ("%s: readv returns with %zd\n", __FUNCTION__, s);
194 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
195 tempfd = fd = mkstemp (fname);
197 printf ("%s: mkstemp failed\n", __FUNCTION__);
200 r = pthread_barrier_wait (&b2);
201 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
203 printf ("%s: barrier_wait failed\n", __FUNCTION__);
208 r = pthread_barrier_wait (&b2);
209 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
211 printf ("%s: barrier_wait failed\n", __FUNCTION__);
216 pthread_cleanup_push (cl, NULL);
219 memset (buf, '\0', sizeof (buf));
220 s = write (fd, buf, sizeof (buf));
222 pthread_cleanup_pop (0);
224 printf ("%s: write returns with %zd\n", __FUNCTION__, s);
231 tf_writev (void *arg)
240 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
241 tempfd = fd = mkstemp (fname);
243 printf ("%s: mkstemp failed\n", __FUNCTION__);
246 r = pthread_barrier_wait (&b2);
247 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
249 printf ("%s: barrier_wait failed\n", __FUNCTION__);
254 r = pthread_barrier_wait (&b2);
255 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
257 printf ("%s: barrier_wait failed\n", __FUNCTION__);
262 pthread_cleanup_push (cl, NULL);
265 memset (buf, '\0', sizeof (buf));
266 struct iovec iov[1] = { [0] = { .iov_base = buf, .iov_len = sizeof (buf) } };
267 s = writev (fd, iov, 1);
269 pthread_cleanup_pop (0);
271 printf ("%s: writev returns with %zd\n", __FUNCTION__, s);
280 int r = pthread_barrier_wait (&b2);
281 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
283 printf ("%s: barrier_wait failed\n", __FUNCTION__);
289 r = pthread_barrier_wait (&b2);
290 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
292 printf ("%s: barrier_wait failed\n", __FUNCTION__);
297 pthread_cleanup_push (cl, NULL);
299 sleep (arg == NULL ? 1000000 : 0);
301 pthread_cleanup_pop (0);
303 printf ("%s: sleep returns\n", __FUNCTION__);
310 tf_usleep (void *arg)
312 int r = pthread_barrier_wait (&b2);
313 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
315 printf ("%s: barrier_wait failed\n", __FUNCTION__);
321 r = pthread_barrier_wait (&b2);
322 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
324 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
329 pthread_cleanup_push (cl, NULL);
331 usleep (arg == NULL ? (useconds_t) ULONG_MAX : 0);
333 pthread_cleanup_pop (0);
335 printf ("%s: usleep returns\n", __FUNCTION__);
342 tf_nanosleep (void *arg)
344 int r = pthread_barrier_wait (&b2);
345 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
347 printf ("%s: barrier_wait failed\n", __FUNCTION__);
353 r = pthread_barrier_wait (&b2);
354 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
356 printf ("%s: barrier_wait failed\n", __FUNCTION__);
361 pthread_cleanup_push (cl, NULL);
363 struct timespec ts = { .tv_sec = arg == NULL ? 10000000 : 0, .tv_nsec = 0 };
364 TEMP_FAILURE_RETRY (nanosleep (&ts, &ts));
366 pthread_cleanup_pop (0);
368 printf ("%s: nanosleep returns\n", __FUNCTION__);
375 tf_select (void *arg)
384 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
385 tempfd = fd = mkstemp (fname);
387 printf ("%s: mkstemp failed\n", __FUNCTION__);
390 r = pthread_barrier_wait (&b2);
391 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
393 printf ("%s: barrier_wait failed\n", __FUNCTION__);
398 r = pthread_barrier_wait (&b2);
399 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
401 printf ("%s: barrier_wait failed\n", __FUNCTION__);
410 pthread_cleanup_push (cl, NULL);
412 s = select (fd + 1, &rfs, NULL, NULL, NULL);
414 pthread_cleanup_pop (0);
416 printf ("%s: select returns with %d (%s)\n", __FUNCTION__, s,
424 tf_pselect (void *arg)
433 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
434 tempfd = fd = mkstemp (fname);
436 printf ("%s: mkstemp failed\n", __FUNCTION__);
439 r = pthread_barrier_wait (&b2);
440 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
442 printf ("%s: barrier_wait failed\n", __FUNCTION__);
447 r = pthread_barrier_wait (&b2);
448 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
450 printf ("%s: barrier_wait failed\n", __FUNCTION__);
459 pthread_cleanup_push (cl, NULL);
461 s = pselect (fd + 1, &rfs, NULL, NULL, NULL, NULL);
463 pthread_cleanup_pop (0);
465 printf ("%s: pselect returns with %d (%s)\n", __FUNCTION__, s,
482 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
483 tempfd = fd = mkstemp (fname);
485 printf ("%s: mkstemp failed\n", __FUNCTION__);
488 r = pthread_barrier_wait (&b2);
489 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
491 printf ("%s: barrier_wait failed\n", __FUNCTION__);
496 r = pthread_barrier_wait (&b2);
497 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
499 printf ("%s: barrier_wait failed\n", __FUNCTION__);
503 struct pollfd rfs[1] = { [0] = { .fd = fd, .events = POLLIN } };
506 pthread_cleanup_push (cl, NULL);
508 s = poll (rfs, 1, -1);
510 pthread_cleanup_pop (0);
512 printf ("%s: poll returns with %d (%s)\n", __FUNCTION__, s,
525 puts ("fork failed");
531 /* Make the program disappear after a while. */
540 struct timespec ts = { .tv_sec = 0, .tv_nsec = 100000000 };
541 while (nanosleep (&ts, &ts) != 0)
544 r = pthread_barrier_wait (&b2);
545 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
547 printf ("%s: barrier_wait failed\n", __FUNCTION__);
552 r = pthread_barrier_wait (&b2);
553 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
555 printf ("%s: barrier_wait failed\n", __FUNCTION__);
560 pthread_cleanup_push (cl, NULL);
564 pthread_cleanup_pop (0);
566 printf ("%s: wait returns with %d (%s)\n", __FUNCTION__, s,
574 tf_waitpid (void *arg)
580 puts ("fork failed");
586 /* Make the program disappear after a while. */
595 struct timespec ts = { .tv_sec = 0, .tv_nsec = 100000000 };
596 while (nanosleep (&ts, &ts) != 0)
599 r = pthread_barrier_wait (&b2);
600 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
602 printf ("%s: barrier_wait failed\n", __FUNCTION__);
607 r = pthread_barrier_wait (&b2);
608 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
610 printf ("%s: barrier_wait failed\n", __FUNCTION__);
615 pthread_cleanup_push (cl, NULL);
617 s = waitpid (-1, NULL, 0);
619 pthread_cleanup_pop (0);
621 printf ("%s: waitpid returns with %d (%s)\n", __FUNCTION__, s,
629 tf_waitid (void *arg)
634 puts ("fork failed");
640 /* Make the program disappear after a while. */
649 struct timespec ts = { .tv_sec = 0, .tv_nsec = 100000000 };
650 while (nanosleep (&ts, &ts) != 0)
653 r = pthread_barrier_wait (&b2);
654 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
656 printf ("%s: barrier_wait failed\n", __FUNCTION__);
661 r = pthread_barrier_wait (&b2);
662 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
664 printf ("%s: barrier_wait failed\n", __FUNCTION__);
669 pthread_cleanup_push (cl, NULL);
672 s = waitid (P_PID, pid, &si, 0);
674 pthread_cleanup_pop (0);
676 printf ("%s: waitid returns with %d (%s)\n", __FUNCTION__, s,
684 tf_sigpause (void *arg)
686 int r = pthread_barrier_wait (&b2);
687 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
689 printf ("%s: barrier_wait failed\n", __FUNCTION__);
695 r = pthread_barrier_wait (&b2);
696 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
698 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
703 pthread_cleanup_push (cl, NULL);
705 /* Just for fun block the cancellation signal. We need to use
706 __xpg_sigpause since otherwise we will get the BSD version. */
707 __xpg_sigpause (SIGCANCEL);
709 pthread_cleanup_pop (0);
711 printf ("%s: sigpause returned\n", __FUNCTION__);
718 tf_sigsuspend (void *arg)
720 int r = pthread_barrier_wait (&b2);
721 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
723 printf ("%s: barrier_wait failed\n", __FUNCTION__);
729 r = pthread_barrier_wait (&b2);
730 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
732 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
737 pthread_cleanup_push (cl, NULL);
739 /* Just for fun block all signals. */
744 pthread_cleanup_pop (0);
746 printf ("%s: sigsuspend returned\n", __FUNCTION__);
753 tf_sigwait (void *arg)
755 int r = pthread_barrier_wait (&b2);
756 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
758 printf ("%s: barrier_wait failed\n", __FUNCTION__);
764 r = pthread_barrier_wait (&b2);
765 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
767 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
774 sigaddset (&mask, SIGUSR1);
775 if (pthread_sigmask (SIG_BLOCK, &mask, NULL) != 0)
777 printf ("%s: pthread_sigmask failed\n", __FUNCTION__);
782 pthread_cleanup_push (cl, NULL);
784 /* Wait for SIGUSR1. */
785 sigwait (&mask, &sig);
787 pthread_cleanup_pop (0);
789 printf ("%s: sigwait returned with signal %d\n", __FUNCTION__, sig);
796 tf_sigwaitinfo (void *arg)
798 int r = pthread_barrier_wait (&b2);
799 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
801 printf ("%s: barrier_wait failed\n", __FUNCTION__);
807 r = pthread_barrier_wait (&b2);
808 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
810 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
817 sigaddset (&mask, SIGUSR1);
818 if (pthread_sigmask (SIG_BLOCK, &mask, NULL) != 0)
820 printf ("%s: pthread_sigmask failed\n", __FUNCTION__);
825 pthread_cleanup_push (cl, NULL);
827 /* Wait for SIGUSR1. */
828 sigwaitinfo (&mask, &info);
830 pthread_cleanup_pop (0);
832 printf ("%s: sigwaitinfo returned with signal %d\n", __FUNCTION__,
840 tf_sigtimedwait (void *arg)
842 int r = pthread_barrier_wait (&b2);
843 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
845 printf ("%s: barrier_wait failed\n", __FUNCTION__);
851 r = pthread_barrier_wait (&b2);
852 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
854 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
861 sigaddset (&mask, SIGUSR1);
862 if (pthread_sigmask (SIG_BLOCK, &mask, NULL) != 0)
864 printf ("%s: pthread_sigmask failed\n", __FUNCTION__);
868 /* Wait for SIGUSR1. */
870 struct timespec ts = { .tv_sec = 60, .tv_nsec = 0 };
871 pthread_cleanup_push (cl, NULL);
873 sigtimedwait (&mask, &info, &ts);
875 pthread_cleanup_pop (0);
877 printf ("%s: sigtimedwait returned with signal %d\n", __FUNCTION__,
887 int r = pthread_barrier_wait (&b2);
888 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
890 printf ("%s: barrier_wait failed\n", __FUNCTION__);
896 r = pthread_barrier_wait (&b2);
897 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
899 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
904 pthread_cleanup_push (cl, NULL);
908 pthread_cleanup_pop (0);
910 printf ("%s: pause returned\n", __FUNCTION__);
917 tf_accept (void *arg)
919 struct sockaddr_un sun;
920 /* To test a non-blocking accept call we make the call file by using
921 a datagrame socket. */
922 int pf = arg == NULL ? SOCK_STREAM : SOCK_DGRAM;
924 tempfd = socket (AF_UNIX, pf, 0);
927 printf ("%s: socket call failed\n", __FUNCTION__);
936 printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
939 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-1-XXXXXX");
940 if (mktemp (sun.sun_path) == NULL)
942 printf ("%s: cannot generate temp file name\n", __FUNCTION__);
946 sun.sun_family = AF_UNIX;
948 while (bind (tempfd, (struct sockaddr *) &sun,
949 offsetof (struct sockaddr_un, sun_path)
950 + strlen (sun.sun_path) + 1) != 0);
952 unlink (sun.sun_path);
956 socklen_t len = sizeof (sun);
958 int r = pthread_barrier_wait (&b2);
959 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
961 printf ("%s: barrier_wait failed\n", __FUNCTION__);
967 r = pthread_barrier_wait (&b2);
968 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
970 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
975 pthread_cleanup_push (cl, NULL);
977 accept (tempfd, (struct sockaddr *) &sun, &len);
979 pthread_cleanup_pop (0);
981 printf ("%s: accept returned\n", __FUNCTION__);
990 struct sockaddr_un sun;
992 tempfd = socket (AF_UNIX, SOCK_STREAM, 0);
995 printf ("%s: first socket call failed\n", __FUNCTION__);
1004 printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
1007 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-2-XXXXXX");
1008 if (mktemp (sun.sun_path) == NULL)
1010 printf ("%s: cannot generate temp file name\n", __FUNCTION__);
1014 sun.sun_family = AF_UNIX;
1016 while (bind (tempfd, (struct sockaddr *) &sun,
1017 offsetof (struct sockaddr_un, sun_path)
1018 + strlen (sun.sun_path) + 1) != 0);
1022 tempfd2 = socket (AF_UNIX, SOCK_STREAM, 0);
1025 printf ("%s: second socket call failed\n", __FUNCTION__);
1029 if (connect (tempfd2, (struct sockaddr *) &sun, sizeof (sun)) != 0)
1031 printf ("%s: connect failed\n", __FUNCTION__);
1035 unlink (sun.sun_path);
1037 int r = pthread_barrier_wait (&b2);
1038 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1040 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1046 r = pthread_barrier_wait (&b2);
1047 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1049 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1054 pthread_cleanup_push (cl, NULL);
1056 /* Very large block, so that the send call blocks. */
1059 send (tempfd2, mem, arg == NULL ? sizeof (mem) : 1, 0);
1061 pthread_cleanup_pop (0);
1063 printf ("%s: send returned\n", __FUNCTION__);
1072 struct sockaddr_un sun;
1074 tempfd = socket (AF_UNIX, SOCK_STREAM, 0);
1077 printf ("%s: first socket call failed\n", __FUNCTION__);
1086 printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
1089 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-3-XXXXXX");
1090 if (mktemp (sun.sun_path) == NULL)
1092 printf ("%s: cannot generate temp file name\n", __FUNCTION__);
1096 sun.sun_family = AF_UNIX;
1098 while (bind (tempfd, (struct sockaddr *) &sun,
1099 offsetof (struct sockaddr_un, sun_path)
1100 + strlen (sun.sun_path) + 1) != 0);
1104 tempfd2 = socket (AF_UNIX, SOCK_STREAM, 0);
1107 printf ("%s: second socket call failed\n", __FUNCTION__);
1111 if (connect (tempfd2, (struct sockaddr *) &sun, sizeof (sun)) != 0)
1113 printf ("%s: connect failed\n", __FUNCTION__);
1117 unlink (sun.sun_path);
1119 int r = pthread_barrier_wait (&b2);
1120 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1122 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1128 r = pthread_barrier_wait (&b2);
1129 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1131 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1136 pthread_cleanup_push (cl, NULL);
1140 recv (tempfd2, mem, arg == NULL ? sizeof (mem) : 0, 0);
1142 pthread_cleanup_pop (0);
1144 printf ("%s: recv returned\n", __FUNCTION__);
1151 tf_recvfrom (void *arg)
1153 struct sockaddr_un sun;
1155 tempfd = socket (AF_UNIX, SOCK_DGRAM, 0);
1158 printf ("%s: first socket call failed\n", __FUNCTION__);
1167 printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
1170 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-4-XXXXXX");
1171 if (mktemp (sun.sun_path) == NULL)
1173 printf ("%s: cannot generate temp file name\n", __FUNCTION__);
1177 sun.sun_family = AF_UNIX;
1179 while (bind (tempfd, (struct sockaddr *) &sun,
1180 offsetof (struct sockaddr_un, sun_path)
1181 + strlen (sun.sun_path) + 1) != 0);
1183 tempfname = strdup (sun.sun_path);
1185 tempfd2 = socket (AF_UNIX, SOCK_DGRAM, 0);
1188 printf ("%s: second socket call failed\n", __FUNCTION__);
1192 int r = pthread_barrier_wait (&b2);
1193 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1195 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1201 r = pthread_barrier_wait (&b2);
1202 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1204 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1209 pthread_cleanup_push (cl, NULL);
1212 socklen_t len = sizeof (sun);
1214 recvfrom (tempfd2, mem, arg == NULL ? sizeof (mem) : 0, 0,
1215 (struct sockaddr *) &sun, &len);
1217 pthread_cleanup_pop (0);
1219 printf ("%s: recvfrom returned\n", __FUNCTION__);
1226 tf_recvmsg (void *arg)
1228 struct sockaddr_un sun;
1230 tempfd = socket (AF_UNIX, SOCK_DGRAM, 0);
1233 printf ("%s: first socket call failed\n", __FUNCTION__);
1242 printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
1245 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-5-XXXXXX");
1246 if (mktemp (sun.sun_path) == NULL)
1248 printf ("%s: cannot generate temp file name\n", __FUNCTION__);
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);
1258 tempfname = strdup (sun.sun_path);
1260 tempfd2 = socket (AF_UNIX, SOCK_DGRAM, 0);
1263 printf ("%s: second socket call failed\n", __FUNCTION__);
1267 int r = pthread_barrier_wait (&b2);
1268 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1270 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1276 r = pthread_barrier_wait (&b2);
1277 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1279 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1284 pthread_cleanup_push (cl, NULL);
1287 struct iovec iov[1];
1288 iov[0].iov_base = mem;
1289 iov[0].iov_len = arg == NULL ? sizeof (mem) : 0;
1293 m.msg_namelen = sizeof (sun);
1296 m.msg_control = NULL;
1297 m.msg_controllen = 0;
1299 recvmsg (tempfd2, &m, 0);
1301 pthread_cleanup_pop (0);
1303 printf ("%s: recvmsg returned\n", __FUNCTION__);
1313 // XXX If somebody can provide a portable test case in which open()
1314 // blocks we can enable this test to run in both rounds.
1317 int r = pthread_barrier_wait (&b2);
1318 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1320 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1324 r = pthread_barrier_wait (&b2);
1325 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1327 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1331 pthread_cleanup_push (cl, NULL);
1333 open ("Makefile", O_RDONLY);
1335 pthread_cleanup_pop (0);
1337 printf ("%s: open returned\n", __FUNCTION__);
1344 tf_close (void *arg)
1347 // XXX If somebody can provide a portable test case in which close()
1348 // blocks we can enable this test to run in both rounds.
1351 char fname[] = "/tmp/tst-cancel-fd-XXXXXX";
1352 tempfd = mkstemp (fname);
1355 printf ("%s: mkstemp failed\n", __FUNCTION__);
1360 int r = pthread_barrier_wait (&b2);
1361 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1363 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1367 r = pthread_barrier_wait (&b2);
1368 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1370 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1374 pthread_cleanup_push (cl, NULL);
1378 pthread_cleanup_pop (0);
1380 printf ("%s: close returned\n", __FUNCTION__);
1387 tf_pread (void *arg)
1390 // XXX If somebody can provide a portable test case in which pread()
1391 // blocks we can enable this test to run in both rounds.
1394 tempfd = open ("Makefile", O_RDONLY);
1397 printf ("%s: cannot open Makefile\n", __FUNCTION__);
1401 int r = pthread_barrier_wait (&b2);
1402 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1404 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1408 r = pthread_barrier_wait (&b2);
1409 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1411 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1415 pthread_cleanup_push (cl, NULL);
1418 pread (tempfd, mem, sizeof (mem), 0);
1420 pthread_cleanup_pop (0);
1422 printf ("%s: pread returned\n", __FUNCTION__);
1429 tf_pwrite (void *arg)
1432 // XXX If somebody can provide a portable test case in which pwrite()
1433 // blocks we can enable this test to run in both rounds.
1436 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
1437 tempfd = mkstemp (fname);
1440 printf ("%s: mkstemp failed\n", __FUNCTION__);
1445 int r = pthread_barrier_wait (&b2);
1446 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1448 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1452 r = pthread_barrier_wait (&b2);
1453 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1455 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1459 pthread_cleanup_push (cl, NULL);
1462 pwrite (tempfd, mem, sizeof (mem), 0);
1464 pthread_cleanup_pop (0);
1466 printf ("%s: pwrite returned\n", __FUNCTION__);
1473 tf_fsync (void *arg)
1476 // XXX If somebody can provide a portable test case in which fsync()
1477 // blocks we can enable this test to run in both rounds.
1480 tempfd = open ("Makefile", O_RDONLY);
1483 printf ("%s: cannot open Makefile\n", __FUNCTION__);
1487 int r = pthread_barrier_wait (&b2);
1488 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1490 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1494 r = pthread_barrier_wait (&b2);
1495 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1497 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1501 pthread_cleanup_push (cl, NULL);
1505 pthread_cleanup_pop (0);
1507 printf ("%s: fsync returned\n", __FUNCTION__);
1514 tf_msync (void *arg)
1517 // XXX If somebody can provide a portable test case in which msync()
1518 // blocks we can enable this test to run in both rounds.
1521 tempfd = open ("Makefile", O_RDONLY);
1524 printf ("%s: cannot open Makefile\n", __FUNCTION__);
1527 void *p = mmap (NULL, 10, PROT_READ, MAP_SHARED, tempfd, 0);
1528 if (p == MAP_FAILED)
1530 printf ("%s: mmap failed\n", __FUNCTION__);
1534 int r = pthread_barrier_wait (&b2);
1535 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1537 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1541 r = pthread_barrier_wait (&b2);
1542 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1544 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1548 pthread_cleanup_push (cl, NULL);
1552 pthread_cleanup_pop (0);
1554 printf ("%s: msync returned\n", __FUNCTION__);
1561 tf_sendto (void *arg)
1564 // XXX If somebody can provide a portable test case in which sendto()
1565 // blocks we can enable this test to run in both rounds.
1568 struct sockaddr_un sun;
1570 tempfd = socket (AF_UNIX, SOCK_DGRAM, 0);
1573 printf ("%s: first socket call failed\n", __FUNCTION__);
1582 printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
1585 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-6-XXXXXX");
1586 if (mktemp (sun.sun_path) == NULL)
1588 printf ("%s: cannot generate temp file name\n", __FUNCTION__);
1592 sun.sun_family = AF_UNIX;
1594 while (bind (tempfd, (struct sockaddr *) &sun,
1595 offsetof (struct sockaddr_un, sun_path)
1596 + strlen (sun.sun_path) + 1) != 0);
1597 tempfname = strdup (sun.sun_path);
1599 tempfd2 = socket (AF_UNIX, SOCK_DGRAM, 0);
1602 printf ("%s: second socket call failed\n", __FUNCTION__);
1606 int r = pthread_barrier_wait (&b2);
1607 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1609 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1613 r = pthread_barrier_wait (&b2);
1614 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1616 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1620 pthread_cleanup_push (cl, NULL);
1624 sendto (tempfd2, mem, arg == NULL ? sizeof (mem) : 1, 0,
1625 (struct sockaddr *) &sun,
1626 offsetof (struct sockaddr_un, sun_path) + strlen (sun.sun_path) + 1);
1628 pthread_cleanup_pop (0);
1630 printf ("%s: sendto returned\n", __FUNCTION__);
1637 tf_sendmsg (void *arg)
1640 // XXX If somebody can provide a portable test case in which sendmsg()
1641 // blocks we can enable this test to run in both rounds.
1644 struct sockaddr_un sun;
1646 tempfd = socket (AF_UNIX, SOCK_DGRAM, 0);
1649 printf ("%s: first socket call failed\n", __FUNCTION__);
1658 printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
1661 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-7-XXXXXX");
1662 if (mktemp (sun.sun_path) == NULL)
1664 printf ("%s: cannot generate temp file name\n", __FUNCTION__);
1668 sun.sun_family = AF_UNIX;
1670 while (bind (tempfd, (struct sockaddr *) &sun,
1671 offsetof (struct sockaddr_un, sun_path)
1672 + strlen (sun.sun_path) + 1) != 0);
1673 tempfname = strdup (sun.sun_path);
1675 tempfd2 = socket (AF_UNIX, SOCK_DGRAM, 0);
1678 printf ("%s: second socket call failed\n", __FUNCTION__);
1682 int r = pthread_barrier_wait (&b2);
1683 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1685 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1689 r = pthread_barrier_wait (&b2);
1690 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1692 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1696 pthread_cleanup_push (cl, NULL);
1699 struct iovec iov[1];
1700 iov[0].iov_base = mem;
1705 m.msg_namelen = (offsetof (struct sockaddr_un, sun_path)
1706 + strlen (sun.sun_path) + 1);
1709 m.msg_control = NULL;
1710 m.msg_controllen = 0;
1712 sendmsg (tempfd2, &m, 0);
1714 pthread_cleanup_pop (0);
1716 printf ("%s: sendmsg returned\n", __FUNCTION__);
1723 tf_creat (void *arg)
1726 // XXX If somebody can provide a portable test case in which sendmsg()
1727 // blocks we can enable this test to run in both rounds.
1730 int r = pthread_barrier_wait (&b2);
1731 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1733 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1737 r = pthread_barrier_wait (&b2);
1738 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1740 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1744 pthread_cleanup_push (cl, NULL);
1746 creat ("tmp/tst-cancel-4-should-not-exist", 0666);
1748 pthread_cleanup_pop (0);
1750 printf ("%s: creat returned\n", __FUNCTION__);
1757 tf_connect (void *arg)
1760 // XXX If somebody can provide a portable test case in which connect()
1761 // blocks we can enable this test to run in both rounds.
1764 struct sockaddr_un sun;
1766 tempfd = socket (AF_UNIX, SOCK_STREAM, 0);
1769 printf ("%s: first socket call failed\n", __FUNCTION__);
1778 printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
1781 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-2-XXXXXX");
1782 if (mktemp (sun.sun_path) == NULL)
1784 printf ("%s: cannot generate temp file name\n", __FUNCTION__);
1788 sun.sun_family = AF_UNIX;
1790 while (bind (tempfd, (struct sockaddr *) &sun,
1791 offsetof (struct sockaddr_un, sun_path)
1792 + strlen (sun.sun_path) + 1) != 0);
1793 tempfname = strdup (sun.sun_path);
1797 tempfd2 = socket (AF_UNIX, SOCK_STREAM, 0);
1800 printf ("%s: second socket call failed\n", __FUNCTION__);
1804 int r = pthread_barrier_wait (&b2);
1805 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1807 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1813 r = pthread_barrier_wait (&b2);
1814 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1816 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1821 pthread_cleanup_push (cl, NULL);
1823 connect (tempfd2, (struct sockaddr *) &sun, sizeof (sun));
1825 pthread_cleanup_pop (0);
1827 printf ("%s: connect returned\n", __FUNCTION__);
1834 tf_tcdrain (void *arg)
1837 // XXX If somebody can provide a portable test case in which tcdrain()
1838 // blocks we can enable this test to run in both rounds.
1841 int r = pthread_barrier_wait (&b2);
1842 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1844 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1850 r = pthread_barrier_wait (&b2);
1851 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1853 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1858 pthread_cleanup_push (cl, NULL);
1860 /* Regardless of stderr being a terminal, the tcdrain call should be
1862 tcdrain (STDERR_FILENO);
1864 pthread_cleanup_pop (0);
1866 printf ("%s: tcdrain returned\n", __FUNCTION__);
1873 tf_msgrcv (void *arg)
1875 tempmsg = msgget (random (), 0666 | IPC_CREAT);
1877 int r = pthread_barrier_wait (&b2);
1878 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1880 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1886 r = pthread_barrier_wait (&b2);
1887 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1889 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1896 pthread_cleanup_push (cl, NULL);
1904 /* We need a positive random number. */
1907 while (randnr <= 0);
1911 s = msgrcv (tempmsg, (struct msgbuf *) &m, 10, randnr, 0);
1913 while (errno == EIDRM || errno == EINTR);
1915 pthread_cleanup_pop (0);
1917 printf ("%s: msgrcv returned %zd with errno = %m\n", __FUNCTION__, s);
1924 tf_msgsnd (void *arg)
1927 // XXX If somebody can provide a portable test case in which msgsnd()
1928 // blocks we can enable this test to run in both rounds.
1931 tempmsg = msgget (random (), 0666 | IPC_CREAT);
1933 int r = pthread_barrier_wait (&b2);
1934 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1936 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1940 r = pthread_barrier_wait (&b2);
1941 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1943 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1947 pthread_cleanup_push (cl, NULL);
1954 /* We need a positive random number. */
1957 while (m.type <= 0);
1958 msgsnd (tempmsg, (struct msgbuf *) &m, sizeof (m.mem), 0);
1960 pthread_cleanup_pop (0);
1962 printf ("%s: msgsnd returned\n", __FUNCTION__);
1971 void *(*tf) (void *);
1976 #define ADD_TEST(name, nbar, early) { #name, tf_##name, nbar, early }
1977 ADD_TEST (read, 2, 0),
1978 ADD_TEST (readv, 2, 0),
1979 ADD_TEST (select, 2, 0),
1980 ADD_TEST (pselect, 2, 0),
1981 ADD_TEST (poll, 2, 0),
1982 ADD_TEST (write, 2, 0),
1983 ADD_TEST (writev, 2, 0),
1984 ADD_TEST (sleep, 2, 0),
1985 ADD_TEST (usleep, 2, 0),
1986 ADD_TEST (nanosleep, 2, 0),
1987 ADD_TEST (wait, 2, 0),
1988 ADD_TEST (waitid, 2, 0),
1989 ADD_TEST (waitpid, 2, 0),
1990 ADD_TEST (sigpause, 2, 0),
1991 ADD_TEST (sigsuspend, 2, 0),
1992 ADD_TEST (sigwait, 2, 0),
1993 ADD_TEST (sigwaitinfo, 2, 0),
1994 ADD_TEST (sigtimedwait, 2, 0),
1995 ADD_TEST (pause, 2, 0),
1996 ADD_TEST (accept, 2, 0),
1997 ADD_TEST (send, 2, 0),
1998 ADD_TEST (recv, 2, 0),
1999 ADD_TEST (recvfrom, 2, 0),
2000 ADD_TEST (recvmsg, 2, 0),
2001 ADD_TEST (open, 2, 1),
2002 ADD_TEST (close, 2, 1),
2003 ADD_TEST (pread, 2, 1),
2004 ADD_TEST (pwrite, 2, 1),
2005 ADD_TEST (fsync, 2, 1),
2006 ADD_TEST (msync, 2, 1),
2007 ADD_TEST (sendto, 2, 1),
2008 ADD_TEST (sendmsg, 2, 1),
2009 ADD_TEST (creat, 2, 1),
2010 ADD_TEST (connect, 2, 1),
2011 ADD_TEST (tcdrain, 2, 1),
2012 ADD_TEST (msgrcv, 2, 0),
2013 ADD_TEST (msgsnd, 2, 1),
2015 #define ntest_tf (sizeof (tests) / sizeof (tests[0]))
2021 if (pipe (fds) != 0)
2023 puts ("pipe failed");
2029 for (cnt = 0; cnt < ntest_tf; ++cnt)
2031 if (tests[cnt].only_early)
2034 if (pthread_barrier_init (&b2, NULL, tests[cnt].nb) != 0)
2036 puts ("b2 init failed");
2040 /* Reset the counter for the cleanup handler. */
2044 if (pthread_create (&th, NULL, tests[cnt].tf, NULL) != 0)
2046 printf ("create for '%s' test failed\n", tests[cnt].name);
2051 int r = pthread_barrier_wait (&b2);
2052 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
2054 printf ("%s: barrier_wait failed\n", __FUNCTION__);
2059 struct timespec ts = { .tv_sec = 0, .tv_nsec = 100000000 };
2060 while (nanosleep (&ts, &ts) != 0)
2063 if (pthread_cancel (th) != 0)
2065 printf ("cancel for '%s' failed\n", tests[cnt].name);
2071 if (pthread_join (th, &status) != 0)
2073 printf ("join for '%s' failed\n", tests[cnt].name);
2077 if (status != PTHREAD_CANCELED)
2079 printf ("thread for '%s' not canceled\n", tests[cnt].name);
2084 if (pthread_barrier_destroy (&b2) != 0)
2086 puts ("barrier_destroy failed");
2093 printf ("cleanup handler not called for '%s'\n", tests[cnt].name);
2099 printf ("cleanup handler called more than once for '%s'\n",
2105 printf ("in-time cancel test of '%s' successful\n", tests[cnt].name);
2121 for (cnt = 0; cnt < ntest_tf; ++cnt)
2123 if (pthread_barrier_init (&b2, NULL, tests[cnt].nb) != 0)
2125 puts ("b2 init failed");
2129 /* Reset the counter for the cleanup handler. */
2133 if (pthread_create (&th, NULL, tests[cnt].tf, (void *) 1l) != 0)
2135 printf ("create for '%s' test failed\n", tests[cnt].name);
2140 int r = pthread_barrier_wait (&b2);
2141 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
2143 printf ("%s: barrier_wait failed\n", __FUNCTION__);
2148 if (pthread_cancel (th) != 0)
2150 printf ("cancel for '%s' failed\n", tests[cnt].name);
2155 r = pthread_barrier_wait (&b2);
2156 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
2158 printf ("%s: barrier_wait failed\n", __FUNCTION__);
2164 if (pthread_join (th, &status) != 0)
2166 printf ("join for '%s' failed\n", tests[cnt].name);
2170 if (status != PTHREAD_CANCELED)
2172 printf ("thread for '%s' not canceled\n", tests[cnt].name);
2177 if (pthread_barrier_destroy (&b2) != 0)
2179 puts ("barrier_destroy failed");
2186 printf ("cleanup handler not called for '%s'\n", tests[cnt].name);
2192 printf ("cleanup handler called more than once for '%s'\n",
2198 printf ("early cancel test of '%s' successful\n", tests[cnt].name);
2210 if (tempfname != NULL)
2218 msgctl (tempmsg, IPC_RMID, NULL);
2227 #define TEST_FUNCTION do_test ()
2228 #include "../test-skeleton.c"