Skip to content

Commit

Permalink
lib: istream-file/unix - don't ignore EINTR for blocking istream reads
Browse files Browse the repository at this point in the history
Just fail the istream read entirely. Although there's a small possibility
that this interrupt was unwanted and should be retried, it's more likely
that a blocking istream is hanging and admin wants to stop the process.
If the EINTR is ignored all the time, it's not possible to abort a
blocking read with ^C or anything else than SIGKILL.
  • Loading branch information
sirainen authored and Timo Sirainen committed Oct 5, 2017
1 parent 874f31b commit 03915cf
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 24 deletions.
30 changes: 14 additions & 16 deletions src/lib/istream-file.c
Expand Up @@ -58,20 +58,18 @@ ssize_t i_stream_file_read(struct istream_private *stream)
}

offset = stream->istream.v_offset + (stream->pos - stream->skip);
do {
if (fstream->file) {
ret = pread(stream->fd, stream->w_buffer + stream->pos,
size, offset);
} else if (fstream->seen_eof) {
/* don't try to read() again. EOF from keyboard (^D)
requires this to work right. */
ret = 0;
} else {
ret = read(stream->fd, stream->w_buffer + stream->pos,
size);
}
} while (unlikely(ret < 0 && errno == EINTR &&
stream->istream.blocking));

if (fstream->file) {
ret = pread(stream->fd, stream->w_buffer + stream->pos,
size, offset);
} else if (fstream->seen_eof) {
/* don't try to read() again. EOF from keyboard (^D)
requires this to work right. */
ret = 0;
} else {
ret = read(stream->fd, stream->w_buffer + stream->pos,
size);
}

if (ret == 0) {
/* EOF */
Expand All @@ -81,8 +79,8 @@ ssize_t i_stream_file_read(struct istream_private *stream)
}

if (unlikely(ret < 0)) {
if (errno == EINTR || errno == EAGAIN) {
i_assert(!stream->istream.blocking);
if ((errno == EINTR || errno == EAGAIN) &&
!stream->istream.blocking) {
ret = 0;
} else {
i_assert(errno != 0);
Expand Down
12 changes: 4 additions & 8 deletions src/lib/istream-unix.c
Expand Up @@ -34,12 +34,8 @@ static ssize_t i_stream_unix_read(struct istream_private *stream)
if (!i_stream_try_alloc(stream, 1, &size))
return -2;

do {
ret = fd_read(stream->fd,
stream->w_buffer + stream->pos, size,
&ustream->read_fd);
} while (unlikely(ret < 0 && errno == EINTR &&
stream->istream.blocking));
ret = fd_read(stream->fd, stream->w_buffer + stream->pos, size,
&ustream->read_fd);
if (ustream->read_fd != -1)
ustream->next_read_fd = FALSE;

Expand All @@ -51,8 +47,8 @@ static ssize_t i_stream_unix_read(struct istream_private *stream)
}

if (unlikely(ret < 0)) {
if (errno == EINTR || errno == EAGAIN) {
i_assert(!stream->istream.blocking);
if ((errno == EINTR || errno == EAGAIN) &&
!stream->istream.blocking) {
return 0;
} else {
i_assert(errno != 0);
Expand Down

0 comments on commit 03915cf

Please sign in to comment.