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(Application::Pipe::READ
, file_actions
);
187 stdout_fd
.dup_for_spawn(Application::Pipe::WRITE
, file_actions
);
188 stderr_fd
.dup_for_spawn(Application::Pipe::WRITE
, 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()
600 void Application::Pipe::dup_for_spawn(const close_t
& arg
, posix_spawn_file_actions_t
& file_actions
)
605 if ((ret
= posix_spawn_file_actions_adddup2(&file_actions
, _pipe_fd
[type
], _std_fd
)) < 0)
607 Error
<< "posix_spawn_file_actions_adddup2(" << strerror(ret
) << ")";
608 fatal_message(strerror(ret
));
611 if ((ret
= posix_spawn_file_actions_addclose(&file_actions
, _pipe_fd
[type
])) < 0)
613 Error
<< "posix_spawn_file_actions_adddup2(" << strerror(ret
) << ")";
614 fatal_message(strerror(ret
));
618 void Application::Pipe::close(const close_t
& arg
)
625 if (::close(_pipe_fd
[type
]) == -1)
627 Error
<< "close(" << strerror(errno
) << ")";
634 void Application::create_argv(const char *args
[])
637 fatal_assert(_argc
== 0);
641 _argc
+= 2; // +2 for libtool --mode=execute
644 _argc
+= 1; // For the command
647 valgrind --error-exitcode=1 --leak-check=yes --show-reachable=yes --track-fds=yes --track-origin=yes --malloc-fill=A5 --free-fill=DE --log-file=
653 else if (_use_ptrcheck
)
656 valgrind --error-exitcode=1 --tool=exp-ptrcheck --log-file=
660 else if (_use_gdb
) // gdb
665 for (Options::const_iterator iter
= _options
.begin(); iter
!= _options
.end(); iter
++)
668 if ((*iter
).second
.empty() == false)
676 for (const char **ptr
= args
; *ptr
; ++ptr
)
682 _argc
+= 1; // for the NULL
684 built_argv
= new char * [_argc
];
690 built_argv
[x
++]= strdup(libtool());
691 built_argv
[x
++]= strdup("--mode=execute");
697 valgrind --error-exitcode=1 --leak-check=yes --show-reachable=yes --track-fds=yes --malloc-fill=A5 --free-fill=DE
699 built_argv
[x
++]= strdup("valgrind");
700 built_argv
[x
++]= strdup("--error-exitcode=1");
701 built_argv
[x
++]= strdup("--leak-check=yes");
702 built_argv
[x
++]= strdup("--show-reachable=yes");
703 built_argv
[x
++]= strdup("--track-fds=yes");
705 built_argv
[x
++]= strdup("--track-origin=yes");
707 built_argv
[x
++]= strdup("--malloc-fill=A5");
708 built_argv
[x
++]= strdup("--free-fill=DE");
710 std::string log_file
= create_tmpfile("valgrind");
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
);
716 else if (_use_ptrcheck
)
719 valgrind --error-exitcode=1 --tool=exp-ptrcheck --log-file=
721 built_argv
[x
++]= strdup("valgrind");
722 built_argv
[x
++]= strdup("--error-exitcode=1");
723 built_argv
[x
++]= strdup("--tool=exp-ptrcheck");
725 std::string log_file
= create_tmpfile("ptrcheck");
727 int length
= snprintf(buffer
, sizeof(buffer
), "--log-file=%s", log_file
.c_str());
728 fatal_assert(length
> 0 and length
< sizeof(buffer
));
729 built_argv
[x
++]= strdup(buffer
);
733 built_argv
[x
++]= strdup("gdb");
736 built_argv
[x
++]= strdup(_exectuble_with_path
.c_str());
738 for (Options::const_iterator iter
= _options
.begin(); iter
!= _options
.end(); iter
++)
740 built_argv
[x
++]= strdup((*iter
).first
.c_str());
741 if ((*iter
).second
.empty() == false)
743 built_argv
[x
++]= strdup((*iter
).second
.c_str());
749 for (const char **ptr
= args
; *ptr
; ++ptr
)
751 built_argv
[x
++]= strdup(*ptr
);
754 built_argv
[x
++]= NULL
;
755 fatal_assert(x
== _argc
);
758 std::string
Application::print()
760 return print_argv(built_argv
, _argc
);
763 std::string
Application::arguments()
765 std::stringstream arg_buffer
;
767 for (size_t x
= 1 + _use_libtool
? 2 : 0;
768 x
< _argc
and built_argv
[x
];
771 arg_buffer
<< built_argv
[x
] << " ";
774 return arg_buffer
.str();
777 void Application::delete_argv()
781 for (size_t x
= 0; x
< _argc
; x
++)
785 ::free(built_argv
[x
]);
795 int exec_cmdline(const std::string
& command
, const char *args
[], bool use_libtool
)
797 Application
app(command
, use_libtool
);
799 Application::error_t ret
= app
.run(args
);
801 if (ret
!= Application::SUCCESS
)
806 return int(app
.wait(false));
809 const char *gearmand_binary()
811 return GEARMAND_BINARY
;
814 const char *drizzled_binary()
816 return DRIZZLED_BINARY
;
819 } // namespace exec_cmdline