memcached.cc 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * libtest
  4. *
  5. * Copyright (C) 2011 Data Differential, http://datadifferential.com/
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 3 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include <libtest/common.h>
  22. #include <libmemcached/memcached.h>
  23. #include <libmemcached/util.h>
  24. using namespace libtest;
  25. #include <cassert>
  26. #include <cerrno>
  27. #include <cstdio>
  28. #include <cstdlib>
  29. #include <cstring>
  30. #include <iostream>
  31. #include <signal.h>
  32. #include <sys/types.h>
  33. #include <sys/wait.h>
  34. #include <unistd.h>
  35. #include <libtest/server.h>
  36. #include <libtest/wait.h>
  37. #include <libtest/memcached.h>
  38. #ifndef __INTEL_COMPILER
  39. #pragma GCC diagnostic ignored "-Wold-style-cast"
  40. #endif
  41. using namespace libtest;
  42. class Memcached : public libtest::Server
  43. {
  44. std::string _username;
  45. std::string _password;
  46. public:
  47. Memcached(const std::string& host_arg, const in_port_t port_arg, const bool is_socket_arg, const std::string& username_arg, const std::string& password_arg) :
  48. libtest::Server(host_arg, port_arg, is_socket_arg),
  49. _username(username_arg),
  50. _password(password_arg)
  51. { }
  52. Memcached(const std::string& host_arg, const in_port_t port_arg, const bool is_socket_arg) :
  53. libtest::Server(host_arg, port_arg, is_socket_arg)
  54. {
  55. set_pid_file();
  56. }
  57. virtual const char *sasl() const
  58. {
  59. return NULL;
  60. }
  61. const std::string& password() const
  62. {
  63. return _password;
  64. }
  65. const std::string& username() const
  66. {
  67. return _username;
  68. }
  69. pid_t get_pid(bool error_is_ok)
  70. {
  71. // Memcached is slow to start, so we need to do this
  72. if (not pid_file().empty())
  73. {
  74. Wait wait(pid_file(), 0);
  75. if (error_is_ok and not wait.successful())
  76. {
  77. Error << "Pidfile was not found:" << pid_file();
  78. return -1;
  79. }
  80. }
  81. pid_t local_pid;
  82. memcached_return_t rc= MEMCACHED_SUCCESS;
  83. if (has_socket())
  84. {
  85. local_pid= libmemcached_util_getpid(socket().c_str(), 0, &rc);
  86. }
  87. else
  88. {
  89. local_pid= libmemcached_util_getpid(hostname().c_str(), port(), &rc);
  90. }
  91. if (error_is_ok and ((memcached_failed(rc) or not is_pid_valid(local_pid))))
  92. {
  93. Error << "libmemcached_util_getpid(" << memcached_strerror(NULL, rc) << ") pid: " << local_pid << " for:" << *this;
  94. }
  95. return local_pid;
  96. }
  97. bool ping()
  98. {
  99. // Memcached is slow to start, so we need to do this
  100. if (not pid_file().empty())
  101. {
  102. Wait wait(pid_file(), 0);
  103. if (not wait.successful())
  104. {
  105. Error << "Pidfile was not found:" << pid_file();
  106. return -1;
  107. }
  108. }
  109. memcached_return_t rc;
  110. bool ret;
  111. if (has_socket())
  112. {
  113. ret= libmemcached_util_ping(socket().c_str(), 0, &rc);
  114. }
  115. else
  116. {
  117. ret= libmemcached_util_ping(hostname().c_str(), port(), &rc);
  118. }
  119. if (memcached_failed(rc) or not ret)
  120. {
  121. Error << "libmemcached_util_ping(" << hostname() << ", " << port() << ") error: " << memcached_strerror(NULL, rc);
  122. }
  123. return ret;
  124. }
  125. const char *name()
  126. {
  127. return "memcached";
  128. };
  129. const char *executable()
  130. {
  131. return MEMCACHED_BINARY;
  132. }
  133. const char *pid_file_option()
  134. {
  135. return "-P ";
  136. }
  137. const char *socket_file_option() const
  138. {
  139. return "-s ";
  140. }
  141. const char *daemon_file_option()
  142. {
  143. return "-d";
  144. }
  145. const char *log_file_option()
  146. {
  147. return NULL;
  148. }
  149. const char *port_option()
  150. {
  151. return "-p ";
  152. }
  153. bool is_libtool()
  154. {
  155. return false;
  156. }
  157. bool broken_socket_cleanup()
  158. {
  159. return true;
  160. }
  161. // Memcached's pidfile is broken
  162. bool broken_pid_file()
  163. {
  164. return true;
  165. }
  166. bool build(int argc, const char *argv[]);
  167. };
  168. class MemcachedSaSL : public Memcached
  169. {
  170. public:
  171. MemcachedSaSL(const std::string& host_arg, const in_port_t port_arg, const bool is_socket_arg, const std::string& username_arg, const std::string &password_arg) :
  172. Memcached(host_arg, port_arg, is_socket_arg, username_arg, password_arg)
  173. { }
  174. const char *name()
  175. {
  176. return "memcached-sasl";
  177. };
  178. const char *sasl() const
  179. {
  180. return " -S -B binary ";
  181. }
  182. const char *executable()
  183. {
  184. return MEMCACHED_SASL_BINARY;
  185. }
  186. pid_t get_pid(bool error_is_ok)
  187. {
  188. // Memcached is slow to start, so we need to do this
  189. if (not pid_file().empty())
  190. {
  191. Wait wait(pid_file(), 0);
  192. if (error_is_ok and not wait.successful())
  193. {
  194. Error << "Pidfile was not found:" << pid_file();
  195. return -1;
  196. }
  197. }
  198. pid_t local_pid;
  199. memcached_return_t rc;
  200. if (has_socket())
  201. {
  202. local_pid= libmemcached_util_getpid2(socket().c_str(), 0, username().c_str(), password().c_str(), &rc);
  203. }
  204. else
  205. {
  206. local_pid= libmemcached_util_getpid2(hostname().c_str(), port(), username().c_str(), password().c_str(), &rc);
  207. }
  208. if (error_is_ok and ((memcached_failed(rc) or not is_pid_valid(local_pid))))
  209. {
  210. Error << "libmemcached_util_getpid2(" << memcached_strerror(NULL, rc) << ") username: " << username() << " password: " << password() << " pid: " << local_pid << " for:" << *this;
  211. }
  212. return local_pid;
  213. }
  214. bool ping()
  215. {
  216. // Memcached is slow to start, so we need to do this
  217. if (not pid_file().empty())
  218. {
  219. Wait wait(pid_file(), 0);
  220. if (not wait.successful())
  221. {
  222. Error << "Pidfile was not found:" << pid_file();
  223. return -1;
  224. }
  225. }
  226. memcached_return_t rc;
  227. bool ret;
  228. if (has_socket())
  229. {
  230. ret= libmemcached_util_ping2(socket().c_str(), 0, username().c_str(), password().c_str(), &rc);
  231. }
  232. else
  233. {
  234. ret= libmemcached_util_ping2(hostname().c_str(), port(), username().c_str(), password().c_str(), &rc);
  235. }
  236. if (memcached_failed(rc) or not ret)
  237. {
  238. Error << "libmemcached_util_ping2(" << hostname() << ", " << port() << ", " << username() << ", " << password() << ") error: " << memcached_strerror(NULL, rc);
  239. }
  240. return ret;
  241. }
  242. };
  243. #include <sstream>
  244. bool Memcached::build(int argc, const char *argv[])
  245. {
  246. std::stringstream arg_buffer;
  247. if (getuid() == 0 or geteuid() == 0)
  248. {
  249. arg_buffer << " -u root ";
  250. }
  251. arg_buffer << " -l 127.0.0.1 ";
  252. arg_buffer << " -m 128 ";
  253. arg_buffer << " -M ";
  254. if (sasl())
  255. {
  256. arg_buffer << sasl();
  257. }
  258. for (int x= 1 ; x < argc ; x++)
  259. {
  260. arg_buffer << " " << argv[x] << " ";
  261. }
  262. set_extra_args(arg_buffer.str());
  263. return true;
  264. }
  265. namespace libtest {
  266. libtest::Server *build_memcached(const std::string& hostname, const in_port_t try_port)
  267. {
  268. return new Memcached(hostname, try_port, false);
  269. }
  270. libtest::Server *build_memcached_socket(const std::string& socket_file, const in_port_t try_port)
  271. {
  272. return new Memcached(socket_file, try_port, true);
  273. }
  274. libtest::Server *build_memcached_sasl(const std::string& hostname, const in_port_t try_port, const std::string& username, const std::string &password)
  275. {
  276. if (username.empty())
  277. {
  278. return new MemcachedSaSL(hostname, try_port, false, "memcached", "memcached");
  279. }
  280. return new MemcachedSaSL(hostname, try_port, false, username, password);
  281. }
  282. libtest::Server *build_memcached_sasl_socket(const std::string& socket_file, const in_port_t try_port, const std::string& username, const std::string &password)
  283. {
  284. if (username.empty())
  285. {
  286. return new MemcachedSaSL(socket_file, try_port, true, "memcached", "memcached");
  287. }
  288. return new MemcachedSaSL(socket_file, try_port, true, username, password);
  289. }
  290. }