Browse Source

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.
Rich Felker 4 years ago
parent
commit
2ab90de7ac
2 changed files with 34 additions and 0 deletions
  1. 17 0
      src/mq/mq_timedreceive.c
  2. 17 0
      src/mq/mq_timedsend.c

+ 17 - 0
src/mq/mq_timedreceive.c

@@ -1,7 +1,24 @@
 #include <mqueue.h>
+#include <errno.h>
 #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
 }

+ 17 - 0
src/mq/mq_timedsend.c

@@ -1,7 +1,24 @@
 #include <mqueue.h>
+#include <errno.h>
 #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
 }