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_nanosleep(const struct timespec *time_to_sleep,
264 struct timespec *time_remaining)
266 struct pthread *curthread;
269 curthread = _get_curthread();
270 _thr_cancel_enter(curthread);
271 ret = __sys_nanosleep(time_to_sleep, time_remaining);
272 _thr_cancel_leave(curthread, 1);
278 * Cancellation behavior:
279 * If the thread is canceled, file is not opened.
282 __thr_openat(int fd, const char *path, int flags, ...)
284 struct pthread *curthread;
289 /* Check if the file is being created: */
290 if ((flags & O_CREAT) != 0) {
291 /* Get the creation mode: */
293 mode = va_arg(ap, int);
299 curthread = _get_curthread();
300 _thr_cancel_enter(curthread);
301 ret = __sys_openat(fd, path, flags, mode);
302 _thr_cancel_leave(curthread, ret == -1);
308 * Cancellation behavior:
309 * Thread may be canceled at start, but if the system call returns something,
310 * the thread is not canceled.
313 __thr_poll(struct pollfd *fds, unsigned int nfds, int timeout)
315 struct pthread *curthread;
318 curthread = _get_curthread();
319 _thr_cancel_enter(curthread);
320 ret = __sys_poll(fds, nfds, timeout);
321 _thr_cancel_leave(curthread, ret == -1);
327 * Cancellation behavior:
328 * Thread may be canceled at start, but if the system call returns something,
329 * the thread is not canceled.
332 __thr_ppoll(struct pollfd pfd[], nfds_t nfds, const struct timespec *
333 timeout, const sigset_t *newsigmask)
335 struct pthread *curthread;
338 curthread = _get_curthread();
339 _thr_cancel_enter(curthread);
340 ret = __sys_ppoll(pfd, nfds, timeout, newsigmask);
341 _thr_cancel_leave(curthread, ret == -1);
347 * Cancellation behavior:
348 * Thread may be canceled at start, but if the system call returns something,
349 * the thread is not canceled.
352 __thr_pselect(int count, fd_set *rfds, fd_set *wfds, fd_set *efds,
353 const struct timespec *timo, const sigset_t *mask)
355 struct pthread *curthread;
358 curthread = _get_curthread();
359 _thr_cancel_enter(curthread);
360 ret = __sys_pselect(count, rfds, wfds, efds, timo, mask);
361 _thr_cancel_leave(curthread, ret == -1);
367 __thr_kevent(int kq, const struct kevent *changelist, int nchanges,
368 struct kevent *eventlist, int nevents, const struct timespec *timeout)
370 struct pthread *curthread;
375 * No blocking, do not make the call cancellable.
377 return (__sys_kevent(kq, changelist, nchanges, eventlist,
380 curthread = _get_curthread();
381 _thr_cancel_enter(curthread);
382 ret = __sys_kevent(kq, changelist, nchanges, eventlist, nevents,
384 _thr_cancel_leave(curthread, ret == -1 && nchanges == 0);
390 * Cancellation behavior:
391 * Thread may be canceled at start, but if the system call got some data,
392 * the thread is not canceled.
395 __thr_read(int fd, void *buf, size_t nbytes)
397 struct pthread *curthread;
400 curthread = _get_curthread();
401 _thr_cancel_enter(curthread);
402 ret = __sys_read(fd, buf, nbytes);
403 _thr_cancel_leave(curthread, ret == -1);
409 * Cancellation behavior:
410 * Thread may be canceled at start, but if the system call got some data,
411 * the thread is not canceled.
414 __thr_readv(int fd, const struct iovec *iov, int iovcnt)
416 struct pthread *curthread;
419 curthread = _get_curthread();
420 _thr_cancel_enter(curthread);
421 ret = __sys_readv(fd, iov, iovcnt);
422 _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_recvfrom(int s, void *b, size_t l, int f, struct sockaddr *from,
435 struct pthread *curthread;
438 curthread = _get_curthread();
439 _thr_cancel_enter(curthread);
440 ret = __sys_recvfrom(s, b, l, f, from, fl);
441 _thr_cancel_leave(curthread, ret == -1);
446 * Cancellation behavior:
447 * Thread may be canceled at start, but if the system call got some data,
448 * the thread is not canceled.
451 __thr_recvmsg(int s, struct msghdr *m, int f)
453 struct pthread *curthread;
456 curthread = _get_curthread();
457 _thr_cancel_enter(curthread);
458 ret = __sys_recvmsg(s, m, f);
459 _thr_cancel_leave(curthread, ret == -1);
464 * Cancellation behavior:
465 * Thread may be canceled at start, but if the system call returns something,
466 * the thread is not canceled.
469 __thr_select(int numfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
470 struct timeval *timeout)
472 struct pthread *curthread;
475 curthread = _get_curthread();
476 _thr_cancel_enter(curthread);
477 ret = __sys_select(numfds, readfds, writefds, exceptfds, timeout);
478 _thr_cancel_leave(curthread, ret == -1);
483 * Cancellation behavior:
484 * Thread may be canceled at start, but if the system call sent
485 * data, the thread is not canceled.
488 __thr_sendmsg(int s, const struct msghdr *m, int f)
490 struct pthread *curthread;
493 curthread = _get_curthread();
494 _thr_cancel_enter(curthread);
495 ret = __sys_sendmsg(s, m, f);
496 _thr_cancel_leave(curthread, ret <= 0);
501 * Cancellation behavior:
502 * Thread may be canceled at start, but if the system call sent some
503 * data, the thread is not canceled.
506 __thr_sendto(int s, const void *m, size_t l, int f, const struct sockaddr *t,
509 struct pthread *curthread;
512 curthread = _get_curthread();
513 _thr_cancel_enter(curthread);
514 ret = __sys_sendto(s, m, l, f, t, tl);
515 _thr_cancel_leave(curthread, ret <= 0);
520 __thr_system(const char *string)
522 struct pthread *curthread;
525 curthread = _get_curthread();
526 _thr_cancel_enter(curthread);
527 ret = __libc_system(string);
528 _thr_cancel_leave(curthread, 1);
533 * Cancellation behavior:
534 * If thread is canceled, the system call is not completed,
535 * this means not all bytes were drained.
538 __thr_tcdrain(int fd)
540 struct pthread *curthread;
543 curthread = _get_curthread();
544 _thr_cancel_enter(curthread);
545 ret = __libc_tcdrain(fd);
546 _thr_cancel_leave(curthread, ret == -1);
551 * Cancellation behavior:
552 * Thread may be canceled at start, but if the system call returns
553 * a child pid, the thread is not canceled.
556 __thr_wait4(pid_t pid, int *status, int options, struct rusage *rusage)
558 struct pthread *curthread;
561 curthread = _get_curthread();
562 _thr_cancel_enter(curthread);
563 ret = __sys_wait4(pid, status, options, rusage);
564 _thr_cancel_leave(curthread, ret <= 0);
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_wait6(idtype_t idtype, id_t id, int *status, int options,
575 struct __wrusage *ru, siginfo_t *infop)
577 struct pthread *curthread;
580 curthread = _get_curthread();
581 _thr_cancel_enter(curthread);
582 ret = __sys_wait6(idtype, id, status, options, ru, infop);
583 _thr_cancel_leave(curthread, ret <= 0);
588 * Cancellation behavior:
589 * Thread may be canceled at start, but if the thread wrote some data,
590 * it is not canceled.
593 __thr_write(int fd, const void *buf, size_t nbytes)
595 struct pthread *curthread;
598 curthread = _get_curthread();
599 _thr_cancel_enter(curthread);
600 ret = __sys_write(fd, buf, nbytes);
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_writev(int fd, const struct iovec *iov, int iovcnt)
613 struct pthread *curthread;
616 curthread = _get_curthread();
617 _thr_cancel_enter(curthread);
618 ret = __sys_writev(fd, iov, iovcnt);
619 _thr_cancel_leave(curthread, (ret <= 0));
624 __thr_interpose_libc(void)
627 __set_error_selector(__error_threaded);
629 *(__libc_interposing_slot(INTERPOS_##name)) = \
630 (interpos_func_t)__thr_##name;
669 SLOT(map_stacks_exec);
672 *(__libc_interposing_slot(
673 INTERPOS__pthread_mutex_init_calloc_cb)) =
674 (interpos_func_t)_pthread_mutex_init_calloc_cb;