Skip to content

Commit

Permalink
y2038: linux: Provide __clock_settime64 implementation
Browse files Browse the repository at this point in the history
This patch provides new __clock_settime64 explicit 64 bit function for
setting the time. Moreover, a 32 bit version - __clock_settime has been
refactored to internally use __clock_settime64.

The __clock_settime is now supposed to be used on 32 bit systems -
hence the necessary checks and conversion to 64 bit type. After this
change it is intrinsically Y2038 safe.

The new 64 bit syscall (clock_settime64) available from Linux
5.1+ has been used when applicable on 32 bit systems.
The execution path on 64 bit systems has not been changed or affected in
any way.

Tests:
- The code has been tested with x86_64/x86 (native compilation):
make PARALLELMFLAGS="-j8" && make xcheck PARALLELMFLAGS="-j8"

- Run specific tests on ARM/x86 32bit systems (qemu):
https://github.com/lmajewski/meta-y2038
and run tests:
https://github.com/lmajewski/y2038-tests/commits/master
on kernels with and without 64 bit time support.

No regressions were observed.

* include/time.h (__clock_settime64):
  Add __clock_settime alias according to __TIMESIZE define
* sysdeps/unix/sysv/linux/clock_settime.c (__clock_settime):
  Refactor this function to be used only on 32 bit machines as a wrapper
  on __clock_settime64.
* sysdeps/unix/sysv/linux/clock_settime.c (__clock_settime64): Add
* sysdeps/unix/sysv/linux/clock_settime.c (__clock_settime64):
  Use clock_settime64 kernel syscall (available from 5.1-rc1+ Linux) by
  32 bit Y2038 safe systems
  • Loading branch information
Lukasz Majewski committed Apr 14, 2019
1 parent 1bbf816 commit 80f3b62
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
8 changes: 8 additions & 0 deletions include/time.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,14 @@ extern __time64_t __timegm64 (struct tm *__tp) __THROW;
libc_hidden_proto (__timegm64)
#endif

#if __TIMESIZE == 64
# define __clock_settime64 __clock_settime
#else
extern int __clock_settime64 (clockid_t clock_id,
const struct __timespec64 *tp);
libc_hidden_proto (__clock_settime64)
#endif

/* Compute the `struct tm' representation of T,
offset OFFSET seconds east of UTC,
and store year, yday, mon, mday, wday, hour, min, sec into *TP.
Expand Down
34 changes: 31 additions & 3 deletions sysdeps/unix/sysv/linux/clock_settime.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@
#include <sysdep.h>
#include <time.h>

#include "kernel-posix-cpu-timers.h"

/* Set CLOCK to value TP. */
int
__clock_settime (clockid_t clock_id, const struct timespec *tp)
__clock_settime64 (clockid_t clock_id, const struct __timespec64 *tp)
{
/* Make sure the time cvalue is OK. */
if (tp->tv_nsec < 0 || tp->tv_nsec >= 1000000000)
Expand All @@ -32,6 +30,36 @@ __clock_settime (clockid_t clock_id, const struct timespec *tp)
return -1;
}

#if defined (__TIMESIZE) && __TIMESIZE != 64
# if defined __NR_clock_settime64
/* Make sure that passed __timespec64 struct pad is 0. */
struct __timespec64 ts = *tp;
ts.tv_pad = 0;
return INLINE_SYSCALL_CALL (clock_settime64, clock_id, &ts);
# else
struct timespec ts32;
valid_timespec64_to_timespec(tp, &ts32);
return INLINE_SYSCALL_CALL (clock_settime, clock_id, &ts32);
# endif
#else
return INLINE_SYSCALL_CALL (clock_settime, clock_id, tp);
#endif
}
weak_alias (__clock_settime, clock_settime)

#if __TIMESIZE != 64
int
__clock_settime (clockid_t clock_id, const struct timespec *tp)
{
struct __timespec64 ts64;

if (! in_time_t_range (tp->tv_sec))
{
__set_errno (EOVERFLOW);
return -1;
}

valid_timespec_to_timespec64 (tp, &ts64);
return __clock_settime64 (clock_id, &ts64);
}
#endif

0 comments on commit 80f3b62

Please sign in to comment.