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.1 2005/02/01 12:38:27 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/cdefs.h>
68 #include <sys/types.h>
70 #include <sys/param.h>
71 #include <sys/select.h>
72 #include <sys/signalvar.h>
73 #include <sys/socket.h>
92 #include "thr_private.h"
94 extern int __creat(const char *, mode_t);
95 extern int __pause(void);
96 extern int __pselect(int count, fd_set *rfds, fd_set *wfds, fd_set *efds,
97 const struct timespec *timo, const sigset_t *mask);
98 extern unsigned int __sleep(unsigned int);
99 extern int __system(const char *);
100 extern int __tcdrain(int);
101 extern pid_t __wait(int *);
102 extern pid_t __sys_wait4(pid_t, int *, int, struct rusage *);
103 extern pid_t __waitpid(pid_t, int *, int);
105 __weak_reference(__accept, accept);
107 __accept(int s, struct sockaddr *addr, socklen_t *addrlen)
109 struct pthread *curthread;
113 curthread = _get_curthread();
114 oldcancel = _thr_cancel_enter(curthread);
115 ret = __sys_accept(s, addr, addrlen);
116 _thr_cancel_leave(curthread, oldcancel);
121 __weak_reference(_aio_suspend, aio_suspend);
124 _aio_suspend(const struct aiocb * const iocbs[], int niocb, const struct
127 struct pthread *curthread = _get_curthread();
131 oldcancel = _thr_cancel_enter(curthread);
132 ret = __sys_aio_suspend(iocbs, niocb, timeout);
133 _thr_cancel_leave(curthread, oldcancel);
138 __weak_reference(__close, close);
143 struct pthread *curthread = _get_curthread();
147 oldcancel = _thr_cancel_enter(curthread);
148 ret = __sys_close(fd);
149 _thr_cancel_leave(curthread, oldcancel);
154 __weak_reference(__connect, connect);
157 __connect(int fd, const struct sockaddr *name, socklen_t namelen)
159 struct pthread *curthread = _get_curthread();
163 curthread = _get_curthread();
164 oldcancel = _thr_cancel_enter(curthread);
165 ret = __sys_connect(fd, name, namelen);
166 _thr_cancel_leave(curthread, oldcancel);
171 __weak_reference(___creat, creat);
174 ___creat(const char *path, mode_t mode)
176 struct pthread *curthread = _get_curthread();
180 oldcancel = _thr_cancel_enter(curthread);
181 ret = __creat(path, mode);
182 _thr_cancel_leave(curthread, oldcancel);
187 __weak_reference(__fcntl, fcntl);
190 __fcntl(int fd, int cmd,...)
192 struct pthread *curthread = _get_curthread();
197 oldcancel = _thr_cancel_enter(curthread);
202 ret = __sys_fcntl(fd, cmd, va_arg(ap, int));
206 ret = __sys_fcntl(fd, cmd, va_arg(ap, int));
210 ret = __sys_fcntl(fd, cmd);
213 ret = __sys_fcntl(fd, cmd, va_arg(ap, void *));
217 _thr_cancel_leave(curthread, oldcancel);
222 __weak_reference(__fsync, fsync);
227 struct pthread *curthread = _get_curthread();
231 oldcancel = _thr_cancel_enter(curthread);
232 ret = __sys_fsync(fd);
233 _thr_cancel_leave(curthread, oldcancel);
238 __weak_reference(__msync, msync);
241 __msync(void *addr, size_t len, int flags)
243 struct pthread *curthread = _get_curthread();
247 oldcancel = _thr_cancel_enter(curthread);
248 ret = __sys_msync(addr, len, flags);
249 _thr_cancel_leave(curthread, oldcancel);
254 __weak_reference(__nanosleep, nanosleep);
257 __nanosleep(const struct timespec *time_to_sleep,
258 struct timespec *time_remaining)
260 struct pthread *curthread = _get_curthread();
264 oldcancel = _thr_cancel_enter(curthread);
265 ret = __sys_nanosleep(time_to_sleep, time_remaining);
266 _thr_cancel_leave(curthread, oldcancel);
271 __weak_reference(___open, open);
274 ___open(const char *path, int flags,...)
276 struct pthread *curthread = _get_curthread();
282 _thread_printf(1, "%s %s\n", __func__, path);
283 oldcancel = _thr_cancel_enter(curthread);
285 /* Check if the file is being created: */
286 if (flags & O_CREAT) {
287 /* Get the creation mode: */
289 mode = va_arg(ap, int);
293 ret = __sys_open(path, flags, mode);
295 _thr_cancel_leave(curthread, oldcancel);
300 __weak_reference(_pause, pause);
305 struct pthread *curthread = _get_curthread();
309 oldcancel = _thr_cancel_enter(curthread);
311 _thr_cancel_leave(curthread, oldcancel);
316 __weak_reference(__poll, poll);
319 __poll(struct pollfd *fds, unsigned int nfds, int timeout)
321 struct pthread *curthread = _get_curthread();
325 oldcancel = _thr_cancel_enter(curthread);
326 ret = __sys_poll(fds, nfds, timeout);
327 _thr_cancel_leave(curthread, oldcancel);
333 __weak_reference(_pselect, pselect);
336 _pselect(int count, fd_set *rfds, fd_set *wfds, fd_set *efds,
337 const struct timespec *timo, const sigset_t *mask)
339 struct pthread *curthread = _get_curthread();
343 oldcancel = _thr_cancel_enter(curthread);
344 ret = __pselect(count, rfds, wfds, efds, timo, mask);
345 _thr_cancel_leave(curthread, oldcancel);
351 __weak_reference(_raise, raise);
358 if (!_thr_isthreaded())
359 ret = kill(getpid(), sig);
361 ret = pthread_kill(pthread_self(), sig);
370 __weak_reference(__read, read);
373 __read(int fd, void *buf, size_t nbytes)
375 struct pthread *curthread = _get_curthread();
379 oldcancel = _thr_cancel_enter(curthread);
380 ret = __sys_read(fd, buf, nbytes);
381 _thr_cancel_leave(curthread, oldcancel);
386 __weak_reference(__readv, readv);
389 __readv(int fd, const struct iovec *iov, int iovcnt)
391 struct pthread *curthread = _get_curthread();
395 oldcancel = _thr_cancel_enter(curthread);
396 ret = __sys_readv(fd, iov, iovcnt);
397 _thr_cancel_leave(curthread, oldcancel);
402 __weak_reference(__recvfrom, recvfrom);
405 __recvfrom(int s, void *b, size_t l, int f, struct sockaddr *from,
408 struct pthread *curthread = _get_curthread();
412 oldcancel = _thr_cancel_enter(curthread);
413 ret = __sys_recvfrom(s, b, l, f, from, fl);
414 _thr_cancel_leave(curthread, oldcancel);
418 __weak_reference(__recvmsg, recvmsg);
421 __recvmsg(int s, struct msghdr *m, int f)
423 struct pthread *curthread = _get_curthread();
427 oldcancel = _thr_cancel_enter(curthread);
428 ret = __sys_recvmsg(s, m, f);
429 _thr_cancel_leave(curthread, oldcancel);
433 __weak_reference(__select, select);
436 __select(int numfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
437 struct timeval *timeout)
439 struct pthread *curthread = _get_curthread();
443 oldcancel = _thr_cancel_enter(curthread);
444 ret = __sys_select(numfds, readfds, writefds, exceptfds, timeout);
445 _thr_cancel_leave(curthread, oldcancel);
449 __weak_reference(__sendmsg, sendmsg);
452 __sendmsg(int s, const struct msghdr *m, int f)
454 struct pthread *curthread = _get_curthread();
458 oldcancel = _thr_cancel_enter(curthread);
459 ret = __sys_sendmsg(s, m, f);
460 _thr_cancel_leave(curthread, oldcancel);
464 __weak_reference(__sendto, sendto);
467 __sendto(int s, const void *m, size_t l, int f, const struct sockaddr *t,
470 struct pthread *curthread = _get_curthread();
474 oldcancel = _thr_cancel_enter(curthread);
475 ret = __sys_sendto(s, m, l, f, t, tl);
476 _thr_cancel_leave(curthread, oldcancel);
481 _sleep(unsigned int seconds)
483 struct pthread *curthread = _get_curthread();
487 oldcancel = _thr_cancel_enter(curthread);
488 ret = __sleep(seconds);
489 _thr_cancel_leave(curthread, oldcancel);
494 __weak_reference(_system, system);
497 _system(const char *string)
499 struct pthread *curthread = _get_curthread();
503 oldcancel = _thr_cancel_enter(curthread);
504 ret = __system(string);
505 _thr_cancel_leave(curthread, oldcancel);
510 __weak_reference(_tcdrain, tcdrain);
515 struct pthread *curthread = _get_curthread();
519 oldcancel = _thr_cancel_enter(curthread);
521 _thr_cancel_leave(curthread, oldcancel);
526 __weak_reference(_vfork, vfork);
534 __weak_reference(_wait, wait);
539 struct pthread *curthread = _get_curthread();
543 oldcancel = _thr_cancel_enter(curthread);
545 _thr_cancel_leave(curthread, oldcancel);
550 __weak_reference(__wait4, wait4);
553 __wait4(pid_t pid, int *istat, int options, struct rusage *rusage)
555 struct pthread *curthread = _get_curthread();
559 oldcancel = _thr_cancel_enter(curthread);
560 ret = __sys_wait4(pid, istat, options, rusage);
561 _thr_cancel_leave(curthread, oldcancel);
566 __weak_reference(_waitpid, waitpid);
569 _waitpid(pid_t wpid, int *status, int options)
571 struct pthread *curthread = _get_curthread();
575 oldcancel = _thr_cancel_enter(curthread);
576 ret = __waitpid(wpid, status, options);
577 _thr_cancel_leave(curthread, oldcancel);
582 __weak_reference(__write, write);
585 __write(int fd, const void *buf, size_t nbytes)
587 struct pthread *curthread = _get_curthread();
591 oldcancel = _thr_cancel_enter(curthread);
592 ret = __sys_write(fd, buf, nbytes);
593 _thr_cancel_leave(curthread, oldcancel);
598 __weak_reference(__writev, writev);
601 __writev(int fd, const struct iovec *iov, int iovcnt)
603 struct pthread *curthread = _get_curthread();
607 oldcancel = _thr_cancel_enter(curthread);
608 ret = __sys_writev(fd, iov, iovcnt);
609 _thr_cancel_leave(curthread, oldcancel);