Merge "MIPS support to sigsuspend and sigwait routines"
diff --git a/libc/Android.mk b/libc/Android.mk
index fd81d7e..88225d6 100644
--- a/libc/Android.mk
+++ b/libc/Android.mk
@@ -209,7 +209,6 @@
 	string/strstr.c \
 	string/strtok.c \
 	string/strtotimeval.c \
-	string/strxfrm.c \
 	string/__memcpy_chk.c \
 	string/__memmove_chk.c \
 	string/__memset_chk.c \
@@ -338,6 +337,9 @@
 	regex/regexec.c \
 	regex/regfree.c \
 
+libc_upstream_netbsd_src_files := \
+	upstream-netbsd/libc/string/strxfrm.c \
+
 # The following files are common, but must be compiled
 # with different C flags when building a static C library.
 #
@@ -677,18 +679,39 @@
 
 
 # ========================================================
+# libc_netbsd.a - upstream NetBSD C library code
+# ========================================================
+#
+# These files are built with the netbsd-compat.h header file
+# automatically included.
+
+include $(CLEAR_VARS)
+
+LOCAL_SRC_FILES := $(libc_upstream_netbsd_src_files)
+LOCAL_CFLAGS := \
+    $(libc_common_cflags) \
+    -include upstream-netbsd/netbsd-compat.h
+LOCAL_C_INCLUDES := $(libc_common_c_includes)
+LOCAL_MODULE := libc_netbsd
+LOCAL_SYSTEM_SHARED_LIBRARIES :=
+
+include $(BUILD_STATIC_LIBRARY)
+
+
+# ========================================================
 # libc_common.a
 # ========================================================
+
 include $(CLEAR_VARS)
 
 LOCAL_SRC_FILES := $(libc_common_src_files)
 LOCAL_CFLAGS := $(libc_common_cflags)
 ifeq ($(TARGET_ARCH),arm)
-LOCAL_CFLAGS += -DCRT_LEGACY_WORKAROUND
+    LOCAL_CFLAGS += -DCRT_LEGACY_WORKAROUND
 endif
 LOCAL_C_INCLUDES := $(libc_common_c_includes)
 LOCAL_MODULE := libc_common
-LOCAL_WHOLE_STATIC_LIBRARIES := libbionic_ssp
+LOCAL_WHOLE_STATIC_LIBRARIES := libbionic_ssp libc_netbsd
 LOCAL_SYSTEM_SHARED_LIBRARIES :=
 
 include $(BUILD_STATIC_LIBRARY)
diff --git a/libc/NOTICE b/libc/NOTICE
index 8c5bdc8..d78b840 100644
--- a/libc/NOTICE
+++ b/libc/NOTICE
@@ -1850,6 +1850,22 @@
 
 -------------------------------------------------------------------
 
+Copyright (C) 2012 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.
+
+-------------------------------------------------------------------
+
 Copyright (c) 1991 by AT&T.
 
 Permission to use, copy, modify, and distribute this software for any
diff --git a/libc/SYSCALLS.TXT b/libc/SYSCALLS.TXT
index e5a8c8d..6f12be8 100644
--- a/libc/SYSCALLS.TXT
+++ b/libc/SYSCALLS.TXT
@@ -1,6 +1,6 @@
 # this file is used to list all the syscalls that will be supported by
 # the Bionic C library. It is used to automatically generate the syscall
-# stubs, the list of syscall constants (__NR_xxxx) and the content of <linux/_unitsd.h>
+# stubs, the list of syscall constants (__NR_xxxx) and the content of <linux/_unistd.h>
 #
 # each non comment line has the following format:
 #
@@ -191,6 +191,7 @@
 int     removexattr(const char *, const char *) 235,235,233
 int     lremovexattr(const char *, const char *) 236,236,234
 int     __statfs64:statfs64(const char *, size_t, struct statfs *)  266,268,255
+long    unshare(unsigned long)  337,310,303
 
 # time
 int           pause ()                       29
diff --git a/libc/arch-arm/bionic/crtend.S b/libc/arch-arm/bionic/crtend.S
index 2f3b1ed..7196fc1 100644
--- a/libc/arch-arm/bionic/crtend.S
+++ b/libc/arch-arm/bionic/crtend.S
@@ -38,3 +38,6 @@
 	.section .ctors, "aw"
 	.long 0
 
