swrap: Add support for accept4()
authorAndreas Schneider <asn@samba.org>
Tue, 3 May 2016 12:59:34 +0000 (14:59 +0200)
committerAndreas Schneider <asn@samba.org>
Tue, 3 May 2016 13:36:27 +0000 (15:36 +0200)
Signed-off-by: Andreas Schneider <asn@samba.org>
Reviewed-by: Ralph Böhme <slow@samba.org>
ConfigureChecks.cmake
config.h.cmake
src/socket_wrapper.c

index 5e96912618b7a3fc28ed70f99dfff99e3a509f0a..48455efed6ed7a8cdb18dc3ee2898ef592f0a64d 100644 (file)
@@ -62,6 +62,7 @@ check_function_exists(signalfd HAVE_SIGNALFD)
 check_function_exists(eventfd HAVE_EVENTFD)
 check_function_exists(timerfd_create HAVE_TIMERFD_CREATE)
 check_function_exists(bindresvport HAVE_BINDRESVPORT)
+check_function_exists(accept4 HAVE_ACCEPT4)
 
 check_function_exists(pledge HAVE_PLEDGE)
 
index 40347f98927934237dbff63381051905c0d764d5..a9b8ce82d80ce9773fef58c104599cd1c3955040 100644 (file)
@@ -39,6 +39,7 @@
 #cmakedefine HAVE_EVENTFD 1
 #cmakedefine HAVE_TIMERFD_CREATE 1
 #cmakedefine HAVE_BINDRESVPORT 1
+#cmakedefine HAVE_ACCEPT4 1
 #cmakedefine HAVE_PLEDGE 1
 
 #cmakedefine HAVE_ACCEPT_PSOCKLEN_T 1
index f78c8968918109eccd0a735d24d842f363739813..6e65a7e812a18ef1b3ecd0161b99b666b4662736 100644 (file)
@@ -335,9 +335,16 @@ static void swrap_log(enum swrap_dbglvl_e dbglvl,
 #include <dlfcn.h>
 
 struct swrap_libc_fns {
+#ifdef HAVE_ACCEPT4
+       int (*libc_accept4)(int sockfd,
+                          struct sockaddr *addr,
+                          socklen_t *addrlen,
+                          int flags);
+#else
        int (*libc_accept)(int sockfd,
                           struct sockaddr *addr,
                           socklen_t *addrlen);
+#endif
        int (*libc_bind)(int sockfd,
                         const struct sockaddr *addr,
                         socklen_t addrlen);
@@ -552,12 +559,26 @@ static void *_swrap_load_lib_function(enum swrap_lib lib, const char *fn_name)
  * has probably something todo with with the linker.
  * So we need load each function at the point it is called the first time.
  */
+#ifdef HAVE_ACCEPT4
+static int libc_accept4(int sockfd,
+                       struct sockaddr *addr,
+                       socklen_t *addrlen,
+                       int flags)
+{
+       swrap_load_lib_function(SWRAP_LIBSOCKET, accept4);
+
+       return swrap.fns.libc_accept4(sockfd, addr, addrlen, flags);
+}
+
+#else /* HAVE_ACCEPT4 */
+
 static int libc_accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen)
 {
        swrap_load_lib_function(SWRAP_LIBSOCKET, accept);
 
        return swrap.fns.libc_accept(sockfd, addr, addrlen);
 }
+#endif /* HAVE_ACCEPT4 */
 
 static int libc_bind(int sockfd,
                     const struct sockaddr *addr,
@@ -2575,7 +2596,10 @@ int pipe(int pipefd[2])
  *   ACCEPT
  ***************************************************************************/
 
-static int swrap_accept(int s, struct sockaddr *addr, socklen_t *addrlen)
+static int swrap_accept(int s,
+                       struct sockaddr *addr,
+                       socklen_t *addrlen,
+                       int flags)
 {
        struct socket_info *parent_si, *child_si;
        struct socket_info_fd *child_fi;
@@ -2596,7 +2620,11 @@ static int swrap_accept(int s, struct sockaddr *addr, socklen_t *addrlen)
 
        parent_si = find_socket_info(s);
        if (!parent_si) {
+#ifdef HAVE_ACCEPT4
+               return libc_accept4(s, addr, addrlen, flags);
+#else
                return libc_accept(s, addr, addrlen);
+#endif
        }
 
        /*
@@ -2609,7 +2637,11 @@ static int swrap_accept(int s, struct sockaddr *addr, socklen_t *addrlen)
                return -1;
        }
 
+#ifdef HAVE_ACCEPT4
+       ret = libc_accept4(s, &un_addr.sa.s, &un_addr.sa_socklen, flags);
+#else
        ret = libc_accept(s, &un_addr.sa.s, &un_addr.sa_socklen);
+#endif
        if (ret == -1) {
                if (errno == ENOTSOCK) {
                        /* Remove stale fds */
@@ -2713,13 +2745,20 @@ static int swrap_accept(int s, struct sockaddr *addr, socklen_t *addrlen)
        return fd;
 }
 
+#ifdef HAVE_ACCEPT4
+int accept4(int s, struct sockaddr *addr, socklen_t *addrlen, int flags)
+{
+       return swrap_accept(s, addr, (socklen_t *)addrlen, flags);
+}
+#endif
+
 #ifdef HAVE_ACCEPT_PSOCKLEN_T
 int accept(int s, struct sockaddr *addr, Psocklen_t addrlen)
 #else
 int accept(int s, struct sockaddr *addr, socklen_t *addrlen)
 #endif
 {
-       return swrap_accept(s, addr, (socklen_t *)addrlen);
+       return swrap_accept(s, addr, (socklen_t *)addrlen, 0);
 }
 
 static int autobind_start_init;