public inbox for glibc-cvs@sourceware.org
help / color / mirror / Atom feed
* [glibc/azanella/y2038] linux: Add fallback for 64-bit time_t SO_{RCV,  SND}TIMEO
@ 2021-06-08 20:48 Adhemerval Zanella
  0 siblings, 0 replies; 10+ messages in thread
From: Adhemerval Zanella @ 2021-06-08 20:48 UTC (permalink / raw)
  To: glibc-cvs

https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=be65a086b42cd83045ee5d479805b4f06213b545

commit be65a086b42cd83045ee5d479805b4f06213b545
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Date:   Mon Sep 7 14:13:58 2020 -0300

    linux: Add fallback for 64-bit time_t SO_{RCV,SND}TIMEO
    
    The constant values will be changed for __TIMESIZE=64, so binaries built
    with 64-bit time support might fail to work properly on old kernels.
    Both {get,set}sockopt will retry the syscall with the old constant
    values and the timeout value adjusted when kernel returns ENOTPROTOPT.
    
    It also adds an internal only SO_{RCV,SND}TIMEO where
    COMPAT_SO_{RCV,SND}TIMEO_OLD indicates pre 32 bit time support and
    COMPAT_SO_{RCV,SND}TIMEO_NEW indicates time64 support.  It allows to
    refer to constant independently of the time_t ABI and kernel version
    used.
    
    Checked on x86_64-linux-gnu and i686-linux-gnu (on 5.4 and on 4.15
    kernel).

Diff:
---
 sysdeps/unix/sysv/linux/getsockopt.c               | 70 ++++++++++++++++++---
 .../unix/sysv/linux/hppa/socket-constants-time64.h | 30 +++++++++
 .../unix/sysv/linux/mips/socket-constants-time64.h | 30 +++++++++
 .../sysv/linux/powerpc/socket-constants-time64.h   | 30 +++++++++
 sysdeps/unix/sysv/linux/setsockopt.c               | 71 +++++++++++++++++++---
 sysdeps/unix/sysv/linux/socket-constants-time64.h  | 30 +++++++++
 .../sysv/linux/sparc/socket-constants-time64.h     | 30 +++++++++
 7 files changed, 276 insertions(+), 15 deletions(-)

diff --git a/sysdeps/unix/sysv/linux/getsockopt.c b/sysdeps/unix/sysv/linux/getsockopt.c
index 76ee8a94d6..688a7de087 100644
--- a/sysdeps/unix/sysv/linux/getsockopt.c
+++ b/sysdeps/unix/sysv/linux/getsockopt.c
@@ -15,16 +15,15 @@
    License along with the GNU C Library; if not, see
    <https://www.gnu.org/licenses/>.  */
 
-#include <errno.h>
-#include <signal.h>
 #include <sys/socket.h>
-
+#include <time.h>
+#include <sysdep.h>
 #include <socketcall.h>
-#include <kernel-features.h>
-#include <sys/syscall.h>
+#include <socket-constants-time64.h>
 
-int
-__getsockopt (int fd, int level, int optname, void *optval, socklen_t *len)
+static int
+getsockopt_syscall (int fd, int level, int optname, void *optval,
+		    socklen_t *len)
 {
 #ifdef __ASSUME_GETSOCKOPT_SYSCALL
   return INLINE_SYSCALL (getsockopt, 5, fd, level, optname, optval, len);
@@ -32,4 +31,61 @@ __getsockopt (int fd, int level, int optname, void *optval, socklen_t *len)
   return SOCKETCALL (getsockopt, fd, level, optname, optval, len);
 #endif
 }
+
+#ifndef __ASSUME_TIME64_SYSCALLS
+static int
+getsockopt32 (int fd, int level, int optname, void *optval,
+	      socklen_t *len)
+{
+  int r = -1;
+
+  if (level != SOL_SOCKET)
+    return r;
+
+  switch (optname)
+    {
+    case COMPAT_SO_RCVTIMEO_NEW:
+    case COMPAT_SO_SNDTIMEO_NEW:
+      {
+	if (optname == COMPAT_SO_RCVTIMEO_NEW)
+	  optname = COMPAT_SO_RCVTIMEO_OLD;
+	if (optname == COMPAT_SO_SNDTIMEO_NEW)
+	  optname = COMPAT_SO_SNDTIMEO_OLD;
+
+	struct __timeval32 tv32;
+	r = getsockopt_syscall (fd, level, optname, &tv32,
+				(socklen_t[]) { sizeof tv32 });
+	if (r < 0)
+	  break;
+
+	/* POSIX states that if the size of the option value is greater than
+	   then option length, the option value argument shall be silently
+	   truncated.  */
+	if (*len >= sizeof (struct __timeval64))
+	  {
+	    struct __timeval64 *tv64 = (struct __timeval64 *) optval;
+	    *tv64 = valid_timeval32_to_timeval64 (tv32);
+	    *len = sizeof (*tv64);
+	  }
+	else
+	  memcpy (optval, &tv32, sizeof tv32);
+      }
+    }
+
+  return r;
+}
+#endif
+
+int
+__getsockopt (int fd, int level, int optname, void *optval, socklen_t *len)
+{
+  int r = getsockopt_syscall (fd, level, optname, optval, len);
+
+#ifndef __ASSUME_TIME64_SYSCALLS
+  if (r == -1 && errno == ENOPROTOOPT)
+    r = getsockopt32 (fd, level, optname, optval, len);
+#endif
+
+ return r;
+}
 weak_alias (__getsockopt, getsockopt)
diff --git a/sysdeps/unix/sysv/linux/hppa/socket-constants-time64.h b/sysdeps/unix/sysv/linux/hppa/socket-constants-time64.h
new file mode 100644
index 0000000000..ea721e0fc2
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/hppa/socket-constants-time64.h
@@ -0,0 +1,30 @@
+/* Compat socket constants used in 64 bit compat code.
+   Copyright (C) 2021 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#ifndef _SOCKET_CONSTANTS_TIME64_H
+#define _SOCKET_CONSTANTS_TIME64_H
+
+/* The compat code requires the SO_* constants used for both 32 and 64 bit
+   time_t, however they were only added on v5.1 kernel.  */
+
+#define COMPAT_SO_RCVTIMEO_OLD 4102
+#define COMPAT_SO_SNDTIMEO_OLD 4101
+#define COMPAT_SO_RCVTIMEO_NEW 16448
+#define COMPAT_SO_SNDTIMEO_NEW 16449
+
+#endif
diff --git a/sysdeps/unix/sysv/linux/mips/socket-constants-time64.h b/sysdeps/unix/sysv/linux/mips/socket-constants-time64.h
new file mode 100644
index 0000000000..ab8bd62853
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/mips/socket-constants-time64.h
@@ -0,0 +1,30 @@
+/* Compat socket constants used in 64 bit compat code.
+   Copyright (C) 2021 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#ifndef _SOCKET_CONSTANTS_TIME64_H
+#define _SOCKET_CONSTANTS_TIME64_H
+
+/* The compat code requires the SO_* constants used for both 32 and 64 bit
+   time_t, however they were only added on v5.1 kernel.  */
+
+#define COMPAT_SO_RCVTIMEO_OLD 4102
+#define COMPAT_SO_SNDTIMEO_OLD 4101
+#define COMPAT_SO_RCVTIMEO_NEW 66
+#define COMPAT_SO_SNDTIMEO_NEW 67
+
+#endif
diff --git a/sysdeps/unix/sysv/linux/powerpc/socket-constants-time64.h b/sysdeps/unix/sysv/linux/powerpc/socket-constants-time64.h
new file mode 100644
index 0000000000..1e48dcca8d
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/powerpc/socket-constants-time64.h
@@ -0,0 +1,30 @@
+/* Compat socket constants used in 64 bit compat code.
+   Copyright (C) 2021 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#ifndef _SOCKET_CONSTANTS_TIME64_H
+#define _SOCKET_CONSTANTS_TIME64_H
+
+/* The compat code requires the SO_* constants used for both 32 and 64 bit
+   time_t, however they were only added on v5.1 kernel.  */
+
+#define COMPAT_SO_RCVTIMEO_OLD 18
+#define COMPAT_SO_SNDTIMEO_OLD 19
+#define COMPAT_SO_RCVTIMEO_NEW 66
+#define COMPAT_SO_SNDTIMEO_NEW 67
+
+#endif
diff --git a/sysdeps/unix/sysv/linux/setsockopt.c b/sysdeps/unix/sysv/linux/setsockopt.c
index 12fd7bdcde..6505202265 100644
--- a/sysdeps/unix/sysv/linux/setsockopt.c
+++ b/sysdeps/unix/sysv/linux/setsockopt.c
@@ -15,21 +15,76 @@
    License along with the GNU C Library; if not, see
    <https://www.gnu.org/licenses/>.  */
 
-#include <errno.h>
-#include <signal.h>
 #include <sys/socket.h>
-
+#include <time.h>
+#include <sysdep.h>
 #include <socketcall.h>
-#include <kernel-features.h>
-#include <sys/syscall.h>
+#include <socket-constants-time64.h>
 
-int
-setsockopt (int fd, int level, int optname, const void *optval, socklen_t len)
+static int
+setsockopt_syscall (int fd, int level, int optname, const void *optval,
+		    socklen_t len)
 {
 #ifdef __ASSUME_SETSOCKOPT_SYSCALL
-  return INLINE_SYSCALL (setsockopt, 5, fd, level, optname, optval, len);
+  return INLINE_SYSCALL_CALL (setsockopt, fd, level, optname, optval, len);
 #else
   return SOCKETCALL (setsockopt, fd, level, optname, optval, len);
 #endif
 }
+
+#ifndef __ASSUME_TIME64_SYSCALLS
+static int
+setsockopt32 (int fd, int level, int optname, const void *optval,
+	      socklen_t len)
+{
+  int r = -1;
+
+  if (level != SOL_SOCKET)
+    return r;
+
+  switch (optname)
+    {
+    case COMPAT_SO_RCVTIMEO_NEW:
+    case COMPAT_SO_SNDTIMEO_NEW:
+      {
+        if (len < sizeof (struct __timeval64))
+	  {
+	    __set_errno (EINVAL);
+	    break;
+	  }
+
+	struct __timeval64 *tv64 = (struct __timeval64 *) optval;
+	if (! in_time_t_range (tv64->tv_sec))
+	  {
+	    __set_errno (EOVERFLOW);
+	    break;
+	  }
+
+	if (optname == COMPAT_SO_RCVTIMEO_NEW)
+	  optname = COMPAT_SO_RCVTIMEO_OLD;
+	if (optname == COMPAT_SO_SNDTIMEO_NEW)
+	  optname = COMPAT_SO_SNDTIMEO_OLD;
+
+	struct __timeval32 tv32 = valid_timeval64_to_timeval32 (*tv64);
+
+	r = setsockopt_syscall (fd, level, optname, &tv32, sizeof (tv32));
+      }
+    }
+
+  return r;
+}
+#endif
+
+int
+setsockopt (int fd, int level, int optname, const void *optval, socklen_t len)
+{
+  int r = setsockopt_syscall (fd, level, optname, optval, len);
+
+#ifndef __ASSUME_TIME64_SYSCALLS
+  if (r == -1 && errno == ENOPROTOOPT)
+    r = setsockopt32 (fd, level, optname, optval, len);
+#endif
+
+  return r;
+}
 weak_alias (setsockopt, __setsockopt)
diff --git a/sysdeps/unix/sysv/linux/socket-constants-time64.h b/sysdeps/unix/sysv/linux/socket-constants-time64.h
new file mode 100644
index 0000000000..e5a3777f28
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/socket-constants-time64.h
@@ -0,0 +1,30 @@
+/* Compat socket constants used in 64 bit compat code.
+   Copyright (C) 2021 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#ifndef _SOCKET_CONSTANTS_TIME64_H
+#define _SOCKET_CONSTANTS_TIME64_H
+
+/* The compat code requires the SO_* constants used for both 32 and 64 bit
+   time_t, however they were only added on v5.1 kernel.  */
+
+#define COMPAT_SO_RCVTIMEO_OLD 20
+#define COMPAT_SO_SNDTIMEO_OLD 21
+#define COMPAT_SO_RCVTIMEO_NEW 66
+#define COMPAT_SO_SNDTIMEO_NEW 67
+
+#endif
diff --git a/sysdeps/unix/sysv/linux/sparc/socket-constants-time64.h b/sysdeps/unix/sysv/linux/sparc/socket-constants-time64.h
new file mode 100644
index 0000000000..b137abdeea
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/sparc/socket-constants-time64.h
@@ -0,0 +1,30 @@
+/* Compat socket constants used in 64 bit compat code.
+   Copyright (C) 2021 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#ifndef _SOCKET_CONSTANTS_TIME64_H
+#define _SOCKET_CONSTANTS_TIME64_H
+
+/* The compat code requires the SO_* constants used for both 32 and 64 bit
+   time_t, however they were only added on v5.1 kernel.  */
+
+#define COMPAT_SO_RCVTIMEO_OLD 8192
+#define COMPAT_SO_SNDTIMEO_OLD 16384
+#define COMPAT_SO_RCVTIMEO_NEW 68
+#define COMPAT_SO_SNDTIMEO_NEW 69
+
+#endif


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [glibc/azanella/y2038] linux: Add fallback for 64-bit time_t SO_{RCV,  SND}TIMEO
@ 2021-05-26 16:24 Adhemerval Zanella
  0 siblings, 0 replies; 10+ messages in thread
From: Adhemerval Zanella @ 2021-05-26 16:24 UTC (permalink / raw)
  To: glibc-cvs

https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=9f71c109db176c607d09d310cd4f550215079abf

commit 9f71c109db176c607d09d310cd4f550215079abf
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Date:   Mon Sep 7 14:13:58 2020 -0300

    linux: Add fallback for 64-bit time_t SO_{RCV,SND}TIMEO
    
    The constant values will be changed for __TIMESIZE=64, so binaries built
    with 64-bit time support might fail to work properly on old kernels.
    Both {get,set}sockopt will retry the syscall with the old constant
    values and the timeout value adjusted when kernel returns ENOTPROTOPT.
    
    It also adds an internal only SO_{RCV,SND}TIMEO where
    COMPAT_SO_{RCV,SND}TIMEO_OLD indicates pre 32 bit time support and
    COMPAT_SO_{RCV,SND}TIMEO_NEW indicates time64 support.  It allows to
    refer to constant independently of the time_t ABI and kernel version
    used.
    
    Checked on x86_64-linux-gnu and i686-linux-gnu (on 5.4 and on 4.15
    kernel).

Diff:
---
 sysdeps/unix/sysv/linux/getsockopt.c               | 67 +++++++++++++++++---
 .../unix/sysv/linux/hppa/socket-constants-time64.h | 30 +++++++++
 .../unix/sysv/linux/mips/socket-constants-time64.h | 30 +++++++++
 .../sysv/linux/powerpc/socket-constants-time64.h   | 30 +++++++++
 sysdeps/unix/sysv/linux/setsockopt.c               | 71 +++++++++++++++++++---
 sysdeps/unix/sysv/linux/socket-constants-time64.h  | 30 +++++++++
 .../sysv/linux/sparc/socket-constants-time64.h     | 30 +++++++++
 7 files changed, 273 insertions(+), 15 deletions(-)

diff --git a/sysdeps/unix/sysv/linux/getsockopt.c b/sysdeps/unix/sysv/linux/getsockopt.c
index 76ee8a94d6..c8e502d976 100644
--- a/sysdeps/unix/sysv/linux/getsockopt.c
+++ b/sysdeps/unix/sysv/linux/getsockopt.c
@@ -15,16 +15,15 @@
    License along with the GNU C Library; if not, see
    <https://www.gnu.org/licenses/>.  */
 
-#include <errno.h>
-#include <signal.h>
 #include <sys/socket.h>
-
+#include <time.h>
+#include <sysdep.h>
 #include <socketcall.h>
-#include <kernel-features.h>
-#include <sys/syscall.h>
+#include <socket-constants-time64.h>
 
-int
-__getsockopt (int fd, int level, int optname, void *optval, socklen_t *len)
+static int
+getsockopt_syscall (int fd, int level, int optname, void *optval,
+		    socklen_t *len)
 {
 #ifdef __ASSUME_GETSOCKOPT_SYSCALL
   return INLINE_SYSCALL (getsockopt, 5, fd, level, optname, optval, len);
@@ -32,4 +31,58 @@ __getsockopt (int fd, int level, int optname, void *optval, socklen_t *len)
   return SOCKETCALL (getsockopt, fd, level, optname, optval, len);
 #endif
 }
