uwrap: Add a workaround for glibc malloc mutex deadlock bug
authorAndreas Schneider <asn@samba.org>
Fri, 21 Jul 2017 08:54:05 +0000 (10:54 +0200)
committerAndreas Schneider <asn@samba.org>
Fri, 21 Jul 2017 15:19:48 +0000 (17:19 +0200)
Signed-off-by: Andreas Schneider <asn@samba.org>
Reviewed-by: Stefan Metzmacher <metze@samba.org>
src/uid_wrapper.c

index a48cc3bda275597cec8126c14446ef5e747e0f0d..0d74d203627459c685648ea4a93b303905b8170d 100644 (file)
@@ -2291,8 +2291,30 @@ long int syscall (long int sysno, ...)
 /****************************
  * CONSTRUCTOR
  ***************************/
+
 void uwrap_constructor(void)
 {
+       char *glibc_malloc_lock_bug;
+
+       /*
+        * This is a workaround for a bug in glibc < 2.24:
+        *
+        * The child handler for the malloc() function is called and locks the
+        * mutex. Then our child handler is called and we try to call setenv().
+        * setenv() wants to malloc and tries to aquire the lock for malloc and
+        * we end up in a deadlock.
+        *
+        * So as a workaround we need to call malloc once before we setup the
+        * handlers.
+        *
+        * See https://sourceware.org/bugzilla/show_bug.cgi?id=16742
+        */
+       glibc_malloc_lock_bug = malloc(1);
+       if (glibc_malloc_lock_bug == NULL) {
+               exit(-1);
+       }
+       glibc_malloc_lock_bug[0] = '\0';
+
        /*
        * If we hold a lock and the application forks, then the child
        * is not able to unlock the mutex and we are in a deadlock.
@@ -2302,6 +2324,8 @@ void uwrap_constructor(void)
                       &uwrap_thread_parent,
                       &uwrap_thread_child);
 
+       free(glibc_malloc_lock_bug);
+
        /* Here is safe place to call uwrap_init() and initialize data
         * for main process.
         */