1 #include <signal.h>
2 #include <errno.h>
3 #include "syscall.h"
4 
5 #define IS32BIT(x) !((x)+0x80000000ULL>>32)
6 #define CLAMP(x) (int)(IS32BIT(x) ? (x) : 0x7fffffffU+((0ULL+(x))>>63))
7 
do_sigtimedwait(const sigset_t * restrict mask,siginfo_t * restrict si,const struct timespec * restrict ts)8 static int do_sigtimedwait(const sigset_t *restrict mask, siginfo_t *restrict si, const struct timespec *restrict ts)
9 {
10 #ifdef SYS_rt_sigtimedwait_time64
11 	time_t s = ts ? ts->tv_sec : 0;
12 	long ns = ts ? ts->tv_nsec : 0;
13 	int r = -ENOSYS;
14 	if (SYS_rt_sigtimedwait == SYS_rt_sigtimedwait_time64 || !IS32BIT(s))
15 		r = __syscall_cp(SYS_rt_sigtimedwait_time64, mask, si,
16 			ts ? ((long long[]){s, ns}) : 0, _NSIG/8);
17 	if (SYS_rt_sigtimedwait == SYS_rt_sigtimedwait_time64 || r!=-ENOSYS)
18 		return r;
19 	return __syscall_cp(SYS_rt_sigtimedwait, mask, si,
20 		ts ? ((long[]){CLAMP(s), ns}) : 0, _NSIG/8);;
21 #else
22 	return __syscall_cp(SYS_rt_sigtimedwait, mask, si, ts, _NSIG/8);
23 #endif
24 }
25 
sigtimedwait(const sigset_t * restrict mask,siginfo_t * restrict si,const struct timespec * restrict timeout)26 int sigtimedwait(const sigset_t *restrict mask, siginfo_t *restrict si, const struct timespec *restrict timeout)
27 {
28 	int ret;
29 	do ret = do_sigtimedwait(mask, si, timeout);
30 	while (ret==-EINTR);
31 	return __syscall_ret(ret);
32 }
33