about summary refs log tree commit diff
path: root/sysdeps/posix/tempname.c
diff options
context:
space:
mode:
authorAdhemerval Zanella <adhemerval.zanella@linaro.org>2019-01-16 18:10:56 +0000
committerAdhemerval Zanella <adhemerval.zanella@linaro.org>2019-03-22 17:30:39 -0300
commit359653aaacad463d916323f03c0ac3c47405aafa (patch)
treea7d4de0a3e520ad4d62a2ed11bb63b55607fb160 /sysdeps/posix/tempname.c
parent6e8ba7fd574f530afb9681f21604475d5756d773 (diff)
downloadglibc-359653aaacad463d916323f03c0ac3c47405aafa.tar.gz
glibc-359653aaacad463d916323f03c0ac3c47405aafa.tar.xz
glibc-359653aaacad463d916323f03c0ac3c47405aafa.zip
Do not use HP_TIMING_NOW for random bits
This patch removes the HP_TIMING_BITS usage for fast random bits and replace
with clock_gettime (CLOCK_MONOTONIC).  It has unspecified starting time and
nano-second accuracy, so its randomness is significantly better than
gettimeofday.

Althoug it should incur in more overhead (specially for architecture that
support hp-timing), the symbol is also common implemented as a vDSO.

Checked on aarch64-linux-gnu, x86_64-linux-gnu, and i686-linux-gnu. I also
checked on a i686-gnu build.

	* include/random-bits.h: New file.
	* resolv/res_mkquery.c [HP_TIMING_AVAIL] (RANDOM_BITS,
	(__res_context_mkquery): Remove usage hp-timing usage and replace with
	random_bits.
	* resolv/res_send.c [HP_TIMING_AVAIL] (nameserver_offset): Likewise.
	* sysdeps/posix/tempname.c [HP_TIMING_AVAIL] (__gen_tempname):
	Likewise.
Diffstat (limited to 'sysdeps/posix/tempname.c')
-rw-r--r--sysdeps/posix/tempname.c40
1 files changed, 12 insertions, 28 deletions
diff --git a/sysdeps/posix/tempname.c b/sysdeps/posix/tempname.c
index 2ed39d1a42..de346949b2 100644
--- a/sysdeps/posix/tempname.c
+++ b/sysdeps/posix/tempname.c
@@ -71,22 +71,15 @@
 #endif
 
 #ifdef _LIBC
-# include <hp-timing.h>
-# if HP_TIMING_AVAIL
-#  define RANDOM_BITS(Var) \
-  if (__glibc_unlikely (value == UINT64_C (0)))				      \
-    {									      \
-      /* If this is the first time this function is used initialize	      \
-	 the variable we accumulate the value in to some somewhat	      \
-	 random value.  If we'd not do this programs at startup time	      \
-	 might have a reduced set of possible names, at least on slow	      \
-	 machines.  */							      \
-      struct timeval tv;						      \
-      __gettimeofday (&tv, NULL);					      \
-      value = ((uint64_t) tv.tv_usec << 16) ^ tv.tv_sec;		      \
-    }									      \
-  HP_TIMING_NOW (Var)
-# endif
+# include <random-bits.h>
+# define RANDOM_BITS(Var) ((Var) = random_bits ())
+# else
+# define RANDOM_BITS(Var) \
+    {                                                                         \
+      struct timeval tv;                                                      \
+      __gettimeofday (&tv, NULL);                                             \
+      (Var) = ((uint64_t) tv.tv_usec << 16) ^ tv.tv_sec;                      \
+    }
 #endif
 
 /* Use the widest available unsigned type if uint64_t is not
@@ -193,8 +186,7 @@ __gen_tempname (char *tmpl, int suffixlen, int flags, int kind)
 {
   int len;
   char *XXXXXX;
-  static uint64_t value;
-  uint64_t random_time_bits;
+  uint64_t value;
   unsigned int count;
   int fd = -1;
   int save_errno = errno;
@@ -227,16 +219,8 @@ __gen_tempname (char *tmpl, int suffixlen, int flags, int kind)
   XXXXXX = &tmpl[len - 6 - suffixlen];
 
   /* Get some more or less random data.  */
-#ifdef RANDOM_BITS
-  RANDOM_BITS (random_time_bits);
-#else
-  {
-    struct timeval tv;
-    __gettimeofday (&tv, NULL);
-    random_time_bits = ((uint64_t) tv.tv_usec << 16) ^ tv.tv_sec;
-  }
-#endif
-  value += random_time_bits ^ __getpid ();
+  RANDOM_BITS (value);
+  value ^= (uint64_t)__getpid () << 32;
 
   for (count = 0; count < attempts; value += 7777, ++count)
     {