# define UWRAP_THREAD
#endif
-# define UWRAP_LOCK(m) do { \
- pthread_mutex_lock(&( m ## _mutex)); \
-} while(0)
-
-# define UWRAP_UNLOCK(m) do { \
- pthread_mutex_unlock(&( m ## _mutex)); \
-} while(0)
-
-/* Add new global locks here please */
-# define UWRAP_LOCK_ALL \
- UWRAP_LOCK(uwrap_id)
-
-# define UWRAP_UNLOCK_ALL \
- UWRAP_UNLOCK(uwrap_id)
-
#ifdef HAVE_CONSTRUCTOR_ATTRIBUTE
#define CONSTRUCTOR_ATTRIBUTE __attribute__ ((constructor))
#else
/* The mutex or accessing the id */
static pthread_mutex_t uwrap_id_mutex = PTHREAD_MUTEX_INITIALIZER;
+#define uwrap_init_mutex(m) _uwrap_init_mutex(m, #m)
+static int _uwrap_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) { \
+ UWRAP_LOG(UWRAP_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;
+}
+#define uwrap_mutex_lock(m) _uwrap_mutex_lock(m, #m, __func__, __LINE__)
+static void _uwrap_mutex_lock(pthread_mutex_t *mutex,
+ const char *name,
+ const char *caller,
+ unsigned line)
+{
+ int ret;
+
+ ret = pthread_mutex_lock(mutex);
+ if (ret != 0) {
+ UWRAP_LOG(UWRAP_LOG_ERROR,
+ "PID(%d):PPID(%d): %s(%u): Couldn't lock pthread "
+ "mutex(%s) - %s",
+ getpid(),
+ getppid(),
+ caller,
+ line,
+ name,
+ strerror(ret));
+ abort();
+ }
+}
+
+#define uwrap_mutex_unlock(m) _uwrap_mutex_unlock(m, #m, __func__, __LINE__)
+static void _uwrap_mutex_unlock(pthread_mutex_t *mutex,
+ const char *name,
+ const char *caller,
+ unsigned line)
+{
+ int ret;
+
+ ret = pthread_mutex_unlock(mutex);
+ if (ret != 0) {
+ UWRAP_LOG(UWRAP_LOG_ERROR,
+ "PID(%d):PPID(%d): %s(%u): Couldn't unlock pthread "
+ "mutex(%s) - %s",
+ getpid(),
+ getppid(),
+ caller,
+ line,
+ name,
+ strerror(ret));
+ abort();
+ }
+}
+
+#define UWRAP_LOCK(m) \
+ do { \
+ uwrap_mutex_lock(&(m##_mutex)); \
+ } while (0)
+
+#define UWRAP_UNLOCK(m) \
+ do { \
+ uwrap_mutex_unlock(&(m##_mutex)); \
+ } while (0)
+
+/* Add new global locks here please */
+#define UWRAP_REINIT_ALL \
+ do { \
+ int ret; \
+ ret = uwrap_init_mutex(&uwrap_id_mutex); \
+ if (ret != 0) \
+ exit(-1); \
+ } while (0)
+
+/* Add new global locks here please */
+#define UWRAP_LOCK_ALL \
+ do { \
+ UWRAP_LOCK(uwrap_id); \
+ } while (0)
+
+#define UWRAP_UNLOCK_ALL \
+ do { \
+ UWRAP_UNLOCK(uwrap_id); \
+ } while (0)
/*********************************************************
* UWRAP PROTOTYPES
struct uwrap_thread *id = uwrap_tls_id;
struct uwrap_thread *u = uwrap.ids;
+ UWRAP_REINIT_ALL;
+
/* uid_wrapper is loaded but not enabled */
if (id == NULL) {
- UWRAP_UNLOCK_ALL;
return;
}
uwrap_export_ids(id);
id->enabled = true;
-
- UWRAP_UNLOCK_ALL;
}
static unsigned long uwrap_get_xid_from_env(const char *envname)
}
glibc_malloc_lock_bug[0] = '\0';
+ UWRAP_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.