forked from osmocom/wireshark
wsutil: Add portability wrapper for clock_gettime(CLOCK_REALTIME)
parent
e877460a9e
commit
fe30cf2f8a
|
@ -81,6 +81,8 @@ elseif(CMAKE_SYSTEM_NAME STREQUAL "SunOS" AND CMAKE_SYSTEM_VERSION MATCHES "5[.]
|
|||
endif()
|
||||
|
||||
check_function_exists("clock_gettime" HAVE_CLOCK_GETTIME)
|
||||
# Some platforms (macOS pre 10.15) are non-conformant with C11 and lack timespec_get()
|
||||
check_function_exists("timespec_get" HAVE_TIMESPEC_GET)
|
||||
check_function_exists("getifaddrs" HAVE_GETIFADDRS)
|
||||
check_function_exists("issetugid" HAVE_ISSETUGID)
|
||||
check_function_exists("setresgid" HAVE_SETRESGID)
|
||||
|
|
|
@ -40,6 +40,9 @@
|
|||
/* Define to 1 if you have the `clock_gettime` function. */
|
||||
#cmakedefine HAVE_CLOCK_GETTIME 1
|
||||
|
||||
/* Define to 1 if you have the `timespec_get` function. */
|
||||
#cmakedefine HAVE_TIMESPEC_GET 1
|
||||
|
||||
/* Define to use the MaxMind DB library */
|
||||
#cmakedefine HAVE_MAXMINDDB 1
|
||||
|
||||
|
|
|
@ -361,6 +361,7 @@ libwsutil.so.0 libwsutil0 #MINVER#
|
|||
ws_buffer_init@Base 1.99.0
|
||||
ws_buffer_remove_start@Base 1.99.0
|
||||
ws_cleanup_sockets@Base 3.1.0
|
||||
ws_clock_get_realtime@Base 3.7.0
|
||||
ws_cmac_buffer@Base 3.1.0
|
||||
ws_escape_string@Base 3.7.0
|
||||
ws_getopt@Base 3.5.1
|
||||
|
|
|
@ -9,12 +9,6 @@
|
|||
|
||||
#include "config.h"
|
||||
|
||||
#include <time.h>
|
||||
#if !defined(_WIN32) && !defined(HAVE_CLOCK_GETTIME)
|
||||
// For gettimeofday()
|
||||
#include <sys/time.h>
|
||||
#endif
|
||||
|
||||
#include "import_text_dialog.h"
|
||||
|
||||
#include "wiretap/wtap.h"
|
||||
|
@ -29,6 +23,7 @@
|
|||
|
||||
#include "file.h"
|
||||
#include "wsutil/file_util.h"
|
||||
#include "wsutil/time_util.h"
|
||||
#include "wsutil/tempfile.h"
|
||||
#include "wsutil/filesystem.h"
|
||||
|
||||
|
@ -618,22 +613,8 @@ void ImportTextDialog::on_timestampFormatLineEdit_textChanged(const QString &tim
|
|||
char time_str[100];
|
||||
QString timefmt = QString(time_format);
|
||||
|
||||
#if defined(HAVE_CLOCK_GETTIME)
|
||||
// Newer POSIX API. Some UN*Xes whose C libraries lack
|
||||
// timespec_get() (C11) have this.
|
||||
clock_gettime(CLOCK_REALTIME, &timenow);
|
||||
#elif defined(_WIN32)
|
||||
// At least some Windows C libraries have this.
|
||||
// Some UN*X C libraries do, as well, but they might not
|
||||
// show it unless you're requesting C11 - or C++17.
|
||||
timespec_get(&timenow, TIME_UTC);
|
||||
#else
|
||||
// Fall back on gettimeofday().
|
||||
struct timeval usectimenow;
|
||||
gettimeofday(&usectimenow, NULL);
|
||||
timenow.tv_sec = usectimenow.tv_sec;
|
||||
timenow.tv_nsec = usectimenow.tv_usec*1000;
|
||||
#endif
|
||||
ws_clock_get_realtime(&timenow);
|
||||
|
||||
/* On windows strftime/wcsftime does not support %s yet, this works on all OSs */
|
||||
timefmt.replace(QString("%s"), QString::number(timenow.tv_sec));
|
||||
/* subsecond example as usec */
|
||||
|
|
|
@ -210,6 +210,30 @@ create_timestamp(void) {
|
|||
return timestamp;
|
||||
}
|
||||
|
||||
struct timespec *
|
||||
ws_clock_get_realtime(struct timespec *ts)
|
||||
{
|
||||
/*
|
||||
* This function is used in the wslog log handler context.
|
||||
* We must not call anything, even indirectly, that might log a message
|
||||
* using wslog (including GLib).
|
||||
*/
|
||||
#if defined(HAVE_CLOCK_GETTIME)
|
||||
if (clock_gettime(CLOCK_REALTIME, ts) == 0)
|
||||
return ts;
|
||||
#elif defined(HAVE_TIMESPEC_GET)
|
||||
if (timespec_get(ts, TIME_UTC) == TIME_UTC)
|
||||
return ts;
|
||||
#endif
|
||||
|
||||
/* Fall back on gettimeofday(). */
|
||||
struct timeval usectimenow;
|
||||
gettimeofday(&usectimenow, NULL);
|
||||
ts->tv_sec = usectimenow.tv_sec;
|
||||
ts->tv_nsec = usectimenow.tv_usec*1000;
|
||||
return ts;
|
||||
}
|
||||
|
||||
/*
|
||||
* Editor modelines - https://www.wireshark.org/tools/modelines.html
|
||||
*
|
||||
|
|
|
@ -63,6 +63,9 @@ void log_resource_usage(gboolean reset_delta, const char *format, ...);
|
|||
WS_DLL_PUBLIC
|
||||
guint64 create_timestamp(void);
|
||||
|
||||
WS_DLL_PUBLIC
|
||||
struct timespec *ws_clock_get_realtime(struct timespec *ts);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif /* __cplusplus */
|
||||
|
|
|
@ -30,6 +30,7 @@
|
|||
#endif
|
||||
|
||||
#include "file_util.h"
|
||||
#include "time_util.h"
|
||||
#include "to_str.h"
|
||||
#include "strtoi.h"
|
||||
|
||||
|
@ -904,22 +905,6 @@ static void log_write_do_work(FILE *fp, gboolean use_color,
|
|||
}
|
||||
|
||||
|
||||
static inline ws_log_time_t *get_timestamp(ws_log_time_t *ts)
|
||||
{
|
||||
assert(ts);
|
||||
#if defined(HAVE_CLOCK_GETTIME)
|
||||
if (clock_gettime(CLOCK_REALTIME, (struct timespec *)ts) == 0)
|
||||
return ts;
|
||||
#elif defined(_WIN32)
|
||||
if (timespec_get((struct timespec *)ts, TIME_UTC) == TIME_UTC)
|
||||
return ts;
|
||||
#endif
|
||||
ts->tv_sec = time(NULL);
|
||||
ts->tv_nsec = -1;
|
||||
return ts;
|
||||
}
|
||||
|
||||
|
||||
static inline struct tm *get_localtime(time_t unix_time, struct tm **cookie)
|
||||
{
|
||||
if (unix_time == (time_t)-1)
|
||||
|
@ -961,7 +946,7 @@ static void log_write_dispatch(const char *domain, enum ws_log_level level,
|
|||
ws_log_time_t tstamp;
|
||||
struct tm *cookie = NULL;
|
||||
|
||||
get_timestamp(&tstamp);
|
||||
ws_clock_get_realtime((struct timespec *)&tstamp);
|
||||
|
||||
if (custom_log) {
|
||||
va_list user_ap_copy;
|
||||
|
|
Loading…
Reference in New Issue