From dc5a24c459da3f169fb289e5f72149c63e3b26d0 Mon Sep 17 00:00:00 2001
From: William Ahern <william+alpine@25thandClement.com>
Date: Thu, 14 Apr 2016 20:10:15 +0000
Subject: [PATCH] musl libc has mt-safe strsignal

---
 src/unix.c | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)

diff --git a/src/unix.c b/src/unix.c
index 516be9e..46ca166 100644
--- a/src/unix.c
+++ b/src/unix.c
@@ -82,6 +82,9 @@
 
 #define UCLIBC_PREREQ(M, m, p) (defined __UCLIBC__ && (__UCLIBC_MAJOR__ > M || (__UCLIBC_MAJOR__ == M && __UCLIBC_MINOR__ > m) || (__UCLIBC_MAJOR__ == M && __UCLIBC_MINOR__ == m && __UCLIBC_SUBLEVEL__ >= p)))
 
+/* NB: uClibc defines __GLIBC__ */
+#define MUSL_MAYBE (__linux && !__BIONIC__ && !__GLIBC__ && !__UCLIBC__)
+
 #define NETBSD_PREREQ(M, m) __NetBSD_Prereq__(M, m, 0)
 
 #define FREEBSD_PREREQ(M, m) (defined __FreeBSD_version && __FreeBSD_version >= ((M) * 100000) + ((m) * 1000))
@@ -318,11 +321,11 @@
 #endif
 
 #ifndef HAVE_SYS_SIGLIST
-#define HAVE_SYS_SIGLIST 1
+#define HAVE_SYS_SIGLIST (!MUSL_MAYBE && !__sun && !_AIX)
 #endif
 
 #ifndef HAVE_DECL_SYS_SIGLIST
-#define HAVE_DECL_SYS_SIGLIST 1
+#define HAVE_DECL_SYS_SIGLIST HAVE_SYS_SIGLIST
 #endif
 
 #ifndef STRERROR_R_CHAR_P
@@ -2607,6 +2610,7 @@ static void unixL_random_buf(lua_State *L, void *buf, size_t bufsiz, const unsig
  * 	              returns NULL on bad signo
  * 	Linux/glibc : safe'ish since 1998; TLS buffer for bad signo;
  * 	              gettext for good signo (is gettext thread-safe?)
+ * 	 Linux/musl : safe; localized; locale structures never deallocated
  * 	    FreeBSD : safe since 8.1; TLS buffer
  * 	     NetBSD : not safe as of 6.1; static buffer
  * 	    OpenBSD : not safe as of 5.6; static buffer
@@ -2621,11 +2625,18 @@ static void unixL_random_buf(lua_State *L, void *buf, size_t bufsiz, const unsig
  * has _sys_siglistp instead of sys_siglist. But we use strsignal on those
  * platforms.
  */
+#ifndef HAVE_MTSAFE_STRSIGNAL
+#define HAVE_MTSAFE_STRSIGNAL_ \
+	(__sun || GLIBC_PREREQ(0,0) || MUSL_MAYBE || \
+	 FREEBSD_PREREQ(8,1) || defined __APPLE__ || defined _AIX)
+#define HAVE_MTSAFE_STRSIGNAL (HAVE_STRSIGNAL && HAVE_MTSAFE_STRSIGNAL_)
+#endif
+
 static const char *unixL_strsignal(lua_State *L, int signo) {
 	const char *info;
 	unixL_State *U;
 
-#if HAVE_STRSIGNAL && (USE_STRSIGNAL || __sun || GLIBC_PREREQ(0,0) || FREEBSD_PREREQ(8,1) || defined __APPLE__ || defined _AIX)
+#if HAVE_MTSAFE_STRSIGNAL
 	/* AIX strsignal(3) cannot handle bad signo */
 	if (signo >= 0 && signo < NSIG && (info = strsignal(signo)))
 		return info;
