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