1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 3 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include <libtest/common.h>
25 using namespace libtest
;
38 #include <sys/types.h>
42 static char **environ
= NULL
;
46 static int exited_successfully(int status
)
53 if (WIFEXITED(status
) == true)
55 return WEXITSTATUS(status
);
57 else if (WIFSIGNALED(status
) == true)
59 return WTERMSIG(status
);
68 std::string
print_argv(char * * & built_argv
, const size_t& argc
)
70 std::stringstream arg_buffer
;
72 for (size_t x
= 0; x
< argc
; x
++)
74 arg_buffer
<< built_argv
[x
] << " ";
77 return arg_buffer
.str();
80 std::string
print_argv(char** argv
)
82 std::stringstream arg_buffer
;
84 for (char** ptr
= argv
; *ptr
; ptr
++)
86 arg_buffer
<< *ptr
<< " ";
89 return arg_buffer
.str();
92 static Application::error_t
int_to_error_t(int arg
)
97 return Application::INVALID
;
100 return Application::SUCCESS
;
104 return Application::FAILURE
;
111 Application::Application(const std::string
& arg
, const bool _use_libtool_arg
) :
112 _use_libtool(_use_libtool_arg
),
113 _use_valgrind(false),
115 _use_ptrcheck(false),
119 stdin_fd(STDIN_FILENO
),
120 stdout_fd(STDOUT_FILENO
),
121 stderr_fd(STDERR_FILENO
),
127 if (libtool() == NULL
)
129 fatal_message("libtool requested, but know libtool was found");
133 // Find just the name of the application with no path
135 size_t found
= arg
.find_last_of("/\\");
138 _exectuble_name
= arg
.substr(found
+1);
142 _exectuble_name
= arg
;
146 if (_use_libtool
and getenv("PWD"))
148 _exectuble_with_path
+= getenv("PWD");
149 _exectuble_with_path
+= "/";
151 _exectuble_with_path
+= _exectuble
;
154 Application::~Application()
160 Application::error_t
Application::run(const char *args
[])
165 _stdout_buffer
.clear();
166 _stderr_buffer
.clear();
168 posix_spawn_file_actions_t file_actions
;
169 posix_spawn_file_actions_init(&file_actions
);
171 stdin_fd
.dup_for_spawn(Application::Pipe::READ
, file_actions
);
172 stdout_fd
.dup_for_spawn(Application::Pipe::WRITE
, file_actions
);
173 stderr_fd
.dup_for_spawn(Application::Pipe::WRITE
, file_actions
);
175 posix_spawnattr_t spawnattr
;
176 posix_spawnattr_init(&spawnattr
);
180 fatal_assert(posix_spawnattr_setsigmask(&spawnattr
, &set
) == 0);
187 std::string gdb_run_file
= create_tmpfile(_exectuble_name
);
188 std::fstream file_stream
;
189 file_stream
.open(gdb_run_file
.c_str(), std::fstream::out
| std::fstream::trunc
);
191 _gdb_filename
= create_tmpfile(_exectuble_name
);
193 << "set logging redirect on" << std::endl
194 << "set logging file " << _gdb_filename
<< std::endl
195 << "set logging overwrite on" << std::endl
196 << "set logging on" << std::endl
197 << "set environment LIBTEST_IN_GDB=1" << std::endl
198 << "run " << arguments() << std::endl
199 << "thread apply all bt" << std::endl
200 << "quit" << std::endl
;
202 fatal_assert(file_stream
.good());
207 // libtool --mode=execute gdb -f -x binary
209 const_cast<char *>(libtool()),
210 const_cast<char *>("--mode=execute"),
211 const_cast<char *>("gdb"),
212 const_cast<char *>("-batch"),
213 const_cast<char *>("-f"),
214 const_cast<char *>("-x"),
215 const_cast<char *>(gdb_run_file
.c_str()),
216 const_cast<char *>(_exectuble_with_path
.c_str()),
219 spawn_ret
= posix_spawnp(&_pid
, libtool(), &file_actions
, &spawnattr
, argv
, environ
);
225 const_cast<char *>("gdb"),
226 const_cast<char *>("-batch"),
227 const_cast<char *>("-f"),
228 const_cast<char *>("-x"),
229 const_cast<char *>(gdb_run_file
.c_str()),
230 const_cast<char *>(_exectuble_with_path
.c_str()),
232 spawn_ret
= posix_spawnp(&_pid
, "gdb", &file_actions
, &spawnattr
, argv
, environ
);
239 spawn_ret
= posix_spawn(&_pid
, built_argv
[0], &file_actions
, &spawnattr
, built_argv
, NULL
);
243 spawn_ret
= posix_spawnp(&_pid
, built_argv
[0], &file_actions
, &spawnattr
, built_argv
, NULL
);
247 posix_spawn_file_actions_destroy(&file_actions
);
248 posix_spawnattr_destroy(&spawnattr
);
250 stdin_fd
.close(Application::Pipe::READ
);
251 stdout_fd
.close(Application::Pipe::WRITE
);
252 stderr_fd
.close(Application::Pipe::WRITE
);
256 if (_will_fail
== false)
258 Error
<< strerror(spawn_ret
) << "(" << spawn_ret
<< ")";
261 return Application::INVALID
;
264 return Application::SUCCESS
;
267 bool Application::check() const
269 if (_pid
> 1 and kill(_pid
, 0) == 0)
277 void Application::murder()
282 while ((count
--) > 0 and check())
284 int kill_ret
= kill(_pid
, SIGTERM
);
289 if ((waitpid_ret
= waitpid(_pid
, &status
, WNOHANG
)) == -1)
298 Error
<< "waitpid() failed after kill with error of " << strerror(errno
);
303 if (waitpid_ret
== 0)
305 libtest::dream(1, 0);
310 Error
<< "kill(pid, SIGTERM) failed after kill with error of " << strerror(errno
);
317 // If for whatever reason it lives, kill it hard
320 (void)kill(_pid
, SIGKILL
);
326 // false means that no data was returned
327 bool Application::slurp()
329 struct pollfd fds
[2];
330 fds
[0].fd
= stdout_fd
.fd();
331 fds
[0].events
= POLLRDNORM
;
333 fds
[1].fd
= stderr_fd
.fd();
334 fds
[1].events
= POLLRDNORM
;
338 if ((active_fd
= poll(fds
, 2, 0)) == -1)
341 switch ((error
= errno
))
343 #ifdef TARGET_OS_LINUX
351 fatal_message(strerror(error
));
355 fatal_message("RLIMIT_NOFILE exceeded, or if OSX the timeout value was invalid");
359 fatal_message(strerror(error
));
371 bool data_was_read
= false;
372 if (fds
[0].revents
& POLLRDNORM
)
374 if (stdout_fd
.read(_stdout_buffer
) == true)
380 if (fds
[1].revents
& POLLRDNORM
)
382 if (stderr_fd
.read(_stderr_buffer
) == true)
388 return data_was_read
;
391 Application::error_t
Application::wait(bool nohang
)
395 return Application::INVALID
;
400 error_t exit_code
= FAILURE
;
404 if ((waited_pid
= waitpid(_pid
, &status
, nohang
? WNOHANG
: 0)) == -1)
409 exit_code
= Application::SUCCESS
;
416 Error
<< "Error occured while waitpid(" << strerror(errno
) << ") on pid " << int(_pid
);
420 else if (waited_pid
== 0)
422 exit_code
= Application::SUCCESS
;
426 if (waited_pid
!= _pid
)
428 throw libtest::fatal(LIBYATL_DEFAULT_PARAM
, "Pid mismatch, %d != %d", int(waited_pid
), int(_pid
));
430 exit_code
= int_to_error_t(exited_successfully(status
));
437 if (exit_code
== Application::INVALID
)
439 Error
<< print_argv(built_argv
, _argc
);
446 void Application::add_long_option(const std::string
& name
, const std::string
& option_value
)
448 std::string
arg(name
);
450 _options
.push_back(std::make_pair(arg
, std::string()));
453 void Application::add_option(const std::string
& arg
)
455 _options
.push_back(std::make_pair(arg
, std::string()));
458 void Application::add_option(const std::string
& name
, const std::string
& value
)
460 _options
.push_back(std::make_pair(name
, value
));
463 Application::Pipe::Pipe(int arg
) :
472 int Application::Pipe::Pipe::fd()
474 if (_std_fd
== STDOUT_FILENO
)
476 return _pipe_fd
[READ
];
478 else if (_std_fd
== STDERR_FILENO
)
480 return _pipe_fd
[READ
];
483 return _pipe_fd
[WRITE
]; // STDIN_FILENO
487 bool Application::Pipe::read(libtest::vchar_t
& arg
)
489 fatal_assert(_std_fd
== STDOUT_FILENO
or _std_fd
== STDERR_FILENO
);
491 bool data_was_read
= false;
494 char buffer
[1024]= { 0 };
495 while ((read_length
= ::read(_pipe_fd
[READ
], buffer
, sizeof(buffer
))))
497 if (read_length
== -1)
505 Error
<< strerror(errno
);
513 arg
.reserve(read_length
+1);
514 for (size_t x
= 0; x
< read_length
; x
++)
516 arg
.push_back(buffer
[x
]);
518 // @todo Suck up all errput code here
521 return data_was_read
;
524 void Application::Pipe::nonblock()
527 if ((ret
= fcntl(_pipe_fd
[READ
], F_GETFL
, 0)) == -1)
529 Error
<< "fcntl(F_GETFL) " << strerror(errno
);
530 throw strerror(errno
);
533 if ((ret
= fcntl(_pipe_fd
[READ
], F_SETFL
, ret
| O_NONBLOCK
)) == -1)
535 Error
<< "fcntl(F_SETFL) " << strerror(errno
);
536 throw strerror(errno
);
540 void Application::Pipe::reset()
546 if (pipe2(_pipe_fd
, O_NONBLOCK
) == -1)
548 if (pipe(_pipe_fd
) == -1)
551 fatal_message(strerror(errno
));
563 void Application::Pipe::cloexec()
566 if ((ret
= fcntl(_pipe_fd
[WRITE
], F_GETFD
, 0)) == -1)
568 Error
<< "fcntl(F_GETFD) " << strerror(errno
);
569 throw strerror(errno
);
572 if ((ret
= fcntl(_pipe_fd
[WRITE
], F_SETFD
, ret
| FD_CLOEXEC
)) == -1)
574 Error
<< "fcntl(F_SETFD) " << strerror(errno
);
575 throw strerror(errno
);
579 Application::Pipe::~Pipe()
585 void Application::Pipe::dup_for_spawn(const close_t
& arg
, posix_spawn_file_actions_t
& file_actions
)
590 if ((ret
= posix_spawn_file_actions_adddup2(&file_actions
, _pipe_fd
[type
], _std_fd
)) < 0)
592 Error
<< "posix_spawn_file_actions_adddup2(" << strerror(ret
) << ")";
593 fatal_message(strerror(ret
));
596 if ((ret
= posix_spawn_file_actions_addclose(&file_actions
, _pipe_fd
[type
])) < 0)
598 Error
<< "posix_spawn_file_actions_adddup2(" << strerror(ret
) << ")";
599 fatal_message(strerror(ret
));
603 void Application::Pipe::close(const close_t
& arg
)
610 if (::close(_pipe_fd
[type
]) == -1)
612 Error
<< "close(" << strerror(errno
) << ")";
619 void Application::create_argv(const char *args
[])
622 fatal_assert(_argc
== 0);
626 _argc
+= 2; // +2 for libtool --mode=execute
629 _argc
+= 1; // For the command
632 valgrind --error-exitcode=1 --leak-check=yes --show-reachable=yes --track-fds=yes --track-origin=yes --malloc-fill=A5 --free-fill=DE --log-file=
638 else if (_use_ptrcheck
)
641 valgrind --error-exitcode=1 --tool=exp-ptrcheck --log-file=
645 else if (_use_gdb
) // gdb
650 for (Options::const_iterator iter
= _options
.begin(); iter
!= _options
.end(); iter
++)
653 if ((*iter
).second
.empty() == false)
661 for (const char **ptr
= args
; *ptr
; ++ptr
)
667 _argc
+= 1; // for the NULL
669 built_argv
= new char * [_argc
];
675 built_argv
[x
++]= strdup(libtool());
676 built_argv
[x
++]= strdup("--mode=execute");
682 valgrind --error-exitcode=1 --leak-check=yes --show-reachable=yes --track-fds=yes --malloc-fill=A5 --free-fill=DE
684 built_argv
[x
++]= strdup("valgrind");
685 built_argv
[x
++]= strdup("--error-exitcode=1");
686 built_argv
[x
++]= strdup("--leak-check=yes");
687 built_argv
[x
++]= strdup("--show-reachable=yes");
688 built_argv
[x
++]= strdup("--track-fds=yes");
690 built_argv
[x
++]= strdup("--track-origin=yes");
692 built_argv
[x
++]= strdup("--malloc-fill=A5");
693 built_argv
[x
++]= strdup("--free-fill=DE");
695 std::string log_file
= create_tmpfile("valgrind");
697 int length
= snprintf(buffer
, sizeof(buffer
), "--log-file=%s", log_file
.c_str());
698 fatal_assert(length
> 0 and length
< sizeof(buffer
));
699 built_argv
[x
++]= strdup(buffer
);
701 else if (_use_ptrcheck
)
704 valgrind --error-exitcode=1 --tool=exp-ptrcheck --log-file=
706 built_argv
[x
++]= strdup("valgrind");
707 built_argv
[x
++]= strdup("--error-exitcode=1");
708 built_argv
[x
++]= strdup("--tool=exp-ptrcheck");
710 std::string log_file
= create_tmpfile("ptrcheck");
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
);
718 built_argv
[x
++]= strdup("gdb");
721 built_argv
[x
++]= strdup(_exectuble_with_path
.c_str());
723 for (Options::const_iterator iter
= _options
.begin(); iter
!= _options
.end(); iter
++)
725 built_argv
[x
++]= strdup((*iter
).first
.c_str());
726 if ((*iter
).second
.empty() == false)
728 built_argv
[x
++]= strdup((*iter
).second
.c_str());
734 for (const char **ptr
= args
; *ptr
; ++ptr
)
736 built_argv
[x
++]= strdup(*ptr
);
739 built_argv
[x
++]= NULL
;
740 fatal_assert(x
== _argc
);
743 std::string
Application::print()
745 return print_argv(built_argv
, _argc
);
748 std::string
Application::arguments()
750 std::stringstream arg_buffer
;
752 for (size_t x
= 1 + _use_libtool
? 2 : 0;
753 x
< _argc
and built_argv
[x
];
756 arg_buffer
<< built_argv
[x
] << " ";
759 return arg_buffer
.str();
762 void Application::delete_argv()
766 for (size_t x
= 0; x
< _argc
; x
++)
770 ::free(built_argv
[x
]);
780 int exec_cmdline(const std::string
& command
, const char *args
[], bool use_libtool
)
782 Application
app(command
, use_libtool
);
784 Application::error_t ret
= app
.run(args
);
786 if (ret
!= Application::SUCCESS
)
791 return int(app
.wait(false));
794 const char *gearmand_binary()
796 return GEARMAND_BINARY
;
799 const char *drizzled_binary()
801 return DRIZZLED_BINARY
;
804 } // namespace exec_cmdline