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