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