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 extern int __fcntl_compat(int, int, ...);
103 * Cancellation behavior:
104 * If thread is canceled, no socket is created.
107 __thr_accept(int s, struct sockaddr *addr, socklen_t *addrlen)
109 struct pthread *curthread;
112 curthread = _get_curthread();
113 _thr_cancel_enter(curthread);
114 ret = __sys_accept(s, addr, addrlen);
115 _thr_cancel_leave(curthread, ret == -1);
121 * Cancellation behavior:
122 * If thread is canceled, no socket is created.
125 __thr_accept4(int s, struct sockaddr *addr, socklen_t *addrlen, int flags)
127 struct pthread *curthread;
130 curthread = _get_curthread();
131 _thr_cancel_enter(curthread);
132 ret = __sys_accept4(s, addr, addrlen, flags);
133 _thr_cancel_leave(curthread, ret == -1);
139 __thr_aio_suspend(const struct aiocb * const iocbs[], int niocb, const struct
142 struct pthread *curthread;
145 curthread = _get_curthread();
146 _thr_cancel_enter(curthread);
147 ret = __sys_aio_suspend(iocbs, niocb, timeout);
148 _thr_cancel_leave(curthread, 1);
154 * Cancellation behavior:
155 * According to manual of close(), the file descriptor is always deleted.
156 * Here, thread is only canceled after the system call, so the file
157 * descriptor is always deleted despite whether the thread is canceled
163 struct pthread *curthread;
166 curthread = _get_curthread();
167 _thr_cancel_enter2(curthread, 0);
168 ret = __sys_close(fd);
169 _thr_cancel_leave(curthread, 1);
175 * Cancellation behavior:
176 * If the thread is canceled, connection is not made.
179 __thr_connect(int fd, const struct sockaddr *name, socklen_t namelen)
181 struct pthread *curthread;
184 curthread = _get_curthread();
185 _thr_cancel_enter(curthread);
186 ret = __sys_connect(fd, name, namelen);
187 _thr_cancel_leave(curthread, ret == -1);
194 * Cancellation behavior:
195 * If thread is canceled, file is not created.
198 __thr_creat(const char *path, mode_t mode)
200 struct pthread *curthread;
203 curthread = _get_curthread();
204 _thr_cancel_enter(curthread);
205 ret = __libc_creat(path, mode);
206 _thr_cancel_leave(curthread, ret == -1);
212 * Cancellation behavior:
213 * According to specification, only F_SETLKW is a cancellation point.
214 * Thread is only canceled at start, or canceled if the system call
215 * is failure, this means the function does not generate side effect
219 __thr_fcntl(int fd, int cmd, ...)
221 struct pthread *curthread;
225 curthread = _get_curthread();
227 if (cmd == F_OSETLKW || cmd == F_SETLKW) {
228 _thr_cancel_enter(curthread);
229 #ifdef SYSCALL_COMPAT
230 ret = __fcntl_compat(fd, cmd, va_arg(ap, void *));
232 ret = __sys_fcntl(fd, cmd, va_arg(ap, void *));
234 _thr_cancel_leave(curthread, ret == -1);
236 #ifdef SYSCALL_COMPAT
237 ret = __fcntl_compat(fd, cmd, va_arg(ap, void *));
239 ret = __sys_fcntl(fd, cmd, va_arg(ap, void *));
248 * Cancellation behavior:
249 * Thread may be canceled after system call.
254 struct pthread *curthread;
257 curthread = _get_curthread();
258 _thr_cancel_enter2(curthread, 0);
259 ret = __sys_fsync(fd);
260 _thr_cancel_leave(curthread, 1);
266 * Cancellation behavior:
267 * Thread may be canceled after system call.
270 __thr_msync(void *addr, size_t len, int flags)
272 struct pthread *curthread;
275 curthread = _get_curthread();
276 _thr_cancel_enter2(curthread, 0);
277 ret = __sys_msync(addr, len, flags);
278 _thr_cancel_leave(curthread, 1);
284 __thr_nanosleep(const struct timespec *time_to_sleep,
285 struct timespec *time_remaining)
287 struct pthread *curthread;
290 curthread = _get_curthread();
291 _thr_cancel_enter(curthread);
292 ret = __sys_nanosleep(time_to_sleep, time_remaining);
293 _thr_cancel_leave(curthread, 1);
299 * Cancellation behavior:
300 * If the thread is canceled, file is not opened.
303 __thr_open(const char *path, int flags,...)
305 struct pthread *curthread;
309 /* Check if the file is being created: */
310 if ((flags & O_CREAT) != 0) {
311 /* Get the creation mode: */
313 mode = va_arg(ap, int);
319 curthread = _get_curthread();
320 _thr_cancel_enter(curthread);
321 ret = __sys_open(path, flags, mode);
322 _thr_cancel_leave(curthread, ret == -1);
328 * Cancellation behavior:
329 * If the thread is canceled, file is not opened.
332 __thr_openat(int fd, const char *path, int flags, ...)
334 struct pthread *curthread;
339 /* Check if the file is being created: */
340 if ((flags & O_CREAT) != 0) {
341 /* Get the creation mode: */
343 mode = va_arg(ap, int);
349 curthread = _get_curthread();
350 _thr_cancel_enter(curthread);
351 ret = __sys_openat(fd, path, flags, mode);
352 _thr_cancel_leave(curthread, ret == -1);
358 * Cancellation behavior:
359 * Thread may be canceled at start, but if the system call returns something,
360 * the thread is not canceled.
363 __thr_poll(struct pollfd *fds, unsigned int nfds, int timeout)
365 struct pthread *curthread;
368 curthread = _get_curthread();
369 _thr_cancel_enter(curthread);
370 ret = __sys_poll(fds, nfds, timeout);
371 _thr_cancel_leave(curthread, ret == -1);
377 * Cancellation behavior:
378 * Thread may be canceled at start, but if the system call returns something,
379 * the thread is not canceled.
382 __thr_pselect(int count, fd_set *rfds, fd_set *wfds, fd_set *efds,
383 const struct timespec *timo, const sigset_t *mask)
385 struct pthread *curthread;
388 curthread = _get_curthread();
389 _thr_cancel_enter(curthread);
390 ret = __sys_pselect(count, rfds, wfds, efds, timo, mask);
391 _thr_cancel_leave(curthread, ret == -1);
397 * Cancellation behavior:
398 * Thread may be canceled at start, but if the system call got some data,
399 * the thread is not canceled.
402 __thr_read(int fd, void *buf, size_t nbytes)
404 struct pthread *curthread;
407 curthread = _get_curthread();
408 _thr_cancel_enter(curthread);
409 ret = __sys_read(fd, buf, nbytes);
410 _thr_cancel_leave(curthread, ret == -1);
416 * Cancellation behavior:
417 * Thread may be canceled at start, but if the system call got some data,
418 * the thread is not canceled.
421 __thr_readv(int fd, const struct iovec *iov, int iovcnt)
423 struct pthread *curthread;
426 curthread = _get_curthread();
427 _thr_cancel_enter(curthread);
428 ret = __sys_readv(fd, iov, iovcnt);
429 _thr_cancel_leave(curthread, ret == -1);
434 * Cancellation behavior:
435 * Thread may be canceled at start, but if the system call got some data,
436 * the thread is not canceled.
439 __thr_recvfrom(int s, void *b, size_t l, int f, struct sockaddr *from,
442 struct pthread *curthread;
445 curthread = _get_curthread();
446 _thr_cancel_enter(curthread);
447 ret = __sys_recvfrom(s, b, l, f, from, fl);
448 _thr_cancel_leave(curthread, ret == -1);
453 * Cancellation behavior:
454 * Thread may be canceled at start, but if the system call got some data,
455 * the thread is not canceled.
458 __thr_recvmsg(int s, struct msghdr *m, int f)
460 struct pthread *curthread;
463 curthread = _get_curthread();
464 _thr_cancel_enter(curthread);
465 ret = __sys_recvmsg(s, m, f);
466 _thr_cancel_leave(curthread, ret == -1);
471 * Cancellation behavior:
472 * Thread may be canceled at start, but if the system call returns something,
473 * the thread is not canceled.
476 __thr_select(int numfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
477 struct timeval *timeout)
479 struct pthread *curthread;
482 curthread = _get_curthread();
483 _thr_cancel_enter(curthread);
484 ret = __sys_select(numfds, readfds, writefds, exceptfds, timeout);
485 _thr_cancel_leave(curthread, ret == -1);
490 * Cancellation behavior:
491 * Thread may be canceled at start, but if the system call sent
492 * data, the thread is not canceled.
495 __thr_sendmsg(int s, const struct msghdr *m, int f)
497 struct pthread *curthread;
500 curthread = _get_curthread();
501 _thr_cancel_enter(curthread);
502 ret = __sys_sendmsg(s, m, f);
503 _thr_cancel_leave(curthread, ret <= 0);
508 * Cancellation behavior:
509 * Thread may be canceled at start, but if the system call sent some
510 * data, the thread is not canceled.
513 __thr_sendto(int s, const void *m, size_t l, int f, const struct sockaddr *t,
516 struct pthread *curthread;
519 curthread = _get_curthread();
520 _thr_cancel_enter(curthread);
521 ret = __sys_sendto(s, m, l, f, t, tl);
522 _thr_cancel_leave(curthread, ret <= 0);
527 __thr_sleep(unsigned int seconds)
529 struct pthread *curthread;
532 curthread = _get_curthread();
533 _thr_cancel_enter(curthread);
534 ret = __libc_sleep(seconds);
535 _thr_cancel_leave(curthread, 1);
540 __thr_system(const char *string)
542 struct pthread *curthread;
545 curthread = _get_curthread();
546 _thr_cancel_enter(curthread);
547 ret = __libc_system(string);
548 _thr_cancel_leave(curthread, 1);
553 * Cancellation behavior:
554 * If thread is canceled, the system call is not completed,
555 * this means not all bytes were drained.
558 __thr_tcdrain(int fd)
560 struct pthread *curthread;
563 curthread = _get_curthread();
564 _thr_cancel_enter(curthread);
565 ret = __libc_tcdrain(fd);
566 _thr_cancel_leave(curthread, ret == -1);
571 __thr_usleep(useconds_t useconds)
573 struct pthread *curthread;
576 curthread = _get_curthread();
577 _thr_cancel_enter(curthread);
578 ret = __libc_usleep(useconds);
579 _thr_cancel_leave(curthread, 1);
584 * Cancellation behavior:
585 * Thread may be canceled at start, but if the system call returns
586 * a child pid, the thread is not canceled.
589 __thr_wait(int *istat)
591 struct pthread *curthread;
594 curthread = _get_curthread();
595 _thr_cancel_enter(curthread);
596 ret = __libc_wait(istat);
597 _thr_cancel_leave(curthread, ret <= 0);
602 * Cancellation behavior:
603 * Thread may be canceled at start, but if the system call returns
604 * a child pid, the thread is not canceled.
607 __thr_wait3(int *status, int options, struct rusage *rusage)
609 struct pthread *curthread;
612 curthread = _get_curthread();
613 _thr_cancel_enter(curthread);
614 ret = __libc_wait3(status, options, rusage);
615 _thr_cancel_leave(curthread, ret <= 0);
620 * Cancellation behavior:
621 * Thread may be canceled at start, but if the system call returns
622 * a child pid, the thread is not canceled.
625 __thr_wait4(pid_t pid, int *status, int options, struct rusage *rusage)
627 struct pthread *curthread;
630 curthread = _get_curthread();
631 _thr_cancel_enter(curthread);
632 ret = __sys_wait4(pid, status, options, rusage);
633 _thr_cancel_leave(curthread, ret <= 0);
638 * Cancellation behavior:
639 * Thread may be canceled at start, but if the system call returns
640 * a child pid, the thread is not canceled.
643 __thr_waitpid(pid_t wpid, int *status, int options)
645 struct pthread *curthread;
648 curthread = _get_curthread();
649 _thr_cancel_enter(curthread);
650 ret = __libc_waitpid(wpid, status, options);
651 _thr_cancel_leave(curthread, ret <= 0);
656 * Cancellation behavior:
657 * Thread may be canceled at start, but if the thread wrote some data,
658 * it is not canceled.
661 __thr_write(int fd, const void *buf, size_t nbytes)
663 struct pthread *curthread;
666 curthread = _get_curthread();
667 _thr_cancel_enter(curthread);
668 ret = __sys_write(fd, buf, nbytes);
669 _thr_cancel_leave(curthread, (ret <= 0));
674 * Cancellation behavior:
675 * Thread may be canceled at start, but if the thread wrote some data,
676 * it is not canceled.
679 __thr_writev(int fd, const struct iovec *iov, int iovcnt)
681 struct pthread *curthread;
684 curthread = _get_curthread();
685 _thr_cancel_enter(curthread);
686 ret = __sys_writev(fd, iov, iovcnt);
687 _thr_cancel_leave(curthread, (ret <= 0));
692 __thr_interpose_libc(void)
695 __error_selector = __error_threaded;
697 *(__libc_interposing_slot(INTERPOS_##name)) = \
698 (interpos_func_t)__thr_##name;
742 *(__libc_interposing_slot(
743 INTERPOS__pthread_mutex_init_calloc_cb)) =
744 (interpos_func_t)_pthread_mutex_init_calloc_cb;