1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 3 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #include <libtest/common.h>
36 static inline std::string
&rtrim(std::string
&s
)
38 s
.erase(std::find_if(s
.rbegin(), s
.rend(), std::not1(std::ptr_fun
<int, int>(std::isspace
))).base(), s
.end());
42 #include <libtest/server.h>
43 #include <libtest/stream.h>
44 #include <libtest/killpid.h>
48 std::ostream
& operator<<(std::ostream
& output
, const Server
&arg
)
52 output
<< arg
.hostname();
56 output
<< arg
.hostname() << ":" << arg
.port();
61 output
<< " Pid:" << arg
.pid();
66 output
<< " Socket:" << arg
.socket();
69 if (arg
.running().empty() == false)
71 output
<< " Exec:" << arg
.running();
74 return output
; // for multiple << operators
77 #define MAGIC_MEMORY 123570
79 Server::Server(const std::string
& host_arg
, const in_port_t port_arg
,
80 const std::string
& executable
, const bool _is_libtool
,
83 _is_socket(is_socket_arg
),
87 _app(executable
, _is_libtool
)
93 if (has_pid() and not kill(_pid
))
95 Error
<< "Unable to kill:" << *this;
98 Application::error_t ret
;
99 if (Application::SUCCESS
!= (ret
= _app
.wait()))
101 Error
<< "Application::wait() " << _running
<< " " << ret
<< ": " << _app
.stderr_result();
111 bool Server::validate()
113 return _magic
== MAGIC_MEMORY
;
116 // If the server exists, kill it
121 // Try to ping, and kill the server #limit number of times
124 is_pid_valid(current_pid
= get_pid()))
126 if (kill(current_pid
))
128 Log
<< "Killed existing server," << *this << " with pid:" << current_pid
;
134 // For whatever reason we could not kill it, and we reached limit
137 Error
<< "Reached limit, could not kill server pid:" << current_pid
;
144 bool Server::wait_for_pidfile() const
146 Wait
wait(pid_file(), 4);
148 return wait
.successful();
153 // If we find that we already have a pid then kill it.
154 if (has_pid() and kill(_pid
) == false)
156 Error
<< "Could not kill() existing server during start() pid:" << _pid
;
160 if (has_pid() == false)
162 fatal_message("has_pid() failed, programer error");
165 // This needs more work.
173 if (args(_app
) == false)
175 Error
<< "Could not build command()";
179 Application::error_t ret
;
180 if (Application::SUCCESS
!= (ret
= _app
.run()))
182 Error
<< "Application::run() " << ret
;
185 _running
= _app
.print();
187 if (valgrind_is_caller())
192 if (pid_file().empty() == false)
194 Wait
wait(pid_file(), 8);
196 if (wait
.successful() == false)
198 libtest::fatal(LIBYATL_DEFAULT_PARAM
,
199 "Unable to open pidfile for: %s",
207 uint32_t timeout
= 20; // This number should be high enough for valgrind startup (which is slow)
211 for (waited
= 0, retry
= 1; ; retry
++, waited
+= this_wait
)
213 if ((pinged
= ping()) == true)
217 else if (waited
>= timeout
)
222 this_wait
= retry
* retry
/ 3 + 1;
223 libtest::dream(this_wait
, 0);
229 // If we happen to have a pid file, lets try to kill it
230 if (pid_file().empty() == false)
232 if (kill_file(pid_file()) == false)
234 fatal_message("Failed to kill off server after startup occurred, when pinging failed");
236 Error
<< "Failed to ping(), waited:" << this_wait
237 << " server started, having pid_file. exec:" << _running
238 << " error:" << _app
.stderr_result();
242 Error
<< "Failed to ping() server started. exec:" << _running
;
248 // A failing get_pid() at this point is considered an error
254 void Server::reset_pid()
266 void Server::add_option(const std::string
& arg
)
268 _options
.push_back(std::make_pair(arg
, std::string()));
271 void Server::add_option(const std::string
& name
, const std::string
& value
)
273 _options
.push_back(std::make_pair(name
, value
));
276 bool Server::set_socket_file()
278 char file_buffer
[FILENAME_MAX
];
281 if (broken_pid_file())
283 snprintf(file_buffer
, sizeof(file_buffer
), "/tmp/%s.socketXXXXXX", name());
287 snprintf(file_buffer
, sizeof(file_buffer
), "var/run/%s.socketXXXXXX", name());
291 if ((fd
= mkstemp(file_buffer
)) == -1)
299 _socket
= file_buffer
;
304 bool Server::set_pid_file()
306 char file_buffer
[FILENAME_MAX
];
309 if (broken_pid_file())
311 snprintf(file_buffer
, sizeof(file_buffer
), "/tmp/%s.pidXXXXXX", name());
315 snprintf(file_buffer
, sizeof(file_buffer
), "var/run/%s.pidXXXXXX", name());
319 if ((fd
= mkstemp(file_buffer
)) == -1)
327 _pid_file
= file_buffer
;
332 bool Server::set_log_file()
334 char file_buffer
[FILENAME_MAX
];
337 snprintf(file_buffer
, sizeof(file_buffer
), "var/log/%s.logXXXXXX", name());
339 if ((fd
= mkstemp(file_buffer
)) == -1)
341 libtest::fatal(LIBYATL_DEFAULT_PARAM
, "mkstemp() failed on %s with %s", file_buffer
, strerror(errno
));
345 _log_file
= file_buffer
;
350 bool Server::args(Application
& app
)
353 // Set a log file if it was requested (and we can)
354 if (has_log_file_option())
357 log_file_option(app
, _log_file
);
360 if (getenv("LIBTEST_SYSLOG") and has_syslog())
362 app
.add_option("--syslog");
367 if (_pid_file
.empty() and set_pid_file() == false)
372 pid_file_option(app
, pid_file());
375 if (has_socket_file_option())
377 if (set_socket_file() == false)
382 socket_file_option(app
, _socket
);
385 if (has_port_option())
387 port_option(app
, _port
);
390 for (Options::const_iterator iter
= _options
.begin(); iter
!= _options
.end(); iter
++)
392 if ((*iter
).second
.empty() == false)
394 app
.add_option((*iter
).first
, (*iter
).second
);
398 app
.add_option((*iter
).first
);
405 bool Server::kill(pid_t pid_arg
)
407 if (check_pid(pid_arg
) and kill_pid(pid_arg
)) // If we kill it, reset
409 if (broken_pid_file() and pid_file().empty() == false)
411 unlink(pid_file().c_str());
414 if (broken_socket_cleanup() and has_socket() and not socket().empty())
416 unlink(socket().c_str());
427 } // namespace libtest