da341f9528d5c08bebeea22524a3ec9adf695cf5
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
)
101 bool Server::validate()
103 return _magic
== MAGIC_MEMORY
;
106 // If the server exists, kill it
111 // Try to ping, and kill the server #limit number of times
113 is_pid_valid(_app
.pid()))
117 Log
<< "Killed existing server," << *this;
123 // For whatever reason we could not kill it, and we reached limit
126 Error
<< "Reached limit, could not kill server";
133 bool Server::wait_for_pidfile() const
135 Wait
wait(pid_file(), 4);
137 return wait
.successful();
140 bool Server::has_pid() const
148 // If we find that we already have a pid then kill it.
149 if (has_pid() == false)
151 fatal_message("has_pid() failed, programer error");
154 // This needs more work.
162 if (args(_app
) == false)
164 Error
<< "Could not build command()";
168 Application::error_t ret
;
169 if (Application::SUCCESS
!= (ret
= _app
.run()))
171 Error
<< "Application::run() " << ret
;
174 _running
= _app
.print();
176 if (valgrind_is_caller())
181 if (pid_file().empty() == false)
183 Wait
wait(pid_file(), 8);
185 if (wait
.successful() == false)
187 libtest::fatal(LIBYATL_DEFAULT_PARAM
,
188 "Unable to open pidfile for: %s",
196 uint32_t timeout
= 20; // This number should be high enough for valgrind startup (which is slow)
200 for (waited
= 0, retry
= 1; ; retry
++, waited
+= this_wait
)
202 if ((pinged
= ping()) == true)
206 else if (waited
>= timeout
)
211 this_wait
= retry
* retry
/ 3 + 1;
212 libtest::dream(this_wait
, 0);
218 // If we happen to have a pid file, lets try to kill it
219 if (pid_file().empty() == false)
221 if (kill_file(pid_file()) == false)
223 fatal_message("Failed to kill off server after startup occurred, when pinging failed");
225 Error
<< "Failed to ping(), waited:" << this_wait
226 << " server started, having pid_file. exec:" << _running
227 << " error:" << _app
.stderr_result();
231 Error
<< "Failed to ping() server started. exec:" << _running
;
237 // A failing get_pid() at this point is considered an error
243 void Server::reset_pid()
255 void Server::add_option(const std::string
& arg
)
257 _options
.push_back(std::make_pair(arg
, std::string()));
260 void Server::add_option(const std::string
& name
, const std::string
& value
)
262 _options
.push_back(std::make_pair(name
, value
));
265 bool Server::set_socket_file()
267 char file_buffer
[FILENAME_MAX
];
270 if (broken_pid_file())
272 snprintf(file_buffer
, sizeof(file_buffer
), "/tmp/%s.socketXXXXXX", name());
276 snprintf(file_buffer
, sizeof(file_buffer
), "var/run/%s.socketXXXXXX", name());
280 if ((fd
= mkstemp(file_buffer
)) == -1)
288 _socket
= file_buffer
;
293 bool Server::set_pid_file()
295 char file_buffer
[FILENAME_MAX
];
298 if (broken_pid_file())
300 snprintf(file_buffer
, sizeof(file_buffer
), "/tmp/%s.pidXXXXXX", name());
304 snprintf(file_buffer
, sizeof(file_buffer
), "var/run/%s.pidXXXXXX", name());
308 if ((fd
= mkstemp(file_buffer
)) == -1)
316 _pid_file
= file_buffer
;
321 bool Server::set_log_file()
323 char file_buffer
[FILENAME_MAX
];
326 snprintf(file_buffer
, sizeof(file_buffer
), "var/log/%s.logXXXXXX", name());
328 if ((fd
= mkstemp(file_buffer
)) == -1)
330 libtest::fatal(LIBYATL_DEFAULT_PARAM
, "mkstemp() failed on %s with %s", file_buffer
, strerror(errno
));
334 _log_file
= file_buffer
;
339 bool Server::args(Application
& app
)
342 // Set a log file if it was requested (and we can)
343 if (has_log_file_option())
346 log_file_option(app
, _log_file
);
349 if (getenv("LIBTEST_SYSLOG") and has_syslog())
351 app
.add_option("--syslog");
356 if (_pid_file
.empty() and set_pid_file() == false)
361 pid_file_option(app
, pid_file());
364 if (has_socket_file_option())
366 if (set_socket_file() == false)
371 socket_file_option(app
, _socket
);
374 if (has_port_option())
376 port_option(app
, _port
);
379 for (Options::const_iterator iter
= _options
.begin(); iter
!= _options
.end(); iter
++)
381 if ((*iter
).second
.empty() == false)
383 app
.add_option((*iter
).first
, (*iter
).second
);
387 app
.add_option((*iter
).first
);
396 if (check_pid(_app
.pid()) and kill_pid(_app
.pid())) // If we kill it, reset
398 if (broken_pid_file() and pid_file().empty() == false)
400 unlink(pid_file().c_str());
403 if (broken_socket_cleanup() and has_socket() and not socket().empty())
405 unlink(socket().c_str());
416 } // namespace libtest