diff mbox

[[PATCH,RFC,2] 23/63] Y2038: add struct __timeval64

Message ID 20180418201819.15952-24-albert.aribaud@3adev.fr
State New
Headers show

Commit Message

Albert ARIBAUD April 18, 2018, 8:17 p.m. UTC
Also, provide static inline functions and macros for checking
and converting between 32-bit and 64-bit timevals.
---
 include/time.h                   | 50 ++++++++++++++++++++++++++++++++++++++++
 time/bits/types/struct_timeval.h |  8 +++++++
 2 files changed, 58 insertions(+)
diff mbox

Patch

diff --git a/include/time.h b/include/time.h
index 82932f9afd..68c52220fd 100644
--- a/include/time.h
+++ b/include/time.h
@@ -194,5 +194,55 @@  static inline bool timespec64_to_timespec(const struct __timespec64 *ts64,
   valid_timespec64_to_timespec(ts64, ts32);
   return true;
 }
+
+/* convert a known valid struct timeval into a struct __timeval64 */
+static inline void valid_timeval_to_timeval64(const struct timeval *tv32,
+                                              struct __timeval64 *tv64)
+{
+  tv64->tv_sec = tv32->tv_sec;
+  tv64->tv_usec = tv32->tv_usec;
+}
+
+/* convert a known valid struct timeval into a struct __timeval64 */
+static inline void valid_timeval64_to_timeval(const struct __timeval64 *tv64,
+					      struct timeval *tv32)
+{
+  tv32->tv_sec = (time_t) tv64->tv_sec;
+  tv32->tv_usec = tv64->tv_usec;
+}
+
+/* check if a struct timeval/__timeval64 is valid */
+#define IS_VALID_TIMEVAL(ts) \
+  ((ts).tv_usec >= 0 && (ts).tv_usec <= 999999)
+
+/* check if a struct timeval/__timeval64 is a valid 32-bit timeval */
+#define IS_VALID_TIMEVAL32(ts) \
+  (fits_in_time_t((ts).tv_sec) && (ts).tv_usec >= 0 && (ts).tv_usec <= 999999)
+
+/* check and convert a struct timeval into a struct __timeval64 */
+static inline bool timeval_to_timeval64(const struct timeval *tv32,
+                                        struct __timeval64 *tv64)
+{
+  /* check that tv_usec holds a valid count of nanoseconds */
+  if (! IS_VALID_TIMEVAL(*tv32))
+    return false;
+  /* all ts32 fields can fit in ts64, so copy them */
+  valid_timeval_to_timeval64(tv32, tv64);
+  /* we will only zero ts64->tv_pad if we pass it to the kernel */
+  return true;
+}
+
+/* check and convert a struct __timeval64 into a struct timeval */
+static inline bool timeval64_to_timeval(const struct __timeval64 *tv64,
+                                        struct timeval *tv32)
+{
+  /* check that tv_usec holds a valid count of nanoseconds */
+  if (! IS_VALID_TIMEVAL32(*tv64))
+    return false;
+  /* all ts64 fields can fit in ts32, so copy them */
+  valid_timeval64_to_timeval(tv64, tv32);
+  return true;
+}
+
 #endif
 #endif
diff --git a/time/bits/types/struct_timeval.h b/time/bits/types/struct_timeval.h
index 70394ce886..13fd48e686 100644
--- a/time/bits/types/struct_timeval.h
+++ b/time/bits/types/struct_timeval.h
@@ -10,4 +10,12 @@  struct timeval
   __time_t tv_sec;		/* Seconds.  */
   __suseconds_t tv_usec;	/* Microseconds.  */
 };
+
+/* 64-bit time version. Here we can simply use 64-bit signed ints and
+   still keep tings Posix-ish. */ 
+struct __timeval64
+{
+  __time64_t tv_sec;		/* Seconds */
+  __int64_t tv_usec;		/* Microseconds */
+};
 #endif