clock_nanosleep: add time64 syscall support, decouple 32-bit time_t

time64 syscall is used only if it's the only one defined for the arch,
or if the requested time does not fit in 32 bits. on current 32-bit
archs where time_t is a 32-bit type, this makes it statically
unreachable.

on 64-bit archs, there is no change to the code after preprocessing.
on current 32-bit archs, the time is moved through an intermediate
copy to remove the assumption that time_t is a 32-bit type.
This commit is contained in:
Rich Felker 2019-07-28 15:08:34 -04:00
parent 2c2c3605d3
commit 1334e36680
1 changed files with 25 additions and 0 deletions

View File

@ -2,12 +2,37 @@
#include <errno.h> #include <errno.h>
#include "syscall.h" #include "syscall.h"
#define IS32BIT(x) !((x)+0x80000000ULL>>32)
#define CLAMP(x) (int)(IS32BIT(x) ? (x) : 0x7fffffffU+((0ULL+(x))>>63))
int __clock_nanosleep(clockid_t clk, int flags, const struct timespec *req, struct timespec *rem) int __clock_nanosleep(clockid_t clk, int flags, const struct timespec *req, struct timespec *rem)
{ {
if (clk == CLOCK_THREAD_CPUTIME_ID) return EINVAL; if (clk == CLOCK_THREAD_CPUTIME_ID) return EINVAL;
#ifdef SYS_clock_nanosleep_time64
time_t s = req->tv_sec;
long ns = req->tv_nsec;
int r = -ENOSYS;
if (SYS_clock_nanosleep == SYS_clock_nanosleep_time64 || !IS32BIT(s))
r = __syscall_cp(SYS_clock_nanosleep_time64, clk, flags,
((long long[]){s, ns}), rem);
if (SYS_clock_nanosleep == SYS_clock_nanosleep_time64 || r!=-ENOSYS)
return -r;
long long extra = s - CLAMP(s);
long ts32[2] = { CLAMP(s), ns };
if (clk == CLOCK_REALTIME && !flags)
r = __syscall_cp(SYS_nanosleep, &ts32, &ts32);
else
r = __syscall_cp(SYS_clock_nanosleep, clk, flags, &ts32, &ts32);
if (r==-EINTR && rem && !(flags & TIMER_ABSTIME)) {
rem->tv_sec = ts32[0] + extra;
rem->tv_nsec = ts32[1];
}
return -r;
#else
if (clk == CLOCK_REALTIME && !flags) if (clk == CLOCK_REALTIME && !flags)
return -__syscall_cp(SYS_nanosleep, req, rem); return -__syscall_cp(SYS_nanosleep, req, rem);
return -__syscall_cp(SYS_clock_nanosleep, clk, flags, req, rem); return -__syscall_cp(SYS_clock_nanosleep, clk, flags, req, rem);
#endif
} }
weak_alias(__clock_nanosleep, clock_nanosleep); weak_alias(__clock_nanosleep, clock_nanosleep);