#define TEST_PORT_BASE MEMCACHED_DEFAULT_PORT+10
-#include <config.h>
-
-#include <iso646.h>
-
-#include <assert.h>
+#include <libtest/common.h>
+
+#include <cassert>
+#include <cerrno>
+#include <cstdio>
+#include <cstdlib>
+#include <cstring>
+#include <ctime>
#include <limits.h>
#include <signal.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
#include <sys/time.h>
-#include <time.h>
#include <unistd.h>
-#include <errno.h>
+#include <iostream>
#include <libmemcached/memcached.h>
#include <libmemcached/util.h>
#include <libtest/server.h>
+#include <libtest/killpid.h>
+#include <libtest/wait.h>
-static void global_sleep(void)
-{
- static struct timespec global_sleep_value= { 0, 50000 };
+#define CERR_PREFIX std::endl << __FILE__ << ":" << __LINE__ << " "
-#ifdef WIN32
- sleep(1);
-#else
- nanosleep(&global_sleep_value, NULL);
-#endif
-}
+#define SOCKET_FILE "/tmp/memcached.socket"
-static bool wait_for_file(const char *filename)
+static pid_t __getpid(server_st& server)
{
- uint32_t timeout= 6;
- uint32_t waited;
- uint32_t this_wait;
- uint32_t retry;
-
- for (waited= 0, retry= 1; ; retry++, waited+= this_wait)
- {
- if ((! access(filename, R_OK)) || (waited >= timeout))
- {
- return true;
- }
-
- this_wait= retry * retry / 3 + 1;
- sleep(this_wait);
- }
-
- return false;
+ memcached_return_t rc;
+ pid_t pid= libmemcached_util_getpid(server.hostname(), server.port(), &rc);
+ return pid;
}
-static void kill_file(const char *file_buffer)
+static bool __ping(server_st& server)
{
- FILE *fp;
+ memcached_return_t rc;
+ bool ret= libmemcached_util_ping(server.hostname(), server.port(), &rc);
+ return ret;
+}
- while ((fp= fopen(file_buffer, "r")))
+static bool cycle_server(server_st *server)
+{
+ while (1)
{
- char pid_buffer[1024];
-
- if (fgets(pid_buffer, sizeof(pid_buffer), fp) != NULL)
+ if (libmemcached_util_ping(server->hostname(), server->port(), NULL))
{
- pid_t pid= (pid_t)atoi(pid_buffer);
- if (pid != 0)
+ // First we try to kill it, and on fail of that we flush it.
+ pid_t pid= libmemcached_util_getpid(server->hostname(), server->port(), NULL);
+
+ if (pid > 0 and kill_pid(pid))
{
- if (kill(pid, SIGTERM) == -1)
- {
- remove(file_buffer); // If this happens we may be dealing with a dead server that left its pid file.
- }
- else
- {
- uint32_t counter= 3;
- while ((kill(pid, 0) == 0) && --counter)
- {
- global_sleep();
- }
- }
+ std::cerr << CERR_PREFIX << "Killed existing server," << *server << " with pid:" << pid << std::endl;
+ continue;
+ }
+ else if (libmemcached_util_flush(server->hostname(), server->port(), NULL)) // If we can flush it, we will just use it
+ {
+ std::cerr << CERR_PREFIX << "Found server on port " << int(server->port()) << ", flushed it!" << std::endl;
+ server->set_used();
+ return true;
+ } // No idea what is wrong here, so we need to find a different port
+ else
+ {
+ return false;
}
}
- global_sleep();
-
- fclose(fp);
+ break;
}
+
+ return true;
}
-void server_startup(server_startup_st *construct)
+bool server_startup(server_startup_st *construct)
{
- if ((construct->server_list= getenv("MEMCACHED_SERVERS")))
+ if (getenv(((char *)"MEMCACHED_SERVERS")))
{
- printf("servers %s\n", construct->server_list);
+ construct->server_list= getenv(((char *)"MEMCACHED_SERVERS"));
+ printf("servers %s\n", construct->server_list.c_str());
construct->count= 0;
}
else
{
+ std::string server_config_string;
+
+ uint32_t port_base= 0;
+ for (uint32_t x= 0; x < (construct->count -1); x++)
{
- char server_string_buffer[8096];
- char *end_ptr;
- end_ptr= server_string_buffer;
+ server_st *server= NULL;
- uint32_t port_base= 0;
- for (uint32_t x= 0; x < construct->count; x++)
{
- int status;
+ char *var;
+ char variable_buffer[1024];
- snprintf(construct->pid_file[x], FILENAME_MAX, "/tmp/memcached.pidXXXXXX");
- int fd;
- if ((fd= mkstemp(construct->pid_file[x])) == -1)
+ snprintf(variable_buffer, sizeof(variable_buffer), "LIBMEMCACHED_PORT_%u", x);
+
+ if ((var= getenv(variable_buffer)))
{
- perror("mkstemp");
- return;
+ server= new server_st((in_port_t)atoi(var), __getpid, __ping);
}
- close(fd);
-
+ else
{
- char *var;
- char variable_buffer[1024];
+ server= new server_st(in_port_t(x +TEST_PORT_BASE +port_base), __getpid, __ping);
- snprintf(variable_buffer, sizeof(variable_buffer), "LIBMEMCACHED_PORT_%u", x);
-
- if ((var= getenv(variable_buffer)))
- {
- construct->port[x]= (in_port_t)atoi(var);
- }
- else
+ while (not cycle_server(server))
{
- do {
- construct->port[x]= (in_port_t)(x + TEST_PORT_BASE + port_base);
-
- if (libmemcached_util_ping("localhost", construct->port[x], NULL))
- {
- if (libmemcached_util_flush("localhost", construct->port[x], NULL))
- {
- fprintf(stderr, "Found server on port %d, flushed it!\n", (int)construct->port[x]);
- construct->is_used[x]= true;
- } // If we can flush it, we will just use it
- else
- {
- fprintf(stderr, "Found server on port %d, could not flush it, so trying next port.\n", (int)construct->port[x]);
- port_base++;
- construct->port[x]= 0;
- }
- }
- } while (construct->port[x] == 0);
+ std::cerr << CERR_PREFIX << "Found server " << *server << ", could not flush it, so trying next port." << std::endl;
+ port_base++;
+ server->set_port(in_port_t(x +TEST_PORT_BASE +port_base));
}
}
+ }
+ if (server->is_used())
+ {
+ std::cerr << std::endl << "Using server at : " << server << std::endl;
+ }
+ else
+ {
char buffer[FILENAME_MAX];
if (x == 0)
{
- snprintf(buffer, sizeof(buffer), "%s -d -P %s -t 1 -p %u -U %u -m 128",
- MEMCACHED_BINARY, construct->pid_file[x], construct->port[x], construct->port[x]);
- }
- else
- {
- snprintf(buffer, sizeof(buffer), "%s -d -P %s -t 1 -p %u -U %u",
- MEMCACHED_BINARY, construct->pid_file[x], construct->port[x], construct->port[x]);
- }
-
- if (construct->is_used[x])
- {
- fprintf(stderr, "USING SERVER: %s\n", buffer);
+ snprintf(buffer, sizeof(buffer), "%s -d -t 1 -p %u -U %u -m 128",
+ MEMCACHED_BINARY, server->port(), server->port());
}
else
{
- if (libmemcached_util_ping("localhost", construct->port[x], NULL))
- {
- fprintf(stderr, "Server on port %u already exists\n", construct->port[x]);
- }
- else
- {
- status= system(buffer);
- fprintf(stderr, "STARTING SERVER: %s status:%d\n", buffer, status);
- }
+ snprintf(buffer, sizeof(buffer), "%s -d -t 1 -p %u -U %u",
+ MEMCACHED_BINARY, server->port(), server->port());
}
+ server->set_command(buffer);
- size_t remaining_length= sizeof(server_string_buffer) - (size_t)(end_ptr -server_string_buffer);
- int count= snprintf(end_ptr, remaining_length, "--server=localhost:%u ", construct->port[x]);
-
- if ((size_t)count >= remaining_length or count < 0)
+ if (not server->start())
{
- fprintf(stderr, "server names grew to be larger then buffer allowed\n");
- abort();
+ std::cerr << CERR_PREFIX << "Failed system(" << buffer << ")" << std::endl;
+ delete server;
+ return false;
}
- end_ptr+= count;
+ std::cerr << "STARTING SERVER: " << buffer << " pid:" << server->pid() << std::endl;
}
- *end_ptr= 0;
-
+ construct->push_server(server);
- for (uint32_t x= 0; x < construct->count; x++)
+ if (x == 0)
{
- if (! wait_for_file(construct->pid_file[x]))
- {
- abort();
- }
+ assert(server->has_port());
+ set_default_port(server->port());
}
- for (uint32_t x= 0; x < construct->count; x++)
- {
- uint32_t counter= 3000; // Absurd, just to catch run away process
+ char port_str[NI_MAXSERV];
+ snprintf(port_str, sizeof(port_str), "%u", int(server->port()));
- if (construct->is_used[x])
- continue;
+ server_config_string+= "--server=";
+ server_config_string+= server->hostname();
+ server_config_string+= ":";
+ server_config_string+= port_str;
+ server_config_string+= " ";
+ }
- while (construct->pids[x] <= 0 && --counter)
- {
- FILE *file= fopen(construct->pid_file[x], "r");
- if (file)
- {
- char pid_buffer[1024];
- char *found= fgets(pid_buffer, sizeof(pid_buffer), file);
-
- if (found)
- {
- construct->pids[x]= atoi(pid_buffer);
- fclose(file);
-
- if (construct->pids[x] > 0)
- break;
- }
- fclose(file);
- }
+ // Socket
+ {
- switch (errno)
- {
- default:
- fprintf(stderr, "Could not open pid file %s -> fopen(%s) -> %s:%d\n", construct->pid_file[x], strerror(errno), __FILE__, __LINE__);
- abort();
-
- case ENOENT:
- case EINTR:
- case EACCES:
- case EINPROGRESS:
- break;
-
- case ENOTCONN:
- continue;
- }
+ std::string socket_file(SOCKET_FILE);
+ char *var;
- // Safety 3rd, check to see if the file has gone away
- if (! wait_for_file(construct->pid_file[x]))
- {
- abort();
- }
- }
+ if ((var= getenv("LIBMEMCACHED_SOCKET")))
+ {
+ socket_file= var;
+ }
- bool was_started= false;
- if (construct->pids[x] > 0)
- {
- counter= 30;
- while (--counter)
- {
- if (kill(construct->pids[x], 0) == 0)
- {
- was_started= true;
- break;
- }
- global_sleep();
- }
- }
+ server_st *server= new server_st(SOCKET_FILE, __getpid, __ping);
+
+ if (not cycle_server(server))
+ {
+ std::cerr << CERR_PREFIX << "Found server " << server << ", could not flush it, failing since socket file is not available." << std::endl;
+ return false;
+ }
+
+ if (server->is_used())
+ {
+ std::cerr << std::endl << "Using server at : " << *server << std::endl;
+ }
+ else
+ {
+ char buffer[FILENAME_MAX];
+ snprintf(buffer, sizeof(buffer), "%s -d -t 1 -s %s", MEMCACHED_BINARY, SOCKET_FILE);
+ server->set_command(buffer);
- if (was_started == false)
+ if (not server->start())
{
- fprintf(stderr, "Failed to open buffer %s(%d)\n", construct->pid_file[x], construct->pids[x]);
- for (uint32_t y= 0; y < construct->count; y++)
- {
- if (construct->pids[y] > 0)
- kill(construct->pids[y], SIGTERM);
- }
- abort();
+ std::cerr << CERR_PREFIX << "Failed system(" << buffer << ")" << std::endl;
+ delete server;
+ return false;
}
+ std::cerr << "STARTING SERVER: " << buffer << " pid:" << server->pid() << std::endl;
}
+ set_default_socket(server->hostname());
+ construct->push_server(server);
- construct->server_list= strndup(server_string_buffer, strlen(server_string_buffer) -1);
+ {
+ server_config_string+= "--socket=\"";
+ server_config_string+= server->hostname();
+ server_config_string+= "\" ";
+ }
}
+
+ server_config_string.resize(server_config_string.size() -1); // Remove final space
+ construct->server_list= server_config_string;
}
srandom((unsigned int)time(NULL));
- printf("\n");
+ std::cerr << std::endl;
+ return true;
}
void server_shutdown(server_startup_st *construct)
{
- if (construct->server_list)
- {
- for (uint32_t x= 0; x < construct->count; x++)
- {
- if (construct->is_used[x])
- continue;
+ if (not construct)
+ return;
- kill_file(construct->pid_file[x]);
- }
-
- free(construct->server_list);
- }
+ construct->shutdown();
}