1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3 * Data Differential YATL (i.e. libtest) library
5 * Copyright (C) 2012 Data Differential, http://datadifferential.com/
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following disclaimer
16 * in the documentation and/or other materials provided with the
19 * * The names of its contributors may not be used to endorse or
20 * promote products derived from this software without specific prior
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 #include <libtest/common.h>
52 static inline std::string
&rtrim(std::string
&s
)
54 s
.erase(std::find_if(s
.rbegin(), s
.rend(), std::not1(std::ptr_fun
<int, int>(std::isspace
))).base(), s
.end());
58 #include <libtest/server.h>
59 #include <libtest/stream.h>
60 #include <libtest/killpid.h>
64 std::ostream
& operator<<(std::ostream
& output
, const Server
&arg
)
68 output
<< arg
.hostname();
72 output
<< arg
.hostname() << ":" << arg
.port();
77 output
<< " Pid:" << arg
.pid();
82 output
<< " Socket:" << arg
.socket();
85 if (arg
.running().empty() == false)
87 output
<< " Exec:" << arg
.running();
90 return output
; // for multiple << operators
93 #define MAGIC_MEMORY 123570
95 Server::Server(const std::string
& host_arg
, const in_port_t port_arg
,
96 const std::string
& executable
, const bool _is_libtool
,
99 _is_socket(is_socket_arg
),
102 _app(executable
, _is_libtool
),
103 out_of_ban_killed_(false)
118 bool Server::validate()
120 return _magic
== MAGIC_MEMORY
;
123 // If the server exists, kill it
128 // Try to ping, and kill the server #limit number of times
130 is_pid_valid(_app
.pid()))
134 Log
<< "Killed existing server," << *this;
140 // For whatever reason we could not kill it, and we reached limit
143 Error
<< "Reached limit, could not kill server";
150 bool Server::wait_for_pidfile() const
152 Wait
wait(pid_file(), 4);
154 return wait
.successful();
157 bool Server::has_pid() const
159 return (_app
.pid() > 1);
165 // If we find that we already have a pid then kill it.
166 if (has_pid() == true)
169 fatal_message("has_pid() failed, programer error");
173 // This needs more work.
181 if (port() == LIBTEST_FAIL_PORT
)
183 throw libtest::start(LIBYATL_DEFAULT_PARAM
,
184 hostname(), port(), "Called failure");
187 if (getenv("YATL_PTRCHECK_SERVER"))
191 else if (getenv("YATL_VALGRIND_SERVER"))
196 if (args(_app
) == false)
198 throw libtest::start(LIBYATL_DEFAULT_PARAM
,
199 hostname(), port(), "Could not build command()");
202 libtest::release_port(_port
);
203 Application::error_t ret
;
204 if (Application::SUCCESS
!= (ret
= _app
.run()))
206 throw libtest::start(LIBYATL_DEFAULT_PARAM
,
207 hostname(), port(), "Application::run() %s", libtest::Application::toString(ret
));
210 _running
= _app
.print();
212 if (valgrind_is_caller())
221 if (pid_file().empty() == false)
223 Wait
wait(pid_file(), 8);
225 if (wait
.successful() == false)
234 char *getcwd_buf
= getcwd(buf
, sizeof(buf
));
235 throw libtest::start(LIBYATL_DEFAULT_PARAM
,
237 "Unable to open pidfile in %s for: %s stderr:%s",
238 getcwd_buf
? getcwd_buf
: "",
240 _app
.stderr_c_str());
245 uint32_t this_wait
= 0;
248 uint32_t timeout
= 20; // This number should be high enough for valgrind startup (which is slow)
252 for (waited
= 0, retry
= 4; ; retry
++, waited
+= this_wait
)
254 if ((pinged
= ping()) == true)
258 else if (waited
>= timeout
)
263 this_wait
= retry
* retry
/ 3 + 1;
264 libtest::dream(this_wait
, 0);
270 // If we happen to have a pid file, lets try to kill it
271 if ((pid_file().empty() == false) and (access(pid_file().c_str(), R_OK
) == 0))
274 if (kill_file(pid_file()) == false)
276 throw libtest::start(LIBYATL_DEFAULT_PARAM
,
278 "Failed to kill off server, waited: %u after startup occurred, when pinging failed: %.*s stderr:%.*s",
280 int(_running
.size()), _running
.c_str(),
281 int(_app
.stderr_result_length()), _app
.stderr_c_str());
285 throw libtest::start(LIBYATL_DEFAULT_PARAM
,
287 "Failed native ping(), pid: %d was alive: %s waited: %u server started, having pid_file. exec: %.*s stderr:%.*s",
289 _app
.check() ? "true" : "false",
291 int(_running
.size()), _running
.c_str(),
292 int(_app
.stderr_result_length()), _app
.stderr_c_str());
297 throw libtest::start(LIBYATL_DEFAULT_PARAM
,
299 "Failed native ping(), pid: %d is alive: %s waited: %u server started. exec: %.*s stderr:%.*s",
301 _app
.check() ? "true" : "false",
303 int(_running
.size()), _running
.c_str(),
304 int(_app
.stderr_result_length()), _app
.stderr_c_str());
314 void Server::reset_pid()
320 pid_t
Server::pid() const
325 void Server::add_option(const std::string
& arg
)
327 _options
.push_back(std::make_pair(arg
, std::string()));
330 void Server::add_option(const std::string
& name
, const std::string
& value
)
332 _options
.push_back(std::make_pair(name
, value
));
335 bool Server::set_socket_file()
337 char file_buffer
[FILENAME_MAX
];
340 if (broken_pid_file())
342 snprintf(file_buffer
, sizeof(file_buffer
), "/tmp/%s.socketXXXXXX", name());
346 snprintf(file_buffer
, sizeof(file_buffer
), "var/run/%s.socketXXXXXX", name());
350 if ((fd
= mkstemp(file_buffer
)) == -1)
358 _socket
= file_buffer
;
363 bool Server::set_pid_file()
365 char file_buffer
[FILENAME_MAX
];
368 if (broken_pid_file())
370 snprintf(file_buffer
, sizeof(file_buffer
), "/tmp/%s.pidXXXXXX", name());
374 snprintf(file_buffer
, sizeof(file_buffer
), "var/run/%s.pidXXXXXX", name());
378 if ((fd
= mkstemp(file_buffer
)) == -1)
380 throw libtest::fatal(LIBYATL_DEFAULT_PARAM
, "mkstemp() failed on %s with %s", file_buffer
, strerror(errno
));
385 _pid_file
= file_buffer
;
390 bool Server::set_log_file()
392 char file_buffer
[FILENAME_MAX
];
395 snprintf(file_buffer
, sizeof(file_buffer
), "var/log/%s.logXXXXXX", name());
397 if ((fd
= mkstemp(file_buffer
)) == -1)
399 throw libtest::fatal(LIBYATL_DEFAULT_PARAM
, "mkstemp() failed on %s with %s", file_buffer
, strerror(errno
));
403 _log_file
= file_buffer
;
408 bool Server::args(Application
& app
)
411 // Set a log file if it was requested (and we can)
412 if (has_log_file_option())
415 log_file_option(app
, _log_file
);
418 if (getenv("LIBTEST_SYSLOG") and has_syslog())
420 app
.add_option("--syslog");
425 if (_pid_file
.empty() and set_pid_file() == false)
430 pid_file_option(app
, pid_file());
433 if (has_socket_file_option())
435 if (set_socket_file() == false)
440 socket_file_option(app
, _socket
);
443 if (has_port_option())
445 port_option(app
, _port
);
448 for (Options::const_iterator iter
= _options
.begin(); iter
!= _options
.end(); ++iter
)
450 if ((*iter
).second
.empty() == false)
452 app
.add_option((*iter
).first
, (*iter
).second
);
456 app
.add_option((*iter
).first
);
465 if (check_pid(_app
.pid())) // If we kill it, reset
468 if (broken_pid_file() and pid_file().empty() == false)
470 unlink(pid_file().c_str());
473 if (broken_socket_cleanup() and has_socket() and not socket().empty())
475 unlink(socket().c_str());
486 } // namespace libtest