Encapsulate more of the cleanup logic.
authorBrian Aker <brian@tangent.org>
Fri, 25 Jan 2013 02:26:50 +0000 (21:26 -0500)
committerBrian Aker <brian@tangent.org>
Fri, 25 Jan 2013 02:26:50 +0000 (21:26 -0500)
libmemcached/connect.cc
libmemcached/error.cc
libmemcached/hosts.cc
libmemcached/instance.cc
libmemcached/instance.hpp
libmemcached/sasl.cc
libmemcached/server.cc
libtest/cmdline.cc
libtest/cmdline.h
util/string.hpp

index 1efeee57aee003ecd2a2e9538eef693d5e57ced7..148735389ff2ac4569b9b46ea575f0635e0793be 100644 (file)
@@ -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);
     }
   }
index f4ce85d0c70498383518502eb8d46950de0091a3..d23802542f299a113f1f1dc40b00b52df985c2e2 100644 (file)
@@ -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;
   }
 
index 20677694ff4db491feaa8e821e7dc30c733f06df..747245f956df0d600f076f106d6d3773d0da9ff2 100644 (file)
@@ -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)
index 593465d8684b324b2026286b18e9a766faabb481..ca3a30d8872ba4e7edc2051865724afdbb726d48 100644 (file)
@@ -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);
 }
index bab2f9ea9f776ae2f358dc45237725d5d754f24f..d0a772b926c03fcf5a53668a5f8335442d69e0e6 100644 (file)
@@ -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);
index 69d6223d9533d60629d5ffdedcc6f319a0b32700..8e180d6f618f0d0a207488d70891097817d9ab81 100644 (file)
@@ -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);
 
index 27e51e96691784cab0c3281c358dd29367e54ac0..ba40a6ee3fc3c07b31f3896d60cb7b64170dd2ab 100644 (file)
@@ -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;
index df4ae1f3e132058e42ce1e2fb4d9ad6ee0045269..741d18d7f7e480d9e3cf9e44c9cec60246d01807 100644 (file)
@@ -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);
       }
 
index 8db4804a1081010678001b876c73b23daffb00d3..6bf71b644c6c842215892823e0030f3903868b71 100644 (file)
@@ -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;
index 9f6786985f843631344fcd0b4ff04930c7df71a6..bf87eab8ff5636622146407a5f2ec9b27899f2ae 100644 (file)
@@ -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