+#if defined(__linux__) && defined(__ELF__)
+	.section .note.GNU-stack,"",%progbits
+#endif
diff --git a/libc/arch-arm/bionic/crtend_so.S b/libc/arch-arm/bionic/crtend_so.S
index a1281c4..0cb9947 100644
--- a/libc/arch-arm/bionic/crtend_so.S
+++ b/libc/arch-arm/bionic/crtend_so.S
@@ -36,3 +36,6 @@
 	.section .fini_array, "aw"
 	.long 0
 
+#if defined(__linux__) && defined(__ELF__)
+	.section .note.GNU-stack,"",%progbits
+#endif
diff --git a/libc/arch-arm/syscalls.mk b/libc/arch-arm/syscalls.mk
index e635e68..b1c669d 100644
--- a/libc/arch-arm/syscalls.mk
+++ b/libc/arch-arm/syscalls.mk
@@ -121,6 +121,7 @@
 syscall_src += arch-arm/syscalls/removexattr.S
 syscall_src += arch-arm/syscalls/lremovexattr.S
 syscall_src += arch-arm/syscalls/__statfs64.S
+syscall_src += arch-arm/syscalls/unshare.S
 syscall_src += arch-arm/syscalls/pause.S
 syscall_src += arch-arm/syscalls/gettimeofday.S
 syscall_src += arch-arm/syscalls/settimeofday.S
diff --git a/libc/arch-arm/syscalls/unshare.S b/libc/arch-arm/syscalls/unshare.S
new file mode 100644
index 0000000..0a92b21
--- /dev/null
+++ b/libc/arch-arm/syscalls/unshare.S
@@ -0,0 +1,14 @@
+/* autogenerated by gensyscalls.py */
+#include <machine/asm.h>
+#include <sys/linux-syscalls.h>
+
+ENTRY(unshare)
+    .save   {r4, r7}
+    stmfd   sp!, {r4, r7}
+    ldr     r7, =__NR_unshare
+    swi     #0
+    ldmfd   sp!, {r4, r7}
+    movs    r0, r0
+    bxpl    lr
+    b       __set_syscall_errno
+END(unshare)
diff --git a/libc/arch-mips/syscalls.mk b/libc/arch-mips/syscalls.mk
index 8c0c4dc..6de0c8f 100644
--- a/libc/arch-mips/syscalls.mk
+++ b/libc/arch-mips/syscalls.mk
@@ -124,6 +124,7 @@
 syscall_src += arch-mips/syscalls/removexattr.S
 syscall_src += arch-mips/syscalls/lremovexattr.S
 syscall_src += arch-mips/syscalls/__statfs64.S
+syscall_src += arch-mips/syscalls/unshare.S
 syscall_src += arch-mips/syscalls/pause.S
 syscall_src += arch-mips/syscalls/gettimeofday.S
 syscall_src += arch-mips/syscalls/settimeofday.S
diff --git a/libc/arch-mips/syscalls/unshare.S b/libc/arch-mips/syscalls/unshare.S
new file mode 100644
index 0000000..11af354
--- /dev/null
+++ b/libc/arch-mips/syscalls/unshare.S
@@ -0,0 +1,22 @@
+/* autogenerated by gensyscalls.py */
+#include <sys/linux-syscalls.h>
+    .text
+    .globl unshare
+    .align 4
+    .ent unshare
+
+unshare:
+    .set noreorder
+    .cpload $t9
+    li $v0, __NR_unshare
+    syscall
+    bnez $a3, 1f
+    move $a0, $v0
+    j $ra
+    nop
+1:
+    la $t9,__set_errno
+    j $t9
+    nop
+    .set reorder
+    .end unshare
diff --git a/libc/arch-x86/syscalls.mk b/libc/arch-x86/syscalls.mk
index f1b7e76..2352221 100644
--- a/libc/arch-x86/syscalls.mk
+++ b/libc/arch-x86/syscalls.mk
@@ -125,6 +125,7 @@
 syscall_src += arch-x86/syscalls/removexattr.S
 syscall_src += arch-x86/syscalls/lremovexattr.S
 syscall_src += arch-x86/syscalls/__statfs64.S