+
+#ifndef __ASSUME_TIME64_SYSCALLS
+static int
+getsockopt32 (int fd, int level, int optname, void *optval,
+	      socklen_t *len)
+{
+  int r = -1;
+
+  if (level != SOL_SOCKET)
+    return r;
+
+  switch (optname)
+    {
+    case COMPAT_SO_RCVTIMEO_NEW:
+    case COMPAT_SO_SNDTIMEO_NEW:
+      {
+        if (*len < sizeof (struct __timeval64))
+	  {
+	    __set_errno (EINVAL);
+	    break;
+	  }
+
+	if (optname == COMPAT_SO_RCVTIMEO_NEW)
+	  optname = COMPAT_SO_RCVTIMEO_OLD;
+	if (optname == COMPAT_SO_SNDTIMEO_NEW)
+	  optname = COMPAT_SO_SNDTIMEO_OLD;
+
+	struct __timeval32 tv32;
+	r = getsockopt_syscall (fd, level, optname, &tv32,
+				(socklen_t[]) { sizeof tv32 });
+	if (r < 0)
+	  break;
+	struct __timeval64 *tv64 = (struct __timeval64 *) optval;
+	*tv64 = valid_timeval32_to_timeval64 (tv32);
+	*len = sizeof (*tv64);
+      }
+    }
+
+  return r;
+}
+#endif
+
+int
+__getsockopt (int fd, int level, int optname, void *optval, socklen_t *len)
+{
+  int r = getsockopt_syscall (fd, level, optname, optval, len);
+
+#ifndef __ASSUME_TIME64_SYSCALLS
+  if (r == -1 && errno == ENOPROTOOPT)
+    r = getsockopt32 (fd, level, optname, optval, len);
+#endif
+
+ return r;
+}
 weak_alias (__getsockopt, getsockopt)
diff --git a/sysdeps/unix/sysv/linux/hppa/socket-constants-time64.h b/sysdeps/unix/sysv/linux/hppa/socket-constants-time64.h
new file mode 100644
index 0000000000..ea721e0fc2
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/hppa/socket-constants-time64.h
@@ -0,0 +1,30 @@
+/* Compat socket constants used in 64 bit compat code.
+   Copyright (C) 2021 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#ifndef _SOCKET_CONSTANTS_TIME64_H
+#define _SOCKET_CONSTANTS_TIME64_H
+
+/* The compat code requires the SO_* constants used for both 32 and 64 bit
+   time_t, however they were only added on v5.1 kernel.  */
+
+#define COMPAT_SO_RCVTIMEO_OLD 4102
+#define COMPAT_SO_SNDTIMEO_OLD 4101
+#define COMPAT_SO_RCVTIMEO_NEW 16448
+#define COMPAT_SO_SNDTIMEO_NEW 16449
+
+#endif
diff --git a/sysdeps/unix/sysv/linux/mips/socket-constants-time64.h b/sysdeps/unix/sysv/linux/mips/socket-constants-time64.h
new file mode 100644
index 0000000000..ab8bd62853
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/mips/socket-constants-time64.h
@@ -0,0 +1,30 @@
+/* Compat socket constants used in 64 bit compat code.
+   Copyright (C) 2021 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#ifndef _SOCKET_CONSTANTS_TIME64_H
+#define _SOCKET_CONSTANTS_TIME64_H
+
+/* The compat code requires the SO_* constants used for both 32 and 64 bit
+   time_t, however they were only added on v5.1 kernel.  */
+
+#define COMPAT_SO_RCVTIMEO_OLD 4102
+#define COMPAT_SO_SNDTIMEO_OLD 4101
+#define COMPAT_SO_RCVTIMEO_NEW 66
+#define COMPAT_SO_SNDTIMEO_NEW 67
+
+#endif
diff --git a/sysdeps/unix/sysv/linux/powerpc/socket-constants-time64.h b/sysdeps/unix/sysv/linux/powerpc/socket-constants-time64.h
new file mode 100644
index 0000000000..1e48dcca8d
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/powerpc/socket-constants-time64.h
@@ -0,0 +1,30 @@
+/* Compat socket constants used in 64 bit compat code.
+   Copyright (C) 2021 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#ifndef _SOCKET_CONSTANTS_TIME64_H
+#define _SOCKET_CONSTANTS_TIME64_H
+
+/* The compat code requires the SO_* constants used for both 32 and 64 bit
+   time_t, however they were only added on v5.1 kernel.  */
+
+#define COMPAT_SO_RCVTIMEO_OLD 18
+#define COMPAT_SO_SNDTIMEO_OLD 19
+#define COMPAT_SO_RCVTIMEO_NEW 66
+#define COMPAT_SO_SNDTIMEO_NEW 67
+
+#endif
diff --git a/sysdeps/unix/sysv/linux/setsockopt.c b/sysdeps/unix/sysv/linux/setsockopt.c
index 12fd7bdcde..6505202265 100644
--- a/sysdeps/unix/sysv/linux/setsockopt.c
+++ b/sysdeps/unix/sysv/linux/setsockopt.c
@@ -15,21 +15,76 @@
    License along with the GNU C Library; if not, see
    <https://www.gnu.org/licenses/>.  */
 
-#include <errno.h>
-#include <signal.h>
 #include <sys/socket.h>
-
+#include <time.h>
+#include <sysdep.h>
 #include <socketcall.h>
-#include <kernel-features.h>
-#include <sys/syscall.h>
+#include <socket-constants-time64.h>
 
-int
-setsockopt (int fd, int level, int optname, const void *optval, socklen_t len)
+static int
+setsockopt_syscall (int fd, int level, int optname, const void *optval,
+		    socklen_t len)
 {
 #ifdef __ASSUME_SETSOCKOPT_SYSCALL
-  return INLINE_SYSCALL (setsockopt, 5, fd, level, optname, optval, len);
+  return INLINE_SYSCALL_CALL (setsockopt, fd, level, optname, optval, len);
 #else
   return SOCKETCALL (setsockopt, fd, level, optname, optval, len);
 #endif
 }
+
+#ifndef __ASSUME_TIME64_SYSCALLS
+static int
+setsockopt32 (int fd, int level, int optname, const void *optval,
+	      socklen_t len)
+{
+  int r = -1;
+
+  if (level != SOL_SOCKET)
+    return r;
+
+  switch (optname)
+    {
+    case COMPAT_SO_RCVTIMEO_NEW:
+    case COMPAT_SO_SNDTIMEO_NEW:
+      {
+        if (len < sizeof (struct __timeval64))
+	  {
+	    __set_errno (EINVAL);
+	    break;
+	  }
+
+	struct __timeval64 *tv64 = (struct __timeval64 *) optval;
+	if (! in_time_t_range (tv64->tv_sec))
+	  {
+	    __set_errno (EOVERFLOW);
+	    break;
+	  }
+
+	if (optname == COMPAT_SO_RCVTIMEO_NEW)
+	  optname = COMPAT_SO_RCVTIMEO_OLD;
+	if (optname == COMPAT_SO_SNDTIMEO_NEW)
+	  optname = COMPAT_SO_SNDTIMEO_OLD;
+
+	struct __timeval32 tv32 = valid_timeval64_to_timeval32 (*tv64);
+
+	r = setsockopt_syscall (fd, level, optname, &tv32, sizeof (tv32));
+      }
+    }
+
+  return r;
+}
+#endif
+
+int
+setsockopt (int fd, int level, int optname, const void *optval, socklen_t len)
+{
+  int r = setsockopt_syscall (fd, level, optname, optval, len);
+
+#ifndef __ASSUME_TIME64_SYSCALLS
+  if (r == -1 && errno == ENOPROTOOPT)
+    r = setsockopt32 (fd, level, optname, optval, len);
+#endif
+
+  return r;
+}
 weak_alias (setsockopt, __setsockopt)
diff --git a/sysdeps/unix/sysv/linux/socket-constants-time64.h b/sysdeps/unix/sysv/linux/socket-constants-time64.h
new file mode 100644
index 0000000000..e5a3777f28
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/socket-constants-time64.h
@@ -0,0 +1,30 @@
+/* Compat socket constants used in 64 bit compat code.
+   Copyright (C) 2021 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#ifndef _SOCKET_CONSTANTS_TIME64_H
+#define _SOCKET_CONSTANTS_TIME64_H
+
+/* The compat code requires the SO_* constants used for both 32 and 64 bit
+   time_t, however they were only added on v5.1 kernel.  */
+
+#define COMPAT_SO_RCVTIMEO_OLD 20
+#define COMPAT_SO_SNDTIMEO_OLD 21
+#define COMPAT_SO_RCVTIMEO_NEW 66
+#define COMPAT_SO_SNDTIMEO_NEW 67
+
+#endif
diff --git a/sysdeps/unix/sysv/linux/sparc/socket-constants-time64.h b/sysdeps/unix/sysv/linux/sparc/socket-constants-time64.h
new file mode 100644
index 0000000000..b137abdeea
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/sparc/socket-constants-time64.h
@@ -0,0 +1,30 @@
+/* Compat socket constants used in 64 bit compat code.
+   Copyright (C) 2021 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#ifndef _SOCKET_CONSTANTS_TIME64_H
+#define _SOCKET_CONSTANTS_TIME64_H
+
+/* The compat code requires the SO_* constants used for both 32 and 64 bit
+   time_t, however they were only added on v5.1 kernel.  */
+
+#define COMPAT_SO_RCVTIMEO_OLD 8192
+#define COMPAT_SO_SNDTIMEO_OLD 16384
+#define COMPAT_SO_RCVTIMEO_NEW 68
+#define COMPAT_SO_SNDTIMEO_NEW 69
+
+#endif


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [glibc/azanella/y2038] linux: Add fallback for 64-bit time_t SO_{RCV,  SND}TIMEO
@ 2021-03-05 19:20 Adhemerval Zanella
  0 siblings, 0 replies; 10+ messages in thread
From: Adhemerval Zanella @ 2021-03-05 19:20 UTC (permalink / raw)
  To: glibc-cvs

https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=ebb7ba10fef2b51ecf872b6d9f9ecfe704df63e8

commit ebb7ba10fef2b51ecf872b6d9f9ecfe704df63e8
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Date:   Mon Sep 7 14:13:58 2020 -0300

    linux: Add fallback for 64-bit time_t SO_{RCV,SND}TIMEO
    
    The constant values will be changed for __TIMESIZE=64, so binaries built
    with 64-bit time support might fail to work properly on old kernels.
    Both {get,set}sockopt will retry the syscall with the old constant
    values and the timeout value adjusted when kernel returns ENOTPROTOPT.
    
    It also adds an internal only SO_{RCV,SND}TIMEO where
    COMPAT_SO_{RCV,SND}TIMEO_OLD indicates pre 32 bit time support and
    COMPAT_SO_{RCV,SND}TIMEO_NEW indicates time64 support.  It allows to
    refer to constant independently of the time_t ABI and kernel version
    used.
    
    Checked on x86_64-linux-gnu and i686-linux-gnu (on 5.4 and on 4.15
    kernel).

Diff:
---
 sysdeps/unix/sysv/linux/getsockopt.c               | 67 +++++++++++++++++---
 .../unix/sysv/linux/hppa/socket-constants-time64.h | 30 +++++++++
 .../unix/sysv/linux/mips/socket-constants-time64.h | 30 +++++++++
 .../sysv/linux/powerpc/socket-constants-time64.h   | 30 +++++++++
 sysdeps/unix/sysv/linux/setsockopt.c               | 71 +++++++++++++++++++---
 sysdeps/unix/sysv/linux/socket-constants-time64.h  | 30 +++++++++
 .../sysv/linux/sparc/socket-constants-time64.h     | 30 +++++++++
 7 files changed, 273 insertions(+), 15 deletions(-)

diff --git a/sysdeps/unix/sysv/linux/getsockopt.c b/sysdeps/unix/sysv/linux/getsockopt.c
index 76ee8a94d6..c8e502d976 100644
--- a/sysdeps/unix/sysv/linux/getsockopt.c
+++ b/sysdeps/unix/sysv/linux/getsockopt.c
@@ -15,16 +15,15 @@
    License along with the GNU C Library; if not, see
    <https://www.gnu.org/licenses/>.  */
 
-#include <errno.h>
-#include <signal.h>
 #include <sys/socket.h>
-
+#include <time.h>
+#include <sysdep.h>
 #include <socketcall.h>
-#include <kernel-features.h>
-#include <sys/syscall.h>
+#include <socket-constants-time64.h>
 
-int
-__getsockopt (int fd, int level, int optname, void *optval, socklen_t *len)
+static int
+getsockopt_syscall (int fd, int level, int optname, void *optval,
+		    socklen_t *len)
 {
 #ifdef __ASSUME_GETSOCKOPT_SYSCALL
   return INLINE_SYSCALL (getsockopt, 5, fd, level, optname, optval, len);
@@ -32,4 +31,58 @@ __getsockopt (int fd, int level, int optname, void *optval, socklen_t *len)
   return SOCKETCALL (getsockopt, fd, level, optname, optval, len);
 #endif
 }
+
+#ifndef __ASSUME_TIME64_SYSCALLS
+static int
+getsockopt32 (int fd, int level, int optname, void *optval,
+	      socklen_t *len)
+{
+  int r = -1;
+
+  if (level != SOL_SOCKET)
+    return r;
+
+  switch (optname)
+    {
+    case COMPAT_SO_RCVTIMEO_NEW:
+    case COMPAT_SO_SNDTIMEO_NEW:
+      {
+        if (*len < sizeof (struct __timeval64))
+	  {
+	    __set_errno (EINVAL);
+	    break;
+	  }
+
+	if (optname == COMPAT_SO_RCVTIMEO_NEW)
+	  optname = COMPAT_SO_RCVTIMEO_OLD;
+	if (optname == COMPAT_SO_SNDTIMEO_NEW)
+	  optname = COMPAT_SO_SNDTIMEO_OLD;
+
+	struct __timeval32 tv32;
+	r = getsockopt_syscall (fd, level, optname, &tv32,
+				(socklen_t[]) { sizeof tv32 });
+	if (r < 0)
+	  break;
+	struct __timeval64 *tv64 = (struct __timeval64 *) optval;
+	*tv64 = valid_timeval32_to_timeval64 (tv32);
+	*len = sizeof (*tv64);
+      }
+    }
+
+  return r;
+}
+#endif
+
+int
+__getsockopt (int fd, int level, int optname, void *optval, socklen_t *len)
+{
+  int r = getsockopt_syscall (fd, level, optname, optval, len);
+
+#ifndef __ASSUME_TIME64_SYSCALLS
+  if (r == -1 && errno == ENOPROTOOPT)
+    r = getsockopt32 (fd, level, optname, optval, len);
+#endif
+
+ return r;
+}
 weak_alias (__getsockopt, getsockopt)
diff --git a/sysdeps/unix/sysv/linux/hppa/socket-constants-time64.h b/sysdeps/unix/sysv/linux/hppa/socket-constants-time64.h
new file mode 100644
index 0000000000..ea721e0fc2
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/hppa/socket-constants-time64.h
@@ -0,0 +1,30 @@
+/* Compat socket constants used in 64 bit compat code.
+   Copyright (C) 2021 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#ifndef _SOCKET_CONSTANTS_TIME64_H
+#define _SOCKET_CONSTANTS_TIME64_H
+
+/* The compat code requires the SO_* constants used for both 32 and 64 bit
+   time_t, however they were only added on v5.1 kernel.  */
+
+#define COMPAT_SO_RCVTIMEO_OLD 4102
+#define COMPAT_SO_SNDTIMEO_OLD 4101
+#define COMPAT_SO_RCVTIMEO_NEW 16448
+#define COMPAT_SO_SNDTIMEO_NEW 16449
+
+#endif
diff --git a/sysdeps/unix/sysv/linux/mips/socket-constants-time64.h b/sysdeps/unix/sysv/linux/mips/socket-constants-time64.h
new file mode 100644
index 0000000000..ab8bd62853
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/mips/socket-constants-time64.h
@@ -0,0 +1,30 @@
+/* Compat socket constants used in 64 bit compat code.
+   Copyright (C) 2021 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#ifndef _SOCKET_CONSTANTS_TIME64_H
+#define _SOCKET_CONSTANTS_TIME64_H
+
+/* The compat code requires the SO_* constants used for both 32 and 64 bit
+   time_t, however they were only added on v5.1 kernel.  */
+
+#define COMPAT_SO_RCVTIMEO_OLD 4102
+#define COMPAT_SO_SNDTIMEO_OLD 4101
+#define COMPAT_SO_RCVTIMEO_NEW 66
+#define COMPAT_SO_SNDTIMEO_NEW 67
+
+#endif
diff --git a/sysdeps/unix/sysv/linux/powerpc/socket-constants-time64.h b/sysdeps/unix/sysv/linux/powerpc/socket-constants-time64.h
new file mode 100644
index 0000000000..1e48dcca8d
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/powerpc/socket-constants-time64.h
@@ -0,0 +1,30 @@
+/* Compat socket constants used in 64 bit compat code.
+   Copyright (C) 2021 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#ifndef _SOCKET_CONSTANTS_TIME64_H
+#define _SOCKET_CONSTANTS_TIME64_H
+
+/* The compat code requires the SO_* constants used for both 32 and 64 bit
+   time_t, however they were only added on v5.1 kernel.  */
+
+#define COMPAT_SO_RCVTIMEO_OLD 18
+#define COMPAT_SO_SNDTIMEO_OLD 19
+#define COMPAT_SO_RCVTIMEO_NEW 66
+#define COMPAT_SO_SNDTIMEO_NEW 67
+
+#endif
diff --git a/sysdeps/unix/sysv/linux/setsockopt.c b/sysdeps/unix/sysv/linux/setsockopt.c
index 12fd7bdcde..6505202265 100644
--- a/sysdeps/unix/sysv/linux/setsockopt.c
+++ b/sysdeps/unix/sysv/linux/setsockopt.c
@@ -15,21 +15,76 @@
    License along with the GNU C Library; if not, see
    <https://www.gnu.org/licenses/>.  */
 
