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.
31 * $DragonFly: src/lib/libthread_xu/thread/thr_syscalls.c,v 1.5 2005/11/02 23:41:17 davidxu Exp $
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. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by John Birrell.
49 * 4. Neither the name of the author nor the names of any co-contributors
50 * may be used to endorse or promote products derived from this software
51 * without specific prior written permission.
53 * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
67 #include <sys/types.h>
69 #include <sys/param.h>
70 #include <sys/select.h>
71 #include <sys/signalvar.h>
72 #include <sys/socket.h>
78 #include <machine/tls.h>
94 #include "thr_private.h"
96 extern int __creat(const char *, mode_t);
97 extern int __pause(void);
98 extern int __pselect(int count, fd_set *rfds, fd_set *wfds, fd_set *efds,
99 const struct timespec *timo, const sigset_t *mask);
100 extern unsigned int __sleep(unsigned int);
101 extern int __system(const char *);
102 extern int __tcdrain(int);
103 extern int __usleep(unsigned int);
104 extern pid_t __wait(int *);
105 extern pid_t __sys_wait4(pid_t, int *, int, struct rusage *);
106 extern pid_t __waitpid(pid_t, int *, int);
108 __weak_reference(__accept, accept);
110 __accept(int s, struct sockaddr *addr, socklen_t *addrlen)
112 struct pthread *curthread;
116 curthread = tls_get_curthread();
117 oldcancel = _thr_cancel_enter(curthread);
118 ret = __sys_accept(s, addr, addrlen);
119 _thr_cancel_leave(curthread, oldcancel);
124 __weak_reference(_aio_suspend, aio_suspend);
127 _aio_suspend(const struct aiocb * const iocbs[], int niocb, const struct
130 struct pthread *curthread = tls_get_curthread();
134 oldcancel = _thr_cancel_enter(curthread);
135 ret = __sys_aio_suspend(iocbs, niocb, timeout);
136 _thr_cancel_leave(curthread, oldcancel);
141 __weak_reference(__close, close);
146 struct pthread *curthread = tls_get_curthread();
150 oldcancel = _thr_cancel_enter(curthread);
151 ret = __sys_close(fd);
152 _thr_cancel_leave(curthread, oldcancel);
157 __weak_reference(__connect, connect);
160 __connect(int fd, const struct sockaddr *name, socklen_t namelen)
162 struct pthread *curthread = tls_get_curthread();
166 oldcancel = _thr_cancel_enter(curthread);
167 ret = __sys_connect(fd, name, namelen);
168 _thr_cancel_leave(curthread, oldcancel);
173 __weak_reference(___creat, creat);
176 ___creat(const char *path, mode_t mode)
178 struct pthread *curthread = tls_get_curthread();
182 oldcancel = _thr_cancel_enter(curthread);
183 ret = __creat(path, mode);
184 _thr_cancel_leave(curthread, oldcancel);
189 __weak_reference(__fcntl, fcntl);
192 __fcntl(int fd, int cmd,...)
194 struct pthread *curthread = tls_get_curthread();
199 oldcancel = _thr_cancel_enter(curthread);
204 ret = __sys_fcntl(fd, cmd, va_arg(ap, int));
208 ret = __sys_fcntl(fd, cmd, va_arg(ap, int));
212 ret = __sys_fcntl(fd, cmd);
215 ret = __sys_fcntl(fd, cmd, va_arg(ap, void *));
219 _thr_cancel_leave(curthread, oldcancel);
224 __weak_reference(__fsync, fsync);
229 struct pthread *curthread = tls_get_curthread();
233 oldcancel = _thr_cancel_enter(curthread);
234 ret = __sys_fsync(fd);
235 _thr_cancel_leave(curthread, oldcancel);
240 __weak_reference(__msync, msync);
243 __msync(void *addr, size_t len, int flags)
245 struct pthread *curthread = tls_get_curthread();
249 oldcancel = _thr_cancel_enter(curthread);
250 ret = __sys_msync(addr, len, flags);
251 _thr_cancel_leave(curthread, oldcancel);
256 __weak_reference(__nanosleep, nanosleep);
259 __nanosleep(const struct timespec *time_to_sleep,
260 struct timespec *time_remaining)
262 struct pthread *curthread = tls_get_curthread();
266 oldcancel = _thr_cancel_enter(curthread);
267 ret = __sys_nanosleep(time_to_sleep, time_remaining);
268 _thr_cancel_leave(curthread, oldcancel);
273 __weak_reference(___open, open);
276 ___open(const char *path, int flags,...)
278 struct pthread *curthread = tls_get_curthread();
284 oldcancel = _thr_cancel_enter(curthread);
286 /* Check if the file is being created: */
287 if (flags & O_CREAT) {
288 /* Get the creation mode: */
290 mode = va_arg(ap, int);
294 ret = __sys_open(path, flags, mode);
296 _thr_cancel_leave(curthread, oldcancel);
301 __weak_reference(_pause, pause);
306 struct pthread *curthread = tls_get_curthread();
310 oldcancel = _thr_cancel_enter(curthread);
312 _thr_cancel_leave(curthread, oldcancel);
317 __weak_reference(__poll, poll);
320 __poll(struct pollfd *fds, unsigned int nfds, int timeout)
322 struct pthread *curthread = tls_get_curthread();
326 oldcancel = _thr_cancel_enter(curthread);
327 ret = __sys_poll(fds, nfds, timeout);
328 _thr_cancel_leave(curthread, oldcancel);
334 __weak_reference(_pselect, pselect);
337 _pselect(int count, fd_set *rfds, fd_set *wfds, fd_set *efds,
338 const struct timespec *timo, const sigset_t *mask)
340 struct pthread *curthread = tls_get_curthread();
344 oldcancel = _thr_cancel_enter(curthread);
345 ret = __pselect(count, rfds, wfds, efds, timo, mask);
346 _thr_cancel_leave(curthread, oldcancel);
352 __weak_reference(_raise, raise);
359 if (!_thr_isthreaded())
360 ret = kill(getpid(), sig);
362 ret = _thr_send_sig(tls_get_curthread(), sig);
366 __weak_reference(__read, read);
369 __read(int fd, void *buf, size_t nbytes)
371 struct pthread *curthread = tls_get_curthread();
375 oldcancel = _thr_cancel_enter(curthread);
376 ret = __sys_read(fd, buf, nbytes);
377 _thr_cancel_leave(curthread, oldcancel);
382 __weak_reference(__readv, readv);
385 __readv(int fd, const struct iovec *iov, int iovcnt)
387 struct pthread *curthread = tls_get_curthread();
391 oldcancel = _thr_cancel_enter(curthread);
392 ret = __sys_readv(fd, iov, iovcnt);
393 _thr_cancel_leave(curthread, oldcancel);
398 __weak_reference(__recvfrom, recvfrom);
401 __recvfrom(int s, void *b, size_t l, int f, struct sockaddr *from,
404 struct pthread *curthread = tls_get_curthread();
408 oldcancel = _thr_cancel_enter(curthread);
409 ret = __sys_recvfrom(s, b, l, f, from, fl);
410 _thr_cancel_leave(curthread, oldcancel);
414 __weak_reference(__recvmsg, recvmsg);
417 __recvmsg(int s, struct msghdr *m, int f)
419 struct pthread *curthread = tls_get_curthread();
423 oldcancel = _thr_cancel_enter(curthread);
424 ret = __sys_recvmsg(s, m, f);
425 _thr_cancel_leave(curthread, oldcancel);
429 __weak_reference(__select, select);
432 __select(int numfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
433 struct timeval *timeout)
435 struct pthread *curthread = tls_get_curthread();
439 oldcancel = _thr_cancel_enter(curthread);
440 ret = __sys_select(numfds, readfds, writefds, exceptfds, timeout);
441 _thr_cancel_leave(curthread, oldcancel);
445 __weak_reference(__sendmsg, sendmsg);
448 __sendmsg(int s, const struct msghdr *m, int f)
450 struct pthread *curthread = tls_get_curthread();
454 oldcancel = _thr_cancel_enter(curthread);
455 ret = __sys_sendmsg(s, m, f);
456 _thr_cancel_leave(curthread, oldcancel);
460 __weak_reference(__sendto, sendto);
463 __sendto(int s, const void *m, size_t l, int f, const struct sockaddr *t,
466 struct pthread *curthread = tls_get_curthread();
470 oldcancel = _thr_cancel_enter(curthread);
471 ret = __sys_sendto(s, m, l, f, t, tl);
472 _thr_cancel_leave(curthread, oldcancel);
477 _sleep(unsigned int seconds)
479 struct pthread *curthread = tls_get_curthread();
483 oldcancel = _thr_cancel_enter(curthread);
484 ret = __sleep(seconds);
485 _thr_cancel_leave(curthread, oldcancel);
490 __weak_reference(_system, system);
493 _system(const char *string)
495 struct pthread *curthread = tls_get_curthread();
499 oldcancel = _thr_cancel_enter(curthread);
500 ret = __system(string);
501 _thr_cancel_leave(curthread, oldcancel);
506 __weak_reference(_tcdrain, tcdrain);
511 struct pthread *curthread = tls_get_curthread();
515 oldcancel = _thr_cancel_enter(curthread);
517 _thr_cancel_leave(curthread, oldcancel);
522 __weak_reference(___usleep, usleep);
525 ___usleep(unsigned int useconds)
527 struct pthread *curthread = tls_get_curthread();
531 oldcancel = _thr_cancel_enter(curthread);
532 ret = __usleep(useconds);
533 _thr_cancel_leave(curthread, oldcancel);
538 __weak_reference(_vfork, vfork);
546 __weak_reference(_wait, wait);
551 struct pthread *curthread = tls_get_curthread();
555 oldcancel = _thr_cancel_enter(curthread);
557 _thr_cancel_leave(curthread, oldcancel);
562 __weak_reference(__wait4, wait4);
565 __wait4(pid_t pid, int *istat, int options, struct rusage *rusage)
567 struct pthread *curthread = tls_get_curthread();
571 oldcancel = _thr_cancel_enter(curthread);
572 ret = __sys_wait4(pid, istat, options, rusage);
573 _thr_cancel_leave(curthread, oldcancel);
578 __weak_reference(_waitpid, waitpid);
581 _waitpid(pid_t wpid, int *status, int options)
583 struct pthread *curthread = tls_get_curthread();
587 oldcancel = _thr_cancel_enter(curthread);
588 ret = __waitpid(wpid, status, options);
589 _thr_cancel_leave(curthread, oldcancel);
594 __weak_reference(__write, write);
597 __write(int fd, const void *buf, size_t nbytes)
599 struct pthread *curthread = tls_get_curthread();
603 oldcancel = _thr_cancel_enter(curthread);
604 ret = __sys_write(fd, buf, nbytes);
605 _thr_cancel_leave(curthread, oldcancel);
610 __weak_reference(__writev, writev);
613 __writev(int fd, const struct iovec *iov, int iovcnt)
615 struct pthread *curthread = tls_get_curthread();
619 oldcancel = _thr_cancel_enter(curthread);
620 ret = __sys_writev(fd, iov, iovcnt);
621 _thr_cancel_leave(curthread, oldcancel);