From: Jorge Pereira Date: Sat, 18 May 2019 01:00:21 +0000 (-0300) Subject: Everybody hates gettimeofday(), Season 1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ecb65c035fac6b735b62a4b24c3d5ccf6cc7d0fa;p=thirdparty%2Ffreeradius-server.git Everybody hates gettimeofday(), Season 1 --- diff --git a/src/bin/unit_test_module.c b/src/bin/unit_test_module.c index d3b57eee2b3..ffa08c315e8 100644 --- a/src/bin/unit_test_module.c +++ b/src/bin/unit_test_module.c @@ -460,13 +460,11 @@ static bool do_xlats(char const *filename, FILE *fp) char input[8192]; char output[8192]; REQUEST *request; - struct timeval now; /* * Create and initialize the new request. */ request = request_alloc(NULL); - gettimeofday(&now, NULL); request->log.dst = talloc_zero(request, log_dst_t); request->log.dst->func = vlog_request; diff --git a/src/lib/server/main_loop.c b/src/lib/server/main_loop.c index e7c34d11a12..41f00e36b1c 100644 --- a/src/lib/server/main_loop.c +++ b/src/lib/server/main_loop.c @@ -255,9 +255,7 @@ int main_loop_init(void) #ifdef HAVE_SYSTEMD_WATCHDOG if (sd_watchdog_interval.tv_sec || sd_watchdog_interval.tv_usec) { - struct timeval now; - fr_time_to_timeval(&now, fr_event_list_time(event_list)); - sd_watchdog_event(event_list, &now, NULL); + sd_watchdog_event(event_list, 0, NULL); } #endif diff --git a/src/lib/server/stats.c b/src/lib/server/stats.c index b6fa3614597..56e45899cc2 100644 --- a/src/lib/server/stats.c +++ b/src/lib/server/stats.c @@ -36,8 +36,8 @@ RCSID("$Id$") #define EMA_SCALE (100) #define F_EMA_SCALE (1000000) -static struct timeval start_time; -static struct timeval hup_time; +static fr_time_t start_time; +static fr_time_t hup_time; #define FR_STATS_INIT { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \ { 0, 0, 0, 0, 0, 0, 0, 0 }} @@ -552,9 +552,9 @@ void request_stats_reply(REQUEST *request) */ if ((flag->vp_uint32 & 0x10) != 0) { vp = ADD_TO_REPLY(FR_FREERADIUS_STATS_START_TIME, VENDORPEC_FREERADIUS); - if (vp) vp->vp_date = start_time.tv_sec; + if (vp) vp->vp_date = fr_time_to_sec(start_time); vp = ADD_TO_REPLY(FR_FREERADIUS_STATS_HUP_TIME, VENDORPEC_FREERADIUS); - if (vp) vp->vp_date = hup_time.tv_sec; + if (vp) vp->vp_date = fr_time_to_sec(hup_time); } /* @@ -800,10 +800,10 @@ void request_stats_reply(REQUEST *request) void radius_stats_init(int flag) { if (!flag) { - gettimeofday(&start_time, NULL); + start_time = fr_time(); hup_time = start_time; /* it's just nicer this way */ } else { - gettimeofday(&hup_time, NULL); + hup_time = fr_time(); } } diff --git a/src/lib/server/xlat_eval.c b/src/lib/server/xlat_eval.c index 7d711d8747b..45cda32a779 100644 --- a/src/lib/server/xlat_eval.c +++ b/src/lib/server/xlat_eval.c @@ -284,25 +284,15 @@ static xlat_action_t xlat_eval_one_letter(TALLOC_CTX *ctx, fr_cursor_t *out, REQ */ case 'c': /* Current epoch time seconds */ - { - struct timeval now; - - gettimeofday(&now, NULL); - MEM(value = fr_value_box_alloc(ctx, FR_TYPE_UINT64, NULL, false)); - value->datum.uint64 = (uint64_t)now.tv_sec; - } + value->datum.uint64 = (uint64_t)fr_time_to_sec(fr_time()); + break; case 'C': /* Current epoch time microsecond component */ - { - struct timeval now; - - gettimeofday(&now, NULL); + MEM(value = fr_value_box_alloc(ctx, FR_TYPE_UINT64, NULL, false)); + value->datum.uint64 = (uint64_t)fr_time_to_usec(fr_time()); - MEM(value = fr_value_box_alloc(ctx, FR_TYPE_UINT32, NULL, false)); - value->datum.uint32 = (uint64_t)now.tv_usec; - } break; /* diff --git a/src/modules/rlm_date/rlm_date.c b/src/modules/rlm_date/rlm_date.c index bdb01a1f131..4259593ed63 100644 --- a/src/modules/rlm_date/rlm_date.c +++ b/src/modules/rlm_date/rlm_date.c @@ -89,7 +89,6 @@ static ssize_t xlat_date_convert(UNUSED TALLOC_CTX *ctx, char **out, size_t outl { rlm_date_t const *inst = mod_inst; struct tm tminfo; - struct timeval now; VALUE_PAIR *vp; memset(&tminfo, 0, sizeof(tminfo)); @@ -100,8 +99,7 @@ static ssize_t xlat_date_convert(UNUSED TALLOC_CTX *ctx, char **out, size_t outl } if (strcmp(fmt, "now") == 0) { - gettimeofday(&now, NULL); - return date_encode_strftime(out, outlen, inst, request, now.tv_sec); + return date_encode_strftime(out, outlen, inst, request, fr_time_to_sec(fr_time())); } if ((xlat_fmt_get_vp(&vp, request, fmt) < 0) || !vp) return 0;