X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=libmemcached%2Fconnect.cc;h=8efbb119b30e10c59756a13d0c006af6bb7cb681;hb=e4e6bde654ddb9017f090f3a36d6e9f5fd70b354;hp=707f7344387af9fbe1661387294d1de7c01a6a94;hpb=805884743f76a5c4dad8ad20e6033630a92e71df;p=awesomized%2Flibmemcached diff --git a/libmemcached/connect.cc b/libmemcached/connect.cc index 707f7344..8efbb119 100644 --- a/libmemcached/connect.cc +++ b/libmemcached/connect.cc @@ -37,7 +37,6 @@ #include -#include #include #include @@ -49,18 +48,14 @@ static memcached_return_t connect_poll(memcached_server_st *ptr) size_t loop_max= 5; - while (--loop_max) // Should only loop on cases of ERESTART or EINTR + if (ptr->root->poll_timeout == 0) { - int error; - if (ptr->root->poll_timeout) - { - error= poll(fds, 1, ptr->root->connect_timeout); - } - else - { - error= 0; - } + return memcached_set_error(*ptr, MEMCACHED_TIMEOUT, MEMCACHED_AT); + } + while (--loop_max) // Should only loop on cases of ERESTART or EINTR + { + int error= poll(fds, 1, ptr->root->connect_timeout); switch (error) { case 1: @@ -105,13 +100,14 @@ static memcached_return_t connect_poll(memcached_server_st *ptr) int err; socklen_t len= sizeof (err); (void)getsockopt(ptr->fd, SOL_SOCKET, SO_ERROR, &err, &len); - ptr->cached_errno= (err == 0) ? get_socket_errno() : err; + memcached_set_errno(*ptr, (err == 0) ? get_socket_errno() : err, MEMCACHED_AT); } else { - ptr->cached_errno= get_socket_errno(); + memcached_set_errno(*ptr, get_socket_errno(), MEMCACHED_AT); } + WATCHPOINT_ASSERT(ptr->fd != INVALID_SOCKET); (void)closesocket(ptr->fd); ptr->fd= INVALID_SOCKET; ptr->state= MEMCACHED_SERVER_STATE_NEW; @@ -127,7 +123,7 @@ static memcached_return_t connect_poll(memcached_server_st *ptr) static memcached_return_t set_hostinfo(memcached_server_st *server) { - assert(! server->address_info); // We cover the case where a programming mistake has been made. + WATCHPOINT_ASSERT(not server->address_info); // We cover the case where a programming mistake has been made. if (server->address_info) { freeaddrinfo(server->address_info); @@ -138,7 +134,9 @@ static memcached_return_t set_hostinfo(memcached_server_st *server) char str_port[NI_MAXSERV]; int length= snprintf(str_port, NI_MAXSERV, "%u", (uint32_t)server->port); if (length >= NI_MAXSERV || length < 0) + { return MEMCACHED_FAILURE; + } struct addrinfo hints; memset(&hints, 0, sizeof(struct addrinfo)); @@ -178,7 +176,7 @@ static memcached_return_t set_hostinfo(memcached_server_st *server) default: { WATCHPOINT_STRING(server->hostname); - WATCHPOINT_STRING(gai_strerror(e)); + WATCHPOINT_STRING(gai_strerror(errcode)); return memcached_set_error(*server, MEMCACHED_HOST_LOOKUP_FAILURE, MEMCACHED_AT, memcached_string_make_from_cstr(gai_strerror(errcode))); } } @@ -350,7 +348,8 @@ static memcached_return_t unix_socket_connect(memcached_server_st *ptr) if ((ptr->fd= socket(AF_UNIX, SOCK_STREAM, 0)) < 0) { - return memcached_set_errno(*ptr, errno, NULL); + memcached_set_errno(*ptr, errno, NULL); + return MEMCACHED_CONNECTION_FAILURE; } struct sockaddr_un servAddr; @@ -377,7 +376,8 @@ static memcached_return_t unix_socket_connect(memcached_server_st *ptr) default: WATCHPOINT_ERRNO(errno); - return memcached_set_errno(*ptr, errno, MEMCACHED_AT); + memcached_set_errno(*ptr, errno, MEMCACHED_AT); + return MEMCACHED_CONNECTION_FAILURE; } } } while (0); @@ -482,15 +482,16 @@ static memcached_return_t network_connect(memcached_server_st *ptr) break; case EINTR: // Special case, we retry ai_addr + WATCHPOINT_ASSERT(ptr->fd != INVALID_SOCKET); (void)closesocket(ptr->fd); ptr->fd= INVALID_SOCKET; - ptr->state= MEMCACHED_SERVER_STATE_NEW; continue; default: break; } + WATCHPOINT_ASSERT(ptr->fd != INVALID_SOCKET); (void)closesocket(ptr->fd); ptr->fd= INVALID_SOCKET; ptr->address_info_next= ptr->address_info_next->ai_next; @@ -542,7 +543,9 @@ memcached_return_t memcached_connect(memcached_server_write_instance_st ptr) memcached_return_t rc= MEMCACHED_NO_SERVERS; if (ptr->fd != INVALID_SOCKET) + { return MEMCACHED_SUCCESS; + } LIBMEMCACHED_MEMCACHED_CONNECT_START(); @@ -592,11 +595,12 @@ memcached_return_t memcached_connect(memcached_server_write_instance_st ptr) case MEMCACHED_CONNECTION_TCP: rc= network_connect(ptr); #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT - if (ptr->fd != INVALID_SOCKET && ptr->root->sasl.callbacks) + if (ptr->fd != INVALID_SOCKET and ptr->root->sasl.callbacks) { rc= memcached_sasl_authenticate_connection(ptr); - if (memcached_failed(rc)) + if (memcached_failed(rc) and ptr->fd != INVALID_SOCKET) { + WATCHPOINT_ASSERT(ptr->fd != INVALID_SOCKET); (void)closesocket(ptr->fd); ptr->fd= INVALID_SOCKET; }