From 25cef1c540e27ffbd374e03ed96d268e949b1c43 Mon Sep 17 00:00:00 2001 From: Gerhard Rieger Date: Sun, 31 Oct 2021 13:04:12 +0100 Subject: [PATCH] Corrected gettimeofday() handling --- CHANGES | 6 +++++- config.h.in | 3 +++ configure.ac | 4 +++- error.c | 12 +++++------ error.h | 2 +- socat.c | 57 ++++++++++++++++++++++++++++------------------------ 6 files changed, 49 insertions(+), 35 deletions(-) diff --git a/CHANGES b/CHANGES index 984d391..0a5d8ef 100644 --- a/CHANGES +++ b/CHANGES @@ -68,7 +68,11 @@ Documentation: Thanks to Emanuele Torre for reporting this issue. Corrected more typos and added missing bug info to CHANGES, performed - some non functional corrections + some non functional corrections. + +Porting: + Corrected building when clock_gettime() not available, with or without + gettimeofday(). ####################### V 1.7.4.1: diff --git a/config.h.in b/config.h.in index 7292c01..62f10d3 100644 --- a/config.h.in +++ b/config.h.in @@ -156,6 +156,9 @@ /* Define if you have the ftruncate64 function */ #undef HAVE_FTRUNCATE64 +/* Define if you have the gettimeofday function */ +#undef HAVE_PROTOTYPE_LIB_gettimeofday + /* Define if you have the clock_gettime function */ #undef HAVE_CLOCK_GETTIME diff --git a/configure.ac b/configure.ac index b6b3cab..09dbb7a 100644 --- a/configure.ac +++ b/configure.ac @@ -1,4 +1,4 @@ -dnl source: configure.in +dnl source: configure.ac dnl Copyright Gerhard Rieger and contributors (see file CHANGES) dnl Published under the GNU General Public License V.2, see file COPYING @@ -1491,6 +1491,8 @@ AC_CHECK_LIB(bsd, openpty, AC_CHECK_LIB(util, openpty, [LIBS="-lutil $LIBS"; AC_DEFINE(HAVE_OPENPTY)]) +AC_CHECK_PROTOTYPE_LIB(gettimeofday) + AC_CHECK_LIB(rt, clock_gettime, [LIBS="-lrt $LIBS"; AC_DEFINE(HAVE_CLOCK_GETTIME)]) diff --git a/error.c b/error.c index b1ce8f6..1cff8d1 100644 --- a/error.c +++ b/error.c @@ -49,7 +49,7 @@ struct diag_opts diagopts = static void msg2( #if HAVE_CLOCK_GETTIME struct timespec *now, -#elif HAVE_GETTIMEOFDAY +#elif HAVE_PROTOTYPE_LIB_gettimeofday struct timeval *now, #else time_t *now, @@ -269,7 +269,7 @@ void msg(int level, const char *format, ...) { diag_dgram.op = DIAG_OP_MSG; #if HAVE_CLOCK_GETTIME clock_gettime(CLOCK_REALTIME, &diag_dgram.now); -#elif HAVE_GETTIMEOFDAY +#elif HAVE_PROTOTYPE_LIB_gettimeofday gettimeofday(&diag_dgram.now, NULL); #else diag_dgram.now = time(NULL); @@ -295,7 +295,7 @@ void msg(int level, const char *format, ...) { void msg2( #if HAVE_CLOCK_GETTIME struct timespec *now, -#elif HAVE_GETTIMEOFDAY +#elif HAVE_PROTOTYPE_LIB_gettimeofday struct timeval *now, #else time_t *now, @@ -315,7 +315,7 @@ void msg2( #if HAVE_CLOCK_GETTIME epoch = now->tv_sec; -#elif HAVE_GETTIMEOFDAY +#elif HAVE_PROTOTYPE_LIB_gettimeofday epoch = now->tv_sec; #else epoch = *now; @@ -329,7 +329,7 @@ void msg2( if (diagopts.micros) { #if HAVE_CLOCK_GETTIME micros = now->tv_nsec/1000; -#elif HAVE_GETTIMEOFDAY +#elif HAVE_PROTOTYPE_LIB_gettimeofday micros = now->tv_usec; #else micros = 0; @@ -387,7 +387,7 @@ void diag_flush(void) { /* we want the actual time, not when this dgram was sent */ #if HAVE_CLOCK_GETTIME clock_gettime(CLOCK_REALTIME, &recv_dgram.now); -#elif HAVE_GETTIMEOFDAY +#elif HAVE_PROTOTYPE_LIB_gettimeofday gettimeofday(&recv_dgram.now, NULL); #else recv_dgram.now = time(NULL); diff --git a/error.h b/error.h index c6a3048..32e5a59 100644 --- a/error.h +++ b/error.h @@ -217,7 +217,7 @@ struct diag_dgram { enum diag_op op; #if HAVE_CLOCK_GETTIME struct timespec now; -#elif HAVE_GETTIMEOFDAY +#elif HAVE_PROTOTYPE_LIB_gettimeofday struct timeval now; #else time_t now; diff --git a/socat.c b/socat.c index efc9d3e..9e02129 100644 --- a/socat.c +++ b/socat.c @@ -1193,40 +1193,45 @@ int _socat(void) { returns 0 on success or -1 if an error occurred */ int gettimestamp(char *timestamp) { size_t bytes; -#if HAVE_GETTIMEOFDAY || 1 +#if HAVE_CLOCK_GETTIME + struct timespec now; +#elif HAVE_PROTOTYPE_LIB_gettimeofday struct timeval now; - int result; +#endif /* !HAVE_PROTOTYPE_LIB_gettimeofday */ time_t nowt; -#else /* !HAVE_GETTIMEOFDAY */ - time_t now; -#endif /* !HAVE_GETTIMEOFDAY */ + int result; -#if HAVE_GETTIMEOFDAY || 1 - result = gettimeofday(&now, NULL); +#if HAVE_CLOCK_GETTIME + result = clock_gettime(CLOCK_REALTIME, &now); if (result < 0) { return result; - } else { - nowt = now.tv_sec; -#if HAVE_STRFTIME - bytes = strftime(timestamp, 20, "%Y/%m/%d %H:%M:%S", localtime(&nowt)); - bytes += sprintf(timestamp+19, "."F_tv_usec" ", now.tv_usec); -#else - strcpy(timestamp, ctime(&nowt)); - bytes = strlen(timestamp); -#endif } -#else /* !HAVE_GETTIMEOFDAY */ - now = time(NULL); if (now == (time_t)-1) { + nowt = now.tv_sec; +#elif HAVE_PROTOTYPE_LIB_gettimeofday + result = Gettimeofday(&now, NULL); + if (result < 0) { + return result; + } + nowt = now.tv_sec; +#else + nowt = time(NULL); + if (nowt == (time_t)-1) { return -1; - } else { -#if HAVE_STRFTIME - bytes = strftime(timestamp, 21, "%Y/%m/%d %H:%M:%S ", localtime(&now)); -#else - strcpy(timestamp, ctime(&now)); - bytes = strlen(timestamp); -#endif } -#endif /* !HAVE_GETTIMEOFDAY */ +#endif +#if HAVE_STRFTIME + bytes = strftime(timestamp, 20, "%Y/%m/%d %H:%M:%S", localtime(&nowt)); +#if HAVE_CLOCK_GETTIME + bytes += sprintf(timestamp+19, "."F_tv_usec" ", now.tv_nsec/1000); +#elif HAVE_PROTOTYPE_LIB_gettimeofday + bytes += sprintf(timestamp+19, "."F_tv_usec" ", now.tv_usec); +#else + strncpy(×tamp[bytes++], " ", 2); +#endif +#else + strcpy(timestamp, ctime(&nowt)); + bytes = strlen(timestamp); +#endif return 0; }