diff mbox series

[v2,1/4] lib: Fix initialization of recursive mutex

Message ID 20220822113919.196953-2-tudor.cretu@arm.com
State Accepted
Headers show
Series syscalls: Fix various syscall tests when compiled with Musl | expand

Commit Message

Tudor Cretu Aug. 22, 2022, 11:39 a.m. UTC
For any libc that doesn't define PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
(e.g. Musl [1]), don't assume that the type of the mutex is the first
member. Use a runtime initializer instead.

[1] https://www.openwall.com/lists/musl/2017/02/20/5

Signed-off-by: Tudor Cretu <tudor.cretu@arm.com>
---
 lib/tst_res.c | 25 +++++++++++++++++--------
 1 file changed, 17 insertions(+), 8 deletions(-)

Comments

Petr Vorel Aug. 23, 2022, 9:22 a.m. UTC | #1
Hi Tudor,

merged this one, thanks!

Kind regards,
Petr
diff mbox series

Patch

diff --git a/lib/tst_res.c b/lib/tst_res.c
index 8d86b48a4..e0896eb05 100644
--- a/lib/tst_res.c
+++ b/lib/tst_res.c
@@ -82,17 +82,26 @@  void *TST_RET_PTR;
 	assert(strlen(buf) > 0);		\
 } while (0)
 
-#ifndef PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
-# ifdef __ANDROID__
-#  define PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP \
-	PTHREAD_RECURSIVE_MUTEX_INITIALIZER
-# else
-/* MUSL: http://www.openwall.com/lists/musl/2017/02/20/5 */
-#  define PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP  { {PTHREAD_MUTEX_RECURSIVE} }
-# endif
+#if !defined(PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP) && defined(__ANDROID__)
+#define PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP PTHREAD_RECURSIVE_MUTEX_INITIALIZER
 #endif
 
+#ifdef PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
 static pthread_mutex_t tmutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
+#else
+static pthread_mutex_t tmutex;
+
+__attribute__((constructor))
+static void init_tmutex(void)
+{
+	pthread_mutexattr_t mutattr = {0};
+
+	pthread_mutexattr_init(&mutattr);
+	pthread_mutexattr_settype(&mutattr, PTHREAD_MUTEX_RECURSIVE);
+	pthread_mutex_init(&tmutex, &mutattr);
+	pthread_mutexattr_destroy(&mutattr);
+}
+#endif
 
 static void check_env(void);
 static void tst_condense(int tnum, int ttype, const char *tmesg);