Merge in tests for SASL
[awesomized/libmemcached] / libtest / server.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
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 #include <libtest/server.h>
42 #include <libtest/stream.h>
43 #include <libtest/killpid.h>
44
45 extern "C" {
46 static bool exited_successfully(int status)
47 {
48 if (WEXITSTATUS(status) == 0)
49 {
50 return true;
51 }
52
53 return true;
54 }
55 }
56
57
58 namespace libtest {
59
60 std::ostream& operator<<(std::ostream& output, const Server &arg)
61 {
62 if (arg.is_socket())
63 {
64 output << arg.hostname();
65 }
66 else
67 {
68 output << arg.hostname() << ":" << arg.port();
69 }
70
71 if (arg.has_pid())
72 {
73 output << " Pid:" << arg.pid();
74 }
75
76 if (arg.has_socket())
77 {
78 output << " Socket:" << arg.socket();
79 }
80
81 if (not arg.running().empty())
82 {
83 output << " Exec:" << arg.running();
84 }
85
86
87 return output; // for multiple << operators
88 }
89
90 void Server::nap(void)
91 {
92 #ifdef WIN32
93 sleep(1);
94 #else
95 struct timespec global_sleep_value= { 0, 50000 };
96 nanosleep(&global_sleep_value, NULL);
97 #endif
98 }
99
100 Server::Server(const std::string& host_arg, const in_port_t port_arg, bool is_socket_arg) :
101 _is_socket(is_socket_arg),
102 _pid(-1),
103 _port(port_arg),
104 _hostname(host_arg)
105 {
106 }
107
108 Server::~Server()
109 {
110 if (has_pid() and not kill(_pid))
111 {
112 Error << "Unable to kill:" << *this;
113 }
114 }
115
116 // If the server exists, kill it
117 bool Server::cycle()
118 {
119 uint32_t limit= 3;
120
121 // Try to ping, and kill the server #limit number of times
122 pid_t current_pid;
123 while (--limit and is_pid_valid(current_pid= get_pid()))
124 {
125 if (kill(current_pid))
126 {
127 Log << "Killed existing server," << *this << " with pid:" << current_pid;
128 nap();
129 continue;
130 }
131 }
132
133 // For whatever reason we could not kill it, and we reached limit
134 if (limit == 0)
135 {
136 Error << "Reached limit, could not kill server pid:" << current_pid;
137 return false;
138 }
139
140 return true;
141 }
142
143 // Grab a one off command
144 bool Server::command(std::string& command_arg)
145 {
146 rebuild_base_command();
147
148 command_arg+= _base_command;
149
150 if (args(command_arg))
151 {
152 return true;
153 }
154
155 return false;
156 }
157
158 bool Server::start()
159 {
160 // If we find that we already have a pid then kill it.
161 if (has_pid() and not kill(_pid))
162 {
163 Error << "Could not kill() existing server during start() pid:" << _pid;
164 return false;
165 }
166 assert(not has_pid());
167
168 _running.clear();
169 if (not command(_running))
170 {
171 Error << "Could not build command()";
172 return false;
173 }
174
175 if (is_valgrind() or is_helgrind())
176 {
177 _running+= " &";
178 }
179
180 int ret= system(_running.c_str());
181 if (not exited_successfully(ret))
182 {
183 Error << "system() failed:" << strerror(errno);
184 _running.clear();
185 return false;
186 }
187
188 if (is_helgrind() or is_valgrind())
189 {
190 sleep(4);
191 }
192
193 if (pid_file_option() and not pid_file().empty())
194 {
195 Wait wait(pid_file(), 8);
196
197 if (not wait.successful())
198 {
199 Error << "Unable to open pidfile for: " << _running;
200 }
201 }
202
203 int count= is_helgrind() or is_valgrind() ? 20 : 5;
204 while (not ping() and --count)
205 {
206 nap();
207 }
208
209 if (count == 0)
210 {
211 // If we happen to have a pid file, lets try to kill it
212 if (pid_file_option() and not pid_file().empty())
213 {
214 kill_file(pid_file());
215 }
216 Error << "Failed to ping() server started with:" << _running;
217 _running.clear();
218 return false;
219 }
220
221 // A failing get_pid() at this point is considered an error
222 _pid= get_pid(true);
223
224 return has_pid();
225 }
226
227 void Server::reset_pid()
228 {
229 _running.clear();
230 _pid_file.clear();
231 _pid= -1;
232 }
233
234 pid_t Server::pid()
235 {
236 return _pid;
237 }
238
239 bool Server::set_socket_file()
240 {
241 char file_buffer[FILENAME_MAX];
242 file_buffer[0]= 0;
243
244 if (broken_pid_file())
245 {
246 snprintf(file_buffer, sizeof(file_buffer), "/tmp/%s.socketXXXXXX", name());
247 }
248 else
249 {
250 snprintf(file_buffer, sizeof(file_buffer), "var/run/%s.socketXXXXXX", name());
251 }
252
253 int fd;
254 if ((fd= mkstemp(file_buffer)) == -1)
255 {
256 perror(file_buffer);
257 return false;
258 }
259 close(fd);
260 unlink(file_buffer);
261
262 _socket= file_buffer;
263
264 return true;
265 }
266
267 bool Server::set_pid_file()
268 {
269 char file_buffer[FILENAME_MAX];
270 file_buffer[0]= 0;
271
272 if (broken_pid_file())
273 {
274 snprintf(file_buffer, sizeof(file_buffer), "/tmp/%s.pidXXXXXX", name());
275 }
276 else
277 {
278 snprintf(file_buffer, sizeof(file_buffer), "var/run/%s.pidXXXXXX", name());
279 }
280
281 int fd;
282 if ((fd= mkstemp(file_buffer)) == -1)
283 {
284 perror(file_buffer);
285 return false;
286 }
287 close(fd);
288 unlink(file_buffer);
289
290 _pid_file= file_buffer;
291
292 return true;
293 }
294
295 bool Server::set_log_file()
296 {
297 char file_buffer[FILENAME_MAX];
298 file_buffer[0]= 0;
299
300 snprintf(file_buffer, sizeof(file_buffer), "var/log/%s.logXXXXXX", name());
301 int fd;
302 if ((fd= mkstemp(file_buffer)) == -1)
303 {
304 perror(file_buffer);
305 return false;
306 }
307 close(fd);
308
309 _log_file= file_buffer;
310
311 return true;
312 }
313
314 void Server::rebuild_base_command()
315 {
316 _base_command.clear();
317 if (is_libtool())
318 {
319 _base_command+= libtool();
320 }
321
322 if (is_debug() and getenv("GDB_COMMAND"))
323 {
324 _base_command+= getenv("GDB_COMMAND");
325 _base_command+= " ";
326 }
327 else if (is_valgrind() and getenv("VALGRIND_COMMAND"))
328 {
329 _base_command+= getenv("VALGRIND_COMMAND");
330 _base_command+= " ";
331 }
332 else if (is_helgrind() and getenv("HELGRIND_COMMAND"))
333 {
334 _base_command+= getenv("HELGRIND_COMMAND");
335 _base_command+= " ";
336 }
337
338 _base_command+= executable();
339 }
340
341 void Server::set_extra_args(const std::string &arg)
342 {
343 _extra_args= arg;
344 }
345
346 bool Server::args(std::string& options)
347 {
348 std::stringstream arg_buffer;
349
350 // Set a log file if it was requested (and we can)
351 if (getenv("LIBTEST_LOG") and log_file_option())
352 {
353 if (not set_log_file())
354 {
355 return false;
356 }
357
358 arg_buffer << " " << log_file_option() << _log_file;
359 }
360
361 // Update pid_file
362 if (pid_file_option())
363 {
364 if (_pid_file.empty() and not set_pid_file())
365 {
366 return false;
367 }
368
369 arg_buffer << " " << pid_file_option() << pid_file();
370 }
371
372 assert(daemon_file_option());
373 if (daemon_file_option() and not is_valgrind() and not is_helgrind())
374 {
375 arg_buffer << " " << daemon_file_option();
376 }
377
378 if (_is_socket and socket_file_option())
379 {
380 if (not set_socket_file())
381 {
382 return false;
383 }
384
385 arg_buffer << " " << socket_file_option() << "\"" << _socket << "\"";
386 }
387
388 assert(port_option());
389 if (port_option() and _port > 0)
390 {
391 arg_buffer << " " << port_option() << _port;
392 }
393
394 options+= arg_buffer.str();
395
396 if (not _extra_args.empty())
397 {
398 options+= _extra_args;
399 }
400
401 return true;
402 }
403
404 bool Server::is_debug() const
405 {
406 return bool(getenv("LIBTEST_MANUAL_GDB"));
407 }
408
409 bool Server::is_valgrind() const
410 {
411 return bool(getenv("LIBTEST_MANUAL_VALGRIND"));
412 }
413
414 bool Server::is_helgrind() const
415 {
416 return bool(getenv("LIBTEST_MANUAL_HELGRIND"));
417 }
418
419 bool Server::kill(pid_t pid_arg)
420 {
421 if (check_pid(pid_arg) and kill_pid(pid_arg)) // If we kill it, reset
422 {
423 if (broken_pid_file() and not pid_file().empty())
424 {
425 unlink(pid_file().c_str());
426 }
427
428 if (broken_socket_cleanup() and has_socket() and not socket().empty())
429 {
430 unlink(socket().c_str());
431 }
432
433 reset_pid();
434
435 return true;
436 }
437
438 return false;
439 }
440
441 } // namespace libtest