Make SIGRTMIN hide the real-time signals we use internally.

__SIGRTMIN will continue to tell the truth. This matches glibc's
behavior (as evidenced by the fact that we don't need a special case
in the strsignal test now).

Change-Id: I1abe1681d516577afa8cd39c837ef12467f68dd2
diff --git a/libc/Android.mk b/libc/Android.mk
index 0b70e07..a0eb612 100644
--- a/libc/Android.mk
+++ b/libc/Android.mk
@@ -129,6 +129,8 @@
     bionic/inotify_init.cpp \
     bionic/lchown.cpp \
     bionic/lfs64_support.cpp \
+    bionic/__libc_current_sigrtmax.cpp \
+    bionic/__libc_current_sigrtmin.cpp \
     bionic/libc_init_common.cpp \
     bionic/libc_logging.cpp \
     bionic/libgen.cpp \
diff --git a/libc/bionic/__libc_current_sigrtmax.cpp b/libc/bionic/__libc_current_sigrtmax.cpp
new file mode 100644
index 0000000..27fcb35
--- /dev/null
+++ b/libc/bionic/__libc_current_sigrtmax.cpp
@@ -0,0 +1,33 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *  * Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *  * Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <signal.h>
+
+int __libc_current_sigrtmax(void) {
+  return __SIGRTMAX;
+}
diff --git a/libc/bionic/__libc_current_sigrtmin.cpp b/libc/bionic/__libc_current_sigrtmin.cpp
new file mode 100644
index 0000000..16b037d
--- /dev/null
+++ b/libc/bionic/__libc_current_sigrtmin.cpp
@@ -0,0 +1,37 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *  * Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *  * Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <signal.h>
+
+// POSIX timers use __SIGRTMIN + 0.
+// libbacktrace uses __SIGRTMIN + 1.
+// libcore uses __SIGRTMIN + 2.
+
+int __libc_current_sigrtmin(void) {
+  return __SIGRTMIN + 3;
+}
diff --git a/libc/bionic/posix_timers.cpp b/libc/bionic/posix_timers.cpp
index 59933ec..312bf9e 100644
--- a/libc/bionic/posix_timers.cpp
+++ b/libc/bionic/posix_timers.cpp
@@ -52,7 +52,7 @@
 // end up with one of our POSIX timer threads handling it (meaning that the intended recipient
 // doesn't). glibc uses SIGRTMIN for its POSIX timer implementation, so in the absence of any
 // reason to use anything else, we use that too.
-static const int TIMER_SIGNAL = SIGRTMIN;
+static const int TIMER_SIGNAL = (__SIGRTMIN + 0);
 
 struct PosixTimer {
   __kernel_timer_t kernel_timer_id;
diff --git a/libc/include/signal.h b/libc/include/signal.h
index 267f3e6..45c1cda 100644
--- a/libc/include/signal.h
+++ b/libc/include/signal.h
@@ -60,6 +60,12 @@
 #define _NSIG (_KERNEL__NSIG + 1)
 #define NSIG _NSIG
 
+/* We take a few real-time signals for ourselves. May as well use the same names as glibc. */
+#define SIGRTMIN (__libc_current_sigrtmin())
+#define SIGRTMAX (__libc_current_sigrtmax())
+extern int __libc_current_sigrtmin(void);
+extern int __libc_current_sigrtmax(void);
+
 extern const char* const sys_siglist[];
 extern const char* const sys_signame[]; /* BSD compatibility. */
 
diff --git a/libc/kernel/tools/defaults.py b/libc/kernel/tools/defaults.py
index b4d823e..8c41cc6 100644
--- a/libc/kernel/tools/defaults.py
+++ b/libc/kernel/tools/defaults.py
@@ -63,6 +63,9 @@
     # The kernel's _NSIG/NSIG are one less than the userspace value, so we need to move them aside.
     "_NSIG": "_KERNEL__NSIG",
     "NSIG": "_KERNEL_NSIG",
+    # The kernel's SIGRTMIN/SIGRTMAX are absolute limits; userspace steals a few.
+    "SIGRTMIN": "__SIGRTMIN",
+    "SIGRTMAX": "__SIGRTMAX",
     }
 
 # this is the set of known static inline functions that we want to keep
