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