pthread_cancelstub.c revision 1.46 1 /* $NetBSD: pthread_cancelstub.c,v 1.46 2025/03/31 14:07:10 riastradh Exp $ */
2
3 /*-
4 * Copyright (c) 2002, 2007 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Nathan J. Williams and Andrew Doran.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /* Disable namespace mangling, Fortification is useless here anyway. */
33 #undef _FORTIFY_SOURCE
34
35 #include <sys/cdefs.h>
36 __RCSID("$NetBSD: pthread_cancelstub.c,v 1.46 2025/03/31 14:07:10 riastradh Exp $");
37
38 /* Need to use libc-private names for atomic operations. */
39 #include "../../common/lib/libc/atomic/atomic_op_namespace.h"
40
41 #ifndef lint
42
43
44 /*
45 * This is necessary because the names are always weak (they are not
46 * POSIX functions).
47 */
48 #define fsync_range _fsync_range
49 #define pollts _pollts
50
51 /*
52 * XXX this is necessary to get the prototypes for the __sigsuspend14
53 * XXX and __msync13 internal names, instead of the application-visible
54 * XXX sigsuspend and msync names. It's kind of gross, but we're pretty
55 * XXX intimate with libc already.
56 */
57 #define __LIBC12_SOURCE__
58
59 #include <sys/msg.h>
60 #include <sys/types.h>
61 #include <sys/uio.h>
62 #include <sys/wait.h>
63 #include <aio.h>
64 #include <errno.h>
65 #include <fcntl.h>
66 #include <mqueue.h>
67 #include <poll.h>
68 #include <stdatomic.h>
69 #include <stdarg.h>
70 #include <unistd.h>
71
72 #include <signal.h>
73 #include <sys/mman.h>
74 #include <sys/select.h>
75 #include <sys/socket.h>
76 #include <sys/event.h>
77 #include <sys/resource.h>
78
79 #include <compat/sys/mman.h>
80 #include <compat/sys/poll.h>
81 #include <compat/sys/select.h>
82 #include <compat/sys/event.h>
83 #include <compat/sys/wait.h>
84 #include <compat/sys/resource.h>
85 #include <compat/include/mqueue.h>
86 #include <compat/include/signal.h>
87
88 #include "pthread.h"
89 #include "pthread_int.h"
90 #include "reentrant.h"
91
92 #define atomic_load_relaxed(p) \
93 atomic_load_explicit(p, memory_order_relaxed)
94
95 int pthread__cancel_stub_binder;
96
97 int _sys_accept(int, struct sockaddr *, socklen_t *);
98 int _sys___aio_suspend50(const struct aiocb * const [], int,
99 const struct timespec *);
100 int __aio_suspend50(const struct aiocb * const [], int,
101 const struct timespec *);
102 int _sys_clock_nanosleep(clockid_t clock_id, int flags,
103 const struct timespec *rqtp, struct timespec *rmtp);
104 int _sys_close(int);
105 int _sys_connect(int, const struct sockaddr *, socklen_t);
106 int _sys_fcntl(int, int, ...);
107 int _sys_fdatasync(int);
108 int _sys_fsync(int);
109 int _sys_fsync_range(int, int, off_t, off_t);
110 int _sys___kevent100(int, const struct kevent *, size_t, struct kevent *,
111 size_t, const struct timespec *);
112 int _sys_mq_send(mqd_t, const char *, size_t, unsigned);
113 ssize_t _sys_mq_receive(mqd_t, char *, size_t, unsigned *);
114 int _sys___mq_timedsend50(mqd_t, const char *, size_t, unsigned,
115 const struct timespec *);
116 ssize_t _sys___mq_timedreceive50(mqd_t, char *, size_t, unsigned *,
117 const struct timespec *);
118 ssize_t _sys_msgrcv(int, void *, size_t, long, int);
119 int _sys_msgsnd(int, const void *, size_t, int);
120 int _sys___msync13(void *, size_t, int);
121 int _sys___nanosleep50(const struct timespec *, struct timespec *);
122 int __nanosleep50(const struct timespec *, struct timespec *);
123 int _sys_open(const char *, int, ...);
124 int _sys_openat(int, const char *, int, ...);
125 int _sys_poll(struct pollfd *, nfds_t, int);
126 int _sys___pollts50(struct pollfd *, nfds_t, const struct timespec *,
127 const sigset_t *);
128 ssize_t _sys_pread(int, void *, size_t, off_t);
129 int _sys___pselect50(int, fd_set *, fd_set *, fd_set *,
130 const struct timespec *, const sigset_t *);
131 ssize_t _sys_pwrite(int, const void *, size_t, off_t);
132 ssize_t _sys_read(int, void *, size_t);
133 ssize_t _sys_readv(int, const struct iovec *, int);
134 ssize_t _sys_recvfrom(int, void * restrict, size_t, int,
135 struct sockaddr * restrict, socklen_t * restrict);
136 ssize_t _sys_recvmsg(int, struct msghdr *, int);
137 int _sys_recvmmsg(int, struct mmsghdr *, unsigned int, unsigned int,
138 struct timespec *);
139 ssize_t _sys_sendto(int, const void *, size_t, int, const struct sockaddr *,
140 socklen_t);
141 ssize_t _sys_sendmsg(int, const struct msghdr *, int);
142 int _sys_sendmmsg(int, struct mmsghdr *, unsigned int, unsigned int);
143 int _sys___select50(int, fd_set *, fd_set *, fd_set *, struct timeval *);
144 int _sys___wait450(pid_t, int *, int, struct rusage *);
145 ssize_t _sys_write(int, const void *, size_t);
146 ssize_t _sys_writev(int, const struct iovec *, int);
147 int _sys___sigsuspend14(const sigset_t *);
148 int ____sigtimedwait50(const sigset_t * __restrict, siginfo_t * __restrict,
149 struct timespec * __restrict);
150 int __sigsuspend14(const sigset_t *);
151
152 #define TESTCANCEL(id) do { \
153 if (__predict_true(!__uselibcstub) && \
154 __predict_false(atomic_load_relaxed(&(id)->pt_cancel) & \
155 PT_CANCEL_CANCELLED)) { \
156 membar_acquire(); \
157 pthread__cancelled(); \
158 } \
159 } while (0)
160
161
162 int
163 accept(int s, struct sockaddr *addr, socklen_t *addrlen)
164 {
165 int retval;
166 pthread_t self;
167
168 self = pthread__self();
169 TESTCANCEL(self);
170 retval = _sys_accept(s, addr, addrlen);
171 TESTCANCEL(self);
172
173 return retval;
174 }
175
176 int
177 __aio_suspend50(const struct aiocb * const list[], int nent,
178 const struct timespec *timeout)
179 {
180 int retval;
181 pthread_t self;
182
183 self = pthread__self();
184 TESTCANCEL(self);
185 retval = _sys___aio_suspend50(list, nent, timeout);
186 TESTCANCEL(self);
187
188 return retval;
189 }
190
191 int
192 __kevent100(int fd, const struct kevent *ev, size_t nev, struct kevent *rev,
193 size_t nrev, const struct timespec *ts)
194 {
195 int retval;
196 pthread_t self;
197
198 self = pthread__self();
199 TESTCANCEL(self);
200 retval = _sys___kevent100(fd, ev, nev, rev, nrev, ts);
201 TESTCANCEL(self);
202
203 return retval;
204 }
205
206 int
207 clock_nanosleep(clockid_t clock_id, int flags,
208 const struct timespec *rqtp, struct timespec *rmtp)
209 {
210 int retval;
211 pthread_t self;
212
213 self = pthread__self();
214 TESTCANCEL(self);
215 retval = _sys_clock_nanosleep(clock_id, flags, rqtp, rmtp);
216 TESTCANCEL(self);
217
218 return retval;
219 }
220
221 int
222 close(int d)
223 {
224 int retval;
225 pthread_t self;
226
227 self = pthread__self();
228 TESTCANCEL(self);
229 retval = _sys_close(d);
230 TESTCANCEL(self);
231
232 return retval;
233 }
234
235 int
236 connect(int s, const struct sockaddr *addr, socklen_t namelen)
237 {
238 int retval;
239 pthread_t self;
240
241 self = pthread__self();
242 TESTCANCEL(self);
243 retval = _sys_connect(s, addr, namelen);
244 TESTCANCEL(self);
245
246 return retval;
247 }
248
249 int
250 fcntl(int fd, int cmd, ...)
251 {
252 int retval;
253 pthread_t self;
254 va_list ap;
255
256 self = pthread__self();
257 TESTCANCEL(self);
258 va_start(ap, cmd);
259 retval = _sys_fcntl(fd, cmd, va_arg(ap, void *));
260 va_end(ap);
261 TESTCANCEL(self);
262
263 return retval;
264 }
265
266 int
267 fdatasync(int d)
268 {
269 int retval;
270 pthread_t self;
271
272 self = pthread__self();
273 TESTCANCEL(self);
274 retval = _sys_fdatasync(d);
275 TESTCANCEL(self);
276
277 return retval;
278 }
279
280 int
281 fsync(int d)
282 {
283 int retval;
284 pthread_t self;
285
286 self = pthread__self();
287 TESTCANCEL(self);
288 retval = _sys_fsync(d);
289 TESTCANCEL(self);
290
291 return retval;
292 }
293
294 int
295 fsync_range(int d, int f, off_t s, off_t e)
296 {
297 int retval;
298 pthread_t self;
299
300 self = pthread__self();
301 TESTCANCEL(self);
302 retval = _sys_fsync_range(d, f, s, e);
303 TESTCANCEL(self);
304
305 return retval;
306 }
307
308 int
309 mq_send(mqd_t mqdes, const char *msg_ptr, size_t msg_len, unsigned msg_prio)
310 {
311 int retval;
312 pthread_t self;
313
314 self = pthread__self();
315 TESTCANCEL(self);
316 retval = _sys_mq_send(mqdes, msg_ptr, msg_len, msg_prio);
317 TESTCANCEL(self);
318
319 return retval;
320 }
321
322 ssize_t
323 mq_receive(mqd_t mqdes, char *msg_ptr, size_t msg_len, unsigned *msg_prio)
324 {
325 ssize_t retval;
326 pthread_t self;
327
328 self = pthread__self();
329 TESTCANCEL(self);
330 retval = _sys_mq_receive(mqdes, msg_ptr, msg_len, msg_prio);
331 TESTCANCEL(self);
332
333 return retval;
334 }
335
336 int
337 __mq_timedsend50(mqd_t mqdes, const char *msg_ptr, size_t msg_len,
338 unsigned msg_prio, const struct timespec *abst)
339 {
340 int retval;
341 pthread_t self;
342
343 self = pthread__self();
344 TESTCANCEL(self);
345 retval = _sys___mq_timedsend50(mqdes, msg_ptr, msg_len, msg_prio, abst);
346 TESTCANCEL(self);
347
348 return retval;
349 }
350
351 ssize_t
352 __mq_timedreceive50(mqd_t mqdes, char *msg_ptr, size_t msg_len, unsigned *msg_prio,
353 const struct timespec *abst)
354 {
355 ssize_t retval;
356 pthread_t self;
357
358 self = pthread__self();
359 TESTCANCEL(self);
360 retval = _sys___mq_timedreceive50(mqdes, msg_ptr, msg_len, msg_prio, abst);
361 TESTCANCEL(self);
362
363 return retval;
364 }
365
366 ssize_t
367 msgrcv(int msgid, void *msgp, size_t msgsz, long msgtyp, int msgflg)
368 {
369 ssize_t retval;
370 pthread_t self;
371
372 self = pthread__self();
373 TESTCANCEL(self);
374 retval = _sys_msgrcv(msgid, msgp, msgsz, msgtyp, msgflg);
375 TESTCANCEL(self);
376
377 return retval;
378 }
379
380 int
381 msgsnd(int msgid, const void *msgp, size_t msgsz, int msgflg)
382 {
383 int retval;
384 pthread_t self;
385
386 self = pthread__self();
387 TESTCANCEL(self);
388 retval = _sys_msgsnd(msgid, msgp, msgsz, msgflg);
389 TESTCANCEL(self);
390
391 return retval;
392 }
393
394 int
395 __msync13(void *addr, size_t len, int flags)
396 {
397 int retval;
398 pthread_t self;
399
400 self = pthread__self();
401 TESTCANCEL(self);
402 retval = _sys___msync13(addr, len, flags);
403 TESTCANCEL(self);
404
405 return retval;
406 }
407
408 int
409 open(const char *path, int flags, ...)
410 {
411 int retval;
412 pthread_t self;
413 va_list ap;
414
415 self = pthread__self();
416 TESTCANCEL(self);
417 va_start(ap, flags);
418 retval = _sys_open(path, flags, va_arg(ap, mode_t));
419 va_end(ap);
420 TESTCANCEL(self);
421
422 return retval;
423 }
424
425 int
426 openat(int fd, const char *path, int flags, ...)
427 {
428 int retval;
429 pthread_t self;
430 va_list ap;
431
432 self = pthread__self();
433 TESTCANCEL(self);
434 va_start(ap, flags);
435 retval = _sys_openat(fd, path, flags, va_arg(ap, mode_t));
436 va_end(ap);
437 TESTCANCEL(self);
438
439 return retval;
440 }
441
442 int
443 __nanosleep50(const struct timespec *rqtp, struct timespec *rmtp)
444 {
445 int retval;
446 pthread_t self;
447
448 self = pthread__self();
449 TESTCANCEL(self);
450 /*
451 * For now, just nanosleep. In the future, maybe pass a ucontext_t
452 * to _lwp_nanosleep() and allow it to recycle our kernel stack.
453 */
454 retval = _sys___nanosleep50(rqtp, rmtp);
455 TESTCANCEL(self);
456
457 return retval;
458 }
459
460 int
461 poll(struct pollfd *fds, nfds_t nfds, int timeout)
462 {
463 int retval;
464 pthread_t self;
465
466 self = pthread__self();
467 TESTCANCEL(self);
468 retval = _sys_poll(fds, nfds, timeout);
469 TESTCANCEL(self);
470
471 return retval;
472 }
473
474 int
475 __pollts50(struct pollfd *fds, nfds_t nfds, const struct timespec *ts,
476 const sigset_t *sigmask)
477 {
478 int retval;
479 pthread_t self;
480
481 self = pthread__self();
482 TESTCANCEL(self);
483 retval = _sys___pollts50(fds, nfds, ts, sigmask);
484 TESTCANCEL(self);
485
486 return retval;
487 }
488
489 ssize_t
490 pread(int d, void *buf, size_t nbytes, off_t offset)
491 {
492 ssize_t retval;
493 pthread_t self;
494
495 self = pthread__self();
496 TESTCANCEL(self);
497 retval = _sys_pread(d, buf, nbytes, offset);
498 TESTCANCEL(self);
499
500 return retval;
501 }
502
503 int
504 __pselect50(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
505 const struct timespec *timeout, const sigset_t *sigmask)
506 {
507 int retval;
508 pthread_t self;
509
510 self = pthread__self();
511 TESTCANCEL(self);
512 retval = _sys___pselect50(nfds, readfds, writefds, exceptfds, timeout,
513 sigmask);
514 TESTCANCEL(self);
515
516 return retval;
517 }
518
519 ssize_t
520 pwrite(int d, const void *buf, size_t nbytes, off_t offset)
521 {
522 ssize_t retval;
523 pthread_t self;
524
525 self = pthread__self();
526 TESTCANCEL(self);
527 retval = _sys_pwrite(d, buf, nbytes, offset);
528 TESTCANCEL(self);
529
530 return retval;
531 }
532
533 ssize_t
534 read(int d, void *buf, size_t nbytes)
535 {
536 ssize_t retval;
537 pthread_t self;
538
539 self = pthread__self();
540 TESTCANCEL(self);
541 retval = _sys_read(d, buf, nbytes);
542 TESTCANCEL(self);
543
544 return retval;
545 }
546
547 ssize_t
548 readv(int d, const struct iovec *iov, int iovcnt)
549 {
550 ssize_t retval;
551 pthread_t self;
552
553 self = pthread__self();
554 TESTCANCEL(self);
555 retval = _sys_readv(d, iov, iovcnt);
556 TESTCANCEL(self);
557
558 return retval;
559 }
560
561 ssize_t
562 recvfrom(int s, void * restrict buf, size_t len, int flags,
563 struct sockaddr * restrict from, socklen_t * restrict fromlen)
564 {
565 ssize_t retval;
566 pthread_t self;
567
568 self = pthread__self();
569 TESTCANCEL(self);
570 retval = _sys_recvfrom(s, buf, len, flags, from, fromlen);
571 TESTCANCEL(self);
572
573 return retval;
574 }
575
576 ssize_t
577 recvmsg(int s, struct msghdr *msg, int flags)
578 {
579 ssize_t retval;
580 pthread_t self;
581
582 self = pthread__self();
583 TESTCANCEL(self);
584 retval = _sys_recvmsg(s, msg, flags);
585 TESTCANCEL(self);
586
587 return retval;
588 }
589
590 int
591 recvmmsg(int s, struct mmsghdr *mmsg, unsigned int vlen,
592 unsigned int flags, struct timespec *timeout)
593 {
594 ssize_t retval;
595 pthread_t self;
596
597 self = pthread__self();
598 TESTCANCEL(self);
599 retval = _sys_recvmmsg(s, mmsg, vlen, flags, timeout);
600 TESTCANCEL(self);
601
602 return retval;
603 }
604
605 int
606 __select50(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
607 struct timeval *timeout)
608 {
609 int retval;
610 pthread_t self;
611
612 self = pthread__self();
613 TESTCANCEL(self);
614 retval = _sys___select50(nfds, readfds, writefds, exceptfds, timeout);
615 TESTCANCEL(self);
616
617 return retval;
618 }
619
620 ssize_t
621 sendto(int s, const void *msg, size_t len, int flags,
622 const struct sockaddr *to, socklen_t tolen)
623 {
624 int retval;
625 pthread_t self;
626
627 self = pthread__self();
628 TESTCANCEL(self);
629 retval = _sys_sendto(s, msg, len, flags, to, tolen);
630 TESTCANCEL(self);
631
632 return retval;
633 }
634
635 ssize_t
636 sendmsg(int s, const struct msghdr *msg, int flags)
637 {
638 int retval;
639 pthread_t self;
640
641 self = pthread__self();
642 TESTCANCEL(self);
643 retval = _sys_sendmsg(s, msg, flags);
644 TESTCANCEL(self);
645
646 return retval;
647 }
648
649 int
650 sendmmsg(int s, struct mmsghdr *mmsg, unsigned int vlen,
651 unsigned int flags)
652 {
653 int retval;
654 pthread_t self;
655
656 self = pthread__self();
657 TESTCANCEL(self);
658 retval = _sys_sendmmsg(s, mmsg, vlen, flags);
659 TESTCANCEL(self);
660
661 return retval;
662 }
663
664
665 pid_t
666 __wait450(pid_t wpid, int *status, int options, struct rusage *rusage)
667 {
668 pid_t retval;
669 pthread_t self;
670
671 self = pthread__self();
672 TESTCANCEL(self);
673 retval = _sys___wait450(wpid, status, options, rusage);
674 TESTCANCEL(self);
675
676 return retval;
677 }
678
679 ssize_t
680 write(int d, const void *buf, size_t nbytes)
681 {
682 ssize_t retval;
683 pthread_t self;
684
685 self = pthread__self();
686 TESTCANCEL(self);
687 retval = _sys_write(d, buf, nbytes);
688 TESTCANCEL(self);
689
690 return retval;
691 }
692
693 ssize_t
694 writev(int d, const struct iovec *iov, int iovcnt)
695 {
696 ssize_t retval;
697 pthread_t self;
698
699 self = pthread__self();
700 TESTCANCEL(self);
701 retval = _sys_writev(d, iov, iovcnt);
702 TESTCANCEL(self);
703
704 return retval;
705 }
706
707 int
708 __sigsuspend14(const sigset_t *sigmask)
709 {
710 pthread_t self;
711 int retval;
712
713 self = pthread__self();
714 TESTCANCEL(self);
715 retval = _sys___sigsuspend14(sigmask);
716 TESTCANCEL(self);
717
718 return retval;
719 }
720
721 int
722 __sigtimedwait50(const sigset_t * __restrict set, siginfo_t * __restrict info,
723 const struct timespec * __restrict timeout)
724 {
725 pthread_t self;
726 int retval;
727 struct timespec tout, *tp;
728
729 if (timeout) {
730 tout = *timeout;
731 tp = &tout;
732 } else
733 tp = NULL;
734
735 self = pthread__self();
736 TESTCANCEL(self);
737 retval = ____sigtimedwait50(set, info, tp);
738 TESTCANCEL(self);
739
740 return retval;
741 }
742
743 int
744 sigwait(const sigset_t * __restrict set, int * __restrict sig)
745 {
746 pthread_t self;
747 int saved_errno;
748 int new_errno;
749 int retval;
750
751 self = pthread__self();
752 saved_errno = errno;
753 TESTCANCEL(self);
754 retval = ____sigtimedwait50(set, NULL, NULL);
755 TESTCANCEL(self);
756 new_errno = errno;
757 errno = saved_errno;
758 if (retval < 0) {
759 return new_errno;
760 }
761 *sig = retval;
762 return 0;
763 }
764
765 __strong_alias(_close, close)
766 __strong_alias(_clock_nanosleep, clock_nanosleep)
767 __strong_alias(_fcntl, fcntl)
768 __strong_alias(_fdatasync, fdatasync)
769 __strong_alias(_fsync, fsync)
770 __weak_alias(fsync_range, _fsync_range)
771 __strong_alias(_mq_send, mq_send)
772 __strong_alias(_mq_receive, mq_receive)
773 __strong_alias(_msgrcv, msgrcv)
774 __strong_alias(_msgsnd, msgsnd)
775 __strong_alias(___msync13, __msync13)
776 __strong_alias(___nanosleep50, __nanosleep50)
777 __strong_alias(_open, open)
778 __strong_alias(_openat, openat)
779 __strong_alias(_poll, poll)
780 __strong_alias(_pread, pread)
781 __strong_alias(_pwrite, pwrite)
782 __strong_alias(_read, read)
783 __strong_alias(_readv, readv)
784 __strong_alias(_recvfrom, recvfrom)
785 __strong_alias(_recvmsg, recvmsg)
786 __strong_alias(_recvmmsg, recvmmsg)
787 __strong_alias(_sendmsg, sendmsg)
788 __strong_alias(_sendmmsg, sendmmsg)
789 __strong_alias(_sendto, sendto)
790 __strong_alias(_sigwait, sigwait)
791 __strong_alias(_write, write)
792 __strong_alias(_writev, writev)
793
794 #endif /* !lint */
795