1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
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.
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.
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
23 #include <libtest/common.h>
35 static inline std::string
&rtrim(std::string
&s
)
37 s
.erase(std::find_if(s
.rbegin(), s
.rend(), std::not1(std::ptr_fun
<int, int>(std::isspace
))).base(), s
.end());
41 #include <libtest/server.h>
42 #include <libtest/stream.h>
43 #include <libtest/killpid.h>
46 static bool exited_successfully(int status
)
48 if (WEXITSTATUS(status
) == 0)
60 std::ostream
& operator<<(std::ostream
& output
, const Server
&arg
)
64 output
<< arg
.hostname();
68 output
<< arg
.hostname() << ":" << arg
.port();
73 output
<< " Pid:" << arg
.pid();
78 output
<< " Socket:" << arg
.socket();
81 if (not arg
.running().empty())
83 output
<< " Exec:" << arg
.running();
87 return output
; // for multiple << operators
90 void Server::nap(void)
95 struct timespec global_sleep_value
= { 0, 50000 };
96 nanosleep(&global_sleep_value
, NULL
);
100 Server::Server(const std::string
& host_arg
, const in_port_t port_arg
, bool is_socket_arg
) :
101 _is_socket(is_socket_arg
),
110 if (has_pid() and not kill(_pid
))
112 Error
<< "Unable to kill:" << *this;
116 std::string
server_startup_st::option_string() const
118 std::string temp
= server_list
;
123 // If the server exists, kill it
128 // Try to ping, and kill the server #limit number of times
130 while (--limit
and (current_pid
= get_pid()) != -1)
132 if (kill(current_pid
))
134 Log
<< "Killed existing server," << *this << " with pid:" << current_pid
;
140 // For whatever reason we could not kill it, and we reached limit
143 Error
<< "Reached limit, could not kill server pid:" << current_pid
;
150 // Grab a one off command
151 bool Server::command(std::string
& command_arg
)
153 rebuild_base_command();
155 command_arg
+= _base_command
;
157 if (args(command_arg
))
167 // If we find that we already have a pid then kill it.
168 if (has_pid() and not kill(_pid
))
170 Error
<< "Could not kill() existing server during start() pid:" << _pid
;
173 assert(not has_pid());
176 if (not command(_running
))
178 Error
<< "Could not build command()";
182 if (is_valgrind() or is_helgrind())
187 int ret
= system(_running
.c_str());
188 if (not exited_successfully(ret
))
190 Error
<< "system() failed:" << strerror(errno
);
200 if (pid_file_option() and not pid_file().empty())
202 Wait
wait(pid_file());
204 if (not wait
.successful())
206 Error
<< "Unable to open pidfile: " << pid_file();
210 int count
= is_helgrind() ? 20 : 5;
211 while (not ping() and --count
)
218 Error
<< "Failed to ping() server once started:" << *this;
223 // A failing get_pid() at this point is considered an error
229 void Server::reset_pid()
241 bool Server::set_socket_file()
243 char file_buffer
[FILENAME_MAX
];
246 if (broken_pid_file())
248 snprintf(file_buffer
, sizeof(file_buffer
), "/tmp/%s.socketXXXXXX", name());
252 snprintf(file_buffer
, sizeof(file_buffer
), "var/run/%s.socketXXXXXX", name());
256 if ((fd
= mkstemp(file_buffer
)) == -1)
264 _socket
= file_buffer
;
269 bool Server::set_pid_file()
271 char file_buffer
[FILENAME_MAX
];
274 if (broken_pid_file())
276 snprintf(file_buffer
, sizeof(file_buffer
), "/tmp/%s.pidXXXXXX", name());
280 snprintf(file_buffer
, sizeof(file_buffer
), "var/run/%s.pidXXXXXX", name());
284 if ((fd
= mkstemp(file_buffer
)) == -1)
292 _pid_file
= file_buffer
;
297 bool Server::set_log_file()
299 char file_buffer
[FILENAME_MAX
];
302 snprintf(file_buffer
, sizeof(file_buffer
), "var/log/%s.logXXXXXX", name());
304 if ((fd
= mkstemp(file_buffer
)) == -1)
311 _log_file
= file_buffer
;
316 void Server::rebuild_base_command()
318 _base_command
.clear();
319 if (is_libtool() and getenv("LIBTOOL_COMMAND"))
321 _base_command
+= getenv("LIBTOOL_COMMAND");
325 if (is_debug() and getenv("GDB_COMMAND"))
327 _base_command
+= getenv("GDB_COMMAND");
330 else if (is_valgrind() and getenv("VALGRIND_COMMAND"))
332 _base_command
+= getenv("VALGRIND_COMMAND");
335 else if (is_helgrind() and getenv("HELGRIND_COMMAND"))
337 _base_command
+= getenv("HELGRIND_COMMAND");
341 _base_command
+= executable();
344 void Server::set_extra_args(const std::string
&arg
)
349 bool Server::args(std::string
& options
)
351 std::stringstream arg_buffer
;
353 // Set a log file if it was requested (and we can)
354 if (getenv("LIBTEST_LOG") and log_file_option())
356 if (not set_log_file())
361 arg_buffer
<< " " << log_file_option() << _log_file
;
365 if (pid_file_option())
367 if (not set_pid_file())
372 arg_buffer
<< " " << pid_file_option() << pid_file();
375 assert(daemon_file_option());
376 if (daemon_file_option() and not is_valgrind() and not is_helgrind())
378 arg_buffer
<< " " << daemon_file_option();
381 if (_is_socket
and socket_file_option())
383 if (not set_socket_file())
388 arg_buffer
<< " " << socket_file_option() << "\"" << _socket
<< "\"";
391 assert(port_option());
392 if (port_option() and _port
> 0)
394 arg_buffer
<< " " << port_option() << _port
;
397 options
+= arg_buffer
.str();
399 if (not _extra_args
.empty())
401 options
+= _extra_args
;
407 bool Server::is_debug() const
409 return bool(getenv("LIBTEST_MANUAL_GDB"));
412 bool Server::is_valgrind() const
414 return bool(getenv("LIBTEST_MANUAL_VALGRIND"));
417 bool Server::is_helgrind() const
419 return bool(getenv("LIBTEST_MANUAL_HELGRIND"));
422 bool Server::kill(pid_t pid_arg
)
424 if (check_pid(pid_arg
) and kill_pid(pid_arg
)) // If we kill it, reset
426 if (broken_pid_file() and not pid_file().empty())
428 unlink(pid_file().c_str());
431 if (broken_socket_cleanup() and has_socket() and not socket().empty())
433 unlink(socket().c_str());
444 void server_startup_st::push_server(Server
*arg
)
446 servers
.push_back(arg
);
448 char port_str
[NI_MAXSERV
];
449 snprintf(port_str
, sizeof(port_str
), "%u", int(arg
->port()));
451 std::string server_config_string
;
452 if (arg
->has_socket())
454 server_config_string
+= "--socket=";
455 server_config_string
+= '"';
456 server_config_string
+= arg
->socket();
457 server_config_string
+= '"';
458 server_config_string
+= " ";
462 server_config_string
+= "--server=";
463 server_config_string
+= arg
->hostname();
464 server_config_string
+= ":";
465 server_config_string
+= port_str
;
466 server_config_string
+= " ";
469 server_list
+= server_config_string
;
473 Server
* server_startup_st::pop_server()
475 Server
*tmp
= servers
.back();
480 void server_startup_st::shutdown(bool remove
)
484 for (std::vector
<Server
*>::iterator iter
= servers
.begin(); iter
!= servers
.end(); iter
++)
492 for (std::vector
<Server
*>::iterator iter
= servers
.begin(); iter
!= servers
.end(); iter
++)
494 if ((*iter
)->has_pid() and not (*iter
)->kill((*iter
)->pid()))
496 Error
<< "Unable to kill:" << *(*iter
);
502 server_startup_st::~server_startup_st()
507 bool server_startup_st::is_debug() const
509 return bool(getenv("LIBTEST_MANUAL_GDB"));
512 bool server_startup_st::is_valgrind() const
514 return bool(getenv("LIBTEST_MANUAL_VALGRIND"));
517 bool server_startup_st::is_helgrind() const
519 return bool(getenv("LIBTEST_MANUAL_HELGRIND"));
523 bool server_startup(server_startup_st
& construct
, const std::string
& server_type
, in_port_t try_port
, int argc
, const char *argv
[])
528 // Look to see if we are being provided ports to use
530 char variable_buffer
[1024];
531 snprintf(variable_buffer
, sizeof(variable_buffer
), "LIBTEST_PORT_%lu", (unsigned long)construct
.count());
534 if ((var
= getenv(variable_buffer
)))
536 in_port_t tmp
= in_port_t(atoi(var
));
543 libtest::Server
*server
= NULL
;
546 else if (server_type
.compare("gearmand") == 0)
552 server
= build_gearmand("localhost", try_port
);
556 Error
<< "Libgearman was not found";
561 Error
<< "No gearmand binary is available";
564 else if (server_type
.compare("blobslap_worker") == 0)
570 server
= build_blobslap_worker(try_port
);
574 Error
<< "Libgearman was not found";
579 Error
<< "No gearmand binary is available";
582 else if (server_type
.compare("memcached") == 0)
584 if (MEMCACHED_BINARY
)
586 if (HAVE_LIBMEMCACHED
)
588 server
= build_memcached("localhost", try_port
);
592 Error
<< "Libmemcached was not found";
597 Error
<< "No memcached binary is available";
602 Error
<< "Failed to start " << server_type
<< ", no support was found to be compiled in for it.";
607 Error
<< "Failure occured while creating server: " << server_type
;
612 We will now cycle the server we have created.
614 if (not server
->cycle())
616 Error
<< "Could not start up server " << *server
;
621 server
->build(argc
, argv
);
623 if (construct
.is_debug())
625 Out
<< "Pausing for startup, hit return when ready.";
626 std::string gdb_command
= server
->base_command();
628 Out
<< "run " << server
->args(options
);
631 else if (not server
->start())
633 Error
<< "Failed to start " << *server
;
639 Out
<< "STARTING SERVER(pid:" << server
->pid() << "): " << server
->running();
642 construct
.push_server(server
);
644 if (default_port() == 0)
646 assert(server
->has_port());
647 set_default_port(server
->port());
655 bool server_startup_st::start_socket_server(const std::string
& server_type
, const in_port_t try_port
, int argc
, const char *argv
[])
660 Server
*server
= NULL
;
663 else if (server_type
.compare("gearmand") == 0)
665 Error
<< "Socket files are not supported for gearmand yet";
667 else if (server_type
.compare("memcached") == 0)
669 if (MEMCACHED_BINARY
)
671 if (HAVE_LIBMEMCACHED
)
673 server
= build_memcached_socket("localhost", try_port
);
677 Error
<< "Libmemcached was not found";
682 Error
<< "No memcached binary is available";
687 Error
<< "Failed to start " << server_type
<< ", no support was found to be compiled in for it.";
692 Error
<< "Failure occured while creating server: " << server_type
;
697 We will now cycle the server we have created.
699 if (not server
->cycle())
701 Error
<< "Could not start up server " << *server
;
706 server
->build(argc
, argv
);
710 Out
<< "Pausing for startup, hit return when ready.";
711 std::string gdb_command
= server
->base_command();
713 Out
<< "run " << server
->args(options
);
716 else if (not server
->start())
718 Error
<< "Failed to start " << *server
;
724 Out
<< "STARTING SERVER(pid:" << server
->pid() << "): " << server
->running();
729 set_default_socket(server
->socket().c_str());
736 } // namespace libtest