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_pselect(int count, fd_set *rfds, fd_set *wfds, fd_set *efds,
331 const struct timespec *timo, const sigset_t *mask)
333 struct pthread *curthread;
336 curthread = _get_curthread();
337 _thr_cancel_enter(curthread);
338 ret = __sys_pselect(count, rfds, wfds, efds, timo, mask);
339 _thr_cancel_leave(curthread, ret == -1);
345 * Cancellation behavior:
346 * Thread may be canceled at start, but if the system call got some data,
347 * the thread is not canceled.
350 __thr_read(int fd, void *buf, size_t nbytes)
352 struct pthread *curthread;
355 curthread = _get_curthread();
356 _thr_cancel_enter(curthread);
357 ret = __sys_read(fd, buf, nbytes);
358 _thr_cancel_leave(curthread, ret == -1);
364 * Cancellation behavior:
365 * Thread may be canceled at start, but if the system call got some data,
366 * the thread is not canceled.
369 __thr_readv(int fd, const struct iovec *iov, int iovcnt)
371 struct pthread *curthread;
374 curthread = _get_curthread();
375 _thr_cancel_enter(curthread);
376 ret = __sys_readv(fd, iov, iovcnt);
377 _thr_cancel_leave(curthread, ret == -1);
382 * Cancellation behavior:
383 * Thread may be canceled at start, but if the system call got some data,
384 * the thread is not canceled.
387 __thr_recvfrom(int s, void *b, size_t l, int f, struct sockaddr *from,
390 struct pthread *curthread;
393 curthread = _get_curthread();
394 _thr_cancel_enter(curthread);
395 ret = __sys_recvfrom(s, b, l, f, from, fl);
396 _thr_cancel_leave(curthread, ret == -1);
401 * Cancellation behavior:
402 * Thread may be canceled at start, but if the system call got some data,
403 * the thread is not canceled.
406 __thr_recvmsg(int s, struct msghdr *m, int f)
408 struct pthread *curthread;
411 curthread = _get_curthread();
412 _thr_cancel_enter(curthread);
413 ret = __sys_recvmsg(s, m, f);
414 _thr_cancel_leave(curthread, ret == -1);
419 * Cancellation behavior:
420 * Thread may be canceled at start, but if the system call returns something,
421 * the thread is not canceled.
424 __thr_select(int numfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
425 struct timeval *timeout)
427 struct pthread *curthread;
430 curthread = _get_curthread();
431 _thr_cancel_enter(curthread);
432 ret = __sys_select(numfds, readfds, writefds, exceptfds, timeout);
433 _thr_cancel_leave(curthread, ret == -1);
438 * Cancellation behavior:
439 * Thread may be canceled at start, but if the system call sent
440 * data, the thread is not canceled.
443 __thr_sendmsg(int s, const struct msghdr *m, int f)
445 struct pthread *curthread;
448 curthread = _get_curthread();
449 _thr_cancel_enter(curthread);
450 ret = __sys_sendmsg(s, m, f);
451 _thr_cancel_leave(curthread, ret <= 0);
456 * Cancellation behavior:
457 * Thread may be canceled at start, but if the system call sent some
458 * data, the thread is not canceled.
461 __thr_sendto(int s, const void *m, size_t l, int f, const struct sockaddr *t,
464 struct pthread *curthread;
467 curthread = _get_curthread();
468 _thr_cancel_enter(curthread);
469 ret = __sys_sendto(s, m, l, f, t, tl);
470 _thr_cancel_leave(curthread, ret <= 0);
475 __thr_system(const char *string)
477 struct pthread *curthread;
480 curthread = _get_curthread();
481 _thr_cancel_enter(curthread);
482 ret = __libc_system(string);
483 _thr_cancel_leave(curthread, 1);
488 * Cancellation behavior:
489 * If thread is canceled, the system call is not completed,
490 * this means not all bytes were drained.
493 __thr_tcdrain(int fd)
495 struct pthread *curthread;
498 curthread = _get_curthread();
499 _thr_cancel_enter(curthread);
500 ret = __libc_tcdrain(fd);
501 _thr_cancel_leave(curthread, ret == -1);
506 * Cancellation behavior:
507 * Thread may be canceled at start, but if the system call returns
508 * a child pid, the thread is not canceled.
511 __thr_wait4(pid_t pid, int *status, int options, struct rusage *rusage)
513 struct pthread *curthread;
516 curthread = _get_curthread();
517 _thr_cancel_enter(curthread);
518 ret = __sys_wait4(pid, status, options, rusage);
519 _thr_cancel_leave(curthread, ret <= 0);
524 * Cancellation behavior:
525 * Thread may be canceled at start, but if the thread wrote some data,
526 * it is not canceled.
529 __thr_write(int fd, const void *buf, size_t nbytes)
531 struct pthread *curthread;
534 curthread = _get_curthread();
535 _thr_cancel_enter(curthread);
536 ret = __sys_write(fd, buf, nbytes);
537 _thr_cancel_leave(curthread, (ret <= 0));
542 * Cancellation behavior:
543 * Thread may be canceled at start, but if the thread wrote some data,
544 * it is not canceled.
547 __thr_writev(int fd, const struct iovec *iov, int iovcnt)
549 struct pthread *curthread;
552 curthread = _get_curthread();
553 _thr_cancel_enter(curthread);
554 ret = __sys_writev(fd, iov, iovcnt);
555 _thr_cancel_leave(curthread, (ret <= 0));
560 __thr_interpose_libc(void)
563 __set_error_selector(__error_threaded);
565 *(__libc_interposing_slot(INTERPOS_##name)) = \
566 (interpos_func_t)__thr_##name;
603 *(__libc_interposing_slot(
604 INTERPOS__pthread_mutex_init_calloc_cb)) =
605 (interpos_func_t)_pthread_mutex_init_calloc_cb;