Put thread-fork-init inside a run-once guard
authorRich Salz <rsalz@openssl.org>
Fri, 18 Aug 2017 15:47:21 +0000 (11:47 -0400)
committerRich Salz <rsalz@openssl.org>
Fri, 18 Aug 2017 15:48:35 +0000 (11:48 -0400)
Thanks to Christian Heimes for pointing this out.

Reviewed-by: Andy Polyakov <appro@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/4191)

crypto/threads_pthread.c

index e28a2021b45a76fb72675b2069b58956a67bbe92..9644c25aea947acbfba6ef7f8bc71601d4e3be9d 100644 (file)
@@ -169,11 +169,20 @@ int CRYPTO_atomic_add(int *val, int amount, int *ret, CRYPTO_RWLOCK *lock)
     return 1;
 }
 
+# ifdef OPENSSL_SYS_UNIX
+static pthread_once_t fork_once_control = PTHREAD_ONCE_INIT;
+
+static void fork_once_func(void)
+{
+    pthread_atfork(OPENSSL_fork_prepare,
+                   OPENSSL_fork_parent, OPENSSL_fork_child);
+}
+# endif
+
 int openssl_init_fork_handlers(void)
 {
 # ifdef OPENSSL_SYS_UNIX
-    if (pthread_atfork(OPENSSL_fork_prepare,
-                       OPENSSL_fork_parent, OPENSSL_fork_child) == 0)
+    if (pthread_once(&fork_once_control, fork_once_func) == 0)
         return 1;
 # endif
     return 0;