Merge "Sync with upstream OpenBSD fts.c."
diff --git a/libc/arch-arm/include/machine/endian.h b/libc/arch-arm/include/machine/endian.h
index 8d9723d..04bba20 100644
--- a/libc/arch-arm/include/machine/endian.h
+++ b/libc/arch-arm/include/machine/endian.h
@@ -67,11 +67,7 @@
 
 #endif  /* __GNUC__ */
 
-#if defined(__ARMEB__)
-#define _BYTE_ORDER _BIG_ENDIAN
-#else
 #define _BYTE_ORDER _LITTLE_ENDIAN
-#endif
 #define __STRICT_ALIGNMENT
 #include <sys/types.h>
 #include <sys/endian.h>
diff --git a/libc/arch-arm64/include/machine/endian.h b/libc/arch-arm64/include/machine/endian.h
index 87a038d..4743733 100644
--- a/libc/arch-arm64/include/machine/endian.h
+++ b/libc/arch-arm64/include/machine/endian.h
@@ -29,9 +29,6 @@
 #ifndef _AARCH64_ENDIAN_H_
 #define _AARCH64_ENDIAN_H_
 
-#include <sys/types.h>
-#include <sys/endian.h>
-
 #ifdef __GNUC__
 
 #define __swap16md(x) ({                                        \
@@ -49,10 +46,8 @@
 
 #endif  /* __GNUC__ */
 
-#if defined(__AARCH64EB__)
-#define _BYTE_ORDER _BIG_ENDIAN
-#else
 #define _BYTE_ORDER _LITTLE_ENDIAN
-#endif
+#include <sys/types.h>
+#include <sys/endian.h>
 
 #endif /* _AARCH64_ENDIAN_H_ */
diff --git a/libc/arch-mips/bionic/atexit.h b/libc/arch-mips/bionic/atexit.h
deleted file mode 100644
index 759008c..0000000
--- a/libc/arch-mips/bionic/atexit.h
+++ /dev/null
@@ -1,36 +0,0 @@
-/*
- * 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.
- */
-
-extern void *__dso_handle;
-extern int __cxa_atexit(void (*func)(void *), void *arg, void *dso);
-
-__attribute__ ((visibility ("hidden")))
-int atexit(void (*func)(void))
-{
-  return (__cxa_atexit((void (*)(void *))func, (void *)0, &__dso_handle));
-}
diff --git a/libc/arch-mips/bionic/crtbegin.c b/libc/arch-mips/bionic/crtbegin.c
index 28e8817..50e9eeb 100644
--- a/libc/arch-mips/bionic/crtbegin.c
+++ b/libc/arch-mips/bionic/crtbegin.c
@@ -91,4 +91,4 @@
 );
 
 #include "../../arch-common/bionic/__dso_handle.h"
-#include "atexit.h"
+#include "../../arch-common/bionic/atexit.h"
diff --git a/libc/arch-mips/include/machine/endian.h b/libc/arch-mips/include/machine/endian.h
index 41a9004..9270e9d 100644
--- a/libc/arch-mips/include/machine/endian.h
+++ b/libc/arch-mips/include/machine/endian.h
@@ -58,11 +58,7 @@
 #endif  /* __mips32r2__ */
 #endif  /* __GNUC__ */
 
-#if defined(__MIPSEB__)
-#define _BYTE_ORDER _BIG_ENDIAN
-#else
 #define _BYTE_ORDER _LITTLE_ENDIAN
-#endif
 #define __STRICT_ALIGNMENT
 #include <sys/types.h>
 #include <sys/endian.h>
diff --git a/libc/arch-mips64/bionic/atexit.h b/libc/arch-mips64/bionic/atexit.h
deleted file mode 100644
index 759008c..0000000
--- a/libc/arch-mips64/bionic/atexit.h
+++ /dev/null
@@ -1,36 +0,0 @@
-/*
- * 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.
- */
-
-extern void *__dso_handle;
-extern int __cxa_atexit(void (*func)(void *), void *arg, void *dso);
-
-__attribute__ ((visibility ("hidden")))
-int atexit(void (*func)(void))
-{
-  return (__cxa_atexit((void (*)(void *))func, (void *)0, &__dso_handle));
-}
diff --git a/libc/arch-mips64/bionic/crtbegin.c b/libc/arch-mips64/bionic/crtbegin.c
index 2ea31ad..1374fea 100644
--- a/libc/arch-mips64/bionic/crtbegin.c
+++ b/libc/arch-mips64/bionic/crtbegin.c
@@ -91,4 +91,4 @@
 );
 
 #include "../../arch-common/bionic/__dso_handle.h"