-#include <errno.h>
-#include <signal.h>
 #include <sys/socket.h>
-
+#include <time.h>
+#include <sysdep.h>
 #include <socketcall.h>
-#include <kernel-features.h>
-#include <sys/syscall.h>
+#include <socket-constants-time64.h>
 
-int
-setsockopt (int fd, int level, int optname, const void *optval, socklen_t len)
+static int
+setsockopt_syscall (int fd, int level, int optname, const void *optval,
+		    socklen_t len)
 {
 #ifdef __ASSUME_SETSOCKOPT_SYSCALL
-  return INLINE_SYSCALL (setsockopt, 5, fd, level, optname, optval, len);
+  return INLINE_SYSCALL_CALL (setsockopt, fd, level, optname, optval, len);
 #else
   return SOCKETCALL (setsockopt, fd, level, optname, optval, len);
 #endif
 }
+
+#ifndef __ASSUME_TIME64_SYSCALLS
+static int
+setsockopt32 (int fd, int level, int optname, const void *optval,
+	      socklen_t len)
+{
+  int r = -1;
+
+  if (level != SOL_SOCKET)
+    return r;
+
+  switch (optname)
+    {
+    case COMPAT_SO_RCVTIMEO_NEW:
+    case COMPAT_SO_SNDTIMEO_NEW:
+      {
+        if (len < sizeof (struct __timeval64))
+	  {
+	    __set_errno (EINVAL);
+	    break;
+	  }
+
+	struct __timeval64 *tv64 = (struct __timeval64 *) optval;
+	if (! in_time_t_range (tv64->tv_sec))
+	  {
+	    __set_errno (EOVERFLOW);
+	    break;
+	  }
+
+	if (optname == COMPAT_SO_RCVTIMEO_NEW)
+	  optname = COMPAT_SO_RCVTIMEO_OLD;
+	if (optname == COMPAT_SO_SNDTIMEO_NEW)
+	  optname = COMPAT_SO_SNDTIMEO_OLD;
+
+	struct __timeval32 tv32 = valid_timeval64_to_timeval32 (*tv64);
+
+	r = setsockopt_syscall (fd, level, optname, &tv32, sizeof (tv32));
+      }
+    }
+
+  return r;
+}
+#endif
+
+int
+setsockopt (int fd, int level, int optname, const void *optval, socklen_t len)
+{
+  int r = setsockopt_syscall (fd, level, optname, optval, len);
+
+#ifndef __ASSUME_TIME64_SYSCALLS
+  if (r == -1 && errno == ENOPROTOOPT)
+    r = setsockopt32 (fd, level, optname, optval, len);
+#endif
+
+  return r;
+}
 weak_alias (setsockopt, __setsockopt)
diff --git a/sysdeps/unix/sysv/linux/socket-constants-time64.h b/sysdeps/unix/sysv/linux/socket-constants-time64.h
new file mode 100644
index 0000000000..e5a3777f28
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/socket-constants-time64.h
@@ -0,0 +1,30 @@
+/* Compat socket constants used in 64 bit compat code.
+   Copyright (C) 2021 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#ifndef _SOCKET_CONSTANTS_TIME64_H
+#define _SOCKET_CONSTANTS_TIME64_H
+
+/* The compat code requires the SO_* constants used for both 32 and 64 bit
+   time_t, however they were only added on v5.1 kernel.  */
+
+#define COMPAT_SO_RCVTIMEO_OLD 20
+#define COMPAT_SO_SNDTIMEO_OLD 21
+#define COMPAT_SO_RCVTIMEO_NEW 66
+#define COMPAT_SO_SNDTIMEO_NEW 67
+
+#endif
diff --git a/sysdeps/unix/sysv/linux/sparc/socket-constants-time64.h b/sysdeps/unix/sysv/linux/sparc/socket-constants-time64.h
new file mode 100644
index 0000000000..b137abdeea
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/sparc/socket-constants-time64.h
@@ -0,0 +1,30 @@
+/* Compat socket constants used in 64 bit compat code.
+   Copyright (C) 2021 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#ifndef _SOCKET_CONSTANTS_TIME64_H
+#define _SOCKET_CONSTANTS_TIME64_H
+
+/* The compat code requires the SO_* constants used for both 32 and 64 bit
+   time_t, however they were only added on v5.1 kernel.  */
+
+#define COMPAT_SO_RCVTIMEO_OLD 8192
+#define COMPAT_SO_SNDTIMEO_OLD 16384
+#define COMPAT_SO_RCVTIMEO_NEW 68
+#define COMPAT_SO_SNDTIMEO_NEW 69
+
+#endif


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [glibc/azanella/y2038] linux: Add fallback for 64-bit time_t SO_{RCV,  SND}TIMEO
@ 2021-03-04 17:36 Adhemerval Zanella
  0 siblings, 0 replies; 10+ messages in thread
From: Adhemerval Zanella @ 2021-03-04 17:36 UTC (permalink / raw)
  To: glibc-cvs

https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=488ba1600d7804bfb033cfbc5ac9c7d0397081df

commit 488ba1600d7804bfb033cfbc5ac9c7d0397081df
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Date:   Mon Sep 7 14:13:58 2020 -0300

    linux: Add fallback for 64-bit time_t SO_{RCV,SND}TIMEO
    
    The constant values will be changed for __TIMESIZE=64, so binaries built
    with 64-bit time support might fail to work properly on old kernels.
    Both {get,set}sockopt will retry the syscall with the old constant
    values and the timeout value adjusted when kernel returns ENOTPROTOPT.
    
    It also adds an internal only SO_{RCV,SND}TIMEO where
    COMPAT_SO_{RCV,SND}TIMEO_OLD indicates pre 32 bit time support and
    COMPAT_SO_{RCV,SND}TIMEO_NEW indicates time64 support.  It allows to
    refer to constant independently of the time_t ABI and kernel version
    used.
    
    Checked on x86_64-linux-gnu and i686-linux-gnu (on 5.4 and on 4.15
    kernel).

Diff:
---
 sysdeps/unix/sysv/linux/getsockopt.c               | 67 +++++++++++++++++---
 .../unix/sysv/linux/hppa/socket-constants-time64.h | 30 +++++++++
 .../unix/sysv/linux/mips/socket-constants-time64.h | 30 +++++++++
 .../sysv/linux/powerpc/socket-constants-time64.h   | 30 +++++++++
 sysdeps/unix/sysv/linux/setsockopt.c               | 71 +++++++++++++++++++---
 sysdeps/unix/sysv/linux/socket-constants-time64.h  | 30 +++++++++
 .../sysv/linux/sparc/socket-constants-time64.h     | 30 +++++++++
 7 files changed, 273 insertions(+), 15 deletions(-)

diff --git a/sysdeps/unix/sysv/linux/getsockopt.c b/sysdeps/unix/sysv/linux/getsockopt.c
index 76ee8a94d6..c8e502d976 100644
--- a/sysdeps/unix/sysv/linux/getsockopt.c
+++ b/sysdeps/unix/sysv/linux/getsockopt.c
@@ -15,16 +15,15 @@
    License along with the GNU C Library; if not, see
    <https://www.gnu.org/licenses/>.  */
 
-#include <errno.h>
-#include <signal.h>
 #include <sys/socket.h>
-
+#include <time.h>
+#include <sysdep.h>
 #include <socketcall.h>
-#include <kernel-features.h>
-#include <sys/syscall.h>
+#include <socket-constants-time64.h>
 
-int
-__getsockopt (int fd, int level, int optname, void *optval, socklen_t *len)
+static int
+getsockopt_syscall (int fd, int level, int optname, void *optval,
+		    socklen_t *len)
 {
 #ifdef __ASSUME_GETSOCKOPT_SYSCALL
   return INLINE_SYSCALL (getsockopt, 5, fd, level, optname, optval, len);
@@ -32,4 +31,58 @@ __getsockopt (int fd, int level, int optname, void *optval, socklen_t *len)
   return SOCKETCALL (getsockopt, fd, level, optname, optval, len);
 #endif
 }
+
+#ifndef __ASSUME_TIME64_SYSCALLS
+static int
+getsockopt32 (int fd, int level, int optname, void *optval,
+	      socklen_t *len)
+{
+  int r = -1;
+
+  if (level != SOL_SOCKET)
+    return r;
+
+  switch (optname)
+    {
+    case COMPAT_SO_RCVTIMEO_NEW:
+    case COMPAT_SO_SNDTIMEO_NEW:
+      {
+        if (*len < sizeof (struct __timeval64))
+	  {
+	    __set_errno (EINVAL);
+	    break;
+	  }
+
+	if (optname == COMPAT_SO_RCVTIMEO_NEW)
+	  optname = COMPAT_SO_RCVTIMEO_OLD;
+	if (optname == COMPAT_SO_SNDTIMEO_NEW)
+	  optname = COMPAT_SO_SNDTIMEO_OLD;
+
+	struct __timeval32 tv32;
+	r = getsockopt_syscall (fd, level, optname, &tv32,
+				(socklen_t[]) { sizeof tv32 });
+	if (r < 0)
+	  break;
+	struct __timeval64 *tv64 = (struct __timeval64 *) optval;
+	*tv64 = valid_timeval32_to_timeval64 (tv32);
+	*len = sizeof (*tv64);
+      }
+    }
+
+  return r;
+}
+#endif
+
+int
+__getsockopt (int fd, int level, int optname, void *optval, socklen_t *len)
+{
+  int r = getsockopt_syscall (fd, level, optname, optval, len);
+
+#ifndef __ASSUME_TIME64_SYSCALLS
+  if (r == -1 && errno == ENOPROTOOPT)
+    r = getsockopt32 (fd, level, optname, optval, len);
+#endif
+
+ return r;
+}
 weak_alias (__getsockopt, getsockopt)
diff --git a/sysdeps/unix/sysv/linux/hppa/socket-constants-time64.h b/sysdeps/unix/sysv/linux/hppa/socket-constants-time64.h
new file mode 100644
index 0000000000..ea721e0fc2
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/hppa/socket-constants-time64.h
@@ -0,0 +1,30 @@
+/* Compat socket constants used in 64 bit compat code.
+   Copyright (C) 2021 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#ifndef _SOCKET_CONSTANTS_TIME64_H
+#define _SOCKET_CONSTANTS_TIME64_H
+
+/* The compat code requires the SO_* constants used for both 32 and 64 bit
+   time_t, however they were only added on v5.1 kernel.  */
+
+#define COMPAT_SO_RCVTIMEO_OLD 4102
+#define COMPAT_SO_SNDTIMEO_OLD 4101
+#define COMPAT_SO_RCVTIMEO_NEW 16448
+#define COMPAT_SO_SNDTIMEO_NEW 16449
+
+#endif
diff --git a/sysdeps/unix/sysv/linux/mips/socket-constants-time64.h b/sysdeps/unix/sysv/linux/mips/socket-constants-time64.h
new file mode 100644
index 0000000000..ab8bd62853
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/mips/socket-constants-time64.h
@@ -0,0 +1,30 @@
+/* Compat socket constants used in 64 bit compat code.
+   Copyright (C) 2021 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#ifndef _SOCKET_CONSTANTS_TIME64_H
+#define _SOCKET_CONSTANTS_TIME64_H
+
+/* The compat code requires the SO_* constants used for both 32 and 64 bit
+   time_t, however they were only added on v5.1 kernel.  */
+
+#define COMPAT_SO_RCVTIMEO_OLD 4102
+#define COMPAT_SO_SNDTIMEO_OLD 4101
+#define COMPAT_SO_RCVTIMEO_NEW 66
+#define COMPAT_SO_SNDTIMEO_NEW 67
+
+#endif
diff --git a/sysdeps/unix/sysv/linux/powerpc/socket-constants-time64.h b/sysdeps/unix/sysv/linux/powerpc/socket-constants-time64.h
new file mode 100644
index 0000000000..1e48dcca8d
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/powerpc/socket-constants-time64.h
@@ -0,0 +1,30 @@
+/* Compat socket constants used in 64 bit compat code.
+   Copyright (C) 2021 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#ifndef _SOCKET_CONSTANTS_TIME64_H
+#define _SOCKET_CONSTANTS_TIME64_H
+
+/* The compat code requires the SO_* constants used for both 32 and 64 bit
+   time_t, however they were only added on v5.1 kernel.  */
+
+#define COMPAT_SO_RCVTIMEO_OLD 18
+#define COMPAT_SO_SNDTIMEO_OLD 19
+#define COMPAT_SO_RCVTIMEO_NEW 66
+#define COMPAT_SO_SNDTIMEO_NEW 67
+
+#endif
diff --git a/sysdeps/unix/sysv/linux/setsockopt.c b/sysdeps/unix/sysv/linux/setsockopt.c
index 12fd7bdcde..6505202265 100644
--- a/sysdeps/unix/sysv/linux/setsockopt.c
+++ b/sysdeps/unix/sysv/linux/setsockopt.c
@@ -15,21 +15,76 @@
    License along with the GNU C Library; if not, see
    <https://www.gnu.org/licenses/>.  */
 
-#include <errno.h>
-#include <signal.h>
 #include <sys/socket.h>
-
+#include <time.h>
+#include <sysdep.h>
 #include <socketcall.h>
-#include <kernel-features.h>
-#include <sys/syscall.h>
+#include <socket-constants-time64.h>
 
-int
-setsockopt (int fd, int level, int optname, const void *optval, socklen_t len)
+static int
+setsockopt_syscall (int fd, int level, int optname, const void *optval,
+		    socklen_t len)
 {
 #ifdef __ASSUME_SETSOCKOPT_SYSCALL
-  return INLINE_SYSCALL (setsockopt, 5, fd, level, optname, optval, len);
+  return INLINE_SYSCALL_CALL (setsockopt, fd, level, optname, optval, len);
 #else
   return SOCKETCALL (setsockopt, fd, level, optname, optval, len);
 #endif
 }
+
+#ifndef __ASSUME_TIME64_SYSCALLS
+static int
+setsockopt32 (int fd, int level, int optname, const void *optval,
+	      socklen_t len)
+{
+  int r = -1;
+
+  if (level != SOL_SOCKET)
+    return r;
+
+  switch (optname)
+    {
+    case COMPAT_SO_RCVTIMEO_NEW:
+    case COMPAT_SO_SNDTIMEO_NEW:
+      {
+        if (len < sizeof (struct __timeval64))
+	  {
+	    __set_errno (EINVAL);
+	    break;
+	  }
+
+	struct __timeval64 *tv64 = (struct __timeval64 *) optval;
+	if (! in_time_t_range (tv64->tv_sec))
+	  {
+	    __set_errno (EOVERFLOW);
+	    break;
+	  }
+
+	if (optname == COMPAT_SO_RCVTIMEO_NEW)
+	  optname = COMPAT_SO_RCVTIMEO_OLD;
+	if (optname == COMPAT_SO_SNDTIMEO_NEW)
+	  optname = COMPAT_SO_SNDTIMEO_OLD;
+
+	struct __timeval32 tv32 = valid_timeval64_to_timeval32 (*tv64);
+
+	r = setsockopt_syscall (fd, level, optname, &tv32, sizeof (tv32));
+      }
+    }
+
+  return r;
+}
+#endif
+
+int
+setsockopt (int fd, int level, int optname, const void *optval, socklen_t len)
+{
+  int r = setsockopt_syscall (fd, level, optname, optval, len);
+
+#ifndef __ASSUME_TIME64_SYSCALLS
+  if (r == -1 && errno == ENOPROTOOPT)
+    r = setsockopt32 (fd, level, optname, optval, len);
+#endif
+
+  return r;
+}
 weak_alias (setsockopt, __setsockopt)
