Skip to content

Commit

Permalink
tools api: Add simple timeout to io read
Browse files Browse the repository at this point in the history
In situations like reading from a pipe it can be useful to have a
timeout so that the caller doesn't block indefinitely. Implement a
simple one based on poll.

Signed-off-by: Ian Rogers <irogers@google.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Yang Jihong <yangjihong1@huawei.com>
Link: http://lore.kernel.org/lkml/20230608061812.3715566-1-irogers@google.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
  • Loading branch information
Ian Rogers authored and Arnaldo Carvalho de Melo committed Jun 14, 2023
1 parent 99d4850 commit f4c0d53
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion tools/lib/api/io.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#define __API_IO__

#include <errno.h>
#include <poll.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
Expand All @@ -23,6 +24,8 @@ struct io {
char *end;
/* Currently accessed data pointer. */
char *data;
/* Read timeout, 0 implies no timeout. */
int timeout_ms;
/* Set true on when the end of file on read error. */
bool eof;
};
Expand All @@ -35,6 +38,7 @@ static inline void io__init(struct io *io, int fd,
io->buf = buf;
io->end = buf;
io->data = buf;
io->timeout_ms = 0;
io->eof = false;
}

Expand All @@ -47,7 +51,29 @@ static inline int io__get_char(struct io *io)
return -1;

if (ptr == io->end) {
ssize_t n = read(io->fd, io->buf, io->buf_len);
ssize_t n;

if (io->timeout_ms != 0) {
struct pollfd pfds[] = {
{
.fd = io->fd,
.events = POLLIN,
},
};

n = poll(pfds, 1, io->timeout_ms);
if (n == 0)
errno = ETIMEDOUT;
if (n > 0 && !(pfds[0].revents & POLLIN)) {
errno = EIO;
n = -1;
}
if (n <= 0) {
io->eof = true;
return -1;
}
}
n = read(io->fd, io->buf, io->buf_len);

if (n <= 0) {
io->eof = true;
Expand Down

0 comments on commit f4c0d53

Please sign in to comment.