From: Brian Aker Date: Sat, 6 Oct 2012 13:05:23 +0000 (-0400) Subject: Clean up SOCK_NONBLOCK X-Git-Tag: 1.0.12~8 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=8c008a3516e571c1efa9ba9c50bdbaddf6e94217;p=m6w6%2Flibmemcached Clean up SOCK_NONBLOCK --- diff --git a/configure.ac b/configure.ac index b8bff611..3827735a 100644 --- a/configure.ac +++ b/configure.ac @@ -255,9 +255,6 @@ AX_CHECK_LIBRARY([LIBEVENT], [event.h], [event], [AC_MSG_WARN([Unable to find libevent])]) AM_CONDITIONAL(HAVE_LIBEVENT, test "x${ax_cv_have_LIBEVENT}" = "xyes") -AX_CHECK_SOCK_CLOEXEC([AC_DEFINE([HAVE_SOCK_CLOEXEC], [1], [Check for SOCK_CLOEXEC.])], - [AC_DEFINE([HAVE_SOCK_CLOEXEC], [0], [Check for SOCK_CLOEXEC.])]) - AX_UUID_GENERATE_TIME_SAFE diff --git a/libmemcached/connect.cc b/libmemcached/connect.cc index 5d180d18..65be51a0 100644 --- a/libmemcached/connect.cc +++ b/libmemcached/connect.cc @@ -43,7 +43,15 @@ #include #ifndef SOCK_CLOEXEC -#define SOCK_CLOEXEC 0 +# define SOCK_CLOEXEC 0 +#endif + +#ifndef SOCK_NONBLOCK +# define SOCK_NONBLOCK 0 +#endif + +#ifndef FD_CLOEXEC +# define FD_CLOEXEC 0 #endif static memcached_return_t connect_poll(org::libmemcached::Instance* server) @@ -245,28 +253,31 @@ static inline void set_socket_nonblocking(org::libmemcached::Instance* server) #else int flags; - do - { - flags= fcntl(server->fd, F_GETFL, 0); - } while (flags == -1 && (errno == EINTR || errno == EAGAIN)); - - if (flags == -1) - { - memcached_set_errno(*server, errno, NULL); - } - else if ((flags & O_NONBLOCK) == 0) + if (SOCK_NONBLOCK == 0) { - int rval; - do { - rval= fcntl(server->fd, F_SETFL, flags | O_NONBLOCK); - } while (rval == -1 && (errno == EINTR or errno == EAGAIN)); + flags= fcntl(server->fd, F_GETFL, 0); + } while (flags == -1 && (errno == EINTR || errno == EAGAIN)); - if (rval == -1) + if (flags == -1) { memcached_set_errno(*server, errno, NULL); } + else if ((flags & O_NONBLOCK) == 0) + { + int rval; + + do + { + rval= fcntl(server->fd, F_SETFL, flags | O_NONBLOCK); + } while (rval == -1 && (errno == EINTR or errno == EAGAIN)); + + if (rval == -1) + { + memcached_set_errno(*server, errno, NULL); + } + } } #endif } @@ -386,7 +397,6 @@ static void set_socket_options(org::libmemcached::Instance* server) assert(error == 0); } - /* libmemcached will always use nonblocking IO to avoid write deadlocks */ set_socket_nonblocking(server); } @@ -396,7 +406,18 @@ static memcached_return_t unix_socket_connect(org::libmemcached::Instance* serve #ifndef WIN32 WATCHPOINT_ASSERT(server->fd == INVALID_SOCKET); - if ((server->fd= socket(AF_UNIX, SOCK_STREAM, 0)) < 0) + int type= SOCK_STREAM; + if (SOCK_CLOEXEC) + { + type|= SOCK_CLOEXEC; + } + + if (SOCK_NONBLOCK) + { + type|= SOCK_NONBLOCK; + } + + if ((server->fd= socket(AF_UNIX, type, 0)) < 0) { memcached_set_errno(*server, errno, NULL); return MEMCACHED_CONNECTION_FAILURE; @@ -481,11 +502,16 @@ static memcached_return_t network_connect(org::libmemcached::Instance* server) } int type= server->address_info_next->ai_socktype; - if (HAVE_SOCK_CLOEXEC) + if (SOCK_CLOEXEC) { type|= SOCK_CLOEXEC; } + if (SOCK_NONBLOCK) + { + type|= SOCK_NONBLOCK; + } + if ((server->fd= socket(server->address_info_next->ai_family, type, server->address_info_next->ai_protocol)) < 0) @@ -493,15 +519,17 @@ static memcached_return_t network_connect(org::libmemcached::Instance* server) return memcached_set_errno(*server, get_socket_errno(), NULL); } - if (HAVE_SOCK_CLOEXEC == 0) + // If SOCK_CLOEXEC exists then we don't need to call the following + if (SOCK_CLOEXEC == 0) { -#ifdef FD_CLOEXEC - int rval; - do + if (FD_CLOEXEC) { - rval= fcntl (server->fd, F_SETFD, FD_CLOEXEC); - } while (rval == -1 && (errno == EINTR or errno == EAGAIN)); -#endif + int rval; + do + { + rval= fcntl (server->fd, F_SETFD, FD_CLOEXEC); + } while (rval == -1 && (errno == EINTR or errno == EAGAIN)); + } } set_socket_options(server); diff --git a/m4/check_sock_cloexec.m4 b/m4/check_sock_cloexec.m4 deleted file mode 100644 index 8dcd14f5..00000000 --- a/m4/check_sock_cloexec.m4 +++ /dev/null @@ -1,27 +0,0 @@ -# Author? -# AX_CHECK_SOCK_CLOEXEC([action-if-found], [action-if-not-found]) -# - -#serial 2 - -AC_DEFUN([AX_CHECK_SOCK_CLOEXEC], [ - AC_CACHE_CHECK([whether SOCK_CLOEXEC is supported], [ax_cv_sock_cloexec], [ - AC_LANG_PUSH([C]) - AC_RUN_IFELSE([ - AC_LANG_PROGRAM([ - - /* SOCK_CLOEXEC test */ -#include -#include - - ], [ - int s= socket(PF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0); - return (s == -1); - ])], - [ax_cv_sock_cloexec=yes], - [ax_cv_sock_cloexec=no], - [AC_MSG_WARN([test program execution failed])]) - ]) - - AS_IF([test x"ax_cv_sock_cloexec" = xyes], [AC_MSG_RESULT([yes]) ; $1], AC_MSG_RESULT([no]) ; $2) - ])