Remove dependencies on obsolete __ARCH_WANT_SYSCALL_DEPRECATED system calls.

(aarch64 kernels don't have these system calls.)

Change-Id: I6f64075aa412f71520f2df71c3d69b647f91c1ca
diff --git a/libc/bionic/pselect.c b/libc/bionic/bionic_time_conversions.cpp
similarity index 60%
copy from libc/bionic/pselect.c
copy to libc/bionic/bionic_time_conversions.cpp
index 76ce2c0..7f3c026 100644
--- a/libc/bionic/pselect.c
+++ b/libc/bionic/bionic_time_conversions.cpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2008 The Android Open Source Project
+ * Copyright (C) 2013 The Android Open Source Project
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -25,35 +25,27 @@
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  */
-#include <sys/select.h>
-#include <signal.h>
-#include <pthread.h>
 
-int
-pselect(int  n, fd_set*  readfds, fd_set*  writefds, fd_set*  errfds,
-        const struct timespec*  timeout, const sigset_t*  sigmask)
-{
-    sigset_t        oldmask;
-    int             result;
-    struct timeval  tv, *tv_timeout = NULL;
+#include "private/bionic_time_conversions.h"
 
-    if (sigmask != NULL)
-        pthread_sigmask( SIG_SETMASK, sigmask, &oldmask );
+bool timespec_from_timeval(timespec& ts, const timeval& tv) {
+  // Whole seconds can just be copied.
+  ts.tv_sec = tv.tv_sec;
 
-    if (timeout != NULL) {
-        tv_timeout = &tv;
-        tv.tv_sec  = timeout->tv_sec;
-        tv.tv_usec = (timeout->tv_nsec + 999)/1000;  // round up
-        if (tv.tv_usec >= 1000000) {
-            tv.tv_sec  += 1;
-            tv.tv_usec -= 1000000;
-        }
-    }
+  // But we might overflow when converting microseconds to nanoseconds.
+  if (tv.tv_usec >= 1000000 || tv.tv_usec < 0) {
+    return false;
+  }
+  ts.tv_nsec = tv.tv_usec * 1000;
+  return true;
+}
 
-    result = select( n, readfds, writefds, errfds, tv_timeout );
+void timespec_from_ms(timespec& ts, const int ms) {
+  ts.tv_sec = ms / 1000;
+  ts.tv_nsec = (ms % 1000) * 1000000;
+}
 
-    if (sigmask != NULL)
-        pthread_sigmask( SIG_SETMASK, &oldmask, NULL );
-
-    return result;
+void timeval_from_timespec(timeval& tv, const timespec& ts) {
+  tv.tv_sec = ts.tv_sec;
+  tv.tv_usec = ts.tv_nsec / 1000;
 }
diff --git a/libc/bionic/pselect.c b/libc/bionic/epoll_pwait.cpp
similarity index 60%
rename from libc/bionic/pselect.c
rename to libc/bionic/epoll_pwait.cpp
index 76ce2c0..f3af93e 100644
--- a/libc/bionic/pselect.c
+++ b/libc/bionic/epoll_pwait.cpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2008 The Android Open Source Project
+ * Copyright (C) 2013 The Android Open Source Project
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -25,35 +25,19 @@
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  */
-#include <sys/select.h>
-#include <signal.h>
-#include <pthread.h>
 
-int
-pselect(int  n, fd_set*  readfds, fd_set*  writefds, fd_set*  errfds,
-        const struct timespec*  timeout, const sigset_t*  sigmask)
-{
-    sigset_t        oldmask;
-    int             result;
-    struct timeval  tv, *tv_timeout = NULL;
+#include <sys/epoll.h>
 
-    if (sigmask != NULL)
-        pthread_sigmask( SIG_SETMASK, sigmask, &oldmask );
+#include "private/kernel_sigset_t.h"
 
-    if (timeout != NULL) {
-        tv_timeout = &tv;
-        tv.tv_sec  = timeout->tv_sec;
-        tv.tv_usec = (timeout->tv_nsec + 999)/1000;  // round up
-        if (tv.tv_usec >= 1000000) {
-            tv.tv_sec  += 1;
-            tv.tv_usec -= 1000000;
-        }
-    }
+extern "C" int __epoll_pwait(int, epoll_event*, int, int, const kernel_sigset_t*, size_t);
 
-    result = select( n, readfds, writefds, errfds, tv_timeout );
-
-    if (sigmask != NULL)
-        pthread_sigmask( SIG_SETMASK, &oldmask, NULL );
-
-    return result;
+int epoll_pwait(int fd, epoll_event* events, int max_events, int timeout, const sigset_t* ss) {
+  kernel_sigset_t kernel_ss;
+  kernel_sigset_t* kernel_ss_ptr = NULL;
+  if (ss != NULL) {
+    kernel_ss.set(ss);
+    kernel_ss_ptr = &kernel_ss;
+  }
+  return __epoll_pwait(fd, events, max_events, timeout, kernel_ss_ptr, sizeof(kernel_ss));
 }
diff --git a/libc/bionic/pselect.c b/libc/bionic/epoll_wait.cpp
similarity index 60%
copy from libc/bionic/pselect.c
copy to libc/bionic/epoll_wait.cpp
index 76ce2c0..deb19da 100644
--- a/libc/bionic/pselect.c
+++ b/libc/bionic/epoll_wait.cpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2008 The Android Open Source Project
+ * Copyright (C) 2013 The Android Open Source Project
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -25,35 +25,9 @@
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  */
-#include <sys/select.h>
-#include <signal.h>
-#include <pthread.h>
 
-int
-pselect(int  n, fd_set*  readfds, fd_set*  writefds, fd_set*  errfds,
-        const struct timespec*  timeout, const sigset_t*  sigmask)
-{
-    sigset_t        oldmask;
-    int             result;
-    struct timeval  tv, *tv_timeout = NULL;
+#include <sys/epoll.h>
 
-    if (sigmask != NULL)
-        pthread_sigmask( SIG_SETMASK, sigmask, &oldmask );
-
-    if (timeout != NULL) {
-        tv_timeout = &tv;
-        tv.tv_sec  = timeout->tv_sec;
-        tv.tv_usec = (timeout->tv_nsec + 999)/1000;  // round up
-        if (tv.tv_usec >= 1000000) {
-            tv.tv_sec  += 1;
-            tv.tv_usec -= 1000000;
-        }
-    }
-
-    result = select( n, readfds, writefds, errfds, tv_timeout );
-
-    if (sigmask != NULL)
-        pthread_sigmask( SIG_SETMASK, &oldmask, NULL );
-
-    return result;
+int epoll_wait(int fd, struct epoll_event* events, int max_events, int timeout) {
+  return epoll_pwait(fd, events, max_events, timeout, NULL);
 }
diff --git a/libc/bionic/fork.cpp b/libc/bionic/fork.cpp
index a3bea20..339a0e8 100644
--- a/libc/bionic/fork.cpp
+++ b/libc/bionic/fork.cpp
@@ -31,7 +31,7 @@
 
 #include "private/bionic_pthread.h"
 
-extern "C" int __fork();
+extern "C" int __clone(int, void*, int*, void*, int*);
 
 int fork() {
   // POSIX mandates that the timers of a fork child process be
@@ -41,7 +41,7 @@
   __timer_table_start_stop(1);
   __bionic_atfork_run_prepare();
 
-  int result = __fork();
+  int result = __clone(SIGCHLD, NULL, NULL, NULL, NULL);
   if (result != 0) {  // Not a child process.
     __timer_table_start_stop(0);
     __bionic_atfork_run_parent();
diff --git a/libc/bionic/pselect.c b/libc/bionic/pause.cpp
similarity index 60%
copy from libc/bionic/pselect.c
copy to libc/bionic/pause.cpp
index 76ce2c0..94a16fb 100644
--- a/libc/bionic/pselect.c
+++ b/libc/bionic/pause.cpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2008 The Android Open Source Project
+ * Copyright (C) 2013 The Android Open Source Project
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -25,35 +25,18 @@
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  */
-#include <sys/select.h>
-#include <signal.h>
-#include <pthread.h>
 
-int
-pselect(int  n, fd_set*  readfds, fd_set*  writefds, fd_set*  errfds,
-        const struct timespec*  timeout, const sigset_t*  sigmask)
-{
-    sigset_t        oldmask;
-    int             result;
-    struct timeval  tv, *tv_timeout = NULL;
+#include <unistd.h>
 
-    if (sigmask != NULL)
-        pthread_sigmask( SIG_SETMASK, sigmask, &oldmask );
+#include "private/kernel_sigset_t.h"
 
-    if (timeout != NULL) {
-        tv_timeout = &tv;
-        tv.tv_sec  = timeout->tv_sec;
-        tv.tv_usec = (timeout->tv_nsec + 999)/1000;  // round up
-        if (tv.tv_usec >= 1000000) {
-            tv.tv_sec  += 1;
-            tv.tv_usec -= 1000000;
-        }
-    }
+extern "C" int __rt_sigprocmask(int, const kernel_sigset_t*, kernel_sigset_t*, size_t);
+extern "C" int __rt_sigsuspend(const kernel_sigset_t*, size_t);
 
-    result = select( n, readfds, writefds, errfds, tv_timeout );
-
-    if (sigmask != NULL)
-        pthread_sigmask( SIG_SETMASK, &oldmask, NULL );
-
-    return result;
+int pause() {
+  kernel_sigset_t mask;
+  if (__rt_sigprocmask(SIG_SETMASK, NULL, &mask, sizeof(mask)) == -1) {
+    return -1;
+  }
+  return __rt_sigsuspend(&mask, sizeof(mask));
 }
diff --git a/libc/bionic/poll.cpp b/libc/bionic/poll.cpp
new file mode 100644
index 0000000..ebb318d
--- /dev/null
+++ b/libc/bionic/poll.cpp
@@ -0,0 +1,110 @@
+/*
+ * Copyright (C) 2013 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 <sys/poll.h>
+#include <sys/select.h>
+
+#include "private/bionic_time_conversions.h"
+#include "private/kernel_sigset_t.h"
+
+extern "C" int __ppoll(pollfd*, unsigned int, timespec*, const kernel_sigset_t*, size_t);
+extern "C" int __pselect6(int, fd_set*, fd_set*, fd_set*, timespec*, void*);
+
+int poll(pollfd* fds, nfds_t fd_count, int ms) {
+  timespec ts;
+  timespec* ts_ptr = NULL;
+  if (ms >= 0) {
+    timespec_from_ms(ts, ms);
+    ts_ptr = &ts;
+  }
+  return __ppoll(fds, fd_count, ts_ptr, NULL, 0);
+}
+
+int ppoll(pollfd* fds, nfds_t fd_count, const timespec* ts, const sigset_t* ss) {
+  timespec mutable_ts;
+  timespec* mutable_ts_ptr = NULL;
+  if (ts != NULL) {
+    mutable_ts = *ts;
+    mutable_ts_ptr = &mutable_ts;
+  }
+
+  kernel_sigset_t kernel_ss;
+  kernel_sigset_t* kernel_ss_ptr = NULL;
+  if (ss != NULL) {
+    kernel_ss.set(ss);
+    kernel_ss_ptr = &kernel_ss;
+  }
+
+  return __ppoll(fds, fd_count, mutable_ts_ptr, kernel_ss_ptr, sizeof(kernel_ss));
+}
+
+int select(int fd_count, fd_set* read_fds, fd_set* write_fds, fd_set* error_fds, timeval* tv) {
+  timespec ts;
+  timespec* ts_ptr = NULL;
+  if (tv != NULL) {
+    if (!timespec_from_timeval(ts, *tv)) {
+      errno = EINVAL;
+      return -1;
+    }
+    ts_ptr = &ts;
+  }
+  int result = __pselect6(fd_count, read_fds, write_fds, error_fds, ts_ptr, NULL);
+  if (tv != NULL) {
+    timeval_from_timespec(*tv, ts);
+  }
+  return result;
+}
+
+int pselect(int fd_count, fd_set* read_fds, fd_set* write_fds, fd_set* error_fds,
+            const timespec* ts, const sigset_t* ss) {
+  timespec mutable_ts;
+  timespec* mutable_ts_ptr = NULL;
+  if (ts != NULL) {
+    mutable_ts = *ts;
+    mutable_ts_ptr = &mutable_ts;
+  }
+
+  kernel_sigset_t kernel_ss;
+  kernel_sigset_t* kernel_ss_ptr = NULL;
+  if (ss != NULL) {
+    kernel_ss.set(ss);
+    kernel_ss_ptr = &kernel_ss;
+  }
+
+  // The Linux kernel only handles 6 arguments and this system call really needs 7,
+  // so the last argument is a void* pointing to:
+  struct pselect6_extra_data_t {
+    uintptr_t ss_addr;
+    size_t ss_len;
+  };
+  pselect6_extra_data_t extra_data;
+  extra_data.ss_addr = reinterpret_cast<uintptr_t>(kernel_ss_ptr);
+  extra_data.ss_len = sizeof(kernel_ss);
+
+  return __pselect6(fd_count, read_fds, write_fds, error_fds, mutable_ts_ptr, &extra_data);
+}
diff --git a/libc/bionic/pthread.c b/libc/bionic/pthread.c
index 7081445..4a4676a 100644
--- a/libc/bionic/pthread.c
+++ b/libc/bionic/pthread.c
@@ -1176,13 +1176,10 @@
 
 int pthread_cond_timeout_np(pthread_cond_t *cond,
                             pthread_mutex_t * mutex,
-                            unsigned msecs)
+                            unsigned ms)
 {
     struct timespec ts;
-
-    ts.tv_sec = msecs / 1000;
-    ts.tv_nsec = (msecs % 1000) * 1000000;
-
+    timespec_from_ms(ts, ms);
     return __pthread_cond_timedwait_relative(cond, mutex, &ts);
 }
 
diff --git a/libc/bionic/utimes.cpp b/libc/bionic/utimes.cpp
index 315765a..8950972 100644
--- a/libc/bionic/utimes.cpp
+++ b/libc/bionic/utimes.cpp
@@ -30,21 +30,13 @@
 #include <sys/stat.h>
 #include <sys/time.h>
 
+#include "private/bionic_time_conversions.h"
+
 int utimes(const char* path, const timeval tv[2]) {
   timespec ts[2];
-
-  // Whole seconds can just be copied.
-  ts[0].tv_sec = tv[0].tv_sec;
-  ts[1].tv_sec = tv[1].tv_sec;
-
-  // But we might overflow when converting microseconds to nanoseconds.
-  if (tv[0].tv_usec >= 1000000 || tv[0].tv_usec < 0 ||
-      tv[1].tv_usec >= 1000000 || tv[1].tv_usec < 0) {
+  if (!timespec_from_timeval(ts[0], tv[0]) || !timespec_from_timeval(ts[1], tv[1])) {
     errno = EINVAL;
     return -1;
   }
-  ts[0].tv_nsec = tv[0].tv_usec * 1000;
-  ts[1].tv_nsec = tv[1].tv_usec * 1000;
-
   return utimensat(AT_FDCWD, path, ts, 0);
 }
diff --git a/libc/bionic/wait.cpp b/libc/bionic/wait.cpp
index 7dbcec2..27453bb 100644
--- a/libc/bionic/wait.cpp
+++ b/libc/bionic/wait.cpp
@@ -44,7 +44,7 @@
 }
 
 int waitid(idtype_t which, id_t id, siginfo_t* info, int options) {
-  /* the system call takes an option struct rusage that we don't need */
+  // The system call takes an optional struct rusage that we don't need.
   return __waitid(which, id, info, options, NULL);
 }