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