Adding a copy of memcached to the tree.
[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 <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, bool is_socket_arg) :
80 _magic(MAGIC_MEMORY),
81 _is_socket(is_socket_arg),
82 _pid(-1),
83 _port(port_arg),
84 _hostname(host_arg)
85 {
86 }
87
88 Server::~Server()
89 {
90 if (has_pid() and not kill(_pid))
91 {
92 Error << "Unable to kill:" << *this;
93 }
94 }
95
96 bool Server::validate()
97 {
98 return _magic == MAGIC_MEMORY;
99 }
100
101 // If the server exists, kill it
102 bool Server::cycle()
103 {
104 uint32_t limit= 3;
105
106 // Try to ping, and kill the server #limit number of times
107 pid_t current_pid;
108 while (--limit and
109 is_pid_valid(current_pid= get_pid()))
110 {
111 if (kill(current_pid))
112 {
113 Log << "Killed existing server," << *this << " with pid:" << current_pid;
114 dream(0, 50000);
115 continue;
116 }
117 }
118
119 // For whatever reason we could not kill it, and we reached limit
120 if (limit == 0)
121 {
122 Error << "Reached limit, could not kill server pid:" << current_pid;
123 return false;
124 }
125
126 return true;
127 }
128
129 bool Server::wait_for_pidfile() const
130 {
131 Wait wait(pid_file(), 4);
132
133 return wait.successful();
134 }
135
136 bool Server::start()
137 {
138 // If we find that we already have a pid then kill it.
139 if (has_pid() and kill(_pid) == false)
140 {
141 Error << "Could not kill() existing server during start() pid:" << _pid;
142 return false;
143 }
144
145 if (has_pid() == false)
146 {
147 fatal_message("has_pid() failed, programer error");
148 }
149
150 Application app(executable(), is_libtool());
151
152 if (is_debug())
153 {
154 app.use_gdb();
155 }
156 else if (getenv("TESTS_ENVIRONMENT"))
157 {
158 if (strstr(getenv("TESTS_ENVIRONMENT"), "gdb"))
159 {
160 app.use_gdb();
161 }
162 }
163
164 if (args(app) == false)
165 {
166 Error << "Could not build command()";
167 return false;
168 }
169
170 Application::error_t ret;
171 if (Application::SUCCESS != (ret= app.run()))
172 {
173 Error << "Application::run() " << ret;
174 return false;
175 }
176 _running= app.print();
177
178 if (Application::SUCCESS != (ret= app.wait()))
179 {
180 Error << "Application::wait() " << _running << " " << ret;
181 return false;
182 }
183
184 if (is_valgrind())
185 {
186 dream(5, 50000);
187 }
188
189 if (pid_file().empty() == false)
190 {
191 Wait wait(pid_file(), 8);
192
193 if (wait.successful() == false)
194 {
195 libtest::fatal(LIBYATL_DEFAULT_PARAM,
196 "Unable to open pidfile for: %s",
197 _running.c_str());
198 }
199 }
200
201 bool pinged= false;
202 {
203 uint32_t timeout= 20; // This number should be high enough for valgrind startup (which is slow)
204 uint32_t waited;
205 uint32_t this_wait;
206 uint32_t retry;
207
208 for (waited= 0, retry= 1; ; retry++, waited+= this_wait)
209 {
210 if ((pinged= ping()) == true)
211 {
212 break;
213 }
214 else if (waited >= timeout)
215 {
216 break;
217 }
218
219 this_wait= retry * retry / 3 + 1;
220 libtest::dream(this_wait, 0);
221 }
222 }
223
224 if (pinged == false)
225 {
226 // If we happen to have a pid file, lets try to kill it
227 if (pid_file().empty() == false)
228 {
229 if (kill_file(pid_file()) == false)
230 {
231 fatal_message("Failed to kill off server after startup occurred, when pinging failed");
232 }
233 Error << "Failed to ping() server started, having pid_file. exec:" << _running;
234 }
235 else
236 {
237 Error << "Failed to ping() server started. exec:" << _running;
238 }
239 _running.clear();
240 return false;
241 }
242
243 // A failing get_pid() at this point is considered an error
244 _pid= get_pid(true);
245
246 return has_pid();
247 }
248
249 void Server::reset_pid()
250 {
251 _running.clear();
252 _pid_file.clear();
253 _pid= -1;
254 }
255
256 pid_t Server::pid()
257 {
258 return _pid;
259 }
260
261 void Server::add_option(const std::string& arg)
262 {
263 _options.push_back(std::make_pair(arg, std::string()));
264 }
265
266 void Server::add_option(const std::string& name, const std::string& value)
267 {
268 _options.push_back(std::make_pair(name, value));
269 }
270
271 bool Server::set_socket_file()
272 {
273 char file_buffer[FILENAME_MAX];
274 file_buffer[0]= 0;
275
276 if (broken_pid_file())
277 {
278 snprintf(file_buffer, sizeof(file_buffer), "/tmp/%s.socketXXXXXX", name());
279 }
280 else
281 {
282 snprintf(file_buffer, sizeof(file_buffer), "var/run/%s.socketXXXXXX", name());
283 }
284
285 int fd;
286 if ((fd= mkstemp(file_buffer)) == -1)
287 {
288 perror(file_buffer);
289 return false;
290 }
291 close(fd);
292 unlink(file_buffer);
293
294 _socket= file_buffer;
295
296 return true;
297 }
298
299 bool Server::set_pid_file()
300 {
301 char file_buffer[FILENAME_MAX];
302 file_buffer[0]= 0;
303
304 if (broken_pid_file())
305 {
306 snprintf(file_buffer, sizeof(file_buffer), "/tmp/%s.pidXXXXXX", name());
307 }
308 else
309 {
310 snprintf(file_buffer, sizeof(file_buffer), "var/run/%s.pidXXXXXX", name());
311 }
312
313 int fd;
314 if ((fd= mkstemp(file_buffer)) == -1)
315 {
316 perror(file_buffer);
317 return false;
318 }
319 close(fd);
320 unlink(file_buffer);
321
322 _pid_file= file_buffer;
323
324 return true;
325 }
326
327 bool Server::set_log_file()
328 {
329 char file_buffer[FILENAME_MAX];
330 file_buffer[0]= 0;
331
332 snprintf(file_buffer, sizeof(file_buffer), "var/log/%s.logXXXXXX", name());
333 int fd;
334 if ((fd= mkstemp(file_buffer)) == -1)
335 {
336 libtest::fatal(LIBYATL_DEFAULT_PARAM, "mkstemp() failed on %s with %s", file_buffer, strerror(errno));
337 }
338 close(fd);
339
340 _log_file= file_buffer;
341
342 return true;
343 }
344
345 bool Server::args(Application& app)
346 {
347
348 // Set a log file if it was requested (and we can)
349 if (has_log_file_option())
350 {
351 set_log_file();
352 log_file_option(app, _log_file);
353 }
354
355 if (getenv("LIBTEST_SYSLOG") and has_syslog())
356 {
357 app.add_option("--syslog");
358 }
359
360 // Update pid_file
361 {
362 if (_pid_file.empty() and set_pid_file() == false)
363 {
364 return false;
365 }
366
367 pid_file_option(app, pid_file());
368 }
369
370 assert(daemon_file_option());
371 if (daemon_file_option() and not is_valgrind() and not is_helgrind())
372 {
373 app.add_option(daemon_file_option());
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::is_debug() const
407 {
408 return bool(getenv("LIBTEST_MANUAL_GDB"));
409 }
410
411 bool Server::is_valgrind() const
412 {
413 return bool(getenv("LIBTEST_MANUAL_VALGRIND"));
414 }
415
416 bool Server::is_helgrind() const
417 {
418 return bool(getenv("LIBTEST_MANUAL_HELGRIND"));
419 }
420
421 bool Server::kill(pid_t pid_arg)
422 {
423 if (check_pid(pid_arg) and kill_pid(pid_arg)) // If we kill it, reset
424 {
425 if (broken_pid_file() and pid_file().empty() == false)
426 {
427 unlink(pid_file().c_str());
428 }
429
430 if (broken_socket_cleanup() and has_socket() and not socket().empty())
431 {
432 unlink(socket().c_str());
433 }
434
435 reset_pid();
436
437 return true;
438 }
439
440 return false;
441 }
442
443 } // namespace libtest