memcached.cc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Data Differential YATL (i.e. libtest) library
  4. *
  5. * Copyright (C) 2012 Data Differential, http://datadifferential.com/
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are
  9. * met:
  10. *
  11. * * Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * * Redistributions in binary form must reproduce the above
  15. * copyright notice, this list of conditions and the following disclaimer
  16. * in the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * * The names of its contributors may not be used to endorse or
  20. * promote products derived from this software without specific prior
  21. * written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  24. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  25. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  26. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  27. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  28. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  29. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  30. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  31. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  32. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  33. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. */
  36. #include "gear_config.h"
  37. #include "libtest/common.h"
  38. #include <libmemcached-1.0/memcached.h>
  39. #include <libmemcachedutil-1.0/util.h>
  40. #include <cassert>
  41. #include <cerrno>
  42. #include <cstdio>
  43. #include <cstdlib>
  44. #include <cstring>
  45. #include <iostream>
  46. #include <signal.h>
  47. #include <sys/types.h>
  48. #include <sys/wait.h>
  49. #include <unistd.h>
  50. #include <libtest/server.h>
  51. #include <libtest/wait.h>
  52. #include <libtest/memcached.h>
  53. #ifndef __INTEL_COMPILER
  54. #pragma GCC diagnostic ignored "-Wold-style-cast"
  55. #endif
  56. namespace libtest {
  57. class Memcached : public libtest::Server
  58. {
  59. std::string _username;
  60. std::string _password;
  61. public:
  62. Memcached(const std::string& host_arg,
  63. const in_port_t port_arg,
  64. const bool is_socket_arg,
  65. const std::string& username_arg,
  66. const std::string& password_arg) :
  67. libtest::Server(host_arg, port_arg,
  68. MEMCACHED_BINARY, false, is_socket_arg),
  69. _username(username_arg),
  70. _password(password_arg)
  71. { }
  72. Memcached(const std::string& host_arg, const in_port_t port_arg, const bool is_socket_arg) :
  73. libtest::Server(host_arg, port_arg,
  74. MEMCACHED_BINARY, false, is_socket_arg)
  75. {
  76. set_pid_file();
  77. }
  78. virtual const char *sasl() const
  79. {
  80. return NULL;
  81. }
  82. const std::string& password() const
  83. {
  84. return _password;
  85. }
  86. const std::string& username() const
  87. {
  88. return _username;
  89. }
  90. bool wait_for_pidfile() const
  91. {
  92. Wait wait(pid(), 4);
  93. return wait.successful();
  94. }
  95. bool ping()
  96. {
  97. if (out_of_ban_killed())
  98. {
  99. return false;
  100. }
  101. if (is_socket())
  102. {
  103. return _app.check();
  104. }
  105. SimpleClient client(_hostname, _port);
  106. std::string response;
  107. return client.send_message("version", response);
  108. }
  109. const char *name()
  110. {
  111. return "memcached";
  112. };
  113. const char *executable()
  114. {
  115. return MEMCACHED_BINARY;
  116. }
  117. bool is_libtool()
  118. {
  119. return false;
  120. }
  121. virtual void pid_file_option(Application& app, const std::string& arg)
  122. {
  123. if (arg.empty() == false)
  124. {
  125. app.add_option("-P", arg);
  126. }
  127. }
  128. const char *socket_file_option() const
  129. {
  130. return "-s ";
  131. }
  132. virtual void port_option(Application& app, in_port_t arg)
  133. {
  134. char buffer[30];
  135. snprintf(buffer, sizeof(buffer), "%d", int(arg));
  136. app.add_option("-p", buffer);
  137. }
  138. bool has_port_option() const
  139. {
  140. return true;
  141. }
  142. bool has_socket_file_option() const
  143. {
  144. return has_socket();
  145. }
  146. void socket_file_option(Application& app, const std::string& socket_arg)
  147. {
  148. if (socket_arg.empty() == false)
  149. {
  150. app.add_option("-s", socket_arg);
  151. }
  152. }
  153. bool broken_socket_cleanup()
  154. {
  155. return true;
  156. }
  157. // Memcached's pidfile is broken
  158. bool broken_pid_file()
  159. {
  160. return true;
  161. }
  162. bool build(size_t argc, const char *argv[]);
  163. };
  164. #include <sstream>
  165. bool Memcached::build(size_t argc, const char *argv[])
  166. {
  167. if (getuid() == 0 or geteuid() == 0)
  168. {
  169. add_option("-u", "root");
  170. }
  171. add_option("-l", "localhost");
  172. #ifndef TARGET_OS_OSX
  173. add_option("-m", "128");
  174. add_option("-M");
  175. #endif
  176. if (sasl())
  177. {
  178. add_option(sasl());
  179. }
  180. for (size_t x= 0 ; x < argc ; x++)
  181. {
  182. add_option(argv[x]);
  183. }
  184. return true;
  185. }
  186. libtest::Server *build_memcached(const std::string& hostname, const in_port_t try_port)
  187. {
  188. return new Memcached(hostname, try_port, false);
  189. }
  190. libtest::Server *build_memcached_socket(const std::string& socket_file, const in_port_t try_port)
  191. {
  192. return new Memcached(socket_file, try_port, true);
  193. }
  194. libtest::Server *build_memcached_sasl(const std::string& hostname, const in_port_t try_port, const std::string& username, const std::string &password)
  195. {
  196. if (username.empty())
  197. {
  198. return new Memcached(hostname, try_port, false, "memcached", "memcached");
  199. }
  200. return new Memcached(hostname, try_port, false, username, password);
  201. }
  202. } // namespace libtest