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
23 #include <libtest/common.h>
35 static inline std::string
&rtrim(std::string
&s
)
37 s
.erase(std::find_if(s
.rbegin(), s
.rend(), std::not1(std::ptr_fun
<int, int>(std::isspace
))).base(), s
.end());
41 #include <libtest/server.h>
42 #include <libtest/stream.h>
43 #include <libtest/killpid.h>
47 std::ostream
& operator<<(std::ostream
& output
, const Server
&arg
)
51 output
<< arg
.hostname();
55 output
<< arg
.hostname() << ":" << arg
.port();
60 output
<< " Pid:" << arg
.pid();
65 output
<< " Socket:" << arg
.socket();
68 if (not arg
.running().empty())
70 output
<< " Exec:" << arg
.running();
74 return output
; // for multiple << operators
77 Server::Server(const std::string
& host_arg
, const in_port_t port_arg
, bool is_socket_arg
) :
78 _is_socket(is_socket_arg
),
87 if (has_pid() and not kill(_pid
))
89 Error
<< "Unable to kill:" << *this;
93 // If the server exists, kill it
98 // Try to ping, and kill the server #limit number of times
100 while (--limit
and is_pid_valid(current_pid
= get_pid()))
102 if (kill(current_pid
))
104 Log
<< "Killed existing server," << *this << " with pid:" << current_pid
;
110 // For whatever reason we could not kill it, and we reached limit
113 Error
<< "Reached limit, could not kill server pid:" << current_pid
;
120 bool Server::wait_for_pidfile() const
122 Wait
wait(pid_file(), 4);
124 return wait
.successful();
129 // If we find that we already have a pid then kill it.
130 if (has_pid() and kill(_pid
) == false)
132 Error
<< "Could not kill() existing server during start() pid:" << _pid
;
135 assert(not has_pid());
137 Application
app(name(), is_libtool());
138 if (args(app
) == false)
140 Error
<< "Could not build command()";
144 if (Application::SUCCESS
!= app
.run())
146 Error
<< "Application::run()";
150 if (Application::SUCCESS
!= app
.wait())
152 Error
<< "Application::wait()";
156 if (is_helgrind() or is_valgrind())
161 if (pid_file().empty() == false)
163 Wait
wait(pid_file(), 8);
165 if (not wait
.successful())
167 Error
<< "Unable to open pidfile for: " << _running
;
173 while ((pinged
= ping()) == false and
174 counter
< (is_helgrind() or is_valgrind() ? 20 : 5))
176 dream(counter
++, 50000);
181 // If we happen to have a pid file, lets try to kill it
182 if (pid_file().empty() == false)
184 Error
<< "We are going to kill it off";
185 kill_file(pid_file());
187 Error
<< "Failed to ping() server started with:" << _running
;
192 // A failing get_pid() at this point is considered an error
198 void Server::reset_pid()
210 void Server::add_option(const std::string
& arg
)
212 _options
.push_back(std::make_pair(arg
, std::string()));
215 void Server::add_option(const std::string
& name
, const std::string
& value
)
217 _options
.push_back(std::make_pair(name
, value
));
220 bool Server::set_socket_file()
222 char file_buffer
[FILENAME_MAX
];
225 if (broken_pid_file())
227 snprintf(file_buffer
, sizeof(file_buffer
), "/tmp/%s.socketXXXXXX", name());
231 snprintf(file_buffer
, sizeof(file_buffer
), "var/run/%s.socketXXXXXX", name());
235 if ((fd
= mkstemp(file_buffer
)) == -1)
243 _socket
= file_buffer
;
248 bool Server::set_pid_file()
250 char file_buffer
[FILENAME_MAX
];
253 if (broken_pid_file())
255 snprintf(file_buffer
, sizeof(file_buffer
), "/tmp/%s.pidXXXXXX", name());
259 snprintf(file_buffer
, sizeof(file_buffer
), "var/run/%s.pidXXXXXX", name());
263 if ((fd
= mkstemp(file_buffer
)) == -1)
271 _pid_file
= file_buffer
;
276 bool Server::set_log_file()
278 char file_buffer
[FILENAME_MAX
];
281 snprintf(file_buffer
, sizeof(file_buffer
), "var/log/%s.logXXXXXX", name());
283 if ((fd
= mkstemp(file_buffer
)) == -1)
290 _log_file
= file_buffer
;
295 bool Server::args(Application
& app
)
298 // Set a log file if it was requested (and we can)
299 if (getenv("LIBTEST_LOG") and has_log_file_option())
301 if (not set_log_file())
306 log_file_option(app
, _log_file
);
309 if (getenv("LIBTEST_SYSLOG") and has_syslog())
311 app
.add_option("--syslog");
316 if (_pid_file
.empty() and set_pid_file() == false)
321 pid_file_option(app
, pid_file());
324 assert(daemon_file_option());
325 if (daemon_file_option() and not is_valgrind() and not is_helgrind())
327 app
.add_option(daemon_file_option());
330 if (_is_socket
and has_socket_file_option())
332 if (not set_socket_file())
337 socket_file_option(app
, _socket
);
340 if (has_port_option())
342 port_option(app
, _port
);
345 for (Options::const_iterator iter
= _options
.begin(); iter
!= _options
.end(); iter
++)
347 if ((*iter
).second
.empty() == false)
349 app
.add_option((*iter
).first
, (*iter
).second
);
353 app
.add_option((*iter
).first
);
360 bool Server::is_debug() const
362 return bool(getenv("LIBTEST_MANUAL_GDB"));
365 bool Server::is_valgrind() const
367 return bool(getenv("LIBTEST_MANUAL_VALGRIND"));
370 bool Server::is_helgrind() const
372 return bool(getenv("LIBTEST_MANUAL_HELGRIND"));
375 bool Server::kill(pid_t pid_arg
)
377 if (check_pid(pid_arg
) and kill_pid(pid_arg
)) // If we kill it, reset
379 if (broken_pid_file() and pid_file().empty() == false)
381 unlink(pid_file().c_str());
384 if (broken_socket_cleanup() and has_socket() and not socket().empty())
386 unlink(socket().c_str());
397 } // namespace libtest