[Libguestfs] [PATCH] Use srandom (time (NULL) + getpid ()) throughout.

Richard W.M. Jones rjones at redhat.com
Thu Jan 26 15:51:20 UTC 2017


It avoids two programs which start at exactly the same time from
having the same random number seed, which helps in some tests.
---
 edit/edit.c                                   | 3 ++-
 fish/fish.c                                   | 2 +-
 tests/mount-local/test-parallel-mount-local.c | 2 +-
 3 files changed, 4 insertions(+), 3 deletions(-)

diff --git a/edit/edit.c b/edit/edit.c
index 2f986a3..1e2641c 100644
--- a/edit/edit.c
+++ b/edit/edit.c
@@ -29,6 +29,7 @@
 #include <error.h>
 #include <assert.h>
 #include <libintl.h>
+#include <sys/types.h>
 #include <sys/time.h>
 #include <sys/stat.h>
 #include <utime.h>
@@ -103,7 +104,7 @@ main (int argc, char *argv[])
   textdomain (PACKAGE);
 
   /* We use random(3) below. */
-  srandom (time (NULL));
+  srandom (time (NULL) + getpid ());
 
   enum { HELP_OPTION = CHAR_MAX + 1 };
 
diff --git a/fish/fish.c b/fish/fish.c
index b7d63cf..52a9093 100644
--- a/fish/fish.c
+++ b/fish/fish.c
@@ -181,7 +181,7 @@ main (int argc, char *argv[])
   textdomain (PACKAGE);
 
   /* We use random(3) in edit.c. */
-  srandom (time (NULL));
+  srandom (time (NULL) + getpid ());
 
   parse_config ();
 
diff --git a/tests/mount-local/test-parallel-mount-local.c b/tests/mount-local/test-parallel-mount-local.c
index 3e5d95c..542f1ed 100644
--- a/tests/mount-local/test-parallel-mount-local.c
+++ b/tests/mount-local/test-parallel-mount-local.c
@@ -85,7 +85,7 @@ main (int argc, char *argv[])
   int r, errors = 0;
   void *status;
 
-  srandom (time (NULL));
+  srandom (time (NULL) + getpid ());
 
   /* If the --test flag is given, then this is the test subprocess. */
   if (argc == 3 && STREQ (argv[1], "--test")) {
-- 
2.9.3




More information about the Libguestfs mailing list