Corrected gettimeofday() handling

This commit is contained in:
Gerhard Rieger 2021-10-31 13:04:12 +01:00
parent 4cebaf45e4
commit 25cef1c540
6 changed files with 49 additions and 35 deletions

View file

@ -68,7 +68,11 @@ Documentation:
Thanks to Emanuele Torre for reporting this issue. Thanks to Emanuele Torre for reporting this issue.
Corrected more typos and added missing bug info to CHANGES, performed 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: ####################### V 1.7.4.1:

View file

@ -156,6 +156,9 @@
/* Define if you have the ftruncate64 function */ /* Define if you have the ftruncate64 function */
#undef HAVE_FTRUNCATE64 #undef HAVE_FTRUNCATE64
/* Define if you have the gettimeofday function */
#undef HAVE_PROTOTYPE_LIB_gettimeofday
/* Define if you have the clock_gettime function */ /* Define if you have the clock_gettime function */
#undef HAVE_CLOCK_GETTIME #undef HAVE_CLOCK_GETTIME

View file

@ -1,4 +1,4 @@
dnl source: configure.in dnl source: configure.ac
dnl Copyright Gerhard Rieger and contributors (see file CHANGES) dnl Copyright Gerhard Rieger and contributors (see file CHANGES)
dnl Published under the GNU General Public License V.2, see file COPYING 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, AC_CHECK_LIB(util, openpty,
[LIBS="-lutil $LIBS"; AC_DEFINE(HAVE_OPENPTY)]) [LIBS="-lutil $LIBS"; AC_DEFINE(HAVE_OPENPTY)])
AC_CHECK_PROTOTYPE_LIB(gettimeofday)
AC_CHECK_LIB(rt, clock_gettime, AC_CHECK_LIB(rt, clock_gettime,
[LIBS="-lrt $LIBS"; AC_DEFINE(HAVE_CLOCK_GETTIME)]) [LIBS="-lrt $LIBS"; AC_DEFINE(HAVE_CLOCK_GETTIME)])

12
error.c
View file

@ -49,7 +49,7 @@ struct diag_opts diagopts =
static void msg2( static void msg2(
#if HAVE_CLOCK_GETTIME #if HAVE_CLOCK_GETTIME
struct timespec *now, struct timespec *now,
#elif HAVE_GETTIMEOFDAY #elif HAVE_PROTOTYPE_LIB_gettimeofday
struct timeval *now, struct timeval *now,
#else #else
time_t *now, time_t *now,
@ -269,7 +269,7 @@ void msg(int level, const char *format, ...) {
diag_dgram.op = DIAG_OP_MSG; diag_dgram.op = DIAG_OP_MSG;
#if HAVE_CLOCK_GETTIME #if HAVE_CLOCK_GETTIME
clock_gettime(CLOCK_REALTIME, &diag_dgram.now); clock_gettime(CLOCK_REALTIME, &diag_dgram.now);
#elif HAVE_GETTIMEOFDAY #elif HAVE_PROTOTYPE_LIB_gettimeofday
gettimeofday(&diag_dgram.now, NULL); gettimeofday(&diag_dgram.now, NULL);
#else #else
diag_dgram.now = time(NULL); diag_dgram.now = time(NULL);
@ -295,7 +295,7 @@ void msg(int level, const char *format, ...) {
void msg2( void msg2(
#if HAVE_CLOCK_GETTIME #if HAVE_CLOCK_GETTIME
struct timespec *now, struct timespec *now,
#elif HAVE_GETTIMEOFDAY #elif HAVE_PROTOTYPE_LIB_gettimeofday
struct timeval *now, struct timeval *now,
#else #else
time_t *now, time_t *now,
@ -315,7 +315,7 @@ void msg2(
#if HAVE_CLOCK_GETTIME #if HAVE_CLOCK_GETTIME
epoch = now->tv_sec; epoch = now->tv_sec;
#elif HAVE_GETTIMEOFDAY #elif HAVE_PROTOTYPE_LIB_gettimeofday
epoch = now->tv_sec; epoch = now->tv_sec;
#else #else
epoch = *now; epoch = *now;
@ -329,7 +329,7 @@ void msg2(
if (diagopts.micros) { if (diagopts.micros) {
#if HAVE_CLOCK_GETTIME #if HAVE_CLOCK_GETTIME
micros = now->tv_nsec/1000; micros = now->tv_nsec/1000;
#elif HAVE_GETTIMEOFDAY #elif HAVE_PROTOTYPE_LIB_gettimeofday
micros = now->tv_usec; micros = now->tv_usec;
#else #else
micros = 0; micros = 0;
@ -387,7 +387,7 @@ void diag_flush(void) {
/* we want the actual time, not when this dgram was sent */ /* we want the actual time, not when this dgram was sent */
#if HAVE_CLOCK_GETTIME #if HAVE_CLOCK_GETTIME
clock_gettime(CLOCK_REALTIME, &recv_dgram.now); clock_gettime(CLOCK_REALTIME, &recv_dgram.now);
#elif HAVE_GETTIMEOFDAY #elif HAVE_PROTOTYPE_LIB_gettimeofday
gettimeofday(&recv_dgram.now, NULL); gettimeofday(&recv_dgram.now, NULL);
#else #else
recv_dgram.now = time(NULL); recv_dgram.now = time(NULL);

View file

@ -217,7 +217,7 @@ struct diag_dgram {
enum diag_op op; enum diag_op op;
#if HAVE_CLOCK_GETTIME #if HAVE_CLOCK_GETTIME
struct timespec now; struct timespec now;
#elif HAVE_GETTIMEOFDAY #elif HAVE_PROTOTYPE_LIB_gettimeofday
struct timeval now; struct timeval now;
#else #else
time_t now; time_t now;

47
socat.c
View file

@ -1193,40 +1193,45 @@ int _socat(void) {
returns 0 on success or -1 if an error occurred */ returns 0 on success or -1 if an error occurred */
int gettimestamp(char *timestamp) { int gettimestamp(char *timestamp) {
size_t bytes; size_t bytes;
#if HAVE_GETTIMEOFDAY || 1 #if HAVE_CLOCK_GETTIME
struct timespec now;
#elif HAVE_PROTOTYPE_LIB_gettimeofday
struct timeval now; struct timeval now;
int result; #endif /* !HAVE_PROTOTYPE_LIB_gettimeofday */
time_t nowt; time_t nowt;
#else /* !HAVE_GETTIMEOFDAY */ int result;
time_t now;
#endif /* !HAVE_GETTIMEOFDAY */
#if HAVE_GETTIMEOFDAY || 1 #if HAVE_CLOCK_GETTIME
result = gettimeofday(&now, NULL); result = clock_gettime(CLOCK_REALTIME, &now);
if (result < 0) { if (result < 0) {
return result; return result;
} else { }
nowt = now.tv_sec; 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;
}
#endif
#if HAVE_STRFTIME #if HAVE_STRFTIME
bytes = strftime(timestamp, 20, "%Y/%m/%d %H:%M:%S", localtime(&nowt)); 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); bytes += sprintf(timestamp+19, "."F_tv_usec" ", now.tv_usec);
#else
strncpy(&timestamp[bytes++], " ", 2);
#endif
#else #else
strcpy(timestamp, ctime(&nowt)); strcpy(timestamp, ctime(&nowt));
bytes = strlen(timestamp); bytes = strlen(timestamp);
#endif #endif
}
#else /* !HAVE_GETTIMEOFDAY */
now = time(NULL); if (now == (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 */
return 0; return 0;
} }