Skip to content

Commit

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

The __settimeofday is now supposed to be used on systems still supporting 32
bit time (__TIMESIZE != 64) - hence the necessary conversion of struct
timeval to 64 bit struct __timespec64.

Internally the settimeofday uses __settimeofday64. This patch is necessary
for having architectures with __WORDSIZE == 32 Y2038 safe.

Build tests:
./src/scripts/build-many-glibcs.py glibcs

Run-time tests:
- 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

Above tests were performed with Y2038 redirection applied as well as without
to test proper usage of both __settimeofday64 and __settimeofday.
  • Loading branch information
Lukasz Majewski committed Jan 18, 2020
1 parent 25c928a commit 2328b06
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
9 changes: 9 additions & 0 deletions include/time.h
Expand Up @@ -8,6 +8,7 @@
# include <time/mktime-internal.h>
# include <endian.h>
# include <time-clockid.h>
# include <sys/time.h>

extern __typeof (strftime_l) __strftime_l;
libc_hidden_proto (__strftime_l)
Expand Down Expand Up @@ -224,6 +225,14 @@ extern int __sched_rr_get_interval64 (pid_t pid, struct __timespec64 *tp);
libc_hidden_proto (__sched_rr_get_interval64);
#endif

#if __TIMESIZE == 64
# define __settimeofday64 __settimeofday
#else
extern int __settimeofday64 (const struct __timeval64 *tv,
const struct timezone *tz);
libc_hidden_proto (__settimeofday64)
#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
19 changes: 15 additions & 4 deletions time/settimeofday.c
Expand Up @@ -22,7 +22,7 @@
/* Set the current time of day and timezone information.
This call is restricted to the super-user. */
int
__settimeofday (const struct timeval *tv, const struct timezone *tz)
__settimeofday64 (const struct __timeval64 *tv, const struct timezone *tz)
{
if (__glibc_unlikely (tz != 0))
{
Expand All @@ -34,11 +34,22 @@ __settimeofday (const struct timeval *tv, const struct timezone *tz)
return __settimezone (tz);
}

struct timespec ts;
TIMEVAL_TO_TIMESPEC (tv, &ts);
return __clock_settime (CLOCK_REALTIME, &ts);
struct __timespec64 ts = timeval64_to_timespec64 (*tv);
return __clock_settime64 (CLOCK_REALTIME, &ts);
}

#if __TIMESIZE != 64
libc_hidden_def (__settimeofday64)

int
__settimeofday (const struct timeval *tv, const struct timezone *tz)
{
struct __timeval64 tv64 = valid_timeval_to_timeval64 (*tv);

return __settimeofday64 (&tv64, tz);
}
#endif

#ifdef VERSION_settimeofday
weak_alias (__settimeofday, __settimeofday_w);
default_symbol_version (__settimeofday_w, settimeofday, VERSION_settimeofday);
Expand Down

0 comments on commit 2328b06

Please sign in to comment.