1/* Copyright (C) 2010-2021 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Andreas Schwab <schwab@redhat.com>, 2010.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19#include <sys/socket.h>
20#include <sysdep.h>
21#include <socketcall.h>
22
23int
24__recvmmsg64 (int fd, struct mmsghdr *vmessages, unsigned int vlen, int flags,
25 struct __timespec64 *timeout)
26{
27#ifndef __NR_recvmmsg_time64
28# define __NR_recvmmsg_time64 __NR_recvmmsg
29#endif
30 int r = SYSCALL_CANCEL (recvmmsg_time64, fd, vmessages, vlen, flags,
31 timeout);
32#ifndef __ASSUME_TIME64_SYSCALLS
33 if (r >= 0 || errno != ENOSYS)
34 return r;
35
36 struct timespec ts32, *pts32 = NULL;
37 if (timeout != NULL)
38 {
39 if (! in_time_t_range (timeout->tv_sec))
40 {
41 __set_errno (EINVAL);
42 return -1;
43 }
44 ts32 = valid_timespec64_to_timespec (*timeout);
45 pts32 = &ts32;
46 }
47
48 socklen_t csize[IOV_MAX];
49 if (vlen > IOV_MAX)
50 vlen = IOV_MAX;
51 for (int i = 0; i < vlen; i++)
52 csize[i] = vmessages[i].msg_hdr.msg_controllen;
53
54# ifdef __ASSUME_RECVMMSG_SYSCALL
55 r = SYSCALL_CANCEL (recvmmsg, fd, vmessages, vlen, flags, pts32);
56# else
57 r = SOCKETCALL_CANCEL (recvmmsg, fd, vmessages, vlen, flags, pts32);
58# endif
59 if (r >= 0)
60 {
61 if (timeout != NULL)
62 *timeout = valid_timespec_to_timespec64 (ts32);
63
64 for (int i=0; i < r; i++)
65 __convert_scm_timestamps (&vmessages[i].msg_hdr, csize[i]);
66 }
67#endif /* __ASSUME_TIME64_SYSCALLS */
68 return r;
69}
70#if __TIMESIZE != 64
71libc_hidden_def (__recvmmsg64)
72
73int
74__recvmmsg (int fd, struct mmsghdr *vmessages, unsigned int vlen, int flags,
75 struct timespec *timeout)
76{
77 struct __timespec64 ts64, *pts64 = NULL;
78 if (timeout != NULL)
79 {
80 ts64 = valid_timespec_to_timespec64 (*timeout);
81 pts64 = &ts64;
82 }
83 int r = __recvmmsg64 (fd, vmessages, vlen, flags, pts64);
84 if (r >= 0 && timeout != NULL)
85 /* The remanining timeout will be always less the input TIMEOUT. */
86 *timeout = valid_timespec64_to_timespec (ts64);
87 return r;
88}
89#endif
90weak_alias (__recvmmsg, recvmmsg)
91