memcached.cc 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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 <config.h>
  37. #include <libtest/common.h>
  38. #include <libmemcached-1.0/memcached.h>
  39. #include <libmemcachedutil-1.0/util.h>
  40. using namespace libtest;
  41. #include <cassert>
  42. #include <cerrno>
  43. #include <cstdio>
  44. #include <cstdlib>
  45. #include <cstring>
  46. #include <iostream>
  47. #include <signal.h>
  48. #include <sys/types.h>
  49. #include <sys/wait.h>
  50. #include <unistd.h>
  51. #include <libtest/server.h>
  52. #include <libtest/wait.h>
  53. #include <libtest/memcached.h>
  54. #ifndef __INTEL_COMPILER
  55. #pragma GCC diagnostic ignored "-Wold-style-cast"
  56. #endif
  57. using namespace libtest;
  58. namespace {
  59. bool is_memcached_libtool()
  60. {
  61. if (MEMCACHED_BINARY and strcmp(MEMCACHED_BINARY, "memcached/memcached") == 0)
  62. {
  63. return true;
  64. }
  65. return false;
  66. }
  67. }
  68. class Memcached : public libtest::Server
  69. {
  70. std::string _username;
  71. std::string _password;
  72. public:
  73. Memcached(const std::string& host_arg,
  74. const in_port_t port_arg,
  75. const bool is_socket_arg,
  76. const std::string& username_arg,
  77. const std::string& password_arg) :
  78. libtest::Server(host_arg, port_arg,
  79. MEMCACHED_BINARY, is_memcached_libtool(), is_socket_arg),
  80. _username(username_arg),
  81. _password(password_arg)
  82. { }
  83. Memcached(const std::string& host_arg, const in_port_t port_arg, const bool is_socket_arg) :
  84. libtest::Server(host_arg, port_arg,
  85. MEMCACHED_BINARY, is_memcached_libtool(), is_socket_arg)
  86. {
  87. set_pid_file();
  88. }
  89. virtual const char *sasl() const
  90. {
  91. return NULL;
  92. }
  93. const std::string& password() const
  94. {
  95. return _password;
  96. }
  97. const std::string& username() const
  98. {
  99. return _username;
  100. }
  101. bool wait_for_pidfile() const
  102. {
  103. Wait wait(pid(), 4);
  104. return wait.successful();
  105. }
  106. bool ping()
  107. {
  108. #if 0
  109. // Memcached is slow to start, so we need to do this
  110. if (pid_file().empty() == false)
  111. {
  112. if (wait_for_pidfile() == false)
  113. {
  114. Error << "Pidfile was not found:" << pid_file() << " :" << running();
  115. return -1;
  116. }
  117. }
  118. #endif
  119. memcached_return_t rc;
  120. bool ret;
  121. if (has_socket())
  122. {
  123. ret= libmemcached_util_ping(socket().c_str(), 0, &rc);
  124. }
  125. else
  126. {
  127. ret= libmemcached_util_ping(hostname().c_str(), port(), &rc);
  128. }
  129. if (memcached_failed(rc) or ret == false)
  130. {
  131. error(memcached_strerror(NULL, rc));
  132. }
  133. return ret;
  134. }
  135. const char *name()
  136. {
  137. return "memcached";
  138. };
  139. const char *executable()
  140. {
  141. return MEMCACHED_BINARY;
  142. }
  143. bool is_libtool()
  144. {
  145. return is_memcached_libtool();
  146. }
  147. virtual void pid_file_option(Application& app, const std::string& arg)
  148. {
  149. if (arg.empty() == false)
  150. {
  151. app.add_option("-P", arg);
  152. }
  153. }
  154. const char *socket_file_option() const
  155. {
  156. return "-s ";
  157. }
  158. virtual void port_option(Application& app, in_port_t arg)
  159. {
  160. char buffer[30];
  161. snprintf(buffer, sizeof(buffer), "%d", int(arg));
  162. app.add_option("-p", buffer);
  163. }
  164. bool has_port_option() const
  165. {
  166. return true;
  167. }
  168. bool has_socket_file_option() const
  169. {
  170. return has_socket();
  171. }
  172. void socket_file_option(Application& app, const std::string& socket_arg)
  173. {
  174. if (socket_arg.empty() == false)
  175. {
  176. app.add_option("-s", socket_arg);
  177. }
  178. }
  179. bool broken_socket_cleanup()
  180. {
  181. return true;
  182. }
  183. // Memcached's pidfile is broken
  184. bool broken_pid_file()
  185. {
  186. return true;
  187. }
  188. bool build(size_t argc, const char *argv[]);
  189. };
  190. class MemcachedLight : public libtest::Server
  191. {
  192. public:
  193. MemcachedLight(const std::string& host_arg, const in_port_t port_arg) :
  194. libtest::Server(host_arg, port_arg, MEMCACHED_LIGHT_BINARY, true)
  195. {
  196. set_pid_file();
  197. }
  198. bool ping()
  199. {
  200. // Memcached is slow to start, so we need to do this
  201. if (not pid_file().empty())
  202. {
  203. if (not wait_for_pidfile())
  204. {
  205. Error << "Pidfile was not found:" << pid_file();
  206. return false;
  207. }
  208. }
  209. std::stringstream error_message;
  210. pid_t local_pid= get_pid_from_file(pid_file(), error_message);
  211. if (local_pid > 0)
  212. {
  213. if (::kill(local_pid, 0) == 0)
  214. {
  215. return true;
  216. }
  217. }
  218. return false;
  219. }
  220. const char *name()
  221. {
  222. return "memcached_light";
  223. };
  224. const char *executable()
  225. {
  226. return MEMCACHED_LIGHT_BINARY;
  227. }
  228. virtual void port_option(Application& app, in_port_t arg)
  229. {
  230. char buffer[1024];
  231. snprintf(buffer, sizeof(buffer), "--port=%d", int(arg));
  232. app.add_option(buffer);
  233. }
  234. bool has_port_option() const
  235. {
  236. return true;
  237. }
  238. bool is_libtool()
  239. {
  240. return true;
  241. }
  242. void log_file_option(Application& app, const std::string& arg)
  243. {
  244. if (arg.empty() == false)
  245. {
  246. std::string buffer("--log-file=");
  247. buffer+= arg;
  248. app.add_option("--verbose");
  249. app.add_option(buffer);
  250. }
  251. }
  252. bool has_log_file_option() const
  253. {
  254. return true;
  255. }
  256. bool build(size_t argc, const char *argv[]);
  257. };
  258. class MemcachedSaSL : public Memcached
  259. {
  260. public:
  261. MemcachedSaSL(const std::string& host_arg,
  262. const in_port_t port_arg,
  263. const bool is_socket_arg,
  264. const std::string& username_arg,
  265. const std::string &password_arg) :
  266. Memcached(host_arg, port_arg, is_socket_arg, username_arg, password_arg)
  267. { }
  268. const char *name()
  269. {
  270. return "memcached-sasl";
  271. };
  272. const char *sasl() const
  273. {
  274. return " -S -B binary ";
  275. }
  276. const char *executable()
  277. {
  278. return MEMCACHED_SASL_BINARY;
  279. }
  280. bool ping()
  281. {
  282. memcached_return_t rc;
  283. bool ret;
  284. if (has_socket())
  285. {
  286. ret= libmemcached_util_ping2(socket().c_str(), 0, username().c_str(), password().c_str(), &rc);
  287. }
  288. else
  289. {
  290. ret= libmemcached_util_ping2(hostname().c_str(), port(), username().c_str(), password().c_str(), &rc);
  291. }
  292. if (memcached_failed(rc) or ret == false)
  293. {
  294. error(memcached_strerror(NULL, rc));
  295. }
  296. return ret;
  297. }
  298. };
  299. #include <sstream>
  300. bool Memcached::build(size_t argc, const char *argv[])
  301. {
  302. if (getuid() == 0 or geteuid() == 0)
  303. {
  304. add_option("-u", "root");
  305. }
  306. add_option("-l", "localhost");
  307. #ifndef TARGET_OS_OSX
  308. add_option("-m", "128");
  309. add_option("-M");
  310. #endif
  311. if (sasl())
  312. {
  313. add_option(sasl());
  314. }
  315. for (int x= 0 ; x < argc ; x++)
  316. {
  317. add_option(argv[x]);
  318. }
  319. return true;
  320. }
  321. bool MemcachedLight::build(size_t argc, const char *argv[])
  322. {
  323. for (size_t x= 0 ; x < argc ; x++)
  324. {
  325. add_option(argv[x]);
  326. }
  327. return true;
  328. }
  329. namespace libtest {
  330. libtest::Server *build_memcached(const std::string& hostname, const in_port_t try_port)
  331. {
  332. return new Memcached(hostname, try_port, false);
  333. }
  334. libtest::Server *build_memcached_socket(const std::string& socket_file, const in_port_t try_port)
  335. {
  336. return new Memcached(socket_file, try_port, true);
  337. }
  338. libtest::Server *build_memcached_light(const std::string& hostname, const in_port_t try_port)
  339. {
  340. return new MemcachedLight(hostname, try_port);
  341. }
  342. libtest::Server *build_memcached_sasl(const std::string& hostname, const in_port_t try_port, const std::string& username, const std::string &password)
  343. {
  344. if (username.empty())
  345. {
  346. return new MemcachedSaSL(hostname, try_port, false, "memcached", "memcached");
  347. }
  348. return new MemcachedSaSL(hostname, try_port, false, username, password);
  349. }
  350. 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)
  351. {
  352. if (username.empty())
  353. {
  354. return new MemcachedSaSL(socket_file, try_port, true, "memcached", "memcached");
  355. }
  356. return new MemcachedSaSL(socket_file, try_port, true, username, password);
  357. }
  358. }