31ea6effab54d2d5d5081efd45c59bb9fdcb5f5f
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
24 #include <libtest/common.h>
36 static inline std::string
&rtrim(std::string
&s
)
38 s
.erase(std::find_if(s
.rbegin(), s
.rend(), std::not1(std::ptr_fun
<int, int>(std::isspace
))).base(), s
.end());
42 #include <libtest/server.h>
43 #include <libtest/stream.h>
44 #include <libtest/killpid.h>
48 std::ostream
& operator<<(std::ostream
& output
, const Server
&arg
)
52 output
<< arg
.hostname();
56 output
<< arg
.hostname() << ":" << arg
.port();
61 output
<< " Pid:" << arg
.pid();
66 output
<< " Socket:" << arg
.socket();
69 if (not arg
.running().empty())
71 output
<< " Exec:" << arg
.running();
74 return output
; // for multiple << operators
77 Server::Server(const std::string
& host_arg
, const in_port_t port_arg
, bool is_socket_arg
) :
78 _is_socket(is_socket_arg
),
87 if (has_pid() and not kill(_pid
))
89 Error
<< "Unable to kill:" << *this;
93 // If the server exists, kill it
98 // Try to ping, and kill the server #limit number of times
100 while (--limit
and is_pid_valid(current_pid
= get_pid()))
102 if (kill(current_pid
))
104 Log
<< "Killed existing server," << *this << " with pid:" << current_pid
;
110 // For whatever reason we could not kill it, and we reached limit
113 Error
<< "Reached limit, could not kill server pid:" << current_pid
;
120 bool Server::wait_for_pidfile() const
122 Wait
wait(pid_file(), 4);
124 return wait
.successful();
129 // If we find that we already have a pid then kill it.
130 if (has_pid() and kill(_pid
) == false)
132 Error
<< "Could not kill() existing server during start() pid:" << _pid
;
136 if (has_pid() == false)
138 fatal_message("has_pid() failed, programer error");
141 Application
app(executable(), is_libtool());
147 else if (getenv("TESTS_ENVIRONMENT"))
149 if (strstr(getenv("TESTS_ENVIRONMENT"), "gdb"))
155 if (args(app
) == false)
157 Error
<< "Could not build command()";
161 Application::error_t ret
;
162 if (Application::SUCCESS
!= (ret
= app
.run()))
164 Error
<< "Application::run() " << ret
;
167 _running
= app
.print();
169 if (Application::SUCCESS
!= (ret
= app
.wait()))
171 Error
<< "Application::wait() " << app
.print() << " " << ret
;
175 if (is_helgrind() or is_valgrind())
180 if (pid_file().empty() == false)
182 Wait
wait(pid_file(), 8);
184 if (wait
.successful() == false)
186 libtest::fatal(LIBYATL_DEFAULT_PARAM
,
187 "Unable to open pidfile for: %s",
194 while ((pinged
= ping()) == false and
195 counter
< (is_helgrind() or is_valgrind() ? 20 : 5))
197 dream(counter
++, 50000);
202 // If we happen to have a pid file, lets try to kill it
203 if (pid_file().empty() == false)
205 if (kill_file(pid_file()) == false)
207 fatal_message("Failed to kill off server after startup occurred, when pinging failed");
209 Error
<< "Failed to ping() server started, having pid_file. exec:" << _running
;
213 Error
<< "Failed to ping() server started. exec:" << _running
;
219 // A failing get_pid() at this point is considered an error
225 void Server::reset_pid()
237 void Server::add_option(const std::string
& arg
)
239 _options
.push_back(std::make_pair(arg
, std::string()));
242 void Server::add_option(const std::string
& name
, const std::string
& value
)
244 _options
.push_back(std::make_pair(name
, value
));
247 bool Server::set_socket_file()
249 char file_buffer
[FILENAME_MAX
];
252 if (broken_pid_file())
254 snprintf(file_buffer
, sizeof(file_buffer
), "/tmp/%s.socketXXXXXX", name());
258 snprintf(file_buffer
, sizeof(file_buffer
), "var/run/%s.socketXXXXXX", name());
262 if ((fd
= mkstemp(file_buffer
)) == -1)
270 _socket
= file_buffer
;
275 bool Server::set_pid_file()
277 char file_buffer
[FILENAME_MAX
];
280 if (broken_pid_file())
282 snprintf(file_buffer
, sizeof(file_buffer
), "/tmp/%s.pidXXXXXX", name());
286 snprintf(file_buffer
, sizeof(file_buffer
), "var/run/%s.pidXXXXXX", name());
290 if ((fd
= mkstemp(file_buffer
)) == -1)
298 _pid_file
= file_buffer
;
303 bool Server::set_log_file()
305 char file_buffer
[FILENAME_MAX
];
308 snprintf(file_buffer
, sizeof(file_buffer
), "var/log/%s.logXXXXXX", name());
310 if ((fd
= mkstemp(file_buffer
)) == -1)
312 libtest::fatal(LIBYATL_DEFAULT_PARAM
, "mkstemp() failed on %s with %s", file_buffer
, strerror(errno
));
316 _log_file
= file_buffer
;
321 bool Server::args(Application
& app
)
324 // Set a log file if it was requested (and we can)
325 if (has_log_file_option())
328 log_file_option(app
, _log_file
);
331 if (getenv("LIBTEST_SYSLOG") and has_syslog())
333 app
.add_option("--syslog");
338 if (_pid_file
.empty() and set_pid_file() == false)
343 pid_file_option(app
, pid_file());
346 assert(daemon_file_option());
347 if (daemon_file_option() and not is_valgrind() and not is_helgrind())
349 app
.add_option(daemon_file_option());
352 if (has_socket_file_option())
354 if (set_socket_file() == false)
359 socket_file_option(app
, _socket
);
362 if (has_port_option())
364 port_option(app
, _port
);
367 for (Options::const_iterator iter
= _options
.begin(); iter
!= _options
.end(); iter
++)
369 if ((*iter
).second
.empty() == false)
371 app
.add_option((*iter
).first
, (*iter
).second
);
375 app
.add_option((*iter
).first
);
382 bool Server::is_debug() const
384 return bool(getenv("LIBTEST_MANUAL_GDB"));
387 bool Server::is_valgrind() const
389 return bool(getenv("LIBTEST_MANUAL_VALGRIND"));
392 bool Server::is_helgrind() const
394 return bool(getenv("LIBTEST_MANUAL_HELGRIND"));
397 bool Server::kill(pid_t pid_arg
)
399 if (check_pid(pid_arg
) and kill_pid(pid_arg
)) // If we kill it, reset
401 if (broken_pid_file() and pid_file().empty() == false)
403 unlink(pid_file().c_str());
406 if (broken_socket_cleanup() and has_socket() and not socket().empty())
408 unlink(socket().c_str());
419 } // namespace libtest