Fix packaging issues.
[m6w6/libmemcached] / libtest / server.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Data Differential YATL (i.e. libtest) library
4 *
5 * Copyright (C) 2012 Data Differential, http://datadifferential.com/
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following disclaimer
16 * in the documentation and/or other materials provided with the
17 * distribution.
18 *
19 * * The names of its contributors may not be used to endorse or
20 * promote products derived from this software without specific prior
21 * written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *
35 */
36
37 #include <config.h>
38 #include <libtest/common.h>
39
40 #include <cassert>
41 #include <cerrno>
42 #include <climits>
43 #include <cstdlib>
44 #include <iostream>
45
46 #include <algorithm>
47 #include <functional>
48 #include <locale>
49 #include <unistd.h>
50
51 // trim from end
52 static inline std::string &rtrim(std::string &s)
53 {
54 s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
55 return s;
56 }
57
58 #include <libtest/server.h>
59 #include <libtest/stream.h>
60 #include <libtest/killpid.h>
61
62 namespace libtest {
63
64 std::ostream& operator<<(std::ostream& output, const Server &arg)
65 {
66 if (arg.is_socket())
67 {
68 output << arg.hostname();
69 }
70 else
71 {
72 output << arg.hostname() << ":" << arg.port();
73 }
74
75 if (arg.has_pid())
76 {
77 output << " Pid:" << arg.pid();
78 }
79
80 if (arg.has_socket())
81 {
82 output << " Socket:" << arg.socket();
83 }
84
85 if (arg.running().empty() == false)
86 {
87 output << " Exec:" << arg.running();
88 }
89
90 return output; // for multiple << operators
91 }
92
93 #define MAGIC_MEMORY 123570
94
95 Server::Server(const std::string& host_arg, const in_port_t port_arg,
96 const std::string& executable, const bool _is_libtool,
97 bool is_socket_arg) :
98 _magic(MAGIC_MEMORY),
99 _is_socket(is_socket_arg),
100 _port(port_arg),
101 _hostname(host_arg),
102 _app(executable, _is_libtool)
103 {
104 }
105
106 Server::~Server()
107 {
108 }
109
110 bool Server::check()
111 {
112 _app.slurp();
113 _app.check();
114 return true;
115 }
116
117 bool Server::validate()
118 {
119 return _magic == MAGIC_MEMORY;
120 }
121
122 // If the server exists, kill it
123 bool Server::cycle()
124 {
125 uint32_t limit= 3;
126
127 // Try to ping, and kill the server #limit number of times
128 while (--limit and
129 is_pid_valid(_app.pid()))
130 {
131 if (kill())
132 {
133 Log << "Killed existing server," << *this;
134 dream(0, 50000);
135 continue;
136 }
137 }
138
139 // For whatever reason we could not kill it, and we reached limit
140 if (limit == 0)
141 {
142 Error << "Reached limit, could not kill server";
143 return false;
144 }
145
146 return true;
147 }
148
149 bool Server::wait_for_pidfile() const
150 {
151 if (has_pid_file())
152 {
153 Wait wait(pid_file(), 4);
154
155 return wait.successful();
156 }
157
158 return true;
159 }
160
161 bool Server::has_pid() const
162 {
163 return (_app.pid() > 1);
164 }
165
166
167 bool Server::start()
168 {
169 // If we find that we already have a pid then kill it.
170 if (has_pid() == true)
171 {
172 #if 0
173 fatal_message("has_pid() failed, programer error");
174 #endif
175 }
176
177 // This needs more work.
178 #if 0
179 if (gdb_is_caller())
180 {
181 _app.use_gdb();
182 }
183 #endif
184
185 if (getenv("YATL_PTRCHECK_SERVER"))
186 {
187 _app.use_ptrcheck();
188 }
189 else if (getenv("YATL_VALGRIND_SERVER"))
190 {
191 _app.use_valgrind();
192 }
193
194 if (args(_app) == false)
195 {
196 Error << "Could not build command()";
197 return false;
198 }
199
200 libtest::release_port(_port);
201 Application::error_t ret;
202 if (Application::SUCCESS != (ret= _app.run()))
203 {
204 Error << "Application::run() " << ret;
205 return false;
206 }
207 _running= _app.print();
208
209 if (valgrind_is_caller())
210 {
211 dream(5, 50000);
212 }
213
214 _app.slurp();
215 if (has_pid_file())
216 {
217 size_t repeat= 5;
218 while (--repeat)
219 {
220 if (pid_file().empty() == false)
221 {
222 Wait wait(pid_file(), 8);
223
224 if (wait.successful() == false)
225 {
226 if (_app.check())
227 {
228 _app.slurp();
229 continue;
230 }
231
232 char buf[PATH_MAX];
233 char *getcwd_buf= getcwd(buf, sizeof(buf));
234 throw libtest::fatal(LIBYATL_DEFAULT_PARAM,
235 "Unable to open pidfile in %s for: %s stderr:%s",
236 getcwd_buf ? getcwd_buf : "",
237 _running.c_str(),
238 _app.stderr_c_str());
239 }
240 }
241 }
242 }
243
244 uint32_t this_wait= 0;
245 bool pinged= false;
246 {
247 uint32_t timeout= 20; // This number should be high enough for valgrind startup (which is slow)
248 uint32_t waited;
249 uint32_t retry;
250
251 for (waited= 0, retry= 1; ; retry++, waited+= this_wait)
252 {
253 if ((pinged= ping()) == true)
254 {
255 break;
256 }
257 else if (waited >= timeout)
258 {
259 break;
260 }
261
262 this_wait= retry * retry / 3 + 1;
263 libtest::dream(this_wait, 0);
264 }
265 }
266
267 if (pinged == false)
268 {
269 // If we happen to have a pid file, lets try to kill it
270 if ((pid_file().empty() == false) and (access(pid_file().c_str(), R_OK) == 0))
271 {
272 _app.slurp();
273 if (kill_file(pid_file()) == false)
274 {
275 throw libtest::fatal(LIBYATL_DEFAULT_PARAM,
276 "Failed to kill off server, waited: %u after startup occurred, when pinging failed: %.*s stderr:%.*s",
277 this_wait,
278 int(_running.size()), _running.c_str(),
279 int(_app.stderr_result_length()), _app.stderr_c_str());
280 }
281
282 throw libtest::fatal(LIBYATL_DEFAULT_PARAM,
283 "Failed native ping(), pid: %d is alive: %s waited: %u server started, having pid_file. exec: %.*s stderr:%.*s",
284 int(_app.pid()),
285 _app.check() ? "true" : "false",
286 this_wait,
287 int(_running.size()), _running.c_str(),
288 int(_app.stderr_result_length()), _app.stderr_c_str());
289 }
290 else
291 {
292 throw libtest::fatal(LIBYATL_DEFAULT_PARAM,
293 "Failed native ping(), pid: %d is alive: %s waited: %u server started. exec: %.*s stderr:%.*s",
294 int(_app.pid()),
295 _app.check() ? "true" : "false",
296 this_wait,
297 int(_running.size()), _running.c_str(),
298 int(_app.stderr_result_length()), _app.stderr_c_str());
299 }
300 _running.clear();
301 return false;
302 }
303
304 return has_pid();
305 }
306
307 void Server::reset_pid()
308 {
309 _running.clear();
310 _pid_file.clear();
311 }
312
313 pid_t Server::pid() const
314 {
315 return _app.pid();
316 }
317
318 void Server::add_option(const std::string& arg)
319 {
320 _options.push_back(std::make_pair(arg, std::string()));
321 }
322
323 void Server::add_option(const std::string& name, const std::string& value)
324 {
325 _options.push_back(std::make_pair(name, value));
326 }
327
328 bool Server::set_socket_file()
329 {
330 char file_buffer[FILENAME_MAX];
331 file_buffer[0]= 0;
332
333 if (broken_pid_file())
334 {
335 snprintf(file_buffer, sizeof(file_buffer), "/tmp/%s.socketXXXXXX", name());
336 }
337 else
338 {
339 snprintf(file_buffer, sizeof(file_buffer), "var/run/%s.socketXXXXXX", name());
340 }
341
342 int fd;
343 if ((fd= mkstemp(file_buffer)) == -1)
344 {
345 perror(file_buffer);
346 return false;
347 }
348 close(fd);
349 unlink(file_buffer);
350
351 _socket= file_buffer;
352
353 return true;
354 }
355
356 bool Server::set_pid_file()
357 {
358 char file_buffer[FILENAME_MAX];
359 file_buffer[0]= 0;
360
361 if (broken_pid_file())
362 {
363 snprintf(file_buffer, sizeof(file_buffer), "/tmp/%s.pidXXXXXX", name());
364 }
365 else
366 {
367 snprintf(file_buffer, sizeof(file_buffer), "var/run/%s.pidXXXXXX", name());
368 }
369
370 int fd;
371 if ((fd= mkstemp(file_buffer)) == -1)
372 {
373 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "mkstemp() failed on %s with %s", file_buffer, strerror(errno));
374 }
375 close(fd);
376 unlink(file_buffer);
377
378 _pid_file= file_buffer;
379
380 return true;
381 }
382
383 bool Server::set_log_file()
384 {
385 char file_buffer[FILENAME_MAX];
386 file_buffer[0]= 0;
387
388 snprintf(file_buffer, sizeof(file_buffer), "var/log/%s.logXXXXXX", name());
389 int fd;
390 if ((fd= mkstemp(file_buffer)) == -1)
391 {
392 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "mkstemp() failed on %s with %s", file_buffer, strerror(errno));
393 }
394 close(fd);
395
396 _log_file= file_buffer;
397
398 return true;
399 }
400
401 bool Server::args(Application& app)
402 {
403
404 // Set a log file if it was requested (and we can)
405 if (has_log_file_option())
406 {
407 set_log_file();
408 log_file_option(app, _log_file);
409 }
410
411 if (getenv("LIBTEST_SYSLOG") and has_syslog())
412 {
413 app.add_option("--syslog");
414 }
415
416 // Update pid_file
417 if (has_pid_file())
418 {
419 if (_pid_file.empty() and set_pid_file() == false)
420 {
421 return false;
422 }
423
424 pid_file_option(app, pid_file());
425 }
426
427 if (has_socket_file_option())
428 {
429 if (set_socket_file() == false)
430 {
431 return false;
432 }
433
434 socket_file_option(app, _socket);
435 }
436
437 if (has_port_option())
438 {
439 port_option(app, _port);
440 }
441
442 for (Options::const_iterator iter= _options.begin(); iter != _options.end(); iter++)
443 {
444 if ((*iter).second.empty() == false)
445 {
446 app.add_option((*iter).first, (*iter).second);
447 }
448 else
449 {
450 app.add_option((*iter).first);
451 }
452 }
453
454 return true;
455 }
456
457 bool Server::kill()
458 {
459 if (check_pid(_app.pid())) // If we kill it, reset
460 {
461 _app.murder();
462 if (broken_pid_file() and pid_file().empty() == false)
463 {
464 unlink(pid_file().c_str());
465 }
466
467 if (broken_socket_cleanup() and has_socket() and not socket().empty())
468 {
469 unlink(socket().c_str());
470 }
471
472 reset_pid();
473
474 return true;
475 }
476
477 return false;
478 }
479
480 } // namespace libtest