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