diff --git a/sysdeps/unix/sysv/linux/socket-constants-time64.h b/sysdeps/unix/sysv/linux/socket-constants-time64.h
new file mode 100644
index 0000000000..e5a3777f28
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/socket-constants-time64.h
@@ -0,0 +1,30 @@
+/* Compat socket constants used in 64 bit compat code.
+   Copyright (C) 2021 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#ifndef _SOCKET_CONSTANTS_TIME64_H
+#define _SOCKET_CONSTANTS_TIME64_H
+
+/* The compat code requires the SO_* constants used for both 32 and 64 bit
+   time_t, however they were only added on v5.1 kernel.  */
+
+#define COMPAT_SO_RCVTIMEO_OLD 20
+#define COMPAT_SO_SNDTIMEO_OLD 21
+#define COMPAT_SO_RCVTIMEO_NEW 66
+#define COMPAT_SO_SNDTIMEO_NEW 67
+
+#endif
diff --git a/sysdeps/unix/sysv/linux/sparc/socket-constants-time64.h b/sysdeps/unix/sysv/linux/sparc/socket-constants-time64.h
new file mode 100644
index 0000000000..b137abdeea
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/sparc/socket-constants-time64.h
@@ -0,0 +1,30 @@
+/* Compat socket constants used in 64 bit compat code.
+   Copyright (C) 2021 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#ifndef _SOCKET_CONSTANTS_TIME64_H
+#define _SOCKET_CONSTANTS_TIME64_H
+
+/* The compat code requires the SO_* constants used for both 32 and 64 bit
+   time_t, however they were only added on v5.1 kernel.  */
+
+#define COMPAT_SO_RCVTIMEO_OLD 8192
+#define COMPAT_SO_SNDTIMEO_OLD 16384
+#define COMPAT_SO_RCVTIMEO_NEW 68
+#define COMPAT_SO_SNDTIMEO_NEW 69
+
+#endif


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [glibc/azanella/y2038] linux: Add fallback for 64-bit time_t SO_{RCV,  SND}TIMEO
@ 2021-03-04 11:28 Adhemerval Zanella
  0 siblings, 0 replies; 10+ messages in thread
From: Adhemerval Zanella @ 2021-03-04 11:28 UTC (permalink / raw)
  To: glibc-cvs

https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=488ba1600d7804bfb033cfbc5ac9c7d0397081df

commit 488ba1600d7804bfb033cfbc5ac9c7d0397081df
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Date:   Mon Sep 7 14:13:58 2020 -0300

    linux: Add fallback for 64-bit time_t SO_{RCV,SND}TIMEO
    
    The constant values will be changed for __TIMESIZE=64, so binaries built
    with 64-bit time support might fail to work properly on old kernels.
    Both {get,set}sockopt will retry the syscall with the old constant
    values and the timeout value adjusted when kernel returns ENOTPROTOPT.
    
    It also adds an internal only SO_{RCV,SND}TIMEO where
    COMPAT_SO_{RCV,SND}TIMEO_OLD indicates pre 32 bit time support and
    COMPAT_SO_{RCV,SND}TIMEO_NEW indicates time64 support.  It allows to
    refer to constant independently of the time_t ABI and kernel version
    used.
    
    Checked on x86_64-linux-gnu and i686-linux-gnu (on 5.4 and on 4.15
    kernel).

Diff:
---
 sysdeps/unix/sysv/linux/getsockopt.c               | 67 +++++++++++++++++---
 .../unix/sysv/linux/hppa/socket-constants-time64.h | 30 +++++++++
 .../unix/sysv/linux/mips/socket-constants-time64.h | 30 +++++++++
 .../sysv/linux/powerpc/socket-constants-time64.h   | 30 +++++++++
 sysdeps/unix/sysv/linux/setsockopt.c               | 71 +++++++++++++++++++---
 sysdeps/unix/sysv/linux/socket-constants-time64.h  | 30 +++++++++
 .../sysv/linux/sparc/socket-constants-time64.h     | 30 +++++++++
 7 files changed, 273 insertions(+), 15 deletions(-)

diff --git a/sysdeps/unix/sysv/linux/getsockopt.c b/sysdeps/unix/sysv/linux/getsockopt.c
index 76ee8a94d6..c8e502d976 100644
--- a/sysdeps/unix/sysv/linux/getsockopt.c
+++ b/sysdeps/unix/sysv/linux/getsockopt.c
@@ -15,16 +15,15 @@
    License along with the GNU C Library; if not, see
    <https://www.gnu.org/licenses/>.  */
 
-#include <errno.h>
-#include <signal.h>
 #include <sys/socket.h>
-
+#include <time.h>
+#include <sysdep.h>
 #include <socketcall.h>
-#include <kernel-features.h>
-#include <sys/syscall.h>
+#include <socket-constants-time64.h>
 
-int
-__getsockopt (int fd, int level, int optname, void *optval, socklen_t *len)
+static int
+getsockopt_syscall (int fd, int level, int optname, void *optval,
+		    socklen_t *len)
 {
 #ifdef __ASSUME_GETSOCKOPT_SYSCALL
   return INLINE_SYSCALL (getsockopt, 5, fd, level, optname, optval, len);
@@ -32,4 +31,58 @@ __getsockopt (int fd, int level, int optname, void *optval, socklen_t *len)
   return SOCKETCALL (getsockopt, fd, level, optname, optval, len);
 #endif
 }
+
+#ifndef __ASSUME_TIME64_SYSCALLS
+static int
+getsockopt32 (int fd, int level, int optname, void *optval,
+	      socklen_t *len)
+{
+  int r = -1;
+
+  if (level != SOL_SOCKET)
+    return r;
+
+  switch (optname)
+    {
+    case COMPAT_SO_RCVTIMEO_NEW:
+    case COMPAT_SO_SNDTIMEO_NEW:
+      {
+        if (*len < sizeof (struct __timeval64))
+	  {
+	    __set_errno (EINVAL);
+	    break;
+	  }
+
+	if (optname == COMPAT_SO_RCVTIMEO_NEW)
+	  optname = COMPAT_SO_RCVTIMEO_OLD;
+	if (optname == COMPAT_SO_SNDTIMEO_NEW)
+	  optname = COMPAT_SO_SNDTIMEO_OLD;
+
+	struct __timeval32 tv32;
+	r = getsockopt_syscall (fd, level, optname, &tv32,
+				(socklen_t[]) { sizeof tv32 });
+	if (r < 0)
+	  break;
+	struct __timeval64 *tv64 = (struct __timeval64 *) optval;
+	*tv64 = valid_timeval32_to_timeval64 (tv32);
+	*len = sizeof (*tv64);
+      }
+    }
+
+  return r;
+}
+#endif
+
+int
+__getsockopt (int fd, int level, int optname, void *optval, socklen_t *len)
+{
+  int r = getsockopt_syscall (fd, level, optname, optval, len);
+
+#ifndef __ASSUME_TIME64_SYSCALLS
+  if (r == -1 && errno == ENOPROTOOPT)
+    r = getsockopt32 (fd, level, optname, optval, len);
+#endif
+
+ return r;
+}
 weak_alias (__getsockopt, getsockopt)
diff --git a/sysdeps/unix/sysv/linux/hppa/socket-constants-time64.h b/sysdeps/unix/sysv/linux/hppa/socket-constants-time64.h
new file mode 100644
index 0000000000..ea721e0fc2
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/hppa/socket-constants-time64.h
@@ -0,0 +1,30 @@
+/* Compat socket constants used in 64 bit compat code.
+   Copyright (C) 2021 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#ifndef _SOCKET_CONSTANTS_TIME64_H
+#define _SOCKET_CONSTANTS_TIME64_H
+
+/* The compat code requires the SO_* constants used for both 32 and 64 bit
+   time_t, however they were only added on v5.1 kernel.  */
+
+#define COMPAT_SO_RCVTIMEO_OLD 4102
+#define COMPAT_SO_SNDTIMEO_OLD 4101
+#define COMPAT_SO_RCVTIMEO_NEW 16448
+#define COMPAT_SO_SNDTIMEO_NEW 16449
+
+#endif
diff --git a/sysdeps/unix/sysv/linux/mips/socket-constants-time64.h b/sysdeps/unix/sysv/linux/mips/socket-constants-time64.h
new file mode 100644
index 0000000000..ab8bd62853
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/mips/socket-constants-time64.h
@@ -0,0 +1,30 @@
+/* Compat socket constants used in 64 bit compat code.
+   Copyright (C) 2021 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#ifndef _SOCKET_CONSTANTS_TIME64_H
+#define _SOCKET_CONSTANTS_TIME64_H
+
+/* The compat code requires the SO_* constants used for both 32 and 64 bit
+   time_t, however they were only added on v5.1 kernel.  */
+
+#define COMPAT_SO_RCVTIMEO_OLD 4102
+#define COMPAT_SO_SNDTIMEO_OLD 4101
+#define COMPAT_SO_RCVTIMEO_NEW 66
+#define COMPAT_SO_SNDTIMEO_NEW 67
+
+#endif
diff --git a/sysdeps/unix/sysv/linux/powerpc/socket-constants-time64.h b/sysdeps/unix/sysv/linux/powerpc/socket-constants-time64.h
new file mode 100644
index 0000000000..1e48dcca8d
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/powerpc/socket-constants-time64.h
@@ -0,0 +1,30 @@
+/* Compat socket constants used in 64 bit compat code.
+   Copyright (C) 2021 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#ifndef _SOCKET_CONSTANTS_TIME64_H
+#define _SOCKET_CONSTANTS_TIME64_H
+
+/* The compat code requires the SO_* constants used for both 32 and 64 bit
+   time_t, however they were only added on v5.1 kernel.  */
+
+#define COMPAT_SO_RCVTIMEO_OLD 18
+#define COMPAT_SO_SNDTIMEO_OLD 19
+#define COMPAT_SO_RCVTIMEO_NEW 66
+#define COMPAT_SO_SNDTIMEO_NEW 67
+
+#endif
diff --git a/sysdeps/unix/sysv/linux/setsockopt.c b/sysdeps/unix/sysv/linux/setsockopt.c
index 12fd7bdcde..6505202265 100644
--- a/sysdeps/unix/sysv/linux/setsockopt.c
+++ b/sysdeps/unix/sysv/linux/setsockopt.c
@@ -15,21 +15,76 @@
    License along with the GNU C Library; if not, see
    <https://www.gnu.org/licenses/>.  */
 
-#include <errno.h>
-#include <signal.h>
 #include <sys/socket.h>
-
+#include <time.h>
+#include <sysdep.h>
 #include <socketcall.h>
-#include <kernel-features.h>
-#include <sys/syscall.h>
+#include <socket-constants-time64.h>
 
-int
-setsockopt (int fd, int level, int optname, const void *optval, socklen_t len)
+static int
+setsockopt_syscall (int fd, int level, int optname, const void *optval,
+		    socklen_t len)
 {
 #ifdef __ASSUME_SETSOCKOPT_SYSCALL
-  return INLINE_SYSCALL (setsockopt, 5, fd, level, optname, optval, len);
+  return INLINE_SYSCALL_CALL (setsockopt, fd, level, optname, optval, len);
 #else
   return SOCKETCALL (setsockopt, fd, level, optname, optval, len);
 #endif
 }
+
+#ifndef __ASSUME_TIME64_SYSCALLS
+static int
+setsockopt32 (int fd, int level, int optname, const void *optval,
+	      socklen_t len)
+{
+  int r = -1;
+
+  if (level != SOL_SOCKET)
+    return r;
+
+  switch (optname)
+    {
+    case COMPAT_SO_RCVTIMEO_NEW:
+    case COMPAT_SO_SNDTIMEO_NEW:
+      {
+        if (len < sizeof (struct __timeval64))
+	  {
+	    __set_errno (EINVAL);
+	    break;
+	  }
+
+	struct __timeval64 *tv64 = (struct __timeval64 *) optval;
+	if (! in_time_t_range (tv64->tv_sec))
+	  {
+	    __set_errno (EOVERFLOW);
+	    break;
+	  }
+
+	if (optname == COMPAT_SO_RCVTIMEO_NEW)
+	  optname = COMPAT_SO_RCVTIMEO_OLD;
+	if (optname == COMPAT_SO_SNDTIMEO_NEW)
+	  optname = COMPAT_SO_SNDTIMEO_OLD;
+
+	struct __timeval32 tv32 = valid_timeval64_to_timeval32 (*tv64);
+
+	r = setsockopt_syscall (fd, level, optname, &tv32, sizeof (tv32));
+      }
+    }
+
+  return r;
+}
+#endif
+
+int
+setsockopt (int fd, int level, int optname, const void *optval, socklen_t len)
+{
+  int r = setsockopt_syscall (fd, level, optname, optval, len);
+
+#ifndef __ASSUME_TIME64_SYSCALLS
+  if (r == -1 && errno == ENOPROTOOPT)
+    r = setsockopt32 (fd, level, optname, optval, len);
+#endif
+
+  return r;
+}
 weak_alias (setsockopt, __setsockopt)
diff --git a/sysdeps/unix/sysv/linux/socket-constants-time64.h b/sysdeps/unix/sysv/linux/socket-constants-time64.h
new file mode 100644
index 0000000000..e5a3777f28
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/socket-constants-time64.h
@@ -0,0 +1,30 @@
+/* Compat socket constants used in 64 bit compat code.
+   Copyright (C) 2021 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#ifndef _SOCKET_CONSTANTS_TIME64_H
+#define _SOCKET_CONSTANTS_TIME64_H
+
+/* The compat code requires the SO_* constants used for both 32 and 64 bit
+   time_t, however they were only added on v5.1 kernel.  */
+
+#define COMPAT_SO_RCVTIMEO_OLD 20
+#define COMPAT_SO_SNDTIMEO_OLD 21
+#define COMPAT_SO_RCVTIMEO_NEW 66
+#define COMPAT_SO_SNDTIMEO_NEW 67
+
+#endif
diff --git a/sysdeps/unix/sysv/linux/sparc/socket-constants-time64.h b/sysdeps/unix/sysv/linux/sparc/socket-constants-time64.h
new file mode 100644
index 0000000000..b137abdeea
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/sparc/socket-constants-time64.h
@@ -0,0 +1,30 @@
+/* Compat socket constants used in 64 bit compat code.
+   Copyright (C) 2021 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#ifndef _SOCKET_CONSTANTS_TIME64_H
+#define _SOCKET_CONSTANTS_TIME64_H
+
+/* The compat code requires the SO_* constants used for both 32 and 64 bit
+   time_t, however they were only added on v5.1 kernel.  */
+
+#define COMPAT_SO_RCVTIMEO_OLD 8192
+#define COMPAT_SO_SNDTIMEO_OLD 16384
+#define COMPAT_SO_RCVTIMEO_NEW 68
+#define COMPAT_SO_SNDTIMEO_NEW 69
+
+#endif


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [glibc/azanella/y2038] linux: Add fallback for 64-bit time_t SO_{RCV,  SND}TIMEO
@ 2021-03-02 12:30 Adhemerval Zanella
  0 siblings, 0 replies; 10+ messages in thread
From: Adhemerval Zanella @ 2021-03-02 12:30 UTC (permalink / raw)
  To: glibc-cvs

https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=7a9d006fb51b7889580a07a7fc874a3354c3b9df

commit 7a9d006fb51b7889580a07a7fc874a3354c3b9df
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Date:   Mon Sep 7 14:13:58 2020 -0300

    linux: Add fallback for 64-bit time_t SO_{RCV,SND}TIMEO
    
    The constant value will be changed for __TIMESIZE=64, so binaries built
    with 64-bit time support might fail to work properly on old kernels.
    Both {get,set}sockopt will retry the syscall with the old constant
    values and the timeout value adjusted when kernel returns ENOTPROTOPT.
    
    It also changes to SO_{RCV,SND}TIMEO to follow the uapi kernel values
    where SO_{RCV,SND}TIMEO_OLD indicates pre 64-bit time support and
    SO_{RCV,SND}TIMEO_NEW indicate time64 support.  It allows to refer to
    constant independently of the time_t abi used (since kernel defines
    SO_{RCV,SND}TIMEO depending of the time_t size).
    
    The hppa, mips, powerpc, and sparc provides its own socket-constant.h,
    which are also updated the the SO_{RCV,SND}TIMEO constants (they are
    missed on 019d828669df966 patch).
    
    Checked on x86_64-linux-gnu and i686-linux-gnu (on 5.4 and on 4.15
    kernel).

