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
),
86 _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
142 return (_app
.pid() > 1);
148 // If we find that we already have a pid then kill it.
149 if (has_pid() == true)
151 fatal_message("has_pid() failed, programer error");
154 // This needs more work.
162 if (getenv("YATL_VALGRIND_SERVER"))
166 else if (args(_app
) == false)
168 Error
<< "Could not build command()";
172 Application::error_t ret
;
173 if (Application::SUCCESS
!= (ret
= _app
.run()))
175 Error
<< "Application::run() " << ret
;
178 _running
= _app
.print();
180 if (valgrind_is_caller())
185 if (pid_file().empty() == false)
187 Wait
wait(pid_file(), 8);
189 if (wait
.successful() == false)
191 throw libtest::fatal(LIBYATL_DEFAULT_PARAM
,
192 "Unable to open pidfile for: %s",
200 uint32_t timeout
= 20; // This number should be high enough for valgrind startup (which is slow)
204 for (waited
= 0, retry
= 1; ; retry
++, waited
+= this_wait
)
206 if ((pinged
= ping()) == true)
210 else if (waited
>= timeout
)
215 this_wait
= retry
* retry
/ 3 + 1;
216 libtest::dream(this_wait
, 0);
222 // If we happen to have a pid file, lets try to kill it
223 if (pid_file().empty() == false)
225 if (kill_file(pid_file()) == false)
227 throw libtest::fatal(LIBYATL_DEFAULT_PARAM
, "Failed to kill off server after startup occurred, when pinging failed: %s", pid_file().c_str());
229 Error
<< "Failed to ping(), waited:" << this_wait
230 << " server started, having pid_file. exec:" << _running
231 << " error:" << _app
.stderr_result();
235 Error
<< "Failed to ping() server started. exec:" << _running
;
244 void Server::reset_pid()
250 pid_t
Server::pid() const
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 throw 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 (false and 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())) // If we kill it, reset
399 if (broken_pid_file() and pid_file().empty() == false)
401 unlink(pid_file().c_str());
404 if (broken_socket_cleanup() and has_socket() and not socket().empty())
406 unlink(socket().c_str());
417 } // namespace libtest