Merge in merge of libtest
[awesomized/libmemcached] / libtest / server.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Libtest library
4 *
5 * Copyright (C) 2011 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 <libtest/common.h>
38
39 #include <cassert>
40 #include <cerrno>
41 #include <cstdlib>
42 #include <iostream>
43
44 #include <algorithm>
45 #include <functional>
46 #include <locale>
47
48 // trim from end
49 static inline std::string &rtrim(std::string &s)
50 {
51 s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
52 return s;
53 }
54
55 #include <libtest/server.h>
56 #include <libtest/stream.h>
57 #include <libtest/killpid.h>
58
59 #ifdef HAVE_LIBGEARMAN
60 #include <libtest/gearmand.h>
61 #endif
62
63 #ifdef HAVE_LIBMEMCACHED
64 #include <libtest/memcached.h>
65 #endif
66
67 namespace libtest {
68
69 std::ostream& operator<<(std::ostream& output, const Server &arg)
70 {
71 if (arg.is_socket())
72 {
73 output << arg.hostname();
74 }
75 else
76 {
77 output << arg.hostname() << ":" << arg.port();
78 }
79
80 if (arg.has_pid())
81 {
82 output << " Pid:" << arg.pid();
83 }
84
85 if (arg.has_socket())
86 {
87 output << " Socket:" << arg.socket();
88 }
89
90 if (not arg.running().empty())
91 {
92 output << " Exec:" << arg.running();
93 }
94
95
96 return output; // for multiple << operators
97 }
98
99 void Server::nap(void)
100 {
101 #ifdef WIN32
102 sleep(1);
103 #else
104 struct timespec global_sleep_value= { 0, 50000 };
105 nanosleep(&global_sleep_value, NULL);
106 #endif
107 }
108
109 Server::Server(const std::string& host_arg, const in_port_t port_arg, bool is_socket_arg) :
110 _is_socket(is_socket_arg),
111 _pid(-1),
112 _port(port_arg),
113 _hostname(host_arg)
114 {
115 }
116
117 Server::~Server()
118 {
119 if (has_pid() and not kill(_pid))
120 {
121 Error << "Unable to kill:" << *this;
122 }
123 }
124
125 std::string server_startup_st::option_string() const
126 {
127 std::string temp= server_list;
128 rtrim(temp);
129 return temp;
130 }
131
132 // If the server exists, kill it
133 bool Server::cycle()
134 {
135 uint32_t limit= 3;
136
137 // Try to ping, and kill the server #limit number of times
138 pid_t current_pid;
139 while (--limit and (current_pid= get_pid()) != -1)
140 {
141 if (kill(current_pid))
142 {
143 Log << "Killed existing server," << *this << " with pid:" << current_pid;
144 nap();
145 continue;
146 }
147 }
148
149 // For whatever reason we could not kill it, and we reached limit
150 if (limit == 0)
151 {
152 Error << "Reached limit, could not kill server pid:" << current_pid;
153 return false;
154 }
155
156 return true;
157 }
158
159 // Grab a one off command
160 bool Server::command(std::string& command_arg)
161 {
162 rebuild_base_command();
163
164 command_arg+= _base_command;
165
166 if (args(command_arg))
167 {
168 return true;
169 }
170
171 return false;
172 }
173
174 bool Server::start()
175 {
176 // If we find that we already have a pid then kill it.
177 if (has_pid() and not kill(_pid))
178 {
179 Error << "Could not kill() existing server during start() pid:" << _pid;
180 return false;
181 }
182 assert(not has_pid());
183
184 _running.clear();
185 if (not command(_running))
186 {
187 Error << "Could not build command()";
188 return false;
189 }
190
191 if (system(_running.c_str()) == -1)
192 {
193 Error << "system() failed:" << strerror(errno);
194 _running.clear();
195 return false;
196 }
197
198 if (pid_file_option() and not pid_file().empty())
199 {
200 Wait wait(pid_file());
201
202 if (not wait.successful())
203 {
204 Error << "Unable to open pidfile: " << pid_file();
205 }
206 }
207
208 int count= 5;
209 while (not ping() and --count)
210 {
211 nap();
212 }
213
214 if (count == 0)
215 {
216 Error << "Failed to ping() server once started:" << *this;
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), "tests/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), "tests/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), "tests/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 --mode=execute ";
320 }
321
322 if (is_debug())
323 {
324 _base_command+= "gdb ";
325 }
326 else if (is_valgrind())
327 {
328 _base_command+= "valgrind --log-file=tests/var/tmp/valgrind.out --leak-check=full --show-reachable=yes ";
329 }
330
331 _base_command+= executable();
332 }
333
334 void Server::set_extra_args(const std::string &arg)
335 {
336 _extra_args= arg;
337 }
338
339 bool Server::args(std::string& options)
340 {
341 std::stringstream arg_buffer;
342
343 // Set a log file if it was requested (and we can)
344 if (getenv("LIBTEST_LOG") and log_file_option())
345 {
346 if (not set_log_file())
347 return false;
348
349 arg_buffer << " " << log_file_option() << _log_file;
350 }
351
352 // Update pid_file
353 if (pid_file_option())
354 {
355 if (not set_pid_file())
356 return false;
357
358 arg_buffer << " " << pid_file_option() << pid_file();
359 }
360
361 assert(daemon_file_option());
362 if (daemon_file_option())
363 {
364 arg_buffer << " " << daemon_file_option();
365 }
366
367 if (_is_socket and socket_file_option())
368 {
369 if (not set_socket_file())
370 return false;
371
372 arg_buffer << " " << socket_file_option() << "\"" << _socket << "\"";
373 }
374
375 assert(port_option());
376 if (port_option() and _port > 0)
377 {
378 arg_buffer << " " << port_option() << _port;
379 }
380
381 options+= arg_buffer.str();
382
383 if (not _extra_args.empty())
384 options+= _extra_args;
385
386 return true;
387 }
388
389 bool Server::is_debug() const
390 {
391 return bool(getenv("LIBTEST_MANUAL_GDB"));
392 }
393
394 bool Server::is_valgrind() const
395 {
396 return bool(getenv("LIBTEST_MANUAL_VALGRIND"));
397 }
398
399 bool Server::kill(pid_t pid_arg)
400 {
401 if (check_pid(pid_arg) and kill_pid(pid_arg)) // If we kill it, reset
402 {
403 if (broken_pid_file() and not pid_file().empty())
404 {
405 unlink(pid_file().c_str());
406 }
407
408 reset_pid();
409
410 return true;
411 }
412
413 return false;
414 }
415
416 void server_startup_st::push_server(Server *arg)
417 {
418 servers.push_back(arg);
419
420 char port_str[NI_MAXSERV];
421 snprintf(port_str, sizeof(port_str), "%u", int(arg->port()));
422
423 std::string server_config_string;
424 if (arg->has_socket())
425 {
426 server_config_string+= "--socket=";
427 server_config_string+= '"';
428 server_config_string+= arg->socket();
429 server_config_string+= '"';
430 server_config_string+= " ";
431 }
432 else
433 {
434 server_config_string+= "--server=";
435 server_config_string+= arg->hostname();
436 server_config_string+= ":";
437 server_config_string+= port_str;
438 server_config_string+= " ";
439 }
440
441 server_list+= server_config_string;
442
443 }
444
445 Server* server_startup_st::pop_server()
446 {
447 Server *tmp= servers.back();
448 servers.pop_back();
449 return tmp;
450 }
451
452 void server_startup_st::shutdown(bool remove)
453 {
454 if (remove)
455 {
456 for (std::vector<Server *>::iterator iter= servers.begin(); iter != servers.end(); iter++)
457 {
458 delete *iter;
459 }
460 servers.clear();
461 }
462 else
463 {
464 for (std::vector<Server *>::iterator iter= servers.begin(); iter != servers.end(); iter++)
465 {
466 if ((*iter)->has_pid() and not (*iter)->kill((*iter)->pid()))
467 {
468 Error << "Unable to kill:" << *(*iter);
469 }
470 }
471 }
472 }
473
474 server_startup_st::~server_startup_st()
475 {
476 shutdown(true);
477 }
478
479 bool server_startup_st::is_debug() const
480 {
481 return bool(getenv("LIBTEST_MANUAL_GDB"));
482 }
483
484 bool server_startup_st::is_valgrind() const
485 {
486 return bool(getenv("LIBTEST_MANUAL_VALGRIND"));
487 }
488
489 bool server_startup(server_startup_st& construct, const std::string& server_type, in_port_t try_port, int argc, const char *argv[])
490 {
491 Outn();
492
493 // Look to see if we are being provided ports to use
494 {
495 char variable_buffer[1024];
496 snprintf(variable_buffer, sizeof(variable_buffer), "LIBTEST_PORT_%lu", (unsigned long)construct.count());
497
498 char *var;
499 if ((var= getenv(variable_buffer)))
500 {
501 in_port_t tmp= in_port_t(atoi(var));
502
503 if (tmp > 0)
504 try_port= tmp;
505 }
506 }
507
508 Server *server= NULL;
509 if (0)
510 { }
511 else if (server_type.compare("gearmand") == 0)
512 {
513 #ifdef GEARMAND_BINARY
514 #ifdef HAVE_LIBGEARMAN
515 server= build_gearmand("localhost", try_port);
516 #else
517 Error << "Libgearman was not found";
518 #endif
519 #else
520 Error << "No gearmand binary is available";
521 #endif
522 }
523 else if (server_type.compare("memcached") == 0)
524 {
525 #ifdef MEMCACHED_BINARY
526 #ifdef HAVE_LIBMEMCACHED
527 server= build_memcached("localhost", try_port);
528 #else
529 Error << "Libmemcached was not found";
530 #endif
531 #else
532 Error << "No memcached binary is available";
533 #endif
534 }
535 else
536 {
537 Error << "Failed to start " << server_type << ", no support was found to be compiled in for it.";
538 }
539
540 if (server == NULL)
541 {
542 Error << "Failure occured while creating server: " << server_type;
543 return false;
544 }
545
546 /*
547 We will now cycle the server we have created.
548 */
549 if (not server->cycle())
550 {
551 Error << "Could not start up server " << *server;
552 delete server;
553 return false;
554 }
555
556 server->build(argc, argv);
557
558 if (construct.is_debug())
559 {
560 Out << "Pausing for startup, hit return when ready.";
561 std::string gdb_command= server->base_command();
562 std::string options;
563 Out << "run " << server->args(options);
564 getchar();
565 }
566 else if (not server->start())
567 {
568 Error << "Failed to start " << *server;
569 delete server;
570 return false;
571 }
572 else
573 {
574 Out << "STARTING SERVER(pid:" << server->pid() << "): " << server->running();
575 }
576
577 construct.push_server(server);
578
579 if (default_port() == 0)
580 {
581 assert(server->has_port());
582 set_default_port(server->port());
583 }
584
585 Outn();
586
587 return true;
588 }
589
590 bool server_startup_st::start_socket_server(const std::string& server_type, const in_port_t try_port, int argc, const char *argv[])
591 {
592 (void)try_port;
593 Outn();
594
595 Server *server= NULL;
596 if (0)
597 { }
598 else if (server_type.compare("gearmand") == 0)
599 {
600 Error << "Socket files are not supported for gearmand yet";
601 }
602 else if (server_type.compare("memcached") == 0)
603 {
604 #ifdef MEMCACHED_BINARY
605 #ifdef HAVE_LIBMEMCACHED
606 server= build_memcached_socket("localhost", try_port);
607 #else
608 Error << "Libmemcached was not found";
609 #endif
610 #else
611 Error << "No memcached binary is available";
612 #endif
613 }
614 else
615 {
616 Error << "Failed to start " << server_type << ", no support was found to be compiled in for it.";
617 }
618
619 if (server == NULL)
620 {
621 Error << "Failure occured while creating server: " << server_type;
622 return false;
623 }
624
625 /*
626 We will now cycle the server we have created.
627 */
628 if (not server->cycle())
629 {
630 Error << "Could not start up server " << *server;
631 delete server;
632 return false;
633 }
634
635 server->build(argc, argv);
636
637 if (is_debug())
638 {
639 Out << "Pausing for startup, hit return when ready.";
640 std::string gdb_command= server->base_command();
641 std::string options;
642 Out << "run " << server->args(options);
643 getchar();
644 }
645 else if (not server->start())
646 {
647 Error << "Failed to start " << *server;
648 delete server;
649 return false;
650 }
651 else
652 {
653 Out << "STARTING SERVER(pid:" << server->pid() << "): " << server->running();
654 }
655
656 push_server(server);
657
658 set_default_socket(server->socket().c_str());
659
660 Outn();
661
662 return true;
663 }
664
665 } // namespace libtest