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
22 #include <libtest/common.h>
24 using namespace libtest
;
35 #include <sys/types.h>
38 static int exited_successfully(int status
)
45 if (WIFEXITED(status
) == true)
47 return WEXITSTATUS(status
);
49 else if (WIFSIGNALED(status
) == true)
51 return WTERMSIG(status
);
60 std::string
print_argv(char * * & built_argv
, const size_t& argc
)
62 std::stringstream arg_buffer
;
64 for (size_t x
= 0; x
< argc
; x
++)
66 arg_buffer
<< built_argv
[x
] << " ";
69 return arg_buffer
.str();
72 std::string
print_argv(char** argv
)
74 std::stringstream arg_buffer
;
76 for (char** ptr
= argv
; *ptr
; ptr
++)
78 arg_buffer
<< *ptr
<< " ";
81 return arg_buffer
.str();
84 static Application::error_t
int_to_error_t(int arg
)
89 return Application::INVALID
;
92 return Application::SUCCESS
;
96 return Application::FAILURE
;
103 Application::Application(const std::string
& arg
, const bool _use_libtool_arg
) :
104 _use_libtool(_use_libtool_arg
),
105 _use_valgrind(false),
114 if (libtool() == NULL
)
116 throw "libtool requested, but know libtool was found";
120 // Find just the name of the application with no path
122 size_t found
= arg
.find_last_of("/\\");
125 _exectuble_name
= arg
.substr(found
+1);
129 _exectuble_name
= arg
;
133 if (_use_libtool
and getenv("PWD"))
135 _exectuble_with_path
+= getenv("PWD");
136 _exectuble_with_path
+= "/";
138 _exectuble_with_path
+= _exectuble
;
141 Application::~Application()
146 Application::error_t
Application::run(const char *args
[])
151 _stdout_buffer
.clear();
152 _stderr_buffer
.clear();
154 posix_spawn_file_actions_t file_actions
;
155 posix_spawn_file_actions_init(&file_actions
);
157 stdin_fd
.dup_for_spawn(Application::Pipe::READ
, file_actions
, STDIN_FILENO
);
158 stdout_fd
.dup_for_spawn(Application::Pipe::WRITE
, file_actions
, STDOUT_FILENO
);
159 stderr_fd
.dup_for_spawn(Application::Pipe::WRITE
, file_actions
, STDERR_FILENO
);
166 std::string gdb_run_file
= create_tmpfile(_exectuble_name
);
167 std::fstream file_stream
;
168 file_stream
.open(gdb_run_file
.c_str(), std::fstream::out
| std::fstream::trunc
);
170 _gdb_filename
= create_tmpfile(_exectuble_name
);
172 << "set logging redirect on" << std::endl
173 << "set logging file " << _gdb_filename
<< std::endl
174 << "set logging overwrite on" << std::endl
175 << "set logging on" << std::endl
176 << "set environment LIBTEST_IN_GDB=1" << std::endl
177 << "run " << arguments() << std::endl
178 << "thread apply all bt" << std::endl
179 << "quit" << std::endl
;
181 fatal_assert(file_stream
.good());
186 // libtool --mode=execute gdb -f -x binary
188 const_cast<char *>(libtool()),
189 const_cast<char *>("--mode=execute"),
190 const_cast<char *>("gdb"),
191 const_cast<char *>("-batch"),
192 const_cast<char *>("-f"),
193 const_cast<char *>("-x"),
194 const_cast<char *>(gdb_run_file
.c_str()),
195 const_cast<char *>(_exectuble_with_path
.c_str()),
198 spawn_ret
= posix_spawnp(&_pid
, libtool(), &file_actions
, NULL
, argv
, NULL
);
204 const_cast<char *>("gdb"),
205 const_cast<char *>("-batch"),
206 const_cast<char *>("-f"),
207 const_cast<char *>("-x"),
208 const_cast<char *>(gdb_run_file
.c_str()),
209 const_cast<char *>(_exectuble_with_path
.c_str()),
211 spawn_ret
= posix_spawnp(&_pid
, "gdb", &file_actions
, NULL
, argv
, NULL
);
218 spawn_ret
= posix_spawn(&_pid
, built_argv
[0], &file_actions
, NULL
, built_argv
, NULL
);
222 spawn_ret
= posix_spawnp(&_pid
, built_argv
[0], &file_actions
, NULL
, built_argv
, NULL
);
226 posix_spawn_file_actions_destroy(&file_actions
);
228 stdin_fd
.close(Application::Pipe::READ
);
229 stdout_fd
.close(Application::Pipe::WRITE
);
230 stderr_fd
.close(Application::Pipe::WRITE
);
234 return Application::INVALID
;
237 return Application::SUCCESS
;
240 Application::error_t
Application::wait()
244 Error
<< "wait() got an invalid pid_t";
245 return Application::INVALID
;
250 char buffer
[1024]= { 0 };
252 while (((read_length
= ::read(stdout_fd
.fd()[0], buffer
, sizeof(buffer
))) != 0) or bail
)
254 if (read_length
== -1)
262 Error
<< strerror(errno
);
266 _stdout_buffer
.reserve(read_length
+1);
267 for (size_t x
= 0; x
< read_length
; x
++)
269 _stdout_buffer
.push_back(buffer
[x
]);
271 // @todo Suck up all output code here
277 char buffer
[1024]= { 0 };
279 while (((read_length
= ::read(stderr_fd
.fd()[0], buffer
, sizeof(buffer
))) != 0) or bail
)
281 if (read_length
== -1)
289 Error
<< strerror(errno
);
293 _stderr_buffer
.reserve(read_length
+1);
294 for (size_t x
= 0; x
< read_length
; x
++)
296 _stderr_buffer
.push_back(buffer
[x
]);
298 // @todo Suck up all errput code here
302 error_t exit_code
= FAILURE
;
306 if ((waited_pid
= waitpid(_pid
, &status
, 0)) == -1)
308 Error
<< "Error occured while waitpid(" << strerror(errno
) << ") on pid " << int(_pid
);
312 if (waited_pid
!= _pid
)
314 throw libtest::fatal(LIBYATL_DEFAULT_PARAM
, "Pid mismatch, %d != %d", int(waited_pid
), int(_pid
));
316 exit_code
= int_to_error_t(exited_successfully(status
));
321 if (exit_code
== Application::INVALID
)
323 Error
<< print_argv(built_argv
, _argc
);
330 void Application::add_option(const std::string
& arg
)
332 _options
.push_back(std::make_pair(arg
, std::string()));
335 void Application::add_option(const std::string
& name
, const std::string
& value
)
337 _options
.push_back(std::make_pair(name
, value
));
340 Application::Pipe::Pipe()
348 void Application::Pipe::reset()
354 if ((ret
= pipe(_fd
)) < 0)
362 ret
= fcntl(_fd
[0], F_GETFL
, 0);
365 Error
<< "fcntl(F_GETFL) " << strerror(ret
);
369 ret
= fcntl(_fd
[0], F_SETFL
, ret
| O_NONBLOCK
);
372 Error
<< "fcntl(F_SETFL) " << strerror(ret
);
378 Application::Pipe::~Pipe()
384 void Application::Pipe::dup_for_spawn(const close_t
& arg
, posix_spawn_file_actions_t
& file_actions
, const int newfildes
)
389 if ((ret
= posix_spawn_file_actions_adddup2(&file_actions
, _fd
[type
], newfildes
)) < 0)
391 Error
<< "posix_spawn_file_actions_adddup2(" << strerror(ret
) << ")";
395 if ((ret
= posix_spawn_file_actions_addclose(&file_actions
, _fd
[type
])) < 0)
397 Error
<< "posix_spawn_file_actions_adddup2(" << strerror(ret
) << ")";
402 void Application::Pipe::close(const close_t
& arg
)
409 if ((ret
= ::close(_fd
[type
])) < 0)
411 Error
<< "close(" << strerror(ret
) << ")";
417 void Application::create_argv(const char *args
[])
419 _argc
= 2 +_use_libtool
? 2 : 0; // +1 for the command, +2 for libtool/mode=execute, +1 for the NULL
423 _argc
+= 2; // +2 for libtool --mode=execute
427 valgrind --error-exitcode=1 --leak-check=yes --show-reachable=yes --track-fds=yes --malloc-fill=A5 --free-fill=DE
433 else if (_use_gdb
) // gdb
438 for (Options::const_iterator iter
= _options
.begin(); iter
!= _options
.end(); iter
++)
441 if ((*iter
).second
.empty() == false)
449 for (const char **ptr
= args
; *ptr
; ++ptr
)
456 built_argv
= new char * [_argc
];
462 built_argv
[x
++]= strdup(libtool());
463 built_argv
[x
++]= strdup("--mode=execute");
469 valgrind --error-exitcode=1 --leak-check=yes --show-reachable=yes --track-fds=yes --malloc-fill=A5 --free-fill=DE
471 built_argv
[x
++]= strdup("valgrind");
472 built_argv
[x
++]= strdup("--error-exitcode=1");
473 built_argv
[x
++]= strdup("--leak-check=yes");
474 built_argv
[x
++]= strdup("--show-reachable=yes");
475 built_argv
[x
++]= strdup("--track-fds=yes");
476 built_argv
[x
++]= strdup("--malloc-fill=A5");
477 built_argv
[x
++]= strdup("--free-fill=DE");
481 built_argv
[x
++]= strdup("gdb");
484 built_argv
[x
++]= strdup(_exectuble_with_path
.c_str());
486 for (Options::const_iterator iter
= _options
.begin(); iter
!= _options
.end(); iter
++)
488 built_argv
[x
++]= strdup((*iter
).first
.c_str());
489 if ((*iter
).second
.empty() == false)
491 built_argv
[x
++]= strdup((*iter
).second
.c_str());
497 for (const char **ptr
= args
; *ptr
; ++ptr
)
499 built_argv
[x
++]= strdup(*ptr
);
502 built_argv
[_argc
-1]= NULL
;
505 std::string
Application::print()
507 return print_argv(built_argv
, _argc
);
510 std::string
Application::arguments()
512 std::stringstream arg_buffer
;
514 for (size_t x
= 1 + _use_libtool
? 2 : 0;
515 x
< _argc
and built_argv
[x
];
518 arg_buffer
<< built_argv
[x
] << " ";
521 return arg_buffer
.str();
524 void Application::delete_argv()
526 if (built_argv
== NULL
)
531 for (size_t x
= 0; x
< _argc
; x
++)
535 ::free(built_argv
[x
]);
544 int exec_cmdline(const std::string
& command
, const char *args
[], bool use_libtool
)
546 Application
app(command
, use_libtool
);
548 Application::error_t ret
= app
.run(args
);
550 if (ret
!= Application::SUCCESS
)
555 return int(app
.wait());
558 const char *gearmand_binary()
560 return GEARMAND_BINARY
;
563 } // namespace exec_cmdline