17aa40
From 58cdc09af08e065c85b2f8834ee9848c010f5afe Mon Sep 17 00:00:00 2001
698723
From: Yu Watanabe <watanabe.yu+github@gmail.com>
698723
Date: Mon, 16 Dec 2019 19:47:48 +0900
698723
Subject: [PATCH] random-util: call initialize_srand() after fork()
698723
698723
(cherry picked from commit a0f11d1d11a546f791855ec9c47c2ff830e6a5aa)
698723
17aa40
Related: #2005008
698723
---
698723
 src/basic/random-util.c | 14 +++++++++++++-
698723
 1 file changed, 13 insertions(+), 1 deletion(-)
698723
698723
diff --git a/src/basic/random-util.c b/src/basic/random-util.c
698723
index 91481559db..801f6ad131 100644
698723
--- a/src/basic/random-util.c
698723
+++ b/src/basic/random-util.c
698723
@@ -4,6 +4,7 @@
698723
 #include <errno.h>
698723
 #include <fcntl.h>
698723
 #include <linux/random.h>
698723
+#include <pthread.h>
698723
 #include <stdbool.h>
698723
 #include <stdint.h>
698723
 #include <stdlib.h>
698723
@@ -26,6 +27,8 @@
698723
 #include "random-util.h"
698723
 #include "time-util.h"
698723
 
698723
+static bool srand_called = false;
698723
+
698723
 int acquire_random_bytes(void *p, size_t n, bool high_quality_required) {
698723
         static int have_syscall = -1;
698723
 
698723
@@ -81,8 +84,12 @@ int acquire_random_bytes(void *p, size_t n, bool high_quality_required) {
698723
         return loop_read_exact(fd, (uint8_t*) p + already_done, n - already_done, true);
698723
 }
698723
 
698723
+static void clear_srand_initialization(void) {
698723
+        srand_called = false;
698723
+}
698723
+
698723
 void initialize_srand(void) {
698723
-        static bool srand_called = false;
698723
+        static bool pthread_atfork_registered = false;
698723
         unsigned x;
698723
 #if HAVE_SYS_AUXV_H
698723
         void *auxv;
698723
@@ -109,6 +116,11 @@ void initialize_srand(void) {
698723
 
698723
         srand(x);
698723
         srand_called = true;
698723
+
698723
+        if (!pthread_atfork_registered) {
698723
+                (void) pthread_atfork(NULL, NULL, clear_srand_initialization);
698723
+                pthread_atfork_registered = true;
698723
+        }
698723
 }
698723
 
698723
 /* INT_MAX gives us only 31 bits, so use 24 out of that. */