local: add support for clock_gettime()

Use clock_gettime() to read the system clock in nanosecond resolution.
This commit is contained in:
Miroslav Lichvar 2016-08-18 15:07:01 +02:00
parent cfe706f032
commit 8d89610ff6
4 changed files with 26 additions and 19 deletions

24
configure vendored
View file

@ -569,6 +569,21 @@ then
fi fi
fi fi
if test_code 'clock_gettime()' 'time.h' '' '' \
'clock_gettime(CLOCK_REALTIME, NULL);'
then
add_def HAVE_CLOCK_GETTIME
else
if test_code 'clock_gettime() in -lrt' 'time.h' '' '-lrt' \
'clock_gettime(CLOCK_REALTIME, NULL);'
then
add_def HAVE_CLOCK_GETTIME
EXTRA_LIBS="$EXTRA_LIBS -lrt"
else
try_phc=0
fi
fi
if test_code 'getaddrinfo()' 'sys/types.h sys/socket.h netdb.h' '' "$EXTRA_LIBS" \ if test_code 'getaddrinfo()' 'sys/types.h sys/socket.h netdb.h' '' "$EXTRA_LIBS" \
'return getaddrinfo(0, 0, 0, 0);' 'return getaddrinfo(0, 0, 0, 0);'
then then
@ -676,16 +691,7 @@ if [ $feat_refclock = "1" ] && [ $feat_phc = "1" ] && [ $try_phc = "1" ] && \
test_code '<linux/ptp_clock.h>' 'sys/ioctl.h linux/ptp_clock.h' '' '' \ test_code '<linux/ptp_clock.h>' 'sys/ioctl.h linux/ptp_clock.h' '' '' \
'ioctl(1, PTP_CLOCK_GETCAPS, 0);' 'ioctl(1, PTP_CLOCK_GETCAPS, 0);'
then then
if test_code 'clock_gettime()' 'time.h' '' '' 'clock_gettime(0, NULL);'; then
add_def FEAT_PHC add_def FEAT_PHC
else
if test_code 'clock_gettime() in -lrt' 'time.h' '' '-lrt' \
'clock_gettime(0, NULL);'
then
EXTRA_LIBS="$EXTRA_LIBS -lrt"
add_def FEAT_PHC
fi
fi
fi fi
if [ $try_setsched = "1" ] && \ if [ $try_setsched = "1" ] && \

12
local.c
View file

@ -350,19 +350,21 @@ void LCL_RemoveDispersionNotifyHandler(LCL_DispersionNotifyHandler handler, void
} }
/* ================================================== */ /* ================================================== */
/* At the moment, this is just gettimeofday(), because
I can't think of a Unix system where it would not be */
void void
LCL_ReadRawTime(struct timespec *ts) LCL_ReadRawTime(struct timespec *ts)
{ {
#if HAVE_CLOCK_GETTIME
if (clock_gettime(CLOCK_REALTIME, ts) < 0)
LOG_FATAL(LOGF_Local, "clock_gettime() failed : %s", strerror(errno));
#else
struct timeval tv; struct timeval tv;
if (gettimeofday(&tv, NULL) < 0) { if (gettimeofday(&tv, NULL) < 0)
LOG_FATAL(LOGF_Local, "gettimeofday() failed"); LOG_FATAL(LOGF_Local, "gettimeofday() failed : %s", stderror(errno));
}
UTI_TimevalToTimespec(&tv, ts); UTI_TimevalToTimespec(&tv, ts);
#endif
} }
/* ================================================== */ /* ================================================== */

View file

@ -31,8 +31,7 @@
#include "sysincl.h" #include "sysincl.h"
/* Read the system clock. This is analogous to gettimeofday(), /* Read the system clock */
but with the timezone information ignored */
extern void LCL_ReadRawTime(struct timespec *ts); extern void LCL_ReadRawTime(struct timespec *ts);
/* Read the system clock, corrected according to all accumulated /* Read the system clock, corrected according to all accumulated

View file

@ -452,8 +452,8 @@ SYS_Linux_EnableSystemCallFilter(int level)
{ {
const int syscalls[] = { const int syscalls[] = {
/* Clock */ /* Clock */
SCMP_SYS(adjtimex), SCMP_SYS(gettimeofday), SCMP_SYS(settimeofday), SCMP_SYS(adjtimex), SCMP_SYS(clock_gettime), SCMP_SYS(gettimeofday),
SCMP_SYS(time), SCMP_SYS(settimeofday), SCMP_SYS(time),
/* Process */ /* Process */
SCMP_SYS(clone), SCMP_SYS(exit), SCMP_SYS(exit_group), SCMP_SYS(getrlimit), SCMP_SYS(clone), SCMP_SYS(exit), SCMP_SYS(exit_group), SCMP_SYS(getrlimit),
SCMP_SYS(rt_sigaction), SCMP_SYS(rt_sigreturn), SCMP_SYS(rt_sigprocmask), SCMP_SYS(rt_sigaction), SCMP_SYS(rt_sigreturn), SCMP_SYS(rt_sigprocmask),