gearmand.cc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 <libtest/gearmand.h>
  23. #include "util/instance.hpp"
  24. #include "util/operation.hpp"
  25. using namespace datadifferential;
  26. using namespace libtest;
  27. #include <cassert>
  28. #include <cerrno>
  29. #include <cstdio>
  30. #include <cstdlib>
  31. #include <cstring>
  32. #include <iostream>
  33. #include <signal.h>
  34. #include <sstream>
  35. #include <sys/types.h>
  36. #include <sys/wait.h>
  37. #include <unistd.h>
  38. #include <libgearman/gearman.h>
  39. #ifndef __INTEL_COMPILER
  40. #pragma GCC diagnostic ignored "-Wold-style-cast"
  41. #endif
  42. class GetPid : public util::Instance::Finish
  43. {
  44. private:
  45. pid_t _pid;
  46. public:
  47. GetPid() :
  48. _pid(-1)
  49. { }
  50. pid_t pid()
  51. {
  52. return _pid;
  53. }
  54. bool call(const bool success, const std::string &response)
  55. {
  56. _pid= -1;
  57. if (success and response.size())
  58. {
  59. _pid= atoi(response.c_str());
  60. }
  61. if (is_pid_valid(_pid) == false)
  62. {
  63. _pid= -1;
  64. return false;
  65. }
  66. return true;
  67. }
  68. };
  69. using namespace libtest;
  70. class Gearmand : public libtest::Server
  71. {
  72. private:
  73. public:
  74. Gearmand(const std::string& host_arg, in_port_t port_arg) :
  75. libtest::Server(host_arg, port_arg)
  76. {
  77. set_pid_file();
  78. }
  79. pid_t get_pid(bool error_is_ok)
  80. {
  81. if (pid_file().empty() == false)
  82. {
  83. Wait wait(pid_file(), 0);
  84. if (error_is_ok and not wait.successful())
  85. {
  86. Error << "Pidfile was not found:" << pid_file();
  87. return -1;
  88. }
  89. }
  90. GetPid *get_instance_pid;
  91. util::Instance instance(hostname(), port());
  92. instance.set_finish(get_instance_pid= new GetPid);
  93. instance.push(new util::Operation(test_literal_param("getpid\r\n"), true));
  94. if (error_is_ok and instance.run() == false)
  95. {
  96. Error << "Failed to obtain pid of server";
  97. }
  98. return get_instance_pid->pid();
  99. }
  100. bool ping()
  101. {
  102. gearman_client_st *client= gearman_client_create(NULL);
  103. if (client == NULL)
  104. {
  105. Error << "Could not allocate memory for gearman_client_create()";
  106. return false;
  107. }
  108. gearman_client_set_timeout(client, 3000);
  109. if (gearman_success(gearman_client_add_server(client, hostname().c_str(), port())))
  110. {
  111. gearman_return_t rc= gearman_client_echo(client, test_literal_param("This is my echo test"));
  112. if (gearman_success(rc))
  113. {
  114. gearman_client_free(client);
  115. return true;
  116. }
  117. #if 0
  118. Error << hostname().c_str() << ":" << port() << " was " << gearman_strerror(rc) << " extended: " << gearman_client_error(client);
  119. #endif
  120. }
  121. else
  122. {
  123. Error << "gearman_client_add_server() " << gearman_client_error(client);
  124. }
  125. gearman_client_free(client);
  126. return false;;
  127. }
  128. const char *name()
  129. {
  130. return "gearmand";
  131. };
  132. const char *executable()
  133. {
  134. return GEARMAND_BINARY;
  135. }
  136. const char *daemon_file_option()
  137. {
  138. return "--daemon";
  139. }
  140. void log_file_option(Application& app, const std::string& arg)
  141. {
  142. if (arg.empty() == false)
  143. {
  144. std::string buffer("--log-file=");
  145. buffer+= arg;
  146. app.add_option("--verbose=DEBUG");
  147. app.add_option(buffer);
  148. }
  149. }
  150. bool has_log_file_option() const
  151. {
  152. return true;
  153. }
  154. bool is_libtool()
  155. {
  156. return true;
  157. }
  158. bool has_syslog() const
  159. {
  160. return true;
  161. }
  162. bool has_port_option() const
  163. {
  164. return true;
  165. }
  166. bool build(size_t argc, const char *argv[]);
  167. };
  168. bool Gearmand::build(size_t argc, const char *argv[])
  169. {
  170. std::stringstream arg_buffer;
  171. if (getuid() == 0 or geteuid() == 0)
  172. {
  173. add_option("-u", "root");
  174. }
  175. add_option("--listen=localhost");
  176. for (size_t x= 0 ; x < argc ; x++)
  177. {
  178. add_option(argv[x]);
  179. }
  180. return true;
  181. }
  182. namespace libtest {
  183. libtest::Server *build_gearmand(const char *hostname, in_port_t try_port)
  184. {
  185. return new Gearmand(hostname, try_port);
  186. }
  187. }