Remove custom version of memcached (no longer needed).
[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 <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 out_of_ban_killed_(false)
104 {
105 }
106
107 Server::~Server()
108 {
109 }
110
111 bool Server::check()
112 {
113 _app.slurp();
114 _app.check();
115 return true;
116 }
117
118 bool Server::validate()
119 {
120 return _magic == MAGIC_MEMORY;
121 }
122
123 // If the server exists, kill it
124 bool Server::cycle()
125 {
126 uint32_t limit= 3;
127
128 // Try to ping, and kill the server #limit number of times
129 while (--limit and
130 is_pid_valid(_app.pid()))
131 {
132 if (kill())
133 {
134 Log << "Killed existing server," << *this;
135 dream(0, 50000);
136 continue;
137 }
138 }
139
140 // For whatever reason we could not kill it, and we reached limit
141 if (limit == 0)
142 {
143 Error << "Reached limit, could not kill server";
144 return false;
145 }
146
147 return true;
148 }
149
150 bool Server::wait_for_pidfile() const
151 {
152 Wait wait(pid_file(), 4);
153
154 return wait.successful();
155 }
156
157 bool Server::has_pid() const
158 {
159 return (_app.pid() > 1);
160 }
161
162
163 bool Server::start()
164 {
165 // If we find that we already have a pid then kill it.
166 if (has_pid() == true)
167 {
168 #if 0
169 fatal_message("has_pid() failed, programer error");
170 #endif
171 }
172
173 // This needs more work.
174 #if 0
175 if (gdb_is_caller())
176 {
177 _app.use_gdb();
178 }
179 #endif
180
181 if (port() == LIBTEST_FAIL_PORT)
182 {
183 throw libtest::start(LIBYATL_DEFAULT_PARAM,
184 hostname(), port(), "Called failure");
185 }
186
187 if (getenv("YATL_PTRCHECK_SERVER"))
188 {
189 _app.use_ptrcheck();
190 }
191 else if (getenv("YATL_VALGRIND_SERVER"))
192 {
193 _app.use_valgrind();
194 }
195
196 if (args(_app) == false)
197 {
198 throw libtest::start(LIBYATL_DEFAULT_PARAM,
199 hostname(), port(), "Could not build command()");
200 }
201
202 libtest::release_port(_port);
203 Application::error_t ret;
204 if (Application::SUCCESS != (ret= _app.run()))
205 {
206 throw libtest::start(LIBYATL_DEFAULT_PARAM,
207 hostname(), port(), "Application::run() %s", libtest::Application::toString(ret));
208 return false;
209 }
210 _running= _app.print();
211
212 if (valgrind_is_caller())
213 {
214 dream(5, 50000);
215 }
216
217 size_t repeat= 5;
218 _app.slurp();
219 while (--repeat)
220 {
221 if (pid_file().empty() == false)
222 {
223 Wait wait(pid_file(), 8);
224
225 if (wait.successful() == false)
226 {
227 if (_app.check())
228 {
229 _app.slurp();
230 continue;
231 }
232
233 char buf[PATH_MAX];
234 char *getcwd_buf= getcwd(buf, sizeof(buf));
235 throw libtest::start(LIBYATL_DEFAULT_PARAM,
236 hostname(), port(),
237 "Unable to open pidfile in %s for: %s stderr:%s",
238 getcwd_buf ? getcwd_buf : "",
239 _running.c_str(),
240 _app.stderr_c_str());
241 }
242 }
243 }
244
245 uint32_t this_wait= 0;
246 bool pinged= false;
247 {
248 uint32_t timeout= 20; // This number should be high enough for valgrind startup (which is slow)
249 uint32_t waited;
250 uint32_t retry;
251
252 for (waited= 0, retry= 4; ; retry++, waited+= this_wait)
253 {
254 if ((pinged= ping()) == true)
255 {
256 break;
257 }
258 else if (waited >= timeout)
259 {
260 break;
261 }
262
263 this_wait= retry * retry / 3 + 1;
264 libtest::dream(this_wait, 0);
265 }
266 }
267
268 if (pinged == false)
269 {
270 // If we happen to have a pid file, lets try to kill it
271 if ((pid_file().empty() == false) and (access(pid_file().c_str(), R_OK) == 0))
272 {
273 _app.slurp();
274 if (kill_file(pid_file()) == false)
275 {
276 throw libtest::start(LIBYATL_DEFAULT_PARAM,
277 hostname(), port(),
278 "Failed to kill off server, waited: %u after startup occurred, when pinging failed: %.*s stderr:%.*s",
279 this_wait,
280 int(_running.size()), _running.c_str(),
281 int(_app.stderr_result_length()), _app.stderr_c_str());
282 }
283 else
284 {
285 throw libtest::start(LIBYATL_DEFAULT_PARAM,
286 hostname(), port(),
287 "Failed native ping(), pid: %d was alive: %s waited: %u server started, having pid_file. exec: %.*s stderr:%.*s",
288 int(_app.pid()),
289 _app.check() ? "true" : "false",
290 this_wait,
291 int(_running.size()), _running.c_str(),
292 int(_app.stderr_result_length()), _app.stderr_c_str());
293 }
294 }
295 else
296 {
297 throw libtest::start(LIBYATL_DEFAULT_PARAM,
298 hostname(), port(),
299 "Failed native ping(), pid: %d is alive: %s waited: %u server started. exec: %.*s stderr:%.*s",
300 int(_app.pid()),
301 _app.check() ? "true" : "false",
302 this_wait,
303 int(_running.size()), _running.c_str(),
304 int(_app.stderr_result_length()), _app.stderr_c_str());
305 }
306 _running.clear();
307
308 return false;
309 }
310
311 return has_pid();
312 }
313
314 void Server::reset_pid()
315 {
316 _running.clear();
317 _pid_file.clear();
318 }
319
320 pid_t Server::pid() const
321 {
322 return _app.pid();
323 }
324
325 void Server::add_option(const std::string& arg)
326 {
327 _options.push_back(std::make_pair(arg, std::string()));
328 }
329
330 void Server::add_option(const std::string& name_, const std::string& value)
331 {
332 _options.push_back(std::make_pair(name_, value));
333 }
334
335 bool Server::set_socket_file()
336 {
337 char file_buffer[FILENAME_MAX];
338 file_buffer[0]= 0;
339
340 if (broken_pid_file())
341 {
342 snprintf(file_buffer, sizeof(file_buffer), "/tmp/%s.socketXXXXXX", name());
343 }
344 else
345 {
346 snprintf(file_buffer, sizeof(file_buffer), "var/run/%s.socketXXXXXX", name());
347 }
348
349 int fd;
350 if ((fd= mkstemp(file_buffer)) == -1)
351 {
352 perror(file_buffer);
353 return false;
354 }
355 close(fd);
356 unlink(file_buffer);
357
358 _socket= file_buffer;
359
360 return true;
361 }
362
363 bool Server::set_pid_file()
364 {
365 char file_buffer[FILENAME_MAX];
366 file_buffer[0]= 0;
367
368 if (broken_pid_file())
369 {
370 snprintf(file_buffer, sizeof(file_buffer), "/tmp/%s.pidXXXXXX", name());
371 }
372 else
373 {
374 snprintf(file_buffer, sizeof(file_buffer), "var/run/%s.pidXXXXXX", name());
375 }
376
377 int fd;
378 if ((fd= mkstemp(file_buffer)) == -1)
379 {
380 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "mkstemp() failed on %s with %s", file_buffer, strerror(errno));
381 }
382 close(fd);
383 unlink(file_buffer);
384
385 _pid_file= file_buffer;
386
387 return true;
388 }
389
390 bool Server::set_log_file()
391 {
392 char file_buffer[FILENAME_MAX];
393 file_buffer[0]= 0;
394
395 snprintf(file_buffer, sizeof(file_buffer), "var/log/%s.logXXXXXX", name());
396 int fd;
397 if ((fd= mkstemp(file_buffer)) == -1)
398 {
399 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "mkstemp() failed on %s with %s", file_buffer, strerror(errno));
400 }
401 close(fd);
402
403 _log_file= file_buffer;
404
405 return true;
406 }
407
408 bool Server::args(Application& app)
409 {
410
411 // Set a log file if it was requested (and we can)
412 if (has_log_file_option())
413 {
414 set_log_file();
415 log_file_option(app, _log_file);
416 }
417
418 if (getenv("LIBTEST_SYSLOG") and has_syslog())
419 {
420 app.add_option("--syslog");
421 }
422
423 // Update pid_file
424 {
425 if (_pid_file.empty() and set_pid_file() == false)
426 {
427 return false;
428 }
429
430 pid_file_option(app, pid_file());
431 }
432
433 if (has_socket_file_option())
434 {
435 if (set_socket_file() == false)
436 {
437 return false;
438 }
439
440 socket_file_option(app, _socket);
441 }
442
443 if (has_port_option())
444 {
445 port_option(app, _port);
446 }
447
448 for (Options::const_iterator iter= _options.begin(); iter != _options.end(); ++iter)
449 {
450 if ((*iter).second.empty() == false)
451 {
452 app.add_option((*iter).first, (*iter).second);
453 }
454 else
455 {
456 app.add_option((*iter).first);
457 }
458 }
459
460 return true;
461 }
462
463 bool Server::kill()
464 {
465 if (check_pid(_app.pid())) // If we kill it, reset
466 {
467 _app.murder();
468 if (broken_pid_file() and pid_file().empty() == false)
469 {
470 unlink(pid_file().c_str());
471 }
472
473 if (broken_socket_cleanup() and has_socket() and not socket().empty())
474 {
475 unlink(socket().c_str());
476 }
477
478 reset_pid();
479
480 return true;
481 }
482
483 return false;
484 }
485
486 } // namespace libtest