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 * Cancellation behavior:
232 * Thread may be canceled after system call.
235 __thr_msync(void *addr, size_t len, int flags)
237 struct pthread *curthread;
240 curthread = _get_curthread();
241 _thr_cancel_enter2(curthread, 0);
242 ret = __sys_msync(addr, len, flags);
243 _thr_cancel_leave(curthread, 1);
249 __thr_nanosleep(const struct timespec *time_to_sleep,
250 struct timespec *time_remaining)
252 struct pthread *curthread;
255 curthread = _get_curthread();
256 _thr_cancel_enter(curthread);
257 ret = __sys_nanosleep(time_to_sleep, time_remaining);
258 _thr_cancel_leave(curthread, 1);
264 * Cancellation behavior:
265 * If the thread is canceled, file is not opened.
268 __thr_openat(int fd, const char *path, int flags, ...)
270 struct pthread *curthread;
275 /* Check if the file is being created: */
276 if ((flags & O_CREAT) != 0) {
277 /* Get the creation mode: */
279 mode = va_arg(ap, int);
285 curthread = _get_curthread();
286 _thr_cancel_enter(curthread);
287 ret = __sys_openat(fd, path, flags, mode);
288 _thr_cancel_leave(curthread, ret == -1);
294 * Cancellation behavior:
295 * Thread may be canceled at start, but if the system call returns something,
296 * the thread is not canceled.
299 __thr_poll(struct pollfd *fds, unsigned int nfds, int timeout)
301 struct pthread *curthread;
304 curthread = _get_curthread();
305 _thr_cancel_enter(curthread);
306 ret = __sys_poll(fds, nfds, timeout);
307 _thr_cancel_leave(curthread, ret == -1);
313 * Cancellation behavior:
314 * Thread may be canceled at start, but if the system call returns something,
315 * the thread is not canceled.
318 __thr_ppoll(struct pollfd pfd[], nfds_t nfds, const struct timespec *
319 timeout, const sigset_t *newsigmask)
321 struct pthread *curthread;
324 curthread = _get_curthread();
325 _thr_cancel_enter(curthread);
326 ret = __sys_ppoll(pfd, nfds, timeout, newsigmask);
327 _thr_cancel_leave(curthread, ret == -1);
333 * Cancellation behavior:
334 * Thread may be canceled at start, but if the system call returns something,
335 * the thread is not canceled.
338 __thr_pselect(int count, fd_set *rfds, fd_set *wfds, fd_set *efds,
339 const struct timespec *timo, const sigset_t *mask)
341 struct pthread *curthread;
344 curthread = _get_curthread();
345 _thr_cancel_enter(curthread);
346 ret = __sys_pselect(count, rfds, wfds, efds, timo, mask);
347 _thr_cancel_leave(curthread, ret == -1);
353 __thr_kevent(int kq, const struct kevent *changelist, int nchanges,
354 struct kevent *eventlist, int nevents, const struct timespec *timeout)
356 struct pthread *curthread;
361 * No blocking, do not make the call cancellable.
363 return (__sys_kevent(kq, changelist, nchanges, eventlist,
366 curthread = _get_curthread();
367 _thr_cancel_enter(curthread);
368 ret = __sys_kevent(kq, changelist, nchanges, eventlist, nevents,
370 _thr_cancel_leave(curthread, ret == -1 && nchanges == 0);
376 * Cancellation behavior:
377 * Thread may be canceled at start, but if the system call got some data,
378 * the thread is not canceled.
381 __thr_read(int fd, void *buf, size_t nbytes)
383 struct pthread *curthread;
386 curthread = _get_curthread();
387 _thr_cancel_enter(curthread);
388 ret = __sys_read(fd, buf, nbytes);
389 _thr_cancel_leave(curthread, ret == -1);
395 * Cancellation behavior:
396 * Thread may be canceled at start, but if the system call got some data,
397 * the thread is not canceled.
400 __thr_readv(int fd, const struct iovec *iov, int iovcnt)
402 struct pthread *curthread;
405 curthread = _get_curthread();
406 _thr_cancel_enter(curthread);
407 ret = __sys_readv(fd, iov, iovcnt);
408 _thr_cancel_leave(curthread, ret == -1);
413 * Cancellation behavior:
414 * Thread may be canceled at start, but if the system call got some data,
415 * the thread is not canceled.
418 __thr_recvfrom(int s, void *b, size_t l, int f, struct sockaddr *from,
421 struct pthread *curthread;
424 curthread = _get_curthread();
425 _thr_cancel_enter(curthread);
426 ret = __sys_recvfrom(s, b, l, f, from, fl);
427 _thr_cancel_leave(curthread, ret == -1);
432 * Cancellation behavior:
433 * Thread may be canceled at start, but if the system call got some data,
434 * the thread is not canceled.
437 __thr_recvmsg(int s, struct msghdr *m, int f)
439 struct pthread *curthread;
442 curthread = _get_curthread();
443 _thr_cancel_enter(curthread);
444 ret = __sys_recvmsg(s, m, f);
445 _thr_cancel_leave(curthread, ret == -1);
450 * Cancellation behavior:
451 * Thread may be canceled at start, but if the system call returns something,
452 * the thread is not canceled.
455 __thr_select(int numfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
456 struct timeval *timeout)
458 struct pthread *curthread;
461 curthread = _get_curthread();
462 _thr_cancel_enter(curthread);
463 ret = __sys_select(numfds, readfds, writefds, exceptfds, timeout);
464 _thr_cancel_leave(curthread, ret == -1);
469 * Cancellation behavior:
470 * Thread may be canceled at start, but if the system call sent
471 * data, the thread is not canceled.
474 __thr_sendmsg(int s, const struct msghdr *m, int f)
476 struct pthread *curthread;
479 curthread = _get_curthread();
480 _thr_cancel_enter(curthread);
481 ret = __sys_sendmsg(s, m, f);
482 _thr_cancel_leave(curthread, ret <= 0);
487 * Cancellation behavior:
488 * Thread may be canceled at start, but if the system call sent some
489 * data, the thread is not canceled.
492 __thr_sendto(int s, const void *m, size_t l, int f, const struct sockaddr *t,
495 struct pthread *curthread;
498 curthread = _get_curthread();
499 _thr_cancel_enter(curthread);
500 ret = __sys_sendto(s, m, l, f, t, tl);
501 _thr_cancel_leave(curthread, ret <= 0);
506 __thr_system(const char *string)
508 struct pthread *curthread;
511 curthread = _get_curthread();
512 _thr_cancel_enter(curthread);
513 ret = __libc_system(string);
514 _thr_cancel_leave(curthread, 1);
519 * Cancellation behavior:
520 * If thread is canceled, the system call is not completed,
521 * this means not all bytes were drained.
524 __thr_tcdrain(int fd)
526 struct pthread *curthread;
529 curthread = _get_curthread();
530 _thr_cancel_enter(curthread);
531 ret = __libc_tcdrain(fd);
532 _thr_cancel_leave(curthread, ret == -1);
537 * Cancellation behavior:
538 * Thread may be canceled at start, but if the system call returns
539 * a child pid, the thread is not canceled.
542 __thr_wait4(pid_t pid, int *status, int options, struct rusage *rusage)
544 struct pthread *curthread;
547 curthread = _get_curthread();
548 _thr_cancel_enter(curthread);
549 ret = __sys_wait4(pid, status, options, rusage);
550 _thr_cancel_leave(curthread, ret <= 0);
555 * Cancellation behavior:
556 * Thread may be canceled at start, but if the system call returns
557 * a child pid, the thread is not canceled.
560 __thr_wait6(idtype_t idtype, id_t id, int *status, int options,
561 struct __wrusage *ru, siginfo_t *infop)
563 struct pthread *curthread;
566 curthread = _get_curthread();
567 _thr_cancel_enter(curthread);
568 ret = __sys_wait6(idtype, id, status, options, ru, infop);
569 _thr_cancel_leave(curthread, ret <= 0);
574 * Cancellation behavior:
575 * Thread may be canceled at start, but if the thread wrote some data,
576 * it is not canceled.
579 __thr_write(int fd, const void *buf, size_t nbytes)
581 struct pthread *curthread;
584 curthread = _get_curthread();
585 _thr_cancel_enter(curthread);
586 ret = __sys_write(fd, buf, nbytes);
587 _thr_cancel_leave(curthread, (ret <= 0));
592 * Cancellation behavior:
593 * Thread may be canceled at start, but if the thread wrote some data,
594 * it is not canceled.
597 __thr_writev(int fd, const struct iovec *iov, int iovcnt)
599 struct pthread *curthread;
602 curthread = _get_curthread();
603 _thr_cancel_enter(curthread);
604 ret = __sys_writev(fd, iov, iovcnt);
605 _thr_cancel_leave(curthread, (ret <= 0));
610 __thr_interpose_libc(void)
613 __set_error_selector(__error_threaded);
615 *(__libc_interposing_slot(INTERPOS_##name)) = \
616 (interpos_func_t)__thr_##name;
656 *(__libc_interposing_slot(
657 INTERPOS__pthread_mutex_init_calloc_cb)) =
658 (interpos_func_t)_pthread_mutex_init_calloc_cb;