Skip to content

Commit

Permalink
xdpdump: Check return value of clock_gettime()
Browse files Browse the repository at this point in the history
Coverity points out that we don't check the return value of
clock_gettime() in xdpdump. Refactor the helper function to allow
returning an error if the syscall fails, and bail out when it does.

Signed-off-by: Toke Høiland-Jørgensen <[email protected]>
  • Loading branch information
tohojo committed Oct 23, 2024
1 parent 9b5f059 commit f8a65e8
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions xdp-dump/xdpdump.c
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ static enum bpf_perf_event_ret handle_perf_event(void *private_data,
/*****************************************************************************
* get_epoch_to_uptime_delta()
*****************************************************************************/
static uint64_t get_epoch_to_uptime_delta(void)
static int get_epoch_to_uptime_delta(uint64_t *delta)
{
/* This function will calculate the rough delta between uptime
* seconds and the epoch time. This is not a precise delta as there is
Expand All @@ -576,10 +576,15 @@ static uint64_t get_epoch_to_uptime_delta(void)
uint64_t uptime;
uint64_t epoch = time(NULL) * 1000000000ULL;

clock_gettime(CLOCK_MONOTONIC, &ts);
if (clock_gettime(CLOCK_MONOTONIC, &ts)) {
pr_warn("ERROR: Failed to get CLOCK_MONOTONIC time: %s(%d)",
strerror(errno), errno);
return -errno;
}
uptime = ts.tv_sec * 1000000000ULL + ts.tv_nsec;

return epoch - uptime;
*delta = epoch - uptime;
return 0;
}

/*****************************************************************************
Expand Down Expand Up @@ -1782,7 +1787,9 @@ static bool capture_on_interface(struct dumpopt *cfg)
perf_ctx.pcap = pcap;
perf_ctx.pcap_dumper = pcap_dumper;
perf_ctx.pcapng_dumper = pcapng_dumper;
perf_ctx.epoch_delta = get_epoch_to_uptime_delta();

if (get_epoch_to_uptime_delta(&perf_ctx.epoch_delta))
goto error_exit;

/* Determine the perf wakeup_events value to use */
#ifdef HAVE_LIBBPF_PERF_BUFFER__CONSUME
Expand Down

0 comments on commit f8a65e8

Please sign in to comment.