-#include "atexit.h"
+#include "../../arch-common/bionic/atexit.h"
diff --git a/libc/arch-mips64/bionic/crtbegin_so.c b/libc/arch-mips64/bionic/crtbegin_so.c
deleted file mode 100644
index d664ce6..0000000
--- a/libc/arch-mips64/bionic/crtbegin_so.c
+++ /dev/null
@@ -1,38 +0,0 @@
-/*
- * 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.
- */
-
-extern void __cxa_finalize(void *);
-extern void *__dso_handle;
-
-__attribute__((visibility("hidden"),destructor))
-void __on_dlclose() {
-  __cxa_finalize(&__dso_handle);
-}
-
-#include "../../arch-common/bionic/__dso_handle_so.h"
-#include "atexit.h"
diff --git a/libc/arch-mips64/include/machine/endian.h b/libc/arch-mips64/include/machine/endian.h
index 41a9004..9270e9d 100644
--- a/libc/arch-mips64/include/machine/endian.h
+++ b/libc/arch-mips64/include/machine/endian.h
@@ -58,11 +58,7 @@
 #endif  /* __mips32r2__ */
 #endif  /* __GNUC__ */
 
-#if defined(__MIPSEB__)
-#define _BYTE_ORDER _BIG_ENDIAN
-#else
 #define _BYTE_ORDER _LITTLE_ENDIAN
-#endif
 #define __STRICT_ALIGNMENT
 #include <sys/types.h>
 #include <sys/endian.h>
diff --git a/libc/arch-x86_64/include/machine/endian.h b/libc/arch-x86_64/include/machine/endian.h
index 7889a37..8a3b0c5 100644
--- a/libc/arch-x86_64/include/machine/endian.h
+++ b/libc/arch-x86_64/include/machine/endian.h
@@ -56,6 +56,7 @@
 #endif	/* __GNUC__ */
 
 #define _BYTE_ORDER _LITTLE_ENDIAN
+#include <sys/types.h>
 #include <sys/endian.h>
 
 #endif /* _MACHINE_ENDIAN_H_ */
diff --git a/libc/bionic/locale.cpp b/libc/bionic/locale.cpp
index 681e038..90aa7b8 100644
--- a/libc/bionic/locale.cpp
+++ b/libc/bionic/locale.cpp
@@ -26,6 +26,7 @@
  * SUCH DAMAGE.
  */
 
+#include <errno.h>
 #include <locale.h>
 #include <pthread.h>
 #include <stdlib.h>
diff --git a/libc/bionic/mntent.cpp b/libc/bionic/mntent.cpp
index f5a8eaa..4afacda 100644
--- a/libc/bionic/mntent.cpp
+++ b/libc/bionic/mntent.cpp
@@ -27,6 +27,7 @@
  */
 
 #include <mntent.h>
+#include <string.h>
 
 #include "private/ThreadLocalBuffer.h"
 
diff --git a/libc/bionic/pthread_atfork.cpp b/libc/bionic/pthread_atfork.cpp
index b845f7d..82e2b59 100644
--- a/libc/bionic/pthread_atfork.cpp
+++ b/libc/bionic/pthread_atfork.cpp
@@ -28,6 +28,7 @@
 
 #include <errno.h>
 #include <pthread.h>
