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 if (waited_pid
!= _pid
)
219 throw libtest::fatal(LIBYATL_DEFAULT_PARAM
, "Pid mismatch, %d != %d", int(waited_pid
), int(_pid
));
221 exit_code
= error_t(exited_successfully(status
));
225 if (exit_code
== Application::INVALID
)
228 Error
<< print_argv(built_argv
, _argc
, _pid
);
235 void Application::add_option(const std::string
& arg
)
237 _options
.push_back(std::make_pair(arg
, std::string()));
240 void Application::add_option(const std::string
& name
, const std::string
& value
)
242 _options
.push_back(std::make_pair(name
, value
));
245 Application::Pipe::Pipe()
253 void Application::Pipe::reset()
259 if ((ret
= pipe(_fd
)) < 0)
267 ret
= fcntl(_fd
[0], F_GETFL
, 0);
270 Error
<< "fcntl(F_GETFL) " << strerror(ret
);
274 ret
= fcntl(_fd
[0], F_SETFL
, ret
| O_NONBLOCK
);
277 Error
<< "fcntl(F_SETFL) " << strerror(ret
);
283 Application::Pipe::~Pipe()
289 void Application::Pipe::dup_for_spawn(const close_t
& arg
, posix_spawn_file_actions_t
& file_actions
, const int newfildes
)
294 if ((ret
= posix_spawn_file_actions_adddup2(&file_actions
, _fd
[type
], newfildes
)) < 0)
296 Error
<< "posix_spawn_file_actions_adddup2(" << strerror(ret
) << ")";
300 if ((ret
= posix_spawn_file_actions_addclose(&file_actions
, _fd
[type
])) < 0)
302 Error
<< "posix_spawn_file_actions_adddup2(" << strerror(ret
) << ")";
307 void Application::Pipe::close(const close_t
& arg
)
314 if ((ret
= ::close(_fd
[type
])) < 0)
316 Error
<< "close(" << strerror(ret
) << ")";
322 void Application::create_argv(const char *args
[])
324 _argc
= 2 +_use_libtool
? 2 : 0; // +1 for the command, +2 for libtool/mode=execute, +1 for the NULL
328 _argc
+= 2; // +2 for libtool --mode=execute
331 for (Options::const_iterator iter
= _options
.begin(); iter
!= _options
.end(); iter
++)
334 if ((*iter
).second
.empty() == false)
342 for (const char **ptr
= args
; *ptr
; ++ptr
)
349 built_argv
= new char * [_argc
];
355 built_argv
[x
++]= strdup(libtool());
356 built_argv
[x
++]= strdup("--mode=execute");
358 built_argv
[x
++]= strdup(_exectuble_with_path
.c_str());
360 for (Options::const_iterator iter
= _options
.begin(); iter
!= _options
.end(); iter
++)
362 built_argv
[x
++]= strdup((*iter
).first
.c_str());
363 if ((*iter
).second
.empty() == false)
365 built_argv
[x
++]= strdup((*iter
).second
.c_str());
371 for (const char **ptr
= args
; *ptr
; ++ptr
)
373 built_argv
[x
++]= strdup(*ptr
);
376 built_argv
[_argc
-1]= NULL
;
379 std::string
Application::print()
381 return print_argv(built_argv
, _argc
, _pid
);
384 void Application::delete_argv()
386 if (built_argv
== NULL
)
391 for (size_t x
= 0; x
< _argc
; x
++)
395 ::free(built_argv
[x
]);
404 int exec_cmdline(const std::string
& command
, const char *args
[], bool use_libtool
)
406 Application
app(command
, use_libtool
);
408 Application::error_t ret
= app
.run(args
);
410 if (ret
!= Application::SUCCESS
)
419 const char *gearmand_binary()
421 return GEARMAND_BINARY
;
424 } // namespace exec_cmdline