Diff:
---
 sysdeps/unix/sysv/linux/bits/socket-constants.h    |  5 ++
 sysdeps/unix/sysv/linux/getsockopt.c               | 72 ++++++++++++++++++--
 .../unix/sysv/linux/hppa/bits/socket-constants.h   |  5 ++
 .../unix/sysv/linux/mips/bits/socket-constants.h   |  5 ++
 .../sysv/linux/powerpc/bits/socket-constants.h     |  5 ++
 sysdeps/unix/sysv/linux/setsockopt.c               | 76 +++++++++++++++++++---
 .../unix/sysv/linux/sparc/bits/socket-constants.h  |  5 ++
 7 files changed, 158 insertions(+), 15 deletions(-)

diff --git a/sysdeps/unix/sysv/linux/bits/socket-constants.h b/sysdeps/unix/sysv/linux/bits/socket-constants.h
index 84f7a333a2..d4e9979605 100644
--- a/sysdeps/unix/sysv/linux/bits/socket-constants.h
+++ b/sysdeps/unix/sysv/linux/bits/socket-constants.h
@@ -48,3 +48,8 @@
 # define SO_SNDTIMEO 21
 #endif
 #define SO_TYPE 3
+
+#define SO_RCVTIMEO_OLD 20
+#define SO_SNDTIMEO_OLD 21
+#define SO_RCVTIMEO_NEW 66
+#define SO_SNDTIMEO_NEW 67
diff --git a/sysdeps/unix/sysv/linux/getsockopt.c b/sysdeps/unix/sysv/linux/getsockopt.c
index 76ee8a94d6..a093e42f96 100644
--- a/sysdeps/unix/sysv/linux/getsockopt.c
+++ b/sysdeps/unix/sysv/linux/getsockopt.c
@@ -15,16 +15,20 @@
    License along with the GNU C Library; if not, see
    <https://www.gnu.org/licenses/>.  */
 
-#include <errno.h>
-#include <signal.h>
 #include <sys/socket.h>
-
+/* The kernel header with SO_* constants is used as default for _GNU_SOURCE,
+   however the new constants that describe 64-bit time support were added
+   only on v5.1.  */
+#if !defined(SO_RCVTIMEO_NEW) || !defined(SO_RCVTIMEO_OLD)
+# include <bits/socket-constants.h>
+#endif
+#include <time.h>
+#include <sysdep.h>
 #include <socketcall.h>
-#include <kernel-features.h>
-#include <sys/syscall.h>
 
-int
-__getsockopt (int fd, int level, int optname, void *optval, socklen_t *len)
+static int
+getsockopt_syscall (int fd, int level, int optname, void *optval,
+		    socklen_t *len)
 {
 #ifdef __ASSUME_GETSOCKOPT_SYSCALL
   return INLINE_SYSCALL (getsockopt, 5, fd, level, optname, optval, len);
@@ -32,4 +36,58 @@ __getsockopt (int fd, int level, int optname, void *optval, socklen_t *len)
   return SOCKETCALL (getsockopt, fd, level, optname, optval, len);
 #endif
 }
+
+#ifndef __ASSUME_TIME64_SYSCALLS
+static int
+getsockopt32 (int fd, int level, int optname, void *optval,
+	      socklen_t *len)
+{
+  int r = -1;
+
+  if (level != SOL_SOCKET)
+    return r;
+
+  switch (optname)
+    {
+    case SO_RCVTIMEO_NEW:
+    case SO_SNDTIMEO_NEW:
+      {
+        if (*len < sizeof (struct __timeval64))
+	  {
+	    __set_errno (EINVAL);
+	    break;
+	  }
+
+	if (optname == SO_RCVTIMEO_NEW)
+	  optname = SO_RCVTIMEO_OLD;
+	if (optname == SO_SNDTIMEO_NEW)
+	  optname = SO_SNDTIMEO_OLD;
+
+	struct __timeval32 tv32;
+	r = getsockopt_syscall (fd, level, optname, &tv32,
+				(socklen_t[]) { sizeof tv32 });
+	if (r < 0)
+	  break;
+	struct __timeval64 *tv64 = (struct __timeval64 *) optval;
+	*tv64 = valid_timeval32_to_timeval64 (tv32);
+	*len = sizeof (*tv64);
+      }
+    }
+
+  return r;
+}
+#endif
+
+int
+__getsockopt (int fd, int level, int optname, void *optval, socklen_t *len)
+{
+  int r = getsockopt_syscall (fd, level, optname, optval, len);
+
+#ifndef __ASSUME_TIME64_SYSCALLS
+  if (r == -1 && errno == ENOPROTOOPT)
+    r = getsockopt32 (fd, level, optname, optval, len);
+#endif
+
+ return r;
+}
 weak_alias (__getsockopt, getsockopt)
diff --git a/sysdeps/unix/sysv/linux/hppa/bits/socket-constants.h b/sysdeps/unix/sysv/linux/hppa/bits/socket-constants.h
index b4fb65b9f1..2413529e71 100644
--- a/sysdeps/unix/sysv/linux/hppa/bits/socket-constants.h
+++ b/sysdeps/unix/sysv/linux/hppa/bits/socket-constants.h
@@ -36,3 +36,8 @@
 #define SO_SNDLOWAT 4099
 #define SO_SNDTIMEO 4101
 #define SO_TYPE 4104
+
+#define SO_RCVTIMEO_OLD 4102
+#define SO_SNDTIMEO_OLD 4101
+#define SO_RCVTIMEO_NEW 16448
+#define SO_SNDTIMEO_NEW 16449
diff --git a/sysdeps/unix/sysv/linux/mips/bits/socket-constants.h b/sysdeps/unix/sysv/linux/mips/bits/socket-constants.h
index a5264536e9..2c124234fc 100644
--- a/sysdeps/unix/sysv/linux/mips/bits/socket-constants.h
+++ b/sysdeps/unix/sysv/linux/mips/bits/socket-constants.h
@@ -36,3 +36,8 @@
 #define SO_SNDLOWAT 4099
 #define SO_SNDTIMEO 4101
 #define SO_TYPE 4104
+
+#define SO_RCVTIMEO_OLD 4102
+#define SO_SNDTIMEO_OLD 4101
+#define SO_RCVTIMEO_NEW 66
+#define SO_SNDTIMEO_NEW 67
diff --git a/sysdeps/unix/sysv/linux/powerpc/bits/socket-constants.h b/sysdeps/unix/sysv/linux/powerpc/bits/socket-constants.h
index f35488b375..682a3f4608 100644
--- a/sysdeps/unix/sysv/linux/powerpc/bits/socket-constants.h
+++ b/sysdeps/unix/sysv/linux/powerpc/bits/socket-constants.h
@@ -36,3 +36,8 @@
 #define SO_SNDLOWAT 17
 #define SO_SNDTIMEO 19
 #define SO_TYPE 3
+
+#define SO_RCVTIMEO_OLD 18
+#define SO_SNDTIMEO_OLD 19
+#define SO_RCVTIMEO_NEW 66
+#define SO_SNDTIMEO_NEW 67
diff --git a/sysdeps/unix/sysv/linux/setsockopt.c b/sysdeps/unix/sysv/linux/setsockopt.c
index 12fd7bdcde..a1f98a937d 100644
--- a/sysdeps/unix/sysv/linux/setsockopt.c
+++ b/sysdeps/unix/sysv/linux/setsockopt.c
@@ -15,21 +15,81 @@
    License along with the GNU C Library; if not, see
    <https://www.gnu.org/licenses/>.  */
 
-#include <errno.h>
-#include <signal.h>
 #include <sys/socket.h>
-
+/* The kernel header with SO_* constants is used as default for _GNU_SOURCE,
+   however the new constants that describe 64-bit time support were added
+   only on v5.1.  */
+#if !defined(SO_RCVTIMEO_NEW) || !defined(SO_RCVTIMEO_OLD)
+# include <bits/socket-constants.h>
+#endif
+#include <time.h>
+#include <sysdep.h>
 #include <socketcall.h>
-#include <kernel-features.h>
-#include <sys/syscall.h>
 
-int
-setsockopt (int fd, int level, int optname, const void *optval, socklen_t len)
+static int
+setsockopt_syscall (int fd, int level, int optname, const void *optval,
+		    socklen_t len)
 {
 #ifdef __ASSUME_SETSOCKOPT_SYSCALL
-  return INLINE_SYSCALL (setsockopt, 5, fd, level, optname, optval, len);
+  return INLINE_SYSCALL_CALL (setsockopt, fd, level, optname, optval, len);
 #else
   return SOCKETCALL (setsockopt, fd, level, optname, optval, len);
 #endif
 }
+
+#ifndef __ASSUME_TIME64_SYSCALLS
+static int
+setsockopt32 (int fd, int level, int optname, const void *optval,
+	      socklen_t len)
+{
+  int r = -1;
+
+  if (level != SOL_SOCKET)
+    return r;
+
+  switch (optname)
+    {
+    case SO_RCVTIMEO_NEW:
+    case SO_SNDTIMEO_NEW:
+      {
+        if (len < sizeof (struct __timeval64))
+	  {
+	    __set_errno (EINVAL);
+	    break;
+	  }
+
+	struct __timeval64 *tv64 = (struct __timeval64 *) optval;
+	if (! in_time_t_range (tv64->tv_sec))
+	  {
+	    __set_errno (EOVERFLOW);
+	    break;
+	  }
+
+	if (optname == SO_RCVTIMEO_NEW)
+	  optname = SO_RCVTIMEO_OLD;
+	if (optname == SO_SNDTIMEO_NEW)
+	  optname = SO_SNDTIMEO_OLD;
+
+	struct __timeval32 tv32 = valid_timeval64_to_timeval32 (*tv64);
+
+	r = setsockopt_syscall (fd, level, optname, &tv32, sizeof (tv32));
+      }
+    }
+
+  return r;
+}
+#endif
+
+int
+setsockopt (int fd, int level, int optname, const void *optval, socklen_t len)
+{
+  int r = setsockopt_syscall (fd, level, optname, optval, len);
+
+#ifndef __ASSUME_TIME64_SYSCALLS
+  if (r == -1 && errno == ENOPROTOOPT)
+    r = setsockopt32 (fd, level, optname, optval, len);
+#endif
+
+  return r;
+}
 weak_alias (setsockopt, __setsockopt)
diff --git a/sysdeps/unix/sysv/linux/sparc/bits/socket-constants.h b/sysdeps/unix/sysv/linux/sparc/bits/socket-constants.h
index a58d0b5f96..3eae2176dd 100644
--- a/sysdeps/unix/sysv/linux/sparc/bits/socket-constants.h
+++ b/sysdeps/unix/sysv/linux/sparc/bits/socket-constants.h
@@ -36,3 +36,8 @@
 #define SO_SNDLOWAT 4096
 #define SO_SNDTIMEO 16384
 #define SO_TYPE 4104
+
+#define SO_RCVTIMEO_OLD 8192
+#define SO_SNDTIMEO_OLD 16384
+#define SO_RCVTIMEO_NEW 68
+#define SO_SNDTIMEO_NEW 69


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [glibc/azanella/y2038] linux: Add fallback for 64-bit time_t SO_{RCV,  SND}TIMEO
@ 2021-03-01 17:35 Adhemerval Zanella
  0 siblings, 0 replies; 10+ messages in thread
From: Adhemerval Zanella @ 2021-03-01 17:35 UTC (permalink / raw)
  To: glibc-cvs

https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=5411ac72e82ade5debd7accd1ed0c2c245032fed

commit 5411ac72e82ade5debd7accd1ed0c2c245032fed
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Date:   Mon Sep 7 14:13:58 2020 -0300

    linux: Add fallback for 64-bit time_t SO_{RCV,SND}TIMEO
    
    The constant value will be changed for __TIMESIZE=64, so binaries built
    with 64-bit time support might fail to work properly on old kernels.
    Both {get,set}sockopt will retry the syscall with the old constant
    values and the timeout value adjusted when kernel returns ENOTPROTOPT.
    
    It also changes to SO_{RCV,SND}TIMEO to follow the uapi kernel values
    where SO_{RCV,SND}TIMEO_OLD indicates pre 64-bit time support and
    SO_{RCV,SND}TIMEO_NEW indicate time64 support.  It allows to refer to
    constant independently of the time_t abi used (since kernel defines
    SO_{RCV,SND}TIMEO depending of the time_t size).
    
    The hppa, mips, powerpc, and sparc provides its own socket-constant.h,
    which are also updated the the SO_{RCV,SND}TIMEO constants (they are
    missed on 019d828669df966 patch).
    
    Checked on x86_64-linux-gnu and i686-linux-gnu (on 5.4 and on 4.15
    kernel).

Diff:
---
 sysdeps/unix/sysv/linux/bits/socket-constants.h    |  5 ++
 sysdeps/unix/sysv/linux/getsockopt.c               | 72 ++++++++++++++++++--
 .../unix/sysv/linux/hppa/bits/socket-constants.h   |  5 ++
 .../unix/sysv/linux/mips/bits/socket-constants.h   |  5 ++
 .../sysv/linux/powerpc/bits/socket-constants.h     |  5 ++
 sysdeps/unix/sysv/linux/setsockopt.c               | 76 +++++++++++++++++++---
 .../unix/sysv/linux/sparc/bits/socket-constants.h  |  5 ++
 7 files changed, 158 insertions(+), 15 deletions(-)

diff --git a/sysdeps/unix/sysv/linux/bits/socket-constants.h b/sysdeps/unix/sysv/linux/bits/socket-constants.h
index 84f7a333a2..d4e9979605 100644
--- a/sysdeps/unix/sysv/linux/bits/socket-constants.h
+++ b/sysdeps/unix/sysv/linux/bits/socket-constants.h
@@ -48,3 +48,8 @@
 # define SO_SNDTIMEO 21
 #endif
 #define SO_TYPE 3
+
+#define SO_RCVTIMEO_OLD 20
+#define SO_SNDTIMEO_OLD 21
+#define SO_RCVTIMEO_NEW 66
+#define SO_SNDTIMEO_NEW 67
diff --git a/sysdeps/unix/sysv/linux/getsockopt.c b/sysdeps/unix/sysv/linux/getsockopt.c
index 76ee8a94d6..a093e42f96 100644
--- a/sysdeps/unix/sysv/linux/getsockopt.c
+++ b/sysdeps/unix/sysv/linux/getsockopt.c
@@ -15,16 +15,20 @@
    License along with the GNU C Library; if not, see
    <https://www.gnu.org/licenses/>.  */
 
-#include <errno.h>
-#include <signal.h>
 #include <sys/socket.h>
-
+/* The kernel header with SO_* constants is used as default for _GNU_SOURCE,
+   however the new constants that describe 64-bit time support were added
+   only on v5.1.  */
+#if !defined(SO_RCVTIMEO_NEW) || !defined(SO_RCVTIMEO_OLD)
+# include <bits/socket-constants.h>
+#endif
+#include <time.h>
+#include <sysdep.h>
 #include <socketcall.h>
-#include <kernel-features.h>
-#include <sys/syscall.h>
 
-int
-__getsockopt (int fd, int level, int optname, void *optval, socklen_t *len)
+static int
+getsockopt_syscall (int fd, int level, int optname, void *optval,
+		    socklen_t *len)
 {
 #ifdef __ASSUME_GETSOCKOPT_SYSCALL
   return INLINE_SYSCALL (getsockopt, 5, fd, level, optname, optval, len);
@@ -32,4 +36,58 @@ __getsockopt (int fd, int level, int optname, void *optval, socklen_t *len)
   return SOCKETCALL (getsockopt, fd, level, optname, optval, len);
 #endif
 }
+
+#ifndef __ASSUME_TIME64_SYSCALLS
+static int
+getsockopt32 (int fd, int level, int optname, void *optval,
+	      socklen_t *len)
+{
+  int r = -1;
+
+  if (level != SOL_SOCKET)
+    return r;
+
+  switch (optname)
+    {
+    case SO_RCVTIMEO_NEW:
+    case SO_SNDTIMEO_NEW:
+      {
+        if (*len < sizeof (struct __timeval64))
+	  {
+	    __set_errno (EINVAL);
+	    break;
+	  }
+
+	if (optname == SO_RCVTIMEO_NEW)
+	  optname = SO_RCVTIMEO_OLD;
+	if (optname == SO_SNDTIMEO_NEW)
+	  optname = SO_SNDTIMEO_OLD;
+
+	struct __timeval32 tv32;
+	r = getsockopt_syscall (fd, level, optname, &tv32,
+				(socklen_t[]) { sizeof tv32 });
+	if (r < 0)
+	  break;
+	struct __timeval64 *tv64 = (struct __timeval64 *) optval;
+	*tv64 = valid_timeval32_to_timeval64 (tv32);
+	*len = sizeof (*tv64);
+      }
+    }
+
+  return r;
+}
+#endif
+
+int
+__getsockopt (int fd, int level, int optname, void *optval, socklen_t *len)
+{
+  int r = getsockopt_syscall (fd, level, optname, optval, len);
+
+#ifndef __ASSUME_TIME64_SYSCALLS
+  if (r == -1 && errno == ENOPROTOOPT)
+    r = getsockopt32 (fd, level, optname, optval, len);
+#endif
+
+ return r;
+}
 weak_alias (__getsockopt, getsockopt)