+syscall_src += arch-x86/syscalls/unshare.S
 syscall_src += arch-x86/syscalls/pause.S
 syscall_src += arch-x86/syscalls/gettimeofday.S
 syscall_src += arch-x86/syscalls/settimeofday.S
diff --git a/libc/arch-x86/syscalls/unshare.S b/libc/arch-x86/syscalls/unshare.S
new file mode 100644
index 0000000..ef31a35
--- /dev/null
+++ b/libc/arch-x86/syscalls/unshare.S
@@ -0,0 +1,23 @@
+/* autogenerated by gensyscalls.py */
+#include <sys/linux-syscalls.h>
+
+    .text
+    .type unshare, @function
+    .globl unshare
+    .align 4
+
+unshare:
+    pushl   %ebx
+    mov     8(%esp), %ebx
+    movl    $__NR_unshare, %eax
+    int     $0x80
+    cmpl    $-129, %eax
+    jb      1f
+    negl    %eax
+    pushl   %eax
+    call    __set_errno
+    addl    $4, %esp
+    orl     $-1, %eax
+1:
+    popl    %ebx
+    ret
diff --git a/libc/include/sys/_sigdefs.h b/libc/include/sys/_sigdefs.h
index 6b47a03..eadf7b9 100644
--- a/libc/include/sys/_sigdefs.h
+++ b/libc/include/sys/_sigdefs.h
@@ -86,7 +86,7 @@
 __BIONIC_SIGDEF(STOP,19,"Stopped (signal)")
 __BIONIC_SIGDEF(TSTP,20,"Stopped")
 __BIONIC_SIGDEF(TTIN,21,"Stopped (tty input)")
-__BIONIC_SIGDEF(TTOU,22,"Stopper (tty output)")
+__BIONIC_SIGDEF(TTOU,22,"Stopped (tty output)")
 __BIONIC_SIGDEF(URG,23,"Urgent I/O condition")
 __BIONIC_SIGDEF(XCPU,24,"CPU time limit exceeded")
 __BIONIC_SIGDEF(XFSZ,25,"File size limit exceeded")
diff --git a/libc/include/sys/linux-syscalls.h b/libc/include/sys/linux-syscalls.h
index eba4d35..48564cd 100644
--- a/libc/include/sys/linux-syscalls.h
+++ b/libc/include/sys/linux-syscalls.h
@@ -154,6 +154,7 @@
 #define __NR_removexattr                  (__NR_SYSCALL_BASE + 235)
 #define __NR_lremovexattr                 (__NR_SYSCALL_BASE + 236)
 #define __NR_statfs64                     (__NR_SYSCALL_BASE + 266)
+#define __NR_unshare                      (__NR_SYSCALL_BASE + 337)
 #define __NR_nanosleep                    (__NR_SYSCALL_BASE + 162)
 #define __NR_clock_gettime                (__NR_SYSCALL_BASE + 263)
 #define __NR_clock_settime                (__NR_SYSCALL_BASE + 262)
@@ -319,6 +320,7 @@
 #define __NR_renameat                     (__NR_SYSCALL_BASE + 302)
 #define __NR_fchmodat                     (__NR_SYSCALL_BASE + 306)
 #define __NR_faccessat                    (__NR_SYSCALL_BASE + 307)
+#define __NR_unshare                      (__NR_SYSCALL_BASE + 310)
 #define __NR_getcpu                       (__NR_SYSCALL_BASE + 318)
 #define __NR_utimensat                    (__NR_SYSCALL_BASE + 320)
 #define __NR_eventfd2                     (__NR_SYSCALL_BASE + 328)
@@ -446,6 +448,7 @@
 #define __NR_renameat                     (__NR_SYSCALL_BASE + 295)
 #define __NR_fchmodat                     (__NR_SYSCALL_BASE + 299)
 #define __NR_faccessat                    (__NR_SYSCALL_BASE + 300)
+#define __NR_unshare                      (__NR_SYSCALL_BASE + 303)
 #define __NR_getcpu                       (__NR_SYSCALL_BASE + 312)
 #define __NR_ioprio_set                   (__NR_SYSCALL_BASE + 314)
 #define __NR_ioprio_get                   (__NR_SYSCALL_BASE + 315)
