]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysdeps/pthread/tst-cancel4.c
Update copyright dates with scripts/update-copyrights
[thirdparty/glibc.git] / sysdeps / pthread / tst-cancel4.c
CommitLineData
2b778ceb 1/* Copyright (C) 2002-2021 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 16 License along with the GNU C Library; if not, see
5a82c748 17 <https://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>
9fe3c80c 41#include <sys/uio.h>
02802faf 42#include <libc-diag.h>
76a50749 43
26676450 44
f23b30e2
UD
45/* Since STREAMS are not supported in the standard Linux kernel and
46 there we don't advertise STREAMS as supported is no need to test
47 the STREAMS related functions. This affects
26676450
UD
48 getmsg() getpmsg() putmsg()
49 putpmsg()
50
51 lockf() and fcntl() are tested in tst-cancel16.
52
53 pthread_join() is tested in tst-join5.
54
55 pthread_testcancel()'s only purpose is to allow cancellation. This
56 is tested in several places.
57
58 sem_wait() and sem_timedwait() are checked in tst-cancel1[2345] tests.
59
1683daeb
UD
60 mq_send(), mq_timedsend(), mq_receive() and mq_timedreceive() are checked
61 in tst-mqueue8{,x} tests.
f23b30e2
UD
62
63 aio_suspend() is tested in tst-cancel17.
64
65 clock_nanosleep() is tested in tst-cancel18.
76a50749 66
b39b6e0c
AZ
67 Linux sendmmsg and recvmmsg are checked in tst-cancel4_1.c and
68 tst-cancel4_2.c respectively.
69*/
76a50749 70
b39b6e0c 71#include "tst-cancel4-common.h"
76a50749 72
2918b0d0 73
483e95d0
UD
74#ifndef IPC_ADDVAL
75# define IPC_ADDVAL 0
76#endif
77
483e95d0 78
76a50749
UD
79static void *
80tf_read (void *arg)
81{
26676450 82 int fd;
26676450
UD
83
84 if (arg == NULL)
85 fd = fds[0];
86 else
87 {
88 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
89 tempfd = fd = mkstemp (fname);
90 if (fd == -1)
fa66f341 91 FAIL_EXIT1 ("mkstemp failed: %m");
26676450
UD
92 unlink (fname);
93
fa66f341 94 xpthread_barrier_wait (&b2);
26676450
UD
95 }
96
fa66f341 97 xpthread_barrier_wait (&b2);
76a50749 98
f23b30e2 99 ssize_t s;
0e0deb03
UD
100 pthread_cleanup_push (cl, NULL);
101
76a50749 102 char buf[100];
f23b30e2 103 s = read (fd, buf, sizeof (buf));
76a50749 104
0e0deb03
UD
105 pthread_cleanup_pop (0);
106
fa66f341 107 FAIL_EXIT1 ("read returns with %zd", s);
76a50749
UD
108}
109
110
111static void *
112tf_readv (void *arg)
113{
26676450 114 int fd;
26676450
UD
115
116 if (arg == NULL)
117 fd = fds[0];
118 else
119 {
120 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
121 tempfd = fd = mkstemp (fname);
122 if (fd == -1)
fa66f341 123 FAIL_EXIT1 ("mkstemp failed: %m");
26676450
UD
124 unlink (fname);
125
fa66f341 126 xpthread_barrier_wait (&b2);
26676450
UD
127 }
128
fa66f341 129 xpthread_barrier_wait (&b2);
76a50749 130
f23b30e2 131 ssize_t s;
0e0deb03
UD
132 pthread_cleanup_push (cl, NULL);
133
76a50749
UD
134 char buf[100];
135 struct iovec iov[1] = { [0] = { .iov_base = buf, .iov_len = sizeof (buf) } };
f23b30e2 136 s = readv (fd, iov, 1);
76a50749 137
0e0deb03
UD
138 pthread_cleanup_pop (0);
139
fa66f341 140 FAIL_EXIT1 ("readv returns with %zd", s);
76a50749
UD
141}
142
143
144static void *
145tf_write (void *arg)
146{
26676450 147 int fd;
26676450
UD
148
149 if (arg == NULL)
150 fd = fds[1];
151 else
152 {
153 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
154 tempfd = fd = mkstemp (fname);
155 if (fd == -1)
fa66f341 156 FAIL_EXIT1 ("mkstemp failed: %m");
26676450
UD
157 unlink (fname);
158
fa66f341 159 xpthread_barrier_wait (&b2);
26676450
UD
160 }
161
fa66f341 162 xpthread_barrier_wait (&b2);
76a50749 163
f23b30e2 164 ssize_t s;
0e0deb03
UD
165 pthread_cleanup_push (cl, NULL);
166
8074c5c5 167 char buf[WRITE_BUFFER_SIZE];
76a50749 168 memset (buf, '\0', sizeof (buf));
f23b30e2 169 s = write (fd, buf, sizeof (buf));
d0d7f85f
AZ
170 /* The write can return a value higher than 0 (meaning partial write)
171 due to the SIGCANCEL, but the thread may still be pending
172 cancellation. */
173 pthread_testcancel ();
76a50749 174
0e0deb03
UD
175 pthread_cleanup_pop (0);
176
fa66f341 177 FAIL_EXIT1 ("write returns with %zd", s);
76a50749
UD
178}
179
180
181static void *
182tf_writev (void *arg)
183{
26676450 184 int fd;
26676450
UD
185
186 if (arg == NULL)
187 fd = fds[1];
188 else
189 {
190 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
191 tempfd = fd = mkstemp (fname);
192 if (fd == -1)
fa66f341 193 FAIL_EXIT1 ("mkstemp failed: %m");
26676450
UD
194 unlink (fname);
195
fa66f341 196 xpthread_barrier_wait (&b2);
26676450
UD
197 }
198
fa66f341 199 xpthread_barrier_wait (&b2);
76a50749 200
f23b30e2 201 ssize_t s;
0e0deb03
UD
202 pthread_cleanup_push (cl, NULL);
203
8074c5c5 204 char buf[WRITE_BUFFER_SIZE];
76a50749
UD
205 memset (buf, '\0', sizeof (buf));
206 struct iovec iov[1] = { [0] = { .iov_base = buf, .iov_len = sizeof (buf) } };
f23b30e2 207 s = writev (fd, iov, 1);
76a50749 208
0e0deb03
UD
209 pthread_cleanup_pop (0);
210
fa66f341 211 FAIL_EXIT1 ("writev returns with %zd", s);
76a50749
UD
212}
213
214
215static void *
216tf_sleep (void *arg)
217{
fa66f341 218 xpthread_barrier_wait (&b2);
76a50749 219
26676450 220 if (arg != NULL)
fa66f341 221 xpthread_barrier_wait (&b2);
26676450 222
0e0deb03
UD
223 pthread_cleanup_push (cl, NULL);
224
26676450 225 sleep (arg == NULL ? 1000000 : 0);
76a50749 226
0e0deb03
UD
227 pthread_cleanup_pop (0);
228
fa66f341 229 FAIL_EXIT1 ("sleep returns");
76a50749
UD
230}
231
232
233static void *
234tf_usleep (void *arg)
235{
fa66f341 236 xpthread_barrier_wait (&b2);
76a50749 237
26676450 238 if (arg != NULL)
fa66f341 239 xpthread_barrier_wait (&b2);
26676450 240
0e0deb03
UD
241 pthread_cleanup_push (cl, NULL);
242
26676450 243 usleep (arg == NULL ? (useconds_t) ULONG_MAX : 0);
76a50749 244
0e0deb03
UD
245 pthread_cleanup_pop (0);
246
fa66f341 247 FAIL_EXIT1 ("usleep returns");
76a50749
UD
248}
249
250
251static void *
252tf_nanosleep (void *arg)
253{
fa66f341 254 xpthread_barrier_wait (&b2);
76a50749 255
26676450 256 if (arg != NULL)
fa66f341 257 xpthread_barrier_wait (&b2);
26676450 258
0e0deb03
UD
259 pthread_cleanup_push (cl, NULL);
260
26676450 261 struct timespec ts = { .tv_sec = arg == NULL ? 10000000 : 0, .tv_nsec = 0 };
f23b30e2 262 TEMP_FAILURE_RETRY (nanosleep (&ts, &ts));
76a50749 263
0e0deb03
UD
264 pthread_cleanup_pop (0);
265
fa66f341 266 FAIL_EXIT1 ("nanosleep returns");
76a50749
UD
267}
268
269
270static void *
271tf_select (void *arg)
272{
26676450 273 int fd;
26676450
UD
274
275 if (arg == NULL)
276 fd = fds[0];
277 else
278 {
279 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
280 tempfd = fd = mkstemp (fname);
281 if (fd == -1)
fa66f341 282 FAIL_EXIT1 ("mkstemp failed: %m");
26676450
UD
283 unlink (fname);
284
fa66f341 285 xpthread_barrier_wait (&b2);
26676450
UD
286 }
287
fa66f341 288 xpthread_barrier_wait (&b2);
76a50749
UD
289
290 fd_set rfs;
291 FD_ZERO (&rfs);
26676450 292 FD_SET (fd, &rfs);
76a50749 293
f23b30e2 294 int s;
0e0deb03
UD
295 pthread_cleanup_push (cl, NULL);
296
f23b30e2
UD
297 s = select (fd + 1, &rfs, NULL, NULL, NULL);
298
299 pthread_cleanup_pop (0);
76a50749 300
fa66f341 301 FAIL_EXIT1 ("select returns with %d: %m", s);
76a50749
UD
302}
303
304
305static void *
306tf_pselect (void *arg)
307{
26676450 308 int fd;
26676450
UD
309
310 if (arg == NULL)
311 fd = fds[0];
312 else
313 {
314 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
315 tempfd = fd = mkstemp (fname);
316 if (fd == -1)
fa66f341 317 FAIL_EXIT1 ("mkstemp failed: %m");
26676450
UD
318 unlink (fname);
319
fa66f341 320 xpthread_barrier_wait (&b2);
26676450
UD
321 }
322
fa66f341 323 xpthread_barrier_wait (&b2);
76a50749
UD
324
325 fd_set rfs;
326 FD_ZERO (&rfs);
26676450 327 FD_SET (fd, &rfs);
76a50749 328
f23b30e2 329 int s;
0e0deb03
UD
330 pthread_cleanup_push (cl, NULL);
331
f23b30e2
UD
332 s = pselect (fd + 1, &rfs, NULL, NULL, NULL, NULL);
333
334 pthread_cleanup_pop (0);
76a50749 335
fa66f341 336 FAIL_EXIT1 ("pselect returns with %d: %m", s);
76a50749
UD
337}
338
339
340static void *
341tf_poll (void *arg)
342{
26676450 343 int fd;
26676450
UD
344
345 if (arg == NULL)
346 fd = fds[0];
347 else
348 {
349 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
350 tempfd = fd = mkstemp (fname);
351 if (fd == -1)
fa66f341 352 FAIL_EXIT1 ("mkstemp failed: %m");
26676450
UD
353 unlink (fname);
354
fa66f341 355 xpthread_barrier_wait (&b2);
26676450
UD
356 }
357
fa66f341 358 xpthread_barrier_wait (&b2);
76a50749 359
26676450 360 struct pollfd rfs[1] = { [0] = { .fd = fd, .events = POLLIN } };
76a50749 361
f23b30e2 362 int s;
0e0deb03
UD
363 pthread_cleanup_push (cl, NULL);
364
f23b30e2
UD
365 s = poll (rfs, 1, -1);
366
367 pthread_cleanup_pop (0);
76a50749 368
fa66f341 369 FAIL_EXIT1 ("poll returns with %d: %m", s);
76a50749
UD
370}
371
372
7c65e900
UD
373static void *
374tf_ppoll (void *arg)
375{
376 int fd;
7c65e900
UD
377
378 if (arg == NULL)
379 fd = fds[0];
380 else
381 {
382 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
383 tempfd = fd = mkstemp (fname);
384 if (fd == -1)
fa66f341 385 FAIL_EXIT1 ("mkstemp failed: %m");
7c65e900
UD
386 unlink (fname);
387
fa66f341 388 xpthread_barrier_wait (&b2);
7c65e900
UD
389 }
390
fa66f341 391 xpthread_barrier_wait (&b2);
7c65e900
UD
392
393 struct pollfd rfs[1] = { [0] = { .fd = fd, .events = POLLIN } };
394
395 int s;
396 pthread_cleanup_push (cl, NULL);
397
398 s = ppoll (rfs, 1, NULL, NULL);
399
400 pthread_cleanup_pop (0);
401
fa66f341 402 FAIL_EXIT1 ("ppoll returns with %d: %m", s);
7c65e900
UD
403}
404
405
76a50749
UD
406static void *
407tf_wait (void *arg)
408{
76a50749
UD
409 pid_t pid = fork ();
410 if (pid == -1)
fa66f341 411 FAIL_EXIT1 ("fork: %m");
76a50749
UD
412
413 if (pid == 0)
414 {
415 /* Make the program disappear after a while. */
26676450
UD
416 if (arg == NULL)
417 sleep (10);
76a50749
UD
418 exit (0);
419 }
420
26676450
UD
421 if (arg != NULL)
422 {
423 struct timespec ts = { .tv_sec = 0, .tv_nsec = 100000000 };
424 while (nanosleep (&ts, &ts) != 0)
425 continue;
426
fa66f341 427 xpthread_barrier_wait (&b2);
26676450
UD
428 }
429
fa66f341 430 xpthread_barrier_wait (&b2);
26676450 431
f23b30e2 432 int s;
0e0deb03
UD
433 pthread_cleanup_push (cl, NULL);
434
f23b30e2
UD
435 s = wait (NULL);
436
437 pthread_cleanup_pop (0);
76a50749 438
fa66f341 439 FAIL_EXIT1 ("wait returns with %d: %m", s);
76a50749
UD
440}
441
442
443static void *
444tf_waitpid (void *arg)
445{
76a50749
UD
446 pid_t pid = fork ();
447 if (pid == -1)
fa66f341 448 FAIL_EXIT1 ("fork: %m");
76a50749
UD
449
450 if (pid == 0)
451 {
452 /* Make the program disappear after a while. */
26676450
UD
453 if (arg == NULL)
454 sleep (10);
76a50749
UD
455 exit (0);
456 }
457
26676450
UD
458 if (arg != NULL)
459 {
460 struct timespec ts = { .tv_sec = 0, .tv_nsec = 100000000 };
461 while (nanosleep (&ts, &ts) != 0)
462 continue;
463
fa66f341 464 xpthread_barrier_wait (&b2);
26676450
UD
465 }
466
fa66f341 467 xpthread_barrier_wait (&b2);
26676450 468
f23b30e2 469 int s;
fa66f341 470 pthread_cleanup_push (cl, NULL);
0e0deb03 471
f23b30e2
UD
472 s = waitpid (-1, NULL, 0);
473
474 pthread_cleanup_pop (0);
76a50749 475
fa66f341 476 FAIL_EXIT1 ("waitpid returns with %d: %m", s);
76a50749
UD
477}
478
479
480static void *
481tf_waitid (void *arg)
482{
76a50749
UD
483 pid_t pid = fork ();
484 if (pid == -1)
fa66f341 485 FAIL_EXIT1 ("fork: %m");
76a50749
UD
486
487 if (pid == 0)
488 {
489 /* Make the program disappear after a while. */
26676450
UD
490 if (arg == NULL)
491 sleep (10);
76a50749
UD
492 exit (0);
493 }
494
26676450
UD
495 if (arg != NULL)
496 {
497 struct timespec ts = { .tv_sec = 0, .tv_nsec = 100000000 };
498 while (nanosleep (&ts, &ts) != 0)
499 continue;
500
fa66f341 501 xpthread_barrier_wait (&b2);
26676450
UD
502 }
503
fa66f341 504 xpthread_barrier_wait (&b2);
26676450 505
f23b30e2 506 int s;
0e0deb03
UD
507 pthread_cleanup_push (cl, NULL);
508
4fb907b7
RM
509#ifndef WEXITED
510# define WEXITED 0
511#endif
76a50749 512 siginfo_t si;
4fb907b7 513 s = waitid (P_PID, pid, &si, WEXITED);
f23b30e2
UD
514
515 pthread_cleanup_pop (0);
76a50749 516
fa66f341 517 FAIL_EXIT1 ("waitid returns with %d: %m", s);
76a50749
UD
518}
519
520
26676450
UD
521static void *
522tf_sigpause (void *arg)
76a50749 523{
fa66f341 524 xpthread_barrier_wait (&b2);
76a50749 525
26676450 526 if (arg != NULL)
fa66f341 527 xpthread_barrier_wait (&b2);
76a50749 528
26676450 529 pthread_cleanup_push (cl, NULL);
0e0deb03 530
02802faf
FW
531 /* This tests the deprecated sigpause and sigmask functions. The
532 file is compiled with -Wno-errno so that the sigmask deprecation
533 warning is not fatal. */
534 DIAG_PUSH_NEEDS_COMMENT;
535 DIAG_IGNORE_NEEDS_COMMENT (4.9, "-Wdeprecated-declarations");
2918b0d0 536 sigpause (sigmask (SIGINT));
02802faf 537 DIAG_POP_NEEDS_COMMENT;
76a50749 538
26676450
UD
539 pthread_cleanup_pop (0);
540
fa66f341 541 FAIL_EXIT1 ("sigpause returned");
26676450
UD
542}
543
544
545static void *
546tf_sigsuspend (void *arg)
547{
fa66f341 548 xpthread_barrier_wait (&b2);
26676450
UD
549
550 if (arg != NULL)
fa66f341 551 xpthread_barrier_wait (&b2);
76a50749 552
26676450 553 pthread_cleanup_push (cl, NULL);
76a50749 554
26676450
UD
555 /* Just for fun block all signals. */
556 sigset_t mask;
557 sigfillset (&mask);
558 sigsuspend (&mask);
76a50749 559
26676450
UD
560 pthread_cleanup_pop (0);
561
fa66f341 562 FAIL_EXIT1 ("sigsuspend returned");
26676450
UD
563}
564
565
566static void *
567tf_sigwait (void *arg)
568{
fa66f341 569 xpthread_barrier_wait (&b2);
26676450
UD
570
571 if (arg != NULL)
fa66f341 572 xpthread_barrier_wait (&b2);
26676450 573
26676450
UD
574 /* Block SIGUSR1. */
575 sigset_t mask;
97b6614c 576 sigemptyset (&mask);
26676450 577 sigaddset (&mask, SIGUSR1);
fa66f341 578 TEST_VERIFY_EXIT (pthread_sigmask (SIG_BLOCK, &mask, NULL) == 0);
26676450 579
26676450 580 int sig;
f23b30e2 581 pthread_cleanup_push (cl, NULL);
26676450 582
f23b30e2
UD
583 /* Wait for SIGUSR1. */
584 sigwait (&mask, &sig);
26676450
UD
585
586 pthread_cleanup_pop (0);
587
fa66f341 588 FAIL_EXIT1 ("sigwait returned with signal %d", sig);
26676450
UD
589}
590
591
592static void *
593tf_sigwaitinfo (void *arg)
594{
fa66f341 595 xpthread_barrier_wait (&b2);
26676450
UD
596
597 if (arg != NULL)
fa66f341 598 xpthread_barrier_wait (&b2);
26676450 599
26676450
UD
600 /* Block SIGUSR1. */
601 sigset_t mask;
97b6614c 602 sigemptyset (&mask);
26676450 603 sigaddset (&mask, SIGUSR1);
fa66f341 604 TEST_VERIFY_EXIT (pthread_sigmask (SIG_BLOCK, &mask, NULL) == 0);
26676450 605
26676450 606 siginfo_t info;
f23b30e2
UD
607 pthread_cleanup_push (cl, NULL);
608
609 /* Wait for SIGUSR1. */
f9cf8735
ST
610 int ret;
611 ret = sigwaitinfo (&mask, &info);
612 if (ret == -1 && errno == ENOSYS)
613 {
614 int sig;
615
616 printf ("sigwaitinfo not supported\n");
617 sigwait (&mask, &sig);
618 }
26676450 619
f23b30e2
UD
620 pthread_cleanup_pop (0);
621
fa66f341 622 FAIL_EXIT1 ("sigwaitinfo returned with signal %d", info.si_signo);
26676450
UD
623}
624
625
626static void *
627tf_sigtimedwait (void *arg)
628{
fa66f341 629 xpthread_barrier_wait (&b2);
26676450
UD
630
631 if (arg != NULL)
fa66f341 632 xpthread_barrier_wait (&b2);
26676450 633
26676450
UD
634 /* Block SIGUSR1. */
635 sigset_t mask;
97b6614c 636 sigemptyset (&mask);
26676450 637 sigaddset (&mask, SIGUSR1);
fa66f341 638 TEST_VERIFY_EXIT (pthread_sigmask (SIG_BLOCK, &mask, NULL) == 0);
26676450
UD
639
640 /* Wait for SIGUSR1. */
641 siginfo_t info;
642 struct timespec ts = { .tv_sec = 60, .tv_nsec = 0 };
f23b30e2
UD
643 pthread_cleanup_push (cl, NULL);
644
f9cf8735
ST
645 int ret;
646 ret = sigtimedwait (&mask, &info, &ts);
647 if (ret == -1 && errno == ENOSYS)
648 {
649 int sig;
650 printf ("sigtimedwait not supported\n");
651
652 sigwait (&mask, &sig);
653 }
26676450 654
f23b30e2
UD
655 pthread_cleanup_pop (0);
656
fa66f341 657 FAIL_EXIT1 ("sigtimedwait returned with signal %d", info.si_signo);
26676450
UD
658}
659
660
661static void *
662tf_pause (void *arg)
663{
fa66f341 664 xpthread_barrier_wait (&b2);
26676450
UD
665
666 if (arg != NULL)
fa66f341 667 xpthread_barrier_wait (&b2);
26676450
UD
668
669 pthread_cleanup_push (cl, NULL);
670
671 pause ();
672
673 pthread_cleanup_pop (0);
674
fa66f341 675 FAIL_EXIT1 ("pause returned");
26676450
UD
676}
677
678
679static void *
680tf_accept (void *arg)
681{
682 struct sockaddr_un sun;
683 /* To test a non-blocking accept call we make the call file by using
684 a datagrame socket. */
685 int pf = arg == NULL ? SOCK_STREAM : SOCK_DGRAM;
686
687 tempfd = socket (AF_UNIX, pf, 0);
688 if (tempfd == -1)
fa66f341
AZ
689 FAIL_EXIT1 ("socket (AF_UNIX, %s, 0): %m", arg == NULL ? "SOCK_STREAM"
690 : "SOCK_DGRAM");
26676450
UD
691
692 int tries = 0;
693 do
694 {
fa66f341 695 TEST_VERIFY_EXIT (tries++ < 10);
26676450 696
f23b30e2 697 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-1-XXXXXX");
fa66f341 698 TEST_VERIFY_EXIT (mktemp (sun.sun_path) != NULL);
26676450
UD
699
700 sun.sun_family = AF_UNIX;
701 }
702 while (bind (tempfd, (struct sockaddr *) &sun,
703 offsetof (struct sockaddr_un, sun_path)
704 + strlen (sun.sun_path) + 1) != 0);
705
706 unlink (sun.sun_path);
707
708 listen (tempfd, 5);
709
710 socklen_t len = sizeof (sun);
711
fa66f341 712 xpthread_barrier_wait (&b2);
26676450
UD
713
714 if (arg != NULL)
fa66f341 715 xpthread_barrier_wait (&b2);
26676450
UD
716
717 pthread_cleanup_push (cl, NULL);
718
719 accept (tempfd, (struct sockaddr *) &sun, &len);
720
721 pthread_cleanup_pop (0);
722
fa66f341 723 FAIL_EXIT1 ("accept returned");
26676450
UD
724}
725
726
727static void *
728tf_send (void *arg)
729{
730 struct sockaddr_un sun;
731
732 tempfd = socket (AF_UNIX, SOCK_STREAM, 0);
733 if (tempfd == -1)
fa66f341 734 FAIL_EXIT1 ("socket (AF_UNIX, SOCK_STREAM, 0): %m");
26676450
UD
735
736 int tries = 0;
737 do
738 {
fa66f341 739 TEST_VERIFY_EXIT (tries++ < 10);
26676450 740
f23b30e2 741 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-2-XXXXXX");
fa66f341 742 TEST_VERIFY_EXIT (mktemp (sun.sun_path) != NULL);
26676450
UD
743
744 sun.sun_family = AF_UNIX;
745 }
746 while (bind (tempfd, (struct sockaddr *) &sun,
747 offsetof (struct sockaddr_un, sun_path)
748 + strlen (sun.sun_path) + 1) != 0);
749
750 listen (tempfd, 5);
751
752 tempfd2 = socket (AF_UNIX, SOCK_STREAM, 0);
753 if (tempfd2 == -1)
fa66f341 754 FAIL_EXIT1 ("socket (AF_UNIX, SOCK_STREAM, 0): %m");
26676450
UD
755
756 if (connect (tempfd2, (struct sockaddr *) &sun, sizeof (sun)) != 0)
fa66f341 757 FAIL_EXIT1 ("connect: %m");
26676450
UD
758
759 unlink (sun.sun_path);
760
b71bc845
ST
761 set_socket_buffer (tempfd2);
762
fa66f341 763 xpthread_barrier_wait (&b2);
26676450
UD
764
765 if (arg != NULL)
fa66f341 766 xpthread_barrier_wait (&b2);
26676450
UD
767
768 pthread_cleanup_push (cl, NULL);
769
fbd01e6c 770 char mem[WRITE_BUFFER_SIZE];
26676450 771
ac54c335 772 size_t s = send (tempfd2, mem, arg == NULL ? sizeof (mem) : 1, 0);
d0d7f85f
AZ
773 /* The send can return a value higher than 0 (meaning partial send)
774 due to the SIGCANCEL, but the thread may still be pending
775 cancellation. */
776 pthread_testcancel ();
ac54c335 777 printf("send returned %zd\n", s);
26676450
UD
778
779 pthread_cleanup_pop (0);
780
fa66f341 781 FAIL_EXIT1 ("send returned");
26676450
UD
782}
783
784
785static void *
786tf_recv (void *arg)
787{
788 struct sockaddr_un sun;
789
790 tempfd = socket (AF_UNIX, SOCK_STREAM, 0);
791 if (tempfd == -1)
fa66f341 792 FAIL_EXIT1 ("socket (AF_UNIX, SOCK_STREAM, 0): %m");
26676450
UD
793
794 int tries = 0;
795 do
796 {
fa66f341 797 TEST_VERIFY_EXIT (tries++ < 10);
26676450 798
f23b30e2 799 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-3-XXXXXX");
fa66f341 800 TEST_VERIFY_EXIT (mktemp (sun.sun_path) != NULL);
26676450
UD
801
802 sun.sun_family = AF_UNIX;
803 }
804 while (bind (tempfd, (struct sockaddr *) &sun,
805 offsetof (struct sockaddr_un, sun_path)
806 + strlen (sun.sun_path) + 1) != 0);
807
808 listen (tempfd, 5);
809
810 tempfd2 = socket (AF_UNIX, SOCK_STREAM, 0);
811 if (tempfd2 == -1)
fa66f341 812 FAIL_EXIT1 ("socket (AF_UNIX, SOCK_STREAM, 0): %m");
26676450
UD
813
814 if (connect (tempfd2, (struct sockaddr *) &sun, sizeof (sun)) != 0)
fa66f341 815 FAIL_EXIT1 ("connect: %m");
26676450
UD
816
817 unlink (sun.sun_path);
818
fa66f341 819 xpthread_barrier_wait (&b2);
26676450
UD
820
821 if (arg != NULL)
fa66f341 822 xpthread_barrier_wait (&b2);
26676450
UD
823
824 pthread_cleanup_push (cl, NULL);
825
826 char mem[70];
827
828 recv (tempfd2, mem, arg == NULL ? sizeof (mem) : 0, 0);
829
830 pthread_cleanup_pop (0);
831
fa66f341 832 FAIL_EXIT1 ("recv returned");
26676450
UD
833}
834
835
836static void *
837tf_recvfrom (void *arg)
838{
839 struct sockaddr_un sun;
840
841 tempfd = socket (AF_UNIX, SOCK_DGRAM, 0);
842 if (tempfd == -1)
fa66f341 843 FAIL_EXIT1 ("socket (AF_UNIX, SOCK_DGRAM, 0): %m");
26676450
UD
844
845 int tries = 0;
846 do
847 {
fa66f341 848 TEST_VERIFY_EXIT (tries++ < 10);
26676450 849
f23b30e2 850 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-4-XXXXXX");
fa66f341 851 TEST_VERIFY_EXIT (mktemp (sun.sun_path) != NULL);
26676450
UD
852
853 sun.sun_family = AF_UNIX;
854 }
855 while (bind (tempfd, (struct sockaddr *) &sun,
856 offsetof (struct sockaddr_un, sun_path)
857 + strlen (sun.sun_path) + 1) != 0);
858
859 tempfname = strdup (sun.sun_path);
860
861 tempfd2 = socket (AF_UNIX, SOCK_DGRAM, 0);
862 if (tempfd2 == -1)
fa66f341 863 FAIL_EXIT1 ("socket (AF_UNIX, SOCK_DGRAM, 0): %m");
26676450 864
fa66f341 865 xpthread_barrier_wait (&b2);
26676450
UD
866
867 if (arg != NULL)
fa66f341 868 xpthread_barrier_wait (&b2);
26676450
UD
869
870 pthread_cleanup_push (cl, NULL);
871
872 char mem[70];
873 socklen_t len = sizeof (sun);
874
875 recvfrom (tempfd2, mem, arg == NULL ? sizeof (mem) : 0, 0,
876 (struct sockaddr *) &sun, &len);
877
878 pthread_cleanup_pop (0);
879
fa66f341 880 FAIL_EXIT1 ("recvfrom returned");
26676450
UD
881}
882
883
884static void *
885tf_recvmsg (void *arg)
886{
887 struct sockaddr_un sun;
888
889 tempfd = socket (AF_UNIX, SOCK_DGRAM, 0);
890 if (tempfd == -1)
fa66f341 891 FAIL_EXIT1 ("socket (AF_UNIX, SOCK_DGRAM, 0): %m");
26676450
UD
892
893 int tries = 0;
894 do
895 {
fa66f341 896 TEST_VERIFY_EXIT (tries++ < 10);
26676450 897
f23b30e2 898 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-5-XXXXXX");
fa66f341 899 TEST_VERIFY_EXIT (mktemp (sun.sun_path) != NULL);
26676450
UD
900
901 sun.sun_family = AF_UNIX;
902 }
903 while (bind (tempfd, (struct sockaddr *) &sun,
904 offsetof (struct sockaddr_un, sun_path)
905 + strlen (sun.sun_path) + 1) != 0);
906
907 tempfname = strdup (sun.sun_path);
908
909 tempfd2 = socket (AF_UNIX, SOCK_DGRAM, 0);
910 if (tempfd2 == -1)
fa66f341 911 FAIL_EXIT1 ("socket (AF_UNIX, SOCK_DGRAM, 0): %m");
26676450 912
fa66f341 913 xpthread_barrier_wait (&b2);
26676450
UD
914
915 if (arg != NULL)
fa66f341 916 xpthread_barrier_wait (&b2);
26676450
UD
917
918 pthread_cleanup_push (cl, NULL);
919
920 char mem[70];
921 struct iovec iov[1];
922 iov[0].iov_base = mem;
923 iov[0].iov_len = arg == NULL ? sizeof (mem) : 0;
924
925 struct msghdr m;
926 m.msg_name = &sun;
927 m.msg_namelen = sizeof (sun);
928 m.msg_iov = iov;
929 m.msg_iovlen = 1;
930 m.msg_control = NULL;
931 m.msg_controllen = 0;
932
933 recvmsg (tempfd2, &m, 0);
934
935 pthread_cleanup_pop (0);
936
fa66f341 937 FAIL_EXIT1 ("recvmsg returned");
26676450
UD
938}
939
047aec8f
UD
940static void *
941tf_open (void *arg)
942{
943 if (arg == NULL)
047aec8f 944 {
2918b0d0
AZ
945 fifofd = mkfifo (fifoname, S_IWUSR | S_IRUSR);
946 if (fifofd == -1)
fa66f341 947 FAIL_EXIT1 ("mkfifo: %m");
2918b0d0
AZ
948 }
949 else
950 {
fa66f341 951 xpthread_barrier_wait (&b2);
047aec8f
UD
952 }
953
fa66f341 954 xpthread_barrier_wait (&b2);
047aec8f 955
2918b0d0 956 pthread_cleanup_push (cl_fifo, NULL);
047aec8f 957
2918b0d0 958 open (arg ? "Makefile" : fifoname, O_RDONLY);
047aec8f
UD
959
960 pthread_cleanup_pop (0);
961
fa66f341 962 FAIL_EXIT1 ("open returned");
047aec8f
UD
963}
964
965
966static void *
967tf_close (void *arg)
968{
969 if (arg == NULL)
970 // XXX If somebody can provide a portable test case in which close()
971 // blocks we can enable this test to run in both rounds.
972 abort ();
973
974 char fname[] = "/tmp/tst-cancel-fd-XXXXXX";
975 tempfd = mkstemp (fname);
976 if (tempfd == -1)
fa66f341 977 FAIL_EXIT1 ("mkstemp failed: %m");
047aec8f
UD
978 unlink (fname);
979
fa66f341 980 xpthread_barrier_wait (&b2);
047aec8f 981
fa66f341 982 xpthread_barrier_wait (&b2);
047aec8f
UD
983
984 pthread_cleanup_push (cl, NULL);
985
986 close (tempfd);
987
988 pthread_cleanup_pop (0);
989
fa66f341 990 FAIL_EXIT1 ("close returned");
047aec8f
UD
991}
992
993
994static void *
995tf_pread (void *arg)
996{
997 if (arg == NULL)
998 // XXX If somebody can provide a portable test case in which pread()
999 // blocks we can enable this test to run in both rounds.
1000 abort ();
1001
1002 tempfd = open ("Makefile", O_RDONLY);
1003 if (tempfd == -1)
fa66f341 1004 FAIL_EXIT1 ("open (\"Makefile\", O_RDONLY): %m");
047aec8f 1005
fa66f341 1006 xpthread_barrier_wait (&b2);
047aec8f 1007
fa66f341 1008 xpthread_barrier_wait (&b2);
047aec8f
UD
1009
1010 pthread_cleanup_push (cl, NULL);
1011
1012 char mem[10];
1013 pread (tempfd, mem, sizeof (mem), 0);
1014
1015 pthread_cleanup_pop (0);
1016
fa66f341 1017 FAIL_EXIT1 ("pread returned");
047aec8f
UD
1018}
1019
1020
1021static void *
1022tf_pwrite (void *arg)
1023{
1024 if (arg == NULL)
1025 // XXX If somebody can provide a portable test case in which pwrite()
1026 // blocks we can enable this test to run in both rounds.
1027 abort ();
1028
1029 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
1030 tempfd = mkstemp (fname);
1031 if (tempfd == -1)
fa66f341 1032 FAIL_EXIT1 ("mkstemp failed: %m");
047aec8f
UD
1033 unlink (fname);
1034
fa66f341 1035 xpthread_barrier_wait (&b2);
047aec8f 1036
fa66f341 1037 xpthread_barrier_wait (&b2);
047aec8f
UD
1038
1039 pthread_cleanup_push (cl, NULL);
1040
1041 char mem[10];
1042 pwrite (tempfd, mem, sizeof (mem), 0);
1043
1044 pthread_cleanup_pop (0);
1045
fa66f341 1046 FAIL_EXIT1 ("pwrite returned");
047aec8f
UD
1047}
1048
4e778151
AZ
1049static void *
1050tf_preadv (void *arg)
1051{
1052 int fd;
4e778151
AZ
1053
1054 if (arg == NULL)
1055 /* XXX If somebody can provide a portable test case in which preadv
1056 blocks we can enable this test to run in both rounds. */
1057 abort ();
1058
1059 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
1060 tempfd = fd = mkstemp (fname);
1061 if (fd == -1)
fa66f341 1062 FAIL_EXIT1 ("mkstemp failed: %m");
4e778151
AZ
1063 unlink (fname);
1064
fa66f341 1065 xpthread_barrier_wait (&b2);
4e778151 1066
fa66f341 1067 xpthread_barrier_wait (&b2);
4e778151
AZ
1068
1069 ssize_t s;
1070 pthread_cleanup_push (cl, NULL);
1071
1072 char buf[100];
1073 struct iovec iov[1] = { [0] = { .iov_base = buf, .iov_len = sizeof (buf) } };
1074 s = preadv (fd, iov, 1, 0);
1075
1076 pthread_cleanup_pop (0);
1077
fa66f341 1078 FAIL_EXIT1 ("preadv returns with %zd", s);
4e778151 1079}
047aec8f 1080
af5fdf5a
AZ
1081static void *
1082tf_pwritev (void *arg)
1083{
1084 int fd;
af5fdf5a
AZ
1085
1086 if (arg == NULL)
1087 /* XXX If somebody can provide a portable test case in which pwritev
1088 blocks we can enable this test to run in both rounds. */
1089 abort ();
1090
1091 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
1092 tempfd = fd = mkstemp (fname);
1093 if (fd == -1)
fa66f341 1094 FAIL_EXIT1 ("mkstemp failed: %m");
af5fdf5a
AZ
1095 unlink (fname);
1096
fa66f341 1097 xpthread_barrier_wait (&b2);
af5fdf5a 1098
fa66f341 1099 xpthread_barrier_wait (&b2);
af5fdf5a
AZ
1100
1101 ssize_t s;
1102 pthread_cleanup_push (cl, NULL);
1103
1104 char buf[WRITE_BUFFER_SIZE];
1105 memset (buf, '\0', sizeof (buf));
1106 struct iovec iov[1] = { [0] = { .iov_base = buf, .iov_len = sizeof (buf) } };
1107 s = pwritev (fd, iov, 1, 0);
1108
1109 pthread_cleanup_pop (0);
1110
fa66f341 1111 FAIL_EXIT1 ("pwritev returns with %zd", s);
af5fdf5a
AZ
1112}
1113
52bd9381
AZ
1114static void *
1115tf_pwritev2 (void *arg)
1116{
1117 int fd;
1118
1119 if (arg == NULL)
1120 /* XXX If somebody can provide a portable test case in which pwritev2
1121 blocks we can enable this test to run in both rounds. */
1122 abort ();
1123
1124 errno = 0;
1125
1126 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
1127 tempfd = fd = mkstemp (fname);
1128 if (fd == -1)
1129 FAIL_EXIT1 ("mkstemp: %m");
1130 unlink (fname);
1131
1132 xpthread_barrier_wait (&b2);
1133
1134 xpthread_barrier_wait (&b2);
1135
1136 ssize_t s;
1137 pthread_cleanup_push (cl, NULL);
1138
1139 char buf[WRITE_BUFFER_SIZE];
1140 memset (buf, '\0', sizeof (buf));
1141 struct iovec iov[1] = { [0] = { .iov_base = buf, .iov_len = sizeof (buf) } };
1142 s = pwritev2 (fd, iov, 1, 0, 0);
1143
1144 pthread_cleanup_pop (0);
1145
1146 FAIL_EXIT1 ("pwritev2 returns with %zd", s);
1147}
1148
1149static void *
1150tf_preadv2 (void *arg)
1151{
1152 int fd;
1153
1154 if (arg == NULL)
1155 /* XXX If somebody can provide a portable test case in which preadv2
1156 blocks we can enable this test to run in both rounds. */
1157 abort ();
1158
1159 errno = 0;
1160
1161 char fname[] = "/tmp/tst-cancel4-fd-XXXXXX";
1162 tempfd = fd = mkstemp (fname);
1163 if (fd == -1)
1164 FAIL_EXIT1 ("mkstemp failed: %m");
1165 unlink (fname);
1166
1167 xpthread_barrier_wait (&b2);
1168
1169 xpthread_barrier_wait (&b2);
1170
1171 ssize_t s;
1172 pthread_cleanup_push (cl, NULL);
1173
1174 char buf[100];
1175 struct iovec iov[1] = { [0] = { .iov_base = buf, .iov_len = sizeof (buf) } };
1176 s = preadv2 (fd, iov, 1, 0, 0);
1177
1178 pthread_cleanup_pop (0);
1179
1180 FAIL_EXIT1 ("preadv2 returns with %zd", s);
1181}
1182
047aec8f
UD
1183static void *
1184tf_fsync (void *arg)
1185{
1186 if (arg == NULL)
1187 // XXX If somebody can provide a portable test case in which fsync()
1188 // blocks we can enable this test to run in both rounds.
1189 abort ();
1190
1191 tempfd = open ("Makefile", O_RDONLY);
1192 if (tempfd == -1)
fa66f341 1193 FAIL_EXIT1 ("open (\"Makefile\", O_RDONLY): %m");
047aec8f 1194
fa66f341 1195 xpthread_barrier_wait (&b2);
047aec8f 1196
fa66f341 1197 xpthread_barrier_wait (&b2);
047aec8f
UD
1198
1199 pthread_cleanup_push (cl, NULL);
1200
1201 fsync (tempfd);
1202
1203 pthread_cleanup_pop (0);
1204
fa66f341 1205 FAIL_EXIT1 ("fsync returned");
047aec8f
UD
1206}
1207
1208
aa75f64c
UD
1209static void *
1210tf_fdatasync (void *arg)
1211{
1212 if (arg == NULL)
1213 // XXX If somebody can provide a portable test case in which fdatasync()
1214 // blocks we can enable this test to run in both rounds.
1215 abort ();
1216
1217 tempfd = open ("Makefile", O_RDONLY);
1218 if (tempfd == -1)
fa66f341 1219 FAIL_EXIT1 ("open (\"Makefile\", O_RDONLY): %m");
aa75f64c 1220
fa66f341 1221 xpthread_barrier_wait (&b2);
aa75f64c 1222
fa66f341 1223 xpthread_barrier_wait (&b2);
aa75f64c
UD
1224
1225 pthread_cleanup_push (cl, NULL);
1226
1227 fdatasync (tempfd);
1228
1229 pthread_cleanup_pop (0);
1230
fa66f341 1231 FAIL_EXIT1 ("fdatasync returned");
aa75f64c
UD
1232}
1233
1234
047aec8f
UD
1235static void *
1236tf_msync (void *arg)
1237{
1238 if (arg == NULL)
1239 // XXX If somebody can provide a portable test case in which msync()
1240 // blocks we can enable this test to run in both rounds.
1241 abort ();
1242
1243 tempfd = open ("Makefile", O_RDONLY);
1244 if (tempfd == -1)
fa66f341 1245 FAIL_EXIT1 ("open (\"Makefile\", O_RDONLY): %m");
047aec8f 1246
fa66f341 1247 void *p = xmmap (NULL, 10, PROT_READ, MAP_SHARED, tempfd);
047aec8f 1248
fa66f341
AZ
1249 xpthread_barrier_wait (&b2);
1250
1251 xpthread_barrier_wait (&b2);
047aec8f
UD
1252
1253 pthread_cleanup_push (cl, NULL);
1254
1255 msync (p, 10, 0);
1256
1257 pthread_cleanup_pop (0);
1258
fa66f341 1259 FAIL_EXIT1 ("msync returned");
047aec8f
UD
1260}
1261
1262
f23b30e2
UD
1263static void *
1264tf_sendto (void *arg)
1265{
f23b30e2
UD
1266 struct sockaddr_un sun;
1267
fbd01e6c 1268 tempfd = socket (AF_UNIX, SOCK_STREAM, 0);
f23b30e2 1269 if (tempfd == -1)
fbd01e6c 1270 FAIL_EXIT1 ("socket (AF_UNIX, SOCK_STREAM, 0): %m");
f23b30e2
UD
1271
1272 int tries = 0;
1273 do
1274 {
fa66f341 1275 TEST_VERIFY_EXIT (tries++ < 10);
f23b30e2
UD
1276
1277 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-6-XXXXXX");
fa66f341 1278 TEST_VERIFY_EXIT (mktemp (sun.sun_path) != NULL);
f23b30e2
UD
1279
1280 sun.sun_family = AF_UNIX;
1281 }
1282 while (bind (tempfd, (struct sockaddr *) &sun,
1283 offsetof (struct sockaddr_un, sun_path)
1284 + strlen (sun.sun_path) + 1) != 0);
f23b30e2 1285
fbd01e6c
AZ
1286 listen (tempfd, 5);
1287
1288 tempfd2 = socket (AF_UNIX, SOCK_STREAM, 0);
f23b30e2 1289 if (tempfd2 == -1)
fbd01e6c 1290 FAIL_EXIT1 ("socket (AF_UNIX, SOCK_STREAM, 0): %m");
f23b30e2 1291
fbd01e6c
AZ
1292 if (connect (tempfd2, (struct sockaddr *) &sun, sizeof (sun)) != 0)
1293 FAIL_EXIT1 ("connect: %m");
1294
1295 unlink (sun.sun_path);
f23b30e2 1296
b71bc845
ST
1297 set_socket_buffer (tempfd2);
1298
fa66f341 1299 xpthread_barrier_wait (&b2);
f23b30e2 1300
fbd01e6c
AZ
1301 if (arg != NULL)
1302 xpthread_barrier_wait (&b2);
1303
f23b30e2
UD
1304 pthread_cleanup_push (cl, NULL);
1305
fbd01e6c 1306 char mem[WRITE_BUFFER_SIZE];
f23b30e2 1307
fbd01e6c 1308 sendto (tempfd2, mem, arg == NULL ? sizeof (mem) : 1, 0, NULL, 0);
f23b30e2
UD
1309
1310 pthread_cleanup_pop (0);
1311
fa66f341 1312 FAIL_EXIT1 ("sendto returned");
f23b30e2
UD
1313}
1314
1315
1316static void *
1317tf_sendmsg (void *arg)
1318{
1319 if (arg == NULL)
1320 // XXX If somebody can provide a portable test case in which sendmsg()
1321 // blocks we can enable this test to run in both rounds.
1322 abort ();
1323
1324 struct sockaddr_un sun;
1325
1326 tempfd = socket (AF_UNIX, SOCK_DGRAM, 0);
1327 if (tempfd == -1)
fa66f341 1328 FAIL_EXIT1 ("socket (AF_UNIX, SOCK_DGRAM, 0): %m");
f23b30e2
UD
1329
1330 int tries = 0;
1331 do
1332 {
fa66f341 1333 TEST_VERIFY_EXIT (tries++ < 10);
f23b30e2
UD
1334
1335 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-7-XXXXXX");
fa66f341 1336 TEST_VERIFY_EXIT (mktemp (sun.sun_path) != NULL);
f23b30e2
UD
1337
1338 sun.sun_family = AF_UNIX;
1339 }
1340 while (bind (tempfd, (struct sockaddr *) &sun,
1341 offsetof (struct sockaddr_un, sun_path)
1342 + strlen (sun.sun_path) + 1) != 0);
1343 tempfname = strdup (sun.sun_path);
1344
1345 tempfd2 = socket (AF_UNIX, SOCK_DGRAM, 0);
1346 if (tempfd2 == -1)
fa66f341 1347 FAIL_EXIT1 ("socket (AF_UNIX, SOCK_DGRAM, 0): %m");
f23b30e2 1348
fa66f341 1349 xpthread_barrier_wait (&b2);
f23b30e2 1350
fa66f341 1351 xpthread_barrier_wait (&b2);
f23b30e2
UD
1352
1353 pthread_cleanup_push (cl, NULL);
1354
1355 char mem[1];
1356 struct iovec iov[1];
1357 iov[0].iov_base = mem;
1358 iov[0].iov_len = 1;
1359
1360 struct msghdr m;
1361 m.msg_name = &sun;
1362 m.msg_namelen = (offsetof (struct sockaddr_un, sun_path)
1363 + strlen (sun.sun_path) + 1);
1364 m.msg_iov = iov;
1365 m.msg_iovlen = 1;
1366 m.msg_control = NULL;
1367 m.msg_controllen = 0;
1368
1369 sendmsg (tempfd2, &m, 0);
1370
1371 pthread_cleanup_pop (0);
1372
fa66f341 1373 FAIL_EXIT1 ("sendmsg returned");
f23b30e2
UD
1374}
1375
1376
1377static void *
1378tf_creat (void *arg)
1379{
1380 if (arg == NULL)
1381 // XXX If somebody can provide a portable test case in which sendmsg()
1382 // blocks we can enable this test to run in both rounds.
1383 abort ();
1384
fa66f341 1385 xpthread_barrier_wait (&b2);
f23b30e2 1386
fa66f341 1387 xpthread_barrier_wait (&b2);
f23b30e2
UD
1388
1389 pthread_cleanup_push (cl, NULL);
1390
1391 creat ("tmp/tst-cancel-4-should-not-exist", 0666);
1392
1393 pthread_cleanup_pop (0);
1394
fa66f341 1395 FAIL_EXIT1 ("creat returned");
f23b30e2
UD
1396}
1397
1398
1399static void *
1400tf_connect (void *arg)
1401{
1402 if (arg == NULL)
1403 // XXX If somebody can provide a portable test case in which connect()
1404 // blocks we can enable this test to run in both rounds.
1405 abort ();
1406
1407 struct sockaddr_un sun;
1408
1409 tempfd = socket (AF_UNIX, SOCK_STREAM, 0);
1410 if (tempfd == -1)
fa66f341 1411 FAIL_EXIT1 ("socket (AF_UNIX, SOCK_STREAM, 0): %m");
f23b30e2
UD
1412
1413 int tries = 0;
1414 do
1415 {
fa66f341 1416 TEST_VERIFY_EXIT (tries++ < 10);
f23b30e2
UD
1417
1418 strcpy (sun.sun_path, "/tmp/tst-cancel4-socket-2-XXXXXX");
fa66f341 1419 TEST_VERIFY_EXIT (mktemp (sun.sun_path) != NULL);
f23b30e2
UD
1420
1421 sun.sun_family = AF_UNIX;
1422 }
1423 while (bind (tempfd, (struct sockaddr *) &sun,
1424 offsetof (struct sockaddr_un, sun_path)
1425 + strlen (sun.sun_path) + 1) != 0);
1426 tempfname = strdup (sun.sun_path);
1427
1428 listen (tempfd, 5);
1429
1430 tempfd2 = socket (AF_UNIX, SOCK_STREAM, 0);
1431 if (tempfd2 == -1)
fa66f341 1432 FAIL_EXIT1 ("socket (AF_UNIX, SOCK_STREAM, 0): %m");
f23b30e2 1433
fa66f341 1434 xpthread_barrier_wait (&b2);
f23b30e2
UD
1435
1436 if (arg != NULL)
fa66f341 1437 xpthread_barrier_wait (&b2);
f23b30e2
UD
1438
1439 pthread_cleanup_push (cl, NULL);
1440
1441 connect (tempfd2, (struct sockaddr *) &sun, sizeof (sun));
1442
1443 pthread_cleanup_pop (0);
1444
fa66f341 1445 FAIL_EXIT1 ("connect returned");
f23b30e2
UD
1446}
1447
1448
1449static void *
1450tf_tcdrain (void *arg)
1451{
1452 if (arg == NULL)
1453 // XXX If somebody can provide a portable test case in which tcdrain()
1454 // blocks we can enable this test to run in both rounds.
1455 abort ();
1456
fa66f341 1457 xpthread_barrier_wait (&b2);
f23b30e2
UD
1458
1459 if (arg != NULL)
fa66f341 1460 xpthread_barrier_wait (&b2);
f23b30e2
UD
1461
1462 pthread_cleanup_push (cl, NULL);
1463
1464 /* Regardless of stderr being a terminal, the tcdrain call should be
1465 canceled. */
1466 tcdrain (STDERR_FILENO);
1467
1468 pthread_cleanup_pop (0);
1469
fa66f341 1470 FAIL_EXIT1 ("tcdrain returned");
f23b30e2
UD
1471}
1472
1473
1474static void *
1475tf_msgrcv (void *arg)
1476{
483e95d0 1477 tempmsg = msgget (IPC_PRIVATE, 0666 | IPC_CREAT);
67060ef5 1478 if (tempmsg == -1)
f9cf8735
ST
1479 {
1480 if (errno == ENOSYS)
1481 {
1482 printf ("msgget not supported\n");
1483 tf_usleep (arg);
1484 pthread_exit (NULL);
1485 }
1486 else
1487 FAIL_EXIT1 ("msgget (IPC_PRIVATE, 0666 | IPC_CREAT): %m");
1488 }
f23b30e2 1489
fa66f341 1490 xpthread_barrier_wait (&b2);
f23b30e2
UD
1491
1492 if (arg != NULL)
fa66f341 1493 xpthread_barrier_wait (&b2);
f23b30e2 1494
7d7ff54c
UD
1495 ssize_t s;
1496
f23b30e2
UD
1497 pthread_cleanup_push (cl, NULL);
1498
1499 struct
1500 {
1501 long int type;
1502 char mem[10];
1503 } m;
ca343e73
UD
1504 int randnr;
1505 /* We need a positive random number. */
1506 do
483e95d0 1507 randnr = random () % 64000;
ca343e73
UD
1508 while (randnr <= 0);
1509 do
1510 {
1511 errno = 0;
7d7ff54c 1512 s = msgrcv (tempmsg, (struct msgbuf *) &m, 10, randnr, 0);
ca343e73 1513 }
7d7ff54c 1514 while (errno == EIDRM || errno == EINTR);
f23b30e2
UD
1515
1516 pthread_cleanup_pop (0);
1517
67060ef5
UD
1518 msgctl (tempmsg, IPC_RMID, NULL);
1519
fa66f341 1520 FAIL_EXIT1 ("msgrcv returned %zd", s);
f23b30e2
UD
1521}
1522
1523
1524static void *
1525tf_msgsnd (void *arg)
1526{
1527 if (arg == NULL)
1528 // XXX If somebody can provide a portable test case in which msgsnd()
1529 // blocks we can enable this test to run in both rounds.
1530 abort ();
1531
483e95d0 1532 tempmsg = msgget (IPC_PRIVATE, 0666 | IPC_CREAT);
67060ef5 1533 if (tempmsg == -1)
f9cf8735
ST
1534 {
1535 if (errno == ENOSYS)
1536 {
1537 printf ("msgget not supported\n");
1538 tf_usleep (arg);
1539 pthread_exit (NULL);
1540 }
1541 else
1542 FAIL_EXIT1 ("msgget (IPC_PRIVATE, 0666 | IPC_CREAT): %m");
1543 }
f23b30e2 1544
fa66f341 1545 xpthread_barrier_wait (&b2);
f23b30e2 1546
fa66f341 1547 xpthread_barrier_wait (&b2);
f23b30e2
UD
1548
1549 pthread_cleanup_push (cl, NULL);
1550
1551 struct
1552 {
1553 long int type;
1554 char mem[1];
1555 } m;
ca343e73
UD
1556 /* We need a positive random number. */
1557 do
483e95d0 1558 m.type = random () % 64000;
ca343e73 1559 while (m.type <= 0);
f23b30e2
UD
1560 msgsnd (tempmsg, (struct msgbuf *) &m, sizeof (m.mem), 0);
1561
1562 pthread_cleanup_pop (0);
1563
67060ef5
UD
1564 msgctl (tempmsg, IPC_RMID, NULL);
1565
fa66f341 1566 FAIL_EXIT1 ("msgsnd returned");
f23b30e2
UD
1567}
1568
1569
b39b6e0c 1570struct cancel_tests tests[] =
26676450 1571{
26676450
UD
1572 ADD_TEST (read, 2, 0),
1573 ADD_TEST (readv, 2, 0),
1574 ADD_TEST (select, 2, 0),
1575 ADD_TEST (pselect, 2, 0),
1576 ADD_TEST (poll, 2, 0),
7c65e900 1577 ADD_TEST (ppoll, 2, 0),
26676450
UD
1578 ADD_TEST (write, 2, 0),
1579 ADD_TEST (writev, 2, 0),
1580 ADD_TEST (sleep, 2, 0),
1581 ADD_TEST (usleep, 2, 0),
1582 ADD_TEST (nanosleep, 2, 0),
1583 ADD_TEST (wait, 2, 0),
1584 ADD_TEST (waitid, 2, 0),
1585 ADD_TEST (waitpid, 2, 0),
1586 ADD_TEST (sigpause, 2, 0),
1587 ADD_TEST (sigsuspend, 2, 0),
1588 ADD_TEST (sigwait, 2, 0),
1589 ADD_TEST (sigwaitinfo, 2, 0),
1590 ADD_TEST (sigtimedwait, 2, 0),
1591 ADD_TEST (pause, 2, 0),
1592 ADD_TEST (accept, 2, 0),
1593 ADD_TEST (send, 2, 0),
1594 ADD_TEST (recv, 2, 0),
1595 ADD_TEST (recvfrom, 2, 0),
1596 ADD_TEST (recvmsg, 2, 0),
4e778151 1597 ADD_TEST (preadv, 2, 1),
52bd9381 1598 ADD_TEST (preadv2, 2, 1),
af5fdf5a 1599 ADD_TEST (pwritev, 2, 1),
52bd9381 1600 ADD_TEST (pwritev2, 2, 1),
047aec8f
UD
1601 ADD_TEST (open, 2, 1),
1602 ADD_TEST (close, 2, 1),
1603 ADD_TEST (pread, 2, 1),
1604 ADD_TEST (pwrite, 2, 1),
1605 ADD_TEST (fsync, 2, 1),
aa75f64c 1606 ADD_TEST (fdatasync, 2, 1),
047aec8f 1607 ADD_TEST (msync, 2, 1),
f23b30e2
UD
1608 ADD_TEST (sendto, 2, 1),
1609 ADD_TEST (sendmsg, 2, 1),
1610 ADD_TEST (creat, 2, 1),
1611 ADD_TEST (connect, 2, 1),
1612 ADD_TEST (tcdrain, 2, 1),
1613 ADD_TEST (msgrcv, 2, 0),
1614 ADD_TEST (msgsnd, 2, 1),
26676450
UD
1615};
1616#define ntest_tf (sizeof (tests) / sizeof (tests[0]))
1617
b39b6e0c 1618#include "tst-cancel4-common.c"