Update from libtest 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 <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 #ifdef HAVE_LIBGEARMAN
46 #include <libtest/gearmand.h>
47 #include <libtest/blobslap_worker.h>
48 #endif
49
50 #ifdef HAVE_LIBMEMCACHED
51 #include <libtest/memcached.h>
52 #endif
53
54 extern "C" {
55 static bool exited_successfully(int status)
56 {
57 if (WEXITSTATUS(status) == 0)
58 {
59 return true;
60 }
61
62 return true;
63 }
64 }
65
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 int ret= system(_running.c_str());
197 if (not exited_successfully(ret))
198 {
199 Error << "system() failed:" << strerror(errno);
200 _running.clear();
201 return false;
202 }
203
204 if (is_helgrind())
205 {
206 sleep(4);
207 }
208
209 if (pid_file_option() and not pid_file().empty())
210 {
211 Wait wait(pid_file());
212
213 if (not wait.successful())
214 {
215 Error << "Unable to open pidfile: " << pid_file();
216 }
217 }
218
219 int count= is_helgrind() ? 20 : 5;
220 while (not ping() and --count)
221 {
222 nap();
223 }
224
225 if (count == 0)
226 {
227 Error << "Failed to ping() server once started:" << *this;
228 _running.clear();
229 return false;
230 }
231
232 // A failing get_pid() at this point is considered an error
233 _pid= get_pid(true);
234
235 return has_pid();
236 }
237
238 void Server::reset_pid()
239 {
240 _running.clear();
241 _pid_file.clear();
242 _pid= -1;
243 }
244
245 pid_t Server::pid()
246 {
247 return _pid;
248 }
249
250 bool Server::set_socket_file()
251 {
252 char file_buffer[FILENAME_MAX];
253 file_buffer[0]= 0;
254
255 if (broken_pid_file())
256 {
257 snprintf(file_buffer, sizeof(file_buffer), "/tmp/%s.socketXXXXXX", name());
258 }
259 else
260 {
261 snprintf(file_buffer, sizeof(file_buffer), "tests/var/run/%s.socketXXXXXX", name());
262 }
263
264 int fd;
265 if ((fd= mkstemp(file_buffer)) == -1)
266 {
267 perror(file_buffer);
268 return false;
269 }
270 close(fd);
271 unlink(file_buffer);
272
273 _socket= file_buffer;
274
275 return true;
276 }
277
278 bool Server::set_pid_file()
279 {
280 char file_buffer[FILENAME_MAX];
281 file_buffer[0]= 0;
282
283 if (broken_pid_file())
284 {
285 snprintf(file_buffer, sizeof(file_buffer), "/tmp/%s.pidXXXXXX", name());
286 }
287 else
288 {
289 snprintf(file_buffer, sizeof(file_buffer), "tests/var/run/%s.pidXXXXXX", name());
290 }
291
292 int fd;
293 if ((fd= mkstemp(file_buffer)) == -1)
294 {
295 perror(file_buffer);
296 return false;
297 }
298 close(fd);
299 unlink(file_buffer);
300
301 _pid_file= file_buffer;
302
303 return true;
304 }
305
306 bool Server::set_log_file()
307 {
308 char file_buffer[FILENAME_MAX];
309 file_buffer[0]= 0;
310
311 snprintf(file_buffer, sizeof(file_buffer), "tests/var/log/%s.logXXXXXX", name());
312 int fd;
313 if ((fd= mkstemp(file_buffer)) == -1)
314 {
315 perror(file_buffer);
316 return false;
317 }
318 close(fd);
319
320 _log_file= file_buffer;
321
322 return true;
323 }
324
325 void Server::rebuild_base_command()
326 {
327 _base_command.clear();
328 if (is_libtool() and getenv("LIBTOOL_COMMAND"))
329 {
330 _base_command+= getenv("LIBTOOL_COMMAND");
331 _base_command+= " ";
332 }
333
334 if (is_debug() and getenv("GDB_COMMAND"))
335 {
336 _base_command+= getenv("GDB_COMMAND");
337 _base_command+= " ";
338 }
339 else if (is_valgrind() and getenv("VALGRIND_COMMAND"))
340 {
341 _base_command+= getenv("VALGRIND_COMMAND");
342 _base_command+= " ";
343 }
344 else if (is_helgrind() and getenv("HELGRIND_COMMAND"))
345 {
346 _base_command+= getenv("HELGRIND_COMMAND");
347 _base_command+= " ";
348 }
349
350 _base_command+= executable();
351 }
352
353 void Server::set_extra_args(const std::string &arg)
354 {
355 _extra_args= arg;
356 }
357
358 bool Server::args(std::string& options)
359 {
360 std::stringstream arg_buffer;
361
362 // Set a log file if it was requested (and we can)
363 if (getenv("LIBTEST_LOG") and log_file_option())
364 {
365 if (not set_log_file())
366 {
367 return false;
368 }
369
370 arg_buffer << " " << log_file_option() << _log_file;
371 }
372
373 // Update pid_file
374 if (pid_file_option())
375 {
376 if (not set_pid_file())
377 {
378 return false;
379 }
380
381 arg_buffer << " " << pid_file_option() << pid_file();
382 }
383
384 assert(daemon_file_option());
385 if (daemon_file_option() and not is_valgrind() and not is_helgrind())
386 {
387 arg_buffer << " " << daemon_file_option();
388 }
389
390 if (_is_socket and socket_file_option())
391 {
392 if (not set_socket_file())
393 {
394 return false;
395 }
396
397 arg_buffer << " " << socket_file_option() << "\"" << _socket << "\"";
398 }
399
400 assert(port_option());
401 if (port_option() and _port > 0)
402 {
403 arg_buffer << " " << port_option() << _port;
404 }
405
406 options+= arg_buffer.str();
407
408 if (not _extra_args.empty())
409 {
410 options+= _extra_args;
411 }
412
413 return true;
414 }
415
416 bool Server::is_debug() const
417 {
418 return bool(getenv("LIBTEST_MANUAL_GDB"));
419 }
420
421 bool Server::is_valgrind() const
422 {
423 return bool(getenv("LIBTEST_MANUAL_VALGRIND"));
424 }
425
426 bool Server::is_helgrind() const
427 {
428 return bool(getenv("LIBTEST_MANUAL_HELGRIND"));
429 }
430
431 bool Server::kill(pid_t pid_arg)
432 {
433 if (check_pid(pid_arg) and kill_pid(pid_arg)) // If we kill it, reset
434 {
435 if (broken_pid_file() and not pid_file().empty())
436 {
437 unlink(pid_file().c_str());
438 }
439
440 reset_pid();
441
442 return true;
443 }
444
445 return false;
446 }
447
448 void server_startup_st::push_server(Server *arg)
449 {
450 servers.push_back(arg);
451
452 char port_str[NI_MAXSERV];
453 snprintf(port_str, sizeof(port_str), "%u", int(arg->port()));
454
455 std::string server_config_string;
456 if (arg->has_socket())
457 {
458 server_config_string+= "--socket=";
459 server_config_string+= '"';
460 server_config_string+= arg->socket();
461 server_config_string+= '"';
462 server_config_string+= " ";
463 }
464 else
465 {
466 server_config_string+= "--server=";
467 server_config_string+= arg->hostname();
468 server_config_string+= ":";
469 server_config_string+= port_str;
470 server_config_string+= " ";
471 }
472
473 server_list+= server_config_string;
474
475 }
476
477 Server* server_startup_st::pop_server()
478 {
479 Server *tmp= servers.back();
480 servers.pop_back();
481 return tmp;
482 }
483
484 void server_startup_st::shutdown(bool remove)
485 {
486 if (remove)
487 {
488 for (std::vector<Server *>::iterator iter= servers.begin(); iter != servers.end(); iter++)
489 {
490 delete *iter;
491 }
492 servers.clear();
493 }
494 else
495 {
496 for (std::vector<Server *>::iterator iter= servers.begin(); iter != servers.end(); iter++)
497 {
498 if ((*iter)->has_pid() and not (*iter)->kill((*iter)->pid()))
499 {
500 Error << "Unable to kill:" << *(*iter);
501 }
502 }
503 }
504 }
505
506 server_startup_st::~server_startup_st()
507 {
508 shutdown(true);
509 }
510
511 bool server_startup_st::is_debug() const
512 {
513 return bool(getenv("LIBTEST_MANUAL_GDB"));
514 }
515
516 bool server_startup_st::is_valgrind() const
517 {
518 return bool(getenv("LIBTEST_MANUAL_VALGRIND"));
519 }
520
521 bool server_startup_st::is_helgrind() const
522 {
523 return bool(getenv("LIBTEST_MANUAL_HELGRIND"));
524 }
525
526
527 bool server_startup(server_startup_st& construct, const std::string& server_type, in_port_t try_port, int argc, const char *argv[])
528 {
529 Outn();
530 (void)try_port;
531
532 // Look to see if we are being provided ports to use
533 {
534 char variable_buffer[1024];
535 snprintf(variable_buffer, sizeof(variable_buffer), "LIBTEST_PORT_%lu", (unsigned long)construct.count());
536
537 char *var;
538 if ((var= getenv(variable_buffer)))
539 {
540 in_port_t tmp= in_port_t(atoi(var));
541
542 if (tmp > 0)
543 try_port= tmp;
544 }
545 }
546
547 Server *server= NULL;
548 if (0)
549 { }
550 else if (server_type.compare("gearmand") == 0)
551 {
552 #ifdef GEARMAND_BINARY
553 #ifdef HAVE_LIBGEARMAN
554 server= build_gearmand("localhost", try_port);
555 #else
556 Error << "Libgearman was not found";
557 #endif
558 #else
559 Error << "No gearmand binary is available";
560 #endif
561 }
562 else if (server_type.compare("blobslap_worker") == 0)
563 {
564 #ifdef GEARMAND_BINARY
565 #ifdef HAVE_LIBGEARMAN
566 server= build_blobslap_worker(try_port);
567 #else
568 Error << "Libgearman was not found";
569 #endif
570 #else
571 Error << "No gearmand binary is available";
572 #endif
573 }
574 else if (server_type.compare("memcached") == 0)
575 {
576 #ifdef MEMCACHED_BINARY
577 #ifdef HAVE_LIBMEMCACHED
578 server= build_memcached("localhost", try_port);
579 #else
580 Error << "Libmemcached was not found";
581 #endif
582 #else
583 Error << "No memcached binary is available";
584 #endif
585 }
586 else
587 {
588 Error << "Failed to start " << server_type << ", no support was found to be compiled in for it.";
589 }
590
591 if (server == NULL)
592 {
593 Error << "Failure occured while creating server: " << server_type;
594 return false;
595 }
596
597 /*
598 We will now cycle the server we have created.
599 */
600 if (not server->cycle())
601 {
602 Error << "Could not start up server " << *server;
603 delete server;
604 return false;
605 }
606
607 server->build(argc, argv);
608
609 if (construct.is_debug())
610 {
611 Out << "Pausing for startup, hit return when ready.";
612 std::string gdb_command= server->base_command();
613 std::string options;
614 Out << "run " << server->args(options);
615 getchar();
616 }
617 else if (not server->start())
618 {
619 Error << "Failed to start " << *server;
620 delete server;
621 return false;
622 }
623 else
624 {
625 Out << "STARTING SERVER(pid:" << server->pid() << "): " << server->running();
626 }
627
628 construct.push_server(server);
629
630 if (default_port() == 0)
631 {
632 assert(server->has_port());
633 set_default_port(server->port());
634 }
635
636 Outn();
637
638 return true;
639 }
640
641 bool server_startup_st::start_socket_server(const std::string& server_type, const in_port_t try_port, int argc, const char *argv[])
642 {
643 (void)try_port;
644 Outn();
645
646 Server *server= NULL;
647 if (0)
648 { }
649 else if (server_type.compare("gearmand") == 0)
650 {
651 Error << "Socket files are not supported for gearmand yet";
652 }
653 else if (server_type.compare("memcached") == 0)
654 {
655 #ifdef MEMCACHED_BINARY
656 #ifdef HAVE_LIBMEMCACHED
657 server= build_memcached_socket("localhost", try_port);
658 #else
659 Error << "Libmemcached was not found";
660 #endif
661 #else
662 Error << "No memcached binary is available";
663 #endif
664 }
665 else
666 {
667 Error << "Failed to start " << server_type << ", no support was found to be compiled in for it.";
668 }
669
670 if (server == NULL)
671 {
672 Error << "Failure occured while creating server: " << server_type;
673 return false;
674 }
675
676 /*
677 We will now cycle the server we have created.
678 */
679 if (not server->cycle())
680 {
681 Error << "Could not start up server " << *server;
682 delete server;
683 return false;
684 }
685
686 server->build(argc, argv);
687
688 if (is_debug())
689 {
690 Out << "Pausing for startup, hit return when ready.";
691 std::string gdb_command= server->base_command();
692 std::string options;
693 Out << "run " << server->args(options);
694 getchar();
695 }
696 else if (not server->start())
697 {
698 Error << "Failed to start " << *server;
699 delete server;
700 return false;
701 }
702 else
703 {
704 Out << "STARTING SERVER(pid:" << server->pid() << "): " << server->running();
705 }
706
707 push_server(server);
708
709 set_default_socket(server->socket().c_str());
710
711 Outn();
712
713 return true;
714 }
715
716 } // namespace libtest