From 8a495a5e9a4977f7226dce8da334f5fb4a724225 Mon Sep 17 00:00:00 2001 From: Andreas Schneider Date: Fri, 4 Nov 2022 14:47:16 +0100 Subject: [PATCH] nwrap: Add NWRAP_REINIT_ALL to initialize mutexes Signed-off-by: Andreas Schneider Reviewed-by: Stefan Metzmacher --- src/nss_wrapper.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/src/nss_wrapper.c b/src/nss_wrapper.c index a1a38d9..07c9757 100644 --- a/src/nss_wrapper.c +++ b/src/nss_wrapper.c @@ -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. -- 2.34.1