uwrap: Add library constructor and move pthread_atfork inside.
authorRobin Hack <hack.robin@gmail.com>
Fri, 23 Jan 2015 14:10:02 +0000 (15:10 +0100)
committerAndreas Schneider <asn@cryptomilk.org>
Wed, 28 Jan 2015 16:17:07 +0000 (17:17 +0100)
Library constructor is used for pthread_atfork call. Moved here because
pthread_atfork is cumulative and should be called only once.

Signed-off-by: Robin Hack <hack.robin@gmail.com>
Reviewed-by: Andreas Schneider <asn@samba.org>
Reviewed-by: Stefan Metzmacher <metze@samba.org>
lib/uid_wrapper/uid_wrapper.c
lib/uid_wrapper/wscript

index a492b965d5d0a3f57c49c7f28b9937dc568688a6..a955e77f0ec3450c6486d6b40a3a2fa6fc6c01c9 100644 (file)
        pthread_mutex_unlock(&( m ## _mutex)); \
 } while(0)
 
+#ifdef HAVE_CONSTRUCTOR_ATTRIBUTE
+#define CONSTRUCTOR_ATTRIBUTE __attribute__ ((constructor))
+#else
+#define CONSTRUCTOR_ATTRIBUTE
+#endif /* HAVE_CONSTRUCTOR_ATTRIBUTE */
+
 #ifdef HAVE_DESTRUCTOR_ATTRIBUTE
 #define DESTRUCTOR_ATTRIBUTE __attribute__ ((destructor))
 #else
@@ -249,6 +255,7 @@ static pthread_mutex_t uwrap_id_mutex = PTHREAD_MUTEX_INITIALIZER;
  *********************************************************/
 
 bool uid_wrapper_enabled(void);
+void uwrap_constructor(void) CONSTRUCTOR_ATTRIBUTE;
 void uwrap_destructor(void) DESTRUCTOR_ATTRIBUTE;
 
 /*********************************************************
@@ -591,15 +598,6 @@ static void uwrap_init(void)
 
        UWRAP_LOG(UWRAP_LOG_DEBUG, "Initialize uid_wrapper");
 
-       /*
-        * 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.
-        * This should prevent such deadlocks.
-        */
-       pthread_atfork(&uwrap_thread_prepare,
-                      &uwrap_thread_parent,
-                      &uwrap_thread_child);
-
        UWRAP_LOCK(uwrap_id);
 
        uwrap.initialised = true;
@@ -1248,6 +1246,21 @@ long int syscall (long int sysno, ...)
 #endif /* HAVE_SYSCALL */
 #endif /* HAVE_SYS_SYSCALL_H || HAVE_SYSCALL_H */
 
+/****************************
+ * CONSTRUCTOR
+ ***************************/
+void uwrap_constructor(void)
+{
+       /*
+       * 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.
+       * This should prevent such deadlocks.
+       */
+       pthread_atfork(&uwrap_thread_prepare,
+                      &uwrap_thread_parent,
+                      &uwrap_thread_child);
+}
+
 /****************************
  * DESTRUCTOR
  ***************************/
index 6b585956e0d90c0b0dca9e2e9045617ea4c1ecdf..bd797e5a72b2702748e31995d500af588c58dedb 100644 (file)
@@ -21,6 +21,23 @@ def configure(conf):
             addmain=False,
             msg='Checking for thread local storage')
 
+        # check HAVE_CONSTRUCTOR_ATTRIBUTE
+        conf.CHECK_CODE('''
+            void test_constructor_attribute(void) __attribute__ ((constructor));
+
+            void test_constructor_attribute(void)
+            {
+                return;
+            }
+
+            int main(void) {
+                return 0;
+            }
+            ''',
+            'HAVE_CONSTRUCTOR_ATTRIBUTE',
+            addmain=False,
+            msg='Checking for library constructor support')
+
         # check HAVE_DESTRUCTOR_ATTRIBUTE
         conf.CHECK_CODE('''
             void test_destructor_attribute(void) __attribute__ ((destructor));