libnm,core: use nm_utils_clock_gettime_*() instead of clock_gettime()

We usually want to combine the fields from "struct timespec" to
have one timestamp in either nanoseconds or milliseconds.

Use nm_utils_clock_gettime_*() util for that.
This commit is contained in:
Thomas Haller 2019-07-23 11:52:19 +02:00
parent 3c0161a385
commit be1727be1f
3 changed files with 20 additions and 21 deletions

View file

@ -36,6 +36,7 @@
#endif
#include "nm-glib-aux/nm-enum-utils.h"
#include "nm-glib-aux/nm-time-utils.h"
#include "nm-glib-aux/nm-secret-utils.h"
#include "systemd/nm-sd-utils-shared.h"
#include "nm-libnm-core-intern/nm-common-macros.h"
@ -5775,24 +5776,23 @@ nm_utils_format_variant_attributes (GHashTable *attributes,
gint64
nm_utils_get_timestamp_msec (void)
{
struct timespec ts;
gint64 ts;
if (clock_gettime (CLOCK_BOOTTIME, &ts) != -1)
goto success;
ts = nm_utils_clock_gettime_ms (CLOCK_BOOTTIME);
if (ts >= 0)
return ts;
if (errno == EINVAL) {
if (ts == -EINVAL) {
/* The fallback to CLOCK_MONOTONIC is taken only if we're running on a
* criminally old kernel, prior to 2.6.39 (released on 18 May, 2011).
* That happens during buildcheck on old builders, we don't expect to
* be actually runs on kernels that old. */
if (clock_gettime (CLOCK_MONOTONIC, &ts) != -1)
goto success;
ts = nm_utils_clock_gettime_ms (CLOCK_MONOTONIC);
if (ts >= 0)
return ts;
}
g_return_val_if_reached (-1);
success:
return (((gint64) ts.tv_sec) * 1000) + (ts.tv_nsec / 1000000);
}
/*****************************************************************************/

View file

@ -40,6 +40,7 @@
#include "nm-glib-aux/nm-random-utils.h"
#include "nm-glib-aux/nm-io-utils.h"
#include "nm-glib-aux/nm-secret-utils.h"
#include "nm-glib-aux/nm-time-utils.h"
#include "nm-utils.h"
#include "nm-core-internal.h"
#include "nm-setting-connection.h"
@ -2546,7 +2547,7 @@ _host_id_read_timestamp (gboolean use_secret_key_file,
&& stat (SECRET_KEY_FILE, &st) == 0) {
/* don't check for overflow or timestamps in the future. We get whatever
* (bogus) date is on the file. */
*out_timestamp_ns = (st.st_mtim.tv_sec * NM_UTILS_NS_PER_SECOND) + st.st_mtim.tv_nsec;
*out_timestamp_ns = nm_utils_timespec_to_ns (&st.st_mtim);
return TRUE;
}

View file

@ -739,29 +739,27 @@ _timestamp_nl_to_ms (guint32 timestamp_nl, gint64 monotonic_ms)
static guint32
_addrtime_timestamp_to_nm (guint32 timestamp, gint32 *out_now_nm)
{
struct timespec tp;
gint64 now_nl, now_nm, result;
int err;
gint64 now_nl;
gint64 now_nm;
gint64 result;
/* timestamp is unset. Default to 1. */
if (!timestamp) {
if (out_now_nm)
*out_now_nm = 0;
NM_SET_OUT (out_now_nm, 0);
return 1;
}
/* do all the calculations in milliseconds scale */
err = clock_gettime (CLOCK_MONOTONIC, &tp);
g_assert (err == 0);
now_nm = nm_utils_get_monotonic_timestamp_ms ();
now_nl = (((gint64) tp.tv_sec) * ((gint64) 1000)) +
(tp.tv_nsec / (NM_UTILS_NS_PER_SECOND/1000));
now_nl = nm_utils_clock_gettime_ms (CLOCK_MONOTONIC);
nm_assert (now_nm >= 1000);
nm_assert (now_nl >= 0);
result = now_nm - (now_nl - _timestamp_nl_to_ms (timestamp, now_nl));
if (out_now_nm)
*out_now_nm = now_nm / 1000;
NM_SET_OUT (out_now_nm, now_nm / 1000);
/* converting the timestamp into nm_utils_get_monotonic_timestamp_ms() scale is
* a good guess but fails in the following situations: