Fix sigsuspend to use rt_sigsuspend on all platforms.

Change-Id: I981c1a66d35480d4457a0a08a1b042dac94daa5b
diff --git a/libc/Android.mk b/libc/Android.mk
index b9db201..88be78d 100644
--- a/libc/Android.mk
+++ b/libc/Android.mk
@@ -184,7 +184,6 @@
     bionic/fstatfs.c \
     bionic/__get_tls.cpp \
     bionic/lseek64.c \
-    bionic/sigsuspend.c \
     bionic/statfs.c \
 
 endif
@@ -248,6 +247,7 @@
     bionic/setlocale.cpp \
     bionic/signalfd.cpp \
     bionic/sigprocmask.cpp \
+    bionic/sigsuspend.cpp \
     bionic/sigwait.cpp \
     bionic/statvfs.cpp \
     bionic/strerror.cpp \
diff --git a/libc/SYSCALLS.TXT b/libc/SYSCALLS.TXT
index cea1878..0d1e808 100644
--- a/libc/SYSCALLS.TXT
+++ b/libc/SYSCALLS.TXT
@@ -233,14 +233,12 @@
 
 # signals
 int     sigaction(int, const struct sigaction*, struct sigaction*)  arm,x86,mips
-int     __sigsuspend:sigsuspend(int unused1, int unused2, unsigned mask)  arm,x86
-int     __sigsuspend:sigsuspend(const sigset_t* mask)  mips
-int     __rt_sigsuspend:rt_sigsuspend(const sigset_t *unewset, size_t sigset_size)  x86_64
-int     __rt_sigaction:rt_sigaction(int sig, const struct sigaction* act, struct sigaction* oact, size_t sigsetsize)  all
-int     __rt_sigprocmask:rt_sigprocmask(int  how, const sigset_t* set, sigset_t* oset, size_t sigsetsize)  all
-int     __rt_sigtimedwait:rt_sigtimedwait(const sigset_t* set, struct siginfo_t* info, struct timespec_t* timeout, size_t  sigset_size)  all
 int     sigpending(sigset_t*)  arm,x86,mips
-int     signalfd4(int fd, const sigset_t* mask, size_t sizemask, int flags)  all
+int     __rt_sigaction:rt_sigaction(int, const struct sigaction*, struct sigaction*, size_t)  all
+int     __rt_sigprocmask:rt_sigprocmask(int, const sigset_t*, sigset_t*, size_t)  all
+int     __rt_sigsuspend:rt_sigsuspend(const sigset_t*, size_t)  all
+int     __rt_sigtimedwait:rt_sigtimedwait(const sigset_t*, struct siginfo_t*, struct timespec_t*, size_t)  all
+int     signalfd4(int, const sigset_t*, size_t, int)  all
 
 # sockets
 int           socket(int, int, int)              arm,mips,x86_64
@@ -296,7 +294,7 @@
 # other
 int     uname(struct utsname*)  all
 mode_t  umask(mode_t)  all
-int      __reboot:reboot(int, int, int, void*)  all
+int     __reboot:reboot(int, int, int, void*)  all
 int     __syslog:syslog(int, char*, int)  all
 int     init_module(void*, unsigned long, const char*)  all
 int     delete_module(const char*, unsigned int)   all
diff --git a/libc/arch-arm/syscalls.mk b/libc/arch-arm/syscalls.mk
index 3a66c1b..dc7e3df 100644
--- a/libc/arch-arm/syscalls.mk
+++ b/libc/arch-arm/syscalls.mk
@@ -152,11 +152,11 @@
 syscall_src += arch-arm/syscalls/timerfd_settime.S
 syscall_src += arch-arm/syscalls/timerfd_gettime.S
 syscall_src += arch-arm/syscalls/sigaction.S
-syscall_src += arch-arm/syscalls/__sigsuspend.S
+syscall_src += arch-arm/syscalls/sigpending.S
 syscall_src += arch-arm/syscalls/__rt_sigaction.S
 syscall_src += arch-arm/syscalls/__rt_sigprocmask.S
+syscall_src += arch-arm/syscalls/__rt_sigsuspend.S
 syscall_src += arch-arm/syscalls/__rt_sigtimedwait.S
