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