Merge lp:~tangent-org/libmemcached/1.0-build Build: jenkins-Libmemcached-1.0-134
[awesomized/libmemcached] / libtest / cmdline.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Data Differential YATL (i.e. libtest) library
4 *
5 * Copyright (C) 2012 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 "mem_config.h"
38 #include "libtest/common.h"
39
40 using namespace libtest;
41
42 #include <cstdlib>
43 #include <cstring>
44 #include <cerrno>
45 #include <fcntl.h>
46 #include <fstream>
47 #include <memory>
48 #ifdef HAVE_POLL_H
49 # include <poll.h>
50 #endif
51 #ifdef HAVE_SPAWN_H
52 # include <spawn.h>
53 #endif
54 #include <sstream>
55 #include <string>
56 #include <sys/stat.h>
57 #include <sys/types.h>
58 #include <unistd.h>
59
60 #include <algorithm>
61
62 #ifndef __USE_GNU
63 static char **environ= NULL;
64 #endif
65
66 #ifndef FD_CLOEXEC
67 # define FD_CLOEXEC 0
68 #endif
69
70 namespace {
71
72 std::string print_argv(libtest::vchar_ptr_t& built_argv)
73 {
74 std::stringstream arg_buffer;
75
76 for (vchar_ptr_t::iterator iter= built_argv.begin();
77 iter == built_argv.end();
78 iter++)
79 {
80 arg_buffer << *iter << " ";
81 }
82
83 return arg_buffer.str();
84 }
85
86 #if 0
87 std::string print_argv(char** argv)
88 {
89 std::stringstream arg_buffer;
90
91 for (char** ptr= argv; *ptr; ++ptr)
92 {
93 arg_buffer << *ptr << " ";
94 }
95
96 return arg_buffer.str();
97 }
98 #endif
99
100 static Application::error_t int_to_error_t(int arg)
101 {
102 switch (arg)
103 {
104 case 127:
105 return Application::INVALID_POSIX_SPAWN;
106
107 case 0:
108 return Application::SUCCESS;
109
110 case 1:
111 return Application::FAILURE;
112
113 default:
114 return Application::UNKNOWN;
115 }
116 }
117 }
118
119 namespace libtest {
120
121 Application::Application(const std::string& arg, const bool _use_libtool_arg) :
122 _use_libtool(_use_libtool_arg),
123 _use_valgrind(false),
124 _use_gdb(false),
125 _use_ptrcheck(false),
126 _will_fail(false),
127 _argc(0),
128 _exectuble(arg),
129 stdin_fd(STDIN_FILENO),
130 stdout_fd(STDOUT_FILENO),
131 stderr_fd(STDERR_FILENO),
132 _pid(-1),
133 _status(0),
134 _app_exit_state(UNINITIALIZED)
135 {
136 if (_use_libtool)
137 {
138 if (libtool() == NULL)
139 {
140 fatal_message("libtool requested, but know libtool was found");
141 }
142 }
143
144 // Find just the name of the application with no path
145 {
146 size_t found= arg.find_last_of("/\\");
147 if (found)
148 {
149 _exectuble_name= arg.substr(found +1);
150 }
151 else
152 {
153 _exectuble_name= arg;
154 }
155 }
156
157 if (_use_libtool and getenv("PWD"))
158 {
159 _exectuble_with_path+= getenv("PWD");
160 _exectuble_with_path+= "/";
161 }
162 _exectuble_with_path+= _exectuble;
163 }
164
165 Application::~Application()
166 {
167 murder();
168 delete_argv();
169 }
170
171 Application::error_t Application::run(const char *args[])
172 {
173 stdin_fd.reset();
174 stdout_fd.reset();
175 stderr_fd.reset();
176 _stdout_buffer.clear();
177 _stderr_buffer.clear();
178
179 posix_spawn_file_actions_t file_actions;
180 posix_spawn_file_actions_init(&file_actions);
181
182 stdin_fd.dup_for_spawn(file_actions);
183 stdout_fd.dup_for_spawn(file_actions);
184 stderr_fd.dup_for_spawn(file_actions);
185
186 posix_spawnattr_t spawnattr;
187 posix_spawnattr_init(&spawnattr);
188
189 short flags= 0;
190
191 // Child should not block signals
192 flags |= POSIX_SPAWN_SETSIGMASK;
193
194 sigset_t mask;
195 sigemptyset(&mask);
196
197 fatal_assert(posix_spawnattr_setsigmask(&spawnattr, &mask) == 0);
198
199 #if defined(POSIX_SPAWN_USEVFORK) || defined(__linux__)
200 // Use USEVFORK on linux
201 flags |= POSIX_SPAWN_USEVFORK;
202 #endif
203
204 flags |= POSIX_SPAWN_SETPGROUP;
205 fatal_assert(posix_spawnattr_setpgroup(&spawnattr, 0) == 0);
206
207 fatal_assert(posix_spawnattr_setflags(&spawnattr, flags) == 0);
208
209 create_argv(args);
210
211 int spawn_ret;
212 if (_use_gdb)
213 {
214 std::string gdb_run_file= create_tmpfile(_exectuble_name);
215 std::fstream file_stream;
216 file_stream.open(gdb_run_file.c_str(), std::fstream::out | std::fstream::trunc);
217
218 _gdb_filename= create_tmpfile(_exectuble_name);
219 file_stream
220 << "set logging redirect on" << std::endl
221 << "set logging file " << _gdb_filename << std::endl
222 << "set logging overwrite on" << std::endl
223 << "set logging on" << std::endl
224 << "set environment LIBTEST_IN_GDB=1" << std::endl
225 << "run " << arguments() << std::endl
226 << "thread apply all bt" << std::endl
227 << "quit" << std::endl;
228
229 fatal_assert(file_stream.good());
230 file_stream.close();
231
232 if (_use_libtool)
233 {
234 // libtool --mode=execute gdb -f -x binary
235 char *argv[]= {
236 const_cast<char *>(libtool()),
237 const_cast<char *>("--mode=execute"),
238 const_cast<char *>("gdb"),
239 const_cast<char *>("-batch"),
240 const_cast<char *>("-f"),
241 const_cast<char *>("-x"),
242 const_cast<char *>(gdb_run_file.c_str()),
243 const_cast<char *>(_exectuble_with_path.c_str()),
244 0};
245
246 spawn_ret= posix_spawnp(&_pid, libtool(), &file_actions, &spawnattr, argv, environ);
247 }
248 else
249 {
250 // gdb binary
251 char *argv[]= {
252 const_cast<char *>("gdb"),
253 const_cast<char *>("-batch"),
254 const_cast<char *>("-f"),
255 const_cast<char *>("-x"),
256 const_cast<char *>(gdb_run_file.c_str()),
257 const_cast<char *>(_exectuble_with_path.c_str()),
258 0};
259 spawn_ret= posix_spawnp(&_pid, "gdb", &file_actions, &spawnattr, argv, environ);
260 }
261 }
262 else
263 {
264 spawn_ret= posix_spawn(&_pid, built_argv[0], &file_actions, &spawnattr, &built_argv[0], NULL);
265 }
266
267 posix_spawn_file_actions_destroy(&file_actions);
268 posix_spawnattr_destroy(&spawnattr);
269
270 stdin_fd.close(Application::Pipe::READ);
271 stdout_fd.close(Application::Pipe::WRITE);
272 stderr_fd.close(Application::Pipe::WRITE);
273
274 if (spawn_ret != 0)
275 {
276 if (_will_fail == false)
277 {
278 Error << strerror(spawn_ret) << "(" << spawn_ret << ")";
279 }
280 _pid= -1;
281 return Application::INVALID_POSIX_SPAWN;
282 }
283
284 assert(_pid != -1);
285 if (_pid == -1)
286 {
287 return Application::INVALID_POSIX_SPAWN;
288 }
289
290 #if 0
291 app_thread_st* _app_thread= new app_thread_st(_pid, _status, built_argv[0], _app_exit_state);
292 int error;
293 if ((error= pthread_create(&_thread, NULL, &app_thread, _app_thread)) != 0)
294 {
295 Error << "pthread_create() died during pthread_create(" << strerror(error) << ")";
296 return Application::FAILURE;
297 }
298 #endif
299
300 return Application::SUCCESS;
301 }
302
303 bool Application::check() const
304 {
305 if (_pid > 1 and kill(_pid, 0) == 0)
306 {
307 return true;
308 }
309
310 return false;
311 }
312
313 void Application::murder()
314 {
315 if (check())
316 {
317 int count= 5;
318 while ((count--) > 0 and check())
319 {
320 if (kill(_pid, SIGTERM) == 0)
321 {
322 join();
323 }
324 else
325 {
326 Error << "kill(pid, SIGTERM) failed after kill with error of " << strerror(errno);
327 continue;
328 }
329
330 break;
331 }
332
333 // If for whatever reason it lives, kill it hard
334 if (check())
335 {
336 Error << "using SIGKILL, things will likely go poorly from this point";
337 (void)kill(_pid, SIGKILL);
338 }
339 }
340 slurp();
341 }
342
343 // false means that no data was returned
344 bool Application::slurp()
345 {
346 struct pollfd fds[2];
347 fds[0].fd= stdout_fd.fd();
348 fds[0].events= POLLRDNORM;
349 fds[0].revents= 0;
350 fds[1].fd= stderr_fd.fd();
351 fds[1].events= POLLRDNORM;
352 fds[1].revents= 0;
353
354 int active_fd;
355 if ((active_fd= poll(fds, 2, 0)) == -1)
356 {
357 int error;
358 switch ((error= errno))
359 {
360 #ifdef TARGET_OS_LINUX
361 case ERESTART:
362 #endif
363 case EINTR:
364 break;
365
366 case EFAULT:
367 case ENOMEM:
368 fatal_message(strerror(error));
369 break;
370
371 case EINVAL:
372 fatal_message("RLIMIT_NOFILE exceeded, or if OSX the timeout value was invalid");
373 break;
374
375 default:
376 fatal_message(strerror(error));
377 break;
378 }
379
380 return false;
381 }
382
383 if (active_fd == 0)
384 {
385 return false;
386 }
387
388 bool data_was_read= false;
389 if (fds[0].revents & POLLRDNORM)
390 {
391 if (stdout_fd.read(_stdout_buffer) == true)
392 {
393 data_was_read= true;
394 }
395 }
396
397 if (fds[1].revents & POLLRDNORM)
398 {
399 if (stderr_fd.read(_stderr_buffer) == true)
400 {
401 data_was_read= true;
402 }
403 }
404
405 return data_was_read;
406 }
407
408 Application::error_t Application::join()
409 {
410 pid_t waited_pid= waitpid(_pid, &_status, 0);
411
412 if (waited_pid == _pid and WIFEXITED(_status) == false)
413 {
414 /*
415 What we are looking for here is how the exit status happened.
416 - 127 means that posix_spawn() itself had an error.
417 - If WEXITSTATUS is positive we need to see if it is a signal that we sent to kill the process. If not something bad happened in the process itself.
418 - Finally something has happened that we don't currently understand.
419 */
420 if (WEXITSTATUS(_status) == 127)
421 {
422 _app_exit_state= Application::INVALID_POSIX_SPAWN;
423 std::string error_string("posix_spawn() failed pid:");
424 error_string+= _pid;
425 error_string+= " name:";
426 error_string+= built_argv[0];
427 throw std::logic_error(error_string);
428 }
429 else if WIFSIGNALED(_status)
430 {
431 if (WTERMSIG(_status) != SIGTERM and WTERMSIG(_status) != SIGHUP)
432 {
433 _app_exit_state= Application::INVALID_POSIX_SPAWN;
434 std::string error_string(built_argv[0]);
435 error_string+= " was killed by signal ";
436 error_string+= strsignal(WTERMSIG(_status));
437 throw std::runtime_error(error_string);
438 }
439
440 #if 0
441 // If we terminted it on purpose then it counts as a success.
442 Out << "waitpid() application terminated at request"
443 << " pid:" << _pid
444 << " name:" << built_argv[0];
445 #endif
446 }
447 else
448 {
449 _app_exit_state= Application::UNKNOWN;
450 Error << "Unknown logic state at exit:" << WEXITSTATUS(_status)
451 << " pid:" << _pid
452 << " name:" << built_argv[0];
453 }
454 }
455 else if (waited_pid == _pid and WIFEXITED(_status))
456 {
457 _app_exit_state= int_to_error_t(WEXITSTATUS(_status));
458 }
459 else if (waited_pid == -1)
460 {
461 _app_exit_state= Application::UNKNOWN;
462 Error << "waitpid() returned errno:" << strerror(errno);
463 }
464 else
465 {
466 _app_exit_state= Application::UNKNOWN;
467 throw std::logic_error("waitpid() returned an unknown value");
468 }
469
470 return _app_exit_state;
471 }
472
473 void Application::add_long_option(const std::string& name, const std::string& option_value)
474 {
475 std::string arg(name);
476 arg+= option_value;
477 _options.push_back(std::make_pair(arg, std::string()));
478 }
479
480 void Application::add_option(const std::string& arg)
481 {
482 _options.push_back(std::make_pair(arg, std::string()));
483 }
484
485 void Application::add_option(const std::string& name, const std::string& value)
486 {
487 _options.push_back(std::make_pair(name, value));
488 }
489
490 Application::Pipe::Pipe(int arg) :
491 _std_fd(arg)
492 {
493 _pipe_fd[READ]= -1;
494 _pipe_fd[WRITE]= -1;
495 _open[READ]= false;
496 _open[WRITE]= false;
497 }
498
499 int Application::Pipe::Pipe::fd()
500 {
501 if (_std_fd == STDOUT_FILENO)
502 {
503 return _pipe_fd[READ];
504 }
505 else if (_std_fd == STDERR_FILENO)
506 {
507 return _pipe_fd[READ];
508 }
509
510 return _pipe_fd[WRITE]; // STDIN_FILENO
511 }
512
513
514 bool Application::Pipe::read(libtest::vchar_t& arg)
515 {
516 fatal_assert(_std_fd == STDOUT_FILENO or _std_fd == STDERR_FILENO);
517
518 bool data_was_read= false;
519
520 ssize_t read_length;
521 char buffer[1024]= { 0 };
522 while ((read_length= ::read(_pipe_fd[READ], buffer, sizeof(buffer))))
523 {
524 if (read_length == -1)
525 {
526 switch(errno)
527 {
528 case EAGAIN:
529 break;
530
531 default:
532 Error << strerror(errno);
533 break;
534 }
535
536 break;
537 }
538
539 data_was_read= true;
540 arg.reserve(read_length +1);
541 for (size_t x= 0; x < size_t(read_length); ++x)
542 {
543 arg.push_back(buffer[x]);
544 }
545 // @todo Suck up all errput code here
546 }
547
548 return data_was_read;
549 }
550
551 void Application::Pipe::nonblock()
552 {
553 int flags;
554 {
555 flags= fcntl(_pipe_fd[READ], F_GETFL, 0);
556 } while (flags == -1 and (errno == EINTR or errno == EAGAIN));
557
558 if (flags == -1)
559 {
560 Error << "fcntl(F_GETFL) " << strerror(errno);
561 throw strerror(errno);
562 }
563
564 int rval;
565 do
566 {
567 rval= fcntl(_pipe_fd[READ], F_SETFL, flags | O_NONBLOCK);
568 } while (rval == -1 and (errno == EINTR or errno == EAGAIN));
569
570 if (rval == -1)
571 {
572 Error << "fcntl(F_SETFL) " << strerror(errno);
573 throw strerror(errno);
574 }
575 }
576
577 void Application::Pipe::reset()
578 {
579 close(READ);
580 close(WRITE);
581
582 #if defined(HAVE_PIPE2) && HAVE_PIPE2
583 if (pipe2(_pipe_fd, O_NONBLOCK|O_CLOEXEC) == -1)
584 #else
585 if (pipe(_pipe_fd) == -1)
586 #endif
587 {
588 fatal_message(strerror(errno));
589 }
590 _open[0]= true;
591 _open[1]= true;
592
593 #if defined(HAVE_PIPE2) && HAVE_PIPE2
594 {
595 nonblock();
596 cloexec();
597 }
598 #endif
599 }
600
601 void Application::Pipe::cloexec()
602 {
603 //if (SOCK_CLOEXEC == 0)
604 {
605 if (FD_CLOEXEC)
606 {
607 int flags;
608 do
609 {
610 flags= fcntl(_pipe_fd[WRITE], F_GETFD, 0);
611 } while (flags == -1 and (errno == EINTR or errno == EAGAIN));
612
613 if (flags == -1)
614 {
615 Error << "fcntl(F_GETFD) " << strerror(errno);
616 throw strerror(errno);
617 }
618
619 int rval;
620 do
621 {
622 rval= fcntl(_pipe_fd[WRITE], F_SETFD, flags | FD_CLOEXEC);
623 } while (rval == -1 && (errno == EINTR or errno == EAGAIN));
624
625 if (rval == -1)
626 {
627 Error << "fcntl(F_SETFD) " << strerror(errno);
628 throw strerror(errno);
629 }
630 }
631 }
632 }
633
634 Application::Pipe::~Pipe()
635 {
636 if (_pipe_fd[0] != -1)
637 {
638 ::close(_pipe_fd[0]);
639 }
640
641 if (_pipe_fd[1] != -1)
642 {
643 ::close(_pipe_fd[1]);
644 }
645 }
646
647 void Application::Pipe::dup_for_spawn(posix_spawn_file_actions_t& file_actions)
648 {
649 int type= STDIN_FILENO == _std_fd ? 0 : 1;
650
651 int ret;
652 if ((ret= posix_spawn_file_actions_adddup2(&file_actions, _pipe_fd[type], _std_fd )) < 0)
653 {
654 Error << "posix_spawn_file_actions_adddup2(" << strerror(ret) << ")";
655 fatal_message(strerror(ret));
656 }
657
658 if ((ret= posix_spawn_file_actions_addclose(&file_actions, _pipe_fd[type])) < 0)
659 {
660 Error << "posix_spawn_file_actions_adddup2(" << strerror(ret) << ")";
661 fatal_message(strerror(ret));
662 }
663 }
664
665 void Application::Pipe::close(const close_t& arg)
666 {
667 int type= int(arg);
668
669 if (_open[type])
670 {
671 if (::close(_pipe_fd[type]) == -1)
672 {
673 Error << "close(" << strerror(errno) << ")";
674 }
675 _open[type]= false;
676 _pipe_fd[type]= -1;
677 }
678 }
679
680 void Application::create_argv(const char *args[])
681 {
682 delete_argv();
683 if (_use_libtool)
684 {
685 assert(libtool());
686 built_argv.push_back(strdup(libtool()));
687 built_argv.push_back(strdup("--mode=execute"));
688 }
689
690 if (_use_valgrind)
691 {
692 /*
693 valgrind --error-exitcode=1 --leak-check=yes --track-fds=yes --malloc-fill=A5 --free-fill=DE
694 */
695 built_argv.push_back(strdup("valgrind"));
696 built_argv.push_back(strdup("--error-exitcode=1"));
697 built_argv.push_back(strdup("--leak-check=yes"));
698 #if 0
699 built_argv.push_back(strdup("--show-reachable=yes"));
700 #endif
701 built_argv.push_back(strdup("--track-fds=yes"));
702 #if 0
703 built_argv[x++]= strdup("--track-origin=yes");
704 #endif
705 built_argv.push_back(strdup("--malloc-fill=A5"));
706 built_argv.push_back(strdup("--free-fill=DE"));
707
708 std::string log_file= create_tmpfile("valgrind");
709 char buffer[1024];
710 int length= snprintf(buffer, sizeof(buffer), "--log-file=%s", log_file.c_str());
711 fatal_assert(length > 0 and size_t(length) < sizeof(buffer));
712 built_argv.push_back(strdup(buffer));
713 }
714 else if (_use_ptrcheck)
715 {
716 /*
717 valgrind --error-exitcode=1 --tool=exp-ptrcheck --log-file=
718 */
719 built_argv.push_back(strdup("valgrind"));
720 built_argv.push_back(strdup("--error-exitcode=1"));
721 built_argv.push_back(strdup("--tool=exp-ptrcheck"));
722 std::string log_file= create_tmpfile("ptrcheck");
723 char buffer[1024];
724 int length= snprintf(buffer, sizeof(buffer), "--log-file=%s", log_file.c_str());
725 fatal_assert(length > 0 and size_t(length) < sizeof(buffer));
726 built_argv.push_back(strdup(buffer));
727 }
728 else if (_use_gdb)
729 {
730 built_argv.push_back(strdup("gdb"));
731 }
732
733 built_argv.push_back(strdup(_exectuble_with_path.c_str()));
734
735 for (Options::const_iterator iter= _options.begin(); iter != _options.end(); ++iter)
736 {
737 built_argv.push_back(strdup((*iter).first.c_str()));
738 if ((*iter).second.empty() == false)
739 {
740 built_argv.push_back(strdup((*iter).second.c_str()));
741 }
742 }
743
744 if (args)
745 {
746 for (const char **ptr= args; *ptr; ++ptr)
747 {
748 built_argv.push_back(strdup(*ptr));
749 }
750 }
751 built_argv.push_back(NULL);
752 }
753
754 std::string Application::print()
755 {
756 return print_argv(built_argv);
757 }
758
759 std::string Application::arguments()
760 {
761 std::stringstream arg_buffer;
762
763 for (size_t x= (1 +_use_libtool) ? 2 : 0;
764 x < _argc and built_argv[x];
765 ++x)
766 {
767 arg_buffer << built_argv[x] << " ";
768 }
769
770 return arg_buffer.str();
771 }
772
773 struct DeleteFromVector
774 {
775 template <class T>
776 void operator() ( T* ptr) const
777 {
778 free(ptr);
779 }
780 };
781
782 void Application::delete_argv()
783 {
784 std::for_each(built_argv.begin(), built_argv.end(), DeleteFromVector());
785
786 built_argv.clear();
787 _argc= 0;
788 }
789
790
791 int exec_cmdline(const std::string& command, const char *args[], bool use_libtool)
792 {
793 Application app(command, use_libtool);
794
795 Application::error_t ret= app.run(args);
796
797 if (ret != Application::SUCCESS)
798 {
799 return int(ret);
800 }
801
802 return int(app.join());
803 }
804
805 const char *gearmand_binary()
806 {
807 return GEARMAND_BINARY;
808 }
809
810 const char *drizzled_binary()
811 {
812 return DRIZZLED_BINARY;
813 }
814
815 } // namespace exec_cmdline