Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

tools/nolibc: fall back to sys_clock_gettime() in gettimeofday()

Newer architectures (like riscv32) do not implement sys_gettimeofday().
In those cases fall back to sys_clock_gettime().
While that does not support the timezone argument of sys_gettimeofday(),
specifying this argument invokes undefined behaviour, so it's safe to ignore.

Signed-off-by: Thomas Weißschuh <thomas.weissschuh@linutronix.de>
Acked-by: Willy Tarreau <w@1wt.eu>
Link: https://lore.kernel.org/r/20250428-nolibc-misc-v2-14-3c043eeab06c@linutronix.de
Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>

authored by

Thomas Weißschuh and committed by
Thomas Weißschuh
5e7392dc a009a0c6

+14 -1
+14 -1
tools/include/nolibc/sys/time.h
··· 13 13 #include "../arch.h" 14 14 #include "../sys.h" 15 15 16 + static int sys_clock_gettime(clockid_t clockid, struct timespec *tp); 17 + 16 18 /* 17 19 * int gettimeofday(struct timeval *tv, struct timezone *tz); 18 20 */ ··· 25 23 #ifdef __NR_gettimeofday 26 24 return my_syscall2(__NR_gettimeofday, tv, tz); 27 25 #else 28 - return __nolibc_enosys(__func__, tv, tz); 26 + (void) tz; /* Non-NULL tz is undefined behaviour */ 27 + 28 + struct timespec tp; 29 + int ret; 30 + 31 + ret = sys_clock_gettime(CLOCK_REALTIME, &tp); 32 + if (!ret && tv) { 33 + tv->tv_sec = tp.tv_sec; 34 + tv->tv_usec = tp.tv_nsec / 1000; 35 + } 36 + 37 + return ret; 29 38 #endif 30 39 } 31 40