-syscall_src += arch-arm/syscalls/sigpending.S
 syscall_src += arch-arm/syscalls/signalfd4.S
 syscall_src += arch-arm/syscalls/socket.S
 syscall_src += arch-arm/syscalls/socketpair.S
diff --git a/libc/arch-arm/syscalls/__sigsuspend.S b/libc/arch-arm/syscalls/__rt_sigsuspend.S
similarity index 76%
rename from libc/arch-arm/syscalls/__sigsuspend.S
rename to libc/arch-arm/syscalls/__rt_sigsuspend.S
index dc519d2..2e63e89 100644
--- a/libc/arch-arm/syscalls/__sigsuspend.S
+++ b/libc/arch-arm/syscalls/__rt_sigsuspend.S
@@ -3,13 +3,13 @@
 #include <linux/err.h>
 #include <machine/asm.h>
 
-ENTRY(__sigsuspend)
+ENTRY(__rt_sigsuspend)
     mov     ip, r7
-    ldr     r7, =__NR_sigsuspend
+    ldr     r7, =__NR_rt_sigsuspend
     swi     #0
     mov     r7, ip
     cmn     r0, #(MAX_ERRNO + 1)
     bxls    lr
     neg     r0, r0
     b       __set_errno
-END(__sigsuspend)
+END(__rt_sigsuspend)
diff --git a/libc/arch-mips/syscalls.mk b/libc/arch-mips/syscalls.mk
index 740f91c..2704200 100644
--- a/libc/arch-mips/syscalls.mk
+++ b/libc/arch-mips/syscalls.mk
@@ -155,11 +155,11 @@
 syscall_src += arch-mips/syscalls/timerfd_settime.S
 syscall_src += arch-mips/syscalls/timerfd_gettime.S
 syscall_src += arch-mips/syscalls/sigaction.S
-syscall_src += arch-mips/syscalls/__sigsuspend.S
+syscall_src += arch-mips/syscalls/sigpending.S
 syscall_src += arch-mips/syscalls/__rt_sigaction.S
 syscall_src += arch-mips/syscalls/__rt_sigprocmask.S
+syscall_src += arch-mips/syscalls/__rt_sigsuspend.S
 syscall_src += arch-mips/syscalls/__rt_sigtimedwait.S
-syscall_src += arch-mips/syscalls/sigpending.S
 syscall_src += arch-mips/syscalls/signalfd4.S
 syscall_src += arch-mips/syscalls/socket.S
 syscall_src += arch-mips/syscalls/socketpair.S
diff --git a/libc/arch-mips/syscalls/__sigsuspend.S b/libc/arch-mips/syscalls/__rt_sigsuspend.S
similarity index 66%
rename from libc/arch-mips/syscalls/__sigsuspend.S
rename to libc/arch-mips/syscalls/__rt_sigsuspend.S
index 2b6f6d7..a1a3fce 100644
--- a/libc/arch-mips/syscalls/__sigsuspend.S
+++ b/libc/arch-mips/syscalls/__rt_sigsuspend.S
@@ -1,14 +1,14 @@
 /* autogenerated by gensyscalls.py */
 #include <asm/unistd.h>
     .text
-    .globl __sigsuspend
+    .globl __rt_sigsuspend
     .align 4
-    .ent __sigsuspend
+    .ent __rt_sigsuspend
 
-__sigsuspend:
+__rt_sigsuspend:
     .set noreorder
     .cpload $t9
-    li $v0, __NR_sigsuspend
+    li $v0, __NR_rt_sigsuspend
     syscall
     bnez $a3, 1f
     move $a0, $v0
@@ -19,4 +19,4 @@
     j $t9
     nop
     .set reorder
-    .end __sigsuspend
+    .end __rt_sigsuspend
diff --git a/libc/arch-x86/syscalls.mk b/libc/arch-x86/syscalls.mk
index a704905..10970a6 100644
--- a/libc/arch-x86/syscalls.mk
+++ b/libc/arch-x86/syscalls.mk
@@ -156,11 +156,11 @@
 syscall_src += arch-x86/syscalls/timerfd_settime.S
 syscall_src += arch-x86/syscalls/timerfd_gettime.S
 syscall_src += arch-x86/syscalls/sigaction.S
