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>
38 static inline std::string
&rtrim(std::string
&s
)
40 s
.erase(std::find_if(s
.rbegin(), s
.rend(), std::not1(std::ptr_fun
<int, int>(std::isspace
))).base(), s
.end());
44 #include <libtest/server.h>
45 #include <libtest/stream.h>
46 #include <libtest/killpid.h>
50 std::ostream
& operator<<(std::ostream
& output
, const Server
&arg
)
54 output
<< arg
.hostname();
58 output
<< arg
.hostname() << ":" << arg
.port();
63 output
<< " Pid:" << arg
.pid();
68 output
<< " Socket:" << arg
.socket();
71 if (arg
.running().empty() == false)
73 output
<< " Exec:" << arg
.running();
76 return output
; // for multiple << operators
79 #define MAGIC_MEMORY 123570
81 Server::Server(const std::string
& host_arg
, const in_port_t port_arg
,
82 const std::string
& executable
, const bool _is_libtool
,
85 _is_socket(is_socket_arg
),
88 _app(executable
, _is_libtool
)
103 bool Server::validate()
105 return _magic
== MAGIC_MEMORY
;
108 // If the server exists, kill it
113 // Try to ping, and kill the server #limit number of times
115 is_pid_valid(_app
.pid()))
119 Log
<< "Killed existing server," << *this;
125 // For whatever reason we could not kill it, and we reached limit
128 Error
<< "Reached limit, could not kill server";
135 bool Server::wait_for_pidfile() const
137 Wait
wait(pid_file(), 4);
139 return wait
.successful();
142 bool Server::has_pid() const
144 return (_app
.pid() > 1);
150 // If we find that we already have a pid then kill it.
151 if (has_pid() == true)
153 fatal_message("has_pid() failed, programer error");
156 // This needs more work.
164 if (getenv("YATL_PTRCHECK_SERVER"))
168 else if (getenv("YATL_VALGRIND_SERVER"))
173 if (args(_app
) == false)
175 Error
<< "Could not build command()";
179 Application::error_t ret
;
180 if (Application::SUCCESS
!= (ret
= _app
.run()))
182 Error
<< "Application::run() " << ret
;
185 _running
= _app
.print();
187 if (valgrind_is_caller())
196 if (pid_file().empty() == false)
198 Wait
wait(pid_file(), 8);
200 if (wait
.successful() == false)
208 getcwd(buf
, sizeof(buf
));
209 throw libtest::fatal(LIBYATL_DEFAULT_PARAM
,
210 "Unable to open pidfile in %s for: %s stderr:%s",
213 _app
.stderr_c_str());
221 uint32_t timeout
= 20; // This number should be high enough for valgrind startup (which is slow)
225 for (waited
= 0, retry
= 1; ; retry
++, waited
+= this_wait
)
227 if ((pinged
= ping()) == true)
231 else if (waited
>= timeout
)
236 this_wait
= retry
* retry
/ 3 + 1;
237 libtest::dream(this_wait
, 0);
243 // If we happen to have a pid file, lets try to kill it
244 if ((pid_file().empty() == false) and (access(pid_file().c_str(), R_OK
) == 0))
247 if (kill_file(pid_file()) == false)
249 throw libtest::fatal(LIBYATL_DEFAULT_PARAM
,
250 "Failed to kill off server after startup occurred, when pinging failed: %s stderr:%s",
252 _app
.stderr_c_str());
255 throw libtest::fatal(LIBYATL_DEFAULT_PARAM
,
256 "Failed to ping(), waited: %u server started, having pid_file. exec: %s stderr:%s",
257 this_wait
, _running
.c_str(),
258 _app
.stderr_c_str());
262 throw libtest::fatal(LIBYATL_DEFAULT_PARAM
,
263 "Failed to ping() server started. exec: %s stderr:%s",
265 _app
.stderr_c_str());
274 void Server::reset_pid()
280 pid_t
Server::pid() const
285 void Server::add_option(const std::string
& arg
)
287 _options
.push_back(std::make_pair(arg
, std::string()));
290 void Server::add_option(const std::string
& name
, const std::string
& value
)
292 _options
.push_back(std::make_pair(name
, value
));
295 bool Server::set_socket_file()
297 char file_buffer
[FILENAME_MAX
];
300 if (broken_pid_file())
302 snprintf(file_buffer
, sizeof(file_buffer
), "/tmp/%s.socketXXXXXX", name());
306 snprintf(file_buffer
, sizeof(file_buffer
), "var/run/%s.socketXXXXXX", name());
310 if ((fd
= mkstemp(file_buffer
)) == -1)
318 _socket
= file_buffer
;
323 bool Server::set_pid_file()
325 char file_buffer
[FILENAME_MAX
];
328 if (broken_pid_file())
330 snprintf(file_buffer
, sizeof(file_buffer
), "/tmp/%s.pidXXXXXX", name());
334 snprintf(file_buffer
, sizeof(file_buffer
), "var/run/%s.pidXXXXXX", name());
338 if ((fd
= mkstemp(file_buffer
)) == -1)
340 throw libtest::fatal(LIBYATL_DEFAULT_PARAM
, "mkstemp() failed on %s with %s", file_buffer
, strerror(errno
));
345 _pid_file
= file_buffer
;
350 bool Server::set_log_file()
352 char file_buffer
[FILENAME_MAX
];
355 snprintf(file_buffer
, sizeof(file_buffer
), "var/log/%s.logXXXXXX", name());
357 if ((fd
= mkstemp(file_buffer
)) == -1)
359 throw libtest::fatal(LIBYATL_DEFAULT_PARAM
, "mkstemp() failed on %s with %s", file_buffer
, strerror(errno
));
363 _log_file
= file_buffer
;
368 bool Server::args(Application
& app
)
371 // Set a log file if it was requested (and we can)
372 if (false and has_log_file_option())
375 log_file_option(app
, _log_file
);
378 if (getenv("LIBTEST_SYSLOG") and has_syslog())
380 app
.add_option("--syslog");
385 if (_pid_file
.empty() and set_pid_file() == false)
390 pid_file_option(app
, pid_file());
393 if (has_socket_file_option())
395 if (set_socket_file() == false)
400 socket_file_option(app
, _socket
);
403 if (has_port_option())
405 port_option(app
, _port
);
408 for (Options::const_iterator iter
= _options
.begin(); iter
!= _options
.end(); iter
++)
410 if ((*iter
).second
.empty() == false)
412 app
.add_option((*iter
).first
, (*iter
).second
);
416 app
.add_option((*iter
).first
);
425 if (check_pid(_app
.pid())) // If we kill it, reset
428 if (broken_pid_file() and pid_file().empty() == false)
430 unlink(pid_file().c_str());
433 if (broken_socket_cleanup() and has_socket() and not socket().empty())
435 unlink(socket().c_str());
446 } // namespace libtest