linux: always update select timeout (bug 27706)

Message ID mvmmtuaw8jd.fsf@suse.de
State Superseded
Headers
Series linux: always update select timeout (bug 27706) |

Commit Message

Andreas Schwab April 7, 2021, 1:04 p.m. UTC
  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
  

Comments

Adhemerval Zanella April 7, 2021, 2:19 p.m. UTC | #1
On 07/04/2021 10:04, Andreas Schwab wrote:
> 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.

Could we the other select fix [1] revised as well? With that I can add
the timeout tests on the generic tests instead of add as Linux
specific.

https://patchwork.sourceware.org/project/glibc/patch/20210331175610.3002618-1-adhemerval.zanella@linaro.org/ ?


> ---
>  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
> +   <https://www.gnu.org/licenses/>.  */
> +
> +#include <sys/select.h>
> +#include <errno.h>
> +#include <unistd.h>
> +#include <support/check.h>
> +#include <support/xsignal.h>
> +
> +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 <support/test-driver.c>
>
  

Patch

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
+   <https://www.gnu.org/licenses/>.  */
+
+#include <sys/select.h>
+#include <errno.h>
+#include <unistd.h>
+#include <support/check.h>
+#include <support/xsignal.h>
+
+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 <support/test-driver.c>