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