Use SIGRTMIN from kernel headers

* configure.ac (ASM_SIGRTMIN): Define to SIGRTMIN from <asm/signal.h>.
* signal.c: Use ASM_SIGRTMIN instead of constants provided by libc.
* tests/sigreturn.c: Use ASM_SIGRTMIN instead of hardcoded value.
Use lower RT_* numbers to support pre-3.18 hppa kernels.
* tests/sigreturn.test: Update regexp.
diff --git a/configure.ac b/configure.ac
index 4f2e02b..e73958c 100644
--- a/configure.ac
+++ b/configure.ac
@@ -711,13 +711,22 @@
 AC_CHECK_SIZEOF([long long])
 AC_CHECK_SIZEOF([off_t],,[#include <sys/types.h>])
 
-AC_CACHE_CHECK([for SA_RESTORER], [st_cv_sa_restorer],
-	       [st_cv_sa_restorer="$(echo SA_RESTORER |
+AC_CACHE_CHECK([for SIGRTMIN], [st_cv_SIGRTMIN],
+	       [[st_cv_SIGRTMIN="$(echo SIGRTMIN |
+			$CPP $CPPFLAGS -P -imacros asm/signal.h - |
+			grep '^[0-9]')"
+	         test -n "$st_cv_SIGRTMIN" || st_cv_SIGRTMIN=no]])
+if test "x$st_cv_SIGRTMIN" != xno; then
+	AC_DEFINE_UNQUOTED([ASM_SIGRTMIN], [$st_cv_SIGRTMIN],
+			   [SIGRTMIN defined in <asm/signal.h>])
+fi
+AC_CACHE_CHECK([for SA_RESTORER], [st_cv_SA_RESTORER],
+	       [st_cv_SA_RESTORER="$(echo SA_RESTORER |
 			$CPP $CPPFLAGS -P -imacros asm/signal.h - |
 			grep ^0x)"
-	        test -n "$st_cv_sa_restorer" || st_cv_sa_restorer=no])
-if test "x$st_cv_sa_restorer" != xno; then
-	AC_DEFINE_UNQUOTED([ASM_SA_RESTORER], [$st_cv_sa_restorer],
+	        test -n "$st_cv_SA_RESTORER" || st_cv_SA_RESTORER=no])
+if test "x$st_cv_SA_RESTORER" != xno; then
+	AC_DEFINE_UNQUOTED([ASM_SA_RESTORER], [$st_cv_SA_RESTORER],
 			   [SA_RESTORER defined in <asm/signal.h>])
 fi
 
diff --git a/signal.c b/signal.c
index a970355..6aab54f 100644
--- a/signal.c
+++ b/signal.c
@@ -102,13 +102,21 @@
 #include "xlat/sigprocmaskcmds.h"
 
 /* Anonymous realtime signals. */
-/* Under glibc 2.1, SIGRTMIN et al are functions, but __SIGRTMIN is a
-   constant.  This is what we want.  Otherwise, just use SIGRTMIN. */
-#ifdef SIGRTMIN
-#ifndef __SIGRTMIN
-#define __SIGRTMIN SIGRTMIN
-#define __SIGRTMAX SIGRTMAX /* likewise */
+#ifndef ASM_SIGRTMIN
+/* Linux kernel >= 3.18 defines SIGRTMIN to 32 on all architectures. */
+# define ASM_SIGRTMIN 32
 #endif
+#ifndef ASM_SIGRTMAX
+/* Under glibc 2.1, SIGRTMAX et al are functions, but __SIGRTMAX is a
+   constant.  This is what we want.  Otherwise, just use SIGRTMAX. */
+# ifdef SIGRTMAX
+#  ifndef __SIGRTMAX
+#   define __SIGRTMAX SIGRTMAX
+#  endif
+# endif
+# ifdef __SIGRTMAX
+#  define ASM_SIGRTMAX __SIGRTMAX
+# endif
 #endif
 
 /* Note on the size of sigset_t:
@@ -143,9 +151,9 @@
 
 		if (s < nsignals)
 			return signalent[s];
-#ifdef SIGRTMIN
-		if (s >= __SIGRTMIN && s <= __SIGRTMAX) {
-			sprintf(buf, "SIGRT_%u", s - __SIGRTMIN);
+#ifdef ASM_SIGRTMAX
+		if (s >= ASM_SIGRTMIN && s <= ASM_SIGRTMAX) {
+			sprintf(buf, "SIGRT_%u", s - ASM_SIGRTMIN);
 			return buf;
 		}
 #endif
@@ -214,9 +222,9 @@
 		if ((unsigned) i < nsignals) {
 			s = stpcpy(s, signalent[i] + 3);
 		}
-#ifdef SIGRTMIN
-		else if (i >= __SIGRTMIN && i <= __SIGRTMAX) {
-			s += sprintf(s, "RT_%u", i - __SIGRTMIN);
+#ifdef ASM_SIGRTMAX
+		else if (i >= ASM_SIGRTMIN && i <= ASM_SIGRTMAX) {
+			s += sprintf(s, "RT_%u", i - ASM_SIGRTMIN);
 		}
 #endif
 		else {
diff --git a/tests/sigreturn.c b/tests/sigreturn.c
index 252f152..a84f65e 100644
--- a/tests/sigreturn.c
+++ b/tests/sigreturn.c
@@ -1,12 +1,20 @@
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
 #include <stdlib.h>
 #include <signal.h>
 
+#ifdef ASM_SIGRTMIN
+# define RT_0 ASM_SIGRTMIN
+#else
+/* Linux kernel >= 3.18 defines SIGRTMIN to 32 on all architectures. */
+# define RT_0 32
+#endif
+
 static void handler(int sig)
 {
 }
 
-#define RT_0 32
-
 int main(void) {
 	sigset_t set;
 	sigemptyset(&set);
@@ -15,8 +23,8 @@
 	sigaddset(&set, RT_0 +  2);
 	sigaddset(&set, RT_0 +  3);
 	sigaddset(&set, RT_0 +  4);
-	sigaddset(&set, RT_0 + 31);
-	sigaddset(&set, RT_0 + 32);
+	sigaddset(&set, RT_0 + 26);
+	sigaddset(&set, RT_0 + 27);
 	sigprocmask(SIG_SETMASK, &set, NULL);
 	signal(SIGUSR1, handler);
 	raise(SIGUSR1);
diff --git a/tests/sigreturn.test b/tests/sigreturn.test
index 0395e82..1e96ede 100755
--- a/tests/sigreturn.test
+++ b/tests/sigreturn.test
@@ -21,7 +21,7 @@
 	}
 }
 
-mask='\[(USR2 CHLD|CHLD USR2) RT_2 RT_3 RT_4 RT_31 RT_32\]'
+mask='\[(USR2 CHLD|CHLD USR2) RT_2 RT_3 RT_4 RT_26 RT_27\]'
 rt_sigprocmask='rt_sigprocmask\(SIG_SETMASK, '"$mask"', NULL, [[:digit:]]+\) += 0'
 osf_sigprocmask='osf_sigprocmask\(SIG_SETMASK, '"$mask"'\) += 0 +\(old mask \[[^]]*\]\)'
 grep_log "$rt_sigprocmask|$osf_sigprocmask"