[klibc] [klibc:master] time: Use clock_* system calls for time-of-day and sleep
klibc-bot for Ben Hutchings
ben at decadent.org.uk
Sun Feb 12 14:48:12 PST 2023
Commit-ID: 0b35ac6c018c83be12a66db6b026b81b3321846a
Gitweb: http://git.kernel.org/?p=libs/klibc/klibc.git;a=commit;h=0b35ac6c018c83be12a66db6b026b81b3321846a
Author: Ben Hutchings <ben at decadent.org.uk>
AuthorDate: Sat, 14 Jan 2023 02:15:10 +0100
Committer: Ben Hutchings <ben at decadent.org.uk>
CommitDate: Sun, 12 Feb 2023 22:10:18 +0100
[klibc] time: Use clock_* system calls for time-of-day and sleep
{get,set}timeofday(), nanosleep(), and time() on't have direct
replacements that use 64-bit time on 32-bit architectures. Instead,
we have to use the clock_*() system calls.
In preparation for using 64-bit time everywhere:
- Make clock_{gettime,nanosleep,settime}() required system calls
- Make {get,set}timeofday(), nanosleep(), and time() use them.
We still need to use the {get,set}timeofday() system calls to
get and set the time zone.
Signed-off-by: Ben Hutchings <ben at decadent.org.uk>
---
usr/include/sys/time.h | 3 +++
usr/include/sys/types.h | 1 +
usr/klibc/Kbuild | 6 ++++--
usr/klibc/SYSCALLS.def | 9 +++++----
usr/klibc/clock_nanosleep.c | 17 +++++++++++++++++
usr/klibc/gettimeofday.c | 22 ++++++++++++++++++++++
usr/klibc/nanosleep.c | 8 ++++++++
usr/klibc/settimeofday.c | 22 ++++++++++++++++++++++
usr/klibc/time.c | 4 ----
9 files changed, 82 insertions(+), 10 deletions(-)
diff --git a/usr/include/sys/time.h b/usr/include/sys/time.h
index da9f5809..e8163303 100644
--- a/usr/include/sys/time.h
+++ b/usr/include/sys/time.h
@@ -50,6 +50,9 @@ static inline int FD_ISSET(int __fd, fd_set *__fdsetp)
__extern int gettimeofday(struct timeval *, struct timezone *);
__extern int settimeofday(const struct timeval *, const struct timezone *);
+__extern int clock_gettime(clockid_t, struct timespec *);
+__extern int clock_settime(clockid_t, const struct timespec *);
+__extern int clock_nanosleep(clockid_t, int, const struct timespec *, struct timespec *);
__extern int getitimer(int, struct itimerval *);
__extern int setitimer(int, const struct itimerval *, struct itimerval *);
__extern int utimes(const char *, const struct timeval[2]);
diff --git a/usr/include/sys/types.h b/usr/include/sys/types.h
index b8cdb8cf..e17bc87f 100644
--- a/usr/include/sys/types.h
+++ b/usr/include/sys/types.h
@@ -33,6 +33,7 @@ typedef __kernel_key_t key_t;
typedef __kernel_suseconds_t suseconds_t;
/* typedef __kernel_timer_t timer_t; */
typedef int timer_t;
+typedef __kernel_clockid_t clockid_t;
typedef __kernel_uid32_t uid_t;
typedef __kernel_gid32_t gid_t;
diff --git a/usr/klibc/Kbuild b/usr/klibc/Kbuild
index 02a18e64..8b132190 100644
--- a/usr/klibc/Kbuild
+++ b/usr/klibc/Kbuild
@@ -29,7 +29,8 @@ klib-y += vsnprintf.o snprintf.o vsprintf.o sprintf.o \
statfs.o fstatfs.o umount.o \
creat.o open.o openat.o \
fread2.o fwrite2.o fgets.o fputc.o fputs.o puts.o putchar.o \
- sleep.o usleep.o strtotimespec.o strtotimeval.o \
+ clock_nanosleep.o nanosleep.o sleep.o usleep.o \
+ strtotimespec.o strtotimeval.o \
raise.o abort.o assert.o alarm.o pause.o \
__signal.o sysv_signal.o bsd_signal.o siglist.o sigabbrev.o \
siglongjmp.o \
@@ -53,7 +54,8 @@ klib-y += vsnprintf.o snprintf.o vsprintf.o sprintf.o \
clearenv.o nullenv.o \
getopt.o getopt_long.o readdir.o scandir.o alphasort.o remove.o \
syslog.o closelog.o pty.o isatty.o reboot.o \
- time.o lseek.o nice.o getpriority.o \
+ gettimeofday.o settimeofday.o time.o \
+ lseek.o nice.o getpriority.o \
futimesat.o utime.o utimes.o \
qsort.o bsearch.o \
lrand48.o jrand48.o mrand48.o nrand48.o srand48.o seed48.o \
diff --git a/usr/klibc/SYSCALLS.def b/usr/klibc/SYSCALLS.def
index ad8eefa4..d46bb548 100644
--- a/usr/klibc/SYSCALLS.def
+++ b/usr/klibc/SYSCALLS.def
@@ -201,11 +201,12 @@ int setitimer(int, const struct itimerval *, struct itimerval *);
/*
* Time-related system calls
*/
-<?> time_t time(time_t *);
clock_t times(struct tms *);
-int gettimeofday(struct timeval *, struct timezone *);
-int settimeofday(const struct timeval *, const struct timezone *);
-int nanosleep(const struct timespec *, struct timespec *);
+int gettimeofday::__gettimeofday(void *, struct timezone *);
+int settimeofday::__settimeofday(const void *, const struct timezone *);
+int clock_gettime(clockid_t, struct timespec *);
+int clock_settime(clockid_t, const struct timespec *);
+int clock_nanosleep::__clock_nanosleep(clockid_t, int, const struct timespec *, struct timespec *);
<?> int pause();
/*
diff --git a/usr/klibc/clock_nanosleep.c b/usr/klibc/clock_nanosleep.c
new file mode 100644
index 00000000..b4fd1e2c
--- /dev/null
+++ b/usr/klibc/clock_nanosleep.c
@@ -0,0 +1,17 @@
+#include <time.h>
+#include <sys/time.h>
+#include <sys/syscall.h>
+
+extern int __clock_nanosleep(clockid_t, int,
+ const struct timespec *, struct timespec *);
+
+/*
+ * POSIX says this has to return a positive error code, but the system
+ * call returns error codes in the usual way.
+ */
+int clock_nanosleep(clockid_t clock_id, int flags,
+ const struct timespec *request, struct timespec *remain)
+{
+ return __clock_nanosleep(clock_id, flags, request, remain) ?
+ errno : 0;
+}
diff --git a/usr/klibc/gettimeofday.c b/usr/klibc/gettimeofday.c
new file mode 100644
index 00000000..919c46df
--- /dev/null
+++ b/usr/klibc/gettimeofday.c
@@ -0,0 +1,22 @@
+#include <time.h>
+#include <sys/time.h>
+#include <sys/syscall.h>
+
+extern int __gettimeofday(void *, struct timezone *);
+
+int gettimeofday(struct timeval *tv, struct timezone *tz)
+{
+ struct timespec ts;
+
+ if (tv) {
+ if (clock_gettime(CLOCK_REALTIME, &ts))
+ return -1;
+ tv->tv_sec = ts.tv_sec;
+ tv->tv_usec = ts.tv_nsec / 1000;
+ }
+
+ if (tz && __gettimeofday(NULL, tz))
+ return -1;
+
+ return 0;
+}
diff --git a/usr/klibc/nanosleep.c b/usr/klibc/nanosleep.c
new file mode 100644
index 00000000..11412196
--- /dev/null
+++ b/usr/klibc/nanosleep.c
@@ -0,0 +1,8 @@
+#include <time.h>
+#include <sys/time.h>
+#include <sys/syscall.h>
+
+int nanosleep(const struct timespec *request, struct timespec *remain)
+{
+ return clock_nanosleep(CLOCK_MONOTONIC, 0, request, remain) ? -1 : 0;
+}
diff --git a/usr/klibc/settimeofday.c b/usr/klibc/settimeofday.c
new file mode 100644
index 00000000..75754db3
--- /dev/null
+++ b/usr/klibc/settimeofday.c
@@ -0,0 +1,22 @@
+#include <time.h>
+#include <sys/time.h>
+#include <sys/syscall.h>
+
+extern int __settimeofday(const void *, const struct timezone *);
+
+int settimeofday(const struct timeval *tv, const struct timezone *tz)
+{
+ struct timespec ts;
+
+ if (tz && __settimeofday(NULL, tz))
+ return -1;
+
+ if (tv) {
+ ts.tv_sec = tv->tv_sec;
+ ts.tv_nsec = tv->tv_usec * 1000;
+ if (clock_settime(CLOCK_REALTIME, &ts))
+ return -1;
+ }
+
+ return 0;
+}
diff --git a/usr/klibc/time.c b/usr/klibc/time.c
index a0c366b9..e8129316 100644
--- a/usr/klibc/time.c
+++ b/usr/klibc/time.c
@@ -6,8 +6,6 @@
#include <sys/time.h>
#include <sys/syscall.h>
-#ifndef __NR_time
-
time_t time(time_t * t)
{
struct timeval tv;
@@ -19,5 +17,3 @@ time_t time(time_t * t)
return (time_t) tv.tv_sec;
}
-
-#endif
More information about the klibc
mailing list