diff --git a/sysdeps/unix/sysv/linux/hppa/bits/socket-constants.h b/sysdeps/unix/sysv/linux/hppa/bits/socket-constants.h
index b4fb65b9f1..2413529e71 100644
--- a/sysdeps/unix/sysv/linux/hppa/bits/socket-constants.h
+++ b/sysdeps/unix/sysv/linux/hppa/bits/socket-constants.h
@@ -36,3 +36,8 @@
 #define SO_SNDLOWAT 4099
 #define SO_SNDTIMEO 4101
 #define SO_TYPE 4104
+
+#define SO_RCVTIMEO_OLD 4102
+#define SO_SNDTIMEO_OLD 4101
+#define SO_RCVTIMEO_NEW 16448
+#define SO_SNDTIMEO_NEW 16449
diff --git a/sysdeps/unix/sysv/linux/mips/bits/socket-constants.h b/sysdeps/unix/sysv/linux/mips/bits/socket-constants.h
index a5264536e9..2c124234fc 100644
--- a/sysdeps/unix/sysv/linux/mips/bits/socket-constants.h
+++ b/sysdeps/unix/sysv/linux/mips/bits/socket-constants.h
@@ -36,3 +36,8 @@
 #define SO_SNDLOWAT 4099
 #define SO_SNDTIMEO 4101
 #define SO_TYPE 4104
+
+#define SO_RCVTIMEO_OLD 4102
+#define SO_SNDTIMEO_OLD 4101
+#define SO_RCVTIMEO_NEW 66
+#define SO_SNDTIMEO_NEW 67
diff --git a/sysdeps/unix/sysv/linux/powerpc/bits/socket-constants.h b/sysdeps/unix/sysv/linux/powerpc/bits/socket-constants.h
index f35488b375..682a3f4608 100644
--- a/sysdeps/unix/sysv/linux/powerpc/bits/socket-constants.h
+++ b/sysdeps/unix/sysv/linux/powerpc/bits/socket-constants.h
@@ -36,3 +36,8 @@
 #define SO_SNDLOWAT 17
 #define SO_SNDTIMEO 19
 #define SO_TYPE 3
+
+#define SO_RCVTIMEO_OLD 18
+#define SO_SNDTIMEO_OLD 19
+#define SO_RCVTIMEO_NEW 66
+#define SO_SNDTIMEO_NEW 67
diff --git a/sysdeps/unix/sysv/linux/setsockopt.c b/sysdeps/unix/sysv/linux/setsockopt.c
index 12fd7bdcde..a1f98a937d 100644
--- a/sysdeps/unix/sysv/linux/setsockopt.c
+++ b/sysdeps/unix/sysv/linux/setsockopt.c
@@ -15,21 +15,81 @@
    License along with the GNU C Library; if not, see
    <https://www.gnu.org/licenses/>.  */
 
-#include <errno.h>
-#include <signal.h>
 #include <sys/socket.h>
-
+/* The kernel header with SO_* constants is used as default for _GNU_SOURCE,
+   however the new constants that describe 64-bit time support were added
+   only on v5.1.  */
+#if !defined(SO_RCVTIMEO_NEW) || !defined(SO_RCVTIMEO_OLD)
+# include <bits/socket-constants.h>
+#endif
+#include <time.h>
+#include <sysdep.h>
 #include <socketcall.h>
-#include <kernel-features.h>
-#include <sys/syscall.h>
 
-int
-setsockopt (int fd, int level, int optname, const void *optval, socklen_t len)
+static int
+setsockopt_syscall (int fd, int level, int optname, const void *optval,
+		    socklen_t len)
 {
 #ifdef __ASSUME_SETSOCKOPT_SYSCALL
-  return INLINE_SYSCALL (setsockopt, 5, fd, level, optname, optval, len);
+  return INLINE_SYSCALL_CALL (setsockopt, fd, level, optname, optval, len);
 #else
   return SOCKETCALL (setsockopt, fd, level, optname, optval, len);
 #endif
 }
+
+#ifndef __ASSUME_TIME64_SYSCALLS
+static int
+setsockopt32 (int fd, int level, int optname, const void *optval,
+	      socklen_t len)
+{
+  int r = -1;
+
+  if (level != SOL_SOCKET)
+    return r;
+
+  switch (optname)
+    {
+    case SO_RCVTIMEO_NEW:
+    case SO_SNDTIMEO_NEW:
+      {
+        if (len < sizeof (struct __timeval64))
+	  {
+	    __set_errno (EINVAL);
+	    break;
+	  }
+
+	struct __timeval64 *tv64 = (struct __timeval64 *) optval;
+	if (! in_time_t_range (tv64->tv_sec))
+	  {
+	    __set_errno (EOVERFLOW);
+	    break;
+	  }
+
+	if (optname == SO_RCVTIMEO_NEW)
+	  optname = SO_RCVTIMEO_OLD;
+	if (optname == SO_SNDTIMEO_NEW)
+	  optname = SO_SNDTIMEO_OLD;
+
+	struct __timeval32 tv32 = valid_timeval64_to_timeval32 (*tv64);
+
+	r = setsockopt_syscall (fd, level, optname, &tv32, sizeof (tv32));
+      }
+    }
+
+  return r;
+}
+#endif
+
+int
+setsockopt (int fd, int level, int optname, const void *optval, socklen_t len)
+{
+  int r = setsockopt_syscall (fd, level, optname, optval, len);
+
+#ifndef __ASSUME_TIME64_SYSCALLS
+  if (r == -1 && errno == ENOPROTOOPT)
+    r = setsockopt32 (fd, level, optname, optval, len);
+#endif
+
+  return r;
+}
 weak_alias (setsockopt, __setsockopt)
diff --git a/sysdeps/unix/sysv/linux/sparc/bits/socket-constants.h b/sysdeps/unix/sysv/linux/sparc/bits/socket-constants.h
index a58d0b5f96..3eae2176dd 100644
--- a/sysdeps/unix/sysv/linux/sparc/bits/socket-constants.h
+++ b/sysdeps/unix/sysv/linux/sparc/bits/socket-constants.h
@@ -36,3 +36,8 @@
 #define SO_SNDLOWAT 4096
 #define SO_SNDTIMEO 16384
 #define SO_TYPE 4104
+
+#define SO_RCVTIMEO_OLD 8192
+#define SO_SNDTIMEO_OLD 16384
+#define SO_RCVTIMEO_NEW 68
+#define SO_SNDTIMEO_NEW 69


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [glibc/azanella/y2038] linux: Add fallback for 64-bit time_t SO_{RCV,  SND}TIMEO
@ 2021-02-26 20:40 Adhemerval Zanella
  0 siblings, 0 replies; 10+ messages in thread
From: Adhemerval Zanella @ 2021-02-26 20:40 UTC (permalink / raw)
  To: glibc-cvs

https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=81e1ecfaca0c228c85cdf9319975153a3a846460

commit 81e1ecfaca0c228c85cdf9319975153a3a846460
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Date:   Mon Sep 7 14:13:58 2020 -0300

    linux: Add fallback for 64-bit time_t SO_{RCV,SND}TIMEO
    
    The constant value will be changed for __TIMESIZE=64, so binaries built
    with 64-bit time support might fail to work properly on old kernels.
    Both {get,set}sockopt will retry the syscall with the old constant
    values and the timeout value adjusted when kernel returns ENOTPROTOPT.
    
    It also changes to SO_{RCV,SND}TIMEO to follow the uapi kernel values
    where SO_{RCV,SND}TIMEO_OLD indicates pre 64-bit time support and
    SO_{RCV,SND}TIMEO_NEW indicate time64 support.  It allows to refer to
    constant independently of the time_t abi used (since kernel defines
    SO_{RCV,SND}TIMEO depending of the time_t size).
    
    The hppa, mips, powerpc, and sparc provides its own socket-constant.h,
    which are also updated the the SO_{RCV,SND}TIMEO constants (they are
    missed on 019d828669df966 patch).
    
    Checked on x86_64-linux-gnu and i686-linux-gnu (on 5.4 and on 4.15
    kernel).

Diff:
---
 sysdeps/unix/sysv/linux/bits/socket-constants.h    |  5 ++
 sysdeps/unix/sysv/linux/getsockopt.c               | 72 ++++++++++++++++++--
 .../unix/sysv/linux/hppa/bits/socket-constants.h   |  5 ++
 .../unix/sysv/linux/mips/bits/socket-constants.h   |  5 ++
 .../sysv/linux/powerpc/bits/socket-constants.h     |  5 ++
 sysdeps/unix/sysv/linux/setsockopt.c               | 76 +++++++++++++++++++---
 .../unix/sysv/linux/sparc/bits/socket-constants.h  |  5 ++
 7 files changed, 158 insertions(+), 15 deletions(-)

diff --git a/sysdeps/unix/sysv/linux/bits/socket-constants.h b/sysdeps/unix/sysv/linux/bits/socket-constants.h
index 84f7a333a2..d4e9979605 100644
--- a/sysdeps/unix/sysv/linux/bits/socket-constants.h
+++ b/sysdeps/unix/sysv/linux/bits/socket-constants.h
@@ -48,3 +48,8 @@
 # define SO_SNDTIMEO 21
 #endif
 #define SO_TYPE 3
+
+#define SO_RCVTIMEO_OLD 20
+#define SO_SNDTIMEO_OLD 21
+#define SO_RCVTIMEO_NEW 66
+#define SO_SNDTIMEO_NEW 67
diff --git a/sysdeps/unix/sysv/linux/getsockopt.c b/sysdeps/unix/sysv/linux/getsockopt.c
index 76ee8a94d6..a093e42f96 100644
--- a/sysdeps/unix/sysv/linux/getsockopt.c
+++ b/sysdeps/unix/sysv/linux/getsockopt.c
@@ -15,16 +15,20 @@
    License along with the GNU C Library; if not, see
    <https://www.gnu.org/licenses/>.  */
 
-#include <errno.h>
-#include <signal.h>
 #include <sys/socket.h>
-
+/* The kernel header with SO_* constants is used as default for _GNU_SOURCE,
+   however the new constants that describe 64-bit time support were added
+   only on v5.1.  */
+#if !defined(SO_RCVTIMEO_NEW) || !defined(SO_RCVTIMEO_OLD)
+# include <bits/socket-constants.h>
+#endif
+#include <time.h>
+#include <sysdep.h>
 #include <socketcall.h>
-#include <kernel-features.h>
-#include <sys/syscall.h>
 
-int
-__getsockopt (int fd, int level, int optname, void *optval, socklen_t *len)
+static int
+getsockopt_syscall (int fd, int level, int optname, void *optval,
+		    socklen_t *len)
 {
 #ifdef __ASSUME_GETSOCKOPT_SYSCALL
   return INLINE_SYSCALL (getsockopt, 5, fd, level, optname, optval, len);
@@ -32,4 +36,58 @@ __getsockopt (int fd, int level, int optname, void *optval, socklen_t *len)
   return SOCKETCALL (getsockopt, fd, level, optname, optval, len);
 #endif
 }
+
+#ifndef __ASSUME_TIME64_SYSCALLS
+static int
+getsockopt32 (int fd, int level, int optname, void *optval,
+	      socklen_t *len)
+{
+  int r = -1;
+
+  if (level != SOL_SOCKET)
+    return r;
+
+  switch (optname)
+    {
+    case SO_RCVTIMEO_NEW:
+    case SO_SNDTIMEO_NEW:
+      {
+        if (*len < sizeof (struct __timeval64))
+	  {
+	    __set_errno (EINVAL);
+	    break;
+	  }
+
+	if (optname == SO_RCVTIMEO_NEW)
+	  optname = SO_RCVTIMEO_OLD;
+	if (optname == SO_SNDTIMEO_NEW)
+	  optname = SO_SNDTIMEO_OLD;
+
+	struct __timeval32 tv32;
+	r = getsockopt_syscall (fd, level, optname, &tv32,
+				(socklen_t[]) { sizeof tv32 });
+	if (r < 0)
+	  break;
+	struct __timeval64 *tv64 = (struct __timeval64 *) optval;
+	*tv64 = valid_timeval32_to_timeval64 (tv32);
+	*len = sizeof (*tv64);
+      }
+    }
+
+  return r;
+}
+#endif
+
+int
+__getsockopt (int fd, int level, int optname, void *optval, socklen_t *len)
+{
+  int r = getsockopt_syscall (fd, level, optname, optval, len);
+
+#ifndef __ASSUME_TIME64_SYSCALLS
+  if (r == -1 && errno == ENOPROTOOPT)
+    r = getsockopt32 (fd, level, optname, optval, len);
+#endif
+
+ return r;
+}
 weak_alias (__getsockopt, getsockopt)
diff --git a/sysdeps/unix/sysv/linux/hppa/bits/socket-constants.h b/sysdeps/unix/sysv/linux/hppa/bits/socket-constants.h
index b4fb65b9f1..2413529e71 100644
--- a/sysdeps/unix/sysv/linux/hppa/bits/socket-constants.h
+++ b/sysdeps/unix/sysv/linux/hppa/bits/socket-constants.h
@@ -36,3 +36,8 @@
 #define SO_SNDLOWAT 4099
 #define SO_SNDTIMEO 4101
 #define SO_TYPE 4104
+
+#define SO_RCVTIMEO_OLD 4102
+#define SO_SNDTIMEO_OLD 4101
+#define SO_RCVTIMEO_NEW 16448
+#define SO_SNDTIMEO_NEW 16449
diff --git a/sysdeps/unix/sysv/linux/mips/bits/socket-constants.h b/sysdeps/unix/sysv/linux/mips/bits/socket-constants.h
index a5264536e9..2c124234fc 100644
--- a/sysdeps/unix/sysv/linux/mips/bits/socket-constants.h
+++ b/sysdeps/unix/sysv/linux/mips/bits/socket-constants.h
@@ -36,3 +36,8 @@
 #define SO_SNDLOWAT 4099
 #define SO_SNDTIMEO 4101
 #define SO_TYPE 4104
+
+#define SO_RCVTIMEO_OLD 4102
+#define SO_SNDTIMEO_OLD 4101
+#define SO_RCVTIMEO_NEW 66
+#define SO_SNDTIMEO_NEW 67
diff --git a/sysdeps/unix/sysv/linux/powerpc/bits/socket-constants.h b/sysdeps/unix/sysv/linux/powerpc/bits/socket-constants.h
index f35488b375..682a3f4608 100644
--- a/sysdeps/unix/sysv/linux/powerpc/bits/socket-constants.h
+++ b/sysdeps/unix/sysv/linux/powerpc/bits/socket-constants.h
@@ -36,3 +36,8 @@
 #define SO_SNDLOWAT 17
 #define SO_SNDTIMEO 19
 #define SO_TYPE 3
+
+#define SO_RCVTIMEO_OLD 18
+#define SO_SNDTIMEO_OLD 19
+#define SO_RCVTIMEO_NEW 66
+#define SO_SNDTIMEO_NEW 67
diff --git a/sysdeps/unix/sysv/linux/setsockopt.c b/sysdeps/unix/sysv/linux/setsockopt.c
index 12fd7bdcde..a1f98a937d 100644
--- a/sysdeps/unix/sysv/linux/setsockopt.c
+++ b/sysdeps/unix/sysv/linux/setsockopt.c
@@ -15,21 +15,81 @@
    License along with the GNU C Library; if not, see
    <https://www.gnu.org/licenses/>.  */
 
-#include <errno.h>
-#include <signal.h>
 #include <sys/socket.h>
-
+/* The kernel header with SO_* constants is used as default for _GNU_SOURCE,
+   however the new constants that describe 64-bit time support were added
+   only on v5.1.  */
+#if !defined(SO_RCVTIMEO_NEW) || !defined(SO_RCVTIMEO_OLD)
+# include <bits/socket-constants.h>
+#endif
+#include <time.h>
+#include <sysdep.h>
 #include <socketcall.h>
-#include <kernel-features.h>
-#include <sys/syscall.h>
 
-int
-setsockopt (int fd, int level, int optname, const void *optval, socklen_t len)
+static int
+setsockopt_syscall (int fd, int level, int optname, const void *optval,
+		    socklen_t len)
 {
 #ifdef __ASSUME_SETSOCKOPT_SYSCALL
-  return INLINE_SYSCALL (setsockopt, 5, fd, level, optname, optval, len);
+  return INLINE_SYSCALL_CALL (setsockopt, fd, level, optname, optval, len);
 #else
   return SOCKETCALL (setsockopt, fd, level, optname, optval, len);
 #endif
 }