-syscall_src += arch-x86/syscalls/__sigsuspend.S
+syscall_src += arch-x86/syscalls/sigpending.S
 syscall_src += arch-x86/syscalls/__rt_sigaction.S
 syscall_src += arch-x86/syscalls/__rt_sigprocmask.S
+syscall_src += arch-x86/syscalls/__rt_sigsuspend.S
 syscall_src += arch-x86/syscalls/__rt_sigtimedwait.S
-syscall_src += arch-x86/syscalls/sigpending.S
 syscall_src += arch-x86/syscalls/signalfd4.S
 syscall_src += arch-x86/syscalls/socket.S
 syscall_src += arch-x86/syscalls/bind.S
diff --git a/libc/arch-x86/syscalls/__sigsuspend.S b/libc/arch-x86/syscalls/__rt_sigsuspend.S
similarity index 65%
rename from libc/arch-x86/syscalls/__sigsuspend.S
rename to libc/arch-x86/syscalls/__rt_sigsuspend.S
index 6a5c6aa..32b2134 100644
--- a/libc/arch-x86/syscalls/__sigsuspend.S
+++ b/libc/arch-x86/syscalls/__rt_sigsuspend.S
@@ -3,14 +3,12 @@
 #include <linux/err.h>
 #include <machine/asm.h>
 
-ENTRY(__sigsuspend)
+ENTRY(__rt_sigsuspend)
     pushl   %ebx
     pushl   %ecx
-    pushl   %edx
-    mov     16(%esp), %ebx
-    mov     20(%esp), %ecx
-    mov     24(%esp), %edx
-    movl    $__NR_sigsuspend, %eax
+    mov     12(%esp), %ebx
+    mov     16(%esp), %ecx
+    movl    $__NR_rt_sigsuspend, %eax
     int     $0x80
     cmpl    $-MAX_ERRNO, %eax
     jb      1f
@@ -20,8 +18,7 @@
     addl    $4, %esp
     orl     $-1, %eax
 1:
-    popl    %edx
     popl    %ecx
     popl    %ebx
     ret
-END(__sigsuspend)
+END(__rt_sigsuspend)
diff --git a/libc/arch-x86_64/syscalls.mk b/libc/arch-x86_64/syscalls.mk
index 51c3646..6b72ca0 100644
--- a/libc/arch-x86_64/syscalls.mk
+++ b/libc/arch-x86_64/syscalls.mk
@@ -149,9 +149,9 @@
 syscall_src += arch-x86_64/syscalls/timerfd_create.S
 syscall_src += arch-x86_64/syscalls/timerfd_settime.S
 syscall_src += arch-x86_64/syscalls/timerfd_gettime.S
-syscall_src += arch-x86_64/syscalls/__rt_sigsuspend.S
 syscall_src += arch-x86_64/syscalls/__rt_sigaction.S
 syscall_src += arch-x86_64/syscalls/__rt_sigprocmask.S
+syscall_src += arch-x86_64/syscalls/__rt_sigsuspend.S
 syscall_src += arch-x86_64/syscalls/__rt_sigtimedwait.S
 syscall_src += arch-x86_64/syscalls/signalfd4.S
 syscall_src += arch-x86_64/syscalls/socket.S
diff --git a/libc/arch-x86_64/x86_64.mk b/libc/arch-x86_64/x86_64.mk
index f293b2d..13442c7 100644
--- a/libc/arch-x86_64/x86_64.mk
+++ b/libc/arch-x86_64/x86_64.mk
@@ -9,7 +9,6 @@
     arch-x86_64/bionic/__set_tls.c \
     arch-x86_64/bionic/sigaction.c \
     arch-x86_64/bionic/sigsetjmp.S \
-    arch-x86_64/bionic/sigsuspend.c \
     arch-x86_64/bionic/syscall.S \
 
 _LIBC_ARCH_STATIC_SRC_FILES := \
diff --git a/libc/bionic/signalfd.cpp b/libc/bionic/signalfd.cpp
index b7e6474..36ef81d 100644
--- a/libc/bionic/signalfd.cpp
+++ b/libc/bionic/signalfd.cpp
@@ -28,7 +28,7 @@
 
 #include <sys/signalfd.h>
 
-#include <private/kernel_sigset_t.h>
+#include "private/kernel_sigset_t.h"
 
 extern "C" int signalfd4(int fd, kernel_sigset_t* mask, size_t sizemask, int flags);
 
