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>
53 static inline std::string
&rtrim(std::string
&s
)
55 s
.erase(std::find_if(s
.rbegin(), s
.rend(), std::not1(std::ptr_fun
<int, int>(std::isspace
))).base(), s
.end());
59 #include <libtest/server.h>
60 #include <libtest/stream.h>
61 #include <libtest/killpid.h>
65 std::ostream
& operator<<(std::ostream
& output
, const Server
&arg
)
69 output
<< arg
.hostname();
73 output
<< arg
.hostname() << ":" << arg
.port();
78 output
<< " Pid:" << arg
.pid();
83 output
<< " Socket:" << arg
.socket();
86 if (arg
.running().empty() == false)
88 output
<< " Exec:" << arg
.running();
91 return output
; // for multiple << operators
100 Buffer(char *b
) : b_(b
) {}
101 ~Buffer() { if (b_
) free(b_
); }
102 char* buf() { return b_
; }
110 #define MAGIC_MEMORY 123570
112 Server::Server(const std::string
& host_arg
, const in_port_t port_arg
,
113 const std::string
& executable
, const bool _is_libtool
,
114 bool is_socket_arg
) :
115 _magic(MAGIC_MEMORY
),
116 _is_socket(is_socket_arg
),
119 _app(executable
, _is_libtool
),
120 out_of_ban_killed_(false),
136 bool Server::validate()
138 return _magic
== MAGIC_MEMORY
;
141 // If the server exists, kill it
146 // Try to ping, and kill the server #limit number of times
148 is_pid_valid(_app
.pid()))
152 Log
<< "Killed existing server," << *this;
158 // For whatever reason we could not kill it, and we reached limit
161 Error
<< "Reached limit, could not kill server";
168 bool Server::wait_for_pidfile() const
170 Wait
wait(pid_file(), 4);
172 return wait
.successful();
175 bool Server::init(const char *argv
[])
179 for (const char **ptr
= argv
; *ptr
; ++ptr
)
191 bool Server::has_pid() const
193 return (_app
.pid() > 1);
199 if (getenv("YATL_GDB_SERVER"))
204 if (port() == LIBTEST_FAIL_PORT
)
206 throw libtest::disconnected(LIBYATL_DEFAULT_PARAM
,
207 hostname(), port(), "Called failure");
210 if (getenv("YATL_PTRCHECK_SERVER"))
212 _app
.use_ptrcheck(true);
214 else if (getenv("YATL_VALGRIND_SERVER"))
216 _app
.use_valgrind(true);
219 out_of_ban_killed(false);
220 if (args(_app
) == false)
222 throw libtest::disconnected(LIBYATL_DEFAULT_PARAM
,
223 hostname(), port(), "Could not build command()");
226 libtest::release_port(_port
);
228 Application::error_t ret
;
229 if (Application::SUCCESS
!= (ret
= _app
.run()))
231 throw libtest::disconnected(LIBYATL_DEFAULT_PARAM
,
232 hostname(), port(), "Application::run() %s", libtest::Application::toString(ret
));
235 _running
= _app
.print();
237 if (valgrind_is_caller())
246 if (pid_file().empty() == false)
248 Wait
wait(pid_file(), 8);
250 if (wait
.successful() == false)
259 Buffer
buf( get_current_dir_name());
260 char *getcwd_buf
= buf
.buf();
262 libtest::vchar_t buf
;
263 buf
.resize(PATH_MAX
);
264 char *getcwd_buf
= getcwd(&buf
[0], buf
.size());
266 throw libtest::disconnected(LIBYATL_DEFAULT_PARAM
,
268 "Unable to open pidfile in %s for: %s stderr:%s",
269 getcwd_buf
? getcwd_buf
: "",
271 _app
.stderr_c_str());
277 uint32_t this_wait
= 0;
282 for (waited
= 0, retry
= 1; ; retry
++, waited
+= this_wait
)
284 if (_app
.check() == false)
289 if ((pinged
= ping()) == true)
293 else if (waited
>= _timeout
)
298 this_wait
= retry
* retry
/ 3 + 1;
299 libtest::dream(this_wait
, 0);
306 Error
<< "Failed to ping(" << _app
.pid() << ") wait: " << this_wait
<< " " << hostname() << ":" << port() << " run:" << _running
<< " " << error();
309 // If we happen to have a pid file, lets try to kill it
310 if ((pid_file().empty() == false) and (access(pid_file().c_str(), R_OK
) == 0))
313 if (kill_file(pid_file()) == false)
315 throw libtest::disconnected(LIBYATL_DEFAULT_PARAM
,
317 "Failed to kill off server, waited: %u after startup occurred, when pinging failed: %.*s stderr:%.*s",
319 int(_running
.size()), _running
.c_str(),
320 int(_app
.stderr_result_length()), _app
.stderr_c_str());
324 throw libtest::disconnected(LIBYATL_DEFAULT_PARAM
,
326 "Failed native ping(), pid: %d was alive: %s waited: %u server started, having pid_file. exec: %.*s stderr:%.*s",
328 _app
.check() ? "true" : "false",
330 int(_running
.size()), _running
.c_str(),
331 int(_app
.stderr_result_length()), _app
.stderr_c_str());
336 throw libtest::disconnected(LIBYATL_DEFAULT_PARAM
,
338 "Failed native ping(), pid: %d is alive: %s waited: %u server started. exec: %.*s stderr:%.*s",
340 _app
.check() ? "true" : "false",
342 int(_running
.size()), _running
.c_str(),
343 int(_app
.stderr_result_length()), _app
.stderr_c_str());
353 void Server::reset_pid()
359 pid_t
Server::pid() const
364 void Server::add_option(const std::string
& arg
)
366 _options
.push_back(std::make_pair(arg
, std::string()));
369 void Server::add_option(const std::string
& name_
, const std::string
& value_
)
371 _options
.push_back(std::make_pair(name_
, value_
));
374 bool Server::set_socket_file()
376 libtest::vchar_t file_buffer
;
377 file_buffer
.resize(FILENAME_MAX
);
380 if (broken_pid_file())
382 snprintf(&file_buffer
[0], file_buffer
.size(), "/tmp/%s.socketXXXXXX", name());
386 snprintf(&file_buffer
[0], file_buffer
.size(), "var/run/%s.socketXXXXXX", name());
390 if ((fd
= mkstemp(&file_buffer
[0])) == -1)
392 perror(&file_buffer
[0]);
396 unlink(&file_buffer
[0]);
398 _socket
= &file_buffer
[0];
403 bool Server::set_pid_file()
405 libtest::vchar_t file_buffer
;
406 file_buffer
.resize(FILENAME_MAX
);
409 if (broken_pid_file())
411 snprintf(&file_buffer
[0], file_buffer
.size(), "/tmp/%s.pidXXXXXX", name());
415 snprintf(&file_buffer
[0], file_buffer
.size(), "var/run/%s.pidXXXXXX", name());
419 if ((fd
= mkstemp(&file_buffer
[0])) == -1)
421 throw libtest::fatal(LIBYATL_DEFAULT_PARAM
, "mkstemp() failed on %s with %s", &file_buffer
[0], strerror(errno
));
424 unlink(&file_buffer
[0]);
426 _pid_file
= &file_buffer
[0];
431 bool Server::set_log_file()
433 libtest::vchar_t file_buffer
;
434 file_buffer
.resize(FILENAME_MAX
);
437 snprintf(&file_buffer
[0], file_buffer
.size(), "var/log/%s.logXXXXXX", name());
439 if ((fd
= mkstemp(&file_buffer
[0])) == -1)
441 throw libtest::fatal(LIBYATL_DEFAULT_PARAM
, "mkstemp() failed on %s with %s", &file_buffer
[0], strerror(errno
));
445 _log_file
= &file_buffer
[0];
450 bool Server::args(Application
& app
)
453 // Set a log file if it was requested (and we can)
454 if (has_log_file_option())
457 log_file_option(app
, _log_file
);
460 if (getenv("LIBTEST_SYSLOG") and has_syslog())
462 app
.add_option("--syslog");
467 if (_pid_file
.empty() and set_pid_file() == false)
472 pid_file_option(app
, pid_file());
475 if (has_socket_file_option())
477 if (set_socket_file() == false)
482 socket_file_option(app
, _socket
);
485 if (has_port_option())
487 port_option(app
, _port
);
490 for (Options::const_iterator iter
= _options
.begin(); iter
!= _options
.end(); ++iter
)
492 if ((*iter
).first
.empty() == false)
494 if ((*iter
).second
.empty() == false)
496 app
.add_option((*iter
).first
, (*iter
).second
);
500 app
.add_option((*iter
).first
);
510 if (check_pid(_app
.pid())) // If we kill it, reset
513 if (broken_pid_file() and pid_file().empty() == false)
515 unlink(pid_file().c_str());
518 if (broken_socket_cleanup() and has_socket() and not socket().empty())
520 unlink(socket().c_str());
531 } // namespace libtest