From: Brian Aker Date: Wed, 13 Mar 2013 03:23:07 +0000 (-0400) Subject: Cleanup around errors on bad --servers for CLI. X-Git-Tag: 1.0.17~2^2 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=29021cad3872f6c72e663431eb1e68b50ef8b83f;p=m6w6%2Flibmemcached Cleanup around errors on bad --servers for CLI. --- diff --git a/clients/memcat.cc b/clients/memcat.cc index 92dce5d3..52b02aca 100644 --- a/clients/memcat.cc +++ b/clients/memcat.cc @@ -39,19 +39,17 @@ static char *opt_file; int main(int argc, char *argv[]) { - memcached_st *memc; char *string; size_t string_length; uint32_t flags; memcached_return_t rc; - memcached_server_st *servers; int return_code= EXIT_SUCCESS; options_parse(argc, argv); initialize_sockets(); - if (!opt_servers) + if (opt_servers == NULL) { char *temp; @@ -59,17 +57,23 @@ int main(int argc, char *argv[]) { opt_servers= strdup(temp); } - else + + if (opt_servers == NULL) { std::cerr << "No servers provied" << std::endl; exit(EXIT_FAILURE); } } - memc= memcached_create(NULL); - process_hash_option(memc, opt_hash); + memcached_server_st* servers= memcached_servers_parse(opt_servers); + if (servers == NULL or memcached_server_list_count(servers) == 0) + { + std::cerr << "Invalid server list provided:" << opt_servers << std::endl; + return EXIT_FAILURE; + } - servers= memcached_servers_parse(opt_servers); + memcached_st* memc= memcached_create(NULL); + process_hash_option(memc, opt_hash); memcached_server_push(memc, servers); memcached_server_list_free(servers); @@ -253,7 +257,7 @@ void options_parse(int argc, char *argv[]) case '?': /* getopt_long already printed an error message. */ - exit(1); + exit(EXIT_FAILURE); default: abort(); } diff --git a/clients/memcp.cc b/clients/memcp.cc index f59e1d8c..7ae5a1f3 100644 --- a/clients/memcp.cc +++ b/clients/memcp.cc @@ -129,21 +129,23 @@ int main(int argc, char *argv[]) { opt_servers= strdup(temp); } - else + else if (argc >= 1 and argv[--argc]) + { + opt_servers= strdup(argv[--argc]); + } + + if (opt_servers == NULL) { std::cerr << "No Servers provided" << std::endl; exit(EXIT_FAILURE); } } - memcached_server_st *servers; - if (opt_servers) - { - servers= memcached_servers_parse(opt_servers); - } - else + memcached_server_st* servers= memcached_servers_parse(opt_servers); + if (servers == NULL or memcached_server_list_count(servers) == 0) { - servers= memcached_servers_parse(argv[--argc]); + std::cerr << "Invalid server list provided:" << opt_servers << std::endl; + return EXIT_FAILURE; } memcached_server_push(memc, servers); diff --git a/clients/memdump.cc b/clients/memdump.cc index bcd277c2..5c6fef29 100644 --- a/clients/memdump.cc +++ b/clients/memdump.cc @@ -62,9 +62,6 @@ int main(int argc, char *argv[]) options_parse(argc, argv); - memcached_st *memc= memcached_create(NULL); - process_hash_option(memc, opt_hash); - if (opt_servers == NULL) { char *temp; @@ -73,22 +70,32 @@ int main(int argc, char *argv[]) { opt_servers= strdup(temp); } - else + else if (argc >= 1 and argv[--argc]) + { + opt_servers= strdup(argv[--argc]); + } + + if (opt_servers == NULL) { std::cerr << "No Servers provided" << std::endl; exit(EXIT_FAILURE); } } - memcached_server_st *servers; - if (opt_servers) + memcached_server_st* servers= memcached_servers_parse(opt_servers); + if (servers == NULL or memcached_server_list_count(servers) == 0) { - servers= memcached_servers_parse(opt_servers); + std::cerr << "Invalid server list provided:" << opt_servers << std::endl; + return EXIT_FAILURE; } - else + + memcached_st *memc= memcached_create(NULL); + if (memc == NULL) { - servers= memcached_servers_parse(argv[--argc]); + std::cerr << "Could not allocate a memcached_st structure.\n" << std::endl; + return EXIT_FAILURE; } + process_hash_option(memc, opt_hash); memcached_server_push(memc, servers); memcached_server_list_free(servers); diff --git a/clients/memexist.cc b/clients/memexist.cc index 34d38936..505f5385 100644 --- a/clients/memexist.cc +++ b/clients/memexist.cc @@ -36,13 +36,10 @@ static void options_parse(int argc, char *argv[]); int main(int argc, char *argv[]) { - memcached_st *memc; - memcached_server_st *servers; - options_parse(argc, argv); initialize_sockets(); - if (opt_servers == 0) + if (opt_servers == NULL) { char *temp; @@ -50,17 +47,24 @@ int main(int argc, char *argv[]) { opt_servers= strdup(temp); } - else + + if (opt_servers == NULL) { std::cerr << "No Servers provided" << std::endl; - return EXIT_FAILURE; + exit(EXIT_FAILURE); } } - memc= memcached_create(NULL); + memcached_server_st* servers= memcached_servers_parse(opt_servers); + if (servers == NULL or memcached_server_list_count(servers) == 0) + { + std::cerr << "Invalid server list provided:" << opt_servers << std::endl; + return EXIT_FAILURE; + } + + memcached_st* memc= memcached_create(NULL); process_hash_option(memc, opt_hash); - servers= memcached_servers_parse(opt_servers); memcached_server_push(memc, servers); memcached_server_list_free(servers); memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, diff --git a/clients/memflush.cc b/clients/memflush.cc index 17abff70..6f9d0c86 100644 --- a/clients/memflush.cc +++ b/clients/memflush.cc @@ -47,16 +47,22 @@ int main(int argc, char *argv[]) { opt_servers= strdup(temp); } - else + + if (opt_servers == false) { std::cerr << "No Servers provided" << std::endl; exit(EXIT_FAILURE); } } - memcached_st *memc= memcached_create(NULL); + memcached_server_st* servers= memcached_servers_parse(opt_servers); + if (servers == NULL or memcached_server_list_count(servers) == 0) + { + std::cerr << "Invalid server list provided:" << opt_servers << std::endl; + return EXIT_FAILURE; + } - memcached_server_st *servers= memcached_servers_parse(opt_servers); + memcached_st *memc= memcached_create(NULL); memcached_server_push(memc, servers); memcached_server_list_free(servers); memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, diff --git a/clients/memping.cc b/clients/memping.cc index 39546d4f..bbbdfda9 100644 --- a/clients/memping.cc +++ b/clients/memping.cc @@ -49,7 +49,8 @@ int main(int argc, char *argv[]) { opt_servers= strdup(temp); } - else + + if (opt_servers == NULL) { std::cerr << "No Servers provided" << std::endl; exit(EXIT_FAILURE); @@ -58,6 +59,12 @@ int main(int argc, char *argv[]) int exit_code= EXIT_SUCCESS; memcached_server_st *servers= memcached_servers_parse(opt_servers); + if (servers == NULL or memcached_server_list_count(servers) == 0) + { + std::cerr << "Invalid server list provided:" << opt_servers << std::endl; + exit_code= EXIT_FAILURE; + } + else { for (uint32_t x= 0; x < memcached_server_list_count(servers); x++) { @@ -65,6 +72,11 @@ int main(int argc, char *argv[]) const char *hostname= servers[x].hostname; in_port_t port= servers[x].port; + if (opt_verbose) + { + std::cout << "Trying to ping " << hostname << ":" << port << std::endl; + } + if (libmemcached_util_ping2(hostname, port, opt_username, opt_passwd, &instance_rc) == false) { std::cerr << "Failed to ping " << hostname << ":" << port << " " << memcached_strerror(NULL, instance_rc) << std::endl; diff --git a/clients/memrm.cc b/clients/memrm.cc index 4e4cd479..9b7ca1d0 100644 --- a/clients/memrm.cc +++ b/clients/memrm.cc @@ -38,13 +38,10 @@ static void options_parse(int argc, char *argv[]); int main(int argc, char *argv[]) { - memcached_st *memc; - memcached_server_st *servers; - options_parse(argc, argv); initialize_sockets(); - if (opt_servers == 0) + if (opt_servers == NULL) { char *temp; @@ -52,17 +49,24 @@ int main(int argc, char *argv[]) { opt_servers= strdup(temp); } - else + + if (opt_servers == NULL) { std::cerr << "No Servers provided" << std::endl; - return EXIT_FAILURE; + exit(EXIT_FAILURE); } } - memc= memcached_create(NULL); + memcached_server_st* servers= memcached_servers_parse(opt_servers); + if (servers == NULL or memcached_server_list_count(servers) == 0) + { + std::cerr << "Invalid server list provided:" << opt_servers << std::endl; + return EXIT_FAILURE; + } + + memcached_st* memc= memcached_create(NULL); process_hash_option(memc, opt_hash); - servers= memcached_servers_parse(opt_servers); memcached_server_push(memc, servers); memcached_server_list_free(servers); memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, diff --git a/clients/memslap.cc b/clients/memslap.cc index e66ab0fd..05a491fb 100644 --- a/clients/memslap.cc +++ b/clients/memslap.cc @@ -212,14 +212,20 @@ int main(int argc, char *argv[]) { opt_servers= strdup(temp); } - else + + if (opt_servers == NULL) { - fprintf(stderr, "No Servers provided\n"); - return EXIT_FAILURE; + std::cerr << "No Servers provided" << std::endl; + exit(EXIT_FAILURE); } } memcached_server_st *servers= memcached_servers_parse(opt_servers); + if (servers == NULL or memcached_server_list_count(servers) == 0) + { + std::cerr << "Invalid server list provided:" << opt_servers << std::endl; + return EXIT_FAILURE; + } pthread_mutex_init(&sleeper_mutex, NULL); pthread_cond_init(&sleep_threshhold, NULL); diff --git a/clients/memstat.cc b/clients/memstat.cc index ae6a3565..2e7d4b81 100644 --- a/clients/memstat.cc +++ b/clients/memstat.cc @@ -105,26 +105,36 @@ int main(int argc, char *argv[]) options_parse(argc, argv); initialize_sockets(); - if (opt_servers == false) + if (opt_servers == NULL) { char *temp; if ((temp= getenv("MEMCACHED_SERVERS"))) { opt_servers= strdup(temp); } - else + + if (opt_servers == NULL) { std::cerr << "No Servers provided" << std::endl; return EXIT_FAILURE; } } + memcached_server_st* servers= memcached_servers_parse(opt_servers); + if (servers == NULL or memcached_server_list_count(servers) == 0) + { + std::cerr << "Invalid server list provided:" << opt_servers << std::endl; + return EXIT_FAILURE; + } + + if (opt_servers) + { + free(opt_servers); + } + memcached_st *memc= memcached_create(NULL); memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, opt_binary); - memcached_server_st *servers= memcached_servers_parse(opt_servers); - free(opt_servers); - memcached_return_t rc= memcached_server_push(memc, servers); memcached_server_list_free(servers); @@ -211,9 +221,8 @@ static void run_analyzer(memcached_st *memc, memcached_stat_st *memc_stat) uint32_t num_of_tests= 32; const char *test_key= "libmemcached_test_key"; - memcached_st **servers; - servers= static_cast(malloc(sizeof(memcached_st*) * server_count)); - if (not servers) + memcached_st **servers= static_cast(malloc(sizeof(memcached_st*) * server_count)); + if (servers == NULL) { fprintf(stderr, "Failed to allocate memory\n"); return; @@ -228,13 +237,18 @@ static void run_analyzer(memcached_st *memc, memcached_stat_st *memc_stat) { fprintf(stderr, "Failed to memcached_create()\n"); if (x > 0) + { memcached_free(servers[0]); + } x--; for (; x > 0; x--) + { memcached_free(servers[x]); + } free(servers); + return; } memcached_server_add(servers[x], @@ -258,8 +272,10 @@ static void run_analyzer(memcached_st *memc, memcached_stat_st *memc_stat) size_t vlen; char *val= memcached_get(servers[x], test_key, strlen(test_key), &vlen, &flags, &rc); - if (rc != MEMCACHED_NOTFOUND && rc != MEMCACHED_SUCCESS) + if (rc != MEMCACHED_NOTFOUND and rc != MEMCACHED_SUCCESS) + { break; + } free(val); } gettimeofday(&end_time, NULL); @@ -302,7 +318,9 @@ static void run_analyzer(memcached_st *memc, memcached_stat_st *memc_stat) printf("\n"); for (uint32_t x= 0; x < server_count; x++) + { memcached_free(servers[x]); + } free(servers); free(analyze_mode); diff --git a/clients/memtouch.cc b/clients/memtouch.cc index 19213569..bf68a3db 100644 --- a/clients/memtouch.cc +++ b/clients/memtouch.cc @@ -54,18 +54,24 @@ int main(int argc, char *argv[]) { opt_servers= strdup(temp); } - else + + if (opt_servers == NULL) { std::cerr << "No Servers provided" << std::endl; - return EXIT_FAILURE; + exit(EXIT_FAILURE); } } + memcached_server_st* servers= memcached_servers_parse(opt_servers); + if (servers == NULL or memcached_server_list_count(servers) == 0) + { + std::cerr << "Invalid server list provided:" << opt_servers << std::endl; + return EXIT_FAILURE; + } + memcached_st *memc= memcached_create(NULL); process_hash_option(memc, opt_hash); - memcached_server_st *servers= memcached_servers_parse(opt_servers); - memcached_server_push(memc, servers); memcached_server_list_free(servers); memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, diff --git a/clients/utilities.cc b/clients/utilities.cc index 15889051..268b7904 100644 --- a/clients/utilities.cc +++ b/clients/utilities.cc @@ -144,7 +144,9 @@ void process_hash_option(memcached_st *memc, char *opt_hash) memcached_return_t rc; if (opt_hash == NULL) + { return; + } set= MEMCACHED_HASH_DEFAULT; /* Just here to solve warning */ if (!strcasecmp(opt_hash, "CRC")) diff --git a/tests/cli.am b/tests/cli.am index bc0d9a06..573be2af 100644 --- a/tests/cli.am +++ b/tests/cli.am @@ -65,6 +65,13 @@ tests_memcat_LDADD= libtest/libtest.la $(TESTS_LDADDS) check_PROGRAMS+= tests/memcat noinst_PROGRAMS+= tests/memcat +tests_memping_SOURCES= tests/memping.cc +tests_memping_CXXFLAGS= $(AM_CXXFLAGS) $(NO_EFF_CXX) +EXTRA_tests_memping_DEPENDENCIES= clients/memping +tests_memping_LDADD= libtest/libtest.la $(TESTS_LDADDS) +check_PROGRAMS+= tests/memping +noinst_PROGRAMS+= tests/memping + tests_memerror_SOURCES= tests/memerror.cc tests_memerror_CXXFLAGS= $(AM_CXXFLAGS) $(NO_EFF_CXX) EXTRA_tests_memerror_DEPENDENCIES= clients/memerror @@ -101,6 +108,9 @@ test-memerror: tests/memerror test-memtouch: tests/memtouch tests/memtouch +test-memping: tests/memping + tests/memping + valgrind-memerror: tests/memerror @$(VALGRIND_COMMAND) tests/memerror diff --git a/tests/memping.cc b/tests/memping.cc new file mode 100644 index 00000000..543efc7c --- /dev/null +++ b/tests/memping.cc @@ -0,0 +1,121 @@ +/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: + * + * Test memping + * + * 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 + * 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. + * + */ + + +/* + Test that we are cycling the servers we are creating during testing. +*/ + +#include + +#include +#include + +using namespace libtest; + +#ifndef __INTEL_COMPILER +#pragma GCC diagnostic ignored "-Wstrict-aliasing" +#endif + +static std::string executable("clients/memping"); + +static test_return_t help_test(void *) +{ + const char *args[]= { "--help", 0 }; + + test_compare(EXIT_SUCCESS, exec_cmdline(executable, args, true)); + + return TEST_SUCCESS; +} + +static test_return_t ping_TEST(void *) +{ + char buffer[1024]; + snprintf(buffer, sizeof(buffer), "--server=localhost:%d", int(default_port())); + const char *args[]= { buffer, 0 }; + + test_compare(EXIT_SUCCESS, exec_cmdline(executable, args, true)); + + return TEST_SUCCESS; +} + +static test_return_t NOT_FOUND_TEST(void *) +{ + char buffer[1024]; + snprintf(buffer, sizeof(buffer), "--server=nonexist.libmemcached.org:%d", int(default_port())); + const char *args[]= { buffer, 0 }; + + test_compare(EXIT_FAILURE, exec_cmdline(executable, args, true)); + + return TEST_SUCCESS; +} + +test_st memping_TESTS[] ={ + {"--help", true, help_test }, + {"ping(FOUND)", true, ping_TEST }, + {"ping(NOT_FOUND)", true, NOT_FOUND_TEST }, + {0, 0, 0} +}; + +collection_st collection[] ={ + {"memping", 0, 0, memping_TESTS }, + {0, 0, 0, 0} +}; + +static void *world_create(server_startup_st& servers, test_return_t& error) +{ + if (libtest::has_memcached() == false) + { + error= TEST_SKIPPED; + return NULL; + } + + if (server_startup(servers, "memcached", libtest::default_port(), NULL) == false) + { + error= TEST_FAILURE; + } + + return &servers; +} + + +void get_world(libtest::Framework* world) +{ + world->collections(collection); + world->create(world_create); +} + +