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 __thr_accept(int s, struct sockaddr *addr, socklen_t *addrlen)
105 struct pthread *curthread;
108 curthread = _get_curthread();
109 _thr_cancel_enter(curthread);
110 ret = __sys_accept(s, addr, addrlen);
111 _thr_cancel_leave(curthread, ret == -1);
117 * Cancellation behavior:
118 * If thread is canceled, no socket is created.
121 __thr_accept4(int s, struct sockaddr *addr, socklen_t *addrlen, int flags)
123 struct pthread *curthread;
126 curthread = _get_curthread();
127 _thr_cancel_enter(curthread);
128 ret = __sys_accept4(s, addr, addrlen, flags);
129 _thr_cancel_leave(curthread, ret == -1);
135 __thr_aio_suspend(const struct aiocb * const iocbs[], int niocb, const struct
138 struct pthread *curthread;
141 curthread = _get_curthread();
142 _thr_cancel_enter(curthread);
143 ret = __sys_aio_suspend(iocbs, niocb, timeout);
144 _thr_cancel_leave(curthread, 1);
150 * Cancellation behavior:
151 * According to manual of close(), the file descriptor is always deleted.
152 * Here, thread is only canceled after the system call, so the file
153 * descriptor is always deleted despite whether the thread is canceled
159 struct pthread *curthread;
162 curthread = _get_curthread();
163 _thr_cancel_enter2(curthread, 0);
164 ret = __sys_close(fd);
165 _thr_cancel_leave(curthread, 1);
171 * Cancellation behavior:
172 * If the thread is canceled, connection is not made.
175 __thr_connect(int fd, const struct sockaddr *name, socklen_t namelen)
177 struct pthread *curthread;
180 curthread = _get_curthread();
181 _thr_cancel_enter(curthread);
182 ret = __sys_connect(fd, name, namelen);
183 _thr_cancel_leave(curthread, ret == -1);
189 * Cancellation behavior:
190 * According to specification, only F_SETLKW is a cancellation point.
191 * Thread is only canceled at start, or canceled if the system call
192 * is failure, this means the function does not generate side effect
196 __thr_fcntl(int fd, int cmd, ...)
198 struct pthread *curthread;
202 curthread = _get_curthread();
204 if (cmd == F_OSETLKW || cmd == F_SETLKW) {
205 _thr_cancel_enter(curthread);
206 #ifdef SYSCALL_COMPAT
207 ret = __fcntl_compat(fd, cmd, va_arg(ap, void *));
209 ret = __sys_fcntl(fd, cmd, va_arg(ap, void *));
211 _thr_cancel_leave(curthread, ret == -1);
213 #ifdef SYSCALL_COMPAT
214 ret = __fcntl_compat(fd, cmd, va_arg(ap, void *));
216 ret = __sys_fcntl(fd, cmd, va_arg(ap, void *));
225 * Cancellation behavior:
226 * Thread may be canceled after system call.
231 struct pthread *curthread;
234 curthread = _get_curthread();
235 _thr_cancel_enter2(curthread, 0);
236 ret = __sys_fsync(fd);
237 _thr_cancel_leave(curthread, 1);
243 * Cancellation behavior:
244 * Thread may be canceled after system call.
247 __thr_msync(void *addr, size_t len, int flags)
249 struct pthread *curthread;
252 curthread = _get_curthread();
253 _thr_cancel_enter2(curthread, 0);
254 ret = __sys_msync(addr, len, flags);
255 _thr_cancel_leave(curthread, 1);
261 __thr_nanosleep(const struct timespec *time_to_sleep,
262 struct timespec *time_remaining)
264 struct pthread *curthread;
267 curthread = _get_curthread();
268 _thr_cancel_enter(curthread);
269 ret = __sys_nanosleep(time_to_sleep, time_remaining);
270 _thr_cancel_leave(curthread, 1);
276 * Cancellation behavior:
277 * If the thread is canceled, file is not opened.
280 __thr_openat(int fd, const char *path, int flags, ...)
282 struct pthread *curthread;
287 /* Check if the file is being created: */
288 if ((flags & O_CREAT) != 0) {
289 /* Get the creation mode: */
291 mode = va_arg(ap, int);
297 curthread = _get_curthread();
298 _thr_cancel_enter(curthread);
299 ret = __sys_openat(fd, path, flags, mode);
300 _thr_cancel_leave(curthread, ret == -1);
306 * Cancellation behavior:
307 * Thread may be canceled at start, but if the system call returns something,
308 * the thread is not canceled.
311 __thr_poll(struct pollfd *fds, unsigned int nfds, int timeout)
313 struct pthread *curthread;
316 curthread = _get_curthread();
317 _thr_cancel_enter(curthread);
318 ret = __sys_poll(fds, nfds, timeout);
319 _thr_cancel_leave(curthread, ret == -1);
325 * Cancellation behavior:
326 * Thread may be canceled at start, but if the system call returns something,
327 * the thread is not canceled.
330 __thr_ppoll(struct pollfd pfd[], nfds_t nfds, const struct timespec *
331 timeout, const sigset_t *newsigmask)
333 struct pthread *curthread;
336 curthread = _get_curthread();
337 _thr_cancel_enter(curthread);
338 ret = __sys_ppoll(pfd, nfds, timeout, newsigmask);
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_pselect(int count, fd_set *rfds, fd_set *wfds, fd_set *efds,
351 const struct timespec *timo, const sigset_t *mask)
353 struct pthread *curthread;
356 curthread = _get_curthread();
357 _thr_cancel_enter(curthread);
358 ret = __sys_pselect(count, rfds, wfds, efds, timo, mask);
359 _thr_cancel_leave(curthread, ret == -1);
365 __thr_kevent(int kq, const struct kevent *changelist, int nchanges,
366 struct kevent *eventlist, int nevents, const struct timespec *timeout)
368 struct pthread *curthread;
373 * No blocking, do not make the call cancellable.
375 return (__sys_kevent(kq, changelist, nchanges, eventlist,
378 curthread = _get_curthread();
379 _thr_cancel_enter(curthread);
380 ret = __sys_kevent(kq, changelist, nchanges, eventlist, nevents,
382 _thr_cancel_leave(curthread, ret == -1 && nchanges == 0);
388 * Cancellation behavior:
389 * Thread may be canceled at start, but if the system call got some data,
390 * the thread is not canceled.
393 __thr_read(int fd, void *buf, size_t nbytes)
395 struct pthread *curthread;
398 curthread = _get_curthread();
399 _thr_cancel_enter(curthread);
400 ret = __sys_read(fd, buf, nbytes);
401 _thr_cancel_leave(curthread, ret == -1);
407 * Cancellation behavior:
408 * Thread may be canceled at start, but if the system call got some data,
409 * the thread is not canceled.
412 __thr_readv(int fd, const struct iovec *iov, int iovcnt)
414 struct pthread *curthread;
417 curthread = _get_curthread();
418 _thr_cancel_enter(curthread);
419 ret = __sys_readv(fd, iov, iovcnt);
420 _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_recvfrom(int s, void *b, size_t l, int f, struct sockaddr *from,
433 struct pthread *curthread;
436 curthread = _get_curthread();
437 _thr_cancel_enter(curthread);
438 ret = __sys_recvfrom(s, b, l, f, from, fl);
439 _thr_cancel_leave(curthread, ret == -1);
444 * Cancellation behavior:
445 * Thread may be canceled at start, but if the system call got some data,
446 * the thread is not canceled.
449 __thr_recvmsg(int s, struct msghdr *m, int f)
451 struct pthread *curthread;
454 curthread = _get_curthread();
455 _thr_cancel_enter(curthread);
456 ret = __sys_recvmsg(s, m, f);
457 _thr_cancel_leave(curthread, ret == -1);
462 * Cancellation behavior:
463 * Thread may be canceled at start, but if the system call returns something,
464 * the thread is not canceled.
467 __thr_select(int numfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
468 struct timeval *timeout)
470 struct pthread *curthread;
473 curthread = _get_curthread();
474 _thr_cancel_enter(curthread);
475 ret = __sys_select(numfds, readfds, writefds, exceptfds, timeout);
476 _thr_cancel_leave(curthread, ret == -1);
481 * Cancellation behavior:
482 * Thread may be canceled at start, but if the system call sent
483 * data, the thread is not canceled.
486 __thr_sendmsg(int s, const struct msghdr *m, int f)
488 struct pthread *curthread;
491 curthread = _get_curthread();
492 _thr_cancel_enter(curthread);
493 ret = __sys_sendmsg(s, m, f);
494 _thr_cancel_leave(curthread, ret <= 0);
499 * Cancellation behavior:
500 * Thread may be canceled at start, but if the system call sent some
501 * data, the thread is not canceled.
504 __thr_sendto(int s, const void *m, size_t l, int f, const struct sockaddr *t,
507 struct pthread *curthread;
510 curthread = _get_curthread();
511 _thr_cancel_enter(curthread);
512 ret = __sys_sendto(s, m, l, f, t, tl);
513 _thr_cancel_leave(curthread, ret <= 0);
518 __thr_system(const char *string)
520 struct pthread *curthread;
523 curthread = _get_curthread();
524 _thr_cancel_enter(curthread);
525 ret = __libc_system(string);
526 _thr_cancel_leave(curthread, 1);
531 * Cancellation behavior:
532 * If thread is canceled, the system call is not completed,
533 * this means not all bytes were drained.
536 __thr_tcdrain(int fd)
538 struct pthread *curthread;
541 curthread = _get_curthread();
542 _thr_cancel_enter(curthread);
543 ret = __libc_tcdrain(fd);
544 _thr_cancel_leave(curthread, ret == -1);
549 * Cancellation behavior:
550 * Thread may be canceled at start, but if the system call returns
551 * a child pid, the thread is not canceled.
554 __thr_wait4(pid_t pid, int *status, int options, struct rusage *rusage)
556 struct pthread *curthread;
559 curthread = _get_curthread();
560 _thr_cancel_enter(curthread);
561 ret = __sys_wait4(pid, status, options, rusage);
562 _thr_cancel_leave(curthread, ret <= 0);
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_wait6(idtype_t idtype, id_t id, int *status, int options,
573 struct __wrusage *ru, siginfo_t *infop)
575 struct pthread *curthread;
578 curthread = _get_curthread();
579 _thr_cancel_enter(curthread);
580 ret = __sys_wait6(idtype, id, status, options, ru, infop);
581 _thr_cancel_leave(curthread, ret <= 0);
586 * Cancellation behavior:
587 * Thread may be canceled at start, but if the thread wrote some data,
588 * it is not canceled.
591 __thr_write(int fd, const void *buf, size_t nbytes)
593 struct pthread *curthread;
596 curthread = _get_curthread();
597 _thr_cancel_enter(curthread);
598 ret = __sys_write(fd, buf, nbytes);
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_writev(int fd, const struct iovec *iov, int iovcnt)
611 struct pthread *curthread;
614 curthread = _get_curthread();
615 _thr_cancel_enter(curthread);
616 ret = __sys_writev(fd, iov, iovcnt);
617 _thr_cancel_leave(curthread, (ret <= 0));
622 __thr_interpose_libc(void)
625 __set_error_selector(__error_threaded);
627 *(__libc_interposing_slot(INTERPOS_##name)) = \
628 (interpos_func_t)__thr_##name;
667 SLOT(map_stacks_exec);
669 *(__libc_interposing_slot(
670 INTERPOS__pthread_mutex_init_calloc_cb)) =
671 (interpos_func_t)_pthread_mutex_init_calloc_cb;