From fc728e92c19e5d7cf0853165950afa72b429fba0 Mon Sep 17 00:00:00 2001 From: Michal Bloch Date: Tue, 31 Mar 2020 19:37:22 +0200 Subject: [PATCH] libdlogutil: split off buffer replenishment Change-Id: Ic4cefeb97d836eaf41eb355c1907f963018e5bfc Signed-off-by: Michal Bloch --- src/libdlogutil/logretrieve.c | 39 +++++++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/src/libdlogutil/logretrieve.c b/src/libdlogutil/logretrieve.c index 7a89395..b82c7c8 100644 --- a/src/libdlogutil/logretrieve.c +++ b/src/libdlogutil/logretrieve.c @@ -286,6 +286,22 @@ static void remove_drained_buffers(dlogutil_state_s *state) } } +static int refill_fdi_buffers(dlogutil_state_s *state, int timeout) +{ + int const nfds = epoll_wait(state->epollfd, state->evs, state->epoll_cnt, timeout); + if (nfds < 0) + return -errno; + + for (int i = 0; i < nfds; ++i) { + struct fd_info *const fdi = state->evs[i].data.ptr; + int const rd = fdi->ops->read(fdi); + if (rd < 0 && rd != -EAGAIN) + return rd; + } + + return 0; +} + /** * @brief Handle input * @details The main loop reading log data @@ -306,27 +322,18 @@ int do_print_once(dlogutil_state_s *state, int timeout, dlogutil_entry_s **out) if (r < 0) return r; + int refill_err = 0; if (r == BUFFER_NEWLY_DRAINED - || r == ALL_BUFFERS_EMPTY) { - int const nfds = epoll_wait(state->epollfd, state->evs, state->epoll_cnt, timeout); - if (nfds < 0) - return -errno; - - for (int i = 0; i < nfds; ++i) { - struct fd_info *const fdi = state->evs[i].data.ptr; - int const rd = fdi->ops->read(fdi); - if (rd < 0) { - if (rd == -EAGAIN) - continue; - else - break; - } - } - } + || r == ALL_BUFFERS_EMPTY) + refill_err = refill_fdi_buffers(state, timeout); if (*out) return 0; + // Don't do that immediately to prevent 'out' leaking + if (refill_err) + return refill_err; + /* The oldest log can be so fresh as to be from the future * (i.e. has a negative age). This can happen when somebody * changes the realtime clock backwards. In that case let -- 2.7.4