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 * $FreeBSD$ 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. Neither the name of the author nor the names of any co-contributors 47 * may be used to endorse or promote products derived from this software 48 * without specific prior written permission. 49 * 50 * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND 51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 53 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 60 * SUCH DAMAGE. 61 * 62 */ 63 64 #include "namespace.h" 65 #include <sys/types.h> 66 #include <sys/mman.h> 67 #include <sys/param.h> 68 #include <sys/select.h> 69 #include <sys/signalvar.h> 70 #include <sys/socket.h> 71 #include <sys/stat.h> 72 #include <sys/time.h> 73 #include <sys/uio.h> 74 #include <sys/wait.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 __pselect(int, fd_set *, fd_set *, fd_set *, 94 const struct timespec *, const sigset_t *); 95 extern unsigned __sleep(unsigned int); 96 extern int __system(const char *); 97 extern int __tcdrain(int); 98 extern int __usleep(useconds_t); 99 extern pid_t __wait(int *); 100 extern pid_t __waitpid(pid_t, int *, int); 101 extern int __sys_aio_suspend(const struct aiocb * const[], int, 102 const struct timespec *); 103 extern int __sys_accept(int, struct sockaddr *, socklen_t *); 104 extern int __sys_connect(int, const struct sockaddr *, socklen_t); 105 extern int __sys_fsync(int); 106 extern int __sys_msync(void *, size_t, int); 107 extern int __sys_poll(struct pollfd *, unsigned, int); 108 extern ssize_t __sys_recv(int, void *, size_t, int); 109 extern ssize_t __sys_recvfrom(int, void *, size_t, int, struct sockaddr *, socklen_t *); 110 extern ssize_t __sys_recvmsg(int, struct msghdr *, int); 111 extern int __sys_select(int, fd_set *, fd_set *, fd_set *, struct timeval *); 112 extern int __sys_sendfile(int, int, off_t, size_t, struct sf_hdtr *, 113 off_t *, int); 114 extern ssize_t __sys_sendmsg(int, const struct msghdr *, int); 115 extern ssize_t __sys_sendto(int, const void *,size_t, int, const struct sockaddr *, socklen_t); 116 extern ssize_t __sys_readv(int, const struct iovec *, int); 117 extern pid_t __sys_wait4(pid_t, int *, int, struct rusage *); 118 extern ssize_t __sys_writev(int, const struct iovec *, int); 119 120 int ___creat(const char *, mode_t); 121 int ___pselect(int, fd_set *, fd_set *, fd_set *, 122 const struct timespec *, const sigset_t *); 123 unsigned ___sleep(unsigned); 124 int ___system(const char *); 125 int ___tcdrain(int); 126 int ___usleep(useconds_t useconds); 127 pid_t ___wait(int *); 128 pid_t ___waitpid(pid_t, int *, int); 129 int __accept(int, struct sockaddr *, socklen_t *); 130 int __aio_suspend(const struct aiocb * const iocbs[], int, 131 const struct timespec *); 132 int __close(int); 133 int __connect(int, const struct sockaddr *, socklen_t); 134 int __fcntl(int, int,...); 135 int __fsync(int); 136 int __msync(void *, size_t, int); 137 int __nanosleep(const struct timespec *, struct timespec *); 138 int __open(const char *, int,...); 139 int __poll(struct pollfd *, unsigned int, int); 140 ssize_t __read(int, void *buf, size_t); 141 ssize_t __readv(int, const struct iovec *, int); 142 ssize_t __recvfrom(int, void *, size_t, int f, struct sockaddr *, socklen_t *); 143 ssize_t __recvmsg(int, struct msghdr *, int); 144 int __select(int, fd_set *, fd_set *, fd_set *, struct timeval *); 145 ssize_t __sendmsg(int, const struct msghdr *, int); 146 ssize_t __sendto(int, const void *, size_t, int, 147 const struct sockaddr *, socklen_t); 148 pid_t __wait3(int *, int, struct rusage *); 149 pid_t __wait4(pid_t, int *, int, struct rusage *); 150 ssize_t __write(int, const void *, size_t); 151 ssize_t __writev(int, const struct iovec *, int); 152 153 __weak_reference(__accept, accept); 154 155 int 156 __accept(int s, struct sockaddr *addr, socklen_t *addrlen) 157 { 158 struct pthread *curthread; 159 int ret; 160 161 curthread = _get_curthread(); 162 _thr_cancel_enter(curthread); 163 ret = __sys_accept(s, addr, addrlen); 164 _thr_cancel_leave(curthread); 165 166 return (ret); 167 } 168 169 __weak_reference(__aio_suspend, aio_suspend); 170 171 int 172 __aio_suspend(const struct aiocb * const iocbs[], int niocb, const struct 173 timespec *timeout) 174 { 175 struct pthread *curthread = _get_curthread(); 176 int ret; 177 178 _thr_cancel_enter(curthread); 179 ret = __sys_aio_suspend(iocbs, niocb, timeout); 180 _thr_cancel_leave(curthread); 181 182 return (ret); 183 } 184 185 __weak_reference(__close, close); 186 187 int 188 __close(int fd) 189 { 190 struct pthread *curthread = _get_curthread(); 191 int ret; 192 193 _thr_cancel_enter(curthread); 194 ret = __sys_close(fd); 195 _thr_cancel_leave(curthread); 196 197 return (ret); 198 } 199 200 __weak_reference(__connect, connect); 201 202 int 203 __connect(int fd, const struct sockaddr *name, socklen_t namelen) 204 { 205 struct pthread *curthread = _get_curthread(); 206 int ret; 207 208 _thr_cancel_enter(curthread); 209 ret = __sys_connect(fd, name, namelen); 210 _thr_cancel_leave(curthread); 211 212 return (ret); 213 } 214 215 __weak_reference(___creat, creat); 216 217 int 218 ___creat(const char *path, mode_t mode) 219 { 220 struct pthread *curthread = _get_curthread(); 221 int ret; 222 223 _thr_cancel_enter(curthread); 224 ret = __creat(path, mode); 225 _thr_cancel_leave(curthread); 226 227 return ret; 228 } 229 230 __weak_reference(__fcntl, fcntl); 231 232 int 233 __fcntl(int fd, int cmd,...) 234 { 235 struct pthread *curthread = _get_curthread(); 236 int ret; 237 va_list ap; 238 239 _thr_cancel_enter(curthread); 240 241 va_start(ap, cmd); 242 switch (cmd) { 243 case F_DUPFD: 244 ret = __sys_fcntl(fd, cmd, va_arg(ap, int)); 245 break; 246 case F_SETFD: 247 case F_SETFL: 248 ret = __sys_fcntl(fd, cmd, va_arg(ap, int)); 249 break; 250 case F_GETFD: 251 case F_GETFL: 252 ret = __sys_fcntl(fd, cmd); 253 break; 254 default: 255 ret = __sys_fcntl(fd, cmd, va_arg(ap, void *)); 256 } 257 va_end(ap); 258 259 _thr_cancel_leave(curthread); 260 261 return (ret); 262 } 263 264 __weak_reference(__fsync, fsync); 265 266 int 267 __fsync(int fd) 268 { 269 struct pthread *curthread = _get_curthread(); 270 int ret; 271 272 _thr_cancel_enter(curthread); 273 ret = __sys_fsync(fd); 274 _thr_cancel_leave(curthread); 275 276 return (ret); 277 } 278 279 __weak_reference(__msync, msync); 280 281 int 282 __msync(void *addr, size_t len, int flags) 283 { 284 struct pthread *curthread = _get_curthread(); 285 int ret; 286 287 _thr_cancel_enter(curthread); 288 ret = __sys_msync(addr, len, flags); 289 _thr_cancel_leave(curthread); 290 291 return ret; 292 } 293 294 __weak_reference(__nanosleep, nanosleep); 295 296 int 297 __nanosleep(const struct timespec *time_to_sleep, 298 struct timespec *time_remaining) 299 { 300 struct pthread *curthread = _get_curthread(); 301 int ret; 302 303 _thr_cancel_enter(curthread); 304 ret = __sys_nanosleep(time_to_sleep, time_remaining); 305 _thr_cancel_leave(curthread); 306 307 return (ret); 308 } 309 310 __weak_reference(__open, open); 311 312 int 313 __open(const char *path, int flags,...) 314 { 315 struct pthread *curthread = _get_curthread(); 316 int ret; 317 int mode = 0; 318 va_list ap; 319 320 _thr_cancel_enter(curthread); 321 322 /* Check if the file is being created: */ 323 if (flags & O_CREAT) { 324 /* Get the creation mode: */ 325 va_start(ap, flags); 326 mode = va_arg(ap, int); 327 va_end(ap); 328 } 329 330 ret = __sys_open(path, flags, mode); 331 332 _thr_cancel_leave(curthread); 333 334 return ret; 335 } 336 337 __weak_reference(__poll, poll); 338 339 int 340 __poll(struct pollfd *fds, unsigned int nfds, int timeout) 341 { 342 struct pthread *curthread = _get_curthread(); 343 int ret; 344 345 _thr_cancel_enter(curthread); 346 ret = __sys_poll(fds, nfds, timeout); 347 _thr_cancel_leave(curthread); 348 349 return ret; 350 } 351 352 __weak_reference(___pselect, pselect); 353 354 int 355 ___pselect(int count, fd_set *rfds, fd_set *wfds, fd_set *efds, 356 const struct timespec *timo, const sigset_t *mask) 357 { 358 struct pthread *curthread = _get_curthread(); 359 int ret; 360 361 _thr_cancel_enter(curthread); 362 ret = __pselect(count, rfds, wfds, efds, timo, mask); 363 _thr_cancel_leave(curthread); 364 365 return (ret); 366 } 367 368 __weak_reference(__read, read); 369 370 ssize_t 371 __read(int fd, void *buf, size_t nbytes) 372 { 373 struct pthread *curthread = _get_curthread(); 374 ssize_t ret; 375 376 _thr_cancel_enter(curthread); 377 ret = __sys_read(fd, buf, nbytes); 378 _thr_cancel_leave(curthread); 379 380 return ret; 381 } 382 383 __weak_reference(__readv, readv); 384 385 ssize_t 386 __readv(int fd, const struct iovec *iov, int iovcnt) 387 { 388 struct pthread *curthread = _get_curthread(); 389 ssize_t ret; 390 391 _thr_cancel_enter(curthread); 392 ret = __sys_readv(fd, iov, iovcnt); 393 _thr_cancel_leave(curthread); 394 395 return ret; 396 } 397 398 __weak_reference(__recvfrom, recvfrom); 399 400 ssize_t 401 __recvfrom(int s, void *b, size_t l, int f, struct sockaddr *from, 402 socklen_t *fl) 403 { 404 struct pthread *curthread = _get_curthread(); 405 ssize_t ret; 406 407 _thr_cancel_enter(curthread); 408 ret = __sys_recvfrom(s, b, l, f, from, fl); 409 _thr_cancel_leave(curthread); 410 return (ret); 411 } 412 413 __weak_reference(__recvmsg, recvmsg); 414 415 ssize_t 416 __recvmsg(int s, struct msghdr *m, int f) 417 { 418 struct pthread *curthread = _get_curthread(); 419 ssize_t ret; 420 421 _thr_cancel_enter(curthread); 422 ret = __sys_recvmsg(s, m, f); 423 _thr_cancel_leave(curthread); 424 return (ret); 425 } 426 427 __weak_reference(__select, select); 428 429 int 430 __select(int numfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, 431 struct timeval *timeout) 432 { 433 struct pthread *curthread = _get_curthread(); 434 int ret; 435 436 _thr_cancel_enter(curthread); 437 ret = __sys_select(numfds, readfds, writefds, exceptfds, timeout); 438 _thr_cancel_leave(curthread); 439 return ret; 440 } 441 442 __weak_reference(__sendmsg, sendmsg); 443 444 ssize_t 445 __sendmsg(int s, const struct msghdr *m, int f) 446 { 447 struct pthread *curthread = _get_curthread(); 448 ssize_t ret; 449 450 _thr_cancel_enter(curthread); 451 ret = __sys_sendmsg(s, m, f); 452 _thr_cancel_leave(curthread); 453 return (ret); 454 } 455 456 __weak_reference(__sendto, sendto); 457 458 ssize_t 459 __sendto(int s, const void *m, size_t l, int f, const struct sockaddr *t, 460 socklen_t tl) 461 { 462 struct pthread *curthread = _get_curthread(); 463 ssize_t ret; 464 465 _thr_cancel_enter(curthread); 466 ret = __sys_sendto(s, m, l, f, t, tl); 467 _thr_cancel_leave(curthread); 468 return (ret); 469 } 470 471 __weak_reference(___sleep, sleep); 472 473 unsigned int 474 ___sleep(unsigned int seconds) 475 { 476 struct pthread *curthread = _get_curthread(); 477 unsigned int ret; 478 479 _thr_cancel_enter(curthread); 480 ret = __sleep(seconds); 481 _thr_cancel_leave(curthread); 482 483 return (ret); 484 } 485 486 __weak_reference(___system, system); 487 488 int 489 ___system(const char *string) 490 { 491 struct pthread *curthread = _get_curthread(); 492 int ret; 493 494 _thr_cancel_enter(curthread); 495 ret = __system(string); 496 _thr_cancel_leave(curthread); 497 498 return ret; 499 } 500 501 __weak_reference(___tcdrain, tcdrain); 502 503 int 504 ___tcdrain(int fd) 505 { 506 struct pthread *curthread = _get_curthread(); 507 int ret; 508 509 _thr_cancel_enter(curthread); 510 ret = __tcdrain(fd); 511 _thr_cancel_leave(curthread); 512 513 return (ret); 514 } 515 516 __weak_reference(___usleep, usleep); 517 518 int 519 ___usleep(useconds_t useconds) 520 { 521 struct pthread *curthread = _get_curthread(); 522 int ret; 523 524 _thr_cancel_enter(curthread); 525 ret = __usleep(useconds); 526 _thr_cancel_leave(curthread); 527 528 return (ret); 529 } 530 531 __weak_reference(___wait, wait); 532 533 pid_t 534 ___wait(int *istat) 535 { 536 struct pthread *curthread = _get_curthread(); 537 pid_t ret; 538 539 _thr_cancel_enter(curthread); 540 ret = __wait(istat); 541 _thr_cancel_leave(curthread); 542 543 return ret; 544 } 545 546 __weak_reference(__wait3, wait3); 547 548 pid_t 549 __wait3(int *status, int options, struct rusage *rusage) 550 { 551 struct pthread *curthread = _get_curthread(); 552 pid_t ret; 553 554 _thr_cancel_enter(curthread); 555 ret = _wait4(WAIT_ANY, status, options, rusage); 556 _thr_cancel_leave(curthread); 557 558 return (ret); 559 } 560 561 __weak_reference(__wait4, wait4); 562 563 pid_t 564 __wait4(pid_t pid, int *status, int options, struct rusage *rusage) 565 { 566 struct pthread *curthread = _get_curthread(); 567 pid_t ret; 568 569 _thr_cancel_enter(curthread); 570 ret = __sys_wait4(pid, status, options, rusage); 571 _thr_cancel_leave(curthread); 572 573 return ret; 574 } 575 576 __weak_reference(___waitpid, waitpid); 577 578 pid_t 579 ___waitpid(pid_t wpid, int *status, int options) 580 { 581 struct pthread *curthread = _get_curthread(); 582 pid_t ret; 583 584 _thr_cancel_enter(curthread); 585 ret = __waitpid(wpid, status, options); 586 _thr_cancel_leave(curthread); 587 588 return ret; 589 } 590 591 __weak_reference(__write, write); 592 593 ssize_t 594 __write(int fd, const void *buf, size_t nbytes) 595 { 596 struct pthread *curthread = _get_curthread(); 597 ssize_t ret; 598 599 _thr_cancel_enter(curthread); 600 ret = __sys_write(fd, buf, nbytes); 601 _thr_cancel_leave(curthread); 602 603 return ret; 604 } 605 606 __weak_reference(__writev, writev); 607 608 ssize_t 609 __writev(int fd, const struct iovec *iov, int iovcnt) 610 { 611 struct pthread *curthread = _get_curthread(); 612 ssize_t ret; 613 614 _thr_cancel_enter(curthread); 615 ret = __sys_writev(fd, iov, iovcnt); 616 _thr_cancel_leave(curthread); 617 618 return ret; 619 } 620