From 9e514c6e5b09edb48332a7b25cc243b930a64ee6 Mon Sep 17 00:00:00 2001 From: Brian Aker Date: Mon, 4 Apr 2011 21:51:26 -0700 Subject: [PATCH] Merge in code that allows us to do a better job of creating servers/kill them when needed. --- Makefile.am | 4 +- libtest/include.am | 15 ++++++++ libtest/server.c | 87 +++++++++++++++++++++++++------------------ libtest/server.h | 6 +++ libtest/test.c | 2 +- tests/include.am | 2 +- tests/mem_functions.c | 2 - 7 files changed, 77 insertions(+), 41 deletions(-) diff --git a/Makefile.am b/Makefile.am index a7bf2cc6..b9fec57e 100644 --- a/Makefile.am +++ b/Makefile.am @@ -40,9 +40,11 @@ endif test-docs: (cd docs && $(MAKE) test-docs) + +include libtest/include.am + include libmemcached/include.am include clients/include.am -include libtest/include.am include libhashkit/include.am include tests/include.am include example/include.am diff --git a/libtest/include.am b/libtest/include.am index f14d9435..fb859b66 100644 --- a/libtest/include.am +++ b/libtest/include.am @@ -7,6 +7,13 @@ # # included from Top Level Makefile.am # All paths should be given relative to the root +# + +LIBUTEST_TMP = ${abs_top_builddir}/tests/var/tmp/ + +CLEANFILES+= \ + tests/var/log/* \ + tests/var/tmp/* noinst_HEADERS+= \ libtest/failed.h \ @@ -23,3 +30,11 @@ libtest_libtest_la_SOURCES=\ libtest_libtest_la_CFLAGS= ${AM_CFLAGS} ${NO_CONVERSION} libtest_libtest_la_CPPFLAGS= ${AM_CPPFLAGS} +tests/var: tests/var/log tests/var/tmp + $(mkdir_p) tests/var + +tests/var/log: + $(mkdir_p) tests/var/log + +tests/var/tmp: + $(mkdir_p) tests/var/tmp diff --git a/libtest/server.c b/libtest/server.c index 31238fe6..6cf05fa1 100644 --- a/libtest/server.c +++ b/libtest/server.c @@ -15,9 +15,7 @@ #define TEST_PORT_BASE MEMCACHED_DEFAULT_PORT+10 -#define PID_FILE_BASE "/tmp/%ulibmemcached_memc.pid" - -#include "config.h" +#include #include #include @@ -117,10 +115,17 @@ void server_startup(server_startup_st *construct) char *end_ptr; end_ptr= server_string_buffer; + uint32_t port_base= 0; for (uint32_t x= 0; x < construct->count; x++) { int status; - in_port_t port; + + snprintf(construct->pid_file[x], FILENAME_MAX, "/tmp/memcached.pidXXXXXX"); + if (mkstemp(construct->pid_file[x]) == -1) + { + perror("mkstemp"); + return; + } { char *var; @@ -130,40 +135,47 @@ void server_startup(server_startup_st *construct) if ((var= getenv(variable_buffer))) { - port= (in_port_t)atoi(var); + construct->port[x]= (in_port_t)atoi(var); } else { - port= (in_port_t)(x + TEST_PORT_BASE); + do { + construct->port[x]= (in_port_t)(x + TEST_PORT_BASE + port_base); + + if (libmemcached_util_ping("localhost", construct->port[x], NULL)) + { + port_base++; + construct->port[x]= 0; + } + } while (construct->port[x] == 0); } } - char buffer[PATH_MAX]; - snprintf(buffer, sizeof(buffer), PID_FILE_BASE, x); - kill_file(buffer); - + char buffer[FILENAME_MAX]; if (x == 0) { - snprintf(buffer, sizeof(buffer), "%s -d -u root -P "PID_FILE_BASE" -t 1 -p %u -U %u -m 128", - MEMCACHED_BINARY, x, port, port); + 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 -u root -P "PID_FILE_BASE" -t 1 -p %u -U %u", - MEMCACHED_BINARY, x, port, port); + 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 (libmemcached_util_ping("localhost", port, NULL)) + + if (libmemcached_util_ping("localhost", construct->port[x], NULL)) { - fprintf(stderr, "Server on port %u already exists\n", port); + 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); } + int count; size_t remaining_length= sizeof(server_string_buffer) - (size_t)(end_ptr -server_string_buffer); - count= snprintf(end_ptr, remaining_length, "localhost:%u,", port); + count= snprintf(end_ptr, remaining_length, "localhost:%u,", construct->port[x]); if ((size_t)count >= remaining_length || count < 0) { @@ -175,17 +187,20 @@ void server_startup(server_startup_st *construct) *end_ptr= 0; - int *pids= calloc(construct->count, sizeof(int)); for (uint32_t x= 0; x < construct->count; x++) { - char buffer[PATH_MAX]; /* Nothing special for number */ - - snprintf(buffer, sizeof(buffer), PID_FILE_BASE, x); + if (! wait_for_file(construct->pid_file[x])) + { + abort(); + } + } + for (uint32_t x= 0; x < construct->count; x++) + { uint32_t counter= 3000; // Absurd, just to catch run away process - while (pids[x] <= 0 && --counter) + while (construct->pids[x] <= 0 && --counter) { - FILE *file= fopen(buffer, "r"); + FILE *file= fopen(construct->pid_file[x], "r"); if (file) { char pid_buffer[1024]; @@ -193,10 +208,10 @@ void server_startup(server_startup_st *construct) if (found) { - pids[x]= atoi(pid_buffer); + construct->pids[x]= atoi(pid_buffer); fclose(file); - if (pids[x] > 0) + if (construct->pids[x] > 0) break; } fclose(file); @@ -204,27 +219,30 @@ void server_startup(server_startup_st *construct) switch (errno) { default: - fprintf(stderr, "%s\n", strerror(errno)); + fprintf(stderr, "%s -> fopen(%s)\n", construct->pid_file[x], strerror(errno)); abort(); case ENOENT: case EINTR: case EACCES: break; + case ENOTCONN: + continue; } - if (! wait_for_file(buffer)) + // Safety 3rd, check to see if the file has gone away + if (! wait_for_file(construct->pid_file[x])) { abort(); } } bool was_started= false; - if (pids[x] > 0) + if (construct->pids[x] > 0) { counter= 30; while (--counter) { - if (kill(pids[x], 0) == 0) + if (kill(construct->pids[x], 0) == 0) { was_started= true; break; @@ -235,16 +253,15 @@ void server_startup(server_startup_st *construct) if (was_started == false) { - fprintf(stderr, "Failed to open buffer %s(%d)\n", buffer, pids[x]); + 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 (pids[y] > 0) - kill(pids[y], SIGTERM); + if (construct->pids[y] > 0) + kill(construct->pids[y], SIGTERM); } abort(); } } - free(pids); construct->server_list= strdup(server_string_buffer); } @@ -272,9 +289,7 @@ void server_shutdown(server_startup_st *construct) { for (uint32_t x= 0; x < construct->count; x++) { - char file_buffer[PATH_MAX]; /* Nothing special for number */ - snprintf(file_buffer, sizeof(file_buffer), PID_FILE_BASE, x); - kill_file(file_buffer); + kill_file(construct->pid_file[x]); } free(construct->server_list); diff --git a/libtest/server.h b/libtest/server.h index e849f29a..454f69ca 100644 --- a/libtest/server.h +++ b/libtest/server.h @@ -9,6 +9,8 @@ #pragma once +#include + /* Server startup and shutdown functions. */ @@ -19,6 +21,7 @@ extern "C" { #include typedef struct server_startup_st server_startup_st; +#define SERVERS_TO_CREATE 5 struct server_startup_st { @@ -26,6 +29,9 @@ struct server_startup_st uint8_t udp; memcached_server_st *servers; char *server_list; + char pid_file[SERVERS_TO_CREATE][FILENAME_MAX]; + in_port_t port[SERVERS_TO_CREATE]; + int pids[SERVERS_TO_CREATE]; }; void server_startup(server_startup_st *construct); diff --git a/libtest/test.c b/libtest/test.c index da989b17..9a79e4c5 100644 --- a/libtest/test.c +++ b/libtest/test.c @@ -10,7 +10,7 @@ Sample test application. */ -#include "config.h" +#include #include diff --git a/tests/include.am b/tests/include.am index 7451efd3..93a08c6e 100644 --- a/tests/include.am +++ b/tests/include.am @@ -107,7 +107,7 @@ check_PROGRAMS+= tests/hash_plus test: check -check-local: $(TEST_DOCS) test-mem test-hash memcapable +check-local: tests/var $(TEST_DOCS) test-mem test-hash memcapable @echo "Tests completed" test-x: check-local test-plus test-memcat test-memcp test-memrm test-memerror test-memdump test-memflush test-memstat diff --git a/tests/mem_functions.c b/tests/mem_functions.c index 9a5c053d..15daa39f 100644 --- a/tests/mem_functions.c +++ b/tests/mem_functions.c @@ -6302,8 +6302,6 @@ collection_st collection[] ={ {0, 0, 0, 0} }; -#define SERVERS_TO_CREATE 5 - #include "libmemcached_world.h" void get_world(world_st *world) -- 2.30.2