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