From bf8213041709c75147393c8bd6b51b8f9e064f7c Mon Sep 17 00:00:00 2001 From: Brian Aker Date: Wed, 23 Jan 2013 16:28:45 -0500 Subject: [PATCH] Minor cleanup in confirming socket close. --- configure.ac | 13 +++- libmemcached/assert.hpp | 2 +- libmemcached/byteorder.cc | 2 +- libmemcached/common.h | 95 +++++++++++-------------- libmemcached/connect.cc | 33 +++------ libmemcached/error.cc | 4 +- libmemcached/error.hpp | 4 +- libmemcached/instance.hpp | 1 + libmemcached/io.cc | 23 ++++-- libmemcached/server_instance.h | 17 +++++ libmemcached/string.cc | 2 +- tests/libmemcached-1.0/mem_functions.cc | 6 +- tests/libmemcached-1.0/pool.cc | 10 +-- win32/wrappers.h | 5 +- 14 files changed, 112 insertions(+), 105 deletions(-) diff --git a/configure.ac b/configure.ac index de966c80..2435c69e 100644 --- a/configure.ac +++ b/configure.ac @@ -113,6 +113,7 @@ AC_CHECK_HEADERS_ONCE([arpa/inet.h]) AC_CHECK_HEADERS_ONCE([errno.h]) AC_CHECK_HEADERS_ONCE([execinfo.h]) AC_CHECK_HEADERS_ONCE([fcntl.h]) +AC_CHECK_HEADERS_ONCE([features.h]) AC_CHECK_HEADERS_ONCE([fnmatch.h]) AC_CHECK_HEADERS_ONCE([inttypes.h]) AC_CHECK_HEADERS_ONCE([libintl.h]) @@ -133,6 +134,7 @@ AC_CHECK_HEADERS_ONCE([stdlib.h]) AC_CHECK_HEADERS_ONCE([sys/socket.h]) AC_CHECK_HEADERS_ONCE([sys/sysctl.h]) AC_CHECK_HEADERS_ONCE([sys/time.h]) +AC_CHECK_HEADERS_ONCE([sys/types.h]) AC_CHECK_HEADERS_ONCE([sys/un.h]) AC_CHECK_HEADERS_ONCE([sys/wait.h]) AC_CHECK_HEADERS_ONCE([syslog.h]) @@ -189,7 +191,16 @@ AM_CONDITIONAL([BUILD_CACHE],[test "x$build_cache" = "xyes"]) AX_COMPILER_VENDOR -AC_CHECK_TYPES([in_port_t]) +AC_CHECK_TYPES([in_port_t],,,[[ + #ifdef HAVE_SYS_TYPES_H + # include + #endif + #ifdef HAVE_NETINET_IN_H + # include + #endif + #ifdef HAVE_NETDB_H + # include + #endif]]) AC_CHECK_TYPES([ptrdiff_t]) AC_HEADER_STDBOOL AC_TYPE_INT16_T diff --git a/libmemcached/assert.hpp b/libmemcached/assert.hpp index d66b096f..cd4a194b 100644 --- a/libmemcached/assert.hpp +++ b/libmemcached/assert.hpp @@ -2,7 +2,7 @@ * * libmcachedd client library. * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ + * Copyright (C) 2011-2013 Data Differential, http://datadifferential.com/ * All rights reserved. * * Redistribution and use in source and binary forms, with or without diff --git a/libmemcached/byteorder.cc b/libmemcached/byteorder.cc index eb37fda8..9f11aa84 100644 --- a/libmemcached/byteorder.cc +++ b/libmemcached/byteorder.cc @@ -46,7 +46,7 @@ static inline uint64_t swap64(uint64_t in) /* Little endian, flip the bytes around until someone makes a faster/better * way to do this. */ uint64_t rv= 0; - for (uint8_t x= 0; x < 8; x++) + for (uint8_t x= 0; x < 8; ++x) { rv= (rv << 8) | (in & 0xff); in >>= 8; diff --git a/libmemcached/common.h b/libmemcached/common.h index c7b84c94..4ab26806 100644 --- a/libmemcached/common.h +++ b/libmemcached/common.h @@ -112,15 +112,10 @@ #endif #include -#include -#include +#include "libmemcached/watchpoint.h" +#include "libmemcached/is.h" typedef struct memcached_st Memcached; -#ifdef __cplusplus -# include "libmemcached/instance.hpp" -#endif -#include - #ifdef HAVE_POLL_H # include #else @@ -128,67 +123,57 @@ typedef struct memcached_st Memcached; #endif #ifdef __cplusplus -extern "C" { +org::libmemcached::Instance* memcached_instance_fetch(memcached_st *ptr, uint32_t server_key); #endif -typedef memcached_return_t (*memcached_server_execute_fn)(memcached_st *ptr, memcached_server_write_instance_st server, void *context); - +/* These are private not to be installed headers */ +#include "libmemcached/error.hpp" +#include "libmemcached/memory.h" +#include "libmemcached/io.h" #ifdef __cplusplus -} // extern "C" +# include "libmemcached/string.hpp" +# include "libmemcached/memcached/protocol_binary.h" +# include "libmemcached/io.hpp" +# include "libmemcached/udp.hpp" +# include "libmemcached/do.hpp" +# include "libmemcached/socket.hpp" +# include "libmemcached/connect.hpp" +# include "libmemcached/allocators.hpp" +# include "libmemcached/hash.hpp" +# include "libmemcached/quit.hpp" +# include "libmemcached/instance.hpp" +# include "libmemcached/server_instance.h" +# include "libmemcached/server.hpp" +# include "libmemcached/flag.hpp" +# include "libmemcached/behavior.hpp" +# include "libmemcached/sasl.hpp" +# include "libmemcached/server_list.hpp" #endif +#include "libmemcached/internal.h" +#include "libmemcached/array.h" +#include "libmemcached/libmemcached_probes.h" +#include "libmemcached/byteorder.h" +#include "libmemcached/initialize_query.h" #ifdef __cplusplus -org::libmemcached::Instance* memcached_instance_fetch(memcached_st *ptr, uint32_t server_key); +# include "libmemcached/response.h" #endif -memcached_return_t memcached_server_execute(memcached_st *ptr, - memcached_server_execute_fn callback, - void *context); -/* These are private not to be installed headers */ -#include -#include -#include -#ifdef __cplusplus -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#endif -#include -#include -#include -#include -#include -#ifdef __cplusplus -#include -#endif -#include -#include +#include "libmemcached/namespace.h" +#include "libmemcached/virtual_bucket.h" #ifdef __cplusplus -#include -#include -#include -#include -#include -#include -#include +# include "libmemcached/backtrace.hpp" +# include "libmemcached/assert.hpp" +# include "libmemcached/server.hpp" +# include "libmemcached/key.hpp" +# include "libmemcached/encoding_key.h" +# include "libmemcached/result.h" +# include "libmemcached/version.hpp" #endif -#include +#include "libmemcached/continuum.hpp" #if !defined(__GNUC__) || (__GNUC__ == 2 && __GNUC_MINOR__ < 96) diff --git a/libmemcached/connect.cc b/libmemcached/connect.cc index d2580b5e..1efeee57 100644 --- a/libmemcached/connect.cc +++ b/libmemcached/connect.cc @@ -118,8 +118,7 @@ static memcached_return_t connect_poll(org::libmemcached::Instance* server) } assert_msg(server->fd != INVALID_SOCKET, "poll() was passed an invalid file descriptor"); - (void)closesocket(server->fd); - server->fd= INVALID_SOCKET; + server->reset_socket(); server->state= MEMCACHED_SERVER_STATE_NEW; return memcached_set_errno(*server, local_errno, MEMCACHED_AT); @@ -463,7 +462,7 @@ static memcached_return_t unix_socket_connect(org::libmemcached::Instance* serve type|= SOCK_NONBLOCK; } - if ((server->fd= socket(AF_UNIX, type, 0)) < 0) + if ((server->fd= socket(AF_UNIX, type, 0)) == -1) { return memcached_set_errno(*server, errno, NULL); } @@ -474,7 +473,7 @@ static memcached_return_t unix_socket_connect(org::libmemcached::Instance* serve servAddr.sun_family= AF_UNIX; strncpy(servAddr.sun_path, server->hostname, sizeof(servAddr.sun_path)); /* Copy filename */ - if (connect(server->fd, (struct sockaddr *)&servAddr, sizeof(servAddr)) < 0) + if (connect(server->fd, (struct sockaddr *)&servAddr, sizeof(servAddr)) == -1) { switch (errno) { @@ -484,22 +483,19 @@ static memcached_return_t unix_socket_connect(org::libmemcached::Instance* serve break; case EINTR: - (void)closesocket(server->fd); - server->fd= INVALID_SOCKET; + server->reset_socket(); continue; case EISCONN: /* We were spinning waiting on connect */ { assert(0); // Programmer error - (void)closesocket(server->fd); - server->fd= INVALID_SOCKET; + server->reset_socket(); continue; } default: WATCHPOINT_ERRNO(errno); - (void)closesocket(server->fd); - server->fd= INVALID_SOCKET; + server->reset_socket(); return memcached_set_errno(*server, errno, MEMCACHED_AT); } } @@ -575,7 +571,7 @@ static memcached_return_t network_connect(org::libmemcached::Instance* server) if (set_socket_options(server) == false) { - (void)closesocket(server->fd); + server->reset_socket(); return MEMCACHED_CONNECTION_FAILURE; } @@ -624,8 +620,7 @@ static memcached_return_t network_connect(org::libmemcached::Instance* server) case EINTR: // Special case, we retry ai_addr WATCHPOINT_ASSERT(server->fd != INVALID_SOCKET); - (void)closesocket(server->fd); - server->fd= INVALID_SOCKET; + server->reset_socket(); continue; case ECONNREFUSED: @@ -636,8 +631,7 @@ static memcached_return_t network_connect(org::libmemcached::Instance* server) } WATCHPOINT_ASSERT(server->fd != INVALID_SOCKET); - (void)closesocket(server->fd); - server->fd= INVALID_SOCKET; + server->reset_socket(); server->address_info_next= server->address_info_next->ai_next; } @@ -645,11 +639,7 @@ static memcached_return_t network_connect(org::libmemcached::Instance* server) if (timeout_error_occured) { - if (server->fd != INVALID_SOCKET) - { - (void)closesocket(server->fd); - server->fd= INVALID_SOCKET; - } + server->reset_socket(); } WATCHPOINT_STRING("Never got a good file descriptor"); @@ -785,8 +775,7 @@ static memcached_return_t _memcached_connect(org::libmemcached::Instance* server if (memcached_failed(rc) and server->fd != INVALID_SOCKET) { WATCHPOINT_ASSERT(server->fd != INVALID_SOCKET); - (void)closesocket(server->fd); - server->fd= INVALID_SOCKET; + server->reset_socket(); } } } diff --git a/libmemcached/error.cc b/libmemcached/error.cc index 85326780..f4ce85d0 100644 --- a/libmemcached/error.cc +++ b/libmemcached/error.cc @@ -36,7 +36,9 @@ */ #include +#include #include +#include #define MAX_ERROR_LENGTH 2048 struct memcached_error_t @@ -139,7 +141,7 @@ static void _set(memcached_st& memc, memcached_string_t *str, memcached_return_t if (local_errno) { -#ifdef STRERROR_R_CHAR_P +#if defined(STRERROR_R_CHAR_P) && STRERROR_R_CHAR_P errmsg_ptr= strerror_r(local_errno, errmsg, sizeof(errmsg)); #elif defined(HAVE_STRERROR_R) && HAVE_STRERROR_R strerror_r(local_errno, errmsg, sizeof(errmsg)); diff --git a/libmemcached/error.hpp b/libmemcached/error.hpp index 5b82d13a..cdbc6ab1 100644 --- a/libmemcached/error.hpp +++ b/libmemcached/error.hpp @@ -2,7 +2,7 @@ * * LibMemcached * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ + * Copyright (C) 2011-2013 Data Differential, http://datadifferential.com/ * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -35,7 +35,7 @@ * */ -#include +#include "libmemcached/common.h" #pragma once diff --git a/libmemcached/instance.hpp b/libmemcached/instance.hpp index 8f66e96d..bab2f9ea 100644 --- a/libmemcached/instance.hpp +++ b/libmemcached/instance.hpp @@ -91,6 +91,7 @@ struct Instance { void start_close_socket(); void close_socket(); + void reset_socket(); uint32_t response_count() const { diff --git a/libmemcached/io.cc b/libmemcached/io.cc index 20233c7d..0660fe9d 100644 --- a/libmemcached/io.cc +++ b/libmemcached/io.cc @@ -712,24 +712,35 @@ void org::libmemcached::Instance::start_close_socket() } } +void org::libmemcached::Instance::reset_socket() +{ + if (fd != INVALID_SOCKET) + { + (void)closesocket(fd); + fd= INVALID_SOCKET; + } +} + void org::libmemcached::Instance::close_socket() { if (fd != INVALID_SOCKET) { + int shutdown_options= SHUT_RD; + if (options.is_shutting_down == false) + { + shutdown_options= SHUT_RDWR; + } + /* in case of death shutdown to avoid blocking at close() */ - if (shutdown(fd, SHUT_RD) == SOCKET_ERROR and get_socket_errno() != ENOTCONN) + if (shutdown(fd, shutdown_options) == SOCKET_ERROR and get_socket_errno() != ENOTCONN) { WATCHPOINT_NUMBER(fd); WATCHPOINT_ERRNO(get_socket_errno()); WATCHPOINT_ASSERT(get_socket_errno()); } - if (closesocket(fd) == SOCKET_ERROR) - { - WATCHPOINT_ERRNO(get_socket_errno()); - } + reset_socket(); state= MEMCACHED_SERVER_STATE_NEW; - fd= INVALID_SOCKET; } state= MEMCACHED_SERVER_STATE_NEW; diff --git a/libmemcached/server_instance.h b/libmemcached/server_instance.h index c840384a..3b69b77b 100644 --- a/libmemcached/server_instance.h +++ b/libmemcached/server_instance.h @@ -38,7 +38,24 @@ #pragma once #ifdef __cplusplus +namespace org { namespace libmemcached { struct Instance; } } typedef struct org::libmemcached::Instance* memcached_server_write_instance_st; #else typedef void* memcached_server_write_instance_st; #endif + +#ifdef __cplusplus +extern "C" { +#endif + +typedef memcached_return_t (*memcached_server_execute_fn)(memcached_st *ptr, memcached_server_write_instance_st server, void *context); + +memcached_return_t memcached_server_execute(memcached_st *ptr, + memcached_server_execute_fn callback, + void *context); + +#ifdef __cplusplus +} // extern "C" +#endif + + diff --git a/libmemcached/string.cc b/libmemcached/string.cc index 7826bd10..ce96b98c 100644 --- a/libmemcached/string.cc +++ b/libmemcached/string.cc @@ -115,7 +115,7 @@ memcached_string_st *memcached_string_create(Memcached *memc, memcached_string_s return NULL; } - self->options.is_initialized= true; + memcached_set_initialized(self, true); WATCHPOINT_ASSERT(self->string == self->end); diff --git a/tests/libmemcached-1.0/mem_functions.cc b/tests/libmemcached-1.0/mem_functions.cc index 89eca072..44143983 100644 --- a/tests/libmemcached-1.0/mem_functions.cc +++ b/tests/libmemcached-1.0/mem_functions.cc @@ -39,7 +39,7 @@ #include #if defined(HAVE_LIBUUID) && HAVE_LIBUUID -#include +# include #endif /* @@ -47,8 +47,8 @@ */ #include -#include -#include +#include "libmemcached/is.h" +#include "libmemcached/server_instance.h" #include diff --git a/tests/libmemcached-1.0/pool.cc b/tests/libmemcached-1.0/pool.cc index 30cc9660..050d2927 100644 --- a/tests/libmemcached-1.0/pool.cc +++ b/tests/libmemcached-1.0/pool.cc @@ -504,15 +504,9 @@ test_return_t regression_bug_962815(memcached_st *memc) test_compare(0, pthread_join(pid[x], NULL)); } - if (pool) - { - memcached_pool_destroy(pool); - } + memcached_pool_destroy(pool); - if (master) - { - memcached_free(master); - } + memcached_free(master); return TEST_SUCCESS; } diff --git a/win32/wrappers.h b/win32/wrappers.h index 976303cb..5bf325e9 100644 --- a/win32/wrappers.h +++ b/win32/wrappers.h @@ -9,8 +9,7 @@ * to avoid a bunch of ifdefs in the rest of the code * */ -#ifndef WIN32_WRAPPERS_H -#define WIN32_WRAPPERS_H 1 +#pragma once #include @@ -76,5 +75,3 @@ #define waitpid(a,b,c) (-1) #define fnmatch(a,b,c) (-1) #define sleep(a) Sleep(a*1000) - -#endif /* WIN32_WRAPPERS_H */ -- 2.30.2