thr_read.c revision 49439
1193323Sed/*
2193323Sed * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au>
3193323Sed * All rights reserved.
4193323Sed *
5193323Sed * Redistribution and use in source and binary forms, with or without
6193323Sed * modification, are permitted provided that the following conditions
7193323Sed * are met:
8193323Sed * 1. Redistributions of source code must retain the above copyright
9193323Sed *    notice, this list of conditions and the following disclaimer.
10193323Sed * 2. Redistributions in binary form must reproduce the above copyright
11193323Sed *    notice, this list of conditions and the following disclaimer in the
12195340Sed *    documentation and/or other materials provided with the distribution.
13193323Sed * 3. All advertising materials mentioning features or use of this software
14193323Sed *    must display the following acknowledgement:
15193323Sed *	This product includes software developed by John Birrell.
16193323Sed * 4. Neither the name of the author nor the names of any co-contributors
17193323Sed *    may be used to endorse or promote products derived from this software
18193323Sed *    without specific prior written permission.
19193323Sed *
20193323Sed * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
21193323Sed * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22193323Sed * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23193323Sed * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24193323Sed * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25193323Sed * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26195340Sed * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27195340Sed * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28193323Sed * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29193323Sed * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30193323Sed * SUCH DAMAGE.
31193323Sed *
32193323Sed * $Id: uthread_read.c,v 1.6 1998/06/10 22:28:43 jb Exp $
33193323Sed *
34193323Sed */
35193323Sed#include <sys/types.h>
36193323Sed#include <sys/fcntl.h>
37195340Sed#include <sys/uio.h>
38195340Sed#include <errno.h>
39195340Sed#include <unistd.h>
40195340Sed#ifdef _THREAD_SAFE
41195340Sed#include <pthread.h>
42195340Sed#include "pthread_private.h"
43195340Sed
44195340Sedssize_t
45195340Sedread(int fd, void *buf, size_t nbytes)
46195340Sed{
47195340Sed	int	ret;
48195340Sed	int	type;
49195340Sed
50195340Sed	/* POSIX says to do just this: */
51195340Sed	if (nbytes == 0)
52195340Sed		return (0);
53193323Sed
54193323Sed	/* Lock the file descriptor for read: */
55193323Sed	if ((ret = _FD_LOCK(fd, FD_READ, NULL)) == 0) {
56193323Sed		/* Get the read/write mode type: */
57193323Sed		type = _thread_fd_table[fd]->flags & O_ACCMODE;
58195340Sed
59193323Sed		/* Check if the file is not open for read: */
60195340Sed		if (type != O_RDONLY && type != O_RDWR) {
61195340Sed			/* File is not open for read: */
62195340Sed			errno = EBADF;
63195340Sed			_FD_UNLOCK(fd, FD_READ);
64195340Sed			return (-1);
65195340Sed		}
66195340Sed
67195340Sed		/* Perform a non-blocking read syscall: */
68195340Sed		while ((ret = _thread_sys_read(fd, buf, nbytes)) < 0) {
69195340Sed			if ((_thread_fd_table[fd]->flags & O_NONBLOCK) == 0 &&
70195340Sed			    (errno == EWOULDBLOCK || errno == EAGAIN)) {
71195340Sed				_thread_run->data.fd.fd = fd;
72195340Sed				_thread_kern_set_timeout(NULL);
73195340Sed
74195340Sed				/* Reset the interrupted operation flag: */
75195340Sed				_thread_run->interrupted = 0;
76195340Sed
77195340Sed				_thread_kern_sched_state(PS_FDR_WAIT,
78193323Sed				    __FILE__, __LINE__);
79195340Sed
80195340Sed				/*
81193323Sed				 * Check if the operation was
82193323Sed				 * interrupted by a signal
83193323Sed				 */
84193323Sed				if (_thread_run->interrupted) {
85193323Sed					errno = EINTR;
86193323Sed					ret = -1;
87193323Sed					break;
88193323Sed				}
89			} else {
90				break;
91			}
92		}
93		_FD_UNLOCK(fd, FD_READ);
94	}
95	return (ret);
96}
97#endif
98