From b01a8cace6484fdd62855d75fd81bfca1c86289f Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Mon, 26 Nov 2018 02:57:34 -0500 Subject: [PATCH] Always try native accept4 --- native/jni/utils/xwrap.cpp | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/native/jni/utils/xwrap.cpp b/native/jni/utils/xwrap.cpp index 1a6600c12..a1344fd2f 100644 --- a/native/jni/utils/xwrap.cpp +++ b/native/jni/utils/xwrap.cpp @@ -172,16 +172,27 @@ int xlisten(int sockfd, int backlog) { return ret; } -int xaccept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags) { +static int accept4_compat(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags) { int fd = accept(sockfd, addr, addrlen); if (fd == -1) { - PLOGE("accept4"); + PLOGE("accept"); + } else { + if (flags & SOCK_CLOEXEC) + fcntl(fd, F_SETFD, FD_CLOEXEC); + if (flags & SOCK_NONBLOCK) { + int i = fcntl(fd, F_GETFL); + fcntl(fd, F_SETFL, i | O_NONBLOCK); + } } - if (flags & SOCK_CLOEXEC) - fcntl(fd, F_SETFD, FD_CLOEXEC); - if (flags & SOCK_NONBLOCK) { - int i = fcntl(fd, F_GETFL); - fcntl(fd, F_SETFL, i | O_NONBLOCK); + return fd; +} + +int xaccept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags) { + int fd = (int) syscall(__NR_accept4, sockfd, addr, addrlen, flags); + if (fd == -1) { + if (errno == ENOSYS) + return accept4_compat(sockfd, addr, addrlen, flags); + PLOGE("accept4"); } return fd; }