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 (arg
.running().empty() == false)
71 output
<< " Exec:" << arg
.running();
74 return output
; // for multiple << operators
77 #define MAGIC_MEMORY 123570
79 Server::Server(const std::string
& host_arg
, const in_port_t port_arg
, bool is_socket_arg
) :
81 _is_socket(is_socket_arg
),
90 if (has_pid() and not kill(_pid
))
92 Error
<< "Unable to kill:" << *this;
96 bool Server::validate()
98 return _magic
== MAGIC_MEMORY
;
101 // If the server exists, kill it
106 // Try to ping, and kill the server #limit number of times
109 is_pid_valid(current_pid
= get_pid()))
111 if (kill(current_pid
))
113 Log
<< "Killed existing server," << *this << " with pid:" << current_pid
;
119 // For whatever reason we could not kill it, and we reached limit
122 Error
<< "Reached limit, could not kill server pid:" << current_pid
;
129 bool Server::wait_for_pidfile() const
131 Wait
wait(pid_file(), 4);
133 return wait
.successful();
138 // If we find that we already have a pid then kill it.
139 if (has_pid() and kill(_pid
) == false)
141 Error
<< "Could not kill() existing server during start() pid:" << _pid
;
145 if (has_pid() == false)
147 fatal_message("has_pid() failed, programer error");
150 Application
app(executable(), is_libtool());
156 else if (getenv("TESTS_ENVIRONMENT"))
158 if (strstr(getenv("TESTS_ENVIRONMENT"), "gdb"))
164 if (args(app
) == false)
166 Error
<< "Could not build command()";
170 Application::error_t ret
;
171 if (Application::SUCCESS
!= (ret
= app
.run()))
173 Error
<< "Application::run() " << ret
;
176 _running
= app
.print();
178 if (Application::SUCCESS
!= (ret
= app
.wait()))
180 Error
<< "Application::wait() " << _running
<< " " << ret
;
184 if (is_helgrind() or is_valgrind())
189 if (pid_file().empty() == false)
191 Wait
wait(pid_file(), 8);
193 if (wait
.successful() == false)
195 libtest::fatal(LIBYATL_DEFAULT_PARAM
,
196 "Unable to open pidfile for: %s",
203 while ((pinged
= ping()) == false and
204 counter
< (is_helgrind() or is_valgrind() ? 20 : 5))
206 dream(counter
++, 50000);
211 // If we happen to have a pid file, lets try to kill it
212 if (pid_file().empty() == false)
214 if (kill_file(pid_file()) == false)
216 fatal_message("Failed to kill off server after startup occurred, when pinging failed");
218 Error
<< "Failed to ping() server started, having pid_file. exec:" << _running
;
222 Error
<< "Failed to ping() server started. exec:" << _running
;
228 // A failing get_pid() at this point is considered an error
234 void Server::reset_pid()
246 void Server::add_option(const std::string
& arg
)
248 _options
.push_back(std::make_pair(arg
, std::string()));
251 void Server::add_option(const std::string
& name
, const std::string
& value
)
253 _options
.push_back(std::make_pair(name
, value
));
256 bool Server::set_socket_file()
258 char file_buffer
[FILENAME_MAX
];
261 if (broken_pid_file())
263 snprintf(file_buffer
, sizeof(file_buffer
), "/tmp/%s.socketXXXXXX", name());
267 snprintf(file_buffer
, sizeof(file_buffer
), "var/run/%s.socketXXXXXX", name());
271 if ((fd
= mkstemp(file_buffer
)) == -1)
279 _socket
= file_buffer
;
284 bool Server::set_pid_file()
286 char file_buffer
[FILENAME_MAX
];
289 if (broken_pid_file())
291 snprintf(file_buffer
, sizeof(file_buffer
), "/tmp/%s.pidXXXXXX", name());
295 snprintf(file_buffer
, sizeof(file_buffer
), "var/run/%s.pidXXXXXX", name());
299 if ((fd
= mkstemp(file_buffer
)) == -1)
307 _pid_file
= file_buffer
;
312 bool Server::set_log_file()
314 char file_buffer
[FILENAME_MAX
];
317 snprintf(file_buffer
, sizeof(file_buffer
), "var/log/%s.logXXXXXX", name());
319 if ((fd
= mkstemp(file_buffer
)) == -1)
321 libtest::fatal(LIBYATL_DEFAULT_PARAM
, "mkstemp() failed on %s with %s", file_buffer
, strerror(errno
));
325 _log_file
= file_buffer
;
330 bool Server::args(Application
& app
)
333 // Set a log file if it was requested (and we can)
334 if (has_log_file_option())
337 log_file_option(app
, _log_file
);
340 if (getenv("LIBTEST_SYSLOG") and has_syslog())
342 app
.add_option("--syslog");
347 if (_pid_file
.empty() and set_pid_file() == false)
352 pid_file_option(app
, pid_file());
355 assert(daemon_file_option());
356 if (daemon_file_option() and not is_valgrind() and not is_helgrind())
358 app
.add_option(daemon_file_option());
361 if (has_socket_file_option())
363 if (set_socket_file() == false)
368 socket_file_option(app
, _socket
);
371 if (has_port_option())
373 port_option(app
, _port
);
376 for (Options::const_iterator iter
= _options
.begin(); iter
!= _options
.end(); iter
++)
378 if ((*iter
).second
.empty() == false)
380 app
.add_option((*iter
).first
, (*iter
).second
);
384 app
.add_option((*iter
).first
);
391 bool Server::is_debug() const
393 return bool(getenv("LIBTEST_MANUAL_GDB"));
396 bool Server::is_valgrind() const
398 return bool(getenv("LIBTEST_MANUAL_VALGRIND"));
401 bool Server::is_helgrind() const
403 return bool(getenv("LIBTEST_MANUAL_HELGRIND"));
406 bool Server::kill(pid_t pid_arg
)
408 if (check_pid(pid_arg
) and kill_pid(pid_arg
)) // If we kill it, reset
410 if (broken_pid_file() and pid_file().empty() == false)
412 unlink(pid_file().c_str());
415 if (broken_socket_cleanup() and has_socket() and not socket().empty())
417 unlink(socket().c_str());
428 } // namespace libtest