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
, const std::string
&command
)
53 if (WIFEXITED(status
) == true)
55 int ret
= WEXITSTATUS(status
);
61 else if (ret
== EXIT_FAILURE
)
63 libtest::Error
<< "Command executed, but returned EXIT_FAILURE: " << command
;
67 libtest::Error
<< "Command executed, but returned " << ret
;
70 else if (WIFSIGNALED(status
) == true)
72 int ret_signal
= WTERMSIG(status
);
73 libtest::Error
<< "Died from signal " << strsignal(ret_signal
);
83 std::ostream
& operator<<(std::ostream
& output
, const Server
&arg
)
87 output
<< arg
.hostname();
91 output
<< arg
.hostname() << ":" << arg
.port();
96 output
<< " Pid:" << arg
.pid();
101 output
<< " Socket:" << arg
.socket();
104 if (not arg
.running().empty())
106 output
<< " Exec:" << arg
.running();
110 return output
; // for multiple << operators
113 Server::Server(const std::string
& host_arg
, const in_port_t port_arg
, bool is_socket_arg
) :
114 _is_socket(is_socket_arg
),
123 if (has_pid() and not kill(_pid
))
125 Error
<< "Unable to kill:" << *this;
129 // If the server exists, kill it
134 // Try to ping, and kill the server #limit number of times
136 while (--limit
and is_pid_valid(current_pid
= get_pid()))
138 if (kill(current_pid
))
140 Log
<< "Killed existing server," << *this << " with pid:" << current_pid
;
146 // For whatever reason we could not kill it, and we reached limit
149 Error
<< "Reached limit, could not kill server pid:" << current_pid
;
156 // Grab a one off command
157 bool Server::command(std::string
& command_arg
)
159 rebuild_base_command();
161 command_arg
+= _base_command
;
163 if (args(command_arg
))
171 bool Server::wait_for_pidfile() const
173 Wait
wait(pid_file(), 4);
175 return wait
.successful();
180 // If we find that we already have a pid then kill it.
181 if (has_pid() and kill(_pid
) == false)
183 Error
<< "Could not kill() existing server during start() pid:" << _pid
;
186 assert(not has_pid());
189 if (command(_running
) == false)
191 Error
<< "Could not build command()";
195 if (is_valgrind() or is_helgrind())
200 int ret
= system(_running
.c_str());
201 if (exited_successfully(ret
, _running
) == false)
203 Error
<< "system(" << _running
<< ") failed: " << strerror(errno
);
208 if (is_helgrind() or is_valgrind())
213 if (pid_file_option() and pid_file().empty() == false)
215 Wait
wait(pid_file(), 8);
217 if (not wait
.successful())
219 Error
<< "Unable to open pidfile for: " << _running
;
225 while ((pinged
= ping()) == false and
226 counter
< (is_helgrind() or is_valgrind() ? 20 : 5))
228 dream(counter
++, 50000);
233 // If we happen to have a pid file, lets try to kill it
234 if (pid_file_option() and pid_file().empty() == false)
236 Error
<< "We are going to kill it off";
237 kill_file(pid_file());
239 Error
<< "Failed to ping() server started with:" << _running
;
244 // A failing get_pid() at this point is considered an error
250 void Server::reset_pid()
262 bool Server::set_socket_file()
264 char file_buffer
[FILENAME_MAX
];
267 if (broken_pid_file())
269 snprintf(file_buffer
, sizeof(file_buffer
), "/tmp/%s.socketXXXXXX", name());
273 snprintf(file_buffer
, sizeof(file_buffer
), "var/run/%s.socketXXXXXX", name());
277 if ((fd
= mkstemp(file_buffer
)) == -1)
285 _socket
= file_buffer
;
290 bool Server::set_pid_file()
292 char file_buffer
[FILENAME_MAX
];
295 if (broken_pid_file())
297 snprintf(file_buffer
, sizeof(file_buffer
), "/tmp/%s.pidXXXXXX", name());
301 snprintf(file_buffer
, sizeof(file_buffer
), "var/run/%s.pidXXXXXX", name());
305 if ((fd
= mkstemp(file_buffer
)) == -1)
313 _pid_file
= file_buffer
;
318 bool Server::set_log_file()
320 char file_buffer
[FILENAME_MAX
];
323 snprintf(file_buffer
, sizeof(file_buffer
), "var/log/%s.logXXXXXX", name());
325 if ((fd
= mkstemp(file_buffer
)) == -1)
332 _log_file
= file_buffer
;
337 void Server::rebuild_base_command()
339 _base_command
.clear();
342 _base_command
+= libtool();
343 _base_command
+= " --mode=execute ";
346 if (is_debug() and getenv("GDB_COMMAND"))
348 _base_command
+= getenv("GDB_COMMAND");
351 else if (is_valgrind() and getenv("VALGRIND_COMMAND"))
353 _base_command
+= getenv("VALGRIND_COMMAND");
356 else if (is_helgrind() and getenv("HELGRIND_COMMAND"))
358 _base_command
+= getenv("HELGRIND_COMMAND");
366 _base_command
+= getenv("PWD");
371 _base_command
+= executable();
374 void Server::set_extra_args(const std::string
&arg
)
379 bool Server::args(std::string
& options
)
381 std::stringstream arg_buffer
;
383 // Set a log file if it was requested (and we can)
384 if (getenv("LIBTEST_LOG") and log_file_option())
386 if (not set_log_file())
391 arg_buffer
<< " " << log_file_option() << _log_file
;
394 if (getenv("LIBTEST_SYSLOG") and has_syslog())
396 arg_buffer
<< " --syslog";
400 if (pid_file_option())
402 if (_pid_file
.empty() and set_pid_file() == false)
407 arg_buffer
<< " " << pid_file_option() << pid_file();
410 assert(daemon_file_option());
411 if (daemon_file_option() and not is_valgrind() and not is_helgrind())
413 arg_buffer
<< " " << daemon_file_option();
416 if (_is_socket
and socket_file_option())
418 if (not set_socket_file())
423 arg_buffer
<< " " << socket_file_option() << "\"" << _socket
<< "\"";
426 assert(port_option());
427 if (port_option() and _port
> 0)
429 arg_buffer
<< " " << port_option() << _port
;
432 options
+= arg_buffer
.str();
434 if (not _extra_args
.empty())
436 options
+= _extra_args
;
442 bool Server::is_debug() const
444 return bool(getenv("LIBTEST_MANUAL_GDB"));
447 bool Server::is_valgrind() const
449 return bool(getenv("LIBTEST_MANUAL_VALGRIND"));
452 bool Server::is_helgrind() const
454 return bool(getenv("LIBTEST_MANUAL_HELGRIND"));
457 bool Server::kill(pid_t pid_arg
)
459 if (check_pid(pid_arg
) and kill_pid(pid_arg
)) // If we kill it, reset
461 if (broken_pid_file() and pid_file().empty() == false)
463 unlink(pid_file().c_str());
466 if (broken_socket_cleanup() and has_socket() and not socket().empty())
468 unlink(socket().c_str());
479 } // namespace libtest