From patchwork Wed Apr 18 20:17:39 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Albert ARIBAUD X-Patchwork-Id: 26802 Received: (qmail 110057 invoked by alias); 18 Apr 2018 20:20:17 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 109958 invoked by uid 89); 18 Apr 2018 20:20:16 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-24.8 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_LAZY_DOMAIN_SECURITY, KAM_NUMSUBJECT, RCVD_IN_DNSWL_LOW, RCVD_IN_RP_RNBL autolearn=ham version=3.3.2 spammy=999999 X-HELO: smtp3-g21.free.fr From: "Albert ARIBAUD (3ADEV)" To: libc-alpha@sourceware.org Cc: "Albert ARIBAUD (3ADEV)" Subject: [[PATCH RFC 2] 23/63] Y2038: add struct __timeval64 Date: Wed, 18 Apr 2018 22:17:39 +0200 Message-Id: <20180418201819.15952-24-albert.aribaud@3adev.fr> In-Reply-To: <20180418201819.15952-23-albert.aribaud@3adev.fr> References: <20180418201819.15952-1-albert.aribaud@3adev.fr> <20180418201819.15952-2-albert.aribaud@3adev.fr> <20180418201819.15952-3-albert.aribaud@3adev.fr> <20180418201819.15952-4-albert.aribaud@3adev.fr> <20180418201819.15952-5-albert.aribaud@3adev.fr> <20180418201819.15952-6-albert.aribaud@3adev.fr> <20180418201819.15952-7-albert.aribaud@3adev.fr> <20180418201819.15952-8-albert.aribaud@3adev.fr> <20180418201819.15952-9-albert.aribaud@3adev.fr> <20180418201819.15952-10-albert.aribaud@3adev.fr> <20180418201819.15952-11-albert.aribaud@3adev.fr> <20180418201819.15952-12-albert.aribaud@3adev.fr> <20180418201819.15952-13-albert.aribaud@3adev.fr> <20180418201819.15952-14-albert.aribaud@3adev.fr> <20180418201819.15952-15-albert.aribaud@3adev.fr> <20180418201819.15952-16-albert.aribaud@3adev.fr> <20180418201819.15952-17-albert.aribaud@3adev.fr> <20180418201819.15952-18-albert.aribaud@3adev.fr> <20180418201819.15952-19-albert.aribaud@3adev.fr> <20180418201819.15952-20-albert.aribaud@3adev.fr> <20180418201819.15952-21-albert.aribaud@3adev.fr> <20180418201819.15952-22-albert.aribaud@3adev.fr> <20180418201819.15952-23-albert.aribaud@3adev.fr> 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 --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