From 0132da1ddb6059b4be3878a4303bb978a86e662c Mon Sep 17 00:00:00 2001 From: Lubomir Rintel Date: Fri, 15 Jun 2018 14:15:27 +0200 Subject: [PATCH] libnm: add nm_utils_get_timestamp_msec() --- libnm-core/nm-utils.c | 34 ++++++++++++++++++++++++++++++++++ libnm-core/nm-utils.h | 5 +++++ libnm/libnm.ver | 1 + libnm/nm-checkpoint.c | 6 ++++-- libnm/nm-device-wifi.c | 3 +++ 5 files changed, 47 insertions(+), 2 deletions(-) diff --git a/libnm-core/nm-utils.c b/libnm-core/nm-utils.c index eb705cbe0a..b700c7f5fd 100644 --- a/libnm-core/nm-utils.c +++ b/libnm-core/nm-utils.c @@ -6223,6 +6223,40 @@ nm_utils_format_variant_attributes (GHashTable *attributes, /*****************************************************************************/ +/* + * nm_utils_get_timestamp_msec(): + * + * Gets current time in milliseconds of CLOCK_BOOTTIME. + * + * Returns: time in milliseconds + * + * Since: 1.12 + */ +gint64 +nm_utils_get_timestamp_msec (void) +{ + struct timespec ts; + + if (clock_gettime (CLOCK_BOOTTIME, &ts) != -1) + goto success; + + if (errno == 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; + } + + g_return_val_if_reached (-1); + +success: + return (((gint64) ts.tv_sec) * 1000) + (ts.tv_nsec / 1000000); +} + +/*****************************************************************************/ + /** * nm_utils_version: * diff --git a/libnm-core/nm-utils.h b/libnm-core/nm-utils.h index 1f645a8042..7024586967 100644 --- a/libnm-core/nm-utils.h +++ b/libnm-core/nm-utils.h @@ -248,6 +248,11 @@ NMTCTfilter *nm_utils_tc_tfilter_from_str (const char *str, GError **error); NM_AVAILABLE_IN_1_12 char *nm_utils_tc_tfilter_to_str (NMTCTfilter *tfilter, GError **error); +/*****************************************************************************/ + +NM_AVAILABLE_IN_1_12 +gint64 nm_utils_get_timestamp_msec (void); + G_END_DECLS #endif /* __NM_UTILS_H__ */ diff --git a/libnm/libnm.ver b/libnm/libnm.ver index f17cc46e88..5011906c65 100644 --- a/libnm/libnm.ver +++ b/libnm/libnm.ver @@ -1366,5 +1366,6 @@ global: nm_setting_wireless_get_wake_on_wlan; nm_setting_wireless_wake_on_wlan_get_type; nm_settings_connection_flags_get_type; + nm_utils_get_timestamp_msec; nm_vpn_service_plugin_shutdown; } libnm_1_10_0; diff --git a/libnm/nm-checkpoint.c b/libnm/nm-checkpoint.c index 153a71048a..ce15d087f4 100644 --- a/libnm/nm-checkpoint.c +++ b/libnm/nm-checkpoint.c @@ -70,8 +70,10 @@ nm_checkpoint_get_devices (NMCheckpoint *checkpoint) * nm_checkpoint_get_created: * @checkpoint: a #NMCheckpoint * - * Gets the timestamp (in CLOCK_BOOTTIME milliseconds) - * of checkpoint creation. + * Gets the timestamp (in CLOCK_BOOTTIME milliseconds) of checkpoint creation. + * + * Use nm_utils_get_timestamp_msec() to obtain current time value suitable for + * comparing to this value. * * Returns: the timestamp of checkpoint creation. * diff --git a/libnm/nm-device-wifi.c b/libnm/nm-device-wifi.c index 1d6e51064c..848e297cca 100644 --- a/libnm/nm-device-wifi.c +++ b/libnm/nm-device-wifi.c @@ -276,6 +276,9 @@ nm_device_wifi_get_access_point_by_path (NMDeviceWifi *device, * Returns the timestamp (in CLOCK_BOOTTIME milliseconds) for the last finished * network scan. A value of -1 means the device never scanned for access points. * + * Use nm_utils_get_timestamp_msec() to obtain current time value suitable for + * comparing to this value. + * * Returns: the last scan time in seconds * * Since: 1.12