From patchwork Wed Apr 7 13:04:06 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Schwab X-Patchwork-Id: 42926 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 9C9F3388A035; Wed, 7 Apr 2021 13:04:18 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mx2.suse.de (mx2.suse.de [195.135.220.15]) by sourceware.org (Postfix) with ESMTPS id 3D0F03858028 for ; Wed, 7 Apr 2021 13:04:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 3D0F03858028 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=schwab@suse.de X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 5EB24AD2D for ; Wed, 7 Apr 2021 13:04:06 +0000 (UTC) From: Andreas Schwab To: libc-alpha@sourceware.org Subject: [PATCH] linux: always update select timeout (bug 27706) X-Yow: Now I am depressed... Date: Wed, 07 Apr 2021 15:04:06 +0200 Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux) MIME-Version: 1.0 X-Spam-Status: No, score=-9.4 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, KAM_SHORT, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, 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: , Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" When select returns, sucessfully or not, the timeout argument should always be updated with the remaining timeout value. Due to a use of the wrong function for conversion, that never happend for configurations that support 64-bit time_t. For other configurations, the timeout was only updated on sucessful return. --- sysdeps/unix/sysv/linux/Makefile | 2 +- sysdeps/unix/sysv/linux/select.c | 6 +- .../sysv/linux/tst-select-update-timeout.c | 55 +++++++++++++++++++ 3 files changed, 59 insertions(+), 4 deletions(-) create mode 100644 sysdeps/unix/sysv/linux/tst-select-update-timeout.c diff --git a/sysdeps/unix/sysv/linux/Makefile b/sysdeps/unix/sysv/linux/Makefile index 303fa297bc..e8524e0483 100644 --- a/sysdeps/unix/sysv/linux/Makefile +++ b/sysdeps/unix/sysv/linux/Makefile @@ -109,7 +109,7 @@ tests += tst-clone tst-clone2 tst-clone3 tst-fanotify tst-personality \ tst-tgkill tst-sysvsem-linux tst-sysvmsg-linux tst-sysvshm-linux \ tst-timerfd tst-ppoll tst-futimens tst-utime tst-utimes \ tst-clock_adjtime tst-adjtimex tst-ntp_adjtime tst-ntp_gettime \ - tst-ntp_gettimex tst-sigtimedwait + tst-ntp_gettimex tst-sigtimedwait tst-select-update-timeout # Test for the symbol version of fcntl that was replaced in glibc 2.28. ifeq ($(have-GLIBC_2.27)$(build-shared),yesyes) diff --git a/sysdeps/unix/sysv/linux/select.c b/sysdeps/unix/sysv/linux/select.c index 415aa87d3c..ad3e0ec634 100644 --- a/sysdeps/unix/sysv/linux/select.c +++ b/sysdeps/unix/sysv/linux/select.c @@ -55,7 +55,7 @@ __select64 (int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, if (r == 0 || errno != ENOSYS) { if (timeout != NULL) - TIMEVAL_TO_TIMESPEC (timeout, &ts64); + TIMESPEC_TO_TIMEVAL (timeout, &ts64); return r; } @@ -84,7 +84,7 @@ __select64 (int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, r = SYSCALL_CANCEL (pselect6, nfds, readfds, writefds, exceptfds, pts32, NULL); # endif - if (r >= 0 && timeout != NULL) + if (timeout != NULL) *timeout = valid_timespec_to_timeval64 (ts32); #endif @@ -105,7 +105,7 @@ __select (int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, ptv64 = &tv64; } int r = __select64 (nfds, readfds, writefds, exceptfds, ptv64); - if (r >= 0 && timeout != NULL) + if (timeout != NULL) /* The remanining timeout will be always less the input TIMEOUT. */ *timeout = valid_timeval64_to_timeval (tv64); return r; diff --git a/sysdeps/unix/sysv/linux/tst-select-update-timeout.c b/sysdeps/unix/sysv/linux/tst-select-update-timeout.c new file mode 100644 index 0000000000..454d4e3ada --- /dev/null +++ b/sysdeps/unix/sysv/linux/tst-select-update-timeout.c @@ -0,0 +1,55 @@ +/* Test for select timeout updated after return. + 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 +#include +#include +#include +#include + +static void +alarm_handler (int signum) +{ + /* Do nothing. */ +} + +static int +do_test (void) +{ + /* When select returns, successfully or not, the timeout argument should + be updated with the remaining timeout value. */ + + struct timeval tv = { .tv_sec = 1 }; + int r = select (0, NULL, NULL, NULL, &tv); + TEST_COMPARE (r, 0); + TEST_COMPARE (tv.tv_sec, 0); + + struct sigaction act = { .sa_handler = alarm_handler }; + xsigaction (SIGALRM, &act, NULL); + alarm (2); + + tv.tv_sec = 10; + r = select (0, NULL, NULL, NULL, &tv); + TEST_COMPARE (r, -1); + TEST_COMPARE (errno, EINTR); + TEST_VERIFY (tv.tv_sec < 10); + + return 0; +} + +#include