thr_syscalls.c revision 112918
1112918Sjeff/* 2112918Sjeff * Copyright (c) 2000 Jason Evans <jasone@freebsd.org>. 3112918Sjeff * Copyright (c) 2002 Daniel M. Eischen <deischen@freebsd.org> 4112918Sjeff * Copyright (c) 2003 Jeff Roberson <jeff@freebsd.org> 5112918Sjeff * All rights reserved. 6112918Sjeff * 7112918Sjeff * Redistribution and use in source and binary forms, with or without 8112918Sjeff * modification, are permitted provided that the following conditions 9112918Sjeff * are met: 10112918Sjeff * 1. Redistributions of source code must retain the above copyright 11112918Sjeff * notice(s), this list of conditions and the following disclaimer as 12112918Sjeff * the first lines of this file unmodified other than the possible 13112918Sjeff * addition of one or more copyright notices. 14112918Sjeff * 2. Redistributions in binary form must reproduce the above copyright 15112918Sjeff * notice(s), this list of conditions and the following disclaimer in 16112918Sjeff * the documentation and/or other materials provided with the 17112918Sjeff * distribution. 18112918Sjeff * 19112918Sjeff * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY 20112918Sjeff * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21112918Sjeff * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22112918Sjeff * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE 23112918Sjeff * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24112918Sjeff * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25112918Sjeff * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 26112918Sjeff * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 27112918Sjeff * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 28112918Sjeff * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 29112918Sjeff * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30112918Sjeff * 31112918Sjeff * $FreeBSD: head/lib/libthr/thread/thr_syscalls.c 112918 2003-04-01 03:46:29Z jeff $ 32112918Sjeff */ 33112918Sjeff 34112918Sjeff/* 35112918Sjeff * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au> 36112918Sjeff * All rights reserved. 37112918Sjeff * 38112918Sjeff * Redistribution and use in source and binary forms, with or without 39112918Sjeff * modification, are permitted provided that the following conditions 40112918Sjeff * are met: 41112918Sjeff * 1. Redistributions of source code must retain the above copyright 42112918Sjeff * notice, this list of conditions and the following disclaimer. 43112918Sjeff * 2. Redistributions in binary form must reproduce the above copyright 44112918Sjeff * notice, this list of conditions and the following disclaimer in the 45112918Sjeff * documentation and/or other materials provided with the distribution. 46112918Sjeff * 3. All advertising materials mentioning features or use of this software 47112918Sjeff * must display the following acknowledgement: 48112918Sjeff * This product includes software developed by John Birrell. 49112918Sjeff * 4. Neither the name of the author nor the names of any co-contributors 50112918Sjeff * may be used to endorse or promote products derived from this software 51112918Sjeff * without specific prior written permission. 52112918Sjeff * 53112918Sjeff * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND 54112918Sjeff * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 55112918Sjeff * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 56112918Sjeff * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 57112918Sjeff * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 58112918Sjeff * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 59112918Sjeff * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 60112918Sjeff * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 61112918Sjeff * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 62112918Sjeff * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 63112918Sjeff * SUCH DAMAGE. 64112918Sjeff * 65112918Sjeff */ 66112918Sjeff 67112918Sjeff#include <sys/cdefs.h> 68112918Sjeff#include <sys/fcntl.h> 69112918Sjeff#include <sys/mman.h> 70112918Sjeff#include <sys/param.h> 71112918Sjeff#include <sys/select.h> 72112918Sjeff#include <sys/time.h> 73112918Sjeff#include <sys/types.h> 74112918Sjeff#include <sys/uio.h> 75112918Sjeff#include <sys/wait.h> 76112918Sjeff 77112918Sjeff#include <aio.h> 78112918Sjeff#include <dirent.h> 79112918Sjeff#include <errno.h> 80112918Sjeff#include <fcntl.h> 81112918Sjeff#include <poll.h> 82112918Sjeff#include <pthread.h> 83112918Sjeff#include <signal.h> 84112918Sjeff#include <stdarg.h> 85112918Sjeff#include <stdio.h> 86112918Sjeff#include <stdlib.h> 87112918Sjeff#include <string.h> 88112918Sjeff#include <termios.h> 89112918Sjeff#include <unistd.h> 90112918Sjeff 91112918Sjeff#include "thr_private.h" 92112918Sjeff 93112918Sjeff__weak_reference(_aio_suspend, aio_suspend); 94112918Sjeff 95112918Sjeffint 96112918Sjeff_aio_suspend(const struct aiocb * const iocbs[], int niocb, const struct 97112918Sjeff timespec *timeout) 98112918Sjeff{ 99112918Sjeff int ret; 100112918Sjeff 101112918Sjeff _thread_enter_cancellation_point(); 102112918Sjeff ret = __sys_aio_suspend(iocbs, niocb, timeout); 103112918Sjeff _thread_leave_cancellation_point(); 104112918Sjeff 105112918Sjeff return ret; 106112918Sjeff} 107112918Sjeff 108112918Sjeff__weak_reference(__close, close); 109112918Sjeff 110112918Sjeffint 111112918Sjeff__close(int fd) 112112918Sjeff{ 113112918Sjeff int ret; 114112918Sjeff 115112918Sjeff _thread_enter_cancellation_point(); 116112918Sjeff ret = __sys_close(fd); 117112918Sjeff _thread_leave_cancellation_point(); 118112918Sjeff 119112918Sjeff return ret; 120112918Sjeff} 121112918Sjeff__weak_reference(___creat, creat); 122112918Sjeff 123112918Sjeffint 124112918Sjeff___creat(const char *path, mode_t mode) 125112918Sjeff{ 126112918Sjeff int ret; 127112918Sjeff 128112918Sjeff _thread_enter_cancellation_point(); 129112918Sjeff ret = __creat(path, mode); 130112918Sjeff _thread_leave_cancellation_point(); 131112918Sjeff 132112918Sjeff return ret; 133112918Sjeff} 134112918Sjeff 135112918Sjeff__weak_reference(__fcntl, fcntl); 136112918Sjeff 137112918Sjeffint 138112918Sjeff__fcntl(int fd, int cmd,...) 139112918Sjeff{ 140112918Sjeff int ret; 141112918Sjeff va_list ap; 142112918Sjeff 143112918Sjeff _thread_enter_cancellation_point(); 144112918Sjeff 145112918Sjeff va_start(ap, cmd); 146112918Sjeff switch (cmd) { 147112918Sjeff case F_DUPFD: 148112918Sjeff case F_SETFD: 149112918Sjeff case F_SETFL: 150112918Sjeff ret = __sys_fcntl(fd, cmd, va_arg(ap, int)); 151112918Sjeff break; 152112918Sjeff case F_GETFD: 153112918Sjeff case F_GETFL: 154112918Sjeff ret = __sys_fcntl(fd, cmd); 155112918Sjeff break; 156112918Sjeff default: 157112918Sjeff ret = __sys_fcntl(fd, cmd, va_arg(ap, void *)); 158112918Sjeff } 159112918Sjeff va_end(ap); 160112918Sjeff 161112918Sjeff _thread_leave_cancellation_point(); 162112918Sjeff 163112918Sjeff return ret; 164112918Sjeff} 165112918Sjeff 166112918Sjeff__weak_reference(__fsync, fsync); 167112918Sjeff 168112918Sjeffint 169112918Sjeff__fsync(int fd) 170112918Sjeff{ 171112918Sjeff int ret; 172112918Sjeff 173112918Sjeff _thread_enter_cancellation_point(); 174112918Sjeff ret = __sys_fsync(fd); 175112918Sjeff _thread_leave_cancellation_point(); 176112918Sjeff 177112918Sjeff return ret; 178112918Sjeff} 179112918Sjeff 180112918Sjeff__weak_reference(__msync, msync); 181112918Sjeff 182112918Sjeffint 183112918Sjeff__msync(void *addr, size_t len, int flags) 184112918Sjeff{ 185112918Sjeff int ret; 186112918Sjeff 187112918Sjeff _thread_enter_cancellation_point(); 188112918Sjeff ret = __sys_msync(addr, len, flags); 189112918Sjeff _thread_leave_cancellation_point(); 190112918Sjeff 191112918Sjeff return ret; 192112918Sjeff} 193112918Sjeff 194112918Sjeff__weak_reference(_nanosleep, nanosleep); 195112918Sjeff 196112918Sjeffint 197112918Sjeff_nanosleep(const struct timespec * time_to_sleep, struct timespec * 198112918Sjeff time_remaining) 199112918Sjeff{ 200112918Sjeff int ret; 201112918Sjeff 202112918Sjeff _thread_enter_cancellation_point(); 203112918Sjeff ret = __sys_nanosleep(time_to_sleep, time_remaining); 204112918Sjeff _thread_leave_cancellation_point(); 205112918Sjeff 206112918Sjeff return ret; 207112918Sjeff} 208112918Sjeff 209112918Sjeff__weak_reference(__open, open); 210112918Sjeff 211112918Sjeffint 212112918Sjeff__open(const char *path, int flags,...) 213112918Sjeff{ 214112918Sjeff int ret; 215112918Sjeff int mode = 0; 216112918Sjeff va_list ap; 217112918Sjeff 218112918Sjeff _thread_enter_cancellation_point(); 219112918Sjeff 220112918Sjeff /* Check if the file is being created: */ 221112918Sjeff if (flags & O_CREAT) { 222112918Sjeff /* Get the creation mode: */ 223112918Sjeff va_start(ap, flags); 224112918Sjeff mode = va_arg(ap, int); 225112918Sjeff va_end(ap); 226112918Sjeff } 227112918Sjeff 228112918Sjeff ret = __sys_open(path, flags, mode); 229112918Sjeff _thread_leave_cancellation_point(); 230112918Sjeff 231112918Sjeff return ret; 232112918Sjeff} 233112918Sjeff 234112918Sjeff__weak_reference(__poll, poll); 235112918Sjeff 236112918Sjeffint 237112918Sjeff__poll(struct pollfd *fds, unsigned int nfds, int timeout) 238112918Sjeff{ 239112918Sjeff int ret; 240112918Sjeff 241112918Sjeff _thread_enter_cancellation_point(); 242112918Sjeff ret = __sys_poll(fds, nfds, timeout); 243112918Sjeff _thread_leave_cancellation_point(); 244112918Sjeff 245112918Sjeff return ret; 246112918Sjeff} 247112918Sjeff 248112918Sjeffextern int __pselect(int count, fd_set *rfds, fd_set *wfds, fd_set *efds, 249112918Sjeff const struct timespec *timo, const sigset_t *mask); 250112918Sjeff 251112918Sjeffint 252112918Sjeffpselect(int count, fd_set *rfds, fd_set *wfds, fd_set *efds, 253112918Sjeff const struct timespec *timo, const sigset_t *mask) 254112918Sjeff{ 255112918Sjeff int ret; 256112918Sjeff 257112918Sjeff _thread_enter_cancellation_point(); 258112918Sjeff ret = __pselect(count, rfds, wfds, efds, timo, mask); 259112918Sjeff _thread_leave_cancellation_point(); 260112918Sjeff 261112918Sjeff return (ret); 262112918Sjeff} 263112918Sjeff 264112918Sjeff__weak_reference(__read, read); 265112918Sjeff 266112918Sjeffssize_t 267112918Sjeff__read(int fd, void *buf, size_t nbytes) 268112918Sjeff{ 269112918Sjeff ssize_t ret; 270112918Sjeff 271112918Sjeff _thread_enter_cancellation_point(); 272112918Sjeff ret = __sys_read(fd, buf, nbytes); 273112918Sjeff _thread_leave_cancellation_point(); 274112918Sjeff 275112918Sjeff return ret; 276112918Sjeff} 277112918Sjeff 278112918Sjeff__weak_reference(__readv, readv); 279112918Sjeff 280112918Sjeffssize_t 281112918Sjeff__readv(int fd, const struct iovec *iov, int iovcnt) 282112918Sjeff{ 283112918Sjeff ssize_t ret; 284112918Sjeff 285112918Sjeff _thread_enter_cancellation_point(); 286112918Sjeff ret = __sys_readv(fd, iov, iovcnt); 287112918Sjeff _thread_leave_cancellation_point(); 288112918Sjeff 289112918Sjeff return ret; 290112918Sjeff} 291112918Sjeff 292112918Sjeff__weak_reference(__select, select); 293112918Sjeff 294112918Sjeffint 295112918Sjeff__select(int numfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, 296112918Sjeff struct timeval *timeout) 297112918Sjeff{ 298112918Sjeff int ret; 299112918Sjeff 300112918Sjeff _thread_enter_cancellation_point(); 301112918Sjeff ret = __sys_select(numfds, readfds, writefds, exceptfds, timeout); 302112918Sjeff _thread_leave_cancellation_point(); 303112918Sjeff 304112918Sjeff return ret; 305112918Sjeff} 306112918Sjeff 307112918Sjeff__weak_reference(_sleep, sleep); 308112918Sjeff 309112918Sjeffunsigned int 310112918Sjeff_sleep(unsigned int seconds) 311112918Sjeff{ 312112918Sjeff unsigned int ret; 313112918Sjeff 314112918Sjeff _thread_enter_cancellation_point(); 315112918Sjeff ret = __sleep(seconds); 316112918Sjeff _thread_leave_cancellation_point(); 317112918Sjeff 318112918Sjeff return ret; 319112918Sjeff} 320112918Sjeff 321112918Sjeff__weak_reference(_system, system); 322112918Sjeff 323112918Sjeffint 324112918Sjeff_system(const char *string) 325112918Sjeff{ 326112918Sjeff int ret; 327112918Sjeff 328112918Sjeff _thread_enter_cancellation_point(); 329112918Sjeff ret = __system(string); 330112918Sjeff _thread_leave_cancellation_point(); 331112918Sjeff 332112918Sjeff return ret; 333112918Sjeff} 334112918Sjeff 335112918Sjeff 336112918Sjeff__weak_reference(_tcdrain, tcdrain); 337112918Sjeff 338112918Sjeffint 339112918Sjeff_tcdrain(int fd) 340112918Sjeff{ 341112918Sjeff int ret; 342112918Sjeff 343112918Sjeff _thread_enter_cancellation_point(); 344112918Sjeff ret = __tcdrain(fd); 345112918Sjeff _thread_leave_cancellation_point(); 346112918Sjeff 347112918Sjeff return ret; 348112918Sjeff} 349112918Sjeff 350112918Sjeff__weak_reference(_wait, wait); 351112918Sjeff 352112918Sjeffpid_t 353112918Sjeff_wait(int *istat) 354112918Sjeff{ 355112918Sjeff pid_t ret; 356112918Sjeff 357112918Sjeff _thread_enter_cancellation_point(); 358112918Sjeff ret = __wait(istat); 359112918Sjeff _thread_leave_cancellation_point(); 360112918Sjeff 361112918Sjeff return ret; 362112918Sjeff} 363112918Sjeff 364112918Sjeff__weak_reference(__wait4, wait4); 365112918Sjeff 366112918Sjeffpid_t 367112918Sjeff__wait4(pid_t pid, int *istat, int options, struct rusage *rusage) 368112918Sjeff{ 369112918Sjeff pid_t ret; 370112918Sjeff 371112918Sjeff _thread_enter_cancellation_point(); 372112918Sjeff ret = _wait4(pid, istat, options, rusage); 373112918Sjeff _thread_leave_cancellation_point(); 374112918Sjeff 375112918Sjeff return ret; 376112918Sjeff} 377112918Sjeff 378112918Sjeff__weak_reference(_waitpid, waitpid); 379112918Sjeff 380112918Sjeffpid_t 381112918Sjeff_waitpid(pid_t wpid, int *status, int options) 382112918Sjeff{ 383112918Sjeff pid_t ret; 384112918Sjeff 385112918Sjeff _thread_enter_cancellation_point(); 386112918Sjeff ret = __waitpid(wpid, status, options); 387112918Sjeff _thread_leave_cancellation_point(); 388112918Sjeff 389112918Sjeff return ret; 390112918Sjeff} 391112918Sjeff 392112918Sjeff__weak_reference(__write, write); 393112918Sjeff 394112918Sjeffssize_t 395112918Sjeff__write(int fd, const void *buf, size_t nbytes) 396112918Sjeff{ 397112918Sjeff ssize_t ret; 398112918Sjeff 399112918Sjeff _thread_enter_cancellation_point(); 400112918Sjeff ret = __sys_write(fd, buf, nbytes); 401112918Sjeff _thread_leave_cancellation_point(); 402112918Sjeff 403112918Sjeff return ret; 404112918Sjeff} 405112918Sjeff 406112918Sjeff__weak_reference(__writev, writev); 407112918Sjeff 408112918Sjeffssize_t 409112918Sjeff__writev(int fd, const struct iovec *iov, int iovcnt) 410112918Sjeff{ 411112918Sjeff ssize_t ret; 412112918Sjeff 413112918Sjeff _thread_enter_cancellation_point(); 414112918Sjeff ret = __sys_writev(fd, iov, iovcnt); 415112918Sjeff _thread_leave_cancellation_point(); 416112918Sjeff 417112918Sjeff return ret; 418112918Sjeff} 419