]> git.ipfire.org Git - thirdparty/glibc.git/blame - nptl/tst-cancel4.c
Update copyright notices with scripts/update-copyrights
[thirdparty/glibc.git] / nptl / tst-cancel4.c
CommitLineData
d4697bc9 1/* Copyright (C) 2002-2014 Free Software Foundation, Inc.
76a50749
UD
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
4
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.
9
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.
14
15 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
76a50749
UD
18
19/* NOTE: this tests functionality beyond POSIX. POSIX does not allow
20 exit to be called more than once. */
21
22#include <errno.h>
047aec8f 23#include <fcntl.h>
76a50749
UD
24#include <limits.h>
25#include <pthread.h>
26676450 26#include <stddef.h>
76a50749
UD
27#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
f23b30e2 30#include <termios.h>
76a50749 31#include <unistd.h>
047aec8f 32#include <sys/mman.h>
f23b30e2 33#include <sys/msg.h>
76a50749
UD
34#include <sys/poll.h>
35#include <sys/select.h>
26676450 36#include <sys/socket.h>
76a50749 37#include <sys/uio.h>
26676450 38#include <sys/un.h>
76a50749
UD
39#include <sys/wait.h>
40
26676450
UD
41#include "pthreadP.h"
42
43
f23b30e2
UD
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
26676450
UD
47 getmsg() getpmsg() putmsg()
48 putpmsg()
49
50 lockf() and fcntl() are tested in tst-cancel16.
51
52 pthread_join() is tested in tst-join5.
53
54 pthread_testcancel()'s only purpose is to allow cancellation. This
55 is tested in several places.
56
57 sem_wait() and sem_timedwait() are checked in tst-cancel1[2345] tests.
58
1683daeb
UD
59 mq_send(), mq_timedsend(), mq_receive() and mq_timedreceive() are checked
60 in tst-mqueue8{,x} tests.
f23b30e2
UD
61
62 aio_suspend() is tested in tst-cancel17.
63
64 clock_nanosleep() is tested in tst-cancel18.
76a50749
UD
65*/
66
67/* Pipe descriptors. */
68static int fds[2];
69
26676450
UD
70/* Temporary file descriptor, to be closed after each round. */
71static int tempfd = -1;
72static int tempfd2 = -1;
73/* Name of temporary file to be removed after each round. */
74static char *tempfname;
f23b30e2
UD
75/* Temporary message queue. */
76static int tempmsg = -1;
26676450 77
76a50749
UD
78/* Often used barrier for two threads. */
79static pthread_barrier_t b2;
80
81
483e95d0
UD
82#ifndef IPC_ADDVAL
83# define IPC_ADDVAL 0
84#endif
85
382466e0 86/* The WRITE_BUFFER_SIZE value needs to be chosen such that if we set
e7074e4c
DM
87 the socket send buffer size to '1', a write of this size on that
88 socket will block.
89
135529b4
DM
90 The Linux kernel imposes a minimum send socket buffer size which
91 has changed over the years. As of Linux 3.10 the value is:
e7074e4c
DM
92
93 2 * (2048 + SKB_DATA_ALIGN(sizeof(struct sk_buff)))
94
95 which is attempting to make sure that with standard MTUs,
96 TCP can always queue up at least 2 full sized packets.
97
98 Furthermore, there is logic in the socket send paths that
99 will allow one more packet (of any size) to be queued up as
100 long as some socket buffer space remains. Blocking only
101 occurs when we try to queue up a new packet and the send
102 buffer space has already been fully consumed.
103
104 Therefore we must set this value to the largest possible value of
105 the formula above (and since it depends upon the size of "struct
106 sk_buff", it is dependent upon machine word size etc.) plus some
107 slack space. */
108
109#define WRITE_BUFFER_SIZE 16384
8074c5c5 110
0e0deb03
UD
111/* Cleanup handling test. */
112static int cl_called;
113
114static void
115cl (void *arg)
116{
117 ++cl_called;
118}
119
120
483e95d0 121
76a50749
UD
122static void *
123tf_read (void *arg)
124{
26676450
UD
125 int fd;
126 int r;
127
128 if (arg == NULL)
129 fd = fds[0];
130 else
131 {
132 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
133 tempfd = fd = mkstemp (fname);
134 if (fd == -1)
135 printf ("%s: mkstemp failed\n", __FUNCTION__);
136 unlink (fname);
137
138 r = pthread_barrier_wait (&b2);
139 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
140 {
141 printf ("%s: barrier_wait failed\n", __FUNCTION__);
142 exit (1);
143 }
144 }
145
146 r = pthread_barrier_wait (&b2);
76a50749
UD
147 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
148 {
149 printf ("%s: barrier_wait failed\n", __FUNCTION__);
150 exit (1);
151 }
152
f23b30e2 153 ssize_t s;
0e0deb03
UD
154 pthread_cleanup_push (cl, NULL);
155
76a50749 156 char buf[100];
f23b30e2 157 s = read (fd, buf, sizeof (buf));
76a50749 158
0e0deb03
UD
159 pthread_cleanup_pop (0);
160
f23b30e2
UD
161 printf ("%s: read returns with %zd\n", __FUNCTION__, s);
162
76a50749
UD
163 exit (1);
164}
165
166
167static void *
168tf_readv (void *arg)
169{
26676450
UD
170 int fd;
171 int r;
172
173 if (arg == NULL)
174 fd = fds[0];
175 else
176 {
177 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
178 tempfd = fd = mkstemp (fname);
179 if (fd == -1)
180 printf ("%s: mkstemp failed\n", __FUNCTION__);
181 unlink (fname);
182
183 r = pthread_barrier_wait (&b2);
184 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
185 {
186 printf ("%s: barrier_wait failed\n", __FUNCTION__);
187 exit (1);
188 }
189 }
190
191 r = pthread_barrier_wait (&b2);
76a50749
UD
192 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
193 {
194 printf ("%s: barrier_wait failed\n", __FUNCTION__);
195 exit (1);
196 }
197
f23b30e2 198 ssize_t s;
0e0deb03
UD
199 pthread_cleanup_push (cl, NULL);
200
76a50749
UD
201 char buf[100];
202 struct iovec iov[1] = { [0] = { .iov_base = buf, .iov_len = sizeof (buf) } };
f23b30e2 203 s = readv (fd, iov, 1);
76a50749 204
0e0deb03
UD
205 pthread_cleanup_pop (0);
206
f23b30e2
UD
207 printf ("%s: readv returns with %zd\n", __FUNCTION__, s);
208
76a50749
UD
209 exit (1);
210}
211
212
213static void *
214tf_write (void *arg)
215{
26676450
UD
216 int fd;
217 int r;
218
219 if (arg == NULL)
220 fd = fds[1];
221 else
222 {
223 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
224 tempfd = fd = mkstemp (fname);
225 if (fd == -1)
226 printf ("%s: mkstemp failed\n", __FUNCTION__);
227 unlink (fname);
228
229 r = pthread_barrier_wait (&b2);
230 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
231 {
232 printf ("%s: barrier_wait failed\n", __FUNCTION__);
233 exit (1);
234 }
235 }
236
237 r = pthread_barrier_wait (&b2);
76a50749
UD
238 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
239 {
240 printf ("%s: barrier_wait failed\n", __FUNCTION__);
241 exit (1);
242 }
243
f23b30e2 244 ssize_t s;
0e0deb03
UD
245 pthread_cleanup_push (cl, NULL);
246
8074c5c5 247 char buf[WRITE_BUFFER_SIZE];
76a50749 248 memset (buf, '\0', sizeof (buf));
f23b30e2 249 s = write (fd, buf, sizeof (buf));
76a50749 250
0e0deb03
UD
251 pthread_cleanup_pop (0);
252
f23b30e2
UD
253 printf ("%s: write returns with %zd\n", __FUNCTION__, s);
254
76a50749
UD
255 exit (1);
256}
257
258
259static void *
260tf_writev (void *arg)
261{
26676450
UD
262 int fd;
263 int r;
264
265 if (arg == NULL)
266 fd = fds[1];
267 else
268 {
269 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
270 tempfd = fd = mkstemp (fname);
271 if (fd == -1)
272 printf ("%s: mkstemp failed\n", __FUNCTION__);
273 unlink (fname);
274
275 r = pthread_barrier_wait (&b2);
276 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
277 {
278 printf ("%s: barrier_wait failed\n", __FUNCTION__);
279 exit (1);
280 }
281 }
282
283 r = pthread_barrier_wait (&b2);
76a50749
UD
284 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
285 {
286 printf ("%s: barrier_wait failed\n", __FUNCTION__);
287 exit (1);
288 }
289
f23b30e2 290 ssize_t s;
0e0deb03
UD
291 pthread_cleanup_push (cl, NULL);
292
8074c5c5 293 char buf[WRITE_BUFFER_SIZE];
76a50749
UD
294 memset (buf, '\0', sizeof (buf));
295 struct iovec iov[1] = { [0] = { .iov_base = buf, .iov_len = sizeof (buf) } };
f23b30e2 296 s = writev (fd, iov, 1);
76a50749 297
0e0deb03
UD
298 pthread_cleanup_pop (0);
299
f23b30e2
UD
300 printf ("%s: writev returns with %zd\n", __FUNCTION__, s);
301
76a50749
UD
302 exit (1);
303}
304
305
306static void *
307tf_sleep (void *arg)
308{
309 int r = pthread_barrier_wait (&b2);
310 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
311 {
312 printf ("%s: barrier_wait failed\n", __FUNCTION__);
313 exit (1);
314 }
315
26676450
UD
316 if (arg != NULL)
317 {
318 r = pthread_barrier_wait (&b2);
319 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
320 {
321 printf ("%s: barrier_wait failed\n", __FUNCTION__);
322 exit (1);
323 }
324 }
325
0e0deb03
UD
326 pthread_cleanup_push (cl, NULL);
327
26676450 328 sleep (arg == NULL ? 1000000 : 0);
76a50749 329
0e0deb03
UD
330 pthread_cleanup_pop (0);
331
76a50749
UD
332 printf ("%s: sleep returns\n", __FUNCTION__);
333
334 exit (1);
335}
336
337
338static void *
339tf_usleep (void *arg)
340{
341 int r = pthread_barrier_wait (&b2);
342 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
343 {
344 printf ("%s: barrier_wait failed\n", __FUNCTION__);
345 exit (1);
346 }
347
26676450
UD
348 if (arg != NULL)
349 {
350 r = pthread_barrier_wait (&b2);
351 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
352 {
353 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
354 exit (1);
355 }
356 }
357
0e0deb03
UD
358 pthread_cleanup_push (cl, NULL);
359
26676450 360 usleep (arg == NULL ? (useconds_t) ULONG_MAX : 0);
76a50749 361
0e0deb03
UD
362 pthread_cleanup_pop (0);
363
76a50749
UD
364 printf ("%s: usleep returns\n", __FUNCTION__);
365
366 exit (1);
367}
368
369
370static void *
371tf_nanosleep (void *arg)
372{
373 int r = pthread_barrier_wait (&b2);
374 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
375 {
376 printf ("%s: barrier_wait failed\n", __FUNCTION__);
377 exit (1);
378 }
379
26676450
UD
380 if (arg != NULL)
381 {
382 r = pthread_barrier_wait (&b2);
383 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
384 {
385 printf ("%s: barrier_wait failed\n", __FUNCTION__);
386 exit (1);
387 }
388 }
389
0e0deb03
UD
390 pthread_cleanup_push (cl, NULL);
391
26676450 392 struct timespec ts = { .tv_sec = arg == NULL ? 10000000 : 0, .tv_nsec = 0 };
f23b30e2 393 TEMP_FAILURE_RETRY (nanosleep (&ts, &ts));
76a50749 394
0e0deb03
UD
395 pthread_cleanup_pop (0);
396
76a50749
UD
397 printf ("%s: nanosleep returns\n", __FUNCTION__);
398
399 exit (1);
400}
401
402
403static void *
404tf_select (void *arg)
405{
26676450
UD
406 int fd;
407 int r;
408
409 if (arg == NULL)
410 fd = fds[0];
411 else
412 {
413 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
414 tempfd = fd = mkstemp (fname);
415 if (fd == -1)
416 printf ("%s: mkstemp failed\n", __FUNCTION__);
417 unlink (fname);
418
419 r = pthread_barrier_wait (&b2);
420 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
421 {
422 printf ("%s: barrier_wait failed\n", __FUNCTION__);
423 exit (1);
424 }
425 }
426
427 r = pthread_barrier_wait (&b2);
76a50749
UD
428 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
429 {
430 printf ("%s: barrier_wait failed\n", __FUNCTION__);
431 exit (1);
432 }
433
434 fd_set rfs;
435 FD_ZERO (&rfs);
26676450 436 FD_SET (fd, &rfs);
76a50749 437
f23b30e2 438 int s;
0e0deb03
UD
439 pthread_cleanup_push (cl, NULL);
440
f23b30e2
UD
441 s = select (fd + 1, &rfs, NULL, NULL, NULL);
442
443 pthread_cleanup_pop (0);
76a50749
UD
444
445 printf ("%s: select returns with %d (%s)\n", __FUNCTION__, s,
446 strerror (errno));
447
448 exit (1);
449}
450
451
452static void *
453tf_pselect (void *arg)
454{
26676450
UD
455 int fd;
456 int r;
457
458 if (arg == NULL)
459 fd = fds[0];
460 else
461 {
462 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
463 tempfd = fd = mkstemp (fname);
464 if (fd == -1)
465 printf ("%s: mkstemp failed\n", __FUNCTION__);
466 unlink (fname);
467
468 r = pthread_barrier_wait (&b2);
469 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
470 {
471 printf ("%s: barrier_wait failed\n", __FUNCTION__);
472 exit (1);
473 }
474 }
475
476 r = pthread_barrier_wait (&b2);
76a50749
UD
477 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
478 {
479 printf ("%s: barrier_wait failed\n", __FUNCTION__);
480 exit (1);
481 }
482
483 fd_set rfs;
484 FD_ZERO (&rfs);
26676450 485 FD_SET (fd, &rfs);
76a50749 486
f23b30e2 487 int s;
0e0deb03
UD
488 pthread_cleanup_push (cl, NULL);
489
f23b30e2
UD
490 s = pselect (fd + 1, &rfs, NULL, NULL, NULL, NULL);
491
492 pthread_cleanup_pop (0);
76a50749
UD
493
494 printf ("%s: pselect returns with %d (%s)\n", __FUNCTION__, s,
495 strerror (errno));
496
497 exit (1);
498}
499
500
501static void *
502tf_poll (void *arg)
503{
26676450
UD
504 int fd;
505 int r;
506
507 if (arg == NULL)
508 fd = fds[0];
509 else
510 {
511 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
512 tempfd = fd = mkstemp (fname);
513 if (fd == -1)
514 printf ("%s: mkstemp failed\n", __FUNCTION__);
515 unlink (fname);
516
517 r = pthread_barrier_wait (&b2);
518 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
519 {
520 printf ("%s: barrier_wait failed\n", __FUNCTION__);
521 exit (1);
522 }
523 }
524
525 r = pthread_barrier_wait (&b2);
76a50749
UD
526 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
527 {
528 printf ("%s: barrier_wait failed\n", __FUNCTION__);
529 exit (1);
530 }
531
26676450 532 struct pollfd rfs[1] = { [0] = { .fd = fd, .events = POLLIN } };
76a50749 533
f23b30e2 534 int s;
0e0deb03
UD
535 pthread_cleanup_push (cl, NULL);
536
f23b30e2
UD
537 s = poll (rfs, 1, -1);
538
539 pthread_cleanup_pop (0);
76a50749
UD
540
541 printf ("%s: poll returns with %d (%s)\n", __FUNCTION__, s,
542 strerror (errno));
543
544 exit (1);
545}
546
547
7c65e900
UD
548static void *
549tf_ppoll (void *arg)
550{
551 int fd;
552 int r;
553
554 if (arg == NULL)
555 fd = fds[0];
556 else
557 {
558 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
559 tempfd = fd = mkstemp (fname);
560 if (fd == -1)
561 printf ("%s: mkstemp failed\n", __FUNCTION__);
562 unlink (fname);
563
564 r = pthread_barrier_wait (&b2);
565 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
566 {
567 printf ("%s: barrier_wait failed\n", __FUNCTION__);
568 exit (1);
569 }
570 }
571
572 r = pthread_barrier_wait (&b2);
573 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
574 {
575 printf ("%s: barrier_wait failed\n", __FUNCTION__);
576 exit (1);
577 }
578
579 struct pollfd rfs[1] = { [0] = { .fd = fd, .events = POLLIN } };
580
581 int s;
582 pthread_cleanup_push (cl, NULL);
583
584 s = ppoll (rfs, 1, NULL, NULL);
585
586 pthread_cleanup_pop (0);
587
588 printf ("%s: ppoll returns with %d (%s)\n", __FUNCTION__, s,
589 strerror (errno));
590
591 exit (1);
592}
593
594
76a50749
UD
595static void *
596tf_wait (void *arg)
597{
76a50749
UD
598 pid_t pid = fork ();
599 if (pid == -1)
600 {
601 puts ("fork failed");
602 exit (1);
603 }
604
605 if (pid == 0)
606 {
607 /* Make the program disappear after a while. */
26676450
UD
608 if (arg == NULL)
609 sleep (10);
76a50749
UD
610 exit (0);
611 }
612
26676450
UD
613 int r;
614 if (arg != NULL)
615 {
616 struct timespec ts = { .tv_sec = 0, .tv_nsec = 100000000 };
617 while (nanosleep (&ts, &ts) != 0)
618 continue;
619
620 r = pthread_barrier_wait (&b2);
621 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
622 {
623 printf ("%s: barrier_wait failed\n", __FUNCTION__);
624 exit (1);
625 }
626 }
627
628 r = pthread_barrier_wait (&b2);
629 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
630 {
631 printf ("%s: barrier_wait failed\n", __FUNCTION__);
632 exit (1);
633 }
634
f23b30e2 635 int s;
0e0deb03
UD
636 pthread_cleanup_push (cl, NULL);
637
f23b30e2
UD
638 s = wait (NULL);
639
640 pthread_cleanup_pop (0);
76a50749
UD
641
642 printf ("%s: wait returns with %d (%s)\n", __FUNCTION__, s,
643 strerror (errno));
644
645 exit (1);
646}
647
648
649static void *
650tf_waitpid (void *arg)
651{
76a50749
UD
652
653 pid_t pid = fork ();
654 if (pid == -1)
655 {
656 puts ("fork failed");
657 exit (1);
658 }
659
660 if (pid == 0)
661 {
662 /* Make the program disappear after a while. */
26676450
UD
663 if (arg == NULL)
664 sleep (10);
76a50749
UD
665 exit (0);
666 }
667
26676450
UD
668 int r;
669 if (arg != NULL)
670 {
671 struct timespec ts = { .tv_sec = 0, .tv_nsec = 100000000 };
672 while (nanosleep (&ts, &ts) != 0)
673 continue;
674
675 r = pthread_barrier_wait (&b2);
676 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
677 {
678 printf ("%s: barrier_wait failed\n", __FUNCTION__);
679 exit (1);
680 }
681 }
682
683 r = pthread_barrier_wait (&b2);
684 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
685 {
686 printf ("%s: barrier_wait failed\n", __FUNCTION__);
687 exit (1);
688 }
689
f23b30e2 690 int s;
26676450 691 pthread_cleanup_push (cl, NULL);
0e0deb03 692
f23b30e2
UD
693 s = waitpid (-1, NULL, 0);
694
695 pthread_cleanup_pop (0);
76a50749
UD
696
697 printf ("%s: waitpid returns with %d (%s)\n", __FUNCTION__, s,
698 strerror (errno));
699
700 exit (1);
701}
702
703
704static void *
705tf_waitid (void *arg)
706{
76a50749
UD
707 pid_t pid = fork ();
708 if (pid == -1)
709 {
710 puts ("fork failed");
711 exit (1);
712 }
713
714 if (pid == 0)
715 {
716 /* Make the program disappear after a while. */
26676450
UD
717 if (arg == NULL)
718 sleep (10);
76a50749
UD
719 exit (0);
720 }
721
26676450
UD
722 int r;
723 if (arg != NULL)
724 {
725 struct timespec ts = { .tv_sec = 0, .tv_nsec = 100000000 };
726 while (nanosleep (&ts, &ts) != 0)
727 continue;
728
729 r = pthread_barrier_wait (&b2);
730 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
731 {
732 printf ("%s: barrier_wait failed\n", __FUNCTION__);
733 exit (1);
734 }
735 }
736
737 r = pthread_barrier_wait (&b2);
738 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
739 {
740 printf ("%s: barrier_wait failed\n", __FUNCTION__);
741 exit (1);
742 }
743
f23b30e2 744 int s;
0e0deb03
UD
745 pthread_cleanup_push (cl, NULL);
746
4fb907b7
RM
747#ifndef WEXITED
748# define WEXITED 0
749#endif
76a50749 750 siginfo_t si;
4fb907b7 751 s = waitid (P_PID, pid, &si, WEXITED);
f23b30e2
UD
752
753 pthread_cleanup_pop (0);
76a50749
UD
754
755 printf ("%s: waitid returns with %d (%s)\n", __FUNCTION__, s,
756 strerror (errno));
757
758 exit (1);
759}
760
761
26676450
UD
762static void *
763tf_sigpause (void *arg)
76a50749 764{
26676450
UD
765 int r = pthread_barrier_wait (&b2);
766 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
76a50749 767 {
26676450 768 printf ("%s: barrier_wait failed\n", __FUNCTION__);
76a50749
UD
769 exit (1);
770 }
771
26676450 772 if (arg != NULL)
76a50749 773 {
26676450
UD
774 r = pthread_barrier_wait (&b2);
775 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
76a50749 776 {
26676450 777 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
76a50749
UD
778 exit (1);
779 }
26676450 780 }
76a50749 781
26676450 782 pthread_cleanup_push (cl, NULL);
0e0deb03 783
26676450
UD
784 /* Just for fun block the cancellation signal. We need to use
785 __xpg_sigpause since otherwise we will get the BSD version. */
786 __xpg_sigpause (SIGCANCEL);
76a50749 787
26676450
UD
788 pthread_cleanup_pop (0);
789
790 printf ("%s: sigpause returned\n", __FUNCTION__);
791
792 exit (1);
793}
794
795
796static void *
797tf_sigsuspend (void *arg)
798{
799 int r = pthread_barrier_wait (&b2);
800 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
801 {
802 printf ("%s: barrier_wait failed\n", __FUNCTION__);
803 exit (1);
804 }
805
806 if (arg != NULL)
807 {
808 r = pthread_barrier_wait (&b2);
76a50749
UD
809 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
810 {
26676450
UD
811 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
812 exit (1);
76a50749 813 }
26676450 814 }
76a50749 815
26676450 816 pthread_cleanup_push (cl, NULL);
76a50749 817
26676450
UD
818 /* Just for fun block all signals. */
819 sigset_t mask;
820 sigfillset (&mask);
821 sigsuspend (&mask);
76a50749 822
26676450
UD
823 pthread_cleanup_pop (0);
824
825 printf ("%s: sigsuspend returned\n", __FUNCTION__);
826
827 exit (1);
828}
829
830
831static void *
832tf_sigwait (void *arg)
833{
834 int r = pthread_barrier_wait (&b2);
835 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
836 {
837 printf ("%s: barrier_wait failed\n", __FUNCTION__);
838 exit (1);
839 }
840
841 if (arg != NULL)
842 {
843 r = pthread_barrier_wait (&b2);
844 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
845 {
846 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
847 exit (1);
848 }
849 }
850
26676450
UD
851 /* Block SIGUSR1. */
852 sigset_t mask;
97b6614c 853 sigemptyset (&mask);
26676450
UD
854 sigaddset (&mask, SIGUSR1);
855 if (pthread_sigmask (SIG_BLOCK, &mask, NULL) != 0)
856 {
857 printf ("%s: pthread_sigmask failed\n", __FUNCTION__);
858 exit (1);
859 }
860
26676450 861 int sig;
f23b30e2 862 pthread_cleanup_push (cl, NULL);
26676450 863
f23b30e2
UD
864 /* Wait for SIGUSR1. */
865 sigwait (&mask, &sig);
26676450
UD
866
867 pthread_cleanup_pop (0);
868
f23b30e2
UD
869 printf ("%s: sigwait returned with signal %d\n", __FUNCTION__, sig);
870
26676450
UD
871 exit (1);
872}
873
874
875static void *
876tf_sigwaitinfo (void *arg)
877{
878 int r = pthread_barrier_wait (&b2);
879 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
880 {
881 printf ("%s: barrier_wait failed\n", __FUNCTION__);
882 exit (1);
883 }
884
885 if (arg != NULL)
886 {
887 r = pthread_barrier_wait (&b2);
888 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
889 {
890 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
891 exit (1);
892 }
893 }
894
26676450
UD
895 /* Block SIGUSR1. */
896 sigset_t mask;
97b6614c 897 sigemptyset (&mask);
26676450
UD
898 sigaddset (&mask, SIGUSR1);
899 if (pthread_sigmask (SIG_BLOCK, &mask, NULL) != 0)
900 {
901 printf ("%s: pthread_sigmask failed\n", __FUNCTION__);
902 exit (1);
903 }
904
26676450 905 siginfo_t info;
f23b30e2
UD
906 pthread_cleanup_push (cl, NULL);
907
908 /* Wait for SIGUSR1. */
26676450
UD
909 sigwaitinfo (&mask, &info);
910
f23b30e2
UD
911 pthread_cleanup_pop (0);
912
26676450
UD
913 printf ("%s: sigwaitinfo returned with signal %d\n", __FUNCTION__,
914 info.si_signo);
915
26676450
UD
916 exit (1);
917}
918
919
920static void *
921tf_sigtimedwait (void *arg)
922{
923 int r = pthread_barrier_wait (&b2);
924 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
925 {
926 printf ("%s: barrier_wait failed\n", __FUNCTION__);
927 exit (1);
928 }
929
930 if (arg != NULL)
931 {
932 r = pthread_barrier_wait (&b2);
933 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
934 {
935 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
936 exit (1);
937 }
938 }
939
26676450
UD
940 /* Block SIGUSR1. */
941 sigset_t mask;
97b6614c 942 sigemptyset (&mask);
26676450
UD
943 sigaddset (&mask, SIGUSR1);
944 if (pthread_sigmask (SIG_BLOCK, &mask, NULL) != 0)
945 {
946 printf ("%s: pthread_sigmask failed\n", __FUNCTION__);
947 exit (1);
948 }
949
950 /* Wait for SIGUSR1. */
951 siginfo_t info;
952 struct timespec ts = { .tv_sec = 60, .tv_nsec = 0 };
f23b30e2
UD
953 pthread_cleanup_push (cl, NULL);
954
26676450
UD
955 sigtimedwait (&mask, &info, &ts);
956
f23b30e2
UD
957 pthread_cleanup_pop (0);
958
26676450
UD
959 printf ("%s: sigtimedwait returned with signal %d\n", __FUNCTION__,
960 info.si_signo);
961
26676450
UD
962 exit (1);
963}
964
965
966static void *
967tf_pause (void *arg)
968{
969 int r = pthread_barrier_wait (&b2);
970 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
971 {
972 printf ("%s: barrier_wait failed\n", __FUNCTION__);
973 exit (1);
974 }
975
976 if (arg != NULL)
977 {
978 r = pthread_barrier_wait (&b2);
979 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
980 {
981 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
982 exit (1);
983 }
984 }
985
986 pthread_cleanup_push (cl, NULL);
987
988 pause ();
989
990 pthread_cleanup_pop (0);
991
992 printf ("%s: pause returned\n", __FUNCTION__);
993
994 exit (1);
995}
996
997
998static void *
999tf_accept (void *arg)
1000{
1001 struct sockaddr_un sun;
1002 /* To test a non-blocking accept call we make the call file by using
1003 a datagrame socket. */
1004 int pf = arg == NULL ? SOCK_STREAM : SOCK_DGRAM;
1005
1006 tempfd = socket (AF_UNIX, pf, 0);
1007 if (tempfd == -1)
1008 {
1009 printf ("%s: socket call failed\n", __FUNCTION__);
1010 exit (1);
1011 }
1012
1013 int tries = 0;
1014 do
1015 {
1016 if (++tries > 10)
1017 {
1018 printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
1019 }
1020
f23b30e2 1021 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-1-XXXXXX");
26676450
UD
1022 if (mktemp (sun.sun_path) == NULL)
1023 {
1024 printf ("%s: cannot generate temp file name\n", __FUNCTION__);
1025 exit (1);
1026 }
1027
1028 sun.sun_family = AF_UNIX;
1029 }
1030 while (bind (tempfd, (struct sockaddr *) &sun,
1031 offsetof (struct sockaddr_un, sun_path)
1032 + strlen (sun.sun_path) + 1) != 0);
1033
1034 unlink (sun.sun_path);
1035
1036 listen (tempfd, 5);
1037
1038 socklen_t len = sizeof (sun);
1039
1040 int r = pthread_barrier_wait (&b2);
1041 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1042 {
1043 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1044 exit (1);
1045 }
1046
1047 if (arg != NULL)
1048 {
1049 r = pthread_barrier_wait (&b2);
1050 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1051 {
1052 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1053 exit (1);
1054 }
1055 }
1056
1057 pthread_cleanup_push (cl, NULL);
1058
1059 accept (tempfd, (struct sockaddr *) &sun, &len);
1060
1061 pthread_cleanup_pop (0);
1062
1063 printf ("%s: accept returned\n", __FUNCTION__);
1064
1065 exit (1);
1066}
1067
1068
1069static void *
1070tf_send (void *arg)
1071{
1072 struct sockaddr_un sun;
1073
1074 tempfd = socket (AF_UNIX, SOCK_STREAM, 0);
1075 if (tempfd == -1)
1076 {
1077 printf ("%s: first socket call failed\n", __FUNCTION__);
1078 exit (1);
1079 }
1080
1081 int tries = 0;
1082 do
1083 {
1084 if (++tries > 10)
1085 {
1086 printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
1087 }
1088
f23b30e2 1089 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-2-XXXXXX");
26676450
UD
1090 if (mktemp (sun.sun_path) == NULL)
1091 {
1092 printf ("%s: cannot generate temp file name\n", __FUNCTION__);
1093 exit (1);
1094 }
1095
1096 sun.sun_family = AF_UNIX;
1097 }
1098 while (bind (tempfd, (struct sockaddr *) &sun,
1099 offsetof (struct sockaddr_un, sun_path)
1100 + strlen (sun.sun_path) + 1) != 0);
1101
1102 listen (tempfd, 5);
1103
1104 tempfd2 = socket (AF_UNIX, SOCK_STREAM, 0);
1105 if (tempfd2 == -1)
1106 {
1107 printf ("%s: second socket call failed\n", __FUNCTION__);
1108 exit (1);
1109 }
1110
1111 if (connect (tempfd2, (struct sockaddr *) &sun, sizeof (sun)) != 0)
1112 {
1113 printf ("%s: connect failed\n", __FUNCTION__);
1114 exit(1);
1115 }
1116
1117 unlink (sun.sun_path);
1118
1119 int r = pthread_barrier_wait (&b2);
1120 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1121 {
1122 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1123 exit (1);
1124 }
1125
1126 if (arg != NULL)
1127 {
1128 r = pthread_barrier_wait (&b2);
1129 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1130 {
1131 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1132 exit (1);
1133 }
1134 }
1135
1136 pthread_cleanup_push (cl, NULL);
1137
1138 /* Very large block, so that the send call blocks. */
1139 char mem[700000];
1140
1141 send (tempfd2, mem, arg == NULL ? sizeof (mem) : 1, 0);
1142
1143 pthread_cleanup_pop (0);
1144
1145 printf ("%s: send returned\n", __FUNCTION__);
1146
1147 exit (1);
1148}
1149
1150
1151static void *
1152tf_recv (void *arg)
1153{
1154 struct sockaddr_un sun;
1155
1156 tempfd = socket (AF_UNIX, SOCK_STREAM, 0);
1157 if (tempfd == -1)
1158 {
1159 printf ("%s: first socket call failed\n", __FUNCTION__);
1160 exit (1);
1161 }
1162
1163 int tries = 0;
1164 do
1165 {
1166 if (++tries > 10)
1167 {
1168 printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
1169 }
1170
f23b30e2 1171 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-3-XXXXXX");
26676450
UD
1172 if (mktemp (sun.sun_path) == NULL)
1173 {
1174 printf ("%s: cannot generate temp file name\n", __FUNCTION__);
1175 exit (1);
1176 }
1177
1178 sun.sun_family = AF_UNIX;
1179 }
1180 while (bind (tempfd, (struct sockaddr *) &sun,
1181 offsetof (struct sockaddr_un, sun_path)
1182 + strlen (sun.sun_path) + 1) != 0);
1183
1184 listen (tempfd, 5);
1185
1186 tempfd2 = socket (AF_UNIX, SOCK_STREAM, 0);
1187 if (tempfd2 == -1)
1188 {
1189 printf ("%s: second socket call failed\n", __FUNCTION__);
1190 exit (1);
1191 }
1192
1193 if (connect (tempfd2, (struct sockaddr *) &sun, sizeof (sun)) != 0)
1194 {
1195 printf ("%s: connect failed\n", __FUNCTION__);
1196 exit(1);
1197 }
1198
1199 unlink (sun.sun_path);
1200
1201 int r = pthread_barrier_wait (&b2);
1202 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1203 {
1204 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1205 exit (1);
1206 }
1207
1208 if (arg != NULL)
1209 {
1210 r = pthread_barrier_wait (&b2);
1211 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1212 {
1213 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1214 exit (1);
1215 }
1216 }
1217
1218 pthread_cleanup_push (cl, NULL);
1219
1220 char mem[70];
1221
1222 recv (tempfd2, mem, arg == NULL ? sizeof (mem) : 0, 0);
1223
1224 pthread_cleanup_pop (0);
1225
1226 printf ("%s: recv returned\n", __FUNCTION__);
1227
1228 exit (1);
1229}
1230
1231
1232static void *
1233tf_recvfrom (void *arg)
1234{
1235 struct sockaddr_un sun;
1236
1237 tempfd = socket (AF_UNIX, SOCK_DGRAM, 0);
1238 if (tempfd == -1)
1239 {
1240 printf ("%s: first socket call failed\n", __FUNCTION__);
1241 exit (1);
1242 }
1243
1244 int tries = 0;
1245 do
1246 {
1247 if (++tries > 10)
1248 {
1249 printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
1250 }
1251
f23b30e2 1252 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-4-XXXXXX");
26676450
UD
1253 if (mktemp (sun.sun_path) == NULL)
1254 {
1255 printf ("%s: cannot generate temp file name\n", __FUNCTION__);
1256 exit (1);
1257 }
1258
1259 sun.sun_family = AF_UNIX;
1260 }
1261 while (bind (tempfd, (struct sockaddr *) &sun,
1262 offsetof (struct sockaddr_un, sun_path)
1263 + strlen (sun.sun_path) + 1) != 0);
1264
1265 tempfname = strdup (sun.sun_path);
1266
1267 tempfd2 = socket (AF_UNIX, SOCK_DGRAM, 0);
1268 if (tempfd2 == -1)
1269 {
1270 printf ("%s: second socket call failed\n", __FUNCTION__);
1271 exit (1);
1272 }
1273
1274 int r = pthread_barrier_wait (&b2);
1275 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1276 {
1277 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1278 exit (1);
1279 }
1280
1281 if (arg != NULL)
1282 {
1283 r = pthread_barrier_wait (&b2);
1284 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1285 {
1286 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1287 exit (1);
1288 }
1289 }
1290
1291 pthread_cleanup_push (cl, NULL);
1292
1293 char mem[70];
1294 socklen_t len = sizeof (sun);
1295
1296 recvfrom (tempfd2, mem, arg == NULL ? sizeof (mem) : 0, 0,
1297 (struct sockaddr *) &sun, &len);
1298
1299 pthread_cleanup_pop (0);
1300
1301 printf ("%s: recvfrom returned\n", __FUNCTION__);
1302
1303 exit (1);
1304}
1305
1306
1307static void *
1308tf_recvmsg (void *arg)
1309{
1310 struct sockaddr_un sun;
1311
1312 tempfd = socket (AF_UNIX, SOCK_DGRAM, 0);
1313 if (tempfd == -1)
1314 {
1315 printf ("%s: first socket call failed\n", __FUNCTION__);
1316 exit (1);
1317 }
1318
1319 int tries = 0;
1320 do
1321 {
1322 if (++tries > 10)
1323 {
1324 printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
1325 }
1326
f23b30e2 1327 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-5-XXXXXX");
26676450
UD
1328 if (mktemp (sun.sun_path) == NULL)
1329 {
1330 printf ("%s: cannot generate temp file name\n", __FUNCTION__);
1331 exit (1);
1332 }
1333
1334 sun.sun_family = AF_UNIX;
1335 }
1336 while (bind (tempfd, (struct sockaddr *) &sun,
1337 offsetof (struct sockaddr_un, sun_path)
1338 + strlen (sun.sun_path) + 1) != 0);
1339
1340 tempfname = strdup (sun.sun_path);
1341
1342 tempfd2 = socket (AF_UNIX, SOCK_DGRAM, 0);
1343 if (tempfd2 == -1)
1344 {
1345 printf ("%s: second socket call failed\n", __FUNCTION__);
1346 exit (1);
1347 }
1348
1349 int r = pthread_barrier_wait (&b2);
1350 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1351 {
1352 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1353 exit (1);
1354 }
1355
1356 if (arg != NULL)
1357 {
1358 r = pthread_barrier_wait (&b2);
1359 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1360 {
1361 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1362 exit (1);
1363 }
1364 }
1365
1366 pthread_cleanup_push (cl, NULL);
1367
1368 char mem[70];
1369 struct iovec iov[1];
1370 iov[0].iov_base = mem;
1371 iov[0].iov_len = arg == NULL ? sizeof (mem) : 0;
1372
1373 struct msghdr m;
1374 m.msg_name = &sun;
1375 m.msg_namelen = sizeof (sun);
1376 m.msg_iov = iov;
1377 m.msg_iovlen = 1;
1378 m.msg_control = NULL;
1379 m.msg_controllen = 0;
1380
1381 recvmsg (tempfd2, &m, 0);
1382
1383 pthread_cleanup_pop (0);
1384
1385 printf ("%s: recvmsg returned\n", __FUNCTION__);
1386
1387 exit (1);
1388}
1389
1390
047aec8f
UD
1391static void *
1392tf_open (void *arg)
1393{
1394 if (arg == NULL)
1395 // XXX If somebody can provide a portable test case in which open()
1396 // blocks we can enable this test to run in both rounds.
1397 abort ();
1398
1399 int r = pthread_barrier_wait (&b2);
1400 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1401 {
1402 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1403 exit (1);
1404 }
1405
1406 r = pthread_barrier_wait (&b2);
1407 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1408 {
1409 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1410 exit (1);
1411 }
1412
1413 pthread_cleanup_push (cl, NULL);
1414
1415 open ("Makefile", O_RDONLY);
1416
1417 pthread_cleanup_pop (0);
1418
1419 printf ("%s: open returned\n", __FUNCTION__);
1420
1421 exit (1);
1422}
1423
1424
1425static void *
1426tf_close (void *arg)
1427{
1428 if (arg == NULL)
1429 // XXX If somebody can provide a portable test case in which close()
1430 // blocks we can enable this test to run in both rounds.
1431 abort ();
1432
1433 char fname[] = "/tmp/tst-cancel-fd-XXXXXX";
1434 tempfd = mkstemp (fname);
1435 if (tempfd == -1)
1436 {
1437 printf ("%s: mkstemp failed\n", __FUNCTION__);
1438 exit (1);
1439 }
1440 unlink (fname);
1441
1442 int r = pthread_barrier_wait (&b2);
1443 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1444 {
1445 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1446 exit (1);
1447 }
1448
1449 r = pthread_barrier_wait (&b2);
1450 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1451 {
1452 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1453 exit (1);
1454 }
1455
1456 pthread_cleanup_push (cl, NULL);
1457
1458 close (tempfd);
1459
1460 pthread_cleanup_pop (0);
1461
1462 printf ("%s: close returned\n", __FUNCTION__);
1463
1464 exit (1);
1465}
1466
1467
1468static void *
1469tf_pread (void *arg)
1470{
1471 if (arg == NULL)
1472 // XXX If somebody can provide a portable test case in which pread()
1473 // blocks we can enable this test to run in both rounds.
1474 abort ();
1475
1476 tempfd = open ("Makefile", O_RDONLY);
1477 if (tempfd == -1)
1478 {
1479 printf ("%s: cannot open Makefile\n", __FUNCTION__);
1480 exit (1);
1481 }
1482
1483 int r = pthread_barrier_wait (&b2);
1484 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1485 {
1486 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1487 exit (1);
1488 }
1489
1490 r = pthread_barrier_wait (&b2);
1491 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1492 {
1493 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1494 exit (1);
1495 }
1496
1497 pthread_cleanup_push (cl, NULL);
1498
1499 char mem[10];
1500 pread (tempfd, mem, sizeof (mem), 0);
1501
1502 pthread_cleanup_pop (0);
1503
1504 printf ("%s: pread returned\n", __FUNCTION__);
1505
1506 exit (1);
1507}
1508
1509
1510static void *
1511tf_pwrite (void *arg)
1512{
1513 if (arg == NULL)
1514 // XXX If somebody can provide a portable test case in which pwrite()
1515 // blocks we can enable this test to run in both rounds.
1516 abort ();
1517
1518 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
1519 tempfd = mkstemp (fname);
1520 if (tempfd == -1)
1521 {
1522 printf ("%s: mkstemp failed\n", __FUNCTION__);
1523 exit (1);
1524 }
1525 unlink (fname);
1526
1527 int r = pthread_barrier_wait (&b2);
1528 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1529 {
1530 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1531 exit (1);
1532 }
1533
1534 r = pthread_barrier_wait (&b2);
1535 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1536 {
1537 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1538 exit (1);
1539 }
1540
1541 pthread_cleanup_push (cl, NULL);
1542
1543 char mem[10];
1544 pwrite (tempfd, mem, sizeof (mem), 0);
1545
1546 pthread_cleanup_pop (0);
1547
1548 printf ("%s: pwrite returned\n", __FUNCTION__);
1549
1550 exit (1);
1551}
1552
1553
1554static void *
1555tf_fsync (void *arg)
1556{
1557 if (arg == NULL)
1558 // XXX If somebody can provide a portable test case in which fsync()
1559 // blocks we can enable this test to run in both rounds.
1560 abort ();
1561
1562 tempfd = open ("Makefile", O_RDONLY);
1563 if (tempfd == -1)
1564 {
1565 printf ("%s: cannot open Makefile\n", __FUNCTION__);
1566 exit (1);
1567 }
1568
1569 int r = pthread_barrier_wait (&b2);
1570 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1571 {
1572 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1573 exit (1);
1574 }
1575
1576 r = pthread_barrier_wait (&b2);
1577 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1578 {
1579 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1580 exit (1);
1581 }
1582
1583 pthread_cleanup_push (cl, NULL);
1584
1585 fsync (tempfd);
1586
1587 pthread_cleanup_pop (0);
1588
1589 printf ("%s: fsync returned\n", __FUNCTION__);
1590
1591 exit (1);
1592}
1593
1594
aa75f64c
UD
1595static void *
1596tf_fdatasync (void *arg)
1597{
1598 if (arg == NULL)
1599 // XXX If somebody can provide a portable test case in which fdatasync()
1600 // blocks we can enable this test to run in both rounds.
1601 abort ();
1602
1603 tempfd = open ("Makefile", O_RDONLY);
1604 if (tempfd == -1)
1605 {
1606 printf ("%s: cannot open Makefile\n", __FUNCTION__);
1607 exit (1);
1608 }
1609
1610 int r = pthread_barrier_wait (&b2);
1611 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1612 {
1613 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1614 exit (1);
1615 }
1616
1617 r = pthread_barrier_wait (&b2);
1618 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1619 {
1620 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1621 exit (1);
1622 }
1623
1624 pthread_cleanup_push (cl, NULL);
1625
1626 fdatasync (tempfd);
1627
1628 pthread_cleanup_pop (0);
1629
1630 printf ("%s: fdatasync returned\n", __FUNCTION__);
1631
1632 exit (1);
1633}
1634
1635
047aec8f
UD
1636static void *
1637tf_msync (void *arg)
1638{
1639 if (arg == NULL)
1640 // XXX If somebody can provide a portable test case in which msync()
1641 // blocks we can enable this test to run in both rounds.
1642 abort ();
1643
1644 tempfd = open ("Makefile", O_RDONLY);
1645 if (tempfd == -1)
1646 {
1647 printf ("%s: cannot open Makefile\n", __FUNCTION__);
1648 exit (1);
1649 }
1650 void *p = mmap (NULL, 10, PROT_READ, MAP_SHARED, tempfd, 0);
1651 if (p == MAP_FAILED)
1652 {
1653 printf ("%s: mmap failed\n", __FUNCTION__);
1654 exit (1);
1655 }
1656
1657 int r = pthread_barrier_wait (&b2);
1658 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1659 {
1660 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1661 exit (1);
1662 }
1663
1664 r = pthread_barrier_wait (&b2);
1665 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1666 {
1667 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1668 exit (1);
1669 }
1670
1671 pthread_cleanup_push (cl, NULL);
1672
1673 msync (p, 10, 0);
1674
1675 pthread_cleanup_pop (0);
1676
1677 printf ("%s: msync returned\n", __FUNCTION__);
1678
1679 exit (1);
1680}
1681
1682
f23b30e2
UD
1683static void *
1684tf_sendto (void *arg)
1685{
1686 if (arg == NULL)
1687 // XXX If somebody can provide a portable test case in which sendto()
1688 // blocks we can enable this test to run in both rounds.
1689 abort ();
1690
1691 struct sockaddr_un sun;
1692
1693 tempfd = socket (AF_UNIX, SOCK_DGRAM, 0);
1694 if (tempfd == -1)
1695 {
1696 printf ("%s: first socket call failed\n", __FUNCTION__);
1697 exit (1);
1698 }
1699
1700 int tries = 0;
1701 do
1702 {
1703 if (++tries > 10)
1704 {
1705 printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
1706 }
1707
1708 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-6-XXXXXX");
1709 if (mktemp (sun.sun_path) == NULL)
1710 {
1711 printf ("%s: cannot generate temp file name\n", __FUNCTION__);
1712 exit (1);
1713 }
1714
1715 sun.sun_family = AF_UNIX;
1716 }
1717 while (bind (tempfd, (struct sockaddr *) &sun,
1718 offsetof (struct sockaddr_un, sun_path)
1719 + strlen (sun.sun_path) + 1) != 0);
1720 tempfname = strdup (sun.sun_path);
1721
1722 tempfd2 = socket (AF_UNIX, SOCK_DGRAM, 0);
1723 if (tempfd2 == -1)
1724 {
1725 printf ("%s: second socket call failed\n", __FUNCTION__);
1726 exit (1);
1727 }
1728
1729 int r = pthread_barrier_wait (&b2);
1730 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1731 {
1732 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1733 exit (1);
1734 }
1735
1736 r = pthread_barrier_wait (&b2);
1737 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1738 {
1739 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1740 exit (1);
1741 }
1742
1743 pthread_cleanup_push (cl, NULL);
1744
1745 char mem[1];
1746
1747 sendto (tempfd2, mem, arg == NULL ? sizeof (mem) : 1, 0,
1748 (struct sockaddr *) &sun,
1749 offsetof (struct sockaddr_un, sun_path) + strlen (sun.sun_path) + 1);
1750
1751 pthread_cleanup_pop (0);
1752
1753 printf ("%s: sendto returned\n", __FUNCTION__);
1754
1755 exit (1);
1756}
1757
1758
1759static void *
1760tf_sendmsg (void *arg)
1761{
1762 if (arg == NULL)
1763 // XXX If somebody can provide a portable test case in which sendmsg()
1764 // blocks we can enable this test to run in both rounds.
1765 abort ();
1766
1767 struct sockaddr_un sun;
1768
1769 tempfd = socket (AF_UNIX, SOCK_DGRAM, 0);
1770 if (tempfd == -1)
1771 {
1772 printf ("%s: first socket call failed\n", __FUNCTION__);
1773 exit (1);
1774 }
1775
1776 int tries = 0;
1777 do
1778 {
1779 if (++tries > 10)
1780 {
1781 printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
1782 }
1783
1784 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-7-XXXXXX");
1785 if (mktemp (sun.sun_path) == NULL)
1786 {
1787 printf ("%s: cannot generate temp file name\n", __FUNCTION__);
1788 exit (1);
1789 }
1790
1791 sun.sun_family = AF_UNIX;
1792 }
1793 while (bind (tempfd, (struct sockaddr *) &sun,
1794 offsetof (struct sockaddr_un, sun_path)
1795 + strlen (sun.sun_path) + 1) != 0);
1796 tempfname = strdup (sun.sun_path);
1797
1798 tempfd2 = socket (AF_UNIX, SOCK_DGRAM, 0);
1799 if (tempfd2 == -1)
1800 {
1801 printf ("%s: second socket call failed\n", __FUNCTION__);
1802 exit (1);
1803 }
1804
1805 int r = pthread_barrier_wait (&b2);
1806 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1807 {
1808 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1809 exit (1);
1810 }
1811
1812 r = pthread_barrier_wait (&b2);
1813 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1814 {
1815 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1816 exit (1);
1817 }
1818
1819 pthread_cleanup_push (cl, NULL);
1820
1821 char mem[1];
1822 struct iovec iov[1];
1823 iov[0].iov_base = mem;
1824 iov[0].iov_len = 1;
1825
1826 struct msghdr m;
1827 m.msg_name = &sun;
1828 m.msg_namelen = (offsetof (struct sockaddr_un, sun_path)
1829 + strlen (sun.sun_path) + 1);
1830 m.msg_iov = iov;
1831 m.msg_iovlen = 1;
1832 m.msg_control = NULL;
1833 m.msg_controllen = 0;
1834
1835 sendmsg (tempfd2, &m, 0);
1836
1837 pthread_cleanup_pop (0);
1838
1839 printf ("%s: sendmsg returned\n", __FUNCTION__);
1840
1841 exit (1);
1842}
1843
1844
1845static void *
1846tf_creat (void *arg)
1847{
1848 if (arg == NULL)
1849 // XXX If somebody can provide a portable test case in which sendmsg()
1850 // blocks we can enable this test to run in both rounds.
1851 abort ();
1852
1853 int r = pthread_barrier_wait (&b2);
1854 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1855 {
1856 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1857 exit (1);
1858 }
1859
1860 r = pthread_barrier_wait (&b2);
1861 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1862 {
1863 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1864 exit (1);
1865 }
1866
1867 pthread_cleanup_push (cl, NULL);
1868
1869 creat ("tmp/tst-cancel-4-should-not-exist", 0666);
1870
1871 pthread_cleanup_pop (0);
1872
1873 printf ("%s: creat returned\n", __FUNCTION__);
1874
1875 exit (1);
1876}
1877
1878
1879static void *
1880tf_connect (void *arg)
1881{
1882 if (arg == NULL)
1883 // XXX If somebody can provide a portable test case in which connect()
1884 // blocks we can enable this test to run in both rounds.
1885 abort ();
1886
1887 struct sockaddr_un sun;
1888
1889 tempfd = socket (AF_UNIX, SOCK_STREAM, 0);
1890 if (tempfd == -1)
1891 {
1892 printf ("%s: first socket call failed\n", __FUNCTION__);
1893 exit (1);
1894 }
1895
1896 int tries = 0;
1897 do
1898 {
1899 if (++tries > 10)
1900 {
1901 printf ("%s: too many unsuccessful bind calls\n", __FUNCTION__);
1902 }
1903
1904 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-2-XXXXXX");
1905 if (mktemp (sun.sun_path) == NULL)
1906 {
1907 printf ("%s: cannot generate temp file name\n", __FUNCTION__);
1908 exit (1);
1909 }
1910
1911 sun.sun_family = AF_UNIX;
1912 }
1913 while (bind (tempfd, (struct sockaddr *) &sun,
1914 offsetof (struct sockaddr_un, sun_path)
1915 + strlen (sun.sun_path) + 1) != 0);
1916 tempfname = strdup (sun.sun_path);
1917
1918 listen (tempfd, 5);
1919
1920 tempfd2 = socket (AF_UNIX, SOCK_STREAM, 0);
1921 if (tempfd2 == -1)
1922 {
1923 printf ("%s: second socket call failed\n", __FUNCTION__);
1924 exit (1);
1925 }
1926
1927 int r = pthread_barrier_wait (&b2);
1928 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1929 {
1930 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1931 exit (1);
1932 }
1933
1934 if (arg != NULL)
1935 {
1936 r = pthread_barrier_wait (&b2);
1937 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1938 {
1939 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1940 exit (1);
1941 }
1942 }
1943
1944 pthread_cleanup_push (cl, NULL);
1945
1946 connect (tempfd2, (struct sockaddr *) &sun, sizeof (sun));
1947
1948 pthread_cleanup_pop (0);
1949
1950 printf ("%s: connect returned\n", __FUNCTION__);
1951
1952 exit (1);
1953}
1954
1955
1956static void *
1957tf_tcdrain (void *arg)
1958{
1959 if (arg == NULL)
1960 // XXX If somebody can provide a portable test case in which tcdrain()
1961 // blocks we can enable this test to run in both rounds.
1962 abort ();
1963
1964 int r = pthread_barrier_wait (&b2);
1965 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1966 {
1967 printf ("%s: barrier_wait failed\n", __FUNCTION__);
1968 exit (1);
1969 }
1970
1971 if (arg != NULL)
1972 {
1973 r = pthread_barrier_wait (&b2);
1974 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
1975 {
1976 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
1977 exit (1);
1978 }
1979 }
1980
1981 pthread_cleanup_push (cl, NULL);
1982
1983 /* Regardless of stderr being a terminal, the tcdrain call should be
1984 canceled. */
1985 tcdrain (STDERR_FILENO);
1986
1987 pthread_cleanup_pop (0);
1988
1989 printf ("%s: tcdrain returned\n", __FUNCTION__);
1990
1991 exit (1);
1992}
1993
1994
1995static void *
1996tf_msgrcv (void *arg)
1997{
483e95d0 1998 tempmsg = msgget (IPC_PRIVATE, 0666 | IPC_CREAT);
67060ef5
UD
1999 if (tempmsg == -1)
2000 {
2001 printf ("%s: msgget failed: %s\n", __FUNCTION__, strerror (errno));
2002 exit (1);
2003 }
f23b30e2
UD
2004
2005 int r = pthread_barrier_wait (&b2);
2006 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
2007 {
2008 printf ("%s: barrier_wait failed\n", __FUNCTION__);
2009 exit (1);
2010 }
2011
2012 if (arg != NULL)
2013 {
2014 r = pthread_barrier_wait (&b2);
2015 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
2016 {
2017 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
2018 exit (1);
2019 }
2020 }
2021
7d7ff54c
UD
2022 ssize_t s;
2023
f23b30e2
UD
2024 pthread_cleanup_push (cl, NULL);
2025
2026 struct
2027 {
2028 long int type;
2029 char mem[10];
2030 } m;
ca343e73
UD
2031 int randnr;
2032 /* We need a positive random number. */
2033 do
483e95d0 2034 randnr = random () % 64000;
ca343e73
UD
2035 while (randnr <= 0);
2036 do
2037 {
2038 errno = 0;
7d7ff54c 2039 s = msgrcv (tempmsg, (struct msgbuf *) &m, 10, randnr, 0);
ca343e73 2040 }
7d7ff54c 2041 while (errno == EIDRM || errno == EINTR);
f23b30e2
UD
2042
2043 pthread_cleanup_pop (0);
2044
7d7ff54c 2045 printf ("%s: msgrcv returned %zd with errno = %m\n", __FUNCTION__, s);
f23b30e2 2046
67060ef5
UD
2047 msgctl (tempmsg, IPC_RMID, NULL);
2048
f23b30e2
UD
2049 exit (1);
2050}
2051
2052
2053static void *
2054tf_msgsnd (void *arg)
2055{
2056 if (arg == NULL)
2057 // XXX If somebody can provide a portable test case in which msgsnd()
2058 // blocks we can enable this test to run in both rounds.
2059 abort ();
2060
483e95d0 2061 tempmsg = msgget (IPC_PRIVATE, 0666 | IPC_CREAT);
67060ef5
UD
2062 if (tempmsg == -1)
2063 {
2064 printf ("%s: msgget failed: %s\n", __FUNCTION__, strerror (errno));
2065 exit (1);
2066 }
f23b30e2
UD
2067
2068 int r = pthread_barrier_wait (&b2);
2069 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
2070 {
2071 printf ("%s: barrier_wait failed\n", __FUNCTION__);
2072 exit (1);
2073 }
2074
2075 r = pthread_barrier_wait (&b2);
2076 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
2077 {
2078 printf ("%s: 2nd barrier_wait failed\n", __FUNCTION__);
2079 exit (1);
2080 }
2081
2082 pthread_cleanup_push (cl, NULL);
2083
2084 struct
2085 {
2086 long int type;
2087 char mem[1];
2088 } m;
ca343e73
UD
2089 /* We need a positive random number. */
2090 do
483e95d0 2091 m.type = random () % 64000;
ca343e73 2092 while (m.type <= 0);
f23b30e2
UD
2093 msgsnd (tempmsg, (struct msgbuf *) &m, sizeof (m.mem), 0);
2094
2095 pthread_cleanup_pop (0);
2096
2097 printf ("%s: msgsnd returned\n", __FUNCTION__);
2098
67060ef5
UD
2099 msgctl (tempmsg, IPC_RMID, NULL);
2100
f23b30e2
UD
2101 exit (1);
2102}
2103
2104
26676450
UD
2105static struct
2106{
2107 const char *name;
2108 void *(*tf) (void *);
2109 int nb;
2110 int only_early;
2111} tests[] =
2112{
2113#define ADD_TEST(name, nbar, early) { #name, tf_##name, nbar, early }
2114 ADD_TEST (read, 2, 0),
2115 ADD_TEST (readv, 2, 0),
2116 ADD_TEST (select, 2, 0),
2117 ADD_TEST (pselect, 2, 0),
2118 ADD_TEST (poll, 2, 0),
7c65e900 2119 ADD_TEST (ppoll, 2, 0),
26676450
UD
2120 ADD_TEST (write, 2, 0),
2121 ADD_TEST (writev, 2, 0),
2122 ADD_TEST (sleep, 2, 0),
2123 ADD_TEST (usleep, 2, 0),
2124 ADD_TEST (nanosleep, 2, 0),
2125 ADD_TEST (wait, 2, 0),
2126 ADD_TEST (waitid, 2, 0),
2127 ADD_TEST (waitpid, 2, 0),
2128 ADD_TEST (sigpause, 2, 0),
2129 ADD_TEST (sigsuspend, 2, 0),
2130 ADD_TEST (sigwait, 2, 0),
2131 ADD_TEST (sigwaitinfo, 2, 0),
2132 ADD_TEST (sigtimedwait, 2, 0),
2133 ADD_TEST (pause, 2, 0),
2134 ADD_TEST (accept, 2, 0),
2135 ADD_TEST (send, 2, 0),
2136 ADD_TEST (recv, 2, 0),
2137 ADD_TEST (recvfrom, 2, 0),
2138 ADD_TEST (recvmsg, 2, 0),
047aec8f
UD
2139 ADD_TEST (open, 2, 1),
2140 ADD_TEST (close, 2, 1),
2141 ADD_TEST (pread, 2, 1),
2142 ADD_TEST (pwrite, 2, 1),
2143 ADD_TEST (fsync, 2, 1),
aa75f64c 2144 ADD_TEST (fdatasync, 2, 1),
047aec8f 2145 ADD_TEST (msync, 2, 1),
f23b30e2
UD
2146 ADD_TEST (sendto, 2, 1),
2147 ADD_TEST (sendmsg, 2, 1),
2148 ADD_TEST (creat, 2, 1),
2149 ADD_TEST (connect, 2, 1),
2150 ADD_TEST (tcdrain, 2, 1),
2151 ADD_TEST (msgrcv, 2, 0),
2152 ADD_TEST (msgsnd, 2, 1),
26676450
UD
2153};
2154#define ntest_tf (sizeof (tests) / sizeof (tests[0]))
2155
2156
2157static int
2158do_test (void)
2159{
8074c5c5
RM
2160 int val;
2161 socklen_t len;
2162
2163 if (socketpair (AF_UNIX, SOCK_STREAM, PF_UNIX, fds) != 0)
2164 {
2165 perror ("socketpair");
2166 exit (1);
2167 }
2168
2169 val = 1;
2170 len = sizeof(val);
2171 setsockopt (fds[1], SOL_SOCKET, SO_SNDBUF, &val, sizeof(val));
2172 if (getsockopt (fds[1], SOL_SOCKET, SO_SNDBUF, &val, &len) < 0)
2173 {
2174 perror ("getsockopt");
2175 exit (1);
2176 }
2177 if (val >= WRITE_BUFFER_SIZE)
26676450 2178 {
8074c5c5 2179 puts ("minimum write buffer size too large");
26676450
UD
2180 exit (1);
2181 }
8074c5c5 2182 setsockopt (fds[1], SOL_SOCKET, SO_SNDBUF, &val, sizeof(val));
26676450
UD
2183
2184 int result = 0;
2185 size_t cnt;
2186 for (cnt = 0; cnt < ntest_tf; ++cnt)
2187 {
2188 if (tests[cnt].only_early)
2189 continue;
2190
2191 if (pthread_barrier_init (&b2, NULL, tests[cnt].nb) != 0)
2192 {
2193 puts ("b2 init failed");
2194 exit (1);
2195 }
2196
2197 /* Reset the counter for the cleanup handler. */
2198 cl_called = 0;
2199
2200 pthread_t th;
2201 if (pthread_create (&th, NULL, tests[cnt].tf, NULL) != 0)
2202 {
2203 printf ("create for '%s' test failed\n", tests[cnt].name);
2204 result = 1;
2205 continue;
2206 }
2207
2208 int r = pthread_barrier_wait (&b2);
2209 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
2210 {
2211 printf ("%s: barrier_wait failed\n", __FUNCTION__);
2212 result = 1;
2213 continue;
2214 }
2215
2216 struct timespec ts = { .tv_sec = 0, .tv_nsec = 100000000 };
2217 while (nanosleep (&ts, &ts) != 0)
2218 continue;
2219
2220 if (pthread_cancel (th) != 0)
2221 {
2222 printf ("cancel for '%s' failed\n", tests[cnt].name);
2223 result = 1;
2224 continue;
2225 }
2226
2227 void *status;
2228 if (pthread_join (th, &status) != 0)
2229 {
2230 printf ("join for '%s' failed\n", tests[cnt].name);
2231 result = 1;
2232 continue;
2233 }
2234 if (status != PTHREAD_CANCELED)
2235 {
2236 printf ("thread for '%s' not canceled\n", tests[cnt].name);
2237 result = 1;
2238 continue;
2239 }
2240
2241 if (pthread_barrier_destroy (&b2) != 0)
2242 {
2243 puts ("barrier_destroy failed");
2244 result = 1;
2245 continue;
2246 }
2247
2248 if (cl_called == 0)
2249 {
2250 printf ("cleanup handler not called for '%s'\n", tests[cnt].name);
2251 result = 1;
2252 continue;
2253 }
2254 if (cl_called > 1)
2255 {
2256 printf ("cleanup handler called more than once for '%s'\n",
2257 tests[cnt].name);
2258 result = 1;
2259 continue;
2260 }
2261
2262 printf ("in-time cancel test of '%s' successful\n", tests[cnt].name);
2263
2264 if (tempfd != -1)
2265 {
2266 close (tempfd);
2267 tempfd = -1;
2268 }
2269 if (tempfd2 != -1)
2270 {
2271 close (tempfd2);
2272 tempfd2 = -1;
2273 }
bb606fbe
UD
2274 if (tempfname != NULL)
2275 {
2276 unlink (tempfname);
2277 free (tempfname);
2278 tempfname = NULL;
2279 }
2280 if (tempmsg != -1)
2281 {
2282 msgctl (tempmsg, IPC_RMID, NULL);
2283 tempmsg = -1;
2284 }
26676450
UD
2285 }
2286
2287 for (cnt = 0; cnt < ntest_tf; ++cnt)
2288 {
2289 if (pthread_barrier_init (&b2, NULL, tests[cnt].nb) != 0)
2290 {
2291 puts ("b2 init failed");
2292 exit (1);
2293 }
2294
2295 /* Reset the counter for the cleanup handler. */
2296 cl_called = 0;
2297
2298 pthread_t th;
2299 if (pthread_create (&th, NULL, tests[cnt].tf, (void *) 1l) != 0)
2300 {
2301 printf ("create for '%s' test failed\n", tests[cnt].name);
2302 result = 1;
2303 continue;
2304 }
2305
2306 int r = pthread_barrier_wait (&b2);
2307 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
2308 {
2309 printf ("%s: barrier_wait failed\n", __FUNCTION__);
2310 result = 1;
2311 continue;
2312 }
2313
2314 if (pthread_cancel (th) != 0)
2315 {
2316 printf ("cancel for '%s' failed\n", tests[cnt].name);
2317 result = 1;
2318 continue;
2319 }
2320
2321 r = pthread_barrier_wait (&b2);
2322 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
2323 {
2324 printf ("%s: barrier_wait failed\n", __FUNCTION__);
2325 result = 1;
2326 continue;
2327 }
2328
2329 void *status;
2330 if (pthread_join (th, &status) != 0)
2331 {
2332 printf ("join for '%s' failed\n", tests[cnt].name);
0e0deb03
UD
2333 result = 1;
2334 continue;
76a50749
UD
2335 }
2336 if (status != PTHREAD_CANCELED)
2337 {
0e0deb03
UD
2338 printf ("thread for '%s' not canceled\n", tests[cnt].name);
2339 result = 1;
2340 continue;
76a50749 2341 }
76a50749
UD
2342
2343 if (pthread_barrier_destroy (&b2) != 0)
2344 {
2345 puts ("barrier_destroy failed");
0e0deb03
UD
2346 result = 1;
2347 continue;
76a50749 2348 }
0e0deb03
UD
2349
2350 if (cl_called == 0)
2351 {
2352 printf ("cleanup handler not called for '%s'\n", tests[cnt].name);
2353 result = 1;
2354 continue;
2355 }
2356 if (cl_called > 1)
2357 {
2358 printf ("cleanup handler called more than once for '%s'\n",
2359 tests[cnt].name);
2360 result = 1;
2361 continue;
2362 }
2363
26676450
UD
2364 printf ("early cancel test of '%s' successful\n", tests[cnt].name);
2365
2366 if (tempfd != -1)
2367 {
2368 close (tempfd);
2369 tempfd = -1;
2370 }
2371 if (tempfd2 != -1)
2372 {
2373 close (tempfd2);
2374 tempfd2 = -1;
2375 }
f23b30e2
UD
2376 if (tempfname != NULL)
2377 {
2378 unlink (tempfname);
2379 free (tempfname);
2380 tempfname = NULL;
2381 }
2382 if (tempmsg != -1)
2383 {
2384 msgctl (tempmsg, IPC_RMID, NULL);
2385 tempmsg = -1;
2386 }
76a50749
UD
2387 }
2388
0e0deb03 2389 return result;
76a50749
UD
2390}
2391
2392#define TIMEOUT 60
2393#define TEST_FUNCTION do_test ()
2394#include "../test-skeleton.c"