gearmand.cc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 <libtest/gearmand.h>
  39. #include "libgearman/ssl.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 <sstream>
  49. #include <sys/types.h>
  50. #include <sys/wait.h>
  51. #include <unistd.h>
  52. #ifndef __INTEL_COMPILER
  53. #pragma GCC diagnostic ignored "-Wold-style-cast"
  54. #endif
  55. using namespace libtest;
  56. class Gearmand : public libtest::Server
  57. {
  58. private:
  59. public:
  60. Gearmand(const std::string& host_arg, in_port_t port_arg, bool libtool_, const char* binary);
  61. bool ping()
  62. {
  63. reset_error();
  64. if (out_of_ban_killed())
  65. {
  66. return false;
  67. }
  68. SimpleClient client(_hostname, _port);
  69. std::string response;
  70. bool ret= client.send_message("version", response);
  71. if (ret == false)
  72. {
  73. ASSERT_TRUE_(client.is_error(), "client.send_message() failed but no error was set");
  74. }
  75. if (client.is_error())
  76. {
  77. error(client.error_file(), client.error_line(), client.error());
  78. }
  79. return ret;
  80. }
  81. const char *name()
  82. {
  83. return "gearmand";
  84. };
  85. void log_file_option(Application& app, const std::string& arg)
  86. {
  87. if (arg.empty() == false)
  88. {
  89. std::string buffer("--log-file=");
  90. buffer+= arg;
  91. // @note leave the logic as a placeholder
  92. #if defined(VCS_CHECKOUT) && VCS_CHECKOUT
  93. app.add_option("--verbose=INFO");
  94. #else
  95. app.add_option("--verbose=INFO");
  96. #endif
  97. app.add_option(buffer);
  98. }
  99. }
  100. bool has_log_file_option() const
  101. {
  102. return true;
  103. }
  104. bool is_libtool()
  105. {
  106. return true;
  107. }
  108. bool has_syslog() const
  109. {
  110. return false; // --syslog.errmsg-enable
  111. }
  112. bool has_port_option() const
  113. {
  114. return true;
  115. }
  116. bool build();
  117. };
  118. Gearmand::Gearmand(const std::string& host_arg, in_port_t port_arg, bool libtool_, const char* binary_arg) :
  119. libtest::Server(host_arg, port_arg, binary_arg, libtool_)
  120. {
  121. set_pid_file();
  122. }
  123. bool Gearmand::build()
  124. {
  125. if (getuid() == 0 or geteuid() == 0)
  126. {
  127. add_option("-u", "root");
  128. }
  129. add_option("--listen=localhost");
  130. if (is_ssl())
  131. {
  132. #if defined(HAVE_SSL) && HAVE_SSL
  133. add_option("--ssl");
  134. add_option("--ssl-ca-file=" YATL_CA_CERT_PEM);
  135. add_option("--ssl-certificate=" YATL_CERT_PEM);
  136. add_option("--ssl-key=" YATL_CERT_KEY_PEM);
  137. #endif
  138. }
  139. return true;
  140. }
  141. namespace libtest {
  142. libtest::Server *build_gearmand(const char *hostname, in_port_t try_port, const char* binary)
  143. {
  144. if (binary == NULL)
  145. {
  146. #if defined(HAVE_GEARMAND_BINARY)
  147. # if defined(GEARMAND_BINARY)
  148. if (HAVE_GEARMAND_BINARY)
  149. {
  150. binary= GEARMAND_BINARY;
  151. }
  152. # endif
  153. #endif
  154. }
  155. if (binary == NULL)
  156. {
  157. return NULL;
  158. }
  159. bool is_libtool_script= true;
  160. if (binary[0] == '/')
  161. {
  162. is_libtool_script= false;
  163. }
  164. return new Gearmand(hostname, try_port, is_libtool_script, binary);
  165. }
  166. }