d024d054ab02e4e6d1e284c7a601930f49f8238e
[awesomized/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 "libtest/yatlcon.h"
38
39 #include <libtest/common.h>
40
41 #include <cassert>
42 #include <cerrno>
43 #include <climits>
44 #include <cstdlib>
45 #include <iostream>
46
47 #include <algorithm>
48 #include <functional>
49 #include <locale>
50 #include <unistd.h>
51
52 // trim from end
53 static inline std::string &rtrim(std::string &s)
54 {
55 s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
56 return s;
57 }
58
59 #include <libtest/server.h>
60 #include <libtest/stream.h>
61 #include <libtest/killpid.h>
62
63 namespace libtest {
64
65 std::ostream& operator<<(std::ostream& output, const Server &arg)
66 {
67 if (arg.is_socket())
68 {
69 output << arg.hostname();
70 }
71 else
72 {
73 output << arg.hostname() << ":" << arg.port();
74 }
75
76 if (arg.has_pid())
77 {
78 output << " Pid:" << arg.pid();
79 }
80
81 if (arg.has_socket())
82 {
83 output << " Socket:" << arg.socket();
84 }
85
86 if (arg.running().empty() == false)
87 {
88 output << " Exec:" << arg.running();
89 }
90
91 return output; // for multiple << operators
92 }
93
94 #ifdef __GLIBC__
95 namespace {
96
97 class Buffer
98 {
99 public:
100 Buffer(char *b) : b_(b) {}
101 ~Buffer() { if (b_) free(b_); }
102 char* buf() { return b_; }
103 private:
104 char *b_;
105 };
106
107 }
108 #endif // __GLIBC__
109
110 #define MAGIC_MEMORY 123570
111
112 Server::Server(const std::string& host_arg, const in_port_t port_arg,
113 const std::string& executable, const bool _is_libtool,
114 bool is_socket_arg) :
115 _magic(MAGIC_MEMORY),
116 _is_socket(is_socket_arg),
117 _port(port_arg),
118 _hostname(host_arg),
119 _app(executable, _is_libtool),
120 out_of_ban_killed_(false),
121 _timeout(40)
122 {
123 }
124
125 Server::~Server()
126 {
127 kill();
128 }
129
130 bool Server::check()
131 {
132 _app.slurp();
133 _app.check();
134 return true;
135 }
136
137 bool Server::validate()
138 {
139 return _magic == MAGIC_MEMORY;
140 }
141
142 // If the server exists, kill it
143 bool Server::cycle()
144 {
145 uint32_t limit= 3;
146
147 // Try to ping, and kill the server #limit number of times
148 while (--limit and
149 is_pid_valid(_app.pid()))
150 {
151 if (kill())
152 {
153 Log << "Killed existing server," << *this;
154 dream(0, 50000);
155 continue;
156 }
157 }
158
159 // For whatever reason we could not kill it, and we reached limit
160 if (limit == 0)
161 {
162 Error << "Reached limit, could not kill server";
163 return false;
164 }
165
166 return true;
167 }
168
169 bool Server::wait_for_pidfile() const
170 {
171 Wait wait(pid_file(), 4);
172
173 return wait.successful();
174 }
175
176 bool Server::init(const char *argv[])
177 {
178 if (argv)
179 {
180 for (const char **ptr= argv; *ptr ; ++ptr)
181 {
182 if (ptr)
183 {
184 add_option(*ptr);
185 }
186 }
187 }
188
189 return build();
190 }
191
192 bool Server::has_pid() const
193 {
194 return (_app.pid() > 1);
195 }
196
197
198 bool Server::start()
199 {
200 // If we find that we already have a pid then kill it.
201 if (has_pid() == true)
202 {
203 #if 0
204 fatal_message("has_pid() failed, programer error");
205 #endif
206 }
207
208 if (getenv("YATL_GDB_SERVER"))
209 {
210 _app.use_gdb(true);
211 }
212
213 if (port() == LIBTEST_FAIL_PORT)
214 {
215 throw libtest::disconnected(LIBYATL_DEFAULT_PARAM,
216 hostname(), port(), "Called failure");
217 }
218
219 if (getenv("YATL_PTRCHECK_SERVER"))
220 {
221 _app.use_ptrcheck(true);
222 }
223 else if (getenv("YATL_VALGRIND_SERVER"))
224 {
225 _app.use_valgrind(true);
226 }
227
228 out_of_ban_killed(false);
229 if (args(_app) == false)
230 {
231 throw libtest::disconnected(LIBYATL_DEFAULT_PARAM,
232 hostname(), port(), "Could not build command()");
233 }
234
235 libtest::release_port(_port);
236
237 Application::error_t ret;
238 if (Application::SUCCESS != (ret= _app.run()))
239 {
240 throw libtest::disconnected(LIBYATL_DEFAULT_PARAM,
241 hostname(), port(), "Application::run() %s", libtest::Application::toString(ret));
242 return false;
243 }
244 _running= _app.print();
245
246 if (valgrind_is_caller())
247 {
248 dream(5, 50000);
249 }
250
251 size_t repeat= 5;
252 _app.slurp();
253 while (--repeat)
254 {
255 if (pid_file().empty() == false)
256 {
257 Wait wait(pid_file(), 8);
258
259 if (wait.successful() == false)
260 {
261 if (_app.check())
262 {
263 _app.slurp();
264 continue;
265 }
266
267 #ifdef __GLIBC__
268 Buffer buf( get_current_dir_name());
269 char *getcwd_buf= buf.buf();
270 #else
271 libtest::vchar_t buf;
272 buf.resize(PATH_MAX);
273 char *getcwd_buf= getcwd(&buf[0], buf.size());
274 #endif // __GLIBC__
275 throw libtest::disconnected(LIBYATL_DEFAULT_PARAM,
276 hostname(), port(),
277 "Unable to open pidfile in %s for: %s stderr:%s",
278 getcwd_buf ? getcwd_buf : "",
279 _running.c_str(),
280 _app.stderr_c_str());
281 }
282 }
283 }
284
285 bool pinged= false;
286 uint32_t this_wait= 0;
287 {
288 uint32_t waited;
289 uint32_t retry;
290
291 for (waited= 0, retry= 1; ; retry++, waited+= this_wait)
292 {
293 if (_app.check() == false)
294 {
295 break;
296 }
297
298 if ((pinged= ping()) == true)
299 {
300 break;
301 }
302 else if (waited >= _timeout)
303 {
304 break;
305 }
306
307 this_wait= retry * retry / 3 + 1;
308 libtest::dream(this_wait, 0);
309 }
310 }
311
312 if (pinged == false)
313 {
314 #if 0
315 Error << "Failed to ping(" << _app.pid() << ") wait: " << this_wait << " " << hostname() << ":" << port() << " run:" << _running << " " << error();
316 #endif
317
318 // If we happen to have a pid file, lets try to kill it
319 if ((pid_file().empty() == false) and (access(pid_file().c_str(), R_OK) == 0))
320 {
321 _app.slurp();
322 if (kill_file(pid_file()) == false)
323 {
324 throw libtest::disconnected(LIBYATL_DEFAULT_PARAM,
325 hostname(), port(),
326 "Failed to kill off server, waited: %u after startup occurred, when pinging failed: %.*s stderr:%.*s",
327 this_wait,
328 int(_running.size()), _running.c_str(),
329 int(_app.stderr_result_length()), _app.stderr_c_str());
330 }
331 else
332 {
333 throw libtest::disconnected(LIBYATL_DEFAULT_PARAM,
334 hostname(), port(),
335 "Failed native ping(), pid: %d was alive: %s waited: %u server started, having pid_file. exec: %.*s stderr:%.*s",
336 int(_app.pid()),
337 _app.check() ? "true" : "false",
338 this_wait,
339 int(_running.size()), _running.c_str(),
340 int(_app.stderr_result_length()), _app.stderr_c_str());
341 }
342 }
343 else
344 {
345 throw libtest::disconnected(LIBYATL_DEFAULT_PARAM,
346 hostname(), port(),
347 "Failed native ping(), pid: %d is alive: %s waited: %u server started. exec: %.*s stderr:%.*s",
348 int(_app.pid()),
349 _app.check() ? "true" : "false",
350 this_wait,
351 int(_running.size()), _running.c_str(),
352 int(_app.stderr_result_length()), _app.stderr_c_str());
353 }
354 _running.clear();
355
356 return false;
357 }
358
359 return has_pid();
360 }
361
362 void Server::reset_pid()
363 {
364 _running.clear();
365 _pid_file.clear();
366 }
367
368 pid_t Server::pid() const
369 {
370 return _app.pid();
371 }
372
373 void Server::add_option(const std::string& arg)
374 {
375 _options.push_back(std::make_pair(arg, std::string()));
376 }
377
378 void Server::add_option(const std::string& name_, const std::string& value_)
379 {
380 _options.push_back(std::make_pair(name_, value_));
381 }
382
383 bool Server::set_socket_file()
384 {
385 libtest::vchar_t file_buffer;
386 file_buffer.resize(FILENAME_MAX);
387 file_buffer[0]= 0;
388
389 if (broken_pid_file())
390 {
391 snprintf(&file_buffer[0], file_buffer.size(), "/tmp/%s.socketXXXXXX", name());
392 }
393 else
394 {
395 snprintf(&file_buffer[0], file_buffer.size(), "var/run/%s.socketXXXXXX", name());
396 }
397
398 int fd;
399 if ((fd= mkstemp(&file_buffer[0])) == -1)
400 {
401 perror(&file_buffer[0]);
402 return false;
403 }
404 close(fd);
405 unlink(&file_buffer[0]);
406
407 _socket= &file_buffer[0];
408
409 return true;
410 }
411
412 bool Server::set_pid_file()
413 {
414 libtest::vchar_t file_buffer;
415 file_buffer.resize(FILENAME_MAX);
416 file_buffer[0]= 0;
417
418 if (broken_pid_file())
419 {
420 snprintf(&file_buffer[0], file_buffer.size(), "/tmp/%s.pidXXXXXX", name());
421 }
422 else
423 {
424 snprintf(&file_buffer[0], file_buffer.size(), "var/run/%s.pidXXXXXX", name());
425 }
426
427 int fd;
428 if ((fd= mkstemp(&file_buffer[0])) == -1)
429 {
430 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "mkstemp() failed on %s with %s", &file_buffer[0], strerror(errno));
431 }
432 close(fd);
433 unlink(&file_buffer[0]);
434
435 _pid_file= &file_buffer[0];
436
437 return true;
438 }
439
440 bool Server::set_log_file()
441 {
442 libtest::vchar_t file_buffer;
443 file_buffer.resize(FILENAME_MAX);
444 file_buffer[0]= 0;
445
446 snprintf(&file_buffer[0], file_buffer.size(), "var/log/%s.logXXXXXX", name());
447 int fd;
448 if ((fd= mkstemp(&file_buffer[0])) == -1)
449 {
450 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "mkstemp() failed on %s with %s", &file_buffer[0], strerror(errno));
451 }
452 close(fd);
453
454 _log_file= &file_buffer[0];
455
456 return true;
457 }
458
459 bool Server::args(Application& app)
460 {
461
462 // Set a log file if it was requested (and we can)
463 if (has_log_file_option())
464 {
465 set_log_file();
466 log_file_option(app, _log_file);
467 }
468
469 if (getenv("LIBTEST_SYSLOG") and has_syslog())
470 {
471 app.add_option("--syslog");
472 }
473
474 // Update pid_file
475 {
476 if (_pid_file.empty() and set_pid_file() == false)
477 {
478 return false;
479 }
480
481 pid_file_option(app, pid_file());
482 }
483
484 if (has_socket_file_option())
485 {
486 if (set_socket_file() == false)
487 {
488 return false;
489 }
490
491 socket_file_option(app, _socket);
492 }
493
494 if (has_port_option())
495 {
496 port_option(app, _port);
497 }
498
499 for (Options::const_iterator iter= _options.begin(); iter != _options.end(); ++iter)
500 {
501 if ((*iter).first.empty() == false)
502 {
503 if ((*iter).second.empty() == false)
504 {
505 app.add_option((*iter).first, (*iter).second);
506 }
507 else
508 {
509 app.add_option((*iter).first);
510 }
511 }
512 }
513
514 return true;
515 }
516
517 bool Server::kill()
518 {
519 if (check_pid(_app.pid())) // If we kill it, reset
520 {
521 _app.murder();
522 if (broken_pid_file() and pid_file().empty() == false)
523 {
524 unlink(pid_file().c_str());
525 }
526
527 if (broken_socket_cleanup() and has_socket() and not socket().empty())
528 {
529 unlink(socket().c_str());
530 }
531
532 reset_pid();
533
534 return true;
535 }
536
537 return false;
538 }
539
540 } // namespace libtest