nwrap: Use a constructor to initialize pthread and nwrap
authorAndreas Schneider <asn@samba.org>
Mon, 27 Mar 2017 14:23:08 +0000 (16:23 +0200)
committerAndreas Schneider <asn@samba.org>
Wed, 31 May 2017 13:26:46 +0000 (15:26 +0200)
Signed-off-by: Andreas Schneider <asn@samba.org>
Reviewed-by: Michael Adam <obnox@samba.org>
src/nss_wrapper.c

index 5a0126d9155170dd38ec50e093a4fd5d8da45a7b..2d57c8aef5511491d41059525176ac46cfb39653 100644 (file)
@@ -142,6 +142,12 @@ typedef nss_status_t NSS_STATUS;
 #define PRINTF_ATTRIBUTE(a,b)
 #endif /* HAVE_ATTRIBUTE_PRINTF_FORMAT */
 
+#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
@@ -796,6 +802,7 @@ static struct nwrap_he nwrap_he_global;
 static void nwrap_init(void);
 static bool nwrap_gr_parse_line(struct nwrap_cache *nwrap, char *line);
 static void nwrap_gr_unload(struct nwrap_cache *nwrap);
+void nwrap_constructor(void) CONSTRUCTOR_ATTRIBUTE;
 void nwrap_destructor(void) DESTRUCTOR_ATTRIBUTE;
 
 /*********************************************************
@@ -1561,10 +1568,6 @@ static void nwrap_init(void)
 
        nwrap_initialized = true;
 
-       /* Initialize pthread_atfork handlers */
-       pthread_atfork(&nwrap_thread_prepare, &nwrap_thread_parent,
-                      &nwrap_thread_child);
-
        env = getenv("NSS_WRAPPER_MAX_HOSTENTS");
        if (env != NULL) {
                max_hostents_tmp = (size_t)strtol(env, &endptr, 10);
@@ -5529,6 +5532,22 @@ int gethostname(char *name, size_t len)
        return nwrap_gethostname(name, len);
 }
 
+/****************************
+ * CONSTRUCTOR
+ ***************************/
+void nwrap_constructor(void)
+{
+       pthread_atfork(&nwrap_thread_prepare,
+                      &nwrap_thread_parent,
+                      &nwrap_thread_child);
+
+       /*
+        * Here is safe place to call nwrap_init() and initialize data
+        * for the main process.
+        */
+       nwrap_init();
+}
+
 /****************************
  * DESTRUCTOR
  ***************************/