+ // 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 after startup occurred, when pinging failed: %s stderr:%s",
+ pid_file().c_str(),
+ _app.stderr_c_str());
+ }
+
+ throw libtest::fatal(LIBYATL_DEFAULT_PARAM,
+ "Failed to ping(), waited: %u server started, having pid_file. exec: %s stderr:%s",
+ this_wait, _running.c_str(),
+ _app.stderr_c_str());
+ }
+ else
+ {
+ throw libtest::fatal(LIBYATL_DEFAULT_PARAM,
+ "Failed to ping() server started. exec: %s stderr:%s",
+ _running.c_str(),
+ _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 (false and 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);
+ }
+
+ if (has_port_option())
+ {
+ port_option(app, _port);
+ }
+
+ for (Options::const_iterator iter= _options.begin(); iter != _options.end(); iter++)
+ {
+ if ((*iter).second.empty() == false)
+ {
+ app.add_option((*iter).first, (*iter).second);
+ }
+ else
+ {
+ app.add_option((*iter).first);
+ }
+ }
+
+ return true;
+}
+
+bool Server::kill()
+{
+ if (check_pid(_app.pid())) // If we kill it, reset
+ {
+ _app.murder();
+ if (broken_pid_file() and pid_file().empty() == false)
+ {
+ unlink(pid_file().c_str());
+ }
+
+ if (broken_socket_cleanup() and has_socket() and not socket().empty())
+ {
+ unlink(socket().c_str());
+ }
+