nwrap: Add NWRAP_REINIT_ALL to initialize mutexes
authorAndreas Schneider <asn@samba.org>
Fri, 4 Nov 2022 13:47:16 +0000 (14:47 +0100)
committerAndreas Schneider <asn@samba.org>
Fri, 4 Nov 2022 13:47:40 +0000 (14:47 +0100)
Signed-off-by: Andreas Schneider <asn@samba.org>
Reviewed-by: Stefan Metzmacher <metze@samba.org>
src/nss_wrapper.c

index a1a38d947206f924f91a41af7a0220bd9f54fef9..07c975765e4bb2e07bfc4999d5b57a51466b6f07 100644 (file)
@@ -187,10 +187,29 @@ static pthread_mutex_t nwrap_he_global_mutex = PTHREAD_MUTEX_INITIALIZER;
 static pthread_mutex_t nwrap_pw_global_mutex = PTHREAD_MUTEX_INITIALIZER;
 static pthread_mutex_t nwrap_sp_global_mutex = PTHREAD_MUTEX_INITIALIZER;
 
+#define nss_wrapper_init_mutex(m) \
+       _nss_wrapper_init_mutex(m, #m)
+
 /* Add new global locks here please */
 /* Also don't forget to add locks to
  * nwrap_init() function.
  */
+# define NWRAP_REINIT_ALL do { \
+       int ret; \
+       ret = nss_wrapper_init_mutex(&nwrap_initialized_mutex); \
+       if (ret != 0) exit(-1); \
+       ret = nss_wrapper_init_mutex(&nwrap_global_mutex); \
+       if (ret != 0) exit(-1); \
+       ret = nss_wrapper_init_mutex(&nwrap_gr_global_mutex); \
+       if (ret != 0) exit(-1); \
+       ret = nss_wrapper_init_mutex(&nwrap_he_global_mutex); \
+       if (ret != 0) exit(-1); \
+       ret = nss_wrapper_init_mutex(&nwrap_pw_global_mutex); \
+       if (ret != 0) exit(-1); \
+       ret = nss_wrapper_init_mutex(&nwrap_sp_global_mutex); \
+       if (ret != 0) exit(-1); \
+} while(0)
+
 # define NWRAP_LOCK_ALL do { \
        nwrap_mutex_lock(&nwrap_initialized_mutex); \
        nwrap_mutex_lock(&nwrap_global_mutex); \
@@ -1917,6 +1936,34 @@ static void nwrap_backend_init(struct nwrap_main *r)
        }
 }
 
+static int _nss_wrapper_init_mutex(pthread_mutex_t *m, const char *name)
+{
+       pthread_mutexattr_t ma;
+       bool need_destroy = false;
+       int ret = 0;
+
+#define __CHECK(cmd) do { \
+       ret = cmd; \
+       if (ret != 0) { \
+               NWRAP_LOG(NWRAP_LOG_ERROR, \
+                         "%s: %s - failed %d", \
+                         name, #cmd, ret); \
+               goto done; \
+       } \
+} while(0)
+
+       *m = (pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER;
+       __CHECK(pthread_mutexattr_init(&ma));
+       need_destroy = true;
+       __CHECK(pthread_mutexattr_settype(&ma, PTHREAD_MUTEX_ERRORCHECK));
+       __CHECK(pthread_mutex_init(m, &ma));
+done:
+       if (need_destroy) {
+               pthread_mutexattr_destroy(&ma);
+       }
+       return ret;
+}
+
 static void nwrap_init(void)
 {
        const char *env;
@@ -6411,7 +6458,7 @@ static void nwrap_thread_parent(void)
 
 static void nwrap_thread_child(void)
 {
-       NWRAP_UNLOCK_ALL;
+       NWRAP_REINIT_ALL;
 }
 
 /****************************
@@ -6419,6 +6466,8 @@ static void nwrap_thread_child(void)
  ***************************/
 void nwrap_constructor(void)
 {
+       NWRAP_REINIT_ALL;
+
        /*
         * 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.