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),
117 if (libtool() == NULL
)
119 throw "libtool requested, but know libtool was found";
123 // Find just the name of the application with no path
125 size_t found
= arg
.find_last_of("/\\");
128 _exectuble_name
= arg
.substr(found
+1);
132 _exectuble_name
= arg
;
136 if (_use_libtool
and getenv("PWD"))
138 _exectuble_with_path
+= getenv("PWD");
139 _exectuble_with_path
+= "/";
141 _exectuble_with_path
+= _exectuble
;
144 Application::~Application()
149 Application::error_t
Application::run(const char *args
[])
154 _stdout_buffer
.clear();
155 _stderr_buffer
.clear();
157 posix_spawn_file_actions_t file_actions
;
158 posix_spawn_file_actions_init(&file_actions
);
160 stdin_fd
.dup_for_spawn(Application::Pipe::READ
, file_actions
, STDIN_FILENO
);
161 stdout_fd
.dup_for_spawn(Application::Pipe::WRITE
, file_actions
, STDOUT_FILENO
);
162 stderr_fd
.dup_for_spawn(Application::Pipe::WRITE
, file_actions
, STDERR_FILENO
);
169 std::string gdb_run_file
= create_tmpfile(_exectuble_name
);
170 std::fstream file_stream
;
171 file_stream
.open(gdb_run_file
.c_str(), std::fstream::out
| std::fstream::trunc
);
173 _gdb_filename
= create_tmpfile(_exectuble_name
);
175 << "set logging redirect on" << std::endl
176 << "set logging file " << _gdb_filename
<< std::endl
177 << "set logging overwrite on" << std::endl
178 << "set logging on" << std::endl
179 << "set environment LIBTEST_IN_GDB=1" << std::endl
180 << "run " << arguments() << std::endl
181 << "thread apply all bt" << std::endl
182 << "quit" << std::endl
;
184 fatal_assert(file_stream
.good());
189 // libtool --mode=execute gdb -f -x binary
191 const_cast<char *>(libtool()),
192 const_cast<char *>("--mode=execute"),
193 const_cast<char *>("gdb"),
194 const_cast<char *>("-batch"),
195 const_cast<char *>("-f"),
196 const_cast<char *>("-x"),
197 const_cast<char *>(gdb_run_file
.c_str()),
198 const_cast<char *>(_exectuble_with_path
.c_str()),
201 spawn_ret
= posix_spawnp(&_pid
, libtool(), &file_actions
, NULL
, argv
, NULL
);
207 const_cast<char *>("gdb"),
208 const_cast<char *>("-batch"),
209 const_cast<char *>("-f"),
210 const_cast<char *>("-x"),
211 const_cast<char *>(gdb_run_file
.c_str()),
212 const_cast<char *>(_exectuble_with_path
.c_str()),
214 spawn_ret
= posix_spawnp(&_pid
, "gdb", &file_actions
, NULL
, argv
, NULL
);
221 spawn_ret
= posix_spawn(&_pid
, built_argv
[0], &file_actions
, NULL
, built_argv
, NULL
);
225 spawn_ret
= posix_spawnp(&_pid
, built_argv
[0], &file_actions
, NULL
, built_argv
, NULL
);
229 posix_spawn_file_actions_destroy(&file_actions
);
231 stdin_fd
.close(Application::Pipe::READ
);
232 stdout_fd
.close(Application::Pipe::WRITE
);
233 stderr_fd
.close(Application::Pipe::WRITE
);
237 return Application::INVALID
;
240 return Application::SUCCESS
;
243 bool Application::check() const
245 Error
<< "Testing " << _exectuble
;
246 if (kill(_pid
, 0) == 0)
254 void Application::murder()
260 // false means that no data was returned
261 bool Application::slurp()
263 struct pollfd fds
[2];
264 fds
[0].fd
= stdout_fd
.fd()[0];
265 fds
[0].events
= POLLIN
;
267 fds
[1].fd
= stderr_fd
.fd()[0];
268 fds
[1].events
= POLLIN
;
272 if ((active_fd
= poll(fds
, 2, 400)) == -1)
275 switch ((error
= errno
))
277 #ifdef TARGET_OS_LINUX
285 fatal_message(strerror(error
));
289 fatal_message("RLIMIT_NOFILE exceeded, or if OSX the timeout value was invalid");
293 fatal_message(strerror(error
));
303 if (fds
[0].revents
== POLLIN
)
306 char buffer
[1024]= { 0 };
307 while ((read_length
= ::read(stdout_fd
.fd()[0], buffer
, sizeof(buffer
))))
309 if (read_length
== -1)
317 Error
<< strerror(errno
);
323 _stdout_buffer
.reserve(read_length
+1);
324 for (size_t x
= 0; x
< read_length
; x
++)
326 _stdout_buffer
.push_back(buffer
[x
]);
328 // @todo Suck up all output code here
332 if (fds
[1].revents
== POLLIN
)
335 char buffer
[1024]= { 0 };
336 while ((read_length
= ::read(stderr_fd
.fd()[0], buffer
, sizeof(buffer
))))
338 if (read_length
== -1)
346 Error
<< strerror(errno
);
352 _stderr_buffer
.reserve(read_length
+1);
353 for (size_t x
= 0; x
< read_length
; x
++)
355 _stderr_buffer
.push_back(buffer
[x
]);
357 // @todo Suck up all errput code here
364 Application::error_t
Application::wait()
366 fatal_assert(_pid
!= -1);
370 error_t exit_code
= FAILURE
;
374 if ((waited_pid
= waitpid(_pid
, &status
, 0)) == -1)
379 exit_code
= Application::SUCCESS
;
383 Error
<< "Error occured while waitpid(" << strerror(errno
) << ") on pid " << int(_pid
);
388 if (waited_pid
!= _pid
)
390 throw libtest::fatal(LIBYATL_DEFAULT_PARAM
, "Pid mismatch, %d != %d", int(waited_pid
), int(_pid
));
392 exit_code
= int_to_error_t(exited_successfully(status
));
397 if (exit_code
== Application::INVALID
)
399 Error
<< print_argv(built_argv
, _argc
);
406 void Application::add_long_option(const std::string
& name
, const std::string
& option_value
)
408 std::string
arg(name
);
410 _options
.push_back(std::make_pair(arg
, std::string()));
413 void Application::add_option(const std::string
& arg
)
415 _options
.push_back(std::make_pair(arg
, std::string()));
418 void Application::add_option(const std::string
& name
, const std::string
& value
)
420 _options
.push_back(std::make_pair(name
, value
));
423 Application::Pipe::Pipe()
431 void Application::Pipe::nonblock()
434 if ((ret
= fcntl(_fd
[0], F_GETFL
, 0)) == -1)
436 Error
<< "fcntl(F_GETFL) " << strerror(errno
);
437 throw strerror(errno
);
440 if ((ret
= fcntl(_fd
[0], F_SETFL
, ret
| O_NONBLOCK
)) == -1)
442 Error
<< "fcntl(F_SETFL) " << strerror(errno
);
443 throw strerror(errno
);
447 void Application::Pipe::reset()
454 throw strerror(errno
);
465 Application::Pipe::~Pipe()
471 void Application::Pipe::dup_for_spawn(const close_t
& arg
, posix_spawn_file_actions_t
& file_actions
, const int newfildes
)
476 if ((ret
= posix_spawn_file_actions_adddup2(&file_actions
, _fd
[type
], newfildes
)) < 0)
478 Error
<< "posix_spawn_file_actions_adddup2(" << strerror(ret
) << ")";
482 if ((ret
= posix_spawn_file_actions_addclose(&file_actions
, _fd
[type
])) < 0)
484 Error
<< "posix_spawn_file_actions_adddup2(" << strerror(ret
) << ")";
489 void Application::Pipe::close(const close_t
& arg
)
496 if (::close(_fd
[type
]) == -1)
498 Error
<< "close(" << strerror(errno
) << ")";
505 void Application::create_argv(const char *args
[])
507 _argc
= 2 +_use_libtool
? 2 : 0; // +1 for the command, +2 for libtool/mode=execute, +1 for the NULL
511 _argc
+= 2; // +2 for libtool --mode=execute
515 valgrind --error-exitcode=1 --leak-check=yes --show-reachable=yes --track-fds=yes --malloc-fill=A5 --free-fill=DE
521 else if (_use_gdb
) // gdb
526 for (Options::const_iterator iter
= _options
.begin(); iter
!= _options
.end(); iter
++)
529 if ((*iter
).second
.empty() == false)
537 for (const char **ptr
= args
; *ptr
; ++ptr
)
544 built_argv
= new char * [_argc
];
550 built_argv
[x
++]= strdup(libtool());
551 built_argv
[x
++]= strdup("--mode=execute");
557 valgrind --error-exitcode=1 --leak-check=yes --show-reachable=yes --track-fds=yes --malloc-fill=A5 --free-fill=DE
559 built_argv
[x
++]= strdup("valgrind");
560 built_argv
[x
++]= strdup("--error-exitcode=1");
561 built_argv
[x
++]= strdup("--leak-check=yes");
562 built_argv
[x
++]= strdup("--show-reachable=yes");
563 built_argv
[x
++]= strdup("--track-fds=yes");
564 built_argv
[x
++]= strdup("--malloc-fill=A5");
565 built_argv
[x
++]= strdup("--free-fill=DE");
569 built_argv
[x
++]= strdup("gdb");
572 built_argv
[x
++]= strdup(_exectuble_with_path
.c_str());
574 for (Options::const_iterator iter
= _options
.begin(); iter
!= _options
.end(); iter
++)
576 built_argv
[x
++]= strdup((*iter
).first
.c_str());
577 if ((*iter
).second
.empty() == false)
579 built_argv
[x
++]= strdup((*iter
).second
.c_str());
585 for (const char **ptr
= args
; *ptr
; ++ptr
)
587 built_argv
[x
++]= strdup(*ptr
);
590 built_argv
[_argc
-1]= NULL
;
593 std::string
Application::print()
595 return print_argv(built_argv
, _argc
);
598 std::string
Application::arguments()
600 std::stringstream arg_buffer
;
602 for (size_t x
= 1 + _use_libtool
? 2 : 0;
603 x
< _argc
and built_argv
[x
];
606 arg_buffer
<< built_argv
[x
] << " ";
609 return arg_buffer
.str();
612 void Application::delete_argv()
616 for (size_t x
= 0; x
< _argc
; x
++)
620 ::free(built_argv
[x
]);
630 int exec_cmdline(const std::string
& command
, const char *args
[], bool use_libtool
)
632 Application
app(command
, use_libtool
);
634 Application::error_t ret
= app
.run(args
);
636 if (ret
!= Application::SUCCESS
)
641 return int(app
.wait());
644 const char *gearmand_binary()
646 return GEARMAND_BINARY
;
649 } // namespace exec_cmdline