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