From: Brian Aker Date: Fri, 25 Jan 2013 02:26:50 +0000 (-0500) Subject: Encapsulate more of the cleanup logic. X-Git-Tag: 1.0.16~3^2~2 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=51413212545b0e50b0f79f13994a792402814614;p=awesomized%2Flibmemcached Encapsulate more of the cleanup logic. --- diff --git a/libmemcached/connect.cc b/libmemcached/connect.cc index 1efeee57..14873538 100644 --- a/libmemcached/connect.cc +++ b/libmemcached/connect.cc @@ -164,12 +164,7 @@ static memcached_return_t connect_poll(org::libmemcached::Instance* server) static memcached_return_t set_hostinfo(org::libmemcached::Instance* server) { assert(server->type != MEMCACHED_CONNECTION_UNIX_SOCKET); - if (server->address_info) - { - freeaddrinfo(server->address_info); - server->address_info= NULL; - server->address_info_next= NULL; - } + server->clear_addrinfo(); char str_port[MEMCACHED_NI_MAXSERV]; int length= snprintf(str_port, MEMCACHED_NI_MAXSERV, "%u", uint32_t(server->port())); @@ -182,9 +177,7 @@ static memcached_return_t set_hostinfo(org::libmemcached::Instance* server) struct addrinfo hints; memset(&hints, 0, sizeof(struct addrinfo)); -#if 0 hints.ai_family= AF_INET; -#endif if (memcached_is_udp(server->root)) { hints.ai_protocol= IPPROTO_UDP; @@ -199,54 +192,35 @@ static memcached_return_t set_hostinfo(org::libmemcached::Instance* server) assert(server->address_info == NULL); assert(server->address_info_next == NULL); int errcode; - switch(errcode= getaddrinfo(server->hostname, str_port, &hints, &server->address_info)) + assert(server->hostname()); + switch(errcode= getaddrinfo(server->hostname(), str_port, &hints, &server->address_info)) { case 0: + server->address_info_next= server->address_info; + server->state= MEMCACHED_SERVER_STATE_ADDRINFO; break; case EAI_AGAIN: return memcached_set_error(*server, MEMCACHED_TIMEOUT, MEMCACHED_AT, memcached_string_make_from_cstr(gai_strerror(errcode))); case EAI_SYSTEM: - if (server->address_info) - { - freeaddrinfo(server->address_info); - server->address_info= NULL; - server->address_info_next= NULL; - } + server->clear_addrinfo(); return memcached_set_errno(*server, errno, MEMCACHED_AT, memcached_literal_param("getaddrinfo(EAI_SYSTEM)")); case EAI_BADFLAGS: - if (server->address_info) - { - freeaddrinfo(server->address_info); - server->address_info= NULL; - server->address_info_next= NULL; - } + server->clear_addrinfo(); return memcached_set_error(*server, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, memcached_literal_param("getaddrinfo(EAI_BADFLAGS)")); case EAI_MEMORY: - if (server->address_info) - { - freeaddrinfo(server->address_info); - server->address_info= NULL; - server->address_info_next= NULL; - } + server->clear_addrinfo(); return memcached_set_error(*server, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT, memcached_literal_param("getaddrinfo(EAI_MEMORY)")); default: { - if (server->address_info) - { - freeaddrinfo(server->address_info); - server->address_info= NULL; - server->address_info_next= NULL; - } + server->clear_addrinfo(); return memcached_set_error(*server, MEMCACHED_HOST_LOOKUP_FAILURE, MEMCACHED_AT, memcached_string_make_from_cstr(gai_strerror(errcode))); } } - server->address_info_next= server->address_info; - server->state= MEMCACHED_SERVER_STATE_ADDRINFO; return MEMCACHED_SUCCESS; } @@ -471,7 +445,7 @@ static memcached_return_t unix_socket_connect(org::libmemcached::Instance* serve memset(&servAddr, 0, sizeof (struct sockaddr_un)); servAddr.sun_family= AF_UNIX; - strncpy(servAddr.sun_path, server->hostname, sizeof(servAddr.sun_path)); /* Copy filename */ + strncpy(servAddr.sun_path, server->hostname(), sizeof(servAddr.sun_path)); /* Copy filename */ if (connect(server->fd, (struct sockaddr *)&servAddr, sizeof(servAddr)) == -1) { @@ -533,11 +507,8 @@ static memcached_return_t network_connect(org::libmemcached::Instance* server) } } - if (server->address_info_next == NULL) - { - server->address_info_next= server->address_info; - server->state= MEMCACHED_SERVER_STATE_ADDRINFO; - } + assert(server->address_info_next); + assert(server->address_info); /* Create the socket */ while (server->address_info_next and server->fd == INVALID_SOCKET) @@ -754,7 +725,7 @@ static memcached_return_t _memcached_connect(org::libmemcached::Instance* server return memcached_set_error(*server, MEMCACHED_INVALID_HOST_PROTOCOL, MEMCACHED_AT, memcached_literal_param("SASL is not supported for UDP connections")); } - if (server->hostname[0] == '/') + if (server->hostname()[0] == '/') { server->type= MEMCACHED_CONNECTION_UNIX_SOCKET; } @@ -771,7 +742,6 @@ static memcached_return_t _memcached_connect(org::libmemcached::Instance* server if (server->fd != INVALID_SOCKET and server->root->sasl.callbacks) { rc= memcached_sasl_authenticate_connection(server); - fprintf(stderr, "%s:%d %s\n", __FILE__, __LINE__, memcached_strerror(NULL, rc)); if (memcached_failed(rc) and server->fd != INVALID_SOCKET) { WATCHPOINT_ASSERT(server->fd != INVALID_SOCKET); @@ -811,7 +781,7 @@ static memcached_return_t _memcached_connect(org::libmemcached::Instance* server if (in_timeout) { char buffer[1024]; - int snprintf_length= snprintf(buffer, sizeof(buffer), "%s:%d", server->hostname, int(server->port())); + int snprintf_length= snprintf(buffer, sizeof(buffer), "%s:%d", server->hostname(), int(server->port())); return memcached_set_error(*server, MEMCACHED_SERVER_TEMPORARILY_DISABLED, MEMCACHED_AT, buffer, snprintf_length); } } diff --git a/libmemcached/error.cc b/libmemcached/error.cc index f4ce85d0..d2380254 100644 --- a/libmemcached/error.cc +++ b/libmemcached/error.cc @@ -262,12 +262,12 @@ static inline size_t append_host_to_string(org::libmemcached::Instance& self, ch case MEMCACHED_CONNECTION_TCP: case MEMCACHED_CONNECTION_UDP: size+= snprintf(buffer, buffer_length, " host: %s:%d", - self.hostname, int(self.port())); + self.hostname(), int(self.port())); break; case MEMCACHED_CONNECTION_UNIX_SOCKET: size+= snprintf(buffer, buffer_length, " socket: %s", - self.hostname); + self.hostname()); break; } diff --git a/libmemcached/hosts.cc b/libmemcached/hosts.cc index 20677694..747245f9 100644 --- a/libmemcached/hosts.cc +++ b/libmemcached/hosts.cc @@ -49,7 +49,7 @@ static int compare_servers(const void *p1, const void *p2) memcached_server_instance_st a= (memcached_server_instance_st)p1; memcached_server_instance_st b= (memcached_server_instance_st)p2; - int return_value= strcmp(a->hostname, b->hostname); + int return_value= strcmp(a->_hostname, b->_hostname); if (return_value == 0) { @@ -224,7 +224,7 @@ static memcached_return_t update_continuum(Memcached *ptr) if (DEBUG) { printf("ketama_weighted:%s|%d|%llu|%u\n", - list[host_index].hostname, + list[host_index]._hostname, list[host_index].port(), (unsigned long long)list[host_index].weight, pointer_per_server); @@ -245,7 +245,7 @@ static memcached_return_t update_continuum(Memcached *ptr) // If hostname is not available then: /ip:port-index sort_host_length= snprintf(sort_host, sizeof(sort_host), "/%s:%u-%u", - list[host_index].hostname, + list[host_index]._hostname, (uint32_t)list[host_index].port(), pointer_index); @@ -290,14 +290,14 @@ static memcached_return_t update_continuum(Memcached *ptr) { sort_host_length= snprintf(sort_host, sizeof(sort_host), "%s-%u", - list[host_index].hostname, + list[host_index]._hostname, pointer_index - 1); } else { sort_host_length= snprintf(sort_host, sizeof(sort_host), "%s:%u-%u", - list[host_index].hostname, + list[host_index]._hostname, (uint32_t)list[host_index].port(), pointer_index - 1); } @@ -474,13 +474,13 @@ memcached_return_t memcached_instance_push(memcached_st *ptr, const struct org:: // instance allocated. for (uint32_t x= 0; x < number_of_hosts; ++x, ++original_host_size) { - WATCHPOINT_ASSERT(list[x].hostname[0] != 0); + WATCHPOINT_ASSERT(list[x]._hostname[0] != 0); // We have extended the array, and now we will find it, and use it. org::libmemcached::Instance* instance= memcached_instance_fetch(ptr, original_host_size); WATCHPOINT_ASSERT(instance); - memcached_string_t hostname= { memcached_string_make_from_cstr(list[x].hostname) }; + memcached_string_t hostname= { memcached_string_make_from_cstr(list[x]._hostname) }; if (__instance_create_with(ptr, instance, hostname, list[x].port(), list[x].weight, list[x].type) == NULL) diff --git a/libmemcached/instance.cc b/libmemcached/instance.cc index 593465d8..ca3a30d8 100644 --- a/libmemcached/instance.cc +++ b/libmemcached/instance.cc @@ -84,8 +84,7 @@ static inline void _server_init(org::libmemcached::Instance* self, Memcached *ro self->version= UINT_MAX; } self->limit_maxbytes= 0; - memcpy(self->hostname, hostname.c_str, hostname.size); - self->hostname[hostname.size]= 0; + self->hostname(hostname); } static org::libmemcached::Instance* _server_create(org::libmemcached::Instance* self, const memcached_st *memc) @@ -167,12 +166,7 @@ void __instance_free(org::libmemcached::Instance* self) { memcached_quit_server(self, false); - if (self->address_info) - { - freeaddrinfo(self->address_info); - self->address_info= NULL; - self->address_info_next= NULL; - } + self->clear_addrinfo(); assert(self->address_info_next == NULL); memcached_error_free(*self); @@ -297,10 +291,10 @@ static org::libmemcached::Instance* memcached_instance_clone(org::libmemcached:: return NULL; } - memcached_string_t hostname= { memcached_string_make_from_cstr(source->hostname) }; + memcached_string_t hostname_= { memcached_string_make_from_cstr(source->hostname()) }; return __instance_create_with(source->root, NULL, - hostname, + hostname_, source->port(), source->weight, source->type); } diff --git a/libmemcached/instance.hpp b/libmemcached/instance.hpp index bab2f9ea..d0a772b9 100644 --- a/libmemcached/instance.hpp +++ b/libmemcached/instance.hpp @@ -2,7 +2,7 @@ * * Libmemcached library * - * Copyright (C) 2012 Data Differential, http://datadifferential.com/ + * Copyright (C) 2012-2013 Data Differential, http://datadifferential.com/ * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -56,6 +56,8 @@ # define MEMCACHED_NI_MAXSERV 32 #endif +#include "libmemcached/string.hpp" + namespace org { namespace libmemcached { @@ -119,6 +121,25 @@ struct Instance { return _revents; } + const char* hostname() + { + return _hostname; + } + + void hostname(const memcached_string_t& hostname_) + { + if (hostname_.size) + { + memcpy(_hostname, hostname_.c_str, hostname_.size); + _hostname[hostname_.size]= 0; + } + else + { + memcpy(_hostname, memcached_literal_param("localhost")); + _hostname[memcached_literal_param_size("localhost")]= 0; + } + } + void events(short); void revents(short); @@ -154,7 +175,17 @@ struct Instance { struct memcached_error_t *error_messages; char read_buffer[MEMCACHED_MAX_BUFFER]; char write_buffer[MEMCACHED_MAX_BUFFER]; - char hostname[MEMCACHED_NI_MAXHOST]; + char _hostname[MEMCACHED_NI_MAXHOST]; + + void clear_addrinfo() + { + if (address_info) + { + freeaddrinfo(address_info); + address_info= NULL; + address_info_next= NULL; + } + } }; } // namespace libmemcached @@ -162,7 +193,7 @@ struct Instance { org::libmemcached::Instance* __instance_create_with(memcached_st *memc, org::libmemcached::Instance* self, - const memcached_string_t& hostname, + const memcached_string_t& _hostname, const in_port_t port, uint32_t weight, const memcached_connection_t type); diff --git a/libmemcached/sasl.cc b/libmemcached/sasl.cc index 69d6223d..8e180d6f 100644 --- a/libmemcached/sasl.cc +++ b/libmemcached/sasl.cc @@ -215,7 +215,7 @@ memcached_return_t memcached_sasl_authenticate_connection(org::libmemcached::Ins sasl_conn_t *conn; int ret; - if ((ret= sasl_client_new("memcached", server->hostname, laddr, raddr, server->root->sasl.callbacks, 0, &conn) ) != SASL_OK) + if ((ret= sasl_client_new("memcached", server->_hostname, laddr, raddr, server->root->sasl.callbacks, 0, &conn) ) != SASL_OK) { const char *sasl_error_msg= sasl_errstring(ret, NULL, NULL); diff --git a/libmemcached/server.cc b/libmemcached/server.cc index 27e51e96..ba40a6ee 100644 --- a/libmemcached/server.cc +++ b/libmemcached/server.cc @@ -196,7 +196,7 @@ const char *memcached_server_name(const memcached_server_instance_st self) WATCHPOINT_ASSERT(self); if (self) { - return self->hostname; + return self->_hostname; } return NULL; diff --git a/libtest/cmdline.cc b/libtest/cmdline.cc index df4ae1f3..741d18d7 100644 --- a/libtest/cmdline.cc +++ b/libtest/cmdline.cc @@ -409,8 +409,10 @@ bool Application::slurp() Application::error_t Application::join() { pid_t waited_pid= waitpid(_pid, &_status, 0); + slurp(); if (waited_pid == _pid and WIFEXITED(_status) == false) { + /* What we are looking for here is how the exit status happened. - 127 means that posix_spawn() itself had an error. @@ -423,7 +425,12 @@ Application::error_t Application::join() std::string error_string("posix_spawn() failed pid:"); error_string+= _pid; error_string+= " name:"; - error_string+= built_argv[0]; + error_string+= print_argv(built_argv); + if (stderr_result_length()) + { + error_string+= " stderr: "; + error_string+= stderr_c_str(); + } throw std::logic_error(error_string); } else if (WIFSIGNALED(_status)) @@ -431,9 +438,20 @@ Application::error_t Application::join() if (WTERMSIG(_status) != SIGTERM and WTERMSIG(_status) != SIGHUP) { _app_exit_state= Application::INVALID_POSIX_SPAWN; - std::string error_string(built_argv[0]); + std::string error_string(print_argv(built_argv)); error_string+= " was killed by signal "; error_string+= strsignal(WTERMSIG(_status)); + if (stdout_result_length()) + { + error_string+= " stdout: "; + error_string+= stdout_c_str(); + } + if (stderr_result_length()) + { + error_string+= " stderr: "; + error_string+= stderr_c_str(); + } + throw std::runtime_error(error_string); } diff --git a/libtest/cmdline.h b/libtest/cmdline.h index 8db4804a..6bf71b64 100644 --- a/libtest/cmdline.h +++ b/libtest/cmdline.h @@ -150,6 +150,11 @@ public: return _stdout_buffer.size(); } + const char* stdout_c_str() const + { + return &_stdout_buffer[0]; + } + libtest::vchar_t stderr_result() const { return _stderr_buffer; diff --git a/util/string.hpp b/util/string.hpp index 9f678698..bf87eab8 100644 --- a/util/string.hpp +++ b/util/string.hpp @@ -2,7 +2,7 @@ * * DataDifferential Utility Library * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ + * Copyright (C) 2011-2013 Data Differential, http://datadifferential.com/ * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are