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),
112 stdin_fd(STDIN_FILENO
),
113 stdout_fd(STDOUT_FILENO
),
114 stderr_fd(STDERR_FILENO
),
120 if (libtool() == NULL
)
122 fatal_message("libtool requested, but know libtool was found");
126 // Find just the name of the application with no path
128 size_t found
= arg
.find_last_of("/\\");
131 _exectuble_name
= arg
.substr(found
+1);
135 _exectuble_name
= arg
;
139 if (_use_libtool
and getenv("PWD"))
141 _exectuble_with_path
+= getenv("PWD");
142 _exectuble_with_path
+= "/";
144 _exectuble_with_path
+= _exectuble
;
147 Application::~Application()
153 Application::error_t
Application::run(const char *args
[])
158 _stdout_buffer
.clear();
159 _stderr_buffer
.clear();
161 posix_spawn_file_actions_t file_actions
;
162 posix_spawn_file_actions_init(&file_actions
);
164 stdin_fd
.dup_for_spawn(Application::Pipe::READ
, file_actions
);
165 stdout_fd
.dup_for_spawn(Application::Pipe::WRITE
, file_actions
);
166 stderr_fd
.dup_for_spawn(Application::Pipe::WRITE
, file_actions
);
168 posix_spawnattr_t spawnattr
;
169 posix_spawnattr_init(&spawnattr
);
173 fatal_assert(posix_spawnattr_setsigmask(&spawnattr
, &set
) == 0);
180 std::string gdb_run_file
= create_tmpfile(_exectuble_name
);
181 std::fstream file_stream
;
182 file_stream
.open(gdb_run_file
.c_str(), std::fstream::out
| std::fstream::trunc
);
184 _gdb_filename
= create_tmpfile(_exectuble_name
);
186 << "set logging redirect on" << std::endl
187 << "set logging file " << _gdb_filename
<< std::endl
188 << "set logging overwrite on" << std::endl
189 << "set logging on" << std::endl
190 << "set environment LIBTEST_IN_GDB=1" << std::endl
191 << "run " << arguments() << std::endl
192 << "thread apply all bt" << std::endl
193 << "quit" << std::endl
;
195 fatal_assert(file_stream
.good());
200 // libtool --mode=execute gdb -f -x binary
202 const_cast<char *>(libtool()),
203 const_cast<char *>("--mode=execute"),
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()),
212 spawn_ret
= posix_spawnp(&_pid
, libtool(), &file_actions
, &spawnattr
, argv
, NULL
);
218 const_cast<char *>("gdb"),
219 const_cast<char *>("-batch"),
220 const_cast<char *>("-f"),
221 const_cast<char *>("-x"),
222 const_cast<char *>(gdb_run_file
.c_str()),
223 const_cast<char *>(_exectuble_with_path
.c_str()),
225 spawn_ret
= posix_spawnp(&_pid
, "gdb", &file_actions
, &spawnattr
, argv
, NULL
);
232 spawn_ret
= posix_spawn(&_pid
, built_argv
[0], &file_actions
, &spawnattr
, built_argv
, NULL
);
236 spawn_ret
= posix_spawnp(&_pid
, built_argv
[0], &file_actions
, &spawnattr
, built_argv
, NULL
);
240 posix_spawn_file_actions_destroy(&file_actions
);
241 posix_spawnattr_destroy(&spawnattr
);
243 stdin_fd
.close(Application::Pipe::READ
);
244 stdout_fd
.close(Application::Pipe::WRITE
);
245 stderr_fd
.close(Application::Pipe::WRITE
);
249 Error
<< strerror(spawn_ret
) << "(" << spawn_ret
<< ")";
251 return Application::INVALID
;
254 return Application::SUCCESS
;
257 bool Application::check() const
259 if (_pid
> 1 and kill(_pid
, 0) == 0)
267 void Application::murder()
272 while ((count
--) > 0 and check())
274 int kill_ret
= kill(_pid
, SIGTERM
);
279 if ((waitpid_ret
= waitpid(_pid
, &status
, WNOHANG
)) == -1)
288 Error
<< "waitpid() failed after kill with error of " << strerror(errno
);
293 if (waitpid_ret
== 0)
295 libtest::dream(1, 0);
300 Error
<< "kill(pid, SIGTERM) failed after kill with error of " << strerror(errno
);
307 // If for whatever reason it lives, kill it hard
310 (void)kill(_pid
, SIGKILL
);
316 // false means that no data was returned
317 bool Application::slurp()
319 struct pollfd fds
[2];
320 fds
[0].fd
= stdout_fd
.fd();
321 fds
[0].events
= POLLRDNORM
;
323 fds
[1].fd
= stderr_fd
.fd();
324 fds
[1].events
= POLLRDNORM
;
328 if ((active_fd
= poll(fds
, 2, 0)) == -1)
331 switch ((error
= errno
))
333 #ifdef TARGET_OS_LINUX
341 fatal_message(strerror(error
));
345 fatal_message("RLIMIT_NOFILE exceeded, or if OSX the timeout value was invalid");
349 fatal_message(strerror(error
));
361 bool data_was_read
= false;
362 if (fds
[0].revents
& POLLRDNORM
)
364 if (stdout_fd
.read(_stdout_buffer
) == true)
370 if (fds
[1].revents
& POLLRDNORM
)
372 if (stderr_fd
.read(_stderr_buffer
) == true)
378 return data_was_read
;
381 Application::error_t
Application::wait(bool nohang
)
385 return Application::INVALID
;
390 error_t exit_code
= FAILURE
;
394 if ((waited_pid
= waitpid(_pid
, &status
, nohang
? WNOHANG
: 0)) == -1)
399 exit_code
= Application::SUCCESS
;
406 Error
<< "Error occured while waitpid(" << strerror(errno
) << ") on pid " << int(_pid
);
410 else if (waited_pid
== 0)
412 exit_code
= Application::SUCCESS
;
416 if (waited_pid
!= _pid
)
418 throw libtest::fatal(LIBYATL_DEFAULT_PARAM
, "Pid mismatch, %d != %d", int(waited_pid
), int(_pid
));
420 exit_code
= int_to_error_t(exited_successfully(status
));
427 if (exit_code
== Application::INVALID
)
429 Error
<< print_argv(built_argv
, _argc
);
436 void Application::add_long_option(const std::string
& name
, const std::string
& option_value
)
438 std::string
arg(name
);
440 _options
.push_back(std::make_pair(arg
, std::string()));
443 void Application::add_option(const std::string
& arg
)
445 _options
.push_back(std::make_pair(arg
, std::string()));
448 void Application::add_option(const std::string
& name
, const std::string
& value
)
450 _options
.push_back(std::make_pair(name
, value
));
453 Application::Pipe::Pipe(int arg
) :
462 int Application::Pipe::Pipe::fd()
464 if (_std_fd
== STDOUT_FILENO
)
466 return _pipe_fd
[READ
];
468 else if (_std_fd
== STDERR_FILENO
)
470 return _pipe_fd
[READ
];
473 return _pipe_fd
[WRITE
]; // STDIN_FILENO
477 bool Application::Pipe::read(libtest::vchar_t
& arg
)
479 fatal_assert(_std_fd
== STDOUT_FILENO
or _std_fd
== STDERR_FILENO
);
481 bool data_was_read
= false;
484 char buffer
[1024]= { 0 };
485 while ((read_length
= ::read(_pipe_fd
[READ
], buffer
, sizeof(buffer
))))
487 if (read_length
== -1)
495 Error
<< strerror(errno
);
503 arg
.reserve(read_length
+1);
504 for (size_t x
= 0; x
< read_length
; x
++)
506 arg
.push_back(buffer
[x
]);
508 // @todo Suck up all errput code here
511 return data_was_read
;
514 void Application::Pipe::nonblock()
517 if ((ret
= fcntl(_pipe_fd
[READ
], F_GETFL
, 0)) == -1)
519 Error
<< "fcntl(F_GETFL) " << strerror(errno
);
520 throw strerror(errno
);
523 if ((ret
= fcntl(_pipe_fd
[READ
], F_SETFL
, ret
| O_NONBLOCK
)) == -1)
525 Error
<< "fcntl(F_SETFL) " << strerror(errno
);
526 throw strerror(errno
);
530 void Application::Pipe::reset()
536 if (pipe2(_pipe_fd
, O_NONBLOCK
) == -1)
538 if (pipe(_pipe_fd
) == -1)
541 fatal_message(strerror(errno
));
553 void Application::Pipe::cloexec()
556 if ((ret
= fcntl(_pipe_fd
[WRITE
], F_GETFD
, 0)) == -1)
558 Error
<< "fcntl(F_GETFD) " << strerror(errno
);
559 throw strerror(errno
);
562 if ((ret
= fcntl(_pipe_fd
[WRITE
], F_SETFD
, ret
| FD_CLOEXEC
)) == -1)
564 Error
<< "fcntl(F_SETFD) " << strerror(errno
);
565 throw strerror(errno
);
569 Application::Pipe::~Pipe()
575 void Application::Pipe::dup_for_spawn(const close_t
& arg
, posix_spawn_file_actions_t
& file_actions
)
580 if ((ret
= posix_spawn_file_actions_adddup2(&file_actions
, _pipe_fd
[type
], _std_fd
)) < 0)
582 Error
<< "posix_spawn_file_actions_adddup2(" << strerror(ret
) << ")";
583 fatal_message(strerror(ret
));
586 if ((ret
= posix_spawn_file_actions_addclose(&file_actions
, _pipe_fd
[type
])) < 0)
588 Error
<< "posix_spawn_file_actions_adddup2(" << strerror(ret
) << ")";
589 fatal_message(strerror(ret
));
593 void Application::Pipe::close(const close_t
& arg
)
600 if (::close(_pipe_fd
[type
]) == -1)
602 Error
<< "close(" << strerror(errno
) << ")";
609 void Application::create_argv(const char *args
[])
612 fatal_assert(_argc
== 0);
616 _argc
+= 2; // +2 for libtool --mode=execute
619 _argc
+= 1; // For the command
622 valgrind --error-exitcode=1 --leak-check=yes --show-reachable=yes --track-fds=yes --malloc-fill=A5 --free-fill=DE
628 else if (_use_gdb
) // gdb
633 for (Options::const_iterator iter
= _options
.begin(); iter
!= _options
.end(); iter
++)
636 if ((*iter
).second
.empty() == false)
644 for (const char **ptr
= args
; *ptr
; ++ptr
)
650 _argc
+= 1; // for the NULL
652 built_argv
= new char * [_argc
];
658 built_argv
[x
++]= strdup(libtool());
659 built_argv
[x
++]= strdup("--mode=execute");
665 valgrind --error-exitcode=1 --leak-check=yes --show-reachable=yes --track-fds=yes --malloc-fill=A5 --free-fill=DE
667 built_argv
[x
++]= strdup("valgrind");
668 built_argv
[x
++]= strdup("--error-exitcode=1");
669 built_argv
[x
++]= strdup("--leak-check=yes");
670 built_argv
[x
++]= strdup("--show-reachable=yes");
671 built_argv
[x
++]= strdup("--track-fds=yes");
672 built_argv
[x
++]= strdup("--malloc-fill=A5");
673 built_argv
[x
++]= strdup("--free-fill=DE");
677 built_argv
[x
++]= strdup("gdb");
680 built_argv
[x
++]= strdup(_exectuble_with_path
.c_str());
682 for (Options::const_iterator iter
= _options
.begin(); iter
!= _options
.end(); iter
++)
684 built_argv
[x
++]= strdup((*iter
).first
.c_str());
685 if ((*iter
).second
.empty() == false)
687 built_argv
[x
++]= strdup((*iter
).second
.c_str());
693 for (const char **ptr
= args
; *ptr
; ++ptr
)
695 built_argv
[x
++]= strdup(*ptr
);
698 built_argv
[x
++]= NULL
;
699 fatal_assert(x
== _argc
);
702 std::string
Application::print()
704 return print_argv(built_argv
, _argc
);
707 std::string
Application::arguments()
709 std::stringstream arg_buffer
;
711 for (size_t x
= 1 + _use_libtool
? 2 : 0;
712 x
< _argc
and built_argv
[x
];
715 arg_buffer
<< built_argv
[x
] << " ";
718 return arg_buffer
.str();
721 void Application::delete_argv()
725 for (size_t x
= 0; x
< _argc
; x
++)
729 ::free(built_argv
[x
]);
739 int exec_cmdline(const std::string
& command
, const char *args
[], bool use_libtool
)
741 Application
app(command
, use_libtool
);
743 Application::error_t ret
= app
.run(args
);
745 if (ret
!= Application::SUCCESS
)
750 return int(app
.wait(false));
753 const char *gearmand_binary()
755 return GEARMAND_BINARY
;
758 } // namespace exec_cmdline