Merge in sasl update
[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)
47 {
48 if (WEXITSTATUS(status) == 0)
49 {
50 return true;
51 }
52
53 return true;
54 }
55 }
56
57
58 namespace libtest {
59
60 std::ostream& operator<<(std::ostream& output, const Server &arg)
61 {
62 if (arg.is_socket())
63 {
64 output << arg.hostname();
65 }
66 else
67 {
68 output << arg.hostname() << ":" << arg.port();
69 }
70
71 if (arg.has_pid())
72 {
73 output << " Pid:" << arg.pid();
74 }
75
76 if (arg.has_socket())
77 {
78 output << " Socket:" << arg.socket();
79 }
80
81 if (not arg.running().empty())
82 {
83 output << " Exec:" << arg.running();
84 }
85
86
87 return output; // for multiple << operators
88 }
89
90 void Server::nap(void)
91 {
92 #ifdef WIN32
93 sleep(1);
94 #else
95 struct timespec global_sleep_value= { 0, 50000 };
96 nanosleep(&global_sleep_value, NULL);
97 #endif
98 }
99
100 Server::Server(const std::string& host_arg, const in_port_t port_arg, bool is_socket_arg) :
101 _is_socket(is_socket_arg),
102 _pid(-1),
103 _port(port_arg),
104 _hostname(host_arg)
105 {
106 }
107
108 Server::~Server()
109 {
110 if (has_pid() and not kill(_pid))
111 {
112 Error << "Unable to kill:" << *this;
113 }
114 }
115
116 std::string server_startup_st::option_string() const
117 {
118 std::string temp= server_list;
119 rtrim(temp);
120 return temp;
121 }
122
123 // If the server exists, kill it
124 bool Server::cycle()
125 {
126 uint32_t limit= 3;
127
128 // Try to ping, and kill the server #limit number of times
129 pid_t current_pid;
130 while (--limit and is_pid_valid(current_pid= get_pid()))
131 {
132 if (kill(current_pid))
133 {
134 Log << "Killed existing server," << *this << " with pid:" << current_pid;
135 nap();
136 continue;
137 }
138 }
139
140 // For whatever reason we could not kill it, and we reached limit
141 if (limit == 0)
142 {
143 Error << "Reached limit, could not kill server pid:" << current_pid;
144 return false;
145 }
146
147 return true;
148 }
149
150 // Grab a one off command
151 bool Server::command(std::string& command_arg)
152 {
153 rebuild_base_command();
154
155 command_arg+= _base_command;
156
157 if (args(command_arg))
158 {
159 return true;
160 }
161
162 return false;
163 }
164
165 bool Server::start()
166 {
167 // If we find that we already have a pid then kill it.
168 if (has_pid() and not kill(_pid))
169 {
170 Error << "Could not kill() existing server during start() pid:" << _pid;
171 return false;
172 }
173 assert(not has_pid());
174
175 _running.clear();
176 if (not command(_running))
177 {
178 Error << "Could not build command()";
179 return false;
180 }
181
182 if (is_valgrind() or is_helgrind())
183 {
184 _running+= " &";
185 }
186
187 int ret= system(_running.c_str());
188 if (not exited_successfully(ret))
189 {
190 Error << "system() failed:" << strerror(errno);
191 _running.clear();
192 return false;
193 }
194
195 if (is_helgrind() or is_valgrind())
196 {
197 sleep(4);
198 }
199
200 if (pid_file_option() and not pid_file().empty())
201 {
202 Wait wait(pid_file(), 8);
203
204 if (not wait.successful())
205 {
206 Error << "Unable to open pidfile for: " << _running;
207 }
208 }
209
210 int count= is_helgrind() or is_valgrind() ? 20 : 5;
211 while (not ping() and --count)
212 {
213 nap();
214 }
215
216 if (count == 0)
217 {
218 // If we happen to have a pid file, lets try to kill it
219 if (pid_file_option() and not pid_file().empty())
220 {
221 kill_file(pid_file());
222 }
223 Error << "Failed to ping() server started with:" << _running;
224 _running.clear();
225 return false;
226 }
227
228 // A failing get_pid() at this point is considered an error
229 _pid= get_pid(true);
230
231 return has_pid();
232 }
233
234 void Server::reset_pid()
235 {
236 _running.clear();
237 _pid_file.clear();
238 _pid= -1;
239 }
240
241 pid_t Server::pid()
242 {
243 return _pid;
244 }
245
246 bool Server::set_socket_file()
247 {
248 char file_buffer[FILENAME_MAX];
249 file_buffer[0]= 0;
250
251 if (broken_pid_file())
252 {
253 snprintf(file_buffer, sizeof(file_buffer), "/tmp/%s.socketXXXXXX", name());
254 }
255 else
256 {
257 snprintf(file_buffer, sizeof(file_buffer), "var/run/%s.socketXXXXXX", name());
258 }
259
260 int fd;
261 if ((fd= mkstemp(file_buffer)) == -1)
262 {
263 perror(file_buffer);
264 return false;
265 }
266 close(fd);
267 unlink(file_buffer);
268
269 _socket= file_buffer;
270
271 return true;
272 }
273
274 bool Server::set_pid_file()
275 {
276 char file_buffer[FILENAME_MAX];
277 file_buffer[0]= 0;
278
279 if (broken_pid_file())
280 {
281 snprintf(file_buffer, sizeof(file_buffer), "/tmp/%s.pidXXXXXX", name());
282 }
283 else
284 {
285 snprintf(file_buffer, sizeof(file_buffer), "var/run/%s.pidXXXXXX", name());
286 }
287
288 int fd;
289 if ((fd= mkstemp(file_buffer)) == -1)
290 {
291 perror(file_buffer);
292 return false;
293 }
294 close(fd);
295 unlink(file_buffer);
296
297 _pid_file= file_buffer;
298
299 return true;
300 }
301
302 bool Server::set_log_file()
303 {
304 char file_buffer[FILENAME_MAX];
305 file_buffer[0]= 0;
306
307 snprintf(file_buffer, sizeof(file_buffer), "var/log/%s.logXXXXXX", name());
308 int fd;
309 if ((fd= mkstemp(file_buffer)) == -1)
310 {
311 perror(file_buffer);
312 return false;
313 }
314 close(fd);
315
316 _log_file= file_buffer;
317
318 return true;
319 }
320
321 void Server::rebuild_base_command()
322 {
323 _base_command.clear();
324 if (is_libtool())
325 {
326 _base_command+= libtool();
327 }
328
329 if (is_debug() and getenv("GDB_COMMAND"))
330 {
331 _base_command+= getenv("GDB_COMMAND");
332 _base_command+= " ";
333 }
334 else if (is_valgrind() and getenv("VALGRIND_COMMAND"))
335 {
336 _base_command+= getenv("VALGRIND_COMMAND");
337 _base_command+= " ";
338 }
339 else if (is_helgrind() and getenv("HELGRIND_COMMAND"))
340 {
341 _base_command+= getenv("HELGRIND_COMMAND");
342 _base_command+= " ";
343 }
344
345 _base_command+= executable();
346 }
347
348 void Server::set_extra_args(const std::string &arg)
349 {
350 _extra_args= arg;
351 }
352
353 bool Server::args(std::string& options)
354 {
355 std::stringstream arg_buffer;
356
357 // Set a log file if it was requested (and we can)
358 if (getenv("LIBTEST_LOG") and log_file_option())
359 {
360 if (not set_log_file())
361 {
362 return false;
363 }
364
365 arg_buffer << " " << log_file_option() << _log_file;
366 }
367
368 // Update pid_file
369 if (pid_file_option())
370 {
371 if (_pid_file.empty() and not set_pid_file())
372 {
373 return false;
374 }
375
376 arg_buffer << " " << pid_file_option() << pid_file();
377 }
378
379 assert(daemon_file_option());
380 if (daemon_file_option() and not is_valgrind() and not is_helgrind())
381 {
382 arg_buffer << " " << daemon_file_option();
383 }
384
385 if (_is_socket and socket_file_option())
386 {
387 if (not set_socket_file())
388 {
389 return false;
390 }
391
392 arg_buffer << " " << socket_file_option() << "\"" << _socket << "\"";
393 }
394
395 assert(port_option());
396 if (port_option() and _port > 0)
397 {
398 arg_buffer << " " << port_option() << _port;
399 }
400
401 options+= arg_buffer.str();
402
403 if (not _extra_args.empty())
404 {
405 options+= _extra_args;
406 }
407
408 return true;
409 }
410
411 bool Server::is_debug() const
412 {
413 return bool(getenv("LIBTEST_MANUAL_GDB"));
414 }
415
416 bool Server::is_valgrind() const
417 {
418 return bool(getenv("LIBTEST_MANUAL_VALGRIND"));
419 }
420
421 bool Server::is_helgrind() const
422 {
423 return bool(getenv("LIBTEST_MANUAL_HELGRIND"));
424 }
425
426 bool Server::kill(pid_t pid_arg)
427 {
428 if (check_pid(pid_arg) and kill_pid(pid_arg)) // If we kill it, reset
429 {
430 if (broken_pid_file() and not pid_file().empty())
431 {
432 unlink(pid_file().c_str());
433 }
434
435 if (broken_socket_cleanup() and has_socket() and not socket().empty())
436 {
437 unlink(socket().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 libtest::Server *server= NULL;
548 if (0)
549 { }
550 else if (server_type.compare("gearmand") == 0)
551 {
552 if (GEARMAND_BINARY)
553 {
554 if (HAVE_LIBGEARMAN)
555 {
556 server= build_gearmand("localhost", try_port);
557 }
558 else
559 {
560 Error << "Libgearman was not found";
561 }
562 }
563 else
564 {
565 Error << "No gearmand binary is available";
566 }
567 }
568 else if (server_type.compare("blobslap_worker") == 0)
569 {
570 if (GEARMAND_BINARY and GEARMAND_BLOBSLAP_WORKER)
571 {
572 if (HAVE_LIBGEARMAN)
573 {
574 server= build_blobslap_worker(try_port);
575 }
576 else
577 {
578 Error << "Libgearman was not found";
579 }
580 }
581 else
582 {
583 Error << "No gearmand binary is available";
584 }
585 }
586 else if (server_type.compare("memcached-sasl") == 0)
587 {
588 if (MEMCACHED_SASL_BINARY)
589 {
590 if (HAVE_LIBMEMCACHED)
591 {
592 server= build_memcached_sasl("localhost", try_port, construct.username(), construct.password());
593 }
594 else
595 {
596 Error << "Libmemcached was not found";
597 }
598 }
599 else
600 {
601 Error << "No memcached binary that was compiled with sasl is available";
602 }
603 }
604 else if (server_type.compare("memcached") == 0)
605 {
606 if (MEMCACHED_BINARY)
607 {
608 if (HAVE_LIBMEMCACHED)
609 {
610 server= build_memcached("localhost", try_port);
611 }
612 else
613 {
614 Error << "Libmemcached was not found";
615 }
616 }
617 else
618 {
619 Error << "No memcached binary is available";
620 }
621 }
622 else
623 {
624 Error << "Failed to start " << server_type << ", no support was found to be compiled in for it.";
625 }
626
627 if (server == NULL)
628 {
629 Error << "Failure occured while creating server: " << server_type;
630 return false;
631 }
632
633 /*
634 We will now cycle the server we have created.
635 */
636 if (not server->cycle())
637 {
638 Error << "Could not start up server " << *server;
639 delete server;
640 return false;
641 }
642
643 server->build(argc, argv);
644
645 if (construct.is_debug())
646 {
647 Out << "Pausing for startup, hit return when ready.";
648 std::string gdb_command= server->base_command();
649 std::string options;
650 Out << "run " << server->args(options);
651 getchar();
652 }
653 else if (not server->start())
654 {
655 Error << "Failed to start " << *server;
656 delete server;
657 return false;
658 }
659 else
660 {
661 Out << "STARTING SERVER(pid:" << server->pid() << "): " << server->running();
662 }
663
664 construct.push_server(server);
665
666 if (default_port() == 0)
667 {
668 assert(server->has_port());
669 set_default_port(server->port());
670 }
671
672 Outn();
673
674 return true;
675 }
676
677 bool server_startup_st::start_socket_server(const std::string& server_type, const in_port_t try_port, int argc, const char *argv[])
678 {
679 (void)try_port;
680 Outn();
681
682 Server *server= NULL;
683 if (0)
684 { }
685 else if (server_type.compare("gearmand") == 0)
686 {
687 Error << "Socket files are not supported for gearmand yet";
688 }
689 else if (server_type.compare("memcached-sasl") == 0)
690 {
691 if (MEMCACHED_SASL_BINARY)
692 {
693 if (HAVE_LIBMEMCACHED)
694 {
695 server= build_memcached_sasl_socket("localhost", try_port, username(), password());
696 }
697 else
698 {
699 Error << "Libmemcached was not found";
700 }
701 }
702 else
703 {
704 Error << "No memcached binary is available";
705 }
706 }
707 else if (server_type.compare("memcached") == 0)
708 {
709 if (MEMCACHED_BINARY)
710 {
711 if (HAVE_LIBMEMCACHED)
712 {
713 server= build_memcached_socket("localhost", try_port);
714 }
715 else
716 {
717 Error << "Libmemcached was not found";
718 }
719 }
720 else
721 {
722 Error << "No memcached binary is available";
723 }
724 }
725 else
726 {
727 Error << "Failed to start " << server_type << ", no support was found to be compiled in for it.";
728 }
729
730 if (server == NULL)
731 {
732 Error << "Failure occured while creating server: " << server_type;
733 return false;
734 }
735
736 /*
737 We will now cycle the server we have created.
738 */
739 if (not server->cycle())
740 {
741 Error << "Could not start up server " << *server;
742 delete server;
743 return false;
744 }
745
746 server->build(argc, argv);
747
748 if (is_debug())
749 {
750 Out << "Pausing for startup, hit return when ready.";
751 std::string gdb_command= server->base_command();
752 std::string options;
753 Out << "run " << server->args(options);
754 getchar();
755 }
756 else if (not server->start())
757 {
758 Error << "Failed to start " << *server;
759 delete server;
760 return false;
761 }
762 else
763 {
764 Out << "STARTING SERVER(pid:" << server->pid() << "): " << server->running();
765 }
766
767 push_server(server);
768
769 set_default_socket(server->socket().c_str());
770
771 Outn();
772
773 return true;
774 }
775
776 } // namespace libtest