return EXIT_FAILURE;
}
- if (opt_username and initialize_sasl(memc, opt_username, opt_passwd) == false)
+ if (opt_username)
{
- memcached_free(memc);
- return EXIT_FAILURE;
+ memcached_return_t ret;
+ if (memcached_failed(ret= memcached_set_sasl_auth_data(memc, opt_username, opt_passwd)))
+ {
+ std::cerr << memcached_last_error_message(memc) << std::endl;
+ memcached_free(memc);
+ return EXIT_FAILURE;
+ }
}
while (optind < argc)
if (opt_hash)
free(opt_hash);
- shutdown_sasl();
-
return return_code;
}
return EXIT_FAILURE;
}
- if (initialize_sasl(memc, opt_username, opt_passwd) == false)
+ if (opt_username)
{
- std::cerr << "Failed to initialize SASL support." << std::endl;
- memcached_free(memc);
- return EXIT_FAILURE;
+ memcached_return_t ret;
+ if (memcached_failed(ret= memcached_set_sasl_auth_data(memc, opt_username, opt_passwd)))
+ {
+ std::cerr << memcached_last_error_message(memc) << std::endl;
+ memcached_free(memc);
+ return EXIT_FAILURE;
+ }
}
while (optind < argc)
free(opt_servers);
if (opt_hash)
free(opt_hash);
- shutdown_sasl();
return return_code;
}
return EXIT_FAILURE;
}
- if (opt_username and initialize_sasl(memc, opt_username, opt_passwd) == false)
+ if (opt_username)
{
- std::cerr << "Failed to initialize SASL support." << std::endl;
-
- memcached_free(memc);
- return EXIT_FAILURE;
+ memcached_return_t ret;
+ if (memcached_failed(ret= memcached_set_sasl_auth_data(memc, opt_username, opt_passwd)))
+ {
+ std::cerr << memcached_last_error_message(memc) << std::endl;
+ memcached_free(memc);
+ return EXIT_FAILURE;
+ }
}
rc= memcached_dump(memc, callbacks, NULL, 1);
if (opt_hash)
free(opt_hash);
- shutdown_sasl();
-
return EXIT_SUCCESS;
}
return EXIT_FAILURE;
}
-
- if (!initialize_sasl(memc, opt_username, opt_passwd))
+ if (opt_username)
{
- memcached_free(memc);
- return EXIT_FAILURE;
+ memcached_return_t ret;
+ if (memcached_failed(ret= memcached_set_sasl_auth_data(memc, opt_username, opt_passwd)))
+ {
+ std::cerr << memcached_last_error_message(memc) << std::endl;
+ memcached_free(memc);
+ return EXIT_FAILURE;
+ }
}
rc = memcached_flush(memc, opt_expire);
free(opt_servers);
- shutdown_sasl();
-
return EXIT_SUCCESS;
}
free(opt_servers);
- shutdown_sasl();
-
return exit_code;
}
opt_servers= strdup(temp);
else
{
- fprintf(stderr, "No Servers provided\n");
- exit(1);
+ std::cerr << "No Servers provided" << std::endl;
+ return EXIT_FAILURE;
}
}
return EXIT_FAILURE;
}
-
- if (opt_username and initialize_sasl(memc, opt_username, opt_passwd) == false)
+ if (opt_username)
{
- std::cerr << "Failed to initialize SASL support." << std::endl;
-
- memcached_free(memc);
- return EXIT_FAILURE;
+ memcached_return_t ret;
+ if (memcached_failed(ret= memcached_set_sasl_auth_data(memc, opt_username, opt_passwd)))
+ {
+ std::cerr << memcached_last_error_message(memc) << std::endl;
+ memcached_free(memc);
+ return EXIT_FAILURE;
+ }
}
while (optind < argc)
{
if (opt_verbose)
- printf("key: %s\nexpires: %llu\n", argv[optind], (unsigned long long)opt_expire);
- rc = memcached_delete(memc, argv[optind], strlen(argv[optind]), opt_expire);
+ {
+ std::cout << "key: " << argv[optind] << std::endl;
+ std::cout << "expires: " << opt_expire << std::endl;
+ }
+ rc= memcached_delete(memc, argv[optind], strlen(argv[optind]), opt_expire);
- if (rc != MEMCACHED_SUCCESS)
+ if (memcached_failed(rc))
{
- fprintf(stderr, "memrm: %s: memcache error %s",
- argv[optind], memcached_strerror(memc, rc));
+ std::cerr << PROGRAM_NAME << ": " << argv[optind] << ": error " << memcached_strerror(memc, rc) << std::endl;
+
if (memcached_last_error_errno(memc))
- fprintf(stderr, " system error %s", strerror(memcached_last_error_errno(memc)));
- fprintf(stderr, "\n");
+ {
+ std::cerr << " system error " << strerror(memcached_last_error_errno(memc));
+ }
+ std::cerr << std::endl;
return_code= -1;
}
if (opt_hash)
free(opt_hash);
- shutdown_sasl();
-
return return_code;
}
}
}
-
-static char *username;
-static char *passwd;
-
-#if defined(LIBMEMCACHED_WITH_SASL_SUPPORT) && LIBMEMCACHED_WITH_SASL_SUPPORT
-
-static int get_username(void *context, int id, const char **result, unsigned int *len)
-{
- (void)context;
- if (!result || (id != SASL_CB_USER && id != SASL_CB_AUTHNAME))
- return SASL_BADPARAM;
-
- *result= username;
- if (len)
- {
- *len= (username == NULL) ? 0 : (unsigned int)strlen(username);
- }
-
- return SASL_OK;
-}
-
-static int get_password(sasl_conn_t *conn, void *context, int id,
- sasl_secret_t **psecret)
-{
- (void)context;
- static sasl_secret_t* ptr;
-
- if (!conn || ! psecret || id != SASL_CB_PASS)
- return SASL_BADPARAM;
-
- if (passwd == NULL)
- {
- *psecret= NULL;
- return SASL_OK;
- }
-
- size_t len= strlen(passwd);
- ptr= (sasl_secret_t *)malloc(sizeof(sasl_secret_t) + len +1);
- if (not ptr)
- return SASL_NOMEM;
-
- ptr->len= len;
- memcpy(ptr->data, passwd, len);
- ptr->data[len]= 0;
-
- *psecret= ptr;
- return SASL_OK;
-}
-
-typedef int (*local_sasl_fn)(void);
-
-/* callbacks we support */
-static sasl_callback_t sasl_callbacks[] = {
- { SASL_CB_USER, (local_sasl_fn)get_username, NULL },
- { SASL_CB_AUTHNAME, (local_sasl_fn)get_username, NULL },
- { SASL_CB_PASS, (local_sasl_fn)get_password, NULL },
- { SASL_CB_LIST_END, NULL, NULL }
-};
-
-#endif
-
-bool initialize_sasl(memcached_st *memc, char *user, char *password)
-{
- if (LIBMEMCACHED_WITH_SASL_SUPPORT == 0)
- {
- return false;
- }
-
- if (memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, (uint64_t)true) == 0)
- {
- return false;
- }
-
- if (user != NULL && password != NULL)
- {
- username= user;
- passwd= password;
-
-#if defined(LIBMEMCACHED_WITH_SASL_SUPPORT) && LIBMEMCACHED_WITH_SASL_SUPPORT
- if (sasl_client_init(NULL) != SASL_OK)
- {
- fprintf(stderr, "Failed to initialize sasl library!\n");
- return false;
- }
- memcached_set_sasl_callbacks(memc, sasl_callbacks);
-#else
- (void)memc;
-#endif
- }
-
- return true;
-}
-
-void shutdown_sasl(void)
-{
- if (username or passwd)
- {
-#if defined(LIBMEMCACHED_WITH_SASL_SUPPORT) && LIBMEMCACHED_WITH_SASL_SUPPORT
- sasl_done();
-#endif
- }
-}
-
void initialize_sockets(void)
{
/* Define the function for all platforms to avoid #ifdefs in each program */
AM_CONDITIONAL(BUILDING_LIBMEMCACHED, true)
AM_CONDITIONAL(HAVE_LIBMEMCACHED, false)
-AC_SUBST(_WITH_LIBMEMCACHED_SUPPORT, ["_WITH_LIBMEMCACHED_SUPPORT 1"])
+AC_DEFINE([HAVE_LIBMEMCACHED], [1], [Enables libmemcached Support])
AM_CONDITIONAL(BUILDING_GEARMAN, false)
AM_CONDITIONAL(HAVE_LIBGEARMAN, false)
-AC_SUBST(_WITH_LIBGEARMAN_SUPPORT, ["_WITH_LIBGEARMAN_SUPPORT 0"])
+AC_DEFINE([HAVE_LIBGEARMAN], [0], [Enables libgearman Support])
AC_SEARCH_LIBS(getopt_long, gnugetopt)
AC_SEARCH_LIBS(gethostbyname, nsl)
void _libmemcached_free(const memcached_st*, void *mem, void*)
{
if (mem)
+ {
free(mem);
+ }
}
void *_libmemcached_malloc(const memcached_st *, size_t size, void *)
{
void *ret = _libmemcached_malloc(self, nelem * size, context);
if (not ret)
+ {
memset(ret, 0, nelem * size);
+ }
return ret;
}
memcached_calloc_fn mem_calloc,
void *context)
{
+ if (self == NULL)
+ {
+ return MEMCACHED_INVALID_ARGUMENTS;
+ }
+
/* All should be set, or none should be set */
if (mem_malloc == NULL && mem_free == NULL && mem_realloc == NULL && mem_calloc == NULL)
{
}
else if (mem_malloc == NULL || mem_free == NULL || mem_realloc == NULL || mem_calloc == NULL)
{
- return MEMCACHED_FAILURE;
+ return memcached_set_error(*self, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, memcached_literal_param("NULL parameter provided for one or more allocators"));
}
else
{
memcached_realloc_fn *mem_realloc,
memcached_calloc_fn *mem_calloc)
{
+ if (self == NULL)
+ {
+ return;
+ }
+
*mem_malloc= self->allocators.malloc;
*mem_free= self->allocators.free;
*mem_realloc= self->allocators.realloc;
#ifdef __cplusplus
#include <libmemcached/backtrace.hpp>
#include <libmemcached/assert.hpp>
+#include <libmemcached/server.hpp>
#endif
#include <libmemcached/continuum.hpp>
LIBMEMCACHED_LOCAL
memcached_return_t memcached_purge(memcached_server_write_instance_st ptr);
-LIBMEMCACHED_LOCAL
- memcached_server_st *__server_create_with(const memcached_st *memc,
- memcached_server_write_instance_st host,
- const char *hostname,
- in_port_t port,
- uint32_t weight,
- memcached_connection_t type);
-
static inline memcached_return_t memcached_validate_key_length(size_t key_length, bool binary)
{
hints.ai_protocol= IPPROTO_TCP;
}
+ server->address_info= NULL;
int errcode;
switch(errcode= getaddrinfo(server->hostname, str_port, &hints, &server->address_info))
{
#include <sys/time.h>
/* Protoypes (static) */
-static memcached_return_t server_add(memcached_st *ptr, const char *hostname,
- in_port_t port,
- uint32_t weight,
- memcached_connection_t type);
-
static memcached_return_t update_continuum(memcached_st *ptr);
static int compare_servers(const void *p1, const void *p2)
return MEMCACHED_SUCCESS;
}
+static memcached_return_t server_add(memcached_st *ptr,
+ const memcached_string_t& hostname,
+ in_port_t port,
+ uint32_t weight,
+ memcached_connection_t type)
+{
+ assert_msg(ptr, "Programmer mistake, somehow server_add() was passed a NULL memcached_st");
+ if ( (ptr->flags.use_udp and type != MEMCACHED_CONNECTION_UDP)
+ or ( (type == MEMCACHED_CONNECTION_UDP) and (not ptr->flags.use_udp) ) )
+ {
+ return memcached_set_error(*ptr, MEMCACHED_INVALID_HOST_PROTOCOL, MEMCACHED_AT);
+ }
+
+ memcached_server_st *new_host_list= static_cast<memcached_server_st*>(libmemcached_realloc(ptr, memcached_server_list(ptr),
+ sizeof(memcached_server_st) * (ptr->number_of_hosts + 1)));
+
+ if (not new_host_list)
+ {
+ return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
+ }
+
+ memcached_server_list_set(ptr, new_host_list);
+
+ /* TODO: Check return type */
+ memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, memcached_server_count(ptr));
+
+ if (not __server_create_with(ptr, instance, hostname, port, weight, type))
+ {
+ return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT);
+ }
+
+ if (weight > 1)
+ {
+ ptr->ketama.weighted= true;
+ }
+
+ ptr->number_of_hosts++;
+
+ // @note we place the count in the bottom of the server list
+ instance= memcached_server_instance_fetch(ptr, 0);
+ memcached_servers_set_count(instance, memcached_server_count(ptr));
+
+ return run_distribution(ptr);
+}
+
memcached_return_t memcached_server_push(memcached_st *ptr, const memcached_server_list_st list)
{
instance= memcached_server_instance_fetch(ptr, memcached_server_count(ptr));
WATCHPOINT_ASSERT(instance);
- if (not __server_create_with(ptr, instance, list[x].hostname,
- list[x].port, list[x].weight, list[x].type))
+ memcached_string_t hostname= { memcached_string_make_from_cstr(list[x].hostname) };
+ if (__server_create_with(ptr, instance,
+ hostname,
+ list[x].port, list[x].weight, list[x].type) == NULL)
{
return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT);
}
const char *filename,
uint32_t weight)
{
- if (! filename)
+ if (ptr == NULL)
+ {
return MEMCACHED_FAILURE;
+ }
+
+ memcached_string_t _filename= { memcached_string_make_from_cstr(filename) };
+ if (memcached_is_valid_servername(_filename) == false)
+ {
+ memcached_set_error(*ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, memcached_literal_param("Invalid filename for socket provided"));
+ }
- return server_add(ptr, filename, 0, weight, MEMCACHED_CONNECTION_UNIX_SOCKET);
+ return server_add(ptr, _filename, 0, weight, MEMCACHED_CONNECTION_UNIX_SOCKET);
}
memcached_return_t memcached_server_add_udp(memcached_st *ptr,
in_port_t port,
uint32_t weight)
{
+ if (ptr == NULL)
+ {
+ return MEMCACHED_INVALID_ARGUMENTS;
+ }
+
if (not port)
+ {
port= MEMCACHED_DEFAULT_PORT;
+ }
if (not hostname)
+ {
hostname= "localhost";
+ }
- return server_add(ptr, hostname, port, weight, MEMCACHED_CONNECTION_UDP);
+ memcached_string_t _hostname= { memcached_string_make_from_cstr(hostname) };
+ if (memcached_is_valid_servername(_hostname) == false)
+ {
+ memcached_set_error(*ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, memcached_literal_param("Invalid hostname provided"));
+ }
+
+ return server_add(ptr, _hostname, port, weight, MEMCACHED_CONNECTION_UDP);
}
memcached_return_t memcached_server_add(memcached_st *ptr,
in_port_t port,
uint32_t weight)
{
- if (not port)
- port= MEMCACHED_DEFAULT_PORT;
-
- if (not hostname)
- hostname= "localhost";
-
- return server_add(ptr, hostname, port, weight, hostname[0] == '/' ? MEMCACHED_CONNECTION_UNIX_SOCKET : MEMCACHED_CONNECTION_TCP);
-}
-
-static memcached_return_t server_add(memcached_st *ptr, const char *hostname,
- in_port_t port,
- uint32_t weight,
- memcached_connection_t type)
-{
-
- if ( (ptr->flags.use_udp and type != MEMCACHED_CONNECTION_UDP)
- or ( (type == MEMCACHED_CONNECTION_UDP) and (not ptr->flags.use_udp) ) )
+ if (ptr == NULL)
{
- return MEMCACHED_INVALID_HOST_PROTOCOL;
+ return MEMCACHED_INVALID_ARGUMENTS;
}
- memcached_server_st *new_host_list= static_cast<memcached_server_st*>(libmemcached_realloc(ptr, memcached_server_list(ptr),
- sizeof(memcached_server_st) * (ptr->number_of_hosts + 1)));
-
- if (not new_host_list)
- return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
-
- memcached_server_list_set(ptr, new_host_list);
-
- /* TODO: Check return type */
- memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, memcached_server_count(ptr));
-
- if (not __server_create_with(ptr, instance, hostname, port, weight, type))
+ if (port == 0)
{
- return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT);
+ port= MEMCACHED_DEFAULT_PORT;
}
- if (weight > 1)
+ size_t hostname_length= hostname ? strlen(hostname) : 0;
+ if (hostname_length == 0)
{
- ptr->ketama.weighted= true;
+ hostname= "localhost";
+ hostname_length= sizeof("localhost") -1;
}
- ptr->number_of_hosts++;
+ memcached_string_t _hostname= { hostname, hostname_length };
- // @note we place the count in the bottom of the server list
- instance= memcached_server_instance_fetch(ptr, 0);
- memcached_servers_set_count(instance, memcached_server_count(ptr));
+ if (memcached_is_valid_servername(_hostname) == false)
+ {
+ return memcached_set_error(*ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, memcached_literal_param("Invalid hostname provided"));
+ }
- return run_distribution(ptr);
+ return server_add(ptr, _hostname, port, weight, _hostname.c_str[0] == '/' ? MEMCACHED_CONNECTION_UNIX_SOCKET : MEMCACHED_CONNECTION_TCP);
}
memcached_return_t memcached_server_add_parsed(memcached_st *ptr,
memcpy(buffer, hostname, hostname_length);
buffer[hostname_length]= 0;
- return server_add(ptr, buffer,
+ memcached_string_t _hostname= { buffer, hostname_length };
+
+ return server_add(ptr, _hostname,
port,
weight,
MEMCACHED_CONNECTION_TCP);
libmemcached/protocol/binary_handler.h \
libmemcached/protocol/common.h \
libmemcached/response.h \
+ libmemcached/server.hpp \
libmemcached/server_instance.h \
libmemcached/string.hpp \
libmemcached/virtual_bucket.h
libmemcached_libmemcached_la_LIBADD+= $(LTLIBSASL) $(LTLIBSASL2)
libmemcached_libmemcached_la_SOURCES += libmemcached/sasl.cc
+if HAVE_SASL
+libmemcached_libmemcached_la_CXXFLAGS+= ${PTHREAD_CFLAGS}
+libmemcached_libmemcached_la_LIBADD+= ${PTHREAD_LIBS}
+endif
if HAVE_DTRACE
BUILT_SOURCES+= libmemcached/dtrace_probes.h
#if defined(LIBMEMCACHED_WITH_SASL_SUPPORT) && LIBMEMCACHED_WITH_SASL_SUPPORT
#include <sasl/sasl.h>
+#include <pthread.h>
void memcached_set_sasl_callbacks(memcached_st *ptr,
const sasl_callback_t *callbacks)
return MEMCACHED_SUCCESS;
}
+static void sasl_shutdown_function()
+{
+ sasl_done();
+}
+
+static int sasl_startup_state= SASL_OK;
+static pthread_once_t sasl_startup_once= PTHREAD_ONCE_INIT;
+static void sasl_startup_function(void)
+{
+ sasl_startup_state= sasl_client_init(NULL);
+
+ if (sasl_startup_state == SASL_OK)
+ {
+ (void)atexit(sasl_shutdown_function);
+ }
+}
+
memcached_return_t memcached_sasl_authenticate_connection(memcached_server_st *server)
{
if (LIBMEMCACHED_WITH_SASL_SUPPORT == 0)
return rc;
}
- int ret;
- if ((ret= sasl_client_init(NULL)) != SASL_OK)
+ int pthread_error;
+ if ((pthread_error= pthread_once(&sasl_startup_once, sasl_startup_function)) != 0)
{
- const char *sasl_error_msg= sasl_errstring(ret, NULL, NULL);
+ return memcached_set_errno(*server, pthread_error, MEMCACHED_AT);
+ }
+
+ if (sasl_startup_state != SASL_OK)
+ {
+ const char *sasl_error_msg= sasl_errstring(sasl_startup_state, NULL, NULL);
return memcached_set_error(*server, MEMCACHED_AUTH_PROBLEM, MEMCACHED_AT,
memcached_string_make_from_cstr(sasl_error_msg));
}
sasl_conn_t *conn;
+ int ret;
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);
+
+ sasl_dispose(&conn);
+
return memcached_set_error(*server, MEMCACHED_AUTH_PROBLEM, MEMCACHED_AT,
memcached_string_make_from_cstr(sasl_error_msg));
}
if (ret != SASL_OK and ret != SASL_CONTINUE)
{
const char *sasl_error_msg= sasl_errstring(ret, NULL, NULL);
+
+ sasl_dispose(&conn);
+
return memcached_set_error(*server, MEMCACHED_AUTH_PROBLEM, MEMCACHED_AT,
memcached_string_make_from_cstr(sasl_error_msg));
}
return MEMCACHED_INVALID_ARGUMENTS;
}
+ memcached_return_t ret;
+ if (memcached_failed(ret= memcached_behavior_set(ptr, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1)))
+ {
+ return memcached_set_error(*ptr, ret, MEMCACHED_AT, memcached_literal_param("Unable change to binary protocol which is required for SASL."));
+ }
+
memcached_destroy_sasl_auth_data(ptr);
sasl_callback_t *callbacks= (sasl_callback_t*)libmemcached_calloc(ptr, 4, sizeof(sasl_callback_t));
#include <libmemcached/common.h>
static inline void _server_init(memcached_server_st *self, memcached_st *root,
- const char *hostname, in_port_t port,
+ const memcached_string_t& hostname,
+ in_port_t port,
uint32_t weight, memcached_connection_t type)
{
self->options.is_shutting_down= false;
self->root= root;
self->limit_maxbytes= 0;
- if (hostname)
- {
- strncpy(self->hostname, hostname, NI_MAXHOST - 1);
- }
- else
- {
- self->hostname[0]= 0;
- }
+ memcpy(self->hostname, hostname.c_str, hostname.size);
+ self->hostname[hostname.size]= 0;
}
static memcached_server_st *_server_create(memcached_server_st *self, const memcached_st *memc)
self= (memcached_server_st *)libmemcached_malloc(memc, sizeof(memcached_server_st));
if (not self)
+ {
return NULL; /* MEMCACHED_MEMORY_ALLOCATION_FAILURE */
+ }
self->options.is_allocated= true;
}
return self;
}
-memcached_server_st *__server_create_with(const memcached_st *memc,
+memcached_server_st *__server_create_with(memcached_st *memc,
memcached_server_write_instance_st self,
- const char *hostname, in_port_t port,
- uint32_t weight, memcached_connection_t type)
+ const memcached_string_t& hostname,
+ const in_port_t port,
+ uint32_t weight,
+ const memcached_connection_t type)
{
+ if (memcached_is_valid_servername(hostname) == false)
+ {
+ memcached_set_error(*memc, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, memcached_literal_param("Invalid hostname provided"));
+ return NULL;
+ }
+
self= _server_create(self, memc);
if (not self)
{
freeaddrinfo(self->address_info);
self->address_info= NULL;
+ self->address_info_next= NULL;
}
memcached_error_free(*self);
If we do not have a valid object to clone from, we toss an error.
*/
memcached_server_st *memcached_server_clone(memcached_server_st *destination,
- const memcached_server_st *source)
+ memcached_server_st *source)
{
/* We just do a normal create if source is missing */
if (not source)
+ {
return NULL;
+ }
+ memcached_string_t hostname= { memcached_string_make_from_cstr(source->hostname) };
destination= __server_create_with(source->root, destination,
- source->hostname, source->port, source->weight,
+ hostname,
+ source->port, source->weight,
source->type);
if (not destination)
{
LIBMEMCACHED_LOCAL
memcached_server_st *memcached_server_clone(memcached_server_st *destination,
- const memcached_server_st *source);
+ memcached_server_st *source);
LIBMEMCACHED_API
memcached_server_instance_st memcached_server_get_last_disconnect(const memcached_st *ptr);
--- /dev/null
+/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
+ *
+ * Libmemcached library
+ *
+ * Copyright (C) 2011 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
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * * The names of its contributors may not be used to endorse or
+ * promote products derived from this software without specific prior
+ * written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+
+#pragma once
+
+#include <libmemcached/basic_string.h>
+
+static inline bool memcached_is_valid_servername(const memcached_string_t& arg)
+{
+ return arg.size > 0 or arg.size < NI_MAXHOST;
+}
+
+LIBMEMCACHED_LOCAL
+ memcached_server_st *__server_create_with(memcached_st *memc,
+ memcached_server_write_instance_st host,
+ const memcached_string_t& hostname,
+ const in_port_t port,
+ uint32_t weight,
+ const memcached_connection_t type);
port= MEMCACHED_DEFAULT_PORT;
}
+
/* Increment count for hosts */
count= 1;
if (ptr != NULL)
return NULL;
}
+ memcached_string_t _hostname= { memcached_string_make_from_cstr(hostname) };
/* @todo Check return type */
- if (not __server_create_with(NULL, &new_host_list[count-1], hostname, port, weight, port ? MEMCACHED_CONNECTION_TCP : MEMCACHED_CONNECTION_UNIX_SOCKET))
+ if (not __server_create_with(NULL, &new_host_list[count-1], _hostname, port, weight, port ? MEMCACHED_CONNECTION_TCP : MEMCACHED_CONNECTION_UNIX_SOCKET))
{
*error= memcached_set_errno(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT);
return NULL;
return -1;
}
- if (memcached_failed(*ret= memcached_behavior_set(memc_ptr, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1)))
- {
- memcached_free(memc_ptr);
- return false;
- }
-
if (memcached_failed(*ret= memcached_set_sasl_auth_data(memc_ptr, username, password)))
{
memcached_free(memc_ptr);
memcached_server_instance_st instance=
memcached_server_instance_by_position(memc_ptr, 0);
+#if 0
assert_msg(instance and instance->error_messages, " ");
+#endif
if (instance and instance->error_messages)
{
rc= memcached_server_error_return(instance);
return false;
}
- if (memcached_failed(*ret= memcached_behavior_set(memc_ptr, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1)))
- {
- memcached_free(memc_ptr);
- return false;
- }
-
if (memcached_failed(*ret= memcached_set_sasl_auth_data(memc_ptr, username, password)))
{
memcached_free(memc_ptr);
#include <libtest/memcached.h>
#include <libtest/libtool.hpp>
+#include <libtest/killpid.h>
#include <libtest/test.hpp>
#pragma once
enum test_return_t {
- TEST_SUCCESS= 0, /* Backwards compatibility */
+ TEST_SUCCESS,
TEST_FAILURE,
TEST_MEMORY_ALLOCATION_FAILURE,
TEST_SKIPPED,
libtest/memcached.h \
libtest/runner.h \
libtest/server.h \
+ libtest/server_container.h \
libtest/signal.h \
libtest/stats.h \
libtest/stream.h \
libtest/libtool.cc \
libtest/runner.cc \
libtest/server.cc \
+ libtest/server_container.cc \
libtest/signal.cc \
libtest/test.cc
libtest_unittest_LDADD+= libtest/libtest.la
libtest_unittest_SOURCES= libtest/unittest.cc
check_PROGRAMS+= libtest/unittest
+noinst_PROGRAMS+= libtest/unittest
test-unittest: libtest/unittest
@libtest/unittest
return true;
}
-
-pid_t kill_file(const std::string &filename)
+bool check_pid(const std::string &filename)
{
- pid_t ret= -1;
+ if (filename.empty())
+ {
+ return false;
+ }
+
FILE *fp;
+ if ((fp= fopen(filename.c_str(), "r")))
+ {
+ char pid_buffer[1024];
+
+ char *ptr= fgets(pid_buffer, sizeof(pid_buffer), fp);
+ fclose(fp);
+ if (ptr)
+ {
+ pid_t pid= (pid_t)atoi(pid_buffer);
+ if (pid > 0)
+ {
+ return (::kill(pid, 0) == 0);
+ }
+ }
+ }
+
+ return false;
+}
+
+
+bool kill_file(const std::string &filename)
+{
if (filename.empty())
- return ret;
+ {
+ return true;
+ }
+ FILE *fp;
if ((fp= fopen(filename.c_str(), "r")))
{
char pid_buffer[1024];
pid_t pid= (pid_t)atoi(pid_buffer);
if (pid != 0)
{
- kill_pid(pid);
+ bool ret= kill_pid(pid);
unlink(filename.c_str()); // If this happens we may be dealing with a dead server that left its pid file.
+
+ return ret;
}
}
}
- return ret;
+ return false;
}
#define STRINGIFY(x) #x
bool kill_pid(pid_t pid_arg);
-pid_t kill_file(const std::string &filename);
+bool kill_file(const std::string &filename);
+
+bool check_pid(const std::string &filename);
pid_t get_pid_from_file(const std::string &filename, std::stringstream& error_message);
+
+static inline bool check_pid(pid_t pid_arg)
+{
+ return (pid_arg > 1);
+}
}
pid_t local_pid;
- memcached_return_t rc;
+ memcached_return_t rc= MEMCACHED_SUCCESS;
if (has_socket())
{
- if (username().empty())
- {
- local_pid= libmemcached_util_getpid(socket().c_str(), 0, &rc);
- }
- else
- {
- local_pid= libmemcached_util_getpid2(socket().c_str(), 0, username().c_str(), password().c_str(), &rc);
- }
+ local_pid= libmemcached_util_getpid(socket().c_str(), 0, &rc);
}
else
{
- if (username().empty())
- {
- local_pid= libmemcached_util_getpid(hostname().c_str(), port(), &rc);
- }
- else
- {
- local_pid= libmemcached_util_getpid2(hostname().c_str(), port(), username().c_str(), password().c_str(), &rc);
- }
+ local_pid= libmemcached_util_getpid(hostname().c_str(), port(), &rc);
}
if (error_is_ok and ((memcached_failed(rc) or not is_pid_valid(local_pid))))
if (has_socket())
{
- if (username().empty())
- {
ret= libmemcached_util_ping(socket().c_str(), 0, &rc);
- }
- else
- {
- ret= libmemcached_util_ping2(socket().c_str(), 0, username().c_str(), password().c_str(), &rc);
- }
}
else
{
- if (username().empty())
- {
- ret= libmemcached_util_ping(hostname().c_str(), port(), &rc);
- }
- else
- {
- ret= libmemcached_util_ping2(hostname().c_str(), port(), username().c_str(), password().c_str(), &rc);
- }
+ ret= libmemcached_util_ping(hostname().c_str(), port(), &rc);
}
if (memcached_failed(rc) or not ret)
{
- if (username().empty())
- {
- Error << "libmemcached_util_ping(" << hostname() << ", " << port() << ") error: " << memcached_strerror(NULL, rc);
- }
- else
- {
- Error << "libmemcached_util_ping2(" << hostname() << ", " << port() << ", " << username() << ", " << password() << ") error: " << memcached_strerror(NULL, rc);
- }
+ Error << "libmemcached_util_ping(" << hostname() << ", " << port() << ") error: " << memcached_strerror(NULL, rc);
}
return ret;
return MEMCACHED_SASL_BINARY;
}
+ pid_t get_pid(bool error_is_ok)
+ {
+ // Memcached is slow to start, so we need to do this
+ if (not pid_file().empty())
+ {
+ Wait wait(pid_file(), 0);
+
+ if (error_is_ok and not wait.successful())
+ {
+ Error << "Pidfile was not found:" << pid_file();
+ return -1;
+ }
+ }
+
+ pid_t local_pid;
+ memcached_return_t rc;
+ if (has_socket())
+ {
+ local_pid= libmemcached_util_getpid2(socket().c_str(), 0, username().c_str(), password().c_str(), &rc);
+ }
+ else
+ {
+ local_pid= libmemcached_util_getpid2(hostname().c_str(), port(), username().c_str(), password().c_str(), &rc);
+ }
+
+ if (error_is_ok and ((memcached_failed(rc) or not is_pid_valid(local_pid))))
+ {
+ Error << "libmemcached_util_getpid2(" << memcached_strerror(NULL, rc) << ") username: " << username() << " password: " << password() << " pid: " << local_pid << " for:" << *this;
+ }
+
+ return local_pid;
+ }
+
+ bool ping()
+ {
+ // Memcached is slow to start, so we need to do this
+ if (not pid_file().empty())
+ {
+ Wait wait(pid_file(), 0);
+
+ if (not wait.successful())
+ {
+ Error << "Pidfile was not found:" << pid_file();
+ return -1;
+ }
+ }
+
+ memcached_return_t rc;
+ bool ret;
+
+ if (has_socket())
+ {
+ ret= libmemcached_util_ping2(socket().c_str(), 0, username().c_str(), password().c_str(), &rc);
+ }
+ else
+ {
+ ret= libmemcached_util_ping2(hostname().c_str(), port(), username().c_str(), password().c_str(), &rc);
+ }
+
+ if (memcached_failed(rc) or not ret)
+ {
+ Error << "libmemcached_util_ping2(" << hostname() << ", " << port() << ", " << username() << ", " << password() << ") error: " << memcached_strerror(NULL, rc);
+ }
+
+ return ret;
+ }
+
};
libtest::Server *build_memcached_sasl(const std::string& hostname, const in_port_t try_port, const std::string& username, const std::string &password)
{
+ if (username.empty())
+ {
+ return new MemcachedSaSL(hostname, try_port, false, "memcached", "memcached");
+ }
+
return new MemcachedSaSL(hostname, try_port, false, username, password);
}
libtest::Server *build_memcached_sasl_socket(const std::string& socket_file, const in_port_t try_port, const std::string& username, const std::string &password)
{
+ if (username.empty())
+ {
+ return new MemcachedSaSL(socket_file, try_port, true, "memcached", "memcached");
+ }
+
return new MemcachedSaSL(socket_file, try_port, true, username, password);
}
}
}
-std::string server_startup_st::option_string() const
-{
- std::string temp= server_list;
- rtrim(temp);
- return temp;
-}
-
// If the server exists, kill it
bool Server::cycle()
{
return false;
}
-void server_startup_st::push_server(Server *arg)
-{
- servers.push_back(arg);
-
- char port_str[NI_MAXSERV];
- snprintf(port_str, sizeof(port_str), "%u", int(arg->port()));
-
- std::string server_config_string;
- if (arg->has_socket())
- {
- server_config_string+= "--socket=";
- server_config_string+= '"';
- server_config_string+= arg->socket();
- server_config_string+= '"';
- server_config_string+= " ";
- }
- else
- {
- server_config_string+= "--server=";
- server_config_string+= arg->hostname();
- server_config_string+= ":";
- server_config_string+= port_str;
- server_config_string+= " ";
- }
-
- server_list+= server_config_string;
-
-}
-
-Server* server_startup_st::pop_server()
-{
- Server *tmp= servers.back();
- servers.pop_back();
- return tmp;
-}
-
-void server_startup_st::shutdown(bool remove)
-{
- if (remove)
- {
- for (std::vector<Server *>::iterator iter= servers.begin(); iter != servers.end(); iter++)
- {
- delete *iter;
- }
- servers.clear();
- }
- else
- {
- for (std::vector<Server *>::iterator iter= servers.begin(); iter != servers.end(); iter++)
- {
- if ((*iter)->has_pid() and not (*iter)->kill((*iter)->pid()))
- {
- Error << "Unable to kill:" << *(*iter);
- }
- }
- }
-}
-
-server_startup_st::~server_startup_st()
-{
- shutdown(true);
-}
-
-bool server_startup_st::is_debug() const
-{
- return bool(getenv("LIBTEST_MANUAL_GDB"));
-}
-
-bool server_startup_st::is_valgrind() const
-{
- return bool(getenv("LIBTEST_MANUAL_VALGRIND"));
-}
-
-bool server_startup_st::is_helgrind() const
-{
- return bool(getenv("LIBTEST_MANUAL_HELGRIND"));
-}
-
-
-bool server_startup(server_startup_st& construct, const std::string& server_type, in_port_t try_port, int argc, const char *argv[])
-{
- Outn();
- (void)try_port;
-
- // Look to see if we are being provided ports to use
- {
- char variable_buffer[1024];
- snprintf(variable_buffer, sizeof(variable_buffer), "LIBTEST_PORT_%lu", (unsigned long)construct.count());
-
- char *var;
- if ((var= getenv(variable_buffer)))
- {
- in_port_t tmp= in_port_t(atoi(var));
-
- if (tmp > 0)
- try_port= tmp;
- }
- }
-
- libtest::Server *server= NULL;
- if (0)
- { }
- else if (server_type.compare("gearmand") == 0)
- {
- if (GEARMAND_BINARY)
- {
- if (HAVE_LIBGEARMAN)
- {
- server= build_gearmand("localhost", try_port);
- }
- else
- {
- Error << "Libgearman was not found";
- }
- }
- else
- {
- Error << "No gearmand binary is available";
- }
- }
- else if (server_type.compare("blobslap_worker") == 0)
- {
- if (GEARMAND_BINARY and GEARMAND_BLOBSLAP_WORKER)
- {
- if (HAVE_LIBGEARMAN)
- {
- server= build_blobslap_worker(try_port);
- }
- else
- {
- Error << "Libgearman was not found";
- }
- }
- else
- {
- Error << "No gearmand binary is available";
- }
- }
- else if (server_type.compare("memcached-sasl") == 0)
- {
- if (MEMCACHED_SASL_BINARY)
- {
- if (HAVE_LIBMEMCACHED)
- {
- server= build_memcached_sasl("localhost", try_port, construct.username(), construct.password());
- }
- else
- {
- Error << "Libmemcached was not found";
- }
- }
- else
- {
- Error << "No memcached binary that was compiled with sasl is available";
- }
- }
- else if (server_type.compare("memcached") == 0)
- {
- if (MEMCACHED_BINARY)
- {
- if (HAVE_LIBMEMCACHED)
- {
- server= build_memcached("localhost", try_port);
- }
- else
- {
- Error << "Libmemcached was not found";
- }
- }
- else
- {
- Error << "No memcached binary is available";
- }
- }
- else
- {
- Error << "Failed to start " << server_type << ", no support was found to be compiled in for it.";
- }
-
- if (server == NULL)
- {
- Error << "Failure occured while creating server: " << server_type;
- return false;
- }
-
- /*
- We will now cycle the server we have created.
- */
- if (not server->cycle())
- {
- Error << "Could not start up server " << *server;
- delete server;
- return false;
- }
-
- server->build(argc, argv);
-
- if (construct.is_debug())
- {
- Out << "Pausing for startup, hit return when ready.";
- std::string gdb_command= server->base_command();
- std::string options;
- Out << "run " << server->args(options);
- getchar();
- }
- else if (not server->start())
- {
- Error << "Failed to start " << *server;
- delete server;
- return false;
- }
- else
- {
- Out << "STARTING SERVER(pid:" << server->pid() << "): " << server->running();
- }
-
- construct.push_server(server);
-
- if (default_port() == 0)
- {
- assert(server->has_port());
- set_default_port(server->port());
- }
-
- Outn();
-
- return true;
-}
-
-bool server_startup_st::start_socket_server(const std::string& server_type, const in_port_t try_port, int argc, const char *argv[])
-{
- (void)try_port;
- Outn();
-
- Server *server= NULL;
- if (0)
- { }
- else if (server_type.compare("gearmand") == 0)
- {
- Error << "Socket files are not supported for gearmand yet";
- }
- else if (server_type.compare("memcached-sasl") == 0)
- {
- if (MEMCACHED_SASL_BINARY)
- {
- if (HAVE_LIBMEMCACHED)
- {
- server= build_memcached_sasl_socket("localhost", try_port, username(), password());
- }
- else
- {
- Error << "Libmemcached was not found";
- }
- }
- else
- {
- Error << "No memcached binary is available";
- }
- }
- else if (server_type.compare("memcached") == 0)
- {
- if (MEMCACHED_BINARY)
- {
- if (HAVE_LIBMEMCACHED)
- {
- server= build_memcached_socket("localhost", try_port);
- }
- else
- {
- Error << "Libmemcached was not found";
- }
- }
- else
- {
- Error << "No memcached binary is available";
- }
- }
- else
- {
- Error << "Failed to start " << server_type << ", no support was found to be compiled in for it.";
- }
-
- if (server == NULL)
- {
- Error << "Failure occured while creating server: " << server_type;
- return false;
- }
-
- /*
- We will now cycle the server we have created.
- */
- if (not server->cycle())
- {
- Error << "Could not start up server " << *server;
- delete server;
- return false;
- }
-
- server->build(argc, argv);
-
- if (is_debug())
- {
- Out << "Pausing for startup, hit return when ready.";
- std::string gdb_command= server->base_command();
- std::string options;
- Out << "run " << server->args(options);
- getchar();
- }
- else if (not server->start())
- {
- Error << "Failed to start " << *server;
- delete server;
- return false;
- }
- else
- {
- Out << "STARTING SERVER(pid:" << server->pid() << "): " << server->running();
- }
-
- push_server(server);
-
- set_default_socket(server->socket().c_str());
-
- Outn();
-
- return true;
-}
-
} // namespace libtest
std::ostream& operator<<(std::ostream& output, const libtest::Server &arg);
-class server_startup_st
-{
-private:
- std::string server_list;
- bool _socket;
- bool _sasl;
- std::string _username;
- std::string _password;
-
-public:
-
- uint8_t udp;
- std::vector<Server *> servers;
-
- server_startup_st() :
- _socket(false),
- _sasl(false),
- udp(0)
- { }
-
- bool start_socket_server(const std::string& server_type, const in_port_t try_port, int argc, const char *argv[]);
-
- std::string option_string() const;
-
- size_t count() const
- {
- return servers.size();
- }
-
- const std::string& password() const
- {
- return _password;
- }
-
- const std::string& username() const
- {
- return _username;
- }
-
-
- bool is_debug() const;
- bool is_helgrind() const;
- bool is_valgrind() const;
-
- bool socket()
- {
- return _socket;
- }
-
- bool sasl()
- {
- return _sasl;
- }
-
- void set_socket()
- {
- _socket= true;
- }
-
- void set_sasl(const std::string& username_arg, const std::string& password_arg)
- {
- _sasl= true;
- _username= username_arg;
- _password= password_arg;
- }
-
-
- void shutdown(bool remove= false);
- void push_server(Server *);
- Server *pop_server();
-
- ~server_startup_st();
-};
-
-bool server_startup(server_startup_st&, const std::string&, in_port_t try_port, int argc, const char *argv[]);
-
} // namespace libtest
--- /dev/null
+/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
+ *
+ * libtest
+ *
+ * Copyright (C) 2011 Data Differential, http://datadifferential.com/
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include <libtest/common.h>
+
+#include <cassert>
+#include <cerrno>
+#include <cstdlib>
+#include <iostream>
+
+#include <algorithm>
+#include <functional>
+#include <locale>
+
+// trim from end
+static inline std::string &rtrim(std::string &s)
+{
+ s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
+ return s;
+}
+
+namespace libtest {
+
+void server_startup_st::push_server(Server *arg)
+{
+ servers.push_back(arg);
+
+ char port_str[NI_MAXSERV];
+ snprintf(port_str, sizeof(port_str), "%u", int(arg->port()));
+
+ std::string server_config_string;
+ if (arg->has_socket())
+ {
+ server_config_string+= "--socket=";
+ server_config_string+= '"';
+ server_config_string+= arg->socket();
+ server_config_string+= '"';
+ server_config_string+= " ";
+ }
+ else
+ {
+ server_config_string+= "--server=";
+ server_config_string+= arg->hostname();
+ server_config_string+= ":";
+ server_config_string+= port_str;
+ server_config_string+= " ";
+ }
+
+ server_list+= server_config_string;
+
+}
+
+Server* server_startup_st::pop_server()
+{
+ Server *tmp= servers.back();
+ servers.pop_back();
+ return tmp;
+}
+
+void server_startup_st::shutdown(bool remove)
+{
+ if (remove)
+ {
+ for (std::vector<Server *>::iterator iter= servers.begin(); iter != servers.end(); iter++)
+ {
+ delete *iter;
+ }
+ servers.clear();
+ }
+ else
+ {
+ for (std::vector<Server *>::iterator iter= servers.begin(); iter != servers.end(); iter++)
+ {
+ if ((*iter)->has_pid() and not (*iter)->kill((*iter)->pid()))
+ {
+ Error << "Unable to kill:" << *(*iter);
+ }
+ }
+ }
+}
+
+server_startup_st::~server_startup_st()
+{
+ shutdown(true);
+}
+
+bool server_startup_st::is_debug() const
+{
+ return bool(getenv("LIBTEST_MANUAL_GDB"));
+}
+
+bool server_startup_st::is_valgrind() const
+{
+ return bool(getenv("LIBTEST_MANUAL_VALGRIND"));
+}
+
+bool server_startup_st::is_helgrind() const
+{
+ return bool(getenv("LIBTEST_MANUAL_HELGRIND"));
+}
+
+
+bool server_startup(server_startup_st& construct, const std::string& server_type, in_port_t try_port, int argc, const char *argv[])
+{
+ Outn();
+ (void)try_port;
+
+ // Look to see if we are being provided ports to use
+ {
+ char variable_buffer[1024];
+ snprintf(variable_buffer, sizeof(variable_buffer), "LIBTEST_PORT_%lu", (unsigned long)construct.count());
+
+ char *var;
+ if ((var= getenv(variable_buffer)))
+ {
+ in_port_t tmp= in_port_t(atoi(var));
+
+ if (tmp > 0)
+ try_port= tmp;
+ }
+ }
+
+ libtest::Server *server= NULL;
+ if (0)
+ { }
+ else if (server_type.compare("gearmand") == 0)
+ {
+ if (GEARMAND_BINARY)
+ {
+ if (HAVE_LIBGEARMAN)
+ {
+ server= build_gearmand("localhost", try_port);
+ }
+ else
+ {
+ Error << "Libgearman was not found";
+ }
+ }
+ else
+ {
+ Error << "No gearmand binary is available";
+ }
+ }
+ else if (server_type.compare("blobslap_worker") == 0)
+ {
+ if (GEARMAND_BINARY and GEARMAND_BLOBSLAP_WORKER)
+ {
+ if (HAVE_LIBGEARMAN)
+ {
+ server= build_blobslap_worker(try_port);
+ }
+ else
+ {
+ Error << "Libgearman was not found";
+ }
+ }
+ else
+ {
+ Error << "No gearmand binary is available";
+ }
+ }
+ else if (server_type.compare("memcached-sasl") == 0)
+ {
+ if (MEMCACHED_SASL_BINARY)
+ {
+ if (HAVE_LIBMEMCACHED)
+ {
+ server= build_memcached_sasl("localhost", try_port, construct.username(), construct.password());
+ }
+ else
+ {
+ Error << "Libmemcached was not found";
+ }
+ }
+ else
+ {
+ Error << "No memcached binary that was compiled with sasl is available";
+ }
+ }
+ else if (server_type.compare("memcached") == 0)
+ {
+ if (MEMCACHED_BINARY)
+ {
+ if (HAVE_LIBMEMCACHED)
+ {
+ server= build_memcached("localhost", try_port);
+ }
+ else
+ {
+ Error << "Libmemcached was not found";
+ }
+ }
+ else
+ {
+ Error << "No memcached binary is available";
+ }
+ }
+ else
+ {
+ Error << "Failed to start " << server_type << ", no support was found to be compiled in for it.";
+ }
+
+ if (server == NULL)
+ {
+ Error << "Failure occured while creating server: " << server_type;
+ return false;
+ }
+
+ /*
+ We will now cycle the server we have created.
+ */
+ if (not server->cycle())
+ {
+ Error << "Could not start up server " << *server;
+ delete server;
+ return false;
+ }
+
+ server->build(argc, argv);
+
+ if (construct.is_debug())
+ {
+ Out << "Pausing for startup, hit return when ready.";
+ std::string gdb_command= server->base_command();
+ std::string options;
+ Out << "run " << server->args(options);
+ getchar();
+ }
+ else if (not server->start())
+ {
+ Error << "Failed to start " << *server;
+ delete server;
+ return false;
+ }
+ else
+ {
+ Out << "STARTING SERVER(pid:" << server->pid() << "): " << server->running();
+ }
+
+ construct.push_server(server);
+
+ if (default_port() == 0)
+ {
+ assert(server->has_port());
+ set_default_port(server->port());
+ }
+
+ Outn();
+
+ return true;
+}
+
+bool server_startup_st::start_socket_server(const std::string& server_type, const in_port_t try_port, int argc, const char *argv[])
+{
+ (void)try_port;
+ Outn();
+
+ Server *server= NULL;
+ if (0)
+ { }
+ else if (server_type.compare("gearmand") == 0)
+ {
+ Error << "Socket files are not supported for gearmand yet";
+ }
+ else if (server_type.compare("memcached-sasl") == 0)
+ {
+ if (MEMCACHED_SASL_BINARY)
+ {
+ if (HAVE_LIBMEMCACHED)
+ {
+ server= build_memcached_sasl_socket("localhost", try_port, username(), password());
+ }
+ else
+ {
+ Error << "Libmemcached was not found";
+ }
+ }
+ else
+ {
+ Error << "No memcached binary is available";
+ }
+ }
+ else if (server_type.compare("memcached") == 0)
+ {
+ if (MEMCACHED_BINARY)
+ {
+ if (HAVE_LIBMEMCACHED)
+ {
+ server= build_memcached_socket("localhost", try_port);
+ }
+ else
+ {
+ Error << "Libmemcached was not found";
+ }
+ }
+ else
+ {
+ Error << "No memcached binary is available";
+ }
+ }
+ else
+ {
+ Error << "Failed to start " << server_type << ", no support was found to be compiled in for it.";
+ }
+
+ if (server == NULL)
+ {
+ Error << "Failure occured while creating server: " << server_type;
+ return false;
+ }
+
+ /*
+ We will now cycle the server we have created.
+ */
+ if (not server->cycle())
+ {
+ Error << "Could not start up server " << *server;
+ delete server;
+ return false;
+ }
+
+ server->build(argc, argv);
+
+ if (is_debug())
+ {
+ Out << "Pausing for startup, hit return when ready.";
+ std::string gdb_command= server->base_command();
+ std::string options;
+ Out << "run " << server->args(options);
+ getchar();
+ }
+ else if (not server->start())
+ {
+ Error << "Failed to start " << *server;
+ delete server;
+ return false;
+ }
+ else
+ {
+ Out << "STARTING SERVER(pid:" << server->pid() << "): " << server->running();
+ }
+
+ push_server(server);
+
+ set_default_socket(server->socket().c_str());
+
+ Outn();
+
+ return true;
+}
+
+std::string server_startup_st::option_string() const
+{
+ std::string temp= server_list;
+ rtrim(temp);
+ return temp;
+}
+
+
+} // namespace libtest
--- /dev/null
+/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
+ *
+ * libtest
+ *
+ * Copyright (C) 2011 Data Differential, http://datadifferential.com/
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#pragma once
+
+#include <cassert>
+#include <cstdio>
+#include <cstring>
+#include <netdb.h>
+#include <netinet/in.h>
+#include <string>
+#include <unistd.h>
+#include <vector>
+
+namespace libtest {
+
+class server_startup_st
+{
+private:
+ std::string server_list;
+ bool _socket;
+ bool _sasl;
+ std::string _username;
+ std::string _password;
+
+public:
+
+ uint8_t udp;
+ std::vector<Server *> servers;
+
+ server_startup_st() :
+ _socket(false),
+ _sasl(false),
+ udp(0)
+ { }
+
+ bool start_socket_server(const std::string& server_type, const in_port_t try_port, int argc, const char *argv[]);
+
+ std::string option_string() const;
+
+ size_t count() const
+ {
+ return servers.size();
+ }
+
+ const std::string& password() const
+ {
+ return _password;
+ }
+
+ const std::string& username() const
+ {
+ return _username;
+ }
+
+
+ bool is_debug() const;
+ bool is_helgrind() const;
+ bool is_valgrind() const;
+
+ bool socket()
+ {
+ return _socket;
+ }
+
+ bool sasl()
+ {
+ return _sasl;
+ }
+
+ void set_socket()
+ {
+ _socket= true;
+ }
+
+ void set_sasl(const std::string& username_arg, const std::string& password_arg)
+ {
+ _sasl= true;
+ _username= username_arg;
+ _password= password_arg;
+ }
+
+
+ void shutdown(bool remove= false);
+ void push_server(Server *);
+ Server *pop_server();
+
+ ~server_startup_st();
+};
+
+bool server_startup(server_startup_st&, const std::string&, in_port_t try_port, int argc, const char *argv[]);
+
+} // namespace libtest
#include <libtest/error.h>
#include <libtest/server.h>
+#include <libtest/server_container.h>
#include <libtest/wait.h>
#include <libtest/callbacks.h>
#include <libtest/test.h>
#include <libtest/test.hpp>
+#if defined(LIBTEST_WITH_LIBMEMCACHED_SUPPORT) && LIBTEST_WITH_LIBMEMCACHED_SUPPORT
+#include <libmemcached/memcached.h>
+#endif
+
+#if defined(LIBTEST_WITH_LIBGEARMAN_SUPPORT) && LIBTEST_WITH_LIBGEARMAN_SUPPORT
+#include <libgearman/gearman.h>
+#endif
+
#include <cstdlib>
#include <unistd.h>
return TEST_SUCCESS;
}
+static test_return_t test_success_equals_one_test(void *)
+{
+ test_skip(HAVE_LIBMEMCACHED, true);
+ test_zero(MEMCACHED_SUCCESS);
+ return TEST_SUCCESS;
+}
+
static test_return_t test_success_test(void *)
{
return TEST_SUCCESS;
return TEST_SUCCESS;
}
+static test_return_t _compare_test_return_t_test(void *)
+{
+ test_compare(TEST_SUCCESS, TEST_SUCCESS);
+
+ return TEST_SUCCESS;
+}
+
+static test_return_t _compare_memcached_return_t_test(void *)
+{
+ test_skip(HAVE_LIBMEMCACHED, true);
+ test_compare(MEMCACHED_SUCCESS, MEMCACHED_SUCCESS);
+
+ return TEST_SUCCESS;
+}
+
+static test_return_t _compare_gearman_return_t_test(void *)
+{
+ test_skip(HAVE_LIBGEARMAN, true);
+
+ return TEST_SUCCESS;
+}
+
static test_return_t gearmand_cycle_test(void *object)
{
server_startup_st *servers= (server_startup_st*)object;
return TEST_SKIPPED;
}
+static test_return_t memcached_sasl_test(void *object)
+{
+ server_startup_st *servers= (server_startup_st*)object;
+ test_true(servers);
+
+ if (getenv("TESTS_ENVIRONMENT"))
+ {
+ return TEST_SKIPPED;
+ }
+
+ if (MEMCACHED_SASL_BINARY and HAVE_LIBMEMCACHED)
+ {
+ const char *argv[1]= { "cycle_memcached_sasl" };
+ test_true(server_startup(*servers, "memcached-sasl", 9996, 1, argv));
+
+ return TEST_SUCCESS;
+ }
+
+ return TEST_SKIPPED;
+}
+
test_st gearmand_tests[] ={
#if 0
{"pause", 0, pause_test },
test_st memcached_tests[] ={
{"memcached startup-shutdown", 0, memcached_cycle_test },
{"memcached(socket file) startup-shutdown", 0, memcached_socket_cycle_test },
+ {"memcached_sasl() startup-shutdown", 0, memcached_sasl_test },
{0, 0, 0}
};
};
test_st tests_log[] ={
- {"TEST_SUCCESS", 0, test_success_test },
- {"TEST_FAILURE", 0, test_failure_test },
+ {"TEST_SUCCESS", false, test_success_test },
+ {"TEST_FAILURE", false, test_failure_test },
+ {"TEST_SUCCESS == 0", false, test_success_equals_one_test },
{0, 0, 0}
};
{0, 0, 0}
};
+test_st comparison_tests[] ={
+ {"_compare(test_return_t)", 0, _compare_test_return_t_test },
+ {"_compare(memcached_return_t)", 0, _compare_memcached_return_t_test },
+ {"_compare(gearman_return_t)", 0, _compare_gearman_return_t_test },
+ {0, 0, 0}
+};
+
collection_st collection[] ={
{"environment", 0, 0, environment_tests},
{"return values", 0, 0, tests_log},
{"local", 0, 0, local_log},
{"directories", 0, 0, directories_tests},
+ {"comparison", 0, 0, comparison_tests},
{"gearmand", 0, 0, gearmand_tests},
{"memcached", 0, 0, memcached_tests},
{0, 0, 0, 0}
extern "C" {
#endif
-#define LIBTEST@_WITH_LIBMEMCACHED_SUPPORT@
-#define LIBTEST@_WITH_LIBGEARMAN_SUPPORT@
-
#define LIBTEST_VERSION_STRING "@VERSION@"
#define LIBTEST_VERSION_HEX @HEX_VERSION@
tests/pool.h \
tests/print.h \
tests/replication.h \
+ tests/server_add.h \
tests/string.h \
tests/virtual_buckets.h
tests/pool.cc \
tests/print.cc \
tests/replication.cc \
+ tests/server_add.cc \
tests/virtual_buckets.cc
tests_testapp_SOURCES+= clients/generator.cc clients/execute.cc
tests_testapp_DEPENDENCIES= \
valgrind-plus: tests/testplus
@$(VALGRIND_COMMAND) tests/testplus
+valgrind-sasl: tests/sasl
+ @$(VALGRIND_COMMAND) tests/sasl
+
valgrind-hash: tests/testhashkit
@$(VALGRIND_COMMAND) tests/testhashkit
valgrind-hashplus: tests/hash_plus
@$(VALGRIND_COMMAND) tests/hash_plus
-.PHONY += valgrind
-valgrind: valgrind-cycle valgrind-mem valgrind-udp valgrind-plus valgrind-hash valgrind-hashplus
-
helgrind-cycle: tests/cycle
@$(HELGRIND_COMMAND) tests/cycle
return NULL;
}
+ // Assume we are running under valgrind, and bail
+ if (servers.sasl() and getenv("TESTS_ENVIRONMENT"))
+ {
+ error= TEST_SKIPPED;
+ return NULL;
+ }
+
+
in_port_t max_port;
for (uint32_t x= 0; x < SERVERS_TO_CREATE; x++)
{
#define SMALL_STRING_LEN 1024
#include <libtest/test.hpp>
+
+#include "tests/basic.h"
+#include "tests/debug.h"
#include "tests/deprecated.h"
-#include "tests/parser.h"
+#include "tests/error_conditions.h"
#include "tests/ketama.h"
-#include "tests/pool.h"
#include "tests/namespace.h"
-#include "tests/replication.h"
-#include "tests/debug.h"
-#include "tests/basic.h"
-#include "tests/error_conditions.h"
+#include "tests/parser.h"
+#include "tests/pool.h"
#include "tests/print.h"
+#include "tests/replication.h"
+#include "tests/server_add.h"
#include "tests/virtual_buckets.h"
using namespace libtest;
static test_return_t set_memory_alloc(memcached_st *memc)
{
- test_compare(MEMCACHED_FAILURE,
+ test_compare(MEMCACHED_INVALID_ARGUMENTS,
memcached_set_memory_allocators(memc, NULL, my_free,
my_realloc, my_calloc, NULL));
{0, 0, (test_callback_fn*)0}
};
+test_st memcached_server_add_tests[] ={
+ {"memcached_server_add(\"\")", false, (test_callback_fn*)memcached_server_add_empty_test },
+ {"memcached_server_add(NULL)", false, (test_callback_fn*)memcached_server_add_null_test },
+ {0, 0, (test_callback_fn*)0}
+};
+
test_st namespace_tests[] ={
{"basic tests", true, (test_callback_fn*)selection_of_namespace_tests },
{"increment", true, (test_callback_fn*)memcached_increment_namespace },
{"basic", 0, 0, basic_tests},
{"hsieh_availability", 0, 0, hsieh_availability},
{"murmur_availability", 0, 0, murmur_availability},
+ {"memcached_server_add", 0, 0, memcached_server_add_tests},
{"block", 0, 0, tests},
{"binary", (test_callback_fn*)pre_binary, 0, tests},
{"nonblock", (test_callback_fn*)pre_nonblock, 0, tests},
--- /dev/null
+/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
+ *
+ * Libmemcached Client and Server
+ *
+ * Copyright (C) 2011 Data Differential, http://datadifferential.com/
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * * The names of its contributors may not be used to endorse or
+ * promote products derived from this software without specific prior
+ * written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#include <config.h>
+#include <libtest/test.hpp>
+
+using namespace libtest;
+
+#include <libmemcached/memcached.h>
+
+#include <tests/server_add.h>
+
+test_return_t memcached_server_add_null_test(memcached_st*)
+{
+ memcached_st *memc= memcached_create(NULL);
+
+ test_compare(MEMCACHED_SUCCESS, memcached_server_add(memc, NULL, 0));
+
+ memcached_free(memc);
+
+ return TEST_SUCCESS;
+}
+
+test_return_t memcached_server_add_empty_test(memcached_st*)
+{
+ memcached_st *memc= memcached_create(NULL);
+
+ test_compare(MEMCACHED_SUCCESS, memcached_server_add(memc, "", 0));
+
+ memcached_free(memc);
+
+ return TEST_SUCCESS;
+}
--- /dev/null
+/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
+ *
+ * Libmemcached Client and Server
+ *
+ * Copyright (C) 2011 Data Differential, http://datadifferential.com/
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * * The names of its contributors may not be used to endorse or
+ * promote products derived from this software without specific prior
+ * written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#pragma once
+
+test_return_t memcached_server_add_null_test(memcached_st*);
+test_return_t memcached_server_add_empty_test(memcached_st*);