Revert "BUG/MEDIUM: random: implement per-thread and per-process random sequences"

This reverts commit 1c306aa84d785b9c2240bf7767dcc1f2596cfcfd.

It breaks the build on all non-glibc platforms. I got confused by the
man page (which possibly is the most confusing man page I've ever read
about a standard libc function) and mistakenly understood that random_r
was portable, especially since it appears in latest freebsd source as
well but not in released versions, and with a slightly different API :-/

We need to find a different solution with a fallback. Among the
possibilities, we may reintroduce this one with a fallback relying on
locking around the standard functions, keeping fingers crossed for no
other library function to call them in parallel, or we may also provide
our own PRNG, which is not necessarily more difficult than working
around the totally broken up design of the portable API.
diff --git a/src/51d.c b/src/51d.c
index 42d1929..b00f018 100644
--- a/src/51d.c
+++ b/src/51d.c
@@ -700,7 +700,7 @@
 	free(_51d_property_list);
 
 #ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
-	_51d_lru_seed = ha_random();
+	_51d_lru_seed = random();
 	if (global_51degrees.cache_size) {
 		_51d_lru_tree = lru64_new(global_51degrees.cache_size);
 	}
diff --git a/src/backend.c b/src/backend.c
index 87e3b9a..251a7a5 100644
--- a/src/backend.c
+++ b/src/backend.c
@@ -541,7 +541,7 @@
 	do {
 		prev = curr;
 		/* ensure all 32 bits are covered as long as RAND_MAX >= 65535 */
-		hash = ((uint64_t)ha_random() * ((uint64_t)RAND_MAX + 1)) ^ ha_random();
+		hash = ((uint64_t)random() * ((uint64_t)RAND_MAX + 1)) ^ random();
 		curr = chash_get_server_hash(px, hash, avoid);
 		if (!curr)
 			break;
diff --git a/src/flt_spoe.c b/src/flt_spoe.c
index bcdec08..d54fcd4 100644
--- a/src/flt_spoe.c
+++ b/src/flt_spoe.c
@@ -269,7 +269,7 @@
 
 	while (byte < 4) {
 		while (bits < 32) {
-			last |= (uint64_t)ha_random() << bits;
+			last |= (uint64_t)random() << bits;
 			bits += rand_max_bits;
 		}
 		rnd[byte++] = last;
@@ -3109,6 +3109,10 @@
 	struct spoe_config *conf = fconf->conf;
 	struct spoe_agent *agent = conf->agent;
 
+	/* Use a != seed per process */
+	if (relative_pid > 1 && tid == 0)
+		srandom(now_ms * pid);
+
 	agent->rt[tid].engine_id = generate_pseudo_uuid();
 	if (agent->rt[tid].engine_id == NULL)
 		return -1;
diff --git a/src/flt_trace.c b/src/flt_trace.c
index b06ba15..5a26fab 100644
--- a/src/flt_trace.c
+++ b/src/flt_trace.c
@@ -468,7 +468,7 @@
 		unsigned int data = trace_get_htx_datalen(htxbuf(&msg->chn->buf), offset, len);
 
 		if (data) {
-			ret = ha_random() % (ret+1);
+			ret = random() % (ret+1);
 			if (!ret || ret >= data)
 				ret = len;
 		}
@@ -536,7 +536,7 @@
 			unsigned int data = trace_get_htx_datalen(htxbuf(&chn->buf), offset, len);
 
 			if (data) {
-				ret = ha_random() % (ret+1);
+				ret = random() % (ret+1);
 				if (!ret || ret >= data)
 					ret = len;
 			}
@@ -554,7 +554,7 @@
 	else {
 
 		if (ret && conf->rand_forwarding)
-			ret = ha_random() % (ret+1);
+			ret = random() % (ret+1);
 
 		FLT_STRM_TRACE(conf, s, "%-25s: channel=%-10s - mode=%-5s (%s) - "
 			       "offset=%u - len=%u - forward=%d",
diff --git a/src/haproxy.c b/src/haproxy.c
index 8b13d96..ef9010f 100644
--- a/src/haproxy.c
+++ b/src/haproxy.c
@@ -238,8 +238,6 @@
 
 /* per-boot randomness */
 unsigned char boot_seed[20];        /* per-boot random seed (160 bits initially) */
-THREAD_LOCAL char ha_rand_state[32];          /* opaque 256 bits of random state */
-THREAD_LOCAL struct random_data ha_rand_data; /* opaque internal random_r() date */
 
 struct mworker_proc *proc_self = NULL;
 
@@ -1365,59 +1363,6 @@
 }
 
 
-/* Initializes the per-thread, per-process random seed for use with random_r().
- *
- * We cannot pass a global state from one thread to another one because we
- * must still call initstate_r() on it to reset the per-thread pointer, and
- * this will reinitialize our state. What we do instead is that we use the
- * *same* seed for all threads so that they start with the exact same internal
- * state, and will loop over random() a different (and large) number of times
- * to make sure their internal state is totally different. This results in 4
- * billion possible *boot* sequences, and each thread may start with a much
- * greater number of sequences as well (we typically add up to 20 bits, giving
- * 4 trillon possible initial sequences).
- */
-static void ha_random_init_per_thread()
-{
-	unsigned int seed;
-	unsigned int loops;
-	uint64_t u64;
-
-	/* recreate a distinct initial state for each process/thread */
-	seed = read_u32(boot_seed);
-
-	/* start with a strictly different seed per thread/process */
-	seed += (relative_pid * MAX_THREADS)+ tid;
-
-	memset(&ha_rand_data, 0, sizeof(ha_rand_data));
-	initstate_r(seed, ha_rand_state, sizeof(ha_rand_state), &ha_rand_data);
-
-	/* make sure all pids and tids have a different count, we'll
-	 * loop up to ~1 million times on each thread, with a fairly
-	 * different number for each. This should only take a few ms
-	 * per thread and will provide ~20 extra bits of randomness
-	 * to each thread/process, resulting in ~52 bits per thread per
-	 * boot.
-	 */
-	loops = read_u32(boot_seed);
-
-	u64 = read_u64(boot_seed + 4);
-	u64 = (u64 << relative_pid) | (u64 >> (63-relative_pid));
-	loops ^= u64 ^ (u64 >> 32);
-
-	u64 = read_u64(boot_seed + 12);
-	u64 = (u64 << tid) | (u64 >> (63-tid));
-	loops ^= u64 ^ (u64 >> 32);
-	loops %= 1048573;
-
-	/* burn some randoms to mix the internal state */
-	while (loops--) {
-		int32_t drop;
-
-		(void)random_r(&ha_rand_data, &drop);
-	}
-}
-
 /* Performs basic random seed initialization. The main issue with this is that
  * srandom_r() only takes 32 bits and purposely provides a reproducible sequence,
  * which means that there will only be 4 billion possible random sequences once
@@ -1429,10 +1374,6 @@
  * We initialize the current process with the first 32 bits before starting the
  * polling loop, where all this will be changed to have process specific and
  * thread specific sequences.
- *
- * Before starting threads, it's still possible to call random() as srandom()
- * is initialized from this, but after threads and/or processes are started,
- * only ha_random() is expected to be used to guarantee distinct sequences.
  */
 static void ha_random_boot(char *const *argv)
 {
@@ -1503,7 +1444,6 @@
 	blk_SHA1_Final(boot_seed, &ctx);
 
 	srandom(read_u32(boot_seed));
-	ha_random_init_per_thread();
 }
 
 /* considers splicing proxies' maxconn, computes the ideal global.maxpipes
@@ -2840,9 +2780,6 @@
 	ti->clock_id = CLOCK_THREAD_CPUTIME_ID;
 #endif
 #endif
-	/* assign per-process, per-thread randomness */
-	ha_random_init_per_thread();
-
 	/* Now, initialize one thread init at a time. This is better since
 	 * some init code is a bit tricky and may release global resources
 	 * after reallocating them locally. This will also ensure there is
diff --git a/src/memory.c b/src/memory.c
index 0ff3ea8..d1aec59 100644
--- a/src/memory.c
+++ b/src/memory.c
@@ -628,7 +628,7 @@
 	int n;
 
 	if (mem_fail_rate > 0 && !(global.mode & MODE_STARTING)) {
-		int randnb = ha_random() % 100;
+		int randnb = random() % 100;
 
 		if (mem_fail_rate > randnb)
 			ret = 1;
diff --git a/src/pattern.c b/src/pattern.c
index 3ea1f33..8dfe3cf 100644
--- a/src/pattern.c
+++ b/src/pattern.c
@@ -2667,7 +2667,7 @@
 	struct pat_ref *ref, **arr;
 	struct list pr = LIST_HEAD_INIT(pr);
 
-	pat_lru_seed = ha_random();
+	pat_lru_seed = random();
 
 	/* Count pat_refs with user defined unique_id and totalt count */
 	list_for_each_entry(ref, &pattern_reference, list) {
diff --git a/src/peers.c b/src/peers.c
index 640a99f..f5a4f18 100644
--- a/src/peers.c
+++ b/src/peers.c
@@ -2232,7 +2232,7 @@
 					 * retrying otherwise the other end will do the same and we can loop
 					 * for a while.
 					 */
-					curpeer->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + ha_random() % 2000));
+					curpeer->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + random() % 2000));
 					peer_session_forceshutdown(curpeer);
 				}
 				if (maj_ver != (unsigned int)-1 && min_ver != (unsigned int)-1) {
@@ -2685,7 +2685,7 @@
 									ps->reconnect = tick_add(now_ms, MS_TO_TICKS(PEER_RECONNECT_TIMEOUT));
 								}
 								else  {
-									ps->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + ha_random() % 2000));
+									ps->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + random() % 2000));
 									peer_session_forceshutdown(ps);
 									ps->no_hbt++;
 								}
@@ -2741,7 +2741,7 @@
 				 * retrying otherwise the other end will do the same and we can loop
 				 * for a while.
 				 */
-				ps->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + ha_random() % 2000));
+				ps->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + random() % 2000));
 				if (ps->appctx) {
 					peer_session_forceshutdown(ps);
 				}
diff --git a/src/sample.c b/src/sample.c
index fd63902..3c61112 100644
--- a/src/sample.c
+++ b/src/sample.c
@@ -3124,7 +3124,7 @@
 static int
 smp_fetch_rand(const struct arg *args, struct sample *smp, const char *kw, void *private)
 {
-	smp->data.u.sint = ha_random();
+	smp->data.u.sint = random();
 
 	/* reduce if needed. Don't do a modulo, use all bits! */
 	if (args && args[0].type == ARGT_SINT)
@@ -3336,7 +3336,7 @@
 
 		while (byte < 4) {
 			while (bits < 32) {
-				last |= (uint64_t)ha_random() << bits;
+				last |= (uint64_t)random() << bits;
 				bits += rand_max_bits;
 			}
 			rnd[byte++] = last;