diff options
author | Thomas Haller <thaller@redhat.com> | 2019-07-23 11:52:19 +0200 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2019-07-23 12:19:33 +0200 |
commit | be1727be1f93bf181c04e0a742196be154026905 (patch) | |
tree | 4850df1fa00648a063ce7376887b87d518376630 | |
parent | 3c0161a3852f7424523c8761c63313ed61b46a41 (diff) | |
download | NetworkManager-be1727be1f93bf181c04e0a742196be154026905.tar.gz |
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.
-rw-r--r-- | libnm-core/nm-utils.c | 18 | ||||
-rw-r--r-- | src/nm-core-utils.c | 3 | ||||
-rw-r--r-- | src/platform/nm-linux-platform.c | 20 |
3 files changed, 20 insertions, 21 deletions
diff --git a/libnm-core/nm-utils.c b/libnm-core/nm-utils.c index b390b4f99f..6dc3f53dd3 100644 --- a/libnm-core/nm-utils.c +++ b/libnm-core/nm-utils.c @@ -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); } /*****************************************************************************/ diff --git a/src/nm-core-utils.c b/src/nm-core-utils.c index 6784551675..12780e1875 100644 --- a/src/nm-core-utils.c +++ b/src/nm-core-utils.c @@ -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; } diff --git a/src/platform/nm-linux-platform.c b/src/platform/nm-linux-platform.c index c8bbedaf97..e2e1e58149 100644 --- a/src/platform/nm-linux-platform.c +++ b/src/platform/nm-linux-platform.c @@ -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: |