diff options
author | Adhemerval Zanella <adhemerval.zanella@linaro.org> | 2020-09-07 17:08:46 -0300 |
---|---|---|
committer | Adhemerval Zanella <adhemerval.zanella@linaro.org> | 2021-06-15 10:42:06 -0300 |
commit | 13c51549e2077f2f3bf84e8fd0b46d8b0c615912 (patch) | |
tree | b62046c46909d85fcd8a4cf815da4cf3c94ab337 /sysdeps/unix/sysv/linux/convert_scm_timestamps.c | |
parent | 8dfb169c80b56cf25494d052ddf410dc55f2f5a3 (diff) | |
download | glibc-13c51549e2077f2f3bf84e8fd0b46d8b0c615912.tar glibc-13c51549e2077f2f3bf84e8fd0b46d8b0c615912.tar.gz glibc-13c51549e2077f2f3bf84e8fd0b46d8b0c615912.tar.bz2 glibc-13c51549e2077f2f3bf84e8fd0b46d8b0c615912.zip |
linux: Add fallback for 64-bit time_t SO_TIMESTAMP{NS}
The recvmsg handling is more complicated because it requires check the
returned kernel control message and make some convertions. For
!__ASSUME_TIME64_SYSCALLS it converts the first 32-bit time SO_TIMESTAMP
or SO_TIMESTAMPNS and appends it to the control buffer if has extra
space or returns MSG_CTRUNC otherwise. The 32-bit time field is kept
as-is.
Calls with __TIMESIZE=32 will see the converted 64-bit time control
messages as spurious control message of unknown type. Calls with
__TIMESIZE=64 running on pre-time64 kernels will see the original
message as a spurious control ones of unknown typ while running on
kernel with native 64-bit time support will only see the time64 version
of the control message.
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/convert_scm_timestamps.c')
-rw-r--r-- | sysdeps/unix/sysv/linux/convert_scm_timestamps.c | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/sysdeps/unix/sysv/linux/convert_scm_timestamps.c b/sysdeps/unix/sysv/linux/convert_scm_timestamps.c new file mode 100644 index 0000000000..d75a4618dd --- /dev/null +++ b/sysdeps/unix/sysv/linux/convert_scm_timestamps.c @@ -0,0 +1,95 @@ +/* Socket timestamp conversion routines. + 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/>. */ + +#include <kernel-features.h> + +#ifndef __ASSUME_TIME64_SYSCALLS +# include <stdint.h> +# include <string.h> +# include <sys/socket.h> +# include <socket-constants-time64.h> + +/* It converts the first SO_TIMESTAMP or SO_TIMESTAMPNS with 32-bit time and + appends it to the control buffer. The 32-bit time field is kept as-is. + + Calls with __TIMESIZE=32 will see the converted 64-bit time control + messages as spurious control message of unknown type. + + Calls with __TIMESIZE=64 running on pre-time64 kernels will see the + original message as a spurious control ones of unknown typ while running + on kernel with native 64-bit time support will only see the time64 version + of the control message. */ +void +__convert_scm_timestamps (struct msghdr *msg, socklen_t msgsize) +{ + if (msg->msg_control == NULL || msg->msg_controllen == 0) + return; + + /* The returned control message format for SO_TIMESTAMP_NEW is a + 'struct __kernel_sock_timeval' while for SO_TIMESTAMPNS_NEW is a + 'struct __kernel_timespec'. In either case it is two uint64_t + members. */ + uint64_t tvts[2]; + + struct cmsghdr *cmsg, *last = NULL; + int type = 0; + + for (cmsg = CMSG_FIRSTHDR (msg); + cmsg != NULL; + cmsg = CMSG_NXTHDR (msg, cmsg)) + { + if (cmsg->cmsg_level != SOL_SOCKET) + continue; + + switch (cmsg->cmsg_type) + { + case COMPAT_SO_TIMESTAMP_OLD: + if (type != 0) + break; + type = COMPAT_SO_TIMESTAMP_NEW; + goto common; + + case COMPAT_SO_TIMESTAMPNS_OLD: + type = COMPAT_SO_TIMESTAMPNS_NEW; + + /* fallthrough */ + common: + memcpy (tvts, CMSG_DATA (cmsg), sizeof (tvts)); + break; + } + + last = cmsg; + } + + if (last == NULL || type == 0) + return; + + if (CMSG_SPACE (sizeof tvts) > msgsize - msg->msg_controllen) + { + msg->msg_flags |= MSG_CTRUNC; + return; + } + + msg->msg_controllen += CMSG_SPACE (sizeof tvts); + cmsg = CMSG_NXTHDR(msg, last); + cmsg->cmsg_level = SOL_SOCKET; + cmsg->cmsg_type = type; + cmsg->cmsg_len = CMSG_LEN (sizeof tvts); + memcpy (CMSG_DATA (cmsg), tvts, sizeof tvts); +} +#endif |