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.
33 * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au>
34 * All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. Neither the name of the author nor the names of any co-contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 #include "namespace.h"
64 #include <sys/types.h>
66 #include <sys/param.h>
67 #include <sys/select.h>
68 #include <sys/signalvar.h>
69 #include <sys/socket.h>
74 #include <machine/tls.h>
88 #include "un-namespace.h"
90 #include "thr_private.h"
92 extern int __creat(const char *, mode_t);
93 extern int __pause(void);
94 extern int __sys_pselect(int, fd_set *, fd_set *, fd_set *,
95 const struct timespec *, const sigset_t *);
96 extern unsigned __sleep(unsigned int);
97 extern int __system(const char *);
98 extern int __tcdrain(int);
99 extern int __usleep(useconds_t);
100 extern pid_t __wait(int *);
101 extern pid_t __waitpid(pid_t, int *, int);
102 extern int __sys_aio_suspend(const struct aiocb * const[], int,
103 const struct timespec *);
104 extern int __sys_accept(int, struct sockaddr *, socklen_t *);
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_poll(struct pollfd *, unsigned, int);
109 extern int __sys_ppoll(struct pollfd *, unsigned, const struct timespec *,
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 ___usleep(useconds_t);
125 int __accept(int, struct sockaddr *, socklen_t *);
127 int __connect(int, const struct sockaddr *, socklen_t);
128 int __fcntl(int, int,...);
130 int __msync(void *, size_t, int);
131 int __nanosleep(const struct timespec *, struct timespec *);
132 int __clock_nanosleep(clock_t, int, const struct timespec *,
134 int __open(const char *, int,...);
135 int __openat(int fd, const char *, int,...);
136 int __poll(struct pollfd *, unsigned int, int);
137 int __ppoll(struct pollfd *, unsigned int, const struct timespec *,
139 ssize_t __read(int, void *buf, size_t);
140 ssize_t __readv(int, const struct iovec *, int);
141 ssize_t __recvfrom(int, void *, size_t, int f, struct sockaddr *, socklen_t *);
142 ssize_t __recvmsg(int, struct msghdr *, int);
143 int __select(int, fd_set *, fd_set *, fd_set *, struct timeval *);
144 ssize_t __sendmsg(int, const struct msghdr *, int);
145 ssize_t __sendto(int, const void *, size_t, int,
146 const struct sockaddr *, socklen_t);
147 pid_t __wait4(pid_t, int *, int, struct rusage *);
148 ssize_t __write(int, const void *, size_t);
149 ssize_t __writev(int, const struct iovec *, int);
150 int _aio_suspend(const struct aiocb * const iocbs[], int,
151 const struct timespec *);
153 int __pselect(int, fd_set *, fd_set *, fd_set *,
154 const struct timespec *, const sigset_t *);
156 unsigned _sleep(unsigned);
157 int _system(const char *);
165 __accept(int s, struct sockaddr *addr, socklen_t *addrlen)
171 curthread = tls_get_curthread();
172 oldcancel = _thr_cancel_enter(curthread);
173 ret = __sys_accept(s, addr, addrlen);
174 _thr_cancel_leave(curthread, oldcancel);
179 __strong_reference(__accept, accept);
182 _aio_suspend(const struct aiocb * const iocbs[], int niocb, const struct
185 pthread_t curthread = tls_get_curthread();
189 oldcancel = _thr_cancel_enter(curthread);
190 ret = __sys_aio_suspend(iocbs, niocb, timeout);
191 _thr_cancel_leave(curthread, oldcancel);
196 __strong_reference(_aio_suspend, aio_suspend);
201 pthread_t curthread = tls_get_curthread();
205 oldcancel = _thr_cancel_enter(curthread);
206 ret = __sys_close(fd);
207 _thr_cancel_leave(curthread, oldcancel);
212 __strong_reference(__close, close);
215 __connect(int fd, const struct sockaddr *name, socklen_t namelen)
217 pthread_t curthread = tls_get_curthread();
221 oldcancel = _thr_cancel_enter(curthread);
222 ret = __sys_connect(fd, name, namelen);
223 _thr_cancel_leave(curthread, oldcancel);
228 __strong_reference(__connect, connect);
231 ___creat(const char *path, mode_t mode)
233 pthread_t curthread = tls_get_curthread();
237 oldcancel = _thr_cancel_enter(curthread);
238 ret = __creat(path, mode);
239 _thr_cancel_leave(curthread, oldcancel);
244 __strong_reference(___creat, creat);
247 __fcntl(int fd, int cmd,...)
249 pthread_t curthread = tls_get_curthread();
254 oldcancel = _thr_cancel_enter(curthread);
259 ret = __sys_fcntl(fd, cmd, va_arg(ap, int));
263 ret = __sys_fcntl(fd, cmd, va_arg(ap, int));
267 ret = __sys_fcntl(fd, cmd);
270 ret = __sys_fcntl(fd, cmd, va_arg(ap, void *));
274 _thr_cancel_leave(curthread, oldcancel);
279 __strong_reference(__fcntl, fcntl);
284 pthread_t curthread = tls_get_curthread();
288 oldcancel = _thr_cancel_enter(curthread);
289 ret = __sys_fsync(fd);
290 _thr_cancel_leave(curthread, oldcancel);
295 __strong_reference(__fsync, fsync);
298 __msync(void *addr, size_t len, int flags)
300 pthread_t curthread = tls_get_curthread();
304 oldcancel = _thr_cancel_enter(curthread);
305 ret = __sys_msync(addr, len, flags);
306 _thr_cancel_leave(curthread, oldcancel);
311 __strong_reference(__msync, msync);
314 __nanosleep(const struct timespec *time_to_sleep,
315 struct timespec *time_remaining)
317 pthread_t curthread = tls_get_curthread();
321 oldcancel = _thr_cancel_enter(curthread);
322 ret = __sys_nanosleep(time_to_sleep, time_remaining);
323 _thr_cancel_leave(curthread, oldcancel);
328 __strong_reference(__nanosleep, nanosleep);
331 __clock_nanosleep(clock_t clock_id, int flags,
332 const struct timespec *time_to_sleep, struct timespec *time_remaining)
334 pthread_t curthread = tls_get_curthread();
338 oldcancel = _thr_cancel_enter(curthread);
339 ret = __sys_clock_nanosleep(clock_id, flags, time_to_sleep,
341 _thr_cancel_leave(curthread, oldcancel);
346 __strong_reference(__clock_nanosleep, clock_nanosleep);
349 __open(const char *path, int flags,...)
351 pthread_t curthread = tls_get_curthread();
357 oldcancel = _thr_cancel_enter(curthread);
359 /* Check if the file is being created: */
360 if (flags & O_CREAT) {
361 /* Get the creation mode: */
363 mode = va_arg(ap, int);
367 ret = __sys_open(path, flags, mode);
369 _thr_cancel_leave(curthread, oldcancel);
374 __strong_reference(__open, open);
377 __openat(int fd, const char *path, int flags,...)
379 pthread_t curthread = tls_get_curthread();
385 oldcancel = _thr_cancel_enter(curthread);
387 /* Check if the file is being created: */
388 if (flags & O_CREAT) {
389 /* Get the creation mode: */
391 mode = va_arg(ap, int);
395 ret = __sys_openat(fd, path, flags, mode);
397 _thr_cancel_leave(curthread, oldcancel);
402 __strong_reference(__openat, openat);
407 pthread_t curthread = tls_get_curthread();
411 oldcancel = _thr_cancel_enter(curthread);
413 _thr_cancel_leave(curthread, oldcancel);
418 __strong_reference(_pause, pause);
421 __poll(struct pollfd *fds, unsigned int nfds, int timeout)
423 pthread_t curthread = tls_get_curthread();
427 oldcancel = _thr_cancel_enter(curthread);
428 ret = __sys_poll(fds, nfds, timeout);
429 _thr_cancel_leave(curthread, oldcancel);
434 __strong_reference(__poll, poll);
437 __ppoll(struct pollfd *fds, unsigned int nfds, const struct timespec *ts,
438 const sigset_t *mask)
440 pthread_t curthread = tls_get_curthread();
444 oldcancel = _thr_cancel_enter(curthread);
445 ret = __sys_ppoll(fds, nfds, ts, mask);
446 _thr_cancel_leave(curthread, oldcancel);
451 __strong_reference(__ppoll, ppoll);
454 __pselect(int count, fd_set *rfds, fd_set *wfds, fd_set *efds,
455 const struct timespec *timo, const sigset_t *mask)
457 pthread_t curthread = tls_get_curthread();
461 oldcancel = _thr_cancel_enter(curthread);
462 ret = __sys_pselect(count, rfds, wfds, efds, timo, mask);
463 _thr_cancel_leave(curthread, oldcancel);
467 __strong_reference(__pselect, pselect);
475 if (!_thr_isthreaded())
476 ret = kill(getpid(), sig);
478 ret = _thr_send_sig(tls_get_curthread(), sig);
482 __strong_reference(_raise, raise);
485 __read(int fd, void *buf, size_t nbytes)
487 pthread_t curthread = tls_get_curthread();
491 oldcancel = _thr_cancel_enter(curthread);
492 ret = __sys_read(fd, buf, nbytes);
493 _thr_cancel_leave(curthread, oldcancel);
497 __strong_reference(__read, read);
500 __readv(int fd, const struct iovec *iov, int iovcnt)
502 pthread_t curthread = tls_get_curthread();
506 oldcancel = _thr_cancel_enter(curthread);
507 ret = __sys_readv(fd, iov, iovcnt);
508 _thr_cancel_leave(curthread, oldcancel);
512 __strong_reference(__readv, readv);
515 __recvfrom(int s, void *b, size_t l, int f, struct sockaddr *from,
518 pthread_t curthread = tls_get_curthread();
522 oldcancel = _thr_cancel_enter(curthread);
523 ret = __sys_recvfrom(s, b, l, f, from, fl);
524 _thr_cancel_leave(curthread, oldcancel);
528 __strong_reference(__recvfrom, recvfrom);
531 __recvmsg(int s, struct msghdr *m, int f)
533 pthread_t curthread = tls_get_curthread();
537 oldcancel = _thr_cancel_enter(curthread);
538 ret = __sys_recvmsg(s, m, f);
539 _thr_cancel_leave(curthread, oldcancel);
543 __strong_reference(__recvmsg, recvmsg);
546 __select(int numfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
547 struct timeval *timeout)
549 pthread_t curthread = tls_get_curthread();
553 oldcancel = _thr_cancel_enter(curthread);
554 ret = __sys_select(numfds, readfds, writefds, exceptfds, timeout);
555 _thr_cancel_leave(curthread, oldcancel);
559 __strong_reference(__select, select);
562 __sendmsg(int s, const struct msghdr *m, int f)
564 pthread_t curthread = tls_get_curthread();
568 oldcancel = _thr_cancel_enter(curthread);
569 ret = __sys_sendmsg(s, m, f);
570 _thr_cancel_leave(curthread, oldcancel);
574 __strong_reference(__sendmsg, sendmsg);
577 __sendto(int s, const void *m, size_t l, int f, const struct sockaddr *t,
580 pthread_t curthread = tls_get_curthread();
584 oldcancel = _thr_cancel_enter(curthread);
585 ret = __sys_sendto(s, m, l, f, t, tl);
586 _thr_cancel_leave(curthread, oldcancel);
590 __strong_reference(__sendto, sendto);
593 _sleep(unsigned int seconds)
595 pthread_t curthread = tls_get_curthread();
599 oldcancel = _thr_cancel_enter(curthread);
600 ret = __sleep(seconds);
601 _thr_cancel_leave(curthread, oldcancel);
605 __strong_reference(_sleep, sleep);
608 _system(const char *string)
610 pthread_t curthread = tls_get_curthread();
614 oldcancel = _thr_cancel_enter(curthread);
615 ret = __system(string);
616 _thr_cancel_leave(curthread, oldcancel);
620 __strong_reference(_system, system);
625 pthread_t curthread = tls_get_curthread();
629 oldcancel = _thr_cancel_enter(curthread);
631 _thr_cancel_leave(curthread, oldcancel);
635 __strong_reference(_tcdrain, tcdrain);
638 ___usleep(useconds_t useconds)
640 pthread_t curthread = tls_get_curthread();
644 oldcancel = _thr_cancel_enter(curthread);
645 ret = __usleep(useconds);
646 _thr_cancel_leave(curthread, oldcancel);
650 __strong_reference(___usleep, usleep);
654 * REMOVED - vfork() works as per normal. In a threaded environment vfork()
655 * blocks the calling thread only and not other threads.
663 __strong_reference(_vfork, vfork);
669 pthread_t curthread = tls_get_curthread();
673 oldcancel = _thr_cancel_enter(curthread);
675 _thr_cancel_leave(curthread, oldcancel);
679 __strong_reference(_wait, wait);
682 __wait4(pid_t pid, int *istat, int options, struct rusage *rusage)
684 pthread_t curthread = tls_get_curthread();
688 oldcancel = _thr_cancel_enter(curthread);
689 ret = __sys_wait4(pid, istat, options, rusage);
690 _thr_cancel_leave(curthread, oldcancel);
694 __strong_reference(__wait4, wait4);
697 _waitpid(pid_t wpid, int *status, int options)
699 pthread_t curthread = tls_get_curthread();
703 oldcancel = _thr_cancel_enter(curthread);
704 ret = __waitpid(wpid, status, options);
705 _thr_cancel_leave(curthread, oldcancel);
709 __strong_reference(_waitpid, waitpid);
712 __write(int fd, const void *buf, size_t nbytes)
714 pthread_t curthread = tls_get_curthread();
718 oldcancel = _thr_cancel_enter(curthread);
719 ret = __sys_write(fd, buf, nbytes);
720 _thr_cancel_leave(curthread, oldcancel);
724 __strong_reference(__write, write);
727 __writev(int fd, const struct iovec *iov, int iovcnt)
729 pthread_t curthread = tls_get_curthread();
733 oldcancel = _thr_cancel_enter(curthread);
734 ret = __sys_writev(fd, iov, iovcnt);
735 _thr_cancel_leave(curthread, oldcancel);
739 __strong_reference(__writev, writev);