Update free port logic.
[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 <libtest/common.h>
24
25 #include <cassert>
26 #include <cerrno>
27 #include <cstdlib>
28 #include <iostream>
29
30 #include <algorithm>
31 #include <functional>
32 #include <locale>
33
34 // trim from end
35 static inline std::string &rtrim(std::string &s)
36 {
37 s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
38 return s;
39 }
40
41 #include <libtest/server.h>
42 #include <libtest/stream.h>
43 #include <libtest/killpid.h>
44
45 extern "C" {
46 static bool exited_successfully(int status, const std::string &command)
47 {
48 if (status == 0)
49 {
50 return true;
51 }
52
53 if (WIFEXITED(status) == true)
54 {
55 int ret= WEXITSTATUS(status);
56
57 if (ret == 0)
58 {
59 return true;
60 }
61 else if (ret == EXIT_FAILURE)
62 {
63 libtest::Error << "Command executed, but returned EXIT_FAILURE: " << command;
64 }
65 else
66 {
67 libtest::Error << "Command executed, but returned " << ret;
68 }
69 }
70 else if (WIFSIGNALED(status) == true)
71 {
72 int ret_signal= WTERMSIG(status);
73 libtest::Error << "Died from signal " << strsignal(ret_signal);
74 }
75
76 return false;
77 }
78 }
79
80
81 namespace libtest {
82
83 std::ostream& operator<<(std::ostream& output, const Server &arg)
84 {
85 if (arg.is_socket())
86 {
87 output << arg.hostname();
88 }
89 else
90 {
91 output << arg.hostname() << ":" << arg.port();
92 }
93
94 if (arg.has_pid())
95 {
96 output << " Pid:" << arg.pid();
97 }
98
99 if (arg.has_socket())
100 {
101 output << " Socket:" << arg.socket();
102 }
103
104 if (not arg.running().empty())
105 {
106 output << " Exec:" << arg.running();
107 }
108
109
110 return output; // for multiple << operators
111 }
112
113 Server::Server(const std::string& host_arg, const in_port_t port_arg, bool is_socket_arg) :
114 _is_socket(is_socket_arg),
115 _pid(-1),
116 _port(port_arg),
117 _hostname(host_arg)
118 {
119 }
120
121 Server::~Server()
122 {
123 if (has_pid() and not kill(_pid))
124 {
125 Error << "Unable to kill:" << *this;
126 }
127 }
128
129 // If the server exists, kill it
130 bool Server::cycle()
131 {
132 uint32_t limit= 3;
133
134 // Try to ping, and kill the server #limit number of times
135 pid_t current_pid;
136 while (--limit and is_pid_valid(current_pid= get_pid()))
137 {
138 if (kill(current_pid))
139 {
140 Log << "Killed existing server," << *this << " with pid:" << current_pid;
141 dream(0, 50000);
142 continue;
143 }
144 }
145
146 // For whatever reason we could not kill it, and we reached limit
147 if (limit == 0)
148 {
149 Error << "Reached limit, could not kill server pid:" << current_pid;
150 return false;
151 }
152
153 return true;
154 }
155
156 // Grab a one off command
157 bool Server::command(std::string& command_arg)
158 {
159 rebuild_base_command();
160
161 command_arg+= _base_command;
162
163 if (args(command_arg))
164 {
165 return true;
166 }
167
168 return false;
169 }
170
171 bool Server::wait_for_pidfile() const
172 {
173 Wait wait(pid_file(), 4);
174
175 return wait.successful();
176 }
177
178 bool Server::start()
179 {
180 // If we find that we already have a pid then kill it.
181 if (has_pid() and kill(_pid) == false)
182 {
183 Error << "Could not kill() existing server during start() pid:" << _pid;
184 return false;
185 }
186 assert(not has_pid());
187
188 _running.clear();
189 if (command(_running) == false)
190 {
191 Error << "Could not build command()";
192 return false;
193 }
194
195 if (is_valgrind() or is_helgrind())
196 {
197 _running+= " &";
198 }
199
200 int ret= system(_running.c_str());
201 if (exited_successfully(ret, _running) == false)
202 {
203 Error << "system(" << _running << ") failed: " << strerror(errno);
204 _running.clear();
205 return false;
206 }
207
208 if (is_helgrind() or is_valgrind())
209 {
210 dream(5, 50000);
211 }
212
213 if (pid_file_option() and pid_file().empty() == false)
214 {
215 Wait wait(pid_file(), 8);
216
217 if (not wait.successful())
218 {
219 Error << "Unable to open pidfile for: " << _running;
220 }
221 }
222
223 int counter= 0;
224 bool pinged= false;
225 while ((pinged= ping()) == false and
226 counter < (is_helgrind() or is_valgrind() ? 20 : 5))
227 {
228 dream(counter++, 50000);
229 }
230
231 if (pinged == false)
232 {
233 // If we happen to have a pid file, lets try to kill it
234 if (pid_file_option() and pid_file().empty() == false)
235 {
236 Error << "We are going to kill it off";
237 kill_file(pid_file());
238 }
239 Error << "Failed to ping() server started with:" << _running;
240 _running.clear();
241 return false;
242 }
243
244 // A failing get_pid() at this point is considered an error
245 _pid= get_pid(true);
246
247 return has_pid();
248 }
249
250 void Server::reset_pid()
251 {
252 _running.clear();
253 _pid_file.clear();
254 _pid= -1;
255 }
256
257 pid_t Server::pid()
258 {
259 return _pid;
260 }
261
262 bool Server::set_socket_file()
263 {
264 char file_buffer[FILENAME_MAX];
265 file_buffer[0]= 0;
266
267 if (broken_pid_file())
268 {
269 snprintf(file_buffer, sizeof(file_buffer), "/tmp/%s.socketXXXXXX", name());
270 }
271 else
272 {
273 snprintf(file_buffer, sizeof(file_buffer), "var/run/%s.socketXXXXXX", name());
274 }
275
276 int fd;
277 if ((fd= mkstemp(file_buffer)) == -1)
278 {
279 perror(file_buffer);
280 return false;
281 }
282 close(fd);
283 unlink(file_buffer);
284
285 _socket= file_buffer;
286
287 return true;
288 }
289
290 bool Server::set_pid_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.pidXXXXXX", name());
298 }
299 else
300 {
301 snprintf(file_buffer, sizeof(file_buffer), "var/run/%s.pidXXXXXX", 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 _pid_file= file_buffer;
314
315 return true;
316 }
317
318 bool Server::set_log_file()
319 {
320 char file_buffer[FILENAME_MAX];
321 file_buffer[0]= 0;
322
323 snprintf(file_buffer, sizeof(file_buffer), "var/log/%s.logXXXXXX", name());
324 int fd;
325 if ((fd= mkstemp(file_buffer)) == -1)
326 {
327 perror(file_buffer);
328 return false;
329 }
330 close(fd);
331
332 _log_file= file_buffer;
333
334 return true;
335 }
336
337 void Server::rebuild_base_command()
338 {
339 _base_command.clear();
340 if (is_libtool())
341 {
342 _base_command+= libtool();
343 _base_command+= " --mode=execute ";
344 }
345
346 if (is_debug() and getenv("GDB_COMMAND"))
347 {
348 _base_command+= getenv("GDB_COMMAND");
349 _base_command+= " ";
350 }
351 else if (is_valgrind() and getenv("VALGRIND_COMMAND"))
352 {
353 _base_command+= getenv("VALGRIND_COMMAND");
354 _base_command+= " ";
355 }
356 else if (is_helgrind() and getenv("HELGRIND_COMMAND"))
357 {
358 _base_command+= getenv("HELGRIND_COMMAND");
359 _base_command+= " ";
360 }
361
362 if (is_libtool())
363 {
364 if (getenv("PWD"))
365 {
366 _base_command+= getenv("PWD");
367 _base_command+= "/";
368 }
369 }
370
371 _base_command+= executable();
372 }
373
374 void Server::set_extra_args(const std::string &arg)
375 {
376 _extra_args= arg;
377 }
378
379 bool Server::args(std::string& options)
380 {
381 std::stringstream arg_buffer;
382
383 // Set a log file if it was requested (and we can)
384 if (getenv("LIBTEST_LOG") and log_file_option())
385 {
386 if (not set_log_file())
387 {
388 return false;
389 }
390
391 arg_buffer << " " << log_file_option() << _log_file;
392 }
393
394 if (getenv("LIBTEST_SYSLOG") and has_syslog())
395 {
396 arg_buffer << " --syslog";
397 }
398
399 // Update pid_file
400 if (pid_file_option())
401 {
402 if (_pid_file.empty() and set_pid_file() == false)
403 {
404 return false;
405 }
406
407 arg_buffer << " " << pid_file_option() << pid_file();
408 }
409
410 assert(daemon_file_option());
411 if (daemon_file_option() and not is_valgrind() and not is_helgrind())
412 {
413 arg_buffer << " " << daemon_file_option();
414 }
415
416 if (_is_socket and socket_file_option())
417 {
418 if (not set_socket_file())
419 {
420 return false;
421 }
422
423 arg_buffer << " " << socket_file_option() << "\"" << _socket << "\"";
424 }
425
426 assert(port_option());
427 if (port_option() and _port > 0)
428 {
429 arg_buffer << " " << port_option() << _port;
430 }
431
432 options+= arg_buffer.str();
433
434 if (not _extra_args.empty())
435 {
436 options+= _extra_args;
437 }
438
439 return true;
440 }
441
442 bool Server::is_debug() const
443 {
444 return bool(getenv("LIBTEST_MANUAL_GDB"));
445 }
446
447 bool Server::is_valgrind() const
448 {
449 return bool(getenv("LIBTEST_MANUAL_VALGRIND"));
450 }
451
452 bool Server::is_helgrind() const
453 {
454 return bool(getenv("LIBTEST_MANUAL_HELGRIND"));
455 }
456
457 bool Server::kill(pid_t pid_arg)
458 {
459 if (check_pid(pid_arg) and kill_pid(pid_arg)) // If we kill it, reset
460 {
461 if (broken_pid_file() and pid_file().empty() == false)
462 {
463 unlink(pid_file().c_str());
464 }
465
466 if (broken_socket_cleanup() and has_socket() and not socket().empty())
467 {
468 unlink(socket().c_str());
469 }
470
471 reset_pid();
472
473 return true;
474 }
475
476 return false;
477 }
478
479 } // namespace libtest