Merge "Mark sockets on creation (socket()) and accept4()."
diff --git a/libc/Android.mk b/libc/Android.mk
index f0ee09d..caf0bac 100644
--- a/libc/Android.mk
+++ b/libc/Android.mk
@@ -98,6 +98,7 @@
 libc_bionic_src_files := \
     bionic/abort.cpp \
     bionic/accept.cpp \
+    bionic/accept4.cpp \
     bionic/access.cpp \
     bionic/assert.cpp \
     bionic/atof.cpp \
@@ -199,6 +200,7 @@
     bionic/sigprocmask.cpp \
     bionic/sigsuspend.cpp \
     bionic/sigwait.cpp \
+    bionic/socket.cpp \
     bionic/stat.cpp \
     bionic/statvfs.cpp \
     bionic/strerror.cpp \
diff --git a/libc/SYSCALLS.TXT b/libc/SYSCALLS.TXT
index 4a21582..2a891b7 100644
--- a/libc/SYSCALLS.TXT
+++ b/libc/SYSCALLS.TXT
@@ -231,13 +231,12 @@
 int     __signalfd4:signalfd4(int, const sigset_t*, size_t, int)  all
 
 # sockets
-int           socket(int, int, int)              arm,arm64,mips,mips64,x86_64
+int           __socket:socket(int, int, int)              arm,arm64,mips,mips64,x86_64
 int           socketpair(int, int, int, int*)    arm,arm64,mips,mips64,x86_64
 int           bind(int, struct sockaddr*, int)  arm,arm64,mips,mips64,x86_64
 int           __connect:connect(int, struct sockaddr*, socklen_t)   arm,arm64,mips,mips64,x86_64
 int           listen(int, int)                   arm,arm64,mips,mips64,x86_64
-int           __accept:accept(int, struct sockaddr*, socklen_t*)  arm,arm64,mips,mips64,x86_64
-int           accept4(int, struct sockaddr*, socklen_t*, int)  arm,arm64,mips,mips64,x86_64
+int           __accept4:accept4(int, struct sockaddr*, socklen_t*, int)  arm,arm64,mips,mips64,x86_64
 int           getsockname(int, struct sockaddr*, socklen_t*)  arm,arm64,mips,mips64,x86_64
 int           getpeername(int, struct sockaddr*, socklen_t*)  arm,arm64,mips,mips64,x86_64
 int           sendto(int, const void*, size_t, int, const struct sockaddr*, socklen_t)  arm,arm64,mips,mips64,x86_64
@@ -251,11 +250,10 @@
 int           sendmmsg(int, struct mmsghdr*, unsigned int, int)   arm,arm64,mips,mips64,x86_64
 
 # sockets for x86. These are done as an "indexed" call to socketcall syscall.
-int           socket:socketcall:1(int, int, int) x86
+int           __socket:socketcall:1(int, int, int) x86
 int           bind:socketcall:2(int, struct sockaddr*, int)  x86
 int           __connect:socketcall:3(int, struct sockaddr*, socklen_t)   x86
 int           listen:socketcall:4(int, int)                   x86
-int           __accept:socketcall:5(int, struct sockaddr*, socklen_t*)  x86
 int           getsockname:socketcall:6(int, struct sockaddr*, socklen_t*)  x86
 int           getpeername:socketcall:7(int, struct sockaddr*, socklen_t*)  x86
 int           socketpair:socketcall:8(int, int, int, int*)    x86
@@ -266,7 +264,7 @@
 int           getsockopt:socketcall:15(int, int, int, void*, socklen_t*)    x86
 int           sendmsg:socketcall:16(int, const struct msghdr*, unsigned int)  x86
 int           recvmsg:socketcall:17(int, struct msghdr*, unsigned int)   x86
-int           accept4:socketcall:18(int, struct sockaddr*, socklen_t*, int)  x86
+int           __accept4:socketcall:18(int, struct sockaddr*, socklen_t*, int)  x86
 int           recvmmsg:socketcall:19(int, struct mmsghdr*, unsigned int, int, const struct timespec*)   x86
 int           sendmmsg:socketcall:20(int, struct mmsghdr*, unsigned int, int)   x86
 
