From a888fca2a17d92a95d2343cdd1536fc4f83467fa Mon Sep 17 00:00:00 2001 From: Matteo Brancaleoni Date: Tue, 19 Jul 2022 16:23:53 +0200 Subject: [PATCH] pv: add monotonic clock to TimeVal pseudovariable - adds $TV(Sm) pseudovariable which returns the system monotonic clock as a string - on Linux, tries to use CLOCK_MONOTONIC_RAW which does not suffer from ntp time adjustement --- src/modules/pv/pv_time.c | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/modules/pv/pv_time.c b/src/modules/pv/pv_time.c index 4a9cc14dd16..a05134f5e43 100644 --- a/src/modules/pv/pv_time.c +++ b/src/modules/pv/pv_time.c @@ -23,6 +23,8 @@ #include #include #include +#include +#include #include #include #include @@ -33,6 +35,12 @@ #include "pv_time.h" +#if defined(CLOCK_MONOTONIC_RAW) +#define PV_MONOTONIC_CLOCK CLOCK_MONOTONIC_RAW +#else +#define PV_MONOTONIC_CLOCK CLOCK_MONOTONIC +#endif + int pv_parse_time_name(pv_spec_p sp, str *in) { if(sp==NULL || in==NULL || in->len<=0) @@ -306,6 +314,7 @@ int pv_get_timeval(struct sip_msg *msg, pv_param_t *param, pv_value_t *res) { struct timeval tv; + struct timespec ts; str s; struct tm lt; char lbuf[64]; @@ -355,7 +364,18 @@ int pv_get_timeval(struct sip_msg *msg, pv_param_t *param, return pv_get_null(msg, param, res); s.s = _timeval_ts_buf; return pv_get_strval(msg, param, res, &s); - + case 6: + if (!clock_gettime(PV_MONOTONIC_CLOCK, &ts)) { + s.len = snprintf(_timeval_ts_buf, 64, "%" PRIu64, + (u_int64_t)ts.tv_sec * 1000000000 + (u_int64_t)ts.tv_nsec); + if(s.len<0) + return pv_get_null(msg, param, res); + s.s = _timeval_ts_buf; + return pv_get_strval(msg, param, res, &s); + } else { + LM_ERR("unable to get monotonic clock value\n"); + return pv_get_null(msg, param, res); + } default: msg_set_time(msg); return pv_get_uintval(msg, param, res, (unsigned int)msg->tval.tv_sec); @@ -385,6 +405,8 @@ int pv_parse_timeval_name(pv_spec_p sp, str *in) sp->pvp.pvn.u.isname.name.n = 4; else if(strncmp(in->s, "Fn", 2)==0) sp->pvp.pvn.u.isname.name.n = 5; + else if(strncmp(in->s, "Sm", 2)==0) + sp->pvp.pvn.u.isname.name.n = 6; else goto error; break; default: