gearmand.cc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. Error << hostname().c_str() << ":" << port() << " was " << gearman_strerror(rc) << " extended: " << gearman_client_error(client);
  118. }
  119. else
  120. {
  121. Error << "gearman_client_add_server() " << gearman_client_error(client);
  122. }
  123. gearman_client_free(client);
  124. return false;;
  125. }
  126. const char *name()
  127. {
  128. return "gearmand";
  129. };
  130. const char *executable()
  131. {
  132. return GEARMAND_BINARY;
  133. }
  134. const char *pid_file_option()
  135. {
  136. return "--pid-file=";
  137. }
  138. const char *daemon_file_option()
  139. {
  140. return "--daemon";
  141. }
  142. const char *log_file_option()
  143. {
  144. return "-vvvvvvvv --log-file=";
  145. }
  146. const char *port_option()
  147. {
  148. return "--port=";
  149. }
  150. bool is_libtool()
  151. {
  152. return true;
  153. }
  154. bool has_syslog() const
  155. {
  156. return true;
  157. }
  158. bool build(int argc, const char *argv[]);
  159. };
  160. bool Gearmand::build(int argc, const char *argv[])
  161. {
  162. std::stringstream arg_buffer;
  163. if (getuid() == 0 or geteuid() == 0)
  164. {
  165. arg_buffer << " -u root ";
  166. }
  167. arg_buffer << " --listen=localhost ";
  168. for (int x= 1 ; x < argc ; x++)
  169. {
  170. arg_buffer << " " << argv[x] << " ";
  171. }
  172. set_extra_args(arg_buffer.str());
  173. return true;
  174. }
  175. namespace libtest {
  176. libtest::Server *build_gearmand(const char *hostname, in_port_t try_port)
  177. {
  178. return new Gearmand(hostname, try_port);
  179. }
  180. }