8ae002
commit cf0bd2f73bd65beab613865bba567d7787836888
8ae002
Author: Florian Weimer <fweimer@redhat.com>
8ae002
Date:   Tue Feb 28 15:28:45 2017 +0100
8ae002
8ae002
    sunrpc: Improvements for UDP client timeout handling [BZ #20257]
8ae002
    
8ae002
    This commit fixes various aspects in the UDP client timeout handling.
8ae002
    Timeouts are now applied in a more consistent fashion.  Discarded UDP
8ae002
    packets no longer prevent the timeout from happening at all.
8ae002
8ae002
Index: b/inet/deadline.c
8ae002
===================================================================
8ae002
--- /dev/null
8ae002
+++ b/inet/deadline.c
8ae002
@@ -0,0 +1,122 @@
8ae002
+/* Computing deadlines for timeouts.
8ae002
+   Copyright (C) 2017 Free Software Foundation, Inc.
8ae002
+   This file is part of the GNU C Library.
8ae002
+
8ae002
+   The GNU C Library is free software; you can redistribute it and/or
8ae002
+   modify it under the terms of the GNU Lesser General Public
8ae002
+   License as published by the Free Software Foundation; either
8ae002
+   version 2.1 of the License, or (at your option) any later version.
8ae002
+
8ae002
+   The GNU C Library is distributed in the hope that it will be useful,
8ae002
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
8ae002
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
8ae002
+   Lesser General Public License for more details.
8ae002
+
8ae002
+   You should have received a copy of the GNU Lesser General Public
8ae002
+   License along with the GNU C Library; if not, see
8ae002
+   <http://www.gnu.org/licenses/>.  */
8ae002
+
8ae002
+#include <net-internal.h>
8ae002
+
8ae002
+#include <assert.h>
8ae002
+#include <limits.h>
8ae002
+#include <stdio.h>
8ae002
+#include <stdint.h>
8ae002
+#include <time.h>
8ae002
+
8ae002
+struct deadline_current_time internal_function
8ae002
+__deadline_current_time (void)
8ae002
+{
8ae002
+  struct deadline_current_time result;
8ae002
+  if (__clock_gettime (CLOCK_MONOTONIC, &result.current) != 0)
8ae002
+    {
8ae002
+      struct timeval current_tv;
8ae002
+      if (__gettimeofday (&current_tv, NULL) == 0)
8ae002
+        __libc_fatal ("Fatal error: gettimeofday system call failed\n");
8ae002
+      result.current.tv_sec = current_tv.tv_sec;
8ae002
+      result.current.tv_nsec = current_tv.tv_usec * 1000;
8ae002
+    }
8ae002
+  assert (result.current.tv_sec >= 0);
8ae002
+  return result;
8ae002
+}
8ae002
+
8ae002
+/* A special deadline value for which __deadline_is_infinite is
8ae002
+   true.  */
8ae002
+static inline struct deadline
8ae002
+infinite_deadline (void)
8ae002
+{
8ae002
+  return (struct deadline) { { -1, -1 } };
8ae002
+}
8ae002
+
8ae002
+struct deadline internal_function
8ae002
+__deadline_from_timeval (struct deadline_current_time current,
8ae002
+                         struct timeval tv)
8ae002
+{
8ae002
+  assert (__is_timeval_valid_timeout (tv));
8ae002
+
8ae002
+  /* Compute second-based deadline.  Perform the addition in
8ae002
+     uintmax_t, which is unsigned, to simply overflow detection.  */
8ae002
+  uintmax_t sec = current.current.tv_sec;
8ae002
+  sec += tv.tv_sec;
8ae002
+  if (sec < (uintmax_t) tv.tv_sec)
8ae002
+    return infinite_deadline ();
8ae002
+
8ae002
+  /* Compute nanosecond deadline.  */
8ae002
+  int nsec = current.current.tv_nsec + tv.tv_usec * 1000;
8ae002
+  if (nsec >= 1000 * 1000 * 1000)
8ae002
+    {
8ae002
+      /* Carry nanosecond overflow to seconds.  */
8ae002
+      nsec -= 1000 * 1000 * 1000;
8ae002
+      if (sec + 1 < sec)
8ae002
+        return infinite_deadline ();
8ae002
+      ++sec;
8ae002
+    }
8ae002
+  /* This uses a GCC extension, otherwise these casts for detecting
8ae002
+     overflow would not be defined.  */
8ae002
+  if ((time_t) sec < 0 || sec != (uintmax_t) (time_t) sec)
8ae002
+    return infinite_deadline ();
8ae002
+
8ae002
+  return (struct deadline) { { sec, nsec } };
8ae002
+}
8ae002
+
8ae002
+int internal_function
8ae002
+__deadline_to_ms (struct deadline_current_time current,
8ae002
+                  struct deadline deadline)
8ae002
+{
8ae002
+  if (__deadline_is_infinite (deadline))
8ae002
+    return INT_MAX;
8ae002
+
8ae002
+  if (current.current.tv_sec > deadline.absolute.tv_sec
8ae002
+      || (current.current.tv_sec == deadline.absolute.tv_sec
8ae002
+          && current.current.tv_nsec >= deadline.absolute.tv_nsec))
8ae002
+    return 0;
8ae002
+  time_t sec = deadline.absolute.tv_sec - current.current.tv_sec;
8ae002
+  if (sec >= INT_MAX)
8ae002
+    /* This value will overflow below.  */
8ae002
+    return INT_MAX;
8ae002
+  int nsec = deadline.absolute.tv_nsec - current.current.tv_nsec;
8ae002
+  if (nsec < 0)
8ae002
+    {
8ae002
+      /* Borrow from the seconds field.  */
8ae002
+      assert (sec > 0);
8ae002
+      --sec;
8ae002
+      nsec += 1000 * 1000 * 1000;
8ae002
+    }
8ae002
+
8ae002
+  /* Prepare for rounding up to milliseconds.  */
8ae002
+  nsec += 999999;
8ae002
+  if (nsec > 1000 * 1000 * 1000)
8ae002
+    {
8ae002
+      assert (sec < INT_MAX);
8ae002
+      ++sec;
8ae002
+      nsec -= 1000 * 1000 * 1000;
8ae002
+    }
8ae002
+
8ae002
+  unsigned int msec = nsec / (1000 * 1000);
8ae002
+  if (sec > INT_MAX / 1000)
8ae002
+    return INT_MAX;
8ae002
+  msec += sec * 1000;
8ae002
+  if (msec > INT_MAX)
8ae002
+    return INT_MAX;
8ae002
+  return msec;
8ae002
+}
8ae002
Index: b/inet/tst-deadline.c
8ae002
===================================================================
8ae002
--- /dev/null
8ae002
+++ b/inet/tst-deadline.c
8ae002
@@ -0,0 +1,188 @@
8ae002
+/* Tests for computing deadlines for timeouts.
8ae002
+   Copyright (C) 2017 Free Software Foundation, Inc.
8ae002
+   This file is part of the GNU C Library.
8ae002
+
8ae002
+   The GNU C Library is free software; you can redistribute it and/or
8ae002
+   modify it under the terms of the GNU Lesser General Public
8ae002
+   License as published by the Free Software Foundation; either
8ae002
+   version 2.1 of the License, or (at your option) any later version.
8ae002
+
8ae002
+   The GNU C Library is distributed in the hope that it will be useful,
8ae002
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
8ae002
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
8ae002
+   Lesser General Public License for more details.
8ae002
+
8ae002
+   You should have received a copy of the GNU Lesser General Public
8ae002
+   License along with the GNU C Library; if not, see
8ae002
+   <http://www.gnu.org/licenses/>.  */
8ae002
+
8ae002
+#include <inet/net-internal.h>
8ae002
+#include <limits.h>
8ae002
+#include <stdbool.h>
8ae002
+#include <stdint.h>
8ae002
+#include <support/check.h>
8ae002
+
8ae002
+/* Find the maximum value which can be represented in a time_t.  */
8ae002
+static time_t
8ae002
+time_t_max (void)
8ae002
+{
8ae002
+  _Static_assert (0 > (time_t) -1, "time_t is signed");
8ae002
+  uintmax_t current = 1;
8ae002
+  while (true)
8ae002
+    {
8ae002
+      uintmax_t next = current * 2;
8ae002
+      /* This cannot happen because time_t is signed.  */
8ae002
+      TEST_VERIFY_EXIT (next > current);
8ae002
+      ++next;
8ae002
+      if ((time_t) next < 0 || next != (uintmax_t) (time_t) next)
8ae002
+        /* Value cannot be represented in time_t.  Return the previous
8ae002
+           value. */
8ae002
+        return current;
8ae002
+      current = next;
8ae002
+    }
8ae002
+}
8ae002
+
8ae002
+static int
8ae002
+do_test (void)
8ae002
+{
8ae002
+  {
8ae002
+    struct deadline_current_time current_time = __deadline_current_time ();
8ae002
+    TEST_VERIFY (current_time.current.tv_sec >= 0);
8ae002
+    current_time = __deadline_current_time ();
8ae002
+    /* Due to CLOCK_MONOTONIC, either seconds or nanoseconds are
8ae002
+       greater than zero.  This is also true for the gettimeofday
8ae002
+       fallback.  */
8ae002
+    TEST_VERIFY (current_time.current.tv_sec >= 0);
8ae002
+    TEST_VERIFY (current_time.current.tv_sec > 0
8ae002
+                 || current_time.current.tv_nsec > 0);
8ae002
+  }
8ae002
+
8ae002
+  /* Check basic computations of deadlines.  */
8ae002
+  struct deadline_current_time current_time = { { 1, 123456789 } };
8ae002
+  struct deadline deadline = __deadline_from_timeval
8ae002
+    (current_time, (struct timeval) { 0, 1 });
8ae002
+  TEST_VERIFY (deadline.absolute.tv_sec == 1);
8ae002
+  TEST_VERIFY (deadline.absolute.tv_nsec == 123457789);
8ae002
+  TEST_VERIFY (__deadline_to_ms (current_time, deadline) == 1);
8ae002
+
8ae002
+  deadline = __deadline_from_timeval
8ae002
+    (current_time, ((struct timeval) { 0, 2 }));
8ae002
+  TEST_VERIFY (deadline.absolute.tv_sec == 1);
8ae002
+  TEST_VERIFY (deadline.absolute.tv_nsec == 123458789);
8ae002
+  TEST_VERIFY (__deadline_to_ms (current_time, deadline) == 1);
8ae002
+
8ae002
+  deadline = __deadline_from_timeval
8ae002
+    (current_time, ((struct timeval) { 1, 0 }));
8ae002
+  TEST_VERIFY (deadline.absolute.tv_sec == 2);
8ae002
+  TEST_VERIFY (deadline.absolute.tv_nsec == 123456789);
8ae002
+  TEST_VERIFY (__deadline_to_ms (current_time, deadline) == 1000);
8ae002
+
8ae002
+  /* Check if timeouts are correctly rounded up to the next
8ae002
+     millisecond.  */
8ae002
+  for (int i = 0; i < 999999; ++i)
8ae002
+    {
8ae002
+      ++current_time.current.tv_nsec;
8ae002
+      TEST_VERIFY (__deadline_to_ms (current_time, deadline) == 1000);
8ae002
+    }
8ae002
+
8ae002
+  /* A full millisecond has elapsed, so the time to the deadline is
8ae002
+     now less than 1000.  */
8ae002
+  ++current_time.current.tv_nsec;
8ae002
+  TEST_VERIFY (__deadline_to_ms (current_time, deadline) == 999);
8ae002
+
8ae002
+  /* Check __deadline_to_ms carry-over.  */
8ae002
+  current_time = (struct deadline_current_time) { { 9, 123456789 } };
8ae002
+  deadline = (struct deadline) { { 10, 122456789 } };
8ae002
+  TEST_VERIFY (__deadline_to_ms (current_time, deadline) == 999);
8ae002
+  deadline = (struct deadline) { { 10, 122456790 } };
8ae002
+  TEST_VERIFY (__deadline_to_ms (current_time, deadline) == 1000);
8ae002
+  deadline = (struct deadline) { { 10, 123456788 } };
8ae002
+  TEST_VERIFY (__deadline_to_ms (current_time, deadline) == 1000);
8ae002
+  deadline = (struct deadline) { { 10, 123456789 } };
8ae002
+  TEST_VERIFY (__deadline_to_ms (current_time, deadline) == 1000);
8ae002
+
8ae002
+  /* Check __deadline_to_ms overflow.  */
8ae002
+  deadline = (struct deadline) { { INT_MAX - 1, 1 } };
8ae002
+  TEST_VERIFY (__deadline_to_ms (current_time, deadline) == INT_MAX);
8ae002
+
8ae002
+  /* Check __deadline_to_ms for elapsed deadlines.  */
8ae002
+  current_time = (struct deadline_current_time) { { 9, 123456789 } };
8ae002
+  deadline.absolute = current_time.current;
8ae002
+  TEST_VERIFY (__deadline_to_ms (current_time, deadline) == 0);
8ae002
+  current_time = (struct deadline_current_time) { { 9, 123456790 } };
8ae002
+  TEST_VERIFY (__deadline_to_ms (current_time, deadline) == 0);
8ae002
+  current_time = (struct deadline_current_time) { { 10, 0 } };
8ae002
+  TEST_VERIFY (__deadline_to_ms (current_time, deadline) == 0);
8ae002
+  current_time = (struct deadline_current_time) { { 10, 123456788 } };
8ae002
+  TEST_VERIFY (__deadline_to_ms (current_time, deadline) == 0);
8ae002
+  current_time = (struct deadline_current_time) { { 10, 123456789 } };
8ae002
+  TEST_VERIFY (__deadline_to_ms (current_time, deadline) == 0);
8ae002
+
8ae002
+  /* Check carry-over in __deadline_from_timeval.  */
8ae002
+  current_time = (struct deadline_current_time) { { 9, 998000001 } };
8ae002
+  for (int i = 0; i < 2000; ++i)
8ae002
+    {
8ae002
+      deadline = __deadline_from_timeval
8ae002
+        (current_time, (struct timeval) { 1, i });
8ae002
+      TEST_VERIFY (deadline.absolute.tv_sec == 10);
8ae002
+      TEST_VERIFY (deadline.absolute.tv_nsec == 998000001 + i * 1000);
8ae002
+    }
8ae002
+  for (int i = 2000; i < 3000; ++i)
8ae002
+    {
8ae002
+      deadline = __deadline_from_timeval
8ae002
+        (current_time, (struct timeval) { 2, i });
8ae002
+      TEST_VERIFY (deadline.absolute.tv_sec == 12);
8ae002
+      TEST_VERIFY (deadline.absolute.tv_nsec == 1 + (i - 2000) * 1000);
8ae002
+    }
8ae002
+
8ae002
+  /* Check infinite deadlines.  */
8ae002
+  deadline = __deadline_from_timeval
8ae002
+    ((struct deadline_current_time) { { 0, 1000 * 1000 * 1000 - 1000 } },
8ae002
+     (struct timeval) { time_t_max (), 1 });
8ae002
+  TEST_VERIFY (__deadline_is_infinite (deadline));
8ae002
+  deadline = __deadline_from_timeval
8ae002
+    ((struct deadline_current_time) { { 0, 1000 * 1000 * 1000 - 1001 } },
8ae002
+     (struct timeval) { time_t_max (), 1 });
8ae002
+  TEST_VERIFY (!__deadline_is_infinite (deadline));
8ae002
+  deadline = __deadline_from_timeval
8ae002
+    ((struct deadline_current_time)
8ae002
+       { { time_t_max (), 1000 * 1000 * 1000 - 1000 } },
8ae002
+     (struct timeval) { 0, 1 });
8ae002
+  TEST_VERIFY (__deadline_is_infinite (deadline));
8ae002
+  deadline = __deadline_from_timeval
8ae002
+    ((struct deadline_current_time)
8ae002
+       { { time_t_max () / 2 + 1, 0 } },
8ae002
+     (struct timeval) { time_t_max () / 2 + 1, 0 });
8ae002
+  TEST_VERIFY (__deadline_is_infinite (deadline));
8ae002
+
8ae002
+  /* Check __deadline_first behavior.  */
8ae002
+  deadline = __deadline_first
8ae002
+    ((struct deadline) { { 1, 2 } },
8ae002
+     (struct deadline) { { 1, 3 } });
8ae002
+  TEST_VERIFY (deadline.absolute.tv_sec == 1);
8ae002
+  TEST_VERIFY (deadline.absolute.tv_nsec == 2);
8ae002
+  deadline = __deadline_first
8ae002
+    ((struct deadline) { { 1, 3 } },
8ae002
+     (struct deadline) { { 1, 2 } });
8ae002
+  TEST_VERIFY (deadline.absolute.tv_sec == 1);
8ae002
+  TEST_VERIFY (deadline.absolute.tv_nsec == 2);
8ae002
+  deadline = __deadline_first
8ae002
+    ((struct deadline) { { 1, 2 } },
8ae002
+     (struct deadline) { { 2, 1 } });
8ae002
+  TEST_VERIFY (deadline.absolute.tv_sec == 1);
8ae002
+  TEST_VERIFY (deadline.absolute.tv_nsec == 2);
8ae002
+  deadline = __deadline_first
8ae002
+    ((struct deadline) { { 1, 2 } },
8ae002
+     (struct deadline) { { 2, 4 } });
8ae002
+  TEST_VERIFY (deadline.absolute.tv_sec == 1);
8ae002
+  TEST_VERIFY (deadline.absolute.tv_nsec == 2);
8ae002
+  deadline = __deadline_first
8ae002
+    ((struct deadline) { { 2, 4 } },
8ae002
+     (struct deadline) { { 1, 2 } });
8ae002
+  TEST_VERIFY (deadline.absolute.tv_sec == 1);
8ae002
+  TEST_VERIFY (deadline.absolute.tv_nsec == 2);
8ae002
+
8ae002
+  return 0;
8ae002
+}
8ae002
+
8ae002
+#include <support/test-driver.c>
8ae002
Index: b/sunrpc/Makefile
8ae002
===================================================================
8ae002
--- a/sunrpc/Makefile
8ae002
+++ b/sunrpc/Makefile
8ae002
@@ -96,11 +96,13 @@ others += rpcgen
8ae002
 
8ae002
 all: # Make this the default target; it will be defined in Rules.
8ae002
 
8ae002
-tests = tst-xdrmem tst-xdrmem2
8ae002
+tests = tst-xdrmem tst-xdrmem2 tst-udp-timeout \
8ae002
+  tst-udp-nonblocking
8ae002
 xtests := tst-getmyaddr
8ae002
 
8ae002
 ifeq ($(have-thread-library),yes)
8ae002
 xtests += thrsvc
8ae002
+tests += tst-udp-garbage
8ae002
 endif
8ae002
 
8ae002
 headers += $(rpcsvc:%.x=rpcsvc/%.h)
8ae002
@@ -225,3 +227,8 @@ endif
8ae002
 endif
8ae002
 
8ae002
 $(objpfx)thrsvc: $(common-objpfx)linkobj/libc.so $(shared-thread-library)
8ae002
+
8ae002
+$(objpfx)tst-udp-timeout: $(common-objpfx)linkobj/libc.so
8ae002
+$(objpfx)tst-udp-nonblocking: $(common-objpfx)linkobj/libc.so
8ae002
+$(objpfx)tst-udp-garbage: \
8ae002
+  $(common-objpfx)linkobj/libc.so $(shared-thread-library)
8ae002
Index: b/sunrpc/clnt_udp.c
8ae002
===================================================================
8ae002
--- a/sunrpc/clnt_udp.c
8ae002
+++ b/sunrpc/clnt_udp.c
8ae002
@@ -54,6 +54,7 @@
8ae002
 #endif
8ae002
 
8ae002
 #include <kernel-features.h>
8ae002
+#include <inet/net-internal.h>
8ae002
 
8ae002
 extern u_long _create_xid (void);
8ae002
 
8ae002
@@ -79,7 +80,9 @@ static const struct clnt_ops udp_ops =
8ae002
 };
