treewide: Address cert-err33-c clang-tidy warnings for clock and timer functions

For clock_gettime(), we shouldn't ignore errors if they happen at
initialisation phase, because something is seriously wrong and it's
not helpful if we proceed as if nothing happened.

As we're up and running, though, it's probably better to report the
error and use a stale value than to terminate altogether. Make sure
we use a zero value if we don't have a stale one somewhere.

For timerfd_gettime() and timerfd_settime() failures, just report an
error, there isn't much else we can do.

Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
This commit is contained in:
Stefano Brivio 2024-10-25 00:29:50 +02:00
parent 59fe34ee36
commit 099ace64ce
3 changed files with 26 additions and 12 deletions

View file

@ -207,7 +207,8 @@ int main(int argc, char **argv)
struct timespec now; struct timespec now;
struct sigaction sa; struct sigaction sa;
clock_gettime(CLOCK_MONOTONIC, &log_start); if (clock_gettime(CLOCK_MONOTONIC, &log_start))
die_perror("Failed to get CLOCK_MONOTONIC time");
arch_avx2_exec(argv); arch_avx2_exec(argv);
@ -265,7 +266,8 @@ int main(int argc, char **argv)
secret_init(&c); secret_init(&c);
clock_gettime(CLOCK_MONOTONIC, &now); if (clock_gettime(CLOCK_MONOTONIC, &now))
die_perror("Failed to get CLOCK_MONOTONIC time");
flow_init(); flow_init();
@ -313,7 +315,8 @@ loop:
if (nfds == -1 && errno != EINTR) if (nfds == -1 && errno != EINTR)
die_perror("epoll_wait() failed in main loop"); die_perror("epoll_wait() failed in main loop");
clock_gettime(CLOCK_MONOTONIC, &now); if (clock_gettime(CLOCK_MONOTONIC, &now))
err_perror("Failed to get CLOCK_MONOTONIC time");
for (i = 0; i < nfds; i++) { for (i = 0; i < nfds; i++) {
union epoll_ref ref = *((union epoll_ref *)&events[i].data.u64); union epoll_ref ref = *((union epoll_ref *)&events[i].data.u64);

17
pcap.c
View file

@ -100,12 +100,14 @@ static void pcap_frame(const struct iovec *iov, size_t iovcnt,
void pcap(const char *pkt, size_t l2len) void pcap(const char *pkt, size_t l2len)
{ {
struct iovec iov = { (char *)pkt, l2len }; struct iovec iov = { (char *)pkt, l2len };
struct timespec now; struct timespec now = { 0 };
if (pcap_fd == -1) if (pcap_fd == -1)
return; return;
clock_gettime(CLOCK_REALTIME, &now); if (clock_gettime(CLOCK_REALTIME, &now))
err_perror("Failed to get CLOCK_REALTIME time");
pcap_frame(&iov, 1, 0, &now); pcap_frame(&iov, 1, 0, &now);
} }
@ -119,13 +121,14 @@ void pcap(const char *pkt, size_t l2len)
void pcap_multiple(const struct iovec *iov, size_t frame_parts, unsigned int n, void pcap_multiple(const struct iovec *iov, size_t frame_parts, unsigned int n,
size_t offset) size_t offset)
{ {
struct timespec now; struct timespec now = { 0 };
unsigned int i; unsigned int i;
if (pcap_fd == -1) if (pcap_fd == -1)
return; return;
clock_gettime(CLOCK_REALTIME, &now); if (clock_gettime(CLOCK_REALTIME, &now))
err_perror("Failed to get CLOCK_REALTIME time");
for (i = 0; i < n; i++) for (i = 0; i < n; i++)
pcap_frame(iov + i * frame_parts, frame_parts, offset, &now); pcap_frame(iov + i * frame_parts, frame_parts, offset, &now);
@ -143,12 +146,14 @@ void pcap_multiple(const struct iovec *iov, size_t frame_parts, unsigned int n,
/* cppcheck-suppress unusedFunction */ /* cppcheck-suppress unusedFunction */
void pcap_iov(const struct iovec *iov, size_t iovcnt, size_t offset) void pcap_iov(const struct iovec *iov, size_t iovcnt, size_t offset)
{ {
struct timespec now; struct timespec now = { 0 };
if (pcap_fd == -1) if (pcap_fd == -1)
return; return;
clock_gettime(CLOCK_REALTIME, &now); if (clock_gettime(CLOCK_REALTIME, &now))
err_perror("Failed to get CLOCK_REALTIME time");
pcap_frame(iov, iovcnt, offset, &now); pcap_frame(iov, iovcnt, offset, &now);
} }

12
tcp.c
View file

@ -549,7 +549,8 @@ static void tcp_timer_ctl(const struct ctx *c, struct tcp_tap_conn *conn)
(unsigned long long)it.it_value.tv_sec, (unsigned long long)it.it_value.tv_sec,
(unsigned long long)it.it_value.tv_nsec / 1000 / 1000); (unsigned long long)it.it_value.tv_nsec / 1000 / 1000);
timerfd_settime(conn->timer, 0, &it, NULL); if (timerfd_settime(conn->timer, 0, &it, NULL))
flow_err(conn, "failed to set timer: %s", strerror(errno));
} }
/** /**
@ -2235,7 +2236,9 @@ void tcp_timer_handler(const struct ctx *c, union epoll_ref ref)
* timer is currently armed, this event came from a previous setting, * timer is currently armed, this event came from a previous setting,
* and we just set the timer to a new point in the future: discard it. * and we just set the timer to a new point in the future: discard it.
*/ */
timerfd_gettime(conn->timer, &check_armed); if (timerfd_gettime(conn->timer, &check_armed))
flow_err(conn, "failed to read timer: %s", strerror(errno));
if (check_armed.it_value.tv_sec || check_armed.it_value.tv_nsec) if (check_armed.it_value.tv_sec || check_armed.it_value.tv_nsec)
return; return;
@ -2273,7 +2276,10 @@ void tcp_timer_handler(const struct ctx *c, union epoll_ref ref)
* case. This avoids having to preemptively reset the timer on * case. This avoids having to preemptively reset the timer on
* ~ACK_TO_TAP_DUE or ~ACK_FROM_TAP_DUE. * ~ACK_TO_TAP_DUE or ~ACK_FROM_TAP_DUE.
*/ */
timerfd_settime(conn->timer, 0, &new, &old); if (timerfd_settime(conn->timer, 0, &new, &old))
flow_err(conn, "failed to set timer: %s",
strerror(errno));
if (old.it_value.tv_sec == ACT_TIMEOUT) { if (old.it_value.tv_sec == ACT_TIMEOUT) {
flow_dbg(conn, "activity timeout"); flow_dbg(conn, "activity timeout");
tcp_rst(c, conn); tcp_rst(c, conn);