1 /* 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>. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 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 17 * distribution. 18 * 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. 30 * 31 * $DragonFly: src/lib/libthread_xu/thread/thr_syscalls.c,v 1.8 2008/01/10 22:30:27 nth Exp $ 32 */ 33 34 /* 35 * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au> 36 * All rights reserved. 37 * 38 * Redistribution and use in source and binary forms, with or without 39 * modification, are permitted provided that the following conditions 40 * are met: 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. 52 * 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 63 * SUCH DAMAGE. 64 * 65 */ 66 67 #include <sys/types.h> 68 #include <sys/mman.h> 69 #include <sys/param.h> 70 #include <sys/select.h> 71 #include <sys/signalvar.h> 72 #include <sys/socket.h> 73 #include <sys/stat.h> 74 #include <sys/time.h> 75 #include <sys/uio.h> 76 #include <sys/wait.h> 77 78 #include <machine/tls.h> 79 80 #include <aio.h> 81 #include <dirent.h> 82 #include <errno.h> 83 #include <fcntl.h> 84 #include <poll.h> 85 #include <signal.h> 86 #include <stdarg.h> 87 #include <stdio.h> 88 #include <stdlib.h> 89 #include <string.h> 90 #include <termios.h> 91 #include <unistd.h> 92 #include <pthread.h> 93 94 #include "thr_private.h" 95 96 extern int __creat(const char *, mode_t); 97 extern int __pause(void); 98 extern int __sys_pselect(int, fd_set *, fd_set *, fd_set *, 99 const struct timespec *, const sigset_t *); 100 extern unsigned __sleep(unsigned int); 101 extern int __system(const char *); 102 extern int __tcdrain(int); 103 extern int __usleep(unsigned); 104 extern pid_t __wait(int *); 105 extern pid_t __waitpid(pid_t, int *, int); 106 extern int __sys_aio_suspend(const struct aiocb * const[], int, 107 const struct timespec *); 108 extern int __sys_accept(int, struct sockaddr *, socklen_t *); 109 extern int __sys_connect(int, const struct sockaddr *, socklen_t); 110 extern int __sys_fsync(int); 111 extern int __sys_msync(void *, size_t, int); 112 extern int __sys_poll(struct pollfd *, unsigned, int); 113 extern ssize_t __sys_recv(int, void *, size_t, int); 114 extern ssize_t __sys_recvfrom(int, void *, size_t, int, struct sockaddr *, socklen_t *); 115 extern ssize_t __sys_recvmsg(int, struct msghdr *, int); 116 extern int __sys_select(int, fd_set *, fd_set *, fd_set *, struct timeval *); 117 extern int __sys_sendfile(int, int, off_t, size_t, struct sf_hdtr *, 118 off_t *, int); 119 extern ssize_t __sys_sendmsg(int, const struct msghdr *, int); 120 extern ssize_t __sys_sendto(int, const void *,size_t, int, const struct sockaddr *, socklen_t); 121 extern ssize_t __sys_readv(int, const struct iovec *, int); 122 extern pid_t __sys_wait4(pid_t, int *, int, struct rusage *); 123 extern ssize_t __sys_writev(int, const struct iovec *, int); 124 125 int ___creat(const char *, mode_t); 126 int ___usleep(unsigned); 127 int __accept(int, struct sockaddr *, socklen_t *); 128 int __close(int); 129 int __connect(int, const struct sockaddr *, socklen_t); 130 int __fcntl(int, int,...); 131 int __fsync(int); 132 int __msync(void *, size_t, int); 133 int __nanosleep(const struct timespec *, 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 ssize_t __read(int, void *buf, size_t); 138 ssize_t __readv(int, const struct iovec *, int); 139 ssize_t __recvfrom(int, void *, size_t, int f, struct sockaddr *, socklen_t *); 140 ssize_t __recvmsg(int, struct msghdr *, int); 141 int __select(int, fd_set *, fd_set *, fd_set *, struct timeval *); 142 ssize_t __sendmsg(int, const struct msghdr *, int); 143 ssize_t __sendto(int, const void *, size_t, int, 144 const struct sockaddr *, socklen_t); 145 pid_t __wait4(pid_t, int *, int, struct rusage *); 146 ssize_t __write(int, const void *, size_t); 147 ssize_t __writev(int, const struct iovec *, int); 148 int _aio_suspend(const struct aiocb * const iocbs[], int, 149 const struct timespec *); 150 int _pause(void); 151 int __pselect(int, fd_set *, fd_set *, fd_set *, 152 const struct timespec *, const sigset_t *); 153 int _raise(int); 154 unsigned _sleep(unsigned); 155 int _system(const char *); 156 int _tcdrain(int); 157 int _vfork(void); 158 pid_t _wait(int *); 159 pid_t _waitpid(pid_t wpid, int *status, int options); 160 161 int 162 __accept(int s, struct sockaddr *addr, socklen_t *addrlen) 163 { 164 struct pthread *curthread; 165 int oldcancel; 166 int ret; 167 168 curthread = tls_get_curthread(); 169 oldcancel = _thr_cancel_enter(curthread); 170 ret = __sys_accept(s, addr, addrlen); 171 _thr_cancel_leave(curthread, oldcancel); 172 173 return (ret); 174 } 175 176 __strong_reference(__accept, accept); 177 178 int 179 _aio_suspend(const struct aiocb * const iocbs[], int niocb, const struct 180 timespec *timeout) 181 { 182 struct pthread *curthread = tls_get_curthread(); 183 int oldcancel; 184 int ret; 185 186 oldcancel = _thr_cancel_enter(curthread); 187 ret = __sys_aio_suspend(iocbs, niocb, timeout); 188 _thr_cancel_leave(curthread, oldcancel); 189 190 return (ret); 191 } 192 193 __strong_reference(_aio_suspend, aio_suspend); 194 195 int 196 __close(int fd) 197 { 198 struct pthread *curthread = tls_get_curthread(); 199 int oldcancel; 200 int ret; 201 202 oldcancel = _thr_cancel_enter(curthread); 203 ret = __sys_close(fd); 204 _thr_cancel_leave(curthread, oldcancel); 205 206 return (ret); 207 } 208 209 __strong_reference(__close, close); 210 211 int 212 __connect(int fd, const struct sockaddr *name, socklen_t namelen) 213 { 214 struct pthread *curthread = tls_get_curthread(); 215 int oldcancel; 216 int ret; 217 218 oldcancel = _thr_cancel_enter(curthread); 219 ret = __sys_connect(fd, name, namelen); 220 _thr_cancel_leave(curthread, oldcancel); 221 222 return (ret); 223 } 224 225 __strong_reference(__connect, connect); 226 227 int 228 ___creat(const char *path, mode_t mode) 229 { 230 struct pthread *curthread = tls_get_curthread(); 231 int oldcancel; 232 int ret; 233 234 oldcancel = _thr_cancel_enter(curthread); 235 ret = __creat(path, mode); 236 _thr_cancel_leave(curthread, oldcancel); 237 238 return ret; 239 } 240 241 __strong_reference(___creat, creat); 242 243 int 244 __fcntl(int fd, int cmd,...) 245 { 246 struct pthread *curthread = tls_get_curthread(); 247 int oldcancel; 248 int ret; 249 va_list ap; 250 251 oldcancel = _thr_cancel_enter(curthread); 252 253 va_start(ap, cmd); 254 switch (cmd) { 255 case F_DUPFD: 256 ret = __sys_fcntl(fd, cmd, va_arg(ap, int)); 257 break; 258 case F_SETFD: 259 case F_SETFL: 260 ret = __sys_fcntl(fd, cmd, va_arg(ap, int)); 261 break; 262 case F_GETFD: 263 case F_GETFL: 264 ret = __sys_fcntl(fd, cmd); 265 break; 266 default: 267 ret = __sys_fcntl(fd, cmd, va_arg(ap, void *)); 268 } 269 va_end(ap); 270 271 _thr_cancel_leave(curthread, oldcancel); 272 273 return (ret); 274 } 275 276 __strong_reference(__fcntl, fcntl); 277 278 int 279 __fsync(int fd) 280 { 281 struct pthread *curthread = tls_get_curthread(); 282 int oldcancel; 283 int ret; 284 285 oldcancel = _thr_cancel_enter(curthread); 286 ret = __sys_fsync(fd); 287 _thr_cancel_leave(curthread, oldcancel); 288 289 return (ret); 290 } 291 292 __strong_reference(__fsync, fsync); 293 294 int 295 __msync(void *addr, size_t len, int flags) 296 { 297 struct pthread *curthread = tls_get_curthread(); 298 int oldcancel; 299 int ret; 300 301 oldcancel = _thr_cancel_enter(curthread); 302 ret = __sys_msync(addr, len, flags); 303 _thr_cancel_leave(curthread, oldcancel); 304 305 return ret; 306 } 307 308 __strong_reference(__msync, msync); 309 310 int 311 __nanosleep(const struct timespec *time_to_sleep, 312 struct timespec *time_remaining) 313 { 314 struct pthread *curthread = tls_get_curthread(); 315 int oldcancel; 316 int ret; 317 318 oldcancel = _thr_cancel_enter(curthread); 319 ret = __sys_nanosleep(time_to_sleep, time_remaining); 320 _thr_cancel_leave(curthread, oldcancel); 321 322 return (ret); 323 } 324 325 __strong_reference(__nanosleep, nanosleep); 326 327 int 328 __open(const char *path, int flags,...) 329 { 330 struct pthread *curthread = tls_get_curthread(); 331 int oldcancel; 332 int ret; 333 int mode = 0; 334 va_list ap; 335 336 oldcancel = _thr_cancel_enter(curthread); 337 338 /* Check if the file is being created: */ 339 if (flags & O_CREAT) { 340 /* Get the creation mode: */ 341 va_start(ap, flags); 342 mode = va_arg(ap, int); 343 va_end(ap); 344 } 345 346 ret = __sys_open(path, flags, mode); 347 348 _thr_cancel_leave(curthread, oldcancel); 349 350 return ret; 351 } 352 353 __strong_reference(__open, open); 354 355 int 356 __openat(int fd, const char *path, int flags,...) 357 { 358 struct pthread *curthread = tls_get_curthread(); 359 int oldcancel; 360 int ret; 361 int mode = 0; 362 va_list ap; 363 364 oldcancel = _thr_cancel_enter(curthread); 365 366 /* Check if the file is being created: */ 367 if (flags & O_CREAT) { 368 /* Get the creation mode: */ 369 va_start(ap, flags); 370 mode = va_arg(ap, int); 371 va_end(ap); 372 } 373 374 ret = __sys_openat(fd, path, flags, mode); 375 376 _thr_cancel_leave(curthread, oldcancel); 377 378 return ret; 379 } 380 381 __strong_reference(__openat, openat); 382 383 int 384 _pause(void) 385 { 386 struct pthread *curthread = tls_get_curthread(); 387 int oldcancel; 388 int ret; 389 390 oldcancel = _thr_cancel_enter(curthread); 391 ret = __pause(); 392 _thr_cancel_leave(curthread, oldcancel); 393 394 return ret; 395 } 396 397 __strong_reference(_pause, pause); 398 399 int 400 __poll(struct pollfd *fds, unsigned int nfds, int timeout) 401 { 402 struct pthread *curthread = tls_get_curthread(); 403 int oldcancel; 404 int ret; 405 406 oldcancel = _thr_cancel_enter(curthread); 407 ret = __sys_poll(fds, nfds, timeout); 408 _thr_cancel_leave(curthread, oldcancel); 409 410 return ret; 411 } 412 413 __strong_reference(__poll, poll); 414 415 int 416 __pselect(int count, fd_set *rfds, fd_set *wfds, fd_set *efds, 417 const struct timespec *timo, const sigset_t *mask) 418 { 419 struct pthread *curthread = tls_get_curthread(); 420 int oldcancel; 421 int ret; 422 423 oldcancel = _thr_cancel_enter(curthread); 424 ret = __sys_pselect(count, rfds, wfds, efds, timo, mask); 425 _thr_cancel_leave(curthread, oldcancel); 426 427 return (ret); 428 } 429 __strong_reference(__pselect, pselect); 430 431 432 int 433 _raise(int sig) 434 { 435 int ret; 436 437 if (!_thr_isthreaded()) 438 ret = kill(getpid(), sig); 439 else 440 ret = _thr_send_sig(tls_get_curthread(), sig); 441 return (ret); 442 } 443 444 __strong_reference(_raise, raise); 445 446 ssize_t 447 __read(int fd, void *buf, size_t nbytes) 448 { 449 struct pthread *curthread = tls_get_curthread(); 450 int oldcancel; 451 ssize_t ret; 452 453 oldcancel = _thr_cancel_enter(curthread); 454 ret = __sys_read(fd, buf, nbytes); 455 _thr_cancel_leave(curthread, oldcancel); 456 457 return ret; 458 } 459 460 __strong_reference(__read, read); 461 462 ssize_t 463 __readv(int fd, const struct iovec *iov, int iovcnt) 464 { 465 struct pthread *curthread = tls_get_curthread(); 466 int oldcancel; 467 ssize_t ret; 468 469 oldcancel = _thr_cancel_enter(curthread); 470 ret = __sys_readv(fd, iov, iovcnt); 471 _thr_cancel_leave(curthread, oldcancel); 472 473 return ret; 474 } 475 476 __strong_reference(__readv, readv); 477 478 ssize_t 479 __recvfrom(int s, void *b, size_t l, int f, struct sockaddr *from, 480 socklen_t *fl) 481 { 482 struct pthread *curthread = tls_get_curthread(); 483 int oldcancel; 484 ssize_t ret; 485 486 oldcancel = _thr_cancel_enter(curthread); 487 ret = __sys_recvfrom(s, b, l, f, from, fl); 488 _thr_cancel_leave(curthread, oldcancel); 489 return (ret); 490 } 491 492 __strong_reference(__recvfrom, recvfrom); 493 494 ssize_t 495 __recvmsg(int s, struct msghdr *m, int f) 496 { 497 struct pthread *curthread = tls_get_curthread(); 498 ssize_t ret; 499 int oldcancel; 500 501 oldcancel = _thr_cancel_enter(curthread); 502 ret = __sys_recvmsg(s, m, f); 503 _thr_cancel_leave(curthread, oldcancel); 504 return (ret); 505 } 506 507 __strong_reference(__recvmsg, recvmsg); 508 509 int 510 __select(int numfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, 511 struct timeval *timeout) 512 { 513 struct pthread *curthread = tls_get_curthread(); 514 int oldcancel; 515 int ret; 516 517 oldcancel = _thr_cancel_enter(curthread); 518 ret = __sys_select(numfds, readfds, writefds, exceptfds, timeout); 519 _thr_cancel_leave(curthread, oldcancel); 520 return ret; 521 } 522 523 __strong_reference(__select, select); 524 525 ssize_t 526 __sendmsg(int s, const struct msghdr *m, int f) 527 { 528 struct pthread *curthread = tls_get_curthread(); 529 ssize_t ret; 530 int oldcancel; 531 532 oldcancel = _thr_cancel_enter(curthread); 533 ret = __sys_sendmsg(s, m, f); 534 _thr_cancel_leave(curthread, oldcancel); 535 return (ret); 536 } 537 538 __strong_reference(__sendmsg, sendmsg); 539 540 ssize_t 541 __sendto(int s, const void *m, size_t l, int f, const struct sockaddr *t, 542 socklen_t tl) 543 { 544 struct pthread *curthread = tls_get_curthread(); 545 ssize_t ret; 546 int oldcancel; 547 548 oldcancel = _thr_cancel_enter(curthread); 549 ret = __sys_sendto(s, m, l, f, t, tl); 550 _thr_cancel_leave(curthread, oldcancel); 551 return (ret); 552 } 553 554 __strong_reference(__sendto, sendto); 555 556 unsigned int 557 _sleep(unsigned int seconds) 558 { 559 struct pthread *curthread = tls_get_curthread(); 560 int oldcancel; 561 unsigned int ret; 562 563 oldcancel = _thr_cancel_enter(curthread); 564 ret = __sleep(seconds); 565 _thr_cancel_leave(curthread, oldcancel); 566 567 return (ret); 568 } 569 570 int 571 _system(const char *string) 572 { 573 struct pthread *curthread = tls_get_curthread(); 574 int oldcancel; 575 int ret; 576 577 oldcancel = _thr_cancel_enter(curthread); 578 ret = __system(string); 579 _thr_cancel_leave(curthread, oldcancel); 580 581 return ret; 582 } 583 584 __strong_reference(_system, system); 585 586 int 587 _tcdrain(int fd) 588 { 589 struct pthread *curthread = tls_get_curthread(); 590 int oldcancel; 591 int ret; 592 593 oldcancel = _thr_cancel_enter(curthread); 594 ret = __tcdrain(fd); 595 _thr_cancel_leave(curthread, oldcancel); 596 597 return (ret); 598 } 599 600 __strong_reference(_tcdrain, tcdrain); 601 602 int 603 ___usleep(unsigned int useconds) 604 { 605 struct pthread *curthread = tls_get_curthread(); 606 int oldcancel; 607 int ret; 608 609 oldcancel = _thr_cancel_enter(curthread); 610 ret = __usleep(useconds); 611 _thr_cancel_leave(curthread, oldcancel); 612 613 return (ret); 614 } 615 616 __strong_reference(___usleep, usleep); 617 618 int 619 _vfork(void) 620 { 621 return (fork()); 622 } 623 624 __strong_reference(_vfork, vfork); 625 626 pid_t 627 _wait(int *istat) 628 { 629 struct pthread *curthread = tls_get_curthread(); 630 int oldcancel; 631 pid_t ret; 632 633 oldcancel = _thr_cancel_enter(curthread); 634 ret = __wait(istat); 635 _thr_cancel_leave(curthread, oldcancel); 636 637 return ret; 638 } 639 640 __strong_reference(_wait, wait); 641 642 pid_t 643 __wait4(pid_t pid, int *istat, int options, struct rusage *rusage) 644 { 645 struct pthread *curthread = tls_get_curthread(); 646 int oldcancel; 647 pid_t ret; 648 649 oldcancel = _thr_cancel_enter(curthread); 650 ret = __sys_wait4(pid, istat, options, rusage); 651 _thr_cancel_leave(curthread, oldcancel); 652 653 return ret; 654 } 655 656 __strong_reference(__wait4, wait4); 657 658 pid_t 659 _waitpid(pid_t wpid, int *status, int options) 660 { 661 struct pthread *curthread = tls_get_curthread(); 662 int oldcancel; 663 pid_t ret; 664 665 oldcancel = _thr_cancel_enter(curthread); 666 ret = __waitpid(wpid, status, options); 667 _thr_cancel_leave(curthread, oldcancel); 668 669 return ret; 670 } 671 672 __strong_reference(_waitpid, waitpid); 673 674 ssize_t 675 __write(int fd, const void *buf, size_t nbytes) 676 { 677 struct pthread *curthread = tls_get_curthread(); 678 int oldcancel; 679 ssize_t ret; 680 681 oldcancel = _thr_cancel_enter(curthread); 682 ret = __sys_write(fd, buf, nbytes); 683 _thr_cancel_leave(curthread, oldcancel); 684 685 return ret; 686 } 687 688 __strong_reference(__write, write); 689 690 ssize_t 691 __writev(int fd, const struct iovec *iov, int iovcnt) 692 { 693 struct pthread *curthread = tls_get_curthread(); 694 int oldcancel; 695 ssize_t ret; 696 697 oldcancel = _thr_cancel_enter(curthread); 698 ret = __sys_writev(fd, iov, iovcnt); 699 _thr_cancel_leave(curthread, oldcancel); 700 701 return ret; 702 } 703 704 __strong_reference(__writev, writev); 705 706