8ae002
 
8ae002
 /*
8ae002
- * Private data kept per client handle
8ae002
+ * Private data kept per client handle.  This private struct is
8ae002
+ * unfortunately part of the ABI; ypbind contains a copy of it and
8ae002
+ * accesses it through CLIENT::cl_private field.
8ae002
  */
8ae002
 struct cu_data
8ae002
   {
8ae002
@@ -309,28 +312,38 @@ clntudp_call (cl, proc, xargs, argsp, xr
8ae002
   int inlen;
8ae002
   socklen_t fromlen;
8ae002
   struct pollfd fd;
8ae002
-  int milliseconds = (cu->cu_wait.tv_sec * 1000) +
8ae002
-    (cu->cu_wait.tv_usec / 1000);
8ae002
   struct sockaddr_in from;
8ae002
   struct rpc_msg reply_msg;
8ae002
   XDR reply_xdrs;
8ae002
-  struct timeval time_waited;
8ae002
   bool_t ok;
8ae002
   int nrefreshes = 2;		/* number of times to refresh cred */
8ae002
-  struct timeval timeout;
8ae002
   int anyup;			/* any network interface up */
8ae002
 
8ae002
-  if (cu->cu_total.tv_usec == -1)
8ae002
-    {
8ae002
-      timeout = utimeout;	/* use supplied timeout */
8ae002
-    }
8ae002
-  else
8ae002
-    {
8ae002
-      timeout = cu->cu_total;	/* use default timeout */
8ae002
-    }
8ae002
+  struct deadline_current_time current_time = __deadline_current_time ();
8ae002
+  struct deadline total_deadline; /* Determined once by overall timeout.  */
8ae002
+  struct deadline response_deadline; /* Determined anew for each query.  */
8ae002
+
8ae002
+  /* Choose the timeout value.  For non-sending usage (xargs == NULL),
8ae002
+     the total deadline does not matter, only cu->cu_wait is used
8ae002
+     below.  */
8ae002
+  if (xargs != NULL)
8ae002
+    {
8ae002
+      struct timeval tv;
8ae002
+      if (cu->cu_total.tv_usec == -1)
8ae002
+	/* Use supplied timeout.  */
8ae002
+	tv = utimeout;
8ae002
+      else
8ae002
+	/* Use default timeout.  */
8ae002
+	tv = cu->cu_total;
8ae002
+      if (!__is_timeval_valid_timeout (tv))
8ae002
+	return (cu->cu_error.re_status = RPC_TIMEDOUT);
8ae002
+      total_deadline = __deadline_from_timeval (current_time, tv);
8ae002
+    }
8ae002
+
8ae002
+  /* Guard against bad timeout specification.  */
8ae002
+  if (!__is_timeval_valid_timeout (cu->cu_wait))
8ae002
+    return (cu->cu_error.re_status = RPC_TIMEDOUT);
8ae002
 
8ae002
-  time_waited.tv_sec = 0;
8ae002
-  time_waited.tv_usec = 0;
8ae002
 call_again:
8ae002
   xdrs = &(cu->cu_outxdrs);
8ae002
   if (xargs == NULL)
8ae002
@@ -356,27 +369,46 @@ send_again:
8ae002
       return (cu->cu_error.re_status = RPC_CANTSEND);
8ae002
     }
8ae002
 
8ae002
-  /*
8ae002
-   * Hack to provide rpc-based message passing
8ae002
-   */
8ae002
-  if (timeout.tv_sec == 0 && timeout.tv_usec == 0)
8ae002
-    {
8ae002
-      return (cu->cu_error.re_status = RPC_TIMEDOUT);
8ae002
-    }
8ae002
+  /* sendto may have blocked, so recompute the current time.  */
8ae002
+  current_time = __deadline_current_time ();
8ae002
  get_reply:
8ae002
-  /*
8ae002
-   * sub-optimal code appears here because we have
8ae002
-   * some clock time to spare while the packets are in flight.
8ae002
-   * (We assume that this is actually only executed once.)
8ae002
-   */
8ae002
+  response_deadline = __deadline_from_timeval (current_time, cu->cu_wait);
8ae002
+
8ae002
   reply_msg.acpted_rply.ar_verf = _null_auth;
8ae002
   reply_msg.acpted_rply.ar_results.where = resultsp;
8ae002
   reply_msg.acpted_rply.ar_results.proc = xresults;
8ae002
   fd.fd = cu->cu_sock;
8ae002
   fd.events = POLLIN;
8ae002
   anyup = 0;
8ae002
+
8ae002
+  /* Per-response retry loop.  current_time must be up-to-date at the
8ae002
+     top of the loop.  */
8ae002
   for (;;)
8ae002
     {
8ae002
+      int milliseconds;
8ae002
+      if (xargs != NULL)
8ae002
+	{
8ae002
+	  if (__deadline_elapsed (current_time, total_deadline))
8ae002
+	    /* Overall timeout expired.  */
8ae002
+	    return (cu->cu_error.re_status = RPC_TIMEDOUT);
8ae002
+	  milliseconds = __deadline_to_ms
8ae002
+	    (current_time, __deadline_first (total_deadline,
8ae002
+					     response_deadline));
8ae002
+	  if (milliseconds == 0)
8ae002
+	    /* Per-query timeout expired.  */
8ae002
+	    goto send_again;
8ae002
+	}
8ae002
+      else
8ae002
+	{
8ae002
+	  /* xatgs == NULL.  Collect a response without sending a
8ae002
+	     query.  In this mode, we need to ignore the total
8ae002
+	     deadline.  */
8ae002
+	  milliseconds = __deadline_to_ms (current_time, response_deadline);
8ae002
+	  if (milliseconds == 0)
8ae002
+	    /* Cannot send again, so bail out.  */
8ae002
+	    return (cu->cu_error.re_status = RPC_CANTSEND);
8ae002
+	}
8ae002
+
8ae002
       switch (__poll (&fd, 1, milliseconds))
8ae002
 	{
8ae002
 
8ae002
@@ -387,27 +419,10 @@ send_again:
8ae002
 	      if (!anyup)
8ae002
 		return (cu->cu_error.re_status = RPC_CANTRECV);
8ae002
 	    }
8ae002
-
8ae002
-	  time_waited.tv_sec += cu->cu_wait.tv_sec;
8ae002
-	  time_waited.tv_usec += cu->cu_wait.tv_usec;
8ae002
-	  while (time_waited.tv_usec >= 1000000)
8ae002
-	    {
8ae002
-	      time_waited.tv_sec++;
8ae002
-	      time_waited.tv_usec -= 1000000;
8ae002
-	    }
8ae002
-	  if ((time_waited.tv_sec < timeout.tv_sec) ||
8ae002
-	      ((time_waited.tv_sec == timeout.tv_sec) &&
8ae002
-	       (time_waited.tv_usec < timeout.tv_usec)))
8ae002
-	    goto send_again;
8ae002
-	  return (cu->cu_error.re_status = RPC_TIMEDOUT);
8ae002
-
8ae002
-	  /*
8ae002
-	   * buggy in other cases because time_waited is not being
8ae002
-	   * updated.
8ae002
-	   */
8ae002
+	  goto next_response;
8ae002
 	case -1:
8ae002
 	  if (errno == EINTR)
8ae002
-	    continue;
8ae002
+	    goto next_response;
8ae002
 	  cu->cu_error.re_errno = errno;
8ae002
 	  return (cu->cu_error.re_status = RPC_CANTRECV);
8ae002
 	}
8ae002
@@ -463,20 +478,22 @@ send_again:
8ae002
       if (inlen < 0)
8ae002
 	{
8ae002
 	  if (errno == EWOULDBLOCK)
8ae002
-	    continue;
8ae002
+	    goto next_response;
8ae002
 	  cu->cu_error.re_errno = errno;
8ae002
 	  return (cu->cu_error.re_status = RPC_CANTRECV);
8ae002
 	}
8ae002
-      if (inlen < 4)
8ae002
-	continue;
8ae002
+      /* Accept the response if the packet is sufficiently long and
8ae002
+	 the transaction ID matches the query (if available).  */
8ae002
+      if (inlen >= 4
8ae002
+	  && (xargs == NULL
8ae002
+	      || memcmp (cu->cu_inbuf, cu->cu_outbuf,
8ae002
+			 sizeof (u_int32_t)) == 0))
8ae002
+	break;
8ae002
 
8ae002
-      /* see if reply transaction id matches sent id.
8ae002
-	Don't do this if we only wait for a replay */
8ae002
-      if (xargs != NULL
8ae002
-	  && memcmp (cu->cu_inbuf, cu->cu_outbuf, sizeof (u_int32_t)) != 0)
8ae002
-	continue;
8ae002
-      /* we now assume we have the proper reply */
8ae002
-      break;
8ae002
+    next_response:
8ae002
+      /* Update the current time because poll and recvmsg waited for
8ae002
+	 an unknown time.  */
8ae002
+      current_time = __deadline_current_time ();
8ae002
     }
