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
;
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 uint32_t timeout
= 20; // This number should be high enough for valgrind startup (which is slow)
208 for (waited
= 0, retry
= 1; ; retry
++, waited
+= this_wait
)
210 if ((pinged
= ping()) == true)
214 else if (waited
>= timeout
)
219 this_wait
= retry
* retry
/ 3 + 1;
220 libtest::dream(this_wait
, 0);
226 // If we happen to have a pid file, lets try to kill it
227 if (pid_file().empty() == false)
229 if (kill_file(pid_file()) == false)
231 fatal_message("Failed to kill off server after startup occurred, when pinging failed");
233 Error
<< "Failed to ping() server started, having pid_file. exec:" << _running
;
237 Error
<< "Failed to ping() server started. exec:" << _running
;
243 // A failing get_pid() at this point is considered an error
249 void Server::reset_pid()
261 void Server::add_option(const std::string
& arg
)
263 _options
.push_back(std::make_pair(arg
, std::string()));
266 void Server::add_option(const std::string
& name
, const std::string
& value
)
268 _options
.push_back(std::make_pair(name
, value
));
271 bool Server::set_socket_file()
273 char file_buffer
[FILENAME_MAX
];
276 if (broken_pid_file())
278 snprintf(file_buffer
, sizeof(file_buffer
), "/tmp/%s.socketXXXXXX", name());
282 snprintf(file_buffer
, sizeof(file_buffer
), "var/run/%s.socketXXXXXX", name());
286 if ((fd
= mkstemp(file_buffer
)) == -1)
294 _socket
= file_buffer
;
299 bool Server::set_pid_file()
301 char file_buffer
[FILENAME_MAX
];
304 if (broken_pid_file())
306 snprintf(file_buffer
, sizeof(file_buffer
), "/tmp/%s.pidXXXXXX", name());
310 snprintf(file_buffer
, sizeof(file_buffer
), "var/run/%s.pidXXXXXX", name());
314 if ((fd
= mkstemp(file_buffer
)) == -1)
322 _pid_file
= file_buffer
;
327 bool Server::set_log_file()
329 char file_buffer
[FILENAME_MAX
];
332 snprintf(file_buffer
, sizeof(file_buffer
), "var/log/%s.logXXXXXX", name());
334 if ((fd
= mkstemp(file_buffer
)) == -1)
336 libtest::fatal(LIBYATL_DEFAULT_PARAM
, "mkstemp() failed on %s with %s", file_buffer
, strerror(errno
));
340 _log_file
= file_buffer
;
345 bool Server::args(Application
& app
)
348 // Set a log file if it was requested (and we can)
349 if (has_log_file_option())
352 log_file_option(app
, _log_file
);
355 if (getenv("LIBTEST_SYSLOG") and has_syslog())
357 app
.add_option("--syslog");
362 if (_pid_file
.empty() and set_pid_file() == false)
367 pid_file_option(app
, pid_file());
370 assert(daemon_file_option());
371 if (daemon_file_option() and not is_valgrind() and not is_helgrind())
373 app
.add_option(daemon_file_option());
376 if (has_socket_file_option())
378 if (set_socket_file() == false)
383 socket_file_option(app
, _socket
);
386 if (has_port_option())
388 port_option(app
, _port
);
391 for (Options::const_iterator iter
= _options
.begin(); iter
!= _options
.end(); iter
++)
393 if ((*iter
).second
.empty() == false)
395 app
.add_option((*iter
).first
, (*iter
).second
);
399 app
.add_option((*iter
).first
);
406 bool Server::is_debug() const
408 return bool(getenv("LIBTEST_MANUAL_GDB"));
411 bool Server::is_valgrind() const
413 return bool(getenv("LIBTEST_MANUAL_VALGRIND"));
416 bool Server::is_helgrind() const
418 return bool(getenv("LIBTEST_MANUAL_HELGRIND"));
421 bool Server::kill(pid_t pid_arg
)
423 if (check_pid(pid_arg
) and kill_pid(pid_arg
)) // If we kill it, reset
425 if (broken_pid_file() and pid_file().empty() == false)
427 unlink(pid_file().c_str());
430 if (broken_socket_cleanup() and has_socket() and not socket().empty())
432 unlink(socket().c_str());
443 } // namespace libtest