diff --git a/libc/arch-arm/syscalls/__accept.S b/libc/arch-arm/syscalls/__accept.S
deleted file mode 100644
index bae11bc..0000000
--- a/libc/arch-arm/syscalls/__accept.S
+++ /dev/null
@@ -1,14 +0,0 @@
-/* Generated by gensyscalls.py. Do not edit. */
-
-#include <private/bionic_asm.h>
-
-ENTRY(__accept)
-    mov     ip, r7
-    ldr     r7, =__NR_accept
-    swi     #0
-    mov     r7, ip
-    cmn     r0, #(MAX_ERRNO + 1)
-    bxls    lr
-    neg     r0, r0
-    b       __set_errno
-END(__accept)
diff --git a/libc/arch-arm/syscalls/accept4.S b/libc/arch-arm/syscalls/__accept4.S
similarity index 88%
rename from libc/arch-arm/syscalls/accept4.S
rename to libc/arch-arm/syscalls/__accept4.S
index 6d14e79..9a6874b 100644
--- a/libc/arch-arm/syscalls/accept4.S
+++ b/libc/arch-arm/syscalls/__accept4.S
@@ -2,7 +2,7 @@
 
 #include <private/bionic_asm.h>
 
-ENTRY(accept4)
+ENTRY(__accept4)
     mov     ip, r7
     ldr     r7, =__NR_accept4
     swi     #0
@@ -11,4 +11,4 @@
     bxls    lr
     neg     r0, r0
     b       __set_errno
-END(accept4)
+END(__accept4)
diff --git a/libc/arch-arm/syscalls/socket.S b/libc/arch-arm/syscalls/__socket.S
similarity index 89%
rename from libc/arch-arm/syscalls/socket.S
rename to libc/arch-arm/syscalls/__socket.S
index 05fa529..d63028b 100644
--- a/libc/arch-arm/syscalls/socket.S
+++ b/libc/arch-arm/syscalls/__socket.S
@@ -2,7 +2,7 @@
 
 #include <private/bionic_asm.h>
 
-ENTRY(socket)
+ENTRY(__socket)
     mov     ip, r7
     ldr     r7, =__NR_socket
     swi     #0
@@ -11,4 +11,4 @@
     bxls    lr
     neg     r0, r0
     b       __set_errno
