93dc2d
commit 489d0b8b32548bc569cd3067aebf98b030720753
93dc2d
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
93dc2d
Date:   Thu Jan 27 16:45:18 2022 -0300
93dc2d
93dc2d
    Linux: Only generate 64 bit timestamps for 64 bit time_t recvmsg/recvmmsg
93dc2d
    
93dc2d
    The timestamps created by __convert_scm_timestamps only make sense for
93dc2d
    64 bit time_t programs, 32 bit time_t programs will ignore 64 bit time_t
93dc2d
    timestamps since SO_TIMESTAMP will be defined to old values (either by
93dc2d
    glibc or kernel headers).
93dc2d
    
93dc2d
    Worse, if the buffer is not suffice MSG_CTRUNC is set to indicate it
93dc2d
    (which breaks some programs [1]).
93dc2d
    
93dc2d
    This patch makes only 64 bit time_t recvmsg and recvmmsg to call
93dc2d
    __convert_scm_timestamps.  Also, the assumption to called it is changed
93dc2d
    from __ASSUME_TIME64_SYSCALLS to __TIMESIZE != 64 since the setsockopt
93dc2d
    might be called by libraries built without __TIME_BITS=64.  The
93dc2d
    MSG_CTRUNC is only set for the 64 bit symbols, it should happen only
93dc2d
    if 64 bit time_t programs run older kernels.
93dc2d
    
93dc2d
    Checked on x86_64-linux-gnu and i686-linux-gnu.
93dc2d
    
93dc2d
    [1] https://github.com/systemd/systemd/pull/20567
93dc2d
    
93dc2d
    Reviewed-by: Florian Weimer <fweimer@redhat.com>
93dc2d
    
93dc2d
    (cherry picked from commit 948ce73b31fdb0860bcec4b8e62b14e88234f98a)
