2 * Copyright (c) 2014 The FreeBSD Foundation.
3 * Copyright (C) 2005 David Xu <davidxu@freebsd.org>.
4 * Copyright (c) 2003 Daniel Eischen <deischen@freebsd.org>.
5 * Copyright (C) 2000 Jason Evans <jasone@freebsd.org>.
8 * Portions of this software were developed by Konstantin Belousov
9 * under sponsorship from the FreeBSD Foundation.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice(s), this list of conditions and the following disclaimer as
16 * the first lines of this file unmodified other than the possible
17 * addition of one or more copyright notices.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice(s), this list of conditions and the following disclaimer in
20 * the documentation and/or other materials provided with the
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
24 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE
27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
32 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
33 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 * SPDX-License-Identifier: BSD-3-Clause
39 * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au>
40 * All rights reserved.
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. Neither the name of the author nor the names of any co-contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 #include <sys/cdefs.h>
69 __FBSDID("$FreeBSD$");
71 #include "namespace.h"
72 #include <sys/types.h>
74 #include <sys/param.h>
75 #include <sys/select.h>
76 #include <sys/signalvar.h>
77 #include <sys/socket.h>
95 #include "un-namespace.h"
97 #include "libc_private.h"
98 #include "thr_private.h"
101 __thr_accept(int s, struct sockaddr *addr, socklen_t *addrlen)
103 struct pthread *curthread;
106 curthread = _get_curthread();
107 _thr_cancel_enter(curthread);
108 ret = __sys_accept(s, addr, addrlen);
109 _thr_cancel_leave(curthread, ret == -1);
115 * Cancellation behavior:
116 * If thread is canceled, no socket is created.
119 __thr_accept4(int s, struct sockaddr *addr, socklen_t *addrlen, int flags)
121 struct pthread *curthread;
124 curthread = _get_curthread();
125 _thr_cancel_enter(curthread);
126 ret = __sys_accept4(s, addr, addrlen, flags);
127 _thr_cancel_leave(curthread, ret == -1);
133 __thr_aio_suspend(const struct aiocb * const iocbs[], int niocb, const struct
136 struct pthread *curthread;
139 curthread = _get_curthread();
140 _thr_cancel_enter(curthread);
141 ret = __sys_aio_suspend(iocbs, niocb, timeout);
142 _thr_cancel_leave(curthread, 1);
148 * Cancellation behavior:
149 * According to manual of close(), the file descriptor is always deleted.
150 * Here, thread is only canceled after the system call, so the file
151 * descriptor is always deleted despite whether the thread is canceled
157 struct pthread *curthread;
160 curthread = _get_curthread();
161 _thr_cancel_enter2(curthread, 0);
162 ret = __sys_close(fd);
163 _thr_cancel_leave(curthread, 1);
169 * Cancellation behavior:
170 * If the thread is canceled, connection is not made.
173 __thr_connect(int fd, const struct sockaddr *name, socklen_t namelen)
175 struct pthread *curthread;
178 curthread = _get_curthread();
179 _thr_cancel_enter(curthread);
180 ret = __sys_connect(fd, name, namelen);
181 _thr_cancel_leave(curthread, ret == -1);
187 * Cancellation behavior:
188 * According to specification, only F_SETLKW is a cancellation point.
189 * Thread is only canceled at start, or canceled if the system call
190 * is failure, this means the function does not generate side effect
194 __thr_fcntl(int fd, int cmd, ...)
196 struct pthread *curthread;
200 curthread = _get_curthread();
202 if (cmd == F_OSETLKW || cmd == F_SETLKW) {
203 _thr_cancel_enter(curthread);
204 ret = __sys_fcntl(fd, cmd, va_arg(ap, void *));
205 _thr_cancel_leave(curthread, ret == -1);
207 ret = __sys_fcntl(fd, cmd, va_arg(ap, void *));
215 * Cancellation behavior:
216 * Thread may be canceled after system call.
221 struct pthread *curthread;
224 curthread = _get_curthread();
225 _thr_cancel_enter2(curthread, 0);
226 ret = __sys_fsync(fd);
227 _thr_cancel_leave(curthread, 1);
233 __thr_fdatasync(int fd)
235 struct pthread *curthread;
238 curthread = _get_curthread();
239 _thr_cancel_enter2(curthread, 0);
240 ret = __sys_fdatasync(fd);
241 _thr_cancel_leave(curthread, 1);
247 * Cancellation behavior:
248 * Thread may be canceled after system call.
251 __thr_msync(void *addr, size_t len, int flags)
253 struct pthread *curthread;
256 curthread = _get_curthread();
257 _thr_cancel_enter2(curthread, 0);
258 ret = __sys_msync(addr, len, flags);
259 _thr_cancel_leave(curthread, 1);
265 __thr_clock_nanosleep(clockid_t clock_id, int flags,
266 const struct timespec *time_to_sleep, struct timespec *time_remaining)
268 struct pthread *curthread;
271 curthread = _get_curthread();
272 _thr_cancel_enter(curthread);
273 ret = __sys_clock_nanosleep(clock_id, flags, time_to_sleep,
275 _thr_cancel_leave(curthread, 1);
281 __thr_nanosleep(const struct timespec *time_to_sleep,
282 struct timespec *time_remaining)
284 struct pthread *curthread;
287 curthread = _get_curthread();
288 _thr_cancel_enter(curthread);
289 ret = __sys_nanosleep(time_to_sleep, time_remaining);
290 _thr_cancel_leave(curthread, 1);
296 * Cancellation behavior:
297 * If the thread is canceled, file is not opened.
300 __thr_openat(int fd, const char *path, int flags, ...)
302 struct pthread *curthread;
307 /* Check if the file is being created: */
308 if ((flags & O_CREAT) != 0) {
309 /* Get the creation mode: */
311 mode = va_arg(ap, int);
317 curthread = _get_curthread();
318 _thr_cancel_enter(curthread);
319 ret = __sys_openat(fd, path, flags, mode);
320 _thr_cancel_leave(curthread, ret == -1);
326 * Cancellation behavior:
327 * Thread may be canceled at start, but if the system call returns something,
328 * the thread is not canceled.
331 __thr_poll(struct pollfd *fds, unsigned int nfds, int timeout)
333 struct pthread *curthread;
336 curthread = _get_curthread();
337 _thr_cancel_enter(curthread);
338 ret = __sys_poll(fds, nfds, timeout);
339 _thr_cancel_leave(curthread, ret == -1);
345 * Cancellation behavior:
346 * Thread may be canceled at start, but if the system call returns something,
347 * the thread is not canceled.
350 __thr_ppoll(struct pollfd pfd[], nfds_t nfds, const struct timespec *
351 timeout, const sigset_t *newsigmask)
353 struct pthread *curthread;
356 curthread = _get_curthread();
357 _thr_cancel_enter(curthread);
358 ret = __sys_ppoll(pfd, nfds, timeout, newsigmask);
359 _thr_cancel_leave(curthread, ret == -1);
365 * Cancellation behavior:
366 * Thread may be canceled at start, but if the system call returns something,
367 * the thread is not canceled.
370 __thr_pselect(int count, fd_set *rfds, fd_set *wfds, fd_set *efds,
371 const struct timespec *timo, const sigset_t *mask)
373 struct pthread *curthread;
376 curthread = _get_curthread();
377 _thr_cancel_enter(curthread);
378 ret = __sys_pselect(count, rfds, wfds, efds, timo, mask);
379 _thr_cancel_leave(curthread, ret == -1);
385 __thr_kevent(int kq, const struct kevent *changelist, int nchanges,
386 struct kevent *eventlist, int nevents, const struct timespec *timeout)
388 struct pthread *curthread;
393 * No blocking, do not make the call cancellable.
395 return (__sys_kevent(kq, changelist, nchanges, eventlist,
398 curthread = _get_curthread();
399 _thr_cancel_enter(curthread);
400 ret = __sys_kevent(kq, changelist, nchanges, eventlist, nevents,
402 _thr_cancel_leave(curthread, ret == -1 && nchanges == 0);
408 * Cancellation behavior:
409 * Thread may be canceled at start, but if the system call got some data,
410 * the thread is not canceled.
413 __thr_read(int fd, void *buf, size_t nbytes)
415 struct pthread *curthread;
418 curthread = _get_curthread();
419 _thr_cancel_enter(curthread);
420 ret = __sys_read(fd, buf, nbytes);
421 _thr_cancel_leave(curthread, ret == -1);
427 * Cancellation behavior:
428 * Thread may be canceled at start, but if the system call got some data,
429 * the thread is not canceled.
432 __thr_readv(int fd, const struct iovec *iov, int iovcnt)
434 struct pthread *curthread;
437 curthread = _get_curthread();
438 _thr_cancel_enter(curthread);
439 ret = __sys_readv(fd, iov, iovcnt);
440 _thr_cancel_leave(curthread, ret == -1);
445 * Cancellation behavior:
446 * Thread may be canceled at start, but if the system call got some data,
447 * the thread is not canceled.
450 __thr_recvfrom(int s, void *b, size_t l, int f, struct sockaddr *from,
453 struct pthread *curthread;
456 curthread = _get_curthread();
457 _thr_cancel_enter(curthread);
458 ret = __sys_recvfrom(s, b, l, f, from, fl);
459 _thr_cancel_leave(curthread, ret == -1);
464 * Cancellation behavior:
465 * Thread may be canceled at start, but if the system call got some data,
466 * the thread is not canceled.
469 __thr_recvmsg(int s, struct msghdr *m, int f)
471 struct pthread *curthread;
474 curthread = _get_curthread();
475 _thr_cancel_enter(curthread);
476 ret = __sys_recvmsg(s, m, f);
477 _thr_cancel_leave(curthread, ret == -1);
482 * Cancellation behavior:
483 * Thread may be canceled at start, but if the system call returns something,
484 * the thread is not canceled.
487 __thr_select(int numfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
488 struct timeval *timeout)
490 struct pthread *curthread;
493 curthread = _get_curthread();
494 _thr_cancel_enter(curthread);
495 ret = __sys_select(numfds, readfds, writefds, exceptfds, timeout);
496 _thr_cancel_leave(curthread, ret == -1);
501 * Cancellation behavior:
502 * Thread may be canceled at start, but if the system call sent
503 * data, the thread is not canceled.
506 __thr_sendmsg(int s, const struct msghdr *m, int f)
508 struct pthread *curthread;
511 curthread = _get_curthread();
512 _thr_cancel_enter(curthread);
513 ret = __sys_sendmsg(s, m, f);
514 _thr_cancel_leave(curthread, ret <= 0);
519 * Cancellation behavior:
520 * Thread may be canceled at start, but if the system call sent some
521 * data, the thread is not canceled.
524 __thr_sendto(int s, const void *m, size_t l, int f, const struct sockaddr *t,
527 struct pthread *curthread;
530 curthread = _get_curthread();
531 _thr_cancel_enter(curthread);
532 ret = __sys_sendto(s, m, l, f, t, tl);
533 _thr_cancel_leave(curthread, ret <= 0);
538 __thr_system(const char *string)
540 struct pthread *curthread;
543 curthread = _get_curthread();
544 _thr_cancel_enter(curthread);
545 ret = __libc_system(string);
546 _thr_cancel_leave(curthread, 1);
551 * Cancellation behavior:
552 * If thread is canceled, the system call is not completed,
553 * this means not all bytes were drained.
556 __thr_tcdrain(int fd)
558 struct pthread *curthread;
561 curthread = _get_curthread();
562 _thr_cancel_enter(curthread);
563 ret = __libc_tcdrain(fd);
564 _thr_cancel_leave(curthread, ret == -1);
569 * Cancellation behavior:
570 * Thread may be canceled at start, but if the system call returns
571 * a child pid, the thread is not canceled.
574 __thr_wait4(pid_t pid, int *status, int options, struct rusage *rusage)
576 struct pthread *curthread;
579 curthread = _get_curthread();
580 _thr_cancel_enter(curthread);
581 ret = __sys_wait4(pid, status, options, rusage);
582 _thr_cancel_leave(curthread, ret <= 0);
587 * Cancellation behavior:
588 * Thread may be canceled at start, but if the system call returns
589 * a child pid, the thread is not canceled.
592 __thr_wait6(idtype_t idtype, id_t id, int *status, int options,
593 struct __wrusage *ru, siginfo_t *infop)
595 struct pthread *curthread;
598 curthread = _get_curthread();
599 _thr_cancel_enter(curthread);
600 ret = __sys_wait6(idtype, id, status, options, ru, infop);
601 _thr_cancel_leave(curthread, ret <= 0);
606 * Cancellation behavior:
607 * Thread may be canceled at start, but if the thread wrote some data,
608 * it is not canceled.
611 __thr_write(int fd, const void *buf, size_t nbytes)
613 struct pthread *curthread;
616 curthread = _get_curthread();
617 _thr_cancel_enter(curthread);
618 ret = __sys_write(fd, buf, nbytes);
619 _thr_cancel_leave(curthread, (ret <= 0));
624 * Cancellation behavior:
625 * Thread may be canceled at start, but if the thread wrote some data,
626 * it is not canceled.
629 __thr_writev(int fd, const struct iovec *iov, int iovcnt)
631 struct pthread *curthread;
634 curthread = _get_curthread();
635 _thr_cancel_enter(curthread);
636 ret = __sys_writev(fd, iov, iovcnt);
637 _thr_cancel_leave(curthread, (ret <= 0));
642 __thr_interpose_libc(void)
645 __set_error_selector(__error_threaded);
647 *(__libc_interposing_slot(INTERPOS_##name)) = \
648 (interpos_func_t)__thr_##name;
687 SLOT(map_stacks_exec);
689 SLOT(clock_nanosleep);
691 *(__libc_interposing_slot(
692 INTERPOS__pthread_mutex_init_calloc_cb)) =
693 (interpos_func_t)_pthread_mutex_init_calloc_cb;