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.
37 #include "libtest/yatlcon.h"
39 #include <libtest/common.h>
54 static inline std::string
&rtrim(std::string
&s
)
56 s
.erase(std::find_if(s
.rbegin(), s
.rend(), std::not1(std::ptr_fun
<int, int>(std::isspace
))).base(), s
.end());
60 #include <libtest/server.h>
61 #include <libtest/stream.h>
62 #include <libtest/killpid.h>
66 std::ostream
& operator<<(std::ostream
& output
, const Server
&arg
)
70 output
<< arg
.hostname();
74 output
<< arg
.hostname() << ":" << arg
.port();
79 output
<< " Pid:" << arg
.pid();
84 output
<< " Socket:" << arg
.socket();
87 if (arg
.running().empty() == false)
89 output
<< " Exec:" << arg
.running();
92 return output
; // for multiple << operators
101 Buffer(char *b
) : b_(b
) {}
102 ~Buffer() { if (b_
) free(b_
); }
103 char* buf() { return b_
; }
111 #define MAGIC_MEMORY 123570
113 Server::Server(const std::string
& host_arg
, const in_port_t port_arg
,
114 const std::string
& executable
, const bool _is_libtool
,
115 bool is_socket_arg
) :
116 _magic(MAGIC_MEMORY
),
117 _is_socket(is_socket_arg
),
120 _app(executable
, _is_libtool
),
121 out_of_ban_killed_(false),
137 bool Server::validate()
139 return _magic
== MAGIC_MEMORY
;
142 // If the server exists, kill it
147 // Try to ping, and kill the server #limit number of times
149 is_pid_valid(_app
.pid()))
153 Log
<< "Killed existing server," << *this;
159 // For whatever reason we could not kill it, and we reached limit
162 Error
<< "Reached limit, could not kill server";
169 bool Server::wait_for_pidfile() const
171 Wait
wait(pid_file(), 4);
173 return wait
.successful();
176 bool Server::init(const char *argv
[])
180 for (const char **ptr
= argv
; *ptr
; ++ptr
)
192 bool Server::has_pid() const
194 return (_app
.pid() > 1);
200 if (getenv("YATL_GDB_SERVER"))
205 if (port() == LIBTEST_FAIL_PORT
)
207 throw libtest::disconnected(LIBYATL_DEFAULT_PARAM
,
208 hostname(), port(), "Called failure");
211 if (getenv("YATL_PTRCHECK_SERVER"))
213 _app
.use_ptrcheck(true);
215 else if (getenv("YATL_VALGRIND_SERVER"))
217 _app
.use_valgrind(true);
220 out_of_ban_killed(false);
221 if (args(_app
) == false)
223 throw libtest::disconnected(LIBYATL_DEFAULT_PARAM
,
224 hostname(), port(), "Could not build command()");
227 libtest::release_port(_port
);
229 Application::error_t ret
;
230 if (Application::SUCCESS
!= (ret
= _app
.run()))
232 throw libtest::disconnected(LIBYATL_DEFAULT_PARAM
,
233 hostname(), port(), "Application::run() %s", libtest::Application::toString(ret
));
236 _running
= _app
.print();
238 if (valgrind_is_caller())
247 if (pid_file().empty() == false)
249 Wait
wait(pid_file(), 8);
251 if (wait
.successful() == false)
260 Buffer
buf( get_current_dir_name());
261 char *getcwd_buf
= buf
.buf();
263 libtest::vchar_t buf
;
264 buf
.resize(PATH_MAX
);
265 char *getcwd_buf
= getcwd(&buf
[0], buf
.size());
267 throw libtest::disconnected(LIBYATL_DEFAULT_PARAM
,
269 "Unable to open pidfile in %s for: %s stderr:%s",
270 getcwd_buf
? getcwd_buf
: "",
272 _app
.stderr_c_str());
278 uint32_t this_wait
= 0;
283 for (waited
= 0, retry
= 1; ; retry
++, waited
+= this_wait
)
285 if (_app
.check() == false)
290 if ((pinged
= ping()) == true)
294 else if (waited
>= _timeout
)
299 this_wait
= retry
* retry
/ 3 + 1;
300 libtest::dream(this_wait
, 0);
307 Error
<< "Failed to ping(" << _app
.pid() << ") wait: " << this_wait
<< " " << hostname() << ":" << port() << " run:" << _running
<< " " << error();
310 // If we happen to have a pid file, lets try to kill it
311 if ((pid_file().empty() == false) and (access(pid_file().c_str(), R_OK
) == 0))
314 if (kill_file(pid_file()) == false)
316 throw libtest::disconnected(LIBYATL_DEFAULT_PARAM
,
318 "Failed to kill off server, waited: %u after startup occurred, when pinging failed: %.*s stderr:%.*s",
320 int(_running
.size()), _running
.c_str(),
321 int(_app
.stderr_result_length()), _app
.stderr_c_str());
325 throw libtest::disconnected(LIBYATL_DEFAULT_PARAM
,
327 "Failed native ping(), pid: %d was alive: %s waited: %u server started, having pid_file. exec: %.*s stderr:%.*s",
329 _app
.check() ? "true" : "false",
331 int(_running
.size()), _running
.c_str(),
332 int(_app
.stderr_result_length()), _app
.stderr_c_str());
337 throw libtest::disconnected(LIBYATL_DEFAULT_PARAM
,
339 "Failed native ping(), pid: %d is alive: %s waited: %u server started. exec: %.*s stderr:%.*s",
341 _app
.check() ? "true" : "false",
343 int(_running
.size()), _running
.c_str(),
344 int(_app
.stderr_result_length()), _app
.stderr_c_str());
354 void Server::reset_pid()
360 pid_t
Server::pid() const
365 void Server::add_option(const std::string
& arg
)
367 _options
.push_back(std::make_pair(arg
, std::string()));
370 void Server::add_option(const std::string
& name_
, const std::string
& value_
)
372 _options
.push_back(std::make_pair(name_
, value_
));
375 bool Server::set_socket_file()
377 libtest::vchar_t file_buffer
;
378 file_buffer
.resize(FILENAME_MAX
);
381 if (broken_pid_file())
383 snprintf(&file_buffer
[0], file_buffer
.size(), "/tmp/%s.socketXXXXXX", name());
387 snprintf(&file_buffer
[0], file_buffer
.size(), "var/run/%s.socketXXXXXX", name());
391 if ((fd
= mkstemp(&file_buffer
[0])) == -1)
393 perror(&file_buffer
[0]);
397 unlink(&file_buffer
[0]);
399 _socket
= &file_buffer
[0];
404 bool Server::set_pid_file()
406 libtest::vchar_t file_buffer
;
407 file_buffer
.resize(FILENAME_MAX
);
410 if (broken_pid_file())
412 snprintf(&file_buffer
[0], file_buffer
.size(), "/tmp/%s.pidXXXXXX", name());
416 snprintf(&file_buffer
[0], file_buffer
.size(), "var/run/%s.pidXXXXXX", name());
420 if ((fd
= mkstemp(&file_buffer
[0])) == -1)
422 throw libtest::fatal(LIBYATL_DEFAULT_PARAM
, "mkstemp() failed on %s with %s", &file_buffer
[0], strerror(errno
));
425 unlink(&file_buffer
[0]);
427 _pid_file
= &file_buffer
[0];
432 bool Server::set_log_file()
434 libtest::vchar_t file_buffer
;
435 file_buffer
.resize(FILENAME_MAX
);
438 snprintf(&file_buffer
[0], file_buffer
.size(), "var/log/%s.logXXXXXX", name());
440 if ((fd
= mkstemp(&file_buffer
[0])) == -1)
442 throw libtest::fatal(LIBYATL_DEFAULT_PARAM
, "mkstemp() failed on %s with %s", &file_buffer
[0], strerror(errno
));
446 _log_file
= &file_buffer
[0];
451 bool Server::args(Application
& app
)
454 // Set a log file if it was requested (and we can)
455 if (has_log_file_option())
458 log_file_option(app
, _log_file
);
461 if (getenv("LIBTEST_SYSLOG") and has_syslog())
463 app
.add_option("--syslog");
468 if (_pid_file
.empty() and set_pid_file() == false)
473 pid_file_option(app
, pid_file());
476 if (has_socket_file_option())
478 if (set_socket_file() == false)
483 socket_file_option(app
, _socket
);
486 if (has_port_option())
488 port_option(app
, _port
);
491 for (Options::const_iterator iter
= _options
.begin(); iter
!= _options
.end(); ++iter
)
493 if ((*iter
).first
.empty() == false)
495 if ((*iter
).second
.empty() == false)
497 app
.add_option((*iter
).first
, (*iter
).second
);
501 app
.add_option((*iter
).first
);
511 if (check_pid(_app
.pid())) // If we kill it, reset
514 if (broken_pid_file() and pid_file().empty() == false)
516 unlink(pid_file().c_str());
519 if (broken_socket_cleanup() and has_socket() and not socket().empty())
521 unlink(socket().c_str());
532 std::pair
<std::string
, std::string
> Server::output()
537 _app
.stdout_result().data(),
538 _app
.stdout_result().size()
541 _app
.stderr_result().data(),
542 _app
.stderr_result().size()
548 } // namespace libtest