93dc2d
93dc2d
diff --git a/include/sys/socket.h b/include/sys/socket.h
93dc2d
index a1d749f9fa7b9257..6e4cf5077fb885a9 100644
93dc2d
--- a/include/sys/socket.h
93dc2d
+++ b/include/sys/socket.h
93dc2d
@@ -98,15 +98,21 @@ extern int __sendmmsg (int __fd, struct mmsghdr *__vmessages,
93dc2d
 libc_hidden_proto (__sendmmsg)
93dc2d
 #endif
93dc2d
 
93dc2d
-/* Receive a message as described by MESSAGE from socket FD.
93dc2d
-   Returns the number of bytes read or -1 for errors.  */
93dc2d
 extern ssize_t __libc_recvmsg (int __fd, struct msghdr *__message,
93dc2d
 			       int __flags);
93dc2d
 extern ssize_t __recvmsg (int __fd, struct msghdr *__message,
93dc2d
 			  int __flags) attribute_hidden;
93dc2d
 #if __TIMESIZE == 64
93dc2d
+# define __libc_recvmsg64 __libc_recvmsg
93dc2d
+# define __recvmsg64  __recvmsg
93dc2d
 # define __recvmmsg64 __recvmmsg
93dc2d
 #else
93dc2d
+extern ssize_t __libc_recvmsg64 (int __fd, struct msghdr *__message,
93dc2d
+				 int __flags);
93dc2d
+extern ssize_t __recvmsg64 (int __fd, struct msghdr *__message,
93dc2d
+			    int __flags);
93dc2d
+/* Receive a message as described by MESSAGE from socket FD.
93dc2d
+   Returns the number of bytes read or -1 for errors.  */
93dc2d
 extern int __recvmmsg64 (int __fd, struct mmsghdr *vmessages,
93dc2d
 			 unsigned int vlen, int flags,
93dc2d
 			 struct __timespec64 *timeout);
93dc2d
diff --git a/sysdeps/unix/sysv/linux/Makefile b/sysdeps/unix/sysv/linux/Makefile
93dc2d
index 7c75e22c6d0e9ff5..0657f4003e7116c6 100644
93dc2d
--- a/sysdeps/unix/sysv/linux/Makefile
93dc2d
+++ b/sysdeps/unix/sysv/linux/Makefile
93dc2d
@@ -273,8 +273,14 @@ sysdep_routines += cmsg_nxthdr
93dc2d
 CFLAGS-recvmmsg.c = -fexceptions -fasynchronous-unwind-tables
93dc2d
 CFLAGS-sendmmsg.c = -fexceptions -fasynchronous-unwind-tables
93dc2d
 
93dc2d
-tests += tst-socket-timestamp
93dc2d
-tests-time64 += tst-socket-timestamp-time64
93dc2d
+tests += \
93dc2d
+  tst-socket-timestamp \
93dc2d
+  tst-socket-timestamp-compat \
93dc2d
+  # tests
93dc2d
+tests-time64 += \
93dc2d
+  tst-socket-timestamp-time64 \
93dc2d
+  tst-socket-timestamp-compat-time64
93dc2d
+  # tests-time64
93dc2d
 
93dc2d
 tests-special += $(objpfx)tst-socket-consts.out
93dc2d
 $(objpfx)tst-socket-consts.out: ../sysdeps/unix/sysv/linux/tst-socket-consts.py
93dc2d
diff --git a/sysdeps/unix/sysv/linux/recvmmsg.c b/sysdeps/unix/sysv/linux/recvmmsg.c
93dc2d
index 5cd107ffa9be0699..fca9f6582db67fd7 100644
93dc2d
--- a/sysdeps/unix/sysv/linux/recvmmsg.c
93dc2d
+++ b/sysdeps/unix/sysv/linux/recvmmsg.c
93dc2d
@@ -20,9 +20,9 @@
93dc2d
 #include <sysdep.h>
93dc2d
 #include <socketcall.h>
93dc2d
 
93dc2d
-int
93dc2d
-__recvmmsg64 (int fd, struct mmsghdr *vmessages, unsigned int vlen, int flags,
93dc2d
-	      struct __timespec64 *timeout)
93dc2d
+static int
93dc2d
+recvmmsg_syscall (int fd, struct mmsghdr *vmessages, unsigned int vlen,
93dc2d
+		  int flags, struct __timespec64 *timeout)
93dc2d
 {
93dc2d
 #ifndef __NR_recvmmsg_time64
93dc2d
 # define __NR_recvmmsg_time64 __NR_recvmmsg
93dc2d
@@ -45,12 +45,6 @@ __recvmmsg64 (int fd, struct mmsghdr *vmessages, unsigned int vlen, int flags,
93dc2d
       pts32 = &ts3;;
93dc2d
     }
93dc2d
 
93dc2d
-  socklen_t csize[IOV_MAX];
93dc2d
-  if (vlen > IOV_MAX)
93dc2d
-    vlen = IOV_MAX;
93dc2d
-  for (int i = 0; i < vlen; i++)
93dc2d
-    csize[i] = vmessages[i].msg_hdr.msg_controllen;
93dc2d
-
93dc2d
 # ifdef __ASSUME_RECVMMSG_SYSCALL
93dc2d
   r = SYSCALL_CANCEL (recvmmsg, fd, vmessages, vlen, flags, pts32);
93dc2d
 # else
93dc2d
@@ -60,11 +54,31 @@ __recvmmsg64 (int fd, struct mmsghdr *vmessages, unsigned int vlen, int flags,
93dc2d
     {
93dc2d
       if (timeout != NULL)
93dc2d
         *timeout = valid_timespec_to_timespec64 (ts32);
93dc2d
+    }
93dc2d
+#endif
93dc2d
+  return r;
93dc2d
+}
93dc2d
+
93dc2d
+int
93dc2d
+__recvmmsg64 (int fd, struct mmsghdr *vmessages, unsigned int vlen, int flags,
93dc2d
+	      struct __timespec64 *timeout)
93dc2d
+{
93dc2d
+#if __TIMESIZE != 64
93dc2d
+  socklen_t csize[IOV_MAX];
93dc2d
+  if (vlen > IOV_MAX)
93dc2d
+    vlen = IOV_MAX;
93dc2d
+  for (int i = 0; i < vlen; i++)
93dc2d
+    csize[i] = vmessages[i].msg_hdr.msg_controllen;
93dc2d
+#endif
93dc2d
 
93dc2d
+  int r = recvmmsg_syscall (fd, vmessages, vlen, flags, timeout);
93dc2d
+#if __TIMESIZE != 64
93dc2d
+  if (r > 0)
93dc2d
+    {
93dc2d
       for (int i=0; i < r; i++)
93dc2d
         __convert_scm_timestamps (&vmessages[i].msg_hdr, csize[i]);
93dc2d
     }
93dc2d
-#endif /* __ASSUME_TIME64_SYSCALLS  */
93dc2d
+#endif
93dc2d
   return r;
93dc2d
 }
93dc2d
 #if __TIMESIZE != 64
93dc2d
@@ -80,7 +94,7 @@ __recvmmsg (int fd, struct mmsghdr *vmessages, unsigned int vlen, int flags,
93dc2d
       ts64 = valid_timespec_to_timespec64 (*timeout);
93dc2d
       pts64 = &ts6;;
93dc2d
     }
93dc2d
-  int r = __recvmmsg64 (fd, vmessages, vlen, flags, pts64);
93dc2d
+  int r = recvmmsg_syscall (fd, vmessages, vlen, flags, pts64);
93dc2d
   if (r >= 0 && timeout != NULL)
93dc2d
     /* The remanining timeout will be always less the input TIMEOUT.  */
93dc2d
     *timeout = valid_timespec64_to_timespec (ts64);
93dc2d
diff --git a/sysdeps/unix/sysv/linux/recvmsg.c b/sysdeps/unix/sysv/linux/recvmsg.c
93dc2d
index 07212f7c8641a921..c4b4704fd65d80c1 100644
93dc2d
--- a/sysdeps/unix/sysv/linux/recvmsg.c
93dc2d
+++ b/sysdeps/unix/sysv/linux/recvmsg.c
93dc2d
@@ -20,29 +20,41 @@
93dc2d
 #include <sysdep-cancel.h>
93dc2d
 #include <socketcall.h>
93dc2d
 
93dc2d
+static int
93dc2d
+__recvmsg_syscall (int fd, struct msghdr *msg, int flags)
93dc2d
+{
93dc2d
+#ifdef __ASSUME_RECVMSG_SYSCALL
93dc2d
+  return SYSCALL_CANCEL (recvmsg, fd, msg, flags);
93dc2d
+#else
93dc2d
+  return SOCKETCALL_CANCEL (recvmsg, fd, msg, flags);
93dc2d
+#endif
93dc2d
+}
93dc2d
+
93dc2d
 ssize_t
93dc2d
-__libc_recvmsg (int fd, struct msghdr *msg, int flags)
93dc2d
+__libc_recvmsg64 (int fd, struct msghdr *msg, int flags)
93dc2d
 {
93dc2d
   ssize_t r;
93dc2d
-#ifndef __ASSUME_TIME64_SYSCALLS
93dc2d
+#if __TIMESIZE != 64
93dc2d
   socklen_t orig_controllen = msg != NULL ? msg->msg_controllen : 0;
93dc2d
 #endif
93dc2d
 
93dc2d
-#ifdef __ASSUME_RECVMSG_SYSCALL
93dc2d
-  r = SYSCALL_CANCEL (recvmsg, fd, msg, flags);
93dc2d
-#else
93dc2d
-  r = SOCKETCALL_CANCEL (recvmsg, fd, msg, flags);
93dc2d
-#endif
93dc2d
+  r = __recvmsg_syscall (fd, msg, flags);
93dc2d
 
93dc2d
-#ifndef __ASSUME_TIME64_SYSCALLS
93dc2d
+#if __TIMESIZE != 64
93dc2d
   if (r >= 0 && orig_controllen != 0)
93dc2d
     __convert_scm_timestamps (msg, orig_controllen);
93dc2d
 #endif
93dc2d
 
93dc2d
   return r;
93dc2d
 }
93dc2d
-weak_alias (__libc_recvmsg, recvmsg)
93dc2d
-weak_alias (__libc_recvmsg, __recvmsg)
93dc2d
 #if __TIMESIZE != 64
93dc2d
-weak_alias (__recvmsg, __recvmsg64)
93dc2d
+weak_alias (__libc_recvmsg64, __recvmsg64)
93dc2d
+
93dc2d
+ssize_t
93dc2d
+__libc_recvmsg (int fd, struct msghdr *msg, int flags)
93dc2d
+{
93dc2d
+  return __recvmsg_syscall (fd, msg, flags);
93dc2d
+}
93dc2d
 #endif
93dc2d
+weak_alias (__libc_recvmsg, recvmsg)
93dc2d
+weak_alias (__libc_recvmsg, __recvmsg)
93dc2d
diff --git a/sysdeps/unix/sysv/linux/tst-socket-timestamp-compat-time64.c b/sysdeps/unix/sysv/linux/tst-socket-timestamp-compat-time64.c
93dc2d
new file mode 100644
93dc2d
index 0000000000000000..96a0bef0bf4a908b
93dc2d
--- /dev/null
93dc2d
+++ b/sysdeps/unix/sysv/linux/tst-socket-timestamp-compat-time64.c
93dc2d
@@ -0,0 +1 @@
93dc2d
+#include "tst-socket-timestamp-compat.c"
93dc2d
diff --git a/sysdeps/unix/sysv/linux/tst-socket-timestamp-compat.c b/sysdeps/unix/sysv/linux/tst-socket-timestamp-compat.c
93dc2d
new file mode 100644
93dc2d
index 0000000000000000..de261dae5a6385cf
93dc2d
--- /dev/null
93dc2d
+++ b/sysdeps/unix/sysv/linux/tst-socket-timestamp-compat.c
93dc2d
@@ -0,0 +1,265 @@
93dc2d
+/* Check recvmsg/recvmmsg 64-bit timestamp support.
93dc2d
+   Copyright (C) 2022 Free Software Foundation, Inc.
93dc2d
+   This file is part of the GNU C Library.
93dc2d
+
93dc2d
+   The GNU C Library is free software; you can redistribute it and/or
93dc2d
+   modify it under the terms of the GNU Lesser General Public
93dc2d
+   License as published by the Free Software Foundation; either
93dc2d
+   version 2.1 of the License, or (at your option) any later version.
93dc2d
+
93dc2d
+   The GNU C Library is distributed in the hope that it will be useful,
93dc2d
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
93dc2d
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
93dc2d
+   Lesser General Public License for more details.
93dc2d
+
93dc2d
+   You should have received a copy of the GNU Lesser General Public
93dc2d
+   License along with the GNU C Library; if not, see
93dc2d
+   <https://www.gnu.org/licenses/>.  */
93dc2d
+
93dc2d
+#include <arpa/inet.h>
93dc2d
+#include <string.h>
93dc2d
+#include <support/check.h>
93dc2d
+#include <support/xsocket.h>
93dc2d
+#include <support/xunistd.h>
93dc2d
+#include <stdbool.h>
93dc2d
+
93dc2d
+/* AF_INET socket and address used to receive data.  */
93dc2d
+static int srv;
93dc2d
+static struct sockaddr_in srv_addr;
93dc2d
+
93dc2d
+static int
93dc2d
+do_sendto (const struct sockaddr_in *addr, int payload)
93dc2d
+{
93dc2d
+  int s = xsocket (AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
93dc2d
+  xconnect (s, (const struct sockaddr *) addr, sizeof (*addr));
93dc2d
+
93dc2d
+  xsendto (s, &payload, sizeof (payload), 0, (const struct sockaddr *) addr,
93dc2d
+	   sizeof (*addr));
93dc2d
+
93dc2d
+  xclose (s);
93dc2d
+
93dc2d
+  return 0;
93dc2d
+}
93dc2d
+
93dc2d
+static void
93dc2d
+do_recvmsg_ancillary (bool use_multi_call, struct mmsghdr *mmhdr,
93dc2d
+		      void *msgbuf, size_t msgbuflen, int exp_payload)
93dc2d
+{
93dc2d
+  int payload;
93dc2d
+  struct iovec iov =
93dc2d
+    {
93dc2d
+      .iov_base = &payload,
93dc2d
+      .iov_len = sizeof (payload)
93dc2d
+    };
93dc2d
+  mmhdr->msg_hdr.msg_name = NULL;
93dc2d
+  mmhdr->msg_hdr.msg_iov = &iov;
93dc2d
+  mmhdr->msg_hdr.msg_iovlen = 1;
93dc2d
+  mmhdr->msg_hdr.msg_control = msgbuf;
93dc2d
+  mmhdr->msg_hdr.msg_controllen = msgbuflen;
93dc2d
+
93dc2d
+  int r;
93dc2d
+  if (use_multi_call)
93dc2d
+    {
93dc2d
+      r = recvmmsg (srv, mmhdr, 1, 0, NULL);
93dc2d
+      if (r >= 0)
93dc2d
+	r = mmhdr->msg_len;
93dc2d
+    }
93dc2d
+  else
93dc2d
+    r = recvmsg (srv, &mmhdr->msg_hdr, 0);
93dc2d
+  TEST_COMPARE (r, sizeof (int));
93dc2d
+  TEST_COMPARE (payload, exp_payload);
93dc2d
+}
93dc2d
+
93dc2d
+/* Check if recvmsg create the additional 64 bit timestamp if only 32 bit
93dc2d
+   is enabled for 64 bit recvmsg symbol.  */
93dc2d
+static void
93dc2d
+do_test_large_buffer (bool mc)
93dc2d
+{
93dc2d
+  struct mmsghdr mmhdr = { 0 };
93dc2d
+  /* It should be large enought for either timeval/timespec and the
93dc2d
+     64 time type as well.  */
93dc2d
+
93dc2d
+  union
93dc2d
+  {
93dc2d
+    struct cmsghdr cmsghdr;
93dc2d
+    char msgbuf[512];
93dc2d
+  } control;
93dc2d
+
93dc2d
+  /* Enable 32 bit timeval precision and check if no 64 bit timeval stamp
93dc2d
+     is created.  */
93dc2d
+  {
93dc2d
+    int r = setsockopt (srv, SOL_SOCKET, SO_TIMESTAMP_OLD, &(int){1},
93dc2d
+			sizeof (int));
93dc2d
+    TEST_VERIFY_EXIT (r != -1);
93dc2d
+
93dc2d
+    do_sendto (&srv_addr, 42);
93dc2d
+    do_recvmsg_ancillary (mc, &mmhdr, &control, sizeof control, 42);
93dc2d
+
93dc2d
+    bool found_timestamp = false;
93dc2d
+    for (struct cmsghdr *cmsg = CMSG_FIRSTHDR (&mmhdr.msg_hdr);
93dc2d
+	 cmsg != NULL;
93dc2d
+	 cmsg = CMSG_NXTHDR (&mmhdr.msg_hdr, cmsg))
93dc2d
+    {
93dc2d
+      if (cmsg->cmsg_level != SOL_SOCKET)
93dc2d
+	continue;
93dc2d
+
93dc2d
+      if (sizeof (time_t) > 4 && cmsg->cmsg_type == SO_TIMESTAMP_NEW)
93dc2d
+	found_timestamp = true;
93dc2d
+      else
93dc2d
+	TEST_VERIFY (cmsg->cmsg_type != SO_TIMESTAMP_NEW);
93dc2d
+    }
93dc2d
+
93dc2d
+    TEST_COMPARE (found_timestamp, sizeof (time_t) > 4);
93dc2d
+  }
93dc2d
+
93dc2d
+  /* Same as before, but for timespec.  */
93dc2d
+  {
93dc2d
+    int r = setsockopt (srv, SOL_SOCKET, SO_TIMESTAMPNS_OLD, &(int){1},
93dc2d
+			sizeof (int));
93dc2d
+    TEST_VERIFY_EXIT (r != -1);
93dc2d
+
93dc2d
+    do_sendto (&srv_addr, 42);
93dc2d
+    do_recvmsg_ancillary (mc, &mmhdr, &control, sizeof control, 42);
93dc2d
+
93dc2d
+    bool found_timestamp = false;
93dc2d
+    for (struct cmsghdr *cmsg = CMSG_FIRSTHDR (&mmhdr.msg_hdr);
93dc2d
+	 cmsg != NULL;
93dc2d
+	 cmsg = CMSG_NXTHDR (&mmhdr.msg_hdr, cmsg))
93dc2d
+    {
93dc2d
+      if (cmsg->cmsg_level != SOL_SOCKET)
93dc2d
+	continue;
93dc2d
+
93dc2d
+      if (sizeof (time_t) > 4 && cmsg->cmsg_type == SO_TIMESTAMPNS_NEW)
93dc2d
+	found_timestamp = true;
93dc2d
+      else
93dc2d
+	TEST_VERIFY (cmsg->cmsg_type != SO_TIMESTAMPNS_NEW);
93dc2d
+    }
93dc2d
+
93dc2d
+    TEST_COMPARE (found_timestamp, sizeof (time_t) > 4);
93dc2d
+  }
93dc2d
+}
93dc2d
+
93dc2d
+/* Check if recvmsg does not create the additional 64 bit timestamp if
93dc2d
+   only 32 bit timestamp is enabled if the ancillary buffer is not large
93dc2d
+   enought.  Also checks if MSG_CTRUNC is set iff for 64 bit recvmsg
93dc2d
+   symbol.  */
93dc2d
+static void
93dc2d
+do_test_small_buffer (bool mc)
93dc2d
+{
93dc2d
+  struct mmsghdr mmhdr = { 0 };
93dc2d
+
93dc2d
+  /* Enable 32 bit timeval precision and check if no 64 bit timeval stamp
93dc2d
+     is created.  */
93dc2d
+  {
93dc2d
+    int r = setsockopt (srv, SOL_SOCKET, SO_TIMESTAMP_OLD, &(int){1},
93dc2d
+			sizeof (int));
93dc2d
+    TEST_VERIFY_EXIT (r != -1);
93dc2d
+
93dc2d
+    union
93dc2d
+    {
93dc2d
+      struct cmsghdr cmsghdr;
93dc2d
+      char msgbuf[CMSG_SPACE (sizeof (struct timeval))];
93dc2d
+    } control;
93dc2d
+
93dc2d
+    do_sendto (&srv_addr, 42);
93dc2d
+    do_recvmsg_ancillary (mc, &mmhdr, &control, sizeof control, 42);
93dc2d
+
93dc2d
+    bool found_timestamp = false;
93dc2d
+    for (struct cmsghdr *cmsg = CMSG_FIRSTHDR (&mmhdr.msg_hdr);
93dc2d
+	 cmsg != NULL;
93dc2d
+	 cmsg = CMSG_NXTHDR (&mmhdr.msg_hdr, cmsg))
93dc2d
+    {
93dc2d
+      if (cmsg->cmsg_level != SOL_SOCKET)
93dc2d
+	continue;
93dc2d
+
93dc2d
+      if (sizeof (time_t) > 4 && cmsg->cmsg_type == SO_TIMESTAMP_NEW)
93dc2d
+	found_timestamp = true;
93dc2d
+      else
93dc2d
+	TEST_VERIFY (cmsg->cmsg_type != SO_TIMESTAMP_NEW);
93dc2d
+    }
93dc2d
+
93dc2d
+    if (sizeof (time_t) > 4)
93dc2d
+      {
93dc2d
+	TEST_VERIFY ((mmhdr.msg_hdr.msg_flags & MSG_CTRUNC));
93dc2d
+	TEST_COMPARE (found_timestamp, 0);
93dc2d
+      }
93dc2d
+    else
93dc2d
+      {
93dc2d
+	TEST_VERIFY (!(mmhdr.msg_hdr.msg_flags & MSG_CTRUNC));
93dc2d
+	TEST_COMPARE (found_timestamp, 0);
93dc2d
+      }
93dc2d
+  }
93dc2d
+
93dc2d
+  /* Same as before, but for timespec.  */
93dc2d
+  {
93dc2d
+    int r = setsockopt (srv, SOL_SOCKET, SO_TIMESTAMPNS_OLD, &(int){1},
93dc2d
+			sizeof (int));
93dc2d
+    TEST_VERIFY_EXIT (r != -1);
93dc2d
+
93dc2d
+    union
93dc2d
+    {
93dc2d
+      struct cmsghdr cmsghdr;
93dc2d
+      char msgbuf[CMSG_SPACE (sizeof (struct timespec))];
93dc2d
+    } control;
93dc2d
+
93dc2d
+    do_sendto (&srv_addr, 42);
93dc2d
+    do_recvmsg_ancillary (mc, &mmhdr, &control, sizeof control, 42);
93dc2d
+
93dc2d
+    bool found_timestamp = false;
93dc2d
+    for (struct cmsghdr *cmsg = CMSG_FIRSTHDR (&mmhdr.msg_hdr);
93dc2d
+	 cmsg != NULL;
93dc2d
+	 cmsg = CMSG_NXTHDR (&mmhdr.msg_hdr, cmsg))
93dc2d
+    {
93dc2d
+      if (cmsg->cmsg_level != SOL_SOCKET)
93dc2d
+	continue;
93dc2d
+
93dc2d
+      if (sizeof (time_t) > 4 && cmsg->cmsg_type == SO_TIMESTAMPNS_NEW)
93dc2d
+	found_timestamp = true;
93dc2d
+      else
93dc2d
+	TEST_VERIFY (cmsg->cmsg_type != SO_TIMESTAMPNS_NEW);
93dc2d
+    }
93dc2d
+
93dc2d
+    if (sizeof (time_t) > 4)
93dc2d
+      {
93dc2d
+	TEST_VERIFY ((mmhdr.msg_hdr.msg_flags & MSG_CTRUNC));
93dc2d
+	TEST_COMPARE (found_timestamp, 0);
93dc2d
+      }
93dc2d
+    else
93dc2d
+      {
93dc2d
+	TEST_VERIFY ((mmhdr.msg_hdr.msg_flags & MSG_CTRUNC) == 0);
93dc2d
+	TEST_COMPARE (found_timestamp, 0);
93dc2d
+      }
93dc2d
+  }
93dc2d
+}
93dc2d
+
93dc2d
+static int
93dc2d
+do_test (void)
93dc2d
+{
93dc2d
+  /* This test only make sense for ABIs that support 32 bit time_t socket
93dc2d
+     timestampss.  */
93dc2d
+  if (sizeof (time_t) > 4 && __WORDSIZE == 64)
93dc2d
+    return 0;
93dc2d
+
93dc2d
+  srv = xsocket (AF_INET, SOCK_DGRAM, 0);
93dc2d
+  srv_addr = (struct sockaddr_in) {
93dc2d
+    .sin_family = AF_INET,
93dc2d
+    .sin_addr = {.s_addr = htonl (INADDR_LOOPBACK) },
93dc2d
+  };
93dc2d
+  xbind (srv, (struct sockaddr *) &srv_addr, sizeof (srv_addr));
93dc2d
+  {
93dc2d
+    socklen_t sa_len = sizeof (srv_addr);
93dc2d
+    xgetsockname (srv, (struct sockaddr *) &srv_addr, &sa_len);
93dc2d
+    TEST_VERIFY (sa_len == sizeof (srv_addr));
93dc2d
+  }
93dc2d
+
93dc2d
+  /* Check recvmsg;  */
93dc2d
+  do_test_large_buffer (false);
93dc2d
+  do_test_small_buffer (false);
93dc2d
+  /* Check recvmmsg.  */
93dc2d
+  do_test_large_buffer (true);
93dc2d
+  do_test_small_buffer (true);
93dc2d
+
93dc2d
+  return 0;
93dc2d
+}
93dc2d
+
93dc2d
+#include <support/test-driver.c>