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>
57 static char **environ
= NULL
;
61 static int exited_successfully(int status
)
68 if (WIFEXITED(status
) == true)
70 return WEXITSTATUS(status
);
72 else if (WIFSIGNALED(status
) == true)
74 return WTERMSIG(status
);
83 std::string
print_argv(char * * & built_argv
, const size_t& argc
)
85 std::stringstream arg_buffer
;
87 for (size_t x
= 0; x
< argc
; ++x
)
89 arg_buffer
<< built_argv
[x
] << " ";
92 return arg_buffer
.str();
95 std::string
print_argv(char** argv
)
97 std::stringstream arg_buffer
;
99 for (char** ptr
= argv
; *ptr
; ++ptr
)
101 arg_buffer
<< *ptr
<< " ";
104 return arg_buffer
.str();
107 static Application::error_t
int_to_error_t(int arg
)
112 return Application::INVALID
;
115 return Application::SUCCESS
;
119 return Application::FAILURE
;
126 Application::Application(const std::string
& arg
, const bool _use_libtool_arg
) :
127 _use_libtool(_use_libtool_arg
),
128 _use_valgrind(false),
130 _use_ptrcheck(false),
134 stdin_fd(STDIN_FILENO
),
135 stdout_fd(STDOUT_FILENO
),
136 stderr_fd(STDERR_FILENO
),
142 if (libtool() == NULL
)
144 fatal_message("libtool requested, but know libtool was found");
148 // Find just the name of the application with no path
150 size_t found
= arg
.find_last_of("/\\");
153 _exectuble_name
= arg
.substr(found
+1);
157 _exectuble_name
= arg
;
161 if (_use_libtool
and getenv("PWD"))
163 _exectuble_with_path
+= getenv("PWD");
164 _exectuble_with_path
+= "/";
166 _exectuble_with_path
+= _exectuble
;
169 Application::~Application()
175 Application::error_t
Application::run(const char *args
[])
180 _stdout_buffer
.clear();
181 _stderr_buffer
.clear();
183 posix_spawn_file_actions_t file_actions
;
184 posix_spawn_file_actions_init(&file_actions
);
186 stdin_fd
.dup_for_spawn(file_actions
);
187 stdout_fd
.dup_for_spawn(file_actions
);
188 stderr_fd
.dup_for_spawn(file_actions
);
190 posix_spawnattr_t spawnattr
;
191 posix_spawnattr_init(&spawnattr
);
195 fatal_assert(posix_spawnattr_setsigmask(&spawnattr
, &set
) == 0);
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
);
206 _gdb_filename
= create_tmpfile(_exectuble_name
);
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
;
217 fatal_assert(file_stream
.good());
222 // libtool --mode=execute gdb -f -x binary
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()),
234 spawn_ret
= posix_spawnp(&_pid
, libtool(), &file_actions
, &spawnattr
, argv
, environ
);
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()),
247 spawn_ret
= posix_spawnp(&_pid
, "gdb", &file_actions
, &spawnattr
, argv
, environ
);
254 spawn_ret
= posix_spawn(&_pid
, built_argv
[0], &file_actions
, &spawnattr
, built_argv
, NULL
);
258 spawn_ret
= posix_spawnp(&_pid
, built_argv
[0], &file_actions
, &spawnattr
, built_argv
, NULL
);
262 posix_spawn_file_actions_destroy(&file_actions
);
263 posix_spawnattr_destroy(&spawnattr
);
265 stdin_fd
.close(Application::Pipe::READ
);
266 stdout_fd
.close(Application::Pipe::WRITE
);
267 stderr_fd
.close(Application::Pipe::WRITE
);
271 if (_will_fail
== false)
273 Error
<< strerror(spawn_ret
) << "(" << spawn_ret
<< ")";
276 return Application::INVALID
;
279 return Application::SUCCESS
;
282 bool Application::check() const
284 if (_pid
> 1 and kill(_pid
, 0) == 0)
292 void Application::murder()
297 while ((count
--) > 0 and check())
299 int kill_ret
= kill(_pid
, SIGTERM
);
304 if ((waitpid_ret
= waitpid(_pid
, &status
, WNOHANG
)) == -1)
313 Error
<< "waitpid() failed after kill with error of " << strerror(errno
);
318 if (waitpid_ret
== 0)
320 libtest::dream(1, 0);
325 Error
<< "kill(pid, SIGTERM) failed after kill with error of " << strerror(errno
);
332 // If for whatever reason it lives, kill it hard
335 (void)kill(_pid
, SIGKILL
);
341 // false means that no data was returned
342 bool Application::slurp()
344 struct pollfd fds
[2];
345 fds
[0].fd
= stdout_fd
.fd();
346 fds
[0].events
= POLLRDNORM
;
348 fds
[1].fd
= stderr_fd
.fd();
349 fds
[1].events
= POLLRDNORM
;
353 if ((active_fd
= poll(fds
, 2, 0)) == -1)
356 switch ((error
= errno
))
358 #ifdef TARGET_OS_LINUX
366 fatal_message(strerror(error
));
370 fatal_message("RLIMIT_NOFILE exceeded, or if OSX the timeout value was invalid");
374 fatal_message(strerror(error
));
386 bool data_was_read
= false;
387 if (fds
[0].revents
& POLLRDNORM
)
389 if (stdout_fd
.read(_stdout_buffer
) == true)
395 if (fds
[1].revents
& POLLRDNORM
)
397 if (stderr_fd
.read(_stderr_buffer
) == true)
403 return data_was_read
;
406 Application::error_t
Application::wait(bool nohang
)
410 return Application::INVALID
;
415 error_t exit_code
= FAILURE
;
419 if ((waited_pid
= waitpid(_pid
, &status
, nohang
? WNOHANG
: 0)) == -1)
424 exit_code
= Application::SUCCESS
;
431 Error
<< "Error occured while waitpid(" << strerror(errno
) << ") on pid " << int(_pid
);
435 else if (waited_pid
== 0)
437 exit_code
= Application::SUCCESS
;
441 if (waited_pid
!= _pid
)
443 throw libtest::fatal(LIBYATL_DEFAULT_PARAM
, "Pid mismatch, %d != %d", int(waited_pid
), int(_pid
));
445 exit_code
= int_to_error_t(exited_successfully(status
));
452 if (exit_code
== Application::INVALID
)
454 Error
<< print_argv(built_argv
, _argc
);
461 void Application::add_long_option(const std::string
& name
, const std::string
& option_value
)
463 std::string
arg(name
);
465 _options
.push_back(std::make_pair(arg
, std::string()));
468 void Application::add_option(const std::string
& arg
)
470 _options
.push_back(std::make_pair(arg
, std::string()));
473 void Application::add_option(const std::string
& name
, const std::string
& value
)
475 _options
.push_back(std::make_pair(name
, value
));
478 Application::Pipe::Pipe(int arg
) :
487 int Application::Pipe::Pipe::fd()
489 if (_std_fd
== STDOUT_FILENO
)
491 return _pipe_fd
[READ
];
493 else if (_std_fd
== STDERR_FILENO
)
495 return _pipe_fd
[READ
];
498 return _pipe_fd
[WRITE
]; // STDIN_FILENO
502 bool Application::Pipe::read(libtest::vchar_t
& arg
)
504 fatal_assert(_std_fd
== STDOUT_FILENO
or _std_fd
== STDERR_FILENO
);
506 bool data_was_read
= false;
509 char buffer
[1024]= { 0 };
510 while ((read_length
= ::read(_pipe_fd
[READ
], buffer
, sizeof(buffer
))))
512 if (read_length
== -1)
520 Error
<< strerror(errno
);
528 arg
.reserve(read_length
+1);
529 for (size_t x
= 0; x
< read_length
; ++x
)
531 arg
.push_back(buffer
[x
]);
533 // @todo Suck up all errput code here
536 return data_was_read
;
539 void Application::Pipe::nonblock()
542 if ((ret
= fcntl(_pipe_fd
[READ
], F_GETFL
, 0)) == -1)
544 Error
<< "fcntl(F_GETFL) " << strerror(errno
);
545 throw strerror(errno
);
548 if ((ret
= fcntl(_pipe_fd
[READ
], F_SETFL
, ret
| O_NONBLOCK
)) == -1)
550 Error
<< "fcntl(F_SETFL) " << strerror(errno
);
551 throw strerror(errno
);
555 void Application::Pipe::reset()
561 if (pipe2(_pipe_fd
, O_NONBLOCK
) == -1)
563 if (pipe(_pipe_fd
) == -1)
566 fatal_message(strerror(errno
));
578 void Application::Pipe::cloexec()
581 if ((ret
= fcntl(_pipe_fd
[WRITE
], F_GETFD
, 0)) == -1)
583 Error
<< "fcntl(F_GETFD) " << strerror(errno
);
584 throw strerror(errno
);
587 if ((ret
= fcntl(_pipe_fd
[WRITE
], F_SETFD
, ret
| FD_CLOEXEC
)) == -1)
589 Error
<< "fcntl(F_SETFD) " << strerror(errno
);
590 throw strerror(errno
);
594 Application::Pipe::~Pipe()
596 if (_pipe_fd
[0] != -1)
598 ::close(_pipe_fd
[0]);
601 if (_pipe_fd
[1] != -1)
603 ::close(_pipe_fd
[1]);
607 void Application::Pipe::dup_for_spawn(posix_spawn_file_actions_t
& file_actions
)
609 int type
= STDIN_FILENO
== _std_fd
? 0 : 1;
612 if ((ret
= posix_spawn_file_actions_adddup2(&file_actions
, _pipe_fd
[type
], _std_fd
)) < 0)
614 Error
<< "posix_spawn_file_actions_adddup2(" << strerror(ret
) << ")";
615 fatal_message(strerror(ret
));
618 if ((ret
= posix_spawn_file_actions_addclose(&file_actions
, _pipe_fd
[type
])) < 0)
620 Error
<< "posix_spawn_file_actions_adddup2(" << strerror(ret
) << ")";
621 fatal_message(strerror(ret
));
625 void Application::Pipe::close(const close_t
& arg
)
631 if (::close(_pipe_fd
[type
]) == -1)
633 Error
<< "close(" << strerror(errno
) << ")";
640 void Application::create_argv(const char *args
[])
643 fatal_assert(_argc
== 0);
647 _argc
+= 2; // +2 for libtool --mode=execute
650 _argc
+= 1; // For the command
653 valgrind --error-exitcode=1 --leak-check=yes --show-reachable=yes --track-fds=yes --track-origin=yes --malloc-fill=A5 --free-fill=DE --log-file=
659 else if (_use_ptrcheck
)
662 valgrind --error-exitcode=1 --tool=exp-ptrcheck --log-file=
666 else if (_use_gdb
) // gdb
671 for (Options::const_iterator iter
= _options
.begin(); iter
!= _options
.end(); ++iter
)
674 if ((*iter
).second
.empty() == false)
682 for (const char **ptr
= args
; *ptr
; ++ptr
)
688 _argc
+= 1; // for the NULL
690 built_argv
= new char * [_argc
];
696 built_argv
[x
++]= strdup(libtool());
697 built_argv
[x
++]= strdup("--mode=execute");
703 valgrind --error-exitcode=1 --leak-check=yes --show-reachable=yes --track-fds=yes --malloc-fill=A5 --free-fill=DE
705 built_argv
[x
++]= strdup("valgrind");
706 built_argv
[x
++]= strdup("--error-exitcode=1");
707 built_argv
[x
++]= strdup("--leak-check=yes");
708 built_argv
[x
++]= strdup("--show-reachable=yes");
709 built_argv
[x
++]= strdup("--track-fds=yes");
711 built_argv
[x
++]= strdup("--track-origin=yes");
713 built_argv
[x
++]= strdup("--malloc-fill=A5");
714 built_argv
[x
++]= strdup("--free-fill=DE");
716 std::string log_file
= create_tmpfile("valgrind");
718 int length
= snprintf(buffer
, sizeof(buffer
), "--log-file=%s", log_file
.c_str());
719 fatal_assert(length
> 0 and length
< sizeof(buffer
));
720 built_argv
[x
++]= strdup(buffer
);
722 else if (_use_ptrcheck
)
725 valgrind --error-exitcode=1 --tool=exp-ptrcheck --log-file=
727 built_argv
[x
++]= strdup("valgrind");
728 built_argv
[x
++]= strdup("--error-exitcode=1");
729 built_argv
[x
++]= strdup("--tool=exp-ptrcheck");
731 std::string log_file
= create_tmpfile("ptrcheck");
733 int length
= snprintf(buffer
, sizeof(buffer
), "--log-file=%s", log_file
.c_str());
734 fatal_assert(length
> 0 and length
< sizeof(buffer
));
735 built_argv
[x
++]= strdup(buffer
);
739 built_argv
[x
++]= strdup("gdb");
742 built_argv
[x
++]= strdup(_exectuble_with_path
.c_str());
744 for (Options::const_iterator iter
= _options
.begin(); iter
!= _options
.end(); iter
++)
746 built_argv
[x
++]= strdup((*iter
).first
.c_str());
747 if ((*iter
).second
.empty() == false)
749 built_argv
[x
++]= strdup((*iter
).second
.c_str());
755 for (const char **ptr
= args
; *ptr
; ++ptr
)
757 built_argv
[x
++]= strdup(*ptr
);
760 built_argv
[x
++]= NULL
;
761 fatal_assert(x
== _argc
);
764 std::string
Application::print()
766 return print_argv(built_argv
, _argc
);
769 std::string
Application::arguments()
771 std::stringstream arg_buffer
;
773 for (size_t x
= (1 +_use_libtool
) ? 2 : 0;
774 x
< _argc
and built_argv
[x
];
777 arg_buffer
<< built_argv
[x
] << " ";
780 return arg_buffer
.str();
783 void Application::delete_argv()
787 for (size_t x
= 0; x
< _argc
; ++x
)
791 ::free(built_argv
[x
]);
801 int exec_cmdline(const std::string
& command
, const char *args
[], bool use_libtool
)
803 Application
app(command
, use_libtool
);
805 Application::error_t ret
= app
.run(args
);
807 if (ret
!= Application::SUCCESS
)
812 return int(app
.wait(false));
815 const char *gearmand_binary()
817 return GEARMAND_BINARY
;
820 const char *drizzled_binary()
822 return DRIZZLED_BINARY
;
825 } // namespace exec_cmdline