+ std::string print_argv(char** argv)
+ {
+ std::stringstream arg_buffer;
+
+ for (char** ptr= argv; *ptr; ++ptr)
+ {
+ arg_buffer << *ptr << " ";
+ }
+
+ return arg_buffer.str();
+ }
+#endif
+
+ static Application::error_t int_to_error_t(int arg)
+ {
+ switch (arg)
+ {
+ case 127:
+ return Application::INVALID;
+
+ case 0:
+ return Application::SUCCESS;
+
+ default:
+ case 1:
+ return Application::FAILURE;
+ }
+ }
+}
+
+namespace libtest {
+
+Application::Application(const std::string& arg, const bool _use_libtool_arg) :
+ _use_libtool(_use_libtool_arg),
+ _use_valgrind(false),
+ _use_gdb(false),
+ _use_ptrcheck(false),
+ _will_fail(false),
+ _argc(0),
+ _exectuble(arg),
+ stdin_fd(STDIN_FILENO),
+ stdout_fd(STDOUT_FILENO),
+ stderr_fd(STDERR_FILENO),
+ _pid(-1)
+ {
+ if (_use_libtool)
+ {
+ if (libtool() == NULL)
+ {
+ fatal_message("libtool requested, but know libtool was found");
+ }
+ }
+
+ // Find just the name of the application with no path
+ {
+ size_t found= arg.find_last_of("/\\");
+ if (found)
+ {
+ _exectuble_name= arg.substr(found +1);
+ }
+ else
+ {
+ _exectuble_name= arg;
+ }
+ }
+
+ if (_use_libtool and getenv("PWD"))
+ {
+ _exectuble_with_path+= getenv("PWD");
+ _exectuble_with_path+= "/";
+ }
+ _exectuble_with_path+= _exectuble;
+ }
+
+Application::~Application()
+{
+ murder();
+ delete_argv();
+}
+
+Application::error_t Application::run(const char *args[])
+{
+ stdin_fd.reset();
+ stdout_fd.reset();
+ stderr_fd.reset();
+ _stdout_buffer.clear();
+ _stderr_buffer.clear();
+
+ posix_spawn_file_actions_t file_actions;
+ posix_spawn_file_actions_init(&file_actions);
+
+ stdin_fd.dup_for_spawn(file_actions);
+ stdout_fd.dup_for_spawn(file_actions);
+ stderr_fd.dup_for_spawn(file_actions);
+
+ posix_spawnattr_t spawnattr;
+ posix_spawnattr_init(&spawnattr);
+
+ sigset_t set;
+ sigemptyset(&set);
+ fatal_assert(posix_spawnattr_setsigmask(&spawnattr, &set) == 0);
+
+ create_argv(args);
+
+ int spawn_ret;
+ if (_use_gdb)
+ {
+ std::string gdb_run_file= create_tmpfile(_exectuble_name);
+ std::fstream file_stream;
+ file_stream.open(gdb_run_file.c_str(), std::fstream::out | std::fstream::trunc);
+
+ _gdb_filename= create_tmpfile(_exectuble_name);
+ file_stream
+ << "set logging redirect on" << std::endl
+ << "set logging file " << _gdb_filename << std::endl
+ << "set logging overwrite on" << std::endl
+ << "set logging on" << std::endl
+ << "set environment LIBTEST_IN_GDB=1" << std::endl
+ << "run " << arguments() << std::endl
+ << "thread apply all bt" << std::endl
+ << "quit" << std::endl;
+
+ fatal_assert(file_stream.good());
+ file_stream.close();
+
+ if (_use_libtool)
+ {
+ // libtool --mode=execute gdb -f -x binary
+ char *argv[]= {
+ const_cast<char *>(libtool()),
+ const_cast<char *>("--mode=execute"),
+ const_cast<char *>("gdb"),
+ const_cast<char *>("-batch"),
+ const_cast<char *>("-f"),
+ const_cast<char *>("-x"),
+ const_cast<char *>(gdb_run_file.c_str()),
+ const_cast<char *>(_exectuble_with_path.c_str()),
+ 0};
+
+ spawn_ret= posix_spawnp(&_pid, libtool(), &file_actions, &spawnattr, argv, environ);
+ }
+ else
+ {
+ // gdb binary
+ char *argv[]= {
+ const_cast<char *>("gdb"),
+ const_cast<char *>("-batch"),
+ const_cast<char *>("-f"),
+ const_cast<char *>("-x"),
+ const_cast<char *>(gdb_run_file.c_str()),
+ const_cast<char *>(_exectuble_with_path.c_str()),
+ 0};
+ spawn_ret= posix_spawnp(&_pid, "gdb", &file_actions, &spawnattr, argv, environ);
+ }
+ }
+ else
+ {
+
+ if (_use_libtool)
+ {
+ spawn_ret= posix_spawn(&_pid, built_argv[0], &file_actions, &spawnattr, &built_argv[0], NULL);
+ }
+ else
+ {
+ spawn_ret= posix_spawnp(&_pid, built_argv[0], &file_actions, &spawnattr, &built_argv[0], NULL);
+ }
+ }
+
+ posix_spawn_file_actions_destroy(&file_actions);
+ posix_spawnattr_destroy(&spawnattr);
+
+ stdin_fd.close(Application::Pipe::READ);
+ stdout_fd.close(Application::Pipe::WRITE);
+ stderr_fd.close(Application::Pipe::WRITE);
+
+ if (spawn_ret != 0)
+ {
+ if (_will_fail == false)
+ {
+ Error << strerror(spawn_ret) << "(" << spawn_ret << ")";
+ }
+ _pid= -1;
+ return Application::INVALID;
+ }
+
+ return Application::SUCCESS;
+}
+
+bool Application::check() const
+{
+ if (_pid > 1 and kill(_pid, 0) == 0)
+ {
+ return true;
+ }
+
+ return false;
+}
+
+void Application::murder()
+{
+ if (check())
+ {
+ int count= 5;
+ while ((count--) > 0 and check())
+ {
+ int kill_ret= kill(_pid, SIGTERM);
+ if (kill_ret == 0)
+ {
+ int status= 0;
+ pid_t waitpid_ret;
+ if ((waitpid_ret= waitpid(_pid, &status, WNOHANG)) == -1)
+ {
+ switch (errno)
+ {
+ case ECHILD:
+ case EINTR:
+ break;
+
+ default:
+ Error << "waitpid() failed after kill with error of " << strerror(errno);
+ break;
+ }
+ }
+
+ if (waitpid_ret == 0)
+ {
+ libtest::dream(1, 0);
+ }
+ }
+ else
+ {
+ Error << "kill(pid, SIGTERM) failed after kill with error of " << strerror(errno);
+ continue;
+ }
+
+ break;
+ }
+
+ // If for whatever reason it lives, kill it hard
+ if (check())
+ {
+ (void)kill(_pid, SIGKILL);
+ }
+ }
+ slurp();
+}
+
+// false means that no data was returned
+bool Application::slurp()
+{
+ struct pollfd fds[2];
+ fds[0].fd= stdout_fd.fd();
+ fds[0].events= POLLRDNORM;
+ fds[0].revents= 0;
+ fds[1].fd= stderr_fd.fd();
+ fds[1].events= POLLRDNORM;
+ fds[1].revents= 0;
+
+ int active_fd;
+ if ((active_fd= poll(fds, 2, 0)) == -1)
+ {
+ int error;
+ switch ((error= errno))
+ {
+#ifdef TARGET_OS_LINUX
+ case ERESTART: