瀏覽代碼

implement settimeofday in terms of clock_settime, not old syscall

this is yet another place where special handling of time syscalls can
and should be avoided by implementing legacy functions in terms of
their modern replacements. in theory a fallback to SYS_settimeofday
could be added to clock_settime, but SYS_clock_settime has been
available since Linux 2.6.0 or earlier, i.e. all the way back to the
minimum supported version.
Rich Felker 5 年之前
父節點
當前提交
2c2c3605d3
共有 1 個文件被更改,包括 6 次插入1 次删除
  1. 6 1
      src/linux/settimeofday.c

+ 6 - 1
src/linux/settimeofday.c

@@ -1,8 +1,13 @@
 #define _BSD_SOURCE
 #include <sys/time.h>
+#include <time.h>
+#include <errno.h>
 #include "syscall.h"
 
 int settimeofday(const struct timeval *tv, const struct timezone *tz)
 {
-	return syscall(SYS_settimeofday, tv, 0);
+	if (!tv) return 0;
+	if (tv->tv_usec >= 1000000ULL) return __syscall_ret(-EINVAL);
+	return clock_settime(CLOCK_REALTIME, &((struct timespec){
+		.tv_sec = tv->tv_sec, .tv_nsec = tv->tv_usec * 1000}));
 }