memcached.cc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * libtest
  4. *
  5. * Copyright (C) 2011 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/common.h>
  37. #include <libmemcached/memcached.h>
  38. #include <libmemcached/util.h>
  39. using namespace libtest;
  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. using namespace libtest;
  57. class Memcached : public Server
  58. {
  59. public:
  60. Memcached(const std::string& host_arg, const in_port_t port_arg, const bool is_socket_arg) :
  61. Server(host_arg, port_arg, is_socket_arg)
  62. { }
  63. pid_t get_pid(bool error_is_ok)
  64. {
  65. // Memcached is slow to start, so we need to do this
  66. if (not pid_file().empty())
  67. {
  68. Wait wait(pid_file(), 0);
  69. if (error_is_ok and not wait.successful())
  70. {
  71. Error << "Pidfile was not found:" << pid_file();
  72. return -1;
  73. }
  74. }
  75. pid_t local_pid;
  76. memcached_return_t rc;
  77. if (has_socket())
  78. {
  79. local_pid= libmemcached_util_getpid(socket().c_str(), port(), &rc);
  80. }
  81. else
  82. {
  83. local_pid= libmemcached_util_getpid(hostname().c_str(), port(), &rc);
  84. }
  85. if (error_is_ok and ((memcached_failed(rc) or local_pid < 1)))
  86. {
  87. Error << "libmemcached_util_getpid(" << memcached_strerror(NULL, rc) << ") pid: " << local_pid << " for:" << *this;
  88. }
  89. return local_pid;
  90. }
  91. bool ping()
  92. {
  93. // Memcached is slow to start, so we need to do this
  94. if (not pid_file().empty())
  95. {
  96. Wait wait(pid_file(), 0);
  97. if (not wait.successful())
  98. {
  99. Error << "Pidfile was not found:" << pid_file();
  100. return -1;
  101. }
  102. }
  103. memcached_return_t rc;
  104. bool ret;
  105. if (has_socket())
  106. {
  107. ret= libmemcached_util_ping(socket().c_str(), 0, &rc);
  108. }
  109. else
  110. {
  111. ret= libmemcached_util_ping(hostname().c_str(), port(), &rc);
  112. }
  113. if (memcached_failed(rc) or not ret)
  114. {
  115. Error << "libmemcached_util_ping(" << memcached_strerror(NULL, rc) << ")";
  116. }
  117. return ret;
  118. }
  119. const char *name()
  120. {
  121. return "memcached";
  122. };
  123. const char *executable()
  124. {
  125. return MEMCACHED_BINARY;
  126. }
  127. const char *pid_file_option()
  128. {
  129. return "-P ";
  130. }
  131. const char *socket_file_option() const
  132. {
  133. return "-s ";
  134. }
  135. const char *daemon_file_option()
  136. {
  137. return "-d";
  138. }
  139. const char *log_file_option()
  140. {
  141. return NULL;
  142. }
  143. const char *port_option()
  144. {
  145. return "-p ";
  146. }
  147. bool is_libtool()
  148. {
  149. return false;
  150. }
  151. // Memcached's pidfile is broken
  152. bool broken_pid_file()
  153. {
  154. return true;
  155. }
  156. bool build(int argc, const char *argv[]);
  157. };
  158. #include <sstream>
  159. bool Memcached::build(int argc, const char *argv[])
  160. {
  161. std::stringstream arg_buffer;
  162. if (getuid() == 0 or geteuid() == 0)
  163. {
  164. arg_buffer << " -u root ";
  165. }
  166. for (int x= 1 ; x < argc ; x++)
  167. {
  168. arg_buffer << " " << argv[x] << " ";
  169. }
  170. set_extra_args(arg_buffer.str());
  171. return true;
  172. }
  173. namespace libtest {
  174. Server *build_memcached(const std::string& hostname, const in_port_t try_port)
  175. {
  176. return new Memcached(hostname, try_port, false);
  177. }
  178. Server *build_memcached_socket(const std::string& hostname, const in_port_t try_port)
  179. {
  180. return new Memcached(hostname, try_port, true);
  181. }
  182. }