-END(socket)
+END(__socket)
diff --git a/libc/arch-arm64/syscalls/__accept.S b/libc/arch-arm64/syscalls/__accept.S
deleted file mode 100644
index 21b68bc..0000000
--- a/libc/arch-arm64/syscalls/__accept.S
+++ /dev/null
@@ -1,22 +0,0 @@
-/* Generated by gensyscalls.py. Do not edit. */
-
-#include <private/bionic_asm.h>
-
-ENTRY(__accept)
-    stp     x29, x30, [sp, #-16]!
-    mov     x29,  sp
-    str     x8,       [sp, #-16]!
-
-    mov     x8, __NR_accept
-    svc     #0
-
-    ldr     x8,       [sp], #16
-    ldp     x29, x30, [sp], #16
-
-    cmn     x0, #(MAX_ERRNO + 1)
-    cneg    x0, x0, hi
-    b.hi    __set_errno
-
-    ret
-END(__accept)
-.hidden __accept
diff --git a/libc/arch-arm64/syscalls/accept4.S b/libc/arch-arm64/syscalls/__accept4.S
similarity index 88%
rename from libc/arch-arm64/syscalls/accept4.S
rename to libc/arch-arm64/syscalls/__accept4.S
index 3c9227f..c66cd2e 100644
--- a/libc/arch-arm64/syscalls/accept4.S
+++ b/libc/arch-arm64/syscalls/__accept4.S
@@ -2,7 +2,7 @@
 
 #include <private/bionic_asm.h>
 
-ENTRY(accept4)
+ENTRY(__accept4)
     stp     x29, x30, [sp, #-16]!
     mov     x29,  sp
     str     x8,       [sp, #-16]!
@@ -18,4 +18,5 @@
     b.hi    __set_errno
 
     ret
-END(accept4)
+END(__accept4)
+.hidden __accept4
diff --git a/libc/arch-arm64/syscalls/socket.S b/libc/arch-arm64/syscalls/__socket.S
similarity index 88%
rename from libc/arch-arm64/syscalls/socket.S
rename to libc/arch-arm64/syscalls/__socket.S
index 37a3851..aa9da22 100644
--- a/libc/arch-arm64/syscalls/socket.S
+++ b/libc/arch-arm64/syscalls/__socket.S
@@ -2,7 +2,7 @@
 
 #include <private/bionic_asm.h>
 
-ENTRY(socket)
+ENTRY(__socket)
     stp     x29, x30, [sp, #-16]!
     mov     x29,  sp
     str     x8,       [sp, #-16]!
@@ -18,4 +18,5 @@
     b.hi    __set_errno
 
     ret
-END(socket)
+END(__socket)
+.hidden __socket
diff --git a/libc/arch-mips/syscalls/__accept.S b/libc/arch-mips/syscalls/__accept.S
deleted file mode 100644
index d8141fe..0000000
--- a/libc/arch-mips/syscalls/__accept.S
+++ /dev/null
@@ -1,19 +0,0 @@
-/* Generated by gensyscalls.py. Do not edit. */
-
-#include <private/bionic_asm.h>
-
-ENTRY(__accept)
-    .set noreorder
-    .cpload t9
-    li v0, __NR_accept
-    syscall
-    bnez a3, 1f
-    move a0, v0
-    j ra
-    nop
-1:
-    la t9,__set_errno
-    j t9
-    nop
-    .set reorder
-END(__accept)
diff --git a/libc/arch-mips/syscalls/accept4.S b/libc/arch-mips/syscalls/__accept4.S
similarity index 89%
rename from libc/arch-mips/syscalls/accept4.S
rename to libc/arch-mips/syscalls/__accept4.S
index ea1dc60..8634a35 100644
--- a/libc/arch-mips/syscalls/accept4.S
+++ b/libc/arch-mips/syscalls/__accept4.S
@@ -2,7 +2,7 @@
 
 #include <private/bionic_asm.h>
 
-ENTRY(accept4)
+ENTRY(__accept4)
     .set noreorder
     .cpload t9
     li v0, __NR_accept4
@@ -16,4 +16,4 @@
     j t9
     nop
     .set reorder
-END(accept4)
+END(__accept4)
diff --git a/libc/arch-mips/syscalls/socket.S b/libc/arch-mips/syscalls/__socket.S
similarity index 89%
rename from libc/arch-mips/syscalls/socket.S
rename to libc/arch-mips/syscalls/__socket.S
index 2056bcd..6508911 100644
--- a/libc/arch-mips/syscalls/socket.S
+++ b/libc/arch-mips/syscalls/__socket.S
@@ -2,7 +2,7 @@
 
 #include <private/bionic_asm.h>
 
-ENTRY(socket)
+ENTRY(__socket)
     .set noreorder
     .cpload t9
     li v0, __NR_socket
@@ -16,4 +16,4 @@
     j t9
     nop
     .set reorder
-END(socket)
+END(__socket)
diff --git a/libc/arch-mips64/syscalls/__accept.S b/libc/arch-mips64/syscalls/__accept.S
deleted file mode 100644
index 4404a68..0000000
--- a/libc/arch-mips64/syscalls/__accept.S
+++ /dev/null
@@ -1,26 +0,0 @@
-/* Generated by gensyscalls.py. Do not edit. */
-
-#include <private/bionic_asm.h>
-
-ENTRY(__accept)
-    .set push
-    .set noreorder
-    li v0, __NR_accept
-    syscall
-    bnez a3, 1f
-    move a0, v0
-    j ra
-    nop
-1:
-    move t0, ra
-    bal     2f
-    nop
-2:
-    .cpsetup ra, t1, 2b
-    LA t9,__set_errno
-    .cpreturn
-    j t9
-    move ra, t0
-    .set pop
-END(__accept)
-.hidden __accept
diff --git a/libc/arch-mips64/syscalls/accept4.S b/libc/arch-mips64/syscalls/__accept4.S
similarity index 87%
rename from libc/arch-mips64/syscalls/accept4.S
rename to libc/arch-mips64/syscalls/__accept4.S
index 8b5cadb..e68bdb6 100644
--- a/libc/arch-mips64/syscalls/accept4.S
+++ b/libc/arch-mips64/syscalls/__accept4.S
@@ -2,7 +2,7 @@
 
 #include <private/bionic_asm.h>
 
-ENTRY(accept4)
+ENTRY(__accept4)
     .set push
     .set noreorder
     li v0, __NR_accept4
@@ -22,4 +22,5 @@
     j t9
     move ra, t0
     .set pop
-END(accept4)
+END(__accept4)
+.hidden __accept4
diff --git a/libc/arch-mips64/syscalls/socket.S b/libc/arch-mips64/syscalls/__socket.S
similarity index 87%
rename from libc/arch-mips64/syscalls/socket.S
rename to libc/arch-mips64/syscalls/__socket.S
index 2020e2e..d1e0058 100644
--- a/libc/arch-mips64/syscalls/socket.S
+++ b/libc/arch-mips64/syscalls/__socket.S
@@ -2,7 +2,7 @@
 
 #include <private/bionic_asm.h>
 
-ENTRY(socket)
+ENTRY(__socket)
     .set push
     .set noreorder
     li v0, __NR_socket
@@ -22,4 +22,5 @@
     j t9
     move ra, t0
     .set pop
-END(socket)
+END(__socket)
+.hidden __socket
diff --git a/libc/arch-x86/syscalls/__accept.S b/libc/arch-x86/syscalls/__accept.S
deleted file mode 100644
index 31cb350..0000000
--- a/libc/arch-x86/syscalls/__accept.S
+++ /dev/null
@@ -1,27 +0,0 @@
-/* Generated by gensyscalls.py. Do not edit. */
-
-#include <private/bionic_asm.h>
-
-ENTRY(__accept)
-    pushl   %ebx
-    pushl   %ecx
-    .cfi_def_cfa_offset 8
-    .cfi_rel_offset ebx, 0
-    .cfi_rel_offset ecx, 4
-    mov     $5, %ebx
-    mov     %esp, %ecx
-    addl    $12, %ecx
-    movl    $__NR_socketcall, %eax
-    int     $0x80
-    cmpl    $-MAX_ERRNO, %eax
-    jb      1f
-    negl    %eax
-    pushl   %eax
-    call    __set_errno
-    addl    $4, %esp
-    orl     $-1, %eax
-1:
-    popl    %ecx
-    popl    %ebx
-    ret
-END(__accept)
diff --git a/libc/arch-x86/syscalls/accept4.S b/libc/arch-x86/syscalls/__accept4.S
similarity index 94%
rename from libc/arch-x86/syscalls/accept4.S
rename to libc/arch-x86/syscalls/__accept4.S
index dc3c1f5..573eddb 100644
--- a/libc/arch-x86/syscalls/accept4.S
+++ b/libc/arch-x86/syscalls/__accept4.S
@@ -2,7 +2,7 @@
 
 #include <private/bionic_asm.h>
 
-ENTRY(accept4)
+ENTRY(__accept4)
     pushl   %ebx
     pushl   %ecx
     .cfi_def_cfa_offset 8
@@ -24,4 +24,4 @@
     popl    %ecx
     popl    %ebx
     ret
-END(accept4)
+END(__accept4)
diff --git a/libc/arch-x86/syscalls/socket.S b/libc/arch-x86/syscalls/__socket.S
similarity index 94%
rename from libc/arch-x86/syscalls/socket.S
rename to libc/arch-x86/syscalls/__socket.S
index 282681e..5e52d8f 100644
--- a/libc/arch-x86/syscalls/socket.S
+++ b/libc/arch-x86/syscalls/__socket.S
@@ -2,7 +2,7 @@
 
 #include <private/bionic_asm.h>
 
-ENTRY(socket)
+ENTRY(__socket)
     pushl   %ebx
     pushl   %ecx
     .cfi_def_cfa_offset 8
@@ -24,4 +24,4 @@
     popl    %ecx
     popl    %ebx
     ret
-END(socket)
+END(__socket)
diff --git a/libc/arch-x86_64/syscalls/__accept.S b/libc/arch-x86_64/syscalls/__accept.S
deleted file mode 100644
index ff0f5e7..0000000
--- a/libc/arch-x86_64/syscalls/__accept.S
+++ /dev/null
@@ -1,17 +0,0 @@
-/* Generated by gensyscalls.py. Do not edit. */
-
-#include <private/bionic_asm.h>
-
-ENTRY(__accept)
-    movl    $__NR_accept, %eax
-    syscall
-    cmpq    $-MAX_ERRNO, %rax
-    jb      1f
-    negl    %eax
-    movl    %eax, %edi
-    call    __set_errno
-    orq     $-1, %rax
-1:
-    ret
-END(__accept)
-.hidden __accept
diff --git a/libc/arch-x86_64/syscalls/accept4.S b/libc/arch-x86_64/syscalls/__accept4.S
similarity index 85%
rename from libc/arch-x86_64/syscalls/accept4.S
rename to libc/arch-x86_64/syscalls/__accept4.S
index d66d952..cf4837f 100644
--- a/libc/arch-x86_64/syscalls/accept4.S
+++ b/libc/arch-x86_64/syscalls/__accept4.S
@@ -2,7 +2,7 @@
 
 #include <private/bionic_asm.h>
 
-ENTRY(accept4)
+ENTRY(__accept4)
     movq    %rcx, %r10
     movl    $__NR_accept4, %eax
     syscall
@@ -14,4 +14,5 @@
     orq     $-1, %rax
 1:
     ret
-END(accept4)
+END(__accept4)
+.hidden __accept4
diff --git a/libc/arch-x86_64/syscalls/socket.S b/libc/arch-x86_64/syscalls/__socket.S
similarity index 85%
rename from libc/arch-x86_64/syscalls/socket.S
rename to libc/arch-x86_64/syscalls/__socket.S
index 9665ee4..67621fc 100644
--- a/libc/arch-x86_64/syscalls/socket.S
+++ b/libc/arch-x86_64/syscalls/__socket.S
@@ -2,7 +2,7 @@
 
 #include <private/bionic_asm.h>
 
-ENTRY(socket)
+ENTRY(__socket)
     movl    $__NR_socket, %eax
     syscall
     cmpq    $-MAX_ERRNO, %rax
@@ -13,4 +13,5 @@
     orq     $-1, %rax
 1:
     ret
-END(socket)
+END(__socket)
+.hidden __socket
diff --git a/libc/bionic/NetdClient.cpp b/libc/bionic/NetdClient.cpp
index 36796a2..5b0f4fd 100644
--- a/libc/bionic/NetdClient.cpp
+++ b/libc/bionic/NetdClient.cpp
@@ -40,18 +40,19 @@
         // default implementations of functions that it would've overridden.
         return;
     }
-    netdClientInitFunction(netdClientHandle, "netdClientInitAccept", &__netdClientDispatch.accept);
+    netdClientInitFunction(netdClientHandle, "netdClientInitAccept4",
+                           &__netdClientDispatch.accept4);
     netdClientInitFunction(netdClientHandle, "netdClientInitConnect",
                            &__netdClientDispatch.connect);
     netdClientInitFunction(netdClientHandle, "netdClientInitNetIdForResolv",
                            &__netdClientDispatch.netIdForResolv);
+    netdClientInitFunction(netdClientHandle, "netdClientInitSocket", &__netdClientDispatch.socket);
 }
 
 static pthread_once_t netdClientInitOnce = PTHREAD_ONCE_INIT;
 
 extern "C" __LIBC_HIDDEN__ void netdClientInit() {
     if (pthread_once(&netdClientInitOnce, netdClientInitImpl)) {
-        __libc_format_log(ANDROID_LOG_ERROR, "netdClient",
-                          "Unable to initialize netd_client component.");
+        __libc_format_log(ANDROID_LOG_ERROR, "netdClient", "Failed to initialize netd_client");
     }
 }
diff --git a/libc/bionic/NetdClientDispatch.cpp b/libc/bionic/NetdClientDispatch.cpp
index 17bbd9d..67fa197 100644
--- a/libc/bionic/NetdClientDispatch.cpp
+++ b/libc/bionic/NetdClientDispatch.cpp
@@ -22,8 +22,9 @@
 #define __socketcall
 #endif
 
-extern "C" __socketcall int __accept(int, sockaddr*, socklen_t*);
+extern "C" __socketcall int __accept4(int, sockaddr*, socklen_t*, int);
 extern "C" __socketcall int __connect(int, const sockaddr*, socklen_t);
+extern "C" __socketcall int __socket(int, int, int);
 
 static unsigned fallBackNetIdForResolv(unsigned netId) {
     return netId;
@@ -32,7 +33,8 @@
 // This structure is modified only at startup (when libc.so is loaded) and never
 // afterwards, so it's okay that it's read later at runtime without a lock.
 __LIBC_HIDDEN__ NetdClientDispatch __netdClientDispatch __attribute__((aligned(32))) = {
-    __accept,
+    __accept4,
     __connect,
+    __socket,
     fallBackNetIdForResolv,
 };
diff --git a/libc/bionic/accept.cpp b/libc/bionic/accept.cpp
index 8073234..7f7aa06 100644
--- a/libc/bionic/accept.cpp
+++ b/libc/bionic/accept.cpp
@@ -14,10 +14,8 @@
  * limitations under the License.
  */
 
-#include "private/NetdClientDispatch.h"
-
 #include <sys/socket.h>
 
 int accept(int sockfd, sockaddr* addr, socklen_t* addrlen) {
-    return __netdClientDispatch.accept(sockfd, addr, addrlen);
+    return accept4(sockfd, addr, addrlen, 0);
 }
diff --git a/libc/bionic/accept4.cpp b/libc/bionic/accept4.cpp
new file mode 100644
index 0000000..9f58dc1
--- /dev/null
+++ b/libc/bionic/accept4.cpp
@@ -0,0 +1,23 @@
+/*
+ * 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 "private/NetdClientDispatch.h"
+
+#include <sys/socket.h>
+
+int accept4(int sockfd, sockaddr* addr, socklen_t* addrlen, int flags) {
+    return __netdClientDispatch.accept4(sockfd, addr, addrlen, flags);
+}
diff --git a/libc/bionic/socket.cpp b/libc/bionic/socket.cpp
new file mode 100644
index 0000000..2f9e145
--- /dev/null
+++ b/libc/bionic/socket.cpp
@@ -0,0 +1,23 @@
+/*
+ * 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 "private/NetdClientDispatch.h"
+
+#include <sys/socket.h>
+
+int socket(int domain, int type, int protocol) {
+    return __netdClientDispatch.socket(domain, type, protocol);
+}
diff --git a/libc/private/NetdClientDispatch.h b/libc/private/NetdClientDispatch.h
index c256288..8d8947d 100644
--- a/libc/private/NetdClientDispatch.h
+++ b/libc/private/NetdClientDispatch.h
@@ -23,8 +23,9 @@
 __BEGIN_DECLS
 
 struct NetdClientDispatch {
-    int (*accept)(int, struct sockaddr*, socklen_t*);
+    int (*accept4)(int, struct sockaddr*, socklen_t*, int);
     int (*connect)(int, const struct sockaddr*, socklen_t);
+    int (*socket)(int, int, int);
     unsigned (*netIdForResolv)(unsigned);
 };