Defer registration of the arc4random fork-detect handler.

Previously, arc4random would register a fork-detecting pthread_atfork
handler to not have to call getpid() after a fork. pthread_atfork uses
pthread_mutex_lock, which requires the current thread to be initialized,
preventing the use of arc4random for initializing the global stack guard,
which needs to happen before the main thread has been initialized.

Extract the arc4random fork-detection flag and use the existing
arc4random fork handler to set it.

Bug: http://b/29622562
Change-Id: I98c9329fa0e489c3f78cad52747eaaf2f5226b80
diff --git a/libc/bionic/libc_init_common.cpp b/libc/bionic/libc_init_common.cpp
index 6f5b379..2070c9c 100644
--- a/libc/bionic/libc_init_common.cpp
+++ b/libc/bionic/libc_init_common.cpp
@@ -109,6 +109,11 @@
 }
 #endif
 
+static void arc4random_fork_handler() {
+  _rs_forked = 1;
+  _thread_arc4_lock();
+}
+
 void __libc_init_common(KernelArgumentBlock& args) {
   // Initialize various globals.
   environ = args.envp;
@@ -125,7 +130,7 @@
   __pthread_internal_add(main_thread);
 
   // Register atfork handlers to take and release the arc4random lock.
-  pthread_atfork(_thread_arc4_lock, _thread_arc4_unlock, _thread_arc4_unlock);
+  pthread_atfork(arc4random_fork_handler, _thread_arc4_unlock, _thread_arc4_unlock);
 
   __system_properties_init(); // Requires 'environ'.
 }