From be5a505d771a77c640acc35ceaa470c80e62f954 Mon Sep 17 00:00:00 2001
From: Denys Vlasenko <vda.linux@googlemail.com>
Date: Thu, 24 Oct 2019 16:26:55 +0200
Subject: [PATCH] Remove syscall wrappers around clock_gettime, closes 12091

12091 "Direct use of __NR_clock_gettime is not time64-safe".

function                                             old     new   delta
runsv_main                                          1698    1712     +14
startservice                                         378     383      +5
get_mono                                              31      25      -6
date_main                                            932     926      -6
gettimeofday_ns                                       17       -     -17
------------------------------------------------------------------------------
(add/remove: 0/1 grow/shrink: 2/2 up/down: 19/-29)            Total: -10 bytes

Upstream-Status: Backport [https://git.busybox.net/busybox/commit/?id=be5a505d771a77c640acc35ceaa470c80e62f954]
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
---
 Makefile.flags   |  6 ++++--
 coreutils/date.c | 16 +++-------------
 libbb/time.c     | 11 +----------
 runit/runsv.c    | 11 +----------
 4 files changed, 9 insertions(+), 35 deletions(-)

--- a/Makefile.flags
+++ b/Makefile.flags
@@ -129,10 +129,12 @@ endif
 # fall back to using a temp file:
 CRYPT_AVAILABLE := $(shell echo 'int main(void){return 0;}' >crypttest.c; $(CC) $(CFLAGS) -lcrypt -o /dev/null crypttest.c >/dev/null 2>&1 && echo "y"; rm crypttest.c)
 ifeq ($(CRYPT_AVAILABLE),y)
-LDLIBS += m crypt
+LDLIBS += m rt crypt
 else
-LDLIBS += m
+LDLIBS += m rt
 endif
+# libm may be needed for dc, awk, ntpd
+# librt may be needed for clock_gettime()
 
 # libpam may use libpthread, libdl and/or libaudit.
 # On some platforms that requires an explicit -lpthread, -ldl, -laudit.
--- a/coreutils/date.c
+++ b/coreutils/date.c
@@ -33,10 +33,9 @@
 //config:	Enable option (-I) to output an ISO-8601 compliant
 //config:	date/time string.
 //config:
-//config:# defaults to "no": stat's nanosecond field is a bit non-portable
 //config:config FEATURE_DATE_NANO
 //config:	bool "Support %[num]N nanosecond format specifier"
-//config:	default n  # syscall(__NR_clock_gettime) or syscall(__NR_clock_gettime64)
+//config:	default n # stat's nanosecond field is a bit non-portable
 //config:	depends on DATE
 //config:	select PLATFORM_LINUX
 //config:	help
@@ -271,17 +270,8 @@ int date_main(int argc UNUSED_PARAM, cha
 		 */
 #endif
 	} else {
-#if ENABLE_FEATURE_DATE_NANO && defined(__NR_clock_gettime)
-		/* libc has incredibly messy way of doing this,
-		 * typically requiring -lrt. We just skip all this mess */
-		syscall(__NR_clock_gettime, CLOCK_REALTIME, &ts);
-#elif ENABLE_FEATURE_DATE_NANO && __TIMESIZE == 64
-		/* Let's only support the 64 suffix syscalls for 64-bit time_t.
-		 * This simplifies the code for us as we don't need to convert
-		 * between 64-bit and 32-bit. We also don't have a way to
-		 * report overflow errors here.
-		 */
-		syscall(__NR_clock_gettime64, CLOCK_REALTIME, &ts);
+#if ENABLE_FEATURE_DATE_NANO
+		clock_gettime(CLOCK_REALTIME, &ts);
 #else
 		time(&ts.tv_sec);
 #endif
--- a/libbb/time.c
+++ b/libbb/time.c
@@ -253,18 +253,9 @@ char* FAST_FUNC strftime_YYYYMMDDHHMMSS(
 #define CLOCK_MONOTONIC 1
 #endif
 
-/* libc has incredibly messy way of doing this,
- * typically requiring -lrt. We just skip all this mess */
 static void get_mono(struct timespec *ts)
 {
-#if defined(__NR_clock_gettime)
- 	if (syscall(__NR_clock_gettime, CLOCK_MONOTONIC, ts))
-#elif __TIMESIZE == 64
-	if (syscall(__NR_clock_gettime64, CLOCK_MONOTONIC, ts))
-#else
-# error "We currently don't support architectures without " \
-	"the __NR_clock_gettime syscall and 32-bit time_t"
-#endif
+	if (clock_gettime(CLOCK_MONOTONIC, ts))
 		bb_error_msg_and_die("clock_gettime(MONOTONIC) failed");
 }
 unsigned long long FAST_FUNC monotonic_ns(void)
--- a/runit/runsv.c
+++ b/runit/runsv.c
@@ -51,18 +51,9 @@ ADVISED OF THE POSSIBILITY OF SUCH DAMAG
 #if ENABLE_MONOTONIC_SYSCALL
 #include <sys/syscall.h>
 
-/* libc has incredibly messy way of doing this,
- * typically requiring -lrt. We just skip all this mess */
 static void gettimeofday_ns(struct timespec *ts)
 {
-#if defined(__NR_clock_gettime)
-	syscall(__NR_clock_gettime, CLOCK_REALTIME, ts);
-#elif __TIMESIZE == 64
-	syscall(__NR_clock_gettime64, CLOCK_REALTIME, ts);
-#else
-# error "We currently don't support architectures without " \
-	"the __NR_clock_gettime syscall and 32-bit time_t"
-#endif
+	clock_gettime(CLOCK_REALTIME, ts);
 }
 #else
 static void gettimeofday_ns(struct timespec *ts)
