drizzled.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 "libtest/yatlcon.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-5.1/drizzle_client.h>
  59. #endif
  60. using namespace libtest;
  61. namespace libtest {
  62. bool ping_drizzled(const in_port_t _port)
  63. {
  64. (void)(_port);
  65. #if defined(HAVE_LIBDRIZZLE) && HAVE_LIBDRIZZLE
  66. if (HAVE_LIBDRIZZLE)
  67. {
  68. drizzle_st *drizzle= drizzle_create(getenv("MYSQL_SERVER"),
  69. getenv("MYSQL_PORT") ? atoi("MYSQL_PORT") : DRIZZLE_DEFAULT_TCP_PORT,
  70. getenv("MYSQL_USER"),
  71. getenv("MYSQL_PASSWORD"),
  72. getenv("MYSQL_SCHEMA"), 0);
  73. if (drizzle == NULL)
  74. {
  75. return false;
  76. }
  77. bool success= false;
  78. drizzle_return_t rc;
  79. if ((rc= drizzle_connect(drizzle)) == DRIZZLE_RETURN_OK)
  80. {
  81. drizzle_result_st *result= drizzle_ping(drizzle, &rc);
  82. success= bool(result);
  83. drizzle_result_free(result);
  84. }
  85. if (success == true)
  86. { }
  87. else if (rc != DRIZZLE_RETURN_OK)
  88. {
  89. Error << drizzle_error(drizzle) << " localhost:" << _port;
  90. }
  91. drizzle_quit(drizzle);
  92. return success;
  93. }
  94. #endif
  95. return false;
  96. }
  97. } // namespace libtest
  98. class Drizzle : public libtest::Server
  99. {
  100. private:
  101. public:
  102. Drizzle(const std::string& host_arg, in_port_t port_arg) :
  103. libtest::Server(host_arg, port_arg, DRIZZLED_BINARY, false)
  104. {
  105. set_pid_file();
  106. }
  107. bool ping()
  108. {
  109. size_t limit= 5;
  110. while (_app.check() and --limit)
  111. {
  112. if (ping_drizzled(_port))
  113. {
  114. return true;
  115. }
  116. libtest::dream(1, 0);
  117. }
  118. return false;
  119. }
  120. const char *name()
  121. {
  122. return "drizzled";
  123. };
  124. void log_file_option(Application&, const std::string&)
  125. {
  126. }
  127. bool has_log_file_option() const
  128. {
  129. return true;
  130. }
  131. bool broken_pid_file()
  132. {
  133. return true;
  134. }
  135. bool is_libtool()
  136. {
  137. return false;
  138. }
  139. bool has_syslog() const
  140. {
  141. return true;
  142. }
  143. bool is_valgrind() const
  144. {
  145. return false;
  146. }
  147. bool has_port_option() const
  148. {
  149. return true;
  150. }
  151. void port_option(Application& app, in_port_t arg)
  152. {
  153. if (arg > 0)
  154. {
  155. libtest::vchar_t buffer;
  156. buffer.resize(1024);
  157. snprintf(&buffer[1024], buffer.size(), "--drizzle-protocol.port=%d", int(arg));
  158. /* The buffer is reading as 0 length but it was resized above, there's
  159. probably a way to define buffer so this does't happen but for now
  160. we'll disable the warning. */
  161. #pragma GCC diagnostic push
  162. #ifdef __GNUC__
  163. #if __GNUC__ > 10
  164. #pragma GCC diagnostic ignored "-Wstringop-overread"
  165. #endif
  166. #endif
  167. app.add_option(&buffer[1024]);
  168. #pragma GCC diagnostic pop
  169. }
  170. }
  171. bool build();
  172. };
  173. bool Drizzle::build()
  174. {
  175. if (getuid() == 0 or geteuid() == 0)
  176. {
  177. add_option("--user=root");
  178. }
  179. add_option("--verbose=INSPECT");
  180. #if 0
  181. add_option("--datadir=var/drizzle");
  182. #endif
  183. return true;
  184. }
  185. namespace libtest {
  186. libtest::Server *build_drizzled(const char *hostname, in_port_t try_port)
  187. {
  188. return new Drizzle(hostname, try_port);
  189. }
  190. }