From 83df37436d3b10fc51d2ecc164f04527e4069b8b Mon Sep 17 00:00:00 2001
From: Daniel Micay <danielmicay@gmail.com>
Date: Fri, 12 Oct 2018 16:02:23 -0400
Subject: [PATCH] fix usage of pthread_atfork for glibc < 2.28

---
 malloc.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/malloc.c b/malloc.c
index b53bcc0..4ec95c0 100644
--- a/malloc.c
+++ b/malloc.c
@@ -18,6 +18,15 @@
 #include "random.h"
 #include "util.h"
 
+// use __register_atfork directly to avoid linking with libpthread for glibc < 2.28
+#ifdef __GLIBC__
+extern void *__dso_handle;
+extern int __register_atfork(void (*)(void), void (*)(void), void (*)(void), void *);
+#define atfork(prepare, parent, child) __register_atfork(prepare, parent, child, __dso_handle)
+#else
+#define atfork pthread_atfork
+#endif
+
 static_assert(sizeof(void *) == 8, "64-bit only");
 
 static_assert(!WRITE_AFTER_FREE_CHECK || ZERO_ON_FREE, "WRITE_AFTER_FREE_CHECK depends on ZERO_ON_FREE");
@@ -773,7 +782,7 @@ COLD static void init_slow_path(void) {
     mutex_unlock(&lock);
 
     // may allocate, so wait until the allocator is initialized to avoid deadlocking
-    if (pthread_atfork(full_lock, full_unlock, post_fork_child)) {
+    if (atfork(full_lock, full_unlock, post_fork_child)) {
         fatal_error("pthread_atfork failed");
     }
 }