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