1*433d6423SLionel Sambuc /* asyn_read() Author: Kees J. Bot
2*433d6423SLionel Sambuc * 7 Jul 1997
3*433d6423SLionel Sambuc */
4*433d6423SLionel Sambuc #include "asyn.h"
5*433d6423SLionel Sambuc #include <signal.h>
6*433d6423SLionel Sambuc
asyn_read(asynchio_t * asyn,int fd,void * buf,size_t len)7*433d6423SLionel Sambuc ssize_t asyn_read(asynchio_t *asyn, int fd, void *buf, size_t len)
8*433d6423SLionel Sambuc /* Asynchronous read(). Try if a read can be done, if not then set a flag
9*433d6423SLionel Sambuc * indicating that select(2) should look out for it. Returns like a normal
10*433d6423SLionel Sambuc * read or returns -1 with errno set to EAGAIN.
11*433d6423SLionel Sambuc */
12*433d6423SLionel Sambuc {
13*433d6423SLionel Sambuc asynfd_t *afd;
14*433d6423SLionel Sambuc
15*433d6423SLionel Sambuc /* Asyn_wait() may block if this counter equals zero indicating that
16*433d6423SLionel Sambuc * all of the asyn_* functions are "in progress".
17*433d6423SLionel Sambuc */
18*433d6423SLionel Sambuc asyn->asyn_more++;
19*433d6423SLionel Sambuc
20*433d6423SLionel Sambuc if ((unsigned) fd >= FD_SETSIZE) { errno= EBADF; return -1; }
21*433d6423SLionel Sambuc afd= &asyn->asyn_afd[fd];
22*433d6423SLionel Sambuc
23*433d6423SLionel Sambuc /* If this is the first async call on this filedescriptor then
24*433d6423SLionel Sambuc * remember its file flags.
25*433d6423SLionel Sambuc */
26*433d6423SLionel Sambuc if (!afd->afd_seen) {
27*433d6423SLionel Sambuc if ((afd->afd_flags= fcntl(fd, F_GETFL)) < 0) return -1;
28*433d6423SLionel Sambuc afd->afd_seen= 1;
29*433d6423SLionel Sambuc }
30*433d6423SLionel Sambuc
31*433d6423SLionel Sambuc /* Try to read if I/O is pending. */
32*433d6423SLionel Sambuc if (afd->afd_state[SEL_READ] == PENDING) {
33*433d6423SLionel Sambuc sigset_t mask;
34*433d6423SLionel Sambuc ssize_t result;
35*433d6423SLionel Sambuc int err;
36*433d6423SLionel Sambuc
37*433d6423SLionel Sambuc sigemptyset(&mask);
38*433d6423SLionel Sambuc if (sigprocmask(SIG_SETMASK, &mask, &mask) < 0) return -1;
39*433d6423SLionel Sambuc (void) fcntl(fd, F_SETFL, afd->afd_flags | O_NONBLOCK);
40*433d6423SLionel Sambuc
41*433d6423SLionel Sambuc /* Try the actual read. */
42*433d6423SLionel Sambuc result= read(fd, buf, len);
43*433d6423SLionel Sambuc err= errno;
44*433d6423SLionel Sambuc
45*433d6423SLionel Sambuc (void) fcntl(fd, F_SETFL, afd->afd_flags);
46*433d6423SLionel Sambuc (void) sigprocmask(SIG_SETMASK, &mask, nil);
47*433d6423SLionel Sambuc
48*433d6423SLionel Sambuc errno= err;
49*433d6423SLionel Sambuc if (result != -1 || errno != EAGAIN) {
50*433d6423SLionel Sambuc afd->afd_state[SEL_READ]= IDLE;
51*433d6423SLionel Sambuc return result;
52*433d6423SLionel Sambuc }
53*433d6423SLionel Sambuc }
54*433d6423SLionel Sambuc
55*433d6423SLionel Sambuc /* Record this read as "waiting". */
56*433d6423SLionel Sambuc afd->afd_state[SEL_READ]= WAITING;
57*433d6423SLionel Sambuc FD_SET(fd, &asyn->asyn_fdset[SEL_READ]);
58*433d6423SLionel Sambuc errno= EAGAIN;
59*433d6423SLionel Sambuc asyn->asyn_more--;
60*433d6423SLionel Sambuc return -1;
61*433d6423SLionel Sambuc }
62