core: main - functions to set PTHREAD_PROCESS_SHARED for pthread mutex and rwlock
authorDaniel-Constantin Mierla <miconda@gmail.com>
Fri, 13 Sep 2019 11:00:17 +0000 (13:00 +0200)
committerDaniel-Constantin Mierla <miconda@gmail.com>
Fri, 13 Sep 2019 11:00:17 +0000 (13:00 +0200)
- code relocated from src/modules/tls/utils/openssl_mutex_shared/openssl_mutex_shared.c
- simpler option to initialize kamailio with the workaround for libssl
v1.1+ without using the preloaded library approach

src/main.c

index 5af5284..03d39b4 100644 (file)
  * sip router core part.
  */
 
+#ifdef KSR_PTHREAD_MUTEX_SHARED
+#define _GNU_SOURCE
+#include <pthread.h>
+#include <dlfcn.h>
+#endif
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <errno.h>
@@ -2814,3 +2820,72 @@ error:
        }
        return -1;
 }
+
+
+#ifdef KSR_PTHREAD_MUTEX_SHARED
+
+/**
+ * code to set PTHREAD_PROCESS_SHARED attribute for phtread mutex to cope
+ * with libssl 1.1+ thread-only mutex initialization
+ */
+
+#define SYMBOL_EXPORT __attribute__((visibility("default")))
+
+int SYMBOL_EXPORT pthread_mutex_init (pthread_mutex_t *__mutex,
+               const pthread_mutexattr_t *__mutexattr)
+{
+       static int (*real_pthread_mutex_init)(pthread_mutex_t *__mutex,
+                       const pthread_mutexattr_t *__mutexattr) = 0;
+       pthread_mutexattr_t attr;
+       int ret;
+
+       if (!real_pthread_mutex_init) {
+               real_pthread_mutex_init = dlsym(RTLD_NEXT, "pthread_mutex_init");
+               if (!real_pthread_mutex_init) {
+                       return -1;
+               }
+       }
+
+       if (__mutexattr) {
+               pthread_mutexattr_t attr = *__mutexattr;
+               pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
+               return real_pthread_mutex_init(__mutex, &attr);
+       }
+
+       pthread_mutexattr_init(&attr);
+       pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
+       ret = real_pthread_mutex_init(__mutex, &attr);
+       pthread_mutexattr_destroy(&attr);
+
+       return ret;
+}
+
+int SYMBOL_EXPORT pthread_rwlock_init (pthread_rwlock_t *__restrict __rwlock,
+                               const pthread_rwlockattr_t *__restrict __attr)
+{
+       static int (*real_pthread_rwlock_init)(pthread_rwlock_t *__restrict __rwlock,
+                               const pthread_rwlockattr_t *__restrict __attr) = 0;
+       pthread_rwlockattr_t attr;
+       int ret;
+
+       if (!real_pthread_rwlock_init) {
+               real_pthread_rwlock_init = dlsym(RTLD_NEXT, "pthread_rwlock_init");
+               if (!real_pthread_rwlock_init) {
+                       return -1;
+               }
+       }
+
+       if (__attr) {
+               pthread_rwlockattr_t attr = *__attr;
+               pthread_rwlockattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
+               return real_pthread_rwlock_init(__rwlock, &attr);
+       }
+
+       pthread_rwlockattr_init(&attr);
+       pthread_rwlockattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
+       ret = real_pthread_rwlock_init(__rwlock, &attr);
+       pthread_rwlockattr_destroy(&attr);
+
+       return ret;
+}
+#endif