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())
188 if (pid_file().empty() == false)
190 Wait
wait(pid_file(), 8);
192 if (wait
.successful() == false)
199 throw libtest::fatal(LIBYATL_DEFAULT_PARAM
,
200 "Unable to open pidfile for: %s",
209 uint32_t timeout
= 20; // This number should be high enough for valgrind startup (which is slow)
213 for (waited
= 0, retry
= 1; ; retry
++, waited
+= this_wait
)
215 if ((pinged
= ping()) == true)
219 else if (waited
>= timeout
)
224 this_wait
= retry
* retry
/ 3 + 1;
225 libtest::dream(this_wait
, 0);
231 // If we happen to have a pid file, lets try to kill it
232 if ((pid_file().empty() == false) and (access(pid_file().c_str(), R_OK
) == 0))
234 if (kill_file(pid_file()) == false)
236 throw libtest::fatal(LIBYATL_DEFAULT_PARAM
, "Failed to kill off server after startup occurred, when pinging failed: %s", pid_file().c_str());
239 throw libtest::fatal(LIBYATL_DEFAULT_PARAM
,
240 "Failed to ping(), waited: %u server started, having pid_file. exec: %s error:%s",
241 this_wait
, _running
.c_str(), _app
.stderr_c_str());
245 throw libtest::fatal(LIBYATL_DEFAULT_PARAM
,
246 "Failed to ping() server started. exec: %s",
256 void Server::reset_pid()
262 pid_t
Server::pid() const
267 void Server::add_option(const std::string
& arg
)
269 _options
.push_back(std::make_pair(arg
, std::string()));
272 void Server::add_option(const std::string
& name
, const std::string
& value
)
274 _options
.push_back(std::make_pair(name
, value
));
277 bool Server::set_socket_file()
279 char file_buffer
[FILENAME_MAX
];
282 if (broken_pid_file())
284 snprintf(file_buffer
, sizeof(file_buffer
), "/tmp/%s.socketXXXXXX", name());
288 snprintf(file_buffer
, sizeof(file_buffer
), "var/run/%s.socketXXXXXX", name());
292 if ((fd
= mkstemp(file_buffer
)) == -1)
300 _socket
= file_buffer
;
305 bool Server::set_pid_file()
307 char file_buffer
[FILENAME_MAX
];
310 if (broken_pid_file())
312 snprintf(file_buffer
, sizeof(file_buffer
), "/tmp/%s.pidXXXXXX", name());
316 snprintf(file_buffer
, sizeof(file_buffer
), "var/run/%s.pidXXXXXX", name());
320 if ((fd
= mkstemp(file_buffer
)) == -1)
328 _pid_file
= file_buffer
;
333 bool Server::set_log_file()
335 char file_buffer
[FILENAME_MAX
];
338 snprintf(file_buffer
, sizeof(file_buffer
), "var/log/%s.logXXXXXX", name());
340 if ((fd
= mkstemp(file_buffer
)) == -1)
342 throw libtest::fatal(LIBYATL_DEFAULT_PARAM
, "mkstemp() failed on %s with %s", file_buffer
, strerror(errno
));
346 _log_file
= file_buffer
;
351 bool Server::args(Application
& app
)
354 // Set a log file if it was requested (and we can)
355 if (false and has_log_file_option())
358 log_file_option(app
, _log_file
);
361 if (getenv("LIBTEST_SYSLOG") and has_syslog())
363 app
.add_option("--syslog");
368 if (_pid_file
.empty() and set_pid_file() == false)
373 pid_file_option(app
, pid_file());
376 if (has_socket_file_option())
378 if (set_socket_file() == false)
383 socket_file_option(app
, _socket
);
386 if (has_port_option())
388 port_option(app
, _port
);
391 for (Options::const_iterator iter
= _options
.begin(); iter
!= _options
.end(); iter
++)
393 if ((*iter
).second
.empty() == false)
395 app
.add_option((*iter
).first
, (*iter
).second
);
399 app
.add_option((*iter
).first
);
408 if (check_pid(_app
.pid())) // If we kill it, reset
411 if (broken_pid_file() and pid_file().empty() == false)
413 unlink(pid_file().c_str());
416 if (broken_socket_cleanup() and has_socket() and not socket().empty())
418 unlink(socket().c_str());
429 } // namespace libtest