From: Lukasz Majewski <lukma@denx.de>
To: Joseph Myers <joseph@codesourcery.com>,
Alistair Francis <alistair23@gmail.com>,
Alistair Francis <alistair.francis@wdc.com>,
Zack Weinberg <zackw@panix.com>
Cc: Arnd Bergmann <arnd@arndb.de>,
GNU C Library <libc-alpha@sourceware.org>,
Adhemerval Zanella <adhemerval.zanella@linaro.org>,
Florian Weimer <fweimer@redhat.com>,
Carlos O'Donell <carlos@redhat.com>,
Stepan Golosunov <stepan@golosunov.pp.ru>,
Lukasz Majewski <lukma@denx.de>
Subject: [PATCH v8 2/3] y2038: Provide conversion helpers for struct __timespec64
Date: Wed, 18 Sep 2019 21:16:00 -0000 [thread overview]
Message-ID: <20190918211603.8444-3-lukma@denx.de> (raw)
In-Reply-To: <20190918211603.8444-1-lukma@denx.de>
Those functions allow easy conversion between Y2038 safe struct
__timespec64 and other time related data structures (like struct timeval).
* 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 v8:
- None
Changes for v7:
- None
Changes for v6:
- Remove the #ifdef guard on __ASSUME_TIME64_SYSCALLS as those functions
may be needed for fallback execution paths (on e.g. __clock_settime64).
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 | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 83 insertions(+)
diff --git a/include/time.h b/include/time.h
index 9792948744..64b5468115 100644
--- a/include/time.h
+++ b/include/time.h
@@ -178,5 +178,88 @@ in_time_t_range (__time64_t t)
return s == t;
}
+/* 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
--
2.20.1
next prev parent reply other threads:[~2019-09-18 21:16 UTC|newest]
Thread overview: 28+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-09-18 21:16 [PATCH v8 0/3] y2038: Linux: Introduce __clock_settime64 function Lukasz Majewski
2019-09-18 21:16 ` Lukasz Majewski [this message]
2019-09-19 20:17 ` [PATCH v8 2/3] y2038: Provide conversion helpers for struct __timespec64 Joseph Myers
2019-09-19 21:21 ` Lukasz Majewski
2019-09-19 21:29 ` Joseph Myers
2019-09-19 22:03 ` Lukasz Majewski
2019-09-19 22:17 ` Joseph Myers
2019-09-19 22:22 ` Lukasz Majewski
2019-09-18 21:16 ` [PATCH v8 3/3] y2038: linux: Provide __clock_settime64 implementation Lukasz Majewski
2019-09-18 21:43 ` Joseph Myers
2019-09-18 22:34 ` Lukasz Majewski
2019-09-19 22:01 ` Lukasz Majewski
2019-09-18 21:16 ` [PATCH v8 1/3] y2038: Introduce internal for glibc struct __timespec64 Lukasz Majewski
2019-09-19 20:14 ` Joseph Myers
2019-09-23 21:22 ` Lukasz Majewski
2019-09-25 0:47 ` Joseph Myers
2019-09-25 7:45 ` Lukasz Majewski
2019-09-25 12:51 ` Florian Weimer
2019-09-25 13:34 ` Lukasz Majewski
2019-09-25 13:40 ` Florian Weimer
2019-09-25 14:38 ` Lukasz Majewski
2019-09-25 16:29 ` Joseph Myers
2019-09-25 20:03 ` Lukasz Majewski
2019-09-25 12:43 ` Florian Weimer
2019-09-25 13:06 ` Lukasz Majewski
2019-09-25 13:07 ` Florian Weimer
2019-09-18 23:37 ` [PATCH v8 0/3] y2038: Linux: Introduce __clock_settime64 function Alistair Francis
2019-09-19 7:51 ` Lukasz Majewski
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20190918211603.8444-3-lukma@denx.de \
--to=lukma@denx.de \
--cc=adhemerval.zanella@linaro.org \
--cc=alistair.francis@wdc.com \
--cc=alistair23@gmail.com \
--cc=arnd@arndb.de \
--cc=carlos@redhat.com \
--cc=fweimer@redhat.com \
--cc=joseph@codesourcery.com \
--cc=libc-alpha@sourceware.org \
--cc=stepan@golosunov.pp.ru \
--cc=zackw@panix.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).