diff options
author | Adhemerval Zanella <adhemerval.zanella@linaro.org> | 2020-09-07 14:13:58 -0300 |
---|---|---|
committer | Adhemerval Zanella <adhemerval.zanella@linaro.org> | 2021-06-14 18:00:06 -0300 |
commit | 8dfb169c80b56cf25494d052ddf410dc55f2f5a3 (patch) | |
tree | 47ef6107ecb9e9323c8703e5424b77676a27f743 /sysdeps/unix/sysv/linux/getsockopt.c | |
parent | 5767b0ddcb592546fe8d978dd8f06904756373dd (diff) | |
download | glibc-8dfb169c80b56cf25494d052ddf410dc55f2f5a3.tar.gz glibc-8dfb169c80b56cf25494d052ddf410dc55f2f5a3.tar.xz glibc-8dfb169c80b56cf25494d052ddf410dc55f2f5a3.zip |
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). Reviewed-by: Carlos O'Donell <carlos@redhat.com> Tested-by: Carlos O'Donell <carlos@redhat.com>
Diffstat (limited to 'sysdeps/unix/sysv/linux/getsockopt.c')
-rw-r--r-- | sysdeps/unix/sysv/linux/getsockopt.c | 70 |
1 files changed, 63 insertions, 7 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) |