memcached.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 "libtest/yatlcon.h"
  37. #include "libtest/common.h"
  38. #include <cassert>
  39. #include <cerrno>
  40. #include <cstdio>
  41. #include <cstdlib>
  42. #include <cstring>
  43. #include <iostream>
  44. #include <signal.h>
  45. #include <sys/types.h>
  46. #include <sys/wait.h>
  47. #include <unistd.h>
  48. #include <libtest/server.h>
  49. #include <libtest/wait.h>
  50. #include <libtest/memcached.h>
  51. #ifndef __INTEL_COMPILER
  52. #pragma GCC diagnostic ignored "-Wold-style-cast"
  53. #endif
  54. namespace libtest {
  55. class Memcached : public libtest::Server
  56. {
  57. std::string _username;
  58. std::string _password;
  59. public:
  60. Memcached(const std::string& host_arg,
  61. const in_port_t port_arg,
  62. const bool is_socket_arg,
  63. const std::string& username_arg,
  64. const std::string& password_arg) :
  65. libtest::Server(host_arg, port_arg,
  66. memcached_binary(), false, is_socket_arg),
  67. _username(username_arg),
  68. _password(password_arg)
  69. { }
  70. Memcached(const std::string& host_arg, const in_port_t port_arg, const bool is_socket_arg) :
  71. libtest::Server(host_arg, port_arg,
  72. memcached_binary(), false, is_socket_arg)
  73. {
  74. set_pid_file();
  75. }
  76. virtual const char *sasl() const
  77. {
  78. return NULL;
  79. }
  80. const std::string& password() const
  81. {
  82. return _password;
  83. }
  84. const std::string& username() const
  85. {
  86. return _username;
  87. }
  88. bool wait_for_pidfile() const
  89. {
  90. Wait wait(pid(), 4);
  91. return wait.successful();
  92. }
  93. bool ping()
  94. {
  95. if (out_of_ban_killed())
  96. {
  97. return false;
  98. }
  99. if (is_socket())
  100. {
  101. return _app.check();
  102. }
  103. SimpleClient client(_hostname, _port);
  104. std::string response;
  105. return client.send_message("version", response);
  106. }
  107. const char *name()
  108. {
  109. return "memcached";
  110. };
  111. const char *executable()
  112. {
  113. return memcached_binary();
  114. }
  115. bool is_libtool()
  116. {
  117. return false;
  118. }
  119. virtual void pid_file_option(Application& app, const std::string& arg)
  120. {
  121. if (arg.empty() == false)
  122. {
  123. app.add_option("-P", arg);
  124. }
  125. }
  126. const char *socket_file_option() const
  127. {
  128. return "-s ";
  129. }
  130. virtual void port_option(Application& app, in_port_t arg)
  131. {
  132. char buffer[30];
  133. snprintf(buffer, sizeof(buffer), "%d", int(arg));
  134. app.add_option("-p", buffer);
  135. }
  136. bool has_port_option() const
  137. {
  138. return true;
  139. }
  140. bool has_socket_file_option() const
  141. {
  142. return has_socket();
  143. }
  144. void socket_file_option(Application& app, const std::string& socket_arg)
  145. {
  146. if (socket_arg.empty() == false)
  147. {
  148. app.add_option("-s", socket_arg);
  149. }
  150. }
  151. bool broken_socket_cleanup()
  152. {
  153. return true;
  154. }
  155. bool is_valgrind() const
  156. {
  157. return false;
  158. }
  159. // Memcached's pidfile is broken
  160. bool broken_pid_file()
  161. {
  162. return true;
  163. }
  164. bool build();
  165. };
  166. #include <sstream>
  167. bool Memcached::build()
  168. {
  169. if (getuid() == 0 or geteuid() == 0)
  170. {
  171. add_option("-u", "root");
  172. }
  173. add_option("-l", "localhost");
  174. #ifdef __APPLE__
  175. #else
  176. add_option("-m", "128");
  177. add_option("-M");
  178. #endif
  179. if (sasl())
  180. {
  181. add_option(sasl());
  182. }
  183. return true;
  184. }
  185. libtest::Server *build_memcached(const std::string& hostname, const in_port_t try_port)
  186. {
  187. if (has_memcached())
  188. {
  189. return new Memcached(hostname, try_port, false);
  190. }
  191. return NULL;
  192. }
  193. libtest::Server *build_memcached_socket(const std::string& socket_file, const in_port_t try_port)
  194. {
  195. if (has_memcached())
  196. {
  197. return new Memcached(socket_file, try_port, true);
  198. }
  199. return NULL;
  200. }
  201. } // namespace libtest