Fix valgrind issues.
[m6w6/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 <libtest/common.h>
23
24 #include <cassert>
25 #include <cerrno>
26 #include <cstdlib>
27 #include <iostream>
28
29 #include <algorithm>
30 #include <functional>
31 #include <locale>
32
33 // trim from end
34 static inline std::string &rtrim(std::string &s)
35 {
36 s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
37 return s;
38 }
39
40 namespace libtest {
41
42 void server_startup_st::push_server(Server *arg)
43 {
44 servers.push_back(arg);
45
46 char port_str[NI_MAXSERV];
47 snprintf(port_str, sizeof(port_str), "%u", int(arg->port()));
48
49 std::string server_config_string;
50 if (arg->has_socket())
51 {
52 server_config_string+= "--socket=";
53 server_config_string+= '"';
54 server_config_string+= arg->socket();
55 server_config_string+= '"';
56 server_config_string+= " ";
57 }
58 else
59 {
60 server_config_string+= "--server=";
61 server_config_string+= arg->hostname();
62 server_config_string+= ":";
63 server_config_string+= port_str;
64 server_config_string+= " ";
65 }
66
67 server_list+= server_config_string;
68
69 }
70
71 Server* server_startup_st::pop_server()
72 {
73 Server *tmp= servers.back();
74 servers.pop_back();
75 return tmp;
76 }
77
78 bool server_startup_st::shutdown(uint32_t number_of_host)
79 {
80 if (servers.size() > number_of_host)
81 {
82 Server* tmp= servers[number_of_host];
83
84 if (tmp and tmp->has_pid() and not tmp->kill(tmp->pid()))
85 { }
86 else
87 {
88 return true;
89 }
90 }
91
92 return false;
93 }
94
95 void server_startup_st::shutdown_and_remove()
96 {
97 for (std::vector<Server *>::iterator iter= servers.begin(); iter != servers.end(); iter++)
98 {
99 delete *iter;
100 }
101 servers.clear();
102 }
103
104 void server_startup_st::shutdown()
105 {
106 for (std::vector<Server *>::iterator iter= servers.begin(); iter != servers.end(); iter++)
107 {
108 if ((*iter)->has_pid() and not (*iter)->kill((*iter)->pid()))
109 {
110 Error << "Unable to kill:" << *(*iter);
111 }
112 }
113 }
114
115 void server_startup_st::restart()
116 {
117 for (std::vector<Server *>::iterator iter= servers.begin(); iter != servers.end(); iter++)
118 {
119 (*iter)->start();
120 }
121 }
122
123 server_startup_st::~server_startup_st()
124 {
125 shutdown_and_remove();
126 }
127
128 bool server_startup_st::is_debug() const
129 {
130 return bool(getenv("LIBTEST_MANUAL_GDB"));
131 }
132
133 bool server_startup_st::is_valgrind() const
134 {
135 return bool(getenv("LIBTEST_MANUAL_VALGRIND"));
136 }
137
138 bool server_startup_st::is_helgrind() const
139 {
140 return bool(getenv("LIBTEST_MANUAL_HELGRIND"));
141 }
142
143
144 bool server_startup(server_startup_st& construct, const std::string& server_type, in_port_t try_port, int argc, const char *argv[])
145 {
146 Outn();
147 if (try_port <= 0)
148 {
149 libtest::fatal(LIBYATL_DEFAULT_PARAM, "was passed the invalid port number %d", int(try_port));
150 }
151
152 libtest::Server *server= NULL;
153 if (0)
154 { }
155 else if (server_type.compare("gearmand") == 0)
156 {
157 if (GEARMAND_BINARY)
158 {
159 if (HAVE_LIBGEARMAN)
160 {
161 server= build_gearmand("localhost", try_port);
162 }
163 }
164 }
165 else if (server_type.compare("blobslap_worker") == 0)
166 {
167 if (GEARMAND_BINARY)
168 {
169 if (GEARMAND_BLOBSLAP_WORKER)
170 {
171 if (HAVE_LIBGEARMAN)
172 {
173 server= build_blobslap_worker(try_port);
174 }
175 }
176 }
177 }
178 else if (server_type.compare("memcached-sasl") == 0)
179 {
180 if (MEMCACHED_SASL_BINARY)
181 {
182 if (HAVE_LIBMEMCACHED)
183 {
184 server= build_memcached_sasl("localhost", try_port, construct.username(), construct.password());
185 }
186 }
187 }
188 else if (server_type.compare("memcached") == 0)
189 {
190 if (MEMCACHED_BINARY)
191 {
192 if (HAVE_LIBMEMCACHED)
193 {
194 server= build_memcached("localhost", try_port);
195 }
196 }
197 }
198
199 if (server == NULL)
200 {
201 Error << "Failure occured while creating server: " << server_type;
202 return false;
203 }
204
205 /*
206 We will now cycle the server we have created.
207 */
208 if (server->cycle() == false)
209 {
210 Error << "Could not start up server " << *server;
211 delete server;
212 return false;
213 }
214
215 server->build(argc, argv);
216
217 if (construct.is_debug())
218 {
219 Out << "Pausing for startup, hit return when ready.";
220 std::string gdb_command= server->base_command();
221 std::string options;
222 #if 0
223 Out << "run " << server->args(options);
224 #endif
225 getchar();
226 }
227 else if (server->start() == false)
228 {
229 Error << "Failed to start " << *server;
230 delete server;
231 return false;
232 }
233 else
234 {
235 Out << "STARTING SERVER(pid:" << server->pid() << "): " << server->running();
236 }
237
238 construct.push_server(server);
239
240 Outn();
241
242 return true;
243 }
244
245 bool server_startup_st::start_socket_server(const std::string& server_type, const in_port_t try_port, int argc, const char *argv[])
246 {
247 (void)try_port;
248 Outn();
249
250 Server *server= NULL;
251 if (0)
252 { }
253 else if (server_type.compare("gearmand") == 0)
254 {
255 Error << "Socket files are not supported for gearmand yet";
256 }
257 else if (server_type.compare("memcached-sasl") == 0)
258 {
259 if (MEMCACHED_SASL_BINARY)
260 {
261 if (HAVE_LIBMEMCACHED)
262 {
263 server= build_memcached_sasl_socket("localhost", try_port, username(), password());
264 }
265 else
266 {
267 Error << "Libmemcached was not found";
268 }
269 }
270 else
271 {
272 Error << "No memcached binary is available";
273 }
274 }
275 else if (server_type.compare("memcached") == 0)
276 {
277 if (MEMCACHED_BINARY)
278 {
279 if (HAVE_LIBMEMCACHED)
280 {
281 server= build_memcached_socket("localhost", try_port);
282 }
283 else
284 {
285 Error << "Libmemcached was not found";
286 }
287 }
288 else
289 {
290 Error << "No memcached binary is available";
291 }
292 }
293 else
294 {
295 Error << "Failed to start " << server_type << ", no support was found to be compiled in for it.";
296 }
297
298 if (server == NULL)
299 {
300 Error << "Failure occured while creating server: " << server_type;
301 return false;
302 }
303
304 /*
305 We will now cycle the server we have created.
306 */
307 if (not server->cycle())
308 {
309 Error << "Could not start up server " << *server;
310 delete server;
311 return false;
312 }
313
314 server->build(argc, argv);
315
316 if (is_debug())
317 {
318 Out << "Pausing for startup, hit return when ready.";
319 std::string gdb_command= server->base_command();
320 std::string options;
321 #if 0
322 Out << "run " << server->args(options);
323 #endif
324 getchar();
325 }
326 else if (not server->start())
327 {
328 Error << "Failed to start " << *server;
329 delete server;
330 return false;
331 }
332 else
333 {
334 Out << "STARTING SERVER(pid:" << server->pid() << "): " << server->running();
335 }
336
337 push_server(server);
338
339 set_default_socket(server->socket().c_str());
340
341 Outn();
342
343 return true;
344 }
345
346 std::string server_startup_st::option_string() const
347 {
348 std::string temp= server_list;
349 rtrim(temp);
350 return temp;
351 }
352
353
354 } // namespace libtest