66575e74300725dd93b9327be6e339d3682cb649
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();
73 return output
; // for multiple << operators
76 Server::Server(const std::string
& host_arg
, const in_port_t port_arg
, bool is_socket_arg
) :
77 _is_socket(is_socket_arg
),
86 if (has_pid() and not kill(_pid
))
88 Error
<< "Unable to kill:" << *this;
92 // If the server exists, kill it
97 // Try to ping, and kill the server #limit number of times
99 while (--limit
and is_pid_valid(current_pid
= get_pid()))
101 if (kill(current_pid
))
103 Log
<< "Killed existing server," << *this << " with pid:" << current_pid
;
109 // For whatever reason we could not kill it, and we reached limit
112 Error
<< "Reached limit, could not kill server pid:" << current_pid
;
119 bool Server::wait_for_pidfile() const
121 Wait
wait(pid_file(), 4);
123 return wait
.successful();
128 // If we find that we already have a pid then kill it.
129 if (has_pid() and kill(_pid
) == false)
131 Error
<< "Could not kill() existing server during start() pid:" << _pid
;
135 if (has_pid() == false)
137 fatal_message("has_pid() failed, programer error");
140 Application
app(executable(), is_libtool());
141 if (args(app
) == false)
143 Error
<< "Could not build command()";
147 Application::error_t ret
;
148 if (Application::SUCCESS
!= (ret
= app
.run()))
150 Error
<< "Application::run() " << ret
;
153 _running
= app
.print();
155 if (Application::SUCCESS
!= (ret
= app
.wait()))
157 Error
<< "Application::wait() " << app
.print() << " " << ret
;
161 if (is_helgrind() or is_valgrind())
166 if (pid_file().empty() == false)
168 Wait
wait(pid_file(), 8);
170 if (not wait
.successful())
172 Error
<< "Unable to open pidfile for: " << _running
;
178 while ((pinged
= ping()) == false and
179 counter
< (is_helgrind() or is_valgrind() ? 20 : 5))
181 dream(counter
++, 50000);
186 // If we happen to have a pid file, lets try to kill it
187 if (pid_file().empty() == false)
189 Error
<< "We are going to kill it off";
190 kill_file(pid_file());
192 Error
<< "Failed to ping() server started with:" << _running
;
197 // A failing get_pid() at this point is considered an error
203 void Server::reset_pid()
215 void Server::add_option(const std::string
& arg
)
217 _options
.push_back(std::make_pair(arg
, std::string()));
220 void Server::add_option(const std::string
& name
, const std::string
& value
)
222 _options
.push_back(std::make_pair(name
, value
));
225 bool Server::set_socket_file()
227 char file_buffer
[FILENAME_MAX
];
230 if (broken_pid_file())
232 snprintf(file_buffer
, sizeof(file_buffer
), "/tmp/%s.socketXXXXXX", name());
236 snprintf(file_buffer
, sizeof(file_buffer
), "var/run/%s.socketXXXXXX", name());
240 if ((fd
= mkstemp(file_buffer
)) == -1)
248 _socket
= file_buffer
;
253 bool Server::set_pid_file()
255 char file_buffer
[FILENAME_MAX
];
258 if (broken_pid_file())
260 snprintf(file_buffer
, sizeof(file_buffer
), "/tmp/%s.pidXXXXXX", name());
264 snprintf(file_buffer
, sizeof(file_buffer
), "var/run/%s.pidXXXXXX", name());
268 if ((fd
= mkstemp(file_buffer
)) == -1)
276 _pid_file
= file_buffer
;
281 bool Server::set_log_file()
283 char file_buffer
[FILENAME_MAX
];
286 snprintf(file_buffer
, sizeof(file_buffer
), "var/log/%s.logXXXXXX", name());
288 if ((fd
= mkstemp(file_buffer
)) == -1)
295 _log_file
= file_buffer
;
300 bool Server::args(Application
& app
)
303 // Set a log file if it was requested (and we can)
304 if (getenv("LIBTEST_LOG") and has_log_file_option())
306 if (not set_log_file())
311 log_file_option(app
, _log_file
);
314 if (getenv("LIBTEST_SYSLOG") and has_syslog())
316 app
.add_option("--syslog");
321 if (_pid_file
.empty() and set_pid_file() == false)
326 pid_file_option(app
, pid_file());
329 assert(daemon_file_option());
330 if (daemon_file_option() and not is_valgrind() and not is_helgrind())
332 app
.add_option(daemon_file_option());
335 if (has_socket_file_option())
337 if (set_socket_file() == false)
342 socket_file_option(app
, _socket
);
345 if (has_port_option())
347 port_option(app
, _port
);
350 for (Options::const_iterator iter
= _options
.begin(); iter
!= _options
.end(); iter
++)
352 if ((*iter
).second
.empty() == false)
354 app
.add_option((*iter
).first
, (*iter
).second
);
358 app
.add_option((*iter
).first
);
365 bool Server::is_debug() const
367 return bool(getenv("LIBTEST_MANUAL_GDB"));
370 bool Server::is_valgrind() const
372 return bool(getenv("LIBTEST_MANUAL_VALGRIND"));
375 bool Server::is_helgrind() const
377 return bool(getenv("LIBTEST_MANUAL_HELGRIND"));
380 bool Server::kill(pid_t pid_arg
)
382 if (check_pid(pid_arg
) and kill_pid(pid_arg
)) // If we kill it, reset
384 if (broken_pid_file() and pid_file().empty() == false)
386 unlink(pid_file().c_str());
389 if (broken_socket_cleanup() and has_socket() and not socket().empty())
391 unlink(socket().c_str());
402 } // namespace libtest