From 2ab90de7ac53317a63d999b3e26d44d66684464c Mon Sep 17 00:00:00 2001 From: Rich Felker Date: Sun, 28 Jul 2019 15:49:10 -0400 Subject: [PATCH] mq_timedsend, mq_timedreceive: add time64, decouple 32-bit time_t time64 syscall is used only if it's the only one defined for the arch, or if the requested absolute timeout does not fit in 32 bits. on current 32-bit archs where time_t is a 32-bit type, this makes it statically unreachable. on 64-bit archs, there is no change to the code after preprocessing. on current 32-bit archs, the timeout is passed via an intermediate copy to remove the assumption that time_t is a 32-bit type. --- src/mq/mq_timedreceive.c | 17 +++++++++++++++++ src/mq/mq_timedsend.c | 17 +++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/src/mq/mq_timedreceive.c b/src/mq/mq_timedreceive.c index 2cef6a86..f41b6642 100644 --- a/src/mq/mq_timedreceive.c +++ b/src/mq/mq_timedreceive.c @@ -1,7 +1,24 @@ #include +#include #include "syscall.h" +#define IS32BIT(x) !((x)+0x80000000ULL>>32) +#define CLAMP(x) (int)(IS32BIT(x) ? (x) : 0x7fffffffU+((0ULL+(x))>>63)) + ssize_t mq_timedreceive(mqd_t mqd, char *restrict msg, size_t len, unsigned *restrict prio, const struct timespec *restrict at) { +#ifdef SYS_mq_timedreceive_time64 + time_t s = at ? at->tv_sec : 0; + long ns = at ? at->tv_nsec : 0; + long r = -ENOSYS; + if (SYS_mq_timedreceive == SYS_mq_timedreceive_time64 || !IS32BIT(s)) + r = __syscall_cp(SYS_mq_timedreceive_time64, mqd, msg, len, prio, + at ? ((long long []){at->tv_sec, at->tv_nsec}) : 0); + if (SYS_mq_timedreceive == SYS_mq_timedreceive_time64 || r != -ENOSYS) + return __syscall_ret(r); + return syscall_cp(SYS_mq_timedreceive, mqd, msg, len, prio, + at ? ((long[]){CLAMP(s), ns}) : 0); +#else return syscall_cp(SYS_mq_timedreceive, mqd, msg, len, prio, at); +#endif } diff --git a/src/mq/mq_timedsend.c b/src/mq/mq_timedsend.c index 1c00aa0b..56cfcbb8 100644 --- a/src/mq/mq_timedsend.c +++ b/src/mq/mq_timedsend.c @@ -1,7 +1,24 @@ #include +#include #include "syscall.h" +#define IS32BIT(x) !((x)+0x80000000ULL>>32) +#define CLAMP(x) (int)(IS32BIT(x) ? (x) : 0x7fffffffU+((0ULL+(x))>>63)) + int mq_timedsend(mqd_t mqd, const char *msg, size_t len, unsigned prio, const struct timespec *at) { +#ifdef SYS_mq_timedsend_time64 + time_t s = at ? at->tv_sec : 0; + long ns = at ? at->tv_nsec : 0; + long r = -ENOSYS; + if (SYS_mq_timedsend == SYS_mq_timedsend_time64 || !IS32BIT(s)) + r = __syscall_cp(SYS_mq_timedsend_time64, mqd, msg, len, prio, + at ? ((long long []){at->tv_sec, at->tv_nsec}) : 0); + if (SYS_mq_timedsend == SYS_mq_timedsend_time64 || r != -ENOSYS) + return __syscall_ret(r); + return syscall_cp(SYS_mq_timedsend, mqd, msg, len, prio, + at ? ((long[]){CLAMP(s), ns}) : 0); +#else return syscall_cp(SYS_mq_timedsend, mqd, msg, len, prio, at); +#endif } -- 2.25.1