From 81108f8bd1e570ea0359c9a6767f4c3b53e7a4ed Mon Sep 17 00:00:00 2001 From: Sami Kerola Date: Thu, 29 Sep 2016 19:30:02 +0100 Subject: [PATCH] misc: improve random initialization 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 --- mtr.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/mtr.c b/mtr.c index 6b214b4..377283c 100644 --- a/mtr.c +++ b/mtr.c @@ -46,6 +46,7 @@ #include #include #include +#include #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; -- 2.47.2