]> git.ipfire.org Git - thirdparty/mtr.git/commitdiff
misc: improve random initialization 151/head
authorSami Kerola <kerolasa@iki.fi>
Thu, 29 Sep 2016 18:30:02 +0000 (19:30 +0100)
committerSami Kerola <kerolasa@iki.fi>
Thu, 29 Sep 2016 18:43:23 +0000 (19:43 +0100)
Typical linux pid range is 2^15, which is not a lot as a entropy sourcee.
This change uses full 32 bits when random is initialized.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
mtr.c

diff --git a/mtr.c b/mtr.c
index 6b214b43cdc96b659812879423095917dac5197c..377283c93bc16e1def572d5fc31d049a31fc542d 100644 (file)
--- a/mtr.c
+++ b/mtr.c
@@ -46,6 +46,7 @@
 #include <assert.h>
 #include <fcntl.h>
 #include <sys/stat.h>
+#include <sys/time.h>
 
 #include "mtr.h"
 #include "mtr-curses.h"
@@ -611,6 +612,13 @@ static void parse_mtr_options (struct mtr_ctl *ctl, char *string)
   optind = 0;
 }
 
+static void init_rand(void)
+{
+  struct timeval tv;
+
+  gettimeofday(&tv, 0);
+  srand((getpid() << 16) ^ getuid() ^ tv.tv_sec ^ tv.tv_usec);
+}
 
 extern int main(int argc, char **argv)
 {
@@ -662,7 +670,7 @@ extern int main(int argc, char **argv)
   atexit(close_stdout);
 
   /* reset the random seed */
-  srand (getpid());
+  init_rand();
 
   display_detect(&ctl, &argc, &argv);
   ctl.display_mode = DisplayModeDefault;