From patchwork Mon Jun 7 20:35:54 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 1488971 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha256 header.s=default header.b=udqW165b; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4FzQHK64Kxz9sSn for ; Tue, 8 Jun 2021 06:40:29 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 88618388C034 for ; Mon, 7 Jun 2021 20:40:27 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 88618388C034 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1623098427; bh=KfNA3rISOTaaxQ6XiFzBI0+s8t9Q5QFs9TlSluYTOZQ=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=udqW165brh5d/SIpamHlsBDBzcPflIpM0nbwwhB3nKCoh6tgNPYQ4r3w4wEU/6SKr 7sVGXbMggcsi7bkQRXoGED7Gyvdfb8CeZZCcCTgpYSPVnwtrfMsQHZ5kMUxCrPS00i 08MVLppJzM5BUdZNASUxgOjKOUDyNRFWHF2boxng= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qt1-x82e.google.com (mail-qt1-x82e.google.com [IPv6:2607:f8b0:4864:20::82e]) by sourceware.org (Postfix) with ESMTPS id D91653899078 for ; Mon, 7 Jun 2021 20:36:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org D91653899078 Received: by mail-qt1-x82e.google.com with SMTP id t20so13645407qtx.8 for ; Mon, 07 Jun 2021 13:36:27 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=KfNA3rISOTaaxQ6XiFzBI0+s8t9Q5QFs9TlSluYTOZQ=; b=keh5PPoc5gQ842DTZ2qh2thm+5Ik+jsMLSJ+GpGYvbuSo0AXfGW99Drj2XzNRVE/WC K7FOB0KZ1RTEd2vtTqVuoHxW0PhnL4DG0RCmbzULXo5FJQOrK++k5JKutHSoK54WeyNj I6CAIxq6MFG4P/ZLuBIst4I8nCCmyYVDG5WQZMe84UjMuDlw2k5fEeunFCdeLaNWHQo9 +2qVzNfLex+8VXgpR/hA6XkLVr/vT3SUo9jJPMzLFJdzJNpMn9S1z5LdnA657N+YNWqc 3Puw6eaH6ITOkS3O7Oc7T1MBw826WH3+vcLC0en5ty1eN7HvLPkIwbpwddjH5cxatT2Z RKrQ== X-Gm-Message-State: AOAM532ME9z2yBd9P83GacL1RfkY4BJ504Zln0AvANopBBEMIpRcrRte 1dljWnfRDMM+6Rd3coNdY74zYOxXL1WcsA== X-Google-Smtp-Source: ABdhPJwkYRW8L3TnVufmP7LgiNyqzOMNnhn+XIqsv3OALHRZRVli2l33N9pHNuKNC2YZOVXa3pXrog== X-Received: by 2002:a05:622a:10:: with SMTP id x16mr17918018qtw.185.1623098187270; Mon, 07 Jun 2021 13:36:27 -0700 (PDT) Received: from birita.. ([177.194.59.218]) by smtp.googlemail.com with ESMTPSA id t196sm10422123qke.50.2021.06.07.13.36.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Jun 2021 13:36:27 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH v3 05/24] linux: Add fallback for 64-bit time_t SO_TIMESTAMP{NS} Date: Mon, 7 Jun 2021 17:35:54 -0300 Message-Id: <20210607203613.282543-6-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210607203613.282543-1-adhemerval.zanella@linaro.org> References: <20210607203613.282543-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Netto Reply-To: Adhemerval Zanella Errors-To: libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org Sender: "Libc-alpha" 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 Tested-by: Carlos O'Donell --- include/sys/socket.h | 5 + sysdeps/unix/sysv/linux/Makefile | 2 +- sysdeps/unix/sysv/linux/Versions | 1 + .../unix/sysv/linux/convert_scm_timestamps.c | 96 +++++++++++++++++++ sysdeps/unix/sysv/linux/getsockopt.c | 12 +++ .../sysv/linux/hppa/socket-constants-time64.h | 5 + .../sysv/linux/mips/socket-constants-time64.h | 5 + .../linux/powerpc/socket-constants-time64.h | 5 + sysdeps/unix/sysv/linux/recvmsg.c | 23 +++-- sysdeps/unix/sysv/linux/setsockopt.c | 12 +++ .../unix/sysv/linux/socket-constants-time64.h | 5 + .../linux/sparc/socket-constants-time64.h | 5 + 12 files changed, 169 insertions(+), 7 deletions(-) create mode 100644 sysdeps/unix/sysv/linux/convert_scm_timestamps.c diff --git a/include/sys/socket.h b/include/sys/socket.h index 0e39dd2a3a..15d4a62b26 100644 --- a/include/sys/socket.h +++ b/include/sys/socket.h @@ -164,5 +164,10 @@ libc_hidden_proto (__libc_sa_len) libc_hidden_proto (__cmsg_nxthdr) +#ifndef __ASSUME_TIME64_SYSCALLS +extern void __convert_scm_timestamps (struct msghdr *msg, socklen_t msgsize) ; +libc_hidden_proto (__convert_scm_timestamps) +#endif + #endif #endif diff --git a/sysdeps/unix/sysv/linux/Makefile b/sysdeps/unix/sysv/linux/Makefile index bc14f20274..36d5ae020c 100644 --- a/sysdeps/unix/sysv/linux/Makefile +++ b/sysdeps/unix/sysv/linux/Makefile @@ -64,7 +64,7 @@ sysdep_routines += adjtimex clone umount umount2 readahead sysctl \ time64-support pselect32 \ xstat fxstat lxstat xstat64 fxstat64 lxstat64 \ fxstatat fxstatat64 \ - xmknod xmknodat + xmknod xmknodat convert_scm_timestamps CFLAGS-gethostid.c = -fexceptions CFLAGS-tee.c = -fexceptions -fasynchronous-unwind-tables diff --git a/sysdeps/unix/sysv/linux/Versions b/sysdeps/unix/sysv/linux/Versions index c864ad38ca..4637fd651d 100644 --- a/sysdeps/unix/sysv/linux/Versions +++ b/sysdeps/unix/sysv/linux/Versions @@ -194,6 +194,7 @@ libc { __pread64_nocancel; __close_nocancel; __sigtimedwait; + __convert_scm_timestamps; # functions used by nscd __netlink_assert_response; } 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..3c123c28ce --- /dev/null +++ b/sysdeps/unix/sysv/linux/convert_scm_timestamps.c @@ -0,0 +1,96 @@ +/* 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 + . */ + +#include + +#ifndef __ASSUME_TIME64_SYSCALLS +# include +# include +# include +# include + +/* 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); +} +libc_hidden_def (__convert_scm_timestamps) +#endif diff --git a/sysdeps/unix/sysv/linux/getsockopt.c b/sysdeps/unix/sysv/linux/getsockopt.c index 688a7de087..f86b06dec6 100644 --- a/sysdeps/unix/sysv/linux/getsockopt.c +++ b/sysdeps/unix/sysv/linux/getsockopt.c @@ -70,6 +70,18 @@ getsockopt32 (int fd, int level, int optname, void *optval, else memcpy (optval, &tv32, sizeof tv32); } + break; + + case COMPAT_SO_TIMESTAMP_NEW: + case COMPAT_SO_TIMESTAMPNS_NEW: + { + if (optname == COMPAT_SO_TIMESTAMP_NEW) + optname = COMPAT_SO_TIMESTAMP_OLD; + if (optname == COMPAT_SO_TIMESTAMPNS_NEW) + optname = COMPAT_SO_TIMESTAMPNS_OLD; + r = getsockopt_syscall (fd, level, optname, optval, len); + } + break; } return r; diff --git a/sysdeps/unix/sysv/linux/hppa/socket-constants-time64.h b/sysdeps/unix/sysv/linux/hppa/socket-constants-time64.h index 9fe7576aaa..0a48990ad8 100644 --- a/sysdeps/unix/sysv/linux/hppa/socket-constants-time64.h +++ b/sysdeps/unix/sysv/linux/hppa/socket-constants-time64.h @@ -27,4 +27,9 @@ #define COMPAT_SO_RCVTIMEO_NEW 16448 #define COMPAT_SO_SNDTIMEO_NEW 16449 +#define COMPAT_SO_TIMESTAMP_OLD 0x4012 +#define COMPAT_SO_TIMESTAMPNS_OLD 0x4013 +#define COMPAT_SO_TIMESTAMP_NEW 0x4038 +#define COMPAT_SO_TIMESTAMPNS_NEW 0x4039 + #endif diff --git a/sysdeps/unix/sysv/linux/mips/socket-constants-time64.h b/sysdeps/unix/sysv/linux/mips/socket-constants-time64.h index 1252a8a23b..728f7aa456 100644 --- a/sysdeps/unix/sysv/linux/mips/socket-constants-time64.h +++ b/sysdeps/unix/sysv/linux/mips/socket-constants-time64.h @@ -27,4 +27,9 @@ #define COMPAT_SO_RCVTIMEO_NEW 66 #define COMPAT_SO_SNDTIMEO_NEW 67 +#define COMPAT_SO_TIMESTAMP_OLD 29 +#define COMPAT_SO_TIMESTAMPNS_OLD 35 +#define COMPAT_SO_TIMESTAMP_NEW 63 +#define COMPAT_SO_TIMESTAMPNS_NEW 64 + #endif diff --git a/sysdeps/unix/sysv/linux/powerpc/socket-constants-time64.h b/sysdeps/unix/sysv/linux/powerpc/socket-constants-time64.h index 26e8b710ab..c231b7285a 100644 --- a/sysdeps/unix/sysv/linux/powerpc/socket-constants-time64.h +++ b/sysdeps/unix/sysv/linux/powerpc/socket-constants-time64.h @@ -27,4 +27,9 @@ #define COMPAT_SO_RCVTIMEO_NEW 66 #define COMPAT_SO_SNDTIMEO_NEW 67 +#define COMPAT_SO_TIMESTAMP_OLD 29 +#define COMPAT_SO_TIMESTAMPNS_OLD 35 +#define COMPAT_SO_TIMESTAMP_NEW 63 +#define COMPAT_SO_TIMESTAMPNS_NEW 64 + #endif diff --git a/sysdeps/unix/sysv/linux/recvmsg.c b/sysdeps/unix/sysv/linux/recvmsg.c index b209b4ad99..a2a600228b 100644 --- a/sysdeps/unix/sysv/linux/recvmsg.c +++ b/sysdeps/unix/sysv/linux/recvmsg.c @@ -19,16 +19,27 @@ #include #include #include -#include ssize_t __libc_recvmsg (int fd, struct msghdr *msg, int flags) { -# ifdef __ASSUME_RECVMSG_SYSCALL - return SYSCALL_CANCEL (recvmsg, fd, msg, flags); -# else - return SOCKETCALL_CANCEL (recvmsg, fd, msg, flags); -# endif + ssize_t r; +#ifndef __ASSUME_TIME64_SYSCALLS + socklen_t orig_controllen = msg->msg_controllen; +#endif + +#ifdef __ASSUME_RECVMSG_SYSCALL + r = SYSCALL_CANCEL (recvmsg, fd, msg, flags); +#else + r = SOCKETCALL_CANCEL (recvmsg, fd, msg, flags); +#endif + +#ifndef __ASSUME_TIME64_SYSCALLS + if (r >= 0) + __convert_scm_timestamps (msg, orig_controllen); +#endif + + return r; } weak_alias (__libc_recvmsg, recvmsg) weak_alias (__libc_recvmsg, __recvmsg) diff --git a/sysdeps/unix/sysv/linux/setsockopt.c b/sysdeps/unix/sysv/linux/setsockopt.c index 6505202265..a4780a9d33 100644 --- a/sysdeps/unix/sysv/linux/setsockopt.c +++ b/sysdeps/unix/sysv/linux/setsockopt.c @@ -69,6 +69,18 @@ setsockopt32 (int fd, int level, int optname, const void *optval, r = setsockopt_syscall (fd, level, optname, &tv32, sizeof (tv32)); } + break; + + case COMPAT_SO_TIMESTAMP_NEW: + case COMPAT_SO_TIMESTAMPNS_NEW: + { + if (optname == COMPAT_SO_TIMESTAMP_NEW) + optname = COMPAT_SO_TIMESTAMP_OLD; + if (optname == COMPAT_SO_TIMESTAMPNS_NEW) + optname = COMPAT_SO_TIMESTAMPNS_OLD; + r = setsockopt_syscall (fd, level, optname, NULL, 0); + } + break; } return r; diff --git a/sysdeps/unix/sysv/linux/socket-constants-time64.h b/sysdeps/unix/sysv/linux/socket-constants-time64.h index d09c39d6c2..6cb249cc32 100644 --- a/sysdeps/unix/sysv/linux/socket-constants-time64.h +++ b/sysdeps/unix/sysv/linux/socket-constants-time64.h @@ -27,4 +27,9 @@ #define COMPAT_SO_RCVTIMEO_NEW 66 #define COMPAT_SO_SNDTIMEO_NEW 67 +#define COMPAT_SO_TIMESTAMP_OLD 29 +#define COMPAT_SO_TIMESTAMPNS_OLD 35 +#define COMPAT_SO_TIMESTAMP_NEW 63 +#define COMPAT_SO_TIMESTAMPNS_NEW 64 + #endif diff --git a/sysdeps/unix/sysv/linux/sparc/socket-constants-time64.h b/sysdeps/unix/sysv/linux/sparc/socket-constants-time64.h index f4668db537..4242cb6d19 100644 --- a/sysdeps/unix/sysv/linux/sparc/socket-constants-time64.h +++ b/sysdeps/unix/sysv/linux/sparc/socket-constants-time64.h @@ -27,4 +27,9 @@ #define COMPAT_SO_RCVTIMEO_NEW 68 #define COMPAT_SO_SNDTIMEO_NEW 69 +#define COMPAT_SO_TIMESTAMP_OLD 0x001d +#define COMPAT_SO_TIMESTAMPNS_OLD 0x0021 +#define COMPAT_SO_TIMESTAMP_NEW 0x0046 +#define COMPAT_SO_TIMESTAMPNS_NEW 0x0042 + #endif