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