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 * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au>
38 * All rights reserved.
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 * 3. Neither the name of the author nor the names of any co-contributors
49 * may be used to endorse or promote products derived from this software
50 * without specific prior written permission.
52 * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 #include <sys/cdefs.h>
67 __FBSDID("$FreeBSD$");
69 #include "namespace.h"
70 #include <sys/types.h>
72 #include <sys/param.h>
73 #include <sys/select.h>
74 #include <sys/signalvar.h>
75 #include <sys/socket.h>
93 #include "un-namespace.h"
95 #include "libc_private.h"
96 #include "thr_private.h"
99 __thr_accept(int s, struct sockaddr *addr, socklen_t *addrlen)
101 struct pthread *curthread;
104 curthread = _get_curthread();
105 _thr_cancel_enter(curthread);
106 ret = __sys_accept(s, addr, addrlen);
107 _thr_cancel_leave(curthread, ret == -1);
113 * Cancellation behavior:
114 * If thread is canceled, no socket is created.
117 __thr_accept4(int s, struct sockaddr *addr, socklen_t *addrlen, int flags)
119 struct pthread *curthread;
122 curthread = _get_curthread();
123 _thr_cancel_enter(curthread);
124 ret = __sys_accept4(s, addr, addrlen, flags);
125 _thr_cancel_leave(curthread, ret == -1);
131 __thr_aio_suspend(const struct aiocb * const iocbs[], int niocb, const struct
134 struct pthread *curthread;
137 curthread = _get_curthread();
138 _thr_cancel_enter(curthread);
139 ret = __sys_aio_suspend(iocbs, niocb, timeout);
140 _thr_cancel_leave(curthread, 1);
146 * Cancellation behavior:
147 * According to manual of close(), the file descriptor is always deleted.
148 * Here, thread is only canceled after the system call, so the file
149 * descriptor is always deleted despite whether the thread is canceled
155 struct pthread *curthread;
158 curthread = _get_curthread();
159 _thr_cancel_enter2(curthread, 0);
160 ret = __sys_close(fd);
161 _thr_cancel_leave(curthread, 1);
167 * Cancellation behavior:
168 * If the thread is canceled, connection is not made.
171 __thr_connect(int fd, const struct sockaddr *name, socklen_t namelen)
173 struct pthread *curthread;
176 curthread = _get_curthread();
177 _thr_cancel_enter(curthread);
178 ret = __sys_connect(fd, name, namelen);
179 _thr_cancel_leave(curthread, ret == -1);
185 * Cancellation behavior:
186 * According to specification, only F_SETLKW is a cancellation point.
187 * Thread is only canceled at start, or canceled if the system call
188 * is failure, this means the function does not generate side effect
192 __thr_fcntl(int fd, int cmd, ...)
194 struct pthread *curthread;
198 curthread = _get_curthread();
200 if (cmd == F_OSETLKW || cmd == F_SETLKW) {
201 _thr_cancel_enter(curthread);
202 ret = __sys_fcntl(fd, cmd, va_arg(ap, void *));
203 _thr_cancel_leave(curthread, ret == -1);
205 ret = __sys_fcntl(fd, cmd, va_arg(ap, void *));
213 * Cancellation behavior:
214 * Thread may be canceled after system call.
219 struct pthread *curthread;
222 curthread = _get_curthread();
223 _thr_cancel_enter2(curthread, 0);
224 ret = __sys_fsync(fd);
225 _thr_cancel_leave(curthread, 1);
231 __thr_fdatasync(int fd)
233 struct pthread *curthread;
236 curthread = _get_curthread();
237 _thr_cancel_enter2(curthread, 0);
238 ret = __sys_fdatasync(fd);
239 _thr_cancel_leave(curthread, 1);
245 * Cancellation behavior:
246 * Thread may be canceled after system call.
249 __thr_msync(void *addr, size_t len, int flags)
251 struct pthread *curthread;
254 curthread = _get_curthread();
255 _thr_cancel_enter2(curthread, 0);
256 ret = __sys_msync(addr, len, flags);
257 _thr_cancel_leave(curthread, 1);
263 __thr_clock_nanosleep(clockid_t clock_id, int flags,
264 const struct timespec *time_to_sleep, struct timespec *time_remaining)
266 struct pthread *curthread;
269 curthread = _get_curthread();
270 _thr_cancel_enter(curthread);
271 ret = __sys_clock_nanosleep(clock_id, flags, time_to_sleep,
273 _thr_cancel_leave(curthread, 1);
279 __thr_nanosleep(const struct timespec *time_to_sleep,
280 struct timespec *time_remaining)
282 struct pthread *curthread;
285 curthread = _get_curthread();
286 _thr_cancel_enter(curthread);
287 ret = __sys_nanosleep(time_to_sleep, time_remaining);
288 _thr_cancel_leave(curthread, 1);
294 * Cancellation behavior:
295 * If the thread is canceled, file is not opened.
298 __thr_openat(int fd, const char *path, int flags, ...)
300 struct pthread *curthread;
305 /* Check if the file is being created: */
306 if ((flags & O_CREAT) != 0) {
307 /* Get the creation mode: */
309 mode = va_arg(ap, int);
315 curthread = _get_curthread();
316 _thr_cancel_enter(curthread);
317 ret = __sys_openat(fd, path, flags, mode);
318 _thr_cancel_leave(curthread, ret == -1);
324 * Cancellation behavior:
325 * Thread may be canceled at start, but if the system call returns something,
326 * the thread is not canceled.
329 __thr_poll(struct pollfd *fds, unsigned int nfds, int timeout)
331 struct pthread *curthread;
334 curthread = _get_curthread();
335 _thr_cancel_enter(curthread);
336 ret = __sys_poll(fds, nfds, timeout);
337 _thr_cancel_leave(curthread, ret == -1);
343 * Cancellation behavior:
344 * Thread may be canceled at start, but if the system call returns something,
345 * the thread is not canceled.
348 __thr_ppoll(struct pollfd pfd[], nfds_t nfds, const struct timespec *
349 timeout, const sigset_t *newsigmask)
351 struct pthread *curthread;
354 curthread = _get_curthread();
355 _thr_cancel_enter(curthread);
356 ret = __sys_ppoll(pfd, nfds, timeout, newsigmask);
357 _thr_cancel_leave(curthread, ret == -1);
363 * Cancellation behavior:
364 * Thread may be canceled at start, but if the system call returns something,
365 * the thread is not canceled.
368 __thr_pselect(int count, fd_set *rfds, fd_set *wfds, fd_set *efds,
369 const struct timespec *timo, const sigset_t *mask)
371 struct pthread *curthread;
374 curthread = _get_curthread();
375 _thr_cancel_enter(curthread);
376 ret = __sys_pselect(count, rfds, wfds, efds, timo, mask);
377 _thr_cancel_leave(curthread, ret == -1);
383 __thr_kevent(int kq, const struct kevent *changelist, int nchanges,
384 struct kevent *eventlist, int nevents, const struct timespec *timeout)
386 struct pthread *curthread;
391 * No blocking, do not make the call cancellable.
393 return (__sys_kevent(kq, changelist, nchanges, eventlist,
396 curthread = _get_curthread();
397 _thr_cancel_enter(curthread);
398 ret = __sys_kevent(kq, changelist, nchanges, eventlist, nevents,
400 _thr_cancel_leave(curthread, ret == -1 && nchanges == 0);
406 * Cancellation behavior:
407 * Thread may be canceled at start, but if the system call got some data,
408 * the thread is not canceled.
411 __thr_read(int fd, void *buf, size_t nbytes)
413 struct pthread *curthread;
416 curthread = _get_curthread();
417 _thr_cancel_enter(curthread);
418 ret = __sys_read(fd, buf, nbytes);
419 _thr_cancel_leave(curthread, ret == -1);
425 * Cancellation behavior:
426 * Thread may be canceled at start, but if the system call got some data,
427 * the thread is not canceled.
430 __thr_readv(int fd, const struct iovec *iov, int iovcnt)
432 struct pthread *curthread;
435 curthread = _get_curthread();
436 _thr_cancel_enter(curthread);
437 ret = __sys_readv(fd, iov, iovcnt);
438 _thr_cancel_leave(curthread, ret == -1);
443 * Cancellation behavior:
444 * Thread may be canceled at start, but if the system call got some data,
445 * the thread is not canceled.
448 __thr_recvfrom(int s, void *b, size_t l, int f, struct sockaddr *from,
451 struct pthread *curthread;
454 curthread = _get_curthread();
455 _thr_cancel_enter(curthread);
456 ret = __sys_recvfrom(s, b, l, f, from, fl);
457 _thr_cancel_leave(curthread, ret == -1);
462 * Cancellation behavior:
463 * Thread may be canceled at start, but if the system call got some data,
464 * the thread is not canceled.
467 __thr_recvmsg(int s, struct msghdr *m, int f)
469 struct pthread *curthread;
472 curthread = _get_curthread();
473 _thr_cancel_enter(curthread);
474 ret = __sys_recvmsg(s, m, f);
475 _thr_cancel_leave(curthread, ret == -1);
480 * Cancellation behavior:
481 * Thread may be canceled at start, but if the system call returns something,
482 * the thread is not canceled.
485 __thr_select(int numfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
486 struct timeval *timeout)
488 struct pthread *curthread;
491 curthread = _get_curthread();
492 _thr_cancel_enter(curthread);
493 ret = __sys_select(numfds, readfds, writefds, exceptfds, timeout);
494 _thr_cancel_leave(curthread, ret == -1);
499 * Cancellation behavior:
500 * Thread may be canceled at start, but if the system call sent
501 * data, the thread is not canceled.
504 __thr_sendmsg(int s, const struct msghdr *m, int f)
506 struct pthread *curthread;
509 curthread = _get_curthread();
510 _thr_cancel_enter(curthread);
511 ret = __sys_sendmsg(s, m, f);
512 _thr_cancel_leave(curthread, ret <= 0);
517 * Cancellation behavior:
518 * Thread may be canceled at start, but if the system call sent some
519 * data, the thread is not canceled.
522 __thr_sendto(int s, const void *m, size_t l, int f, const struct sockaddr *t,
525 struct pthread *curthread;
528 curthread = _get_curthread();
529 _thr_cancel_enter(curthread);
530 ret = __sys_sendto(s, m, l, f, t, tl);
531 _thr_cancel_leave(curthread, ret <= 0);
536 __thr_system(const char *string)
538 struct pthread *curthread;
541 curthread = _get_curthread();
542 _thr_cancel_enter(curthread);
543 ret = __libc_system(string);
544 _thr_cancel_leave(curthread, 1);
549 * Cancellation behavior:
550 * If thread is canceled, the system call is not completed,
551 * this means not all bytes were drained.
554 __thr_tcdrain(int fd)
556 struct pthread *curthread;
559 curthread = _get_curthread();
560 _thr_cancel_enter(curthread);
561 ret = __libc_tcdrain(fd);
562 _thr_cancel_leave(curthread, ret == -1);
567 * Cancellation behavior:
568 * Thread may be canceled at start, but if the system call returns
569 * a child pid, the thread is not canceled.
572 __thr_wait4(pid_t pid, int *status, int options, struct rusage *rusage)
574 struct pthread *curthread;
577 curthread = _get_curthread();
578 _thr_cancel_enter(curthread);
579 ret = __sys_wait4(pid, status, options, rusage);
580 _thr_cancel_leave(curthread, ret <= 0);
585 * Cancellation behavior:
586 * Thread may be canceled at start, but if the system call returns
587 * a child pid, the thread is not canceled.
590 __thr_wait6(idtype_t idtype, id_t id, int *status, int options,
591 struct __wrusage *ru, siginfo_t *infop)
593 struct pthread *curthread;
596 curthread = _get_curthread();
597 _thr_cancel_enter(curthread);
598 ret = __sys_wait6(idtype, id, status, options, ru, infop);
599 _thr_cancel_leave(curthread, ret <= 0);
604 * Cancellation behavior:
605 * Thread may be canceled at start, but if the thread wrote some data,
606 * it is not canceled.
609 __thr_write(int fd, const void *buf, size_t nbytes)
611 struct pthread *curthread;
614 curthread = _get_curthread();
615 _thr_cancel_enter(curthread);
616 ret = __sys_write(fd, buf, nbytes);
617 _thr_cancel_leave(curthread, (ret <= 0));
622 * Cancellation behavior:
623 * Thread may be canceled at start, but if the thread wrote some data,
624 * it is not canceled.
627 __thr_writev(int fd, const struct iovec *iov, int iovcnt)
629 struct pthread *curthread;
632 curthread = _get_curthread();
633 _thr_cancel_enter(curthread);
634 ret = __sys_writev(fd, iov, iovcnt);
635 _thr_cancel_leave(curthread, (ret <= 0));
640 __thr_interpose_libc(void)
643 __set_error_selector(__error_threaded);
645 *(__libc_interposing_slot(INTERPOS_##name)) = \
646 (interpos_func_t)__thr_##name;
685 SLOT(map_stacks_exec);
687 SLOT(clock_nanosleep);
689 *(__libc_interposing_slot(
690 INTERPOS__pthread_mutex_init_calloc_cb)) =
691 (interpos_func_t)_pthread_mutex_init_calloc_cb;