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