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 // If the server exists, kill it
121 // Try to ping, and kill the server #limit number of times
123 while (--limit
and is_pid_valid(current_pid
= get_pid()))
125 if (kill(current_pid
))
127 Log
<< "Killed existing server," << *this << " with pid:" << current_pid
;
133 // For whatever reason we could not kill it, and we reached limit
136 Error
<< "Reached limit, could not kill server pid:" << current_pid
;
143 // Grab a one off command
144 bool Server::command(std::string
& command_arg
)
146 rebuild_base_command();
148 command_arg
+= _base_command
;
150 if (args(command_arg
))
160 // If we find that we already have a pid then kill it.
161 if (has_pid() and not kill(_pid
))
163 Error
<< "Could not kill() existing server during start() pid:" << _pid
;
166 assert(not has_pid());
169 if (not command(_running
))
171 Error
<< "Could not build command()";
175 if (is_valgrind() or is_helgrind())
180 int ret
= system(_running
.c_str());
181 if (not exited_successfully(ret
))
183 Error
<< "system() failed:" << strerror(errno
);
188 if (is_helgrind() or is_valgrind())
193 if (pid_file_option() and not pid_file().empty())
195 Wait
wait(pid_file(), 8);
197 if (not wait
.successful())
199 Error
<< "Unable to open pidfile for: " << _running
;
203 int count
= is_helgrind() or is_valgrind() ? 20 : 5;
204 while (not ping() and --count
)
211 // If we happen to have a pid file, lets try to kill it
212 if (pid_file_option() and not pid_file().empty())
214 kill_file(pid_file());
216 Error
<< "Failed to ping() server started with:" << _running
;
221 // A failing get_pid() at this point is considered an error
227 void Server::reset_pid()
239 bool Server::set_socket_file()
241 char file_buffer
[FILENAME_MAX
];
244 if (broken_pid_file())
246 snprintf(file_buffer
, sizeof(file_buffer
), "/tmp/%s.socketXXXXXX", name());
250 snprintf(file_buffer
, sizeof(file_buffer
), "var/run/%s.socketXXXXXX", name());
254 if ((fd
= mkstemp(file_buffer
)) == -1)
262 _socket
= file_buffer
;
267 bool Server::set_pid_file()
269 char file_buffer
[FILENAME_MAX
];
272 if (broken_pid_file())
274 snprintf(file_buffer
, sizeof(file_buffer
), "/tmp/%s.pidXXXXXX", name());
278 snprintf(file_buffer
, sizeof(file_buffer
), "var/run/%s.pidXXXXXX", name());
282 if ((fd
= mkstemp(file_buffer
)) == -1)
290 _pid_file
= file_buffer
;
295 bool Server::set_log_file()
297 char file_buffer
[FILENAME_MAX
];
300 snprintf(file_buffer
, sizeof(file_buffer
), "var/log/%s.logXXXXXX", name());
302 if ((fd
= mkstemp(file_buffer
)) == -1)
309 _log_file
= file_buffer
;
314 void Server::rebuild_base_command()
316 _base_command
.clear();
319 _base_command
+= libtool();
322 if (is_debug() and getenv("GDB_COMMAND"))
324 _base_command
+= getenv("GDB_COMMAND");
327 else if (is_valgrind() and getenv("VALGRIND_COMMAND"))
329 _base_command
+= getenv("VALGRIND_COMMAND");
332 else if (is_helgrind() and getenv("HELGRIND_COMMAND"))
334 _base_command
+= getenv("HELGRIND_COMMAND");
338 _base_command
+= executable();
341 void Server::set_extra_args(const std::string
&arg
)
346 bool Server::args(std::string
& options
)
348 std::stringstream arg_buffer
;
350 // Set a log file if it was requested (and we can)
351 if (getenv("LIBTEST_LOG") and log_file_option())
353 if (not set_log_file())
358 arg_buffer
<< " " << log_file_option() << _log_file
;
362 if (pid_file_option())
364 if (_pid_file
.empty() and not set_pid_file())
369 arg_buffer
<< " " << pid_file_option() << pid_file();
372 assert(daemon_file_option());
373 if (daemon_file_option() and not is_valgrind() and not is_helgrind())
375 arg_buffer
<< " " << daemon_file_option();
378 if (_is_socket
and socket_file_option())
380 if (not set_socket_file())
385 arg_buffer
<< " " << socket_file_option() << "\"" << _socket
<< "\"";
388 assert(port_option());
389 if (port_option() and _port
> 0)
391 arg_buffer
<< " " << port_option() << _port
;
394 options
+= arg_buffer
.str();
396 if (not _extra_args
.empty())
398 options
+= _extra_args
;
404 bool Server::is_debug() const
406 return bool(getenv("LIBTEST_MANUAL_GDB"));
409 bool Server::is_valgrind() const
411 return bool(getenv("LIBTEST_MANUAL_VALGRIND"));
414 bool Server::is_helgrind() const
416 return bool(getenv("LIBTEST_MANUAL_HELGRIND"));
419 bool Server::kill(pid_t pid_arg
)
421 if (check_pid(pid_arg
) and kill_pid(pid_arg
)) // If we kill it, reset
423 if (broken_pid_file() and not pid_file().empty())
425 unlink(pid_file().c_str());
428 if (broken_socket_cleanup() and has_socket() and not socket().empty())
430 unlink(socket().c_str());
441 } // namespace libtest