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>
41 static int exited_successfully(int status
)
48 if (WIFEXITED(status
) == true)
50 return WEXITSTATUS(status
);
52 else if (WIFSIGNALED(status
) == true)
54 return WTERMSIG(status
);
63 std::string
print_argv(char * * & built_argv
, const size_t& argc
)
65 std::stringstream arg_buffer
;
67 for (size_t x
= 0; x
< argc
; x
++)
69 arg_buffer
<< built_argv
[x
] << " ";
72 return arg_buffer
.str();
75 std::string
print_argv(char** argv
)
77 std::stringstream arg_buffer
;
79 for (char** ptr
= argv
; *ptr
; ptr
++)
81 arg_buffer
<< *ptr
<< " ";
84 return arg_buffer
.str();
87 static Application::error_t
int_to_error_t(int arg
)
92 return Application::INVALID
;
95 return Application::SUCCESS
;
99 return Application::FAILURE
;
106 Application::Application(const std::string
& arg
, const bool _use_libtool_arg
) :
107 _use_libtool(_use_libtool_arg
),
108 _use_valgrind(false),
110 _use_ptrcheck(false),
113 stdin_fd(STDIN_FILENO
),
114 stdout_fd(STDOUT_FILENO
),
115 stderr_fd(STDERR_FILENO
),
121 if (libtool() == NULL
)
123 fatal_message("libtool requested, but know libtool was found");
127 // Find just the name of the application with no path
129 size_t found
= arg
.find_last_of("/\\");
132 _exectuble_name
= arg
.substr(found
+1);
136 _exectuble_name
= arg
;
140 if (_use_libtool
and getenv("PWD"))
142 _exectuble_with_path
+= getenv("PWD");
143 _exectuble_with_path
+= "/";
145 _exectuble_with_path
+= _exectuble
;
148 Application::~Application()
154 Application::error_t
Application::run(const char *args
[])
159 _stdout_buffer
.clear();
160 _stderr_buffer
.clear();
162 posix_spawn_file_actions_t file_actions
;
163 posix_spawn_file_actions_init(&file_actions
);
165 stdin_fd
.dup_for_spawn(Application::Pipe::READ
, file_actions
);
166 stdout_fd
.dup_for_spawn(Application::Pipe::WRITE
, file_actions
);
167 stderr_fd
.dup_for_spawn(Application::Pipe::WRITE
, file_actions
);
169 posix_spawnattr_t spawnattr
;
170 posix_spawnattr_init(&spawnattr
);
174 fatal_assert(posix_spawnattr_setsigmask(&spawnattr
, &set
) == 0);
181 std::string gdb_run_file
= create_tmpfile(_exectuble_name
);
182 std::fstream file_stream
;
183 file_stream
.open(gdb_run_file
.c_str(), std::fstream::out
| std::fstream::trunc
);
185 _gdb_filename
= create_tmpfile(_exectuble_name
);
187 << "set logging redirect on" << std::endl
188 << "set logging file " << _gdb_filename
<< std::endl
189 << "set logging overwrite on" << std::endl
190 << "set logging on" << std::endl
191 << "set environment LIBTEST_IN_GDB=1" << std::endl
192 << "run " << arguments() << std::endl
193 << "thread apply all bt" << std::endl
194 << "quit" << std::endl
;
196 fatal_assert(file_stream
.good());
201 // libtool --mode=execute gdb -f -x binary
203 const_cast<char *>(libtool()),
204 const_cast<char *>("--mode=execute"),
205 const_cast<char *>("gdb"),
206 const_cast<char *>("-batch"),
207 const_cast<char *>("-f"),
208 const_cast<char *>("-x"),
209 const_cast<char *>(gdb_run_file
.c_str()),
210 const_cast<char *>(_exectuble_with_path
.c_str()),
213 spawn_ret
= posix_spawnp(&_pid
, libtool(), &file_actions
, &spawnattr
, argv
, environ
);
219 const_cast<char *>("gdb"),
220 const_cast<char *>("-batch"),
221 const_cast<char *>("-f"),
222 const_cast<char *>("-x"),
223 const_cast<char *>(gdb_run_file
.c_str()),
224 const_cast<char *>(_exectuble_with_path
.c_str()),
226 spawn_ret
= posix_spawnp(&_pid
, "gdb", &file_actions
, &spawnattr
, argv
, environ
);
233 spawn_ret
= posix_spawn(&_pid
, built_argv
[0], &file_actions
, &spawnattr
, built_argv
, NULL
);
237 spawn_ret
= posix_spawnp(&_pid
, built_argv
[0], &file_actions
, &spawnattr
, built_argv
, NULL
);
241 posix_spawn_file_actions_destroy(&file_actions
);
242 posix_spawnattr_destroy(&spawnattr
);
244 stdin_fd
.close(Application::Pipe::READ
);
245 stdout_fd
.close(Application::Pipe::WRITE
);
246 stderr_fd
.close(Application::Pipe::WRITE
);
250 Error
<< strerror(spawn_ret
) << "(" << spawn_ret
<< ")";
252 return Application::INVALID
;
255 return Application::SUCCESS
;
258 bool Application::check() const
260 if (_pid
> 1 and kill(_pid
, 0) == 0)
268 void Application::murder()
273 while ((count
--) > 0 and check())
275 int kill_ret
= kill(_pid
, SIGTERM
);
280 if ((waitpid_ret
= waitpid(_pid
, &status
, WNOHANG
)) == -1)
289 Error
<< "waitpid() failed after kill with error of " << strerror(errno
);
294 if (waitpid_ret
== 0)
296 libtest::dream(1, 0);
301 Error
<< "kill(pid, SIGTERM) failed after kill with error of " << strerror(errno
);
308 // If for whatever reason it lives, kill it hard
311 (void)kill(_pid
, SIGKILL
);
317 // false means that no data was returned
318 bool Application::slurp()
320 struct pollfd fds
[2];
321 fds
[0].fd
= stdout_fd
.fd();
322 fds
[0].events
= POLLRDNORM
;
324 fds
[1].fd
= stderr_fd
.fd();
325 fds
[1].events
= POLLRDNORM
;
329 if ((active_fd
= poll(fds
, 2, 0)) == -1)
332 switch ((error
= errno
))
334 #ifdef TARGET_OS_LINUX
342 fatal_message(strerror(error
));
346 fatal_message("RLIMIT_NOFILE exceeded, or if OSX the timeout value was invalid");
350 fatal_message(strerror(error
));
362 bool data_was_read
= false;
363 if (fds
[0].revents
& POLLRDNORM
)
365 if (stdout_fd
.read(_stdout_buffer
) == true)
371 if (fds
[1].revents
& POLLRDNORM
)
373 if (stderr_fd
.read(_stderr_buffer
) == true)
379 return data_was_read
;
382 Application::error_t
Application::wait(bool nohang
)
386 return Application::INVALID
;
391 error_t exit_code
= FAILURE
;
395 if ((waited_pid
= waitpid(_pid
, &status
, nohang
? WNOHANG
: 0)) == -1)
400 exit_code
= Application::SUCCESS
;
407 Error
<< "Error occured while waitpid(" << strerror(errno
) << ") on pid " << int(_pid
);
411 else if (waited_pid
== 0)
413 exit_code
= Application::SUCCESS
;
417 if (waited_pid
!= _pid
)
419 throw libtest::fatal(LIBYATL_DEFAULT_PARAM
, "Pid mismatch, %d != %d", int(waited_pid
), int(_pid
));
421 exit_code
= int_to_error_t(exited_successfully(status
));
428 if (exit_code
== Application::INVALID
)
430 Error
<< print_argv(built_argv
, _argc
);
437 void Application::add_long_option(const std::string
& name
, const std::string
& option_value
)
439 std::string
arg(name
);
441 _options
.push_back(std::make_pair(arg
, std::string()));
444 void Application::add_option(const std::string
& arg
)
446 _options
.push_back(std::make_pair(arg
, std::string()));
449 void Application::add_option(const std::string
& name
, const std::string
& value
)
451 _options
.push_back(std::make_pair(name
, value
));
454 Application::Pipe::Pipe(int arg
) :
463 int Application::Pipe::Pipe::fd()
465 if (_std_fd
== STDOUT_FILENO
)
467 return _pipe_fd
[READ
];
469 else if (_std_fd
== STDERR_FILENO
)
471 return _pipe_fd
[READ
];
474 return _pipe_fd
[WRITE
]; // STDIN_FILENO
478 bool Application::Pipe::read(libtest::vchar_t
& arg
)
480 fatal_assert(_std_fd
== STDOUT_FILENO
or _std_fd
== STDERR_FILENO
);
482 bool data_was_read
= false;
485 char buffer
[1024]= { 0 };
486 while ((read_length
= ::read(_pipe_fd
[READ
], buffer
, sizeof(buffer
))))
488 if (read_length
== -1)
496 Error
<< strerror(errno
);
504 arg
.reserve(read_length
+1);
505 for (size_t x
= 0; x
< read_length
; x
++)
507 arg
.push_back(buffer
[x
]);
509 // @todo Suck up all errput code here
512 return data_was_read
;
515 void Application::Pipe::nonblock()
518 if ((ret
= fcntl(_pipe_fd
[READ
], F_GETFL
, 0)) == -1)
520 Error
<< "fcntl(F_GETFL) " << strerror(errno
);
521 throw strerror(errno
);
524 if ((ret
= fcntl(_pipe_fd
[READ
], F_SETFL
, ret
| O_NONBLOCK
)) == -1)
526 Error
<< "fcntl(F_SETFL) " << strerror(errno
);
527 throw strerror(errno
);
531 void Application::Pipe::reset()
537 if (pipe2(_pipe_fd
, O_NONBLOCK
) == -1)
539 if (pipe(_pipe_fd
) == -1)
542 fatal_message(strerror(errno
));
554 void Application::Pipe::cloexec()
557 if ((ret
= fcntl(_pipe_fd
[WRITE
], F_GETFD
, 0)) == -1)
559 Error
<< "fcntl(F_GETFD) " << strerror(errno
);
560 throw strerror(errno
);
563 if ((ret
= fcntl(_pipe_fd
[WRITE
], F_SETFD
, ret
| FD_CLOEXEC
)) == -1)
565 Error
<< "fcntl(F_SETFD) " << strerror(errno
);
566 throw strerror(errno
);
570 Application::Pipe::~Pipe()
576 void Application::Pipe::dup_for_spawn(const close_t
& arg
, posix_spawn_file_actions_t
& file_actions
)
581 if ((ret
= posix_spawn_file_actions_adddup2(&file_actions
, _pipe_fd
[type
], _std_fd
)) < 0)
583 Error
<< "posix_spawn_file_actions_adddup2(" << strerror(ret
) << ")";
584 fatal_message(strerror(ret
));
587 if ((ret
= posix_spawn_file_actions_addclose(&file_actions
, _pipe_fd
[type
])) < 0)
589 Error
<< "posix_spawn_file_actions_adddup2(" << strerror(ret
) << ")";
590 fatal_message(strerror(ret
));
594 void Application::Pipe::close(const close_t
& arg
)
601 if (::close(_pipe_fd
[type
]) == -1)
603 Error
<< "close(" << strerror(errno
) << ")";
610 void Application::create_argv(const char *args
[])
613 fatal_assert(_argc
== 0);
617 _argc
+= 2; // +2 for libtool --mode=execute
620 _argc
+= 1; // For the command
623 valgrind --error-exitcode=1 --leak-check=yes --show-reachable=yes --track-fds=yes --track-origin=yes --malloc-fill=A5 --free-fill=DE --log-file=
629 else if (_use_ptrcheck
)
632 valgrind --error-exitcode=1 --tool=exp-ptrcheck --log-file=
636 else if (_use_gdb
) // gdb
641 for (Options::const_iterator iter
= _options
.begin(); iter
!= _options
.end(); iter
++)
644 if ((*iter
).second
.empty() == false)
652 for (const char **ptr
= args
; *ptr
; ++ptr
)
658 _argc
+= 1; // for the NULL
660 built_argv
= new char * [_argc
];
666 built_argv
[x
++]= strdup(libtool());
667 built_argv
[x
++]= strdup("--mode=execute");
673 valgrind --error-exitcode=1 --leak-check=yes --show-reachable=yes --track-fds=yes --malloc-fill=A5 --free-fill=DE
675 built_argv
[x
++]= strdup("valgrind");
676 built_argv
[x
++]= strdup("--error-exitcode=1");
677 built_argv
[x
++]= strdup("--leak-check=yes");
678 built_argv
[x
++]= strdup("--show-reachable=yes");
679 built_argv
[x
++]= strdup("--track-fds=yes");
681 built_argv
[x
++]= strdup("--track-origin=yes");
683 built_argv
[x
++]= strdup("--malloc-fill=A5");
684 built_argv
[x
++]= strdup("--free-fill=DE");
686 std::string log_file
= create_tmpfile("valgrind");
688 int length
= snprintf(buffer
, sizeof(buffer
), "--log-file=%s", log_file
.c_str());
689 fatal_assert(length
> 0 and length
< sizeof(buffer
));
690 built_argv
[x
++]= strdup(buffer
);
692 else if (_use_ptrcheck
)
695 valgrind --error-exitcode=1 --tool=exp-ptrcheck --log-file=
697 built_argv
[x
++]= strdup("valgrind");
698 built_argv
[x
++]= strdup("--error-exitcode=1");
699 built_argv
[x
++]= strdup("--tool=exp-ptrcheck");
701 std::string log_file
= create_tmpfile("ptrcheck");
703 int length
= snprintf(buffer
, sizeof(buffer
), "--log-file=%s", log_file
.c_str());
704 fatal_assert(length
> 0 and length
< sizeof(buffer
));
705 built_argv
[x
++]= strdup(buffer
);
709 built_argv
[x
++]= strdup("gdb");
712 built_argv
[x
++]= strdup(_exectuble_with_path
.c_str());
714 for (Options::const_iterator iter
= _options
.begin(); iter
!= _options
.end(); iter
++)
716 built_argv
[x
++]= strdup((*iter
).first
.c_str());
717 if ((*iter
).second
.empty() == false)
719 built_argv
[x
++]= strdup((*iter
).second
.c_str());
725 for (const char **ptr
= args
; *ptr
; ++ptr
)
727 built_argv
[x
++]= strdup(*ptr
);
730 built_argv
[x
++]= NULL
;
731 fatal_assert(x
== _argc
);
734 std::string
Application::print()
736 return print_argv(built_argv
, _argc
);
739 std::string
Application::arguments()
741 std::stringstream arg_buffer
;
743 for (size_t x
= 1 + _use_libtool
? 2 : 0;
744 x
< _argc
and built_argv
[x
];
747 arg_buffer
<< built_argv
[x
] << " ";
750 return arg_buffer
.str();
753 void Application::delete_argv()
757 for (size_t x
= 0; x
< _argc
; x
++)
761 ::free(built_argv
[x
]);
771 int exec_cmdline(const std::string
& command
, const char *args
[], bool use_libtool
)
773 Application
app(command
, use_libtool
);
775 Application::error_t ret
= app
.run(args
);
777 if (ret
!= Application::SUCCESS
)
782 return int(app
.wait(false));
785 const char *gearmand_binary()
787 return GEARMAND_BINARY
;
790 } // namespace exec_cmdline