Improvements on valgrind detection.
[awesomized/libmemcached] / libtest / server_container.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * libtest
4 *
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 3 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include <config.h>
23 #include <libtest/common.h>
24
25 #include <cassert>
26 #include <cerrno>
27 #include <cstdlib>
28 #include <iostream>
29
30 #include <algorithm>
31 #include <functional>
32 #include <locale>
33
34 // trim from end
35 static inline std::string &rtrim(std::string &s)
36 {
37 s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
38 return s;
39 }
40
41 namespace libtest {
42
43 void server_startup_st::push_server(Server *arg)
44 {
45 servers.push_back(arg);
46
47 char port_str[NI_MAXSERV];
48 snprintf(port_str, sizeof(port_str), "%u", int(arg->port()));
49
50 std::string server_config_string;
51 if (arg->has_socket())
52 {
53 server_config_string+= "--socket=";
54 server_config_string+= '"';
55 server_config_string+= arg->socket();
56 server_config_string+= '"';
57 server_config_string+= " ";
58 }
59 else
60 {
61 server_config_string+= "--server=";
62 server_config_string+= arg->hostname();
63 server_config_string+= ":";
64 server_config_string+= port_str;
65 server_config_string+= " ";
66 }
67
68 server_list+= server_config_string;
69
70 }
71
72 Server* server_startup_st::pop_server()
73 {
74 Server *tmp= servers.back();
75 servers.pop_back();
76 return tmp;
77 }
78
79 bool server_startup_st::shutdown(uint32_t number_of_host)
80 {
81 if (servers.size() > number_of_host)
82 {
83 Server* tmp= servers[number_of_host];
84
85 if (tmp and tmp->has_pid() and not tmp->kill(tmp->pid()))
86 { }
87 else
88 {
89 return true;
90 }
91 }
92
93 return false;
94 }
95
96 void server_startup_st::shutdown_and_remove()
97 {
98 for (std::vector<Server *>::iterator iter= servers.begin(); iter != servers.end(); iter++)
99 {
100 delete *iter;
101 }
102 servers.clear();
103 }
104
105 void server_startup_st::shutdown()
106 {
107 for (std::vector<Server *>::iterator iter= servers.begin(); iter != servers.end(); iter++)
108 {
109 if ((*iter)->has_pid() and not (*iter)->kill((*iter)->pid()))
110 {
111 Error << "Unable to kill:" << *(*iter);
112 }
113 }
114 }
115
116 void server_startup_st::restart()
117 {
118 for (std::vector<Server *>::iterator iter= servers.begin(); iter != servers.end(); iter++)
119 {
120 (*iter)->start();
121 }
122 }
123
124 #define MAGIC_MEMORY 123575
125 server_startup_st::server_startup_st() :
126 _magic(MAGIC_MEMORY),
127 _socket(false),
128 _sasl(false),
129 _count(5),
130 udp(0)
131 { }
132
133 server_startup_st::~server_startup_st()
134 {
135 shutdown_and_remove();
136 }
137
138 bool server_startup_st::validate()
139 {
140 return _magic == MAGIC_MEMORY;
141 }
142
143
144 bool server_startup_st::is_debug() const
145 {
146 return bool(getenv("LIBTEST_MANUAL_GDB"));
147 }
148
149 bool server_startup_st::is_valgrind() const
150 {
151 return bool(getenv("LIBTEST_MANUAL_VALGRIND"));
152 }
153
154 bool server_startup_st::is_helgrind() const
155 {
156 return bool(getenv("LIBTEST_MANUAL_HELGRIND"));
157 }
158
159
160 bool server_startup(server_startup_st& construct, const std::string& server_type, in_port_t try_port, int argc, const char *argv[])
161 {
162 Outn();
163 if (try_port <= 0)
164 {
165 libtest::fatal(LIBYATL_DEFAULT_PARAM, "was passed the invalid port number %d", int(try_port));
166 }
167
168 libtest::Server *server= NULL;
169 if (0)
170 { }
171 else if (server_type.compare("gearmand") == 0)
172 {
173 if (GEARMAND_BINARY)
174 {
175 if (HAVE_LIBGEARMAN)
176 {
177 server= build_gearmand("localhost", try_port);
178 }
179 }
180 }
181 else if (server_type.compare("blobslap_worker") == 0)
182 {
183 if (GEARMAND_BINARY)
184 {
185 if (GEARMAND_BLOBSLAP_WORKER)
186 {
187 if (HAVE_LIBGEARMAN)
188 {
189 server= build_blobslap_worker(try_port);
190 }
191 }
192 }
193 }
194 else if (server_type.compare("memcached-sasl") == 0)
195 {
196 if (MEMCACHED_SASL_BINARY)
197 {
198 if (HAVE_LIBMEMCACHED)
199 {
200 server= build_memcached_sasl("localhost", try_port, construct.username(), construct.password());
201 }
202 }
203 }
204 else if (server_type.compare("memcached") == 0)
205 {
206 if (MEMCACHED_BINARY)
207 {
208 if (HAVE_LIBMEMCACHED)
209 {
210 server= build_memcached("localhost", try_port);
211 }
212 }
213 }
214 else if (server_type.compare("memcached-light") == 0)
215 {
216 if (MEMCACHED_LIGHT_BINARY)
217 {
218 if (HAVE_LIBMEMCACHED)
219 {
220 server= build_memcached_light("localhost", try_port);
221 }
222 }
223 }
224
225 if (server == NULL)
226 {
227 fatal_message("Launching of an unknown server was attempted");
228 }
229
230 /*
231 We will now cycle the server we have created.
232 */
233 if (server->cycle() == false)
234 {
235 Error << "Could not start up server " << *server;
236 delete server;
237 return false;
238 }
239
240 server->build(argc, argv);
241
242 if (construct.is_debug())
243 {
244 Out << "Pausing for startup, hit return when ready.";
245 std::string gdb_command= server->base_command();
246 std::string options;
247 #if 0
248 Out << "run " << server->args(options);
249 #endif
250 getchar();
251 }
252 else if (server->start() == false)
253 {
254 delete server;
255 return false;
256 }
257 else
258 {
259 Out << "STARTING SERVER(pid:" << server->pid() << "): " << server->running();
260 }
261
262 construct.push_server(server);
263
264 Outn();
265
266 return true;
267 }
268
269 bool server_startup_st::start_socket_server(const std::string& server_type, const in_port_t try_port, int argc, const char *argv[])
270 {
271 (void)try_port;
272 Outn();
273
274 Server *server= NULL;
275 if (0)
276 { }
277 else if (server_type.compare("gearmand") == 0)
278 {
279 Error << "Socket files are not supported for gearmand yet";
280 }
281 else if (server_type.compare("memcached-sasl") == 0)
282 {
283 if (MEMCACHED_SASL_BINARY)
284 {
285 if (HAVE_LIBMEMCACHED)
286 {
287 server= build_memcached_sasl_socket("localhost", try_port, username(), password());
288 }
289 else
290 {
291 Error << "Libmemcached was not found";
292 }
293 }
294 else
295 {
296 Error << "No memcached binary is available";
297 }
298 }
299 else if (server_type.compare("memcached") == 0)
300 {
301 if (MEMCACHED_BINARY)
302 {
303 if (HAVE_LIBMEMCACHED)
304 {
305 server= build_memcached_socket("localhost", try_port);
306 }
307 else
308 {
309 Error << "Libmemcached was not found";
310 }
311 }
312 else
313 {
314 Error << "No memcached binary is available";
315 }
316 }
317 else
318 {
319 Error << "Failed to start " << server_type << ", no support was found to be compiled in for it.";
320 }
321
322 if (server == NULL)
323 {
324 Error << "Failure occured while creating server: " << server_type;
325 return false;
326 }
327
328 /*
329 We will now cycle the server we have created.
330 */
331 if (not server->cycle())
332 {
333 Error << "Could not start up server " << *server;
334 delete server;
335 return false;
336 }
337
338 server->build(argc, argv);
339
340 if (is_debug())
341 {
342 Out << "Pausing for startup, hit return when ready.";
343 std::string gdb_command= server->base_command();
344 std::string options;
345 #if 0
346 Out << "run " << server->args(options);
347 #endif
348 getchar();
349 }
350 else if (not server->start())
351 {
352 Error << "Failed to start " << *server;
353 delete server;
354 return false;
355 }
356 else
357 {
358 Out << "STARTING SERVER(pid:" << server->pid() << "): " << server->running();
359 }
360
361 push_server(server);
362
363 set_default_socket(server->socket().c_str());
364
365 Outn();
366
367 return true;
368 }
369
370 std::string server_startup_st::option_string() const
371 {
372 std::string temp= server_list;
373 rtrim(temp);
374 return temp;
375 }
376
377
378 } // namespace libtest