+
+#ifndef __ASSUME_TIME64_SYSCALLS
+static int
+setsockopt32 (int fd, int level, int optname, const void *optval,
+	      socklen_t len)
+{
+  int r = -1;
+
+  if (level != SOL_SOCKET)
+    return r;
+
+  switch (optname)
+    {
+    case SO_RCVTIMEO_NEW:
+    case SO_SNDTIMEO_NEW:
+      {
+        if (len < sizeof (struct __timeval64))
+	  {
+	    __set_errno (EINVAL);
+	    break;
+	  }
+
+	struct __timeval64 *tv64 = (struct __timeval64 *) optval;
+	if (! in_time_t_range (tv64->tv_sec))
+	  {
+	    __set_errno (EOVERFLOW);
+	    break;
+	  }
+
+	if (optname == SO_RCVTIMEO_NEW)
+	  optname = SO_RCVTIMEO_OLD;
+	if (optname == SO_SNDTIMEO_NEW)
+	  optname = SO_SNDTIMEO_OLD;
+
+	struct __timeval32 tv32 = valid_timeval64_to_timeval32 (*tv64);
+
+	r = setsockopt_syscall (fd, level, optname, &tv32, sizeof (tv32));
+      }
+    }
+
+  return r;
+}
+#endif
+
+int
+setsockopt (int fd, int level, int optname, const void *optval, socklen_t len)
+{
+  int r = setsockopt_syscall (fd, level, optname, optval, len);
+
+#ifndef __ASSUME_TIME64_SYSCALLS
+  if (r == -1 && errno == ENOPROTOOPT)
+    r = setsockopt32 (fd, level, optname, optval, len);
+#endif
+
+  return r;
+}
 weak_alias (setsockopt, __setsockopt)
diff --git a/sysdeps/unix/sysv/linux/sparc/bits/socket-constants.h b/sysdeps/unix/sysv/linux/sparc/bits/socket-constants.h
index a58d0b5f96..3eae2176dd 100644
--- a/sysdeps/unix/sysv/linux/sparc/bits/socket-constants.h
+++ b/sysdeps/unix/sysv/linux/sparc/bits/socket-constants.h
@@ -36,3 +36,8 @@
 #define SO_SNDLOWAT 4096
 #define SO_SNDTIMEO 16384
 #define SO_TYPE 4104
+
+#define SO_RCVTIMEO_OLD 8192
+#define SO_SNDTIMEO_OLD 16384
+#define SO_RCVTIMEO_NEW 68
+#define SO_SNDTIMEO_NEW 69


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [glibc/azanella/y2038] linux: Add fallback for 64-bit time_t SO_{RCV,  SND}TIMEO
@ 2021-02-23 20:38 Adhemerval Zanella
  0 siblings, 0 replies; 10+ messages in thread
From: Adhemerval Zanella @ 2021-02-23 20:38 UTC (permalink / raw)
  To: glibc-cvs

https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=c896605c69b761535b5dd8c0c7e35f27e46ede68

commit c896605c69b761535b5dd8c0c7e35f27e46ede68
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Date:   Mon Sep 7 14:13:58 2020 -0300

    linux: Add fallback for 64-bit time_t SO_{RCV,SND}TIMEO
    
    The constant value will be changed for __TIMESIZE=64, so binaries built
    with 64-bit time support might fail to work properly on old kernels.
    Both {get,set}sockopt will retry the syscall with the old constant
    values and the timeout value adjusted when kernel returns ENOTPROTOPT.
    
    It also changes to SO_{RCV,SND}TIMEO to follow the uapi kernel values
    where SO_{RCV,SND}TIMEO_OLD indicates pre 64-bit time support and
    SO_{RCV,SND}TIMEO_NEW indicate time64 support.  It allows to refer to
    constant independently of the time_t abi used (since kernel defines
    SO_{RCV,SND}TIMEO depending of the time_t size).
    
    The hppa, mips, powerpc, and sparc provides its own socket-constant.h,
    which are also updated the the SO_{RCV,SND}TIMEO constants (they are
    missed on 019d828669df966 patch).
    
    Checked on x86_64-linux-gnu and i686-linux-gnu (on 5.4 and on 4.15
    kernel).

Diff:
---
 sysdeps/unix/sysv/linux/bits/socket-constants.h    |  5 ++
 sysdeps/unix/sysv/linux/getsockopt.c               | 72 ++++++++++++++++++--
 .../unix/sysv/linux/hppa/bits/socket-constants.h   |  5 ++
 .../unix/sysv/linux/mips/bits/socket-constants.h   |  5 ++
 .../sysv/linux/powerpc/bits/socket-constants.h     |  5 ++
 sysdeps/unix/sysv/linux/setsockopt.c               | 76 +++++++++++++++++++---
 .../unix/sysv/linux/sparc/bits/socket-constants.h  |  5 ++
 7 files changed, 158 insertions(+), 15 deletions(-)

diff --git a/sysdeps/unix/sysv/linux/bits/socket-constants.h b/sysdeps/unix/sysv/linux/bits/socket-constants.h
index 84f7a333a2..d4e9979605 100644
--- a/sysdeps/unix/sysv/linux/bits/socket-constants.h
+++ b/sysdeps/unix/sysv/linux/bits/socket-constants.h
@@ -48,3 +48,8 @@
 # define SO_SNDTIMEO 21
 #endif
 #define SO_TYPE 3
+
+#define SO_RCVTIMEO_OLD 20
+#define SO_SNDTIMEO_OLD 21
+#define SO_RCVTIMEO_NEW 66
+#define SO_SNDTIMEO_NEW 67
diff --git a/sysdeps/unix/sysv/linux/getsockopt.c b/sysdeps/unix/sysv/linux/getsockopt.c
index 76ee8a94d6..a093e42f96 100644
--- a/sysdeps/unix/sysv/linux/getsockopt.c
+++ b/sysdeps/unix/sysv/linux/getsockopt.c
@@ -15,16 +15,20 @@
    License along with the GNU C Library; if not, see
    <https://www.gnu.org/licenses/>.  */
 
-#include <errno.h>
-#include <signal.h>
 #include <sys/socket.h>
-
+/* The kernel header with SO_* constants is used as default for _GNU_SOURCE,
+   however the new constants that describe 64-bit time support were added
+   only on v5.1.  */
+#if !defined(SO_RCVTIMEO_NEW) || !defined(SO_RCVTIMEO_OLD)
+# include <bits/socket-constants.h>
+#endif
+#include <time.h>
+#include <sysdep.h>
 #include <socketcall.h>
-#include <kernel-features.h>
-#include <sys/syscall.h>
 
-int
-__getsockopt (int fd, int level, int optname, void *optval, socklen_t *len)
+static int
+getsockopt_syscall (int fd, int level, int optname, void *optval,
+		    socklen_t *len)
 {
 #ifdef __ASSUME_GETSOCKOPT_SYSCALL
   return INLINE_SYSCALL (getsockopt, 5, fd, level, optname, optval, len);
@@ -32,4 +36,58 @@ __getsockopt (int fd, int level, int optname, void *optval, socklen_t *len)
   return SOCKETCALL (getsockopt, fd, level, optname, optval, len);
 #endif
 }
+
+#ifndef __ASSUME_TIME64_SYSCALLS
+static int
+getsockopt32 (int fd, int level, int optname, void *optval,
+	      socklen_t *len)
+{
+  int r = -1;
+
+  if (level != SOL_SOCKET)
+    return r;
+
+  switch (optname)
+    {
+    case SO_RCVTIMEO_NEW:
+    case SO_SNDTIMEO_NEW:
+      {
+        if (*len < sizeof (struct __timeval64))
+	  {
+	    __set_errno (EINVAL);
+	    break;
+	  }
+
+	if (optname == SO_RCVTIMEO_NEW)
+	  optname = SO_RCVTIMEO_OLD;
+	if (optname == SO_SNDTIMEO_NEW)
+	  optname = SO_SNDTIMEO_OLD;
+
+	struct __timeval32 tv32;
+	r = getsockopt_syscall (fd, level, optname, &tv32,
+				(socklen_t[]) { sizeof tv32 });
+	if (r < 0)
+	  break;
+	struct __timeval64 *tv64 = (struct __timeval64 *) optval;
+	*tv64 = valid_timeval32_to_timeval64 (tv32);
+	*len = sizeof (*tv64);
+      }
+    }
+
+  return r;
+}
+#endif
+
+int
+__getsockopt (int fd, int level, int optname, void *optval, socklen_t *len)
+{
+  int r = getsockopt_syscall (fd, level, optname, optval, len);
+
+#ifndef __ASSUME_TIME64_SYSCALLS
+  if (r == -1 && errno == ENOPROTOOPT)
+    r = getsockopt32 (fd, level, optname, optval, len);
+#endif
+
+ return r;
+}
 weak_alias (__getsockopt, getsockopt)
diff --git a/sysdeps/unix/sysv/linux/hppa/bits/socket-constants.h b/sysdeps/unix/sysv/linux/hppa/bits/socket-constants.h
index b4fb65b9f1..2413529e71 100644
--- a/sysdeps/unix/sysv/linux/hppa/bits/socket-constants.h
+++ b/sysdeps/unix/sysv/linux/hppa/bits/socket-constants.h
@@ -36,3 +36,8 @@
 #define SO_SNDLOWAT 4099
 #define SO_SNDTIMEO 4101
 #define SO_TYPE 4104
+
+#define SO_RCVTIMEO_OLD 4102
+#define SO_SNDTIMEO_OLD 4101
+#define SO_RCVTIMEO_NEW 16448
+#define SO_SNDTIMEO_NEW 16449
diff --git a/sysdeps/unix/sysv/linux/mips/bits/socket-constants.h b/sysdeps/unix/sysv/linux/mips/bits/socket-constants.h
index a5264536e9..2c124234fc 100644
--- a/sysdeps/unix/sysv/linux/mips/bits/socket-constants.h
+++ b/sysdeps/unix/sysv/linux/mips/bits/socket-constants.h
@@ -36,3 +36,8 @@
 #define SO_SNDLOWAT 4099
 #define SO_SNDTIMEO 4101
 #define SO_TYPE 4104
+
+#define SO_RCVTIMEO_OLD 4102
+#define SO_SNDTIMEO_OLD 4101
+#define SO_RCVTIMEO_NEW 66
+#define SO_SNDTIMEO_NEW 67
diff --git a/sysdeps/unix/sysv/linux/powerpc/bits/socket-constants.h b/sysdeps/unix/sysv/linux/powerpc/bits/socket-constants.h
index f35488b375..682a3f4608 100644
--- a/sysdeps/unix/sysv/linux/powerpc/bits/socket-constants.h
+++ b/sysdeps/unix/sysv/linux/powerpc/bits/socket-constants.h
@@ -36,3 +36,8 @@
 #define SO_SNDLOWAT 17
 #define SO_SNDTIMEO 19
 #define SO_TYPE 3
+
+#define SO_RCVTIMEO_OLD 18
+#define SO_SNDTIMEO_OLD 19
+#define SO_RCVTIMEO_NEW 66
+#define SO_SNDTIMEO_NEW 67
diff --git a/sysdeps/unix/sysv/linux/setsockopt.c b/sysdeps/unix/sysv/linux/setsockopt.c
index 12fd7bdcde..a1f98a937d 100644
--- a/sysdeps/unix/sysv/linux/setsockopt.c
+++ b/sysdeps/unix/sysv/linux/setsockopt.c
@@ -15,21 +15,81 @@
    License along with the GNU C Library; if not, see
    <https://www.gnu.org/licenses/>.  */
 
-#include <errno.h>
-#include <signal.h>
 #include <sys/socket.h>
-
+/* The kernel header with SO_* constants is used as default for _GNU_SOURCE,
+   however the new constants that describe 64-bit time support were added
+   only on v5.1.  */
+#if !defined(SO_RCVTIMEO_NEW) || !defined(SO_RCVTIMEO_OLD)
+# include <bits/socket-constants.h>
+#endif
+#include <time.h>
+#include <sysdep.h>
 #include <socketcall.h>
-#include <kernel-features.h>
-#include <sys/syscall.h>
 
-int
-setsockopt (int fd, int level, int optname, const void *optval, socklen_t len)
+static int
+setsockopt_syscall (int fd, int level, int optname, const void *optval,
+		    socklen_t len)
 {
 #ifdef __ASSUME_SETSOCKOPT_SYSCALL
-  return INLINE_SYSCALL (setsockopt, 5, fd, level, optname, optval, len);
+  return INLINE_SYSCALL_CALL (setsockopt, fd, level, optname, optval, len);
 #else
   return SOCKETCALL (setsockopt, fd, level, optname, optval, len);
 #endif
 }
+
+#ifndef __ASSUME_TIME64_SYSCALLS
+static int
+setsockopt32 (int fd, int level, int optname, const void *optval,
+	      socklen_t len)
+{
+  int r = -1;
+
+  if (level != SOL_SOCKET)
+    return r;
+
+  switch (optname)
+    {
+    case SO_RCVTIMEO_NEW:
+    case SO_SNDTIMEO_NEW:
+      {
+        if (len < sizeof (struct __timeval64))
+	  {
+	    __set_errno (EINVAL);
+	    break;
+	  }
+
+	struct __timeval64 *tv64 = (struct __timeval64 *) optval;
+	if (! in_time_t_range (tv64->tv_sec))
+	  {
+	    __set_errno (EOVERFLOW);
+	    break;
+	  }
+
+	if (optname == SO_RCVTIMEO_NEW)
+	  optname = SO_RCVTIMEO_OLD;
+	if (optname == SO_SNDTIMEO_NEW)
+	  optname = SO_SNDTIMEO_OLD;
+
+	struct __timeval32 tv32 = valid_timeval64_to_timeval32 (*tv64);
+
+	r = setsockopt_syscall (fd, level, optname, &tv32, sizeof (tv32));
+      }
+    }
+
+  return r;
+}
+#endif
+
+int
+setsockopt (int fd, int level, int optname, const void *optval, socklen_t len)
+{
+  int r = setsockopt_syscall (fd, level, optname, optval, len);
+
+#ifndef __ASSUME_TIME64_SYSCALLS
+  if (r == -1 && errno == ENOPROTOOPT)
+    r = setsockopt32 (fd, level, optname, optval, len);
+#endif
+
+  return r;
+}
 weak_alias (setsockopt, __setsockopt)
diff --git a/sysdeps/unix/sysv/linux/sparc/bits/socket-constants.h b/sysdeps/unix/sysv/linux/sparc/bits/socket-constants.h
index a58d0b5f96..3eae2176dd 100644
--- a/sysdeps/unix/sysv/linux/sparc/bits/socket-constants.h
+++ b/sysdeps/unix/sysv/linux/sparc/bits/socket-constants.h
@@ -36,3 +36,8 @@
 #define SO_SNDLOWAT 4096
 #define SO_SNDTIMEO 16384
 #define SO_TYPE 4104
+
+#define SO_RCVTIMEO_OLD 8192
+#define SO_SNDTIMEO_OLD 16384
+#define SO_RCVTIMEO_NEW 68
+#define SO_SNDTIMEO_NEW 69


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [glibc/azanella/y2038] linux: Add fallback for 64-bit time_t SO_{RCV,  SND}TIMEO
@ 2021-02-23 12:36 Adhemerval Zanella
  0 siblings, 0 replies; 10+ messages in thread
From: Adhemerval Zanella @ 2021-02-23 12:36 UTC (permalink / raw)
  To: glibc-cvs

https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=80770c5d8c523f37f34fb32cb1bb988d10f9fbb1

commit 80770c5d8c523f37f34fb32cb1bb988d10f9fbb1
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Date:   Mon Sep 7 14:13:58 2020 -0300

    linux: Add fallback for 64-bit time_t SO_{RCV,SND}TIMEO
    
    The constant value will be changed for __TIMESIZE=64, so binaries built
    with 64-bit time support might fail to work properly on old kernels.
    Both {get,set}sockopt will retry the syscall with the old constant
    values and the timeout value adjusted when kernel returns ENOTPROTOPT.
    
    It also changes to SO_{RCV,SND}TIMEO to follow the uapi kernel values
    where SO_{RCV,SND}TIMEO_OLD indicates pre 64-bit time support and
    SO_{RCV,SND}TIMEO_NEW indicate time64 support.  It allows to refer to
    constant independently of the time_t abi used (since kernel defines
    SO_{RCV,SND}TIMEO depending of the time_t size).
    
    The hppa, mips, powerpc, and sparc provides its own socket-constant.h,
    which are also updated the the SO_{RCV,SND}TIMEO constants (they are
    missed on 019d828669df966 patch).
    
    Checked on x86_64-linux-gnu and i686-linux-gnu (on 5.4 and on 4.15
    kernel).

