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