2 * Copyright (C) 2005 David Xu <davidxu@freebsd.org>.
3 * Copyright (c) 2003 Daniel Eischen <deischen@freebsd.org>.
4 * Copyright (C) 2000 Jason Evans <jasone@freebsd.org>.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice(s), this list of conditions and the following disclaimer as
12 * the first lines of this file unmodified other than the possible
13 * addition of one or more copyright notices.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice(s), this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
20 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
26 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
28 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
29 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au>
36 * All rights reserved.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. Neither the name of the author nor the names of any co-contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
50 * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 #include "namespace.h"
65 #include <sys/types.h>
67 #include <sys/param.h>
68 #include <sys/select.h>
69 #include <sys/signalvar.h>
70 #include <sys/socket.h>
88 #include "un-namespace.h"
90 #include "thr_private.h"
92 extern int __creat(const char *, mode_t);
93 extern int __pselect(int, fd_set *, fd_set *, fd_set *,
94 const struct timespec *, const sigset_t *);
95 extern unsigned __sleep(unsigned int);
96 extern int __system(const char *);
97 extern int __tcdrain(int);
98 extern int __usleep(useconds_t);
99 extern pid_t __wait(int *);
100 extern pid_t __waitpid(pid_t, int *, int);
101 extern int __sys_aio_suspend(const struct aiocb * const[], int,
102 const struct timespec *);
103 extern int __sys_accept(int, struct sockaddr *, socklen_t *);
104 extern int __sys_connect(int, const struct sockaddr *, socklen_t);
105 extern int __sys_fsync(int);
106 extern int __sys_msync(void *, size_t, int);
107 extern int __sys_pselect(int, fd_set *, fd_set *, fd_set *,
108 const struct timespec *, const sigset_t *);
109 extern int __sys_poll(struct pollfd *, unsigned, int);
110 extern ssize_t __sys_recv(int, void *, size_t, int);
111 extern ssize_t __sys_recvfrom(int, void *, size_t, int, struct sockaddr *, socklen_t *);
112 extern ssize_t __sys_recvmsg(int, struct msghdr *, int);
113 extern int __sys_select(int, fd_set *, fd_set *, fd_set *, struct timeval *);
114 extern int __sys_sendfile(int, int, off_t, size_t, struct sf_hdtr *,
116 extern ssize_t __sys_sendmsg(int, const struct msghdr *, int);
117 extern ssize_t __sys_sendto(int, const void *,size_t, int, const struct sockaddr *, socklen_t);
118 extern ssize_t __sys_readv(int, const struct iovec *, int);
119 extern pid_t __sys_wait4(pid_t, int *, int, struct rusage *);
120 extern ssize_t __sys_writev(int, const struct iovec *, int);
122 int ___creat(const char *, mode_t);
123 int ___pselect(int, fd_set *, fd_set *, fd_set *,
124 const struct timespec *, const sigset_t *);
125 unsigned ___sleep(unsigned);
126 int ___system(const char *);
128 int ___usleep(useconds_t useconds);
129 pid_t ___wait(int *);
130 pid_t ___waitpid(pid_t, int *, int);
131 int __accept(int, struct sockaddr *, socklen_t *);
132 int __aio_suspend(const struct aiocb * const iocbs[], int,
133 const struct timespec *);
135 int __connect(int, const struct sockaddr *, socklen_t);
136 int __fcntl(int, int,...);
137 #ifdef SYSCALL_COMPAT
138 extern int __fcntl_compat(int, int,...);
141 int __msync(void *, size_t, int);
142 int __nanosleep(const struct timespec *, struct timespec *);
143 int __open(const char *, int,...);
144 int __openat(int, const char *, int,...);
145 int __poll(struct pollfd *, unsigned int, int);
146 ssize_t __read(int, void *buf, size_t);
147 ssize_t __readv(int, const struct iovec *, int);
148 ssize_t __recvfrom(int, void *, size_t, int f, struct sockaddr *, socklen_t *);
149 ssize_t __recvmsg(int, struct msghdr *, int);
150 int __select(int, fd_set *, fd_set *, fd_set *, struct timeval *);
151 ssize_t __sendmsg(int, const struct msghdr *, int);
152 ssize_t __sendto(int, const void *, size_t, int,
153 const struct sockaddr *, socklen_t);
154 pid_t __wait3(int *, int, struct rusage *);
155 pid_t __wait4(pid_t, int *, int, struct rusage *);
156 ssize_t __write(int, const void *, size_t);
157 ssize_t __writev(int, const struct iovec *, int);
159 __weak_reference(__accept, accept);
162 * Cancellation behavior:
163 * If thread is canceled, no socket is created.
166 __accept(int s, struct sockaddr *addr, socklen_t *addrlen)
168 struct pthread *curthread;
171 curthread = _get_curthread();
172 _thr_cancel_enter(curthread);
173 ret = __sys_accept(s, addr, addrlen);
174 _thr_cancel_leave(curthread, ret == -1);
179 __weak_reference(__aio_suspend, aio_suspend);
182 __aio_suspend(const struct aiocb * const iocbs[], int niocb, const struct
185 struct pthread *curthread = _get_curthread();
188 _thr_cancel_enter(curthread);
189 ret = __sys_aio_suspend(iocbs, niocb, timeout);
190 _thr_cancel_leave(curthread, 1);
195 __weak_reference(__close, close);
198 * Cancellation behavior:
199 * According to manual of close(), the file descriptor is always deleted.
200 * Here, thread is only canceled after the system call, so the file
201 * descriptor is always deleted despite whether the thread is canceled
207 struct pthread *curthread = _get_curthread();
210 _thr_cancel_enter2(curthread, 0);
211 ret = __sys_close(fd);
212 _thr_cancel_leave(curthread, 1);
217 __weak_reference(__connect, connect);
220 * Cancellation behavior:
221 * If the thread is canceled, connection is not made.
224 __connect(int fd, const struct sockaddr *name, socklen_t namelen)
226 struct pthread *curthread = _get_curthread();
229 _thr_cancel_enter(curthread);
230 ret = __sys_connect(fd, name, namelen);
231 _thr_cancel_leave(curthread, ret == -1);
236 __weak_reference(___creat, creat);
239 * Cancellation behavior:
240 * If thread is canceled, file is not created.
243 ___creat(const char *path, mode_t mode)
245 struct pthread *curthread = _get_curthread();
248 _thr_cancel_enter(curthread);
249 ret = __creat(path, mode);
250 _thr_cancel_leave(curthread, ret == -1);
255 __weak_reference(__fcntl, fcntl);
258 * Cancellation behavior:
259 * According to specification, only F_SETLKW is a cancellation point.
260 * Thread is only canceled at start, or canceled if the system call
261 * is failure, this means the function does not generate side effect
265 __fcntl(int fd, int cmd,...)
267 struct pthread *curthread = _get_curthread();
272 if (cmd == F_OSETLKW || cmd == F_SETLKW) {
273 _thr_cancel_enter(curthread);
274 #ifdef SYSCALL_COMPAT
275 ret = __fcntl_compat(fd, cmd, va_arg(ap, void *));
277 ret = __sys_fcntl(fd, cmd, va_arg(ap, void *));
279 _thr_cancel_leave(curthread, ret == -1);
281 #ifdef SYSCALL_COMPAT
282 ret = __fcntl_compat(fd, cmd, va_arg(ap, void *));
284 ret = __sys_fcntl(fd, cmd, va_arg(ap, void *));
292 __weak_reference(__fsync, fsync);
295 * Cancellation behavior:
296 * Thread may be canceled after system call.
301 struct pthread *curthread = _get_curthread();
304 _thr_cancel_enter2(curthread, 0);
305 ret = __sys_fsync(fd);
306 _thr_cancel_leave(curthread, 1);
311 __weak_reference(__msync, msync);
314 * Cancellation behavior:
315 * Thread may be canceled after system call.
318 __msync(void *addr, size_t len, int flags)
320 struct pthread *curthread = _get_curthread();
323 _thr_cancel_enter2(curthread, 0);
324 ret = __sys_msync(addr, len, flags);
325 _thr_cancel_leave(curthread, 1);
330 __weak_reference(__nanosleep, nanosleep);
333 __nanosleep(const struct timespec *time_to_sleep,
334 struct timespec *time_remaining)
336 struct pthread *curthread = _get_curthread();
339 _thr_cancel_enter(curthread);
340 ret = __sys_nanosleep(time_to_sleep, time_remaining);
341 _thr_cancel_leave(curthread, 1);
346 __weak_reference(__open, open);
349 * Cancellation behavior:
350 * If the thread is canceled, file is not opened.
353 __open(const char *path, int flags,...)
355 struct pthread *curthread = _get_curthread();
360 /* Check if the file is being created: */
361 if (flags & O_CREAT) {
362 /* Get the creation mode: */
364 mode = va_arg(ap, int);
368 _thr_cancel_enter(curthread);
369 ret = __sys_open(path, flags, mode);
370 _thr_cancel_leave(curthread, ret == -1);
375 __weak_reference(__openat, openat);
378 * Cancellation behavior:
379 * If the thread is canceled, file is not opened.
382 __openat(int fd, const char *path, int flags, ...)
384 struct pthread *curthread = _get_curthread();
390 /* Check if the file is being created: */
391 if (flags & O_CREAT) {
392 /* Get the creation mode: */
394 mode = va_arg(ap, int);
398 _thr_cancel_enter(curthread);
399 ret = __sys_openat(fd, path, flags, mode);
400 _thr_cancel_leave(curthread, ret == -1);
405 __weak_reference(__poll, poll);
408 * Cancellation behavior:
409 * Thread may be canceled at start, but if the system call returns something,
410 * the thread is not canceled.
413 __poll(struct pollfd *fds, unsigned int nfds, int timeout)
415 struct pthread *curthread = _get_curthread();
418 _thr_cancel_enter(curthread);
419 ret = __sys_poll(fds, nfds, timeout);
420 _thr_cancel_leave(curthread, ret == -1);
425 __weak_reference(___pselect, pselect);
428 * Cancellation behavior:
429 * Thread may be canceled at start, but if the system call returns something,
430 * the thread is not canceled.
433 ___pselect(int count, fd_set *rfds, fd_set *wfds, fd_set *efds,
434 const struct timespec *timo, const sigset_t *mask)
436 struct pthread *curthread = _get_curthread();
439 _thr_cancel_enter(curthread);
440 ret = __sys_pselect(count, rfds, wfds, efds, timo, mask);
441 _thr_cancel_leave(curthread, ret == -1);
446 __weak_reference(__read, read);
449 * Cancellation behavior:
450 * Thread may be canceled at start, but if the system call got some data,
451 * the thread is not canceled.
454 __read(int fd, void *buf, size_t nbytes)
456 struct pthread *curthread = _get_curthread();
459 _thr_cancel_enter(curthread);
460 ret = __sys_read(fd, buf, nbytes);
461 _thr_cancel_leave(curthread, ret == -1);
466 __weak_reference(__readv, readv);
469 * Cancellation behavior:
470 * Thread may be canceled at start, but if the system call got some data,
471 * the thread is not canceled.
474 __readv(int fd, const struct iovec *iov, int iovcnt)
476 struct pthread *curthread = _get_curthread();
479 _thr_cancel_enter(curthread);
480 ret = __sys_readv(fd, iov, iovcnt);
481 _thr_cancel_leave(curthread, ret == -1);
485 __weak_reference(__recvfrom, recvfrom);
488 * Cancellation behavior:
489 * Thread may be canceled at start, but if the system call got some data,
490 * the thread is not canceled.
493 __recvfrom(int s, void *b, size_t l, int f, struct sockaddr *from,
496 struct pthread *curthread = _get_curthread();
499 _thr_cancel_enter(curthread);
500 ret = __sys_recvfrom(s, b, l, f, from, fl);
501 _thr_cancel_leave(curthread, ret == -1);
505 __weak_reference(__recvmsg, recvmsg);
508 * Cancellation behavior:
509 * Thread may be canceled at start, but if the system call got some data,
510 * the thread is not canceled.
513 __recvmsg(int s, struct msghdr *m, int f)
515 struct pthread *curthread = _get_curthread();
518 _thr_cancel_enter(curthread);
519 ret = __sys_recvmsg(s, m, f);
520 _thr_cancel_leave(curthread, ret == -1);
524 __weak_reference(__select, select);
527 * Cancellation behavior:
528 * Thread may be canceled at start, but if the system call returns something,
529 * the thread is not canceled.
532 __select(int numfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
533 struct timeval *timeout)
535 struct pthread *curthread = _get_curthread();
538 _thr_cancel_enter(curthread);
539 ret = __sys_select(numfds, readfds, writefds, exceptfds, timeout);
540 _thr_cancel_leave(curthread, ret == -1);
544 __weak_reference(__sendmsg, sendmsg);
547 * Cancellation behavior:
548 * Thread may be canceled at start, but if the system call sent
549 * data, the thread is not canceled.
552 __sendmsg(int s, const struct msghdr *m, int f)
554 struct pthread *curthread = _get_curthread();
557 _thr_cancel_enter(curthread);
558 ret = __sys_sendmsg(s, m, f);
559 _thr_cancel_leave(curthread, ret <= 0);
563 __weak_reference(__sendto, sendto);
566 * Cancellation behavior:
567 * Thread may be canceled at start, but if the system call sent some
568 * data, the thread is not canceled.
571 __sendto(int s, const void *m, size_t l, int f, const struct sockaddr *t,
574 struct pthread *curthread = _get_curthread();
577 _thr_cancel_enter(curthread);
578 ret = __sys_sendto(s, m, l, f, t, tl);
579 _thr_cancel_leave(curthread, ret <= 0);
583 __weak_reference(___sleep, sleep);
586 ___sleep(unsigned int seconds)
588 struct pthread *curthread = _get_curthread();
591 _thr_cancel_enter(curthread);
592 ret = __sleep(seconds);
593 _thr_cancel_leave(curthread, 1);
598 __weak_reference(___system, system);
601 ___system(const char *string)
603 struct pthread *curthread = _get_curthread();
606 _thr_cancel_enter(curthread);
607 ret = __system(string);
608 _thr_cancel_leave(curthread, 1);
613 __weak_reference(___tcdrain, tcdrain);
616 * Cancellation behavior:
617 * If thread is canceled, the system call is not completed,
618 * this means not all bytes were drained.
623 struct pthread *curthread = _get_curthread();
626 _thr_cancel_enter(curthread);
628 _thr_cancel_leave(curthread, ret == -1);
632 __weak_reference(___usleep, usleep);
635 ___usleep(useconds_t useconds)
637 struct pthread *curthread = _get_curthread();
640 _thr_cancel_enter(curthread);
641 ret = __usleep(useconds);
642 _thr_cancel_leave(curthread, 1);
647 __weak_reference(___wait, wait);
650 * Cancellation behavior:
651 * Thread may be canceled at start, but if the system call returns
652 * a child pid, the thread is not canceled.
657 struct pthread *curthread = _get_curthread();
660 _thr_cancel_enter(curthread);
662 _thr_cancel_leave(curthread, ret <= 0);
667 __weak_reference(__wait3, wait3);
670 * Cancellation behavior:
671 * Thread may be canceled at start, but if the system call returns
672 * a child pid, the thread is not canceled.
675 __wait3(int *status, int options, struct rusage *rusage)
677 struct pthread *curthread = _get_curthread();
680 _thr_cancel_enter(curthread);
681 ret = _wait4(WAIT_ANY, status, options, rusage);
682 _thr_cancel_leave(curthread, ret <= 0);
687 __weak_reference(__wait4, wait4);
690 * Cancellation behavior:
691 * Thread may be canceled at start, but if the system call returns
692 * a child pid, the thread is not canceled.
695 __wait4(pid_t pid, int *status, int options, struct rusage *rusage)
697 struct pthread *curthread = _get_curthread();
700 _thr_cancel_enter(curthread);
701 ret = __sys_wait4(pid, status, options, rusage);
702 _thr_cancel_leave(curthread, ret <= 0);
707 __weak_reference(___waitpid, waitpid);
710 * Cancellation behavior:
711 * Thread may be canceled at start, but if the system call returns
712 * a child pid, the thread is not canceled.
715 ___waitpid(pid_t wpid, int *status, int options)
717 struct pthread *curthread = _get_curthread();
720 _thr_cancel_enter(curthread);
721 ret = __waitpid(wpid, status, options);
722 _thr_cancel_leave(curthread, ret <= 0);
727 __weak_reference(__write, write);
730 * Cancellation behavior:
731 * Thread may be canceled at start, but if the thread wrote some data,
732 * it is not canceled.
735 __write(int fd, const void *buf, size_t nbytes)
737 struct pthread *curthread = _get_curthread();
740 _thr_cancel_enter(curthread);
741 ret = __sys_write(fd, buf, nbytes);
742 _thr_cancel_leave(curthread, (ret <= 0));
746 __weak_reference(__writev, writev);
749 * Cancellation behavior:
750 * Thread may be canceled at start, but if the thread wrote some data,
751 * it is not canceled.
754 __writev(int fd, const struct iovec *iov, int iovcnt)
756 struct pthread *curthread = _get_curthread();
759 _thr_cancel_enter(curthread);
760 ret = __sys_writev(fd, iov, iovcnt);
761 _thr_cancel_leave(curthread, (ret <= 0));