Diff:
---
 sysdeps/unix/sysv/linux/bits/socket-constants.h    | 19 +++---
 sysdeps/unix/sysv/linux/getsockopt.c               | 72 ++++++++++++++++++--
 .../unix/sysv/linux/hppa/bits/socket-constants.h   | 15 ++++-
 .../unix/sysv/linux/mips/bits/socket-constants.h   | 17 ++++-
 .../sysv/linux/powerpc/bits/socket-constants.h     | 17 ++++-
 sysdeps/unix/sysv/linux/setsockopt.c               | 76 +++++++++++++++++++---
 .../unix/sysv/linux/sparc/bits/socket-constants.h  | 17 ++++-
 7 files changed, 201 insertions(+), 32 deletions(-)

diff --git a/sysdeps/unix/sysv/linux/bits/socket-constants.h b/sysdeps/unix/sysv/linux/bits/socket-constants.h
index 84f7a333a2..24fe91691f 100644
--- a/sysdeps/unix/sysv/linux/bits/socket-constants.h
+++ b/sysdeps/unix/sysv/linux/bits/socket-constants.h
@@ -32,19 +32,20 @@
 #define SO_OOBINLINE 10
 #define SO_RCVBUF 8
 #define SO_RCVLOWAT 18
-#if (__TIMESIZE == 64 && __WORDSIZE == 32 \
-     && (!defined __SYSCALL_WORDSIZE || __SYSCALL_WORDSIZE == 32))
-# define SO_RCVTIMEO 66
-#else
-# define SO_RCVTIMEO 20
-#endif
 #define SO_REUSEADDR 2
 #define SO_SNDBUF 7
 #define SO_SNDLOWAT 19
+#define SO_TYPE 3
+
+#define SO_RCVTIMEO_OLD 20
+#define SO_SNDTIMEO_OLD 21
+#define SO_RCVTIMEO_NEW 66
+#define SO_SNDTIMEO_NEW 67
 #if (__TIMESIZE == 64 && __WORDSIZE == 32 \
      && (!defined __SYSCALL_WORDSIZE || __SYSCALL_WORDSIZE == 32))
-# define SO_SNDTIMEO 67
+# define SO_RCVTIMEO SO_RCVTIMEO_NEW
+# define SO_SNDTIMEO SO_SNDTIMEO_NEW
 #else
-# define SO_SNDTIMEO 21
+# define SO_RCVTIMEO SO_RCVTIMEO_OLD
+# define SO_SNDTIMEO SO_SNDTIMEO_OLD
 #endif
-#define SO_TYPE 3
diff --git a/sysdeps/unix/sysv/linux/getsockopt.c b/sysdeps/unix/sysv/linux/getsockopt.c
index 76ee8a94d6..a093e42f96 100644
--- a/sysdeps/unix/sysv/linux/getsockopt.c
+++ b/sysdeps/unix/sysv/linux/getsockopt.c
@@ -15,16 +15,20 @@
    License along with the GNU C Library; if not, see
    <https://www.gnu.org/licenses/>.  */
 
-#include <errno.h>
-#include <signal.h>
 #include <sys/socket.h>
-
+/* The kernel header with SO_* constants is used as default for _GNU_SOURCE,
+   however the new constants that describe 64-bit time support were added
+   only on v5.1.  */
+#if !defined(SO_RCVTIMEO_NEW) || !defined(SO_RCVTIMEO_OLD)
+# include <bits/socket-constants.h>
+#endif
+#include <time.h>
+#include <sysdep.h>
 #include <socketcall.h>
-#include <kernel-features.h>
-#include <sys/syscall.h>
 
-int
-__getsockopt (int fd, int level, int optname, void *optval, socklen_t *len)
+static int
+getsockopt_syscall (int fd, int level, int optname, void *optval,
+		    socklen_t *len)
 {
 #ifdef __ASSUME_GETSOCKOPT_SYSCALL
   return INLINE_SYSCALL (getsockopt, 5, fd, level, optname, optval, len);
@@ -32,4 +36,58 @@ __getsockopt (int fd, int level, int optname, void *optval, socklen_t *len)
   return SOCKETCALL (getsockopt, fd, level, optname, optval, len);
 #endif
 }
+
+#ifndef __ASSUME_TIME64_SYSCALLS
+static int
+getsockopt32 (int fd, int level, int optname, void *optval,
+	      socklen_t *len)
+{
+  int r = -1;
+
+  if (level != SOL_SOCKET)
+    return r;
+
+  switch (optname)
+    {
+    case SO_RCVTIMEO_NEW:
+    case SO_SNDTIMEO_NEW:
+      {
+        if (*len < sizeof (struct __timeval64))
+	  {
+	    __set_errno (EINVAL);
+	    break;
+	  }
+
+	if (optname == SO_RCVTIMEO_NEW)
+	  optname = SO_RCVTIMEO_OLD;
+	if (optname == SO_SNDTIMEO_NEW)
+	  optname = SO_SNDTIMEO_OLD;
+
+	struct __timeval32 tv32;
+	r = getsockopt_syscall (fd, level, optname, &tv32,
+				(socklen_t[]) { sizeof tv32 });
+	if (r < 0)
+	  break;
+	struct __timeval64 *tv64 = (struct __timeval64 *) optval;
+	*tv64 = valid_timeval32_to_timeval64 (tv32);
+	*len = sizeof (*tv64);
+      }
+    }
+
+  return r;
+}
+#endif
+
+int
+__getsockopt (int fd, int level, int optname, void *optval, socklen_t *len)
+{
+  int r = getsockopt_syscall (fd, level, optname, optval, len);
+
+#ifndef __ASSUME_TIME64_SYSCALLS
+  if (r == -1 && errno == ENOPROTOOPT)
+    r = getsockopt32 (fd, level, optname, optval, len);
+#endif
+
+ return r;
+}
 weak_alias (__getsockopt, getsockopt)
diff --git a/sysdeps/unix/sysv/linux/hppa/bits/socket-constants.h b/sysdeps/unix/sysv/linux/hppa/bits/socket-constants.h
index b4fb65b9f1..7972bf535b 100644
--- a/sysdeps/unix/sysv/linux/hppa/bits/socket-constants.h
+++ b/sysdeps/unix/sysv/linux/hppa/bits/socket-constants.h
@@ -30,9 +30,20 @@
 #define SO_OOBINLINE 256
 #define SO_RCVBUF 4098
 #define SO_RCVLOWAT 4100
-#define SO_RCVTIMEO 4102
 #define SO_REUSEADDR 4
 #define SO_SNDBUF 4097
 #define SO_SNDLOWAT 4099
-#define SO_SNDTIMEO 4101
 #define SO_TYPE 4104
+
+#define SO_RCVTIMEO_OLD 4102
+#define SO_SNDTIMEO_OLD 4101
+#define SO_RCVTIMEO_NEW 16448
+#define SO_SNDTIMEO_NEW 16449
+#if (__TIMESIZE == 64 && __WORDSIZE == 32 \
+     && (!defined __SYSCALL_WORDSIZE || __SYSCALL_WORDSIZE == 32))
+# define SO_RCVTIMEO SO_RCVTIMEO_NEW
+# define SO_SNDTIMEO SO_SNDTIMEO_NEW
+#else
+# define SO_RCVTIMEO SO_RCVTIMEO_OLD
+# define SO_SNDTIMEO SO_SNDTIMEO_OLD
+#endif
diff --git a/sysdeps/unix/sysv/linux/mips/bits/socket-constants.h b/sysdeps/unix/sysv/linux/mips/bits/socket-constants.h
index a5264536e9..ba39d053ac 100644
--- a/sysdeps/unix/sysv/linux/mips/bits/socket-constants.h
+++ b/sysdeps/unix/sysv/linux/mips/bits/socket-constants.h
@@ -20,6 +20,8 @@
 # error "Never include <bits/socket-constants.h> directly; use <sys/socket.h> instead."
 #endif
 
+#include <bits/timesize.h>
+
 #define SOL_SOCKET 65535
 #define SO_ACCEPTCONN 4105
 #define SO_BROADCAST 32
@@ -30,9 +32,20 @@
 #define SO_OOBINLINE 256
 #define SO_RCVBUF 4098
 #define SO_RCVLOWAT 4100
-#define SO_RCVTIMEO 4102
 #define SO_REUSEADDR 4
 #define SO_SNDBUF 4097
 #define SO_SNDLOWAT 4099
-#define SO_SNDTIMEO 4101
 #define SO_TYPE 4104
+
+#define SO_RCVTIMEO_OLD 4102
+#define SO_SNDTIMEO_OLD 4101
+#define SO_RCVTIMEO_NEW 66
+#define SO_SNDTIMEO_NEW 67
+#if (__TIMESIZE == 64 && __WORDSIZE == 32 \
+     && (!defined __SYSCALL_WORDSIZE || __SYSCALL_WORDSIZE == 32))
+# define SO_RCVTIMEO SO_RCVTIMEO_NEW
+# define SO_SNDTIMEO SO_SNDTIMEO_NEW
+#else
+# define SO_RCVTIMEO SO_RCVTIMEO_OLD
+# define SO_SNDTIMEO SO_SNDTIMEO_OLD
+#endif
diff --git a/sysdeps/unix/sysv/linux/powerpc/bits/socket-constants.h b/sysdeps/unix/sysv/linux/powerpc/bits/socket-constants.h
index f35488b375..6005fe257d 100644
--- a/sysdeps/unix/sysv/linux/powerpc/bits/socket-constants.h
+++ b/sysdeps/unix/sysv/linux/powerpc/bits/socket-constants.h
@@ -20,6 +20,8 @@
 # error "Never include <bits/socket-constants.h> directly; use <sys/socket.h> instead."
 #endif
 
+#include <bits/timesize.h>
+
 #define SOL_SOCKET 1
 #define SO_ACCEPTCONN 30
 #define SO_BROADCAST 6
@@ -30,9 +32,20 @@
 #define SO_OOBINLINE 10
 #define SO_RCVBUF 8
 #define SO_RCVLOWAT 16
-#define SO_RCVTIMEO 18
 #define SO_REUSEADDR 2
 #define SO_SNDBUF 7
 #define SO_SNDLOWAT 17
-#define SO_SNDTIMEO 19
 #define SO_TYPE 3
+
+#define SO_RCVTIMEO_OLD 18
+#define SO_SNDTIMEO_OLD 19
+#define SO_RCVTIMEO_NEW 66
+#define SO_SNDTIMEO_NEW 67
+#if (__TIMESIZE == 64 && __WORDSIZE == 32 \
+     && (!defined __SYSCALL_WORDSIZE || __SYSCALL_WORDSIZE == 32))
+# define SO_RCVTIMEO SO_RCVTIMEO_NEW
+# define SO_SNDTIMEO SO_SNDTIMEO_NEW
+#else
+# define SO_RCVTIMEO SO_RCVTIMEO_OLD
+# define SO_SNDTIMEO SO_SNDTIMEO_OLD
+#endif
diff --git a/sysdeps/unix/sysv/linux/setsockopt.c b/sysdeps/unix/sysv/linux/setsockopt.c
index 12fd7bdcde..a1f98a937d 100644
--- a/sysdeps/unix/sysv/linux/setsockopt.c
+++ b/sysdeps/unix/sysv/linux/setsockopt.c
@@ -15,21 +15,81 @@
    License along with the GNU C Library; if not, see
    <https://www.gnu.org/licenses/>.  */
 
-#include <errno.h>
-#include <signal.h>
 #include <sys/socket.h>
-
+/* The kernel header with SO_* constants is used as default for _GNU_SOURCE,
+   however the new constants that describe 64-bit time support were added
+   only on v5.1.  */
+#if !defined(SO_RCVTIMEO_NEW) || !defined(SO_RCVTIMEO_OLD)
+# include <bits/socket-constants.h>
+#endif
+#include <time.h>
+#include <sysdep.h>
 #include <socketcall.h>
-#include <kernel-features.h>
-#include <sys/syscall.h>
 
-int
-setsockopt (int fd, int level, int optname, const void *optval, socklen_t len)
+static int
+setsockopt_syscall (int fd, int level, int optname, const void *optval,
+		    socklen_t len)
 {
 #ifdef __ASSUME_SETSOCKOPT_SYSCALL
-  return INLINE_SYSCALL (setsockopt, 5, fd, level, optname, optval, len);
+  return INLINE_SYSCALL_CALL (setsockopt, fd, level, optname, optval, len);
 #else
   return SOCKETCALL (setsockopt, fd, level, optname, optval, len);
 #endif
 }
+
+#ifndef __ASSUME_TIME64_SYSCALLS
+static int
+setsockopt32 (int fd, int level, int optname, const void *optval,
+	      socklen_t len)
+{
+  int r = -1;
+
+  if (level != SOL_SOCKET)
+    return r;
+
+  switch (optname)
+    {
+    case SO_RCVTIMEO_NEW:
+    case SO_SNDTIMEO_NEW:
+      {
+        if (len < sizeof (struct __timeval64))
+	  {
+	    __set_errno (EINVAL);
+	    break;
+	  }
+
+	struct __timeval64 *tv64 = (struct __timeval64 *) optval;
+	if (! in_time_t_range (tv64->tv_sec))
+	  {
+	    __set_errno (EOVERFLOW);
+	    break;
+	  }
+
+	if (optname == SO_RCVTIMEO_NEW)
+	  optname = SO_RCVTIMEO_OLD;
+	if (optname == SO_SNDTIMEO_NEW)
+	  optname = SO_SNDTIMEO_OLD;
+
+	struct __timeval32 tv32 = valid_timeval64_to_timeval32 (*tv64);
+
+	r = setsockopt_syscall (fd, level, optname, &tv32, sizeof (tv32));
+      }
+    }
+
+  return r;
+}
+#endif
+
+int
+setsockopt (int fd, int level, int optname, const void *optval, socklen_t len)
+{
+  int r = setsockopt_syscall (fd, level, optname, optval, len);
+
+#ifndef __ASSUME_TIME64_SYSCALLS
+  if (r == -1 && errno == ENOPROTOOPT)
+    r = setsockopt32 (fd, level, optname, optval, len);
+#endif
+
+  return r;
+}
 weak_alias (setsockopt, __setsockopt)
diff --git a/sysdeps/unix/sysv/linux/sparc/bits/socket-constants.h b/sysdeps/unix/sysv/linux/sparc/bits/socket-constants.h
index a58d0b5f96..7233c6dd1d 100644
--- a/sysdeps/unix/sysv/linux/sparc/bits/socket-constants.h
+++ b/sysdeps/unix/sysv/linux/sparc/bits/socket-constants.h
@@ -20,6 +20,8 @@
 # error "Never include <bits/socket-constants.h> directly; use <sys/socket.h> instead."
 #endif
 
+#include <bits/timesize.h>
+
 #define SOL_SOCKET 65535
 #define SO_ACCEPTCONN 32768
 #define SO_BROADCAST 32
@@ -30,9 +32,20 @@
 #define SO_OOBINLINE 256
 #define SO_RCVBUF 4098
 #define SO_RCVLOWAT 2048
-#define SO_RCVTIMEO 8192
 #define SO_REUSEADDR 4
 #define SO_SNDBUF 4097
 #define SO_SNDLOWAT 4096
-#define SO_SNDTIMEO 16384
 #define SO_TYPE 4104
+
+#define SO_RCVTIMEO_OLD 8192
+#define SO_SNDTIMEO_OLD 16384
+#define SO_RCVTIMEO_NEW 68
+#define SO_SNDTIMEO_NEW 69
+#if (__TIMESIZE == 64 && __WORDSIZE == 32 \
+     && (!defined __SYSCALL_WORDSIZE || __SYSCALL_WORDSIZE == 32))
+# define SO_RCVTIMEO SO_RCVTIMEO_NEW
+# define SO_SNDTIMEO SO_SNDTIMEO_NEW
+#else
+# define SO_RCVTIMEO SO_RCVTIMEO_OLD
+# define SO_SNDTIMEO SO_SNDTIMEO_OLD
+#endif


^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2021-06-08 20:48 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-06-08 20:48 [glibc/azanella/y2038] linux: Add fallback for 64-bit time_t SO_{RCV, SND}TIMEO Adhemerval Zanella
  -- strict thread matches above, loose matches on Subject: below --
2021-05-26 16:24 Adhemerval Zanella
2021-03-05 19:20 Adhemerval Zanella
2021-03-04 17:36 Adhemerval Zanella
2021-03-04 11:28 Adhemerval Zanella
2021-03-02 12:30 Adhemerval Zanella
2021-03-01 17:35 Adhemerval Zanella
2021-02-26 20:40 Adhemerval Zanella
2021-02-23 20:38 Adhemerval Zanella
2021-02-23 12:36 Adhemerval Zanella

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).