Home | History | Annotate | Line # | Download | only in libpthread
t_cancellation.c revision 1.4
      1  1.4  riastrad /*	$NetBSD: t_cancellation.c,v 1.4 2025/04/05 11:22:32 riastradh Exp $	*/
      2  1.1  riastrad 
      3  1.1  riastrad /*
      4  1.1  riastrad  * Copyright (c) 2025 The NetBSD Foundation, Inc.
      5  1.1  riastrad  * All rights reserved.
      6  1.1  riastrad  *
      7  1.1  riastrad  * Redistribution and use in source and binary forms, with or without
      8  1.1  riastrad  * modification, are permitted provided that the following conditions
      9  1.1  riastrad  * are met:
     10  1.1  riastrad  * 1. Redistributions of source code must retain the above copyright
     11  1.1  riastrad  *    notice, this list of conditions and the following disclaimer.
     12  1.1  riastrad  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  riastrad  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  riastrad  *    documentation and/or other materials provided with the distribution.
     15  1.1  riastrad  *
     16  1.1  riastrad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.1  riastrad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.1  riastrad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.1  riastrad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.1  riastrad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.1  riastrad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.1  riastrad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.1  riastrad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.1  riastrad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.1  riastrad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.1  riastrad  * POSSIBILITY OF SUCH DAMAGE.
     27  1.1  riastrad  */
     28  1.1  riastrad 
     29  1.1  riastrad #include <sys/cdefs.h>
     30  1.4  riastrad __RCSID("$NetBSD: t_cancellation.c,v 1.4 2025/04/05 11:22:32 riastradh Exp $");
     31  1.1  riastrad 
     32  1.4  riastrad #include <sys/event.h>
     33  1.1  riastrad #include <sys/mman.h>
     34  1.1  riastrad #include <sys/msg.h>
     35  1.1  riastrad #include <sys/socket.h>
     36  1.1  riastrad #include <sys/un.h>
     37  1.1  riastrad #include <sys/wait.h>
     38  1.1  riastrad 
     39  1.1  riastrad #include <aio.h>
     40  1.1  riastrad #include <atf-c.h>
     41  1.1  riastrad #include <fcntl.h>
     42  1.1  riastrad #include <mqueue.h>
     43  1.1  riastrad #include <paths.h>
     44  1.1  riastrad #include <poll.h>
     45  1.1  riastrad #include <pthread.h>
     46  1.1  riastrad #include <signal.h>
     47  1.1  riastrad #include <stdatomic.h>
     48  1.1  riastrad #include <string.h>
     49  1.1  riastrad #include <termios.h>
     50  1.1  riastrad #include <threads.h>
     51  1.1  riastrad #include <time.h>
     52  1.1  riastrad #include <unistd.h>
     53  1.1  riastrad 
     54  1.4  riastrad #include "cancelpoint.h"
     55  1.1  riastrad #include "h_macros.h"
     56  1.1  riastrad 
     57  1.1  riastrad static const char *
     58  1.1  riastrad c11thrd_err(int error)
     59  1.1  riastrad {
     60  1.1  riastrad 	static char buf[32];
     61  1.1  riastrad 
     62  1.1  riastrad 	switch (error) {
     63  1.1  riastrad 	case thrd_busy:		return "thrd_busy";
     64  1.1  riastrad 	case thrd_nomem:	return "thrd_nomem";
     65  1.1  riastrad 	case thrd_success:	return "thrd_success";
     66  1.1  riastrad 	case thrd_timedout:	return "thrd_timedout";
     67  1.1  riastrad 	default:
     68  1.1  riastrad 		snprintf(buf, sizeof(buf), "thrd_%d", error);
     69  1.1  riastrad 		return buf;
     70  1.1  riastrad 	}
     71  1.1  riastrad }
     72  1.1  riastrad 
     73  1.1  riastrad #define	RT(x) do							      \
     74  1.1  riastrad {									      \
     75  1.1  riastrad 	int RT_rv = (x);						      \
     76  1.1  riastrad 	ATF_REQUIRE_MSG(RT_rv == 0, "%s: %d (%s)",			      \
     77  1.1  riastrad 	    #x, RT_rv, c11thrd_err(RT_rv));				      \
     78  1.1  riastrad } while (0)
     79  1.1  riastrad 
     80  1.1  riastrad pthread_barrier_t bar;
     81  1.1  riastrad bool cleanup_done;
     82  1.1  riastrad 
     83  1.1  riastrad /* POSIX style */
     84  1.1  riastrad static void *
     85  1.1  riastrad emptythread(void *cookie)
     86  1.1  riastrad {
     87  1.1  riastrad 	return NULL;
     88  1.1  riastrad }
     89  1.1  riastrad 
     90  1.1  riastrad /* C11 style */
     91  1.1  riastrad static int
     92  1.1  riastrad emptythrd(void *cookie)
     93  1.1  riastrad {
     94  1.1  riastrad 	return 123;
     95  1.1  riastrad }
     96  1.1  riastrad 
     97  1.1  riastrad static void
     98  1.1  riastrad cleanup_pthread_join(void *cookie)
     99  1.1  riastrad {
    100  1.1  riastrad 	pthread_t *tp = cookie;
    101  1.1  riastrad 	void *result;
    102  1.1  riastrad 
    103  1.1  riastrad 	RZ(pthread_join(*tp, &result));
    104  1.1  riastrad 	ATF_CHECK_MSG(result == NULL, "result=%p", result);
    105  1.1  riastrad }
    106  1.1  riastrad 
    107  1.1  riastrad static void
    108  1.1  riastrad cleanup_thrd_join(void *cookie)
    109  1.1  riastrad {
    110  1.1  riastrad 	thrd_t *tp = cookie;
    111  1.1  riastrad 	int result;
    112  1.1  riastrad 
    113  1.1  riastrad 	RT(thrd_join(*tp, &result));
    114  1.1  riastrad 	ATF_CHECK_MSG(result == 123, "result=%d", result);
    115  1.1  riastrad }
    116  1.1  riastrad 
    117  1.1  riastrad static void
    118  1.1  riastrad cleanup_msgid(void *cookie)
    119  1.1  riastrad {
    120  1.1  riastrad 	int *msgidp = cookie;
    121  1.1  riastrad 
    122  1.1  riastrad 	/*
    123  1.1  riastrad 	 * These message queue identifiers are persistent, so make sure
    124  1.1  riastrad 	 * to clean them up; otherwise the operator will have to run
    125  1.1  riastrad 	 * `ipcrm -q all' from time to time or else the tests will fail
    126  1.1  riastrad 	 * with ENOSPC.
    127  1.1  riastrad 	 */
    128  1.1  riastrad 	RL(msgctl(*msgidp, IPC_RMID, NULL));
    129  1.1  riastrad }
    130  1.1  riastrad 
    131  1.1  riastrad /*
    132  1.1  riastrad  * List of cancellation points in POSIX:
    133  1.1  riastrad  *
    134  1.1  riastrad  * https://pubs.opengroup.org/onlinepubs/9799919799.2024edition/functions/V2_chap02.html#tag_16_09_05_02
    135  1.1  riastrad  */
    136  1.1  riastrad 
    137  1.1  riastrad static int
    138  1.1  riastrad acceptsetup(void)
    139  1.1  riastrad {
    140  1.1  riastrad 	struct sockaddr_un sun = { .sun_family = AF_LOCAL };
    141  1.1  riastrad 	int sock;
    142  1.1  riastrad 
    143  1.1  riastrad 	strncpy(sun.sun_path, "sock", sizeof(sun.sun_path));
    144  1.1  riastrad 	RL(sock = socket(PF_LOCAL, SOCK_STREAM, 0));
    145  1.1  riastrad 	RL(bind(sock, (const struct sockaddr *)&sun, sizeof(sun)));
    146  1.1  riastrad 	RL(listen(sock, 1));
    147  1.1  riastrad 
    148  1.1  riastrad 	return sock;
    149  1.1  riastrad }
    150  1.1  riastrad 
    151  1.1  riastrad static void
    152  1.1  riastrad cancelpoint_accept(void)
    153  1.1  riastrad {
    154  1.1  riastrad 	const int sock = acceptsetup();
    155  1.1  riastrad 
    156  1.1  riastrad 	cancelpointready();
    157  1.1  riastrad 	RL(accept(sock, NULL, NULL));
    158  1.1  riastrad }
    159  1.1  riastrad 
    160  1.1  riastrad static void
    161  1.1  riastrad cancelpoint_accept4(void)
    162  1.1  riastrad {
    163  1.1  riastrad 	const int sock = acceptsetup();
    164  1.1  riastrad 
    165  1.1  riastrad 	cancelpointready();
    166  1.1  riastrad 	RL(accept4(sock, NULL, NULL, O_CLOEXEC));
    167  1.1  riastrad }
    168  1.1  riastrad 
    169  1.1  riastrad static void
    170  1.1  riastrad cancelpoint_aio_suspend(void)
    171  1.1  riastrad {
    172  1.1  riastrad 	int fd[2];
    173  1.1  riastrad 	char buf[32];
    174  1.1  riastrad 	struct aiocb aio = {
    175  1.1  riastrad 		.aio_offset = 0,
    176  1.1  riastrad 		.aio_buf = buf,
    177  1.1  riastrad 		.aio_nbytes = sizeof(buf),
    178  1.1  riastrad 		.aio_fildes = -1,
    179  1.1  riastrad 	};
    180  1.1  riastrad 	const struct aiocb *const aiolist[] = { &aio };
    181  1.1  riastrad 
    182  1.1  riastrad 	RL(pipe(fd));
    183  1.1  riastrad 	aio.aio_fildes = fd[0];
    184  1.1  riastrad 	RL(aio_read(&aio));
    185  1.1  riastrad 	cancelpointready();
    186  1.1  riastrad 	RL(aio_suspend(aiolist, __arraycount(aiolist), NULL));
    187  1.1  riastrad }
    188  1.1  riastrad 
    189  1.1  riastrad static void
    190  1.1  riastrad cancelpoint_clock_nanosleep(void)
    191  1.1  riastrad {
    192  1.1  riastrad 	/* XXX test all CLOCK_*? */
    193  1.1  riastrad 	struct timespec t = {.tv_sec = 1, .tv_nsec = 0};
    194  1.1  riastrad 
    195  1.1  riastrad 	cancelpointready();
    196  1.1  riastrad 	RL(clock_nanosleep(CLOCK_MONOTONIC, 0, &t, NULL));
    197  1.1  riastrad }
    198  1.1  riastrad 
    199  1.1  riastrad static void
    200  1.1  riastrad cancelpoint_close(void)
    201  1.1  riastrad {
    202  1.1  riastrad 	int fd;
    203  1.1  riastrad 
    204  1.1  riastrad 	RL(fd = open("/dev/null", O_RDWR));
    205  1.1  riastrad 	cancelpointready();
    206  1.1  riastrad 	RL(close(fd));
    207  1.1  riastrad }
    208  1.1  riastrad 
    209  1.1  riastrad static void
    210  1.1  riastrad cancelpoint_cnd_timedwait(void)
    211  1.1  riastrad {
    212  1.1  riastrad 	cnd_t cnd;
    213  1.1  riastrad 	mtx_t mtx;
    214  1.1  riastrad 	struct timespec t = {.tv_sec = 1, .tv_nsec = 0};
    215  1.1  riastrad 
    216  1.1  riastrad 	RT(cnd_init(&cnd));
    217  1.1  riastrad 	RT(mtx_init(&mtx, mtx_plain));
    218  1.1  riastrad 	cancelpointready();
    219  1.1  riastrad 	RT(mtx_lock(&mtx));
    220  1.1  riastrad 	RT(cnd_timedwait(&cnd, &mtx, &t));
    221  1.1  riastrad 	RT(mtx_unlock(&mtx));
    222  1.1  riastrad }
    223  1.1  riastrad 
    224  1.1  riastrad static void
    225  1.1  riastrad cancelpoint_cnd_wait(void)
    226  1.1  riastrad {
    227  1.1  riastrad 	cnd_t cnd;
    228  1.1  riastrad 	mtx_t mtx;
    229  1.1  riastrad 
    230  1.1  riastrad 	RT(cnd_init(&cnd));
    231  1.1  riastrad 	RT(mtx_init(&mtx, mtx_plain));
    232  1.1  riastrad 	cancelpointready();
    233  1.1  riastrad 	RT(mtx_lock(&mtx));
    234  1.1  riastrad 	RT(cnd_wait(&cnd, &mtx));
    235  1.1  riastrad 	RT(mtx_unlock(&mtx));
    236  1.1  riastrad }
    237  1.1  riastrad 
    238  1.1  riastrad static void
    239  1.1  riastrad cancelpoint_connect(void)
    240  1.1  riastrad {
    241  1.1  riastrad 	struct sockaddr_un sun = { .sun_family = AF_LOCAL };
    242  1.1  riastrad 	int sock;
    243  1.1  riastrad 
    244  1.1  riastrad 	strncpy(sun.sun_path, "sock", sizeof(sun.sun_path));
    245  1.1  riastrad 	RL(sock = socket(PF_LOCAL, SOCK_STREAM, 0));
    246  1.1  riastrad 	cancelpointready();
    247  1.1  riastrad 	RL(connect(sock, (const struct sockaddr *)&sun, sizeof(sun)));
    248  1.1  riastrad }
    249  1.1  riastrad 
    250  1.1  riastrad static void
    251  1.1  riastrad cancelpoint_creat(void)
    252  1.1  riastrad {
    253  1.1  riastrad 
    254  1.1  riastrad 	cancelpointready();
    255  1.1  riastrad 	RL(creat("file", 0666));
    256  1.1  riastrad }
    257  1.1  riastrad 
    258  1.1  riastrad static void
    259  1.1  riastrad cancelpoint_fcntl_F_SETLKW(void)
    260  1.1  riastrad {
    261  1.1  riastrad 	int fd;
    262  1.1  riastrad 	struct flock fl = {
    263  1.1  riastrad 		.l_start = 0,
    264  1.1  riastrad 		.l_len = 0,
    265  1.1  riastrad 		.l_type = F_WRLCK,
    266  1.1  riastrad 		.l_whence = SEEK_SET,
    267  1.1  riastrad 	};
    268  1.1  riastrad 
    269  1.1  riastrad 	RL(fd = open("file", O_RDWR|O_CREAT, 0666));
    270  1.1  riastrad 	cancelpointready();
    271  1.1  riastrad 	RL(fcntl(fd, F_SETLKW, &fl));
    272  1.1  riastrad }
    273  1.1  riastrad 
    274  1.1  riastrad static void
    275  1.1  riastrad cancelpoint_fcntl_F_OFD_SETLKW(void)
    276  1.1  riastrad {
    277  1.1  riastrad #ifdef F_OFD_SETLKW
    278  1.1  riastrad 	int fd;
    279  1.1  riastrad 	struct flock fl = {
    280  1.1  riastrad 		.l_start = 0,
    281  1.1  riastrad 		.l_len = 0,
    282  1.1  riastrad 		.l_type = F_WRLCK,
    283  1.1  riastrad 		.l_whence = SEEK_SET,
    284  1.1  riastrad 	};
    285  1.1  riastrad 
    286  1.1  riastrad 	RL(fd = open("file", O_RDWR|O_CREAT, 0666));
    287  1.1  riastrad 	cancelpointready();
    288  1.1  riastrad 	RL(fcntl(fd, F_OFD_SETLKW, &fl));
    289  1.1  riastrad #else
    290  1.1  riastrad 	atf_tc_expect_fail("PR kern/59241: POSIX.1-2024:"
    291  1.1  riastrad 	    " OFD-owned file locks");
    292  1.1  riastrad 	atf_tc_fail("no F_OFD_SETLKW");
    293  1.1  riastrad #endif
    294  1.1  riastrad }
    295  1.1  riastrad 
    296  1.1  riastrad static void
    297  1.1  riastrad cancelpoint_fdatasync(void)
    298  1.1  riastrad {
    299  1.1  riastrad 	int fd;
    300  1.1  riastrad 
    301  1.1  riastrad 	RL(fd = open("file", O_RDWR|O_CREAT, 0666));
    302  1.1  riastrad 	cancelpointready();
    303  1.1  riastrad 	RL(fdatasync(fd));
    304  1.1  riastrad }
    305  1.1  riastrad 
    306  1.1  riastrad static void
    307  1.1  riastrad cancelpoint_fsync(void)
    308  1.1  riastrad {
    309  1.1  riastrad 	int fd;
    310  1.1  riastrad 
    311  1.1  riastrad 	RL(fd = open("file", O_RDWR|O_CREAT, 0666));
    312  1.1  riastrad 	cancelpointready();
    313  1.1  riastrad 	RL(fsync(fd));
    314  1.1  riastrad }
    315  1.1  riastrad 
    316  1.1  riastrad static void
    317  1.4  riastrad cancelpoint_kevent(void)
    318  1.4  riastrad {
    319  1.4  riastrad 	int kq;
    320  1.4  riastrad 	struct kevent ev;
    321  1.4  riastrad 
    322  1.4  riastrad 	EV_SET(&ev, SIGUSR1, EVFILT_SIGNAL, EV_ADD|EV_ENABLE,
    323  1.4  riastrad 	    /*fflags*/0, /*data*/0, /*udata*/0);
    324  1.4  riastrad 
    325  1.4  riastrad 	RL(kq = kqueue());
    326  1.4  riastrad 	RL(kevent(kq, &ev, 1, NULL, 1, &(const struct timespec){0,0}));
    327  1.4  riastrad 	cancelpointready();
    328  1.4  riastrad 	RL(kevent(kq, NULL, 0, &ev, 1, NULL));
    329  1.4  riastrad }
    330  1.4  riastrad 
    331  1.4  riastrad static void
    332  1.1  riastrad cancelpoint_lockf_F_LOCK(void)
    333  1.1  riastrad {
    334  1.1  riastrad 	int fd;
    335  1.1  riastrad 
    336  1.1  riastrad 	RL(fd = open("file", O_RDWR|O_CREAT, 0666));
    337  1.1  riastrad 	cancelpointready();
    338  1.1  riastrad 	RL(lockf(fd, F_LOCK, 0));
    339  1.1  riastrad }
    340  1.1  riastrad 
    341  1.1  riastrad static void
    342  1.1  riastrad cancelpoint_mq_receive(void)
    343  1.1  riastrad {
    344  1.1  riastrad 	mqd_t mq;
    345  1.1  riastrad 	char buf[32];
    346  1.1  riastrad 
    347  1.1  riastrad 	RL(mq = mq_open("mq", O_RDWR|O_CREAT, 0666, NULL));
    348  1.1  riastrad 	cancelpointready();
    349  1.1  riastrad 	RL(mq_receive(mq, buf, sizeof(buf), NULL));
    350  1.1  riastrad }
    351  1.1  riastrad 
    352  1.1  riastrad static void
    353  1.1  riastrad cancelpoint_mq_send(void)
    354  1.1  riastrad {
    355  1.1  riastrad 	mqd_t mq;
    356  1.1  riastrad 	char buf[32] = {0};
    357  1.1  riastrad 
    358  1.1  riastrad 	RL(mq = mq_open("mq", O_RDWR|O_CREAT, 0666, NULL));
    359  1.1  riastrad 	cancelpointready();
    360  1.1  riastrad 	RL(mq_send(mq, buf, sizeof(buf), 0));
    361  1.1  riastrad }
    362  1.1  riastrad 
    363  1.1  riastrad static void
    364  1.1  riastrad cancelpoint_mq_timedreceive(void)
    365  1.1  riastrad {
    366  1.1  riastrad 	mqd_t mq;
    367  1.1  riastrad 	char buf[32];
    368  1.1  riastrad 	struct timespec t = {.tv_sec = 1, .tv_nsec = 0};
    369  1.1  riastrad 
    370  1.1  riastrad 	RL(mq = mq_open("mq", O_RDWR|O_CREAT, 0666, NULL));
    371  1.1  riastrad 	cancelpointready();
    372  1.1  riastrad 	RL(mq_timedreceive(mq, buf, sizeof(buf), NULL, &t));
    373  1.1  riastrad }
    374  1.1  riastrad 
    375  1.1  riastrad static void
    376  1.1  riastrad cancelpoint_mq_timedsend(void)
    377  1.1  riastrad {
    378  1.1  riastrad 	mqd_t mq;
    379  1.1  riastrad 	char buf[32] = {0};
    380  1.1  riastrad 	struct timespec t = {.tv_sec = 1, .tv_nsec = 0};
    381  1.1  riastrad 
    382  1.1  riastrad 	RL(mq = mq_open("mq", O_RDWR|O_CREAT, 0666, NULL));
    383  1.1  riastrad 	cancelpointready();
    384  1.1  riastrad 	RL(mq_timedsend(mq, buf, sizeof(buf), 0, &t));
    385  1.1  riastrad }
    386  1.1  riastrad 
    387  1.1  riastrad static void
    388  1.1  riastrad cancelpoint_msgrcv(void)
    389  1.1  riastrad {
    390  1.1  riastrad 	int msgid;
    391  1.1  riastrad 	char buf[32];
    392  1.1  riastrad 
    393  1.1  riastrad 	RL(msgid = msgget(IPC_PRIVATE, IPC_CREAT));
    394  1.1  riastrad 	pthread_cleanup_push(&cleanup_msgid, &msgid);
    395  1.1  riastrad 	cancelpointready();
    396  1.1  riastrad 	RL(msgrcv(msgid, buf, sizeof(buf), 0, 0));
    397  1.1  riastrad 	pthread_cleanup_pop(/*execute*/1);
    398  1.1  riastrad }
    399  1.1  riastrad 
    400  1.1  riastrad static void
    401  1.1  riastrad cancelpoint_msgsnd(void)
    402  1.1  riastrad {
    403  1.1  riastrad 	int msgid;
    404  1.1  riastrad 	char buf[32] = {0};
    405  1.1  riastrad 
    406  1.1  riastrad 	RL(msgid = msgget(IPC_PRIVATE, IPC_CREAT));
    407  1.1  riastrad 	pthread_cleanup_push(&cleanup_msgid, &msgid);
    408  1.1  riastrad 	cancelpointready();
    409  1.1  riastrad 	RL(msgsnd(msgid, buf, sizeof(buf), 0));
    410  1.1  riastrad 	pthread_cleanup_pop(/*execute*/1);
    411  1.1  riastrad }
    412  1.1  riastrad 
    413  1.1  riastrad static void
    414  1.1  riastrad cancelpoint_msync(void)
    415  1.1  riastrad {
    416  1.1  riastrad 	const unsigned long pagesize = sysconf(_SC_PAGESIZE);
    417  1.1  riastrad 	int fd;
    418  1.1  riastrad 	void *map;
    419  1.1  riastrad 
    420  1.1  riastrad 	RL(fd = open("file", O_RDWR|O_CREAT, 0666));
    421  1.1  riastrad 	RL(ftruncate(fd, pagesize));
    422  1.1  riastrad 	REQUIRE_LIBC(map = mmap(NULL, pagesize, PROT_READ|PROT_WRITE,
    423  1.1  riastrad 		MAP_SHARED, fd, 0),
    424  1.1  riastrad 	    MAP_FAILED);
    425  1.1  riastrad 	cancelpointready();
    426  1.1  riastrad 	RL(msync(map, pagesize, MS_SYNC));
    427  1.1  riastrad }
    428  1.1  riastrad 
    429  1.1  riastrad static void
    430  1.1  riastrad cancelpoint_nanosleep(void)
    431  1.1  riastrad {
    432  1.1  riastrad 	/* XXX test all CLOCK_*? */
    433  1.1  riastrad 	struct timespec t = {.tv_sec = 1, .tv_nsec = 0};
    434  1.1  riastrad 
    435  1.1  riastrad 	cancelpointready();
    436  1.1  riastrad 	RL(nanosleep(&t, NULL));
    437  1.1  riastrad }
    438  1.1  riastrad 
    439  1.1  riastrad static void
    440  1.1  riastrad cancelpoint_open(void)
    441  1.1  riastrad {
    442  1.1  riastrad 
    443  1.1  riastrad 	cancelpointready();
    444  1.1  riastrad 	RL(open("file", O_RDWR));
    445  1.1  riastrad }
    446  1.1  riastrad 
    447  1.1  riastrad static void
    448  1.1  riastrad cancelpoint_openat(void)
    449  1.1  riastrad {
    450  1.1  riastrad 
    451  1.1  riastrad 	cancelpointready();
    452  1.1  riastrad 	RL(openat(AT_FDCWD, "file", O_RDWR));
    453  1.1  riastrad }
    454  1.1  riastrad 
    455  1.1  riastrad static void
    456  1.1  riastrad cancelpoint_pause(void)
    457  1.1  riastrad {
    458  1.1  riastrad 
    459  1.1  riastrad 	cancelpointready();
    460  1.1  riastrad 	RL(pause());
    461  1.1  riastrad }
    462  1.1  riastrad 
    463  1.1  riastrad static void
    464  1.1  riastrad cancelpoint_poll(void)
    465  1.1  riastrad {
    466  1.1  riastrad 	int fd[2];
    467  1.1  riastrad 	struct pollfd pfd;
    468  1.1  riastrad 
    469  1.1  riastrad 	RL(pipe(fd));
    470  1.1  riastrad 	pfd.fd = fd[0];
    471  1.1  riastrad 	pfd.events = POLLIN;
    472  1.1  riastrad 	cancelpointready();
    473  1.1  riastrad 	RL(poll(&pfd, 1, 1000));
    474  1.1  riastrad }
    475  1.1  riastrad 
    476  1.1  riastrad static void
    477  1.1  riastrad cancelpoint_posix_close(void)
    478  1.1  riastrad {
    479  1.1  riastrad #if 0
    480  1.1  riastrad 	int fd;
    481  1.1  riastrad 
    482  1.1  riastrad 	RL(fd = open("file", O_RDWR|O_CREAT, 0666));
    483  1.1  riastrad 	cancelpointready();
    484  1.1  riastrad 	RL(posix_close(fd, POSIX_CLOSE_RESTART));
    485  1.1  riastrad #else
    486  1.1  riastrad 	atf_tc_expect_fail("PR kern/58929: POSIX.1-2024 compliance:"
    487  1.1  riastrad 	    " posix_close, POSIX_CLOSE_RESTART");
    488  1.1  riastrad 	atf_tc_fail("no posix_close");
    489  1.1  riastrad #endif
    490  1.1  riastrad }
    491  1.1  riastrad 
    492  1.1  riastrad static void
    493  1.1  riastrad cancelpoint_ppoll(void)
    494  1.1  riastrad {
    495  1.1  riastrad 	int fd[2];
    496  1.1  riastrad 	struct pollfd pfd;
    497  1.1  riastrad 	struct timespec t = {.tv_sec = 1, .tv_nsec = 0};
    498  1.1  riastrad 
    499  1.1  riastrad 	RL(pipe(fd));
    500  1.1  riastrad 	pfd.fd = fd[0];
    501  1.1  riastrad 	pfd.events = POLLIN;
    502  1.1  riastrad 	cancelpointready();
    503  1.1  riastrad 	RL(ppoll(&pfd, 1, &t, NULL));
    504  1.1  riastrad }
    505  1.1  riastrad 
    506  1.1  riastrad static void
    507  1.1  riastrad cancelpoint_pread(void)
    508  1.1  riastrad {
    509  1.1  riastrad 	int fd;
    510  1.1  riastrad 	char buf[1];
    511  1.1  riastrad 
    512  1.1  riastrad 	RL(fd = open("file", O_RDWR|O_CREAT, 0666));
    513  1.1  riastrad 	cancelpointready();
    514  1.1  riastrad 	RL(pread(fd, buf, sizeof(buf), 1));
    515  1.1  riastrad }
    516  1.1  riastrad 
    517  1.1  riastrad 
    518  1.1  riastrad static void
    519  1.1  riastrad cancelpoint_pselect(void)
    520  1.1  riastrad {
    521  1.1  riastrad 	int fd[2];
    522  1.1  riastrad 	fd_set readfd;
    523  1.1  riastrad 	struct timespec t = {.tv_sec = 1, .tv_nsec = 0};
    524  1.1  riastrad 
    525  1.1  riastrad 	FD_ZERO(&readfd);
    526  1.1  riastrad 
    527  1.1  riastrad 	RL(pipe(fd));
    528  1.1  riastrad 	FD_SET(fd[0], &readfd);
    529  1.1  riastrad 	cancelpointready();
    530  1.1  riastrad 	RL(pselect(fd[0] + 1, &readfd, NULL, NULL, &t, NULL));
    531  1.1  riastrad }
    532  1.1  riastrad 
    533  1.1  riastrad static void
    534  1.1  riastrad cancelpoint_pthread_cond_clockwait(void)
    535  1.1  riastrad {
    536  1.1  riastrad #if 0
    537  1.1  riastrad 	pthread_cond_t cond;
    538  1.1  riastrad 	pthread_mutex_t mutex;
    539  1.1  riastrad 	struct timespec t = {.tv_sec = 1, .tv_nsec = 0};
    540  1.1  riastrad 
    541  1.1  riastrad 	RZ(pthread_cond_init(&cond, NULL));
    542  1.1  riastrad 	RZ(pthread_mutex_init(&mutex, NULL));
    543  1.1  riastrad 	cancelpointready();
    544  1.1  riastrad 	RZ(pthread_mutex_lock(&mutex));
    545  1.1  riastrad 	RZ(pthread_cond_clockwait(&cond, &mutex, CLOCK_MONOTONIC, &t));
    546  1.1  riastrad 	RZ(pthread_mutex_unlock(&mutex));
    547  1.1  riastrad #else
    548  1.1  riastrad 	atf_tc_expect_fail("PR lib/59142: POSIX.1-2024:"
    549  1.1  riastrad 	    " pthread_cond_clockwait and company");
    550  1.1  riastrad 	atf_tc_fail("no posix_cond_clockwait");
    551  1.1  riastrad #endif
    552  1.1  riastrad }
    553  1.1  riastrad 
    554  1.1  riastrad static void
    555  1.1  riastrad cancelpoint_pthread_cond_timedwait(void)
    556  1.1  riastrad {
    557  1.1  riastrad 	pthread_cond_t cond;
    558  1.1  riastrad 	pthread_mutex_t mutex;
    559  1.1  riastrad 	struct timespec t = {.tv_sec = 1, .tv_nsec = 0};
    560  1.1  riastrad 
    561  1.1  riastrad 	RZ(pthread_cond_init(&cond, NULL));
    562  1.1  riastrad 	RZ(pthread_mutex_init(&mutex, NULL));
    563  1.1  riastrad 	cancelpointready();
    564  1.1  riastrad 	RZ(pthread_mutex_lock(&mutex));
    565  1.1  riastrad 	RZ(pthread_cond_timedwait(&cond, &mutex, &t));
    566  1.1  riastrad 	RZ(pthread_mutex_unlock(&mutex));
    567  1.1  riastrad }
    568  1.1  riastrad 
    569  1.1  riastrad static void
    570  1.1  riastrad cancelpoint_pthread_cond_wait(void)
    571  1.1  riastrad {
    572  1.1  riastrad 	pthread_cond_t cond;
    573  1.1  riastrad 	pthread_mutex_t mutex;
    574  1.1  riastrad 
    575  1.1  riastrad 	RZ(pthread_cond_init(&cond, NULL));
    576  1.1  riastrad 	RZ(pthread_mutex_init(&mutex, NULL));
    577  1.1  riastrad 	cancelpointready();
    578  1.1  riastrad 	RZ(pthread_mutex_lock(&mutex));
    579  1.1  riastrad 	RZ(pthread_cond_wait(&cond, &mutex));
    580  1.1  riastrad 	RZ(pthread_mutex_unlock(&mutex));
    581  1.1  riastrad }
    582  1.1  riastrad 
    583  1.1  riastrad static void
    584  1.1  riastrad cancelpoint_pthread_join(void)
    585  1.1  riastrad {
    586  1.1  riastrad 	pthread_t t;
    587  1.1  riastrad 
    588  1.1  riastrad 	RZ(pthread_create(&t, NULL, &emptythread, NULL));
    589  1.1  riastrad 	pthread_cleanup_push(&cleanup_pthread_join, &t);
    590  1.1  riastrad 	cancelpointready();
    591  1.1  riastrad 	RZ(pthread_join(t, NULL));
    592  1.1  riastrad 	pthread_cleanup_pop(/*execute*/0);
    593  1.1  riastrad }
    594  1.1  riastrad 
    595  1.1  riastrad static void
    596  1.1  riastrad cancelpoint_pthread_testcancel(void)
    597  1.1  riastrad {
    598  1.1  riastrad 
    599  1.1  riastrad 	cancelpointready();
    600  1.1  riastrad 	pthread_testcancel();
    601  1.1  riastrad }
    602  1.1  riastrad 
    603  1.1  riastrad static void
    604  1.1  riastrad cancelpoint_pwrite(void)
    605  1.1  riastrad {
    606  1.1  riastrad 	int fd;
    607  1.1  riastrad 	char buf[1] = {0};
    608  1.1  riastrad 
    609  1.1  riastrad 	RL(fd = open("file", O_RDWR|O_CREAT, 0666));
    610  1.1  riastrad 	cancelpointready();
    611  1.1  riastrad 	RL(pwrite(fd, buf, sizeof(buf), 1));
    612  1.1  riastrad }
    613  1.1  riastrad 
    614  1.1  riastrad static void
    615  1.1  riastrad cancelpoint_read(void)
    616  1.1  riastrad {
    617  1.1  riastrad 	int fd;
    618  1.1  riastrad 	char buf[1];
    619  1.1  riastrad 
    620  1.1  riastrad 	RL(fd = open("file", O_RDWR|O_CREAT, 0666));
    621  1.1  riastrad 	cancelpointready();
    622  1.1  riastrad 	RL(read(fd, buf, sizeof(buf)));
    623  1.1  riastrad }
    624  1.1  riastrad 
    625  1.1  riastrad static void
    626  1.1  riastrad cancelpoint_readv(void)
    627  1.1  riastrad {
    628  1.1  riastrad 	int fd;
    629  1.1  riastrad 	char buf[1];
    630  1.1  riastrad 	struct iovec iov = { .iov_base = buf, .iov_len = sizeof(buf) };
    631  1.1  riastrad 
    632  1.1  riastrad 	RL(fd = open("file", O_RDWR|O_CREAT, 0666));
    633  1.1  riastrad 	cancelpointready();
    634  1.1  riastrad 	RL(readv(fd, &iov, 1));
    635  1.1  riastrad }
    636  1.1  riastrad 
    637  1.1  riastrad static void
    638  1.1  riastrad cancelpoint_recv(void)
    639  1.1  riastrad {
    640  1.1  riastrad 	struct sockaddr_un sun = { .sun_family = AF_LOCAL };
    641  1.1  riastrad 	int sock;
    642  1.1  riastrad 	char buf[1];
    643  1.1  riastrad 
    644  1.1  riastrad 	strncpy(sun.sun_path, "sock", sizeof(sun.sun_path));
    645  1.1  riastrad 	RL(sock = socket(PF_LOCAL, SOCK_DGRAM, 0));
    646  1.1  riastrad 	RL(bind(sock, (const struct sockaddr *)&sun, sizeof(sun)));
    647  1.1  riastrad 	cancelpointready();
    648  1.1  riastrad 	RL(recv(sock, buf, sizeof(buf), 0));
    649  1.1  riastrad }
    650  1.1  riastrad 
    651  1.1  riastrad static void
    652  1.1  riastrad cancelpoint_recvfrom(void)
    653  1.1  riastrad {
    654  1.1  riastrad 	struct sockaddr_un sun = { .sun_family = AF_LOCAL };
    655  1.1  riastrad 	int sock;
    656  1.1  riastrad 	char buf[1];
    657  1.1  riastrad 	struct sockaddr_storage ss;
    658  1.1  riastrad 	socklen_t len = sizeof(ss);
    659  1.1  riastrad 
    660  1.1  riastrad 	strncpy(sun.sun_path, "sock", sizeof(sun.sun_path));
    661  1.1  riastrad 	RL(sock = socket(PF_LOCAL, SOCK_DGRAM, 0));
    662  1.1  riastrad 	RL(bind(sock, (const struct sockaddr *)&sun, sizeof(sun)));
    663  1.1  riastrad 	cancelpointready();
    664  1.1  riastrad 	RL(recvfrom(sock, buf, sizeof(buf), 0, (struct sockaddr *)&ss, &len));
    665  1.1  riastrad }
    666  1.1  riastrad 
    667  1.1  riastrad static void
    668  1.1  riastrad cancelpoint_recvmsg(void)
    669  1.1  riastrad {
    670  1.1  riastrad 	struct sockaddr_un sun = { .sun_family = AF_LOCAL };
    671  1.1  riastrad 	int sock;
    672  1.1  riastrad 	char buf[1];
    673  1.1  riastrad 	struct iovec iov = { .iov_base = buf, .iov_len = sizeof(buf) };
    674  1.1  riastrad 	struct msghdr msg = {
    675  1.1  riastrad 		.msg_iov = &iov,
    676  1.1  riastrad 		.msg_iovlen = 1,
    677  1.1  riastrad 	};
    678  1.1  riastrad 
    679  1.1  riastrad 	strncpy(sun.sun_path, "sock", sizeof(sun.sun_path));
    680  1.1  riastrad 	RL(sock = socket(PF_LOCAL, SOCK_DGRAM, 0));
    681  1.1  riastrad 	RL(bind(sock, (const struct sockaddr *)&sun, sizeof(sun)));
    682  1.1  riastrad 	cancelpointready();
    683  1.1  riastrad 	RL(recvmsg(sock, &msg, 0));
    684  1.1  riastrad }
    685  1.1  riastrad 
    686  1.1  riastrad static void
    687  1.1  riastrad cancelpoint_select(void)
    688  1.1  riastrad {
    689  1.1  riastrad 	int fd[2];
    690  1.1  riastrad 	fd_set readfd;
    691  1.1  riastrad 	struct timeval t = {.tv_sec = 1, .tv_usec = 0};
    692  1.1  riastrad 
    693  1.1  riastrad 	FD_ZERO(&readfd);
    694  1.1  riastrad 
    695  1.1  riastrad 	RL(pipe(fd));
    696  1.1  riastrad 	FD_SET(fd[0], &readfd);
    697  1.1  riastrad 	cancelpointready();
    698  1.1  riastrad 	RL(select(fd[0] + 1, &readfd, NULL, NULL, &t));
    699  1.1  riastrad }
    700  1.1  riastrad 
    701  1.1  riastrad static void
    702  1.1  riastrad cancelpoint_send(void)
    703  1.1  riastrad {
    704  1.1  riastrad 	struct sockaddr_un sun = { .sun_family = AF_LOCAL };
    705  1.1  riastrad 	int sock;
    706  1.1  riastrad 	char buf[1] = {0};
    707  1.1  riastrad 
    708  1.1  riastrad 	strncpy(sun.sun_path, "sock", sizeof(sun.sun_path));
    709  1.1  riastrad 	RL(sock = socket(PF_LOCAL, SOCK_DGRAM, 0));
    710  1.1  riastrad 	RL(bind(sock, (const struct sockaddr *)&sun, sizeof(sun)));
    711  1.1  riastrad 	cancelpointready();
    712  1.1  riastrad 	RL(send(sock, buf, sizeof(buf), 0));
    713  1.1  riastrad }
    714  1.1  riastrad 
    715  1.1  riastrad static void
    716  1.1  riastrad cancelpoint_sendto(void)
    717  1.1  riastrad {
    718  1.1  riastrad 	struct sockaddr_un sun = { .sun_family = AF_LOCAL };
    719  1.1  riastrad 	int sock;
    720  1.1  riastrad 	char buf[1] = {0};
    721  1.1  riastrad 
    722  1.1  riastrad 	strncpy(sun.sun_path, "sock", sizeof(sun.sun_path));
    723  1.1  riastrad 	RL(sock = socket(PF_LOCAL, SOCK_DGRAM, 0));
    724  1.1  riastrad 	cancelpointready();
    725  1.1  riastrad 	RL(sendto(sock, buf, sizeof(buf), 0, (const struct sockaddr *)&sun,
    726  1.1  riastrad 		sizeof(sun)));
    727  1.1  riastrad }
    728  1.1  riastrad 
    729  1.1  riastrad static void
    730  1.1  riastrad cancelpoint_sendmsg(void)
    731  1.1  riastrad {
    732  1.1  riastrad 	struct sockaddr_un sun = { .sun_family = AF_LOCAL };
    733  1.1  riastrad 	int sock;
    734  1.1  riastrad 	char buf[1] = {0};
    735  1.1  riastrad 	struct iovec iov = { .iov_base = buf, .iov_len = sizeof(buf) };
    736  1.1  riastrad 	struct msghdr msg = {
    737  1.1  riastrad 		.msg_name = (struct sockaddr *)&sun,
    738  1.1  riastrad 		.msg_namelen = sizeof(sun),
    739  1.1  riastrad 		.msg_iov = &iov,
    740  1.1  riastrad 		.msg_iovlen = 1,
    741  1.1  riastrad 	};
    742  1.1  riastrad 
    743  1.1  riastrad 	strncpy(sun.sun_path, "sock", sizeof(sun.sun_path));
    744  1.1  riastrad 	RL(sock = socket(PF_LOCAL, SOCK_DGRAM, 0));
    745  1.1  riastrad 	cancelpointready();
    746  1.1  riastrad 	RL(sendmsg(sock, &msg, 0));
    747  1.1  riastrad }
    748  1.1  riastrad 
    749  1.1  riastrad static void
    750  1.1  riastrad cancelpoint_sigsuspend(void)
    751  1.1  riastrad {
    752  1.1  riastrad 	sigset_t mask, omask;
    753  1.1  riastrad 
    754  1.1  riastrad 	RL(sigfillset(&mask));
    755  1.1  riastrad 	RL(sigprocmask(SIG_BLOCK, &mask, &omask));
    756  1.1  riastrad 	cancelpointready();
    757  1.1  riastrad 	RL(sigsuspend(&omask));
    758  1.1  riastrad }
    759  1.1  riastrad 
    760  1.1  riastrad static void
    761  1.1  riastrad cancelpoint_sigtimedwait(void)
    762  1.1  riastrad {
    763  1.1  riastrad 	sigset_t mask, omask;
    764  1.1  riastrad 	siginfo_t info;
    765  1.1  riastrad 	struct timespec t = {.tv_sec = 1, .tv_nsec = 0};
    766  1.1  riastrad 
    767  1.1  riastrad 	RL(sigfillset(&mask));
    768  1.1  riastrad 	RL(sigprocmask(SIG_BLOCK, &mask, &omask));
    769  1.1  riastrad 	cancelpointready();
    770  1.1  riastrad 	RL(sigtimedwait(&omask, &info, &t));
    771  1.1  riastrad }
    772  1.1  riastrad 
    773  1.1  riastrad static void
    774  1.1  riastrad cancelpoint_sigwait(void)
    775  1.1  riastrad {
    776  1.1  riastrad 	sigset_t mask, omask;
    777  1.1  riastrad 	int sig;
    778  1.1  riastrad 
    779  1.1  riastrad 	RL(sigfillset(&mask));
    780  1.1  riastrad 	RL(sigprocmask(SIG_BLOCK, &mask, &omask));
    781  1.1  riastrad 	cancelpointready();
    782  1.1  riastrad 	RL(sigwait(&omask, &sig));
    783  1.1  riastrad }
    784  1.1  riastrad 
    785  1.1  riastrad static void
    786  1.1  riastrad cancelpoint_sigwaitinfo(void)
    787  1.1  riastrad {
    788  1.1  riastrad 	sigset_t mask, omask;
    789  1.1  riastrad 	siginfo_t info;
    790  1.1  riastrad 
    791  1.1  riastrad 	RL(sigfillset(&mask));
    792  1.1  riastrad 	RL(sigprocmask(SIG_BLOCK, &mask, &omask));
    793  1.1  riastrad 	cancelpointready();
    794  1.1  riastrad 	RL(sigwaitinfo(&omask, &info));
    795  1.1  riastrad }
    796  1.1  riastrad 
    797  1.1  riastrad static void
    798  1.1  riastrad cancelpoint_sleep(void)
    799  1.1  riastrad {
    800  1.1  riastrad 
    801  1.1  riastrad 	cancelpointready();
    802  1.1  riastrad 	(void)sleep(1);
    803  1.1  riastrad }
    804  1.1  riastrad 
    805  1.1  riastrad static void
    806  1.1  riastrad cancelpoint_tcdrain(void)
    807  1.1  riastrad {
    808  1.1  riastrad 	int hostfd, appfd;
    809  1.1  riastrad 	char *pts;
    810  1.1  riastrad 
    811  1.1  riastrad 	RL(hostfd = posix_openpt(O_RDWR|O_NOCTTY));
    812  1.1  riastrad 	RL(grantpt(hostfd));
    813  1.1  riastrad 	RL(unlockpt(hostfd));
    814  1.1  riastrad 	REQUIRE_LIBC(pts = ptsname(hostfd), NULL);
    815  1.1  riastrad 	RL(appfd = open(pts, O_RDWR|O_NOCTTY));
    816  1.1  riastrad 	cancelpointready();
    817  1.1  riastrad 	RL(tcdrain(appfd));
    818  1.1  riastrad }
    819  1.1  riastrad 
    820  1.1  riastrad static void
    821  1.1  riastrad cancelpoint_thrd_join(void)
    822  1.1  riastrad {
    823  1.1  riastrad 	thrd_t t;
    824  1.1  riastrad 
    825  1.1  riastrad 	RT(thrd_create(&t, &emptythrd, NULL));
    826  1.1  riastrad 	pthread_cleanup_push(&cleanup_thrd_join, &t);
    827  1.1  riastrad 	cancelpointready();
    828  1.1  riastrad 	RT(thrd_join(t, NULL));
    829  1.1  riastrad 	pthread_cleanup_pop(/*execute*/0);
    830  1.1  riastrad }
    831  1.1  riastrad 
    832  1.1  riastrad static void
    833  1.1  riastrad cancelpoint_thrd_sleep(void)
    834  1.1  riastrad {
    835  1.1  riastrad 	struct timespec t = {.tv_sec = 1, .tv_nsec = 0};
    836  1.1  riastrad 
    837  1.1  riastrad 	cancelpointready();
    838  1.1  riastrad 	RT(thrd_sleep(&t, NULL));
    839  1.1  riastrad }
    840  1.1  riastrad 
    841  1.1  riastrad static void
    842  1.1  riastrad cancelpoint_wait(void)
    843  1.1  riastrad {
    844  1.1  riastrad 
    845  1.1  riastrad 	cancelpointready();
    846  1.1  riastrad 	RL(wait(NULL));
    847  1.1  riastrad }
    848  1.1  riastrad 
    849  1.1  riastrad static void
    850  1.1  riastrad cancelpoint_waitid(void)
    851  1.1  riastrad {
    852  1.1  riastrad 
    853  1.1  riastrad 	cancelpointready();
    854  1.1  riastrad 	RL(waitid(P_ALL, 0, NULL, 0));
    855  1.1  riastrad }
    856  1.1  riastrad 
    857  1.1  riastrad static void
    858  1.1  riastrad cancelpoint_waitpid(void)
    859  1.1  riastrad {
    860  1.1  riastrad 
    861  1.1  riastrad 	cancelpointready();
    862  1.1  riastrad 	RL(waitpid(-1, NULL, 0));
    863  1.1  riastrad }
    864  1.1  riastrad 
    865  1.1  riastrad static void
    866  1.1  riastrad cancelpoint_write(void)
    867  1.1  riastrad {
    868  1.1  riastrad 	int fd;
    869  1.1  riastrad 	char buf[1] = {0};
    870  1.1  riastrad 
    871  1.1  riastrad 	RL(fd = open("file", O_RDWR|O_CREAT, 0666));
    872  1.1  riastrad 	cancelpointready();
    873  1.1  riastrad 	RL(write(fd, buf, sizeof(buf)));
    874  1.1  riastrad }
    875  1.1  riastrad 
    876  1.1  riastrad static void
    877  1.1  riastrad cancelpoint_writev(void)
    878  1.1  riastrad {
    879  1.1  riastrad 	int fd;
    880  1.1  riastrad 	char buf[1] = {0};
    881  1.1  riastrad 	struct iovec iov = { .iov_base = buf, .iov_len = sizeof(buf) };
    882  1.1  riastrad 
    883  1.1  riastrad 	RL(fd = open("file", O_RDWR|O_CREAT, 0666));
    884  1.1  riastrad 	cancelpointready();
    885  1.1  riastrad 	RL(writev(fd, &iov, 1));
    886  1.1  riastrad }
    887  1.1  riastrad 
    888  1.1  riastrad TEST_CANCELPOINT(cancelpoint_accept, __nothing)
    889  1.3  riastrad TEST_CANCELPOINT(cancelpoint_accept4, __nothing)
    890  1.1  riastrad TEST_CANCELPOINT(cancelpoint_aio_suspend, __nothing)
    891  1.1  riastrad TEST_CANCELPOINT(cancelpoint_clock_nanosleep, __nothing)
    892  1.1  riastrad TEST_CANCELPOINT(cancelpoint_close, __nothing)
    893  1.1  riastrad TEST_CANCELPOINT(cancelpoint_cnd_timedwait, __nothing)
    894  1.1  riastrad TEST_CANCELPOINT(cancelpoint_cnd_wait, __nothing)
    895  1.1  riastrad TEST_CANCELPOINT(cancelpoint_connect, __nothing)
    896  1.1  riastrad TEST_CANCELPOINT(cancelpoint_creat, __nothing)
    897  1.1  riastrad TEST_CANCELPOINT(cancelpoint_fcntl_F_SETLKW, __nothing)
    898  1.1  riastrad TEST_CANCELPOINT(cancelpoint_fcntl_F_OFD_SETLKW, __nothing)
    899  1.1  riastrad TEST_CANCELPOINT(cancelpoint_fdatasync, __nothing)
    900  1.1  riastrad TEST_CANCELPOINT(cancelpoint_fsync, __nothing)
    901  1.4  riastrad TEST_CANCELPOINT(cancelpoint_kevent, __nothing)
    902  1.1  riastrad TEST_CANCELPOINT(cancelpoint_lockf_F_LOCK, __nothing)
    903  1.1  riastrad TEST_CANCELPOINT(cancelpoint_mq_receive, __nothing)
    904  1.1  riastrad TEST_CANCELPOINT(cancelpoint_mq_send, __nothing)
    905  1.1  riastrad TEST_CANCELPOINT(cancelpoint_mq_timedreceive, __nothing)
    906  1.1  riastrad TEST_CANCELPOINT(cancelpoint_mq_timedsend, __nothing)
    907  1.1  riastrad TEST_CANCELPOINT(cancelpoint_msgrcv, __nothing)
    908  1.1  riastrad TEST_CANCELPOINT(cancelpoint_msgsnd, __nothing)
    909  1.1  riastrad TEST_CANCELPOINT(cancelpoint_msync, __nothing)
    910  1.1  riastrad TEST_CANCELPOINT(cancelpoint_nanosleep, __nothing)
    911  1.1  riastrad TEST_CANCELPOINT(cancelpoint_open, __nothing)
    912  1.1  riastrad TEST_CANCELPOINT(cancelpoint_openat, __nothing)
    913  1.1  riastrad TEST_CANCELPOINT(cancelpoint_pause, __nothing)
    914  1.1  riastrad TEST_CANCELPOINT(cancelpoint_poll, __nothing)
    915  1.1  riastrad TEST_CANCELPOINT(cancelpoint_posix_close, __nothing)
    916  1.1  riastrad TEST_CANCELPOINT(cancelpoint_ppoll, __nothing)
    917  1.1  riastrad TEST_CANCELPOINT(cancelpoint_pread, __nothing)
    918  1.1  riastrad TEST_CANCELPOINT(cancelpoint_pselect, __nothing)
    919  1.1  riastrad TEST_CANCELPOINT(cancelpoint_pthread_cond_clockwait, __nothing)
    920  1.1  riastrad TEST_CANCELPOINT(cancelpoint_pthread_cond_timedwait, __nothing)
    921  1.1  riastrad TEST_CANCELPOINT(cancelpoint_pthread_cond_wait, __nothing)
    922  1.1  riastrad TEST_CANCELPOINT(cancelpoint_pthread_join, __nothing)
    923  1.1  riastrad TEST_CANCELPOINT(cancelpoint_pthread_testcancel, __nothing)
    924  1.1  riastrad TEST_CANCELPOINT(cancelpoint_pwrite, __nothing)
    925  1.1  riastrad TEST_CANCELPOINT(cancelpoint_read, __nothing)
    926  1.1  riastrad TEST_CANCELPOINT(cancelpoint_readv, __nothing)
    927  1.1  riastrad TEST_CANCELPOINT(cancelpoint_recv, __nothing)
    928  1.1  riastrad TEST_CANCELPOINT(cancelpoint_recvfrom, __nothing)
    929  1.1  riastrad TEST_CANCELPOINT(cancelpoint_recvmsg, __nothing)
    930  1.1  riastrad TEST_CANCELPOINT(cancelpoint_select, __nothing)
    931  1.1  riastrad TEST_CANCELPOINT(cancelpoint_send, __nothing)
    932  1.1  riastrad TEST_CANCELPOINT(cancelpoint_sendto, __nothing)
    933  1.1  riastrad TEST_CANCELPOINT(cancelpoint_sendmsg, __nothing)
    934  1.1  riastrad TEST_CANCELPOINT(cancelpoint_sigsuspend, __nothing)
    935  1.1  riastrad TEST_CANCELPOINT(cancelpoint_sigtimedwait, __nothing)
    936  1.1  riastrad TEST_CANCELPOINT(cancelpoint_sigwait, __nothing)
    937  1.1  riastrad TEST_CANCELPOINT(cancelpoint_sigwaitinfo, __nothing)
    938  1.1  riastrad TEST_CANCELPOINT(cancelpoint_sleep, __nothing)
    939  1.3  riastrad TEST_CANCELPOINT(cancelpoint_tcdrain, __nothing)
    940  1.1  riastrad TEST_CANCELPOINT(cancelpoint_thrd_join, __nothing)
    941  1.1  riastrad TEST_CANCELPOINT(cancelpoint_thrd_sleep, __nothing)
    942  1.1  riastrad TEST_CANCELPOINT(cancelpoint_wait, __nothing)
    943  1.1  riastrad TEST_CANCELPOINT(cancelpoint_waitid, __nothing)
    944  1.1  riastrad TEST_CANCELPOINT(cancelpoint_waitpid, __nothing)
    945  1.1  riastrad TEST_CANCELPOINT(cancelpoint_write, __nothing)
    946  1.1  riastrad TEST_CANCELPOINT(cancelpoint_writev, __nothing)
    947  1.1  riastrad 
    948  1.1  riastrad ATF_TC(cleanuppop0);
    949  1.1  riastrad ATF_TC_HEAD(cleanuppop0, tc)
    950  1.1  riastrad {
    951  1.1  riastrad 	atf_tc_set_md_var(tc, "descr", "Test pthread_cleanup_pop(0)");
    952  1.1  riastrad }
    953  1.1  riastrad ATF_TC_BODY(cleanuppop0, tc)
    954  1.1  riastrad {
    955  1.1  riastrad 
    956  1.1  riastrad 	pthread_cleanup_push(&cleanup, &cleanup_done);
    957  1.1  riastrad 	pthread_cleanup_pop(/*execute*/0);
    958  1.1  riastrad 	ATF_CHECK(!cleanup_done);
    959  1.1  riastrad }
    960  1.1  riastrad 
    961  1.1  riastrad ATF_TC(cleanuppop1);
    962  1.1  riastrad ATF_TC_HEAD(cleanuppop1, tc)
    963  1.1  riastrad {
    964  1.1  riastrad 	atf_tc_set_md_var(tc, "descr", "Test pthread_cleanup_pop(1)");
    965  1.1  riastrad }
    966  1.1  riastrad ATF_TC_BODY(cleanuppop1, tc)
    967  1.1  riastrad {
    968  1.1  riastrad 
    969  1.1  riastrad 	pthread_cleanup_push(&cleanup, &cleanup_done);
    970  1.1  riastrad 	pthread_cleanup_pop(/*execute*/1);
    971  1.1  riastrad 	ATF_CHECK(cleanup_done);
    972  1.1  riastrad }
    973  1.1  riastrad 
    974  1.1  riastrad static void *
    975  1.1  riastrad cancelself_async(void *cookie)
    976  1.1  riastrad {
    977  1.1  riastrad 	int *n = cookie;
    978  1.1  riastrad 
    979  1.1  riastrad 	RZ(pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL));
    980  1.1  riastrad 
    981  1.1  riastrad 	pthread_cleanup_push(&cleanup, &cleanup_done);
    982  1.1  riastrad 
    983  1.1  riastrad 	*n = 1;
    984  1.1  riastrad 	RZ(pthread_cancel(pthread_self())); /* cancel */
    985  1.1  riastrad 	*n = 2;
    986  1.1  riastrad 	RZ(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL));
    987  1.1  riastrad 	pthread_testcancel();
    988  1.1  riastrad 	*n = 3;
    989  1.1  riastrad 	RZ(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL));
    990  1.1  riastrad 	pthread_testcancel();
    991  1.1  riastrad 	*n = 4;
    992  1.1  riastrad 
    993  1.1  riastrad 	pthread_cleanup_pop(/*execute*/0);
    994  1.1  riastrad 	return NULL;
    995  1.1  riastrad }
    996  1.1  riastrad 
    997  1.1  riastrad ATF_TC(cancelself_async);
    998  1.1  riastrad ATF_TC_HEAD(cancelself_async, tc)
    999  1.1  riastrad {
   1000  1.1  riastrad 	atf_tc_set_md_var(tc, "descr",
   1001  1.1  riastrad 	    "Test pthread_cancel(pthread_self()) async");
   1002  1.1  riastrad }
   1003  1.1  riastrad ATF_TC_BODY(cancelself_async, tc)
   1004  1.1  riastrad {
   1005  1.1  riastrad 	int n = 0;
   1006  1.1  riastrad 	pthread_t t;
   1007  1.1  riastrad 
   1008  1.1  riastrad 	RZ(pthread_create(&t, NULL, &cancelself_async, &n));
   1009  1.1  riastrad 
   1010  1.1  riastrad 	alarm(1);
   1011  1.1  riastrad 	RZ(pthread_join(t, NULL));
   1012  1.1  riastrad 
   1013  1.1  riastrad 	atf_tc_expect_fail("lib/59135: PTHREAD_CANCEL_ASYNCHRONOUS"
   1014  1.1  riastrad 	    " doesn't do much");
   1015  1.1  riastrad 	ATF_CHECK_MSG(n == 1, "n=%d", n);
   1016  1.1  riastrad 	atf_tc_expect_pass();
   1017  1.1  riastrad 	ATF_CHECK(cleanup_done);
   1018  1.1  riastrad }
   1019  1.1  riastrad 
   1020  1.1  riastrad static void *
   1021  1.1  riastrad cancelself_deferred(void *cookie)
   1022  1.1  riastrad {
   1023  1.1  riastrad 	int *n = cookie;
   1024  1.1  riastrad 
   1025  1.1  riastrad 	/* PTHREAD_CANCEL_DEFERRED by default */
   1026  1.1  riastrad 
   1027  1.1  riastrad 	pthread_cleanup_push(&cleanup, &cleanup_done);
   1028  1.1  riastrad 
   1029  1.1  riastrad 	*n = 1;
   1030  1.1  riastrad 	RZ(pthread_cancel(pthread_self()));
   1031  1.1  riastrad 	*n = 2;
   1032  1.1  riastrad 	RZ(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL));
   1033  1.1  riastrad 	*n = 3;
   1034  1.1  riastrad 	pthread_testcancel();
   1035  1.1  riastrad 	*n = 4;
   1036  1.1  riastrad 	RZ(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL));
   1037  1.1  riastrad 	*n = 5;
   1038  1.1  riastrad 	pthread_testcancel(); /* cancel */
   1039  1.1  riastrad 	*n = 6;
   1040  1.1  riastrad 
   1041  1.1  riastrad 	pthread_cleanup_pop(/*execute*/0);
   1042  1.1  riastrad 	return NULL;
   1043  1.1  riastrad }
   1044  1.1  riastrad 
   1045  1.1  riastrad ATF_TC(cancelself_deferred);
   1046  1.1  riastrad ATF_TC_HEAD(cancelself_deferred, tc)
   1047  1.1  riastrad {
   1048  1.1  riastrad 	atf_tc_set_md_var(tc, "descr",
   1049  1.1  riastrad 	    "Test pthread_cancel(pthread_self()) deferred");
   1050  1.1  riastrad }
   1051  1.1  riastrad ATF_TC_BODY(cancelself_deferred, tc)
   1052  1.1  riastrad {
   1053  1.1  riastrad 	int n = 0;
   1054  1.1  riastrad 	pthread_t t;
   1055  1.1  riastrad 
   1056  1.1  riastrad 	RZ(pthread_create(&t, NULL, &cancelself_deferred, &n));
   1057  1.1  riastrad 
   1058  1.1  riastrad 	alarm(1);
   1059  1.1  riastrad 	RZ(pthread_join(t, NULL));
   1060  1.1  riastrad 
   1061  1.1  riastrad 	ATF_CHECK_MSG(n == 5, "n=%d", n);
   1062  1.1  riastrad 	ATF_CHECK(cleanup_done);
   1063  1.1  riastrad }
   1064  1.1  riastrad 
   1065  1.1  riastrad static void *
   1066  1.1  riastrad defaults(void *cookie)
   1067  1.1  riastrad {
   1068  1.1  riastrad 	int state, type;
   1069  1.1  riastrad 
   1070  1.1  riastrad 	fprintf(stderr, "created thread\n");
   1071  1.1  riastrad 
   1072  1.1  riastrad 	RZ(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &state));
   1073  1.1  riastrad 	RZ(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &type));
   1074  1.1  riastrad 
   1075  1.1  riastrad 	ATF_CHECK_MSG(state == PTHREAD_CANCEL_ENABLE,
   1076  1.1  riastrad 	    "state=%d PTHREAD_CANCEL_ENABLE=%d PTHREAD_CANCEL_DISABLE=%d",
   1077  1.1  riastrad 	    state, PTHREAD_CANCEL_ENABLE, PTHREAD_CANCEL_DISABLE);
   1078  1.1  riastrad 
   1079  1.1  riastrad 	ATF_CHECK_MSG(type == PTHREAD_CANCEL_DEFERRED,
   1080  1.1  riastrad 	    "type=%d"
   1081  1.1  riastrad 	    " PTHREAD_CANCEL_DEFERRED=%d PTHREAD_CANCEL_ASYNCHRONOUS=%d",
   1082  1.1  riastrad 	    type, PTHREAD_CANCEL_DEFERRED, PTHREAD_CANCEL_ASYNCHRONOUS);
   1083  1.1  riastrad 
   1084  1.1  riastrad 	return NULL;
   1085  1.1  riastrad }
   1086  1.1  riastrad 
   1087  1.1  riastrad ATF_TC(defaults);
   1088  1.1  riastrad ATF_TC_HEAD(defaults, tc)
   1089  1.1  riastrad {
   1090  1.1  riastrad 	atf_tc_set_md_var(tc, "descr", "Test default cancelability");
   1091  1.1  riastrad }
   1092  1.1  riastrad ATF_TC_BODY(defaults, tc)
   1093  1.1  riastrad {
   1094  1.1  riastrad 	pthread_t t;
   1095  1.1  riastrad 
   1096  1.1  riastrad 	fprintf(stderr, "initial thread\n");
   1097  1.1  riastrad 	(void)defaults(NULL);
   1098  1.1  riastrad 
   1099  1.1  riastrad 	RZ(pthread_create(&t, NULL, &defaults, NULL));
   1100  1.1  riastrad 
   1101  1.1  riastrad 	alarm(1);
   1102  1.1  riastrad 	RZ(pthread_join(t, NULL));
   1103  1.1  riastrad }
   1104  1.1  riastrad 
   1105  1.1  riastrad static void *
   1106  1.1  riastrad disable_enable(void *cookie)
   1107  1.1  riastrad {
   1108  1.1  riastrad 	int *n = cookie;
   1109  1.1  riastrad 
   1110  1.1  riastrad 	pthread_cleanup_push(&cleanup, &cleanup_done);
   1111  1.1  riastrad 
   1112  1.1  riastrad 	*n = 1;
   1113  1.1  riastrad 	pthread_testcancel();
   1114  1.1  riastrad 	*n = 2;
   1115  1.1  riastrad 	RZ(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL));
   1116  1.1  riastrad 	*n = 3;
   1117  1.1  riastrad 	(void)pthread_barrier_wait(&bar);
   1118  1.1  riastrad 	*n = 4;
   1119  1.1  riastrad 	pthread_testcancel();
   1120  1.1  riastrad 	*n = 5;
   1121  1.1  riastrad 	(void)pthread_barrier_wait(&bar);
   1122  1.1  riastrad 	*n = 6;
   1123  1.1  riastrad 	pthread_testcancel();
   1124  1.1  riastrad 	*n = 7;
   1125  1.1  riastrad 	RZ(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL));
   1126  1.1  riastrad 	*n = 8;
   1127  1.1  riastrad 	pthread_testcancel(); /* cancel */
   1128  1.1  riastrad 	*n = 9;
   1129  1.1  riastrad 
   1130  1.1  riastrad 	pthread_cleanup_pop(/*execute*/0);
   1131  1.1  riastrad 	return NULL;
   1132  1.1  riastrad }
   1133  1.1  riastrad 
   1134  1.1  riastrad ATF_TC(disable_enable);
   1135  1.1  riastrad ATF_TC_HEAD(disable_enable, tc)
   1136  1.1  riastrad {
   1137  1.1  riastrad 	atf_tc_set_md_var(tc, "descr",
   1138  1.1  riastrad 	    "Test disabling and re-enabling cancellation");
   1139  1.1  riastrad }
   1140  1.1  riastrad ATF_TC_BODY(disable_enable, tc)
   1141  1.1  riastrad {
   1142  1.1  riastrad 	int n = 0;
   1143  1.1  riastrad 	pthread_t t;
   1144  1.1  riastrad 
   1145  1.1  riastrad 	RZ(pthread_barrier_init(&bar, NULL, 2));
   1146  1.1  riastrad 
   1147  1.1  riastrad 	RZ(pthread_create(&t, NULL, &disable_enable, &n));
   1148  1.1  riastrad 
   1149  1.1  riastrad 	(void)pthread_barrier_wait(&bar);
   1150  1.1  riastrad 	RZ(pthread_cancel(t));
   1151  1.1  riastrad 	(void)pthread_barrier_wait(&bar);
   1152  1.1  riastrad 
   1153  1.1  riastrad 	alarm(1);
   1154  1.1  riastrad 	RZ(pthread_join(t, NULL));
   1155  1.1  riastrad 
   1156  1.1  riastrad 	ATF_CHECK_MSG(n == 8, "n=%d", n);
   1157  1.1  riastrad 	ATF_CHECK(cleanup_done);
   1158  1.1  riastrad }
   1159  1.1  riastrad 
   1160  1.1  riastrad static void *
   1161  1.1  riastrad notestcancel_loop_async(void *cookie)
   1162  1.1  riastrad {
   1163  1.1  riastrad 
   1164  1.1  riastrad 	RZ(pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL));
   1165  1.1  riastrad 
   1166  1.1  riastrad 	pthread_cleanup_push(&cleanup, &cleanup_done);
   1167  1.1  riastrad 	(void)pthread_barrier_wait(&bar);
   1168  1.1  riastrad 	for (;;)
   1169  1.1  riastrad 		__insn_barrier();
   1170  1.1  riastrad 	pthread_cleanup_pop(/*execute*/0);
   1171  1.1  riastrad 
   1172  1.1  riastrad 	return NULL;
   1173  1.1  riastrad }
   1174  1.1  riastrad 
   1175  1.1  riastrad ATF_TC(notestcancel_loop_async);
   1176  1.1  riastrad ATF_TC_HEAD(notestcancel_loop_async, tc)
   1177  1.1  riastrad {
   1178  1.1  riastrad 	atf_tc_set_md_var(tc, "descr",
   1179  1.1  riastrad 	    "Test nothing in a loop with PTHREAD_CANCEL_ASYNCHRONOUS");
   1180  1.1  riastrad }
   1181  1.1  riastrad ATF_TC_BODY(notestcancel_loop_async, tc)
   1182  1.1  riastrad {
   1183  1.1  riastrad 	pthread_t t;
   1184  1.1  riastrad 	void *result;
   1185  1.1  riastrad 
   1186  1.1  riastrad 	RZ(pthread_barrier_init(&bar, NULL, 2));
   1187  1.1  riastrad 	RZ(pthread_create(&t, NULL, &notestcancel_loop_async, NULL));
   1188  1.1  riastrad 
   1189  1.1  riastrad 	(void)pthread_barrier_wait(&bar);
   1190  1.1  riastrad 	RZ(pthread_cancel(t));
   1191  1.1  riastrad 
   1192  1.1  riastrad 	atf_tc_expect_signal(SIGALRM, "lib/59135: PTHREAD_CANCEL_ASYNCHRONOUS"
   1193  1.1  riastrad 	    " doesn't do much");
   1194  1.1  riastrad 	alarm(1);
   1195  1.1  riastrad 	RZ(pthread_join(t, &result));
   1196  1.1  riastrad 	ATF_CHECK_MSG(result == PTHREAD_CANCELED,
   1197  1.1  riastrad 	    "result=%p PTHREAD_CANCELED=%p", result, PTHREAD_CANCELED);
   1198  1.1  riastrad 	ATF_CHECK(cleanup_done);
   1199  1.1  riastrad }
   1200  1.1  riastrad 
   1201  1.1  riastrad static void *
   1202  1.1  riastrad disable_enable_async(void *cookie)
   1203  1.1  riastrad {
   1204  1.1  riastrad 	int *n = cookie;
   1205  1.1  riastrad 
   1206  1.1  riastrad 	pthread_cleanup_push(&cleanup, &cleanup_done);
   1207  1.1  riastrad 
   1208  1.1  riastrad 	RZ(pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL));
   1209  1.1  riastrad 
   1210  1.1  riastrad 	*n = 1;
   1211  1.1  riastrad 	pthread_testcancel();
   1212  1.1  riastrad 	*n = 2;
   1213  1.1  riastrad 	RZ(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL));
   1214  1.1  riastrad 	*n = 3;
   1215  1.1  riastrad 	(void)pthread_barrier_wait(&bar);
   1216  1.1  riastrad 	*n = 4;
   1217  1.1  riastrad 	pthread_testcancel();
   1218  1.1  riastrad 	*n = 5;
   1219  1.1  riastrad 	(void)pthread_barrier_wait(&bar);
   1220  1.1  riastrad 	*n = 6;
   1221  1.1  riastrad 	pthread_testcancel();
   1222  1.1  riastrad 	*n = 7;
   1223  1.1  riastrad 	RZ(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL)); /* cancel */
   1224  1.1  riastrad 	*n = 8;
   1225  1.1  riastrad 	pthread_testcancel();
   1226  1.1  riastrad 	*n = 9;
   1227  1.1  riastrad 
   1228  1.1  riastrad 	pthread_cleanup_pop(/*execute*/0);
   1229  1.1  riastrad 	return NULL;
   1230  1.1  riastrad }
   1231  1.1  riastrad 
   1232  1.1  riastrad ATF_TC(disable_enable_async);
   1233  1.1  riastrad ATF_TC_HEAD(disable_enable_async, tc)
   1234  1.1  riastrad {
   1235  1.1  riastrad 	atf_tc_set_md_var(tc, "descr",
   1236  1.1  riastrad 	    "Test disabling and re-enabling cancellation when asynchronous");
   1237  1.1  riastrad }
   1238  1.1  riastrad ATF_TC_BODY(disable_enable_async, tc)
   1239  1.1  riastrad {
   1240  1.1  riastrad 	int n = 0;
   1241  1.1  riastrad 	pthread_t t;
   1242  1.1  riastrad 
   1243  1.1  riastrad 	RZ(pthread_barrier_init(&bar, NULL, 2));
   1244  1.1  riastrad 
   1245  1.1  riastrad 	RZ(pthread_create(&t, NULL, &disable_enable_async, &n));
   1246  1.1  riastrad 
   1247  1.1  riastrad 	(void)pthread_barrier_wait(&bar);
   1248  1.1  riastrad 	RZ(pthread_cancel(t));
   1249  1.1  riastrad 	(void)pthread_barrier_wait(&bar);
   1250  1.1  riastrad 
   1251  1.1  riastrad 	alarm(1);
   1252  1.1  riastrad 	RZ(pthread_join(t, NULL));
   1253  1.1  riastrad 
   1254  1.1  riastrad 	ATF_CHECK_MSG(n == 7, "n=%d", n);
   1255  1.1  riastrad 	ATF_CHECK(cleanup_done);
   1256  1.1  riastrad }
   1257  1.1  riastrad 
   1258  1.1  riastrad static void *
   1259  1.1  riastrad disable_enable_setcanceltype_async(void *cookie)
   1260  1.1  riastrad {
   1261  1.1  riastrad 	int *n = cookie;
   1262  1.1  riastrad 
   1263  1.1  riastrad 	pthread_cleanup_push(&cleanup, &cleanup_done);
   1264  1.1  riastrad 
   1265  1.1  riastrad 	*n = 1;
   1266  1.1  riastrad 	pthread_testcancel();
   1267  1.1  riastrad 	*n = 2;
   1268  1.1  riastrad 	RZ(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL));
   1269  1.1  riastrad 	*n = 3;
   1270  1.1  riastrad 	(void)pthread_barrier_wait(&bar);
   1271  1.1  riastrad 	*n = 4;
   1272  1.1  riastrad 	pthread_testcancel();
   1273  1.1  riastrad 	*n = 5;
   1274  1.1  riastrad 	(void)pthread_barrier_wait(&bar);
   1275  1.1  riastrad 	*n = 6;
   1276  1.1  riastrad 	pthread_testcancel();
   1277  1.1  riastrad 	*n = 7;
   1278  1.1  riastrad 	RZ(pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL));
   1279  1.1  riastrad 	*n = 8;
   1280  1.1  riastrad 	pthread_testcancel();
   1281  1.1  riastrad 	*n = 9;
   1282  1.1  riastrad 	RZ(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL)); /* cancel */
   1283  1.1  riastrad 	*n = 10;
   1284  1.1  riastrad 	pthread_testcancel();
   1285  1.1  riastrad 	*n = 11;
   1286  1.1  riastrad 
   1287  1.1  riastrad 	pthread_cleanup_pop(/*execute*/0);
   1288  1.1  riastrad 	return NULL;
   1289  1.1  riastrad }
   1290  1.1  riastrad 
   1291  1.1  riastrad ATF_TC(disable_enable_setcanceltype_async);
   1292  1.1  riastrad ATF_TC_HEAD(disable_enable_setcanceltype_async, tc)
   1293  1.1  riastrad {
   1294  1.1  riastrad 	atf_tc_set_md_var(tc, "descr",
   1295  1.1  riastrad 	    "Test disabling cancellation, setting it async, and re-enabling");
   1296  1.1  riastrad }
   1297  1.1  riastrad ATF_TC_BODY(disable_enable_setcanceltype_async, tc)
   1298  1.1  riastrad {
   1299  1.1  riastrad 	int n = 0;
   1300  1.1  riastrad 	pthread_t t;
   1301  1.1  riastrad 
   1302  1.1  riastrad 	RZ(pthread_barrier_init(&bar, NULL, 2));
   1303  1.1  riastrad 
   1304  1.1  riastrad 	RZ(pthread_create(&t, NULL, &disable_enable_setcanceltype_async, &n));
   1305  1.1  riastrad 
   1306  1.1  riastrad 	(void)pthread_barrier_wait(&bar);
   1307  1.1  riastrad 	RZ(pthread_cancel(t));
   1308  1.1  riastrad 	(void)pthread_barrier_wait(&bar);
   1309  1.1  riastrad 
   1310  1.1  riastrad 	alarm(1);
   1311  1.1  riastrad 	RZ(pthread_join(t, NULL));
   1312  1.1  riastrad 
   1313  1.1  riastrad 	ATF_CHECK_MSG(n == 9, "n=%d", n);
   1314  1.1  riastrad 	ATF_CHECK(cleanup_done);
   1315  1.1  riastrad }
   1316  1.1  riastrad 
   1317  1.1  riastrad static void *
   1318  1.1  riastrad setcanceltype_async(void *cookie)
   1319  1.1  riastrad {
   1320  1.1  riastrad 	int *n = cookie;
   1321  1.1  riastrad 
   1322  1.1  riastrad 	pthread_cleanup_push(&cleanup, &cleanup_done);
   1323  1.1  riastrad 
   1324  1.1  riastrad 	*n = 1;
   1325  1.1  riastrad 	pthread_testcancel();
   1326  1.1  riastrad 	*n = 2;
   1327  1.1  riastrad 	(void)pthread_barrier_wait(&bar);
   1328  1.1  riastrad 	*n = 3;
   1329  1.1  riastrad 	(void)pthread_barrier_wait(&bar);
   1330  1.1  riastrad 	*n = 4;
   1331  1.1  riastrad 	RZ(pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,
   1332  1.1  riastrad 		NULL)); /* cancel */
   1333  1.1  riastrad 	*n = 5;
   1334  1.1  riastrad 
   1335  1.1  riastrad 	pthread_cleanup_pop(/*execute*/0);
   1336  1.1  riastrad 	return NULL;
   1337  1.1  riastrad }
   1338  1.1  riastrad 
   1339  1.1  riastrad ATF_TC(setcanceltype_async);
   1340  1.1  riastrad ATF_TC_HEAD(setcanceltype_async, tc)
   1341  1.1  riastrad {
   1342  1.1  riastrad 	atf_tc_set_md_var(tc, "descr",
   1343  1.1  riastrad 	    "Test disabling cancellation, setting it async, and re-enabling");
   1344  1.1  riastrad }
   1345  1.1  riastrad ATF_TC_BODY(setcanceltype_async, tc)
   1346  1.1  riastrad {
   1347  1.1  riastrad 	int n = 0;
   1348  1.1  riastrad 	pthread_t t;
   1349  1.1  riastrad 
   1350  1.1  riastrad 	RZ(pthread_barrier_init(&bar, NULL, 2));
   1351  1.1  riastrad 
   1352  1.1  riastrad 	RZ(pthread_create(&t, NULL, &setcanceltype_async, &n));
   1353  1.1  riastrad 
   1354  1.1  riastrad 	(void)pthread_barrier_wait(&bar);
   1355  1.1  riastrad 	RZ(pthread_cancel(t));
   1356  1.1  riastrad 	(void)pthread_barrier_wait(&bar);
   1357  1.1  riastrad 
   1358  1.1  riastrad 	alarm(1);
   1359  1.1  riastrad 	RZ(pthread_join(t, NULL));
   1360  1.1  riastrad 
   1361  1.1  riastrad 	ATF_CHECK_MSG(n == 4, "n=%d", n);
   1362  1.1  riastrad 	ATF_CHECK(cleanup_done);
   1363  1.1  riastrad }
   1364  1.1  riastrad 
   1365  1.1  riastrad static void
   1366  1.1  riastrad sighandler(int signo)
   1367  1.1  riastrad {
   1368  1.1  riastrad 	int state;
   1369  1.1  riastrad 
   1370  1.1  riastrad 	RZ(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &state));
   1371  1.1  riastrad 	RZ(pthread_setcancelstate(state, NULL));
   1372  1.1  riastrad }
   1373  1.1  riastrad 
   1374  1.1  riastrad static void *
   1375  1.1  riastrad sigsafecancelstate(void *cookie)
   1376  1.1  riastrad {
   1377  1.1  riastrad 	atomic_ulong *n = cookie;
   1378  1.1  riastrad 	char name[128];
   1379  1.1  riastrad 
   1380  1.1  riastrad 	pthread_cleanup_push(&cleanup, &cleanup_done);
   1381  1.1  riastrad 	REQUIRE_LIBC(signal(SIGUSR1, &sighandler), SIG_ERR);
   1382  1.1  riastrad 
   1383  1.1  riastrad 	(void)pthread_barrier_wait(&bar);
   1384  1.1  riastrad 
   1385  1.1  riastrad 	while (atomic_load_explicit(n, memory_order_relaxed) != 0) {
   1386  1.1  riastrad 		/*
   1387  1.1  riastrad 		 * Do some things that might take the same lock as
   1388  1.1  riastrad 		 * pthread_setcancelstate.
   1389  1.1  riastrad 		 */
   1390  1.1  riastrad 		RZ(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL));
   1391  1.1  riastrad 		RZ(pthread_getname_np(pthread_self(), name, sizeof(name)));
   1392  1.1  riastrad 		RZ(pthread_setname_np(pthread_self(), "%s", name));
   1393  1.1  riastrad 	}
   1394  1.1  riastrad 
   1395  1.1  riastrad 	pthread_cleanup_pop(/*execute*/1);
   1396  1.1  riastrad 	return NULL;
   1397  1.1  riastrad }
   1398  1.1  riastrad 
   1399  1.1  riastrad ATF_TC(sigsafecancelstate);
   1400  1.1  riastrad ATF_TC_HEAD(sigsafecancelstate, tc)
   1401  1.1  riastrad {
   1402  1.1  riastrad 	atf_tc_set_md_var(tc, "descr",
   1403  1.1  riastrad 	    "Test pthread_setcancelstate async-signal-safety");
   1404  1.1  riastrad }
   1405  1.1  riastrad ATF_TC_BODY(sigsafecancelstate, tc)
   1406  1.1  riastrad {
   1407  1.1  riastrad 	pthread_t t;
   1408  1.1  riastrad 	atomic_ulong n = 10000;
   1409  1.1  riastrad 	void *result;
   1410  1.1  riastrad 
   1411  1.1  riastrad 	RZ(pthread_barrier_init(&bar, NULL, 2));
   1412  1.1  riastrad 	RZ(pthread_create(&t, NULL, &sigsafecancelstate, &n));
   1413  1.1  riastrad 
   1414  1.1  riastrad 	(void)pthread_barrier_wait(&bar);
   1415  1.1  riastrad 
   1416  1.1  riastrad 	while (atomic_load_explicit(&n, memory_order_relaxed)) {
   1417  1.1  riastrad 		pthread_kill(t, SIGUSR1);
   1418  1.1  riastrad 		atomic_store_explicit(&n,
   1419  1.1  riastrad 		    atomic_load_explicit(&n, memory_order_relaxed) - 1,
   1420  1.1  riastrad 		    memory_order_relaxed);
   1421  1.1  riastrad 	}
   1422  1.1  riastrad 
   1423  1.1  riastrad 	alarm(1);
   1424  1.1  riastrad 	RZ(pthread_join(t, &result));
   1425  1.1  riastrad 	ATF_CHECK_MSG(result == NULL, "result=%p", result);
   1426  1.1  riastrad 	ATF_CHECK(cleanup_done);
   1427  1.1  riastrad }
   1428  1.1  riastrad 
   1429  1.1  riastrad static void *
   1430  1.1  riastrad testcancel_loop(void *cookie)
   1431  1.1  riastrad {
   1432  1.1  riastrad 
   1433  1.1  riastrad 	pthread_cleanup_push(&cleanup, &cleanup_done);
   1434  1.1  riastrad 	(void)pthread_barrier_wait(&bar);
   1435  1.1  riastrad 	for (;;)
   1436  1.1  riastrad 		pthread_testcancel();
   1437  1.1  riastrad 	pthread_cleanup_pop(/*execute*/0);
   1438  1.1  riastrad 
   1439  1.1  riastrad 	return NULL;
   1440  1.1  riastrad }
   1441  1.1  riastrad 
   1442  1.1  riastrad ATF_TC(testcancel_loop);
   1443  1.1  riastrad ATF_TC_HEAD(testcancel_loop, tc)
   1444  1.1  riastrad {
   1445  1.1  riastrad 	atf_tc_set_md_var(tc, "descr",
   1446  1.1  riastrad 	    "Test pthread_testcancel in a loop");
   1447  1.1  riastrad }
   1448  1.1  riastrad ATF_TC_BODY(testcancel_loop, tc)
   1449  1.1  riastrad {
   1450  1.1  riastrad 	pthread_t t;
   1451  1.1  riastrad 	void *result;
   1452  1.1  riastrad 
   1453  1.1  riastrad 	RZ(pthread_barrier_init(&bar, NULL, 2));
   1454  1.1  riastrad 	RZ(pthread_create(&t, NULL, &testcancel_loop, NULL));
   1455  1.1  riastrad 
   1456  1.1  riastrad 	(void)pthread_barrier_wait(&bar);
   1457  1.1  riastrad 	RZ(pthread_cancel(t));
   1458  1.1  riastrad 
   1459  1.1  riastrad 	alarm(1);
   1460  1.1  riastrad 	RZ(pthread_join(t, &result));
   1461  1.1  riastrad 	ATF_CHECK_MSG(result == PTHREAD_CANCELED,
   1462  1.1  riastrad 	    "result=%p PTHREAD_CANCELED=%p", result, PTHREAD_CANCELED);
   1463  1.1  riastrad 	ATF_CHECK(cleanup_done);
   1464  1.1  riastrad }
   1465  1.1  riastrad 
   1466  1.1  riastrad ATF_TP_ADD_TCS(tp)
   1467  1.1  riastrad {
   1468  1.1  riastrad 
   1469  1.1  riastrad 	ADD_TEST_CANCELPOINT(cancelpoint_accept);
   1470  1.1  riastrad 	ADD_TEST_CANCELPOINT(cancelpoint_accept4);
   1471  1.1  riastrad 	ADD_TEST_CANCELPOINT(cancelpoint_aio_suspend);
   1472  1.1  riastrad 	ADD_TEST_CANCELPOINT(cancelpoint_clock_nanosleep);
   1473  1.1  riastrad 	ADD_TEST_CANCELPOINT(cancelpoint_close);
   1474  1.1  riastrad 	ADD_TEST_CANCELPOINT(cancelpoint_cnd_timedwait);
   1475  1.1  riastrad 	ADD_TEST_CANCELPOINT(cancelpoint_cnd_wait);
   1476  1.1  riastrad 	ADD_TEST_CANCELPOINT(cancelpoint_connect);
   1477  1.1  riastrad 	ADD_TEST_CANCELPOINT(cancelpoint_creat);
   1478  1.1  riastrad 	ADD_TEST_CANCELPOINT(cancelpoint_fcntl_F_SETLKW);
   1479  1.1  riastrad 	ADD_TEST_CANCELPOINT(cancelpoint_fcntl_F_OFD_SETLKW);
   1480  1.1  riastrad 	ADD_TEST_CANCELPOINT(cancelpoint_fdatasync);
   1481  1.1  riastrad 	ADD_TEST_CANCELPOINT(cancelpoint_fsync);
   1482  1.4  riastrad 	ADD_TEST_CANCELPOINT(cancelpoint_kevent);
   1483  1.1  riastrad 	ADD_TEST_CANCELPOINT(cancelpoint_lockf_F_LOCK);
   1484  1.1  riastrad 	ADD_TEST_CANCELPOINT(cancelpoint_mq_receive);
   1485  1.1  riastrad 	ADD_TEST_CANCELPOINT(cancelpoint_mq_send);
   1486  1.1  riastrad 	ADD_TEST_CANCELPOINT(cancelpoint_mq_timedreceive);
   1487  1.1  riastrad 	ADD_TEST_CANCELPOINT(cancelpoint_mq_timedsend);
   1488  1.1  riastrad 	ADD_TEST_CANCELPOINT(cancelpoint_msgrcv);
   1489  1.1  riastrad 	ADD_TEST_CANCELPOINT(cancelpoint_msgsnd);
   1490  1.1  riastrad 	ADD_TEST_CANCELPOINT(cancelpoint_msync);
   1491  1.1  riastrad 	ADD_TEST_CANCELPOINT(cancelpoint_nanosleep);
   1492  1.1  riastrad 	ADD_TEST_CANCELPOINT(cancelpoint_open);
   1493  1.1  riastrad 	ADD_TEST_CANCELPOINT(cancelpoint_openat);
   1494  1.1  riastrad 	ADD_TEST_CANCELPOINT(cancelpoint_pause);
   1495  1.1  riastrad 	ADD_TEST_CANCELPOINT(cancelpoint_poll);
   1496  1.1  riastrad 	ADD_TEST_CANCELPOINT(cancelpoint_posix_close);
   1497  1.1  riastrad 	ADD_TEST_CANCELPOINT(cancelpoint_ppoll);
   1498  1.1  riastrad 	ADD_TEST_CANCELPOINT(cancelpoint_pread);
   1499  1.1  riastrad 	ADD_TEST_CANCELPOINT(cancelpoint_pselect);
   1500  1.1  riastrad 	ADD_TEST_CANCELPOINT(cancelpoint_pthread_cond_clockwait);
   1501  1.1  riastrad 	ADD_TEST_CANCELPOINT(cancelpoint_pthread_cond_timedwait);
   1502  1.1  riastrad 	ADD_TEST_CANCELPOINT(cancelpoint_pthread_cond_wait);
   1503  1.1  riastrad 	ADD_TEST_CANCELPOINT(cancelpoint_pthread_join);
   1504  1.1  riastrad 	ADD_TEST_CANCELPOINT(cancelpoint_pthread_testcancel);
   1505  1.1  riastrad 	ADD_TEST_CANCELPOINT(cancelpoint_pwrite);
   1506  1.1  riastrad 	ADD_TEST_CANCELPOINT(cancelpoint_read);
   1507  1.1  riastrad 	ADD_TEST_CANCELPOINT(cancelpoint_readv);
   1508  1.1  riastrad 	ADD_TEST_CANCELPOINT(cancelpoint_recv);
   1509  1.1  riastrad 	ADD_TEST_CANCELPOINT(cancelpoint_recvfrom);
   1510  1.1  riastrad 	ADD_TEST_CANCELPOINT(cancelpoint_recvmsg);
   1511  1.1  riastrad 	ADD_TEST_CANCELPOINT(cancelpoint_select);
   1512  1.1  riastrad 	ADD_TEST_CANCELPOINT(cancelpoint_send);
   1513  1.1  riastrad 	ADD_TEST_CANCELPOINT(cancelpoint_sendto);
   1514  1.1  riastrad 	ADD_TEST_CANCELPOINT(cancelpoint_sendmsg);
   1515  1.1  riastrad 	ADD_TEST_CANCELPOINT(cancelpoint_sigsuspend);
   1516  1.1  riastrad 	ADD_TEST_CANCELPOINT(cancelpoint_sigtimedwait);
   1517  1.1  riastrad 	ADD_TEST_CANCELPOINT(cancelpoint_sigwait);
   1518  1.1  riastrad 	ADD_TEST_CANCELPOINT(cancelpoint_sigwaitinfo);
   1519  1.1  riastrad 	ADD_TEST_CANCELPOINT(cancelpoint_sleep);
   1520  1.1  riastrad 	ADD_TEST_CANCELPOINT(cancelpoint_tcdrain);
   1521  1.1  riastrad 	ADD_TEST_CANCELPOINT(cancelpoint_thrd_join);
   1522  1.1  riastrad 	ADD_TEST_CANCELPOINT(cancelpoint_thrd_sleep);
   1523  1.1  riastrad 	ADD_TEST_CANCELPOINT(cancelpoint_wait);
   1524  1.1  riastrad 	ADD_TEST_CANCELPOINT(cancelpoint_waitid);
   1525  1.1  riastrad 	ADD_TEST_CANCELPOINT(cancelpoint_waitpid);
   1526  1.1  riastrad 	ADD_TEST_CANCELPOINT(cancelpoint_write);
   1527  1.1  riastrad 	ADD_TEST_CANCELPOINT(cancelpoint_writev);
   1528  1.1  riastrad 
   1529  1.1  riastrad 	ATF_TP_ADD_TC(tp, cleanuppop0);
   1530  1.1  riastrad 	ATF_TP_ADD_TC(tp, cleanuppop1);
   1531  1.1  riastrad 	ATF_TP_ADD_TC(tp, cancelself_async);
   1532  1.1  riastrad 	ATF_TP_ADD_TC(tp, cancelself_deferred);
   1533  1.1  riastrad 	ATF_TP_ADD_TC(tp, defaults);
   1534  1.1  riastrad 	ATF_TP_ADD_TC(tp, disable_enable);
   1535  1.1  riastrad 	ATF_TP_ADD_TC(tp, disable_enable_async);
   1536  1.1  riastrad 	ATF_TP_ADD_TC(tp, disable_enable_setcanceltype_async);
   1537  1.1  riastrad 	ATF_TP_ADD_TC(tp, setcanceltype_async);
   1538  1.1  riastrad 	ATF_TP_ADD_TC(tp, notestcancel_loop_async);
   1539  1.1  riastrad 	ATF_TP_ADD_TC(tp, sigsafecancelstate);
   1540  1.1  riastrad 	ATF_TP_ADD_TC(tp, testcancel_loop);
   1541  1.1  riastrad 
   1542  1.1  riastrad 	return atf_no_error();
   1543  1.1  riastrad }
   1544