Encapsulate more of the cleanup logic.
[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 /*
417 What we are looking for here is how the exit status happened.
418 - 127 means that posix_spawn() itself had an error.
419 - 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.
420 - Finally something has happened that we don't currently understand.
421 */
422 if (WEXITSTATUS(_status) == 127)
423 {
424 _app_exit_state= Application::INVALID_POSIX_SPAWN;
425 std::string error_string("posix_spawn() failed pid:");
426 error_string+= _pid;
427 error_string+= " name:";
428 error_string+= print_argv(built_argv);
429 if (stderr_result_length())
430 {
431 error_string+= " stderr: ";
432 error_string+= stderr_c_str();
433 }
434 throw std::logic_error(error_string);
435 }
436 else if (WIFSIGNALED(_status))
437 {
438 if (WTERMSIG(_status) != SIGTERM and WTERMSIG(_status) != SIGHUP)
439 {
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 if (stdout_result_length())
445 {
446 error_string+= " stdout: ";
447 error_string+= stdout_c_str();
448 }
449 if (stderr_result_length())
450 {
451 error_string+= " stderr: ";
452 error_string+= stderr_c_str();
453 }
454
455 throw std::runtime_error(error_string);
456 }
457
458 // If we terminted it on purpose then it counts as a success.
459 #if defined(DEBUG)
460 if (DEBUG)
461 {
462 Out << "waitpid() application terminated at request"
463 << " pid:" << _pid
464 << " name:" << built_argv[0];
465 }
466 #endif
467 }
468 else
469 {
470 _app_exit_state= Application::UNKNOWN;
471 Error << "Unknown logic state at exit:" << WEXITSTATUS(_status)
472 << " pid:" << _pid
473 << " name:" << built_argv[0];
474 }
475 }
476 else if (waited_pid == _pid and WIFEXITED(_status))
477 {
478 _app_exit_state= int_to_error_t(WEXITSTATUS(_status));
479 }
480 else if (waited_pid == -1)
481 {
482 _app_exit_state= Application::UNKNOWN;
483 Error << "waitpid() returned errno:" << strerror(errno);
484 }
485 else
486 {
487 _app_exit_state= Application::UNKNOWN;
488 throw std::logic_error("waitpid() returned an unknown value");
489 }
490
491 return _app_exit_state;
492 }
493
494 void Application::add_long_option(const std::string& name, const std::string& option_value)
495 {
496 std::string arg(name);
497 arg+= option_value;
498 _options.push_back(std::make_pair(arg, std::string()));
499 }
500
501 void Application::add_option(const std::string& arg)
502 {
503 _options.push_back(std::make_pair(arg, std::string()));
504 }
505
506 void Application::add_option(const std::string& name, const std::string& value)
507 {
508 _options.push_back(std::make_pair(name, value));
509 }
510
511 Application::Pipe::Pipe(int arg) :
512 _std_fd(arg)
513 {
514 _pipe_fd[READ]= -1;
515 _pipe_fd[WRITE]= -1;
516 _open[READ]= false;
517 _open[WRITE]= false;
518 }
519
520 int Application::Pipe::Pipe::fd()
521 {
522 if (_std_fd == STDOUT_FILENO)
523 {
524 return _pipe_fd[READ];
525 }
526 else if (_std_fd == STDERR_FILENO)
527 {
528 return _pipe_fd[READ];
529 }
530
531 return _pipe_fd[WRITE]; // STDIN_FILENO
532 }
533
534
535 bool Application::Pipe::read(libtest::vchar_t& arg)
536 {
537 fatal_assert(_std_fd == STDOUT_FILENO or _std_fd == STDERR_FILENO);
538
539 bool data_was_read= false;
540
541 libtest::vchar_t buffer;
542 buffer.resize(1024);
543 ssize_t read_length;
544 while ((read_length= ::read(_pipe_fd[READ], &buffer[0], buffer.size())))
545 {
546 if (read_length == -1)
547 {
548 switch(errno)
549 {
550 case EAGAIN:
551 break;
552
553 default:
554 Error << strerror(errno);
555 break;
556 }
557
558 break;
559 }
560
561 data_was_read= true;
562 arg.reserve(read_length +1);
563 for (size_t x= 0; x < size_t(read_length); ++x)
564 {
565 arg.push_back(buffer[x]);
566 }
567 // @todo Suck up all errput code here
568 }
569
570 return data_was_read;
571 }
572
573 void Application::Pipe::nonblock()
574 {
575 int flags;
576 {
577 flags= fcntl(_pipe_fd[READ], F_GETFL, 0);
578 } while (flags == -1 and (errno == EINTR or errno == EAGAIN));
579
580 if (flags == -1)
581 {
582 Error << "fcntl(F_GETFL) " << strerror(errno);
583 throw strerror(errno);
584 }
585
586 int rval;
587 do
588 {
589 rval= fcntl(_pipe_fd[READ], F_SETFL, flags | O_NONBLOCK);
590 } while (rval == -1 and (errno == EINTR or errno == EAGAIN));
591
592 if (rval == -1)
593 {
594 Error << "fcntl(F_SETFL) " << strerror(errno);
595 throw strerror(errno);
596 }
597 }
598
599 void Application::Pipe::reset()
600 {
601 close(READ);
602 close(WRITE);
603
604 #if defined(HAVE_PIPE2) && HAVE_PIPE2
605 if (pipe2(_pipe_fd, O_NONBLOCK|O_CLOEXEC) == -1)
606 #else
607 if (pipe(_pipe_fd) == -1)
608 #endif
609 {
610 FATAL(strerror(errno));
611 }
612 _open[0]= true;
613 _open[1]= true;
614
615 #if defined(HAVE_PIPE2) && HAVE_PIPE2
616 {
617 nonblock();
618 cloexec();
619 }
620 #endif
621 }
622
623 void Application::Pipe::cloexec()
624 {
625 //if (SOCK_CLOEXEC == 0)
626 {
627 if (FD_CLOEXEC)
628 {
629 int flags;
630 do
631 {
632 flags= fcntl(_pipe_fd[WRITE], F_GETFD, 0);
633 } while (flags == -1 and (errno == EINTR or errno == EAGAIN));
634
635 if (flags == -1)
636 {
637 Error << "fcntl(F_GETFD) " << strerror(errno);
638 throw strerror(errno);
639 }
640
641 int rval;
642 do
643 {
644 rval= fcntl(_pipe_fd[WRITE], F_SETFD, flags | FD_CLOEXEC);
645 } while (rval == -1 && (errno == EINTR or errno == EAGAIN));
646
647 if (rval == -1)
648 {
649 Error << "fcntl(F_SETFD) " << strerror(errno);
650 throw strerror(errno);
651 }
652 }
653 }
654 }
655
656 Application::Pipe::~Pipe()
657 {
658 if (_pipe_fd[0] != -1)
659 {
660 ::close(_pipe_fd[0]);
661 }
662
663 if (_pipe_fd[1] != -1)
664 {
665 ::close(_pipe_fd[1]);
666 }
667 }
668
669 void Application::Pipe::dup_for_spawn(posix_spawn_file_actions_t& file_actions)
670 {
671 int type= STDIN_FILENO == _std_fd ? 0 : 1;
672
673 int ret;
674 if ((ret= posix_spawn_file_actions_adddup2(&file_actions, _pipe_fd[type], _std_fd )) < 0)
675 {
676 FATAL("posix_spawn_file_actions_adddup2(%s)", strerror(ret));
677 }
678
679 if ((ret= posix_spawn_file_actions_addclose(&file_actions, _pipe_fd[type])) < 0)
680 {
681 FATAL("posix_spawn_file_actions_addclose(%s)", strerror(ret));
682 }
683 }
684
685 void Application::Pipe::close(const close_t& arg)
686 {
687 int type= int(arg);
688
689 if (_open[type])
690 {
691 if (::close(_pipe_fd[type]) == -1)
692 {
693 Error << "close(" << strerror(errno) << ")";
694 }
695 _open[type]= false;
696 _pipe_fd[type]= -1;
697 }
698 }
699
700 void Application::create_argv(const char *args[])
701 {
702 delete_argv();
703 if (_use_libtool)
704 {
705 assert(libtool());
706 vchar::append(built_argv, libtool());
707 vchar::append(built_argv, "--mode=execute");
708 }
709
710 if (_use_valgrind)
711 {
712 /*
713 valgrind --error-exitcode=1 --leak-check=yes --track-fds=yes --malloc-fill=A5 --free-fill=DE
714 */
715 vchar::append(built_argv, "valgrind");
716 vchar::append(built_argv, "--error-exitcode=1");
717 vchar::append(built_argv, "--leak-check=yes");
718 #if 0
719 vchar::append(built_argv, "--show-reachable=yes"));
720 #endif
721 vchar::append(built_argv, "--track-fds=yes");
722 #if 0
723 built_argv[x++]= strdup("--track-origin=yes");
724 #endif
725 vchar::append(built_argv, "--malloc-fill=A5");
726 vchar::append(built_argv, "--free-fill=DE");
727
728 std::string log_file= create_tmpfile("valgrind");
729 libtest::vchar_t buffer;
730 buffer.resize(1024);
731 int length= snprintf(&buffer[0], buffer.size(), "--log-file=%s", log_file.c_str());
732 fatal_assert(length > 0 and size_t(length) < buffer.size());
733 vchar::append(built_argv, &buffer[0]);
734 }
735 else if (_use_ptrcheck)
736 {
737 /*
738 valgrind --error-exitcode=1 --tool=exp-ptrcheck --log-file=
739 */
740 vchar::append(built_argv, "valgrind");
741 vchar::append(built_argv, "--error-exitcode=1");
742 vchar::append(built_argv, "--tool=exp-ptrcheck");
743 std::string log_file= create_tmpfile("ptrcheck");
744 libtest::vchar_t buffer;
745 buffer.resize(1024);
746 int length= snprintf(&buffer[0], buffer.size(), "--log-file=%s", log_file.c_str());
747 fatal_assert(length > 0 and size_t(length) < buffer.size());
748 vchar::append(built_argv, &buffer[0]);
749 }
750 else if (_use_gdb)
751 {
752 vchar::append(built_argv, "gdb");
753 }
754
755 vchar::append(built_argv, _exectuble_with_path.c_str());
756
757 for (Options::const_iterator iter= _options.begin(); iter != _options.end(); ++iter)
758 {
759 vchar::append(built_argv, (*iter).first.c_str());
760 if ((*iter).second.empty() == false)
761 {
762 vchar::append(built_argv, (*iter).second.c_str());
763 }
764 }
765
766 if (args)
767 {
768 for (const char **ptr= args; *ptr; ++ptr)
769 {
770 vchar::append(built_argv, *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 // Skip printing out the libtool reference
786 for (size_t x= _use_libtool ? 2 : 0; x < _argc; ++x)
787 {
788 if (built_argv[x])
789 {
790 arg_buffer << built_argv[x] << " ";
791 }
792 }
793
794 return arg_buffer.str();
795 }
796
797 void Application::delete_argv()
798 {
799 std::for_each(built_argv.begin(), built_argv.end(), FreeFromVector());
800
801 built_argv.clear();
802 _argc= 0;
803 }
804
805
806 int exec_cmdline(const std::string& command, const char *args[], bool use_libtool)
807 {
808 Application app(command, use_libtool);
809
810 Application::error_t ret= app.run(args);
811
812 if (ret != Application::SUCCESS)
813 {
814 return int(ret);
815 }
816
817 return int(app.join());
818 }
819
820 const char *gearmand_binary()
821 {
822 return GEARMAND_BINARY;
823 }
824
825 const char *drizzled_binary()
826 {
827 return DRIZZLED_BINARY;
828 }
829
830 } // namespace exec_cmdline