diff --git a/libc/bionic/sigsuspend.c b/libc/bionic/sigsuspend.c
deleted file mode 100644
index fd08631..0000000
--- a/libc/bionic/sigsuspend.c
+++ /dev/null
@@ -1,43 +0,0 @@
-/*
- * Copyright (C) 2008 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>
-#ifdef __mips__
-extern int __sigsuspend(const sigset_t *);
-#else
-extern int __sigsuspend(int, int, unsigned int);
-#endif
-
-int sigsuspend(const sigset_t *_mask)
-{
-#ifdef __mips__
-        return __sigsuspend(_mask);
-#else
-        unsigned int    mask = (unsigned int)*_mask;
-        return __sigsuspend(0, 0, mask);
-#endif
-}
diff --git a/libc/arch-x86_64/bionic/sigsuspend.c b/libc/bionic/sigsuspend.cpp
similarity index 82%
rename from libc/arch-x86_64/bionic/sigsuspend.c
rename to libc/bionic/sigsuspend.cpp
index 40bfaea..fb846b8 100644
--- a/libc/arch-x86_64/bionic/sigsuspend.c
+++ b/libc/bionic/sigsuspend.cpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2013 The Android Open Source Project
+ * Copyright (C) 2008 The Android Open Source Project
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -28,8 +28,11 @@
 
 #include <signal.h>
 
-extern int __rt_sigsuspend(const sigset_t*, size_t);
+#include "private/kernel_sigset_t.h"
 
-int sigsuspend(const sigset_t* set) {
-  return __rt_sigsuspend(set, sizeof(sigset_t));
+extern "C" int __rt_sigsuspend(const kernel_sigset_t*, size_t);
+
+int sigsuspend(const sigset_t* bionic_set) {
+  kernel_sigset_t set(bionic_set);
+  return __rt_sigsuspend(&set, sizeof(set));
 }
diff --git a/libc/bionic/sigwait.cpp b/libc/bionic/sigwait.cpp
index 1546fd6..873a368 100644
--- a/libc/bionic/sigwait.cpp
+++ b/libc/bionic/sigwait.cpp
@@ -31,7 +31,7 @@
 #include <string.h>
 #include <time.h>
 
-#include <private/kernel_sigset_t.h>
+#include "private/kernel_sigset_t.h"
 
 extern "C" int __rt_sigtimedwait(const sigset_t* uthese, siginfo_t* uinfo, const struct timespec* uts, size_t sigsetsize);
 
diff --git a/tests/signal_test.cpp b/tests/signal_test.cpp
index e41dc1c..70e9017 100644
--- a/tests/signal_test.cpp
+++ b/tests/signal_test.cpp
@@ -153,3 +153,36 @@
   ASSERT_EQ(0, errno);
   ASSERT_EQ(SIGALRM, received_signal);
 }
+
+static int gSigSuspendTestHelperCallCount = 0;
+
+static void SigSuspendTestHelper(int) {
+  ++gSigSuspendTestHelperCallCount;
+}
+
+TEST(signal, sigsuspend) {
+  ScopedSignalHandler ssh(SIGALRM, SigSuspendTestHelper);
+
+  // Block SIGALRM.
+  sigset_t just_SIGALRM;
+  sigemptyset(&just_SIGALRM);
+  sigaddset(&just_SIGALRM, SIGALRM);
+  sigset_t original_set;
+  ASSERT_EQ(0, sigprocmask(SIG_BLOCK, &just_SIGALRM, &original_set));
+
+  // Raise SIGALRM and check our signal handler wasn't called.
+  raise(SIGALRM);
+  ASSERT_EQ(0, gSigSuspendTestHelperCallCount);
+
+  // Use sigsuspend to block everything except SIGALRM...
+  sigset_t not_SIGALRM;
+  sigfillset(&not_SIGALRM);
+  sigdelset(&not_SIGALRM, SIGALRM);
+  ASSERT_EQ(-1, sigsuspend(&not_SIGALRM));
+  ASSERT_EQ(EINTR, errno);
+  // ...and check that we now receive our pending SIGALRM.
+  ASSERT_EQ(1, gSigSuspendTestHelperCallCount);
+
+  // Restore the original set.
+  assert(0 == sigprocmask(SIG_SETMASK, &original_set, NULL));
+}