Cleans up the assert and some other things that Fedora 15 found.
[m6w6/libmemcached] / libtest / server.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Libtest library
4 *
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following disclaimer
16 * in the documentation and/or other materials provided with the
17 * distribution.
18 *
19 * * The names of its contributors may not be used to endorse or
20 * promote products derived from this software without specific prior
21 * written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *
35 */
36
37 #include <libtest/common.h>
38
39 #include <cassert>
40 #include <cerrno>
41 #include <cstdlib>
42 #include <iostream>
43
44 #include <algorithm>
45 #include <functional>
46 #include <locale>
47
48 // trim from end
49 static inline std::string &rtrim(std::string &s)
50 {
51 s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
52 return s;
53 }
54
55 #include <libtest/server.h>
56 #include <libtest/stream.h>
57 #include <libtest/killpid.h>
58
59 #ifdef HAVE_LIBGEARMAN
60 #include <libtest/gearmand.h>
61 #endif
62
63 #ifdef HAVE_LIBMEMCACHED
64 #include <libtest/memcached.h>
65 #endif
66
67 namespace libtest {
68
69 std::ostream& operator<<(std::ostream& output, const Server &arg)
70 {
71 if (arg.is_socket())
72 {
73 output << arg.hostname();
74 }
75 else
76 {
77 output << arg.hostname() << ":" << arg.port();
78 }
79
80 if (arg.has_pid())
81 {
82 output << " Pid:" << arg.pid();
83 }
84
85 if (arg.has_socket())
86 {
87 output << " Socket:" << arg.socket();
88 }
89
90 if (not arg.running().empty())
91 {
92 output << " Exec:" << arg.running();
93 }
94
95
96 return output; // for multiple << operators
97 }
98
99 void Server::nap(void)
100 {
101 #ifdef WIN32
102 sleep(1);
103 #else
104 struct timespec global_sleep_value= { 0, 50000 };
105 nanosleep(&global_sleep_value, NULL);
106 #endif
107 }
108
109 Server::Server(const std::string& host_arg, const in_port_t port_arg, bool is_socket_arg) :
110 _is_socket(is_socket_arg),
111 _pid(-1),
112 _port(port_arg),
113 _hostname(host_arg)
114 {
115 }
116
117 Server::~Server()
118 {
119 if (has_pid() and not kill(_pid))
120 {
121 Error << "Unable to kill:" << *this;
122 }
123 }
124
125 std::string server_startup_st::option_string() const
126 {
127 std::string temp= server_list;
128 rtrim(temp);
129 return temp;
130 }
131
132 // If the server exists, kill it
133 bool Server::cycle()
134 {
135 uint32_t limit= 3;
136
137 // Try to ping, and kill the server #limit number of times
138 pid_t current_pid;
139 while (--limit and (current_pid= get_pid()) != -1)
140 {
141 if (kill(current_pid))
142 {
143 Log << "Killed existing server," << *this << " with pid:" << current_pid;
144 nap();
145 continue;
146 }
147 }
148
149 // For whatever reason we could not kill it, and we reached limit
150 if (limit == 0)
151 {
152 Error << "Reached limit, could not kill server pid:" << current_pid;
153 return false;
154 }
155
156 return true;
157 }
158
159 // Grab a one off command
160 bool Server::command(std::string& command_arg)
161 {
162 rebuild_base_command();
163
164 command_arg+= _base_command;
165
166 if (args(command_arg))
167 {
168 return true;
169 }
170
171 return false;
172 }
173
174 bool Server::start()
175 {
176 // If we find that we already have a pid then kill it.
177 if (has_pid() and not kill(_pid))
178 {
179 Error << "Could not kill() existing server during start() pid:" << _pid;
180 return false;
181 }
182 assert(not has_pid());
183
184 _running.clear();
185 if (not command(_running))
186 {
187 Error << "Could not build command()";
188 return false;
189 }
190
191 if (is_valgrind() or is_helgrind())
192 {
193 _running+= " &";
194 }
195
196 if (system(_running.c_str()) == -1)
197 {
198 Error << "system() failed:" << strerror(errno);
199 _running.clear();
200 return false;
201 }
202
203 if (is_helgrind())
204 {
205 sleep(4);
206 }
207
208 if (pid_file_option() and not pid_file().empty())
209 {
210 Wait wait(pid_file());
211
212 if (not wait.successful())
213 {
214 Error << "Unable to open pidfile: " << pid_file();
215 }
216 }
217
218 int count= is_helgrind() ? 20 : 5;
219 while (not ping() and --count)
220 {
221 nap();
222 }
223
224 if (count == 0)
225 {
226 Error << "Failed to ping() server once started:" << *this;
227 _running.clear();
228 return false;
229 }
230
231 // A failing get_pid() at this point is considered an error
232 _pid= get_pid(true);
233
234 return has_pid();
235 }
236
237 void Server::reset_pid()
238 {
239 _running.clear();
240 _pid_file.clear();
241 _pid= -1;
242 }
243
244 pid_t Server::pid()
245 {
246 return _pid;
247 }
248
249 bool Server::set_socket_file()
250 {
251 char file_buffer[FILENAME_MAX];
252 file_buffer[0]= 0;
253
254 if (broken_pid_file())
255 {
256 snprintf(file_buffer, sizeof(file_buffer), "/tmp/%s.socketXXXXXX", name());
257 }
258 else
259 {
260 snprintf(file_buffer, sizeof(file_buffer), "tests/var/run/%s.socketXXXXXX", name());
261 }
262
263 int fd;
264 if ((fd= mkstemp(file_buffer)) == -1)
265 {
266 perror(file_buffer);
267 return false;
268 }
269 close(fd);
270 unlink(file_buffer);
271
272 _socket= file_buffer;
273
274 return true;
275 }
276
277 bool Server::set_pid_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.pidXXXXXX", name());
285 }
286 else
287 {
288 snprintf(file_buffer, sizeof(file_buffer), "tests/var/run/%s.pidXXXXXX", 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 _pid_file= file_buffer;
301
302 return true;
303 }
304
305 bool Server::set_log_file()
306 {
307 char file_buffer[FILENAME_MAX];
308 file_buffer[0]= 0;
309
310 snprintf(file_buffer, sizeof(file_buffer), "tests/var/log/%s.logXXXXXX", name());
311 int fd;
312 if ((fd= mkstemp(file_buffer)) == -1)
313 {
314 perror(file_buffer);
315 return false;
316 }
317 close(fd);
318
319 _log_file= file_buffer;
320
321 return true;
322 }
323
324 void Server::rebuild_base_command()
325 {
326 _base_command.clear();
327 if (is_libtool())
328 {
329 _base_command+= "./libtool --mode=execute ";
330 }
331
332 if (is_debug())
333 {
334 _base_command+= "gdb ";
335 }
336 else if (is_valgrind())
337 {
338 _base_command+= "valgrind --log-file=tests/var/tmp/valgrind.out --error-exitcode=1 --leak-check=yes --show-reachable=yes --track-fds=yes --malloc-fill=A5 --free-fill=DE ";
339
340 }
341 else if (is_helgrind())
342 {
343 _base_command+= "valgrind --log-file=tests/var/tmp/helgrind.out --tool=helgrind --read-var-info=yes --error-exitcode=1 -v ";
344 }
345
346 _base_command+= executable();
347 }
348
349 void Server::set_extra_args(const std::string &arg)
350 {
351 _extra_args= arg;
352 }
353
354 bool Server::args(std::string& options)
355 {
356 std::stringstream arg_buffer;
357
358 // Set a log file if it was requested (and we can)
359 if (getenv("LIBTEST_LOG") and log_file_option())
360 {
361 if (not set_log_file())
362 return false;
363
364 arg_buffer << " " << log_file_option() << _log_file;
365 }
366
367 // Update pid_file
368 if (pid_file_option())
369 {
370 if (not set_pid_file())
371 return false;
372
373 arg_buffer << " " << pid_file_option() << pid_file();
374 }
375
376 assert(daemon_file_option());
377 if (daemon_file_option() and not is_valgrind() and not is_helgrind())
378 {
379 arg_buffer << " " << daemon_file_option();
380 }
381
382 if (_is_socket and socket_file_option())
383 {
384 if (not set_socket_file())
385 return false;
386
387 arg_buffer << " " << socket_file_option() << "\"" << _socket << "\"";
388 }
389
390 assert(port_option());
391 if (port_option() and _port > 0)
392 {
393 arg_buffer << " " << port_option() << _port;
394 }
395
396 options+= arg_buffer.str();
397
398 if (not _extra_args.empty())
399 options+= _extra_args;
400
401 return true;
402 }
403
404 bool Server::is_debug() const
405 {
406 return bool(getenv("LIBTEST_MANUAL_GDB"));
407 }
408
409 bool Server::is_valgrind() const
410 {
411 return bool(getenv("LIBTEST_MANUAL_VALGRIND"));
412 }
413
414 bool Server::is_helgrind() const
415 {
416 return bool(getenv("LIBTEST_MANUAL_HELGRIND"));
417 }
418
419 bool Server::kill(pid_t pid_arg)
420 {
421 if (check_pid(pid_arg) and kill_pid(pid_arg)) // If we kill it, reset
422 {
423 if (broken_pid_file() and not pid_file().empty())
424 {
425 unlink(pid_file().c_str());
426 }
427
428 reset_pid();
429
430 return true;
431 }
432
433 return false;
434 }
435
436 void server_startup_st::push_server(Server *arg)
437 {
438 servers.push_back(arg);
439
440 char port_str[NI_MAXSERV];
441 snprintf(port_str, sizeof(port_str), "%u", int(arg->port()));
442
443 std::string server_config_string;
444 if (arg->has_socket())
445 {
446 server_config_string+= "--socket=";
447 server_config_string+= '"';
448 server_config_string+= arg->socket();
449 server_config_string+= '"';
450 server_config_string+= " ";
451 }
452 else
453 {
454 server_config_string+= "--server=";
455 server_config_string+= arg->hostname();
456 server_config_string+= ":";
457 server_config_string+= port_str;
458 server_config_string+= " ";
459 }
460
461 server_list+= server_config_string;
462
463 }
464
465 Server* server_startup_st::pop_server()
466 {
467 Server *tmp= servers.back();
468 servers.pop_back();
469 return tmp;
470 }
471
472 void server_startup_st::shutdown(bool remove)
473 {
474 if (remove)
475 {
476 for (std::vector<Server *>::iterator iter= servers.begin(); iter != servers.end(); iter++)
477 {
478 delete *iter;
479 }
480 servers.clear();
481 }
482 else
483 {
484 for (std::vector<Server *>::iterator iter= servers.begin(); iter != servers.end(); iter++)
485 {
486 if ((*iter)->has_pid() and not (*iter)->kill((*iter)->pid()))
487 {
488 Error << "Unable to kill:" << *(*iter);
489 }
490 }
491 }
492 }
493
494 server_startup_st::~server_startup_st()
495 {
496 shutdown(true);
497 }
498
499 bool server_startup_st::is_debug() const
500 {
501 return bool(getenv("LIBTEST_MANUAL_GDB"));
502 }
503
504 bool server_startup_st::is_valgrind() const
505 {
506 return bool(getenv("LIBTEST_MANUAL_VALGRIND"));
507 }
508
509 bool server_startup_st::is_helgrind() const
510 {
511 return bool(getenv("LIBTEST_MANUAL_HELGRIND"));
512 }
513
514
515 bool server_startup(server_startup_st& construct, const std::string& server_type, in_port_t try_port, int argc, const char *argv[])
516 {
517 Outn();
518
519 // Look to see if we are being provided ports to use
520 {
521 char variable_buffer[1024];
522 snprintf(variable_buffer, sizeof(variable_buffer), "LIBTEST_PORT_%lu", (unsigned long)construct.count());
523
524 char *var;
525 if ((var= getenv(variable_buffer)))
526 {
527 in_port_t tmp= in_port_t(atoi(var));
528
529 if (tmp > 0)
530 try_port= tmp;
531 }
532 }
533
534 Server *server= NULL;
535 if (0)
536 { }
537 else if (server_type.compare("gearmand") == 0)
538 {
539 #ifdef GEARMAND_BINARY
540 #ifdef HAVE_LIBGEARMAN
541 server= build_gearmand("localhost", try_port);
542 #else
543 Error << "Libgearman was not found";
544 #endif
545 #else
546 Error << "No gearmand binary is available";
547 #endif
548 }
549 else if (server_type.compare("memcached") == 0)
550 {
551 #ifdef MEMCACHED_BINARY
552 #ifdef HAVE_LIBMEMCACHED
553 server= build_memcached("localhost", try_port);
554 #else
555 Error << "Libmemcached was not found";
556 #endif
557 #else
558 Error << "No memcached binary is available";
559 #endif
560 }
561 else
562 {
563 Error << "Failed to start " << server_type << ", no support was found to be compiled in for it.";
564 }
565
566 if (server == NULL)
567 {
568 Error << "Failure occured while creating server: " << server_type;
569 return false;
570 }
571
572 /*
573 We will now cycle the server we have created.
574 */
575 if (not server->cycle())
576 {
577 Error << "Could not start up server " << *server;
578 delete server;
579 return false;
580 }
581
582 server->build(argc, argv);
583
584 if (construct.is_debug())
585 {
586 Out << "Pausing for startup, hit return when ready.";
587 std::string gdb_command= server->base_command();
588 std::string options;
589 Out << "run " << server->args(options);
590 getchar();
591 }
592 else if (not server->start())
593 {
594 Error << "Failed to start " << *server;
595 delete server;
596 return false;
597 }
598 else
599 {
600 Out << "STARTING SERVER(pid:" << server->pid() << "): " << server->running();
601 }
602
603 construct.push_server(server);
604
605 if (default_port() == 0)
606 {
607 assert(server->has_port());
608 set_default_port(server->port());
609 }
610
611 Outn();
612
613 return true;
614 }
615
616 bool server_startup_st::start_socket_server(const std::string& server_type, const in_port_t try_port, int argc, const char *argv[])
617 {
618 (void)try_port;
619 Outn();
620
621 Server *server= NULL;
622 if (0)
623 { }
624 else if (server_type.compare("gearmand") == 0)
625 {
626 Error << "Socket files are not supported for gearmand yet";
627 }
628 else if (server_type.compare("memcached") == 0)
629 {
630 #ifdef MEMCACHED_BINARY
631 #ifdef HAVE_LIBMEMCACHED
632 server= build_memcached_socket("localhost", try_port);
633 #else
634 Error << "Libmemcached was not found";
635 #endif
636 #else
637 Error << "No memcached binary is available";
638 #endif
639 }
640 else
641 {
642 Error << "Failed to start " << server_type << ", no support was found to be compiled in for it.";
643 }
644
645 if (server == NULL)
646 {
647 Error << "Failure occured while creating server: " << server_type;
648 return false;
649 }
650
651 /*
652 We will now cycle the server we have created.
653 */
654 if (not server->cycle())
655 {
656 Error << "Could not start up server " << *server;
657 delete server;
658 return false;
659 }
660
661 server->build(argc, argv);
662
663 if (is_debug())
664 {
665 Out << "Pausing for startup, hit return when ready.";
666 std::string gdb_command= server->base_command();
667 std::string options;
668 Out << "run " << server->args(options);
669 getchar();
670 }
671 else if (not server->start())
672 {
673 Error << "Failed to start " << *server;
674 delete server;
675 return false;
676 }
677 else
678 {
679 Out << "STARTING SERVER(pid:" << server->pid() << "): " << server->running();
680 }
681
682 push_server(server);
683
684 set_default_socket(server->socket().c_str());
685
686 Outn();
687
688 return true;
689 }
690
691 } // namespace libtest