+ uint32_t limit= 3;
+
+ // Try to ping, and kill the server #limit number of times
+ pid_t current_pid;
+ while (--limit and is_pid_valid(current_pid= get_pid()))
+ {
+ if (kill(current_pid))
+ {
+ Log << "Killed existing server," << *this << " with pid:" << current_pid;
+ dream(0, 50000);
+ continue;
+ }
+ }
+
+ // For whatever reason we could not kill it, and we reached limit
+ if (limit == 0)
+ {
+ Error << "Reached limit, could not kill server pid:" << current_pid;
+ return false;
+ }
+
+ return true;
+}
+
+bool Server::wait_for_pidfile() const
+{
+ Wait wait(pid_file(), 4);
+
+ return wait.successful();
+}
+
+bool Server::start()
+{
+ // If we find that we already have a pid then kill it.
+ if (has_pid() and kill(_pid) == false)
+ {
+ Error << "Could not kill() existing server during start() pid:" << _pid;
+ return false;
+ }
+
+ if (has_pid() == false)
+ {
+ fatal_message("has_pid() failed, programer error");
+ }
+
+ Application app(name(), is_libtool());
+ if (args(app) == false)
+ {
+ Error << "Could not build command()";
+ return false;
+ }
+
+ Application::error_t ret;
+ if (Application::SUCCESS != (ret= app.run()))
+ {
+ Error << "Application::run() " << ret;
+ return false;
+ }
+
+ if (Application::SUCCESS != (ret= app.wait()))
+ {
+ Error << "Application::wait() " << ret;
+ return false;
+ }
+
+ if (is_helgrind() or is_valgrind())
+ {
+ dream(5, 50000);
+ }
+
+ if (pid_file().empty() == false)
+ {
+ Wait wait(pid_file(), 8);
+
+ if (not wait.successful())
+ {
+ Error << "Unable to open pidfile for: " << _running;
+ }
+ }
+
+ int counter= 0;
+ bool pinged= false;
+ while ((pinged= ping()) == false and
+ counter < (is_helgrind() or is_valgrind() ? 20 : 5))
+ {
+ dream(counter++, 50000);
+ }
+
+ if (pinged == false)
+ {
+ // If we happen to have a pid file, lets try to kill it
+ if (pid_file().empty() == false)
+ {
+ Error << "We are going to kill it off";
+ kill_file(pid_file());
+ }
+ Error << "Failed to ping() server started with:" << _running;
+ _running.clear();
+ return false;
+ }
+
+ // A failing get_pid() at this point is considered an error
+ _pid= get_pid(true);
+
+ return has_pid();
+}
+
+void Server::reset_pid()
+{
+ _running.clear();
+ _pid_file.clear();