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();
71 static Application::error_t
int_to_error_t(int arg
)
76 return Application::INVALID
;
79 return Application::SUCCESS
;
83 return Application::FAILURE
;
90 Application::Application(const std::string
& arg
, const bool _use_libtool_arg
) :
91 _use_libtool(_use_libtool_arg
),
99 if (libtool() == NULL
)
101 throw "libtool requested, but know libtool was found";
105 if (_use_libtool
and getenv("PWD"))
107 _exectuble_with_path
+= getenv("PWD");
108 _exectuble_with_path
+= "/";
110 _exectuble_with_path
+= _exectuble
;
113 Application::~Application()
118 Application::error_t
Application::run(const char *args
[])
123 _stdout_buffer
.clear();
124 _stderr_buffer
.clear();
126 posix_spawn_file_actions_t file_actions
;
127 posix_spawn_file_actions_init(&file_actions
);
129 stdin_fd
.dup_for_spawn(Application::Pipe::READ
, file_actions
, STDIN_FILENO
);
130 stdout_fd
.dup_for_spawn(Application::Pipe::WRITE
, file_actions
, STDOUT_FILENO
);
131 stderr_fd
.dup_for_spawn(Application::Pipe::WRITE
, file_actions
, STDERR_FILENO
);
138 spawn_ret
= posix_spawn(&_pid
, built_argv
[0], &file_actions
, NULL
, built_argv
, NULL
);
142 spawn_ret
= posix_spawnp(&_pid
, built_argv
[0], &file_actions
, NULL
, built_argv
, NULL
);
145 posix_spawn_file_actions_destroy(&file_actions
);
147 stdin_fd
.close(Application::Pipe::READ
);
148 stdout_fd
.close(Application::Pipe::WRITE
);
149 stderr_fd
.close(Application::Pipe::WRITE
);
154 return Application::INVALID
;
157 return Application::SUCCESS
;
160 Application::error_t
Application::wait()
164 Error
<< "wait() got an invalid pid_t";
165 return Application::INVALID
;
170 char buffer
[1024]= { 0 };
172 while (((read_length
= ::read(stdout_fd
.fd()[0], buffer
, sizeof(buffer
))) != 0) or bail
)
174 if (read_length
== -1)
182 Error
<< strerror(errno
);
186 _stdout_buffer
.reserve(read_length
+1);
187 for (size_t x
= 0; x
< read_length
; x
++)
189 _stdout_buffer
.push_back(buffer
[x
]);
191 // @todo Suck up all output code here
197 char buffer
[1024]= { 0 };
199 while (((read_length
= ::read(stderr_fd
.fd()[0], buffer
, sizeof(buffer
))) != 0) or bail
)
201 if (read_length
== -1)
209 Error
<< strerror(errno
);
213 _stderr_buffer
.reserve(read_length
+1);
214 for (size_t x
= 0; x
< read_length
; x
++)
216 _stderr_buffer
.push_back(buffer
[x
]);
218 // @todo Suck up all errput code here
222 error_t exit_code
= FAILURE
;
226 if ((waited_pid
= waitpid(_pid
, &status
, 0)) == -1)
228 Error
<< "Error occured while waitpid(" << strerror(errno
) << ") on pid " << int(_pid
);
232 if (waited_pid
!= _pid
)
234 throw libtest::fatal(LIBYATL_DEFAULT_PARAM
, "Pid mismatch, %d != %d", int(waited_pid
), int(_pid
));
236 exit_code
= int_to_error_t(exited_successfully(status
));
240 if (exit_code
== Application::INVALID
)
242 Error
<< print_argv(built_argv
, _argc
, _pid
);
249 void Application::add_option(const std::string
& arg
)
251 _options
.push_back(std::make_pair(arg
, std::string()));
254 void Application::add_option(const std::string
& name
, const std::string
& value
)
256 _options
.push_back(std::make_pair(name
, value
));
259 Application::Pipe::Pipe()
267 void Application::Pipe::reset()
273 if ((ret
= pipe(_fd
)) < 0)
281 ret
= fcntl(_fd
[0], F_GETFL
, 0);
284 Error
<< "fcntl(F_GETFL) " << strerror(ret
);
288 ret
= fcntl(_fd
[0], F_SETFL
, ret
| O_NONBLOCK
);
291 Error
<< "fcntl(F_SETFL) " << strerror(ret
);
297 Application::Pipe::~Pipe()
303 void Application::Pipe::dup_for_spawn(const close_t
& arg
, posix_spawn_file_actions_t
& file_actions
, const int newfildes
)
308 if ((ret
= posix_spawn_file_actions_adddup2(&file_actions
, _fd
[type
], newfildes
)) < 0)
310 Error
<< "posix_spawn_file_actions_adddup2(" << strerror(ret
) << ")";
314 if ((ret
= posix_spawn_file_actions_addclose(&file_actions
, _fd
[type
])) < 0)
316 Error
<< "posix_spawn_file_actions_adddup2(" << strerror(ret
) << ")";
321 void Application::Pipe::close(const close_t
& arg
)
328 if ((ret
= ::close(_fd
[type
])) < 0)
330 Error
<< "close(" << strerror(ret
) << ")";
336 void Application::create_argv(const char *args
[])
338 _argc
= 2 +_use_libtool
? 2 : 0; // +1 for the command, +2 for libtool/mode=execute, +1 for the NULL
342 _argc
+= 2; // +2 for libtool --mode=execute
345 for (Options::const_iterator iter
= _options
.begin(); iter
!= _options
.end(); iter
++)
348 if ((*iter
).second
.empty() == false)
356 for (const char **ptr
= args
; *ptr
; ++ptr
)
363 built_argv
= new char * [_argc
];
369 built_argv
[x
++]= strdup(libtool());
370 built_argv
[x
++]= strdup("--mode=execute");
372 built_argv
[x
++]= strdup(_exectuble_with_path
.c_str());
374 for (Options::const_iterator iter
= _options
.begin(); iter
!= _options
.end(); iter
++)
376 built_argv
[x
++]= strdup((*iter
).first
.c_str());
377 if ((*iter
).second
.empty() == false)
379 built_argv
[x
++]= strdup((*iter
).second
.c_str());
385 for (const char **ptr
= args
; *ptr
; ++ptr
)
387 built_argv
[x
++]= strdup(*ptr
);
390 built_argv
[_argc
-1]= NULL
;
393 std::string
Application::print()
395 return print_argv(built_argv
, _argc
, _pid
);
398 void Application::delete_argv()
400 if (built_argv
== NULL
)
405 for (size_t x
= 0; x
< _argc
; x
++)
409 ::free(built_argv
[x
]);
418 int exec_cmdline(const std::string
& command
, const char *args
[], bool use_libtool
)
420 Application
app(command
, use_libtool
);
422 Application::error_t ret
= app
.run(args
);
424 if (ret
!= Application::SUCCESS
)
433 const char *gearmand_binary()
435 return GEARMAND_BINARY
;
438 } // namespace exec_cmdline