gearmand.cc 4.8 KB

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