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