1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3 * Data Differential YATL (i.e. libtest) library
5 * Copyright (C) 2012 Data Differential, http://datadifferential.com/
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
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
19 * * The names of its contributors may not be used to endorse or
20 * promote products derived from this software without specific prior
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.
38 #include <libtest/common.h>
40 using namespace libtest
;
53 #include <sys/types.h>
59 static char **environ
= NULL
;
63 static int exited_successfully(int status
)
70 if (WIFEXITED(status
) == true)
72 return WEXITSTATUS(status
);
74 else if (WIFSIGNALED(status
) == true)
76 return WTERMSIG(status
);
85 std::string
print_argv(libtest::vchar_ptr_t
& built_argv
)
87 std::stringstream arg_buffer
;
89 for (vchar_ptr_t::iterator iter
= built_argv
.begin();
90 iter
== built_argv
.end();
93 arg_buffer
<< *iter
<< " ";
96 return arg_buffer
.str();
100 std::string
print_argv(char** argv
)
102 std::stringstream arg_buffer
;
104 for (char** ptr
= argv
; *ptr
; ++ptr
)
106 arg_buffer
<< *ptr
<< " ";
109 return arg_buffer
.str();
113 static Application::error_t
int_to_error_t(int arg
)
118 return Application::INVALID
;
121 return Application::SUCCESS
;
125 return Application::FAILURE
;
132 Application::Application(const std::string
& arg
, const bool _use_libtool_arg
) :
133 _use_libtool(_use_libtool_arg
),
134 _use_valgrind(false),
136 _use_ptrcheck(false),
140 stdin_fd(STDIN_FILENO
),
141 stdout_fd(STDOUT_FILENO
),
142 stderr_fd(STDERR_FILENO
),
147 if (libtool() == NULL
)
149 fatal_message("libtool requested, but know libtool was found");
153 // Find just the name of the application with no path
155 size_t found
= arg
.find_last_of("/\\");
158 _exectuble_name
= arg
.substr(found
+1);
162 _exectuble_name
= arg
;
166 if (_use_libtool
and getenv("PWD"))
168 _exectuble_with_path
+= getenv("PWD");
169 _exectuble_with_path
+= "/";
171 _exectuble_with_path
+= _exectuble
;
174 Application::~Application()
180 Application::error_t
Application::run(const char *args
[])
185 _stdout_buffer
.clear();
186 _stderr_buffer
.clear();
188 posix_spawn_file_actions_t file_actions
;
189 posix_spawn_file_actions_init(&file_actions
);
191 stdin_fd
.dup_for_spawn(file_actions
);
192 stdout_fd
.dup_for_spawn(file_actions
);
193 stderr_fd
.dup_for_spawn(file_actions
);
195 posix_spawnattr_t spawnattr
;
196 posix_spawnattr_init(&spawnattr
);
200 fatal_assert(posix_spawnattr_setsigmask(&spawnattr
, &set
) == 0);
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
);
211 _gdb_filename
= create_tmpfile(_exectuble_name
);
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
;
222 fatal_assert(file_stream
.good());
227 // libtool --mode=execute gdb -f -x binary
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()),
239 spawn_ret
= posix_spawnp(&_pid
, libtool(), &file_actions
, &spawnattr
, argv
, environ
);
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()),
252 spawn_ret
= posix_spawnp(&_pid
, "gdb", &file_actions
, &spawnattr
, argv
, environ
);
260 spawn_ret
= posix_spawn(&_pid
, built_argv
[0], &file_actions
, &spawnattr
, &built_argv
[0], NULL
);
264 spawn_ret
= posix_spawnp(&_pid
, built_argv
[0], &file_actions
, &spawnattr
, &built_argv
[0], NULL
);
268 posix_spawn_file_actions_destroy(&file_actions
);
269 posix_spawnattr_destroy(&spawnattr
);
271 stdin_fd
.close(Application::Pipe::READ
);
272 stdout_fd
.close(Application::Pipe::WRITE
);
273 stderr_fd
.close(Application::Pipe::WRITE
);
277 if (_will_fail
== false)
279 Error
<< strerror(spawn_ret
) << "(" << spawn_ret
<< ")";
282 return Application::INVALID
;
285 return Application::SUCCESS
;
288 bool Application::check() const
290 if (_pid
> 1 and kill(_pid
, 0) == 0)
298 void Application::murder()
303 while ((count
--) > 0 and check())
305 int kill_ret
= kill(_pid
, SIGTERM
);
310 if ((waitpid_ret
= waitpid(_pid
, &status
, WNOHANG
)) == -1)
319 Error
<< "waitpid() failed after kill with error of " << strerror(errno
);
324 if (waitpid_ret
== 0)
326 libtest::dream(1, 0);
331 Error
<< "kill(pid, SIGTERM) failed after kill with error of " << strerror(errno
);
338 // If for whatever reason it lives, kill it hard
341 (void)kill(_pid
, SIGKILL
);
347 // false means that no data was returned
348 bool Application::slurp()
350 struct pollfd fds
[2];
351 fds
[0].fd
= stdout_fd
.fd();
352 fds
[0].events
= POLLRDNORM
;
354 fds
[1].fd
= stderr_fd
.fd();
355 fds
[1].events
= POLLRDNORM
;
359 if ((active_fd
= poll(fds
, 2, 0)) == -1)
362 switch ((error
= errno
))
364 #ifdef TARGET_OS_LINUX
372 fatal_message(strerror(error
));
376 fatal_message("RLIMIT_NOFILE exceeded, or if OSX the timeout value was invalid");
380 fatal_message(strerror(error
));
392 bool data_was_read
= false;
393 if (fds
[0].revents
& POLLRDNORM
)
395 if (stdout_fd
.read(_stdout_buffer
) == true)
401 if (fds
[1].revents
& POLLRDNORM
)
403 if (stderr_fd
.read(_stderr_buffer
) == true)
409 return data_was_read
;
412 Application::error_t
Application::wait(bool nohang
)
416 return Application::INVALID
;
421 error_t exit_code
= FAILURE
;
425 if ((waited_pid
= waitpid(_pid
, &status
, nohang
? WNOHANG
: 0)) == -1)
430 exit_code
= Application::SUCCESS
;
437 Error
<< "Error occured while waitpid(" << strerror(errno
) << ") on pid " << int(_pid
);
441 else if (waited_pid
== 0)
443 exit_code
= Application::SUCCESS
;
447 if (waited_pid
!= _pid
)
449 throw libtest::fatal(LIBYATL_DEFAULT_PARAM
, "Pid mismatch, %d != %d", int(waited_pid
), int(_pid
));
451 exit_code
= int_to_error_t(exited_successfully(status
));
458 if (exit_code
== Application::INVALID
)
460 Error
<< print_argv(built_argv
, _argc
);
467 Application::error_t
Application::join()
471 return Application::INVALID
;
476 error_t exit_code
= FAILURE
;
481 waited_pid
= waitpid(_pid
, &status
, 0);
482 } while (waited_pid
== -1 and (errno
== EINTR
or errno
== EAGAIN
));
484 if (waited_pid
== -1)
489 exit_code
= Application::SUCCESS
;
496 Error
<< "Error occured while waitpid(" << strerror(errno
) << ") on pid " << int(_pid
);
500 else if (waited_pid
== 0)
502 exit_code
= Application::SUCCESS
;
506 if (waited_pid
!= _pid
)
508 throw libtest::fatal(LIBYATL_DEFAULT_PARAM
, "Pid mismatch, %d != %d", int(waited_pid
), int(_pid
));
511 exit_code
= int_to_error_t(exited_successfully(status
));
518 if (exit_code
== Application::INVALID
)
520 Error
<< print_argv(built_argv
, _argc
);
527 void Application::add_long_option(const std::string
& name
, const std::string
& option_value
)
529 std::string
arg(name
);
531 _options
.push_back(std::make_pair(arg
, std::string()));
534 void Application::add_option(const std::string
& arg
)
536 _options
.push_back(std::make_pair(arg
, std::string()));
539 void Application::add_option(const std::string
& name
, const std::string
& value
)
541 _options
.push_back(std::make_pair(name
, value
));
544 Application::Pipe::Pipe(int arg
) :
553 int Application::Pipe::Pipe::fd()
555 if (_std_fd
== STDOUT_FILENO
)
557 return _pipe_fd
[READ
];
559 else if (_std_fd
== STDERR_FILENO
)
561 return _pipe_fd
[READ
];
564 return _pipe_fd
[WRITE
]; // STDIN_FILENO
568 bool Application::Pipe::read(libtest::vchar_t
& arg
)
570 fatal_assert(_std_fd
== STDOUT_FILENO
or _std_fd
== STDERR_FILENO
);
572 bool data_was_read
= false;
575 char buffer
[1024]= { 0 };
576 while ((read_length
= ::read(_pipe_fd
[READ
], buffer
, sizeof(buffer
))))
578 if (read_length
== -1)
586 Error
<< strerror(errno
);
594 arg
.reserve(read_length
+1);
595 for (size_t x
= 0; x
< size_t(read_length
); ++x
)
597 arg
.push_back(buffer
[x
]);
599 // @todo Suck up all errput code here
602 return data_was_read
;
605 void Application::Pipe::nonblock()
608 if ((ret
= fcntl(_pipe_fd
[READ
], F_GETFL
, 0)) == -1)
610 Error
<< "fcntl(F_GETFL) " << strerror(errno
);
611 throw strerror(errno
);
614 if ((ret
= fcntl(_pipe_fd
[READ
], F_SETFL
, ret
| O_NONBLOCK
)) == -1)
616 Error
<< "fcntl(F_SETFL) " << strerror(errno
);
617 throw strerror(errno
);
621 void Application::Pipe::reset()
627 if (pipe2(_pipe_fd
, O_NONBLOCK
) == -1)
629 if (pipe(_pipe_fd
) == -1)
632 fatal_message(strerror(errno
));
644 void Application::Pipe::cloexec()
647 if ((ret
= fcntl(_pipe_fd
[WRITE
], F_GETFD
, 0)) == -1)
649 Error
<< "fcntl(F_GETFD) " << strerror(errno
);
650 throw strerror(errno
);
653 if ((ret
= fcntl(_pipe_fd
[WRITE
], F_SETFD
, ret
| FD_CLOEXEC
)) == -1)
655 Error
<< "fcntl(F_SETFD) " << strerror(errno
);
656 throw strerror(errno
);
660 Application::Pipe::~Pipe()
662 if (_pipe_fd
[0] != -1)
664 ::close(_pipe_fd
[0]);
667 if (_pipe_fd
[1] != -1)
669 ::close(_pipe_fd
[1]);
673 void Application::Pipe::dup_for_spawn(posix_spawn_file_actions_t
& file_actions
)
675 int type
= STDIN_FILENO
== _std_fd
? 0 : 1;
678 if ((ret
= posix_spawn_file_actions_adddup2(&file_actions
, _pipe_fd
[type
], _std_fd
)) < 0)
680 Error
<< "posix_spawn_file_actions_adddup2(" << strerror(ret
) << ")";
681 fatal_message(strerror(ret
));
684 if ((ret
= posix_spawn_file_actions_addclose(&file_actions
, _pipe_fd
[type
])) < 0)
686 Error
<< "posix_spawn_file_actions_adddup2(" << strerror(ret
) << ")";
687 fatal_message(strerror(ret
));
691 void Application::Pipe::close(const close_t
& arg
)
697 if (::close(_pipe_fd
[type
]) == -1)
699 Error
<< "close(" << strerror(errno
) << ")";
706 void Application::create_argv(const char *args
[])
712 built_argv
.push_back(strdup(libtool()));
713 built_argv
.push_back(strdup("--mode=execute"));
719 valgrind --error-exitcode=1 --leak-check=yes --show-reachable=yes --track-fds=yes --malloc-fill=A5 --free-fill=DE
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"));
727 built_argv
[x
++]= strdup("--track-origin=yes");
729 built_argv
.push_back(strdup("--malloc-fill=A5"));
730 built_argv
.push_back(strdup("--free-fill=DE"));
732 std::string log_file
= create_tmpfile("valgrind");
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
));
738 else if (_use_ptrcheck
)
741 valgrind --error-exitcode=1 --tool=exp-ptrcheck --log-file=
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");
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
));
754 built_argv
.push_back(strdup("gdb"));
757 built_argv
.push_back(strdup(_exectuble_with_path
.c_str()));
759 for (Options::const_iterator iter
= _options
.begin(); iter
!= _options
.end(); ++iter
)
761 built_argv
.push_back(strdup((*iter
).first
.c_str()));
762 if ((*iter
).second
.empty() == false)
764 built_argv
.push_back(strdup((*iter
).second
.c_str()));
770 for (const char **ptr
= args
; *ptr
; ++ptr
)
772 built_argv
.push_back(strdup(*ptr
));
775 built_argv
.push_back(NULL
);
778 std::string
Application::print()
780 return print_argv(built_argv
);
783 std::string
Application::arguments()
785 std::stringstream arg_buffer
;
787 for (size_t x
= (1 +_use_libtool
) ? 2 : 0;
788 x
< _argc
and built_argv
[x
];
791 arg_buffer
<< built_argv
[x
] << " ";
794 return arg_buffer
.str();
797 struct DeleteFromVector
800 void operator() ( T
* ptr
) const
806 void Application::delete_argv()
808 std::for_each(built_argv
.begin(), built_argv
.end(), DeleteFromVector());
815 int exec_cmdline(const std::string
& command
, const char *args
[], bool use_libtool
)
817 Application
app(command
, use_libtool
);
819 Application::error_t ret
= app
.run(args
);
821 if (ret
!= Application::SUCCESS
)
826 return int(app
.join());
829 const char *gearmand_binary()
831 return GEARMAND_BINARY
;
834 const char *drizzled_binary()
836 return DRIZZLED_BINARY
;
839 } // namespace exec_cmdline