Update all licenses to BSD.
[m6w6/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(Application::Pipe::READ, file_actions);
187 stdout_fd.dup_for_spawn(Application::Pipe::WRITE, file_actions);
188 stderr_fd.dup_for_spawn(Application::Pipe::WRITE, 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 close(READ);
597 close(WRITE);
598 }
599
600 void Application::Pipe::dup_for_spawn(const close_t& arg, posix_spawn_file_actions_t& file_actions)
601 {
602 int type= int(arg);
603
604 int ret;
605 if ((ret= posix_spawn_file_actions_adddup2(&file_actions, _pipe_fd[type], _std_fd )) < 0)
606 {
607 Error << "posix_spawn_file_actions_adddup2(" << strerror(ret) << ")";
608 fatal_message(strerror(ret));
609 }
610
611 if ((ret= posix_spawn_file_actions_addclose(&file_actions, _pipe_fd[type])) < 0)
612 {
613 Error << "posix_spawn_file_actions_adddup2(" << strerror(ret) << ")";
614 fatal_message(strerror(ret));
615 }
616 }
617
618 void Application::Pipe::close(const close_t& arg)
619 {
620 int type= int(arg);
621
622 if (_open[type])
623 {
624 int ret;
625 if (::close(_pipe_fd[type]) == -1)
626 {
627 Error << "close(" << strerror(errno) << ")";
628 }
629 _open[type]= false;
630 _pipe_fd[type]= -1;
631 }
632 }
633
634 void Application::create_argv(const char *args[])
635 {
636 delete_argv();
637 fatal_assert(_argc == 0);
638
639 if (_use_libtool)
640 {
641 _argc+= 2; // +2 for libtool --mode=execute
642 }
643
644 _argc+= 1; // For the command
645
646 /*
647 valgrind --error-exitcode=1 --leak-check=yes --show-reachable=yes --track-fds=yes --track-origin=yes --malloc-fill=A5 --free-fill=DE --log-file=
648 */
649 if (_use_valgrind)
650 {
651 _argc+= 8;
652 }
653 else if (_use_ptrcheck)
654 {
655 /*
656 valgrind --error-exitcode=1 --tool=exp-ptrcheck --log-file=
657 */
658 _argc+= 4;
659 }
660 else if (_use_gdb) // gdb
661 {
662 _argc+= 1;
663 }
664
665 for (Options::const_iterator iter= _options.begin(); iter != _options.end(); iter++)
666 {
667 _argc++;
668 if ((*iter).second.empty() == false)
669 {
670 _argc++;
671 }
672 }
673
674 if (args)
675 {
676 for (const char **ptr= args; *ptr; ++ptr)
677 {
678 _argc++;
679 }
680 }
681
682 _argc+= 1; // for the NULL
683
684 built_argv= new char * [_argc];
685
686 size_t x= 0;
687 if (_use_libtool)
688 {
689 assert(libtool());
690 built_argv[x++]= strdup(libtool());
691 built_argv[x++]= strdup("--mode=execute");
692 }
693
694 if (_use_valgrind)
695 {
696 /*
697 valgrind --error-exitcode=1 --leak-check=yes --show-reachable=yes --track-fds=yes --malloc-fill=A5 --free-fill=DE
698 */
699 built_argv[x++]= strdup("valgrind");
700 built_argv[x++]= strdup("--error-exitcode=1");
701 built_argv[x++]= strdup("--leak-check=yes");
702 built_argv[x++]= strdup("--show-reachable=yes");
703 built_argv[x++]= strdup("--track-fds=yes");
704 #if 0
705 built_argv[x++]= strdup("--track-origin=yes");
706 #endif
707 built_argv[x++]= strdup("--malloc-fill=A5");
708 built_argv[x++]= strdup("--free-fill=DE");
709
710 std::string log_file= create_tmpfile("valgrind");
711 char buffer[1024];
712 int length= snprintf(buffer, sizeof(buffer), "--log-file=%s", log_file.c_str());
713 fatal_assert(length > 0 and length < sizeof(buffer));
714 built_argv[x++]= strdup(buffer);
715 }
716 else if (_use_ptrcheck)
717 {
718 /*
719 valgrind --error-exitcode=1 --tool=exp-ptrcheck --log-file=
720 */
721 built_argv[x++]= strdup("valgrind");
722 built_argv[x++]= strdup("--error-exitcode=1");
723 built_argv[x++]= strdup("--tool=exp-ptrcheck");
724 _argc+= 4;
725 std::string log_file= create_tmpfile("ptrcheck");
726 char buffer[1024];
727 int length= snprintf(buffer, sizeof(buffer), "--log-file=%s", log_file.c_str());
728 fatal_assert(length > 0 and length < sizeof(buffer));
729 built_argv[x++]= strdup(buffer);
730 }
731 else if (_use_gdb)
732 {
733 built_argv[x++]= strdup("gdb");
734 }
735
736 built_argv[x++]= strdup(_exectuble_with_path.c_str());
737
738 for (Options::const_iterator iter= _options.begin(); iter != _options.end(); iter++)
739 {
740 built_argv[x++]= strdup((*iter).first.c_str());
741 if ((*iter).second.empty() == false)
742 {
743 built_argv[x++]= strdup((*iter).second.c_str());
744 }
745 }
746
747 if (args)
748 {
749 for (const char **ptr= args; *ptr; ++ptr)
750 {
751 built_argv[x++]= strdup(*ptr);
752 }
753 }
754 built_argv[x++]= NULL;
755 fatal_assert(x == _argc);
756 }
757
758 std::string Application::print()
759 {
760 return print_argv(built_argv, _argc);
761 }
762
763 std::string Application::arguments()
764 {
765 std::stringstream arg_buffer;
766
767 for (size_t x= 1 + _use_libtool ? 2 : 0;
768 x < _argc and built_argv[x];
769 x++)
770 {
771 arg_buffer << built_argv[x] << " ";
772 }
773
774 return arg_buffer.str();
775 }
776
777 void Application::delete_argv()
778 {
779 if (built_argv)
780 {
781 for (size_t x= 0; x < _argc; x++)
782 {
783 if (built_argv[x])
784 {
785 ::free(built_argv[x]);
786 }
787 }
788 delete[] built_argv;
789 built_argv= NULL;
790 _argc= 0;
791 }
792 }
793
794
795 int exec_cmdline(const std::string& command, const char *args[], bool use_libtool)
796 {
797 Application app(command, use_libtool);
798
799 Application::error_t ret= app.run(args);
800
801 if (ret != Application::SUCCESS)
802 {
803 return int(ret);
804 }
805
806 return int(app.wait(false));
807 }
808
809 const char *gearmand_binary()
810 {
811 return GEARMAND_BINARY;
812 }
813
814 const char *drizzled_binary()
815 {
816 return DRIZZLED_BINARY;
817 }
818
819 } // namespace exec_cmdline