diff --git a/libc/kernel/uapi/asm-arm/asm/signal.h b/libc/kernel/uapi/asm-arm/asm/signal.h
index 512e22a..fd39aaa 100644
--- a/libc/kernel/uapi/asm-arm/asm/signal.h
+++ b/libc/kernel/uapi/asm-arm/asm/signal.h
@@ -66,8 +66,8 @@
 #define SIGSYS 31
 #define SIGUNUSED 31
 /* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
-#define SIGRTMIN 32
-#define SIGRTMAX _KERNEL__NSIG
+#define __SIGRTMIN 32
+#define __SIGRTMAX _KERNEL__NSIG
 #define SIGSWI 32
 #define SA_NOCLDSTOP 0x00000001
 /* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
diff --git a/libc/kernel/uapi/asm-generic/signal.h b/libc/kernel/uapi/asm-generic/signal.h
index fe7d9a09..a16622a 100644
--- a/libc/kernel/uapi/asm-generic/signal.h
+++ b/libc/kernel/uapi/asm-generic/signal.h
@@ -66,9 +66,9 @@
 #define SIGSYS 31
 #define SIGUNUSED 31
 /* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
-#define SIGRTMIN 32
+#define __SIGRTMIN 32
 #ifndef SIGRTMAX
-#define SIGRTMAX _KERNEL__NSIG
+#define __SIGRTMAX _KERNEL__NSIG
 #endif
 /* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
 #define SA_NOCLDSTOP 0x00000001
diff --git a/libc/kernel/uapi/asm-mips/asm/signal.h b/libc/kernel/uapi/asm-mips/asm/signal.h
index 53f5015..b774a66 100644
--- a/libc/kernel/uapi/asm-mips/asm/signal.h
+++ b/libc/kernel/uapi/asm-mips/asm/signal.h
@@ -71,8 +71,8 @@
 #define SIGXCPU 30
 #define SIGXFSZ 31
 /* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
-#define SIGRTMIN 32
-#define SIGRTMAX _KERNEL__NSIG
+#define __SIGRTMIN 32
+#define __SIGRTMAX _KERNEL__NSIG
 #define SA_ONSTACK 0x08000000
 #define SA_RESETHAND 0x80000000
 /* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
diff --git a/libc/kernel/uapi/asm-x86/asm/signal.h b/libc/kernel/uapi/asm-x86/asm/signal.h
index 6f5b435..308c7a9 100644
--- a/libc/kernel/uapi/asm-x86/asm/signal.h
+++ b/libc/kernel/uapi/asm-x86/asm/signal.h
@@ -71,8 +71,8 @@
 #define SIGSYS 31
 #define SIGUNUSED 31
 /* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
-#define SIGRTMIN 32
-#define SIGRTMAX _KERNEL__NSIG
+#define __SIGRTMIN 32
+#define __SIGRTMAX _KERNEL__NSIG
 #define SA_NOCLDSTOP 0x00000001u
 #define SA_NOCLDWAIT 0x00000002u
 /* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
diff --git a/tests/signal_test.cpp b/tests/signal_test.cpp
index 6d55bef..af98964 100644
--- a/tests/signal_test.cpp
+++ b/tests/signal_test.cpp
@@ -252,3 +252,21 @@
   ASSERT_TRUE(sys_siglist[0] == NULL);
   ASSERT_STREQ("Hangup", sys_siglist[SIGHUP]);
 }
+
+TEST(signal, limits) {
+  // This comes from the kernel.
+  ASSERT_EQ(32, __SIGRTMIN);
+
+  // We reserve a non-zero number at the bottom for ourselves.
+  ASSERT_GT(SIGRTMIN, __SIGRTMIN);
+
+  // MIPS has more signals than everyone else.
+#if defined(__mips__)
+  ASSERT_EQ(128, __SIGRTMAX);
+#else
+  ASSERT_EQ(64, __SIGRTMAX);
+#endif
+
+  // We don't currently reserve any at the top.
+  ASSERT_EQ(SIGRTMAX, __SIGRTMAX);
+}
diff --git a/tests/string_test.cpp b/tests/string_test.cpp
index 14b284e..5ccc63d 100644
--- a/tests/string_test.cpp
+++ b/tests/string_test.cpp
@@ -100,11 +100,9 @@
   ASSERT_STREQ("Hangup", strsignal(1));
 
   // A real-time signal.
-#ifdef __GLIBC__ // glibc reserves real-time signals for internal use, and doesn't count those.
-  ASSERT_STREQ("Real-time signal 14", strsignal(48));
-#else
-  ASSERT_STREQ("Real-time signal 16", strsignal(48));
-#endif
+  ASSERT_STREQ("Real-time signal 14", strsignal(SIGRTMIN + 14));
+  // One of the signals the C library keeps to itself.
+  ASSERT_STREQ("Unknown signal 32", strsignal(__SIGRTMIN));
 
   // Errors.
   ASSERT_STREQ("Unknown signal -1", strsignal(-1)); // Too small.