+ uint32_t this_wait= 0;
+ bool pinged= false;
+ {
+ uint32_t timeout= 20; // This number should be high enough for valgrind startup (which is slow)
+ uint32_t waited;
+ uint32_t retry;
+
+ for (waited= 0, retry= 1; ; retry++, waited+= this_wait)
+ {
+ if ((pinged= ping()) == true)
+ {
+ break;
+ }
+ else if (waited >= timeout)
+ {
+ break;
+ }
+
+ this_wait= retry * retry / 3 + 1;
+ libtest::dream(this_wait, 0);
+ }
+ }
+
+ if (pinged == false)
+ {
+ // If we happen to have a pid file, lets try to kill it
+ if ((pid_file().empty() == false) and (access(pid_file().c_str(), R_OK) == 0))
+ {
+ _app.slurp();
+ if (kill_file(pid_file()) == false)
+ {
+ throw libtest::fatal(LIBYATL_DEFAULT_PARAM,
+ "Failed to kill off server, waited: %u after startup occurred, when pinging failed: %.*s stderr:%.*s",
+ this_wait,
+ int(_running.size()), _running.c_str(),
+ int(_app.stderr_result_length()), _app.stderr_c_str());
+ }
+
+ throw libtest::fatal(LIBYATL_DEFAULT_PARAM,
+ "Failed native ping(), pid: %d is alive: %s waited: %u server started, having pid_file. exec: %.*s stderr:%.*s",
+ int(_app.pid()),
+ _app.check() ? "true" : "false",
+ this_wait,
+ int(_running.size()), _running.c_str(),
+ int(_app.stderr_result_length()), _app.stderr_c_str());
+ }
+ else
+ {
+ throw libtest::fatal(LIBYATL_DEFAULT_PARAM,
+ "Failed native ping(), pid: %d is alive: %s waited: %u server started. exec: %.*s stderr:%.*s",
+ int(_app.pid()),
+ _app.check() ? "true" : "false",
+ this_wait,
+ int(_running.size()), _running.c_str(),
+ int(_app.stderr_result_length()), _app.stderr_c_str());
+ }
+ _running.clear();
+ return false;
+ }
+
+ return has_pid();
+}
+
+void Server::reset_pid()
+{
+ _running.clear();
+ _pid_file.clear();
+}
+
+pid_t Server::pid() const
+{
+ return _app.pid();
+}
+
+void Server::add_option(const std::string& arg)
+{
+ _options.push_back(std::make_pair(arg, std::string()));
+}
+
+void Server::add_option(const std::string& name, const std::string& value)
+{
+ _options.push_back(std::make_pair(name, value));
+}
+
+bool Server::set_socket_file()
+{
+ char file_buffer[FILENAME_MAX];
+ file_buffer[0]= 0;
+
+ if (broken_pid_file())
+ {
+ snprintf(file_buffer, sizeof(file_buffer), "/tmp/%s.socketXXXXXX", name());
+ }
+ else
+ {
+ snprintf(file_buffer, sizeof(file_buffer), "var/run/%s.socketXXXXXX", name());
+ }
+
+ int fd;
+ if ((fd= mkstemp(file_buffer)) == -1)
+ {
+ perror(file_buffer);
+ return false;
+ }
+ close(fd);
+ unlink(file_buffer);
+
+ _socket= file_buffer;
+
+ return true;
+}
+
+bool Server::set_pid_file()
+{
+ char file_buffer[FILENAME_MAX];
+ file_buffer[0]= 0;
+
+ if (broken_pid_file())
+ {
+ snprintf(file_buffer, sizeof(file_buffer), "/tmp/%s.pidXXXXXX", name());
+ }
+ else
+ {
+ snprintf(file_buffer, sizeof(file_buffer), "var/run/%s.pidXXXXXX", name());
+ }
+
+ int fd;
+ if ((fd= mkstemp(file_buffer)) == -1)
+ {
+ throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "mkstemp() failed on %s with %s", file_buffer, strerror(errno));
+ }
+ close(fd);
+ unlink(file_buffer);
+
+ _pid_file= file_buffer;
+
+ return true;
+}
+
+bool Server::set_log_file()
+{
+ char file_buffer[FILENAME_MAX];
+ file_buffer[0]= 0;
+
+ snprintf(file_buffer, sizeof(file_buffer), "var/log/%s.logXXXXXX", name());
+ int fd;
+ if ((fd= mkstemp(file_buffer)) == -1)
+ {
+ throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "mkstemp() failed on %s with %s", file_buffer, strerror(errno));
+ }
+ close(fd);
+
+ _log_file= file_buffer;
+
+ return true;
+}
+
+bool Server::args(Application& app)
+{
+
+ // Set a log file if it was requested (and we can)
+ if (has_log_file_option())
+ {
+ set_log_file();
+ log_file_option(app, _log_file);
+ }
+
+ if (getenv("LIBTEST_SYSLOG") and has_syslog())
+ {
+ app.add_option("--syslog");
+ }
+
+ // Update pid_file
+ {
+ if (_pid_file.empty() and set_pid_file() == false)
+ {
+ return false;
+ }
+
+ pid_file_option(app, pid_file());
+ }
+
+ if (has_socket_file_option())
+ {
+ if (set_socket_file() == false)
+ {
+ return false;
+ }
+
+ socket_file_option(app, _socket);