thr_read.c revision 75369
113546Sjulian/* 235509Sjb * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au> 313546Sjulian * All rights reserved. 413546Sjulian * 513546Sjulian * Redistribution and use in source and binary forms, with or without 613546Sjulian * modification, are permitted provided that the following conditions 713546Sjulian * are met: 813546Sjulian * 1. Redistributions of source code must retain the above copyright 913546Sjulian * notice, this list of conditions and the following disclaimer. 1013546Sjulian * 2. Redistributions in binary form must reproduce the above copyright 1113546Sjulian * notice, this list of conditions and the following disclaimer in the 1213546Sjulian * documentation and/or other materials provided with the distribution. 1313546Sjulian * 3. All advertising materials mentioning features or use of this software 1413546Sjulian * must display the following acknowledgement: 1513546Sjulian * This product includes software developed by John Birrell. 1613546Sjulian * 4. Neither the name of the author nor the names of any co-contributors 1713546Sjulian * may be used to endorse or promote products derived from this software 1813546Sjulian * without specific prior written permission. 1913546Sjulian * 2013546Sjulian * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND 2113546Sjulian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2213546Sjulian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2349439Sdeischen * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 2413546Sjulian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2513546Sjulian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2613546Sjulian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2713546Sjulian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2813546Sjulian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2913546Sjulian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3013546Sjulian * SUCH DAMAGE. 3113546Sjulian * 3250476Speter * $FreeBSD: head/lib/libkse/thread/thr_read.c 75369 2001-04-10 04:19:21Z deischen $ 3324518Sjb * 3413546Sjulian */ 3513546Sjulian#include <sys/types.h> 3613546Sjulian#include <sys/fcntl.h> 3713546Sjulian#include <sys/uio.h> 3813546Sjulian#include <errno.h> 3913546Sjulian#include <unistd.h> 4013546Sjulian#include <pthread.h> 4113546Sjulian#include "pthread_private.h" 4213546Sjulian 4375369Sdeischen__weak_reference(__read, read); 4471581Sdeischen 4513546Sjulianssize_t 4656698Sjasone_read(int fd, void *buf, size_t nbytes) 4713546Sjulian{ 4871581Sdeischen struct pthread *curthread = _get_curthread(); 4922315Sjulian int ret; 5036877Sjb int type; 5122315Sjulian 5236830Sjb /* POSIX says to do just this: */ 5353812Salfred if (nbytes == 0) { 5436830Sjb return (0); 5553812Salfred } 5636830Sjb 5722315Sjulian /* Lock the file descriptor for read: */ 5836830Sjb if ((ret = _FD_LOCK(fd, FD_READ, NULL)) == 0) { 5936877Sjb /* Get the read/write mode type: */ 6036877Sjb type = _thread_fd_table[fd]->flags & O_ACCMODE; 6136877Sjb 6236877Sjb /* Check if the file is not open for read: */ 6336877Sjb if (type != O_RDONLY && type != O_RDWR) { 6436877Sjb /* File is not open for read: */ 6536877Sjb errno = EBADF; 6636877Sjb _FD_UNLOCK(fd, FD_READ); 6736877Sjb return (-1); 6836877Sjb } 6936877Sjb 7022315Sjulian /* Perform a non-blocking read syscall: */ 7171581Sdeischen while ((ret = __sys_read(fd, buf, nbytes)) < 0) { 7224518Sjb if ((_thread_fd_table[fd]->flags & O_NONBLOCK) == 0 && 7322315Sjulian (errno == EWOULDBLOCK || errno == EAGAIN)) { 7471581Sdeischen curthread->data.fd.fd = fd; 7513546Sjulian _thread_kern_set_timeout(NULL); 7622315Sjulian 7722315Sjulian /* Reset the interrupted operation flag: */ 7871581Sdeischen curthread->interrupted = 0; 7922315Sjulian 8022315Sjulian _thread_kern_sched_state(PS_FDR_WAIT, 8122315Sjulian __FILE__, __LINE__); 8222315Sjulian 8322315Sjulian /* 8422315Sjulian * Check if the operation was 8522315Sjulian * interrupted by a signal 8622315Sjulian */ 8771581Sdeischen if (curthread->interrupted) { 8835509Sjb errno = EINTR; 8913546Sjulian ret = -1; 9013546Sjulian break; 9113546Sjulian } 9213546Sjulian } else { 9313546Sjulian break; 9413546Sjulian } 9513546Sjulian } 9636830Sjb _FD_UNLOCK(fd, FD_READ); 9713546Sjulian } 9813546Sjulian return (ret); 9913546Sjulian} 10055838Sjasone 10156698Sjasonessize_t 10271581Sdeischen__read(int fd, void *buf, size_t nbytes) 10356698Sjasone{ 10456698Sjasone ssize_t ret; 10556698Sjasone 10656698Sjasone _thread_enter_cancellation_point(); 10756698Sjasone ret = _read(fd, buf, nbytes); 10856698Sjasone _thread_leave_cancellation_point(); 10956698Sjasone 11056698Sjasone return ret; 11156698Sjasone} 112