+#include <stdlib.h>
 
 struct atfork_t {
   atfork_t* next;
diff --git a/libc/bionic/pthread_internals.cpp b/libc/bionic/pthread_internals.cpp
index 19c00d4..2270d96 100644
--- a/libc/bionic/pthread_internals.cpp
+++ b/libc/bionic/pthread_internals.cpp
@@ -28,6 +28,8 @@
 
 #include "pthread_internal.h"
 
+#include <stdlib.h>
+
 #include "private/bionic_futex.h"
 #include "private/bionic_tls.h"
 #include "private/ScopedPthreadMutexLocker.h"
diff --git a/libc/bionic/pthread_key.cpp b/libc/bionic/pthread_key.cpp
index 27eab27..b47ef22 100644
--- a/libc/bionic/pthread_key.cpp
+++ b/libc/bionic/pthread_key.cpp
@@ -26,6 +26,7 @@
  * SUCH DAMAGE.
  */
 
+#include <errno.h>
 #include <pthread.h>
 
 #include "private/bionic_tls.h"
diff --git a/libc/bionic/raise.cpp b/libc/bionic/raise.cpp
index f69d90b..0dd0ad7 100644
--- a/libc/bionic/raise.cpp
+++ b/libc/bionic/raise.cpp
@@ -27,6 +27,7 @@
  */
 
 #include <pthread.h>
+#include <signal.h>
 
 int raise(int sig) {
   int rc = pthread_kill(pthread_self(), sig);
diff --git a/libc/arch-mips/bionic/crtbegin_so.c b/libc/include/machine/pthread_types.h
similarity index 75%
rename from libc/arch-mips/bionic/crtbegin_so.c
rename to libc/include/machine/pthread_types.h
index d664ce6..900541c 100644
--- a/libc/arch-mips/bionic/crtbegin_so.c
+++ b/libc/include/machine/pthread_types.h
@@ -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
@@ -26,13 +26,23 @@
  * SUCH DAMAGE.
  */
 
-extern void __cxa_finalize(void *);
-extern void *__dso_handle;
+#ifndef _MACHINE_PTHREAD_TYPES_H_
+#define _MACHINE_PTHREAD_TYPES_H_
 
-__attribute__((visibility("hidden"),destructor))
-void __on_dlclose() {
-  __cxa_finalize(&__dso_handle);
-}
+#include <sys/types.h>
 
-#include "../../arch-common/bionic/__dso_handle_so.h"
-#include "atexit.h"
+typedef long pthread_t;
+
+typedef struct {
+  uint32_t flags;
+  void* stack_base;
+  size_t stack_size;
+  size_t guard_size;
+  int32_t sched_policy;
+  int32_t sched_priority;
+#ifdef __LP64__
+  char __reserved[16];
+#endif
+} pthread_attr_t;
+
+#endif /* _MACHINE_PTHREAD_TYPES_H_ */
diff --git a/libc/include/pthread.h b/libc/include/pthread.h
index 86a1005..24dba1b 100644
--- a/libc/include/pthread.h
+++ b/libc/include/pthread.h
@@ -29,11 +29,12 @@
 #ifndef _PTHREAD_H_
 #define _PTHREAD_H_
 
-#include <time.h>
-#include <signal.h>
-#include <sched.h>
 #include <limits.h>
+#include <machine/pthread_types.h>
+#include <sched.h>
+#include <sys/cdefs.h>
 #include <sys/types.h>
+#include <time.h>
 
 #if defined(__LP64__)
   #define __RESERVED_INITIALIZER , {0}
@@ -76,18 +77,6 @@
 
 #define PTHREAD_COND_INITIALIZER  {0 __RESERVED_INITIALIZER}
 
-typedef struct {
-  uint32_t flags;
-  void* stack_base;
-  size_t stack_size;
-  size_t guard_size;
-  int32_t sched_policy;
-  int32_t sched_priority;
-#ifdef __LP64__
-  char __reserved[16];
-#endif
-} pthread_attr_t;
-
 typedef long pthread_mutexattr_t;
 typedef long pthread_condattr_t;
 
@@ -118,7 +107,6 @@
 #endif
 
 typedef int pthread_key_t;
-typedef long pthread_t;
 
 typedef volatile int pthread_once_t;
 
@@ -195,8 +183,6 @@
 int pthread_key_create(pthread_key_t*, void (*)(void*)) __nonnull((1));
 int pthread_key_delete(pthread_key_t);
 
-int pthread_kill(pthread_t, int);
-
 int pthread_mutexattr_destroy(pthread_mutexattr_t*) __nonnull((1));
 int pthread_mutexattr_getpshared(const pthread_mutexattr_t*, int*) __nonnull((1, 2));
 int pthread_mutexattr_gettype(const pthread_mutexattr_t*, int*) __nonnull((1, 2));
@@ -236,8 +222,6 @@
 
 int pthread_setspecific(pthread_key_t, const void*);
 
-int pthread_sigmask(int, const sigset_t*, sigset_t*);
-
 typedef void (*__pthread_cleanup_func_t)(void*);
 
 typedef struct __pthread_cleanup_t {
diff --git a/libc/include/sched.h b/libc/include/sched.h
index 6155ab7..4f9e2a6 100644
--- a/libc/include/sched.h
+++ b/libc/include/sched.h
@@ -39,9 +39,8 @@
 #define SCHED_OTHER SCHED_NORMAL
 
 struct sched_param {
-  int __sched_priority;
+  int sched_priority;
 };
-#define sched_priority __sched_priority
 
 extern int sched_setscheduler(pid_t, int, const struct sched_param*);
 extern int sched_getscheduler(pid_t);
diff --git a/libc/include/signal.h b/libc/include/signal.h
index e23e65b..6d89ef7 100644
--- a/libc/include/signal.h
+++ b/libc/include/signal.h
@@ -29,12 +29,13 @@
 #ifndef _SIGNAL_H_
 #define _SIGNAL_H_
 
-#include <errno.h>
-#include <sys/cdefs.h>
-#include <limits.h>		/* For LONG_BIT */
-#include <string.h>		/* For memset() */
-#include <sys/types.h>
 #include <asm/sigcontext.h>
+#include <errno.h>
+#include <limits.h>
+#include <machine/pthread_types.h>
+#include <string.h>
+#include <sys/cdefs.h>
+#include <sys/types.h>
 
 #if defined(__LP64__) || defined(__mips__)
 /* For 64-bit (and mips), the kernel's struct sigaction doesn't match the POSIX one,
@@ -129,6 +130,9 @@
 extern void psiginfo(const siginfo_t*, const char*);
 extern void psignal(int, const char*);
 
+extern int pthread_kill(pthread_t, int);
+extern int pthread_sigmask(int, const sigset_t*, sigset_t*);
+
 __END_DECLS
 
 #endif /* _SIGNAL_H_ */
diff --git a/libc/private/kernel_sigset_t.h b/libc/private/kernel_sigset_t.h
index b2d6386..9415fcf 100644
--- a/libc/private/kernel_sigset_t.h
+++ b/libc/private/kernel_sigset_t.h
@@ -17,6 +17,8 @@
 #ifndef LIBC_PRIVATE_KERNEL_SIGSET_T_H_
 #define LIBC_PRIVATE_KERNEL_SIGSET_T_H_
 
+#include <signal.h>
+
 // Our sigset_t is wrong for ARM and x86. It's 32-bit but the kernel expects 64 bits.
 // This means we can't support real-time signals correctly until we can change the ABI.
 // In the meantime, we can use this union to pass an appropriately-sized block of memory
diff --git a/tests/gtest_ex.h b/tests/gtest_ex.h
new file mode 100644
index 0000000..fe1d894
--- /dev/null
+++ b/tests/gtest_ex.h
@@ -0,0 +1,40 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <gtest/gtest.h>
+
+#include <sys/types.h>
+#include <sys/wait.h>
+
+#include <errno.h>
+#include <string.h>
+#include <unistd.h>
+
+template<typename F>
+void test_isolated(F test) {
+  int pid = fork();
+  ASSERT_NE(-1, pid) << strerror(errno);
+
+  if (pid == 0) {
+    test();
+    _exit(testing::Test::HasFailure() ? 1 : 0);
+  }
+
+  int status;
+  ASSERT_EQ(pid, waitpid(pid, &status, 0));
+  ASSERT_TRUE(WIFEXITED(status));
+  ASSERT_EQ(0, WEXITSTATUS(status)) << "Forked test has failed, see above..";
+}
diff --git a/tests/pthread_test.cpp b/tests/pthread_test.cpp
index b5e12a5..797468e 100644
--- a/tests/pthread_test.cpp
+++ b/tests/pthread_test.cpp
@@ -19,6 +19,7 @@
 #include "private/ScopeGuard.h"
 #include "BionicDeathTest.h"
 #include "ScopedSignalHandler.h"
+#include "gtest_ex.h"
 
 #include <errno.h>
 #include <inttypes.h>
@@ -721,22 +722,24 @@
 static void AtForkChild1() { g_atfork_child_calls = (g_atfork_child_calls << 4) | 1; }
 static void AtForkChild2() { g_atfork_child_calls = (g_atfork_child_calls << 4) | 2; }
 
-TEST(pthread, pthread_atfork) {
-  ASSERT_EQ(0, pthread_atfork(AtForkPrepare1, AtForkParent1, AtForkChild1));
-  ASSERT_EQ(0, pthread_atfork(AtForkPrepare2, AtForkParent2, AtForkChild2));
+TEST(pthread, pthread_atfork_smoke) {
+  test_isolated([] {
+    ASSERT_EQ(0, pthread_atfork(AtForkPrepare1, AtForkParent1, AtForkChild1));
+    ASSERT_EQ(0, pthread_atfork(AtForkPrepare2, AtForkParent2, AtForkChild2));
 
-  int pid = fork();
-  ASSERT_NE(-1, pid) << strerror(errno);
+    int pid = fork();
+    ASSERT_NE(-1, pid) << strerror(errno);
 
-  // Child and parent calls are made in the order they were registered.
-  if (pid == 0) {
-    ASSERT_EQ(0x12, g_atfork_child_calls);
-    _exit(0);
-  }
-  ASSERT_EQ(0x12, g_atfork_parent_calls);
+    // Child and parent calls are made in the order they were registered.
+    if (pid == 0) {
+      ASSERT_EQ(0x12, g_atfork_child_calls);
+      _exit(0);
+    }
+    ASSERT_EQ(0x12, g_atfork_parent_calls);
 
-  // Prepare calls are made in the reverse order.
-  ASSERT_EQ(0x21, g_atfork_prepare_calls);
+    // Prepare calls are made in the reverse order.
+    ASSERT_EQ(0x21, g_atfork_prepare_calls);
+  });
 }
 
 TEST(pthread, pthread_attr_getscope) {