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