[v5,2/3] y2038: Provide conversion helpers for struct __timespec64
Commit Message
Those functions allow easy conversion between Y2038 safe struct
__timespec64 and other time related data structures (like struct timeval).
Moreover, those functions are NOT compiled when 64 bit time support is
enabled (i.e. the __ASSUME_TIME64_SYSCALLS is defined) and are used only
in 32 bit wrappers (like e.g. __clock_settime()).
* include/time.h (valid_timeval_to_timespec64): Add.
* include/time.h (valid_timespec_to_timespec64): Likewise.
* include/time.h (valid_timespec64_to_timespec): Likewise.
* include/time.h (valid_timespec64_to_timeval): Likewise.
* include/time.h (IS_VALID_NANOSECONDS): Likewise.
* include/time.h (timespec_to_timespec64): Likewise.
* include/time.h (timespec64_to_timespec): Likewise.
* include/time.h (timespec64_to_timeval): Likewise.
---
Changes for v5:
- This code is now only compiled in when __ASSUME_TIME64_SYSCALLS is NOT
defined. Previously it was depending on #if __TIMESIZE != 64.
Changes for v4:
- None
Changes for v3:
- Remove misleading comments regarding clearing tv_pad values during
conversion (as Linux kernel on its own ignores upper 32 bits of tv_nsec).
Changes for v3:
- Remove timespec64_clear_padding function - as kernel ignores upper 32
bits of tv_nsec when passed via syscall to the Linux kernel
Changes for v2:
- Add timespec64_clear_padding function
---
include/time.h | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 85 insertions(+)
Comments
29.05.2019 в 18:18:49 +0200 Lukasz Majewski написал:
> Those functions allow easy conversion between Y2038 safe struct
> __timespec64 and other time related data structures (like struct timeval).
>
> Moreover, those functions are NOT compiled when 64 bit time support is
> enabled (i.e. the __ASSUME_TIME64_SYSCALLS is defined) and are used only
> in 32 bit wrappers (like e.g. __clock_settime()).
> Changes for v5:
> - This code is now only compiled in when __ASSUME_TIME64_SYSCALLS is NOT
> defined. Previously it was depending on #if __TIMESIZE != 64.
That's wrong. valid_timespec_to_timespec64 is used when __TIMESIZE !=
64. Even when __ASSUME_TIME64_SYSCALLS is defined.
And when __ASSUME_TIME64_SYSCALLS is not defined __TIMESIZE should be
32. (Supporting __TIMESIZE == 64 without __ASSUME_TIME64_SYSCALLS is
a bad idea.)
Also, it doesn't look like __ASSUME_TIME64_SYSCALLS can be defined by
any header included from include/time.h.
Hi Stepan,
> 29.05.2019 в 18:18:49 +0200 Lukasz Majewski написал:
> > Those functions allow easy conversion between Y2038 safe struct
> > __timespec64 and other time related data structures (like struct
> > timeval).
> >
> > Moreover, those functions are NOT compiled when 64 bit time support
> > is enabled (i.e. the __ASSUME_TIME64_SYSCALLS is defined) and are
> > used only in 32 bit wrappers (like e.g. __clock_settime()).
>
>
> > Changes for v5:
> > - This code is now only compiled in when __ASSUME_TIME64_SYSCALLS
> > is NOT defined. Previously it was depending on #if __TIMESIZE !=
> > 64.
>
> That's wrong. valid_timespec_to_timespec64 is used when __TIMESIZE !=
> 64. Even when __ASSUME_TIME64_SYSCALLS is defined.
Hmm.. Then I think that I should have kept [1]:
#if __TIMESIZE != 64
(helper functions)
#endif
>
> And when __ASSUME_TIME64_SYSCALLS is not defined __TIMESIZE should be
> 32. (Supporting __TIMESIZE == 64 without __ASSUME_TIME64_SYSCALLS is
> a bad idea.)
The above assumption is implicit. When __ASSUME_TIME64_SYSCALLS is
defined 64 bit versions of syscalls are used and hence we don't need
the conversion.
Shall I add more formalism here or just use #ifdef from [1] ?
>
> Also, it doesn't look like __ASSUME_TIME64_SYSCALLS can be defined by
> any header included from include/time.h.
Ok.
Best regards,
Lukasz Majewski
--
DENX Software Engineering GmbH, Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-59 Fax: (+49)-8142-66989-80 Email: lukma@denx.de
@@ -181,5 +181,90 @@ in_time_t_range (__time64_t t)
return s == t;
}
+# ifndef __ASSUME_TIME64_SYSCALLS
+/* Convert a known valid struct timeval into a struct __timespec64. */
+static inline void
+valid_timeval_to_timespec64 (const struct timeval *tv32,
+ struct __timespec64 *ts64)
+{
+ ts64->tv_sec = tv32->tv_sec;
+ ts64->tv_nsec = tv32->tv_usec * 1000;
+}
+
+/* Convert a known valid struct timespec into a struct __timespec64. */
+static inline void
+valid_timespec_to_timespec64 (const struct timespec *ts32,
+ struct __timespec64 *ts64)
+{
+ ts64->tv_sec = ts32->tv_sec;
+ ts64->tv_nsec = ts32->tv_nsec;
+}
+
+/* Convert a known valid struct __timespec64 into a struct timespec. */
+static inline void
+valid_timespec64_to_timespec (const struct __timespec64 *ts64,
+ struct timespec *ts32)
+{
+ ts32->tv_sec = (time_t) ts64->tv_sec;
+ ts32->tv_nsec = ts64->tv_nsec;
+}
+
+/* Convert a known valid struct __timespec64 into a struct timeval. */
+static inline void
+valid_timespec64_to_timeval (const struct __timespec64 *ts64,
+ struct timeval *tv32)
+{
+ tv32->tv_sec = (time_t) ts64->tv_sec;
+ tv32->tv_usec = ts64->tv_nsec / 1000;
+}
+
+/* Check if a value lies with the valid nanoseconds range. */
+#define IS_VALID_NANOSECONDS(ns) ((ns) >= 0 && (ns) <= 999999999)
+
+/* Check and convert a struct timespec into a struct __timespec64. */
+static inline bool
+timespec_to_timespec64 (const struct timespec *ts32,
+ struct __timespec64 *ts64)
+{
+ /* Check that ts32 holds a valid count of nanoseconds. */
+ if (! IS_VALID_NANOSECONDS (ts32->tv_nsec))
+ return false;
+ /* All ts32 fields can fit in ts64, so copy them. */
+ valid_timespec_to_timespec64 (ts32, ts64);
+ return true;
+}
+
+/* Check and convert a struct __timespec64 into a struct timespec. */
+static inline bool
+timespec64_to_timespec (const struct __timespec64 *ts64,
+ struct timespec *ts32)
+{
+ /* Check that tv_nsec holds a valid count of nanoseconds. */
+ if (! IS_VALID_NANOSECONDS (ts64->tv_nsec))
+ return false;
+ /* Check that tv_sec can fit in a __time_t. */
+ if (! in_time_t_range (ts64->tv_sec))
+ return false;
+ /* All ts64 fields can fit in ts32, so copy them. */
+ valid_timespec64_to_timespec (ts64, ts32);
+ return true;
+}
+
+/* Check and convert a struct __timespec64 into a struct timeval. */
+static inline bool
+timespec64_to_timeval (const struct __timespec64 *ts64,
+ struct timeval *tv32)
+{
+ /* Check that tv_nsec holds a valid count of nanoseconds. */
+ if (! IS_VALID_NANOSECONDS (ts64->tv_nsec))
+ return false;
+ /* Check that tv_sec can fit in a __time_t. */
+ if (! in_time_t_range (ts64->tv_sec))
+ return false;
+ /* All ts64 fields can fit in tv32, so copy them. */
+ valid_timespec64_to_timeval (ts64, tv32);
+ return true;
+}
+# endif
#endif
#endif