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 "mem_config.h"
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
99 Buffer(char *b
) : b_(b
) {}
100 ~Buffer() { free(b_
); }
101 char* buf() { return b_
; }
109 #define MAGIC_MEMORY 123570
111 Server::Server(const std::string
& host_arg
, const in_port_t port_arg
,
112 const std::string
& executable
, const bool _is_libtool
,
113 bool is_socket_arg
) :
114 _magic(MAGIC_MEMORY
),
115 _is_socket(is_socket_arg
),
118 _app(executable
, _is_libtool
),
119 out_of_ban_killed_(false)
135 bool Server::validate()
137 return _magic
== MAGIC_MEMORY
;
140 // If the server exists, kill it
145 // Try to ping, and kill the server #limit number of times
147 is_pid_valid(_app
.pid()))
151 Log
<< "Killed existing server," << *this;
157 // For whatever reason we could not kill it, and we reached limit
160 Error
<< "Reached limit, could not kill server";
167 bool Server::wait_for_pidfile() const
169 Wait
wait(pid_file(), 4);
171 return wait
.successful();
174 bool Server::has_pid() const
176 return (_app
.pid() > 1);
182 // If we find that we already have a pid then kill it.
183 if (has_pid() == true)
186 fatal_message("has_pid() failed, programer error");
190 // This needs more work.
198 if (port() == LIBTEST_FAIL_PORT
)
200 throw libtest::disconnected(LIBYATL_DEFAULT_PARAM
,
201 hostname(), port(), "Called failure");
204 if (getenv("YATL_PTRCHECK_SERVER"))
208 else if (getenv("YATL_VALGRIND_SERVER"))
213 out_of_ban_killed(false);
214 if (args(_app
) == false)
216 throw libtest::disconnected(LIBYATL_DEFAULT_PARAM
,
217 hostname(), port(), "Could not build command()");
220 libtest::release_port(_port
);
222 Application::error_t ret
;
223 if (Application::SUCCESS
!= (ret
= _app
.run()))
225 throw libtest::disconnected(LIBYATL_DEFAULT_PARAM
,
226 hostname(), port(), "Application::run() %s", libtest::Application::toString(ret
));
229 _running
= _app
.print();
231 if (valgrind_is_caller())
240 if (pid_file().empty() == false)
242 Wait
wait(pid_file(), 8);
244 if (wait
.successful() == false)
253 Buffer
buf( get_current_dir_name());
254 char *getcwd_buf
= buf
.buf();
257 char *getcwd_buf
= getcwd(buf
, sizeof(buf
));
259 throw libtest::disconnected(LIBYATL_DEFAULT_PARAM
,
261 "Unable to open pidfile in %s for: %s stderr:%s",
262 getcwd_buf
? getcwd_buf
: "",
264 _app
.stderr_c_str());
270 uint32_t this_wait
= 0;
272 uint32_t timeout
= 40; // This number should be high enough for valgrind startup (which is slow)
276 for (waited
= 0, retry
= 7; ; retry
++, waited
+= this_wait
)
278 if (_app
.check() == false)
283 if ((pinged
= ping()) == true)
287 else if (waited
>= timeout
)
292 Error
<< "ping(" << _app
.pid() << ") wait: " << this_wait
<< " " << hostname() << ":" << port() << " " << error();
294 this_wait
= retry
* retry
/ 3 + 1;
295 libtest::dream(this_wait
, 0);
301 // If we happen to have a pid file, lets try to kill it
302 if ((pid_file().empty() == false) and (access(pid_file().c_str(), R_OK
) == 0))
305 if (kill_file(pid_file()) == false)
307 throw libtest::disconnected(LIBYATL_DEFAULT_PARAM
,
309 "Failed to kill off server, waited: %u after startup occurred, when pinging failed: %.*s stderr:%.*s",
311 int(_running
.size()), _running
.c_str(),
312 int(_app
.stderr_result_length()), _app
.stderr_c_str());
316 throw libtest::disconnected(LIBYATL_DEFAULT_PARAM
,
318 "Failed native ping(), pid: %d was alive: %s waited: %u server started, having pid_file. exec: %.*s stderr:%.*s",
320 _app
.check() ? "true" : "false",
322 int(_running
.size()), _running
.c_str(),
323 int(_app
.stderr_result_length()), _app
.stderr_c_str());
328 throw libtest::disconnected(LIBYATL_DEFAULT_PARAM
,
330 "Failed native ping(), pid: %d is alive: %s waited: %u server started. exec: %.*s stderr:%.*s",
332 _app
.check() ? "true" : "false",
334 int(_running
.size()), _running
.c_str(),
335 int(_app
.stderr_result_length()), _app
.stderr_c_str());
345 void Server::reset_pid()
351 pid_t
Server::pid() const
356 void Server::add_option(const std::string
& arg
)
358 _options
.push_back(std::make_pair(arg
, std::string()));
361 void Server::add_option(const std::string
& name_
, const std::string
& value_
)
363 _options
.push_back(std::make_pair(name_
, value_
));
366 bool Server::set_socket_file()
368 char file_buffer
[FILENAME_MAX
];
371 if (broken_pid_file())
373 snprintf(file_buffer
, sizeof(file_buffer
), "/tmp/%s.socketXXXXXX", name());
377 snprintf(file_buffer
, sizeof(file_buffer
), "var/run/%s.socketXXXXXX", name());
381 if ((fd
= mkstemp(file_buffer
)) == -1)
389 _socket
= file_buffer
;
394 bool Server::set_pid_file()
396 char file_buffer
[FILENAME_MAX
];
399 if (broken_pid_file())
401 snprintf(file_buffer
, sizeof(file_buffer
), "/tmp/%s.pidXXXXXX", name());
405 snprintf(file_buffer
, sizeof(file_buffer
), "var/run/%s.pidXXXXXX", name());
409 if ((fd
= mkstemp(file_buffer
)) == -1)
411 throw libtest::fatal(LIBYATL_DEFAULT_PARAM
, "mkstemp() failed on %s with %s", file_buffer
, strerror(errno
));
416 _pid_file
= file_buffer
;
421 bool Server::set_log_file()
423 char file_buffer
[FILENAME_MAX
];
426 snprintf(file_buffer
, sizeof(file_buffer
), "var/log/%s.logXXXXXX", name());
428 if ((fd
= mkstemp(file_buffer
)) == -1)
430 throw libtest::fatal(LIBYATL_DEFAULT_PARAM
, "mkstemp() failed on %s with %s", file_buffer
, strerror(errno
));
434 _log_file
= file_buffer
;
439 bool Server::args(Application
& app
)
442 // Set a log file if it was requested (and we can)
443 if (has_log_file_option())
446 log_file_option(app
, _log_file
);
449 if (getenv("LIBTEST_SYSLOG") and has_syslog())
451 app
.add_option("--syslog");
456 if (_pid_file
.empty() and set_pid_file() == false)
461 pid_file_option(app
, pid_file());
464 if (has_socket_file_option())
466 if (set_socket_file() == false)
471 socket_file_option(app
, _socket
);
474 if (has_port_option())
476 port_option(app
, _port
);
479 for (Options::const_iterator iter
= _options
.begin(); iter
!= _options
.end(); ++iter
)
481 if ((*iter
).second
.empty() == false)
483 app
.add_option((*iter
).first
, (*iter
).second
);
487 app
.add_option((*iter
).first
);
496 if (check_pid(_app
.pid())) // If we kill it, reset
499 if (broken_pid_file() and pid_file().empty() == false)
501 unlink(pid_file().c_str());
504 if (broken_socket_cleanup() and has_socket() and not socket().empty())
506 unlink(socket().c_str());
517 } // namespace libtest