Add in better testing for gdb.
[m6w6/libmemcached] / libtest / server.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * libtest
4 *
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 *
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.
11 *
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.
16 *
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
20 */
21
22
23 #include <libtest/common.h>
24
25 #include <cassert>
26 #include <cerrno>
27 #include <cstdlib>
28 #include <iostream>
29
30 #include <algorithm>
31 #include <functional>
32 #include <locale>
33
34 // trim from end
35 static inline std::string &rtrim(std::string &s)
36 {
37 s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
38 return s;
39 }
40
41 #include <libtest/server.h>
42 #include <libtest/stream.h>
43 #include <libtest/killpid.h>
44
45 namespace libtest {
46
47 std::ostream& operator<<(std::ostream& output, const Server &arg)
48 {
49 if (arg.is_socket())
50 {
51 output << arg.hostname();
52 }
53 else
54 {
55 output << arg.hostname() << ":" << arg.port();
56 }
57
58 if (arg.has_pid())
59 {
60 output << " Pid:" << arg.pid();
61 }
62
63 if (arg.has_socket())
64 {
65 output << " Socket:" << arg.socket();
66 }
67
68 if (not arg.running().empty())
69 {
70 output << " Exec:" << arg.running();
71 }
72
73 return output; // for multiple << operators
74 }
75
76 Server::Server(const std::string& host_arg, const in_port_t port_arg, bool is_socket_arg) :
77 _is_socket(is_socket_arg),
78 _pid(-1),
79 _port(port_arg),
80 _hostname(host_arg)
81 {
82 }
83
84 Server::~Server()
85 {
86 if (has_pid() and not kill(_pid))
87 {
88 Error << "Unable to kill:" << *this;
89 }
90 }
91
92 // If the server exists, kill it
93 bool Server::cycle()
94 {
95 uint32_t limit= 3;
96
97 // Try to ping, and kill the server #limit number of times
98 pid_t current_pid;
99 while (--limit and is_pid_valid(current_pid= get_pid()))
100 {
101 if (kill(current_pid))
102 {
103 Log << "Killed existing server," << *this << " with pid:" << current_pid;
104 dream(0, 50000);
105 continue;
106 }
107 }
108
109 // For whatever reason we could not kill it, and we reached limit
110 if (limit == 0)
111 {
112 Error << "Reached limit, could not kill server pid:" << current_pid;
113 return false;
114 }
115
116 return true;
117 }
118
119 bool Server::wait_for_pidfile() const
120 {
121 Wait wait(pid_file(), 4);
122
123 return wait.successful();
124 }
125
126 bool Server::start()
127 {
128 // If we find that we already have a pid then kill it.
129 if (has_pid() and kill(_pid) == false)
130 {
131 Error << "Could not kill() existing server during start() pid:" << _pid;
132 return false;
133 }
134
135 if (has_pid() == false)
136 {
137 fatal_message("has_pid() failed, programer error");
138 }
139
140 Application app(executable(), is_libtool());
141
142 if (is_debug())
143 {
144 app.use_gdb();
145 }
146 else if (getenv("TESTS_ENVIRONMENT"))
147 {
148 if (strstr(getenv("TESTS_ENVIRONMENT"), "gdb"))
149 {
150 app.use_gdb();
151 }
152 }
153
154 if (args(app) == false)
155 {
156 Error << "Could not build command()";
157 return false;
158 }
159
160 Application::error_t ret;
161 if (Application::SUCCESS != (ret= app.run()))
162 {
163 Error << "Application::run() " << ret;
164 return false;
165 }
166 _running= app.print();
167
168 if (Application::SUCCESS != (ret= app.wait()))
169 {
170 Error << "Application::wait() " << app.print() << " " << ret;
171 return false;
172 }
173
174 if (is_helgrind() or is_valgrind())
175 {
176 dream(5, 50000);
177 }
178
179 if (pid_file().empty() == false)
180 {
181 Wait wait(pid_file(), 8);
182
183 if (not wait.successful())
184 {
185 Error << "Unable to open pidfile for: " << _running;
186 }
187 }
188
189 int counter= 0;
190 bool pinged= false;
191 while ((pinged= ping()) == false and
192 counter < (is_helgrind() or is_valgrind() ? 20 : 5))
193 {
194 dream(counter++, 50000);
195 }
196
197 if (pinged == false)
198 {
199 // If we happen to have a pid file, lets try to kill it
200 if (pid_file().empty() == false)
201 {
202 if (kill_file(pid_file()) == false)
203 {
204 fatal_message("Failed to kill off server after startup occurred, when pinging failed");
205 }
206 Error << "Failed to ping() server started, having pid_file. exec:" << _running;
207 }
208 else
209 {
210 Error << "Failed to ping() server started. exec:" << _running;
211 }
212 _running.clear();
213 return false;
214 }
215
216 // A failing get_pid() at this point is considered an error
217 _pid= get_pid(true);
218
219 return has_pid();
220 }
221
222 void Server::reset_pid()
223 {
224 _running.clear();
225 _pid_file.clear();
226 _pid= -1;
227 }
228
229 pid_t Server::pid()
230 {
231 return _pid;
232 }
233
234 void Server::add_option(const std::string& arg)
235 {
236 _options.push_back(std::make_pair(arg, std::string()));
237 }
238
239 void Server::add_option(const std::string& name, const std::string& value)
240 {
241 _options.push_back(std::make_pair(name, value));
242 }
243
244 bool Server::set_socket_file()
245 {
246 char file_buffer[FILENAME_MAX];
247 file_buffer[0]= 0;
248
249 if (broken_pid_file())
250 {
251 snprintf(file_buffer, sizeof(file_buffer), "/tmp/%s.socketXXXXXX", name());
252 }
253 else
254 {
255 snprintf(file_buffer, sizeof(file_buffer), "var/run/%s.socketXXXXXX", name());
256 }
257
258 int fd;
259 if ((fd= mkstemp(file_buffer)) == -1)
260 {
261 perror(file_buffer);
262 return false;
263 }
264 close(fd);
265 unlink(file_buffer);
266
267 _socket= file_buffer;
268
269 return true;
270 }
271
272 bool Server::set_pid_file()
273 {
274 char file_buffer[FILENAME_MAX];
275 file_buffer[0]= 0;
276
277 if (broken_pid_file())
278 {
279 snprintf(file_buffer, sizeof(file_buffer), "/tmp/%s.pidXXXXXX", name());
280 }
281 else
282 {
283 snprintf(file_buffer, sizeof(file_buffer), "var/run/%s.pidXXXXXX", name());
284 }
285
286 int fd;
287 if ((fd= mkstemp(file_buffer)) == -1)
288 {
289 perror(file_buffer);
290 return false;
291 }
292 close(fd);
293 unlink(file_buffer);
294
295 _pid_file= file_buffer;
296
297 return true;
298 }
299
300 bool Server::set_log_file()
301 {
302 char file_buffer[FILENAME_MAX];
303 file_buffer[0]= 0;
304
305 snprintf(file_buffer, sizeof(file_buffer), "var/log/%s.logXXXXXX", name());
306 int fd;
307 if ((fd= mkstemp(file_buffer)) == -1)
308 {
309 libtest::fatal(LIBYATL_DEFAULT_PARAM, "mkstemp() failed on %s with %s", file_buffer, strerror(errno));
310 }
311 close(fd);
312
313 _log_file= file_buffer;
314
315 return true;
316 }
317
318 bool Server::args(Application& app)
319 {
320
321 // Set a log file if it was requested (and we can)
322 if (has_log_file_option())
323 {
324 set_log_file();
325 log_file_option(app, _log_file);
326 }
327
328 if (getenv("LIBTEST_SYSLOG") and has_syslog())
329 {
330 app.add_option("--syslog");
331 }
332
333 // Update pid_file
334 {
335 if (_pid_file.empty() and set_pid_file() == false)
336 {
337 return false;
338 }
339
340 pid_file_option(app, pid_file());
341 }
342
343 assert(daemon_file_option());
344 if (daemon_file_option() and not is_valgrind() and not is_helgrind())
345 {
346 app.add_option(daemon_file_option());
347 }
348
349 if (has_socket_file_option())
350 {
351 if (set_socket_file() == false)
352 {
353 return false;
354 }
355
356 socket_file_option(app, _socket);
357 }
358
359 if (has_port_option())
360 {
361 port_option(app, _port);
362 }
363
364 for (Options::const_iterator iter= _options.begin(); iter != _options.end(); iter++)
365 {
366 if ((*iter).second.empty() == false)
367 {
368 app.add_option((*iter).first, (*iter).second);
369 }
370 else
371 {
372 app.add_option((*iter).first);
373 }
374 }
375
376 return true;
377 }
378
379 bool Server::is_debug() const
380 {
381 return bool(getenv("LIBTEST_MANUAL_GDB"));
382 }
383
384 bool Server::is_valgrind() const
385 {
386 return bool(getenv("LIBTEST_MANUAL_VALGRIND"));
387 }
388
389 bool Server::is_helgrind() const
390 {
391 return bool(getenv("LIBTEST_MANUAL_HELGRIND"));
392 }
393
394 bool Server::kill(pid_t pid_arg)
395 {
396 if (check_pid(pid_arg) and kill_pid(pid_arg)) // If we kill it, reset
397 {
398 if (broken_pid_file() and pid_file().empty() == false)
399 {
400 unlink(pid_file().c_str());
401 }
402
403 if (broken_socket_cleanup() and has_socket() and not socket().empty())
404 {
405 unlink(socket().c_str());
406 }
407
408 reset_pid();
409
410 return true;
411 }
412
413 return false;
414 }
415
416 } // namespace libtest