gearmand.cc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /* Gearman server and library
  2. * Copyright (C) 2011 Data Differential, http://datadifferential.com/
  3. * Copyright (C) 2008 Brian Aker, Eric Day
  4. * All rights reserved.
  5. *
  6. * Use and distribution licensed under the BSD license. See
  7. * the COPYING file in the parent directory for full text.
  8. */
  9. #include <libtest/common.h>
  10. #include "util/instance.h"
  11. #include "util/operation.h"
  12. using namespace gearman_util;
  13. #include <cassert>
  14. #include <cerrno>
  15. #include <cstdio>
  16. #include <cstdlib>
  17. #include <cstring>
  18. #include <iostream>
  19. #include <signal.h>
  20. #include <sys/types.h>
  21. #include <sys/wait.h>
  22. #include <unistd.h>
  23. #include <libtest/server.h>
  24. #include <libtest/wait.h>
  25. #include <libgearman/gearman.h>
  26. #ifndef __INTEL_COMPILER
  27. #pragma GCC diagnostic ignored "-Wold-style-cast"
  28. #endif
  29. class GetPid : public Instance::Finish
  30. {
  31. private:
  32. pid_t _pid;
  33. public:
  34. GetPid() :
  35. _pid(-1)
  36. { }
  37. pid_t pid()
  38. {
  39. return _pid;
  40. }
  41. bool call(const bool success, const std::string &response)
  42. {
  43. _pid= -1;
  44. if (success and response.size())
  45. {
  46. _pid= atoi(response.c_str());
  47. }
  48. if (_pid < 1)
  49. {
  50. _pid= -1;
  51. return true;
  52. }
  53. return false;
  54. }
  55. };
  56. using namespace libtest;
  57. class Gearmand : public Server
  58. {
  59. private:
  60. public:
  61. Gearmand(const std::string& host_arg, in_port_t port_arg) :
  62. Server(host_arg, port_arg)
  63. { }
  64. pid_t get_pid()
  65. {
  66. GetPid *getpid;
  67. Instance instance(hostname(), port());
  68. instance.set_finish(getpid= new GetPid);
  69. instance.push(new Operation(test_literal_param("getpid\r\n"), true));
  70. instance.run();
  71. _pid= getpid->pid();
  72. return _pid;
  73. }
  74. bool ping()
  75. {
  76. int limit= 5; // sleep cycles
  77. while (--limit)
  78. {
  79. gearman_client_st *client= gearman_client_create(NULL);
  80. if (not client)
  81. {
  82. Error << "Could not allocate memory for gearman_client_create()";
  83. return false;
  84. }
  85. if (gearman_success(gearman_client_add_server(client, hostname().c_str(), port())))
  86. {
  87. gearman_return_t rc= gearman_client_echo(client, gearman_literal_param("This is my echo test"));
  88. if (gearman_success(rc))
  89. {
  90. gearman_client_free(client);
  91. return true;
  92. }
  93. if (rc == GEARMAN_COULD_NOT_CONNECT)
  94. {
  95. sleep();
  96. }
  97. }
  98. gearman_client_free(client);
  99. }
  100. return false;;
  101. }
  102. const char *name()
  103. {
  104. return "gearmand";
  105. };
  106. const char *executable()
  107. {
  108. return "./gearmand/gearmand";
  109. }
  110. const char *pid_file_option()
  111. {
  112. return "--pid-file=";
  113. }
  114. const char *daemon_file_option()
  115. {
  116. return "--daemon";
  117. }
  118. const char *log_file_option()
  119. {
  120. return "--log_file=";
  121. }
  122. const char *port_option()
  123. {
  124. return "--port=";
  125. }
  126. bool is_libtool()
  127. {
  128. return true;
  129. }
  130. bool build(int argc, const char *argv[]);
  131. };
  132. #include <sstream>
  133. bool Gearmand::build(int argc, const char *argv[])
  134. {
  135. std::stringstream arg_buffer;
  136. if (getuid() == 0 or geteuid() == 0)
  137. {
  138. arg_buffer << " -u root ";
  139. }
  140. for (int x= 1 ; x < argc ; x++)
  141. {
  142. arg_buffer << " " << argv[x] << " ";
  143. }
  144. set_extra_args(arg_buffer.str());
  145. return true;
  146. }
  147. bool libtest::server_startup(server_startup_st& construct, in_port_t try_port, int argc, const char *argv[])
  148. {
  149. Logn();
  150. // Look to see if we are being provided ports to use
  151. {
  152. char variable_buffer[1024];
  153. snprintf(variable_buffer, sizeof(variable_buffer), "LIBTEST_PORT_%lu", (unsigned long)construct.count());
  154. char *var;
  155. if ((var= getenv(variable_buffer)))
  156. {
  157. in_port_t tmp= in_port_t(atoi(var));
  158. if (tmp > 0)
  159. try_port= tmp;
  160. }
  161. }
  162. Gearmand *server= new Gearmand("localhost", try_port);
  163. assert(server);
  164. if (server == NULL)
  165. {
  166. Error << "Failure calling new for Gearmand";
  167. return false;
  168. }
  169. /*
  170. We will now cycle the server we have created.
  171. */
  172. if (not server->cycle())
  173. {
  174. Error << "Could not start up server " << *server;
  175. delete server;
  176. return false;
  177. }
  178. server->build(argc, argv);
  179. if (construct.is_debug())
  180. {
  181. Log << "Pausing for startup, hit return when ready.";
  182. std::string gdb_command= server->base_command();
  183. std::string options;
  184. Log << "run " << server->args(options);
  185. getchar();
  186. }
  187. else if (not server->start())
  188. {
  189. Error << "Failed to start " << *server;
  190. delete server;
  191. return false;
  192. }
  193. else
  194. {
  195. Log << "STARTING SERVER(pid:" << server->pid() << "): " << server->running();
  196. }
  197. construct.push_server(server);
  198. if (default_port() == 0)
  199. {
  200. assert(server->has_port());
  201. set_default_port(server->port());
  202. }
  203. char port_str[NI_MAXSERV];
  204. snprintf(port_str, sizeof(port_str), "%u", int(server->port()));
  205. std::string server_config_string;
  206. server_config_string+= "--server=";
  207. server_config_string+= server->hostname();
  208. server_config_string+= ":";
  209. server_config_string+= port_str;
  210. server_config_string+= " ";
  211. construct.server_list+= server_config_string;
  212. Logn();
  213. srandom((unsigned int)time(NULL));
  214. return true;
  215. }