Merge of cppcheck, assert fix.
[awesomized/libmemcached] / libtest / server_container.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 <cassert>
41 #include <cerrno>
42 #include <cstdlib>
43 #include <iostream>
44
45 #include <algorithm>
46 #include <functional>
47 #include <locale>
48
49 // trim from end
50 static inline std::string &rtrim(std::string &s)
51 {
52 s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
53 return s;
54 }
55
56 namespace libtest {
57
58 Server* server_startup_st::last()
59 {
60 return servers.back();
61 }
62
63 void server_startup_st::push_server(Server *arg)
64 {
65 servers.push_back(arg);
66
67 std::string server_config_string;
68 if (arg->has_socket())
69 {
70 server_config_string+= "--socket=";
71 server_config_string+= '"';
72 server_config_string+= arg->socket();
73 server_config_string+= '"';
74 server_config_string+= " ";
75 }
76 else
77 {
78 char port_str[NI_MAXSERV]= { 0 };
79 snprintf(port_str, sizeof(port_str), "%u", int(arg->port()));
80
81 server_config_string+= "--server=";
82 server_config_string+= arg->hostname();
83 server_config_string+= ":";
84 server_config_string+= port_str;
85 server_config_string+= " ";
86 }
87
88 server_list+= server_config_string;
89
90 }
91
92 Server* server_startup_st::pop_server()
93 {
94 Server *tmp= servers.back();
95 servers.pop_back();
96 return tmp;
97 }
98
99 // host_to_shutdown => host number to shutdown in array
100 bool server_startup_st::shutdown(uint32_t host_to_shutdown)
101 {
102 if (servers.size() > host_to_shutdown)
103 {
104 Server* tmp= servers[host_to_shutdown];
105
106 if (tmp and tmp->kill() == false)
107 { }
108 else
109 {
110 return true;
111 }
112 }
113
114 return false;
115 }
116
117 void server_startup_st::clear()
118 {
119 for (std::vector<Server *>::iterator iter= servers.begin(); iter != servers.end(); ++iter)
120 {
121 delete *iter;
122 }
123 servers.clear();
124 }
125
126 bool server_startup_st::check() const
127 {
128 bool success= true;
129 for (std::vector<Server *>::const_iterator iter= servers.begin(); iter != servers.end(); ++iter)
130 {
131 if ((*iter)->check() == false)
132 {
133 success= false;
134 }
135 }
136
137 return success;
138 }
139
140 bool server_startup_st::shutdown()
141 {
142 bool success= true;
143 for (std::vector<Server *>::iterator iter= servers.begin(); iter != servers.end(); ++iter)
144 {
145 if ((*iter)->has_pid() and (*iter)->kill() == false)
146 {
147 Error << "Unable to kill:" << *(*iter);
148 success= false;
149 }
150 }
151
152 return success;
153 }
154
155 void server_startup_st::restart()
156 {
157 for (std::vector<Server *>::iterator iter= servers.begin(); iter != servers.end(); ++iter)
158 {
159 (*iter)->start();
160 }
161 }
162
163 #define MAGIC_MEMORY 123575
164 server_startup_st::server_startup_st() :
165 _magic(MAGIC_MEMORY),
166 _socket(false),
167 _sasl(false),
168 _count(0),
169 udp(0),
170 _servers_to_run(5)
171 { }
172
173 server_startup_st::~server_startup_st()
174 {
175 clear();
176 }
177
178 bool server_startup_st::validate()
179 {
180 return _magic == MAGIC_MEMORY;
181 }
182
183 bool server_startup(server_startup_st& construct, const std::string& server_type, in_port_t try_port, int argc, const char *argv[], const bool opt_startup_message)
184 {
185 construct.start_server(server_type, try_port, argc, argv, opt_startup_message);
186 }
187
188 bool server_startup_st::start_server(const std::string& server_type, in_port_t try_port, int argc, const char *argv[], const bool opt_startup_message)
189 {
190 if (try_port <= 0)
191 {
192 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "was passed the invalid port number %d", int(try_port));
193 }
194
195 libtest::Server *server= NULL;
196 try {
197 if (0)
198 { }
199 else if (server_type.compare("gearmand") == 0)
200 {
201 if (GEARMAND_BINARY)
202 {
203 if (HAVE_LIBGEARMAN)
204 {
205 server= build_gearmand("localhost", try_port);
206 }
207 }
208 }
209 else if (server_type.compare("hostile-gearmand") == 0)
210 {
211 if (GEARMAND_BINARY)
212 {
213 if (HAVE_LIBGEARMAN)
214 {
215 server= build_gearmand("localhost", try_port, "gearmand/hostile_gearmand");
216 }
217 }
218 }
219 else if (server_type.compare("drizzled") == 0)
220 {
221 if (DRIZZLED_BINARY)
222 {
223 if (HAVE_LIBDRIZZLE)
224 {
225 server= build_drizzled("localhost", try_port);
226 }
227 }
228 }
229 else if (server_type.compare("blobslap_worker") == 0)
230 {
231 if (GEARMAND_BINARY)
232 {
233 if (GEARMAND_BLOBSLAP_WORKER)
234 {
235 if (HAVE_LIBGEARMAN)
236 {
237 server= build_blobslap_worker(try_port);
238 }
239 }
240 }
241 }
242 else if (server_type.compare("memcached-sasl") == 0)
243 {
244 if (MEMCACHED_SASL_BINARY)
245 {
246 if (HAVE_LIBMEMCACHED)
247 {
248 server= build_memcached_sasl("localhost", try_port, username(), password());
249 }
250 }
251 }
252 else if (server_type.compare("memcached") == 0)
253 {
254 if (HAVE_MEMCACHED_BINARY)
255 {
256 if (HAVE_LIBMEMCACHED)
257 {
258 server= build_memcached("localhost", try_port);
259 }
260 }
261 }
262 else if (server_type.compare("memcached-light") == 0)
263 {
264 if (MEMCACHED_LIGHT_BINARY)
265 {
266 if (HAVE_LIBMEMCACHED)
267 {
268 server= build_memcached_light("localhost", try_port);
269 }
270 }
271 }
272
273 if (server == NULL)
274 {
275 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "Launching of an unknown server was attempted: %s", server_type.c_str());
276 }
277
278 /*
279 We will now cycle the server we have created.
280 */
281 if (server->cycle() == false)
282 {
283 Error << "Could not start up server " << *server;
284 delete server;
285 return false;
286 }
287
288 server->build(argc, argv);
289
290 #if 0
291 if (false)
292 {
293 Out << "Pausing for startup, hit return when ready.";
294 std::string gdb_command= server->base_command();
295 getchar();
296 }
297 else
298 #endif
299 if (server->start() == false)
300 {
301 delete server;
302 return false;
303 }
304 else
305 {
306 if (opt_startup_message)
307 {
308 Outn();
309 Out << "STARTING SERVER(pid:" << server->pid() << "): " << server->running();
310 Outn();
311 }
312 }
313 }
314 catch (...)
315 {
316 delete server;
317 throw;
318 }
319
320 push_server(server);
321
322 return true;
323 }
324
325 bool server_startup_st::start_socket_server(const std::string& server_type, const in_port_t try_port, int argc,
326 const char *argv[],
327 const bool opt_startup_message)
328 {
329 (void)try_port;
330 Outn();
331
332 Server *server= NULL;
333 try {
334 if (0)
335 { }
336 else if (server_type.compare("gearmand") == 0)
337 {
338 Error << "Socket files are not supported for gearmand yet";
339 }
340 else if (server_type.compare("memcached-sasl") == 0)
341 {
342 if (MEMCACHED_SASL_BINARY)
343 {
344 if (HAVE_LIBMEMCACHED)
345 {
346 server= build_memcached_sasl_socket("localhost", try_port, username(), password());
347 }
348 else
349 {
350 Error << "Libmemcached was not found";
351 }
352 }
353 else
354 {
355 Error << "No memcached binary is available";
356 }
357 }
358 else if (server_type.compare("memcached") == 0)
359 {
360 if (MEMCACHED_BINARY)
361 {
362 if (HAVE_LIBMEMCACHED)
363 {
364 server= build_memcached_socket("localhost", try_port);
365 }
366 else
367 {
368 Error << "Libmemcached was not found";
369 }
370 }
371 else
372 {
373 Error << "No memcached binary is available";
374 }
375 }
376 else
377 {
378 Error << "Failed to start " << server_type << ", no support was found to be compiled in for it.";
379 }
380
381 if (server == NULL)
382 {
383 Error << "Failure occured while creating server: " << server_type;
384 return false;
385 }
386
387 /*
388 We will now cycle the server we have created.
389 */
390 if (server->cycle() == false)
391 {
392 Error << "Could not start up server " << *server;
393 delete server;
394 return false;
395 }
396
397 server->build(argc, argv);
398
399 #if 0
400 if (false)
401 {
402 Out << "Pausing for startup, hit return when ready.";
403 std::string gdb_command= server->base_command();
404 std::string options;
405 Out << "run " << server->args(options);
406 getchar();
407 }
408 else
409 #endif
410 if (server->start() == false)
411 {
412 Error << "Failed to start " << *server;
413 delete server;
414 return false;
415 }
416 else
417 {
418 if (opt_startup_message)
419 {
420 Outn();
421 Out << "STARTING SERVER(pid:" << server->pid() << "): " << server->running();
422 Outn();
423 }
424 }
425 }
426 catch (...)
427 {
428 delete server;
429 throw;
430 }
431
432 push_server(server);
433
434 set_default_socket(server->socket().c_str());
435
436 Outn();
437
438 return true;
439 }
440
441 std::string server_startup_st::option_string() const
442 {
443 std::string temp= server_list;
444 rtrim(temp);
445 return temp;
446 }
447
448
449 } // namespace libtest