Module: kamailio
Branch: master
Commit: 5d09caf029ed4271ac6dc856cb70cbf375b4c7f1
URL:
https://github.com/kamailio/kamailio/commit/5d09caf029ed4271ac6dc856cb70cbf…
Author: Matteo Brancaleoni <matteo.brancaleoni(a)voismart.it>
Committer: Henning Westerholt <hw(a)gilawa.com>
Date: 2022-08-23T11:11:47+02:00
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
---
Modified: src/modules/pv/pv_time.c
---
Diff:
https://github.com/kamailio/kamailio/commit/5d09caf029ed4271ac6dc856cb70cbf…
Patch:
https://github.com/kamailio/kamailio/commit/5d09caf029ed4271ac6dc856cb70cbf…
---
diff --git a/src/modules/pv/pv_time.c b/src/modules/pv/pv_time.c
index 4a9cc14dd1..a05134f5e4 100644
--- a/src/modules/pv/pv_time.c
+++ b/src/modules/pv/pv_time.c
@@ -23,6 +23,8 @@
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
+#include <stdint.h>
+#include <inttypes.h>
#include <stdio.h>
#include <time.h>
#include <sys/time.h>
@@ -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: