thr_read.c revision 103388
121308Sache/* 221308Sache * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au> 321308Sache * All rights reserved. 421308Sache * 521308Sache * Redistribution and use in source and binary forms, with or without 621308Sache * modification, are permitted provided that the following conditions 721308Sache * are met: 821308Sache * 1. Redistributions of source code must retain the above copyright 921308Sache * notice, this list of conditions and the following disclaimer. 1058310Sache * 2. Redistributions in binary form must reproduce the above copyright 1121308Sache * notice, this list of conditions and the following disclaimer in the 1221308Sache * documentation and/or other materials provided with the distribution. 1321308Sache * 3. All advertising materials mentioning features or use of this software 1421308Sache * must display the following acknowledgement: 1521308Sache * This product includes software developed by John Birrell. 1621308Sache * 4. Neither the name of the author nor the names of any co-contributors 1721308Sache * may be used to endorse or promote products derived from this software 1821308Sache * without specific prior written permission. 1921308Sache * 2058310Sache * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND 2121308Sache * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2221308Sache * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2321308Sache * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 2421308Sache * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2521308Sache * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2621308Sache * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2721308Sache * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2821308Sache * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2921308Sache * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3021308Sache * SUCH DAMAGE. 3121308Sache * 3221308Sache * $FreeBSD: head/lib/libkse/thread/thr_read.c 103388 2002-09-16 08:45:36Z mini $ 3358310Sache * 3458310Sache */ 3558310Sache#include <sys/types.h> 3621308Sache#include <sys/fcntl.h> 3758310Sache#include <sys/uio.h> 3821308Sache#include <errno.h> 3921308Sache#include <unistd.h> 4021308Sache#include <pthread.h> 4121308Sache#include "thr_private.h" 4221308Sache 4321308Sache__weak_reference(__read, read); 4458310Sache 4521308Sachessize_t 4621308Sache_read(int fd, void *buf, size_t nbytes) 4721308Sache{ 4821308Sache struct pthread *curthread = _get_curthread(); 4921308Sache int ret; 5021308Sache int type; 5121308Sache 5221308Sache /* POSIX says to do just this: */ 5321308Sache if (nbytes == 0) { 5421308Sache return (0); 5521308Sache } 5621308Sache 5721308Sache /* Lock the file descriptor for read: */ 5821308Sache if ((ret = _FD_LOCK(fd, FD_READ, NULL)) == 0) { 5921308Sache /* Get the read/write mode type: */ 6021308Sache type = _thread_fd_getflags(fd) & O_ACCMODE; 6121308Sache 6221308Sache /* Check if the file is not open for read: */ 6321308Sache if (type != O_RDONLY && type != O_RDWR) { 6421308Sache /* File is not open for read: */ 6521308Sache errno = EBADF; 6621308Sache _FD_UNLOCK(fd, FD_READ); 6721308Sache return (-1); 6821308Sache } 6921308Sache 7021308Sache /* Perform a non-blocking read syscall: */ 7121308Sache while ((ret = __sys_read(fd, buf, nbytes)) < 0) { 7221308Sache if ((_thread_fd_getflags(fd) & O_NONBLOCK) == 0 && 7321308Sache (errno == EWOULDBLOCK || errno == EAGAIN)) { 7421308Sache curthread->data.fd.fd = fd; 7521308Sache _thread_kern_set_timeout(NULL); 7621308Sache 7721308Sache /* Reset the interrupted operation flag: */ 7821308Sache curthread->interrupted = 0; 7921308Sache 8021308Sache _thread_kern_sched_state(PS_FDR_WAIT, 8121308Sache __FILE__, __LINE__); 8221308Sache 8321308Sache /* 8421308Sache * Check if the operation was 8521308Sache * interrupted by a signal 8621308Sache */ 8721308Sache if (curthread->interrupted) { 8821308Sache errno = EINTR; 8921308Sache ret = -1; 9021308Sache break; 9121308Sache } 9221308Sache } else { 9321308Sache break; 9421308Sache } 9521308Sache } 9621308Sache _FD_UNLOCK(fd, FD_READ); 9721308Sache } 9821308Sache return (ret); 9921308Sache} 10021308Sache 10121308Sachessize_t 10221308Sache__read(int fd, void *buf, size_t nbytes) 10321308Sache{ 10421308Sache ssize_t ret; 10521308Sache 10621308Sache _thread_enter_cancellation_point(); 10721308Sache ret = _read(fd, buf, nbytes); 10821308Sache _thread_leave_cancellation_point(); 10921308Sache 11021308Sache return ret; 11121308Sache} 11221308Sache