diff --git a/libc/string/strxfrm.c b/libc/string/strxfrm.c
deleted file mode 100755
index 3c4d707..0000000
--- a/libc/string/strxfrm.c
+++ /dev/null
@@ -1,47 +0,0 @@
-/*
- * Copyright (C) 2009 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 <string.h>
-
-/*
- * Transform string s2 to string s1 using the current locale so that
- * strcmp of transformed strings yields the same result as strcoll.
- * Since Bionic really does not support locales, we assume we always use
- * the C locale.
- *
- * This function is provided to make libstdc++-v3 usable.
- */
-size_t
-strxfrm(char *s1, const char *s2, size_t n)
-{
-	size_t len = strlen(s2) + 1;
-
-	if (len < n)
-		n = len;
-	memcpy(s1, s2, n);
-	return len;
-}
diff --git a/libc/upstream-netbsd/README.txt b/libc/upstream-netbsd/README.txt
new file mode 100644
index 0000000..86af6eb
--- /dev/null
+++ b/libc/upstream-netbsd/README.txt
@@ -0,0 +1,9 @@
+This directory contains upstream NetBSD source. You should not edit these
+files directly. Make fixes upstream and then pull down the new version of
+the file.
+
+Note that code in the other 'netbsd' directory contains Android modifications.
+We should work towards getting as many of those changes as possible upstream
+and then losing those files in favor of pure upstream copies here instead.
+
+TODO: write a script to make this process automated.
diff --git a/libc/upstream-netbsd/libc/string/strxfrm.c b/libc/upstream-netbsd/libc/string/strxfrm.c
new file mode 100644
index 0000000..42c2a24
--- /dev/null
+++ b/libc/upstream-netbsd/libc/string/strxfrm.c
@@ -0,0 +1,70 @@
+/*	$NetBSD: strxfrm.c,v 1.12 2012/06/25 22:32:46 abs Exp $	*/
+
+/*-
+ * Copyright (c) 1990, 1993
+ *	The Regents of the University of California.  All rights reserved.
+ *
+ * This code is derived from software contributed to Berkeley by
+ * Chris Torek.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. 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.
+ * 3. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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/cdefs.h>
+#if defined(LIBC_SCCS) && !defined(lint)
+#if 0
+static char sccsid[] = "@(#)strxfrm.c	8.1 (Berkeley) 6/4/93";
+#else
+__RCSID("$NetBSD: strxfrm.c,v 1.12 2012/06/25 22:32:46 abs Exp $");
+#endif
+#endif /* LIBC_SCCS and not lint */
+
+#include <assert.h>
+#include <string.h>
+
+/*
+ * Transform src, storing the result in dst, such that
+ * strcmp() on transformed strings returns what strcoll()
+ * on the original untransformed strings would return.
+ */
+size_t
+strxfrm(char *dst, const char *src, size_t n)
+{
+	size_t srclen, copysize;
+
+	_DIAGASSERT(src != NULL);
+
+	/*
+	 * Since locales are unimplemented, this is just a copy.
+	 */
+	srclen = strlen(src);
+	if (n != 0) {
+		_DIAGASSERT(dst != NULL);
+		copysize = srclen < n ? srclen : n - 1;
+		(void)memcpy(dst, src, copysize);
+		dst[copysize] = 0;
+	}
+	return (srclen);
+}
diff --git a/libc/upstream-netbsd/netbsd-compat.h b/libc/upstream-netbsd/netbsd-compat.h
new file mode 100644
index 0000000..a52052a
--- /dev/null
+++ b/libc/upstream-netbsd/netbsd-compat.h
@@ -0,0 +1,24 @@
+/*
+ * Copyright (C) 2012 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.
+ */
+
+#ifndef _BIONIC_NETBSD_COMPAT_H_included
+#define _BIONIC_NETBSD_COMPAT_H_included
+
+// NetBSD uses _DIAGASSERT to null-check arguments and the like.
+#include <assert.h>
+#define _DIAGASSERT(e) ((e) ? (void) 0 : __assert2(__FILE__, __LINE__, __func__, #e))
+
+#endif