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 fatal_message("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()
150 Application::error_t
Application::run(const char *args
[])
155 _stdout_buffer
.clear();
156 _stderr_buffer
.clear();
158 posix_spawn_file_actions_t file_actions
;
159 posix_spawn_file_actions_init(&file_actions
);
161 stdin_fd
.dup_for_spawn(Application::Pipe::READ
, file_actions
, STDIN_FILENO
);
162 stdout_fd
.dup_for_spawn(Application::Pipe::WRITE
, file_actions
, STDOUT_FILENO
);
163 stderr_fd
.dup_for_spawn(Application::Pipe::WRITE
, file_actions
, STDERR_FILENO
);
170 std::string gdb_run_file
= create_tmpfile(_exectuble_name
);
171 std::fstream file_stream
;
172 file_stream
.open(gdb_run_file
.c_str(), std::fstream::out
| std::fstream::trunc
);
174 _gdb_filename
= create_tmpfile(_exectuble_name
);
176 << "set logging redirect on" << std::endl
177 << "set logging file " << _gdb_filename
<< std::endl
178 << "set logging overwrite on" << std::endl
179 << "set logging on" << std::endl
180 << "set environment LIBTEST_IN_GDB=1" << std::endl
181 << "run " << arguments() << std::endl
182 << "thread apply all bt" << std::endl
183 << "quit" << std::endl
;
185 fatal_assert(file_stream
.good());
190 // libtool --mode=execute gdb -f -x binary
192 const_cast<char *>(libtool()),
193 const_cast<char *>("--mode=execute"),
194 const_cast<char *>("gdb"),
195 const_cast<char *>("-batch"),
196 const_cast<char *>("-f"),
197 const_cast<char *>("-x"),
198 const_cast<char *>(gdb_run_file
.c_str()),
199 const_cast<char *>(_exectuble_with_path
.c_str()),
202 spawn_ret
= posix_spawnp(&_pid
, libtool(), &file_actions
, NULL
, argv
, NULL
);
208 const_cast<char *>("gdb"),
209 const_cast<char *>("-batch"),
210 const_cast<char *>("-f"),
211 const_cast<char *>("-x"),
212 const_cast<char *>(gdb_run_file
.c_str()),
213 const_cast<char *>(_exectuble_with_path
.c_str()),
215 spawn_ret
= posix_spawnp(&_pid
, "gdb", &file_actions
, NULL
, argv
, NULL
);
222 spawn_ret
= posix_spawn(&_pid
, built_argv
[0], &file_actions
, NULL
, built_argv
, NULL
);
226 spawn_ret
= posix_spawnp(&_pid
, built_argv
[0], &file_actions
, NULL
, built_argv
, NULL
);
230 posix_spawn_file_actions_destroy(&file_actions
);
232 stdin_fd
.close(Application::Pipe::READ
);
233 stdout_fd
.close(Application::Pipe::WRITE
);
234 stderr_fd
.close(Application::Pipe::WRITE
);
239 return Application::INVALID
;
242 return Application::SUCCESS
;
245 bool Application::check() const
247 Error
<< "Testing " << _exectuble
;
248 if (kill(_pid
, 0) == 0)
256 void Application::murder()
262 // false means that no data was returned
263 bool Application::slurp()
265 struct pollfd fds
[2];
266 fds
[0].fd
= stdout_fd
.fd()[0];
267 fds
[0].events
= POLLIN
;
269 fds
[1].fd
= stderr_fd
.fd()[0];
270 fds
[1].events
= POLLIN
;
274 if ((active_fd
= poll(fds
, 2, 400)) == -1)
277 switch ((error
= errno
))
279 #ifdef TARGET_OS_LINUX
287 fatal_message(strerror(error
));
291 fatal_message("RLIMIT_NOFILE exceeded, or if OSX the timeout value was invalid");
295 fatal_message(strerror(error
));
305 bool data_was_read
= false;
306 if (fds
[0].revents
& POLLIN
)
309 char buffer
[1024]= { 0 };
310 while ((read_length
= ::read(stdout_fd
.fd()[0], buffer
, sizeof(buffer
))))
312 if (read_length
== -1)
320 Error
<< strerror(errno
);
328 _stdout_buffer
.reserve(read_length
+1);
329 for (size_t x
= 0; x
< read_length
; x
++)
331 _stdout_buffer
.push_back(buffer
[x
]);
333 // @todo Suck up all output code here
337 if (fds
[1].revents
& POLLIN
)
339 stderr_fd
.nonblock();
342 char buffer
[1024]= { 0 };
343 while ((read_length
= ::read(stderr_fd
.fd()[0], buffer
, sizeof(buffer
))))
345 if (read_length
== -1)
353 Error
<< strerror(errno
);
361 _stderr_buffer
.reserve(read_length
+1);
362 for (size_t x
= 0; x
< read_length
; x
++)
364 _stderr_buffer
.push_back(buffer
[x
]);
366 // @todo Suck up all errput code here
370 return data_was_read
;
373 Application::error_t
Application::wait(bool nohang
)
377 return Application::INVALID
;
382 error_t exit_code
= FAILURE
;
386 if ((waited_pid
= waitpid(_pid
, &status
, nohang
? WNOHANG
: 0)) == -1)
391 exit_code
= Application::SUCCESS
;
395 Error
<< "Error occured while waitpid(" << strerror(errno
) << ") on pid " << int(_pid
);
398 else if (waited_pid
== 0)
400 exit_code
= Application::SUCCESS
;
404 if (waited_pid
!= _pid
)
406 throw libtest::fatal(LIBYATL_DEFAULT_PARAM
, "Pid mismatch, %d != %d", int(waited_pid
), int(_pid
));
408 exit_code
= int_to_error_t(exited_successfully(status
));
413 if (exit_code
== Application::INVALID
)
415 Error
<< print_argv(built_argv
, _argc
);
422 void Application::add_long_option(const std::string
& name
, const std::string
& option_value
)
424 std::string
arg(name
);
426 _options
.push_back(std::make_pair(arg
, std::string()));
429 void Application::add_option(const std::string
& arg
)
431 _options
.push_back(std::make_pair(arg
, std::string()));
434 void Application::add_option(const std::string
& name
, const std::string
& value
)
436 _options
.push_back(std::make_pair(name
, value
));
439 Application::Pipe::Pipe()
447 void Application::Pipe::nonblock()
450 if ((ret
= fcntl(_fd
[0], F_GETFL
, 0)) == -1)
452 Error
<< "fcntl(F_GETFL) " << strerror(errno
);
453 throw strerror(errno
);
456 if ((ret
= fcntl(_fd
[0], F_SETFL
, ret
| O_NONBLOCK
)) == -1)
458 Error
<< "fcntl(F_SETFL) " << strerror(errno
);
459 throw strerror(errno
);
463 void Application::Pipe::reset()
470 throw fatal_message(strerror(errno
));
481 Application::Pipe::~Pipe()
487 void Application::Pipe::dup_for_spawn(const close_t
& arg
, posix_spawn_file_actions_t
& file_actions
, const int newfildes
)
492 if ((ret
= posix_spawn_file_actions_adddup2(&file_actions
, _fd
[type
], newfildes
)) < 0)
494 Error
<< "posix_spawn_file_actions_adddup2(" << strerror(ret
) << ")";
495 throw fatal_message(strerror(ret
));
498 if ((ret
= posix_spawn_file_actions_addclose(&file_actions
, _fd
[type
])) < 0)
500 Error
<< "posix_spawn_file_actions_adddup2(" << strerror(ret
) << ")";
501 throw fatal_message(strerror(ret
));
505 void Application::Pipe::close(const close_t
& arg
)
512 if (::close(_fd
[type
]) == -1)
514 Error
<< "close(" << strerror(errno
) << ")";
521 void Application::create_argv(const char *args
[])
523 _argc
= 2 +_use_libtool
? 2 : 0; // +1 for the command, +2 for libtool/mode=execute, +1 for the NULL
527 _argc
+= 2; // +2 for libtool --mode=execute
531 valgrind --error-exitcode=1 --leak-check=yes --show-reachable=yes --track-fds=yes --malloc-fill=A5 --free-fill=DE
537 else if (_use_gdb
) // gdb
542 for (Options::const_iterator iter
= _options
.begin(); iter
!= _options
.end(); iter
++)
545 if ((*iter
).second
.empty() == false)
553 for (const char **ptr
= args
; *ptr
; ++ptr
)
560 built_argv
= new char * [_argc
];
566 built_argv
[x
++]= strdup(libtool());
567 built_argv
[x
++]= strdup("--mode=execute");
573 valgrind --error-exitcode=1 --leak-check=yes --show-reachable=yes --track-fds=yes --malloc-fill=A5 --free-fill=DE
575 built_argv
[x
++]= strdup("valgrind");
576 built_argv
[x
++]= strdup("--error-exitcode=1");
577 built_argv
[x
++]= strdup("--leak-check=yes");
578 built_argv
[x
++]= strdup("--show-reachable=yes");
579 built_argv
[x
++]= strdup("--track-fds=yes");
580 built_argv
[x
++]= strdup("--malloc-fill=A5");
581 built_argv
[x
++]= strdup("--free-fill=DE");
585 built_argv
[x
++]= strdup("gdb");
588 built_argv
[x
++]= strdup(_exectuble_with_path
.c_str());
590 for (Options::const_iterator iter
= _options
.begin(); iter
!= _options
.end(); iter
++)
592 built_argv
[x
++]= strdup((*iter
).first
.c_str());
593 if ((*iter
).second
.empty() == false)
595 built_argv
[x
++]= strdup((*iter
).second
.c_str());
601 for (const char **ptr
= args
; *ptr
; ++ptr
)
603 built_argv
[x
++]= strdup(*ptr
);
606 built_argv
[_argc
-1]= NULL
;
609 std::string
Application::print()
611 return print_argv(built_argv
, _argc
);
614 std::string
Application::arguments()
616 std::stringstream arg_buffer
;
618 for (size_t x
= 1 + _use_libtool
? 2 : 0;
619 x
< _argc
and built_argv
[x
];
622 arg_buffer
<< built_argv
[x
] << " ";
625 return arg_buffer
.str();
628 void Application::delete_argv()
632 for (size_t x
= 0; x
< _argc
; x
++)
636 ::free(built_argv
[x
]);
646 int exec_cmdline(const std::string
& command
, const char *args
[], bool use_libtool
)
648 Application
app(command
, use_libtool
);
650 Application::error_t ret
= app
.run(args
);
652 if (ret
!= Application::SUCCESS
)
657 return int(app
.wait(false));
660 const char *gearmand_binary()
662 return GEARMAND_BINARY
;
665 } // namespace exec_cmdline