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