Skip to content

Commit 1079c4b

Browse files
stefanbellergitster
authored andcommitted
xread: poll on non blocking fds
The man page of read(2) says: EAGAIN The file descriptor fd refers to a file other than a socket and has been marked nonblocking (O_NONBLOCK), and the read would block. EAGAIN or EWOULDBLOCK The file descriptor fd refers to a socket and has been marked nonblocking (O_NONBLOCK), and the read would block. POSIX.1-2001 allows either error to be returned for this case, and does not require these constants to have the same value, so a portable application should check for both possibilities. If we get an EAGAIN or EWOULDBLOCK the fd must have set O_NONBLOCK. As the intent of xread is to read as much as possible either until the fd is EOF or an actual error occurs, we can ease the feeder of the fd by not spinning the whole time, but rather wait for it politely by not busy waiting. We should not care if the call to poll failed, as we're in an infinite loop and can only get out with the correct read(). Signed-off-by: Stefan Beller <[email protected]> Acked-by: Johannes Sixt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent fbf7164 commit 1079c4b

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

wrapper.c

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,24 @@ ssize_t xread(int fd, void *buf, size_t len)
236236
len = MAX_IO_SIZE;
237237
while (1) {
238238
nr = read(fd, buf, len);
239-
if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
240-
continue;
239+
if (nr < 0) {
240+
if (errno == EINTR)
241+
continue;
242+
if (errno == EAGAIN || errno == EWOULDBLOCK) {
243+
struct pollfd pfd;
244+
pfd.events = POLLIN;
245+
pfd.fd = fd;
246+
/*
247+
* it is OK if this poll() failed; we
248+
* want to leave this infinite loop
249+
* only when read() returns with
250+
* success, or an expected failure,
251+
* which would be checked by the next
252+
* call to read(2).
253+
*/
254+
poll(&pfd, 1, -1);
255+
}
256+
}
241257
return nr;
242258
}
243259
}

0 commit comments

Comments
 (0)