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