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