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
;
34 #include <sys/types.h>
37 static int exited_successfully(int status
)
44 if (WIFEXITED(status
) == true)
46 return WEXITSTATUS(status
);
48 else if (WIFSIGNALED(status
) == true)
50 return WTERMSIG(status
);
59 std::string
print_argv(char * * & built_argv
, const size_t& argc
, const pid_t
& pid
)
61 std::stringstream arg_buffer
;
63 for (size_t x
= 0; x
< argc
; x
++)
65 arg_buffer
<< built_argv
[x
] << " ";
68 return arg_buffer
.str();
75 Application::Application(const std::string
& arg
, const bool _use_libtool_arg
) :
76 _use_libtool(_use_libtool_arg
),
84 if (libtool() == NULL
)
86 throw "libtool requested, but know libtool was found";
90 if (_use_libtool
and getenv("PWD"))
92 _exectuble_with_path
+= getenv("PWD");
93 _exectuble_with_path
+= "/";
95 _exectuble_with_path
+= _exectuble
;
98 Application::~Application()
103 Application::error_t
Application::run(const char *args
[])
108 _stdout_buffer
.clear();
109 _stderr_buffer
.clear();
111 posix_spawn_file_actions_t file_actions
;
112 posix_spawn_file_actions_init(&file_actions
);
114 stdin_fd
.dup_for_spawn(Application::Pipe::READ
, file_actions
, STDIN_FILENO
);
115 stdout_fd
.dup_for_spawn(Application::Pipe::WRITE
, file_actions
, STDOUT_FILENO
);
116 stderr_fd
.dup_for_spawn(Application::Pipe::WRITE
, file_actions
, STDERR_FILENO
);
123 spawn_ret
= posix_spawn(&_pid
, built_argv
[0], &file_actions
, NULL
, built_argv
, NULL
);
127 spawn_ret
= posix_spawnp(&_pid
, built_argv
[0], &file_actions
, NULL
, built_argv
, NULL
);
130 posix_spawn_file_actions_destroy(&file_actions
);
132 stdin_fd
.close(Application::Pipe::READ
);
133 stdout_fd
.close(Application::Pipe::WRITE
);
134 stderr_fd
.close(Application::Pipe::WRITE
);
139 return Application::INVALID
;
142 return Application::SUCCESS
;
145 Application::error_t
Application::wait()
149 Error
<< "wait() got an invalid pid_t";
150 return Application::INVALID
;
155 char buffer
[1024]= { 0 };
157 while (((read_length
= ::read(stdout_fd
.fd()[0], buffer
, sizeof(buffer
))) != 0) or bail
)
159 if (read_length
== -1)
167 Error
<< strerror(errno
);
171 _stdout_buffer
.reserve(read_length
+1);
172 for (size_t x
= 0; x
< read_length
; x
++)
174 _stdout_buffer
.push_back(buffer
[x
]);
176 // @todo Suck up all output code here
182 char buffer
[1024]= { 0 };
184 while (((read_length
= ::read(stderr_fd
.fd()[0], buffer
, sizeof(buffer
))) != 0) or bail
)
186 if (read_length
== -1)
194 Error
<< strerror(errno
);
198 _stderr_buffer
.reserve(read_length
+1);
199 for (size_t x
= 0; x
< read_length
; x
++)
201 _stderr_buffer
.push_back(buffer
[x
]);
203 // @todo Suck up all errput code here
207 error_t exit_code
= FAILURE
;
211 if ((waited_pid
= waitpid(_pid
, &status
, 0)) == -1)
213 Error
<< "Error occured while waitpid(" << strerror(errno
) << ") on pid " << int(_pid
);
217 assert(waited_pid
== _pid
);
218 exit_code
= error_t(exited_successfully(status
));
222 if (exit_code
== Application::INVALID
)
225 Error
<< print_argv(built_argv
, _argc
, _pid
);
232 void Application::add_option(const std::string
& arg
)
234 _options
.push_back(std::make_pair(arg
, std::string()));
237 void Application::add_option(const std::string
& name
, const std::string
& value
)
239 _options
.push_back(std::make_pair(name
, value
));
242 Application::Pipe::Pipe()
250 void Application::Pipe::reset()
256 if ((ret
= pipe(_fd
)) < 0)
264 ret
= fcntl(_fd
[0], F_GETFL
, 0);
267 Error
<< "fcntl(F_GETFL) " << strerror(ret
);
271 ret
= fcntl(_fd
[0], F_SETFL
, ret
| O_NONBLOCK
);
274 Error
<< "fcntl(F_SETFL) " << strerror(ret
);
280 Application::Pipe::~Pipe()
286 void Application::Pipe::dup_for_spawn(const close_t
& arg
, posix_spawn_file_actions_t
& file_actions
, const int newfildes
)
291 if ((ret
= posix_spawn_file_actions_adddup2(&file_actions
, _fd
[type
], newfildes
)) < 0)
293 Error
<< "posix_spawn_file_actions_adddup2(" << strerror(ret
) << ")";
297 if ((ret
= posix_spawn_file_actions_addclose(&file_actions
, _fd
[type
])) < 0)
299 Error
<< "posix_spawn_file_actions_adddup2(" << strerror(ret
) << ")";
304 void Application::Pipe::close(const close_t
& arg
)
311 if ((ret
= ::close(_fd
[type
])) < 0)
313 Error
<< "close(" << strerror(ret
) << ")";
319 void Application::create_argv(const char *args
[])
321 _argc
= 2 +_use_libtool
? 2 : 0; // +1 for the command, +2 for libtool/mode=execute, +1 for the NULL
325 _argc
+= 2; // +2 for libtool --mode=execute
328 for (Options::const_iterator iter
= _options
.begin(); iter
!= _options
.end(); iter
++)
331 if ((*iter
).second
.empty() == false)
339 for (const char **ptr
= args
; *ptr
; ++ptr
)
346 built_argv
= new char * [_argc
];
352 built_argv
[x
++]= strdup(libtool());
353 built_argv
[x
++]= strdup("--mode=execute");
355 built_argv
[x
++]= strdup(_exectuble_with_path
.c_str());
357 for (Options::const_iterator iter
= _options
.begin(); iter
!= _options
.end(); iter
++)
359 built_argv
[x
++]= strdup((*iter
).first
.c_str());
360 if ((*iter
).second
.empty() == false)
362 built_argv
[x
++]= strdup((*iter
).second
.c_str());
368 for (const char **ptr
= args
; *ptr
; ++ptr
)
370 built_argv
[x
++]= strdup(*ptr
);
373 built_argv
[_argc
-1]= NULL
;
376 std::string
Application::print()
378 return print_argv(built_argv
, _argc
, _pid
);
381 void Application::delete_argv()
383 if (built_argv
== NULL
)
388 for (size_t x
= 0; x
< _argc
; x
++)
392 ::free(built_argv
[x
]);
401 int exec_cmdline(const std::string
& command
, const char *args
[], bool use_libtool
)
403 Application
app(command
, use_libtool
);
405 Application::error_t ret
= app
.run(args
);
407 if (ret
!= Application::SUCCESS
)
416 const char *gearmand_binary()
418 return GEARMAND_BINARY
;
421 } // namespace exec_cmdline