Update from libtest
[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 continue;
205 }
206
207 char buf[PATH_MAX];
208 char *getcwd_buf= getcwd(buf, sizeof(buf));
209 throw libtest::fatal(LIBYATL_DEFAULT_PARAM,
210 "Unable to open pidfile in %s for: %s stderr:%s",
211 getcwd_buf ? getcwd_buf : "",
212 _running.c_str(),
213 _app.stderr_c_str());
214 }
215 }
216 }
217
218 uint32_t this_wait= 0;
219 bool pinged= false;
220 {
221 uint32_t timeout= 20; // This number should be high enough for valgrind startup (which is slow)
222 uint32_t waited;
223 uint32_t retry;
224
225 for (waited= 0, retry= 1; ; retry++, waited+= this_wait)
226 {
227 if ((pinged= ping()) == true)
228 {
229 break;
230 }
231 else if (waited >= timeout)
232 {
233 break;
234 }
235
236 this_wait= retry * retry / 3 + 1;
237 libtest::dream(this_wait, 0);
238 }
239 }
240
241 if (pinged == false)
242 {
243 // If we happen to have a pid file, lets try to kill it
244 if ((pid_file().empty() == false) and (access(pid_file().c_str(), R_OK) == 0))
245 {
246 _app.slurp();
247 if (kill_file(pid_file()) == false)
248 {
249 throw libtest::fatal(LIBYATL_DEFAULT_PARAM,
250 "Failed to kill off server, waited: %u after startup occurred, when pinging failed: %s stderr:%s",
251 this_wait,
252 pid_file().c_str(),
253 _app.stderr_c_str());
254 }
255
256 throw libtest::fatal(LIBYATL_DEFAULT_PARAM,
257 "Failed to ping(), waited: %u server started, having pid_file. exec: %s stderr:%s",
258 this_wait, _running.c_str(),
259 _app.stderr_c_str());
260 }
261 else
262 {
263 throw libtest::fatal(LIBYATL_DEFAULT_PARAM,
264 "Failed to ping(), waited: %u server started. exec: %s stderr:%s",
265 this_wait,
266 _running.c_str(),
267 _app.stderr_c_str());
268 }
269 _running.clear();
270 return false;
271 }
272
273 return has_pid();
274 }
275
276 void Server::reset_pid()
277 {
278 _running.clear();
279 _pid_file.clear();
280 }
281
282 pid_t Server::pid() const
283 {
284 return _app.pid();
285 }
286
287 void Server::add_option(const std::string& arg)
288 {
289 _options.push_back(std::make_pair(arg, std::string()));
290 }
291
292 void Server::add_option(const std::string& name, const std::string& value)
293 {
294 _options.push_back(std::make_pair(name, value));
295 }
296
297 bool Server::set_socket_file()
298 {
299 char file_buffer[FILENAME_MAX];
300 file_buffer[0]= 0;
301
302 if (broken_pid_file())
303 {
304 snprintf(file_buffer, sizeof(file_buffer), "/tmp/%s.socketXXXXXX", name());
305 }
306 else
307 {
308 snprintf(file_buffer, sizeof(file_buffer), "var/run/%s.socketXXXXXX", name());
309 }
310
311 int fd;
312 if ((fd= mkstemp(file_buffer)) == -1)
313 {
314 perror(file_buffer);
315 return false;
316 }
317 close(fd);
318 unlink(file_buffer);
319
320 _socket= file_buffer;
321
322 return true;
323 }
324
325 bool Server::set_pid_file()
326 {
327 char file_buffer[FILENAME_MAX];
328 file_buffer[0]= 0;
329
330 if (broken_pid_file())
331 {
332 snprintf(file_buffer, sizeof(file_buffer), "/tmp/%s.pidXXXXXX", name());
333 }
334 else
335 {
336 snprintf(file_buffer, sizeof(file_buffer), "var/run/%s.pidXXXXXX", name());
337 }
338
339 int fd;
340 if ((fd= mkstemp(file_buffer)) == -1)
341 {
342 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "mkstemp() failed on %s with %s", file_buffer, strerror(errno));
343 }
344 close(fd);
345 unlink(file_buffer);
346
347 _pid_file= file_buffer;
348
349 return true;
350 }
351
352 bool Server::set_log_file()
353 {
354 char file_buffer[FILENAME_MAX];
355 file_buffer[0]= 0;
356
357 snprintf(file_buffer, sizeof(file_buffer), "var/log/%s.logXXXXXX", name());
358 int fd;
359 if ((fd= mkstemp(file_buffer)) == -1)
360 {
361 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "mkstemp() failed on %s with %s", file_buffer, strerror(errno));
362 }
363 close(fd);
364
365 _log_file= file_buffer;
366
367 return true;
368 }
369
370 bool Server::args(Application& app)
371 {
372
373 // Set a log file if it was requested (and we can)
374 if (false and has_log_file_option())
375 {
376 set_log_file();
377 log_file_option(app, _log_file);
378 }
379
380 if (getenv("LIBTEST_SYSLOG") and has_syslog())
381 {
382 app.add_option("--syslog");
383 }
384
385 // Update pid_file
386 {
387 if (_pid_file.empty() and set_pid_file() == false)
388 {
389 return false;
390 }
391
392 pid_file_option(app, pid_file());
393 }
394
395 if (has_socket_file_option())
396 {
397 if (set_socket_file() == false)
398 {
399 return false;
400 }
401
402 socket_file_option(app, _socket);
403 }
404
405 if (has_port_option())
406 {
407 port_option(app, _port);
408 }
409
410 for (Options::const_iterator iter= _options.begin(); iter != _options.end(); iter++)
411 {
412 if ((*iter).second.empty() == false)
413 {
414 app.add_option((*iter).first, (*iter).second);
415 }
416 else
417 {
418 app.add_option((*iter).first);
419 }
420 }
421
422 return true;
423 }
424
425 bool Server::kill()
426 {
427 if (check_pid(_app.pid())) // If we kill it, reset
428 {
429 _app.murder();
430 if (broken_pid_file() and pid_file().empty() == false)
431 {
432 unlink(pid_file().c_str());
433 }
434
435 if (broken_socket_cleanup() and has_socket() and not socket().empty())
436 {
437 unlink(socket().c_str());
438 }
439
440 reset_pid();
441
442 return true;
443 }
444
445 return false;
446 }
447
448 } // namespace libtest