drizzled.cc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 <config.h>
  37. #include <libtest/common.h>
  38. #include <libtest/drizzled.h>
  39. #include "util/instance.hpp"
  40. #include "util/operation.hpp"
  41. using namespace datadifferential;
  42. using namespace libtest;
  43. #include <cassert>
  44. #include <cerrno>
  45. #include <cstdio>
  46. #include <cstdlib>
  47. #include <cstring>
  48. #include <iostream>
  49. #include <signal.h>
  50. #include <sstream>
  51. #include <sys/types.h>
  52. #include <sys/wait.h>
  53. #include <unistd.h>
  54. #ifndef __INTEL_COMPILER
  55. #pragma GCC diagnostic ignored "-Wold-style-cast"
  56. #endif
  57. #if defined(HAVE_LIBDRIZZLE) && HAVE_LIBDRIZZLE
  58. #include <libdrizzle-1.0/drizzle_client.h>
  59. #endif
  60. using namespace libtest;
  61. namespace libtest {
  62. bool ping_drizzled(const in_port_t _port)
  63. {
  64. #if defined(HAVE_LIBDRIZZLE) && HAVE_LIBDRIZZLE
  65. {
  66. drizzle_st *drizzle= drizzle_create(NULL);
  67. if (drizzle == NULL)
  68. {
  69. return false;
  70. }
  71. drizzle_con_st *con;
  72. if ((con= drizzle_con_create(drizzle, NULL)) == NULL)
  73. {
  74. drizzle_free(drizzle);
  75. return false;
  76. }
  77. drizzle_con_set_tcp(con, "localhost", _port);
  78. drizzle_con_set_auth(con, "root", 0);
  79. bool success= false;
  80. drizzle_return_t rc;
  81. if ((rc= drizzle_con_connect(con)) == DRIZZLE_RETURN_OK)
  82. {
  83. drizzle_result_st *result= drizzle_ping(con, NULL, &rc);
  84. success= bool(result);
  85. drizzle_result_free(result);
  86. }
  87. if (success == true)
  88. { }
  89. else if (rc != DRIZZLE_RETURN_OK)
  90. {
  91. Error << drizzle_error(drizzle) << " localhost:" << _port;
  92. }
  93. drizzle_con_free(con);
  94. drizzle_free(drizzle);
  95. return success;
  96. }
  97. #endif
  98. return false;
  99. }
  100. } // namespace libtest
  101. class Drizzle : public libtest::Server
  102. {
  103. private:
  104. public:
  105. Drizzle(const std::string& host_arg, in_port_t port_arg) :
  106. libtest::Server(host_arg, port_arg, DRIZZLED_BINARY, false)
  107. {
  108. set_pid_file();
  109. }
  110. bool ping()
  111. {
  112. size_t limit= 5;
  113. while (_app.check() and --limit)
  114. {
  115. if (ping_drizzled(_port))
  116. {
  117. return true;
  118. }
  119. libtest::dream(1, 0);
  120. }
  121. return false;
  122. }
  123. const char *name()
  124. {
  125. return "drizzled";
  126. };
  127. void log_file_option(Application& app, const std::string& arg)
  128. {
  129. }
  130. bool has_log_file_option() const
  131. {
  132. return true;
  133. }
  134. bool broken_pid_file()
  135. {
  136. return true;
  137. }
  138. bool is_libtool()
  139. {
  140. return false;
  141. }
  142. bool has_syslog() const
  143. {
  144. return true;
  145. }
  146. bool has_port_option() const
  147. {
  148. return true;
  149. }
  150. void port_option(Application& app, in_port_t arg)
  151. {
  152. if (arg > 0)
  153. {
  154. char buffer[1024];
  155. snprintf(buffer, sizeof(buffer), "--drizzle-protocol.port=%d", int(arg));
  156. app.add_option(buffer);
  157. }
  158. }
  159. bool build(size_t argc, const char *argv[]);
  160. };
  161. bool Drizzle::build(size_t argc, const char *argv[])
  162. {
  163. if (getuid() == 0 or geteuid() == 0)
  164. {
  165. add_option("--user=root");
  166. }
  167. add_option("--verbose=INSPECT");
  168. #if 0
  169. add_option("--datadir=var/drizzle");
  170. #endif
  171. for (size_t x= 0 ; x < argc ; x++)
  172. {
  173. add_option(argv[x]);
  174. }
  175. return true;
  176. }
  177. namespace libtest {
  178. libtest::Server *build_drizzled(const char *hostname, in_port_t try_port)
  179. {
  180. return new Drizzle(hostname, try_port);
  181. }
  182. }