8ae002
 
8ae002
   /*
8ae002
Index: b/sunrpc/tst-udp-garbage.c
8ae002
===================================================================
8ae002
--- /dev/null
8ae002
+++ b/sunrpc/tst-udp-garbage.c
8ae002
@@ -0,0 +1,104 @@
8ae002
+/* Test that garbage packets do not affect timeout handling.
8ae002
+   Copyright (C) 2017 Free Software Foundation, Inc.
8ae002
+   This file is part of the GNU C Library.
8ae002
+
8ae002
+   The GNU C Library is free software; you can redistribute it and/or
8ae002
+   modify it under the terms of the GNU Lesser General Public
8ae002
+   License as published by the Free Software Foundation; either
8ae002
+   version 2.1 of the License, or (at your option) any later version.
8ae002
+
8ae002
+   The GNU C Library is distributed in the hope that it will be useful,
8ae002
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
8ae002
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
8ae002
+   Lesser General Public License for more details.
8ae002
+
8ae002
+   You should have received a copy of the GNU Lesser General Public
8ae002
+   License along with the GNU C Library; if not, see
8ae002
+   <http://www.gnu.org/licenses/>.  */
8ae002
+
8ae002
+#include <netinet/in.h>
8ae002
+#include <rpc/clnt.h>
8ae002
+#include <rpc/svc.h>
8ae002
+#include <stdbool.h>
8ae002
+#include <support/check.h>
8ae002
+#include <support/namespace.h>
8ae002
+#include <support/xsocket.h>
8ae002
+#include <support/xthread.h>
8ae002
+#include <sys/socket.h>
8ae002
+#include <unistd.h>
8ae002
+
8ae002
+/* Descriptor for the server UDP socket.  */
8ae002
+static int server_fd;
8ae002
+
8ae002
+static void *
8ae002
+garbage_sender_thread (void *unused)
8ae002
+{
8ae002
+  while (true)
8ae002
+    {
8ae002
+      struct sockaddr_storage sa;
8ae002
+      socklen_t salen = sizeof (sa);
8ae002
+      char buf[1];
8ae002
+      if (recvfrom (server_fd, buf, sizeof (buf), 0,
8ae002
+                    (struct sockaddr *) &sa, &salen) < 0)
8ae002
+        FAIL_EXIT1 ("recvfrom: %m");
8ae002
+
8ae002
+      /* Send garbage packets indefinitely.  */
8ae002
+      buf[0] = 0;
8ae002
+      while (true)
8ae002
+        {
8ae002
+          /* sendto can fail if the client closed the socket.  */
8ae002
+          if (sendto (server_fd, buf, sizeof (buf), 0,
8ae002
+                      (struct sockaddr *) &sa, salen) < 0)
8ae002
+            break;
8ae002
+
8ae002
+          /* Wait a bit, to avoid burning too many CPU cycles in a
8ae002
+             tight loop.  The wait period must be much shorter than
8ae002
+             the client timeouts configured below.  */
8ae002
+          usleep (50 * 1000);
8ae002
+        }
8ae002
+    }
8ae002
+}
8ae002
+
8ae002
+static int
8ae002
+do_test (void)
8ae002
+{
8ae002
+  support_become_root ();
8ae002
+  support_enter_network_namespace ();
8ae002
+
8ae002
+  server_fd = xsocket (AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP);
8ae002
+  struct sockaddr_in server_address =
8ae002
+    {
8ae002
+      .sin_family = AF_INET,
8ae002
+      .sin_addr.s_addr = htonl (INADDR_LOOPBACK),
8ae002
+    };
8ae002
+  xbind (server_fd,
8ae002
+         (struct sockaddr *) &server_address, sizeof (server_address));
8ae002
+  {
8ae002
+    socklen_t sinlen = sizeof (server_address);
8ae002
+    xgetsockname (server_fd, (struct sockaddr *) &server_address, &sinlen);
8ae002
+    TEST_VERIFY (sizeof (server_address) == sinlen);
8ae002
+  }
8ae002
+
8ae002
+  /* Garbage packet source.  */
8ae002
+  xpthread_detach (xpthread_create (NULL, garbage_sender_thread, NULL));
8ae002
+
8ae002
+  /* Test client.  Use an arbitrary timeout of one second, which is
8ae002
+     much longer than the garbage packet interval, but still
8ae002
+     reasonably short, so that the test completes quickly.  */
8ae002
+  int client_fd = RPC_ANYSOCK;
8ae002
+  CLIENT *clnt = clntudp_create (&server_address,
8ae002
+                                 1, 2, /* Arbitrary RPC endpoint numbers.  */
8ae002
+                                 (struct timeval) { 1, 0 },
8ae002
+                                 &client_fd);
8ae002
+  if (clnt == NULL)
8ae002
+    FAIL_EXIT1 ("clntudp_create: %m");
8ae002
+
8ae002
+  TEST_VERIFY (clnt_call (clnt, 3, /* Arbitrary RPC procedure number.  */
8ae002
+                          (xdrproc_t) xdr_void, NULL,
8ae002
+                          (xdrproc_t) xdr_void, NULL,
8ae002
+                          ((struct timeval) { 1, 0 })));
8ae002
+
8ae002
+  return 0;
8ae002
+}
8ae002
+
8ae002
+#include <support/test-driver.c>
8ae002
Index: b/sunrpc/tst-udp-nonblocking.c
8ae002
===================================================================
8ae002
--- /dev/null
8ae002
+++ b/sunrpc/tst-udp-nonblocking.c
8ae002
@@ -0,0 +1,333 @@
8ae002
+/* Test non-blocking use of the UDP client.
8ae002
+   Copyright (C) 2017 Free Software Foundation, Inc.
8ae002
+   This file is part of the GNU C Library.
8ae002
+
8ae002
+   The GNU C Library is free software; you can redistribute it and/or
8ae002
+   modify it under the terms of the GNU Lesser General Public
8ae002
+   License as published by the Free Software Foundation; either
8ae002
+   version 2.1 of the License, or (at your option) any later version.
8ae002
+
8ae002
+   The GNU C Library is distributed in the hope that it will be useful,
8ae002
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
8ae002
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
8ae002
+   Lesser General Public License for more details.
8ae002
+
8ae002
+   You should have received a copy of the GNU Lesser General Public
8ae002
+   License along with the GNU C Library; if not, see
8ae002
+   <http://www.gnu.org/licenses/>.  */
8ae002
+
8ae002
+#include <netinet/in.h>
8ae002
+#include <rpc/clnt.h>
8ae002
+#include <rpc/svc.h>
8ae002
+#include <stdbool.h>
8ae002
+#include <string.h>
8ae002
+#include <support/check.h>
8ae002
+#include <support/namespace.h>
8ae002
+#include <support/test-driver.h>
8ae002
+#include <support/xsocket.h>
8ae002
+#include <support/xunistd.h>
8ae002
+#include <sys/socket.h>
8ae002
+#include <time.h>
8ae002
+#include <unistd.h>
8ae002
+
8ae002
+/* Test data serialization and deserialization.   */
8ae002
+
8ae002
+struct test_query
8ae002
+{
8ae002
+  uint32_t a;
8ae002
+  uint32_t b;
8ae002
+  uint32_t timeout_ms;
8ae002
+};
8ae002
+
8ae002
+static bool_t
8ae002
+xdr_test_query (XDR *xdrs, void *data, ...)
8ae002
+{
8ae002
+  struct test_query *p = data;
8ae002
+  return xdr_uint32_t (xdrs, &p->a)
8ae002
+    && xdr_uint32_t (xdrs, &p->b)
8ae002
+    && xdr_uint32_t (xdrs, &p->timeout_ms);
8ae002
+}
8ae002
+
8ae002
+struct test_response
8ae002
+{
8ae002
+  uint32_t server_id;
8ae002
+  uint32_t seq;
8ae002
+  uint32_t sum;
8ae002
+};
8ae002
+
8ae002
+static bool_t
8ae002
+xdr_test_response (XDR *xdrs, void *data, ...)
8ae002
+{
8ae002
+  struct test_response *p = data;
8ae002
+  return xdr_uint32_t (xdrs, &p->server_id)
8ae002
+    && xdr_uint32_t (xdrs, &p->seq)
8ae002
+    && xdr_uint32_t (xdrs, &p->sum);
8ae002
+}
8ae002
+
8ae002
+/* Implementation of the test server.  */
8ae002
+
8ae002
+enum
8ae002
+  {
8ae002
+    /* Number of test servers to run. */
8ae002
+    SERVER_COUNT = 3,
8ae002
+
8ae002
+    /* RPC parameters, chosen at random.  */
8ae002
+    PROGNUM = 8242,
8ae002
+    VERSNUM = 19654,
8ae002
+
8ae002
+    /* Main RPC operation.  */
8ae002
+    PROC_ADD = 1,
8ae002
+
8ae002
+    /* Request process termination.  */
8ae002
+    PROC_EXIT,
8ae002
+
8ae002
+    /* Special exit status to mark successful processing.  */
8ae002
+    EXIT_MARKER = 55,
8ae002
+  };
8ae002
+
8ae002
+/* Set by the parent process to tell test servers apart.  */
8ae002
+static int server_id;
8ae002
+
8ae002
+/* Implementation of the test server.  */
8ae002
+static void
8ae002
+server_dispatch (struct svc_req *request, SVCXPRT *transport)
8ae002
+{
8ae002
+  /* Query sequence number.  */
8ae002
+  static uint32_t seq = 0;
8ae002
+  ++seq;
8ae002
+  static bool proc_add_seen;
8ae002
+
8ae002
+  if (test_verbose)
8ae002
+    printf ("info: server_dispatch server_id=%d seq=%u rq_proc=%lu\n",
8ae002
+            server_id, seq, request->rq_proc);
8ae002
+
8ae002
+  switch (request->rq_proc)
8ae002
+    {
8ae002
+    case PROC_ADD:
8ae002
+      {
8ae002
+        struct test_query query;
8ae002
+        memset (&query, 0xc0, sizeof (query));
8ae002
+        TEST_VERIFY_EXIT
8ae002
+          (svc_getargs (transport, xdr_test_query,
8ae002
+                        (void *) &query));
8ae002
+
8ae002
+        if (test_verbose)
8ae002
+          printf ("  a=%u b=%u timeout_ms=%u\n",
8ae002
+                  query.a, query.b, query.timeout_ms);
8ae002
+
8ae002
+        usleep (query.timeout_ms * 1000);
8ae002
+
8ae002
+        struct test_response response =
8ae002
+          {
8ae002
+            .server_id = server_id,
8ae002
+            .seq = seq,
8ae002
+            .sum = query.a + query.b,
8ae002
+          };
8ae002
+        TEST_VERIFY (svc_sendreply (transport, xdr_test_response,
8ae002
+                                    (void *) &response));
8ae002
+        if (test_verbose)
8ae002
+          printf ("  server id %d response seq=%u sent\n", server_id, seq);
8ae002
+        proc_add_seen = true;
8ae002
+      }
8ae002
+      break;
8ae002
+
8ae002
+    case PROC_EXIT:
8ae002
+      TEST_VERIFY (proc_add_seen);
8ae002
+      TEST_VERIFY (svc_sendreply (transport, (xdrproc_t) xdr_void, NULL));
8ae002
+      _exit (EXIT_MARKER);
8ae002
+      break;
8ae002
+
8ae002
+    default:
8ae002
+      FAIL_EXIT1 ("invalid rq_proc value: %lu", request->rq_proc);
8ae002
+      break;
8ae002
+    }
8ae002
+}
8ae002
+
8ae002
+/* Return the number seconds since an arbitrary point in time.  */
8ae002
+static double
8ae002
+get_ticks (void)
8ae002
+{
8ae002
+  {
8ae002
+    struct timespec ts;
8ae002
+    if (clock_gettime (CLOCK_MONOTONIC, &ts) == 0)
8ae002
+      return ts.tv_sec + ts.tv_nsec * 1e-9;
8ae002
+  }
8ae002
+  {
8ae002
+    struct timeval tv;
8ae002
+    TEST_VERIFY_EXIT (gettimeofday (&tv, NULL) == 0);
8ae002
+    return tv.tv_sec + tv.tv_usec * 1e-6;
8ae002
+  }
8ae002
+}
8ae002
+
8ae002
+static int
8ae002
+do_test (void)
8ae002
+{
8ae002
+  support_become_root ();
8ae002
+  support_enter_network_namespace ();
8ae002
+
8ae002
+  /* Information about the test servers.  */
8ae002
+  struct
8ae002
+  {
8ae002
+    SVCXPRT *transport;
8ae002
+    struct sockaddr_in address;
8ae002
+    pid_t pid;
8ae002
+    uint32_t xid;
8ae002
+  } servers[SERVER_COUNT];
8ae002
+
8ae002
+  /* Spawn the test servers.  */
8ae002
+  for (int i = 0; i < SERVER_COUNT; ++i)
8ae002
+    {
8ae002
+      servers[i].transport = svcudp_create (RPC_ANYSOCK);
8ae002
+      TEST_VERIFY_EXIT (servers[i].transport != NULL);
8ae002
+      servers[i].address = (struct sockaddr_in)
8ae002
+        {
8ae002
+          .sin_family = AF_INET,
8ae002
+          .sin_addr.s_addr = htonl (INADDR_LOOPBACK),
8ae002
+          .sin_port = htons (servers[i].transport->xp_port),
8ae002
+        };
8ae002
+      servers[i].xid = 0xabcd0101 + i;
8ae002
+      if (test_verbose)
8ae002
+        printf ("info: setting up server %d xid=%x on port %d\n",
8ae002
+                i, servers[i].xid, servers[i].transport->xp_port);
8ae002
+
8ae002
+      server_id = i;
8ae002
+      servers[i].pid = xfork ();
8ae002
+      if (servers[i].pid == 0)
8ae002
+        {
8ae002
+          TEST_VERIFY (svc_register (servers[i].transport,
8ae002
+                                     PROGNUM, VERSNUM, server_dispatch, 0));
8ae002
+          svc_run ();
8ae002
+          FAIL_EXIT1 ("supposed to be unreachable");
8ae002
+        }
8ae002
+      /* We need to close the socket so that we do not accidentally
8ae002
+         consume the request.  */
8ae002
+      TEST_VERIFY (close (servers[i].transport->xp_sock) == 0);
8ae002
+    }
8ae002
+
8ae002
+
8ae002
+  /* The following code mirrors what ypbind does.  */
8ae002
+
8ae002
+  /* Copied from clnt_udp.c (like ypbind).  */
8ae002
+  struct cu_data
8ae002
+  {
8ae002
+    int cu_sock;
8ae002
+    bool_t cu_closeit;
8ae002
+    struct sockaddr_in cu_raddr;
8ae002
+    int cu_rlen;
8ae002
+    struct timeval cu_wait;
8ae002
+    struct timeval cu_total;
8ae002
+    struct rpc_err cu_error;
8ae002
+    XDR cu_outxdrs;
8ae002
+    u_int cu_xdrpos;
8ae002
+    u_int cu_sendsz;
8ae002
+    char *cu_outbuf;
8ae002
+    u_int cu_recvsz;
8ae002
+    char cu_inbuf[1];
8ae002
+  };
8ae002
+
8ae002
+  int client_socket = xsocket (AF_INET, SOCK_DGRAM | SOCK_NONBLOCK, 0);
8ae002
+  CLIENT *clnt = clntudp_create (&servers[0].address, PROGNUM, VERSNUM,
8ae002
+                                 /* 5 seconds per-response timeout.  */
8ae002
+                                 ((struct timeval) { 5, 0 }),
8ae002
+                                 &client_socket);
8ae002
+  TEST_VERIFY (clnt != NULL);
8ae002
+  clnt->cl_auth = authunix_create_default ();
8ae002
+  {
8ae002
+    struct timeval zero = { 0, 0 };
8ae002
+    TEST_VERIFY (clnt_control (clnt, CLSET_TIMEOUT, (void *) &zero));
8ae002
+  }
8ae002
+
8ae002
+  /* Poke at internal data structures (like ypbind).  */
8ae002
+  struct cu_data *cu = (struct cu_data *) clnt->cl_private;
8ae002
+
8ae002
+  /* Send a ping to each server.  */
8ae002
+  double before_pings = get_ticks ();
8ae002
+  for (int i = 0; i < SERVER_COUNT; ++i)
8ae002
+    {
8ae002
+      if (test_verbose)
8ae002
+        printf ("info: sending server %d ping\n", i);
8ae002
+      /* Reset the xid because it is changed by each invocation of
8ae002
+         clnt_call.  Subtract one to compensate for the xid update
8ae002
+         during the call.  */
8ae002
+      *((u_int32_t *) (cu->cu_outbuf)) = servers[i].xid - 1;
8ae002
+      cu->cu_raddr = servers[i].address;
8ae002
+
8ae002
+      struct test_query query = { .a = 100, .b = i + 1 };
8ae002
+      if (i == 1)
8ae002
+        /* Shorter timeout to prefer this server.  These timeouts must
8ae002
+           be much shorter than the 5-second per-response timeout
8ae002
+           configured with clntudp_create.  */
8ae002
+        query.timeout_ms = 700;
8ae002
+      else
8ae002
+        query.timeout_ms = 1400;
8ae002
+      struct test_response response = { 0 };
8ae002
+      /* NB: Do not check the return value.  The server reply will
8ae002
+         prove that the call worked.  */
8ae002
+      double before_one_ping = get_ticks ();
8ae002
+      clnt_call (clnt, PROC_ADD,
8ae002
+                 xdr_test_query, (void *) &query,
8ae002
+                 xdr_test_response, (void *) &response,
8ae002
+                 ((struct timeval) { 0, 0 }));
8ae002
+      double after_one_ping = get_ticks ();
8ae002
+      if (test_verbose)
8ae002
+        printf ("info: non-blocking send took %f seconds\n",
8ae002
+                after_one_ping - before_one_ping);
8ae002
+      /* clnt_call should return immediately.  Accept some delay in
8ae002
+         case the process is descheduled.  */
8ae002
+      TEST_VERIFY (after_one_ping - before_one_ping < 0.3);
8ae002
+    }
8ae002
+
8ae002
+  /* Collect the non-blocking response.  */
8ae002
+  if (test_verbose)
8ae002
+    printf ("info: collecting response\n");
8ae002
+  struct test_response response = { 0 };
8ae002
+  TEST_VERIFY
8ae002
+    (clnt_call (clnt, PROC_ADD, NULL, NULL,
8ae002
+                xdr_test_response, (void *) &response,
8ae002
+                ((struct timeval) { 0, 0 })) == RPC_SUCCESS);
8ae002
+  double after_pings = get_ticks ();
8ae002
+  if (test_verbose)
8ae002
+    printf ("info: send/receive took %f seconds\n",
8ae002
+            after_pings - before_pings);
8ae002
+  /* Expected timeout is 0.7 seconds.  */
8ae002
+  TEST_VERIFY (0.7 <= after_pings - before_pings);
8ae002
+  TEST_VERIFY (after_pings - before_pings < 1.2);
8ae002
+
8ae002
+  uint32_t xid;
8ae002
+  memcpy (&xid, &cu->cu_inbuf, sizeof (xid));
8ae002
+  if (test_verbose)
8ae002
+    printf ("info: non-blocking response: xid=%x server_id=%u seq=%u sum=%u\n",
8ae002
+            xid, response.server_id, response.seq, response.sum);
8ae002
+  /* Check that the reply from the preferred server was used.  */
8ae002
+  TEST_VERIFY (servers[1].xid == xid);
8ae002
+  TEST_VERIFY (response.server_id == 1);
8ae002
+  TEST_VERIFY (response.seq == 1);
8ae002
+  TEST_VERIFY (response.sum == 102);
8ae002
+
8ae002
+  auth_destroy (clnt->cl_auth);
8ae002
+  clnt_destroy (clnt);
8ae002
+
8ae002
+  for (int i = 0; i < SERVER_COUNT; ++i)
8ae002
+    {
8ae002
+      if (test_verbose)
8ae002
+        printf ("info: requesting server %d termination\n", i);
8ae002
+      client_socket = RPC_ANYSOCK;
8ae002
+      clnt = clntudp_create (&servers[i].address, PROGNUM, VERSNUM,
8ae002
+                             ((struct timeval) { 5, 0 }),
8ae002
+                             &client_socket);
8ae002
+      TEST_VERIFY_EXIT (clnt != NULL);
8ae002
+      TEST_VERIFY (clnt_call (clnt, PROC_EXIT,
8ae002
+                              (xdrproc_t) xdr_void, NULL,
8ae002
+                              (xdrproc_t) xdr_void, NULL,
8ae002
+                              ((struct timeval) { 3, 0 })) == RPC_SUCCESS);
8ae002
+      clnt_destroy (clnt);
8ae002
+
8ae002
+      int status;
8ae002
+      xwaitpid (servers[i].pid, &status, 0);
8ae002
+      TEST_VERIFY (WIFEXITED (status) && WEXITSTATUS (status) == EXIT_MARKER);
8ae002
+    }
8ae002
+
8ae002
+  return 0;
8ae002
+}
8ae002
+
8ae002
+#include <support/test-driver.c>
8ae002
Index: b/sunrpc/tst-udp-timeout.c
8ae002
===================================================================
8ae002
--- /dev/null
8ae002
+++ b/sunrpc/tst-udp-timeout.c
8ae002
@@ -0,0 +1,402 @@
8ae002
+/* Test timeout handling in the UDP client.
8ae002
+   Copyright (C) 2017 Free Software Foundation, Inc.
8ae002
+   This file is part of the GNU C Library.
8ae002
+
8ae002
+   The GNU C Library is free software; you can redistribute it and/or
8ae002
+   modify it under the terms of the GNU Lesser General Public
8ae002
+   License as published by the Free Software Foundation; either
8ae002
+   version 2.1 of the License, or (at your option) any later version.
8ae002
+
8ae002
+   The GNU C Library is distributed in the hope that it will be useful,
8ae002
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
8ae002
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
8ae002
+   Lesser General Public License for more details.
8ae002
+
8ae002
+   You should have received a copy of the GNU Lesser General Public
8ae002
+   License along with the GNU C Library; if not, see
8ae002
+   <http://www.gnu.org/licenses/>.  */
8ae002
+
8ae002
+#include <netinet/in.h>
8ae002
+#include <rpc/clnt.h>
8ae002
+#include <rpc/svc.h>
8ae002
+#include <stdbool.h>
8ae002
+#include <string.h>
8ae002
+#include <support/check.h>
8ae002
+#include <support/namespace.h>
8ae002
+#include <support/test-driver.h>
8ae002
+#include <support/xsocket.h>
8ae002
+#include <support/xunistd.h>
8ae002
+#include <sys/socket.h>
8ae002
+#include <time.h>
8ae002
+#include <unistd.h>
8ae002
+
8ae002
+/* Test data serialization and deserialization.   */
8ae002
+
8ae002
+struct test_query
8ae002
+{
8ae002
+  uint32_t a;
8ae002
+  uint32_t b;
8ae002
+  uint32_t timeout_ms;
8ae002
+  uint32_t wait_for_seq;
8ae002
+  uint32_t garbage_packets;
8ae002
+};
8ae002
+
8ae002
+static bool_t
8ae002
+xdr_test_query (XDR *xdrs, void *data, ...)
8ae002
+{
8ae002
+  struct test_query *p = data;
8ae002
+  return xdr_uint32_t (xdrs, &p->a)
8ae002
+    && xdr_uint32_t (xdrs, &p->b)
8ae002
+    && xdr_uint32_t (xdrs, &p->timeout_ms)
8ae002
+    && xdr_uint32_t (xdrs, &p->wait_for_seq)
8ae002
+    && xdr_uint32_t (xdrs, &p->garbage_packets);
8ae002
+}
8ae002
+
8ae002
+struct test_response
8ae002
+{
8ae002
+  uint32_t seq;
8ae002
+  uint32_t sum;
8ae002
+};
8ae002
+
8ae002
+static bool_t
8ae002
+xdr_test_response (XDR *xdrs, void *data, ...)
8ae002
+{
8ae002
+  struct test_response *p = data;
8ae002
+  return xdr_uint32_t (xdrs, &p->seq)
8ae002
+    && xdr_uint32_t (xdrs, &p->sum);
8ae002
+}
8ae002
+
8ae002
+/* Implementation of the test server.  */
8ae002
+
8ae002
+enum
8ae002
+  {
8ae002
+    /* RPC parameters, chosen at random.  */
8ae002
+    PROGNUM = 15717,
8ae002
+    VERSNUM = 13689,
8ae002
+
8ae002
+    /* Main RPC operation.  */
8ae002
+    PROC_ADD = 1,
8ae002
+
8ae002
+    /* Reset the sequence number.  */
8ae002
+    PROC_RESET_SEQ,
8ae002
+
8ae002
+    /* Request process termination.  */
8ae002
+    PROC_EXIT,
8ae002
+
8ae002
+    /* Special exit status to mark successful processing.  */
8ae002
+    EXIT_MARKER = 55,
8ae002
+  };
8ae002
+
8ae002
+static void
8ae002
+server_dispatch (struct svc_req *request, SVCXPRT *transport)
8ae002
+{
8ae002
+  /* Query sequence number.  */
8ae002
+  static uint32_t seq = 0;
8ae002
+  ++seq;
8ae002
+
8ae002
+  if (test_verbose)
8ae002
+    printf ("info: server_dispatch seq=%u rq_proc=%lu\n",
8ae002
+            seq, request->rq_proc);
8ae002
+
8ae002
+  switch (request->rq_proc)
8ae002
+    {
8ae002
+    case PROC_ADD:
8ae002
+      {
8ae002
+        struct test_query query;
8ae002
+        memset (&query, 0xc0, sizeof (query));
8ae002
+        TEST_VERIFY_EXIT
8ae002
+          (svc_getargs (transport, xdr_test_query,
8ae002
+                        (void *) &query));
8ae002
+
8ae002
+        if (test_verbose)
8ae002
+          printf ("  a=%u b=%u timeout_ms=%u wait_for_seq=%u"
8ae002
+                  " garbage_packets=%u\n",
8ae002
+                  query.a, query.b, query.timeout_ms, query.wait_for_seq,
8ae002
+                  query.garbage_packets);
8ae002
+
8ae002
+        if (seq < query.wait_for_seq)
8ae002
+          {
8ae002
+            /* No response at this point.  */
8ae002
+            if (test_verbose)
8ae002
+              printf ("  skipped response\n");
8ae002
+            break;
8ae002
+          }
8ae002
+
8ae002
+        if (query.garbage_packets > 0)
8ae002
+          {
8ae002
+            int per_packet_timeout;
8ae002
+            if (query.timeout_ms > 0)
8ae002
+              per_packet_timeout
8ae002
+                = query.timeout_ms * 1000 / query.garbage_packets;
8ae002
+            else
8ae002
+              per_packet_timeout = 0;
8ae002
+
8ae002
+            char buf[20];
8ae002
+            memset (&buf, 0xc0, sizeof (buf));
8ae002
+            for (int i = 0; i < query.garbage_packets; ++i)
8ae002
+              {
8ae002
+                /* 13 is relatively prime to 20 = sizeof (buf) + 1, so
8ae002
+                   the len variable will cover the entire interval
8ae002
+                   [0, 20] if query.garbage_packets is sufficiently
8ae002
+                   large.  */
8ae002
+                size_t len = (i * 13 + 1) % (sizeof (buf) + 1);
8ae002
+                TEST_VERIFY (sendto (transport->xp_sock,
8ae002
+                                     buf, len, MSG_NOSIGNAL,
8ae002
+                                     (struct sockaddr *) &transport->xp_raddr,
8ae002
+                                     transport->xp_addrlen) == len);
8ae002
+                if (per_packet_timeout > 0)
8ae002
+                  usleep (per_packet_timeout);
8ae002
+              }
8ae002
+          }
8ae002
+        else if (query.timeout_ms > 0)
8ae002
+          usleep (query.timeout_ms * 1000);
8ae002
+
8ae002
+        struct test_response response =
8ae002
+          {
8ae002
+            .seq = seq,
8ae002
+            .sum = query.a + query.b,
8ae002
+          };
8ae002
+        TEST_VERIFY (svc_sendreply (transport, xdr_test_response,
8ae002
+                                    (void *) &response));
8ae002
+      }
8ae002
+      break;
8ae002
+
8ae002
+    case PROC_RESET_SEQ:
8ae002
+      seq = 0;
8ae002
+      TEST_VERIFY (svc_sendreply (transport, (xdrproc_t) xdr_void, NULL));
8ae002
+      break;
8ae002
+
8ae002
+    case PROC_EXIT:
8ae002
+      TEST_VERIFY (svc_sendreply (transport, (xdrproc_t) xdr_void, NULL));
8ae002
+      _exit (EXIT_MARKER);
8ae002
+      break;
8ae002
+
8ae002
+    default:
8ae002
+      FAIL_EXIT1 ("invalid rq_proc value: %lu", request->rq_proc);
8ae002
+      break;
8ae002
+    }
8ae002
+}
8ae002
+
8ae002
+/* Implementation of the test client.  */
8ae002
+
8ae002
+static struct test_response
8ae002
+test_call (CLIENT *clnt, int proc, struct test_query query,
8ae002
+           struct timeval timeout)
8ae002
+{
8ae002
+  if (test_verbose)
8ae002
+    printf ("info: test_call proc=%d timeout=%lu.%06lu\n",
8ae002
+            proc, (unsigned long) timeout.tv_sec,
8ae002
+            (unsigned long) timeout.tv_usec);
8ae002
+  struct test_response response;
8ae002
+  TEST_VERIFY_EXIT (clnt_call (clnt, proc,
8ae002
+                               xdr_test_query, (void *) &query,
8ae002
+                               xdr_test_response, (void *) &response,
8ae002
+                               timeout)
8ae002
+                    == RPC_SUCCESS);
8ae002
+  return response;
8ae002
+}
8ae002
+
8ae002
+static void
8ae002
+test_call_timeout (CLIENT *clnt, int proc, struct test_query query,
8ae002
+                   struct timeval timeout)
8ae002
+{
8ae002
+  struct test_response response;
8ae002
+  TEST_VERIFY (clnt_call (clnt, proc,
8ae002
+                          xdr_test_query, (void *) &query,
8ae002
+                          xdr_test_response, (void *) &response,
8ae002
+                          timeout)
8ae002
+               == RPC_TIMEDOUT);
8ae002
+}
8ae002
+
8ae002
+/* Complete one regular RPC call to drain the server socket
8ae002
+   buffer.  Resets the sequence number.  */
8ae002
+static void
8ae002
+test_call_flush (CLIENT *clnt)
8ae002
+{
8ae002
+  /* This needs a longer timeout to flush out all pending requests.
8ae002
+     The choice of 5 seconds is larger than the per-response timeouts
8ae002
+     requested via the timeout_ms field.  */
8ae002
+  if (test_verbose)
8ae002
+    printf ("info: flushing pending queries\n");
8ae002
+  TEST_VERIFY_EXIT (clnt_call (clnt, PROC_RESET_SEQ,
8ae002
+                               (xdrproc_t) xdr_void, NULL,
8ae002
+                               (xdrproc_t) xdr_void, NULL,
8ae002
+                               ((struct timeval) { 5, 0 }))
8ae002
+                    == RPC_SUCCESS);
8ae002
+}
8ae002
+
8ae002
+/* Return the number seconds since an arbitrary point in time.  */
8ae002
+static double
8ae002
+get_ticks (void)
8ae002
+{
8ae002
+  {
8ae002
+    struct timespec ts;
8ae002
+    if (clock_gettime (CLOCK_MONOTONIC, &ts) == 0)
8ae002
+      return ts.tv_sec + ts.tv_nsec * 1e-9;
8ae002
+  }
8ae002
+  {
8ae002
+    struct timeval tv;
8ae002
+    TEST_VERIFY_EXIT (gettimeofday (&tv, NULL) == 0);
8ae002
+    return tv.tv_sec + tv.tv_usec * 1e-6;
8ae002
+  }
8ae002
+}
8ae002
+
8ae002
+static void
8ae002
+test_udp_server (int port)
8ae002
+{
8ae002
+  struct sockaddr_in sin =
8ae002
+    {
8ae002
+      .sin_family = AF_INET,
8ae002
+      .sin_addr.s_addr = htonl (INADDR_LOOPBACK),
8ae002
+      .sin_port = htons (port)
8ae002
+    };
8ae002
+  int sock = RPC_ANYSOCK;
8ae002
+
8ae002
+  /* The client uses a 1.5 second timeout for retries.  The timeouts
8ae002
+     are arbitrary, but chosen so that there is a substantial gap
8ae002
+     between them, but the total time spent waiting is not too
8ae002
+     large.  */
8ae002
+  CLIENT *clnt = clntudp_create (&sin, PROGNUM, VERSNUM,
8ae002
+                                 (struct timeval) { 1, 500 * 1000 },
8ae002
+                                 &sock);
8ae002
+  TEST_VERIFY_EXIT (clnt != NULL);
8ae002
+
8ae002
+  /* Basic call/response test.  */
8ae002
+  struct test_response response = test_call
8ae002
+    (clnt, PROC_ADD,
8ae002
+     (struct test_query) { .a = 17, .b = 4 },
8ae002
+     (struct timeval) { 3, 0 });
8ae002
+  TEST_VERIFY (response.sum == 21);
8ae002
+  TEST_VERIFY (response.seq == 1);
8ae002
+
8ae002
+  /* Check that garbage packets do not interfere with timeout
8ae002
+     processing.  */
8ae002
+  double before = get_ticks ();
8ae002
+  response = test_call
8ae002
+    (clnt, PROC_ADD,
8ae002
+     (struct test_query) {
8ae002
+       .a = 19, .b = 4, .timeout_ms = 500, .garbage_packets = 21,
8ae002
+     },
8ae002
+     (struct timeval) { 3, 0 });
8ae002
+  TEST_VERIFY (response.sum == 23);
8ae002
+  TEST_VERIFY (response.seq == 2);
8ae002
+  double after = get_ticks ();
8ae002
+  if (test_verbose)
8ae002
+    printf ("info: 21 garbage packets took %f seconds\n", after - before);
8ae002
+  /* Expected timeout is 0.5 seconds.  Add some slack in case process
8ae002
+     scheduling delays processing the query or response, but do not
8ae002
+     accept a retry (which would happen at 1.5 seconds).  */
8ae002
+  TEST_VERIFY (0.5 <= after - before);
8ae002
+  TEST_VERIFY (after - before < 1.2);
8ae002
+  test_call_flush (clnt);
8ae002
+
8ae002
+  /* Check that missing a response introduces a 1.5 second timeout, as
8ae002
+     requested when calling clntudp_create.  */
8ae002
+  before = get_ticks ();
8ae002
+  response = test_call
8ae002
+    (clnt, PROC_ADD,
8ae002
+     (struct test_query) { .a = 170, .b = 40, .wait_for_seq = 2 },
8ae002
+     (struct timeval) { 3, 0 });
8ae002
+  TEST_VERIFY (response.sum == 210);
8ae002
+  TEST_VERIFY (response.seq == 2);
8ae002
+  after = get_ticks ();
8ae002
+  if (test_verbose)
8ae002
+    printf ("info: skipping one response took %f seconds\n",
8ae002
+            after - before);
8ae002
+  /* Expected timeout is 1.5 seconds.  Do not accept a second retry
8ae002
+     (which would happen at 3 seconds).  */
8ae002
+  TEST_VERIFY (1.5 <= after - before);
8ae002
+  TEST_VERIFY (after - before < 2.9);
8ae002
+  test_call_flush (clnt);
8ae002
+
8ae002
+  /* Check that the overall timeout wins against the per-query
8ae002
+     timeout.  */
8ae002
+  before = get_ticks ();
8ae002
+  test_call_timeout
8ae002
+    (clnt, PROC_ADD,
8ae002
+     (struct test_query) { .a = 170, .b = 41, .wait_for_seq = 2 },
8ae002
+     (struct timeval) { 0, 750 * 1000 });
8ae002
+  after = get_ticks ();
8ae002
+  if (test_verbose)
8ae002
+    printf ("info: 0.75 second timeout took %f seconds\n",
8ae002
+            after - before);
8ae002
+  TEST_VERIFY (0.75 <= after - before);
8ae002
+  TEST_VERIFY (after - before < 1.4);
8ae002
+  test_call_flush (clnt);
8ae002
+
8ae002
+  for (int with_garbage = 0; with_garbage < 2; ++with_garbage)
8ae002
+    {
8ae002
+      /* Check that no response at all causes the client to bail out.  */
8ae002
+      before = get_ticks ();
8ae002
+      test_call_timeout
8ae002
+        (clnt, PROC_ADD,
8ae002
+         (struct test_query) {
8ae002
+           .a = 170, .b = 40, .timeout_ms = 1200,
8ae002
+           .garbage_packets = with_garbage * 21
8ae002
+         },
8ae002
+         (struct timeval) { 0, 750 * 1000 });
8ae002
+      after = get_ticks ();
8ae002
+      if (test_verbose)
8ae002
+        printf ("info: test_udp_server: 0.75 second timeout took %f seconds"
8ae002
+                " (garbage %d)\n",
8ae002
+                after - before, with_garbage);
8ae002
+      TEST_VERIFY (0.75 <= after - before);
8ae002
+      TEST_VERIFY (after - before < 1.4);
8ae002
+      test_call_flush (clnt);
8ae002
+
8ae002
+      /* As above, but check the total timeout.  */
8ae002
+      before = get_ticks ();
8ae002
+      test_call_timeout
8ae002
+        (clnt, PROC_ADD,
8ae002
+         (struct test_query) {
8ae002
+           .a = 170, .b = 40, .timeout_ms = 3000,
8ae002
+           .garbage_packets = with_garbage * 30
8ae002
+         },
8ae002
+         (struct timeval) { 2, 300 * 1000 });
8ae002
+      after = get_ticks ();
8ae002
+      if (test_verbose)
8ae002
+        printf ("info: test_udp_server: 2.3 second timeout took %f seconds"
8ae002
+                " (garbage %d)\n",
8ae002
+                after - before, with_garbage);
8ae002
+      TEST_VERIFY (2.3 <= after - before);
8ae002
+      TEST_VERIFY (after - before < 3.0);
8ae002
+      test_call_flush (clnt);
8ae002
+    }
8ae002
+
8ae002
+  TEST_VERIFY_EXIT (clnt_call (clnt, PROC_EXIT,
8ae002
+                               (xdrproc_t) xdr_void, NULL,
8ae002
+                               (xdrproc_t) xdr_void, NULL,
8ae002
+                               ((struct timeval) { 5, 0 }))
8ae002
+                    == RPC_SUCCESS);
8ae002
+  clnt_destroy (clnt);
8ae002
+}
8ae002
+
8ae002
+static int
8ae002
+do_test (void)
8ae002
+{
8ae002
+  support_become_root ();
8ae002
+  support_enter_network_namespace ();
8ae002
+
8ae002
+  SVCXPRT *transport = svcudp_create (RPC_ANYSOCK);
8ae002
+  TEST_VERIFY_EXIT (transport != NULL);
8ae002
+  TEST_VERIFY (svc_register (transport, PROGNUM, VERSNUM, server_dispatch, 0));
8ae002
+
8ae002
+  pid_t pid = xfork ();
8ae002
+  if (pid == 0)
8ae002
+    {
8ae002
+      svc_run ();
8ae002
+      FAIL_EXIT1 ("supposed to be unreachable");
8ae002
+    }
8ae002
+  test_udp_server (transport->xp_port);
8ae002
+
8ae002
+  int status;
8ae002
+  xwaitpid (pid, &status, 0);
8ae002
+  TEST_VERIFY (WIFEXITED (status) && WEXITSTATUS (status) == EXIT_MARKER);
8ae002
+
8ae002
+  SVC_DESTROY (transport);
8ae002
+  return 0;
8ae002
+}
8ae002
+
8ae002
+/* The minimum run time is around 17 seconds.  */
8ae002
+#define TIMEOUT 25
8ae002
+#include <support/test-driver.c>
8ae002
Index: b/inet/net-internal.h
8ae002
===================================================================
8ae002
--- /dev/null
8ae002
+++ b/inet/net-internal.h
8ae002
@@ -0,0 +1,112 @@
8ae002
+/* Network-related functions for internal library use.
8ae002
+   Copyright (C) 2016-2017 Free Software Foundation, Inc.
8ae002
+   This file is part of the GNU C Library.
8ae002
+
8ae002
+   The GNU C Library is free software; you can redistribute it and/or
8ae002
+   modify it under the terms of the GNU Lesser General Public
8ae002
+   License as published by the Free Software Foundation; either
8ae002
+   version 2.1 of the License, or (at your option) any later version.
8ae002
+
8ae002
+   The GNU C Library is distributed in the hope that it will be useful,
8ae002
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
8ae002
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
8ae002
+   Lesser General Public License for more details.
8ae002
+
8ae002
+   You should have received a copy of the GNU Lesser General Public
8ae002
+   License along with the GNU C Library; if not, see
8ae002
+   <http://www.gnu.org/licenses/>.  */
8ae002
+
8ae002
+#ifndef _NET_INTERNAL_H
8ae002
+#define _NET_INTERNAL_H 1
8ae002
+
8ae002
+#include <stdbool.h>
8ae002
+#include <stdint.h>
8ae002
+#include <sys/time.h>
8ae002
+
8ae002
+/* Deadline handling for enforcing timeouts.
8ae002
+
8ae002
+   Code should call __deadline_current_time to obtain the current time
8ae002
+   and cache it locally.  The cache needs updating after every
8ae002
+   long-running or potentially blocking operation.  Deadlines relative
8ae002
+   to the current time can be computed using __deadline_from_timeval.
8ae002
+   The deadlines may have to be recomputed in response to certain
8ae002
+   events (such as an incoming packet), but they are absolute (not
8ae002
+   relative to the current time).  A timeout suitable for use with the
8ae002
+   poll function can be computed from such a deadline using
8ae002
+   __deadline_to_ms.
8ae002
+
8ae002
+   The fields in the structs defined belowed should only be used
8ae002
+   within the implementation.  */
8ae002
+
8ae002
+/* Cache of the current time.  Used to compute deadlines from relative
8ae002
+   timeouts and vice versa.  */
8ae002
+struct deadline_current_time
8ae002
+{
8ae002
+  struct timespec current;
8ae002
+};
8ae002
+
8ae002
+/* Return the current time.  Terminates the process if the current
8ae002
+   time is not available.  */
8ae002
+struct deadline_current_time __deadline_current_time (void)
8ae002
+  internal_function attribute_hidden;
8ae002
+
8ae002
+/* Computed absolute deadline.  */
8ae002
+struct deadline
8ae002
+{
8ae002
+  struct timespec absolute;
8ae002
+};
8ae002
+
8ae002
+
8ae002
+/* For internal use only.  */
8ae002
+static inline bool
8ae002
+__deadline_is_infinite (struct deadline deadline)
8ae002
+{
8ae002
+  return deadline.absolute.tv_nsec < 0;
8ae002
+}
8ae002
+
8ae002
+/* Return true if the current time is at the deadline or past it.  */
8ae002
+static inline bool
8ae002
+__deadline_elapsed (struct deadline_current_time current,
8ae002
+                    struct deadline deadline)
8ae002
+{
8ae002
+  return !__deadline_is_infinite (deadline)
8ae002
+    && (current.current.tv_sec > deadline.absolute.tv_sec
8ae002
+        || (current.current.tv_sec == deadline.absolute.tv_sec
8ae002
+            && current.current.tv_nsec >= deadline.absolute.tv_nsec));
8ae002
+}
8ae002
+
8ae002
+/* Return the deadline which occurs first.  */
8ae002
+static inline struct deadline
8ae002
+__deadline_first (struct deadline left, struct deadline right)
8ae002
+{
8ae002
+  if (__deadline_is_infinite (right)
8ae002
+      || left.absolute.tv_sec < right.absolute.tv_sec
8ae002
+      || (left.absolute.tv_sec == right.absolute.tv_sec
8ae002
+          && left.absolute.tv_nsec < right.absolute.tv_nsec))
8ae002
+    return left;
8ae002
+  else
8ae002
+    return right;
8ae002
+}
8ae002
+
8ae002
+/* Add TV to the current time and return it.  Returns a special
8ae002
+   infinite absolute deadline on overflow.  */
8ae002
+struct deadline __deadline_from_timeval (struct deadline_current_time,
8ae002
+                                         struct timeval tv)
8ae002
+  internal_function attribute_hidden;
8ae002
+
8ae002
+/* Compute the number of milliseconds until the specified deadline,
8ae002
+   from the current time in the argument.  The result is mainly for
8ae002
+   use with poll.  If the deadline has already passed, return 0.  If
8ae002
+   the result would overflow an int, return INT_MAX.  */
8ae002
+int __deadline_to_ms (struct deadline_current_time, struct deadline)
8ae002
+  internal_function attribute_hidden;
8ae002
+
8ae002
+/* Return true if TV.tv_sec is non-negative and TV.tv_usec is in the
8ae002
+   interval [0, 999999].  */
8ae002
+static inline bool
8ae002
+__is_timeval_valid_timeout (struct timeval tv)
8ae002
+{
8ae002
+  return tv.tv_sec >= 0 && tv.tv_usec >= 0 && tv.tv_usec < 1000 * 1000;
8ae002
+}
8ae002
+
8ae002
+#endif /* _NET_INTERNAL_H */
8ae002
Index: b/inet/Makefile
8ae002
===================================================================
8ae002
--- a/inet/Makefile
8ae002
+++ b/inet/Makefile
8ae002
@@ -44,13 +44,18 @@ routines := htonl htons		\
8ae002
 	    getaliasent_r getaliasent getaliasname getaliasname_r \
8ae002
 	    in6_addr getnameinfo if_index ifaddrs inet6_option \
8ae002
 	    getipv4sourcefilter setipv4sourcefilter \
8ae002
-	    getsourcefilter setsourcefilter inet6_opt inet6_rth
8ae002
+	    getsourcefilter setsourcefilter inet6_opt inet6_rth \
8ae002
+	    deadline
8ae002
 
8ae002
 aux := check_pf check_native ifreq
8ae002
 
8ae002
 tests := htontest test_ifindex tst-ntoa tst-ether_aton tst-network \
8ae002
 	 tst-gethnm test-ifaddrs bug-if1 test-inet6_opt tst-ether_line \
8ae002
-	 tst-getni1 tst-getni2 tst-inet6_rth tst-checks
8ae002
+	 tst-getni1 tst-getni2 tst-inet6_rth tst-checks tst-deadline
8ae002
+
8ae002
+# tst-deadline must be linked statically so that we can access
8ae002
+# internal functions.
8ae002
+tests-static += tst-deadline
8ae002
 
8ae002
 include ../Rules
8ae002