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_accept4(int, struct sockaddr *, socklen_t *, int);
105 extern int __sys_connect(int, const struct sockaddr *, socklen_t);
106 extern int __sys_fsync(int);
107 extern int __sys_msync(void *, size_t, int);
108 extern int __sys_pselect(int, fd_set *, fd_set *, fd_set *,
109 const struct timespec *, const sigset_t *);
110 extern int __sys_poll(struct pollfd *, unsigned, int);
111 extern ssize_t __sys_recv(int, void *, size_t, int);
112 extern ssize_t __sys_recvfrom(int, void *, size_t, int, struct sockaddr *, socklen_t *);
113 extern ssize_t __sys_recvmsg(int, struct msghdr *, int);
114 extern int __sys_select(int, fd_set *, fd_set *, fd_set *, struct timeval *);
115 extern int __sys_sendfile(int, int, off_t, size_t, struct sf_hdtr *,
117 extern ssize_t __sys_sendmsg(int, const struct msghdr *, int);
118 extern ssize_t __sys_sendto(int, const void *,size_t, int, const struct sockaddr *, socklen_t);
119 extern ssize_t __sys_readv(int, const struct iovec *, int);
120 extern pid_t __sys_wait4(pid_t, int *, int, struct rusage *);
121 extern ssize_t __sys_writev(int, const struct iovec *, int);
123 int ___creat(const char *, mode_t);
124 int ___pselect(int, fd_set *, fd_set *, fd_set *,
125 const struct timespec *, const sigset_t *);
126 unsigned ___sleep(unsigned);
127 int ___system(const char *);
129 int ___usleep(useconds_t useconds);
130 pid_t ___wait(int *);
131 pid_t ___waitpid(pid_t, int *, int);
132 int __accept(int, struct sockaddr *, socklen_t *);
133 int __accept4(int, struct sockaddr *, socklen_t *, int);
134 int __aio_suspend(const struct aiocb * const iocbs[], int,
135 const struct timespec *);
137 int __connect(int, const struct sockaddr *, socklen_t);
138 int __fcntl(int, int,...);
139 #ifdef SYSCALL_COMPAT
140 extern int __fcntl_compat(int, int,...);
143 int __msync(void *, size_t, int);
144 int __nanosleep(const struct timespec *, struct timespec *);
145 int __open(const char *, int,...);
146 int __openat(int, const char *, int,...);
147 int __poll(struct pollfd *, unsigned int, int);
148 ssize_t __read(int, void *buf, size_t);
149 ssize_t __readv(int, const struct iovec *, int);
150 ssize_t __recvfrom(int, void *, size_t, int f, struct sockaddr *, socklen_t *);
151 ssize_t __recvmsg(int, struct msghdr *, int);
152 int __select(int, fd_set *, fd_set *, fd_set *, struct timeval *);
153 ssize_t __sendmsg(int, const struct msghdr *, int);
154 ssize_t __sendto(int, const void *, size_t, int,
155 const struct sockaddr *, socklen_t);
156 pid_t __wait3(int *, int, struct rusage *);
157 pid_t __wait4(pid_t, int *, int, struct rusage *);
158 ssize_t __write(int, const void *, size_t);
159 ssize_t __writev(int, const struct iovec *, int);
161 __weak_reference(__accept, accept);
164 * Cancellation behavior:
165 * If thread is canceled, no socket is created.
168 __accept(int s, struct sockaddr *addr, socklen_t *addrlen)
170 struct pthread *curthread;
173 curthread = _get_curthread();
174 _thr_cancel_enter(curthread);
175 ret = __sys_accept(s, addr, addrlen);
176 _thr_cancel_leave(curthread, ret == -1);
181 __weak_reference(__accept4, accept4);
184 * Cancellation behavior:
185 * If thread is canceled, no socket is created.
188 __accept4(int s, struct sockaddr *addr, socklen_t *addrlen, int flags)
190 struct pthread *curthread;
193 curthread = _get_curthread();
194 _thr_cancel_enter(curthread);
195 ret = __sys_accept4(s, addr, addrlen, flags);
196 _thr_cancel_leave(curthread, ret == -1);
201 __weak_reference(__aio_suspend, aio_suspend);
204 __aio_suspend(const struct aiocb * const iocbs[], int niocb, const struct
207 struct pthread *curthread = _get_curthread();
210 _thr_cancel_enter(curthread);
211 ret = __sys_aio_suspend(iocbs, niocb, timeout);
212 _thr_cancel_leave(curthread, 1);
217 __weak_reference(__close, close);
220 * Cancellation behavior:
221 * According to manual of close(), the file descriptor is always deleted.
222 * Here, thread is only canceled after the system call, so the file
223 * descriptor is always deleted despite whether the thread is canceled
229 struct pthread *curthread = _get_curthread();
232 _thr_cancel_enter2(curthread, 0);
233 ret = __sys_close(fd);
234 _thr_cancel_leave(curthread, 1);
239 __weak_reference(__connect, connect);
242 * Cancellation behavior:
243 * If the thread is canceled, connection is not made.
246 __connect(int fd, const struct sockaddr *name, socklen_t namelen)
248 struct pthread *curthread = _get_curthread();
251 _thr_cancel_enter(curthread);
252 ret = __sys_connect(fd, name, namelen);
253 _thr_cancel_leave(curthread, ret == -1);
258 __weak_reference(___creat, creat);
261 * Cancellation behavior:
262 * If thread is canceled, file is not created.
265 ___creat(const char *path, mode_t mode)
267 struct pthread *curthread = _get_curthread();
270 _thr_cancel_enter(curthread);
271 ret = __creat(path, mode);
272 _thr_cancel_leave(curthread, ret == -1);
277 __weak_reference(__fcntl, fcntl);
280 * Cancellation behavior:
281 * According to specification, only F_SETLKW is a cancellation point.
282 * Thread is only canceled at start, or canceled if the system call
283 * is failure, this means the function does not generate side effect
287 __fcntl(int fd, int cmd,...)
289 struct pthread *curthread = _get_curthread();
294 if (cmd == F_OSETLKW || cmd == F_SETLKW) {
295 _thr_cancel_enter(curthread);
296 #ifdef SYSCALL_COMPAT
297 ret = __fcntl_compat(fd, cmd, va_arg(ap, void *));
299 ret = __sys_fcntl(fd, cmd, va_arg(ap, void *));
301 _thr_cancel_leave(curthread, ret == -1);
303 #ifdef SYSCALL_COMPAT
304 ret = __fcntl_compat(fd, cmd, va_arg(ap, void *));
306 ret = __sys_fcntl(fd, cmd, va_arg(ap, void *));
314 __weak_reference(__fsync, fsync);
317 * Cancellation behavior:
318 * Thread may be canceled after system call.
323 struct pthread *curthread = _get_curthread();
326 _thr_cancel_enter2(curthread, 0);
327 ret = __sys_fsync(fd);
328 _thr_cancel_leave(curthread, 1);
333 __weak_reference(__msync, msync);
336 * Cancellation behavior:
337 * Thread may be canceled after system call.
340 __msync(void *addr, size_t len, int flags)
342 struct pthread *curthread = _get_curthread();
345 _thr_cancel_enter2(curthread, 0);
346 ret = __sys_msync(addr, len, flags);
347 _thr_cancel_leave(curthread, 1);
352 __weak_reference(__nanosleep, nanosleep);
355 __nanosleep(const struct timespec *time_to_sleep,
356 struct timespec *time_remaining)
358 struct pthread *curthread = _get_curthread();
361 _thr_cancel_enter(curthread);
362 ret = __sys_nanosleep(time_to_sleep, time_remaining);
363 _thr_cancel_leave(curthread, 1);
368 __weak_reference(__open, open);
371 * Cancellation behavior:
372 * If the thread is canceled, file is not opened.
375 __open(const char *path, int flags,...)
377 struct pthread *curthread = _get_curthread();
382 /* Check if the file is being created: */
383 if (flags & O_CREAT) {
384 /* Get the creation mode: */
386 mode = va_arg(ap, int);
390 _thr_cancel_enter(curthread);
391 ret = __sys_open(path, flags, mode);
392 _thr_cancel_leave(curthread, ret == -1);
397 __weak_reference(__openat, openat);
400 * Cancellation behavior:
401 * If the thread is canceled, file is not opened.
404 __openat(int fd, const char *path, int flags, ...)
406 struct pthread *curthread = _get_curthread();
412 /* Check if the file is being created: */
413 if (flags & O_CREAT) {
414 /* Get the creation mode: */
416 mode = va_arg(ap, int);
420 _thr_cancel_enter(curthread);
421 ret = __sys_openat(fd, path, flags, mode);
422 _thr_cancel_leave(curthread, ret == -1);
427 __weak_reference(__poll, poll);
430 * Cancellation behavior:
431 * Thread may be canceled at start, but if the system call returns something,
432 * the thread is not canceled.
435 __poll(struct pollfd *fds, unsigned int nfds, int timeout)
437 struct pthread *curthread = _get_curthread();
440 _thr_cancel_enter(curthread);
441 ret = __sys_poll(fds, nfds, timeout);
442 _thr_cancel_leave(curthread, ret == -1);
447 __weak_reference(___pselect, pselect);
450 * Cancellation behavior:
451 * Thread may be canceled at start, but if the system call returns something,
452 * the thread is not canceled.
455 ___pselect(int count, fd_set *rfds, fd_set *wfds, fd_set *efds,
456 const struct timespec *timo, const sigset_t *mask)
458 struct pthread *curthread = _get_curthread();
461 _thr_cancel_enter(curthread);
462 ret = __sys_pselect(count, rfds, wfds, efds, timo, mask);
463 _thr_cancel_leave(curthread, ret == -1);
468 __weak_reference(__read, read);
471 * Cancellation behavior:
472 * Thread may be canceled at start, but if the system call got some data,
473 * the thread is not canceled.
476 __read(int fd, void *buf, size_t nbytes)
478 struct pthread *curthread = _get_curthread();
481 _thr_cancel_enter(curthread);
482 ret = __sys_read(fd, buf, nbytes);
483 _thr_cancel_leave(curthread, ret == -1);
488 __weak_reference(__readv, readv);
491 * Cancellation behavior:
492 * Thread may be canceled at start, but if the system call got some data,
493 * the thread is not canceled.
496 __readv(int fd, const struct iovec *iov, int iovcnt)
498 struct pthread *curthread = _get_curthread();
501 _thr_cancel_enter(curthread);
502 ret = __sys_readv(fd, iov, iovcnt);
503 _thr_cancel_leave(curthread, ret == -1);
507 __weak_reference(__recvfrom, recvfrom);
510 * Cancellation behavior:
511 * Thread may be canceled at start, but if the system call got some data,
512 * the thread is not canceled.
515 __recvfrom(int s, void *b, size_t l, int f, struct sockaddr *from,
518 struct pthread *curthread = _get_curthread();
521 _thr_cancel_enter(curthread);
522 ret = __sys_recvfrom(s, b, l, f, from, fl);
523 _thr_cancel_leave(curthread, ret == -1);
527 __weak_reference(__recvmsg, recvmsg);
530 * Cancellation behavior:
531 * Thread may be canceled at start, but if the system call got some data,
532 * the thread is not canceled.
535 __recvmsg(int s, struct msghdr *m, int f)
537 struct pthread *curthread = _get_curthread();
540 _thr_cancel_enter(curthread);
541 ret = __sys_recvmsg(s, m, f);
542 _thr_cancel_leave(curthread, ret == -1);
546 __weak_reference(__select, select);
549 * Cancellation behavior:
550 * Thread may be canceled at start, but if the system call returns something,
551 * the thread is not canceled.
554 __select(int numfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
555 struct timeval *timeout)
557 struct pthread *curthread = _get_curthread();
560 _thr_cancel_enter(curthread);
561 ret = __sys_select(numfds, readfds, writefds, exceptfds, timeout);
562 _thr_cancel_leave(curthread, ret == -1);
566 __weak_reference(__sendmsg, sendmsg);
569 * Cancellation behavior:
570 * Thread may be canceled at start, but if the system call sent
571 * data, the thread is not canceled.
574 __sendmsg(int s, const struct msghdr *m, int f)
576 struct pthread *curthread = _get_curthread();
579 _thr_cancel_enter(curthread);
580 ret = __sys_sendmsg(s, m, f);
581 _thr_cancel_leave(curthread, ret <= 0);
585 __weak_reference(__sendto, sendto);
588 * Cancellation behavior:
589 * Thread may be canceled at start, but if the system call sent some
590 * data, the thread is not canceled.
593 __sendto(int s, const void *m, size_t l, int f, const struct sockaddr *t,
596 struct pthread *curthread = _get_curthread();
599 _thr_cancel_enter(curthread);
600 ret = __sys_sendto(s, m, l, f, t, tl);
601 _thr_cancel_leave(curthread, ret <= 0);
605 __weak_reference(___sleep, sleep);
608 ___sleep(unsigned int seconds)
610 struct pthread *curthread = _get_curthread();
613 _thr_cancel_enter(curthread);
614 ret = __sleep(seconds);
615 _thr_cancel_leave(curthread, 1);
620 __weak_reference(___system, system);
623 ___system(const char *string)
625 struct pthread *curthread = _get_curthread();
628 _thr_cancel_enter(curthread);
629 ret = __system(string);
630 _thr_cancel_leave(curthread, 1);
635 __weak_reference(___tcdrain, tcdrain);
638 * Cancellation behavior:
639 * If thread is canceled, the system call is not completed,
640 * this means not all bytes were drained.
645 struct pthread *curthread = _get_curthread();
648 _thr_cancel_enter(curthread);
650 _thr_cancel_leave(curthread, ret == -1);
654 __weak_reference(___usleep, usleep);
657 ___usleep(useconds_t useconds)
659 struct pthread *curthread = _get_curthread();
662 _thr_cancel_enter(curthread);
663 ret = __usleep(useconds);
664 _thr_cancel_leave(curthread, 1);
669 __weak_reference(___wait, wait);
672 * Cancellation behavior:
673 * Thread may be canceled at start, but if the system call returns
674 * a child pid, the thread is not canceled.
679 struct pthread *curthread = _get_curthread();
682 _thr_cancel_enter(curthread);
684 _thr_cancel_leave(curthread, ret <= 0);
689 __weak_reference(__wait3, wait3);
692 * Cancellation behavior:
693 * Thread may be canceled at start, but if the system call returns
694 * a child pid, the thread is not canceled.
697 __wait3(int *status, int options, struct rusage *rusage)
699 struct pthread *curthread = _get_curthread();
702 _thr_cancel_enter(curthread);
703 ret = _wait4(WAIT_ANY, status, options, rusage);
704 _thr_cancel_leave(curthread, ret <= 0);
709 __weak_reference(__wait4, wait4);
712 * Cancellation behavior:
713 * Thread may be canceled at start, but if the system call returns
714 * a child pid, the thread is not canceled.
717 __wait4(pid_t pid, int *status, int options, struct rusage *rusage)
719 struct pthread *curthread = _get_curthread();
722 _thr_cancel_enter(curthread);
723 ret = __sys_wait4(pid, status, options, rusage);
724 _thr_cancel_leave(curthread, ret <= 0);
729 __weak_reference(___waitpid, waitpid);
732 * Cancellation behavior:
733 * Thread may be canceled at start, but if the system call returns
734 * a child pid, the thread is not canceled.
737 ___waitpid(pid_t wpid, int *status, int options)
739 struct pthread *curthread = _get_curthread();
742 _thr_cancel_enter(curthread);
743 ret = __waitpid(wpid, status, options);
744 _thr_cancel_leave(curthread, ret <= 0);
749 __weak_reference(__write, write);
752 * Cancellation behavior:
753 * Thread may be canceled at start, but if the thread wrote some data,
754 * it is not canceled.
757 __write(int fd, const void *buf, size_t nbytes)
759 struct pthread *curthread = _get_curthread();
762 _thr_cancel_enter(curthread);
763 ret = __sys_write(fd, buf, nbytes);
764 _thr_cancel_leave(curthread, (ret <= 0));
768 __weak_reference(__writev, writev);
771 * Cancellation behavior:
772 * Thread may be canceled at start, but if the thread wrote some data,
773 * it is not canceled.
776 __writev(int fd, const struct iovec *iov, int iovcnt)
778 struct pthread *curthread = _get_curthread();
781 _thr_cancel_enter(curthread);
782 ret = __sys_writev(fd, iov, iovcnt);
783 _thr_cancel_leave(curthread, (ret <= 0));