55e6b5b66ba0a31a3e6c7bf00ab3a68c73946c3c
[m6w6/libmemcached] / libtest / cmdline.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Data Differential YATL (i.e. libtest) library
4 *
5 * Copyright (C) 2012-2013 Data Differential, http://datadifferential.com/
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following disclaimer
16 * in the documentation and/or other materials provided with the
17 * distribution.
18 *
19 * * The names of its contributors may not be used to endorse or
20 * promote products derived from this software without specific prior
21 * written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *
35 */
36
37 #include "libtest/yatlcon.h"
38
39 #include "libtest/common.h"
40
41 using namespace libtest;
42
43 #include <cstdlib>
44 #include <cstring>
45 #include <cerrno>
46 #include <fcntl.h>
47 #include <fstream>
48 #include <memory>
49 #ifdef HAVE_POLL_H
50 # include <poll.h>
51 #endif
52 #ifdef HAVE_SPAWN_H
53 # include <spawn.h>
54 #endif
55 #include <sstream>
56 #include <string>
57 #include <sys/stat.h>
58 #include <sys/types.h>
59 #include <unistd.h>
60
61 #include <algorithm>
62 #include <stdexcept>
63
64 #ifndef __USE_GNU
65 static char **environ= NULL;
66 #endif
67
68 #ifndef FD_CLOEXEC
69 # define FD_CLOEXEC 0
70 #endif
71
72 namespace {
73
74 std::string print_argv(libtest::vchar_ptr_t& built_argv)
75 {
76 std::stringstream arg_buffer;
77
78 for (vchar_ptr_t::iterator iter= built_argv.begin();
79 iter != built_argv.end();
80 ++iter)
81 {
82 if (*iter)
83 {
84 arg_buffer << *iter << " ";
85 }
86 }
87
88 return arg_buffer.str();
89 }
90
91 #if 0
92 std::string print_argv(char** argv)
93 {
94 std::stringstream arg_buffer;
95
96 for (char** ptr= argv; *ptr; ++ptr)
97 {
98 arg_buffer << *ptr << " ";
99 }
100
101 return arg_buffer.str();
102 }
103 #endif
104
105 static Application::error_t int_to_error_t(int arg)
106 {
107 switch (arg)
108 {
109 case 127:
110 return Application::INVALID_POSIX_SPAWN;
111
112 case 0:
113 return Application::SUCCESS;
114
115 case 1:
116 return Application::FAILURE;
117
118 default:
119 return Application::UNKNOWN;
120 }
121 }
122 }
123
124 namespace libtest {
125
126 Application::Application(const std::string& arg, const bool _use_libtool_arg) :
127 _use_libtool(_use_libtool_arg),
128 _use_valgrind(false),
129 _use_gdb(false),
130 _use_ptrcheck(false),
131 _will_fail(false),
132 _argc(0),
133 _exectuble(arg),
134 stdin_fd(STDIN_FILENO),
135 stdout_fd(STDOUT_FILENO),
136 stderr_fd(STDERR_FILENO),
137 _pid(-1),
138 _status(0),
139 _app_exit_state(UNINITIALIZED)
140 {
141 if (_use_libtool)
142 {
143 if (libtool() == NULL)
144 {
145 FATAL("libtool requested, but no libtool was found");
146 }
147 }
148
149 // Find just the name of the application with no path
150 {
151 size_t found= arg.find_last_of("/\\");
152 if (found)
153 {
154 _exectuble_name= arg.substr(found +1);
155 }
156 else
157 {
158 _exectuble_name= arg;
159 }
160 }
161
162 if (_use_libtool and getenv("PWD"))
163 {
164 _exectuble_with_path+= getenv("PWD");
165 _exectuble_with_path+= "/";
166 }
167 _exectuble_with_path+= _exectuble;
168 }
169
170 Application::~Application()
171 {
172 murder();
173 delete_argv();
174 }
175
176 Application::error_t Application::run(const char *args[])
177 {
178 stdin_fd.reset();
179 stdout_fd.reset();
180 stderr_fd.reset();
181 _stdout_buffer.clear();
182 _stderr_buffer.clear();
183
184 posix_spawn_file_actions_t file_actions;
185 posix_spawn_file_actions_init(&file_actions);
186
187 stdin_fd.dup_for_spawn(file_actions);
188 stdout_fd.dup_for_spawn(file_actions);
189 stderr_fd.dup_for_spawn(file_actions);
190
191 posix_spawnattr_t spawnattr;
192 posix_spawnattr_init(&spawnattr);
193
194 short flags= 0;
195
196 // Child should not block signals
197 flags |= POSIX_SPAWN_SETSIGMASK;
198
199 sigset_t mask;
200 sigemptyset(&mask);
201
202 fatal_assert(posix_spawnattr_setsigmask(&spawnattr, &mask) == 0);
203
204 #if defined(POSIX_SPAWN_USEVFORK) || defined(__linux__)
205 // Use USEVFORK on linux
206 flags |= POSIX_SPAWN_USEVFORK;
207 #endif
208
209 flags |= POSIX_SPAWN_SETPGROUP;
210 fatal_assert(posix_spawnattr_setpgroup(&spawnattr, 0) == 0);
211
212 fatal_assert(posix_spawnattr_setflags(&spawnattr, flags) == 0);
213
214 create_argv(args);
215
216 int spawn_ret;
217 if (_use_gdb)
218 {
219 std::string gdb_run_file= create_tmpfile(_exectuble_name);
220 std::fstream file_stream;
221 file_stream.open(gdb_run_file.c_str(), std::fstream::out | std::fstream::trunc);
222
223 _gdb_filename= create_tmpfile(_exectuble_name);
224 file_stream
225 << "set logging redirect on" << std::endl
226 << "set logging file " << _gdb_filename << std::endl
227 << "set logging overwrite on" << std::endl
228 << "set logging on" << std::endl
229 << "set environment LIBTEST_IN_GDB=1" << std::endl
230 << "run " << arguments() << std::endl
231 << "thread apply all bt" << std::endl
232 << "quit" << std::endl;
233
234 fatal_assert(file_stream.good());
235 file_stream.close();
236
237 if (_use_libtool)
238 {
239 // libtool --mode=execute gdb -f -x binary
240 char *argv[]= {
241 const_cast<char *>(libtool()),
242 const_cast<char *>("--mode=execute"),
243 const_cast<char *>("gdb"),
244 const_cast<char *>("-batch"),
245 const_cast<char *>("-f"),
246 const_cast<char *>("-x"),
247 const_cast<char *>(gdb_run_file.c_str()),
248 const_cast<char *>(_exectuble_with_path.c_str()),
249 0};
250
251 spawn_ret= posix_spawnp(&_pid, libtool(), &file_actions, &spawnattr, argv, environ);
252 }
253 else
254 {
255 // gdb binary
256 char *argv[]= {
257 const_cast<char *>("gdb"),
258 const_cast<char *>("-batch"),
259 const_cast<char *>("-f"),
260 const_cast<char *>("-x"),
261 const_cast<char *>(gdb_run_file.c_str()),
262 const_cast<char *>(_exectuble_with_path.c_str()),
263 0};
264 spawn_ret= posix_spawnp(&_pid, "gdb", &file_actions, &spawnattr, argv, environ);
265 }
266 }
267 else
268 {
269 spawn_ret= posix_spawn(&_pid, built_argv[0], &file_actions, &spawnattr, &built_argv[0], NULL);
270 }
271
272 posix_spawn_file_actions_destroy(&file_actions);
273 posix_spawnattr_destroy(&spawnattr);
274
275 stdin_fd.close(Application::Pipe::READ);
276 stdout_fd.close(Application::Pipe::WRITE);
277 stderr_fd.close(Application::Pipe::WRITE);
278
279 if (spawn_ret != 0)
280 {
281 if (_will_fail == false)
282 {
283 std::string sb;
284 char buf[1024];
285
286 std::for_each(built_argv.begin(), built_argv.end()-1, [&sb](const char *a) {
287 if (sb.size()) {
288 sb.append(" ");
289 }
290 sb.append(a);
291 });
292 Error << strerror(spawn_ret) << "(" << spawn_ret << "): " << sb << " cwd:" << getcwd(buf, sizeof(buf)-1);
293 }
294 _pid= -1;
295 return Application::INVALID_POSIX_SPAWN;
296 }
297
298 assert(_pid != -1);
299 if (_pid == -1)
300 {
301 return Application::INVALID_POSIX_SPAWN;
302 }
303
304 #if 0
305 app_thread_st* _app_thread= new app_thread_st(_pid, _status, built_argv[0], _app_exit_state);
306 int error;
307 if ((error= pthread_create(&_thread, NULL, &app_thread, _app_thread)) != 0)
308 {
309 Error << "pthread_create() died during pthread_create(" << strerror(error) << ")";
310 return Application::FAILURE;
311 }
312 #endif
313
314 return Application::SUCCESS;
315 }
316
317 bool Application::check() const
318 {
319 if (_pid > 1 and kill(_pid, 0) == 0)
320 {
321 return true;
322 }
323
324 return false;
325 }
326
327 void Application::murder()
328 {
329 if (check())
330 {
331 int count= 5;
332 while ((count--) > 0 and check())
333 {
334 if (kill(_pid, SIGTERM) == 0)
335 {
336 join();
337 }
338 else
339 {
340 Error << "kill(pid, SIGTERM) failed after kill with error of " << strerror(errno);
341 continue;
342 }
343
344 break;
345 }
346
347 // If for whatever reason it lives, kill it hard
348 if (check())
349 {
350 Error << "using SIGKILL, things will likely go poorly from this point";
351 (void)kill(_pid, SIGKILL);
352 }
353 }
354 slurp();
355 }
356
357 // false means that no data was returned
358 bool Application::slurp()
359 {
360 struct pollfd fds[2];
361 fds[0].fd= stdout_fd.fd();
362 fds[0].events= POLLRDNORM;
363 fds[0].revents= 0;
364 fds[1].fd= stderr_fd.fd();
365 fds[1].events= POLLRDNORM;
366 fds[1].revents= 0;
367
368 int active_fd;
369 if ((active_fd= poll(fds, 2, 0)) == -1)
370 {
371 int error;
372 switch ((error= errno))
373 {
374 #ifdef __linux
375 case ERESTART:
376 #endif
377 case EINTR:
378 break;
379
380 case EFAULT:
381 case ENOMEM:
382 FATAL(strerror(error));
383 break;
384
385 case EINVAL:
386 FATAL("RLIMIT_NOFILE exceeded, or if OSX the timeout value was invalid");
387 break;
388
389 default:
390 FATAL(strerror(error));
391 break;
392 }
393
394 return false;
395 }
396
397 if (active_fd == 0)
398 {
399 return false;
400 }
401
402 bool data_was_read= false;
403 if (fds[0].revents & POLLRDNORM)
404 {
405 if (stdout_fd.read(_stdout_buffer) == true)
406 {
407 data_was_read= true;
408 }
409 }
410
411 if (fds[1].revents & POLLRDNORM)
412 {
413 if (stderr_fd.read(_stderr_buffer) == true)
414 {
415 data_was_read= true;
416 }
417 }
418
419 return data_was_read;
420 }
421
422 std::pair<std::string, std::string> Application::output()
423 {
424 slurp();
425 return {
426 std::string {
427 stdout_result().data(),
428 stdout_result().size()
429 },
430 std::string {
431 stderr_result().data(),
432 stderr_result().size()
433 }
434 };
435
436 }
437
438 Application::error_t Application::join()
439 {
440 pid_t waited_pid= waitpid(_pid, &_status, WUNTRACED);
441 slurp();
442 if (waited_pid == _pid and WIFEXITED(_status) == false)
443 {
444 /*
445 What we are looking for here is how the exit status happened.
446 - 127 means that posix_spawn() itself had an error.
447 - If WEXITSTATUS is positive we need to see if it is a signal that we sent to kill the process. If not something bad happened in the process itself.
448 - Finally something has happened that we don't currently understand.
449 */
450 if (WEXITSTATUS(_status) == 127)
451 {
452 _app_exit_state= Application::INVALID_POSIX_SPAWN;
453 std::string error_string("posix_spawn() failed pid:");
454 error_string+= _pid;
455 error_string+= " name:";
456 error_string+= print_argv(built_argv);
457 if (stderr_result_length())
458 {
459 error_string+= " stderr: ";
460 error_string+= stderr_c_str();
461 }
462 throw std::logic_error(error_string);
463 }
464 else if (WIFSIGNALED(_status))
465 {
466 if (WTERMSIG(_status) != SIGTERM and WTERMSIG(_status) != SIGHUP)
467 {
468 slurp();
469 _app_exit_state= Application::INVALID_POSIX_SPAWN;
470 std::string error_string(print_argv(built_argv));
471 error_string+= " was killed by signal ";
472 error_string+= strsignal(WTERMSIG(_status));
473
474 if (stdout_result_length())
475 {
476 error_string+= " stdout: ";
477 error_string+= stdout_c_str();
478 }
479
480 if (stderr_result_length())
481 {
482 error_string+= " stderr: ";
483 error_string+= stderr_c_str();
484 }
485
486 throw std::runtime_error(error_string);
487 }
488
489 // If we terminted it on purpose then it counts as a success.
490 #if defined(DEBUG)
491 if (DEBUG)
492 {
493 Out << "waitpid() application terminated at request"
494 << " pid:" << _pid
495 << " name:" << built_argv[0];
496 }
497 #endif
498 }
499 else
500 {
501 _app_exit_state= Application::UNKNOWN;
502 Error << "Unknown logic state at exit:" << WEXITSTATUS(_status)
503 << " pid:" << _pid
504 << " name:" << built_argv[0];
505 }
506 }
507 else if (waited_pid == _pid and WIFEXITED(_status))
508 {
509 _app_exit_state= int_to_error_t(WEXITSTATUS(_status));
510 }
511 else if (waited_pid == -1)
512 {
513 std::string error_string;
514 if (stdout_result_length())
515 {
516 error_string+= " stdout: ";
517 error_string+= stdout_c_str();
518 }
519
520 if (stderr_result_length())
521 {
522 error_string+= " stderr: ";
523 error_string+= stderr_c_str();
524 }
525 Error << "waitpid() returned errno:" << strerror(errno) << " " << error_string;
526 _app_exit_state= Application::UNKNOWN;
527 }
528 else
529 {
530 _app_exit_state= Application::UNKNOWN;
531 throw std::logic_error("waitpid() returned an unknown value");
532 }
533
534 return _app_exit_state;
535 }
536
537 void Application::add_long_option(const std::string& name, const std::string& option_value)
538 {
539 std::string arg(name);
540 arg+= option_value;
541 _options.push_back(std::make_pair(arg, std::string()));
542 }
543
544 void Application::add_option(const std::string& arg)
545 {
546 _options.push_back(std::make_pair(arg, std::string()));
547 }
548
549 void Application::add_option(const std::string& name, const std::string& value)
550 {
551 _options.push_back(std::make_pair(name, value));
552 }
553
554 Application::Pipe::Pipe(int arg) :
555 _std_fd(arg)
556 {
557 _pipe_fd[READ]= -1;
558 _pipe_fd[WRITE]= -1;
559 _open[READ]= false;
560 _open[WRITE]= false;
561 }
562
563 int Application::Pipe::Pipe::fd()
564 {
565 if (_std_fd == STDOUT_FILENO)
566 {
567 return _pipe_fd[READ];
568 }
569 else if (_std_fd == STDERR_FILENO)
570 {
571 return _pipe_fd[READ];
572 }
573
574 return _pipe_fd[WRITE]; // STDIN_FILENO
575 }
576
577
578 bool Application::Pipe::read(libtest::vchar_t& arg)
579 {
580 fatal_assert(_std_fd == STDOUT_FILENO or _std_fd == STDERR_FILENO);
581
582 bool data_was_read= false;
583
584 libtest::vchar_t buffer;
585 buffer.resize(1024);
586 ssize_t read_length;
587 while ((read_length= ::read(_pipe_fd[READ], &buffer[0], buffer.size())))
588 {
589 if (read_length == -1)
590 {
591 switch(errno)
592 {
593 case EAGAIN:
594 break;
595
596 default:
597 Error << strerror(errno);
598 break;
599 }
600
601 break;
602 }
603
604 data_was_read= true;
605 arg.reserve(read_length +1);
606 for (size_t x= 0; x < size_t(read_length); ++x)
607 {
608 arg.push_back(buffer[x]);
609 }
610 // @todo Suck up all errput code here
611 }
612
613 return data_was_read;
614 }
615
616 void Application::Pipe::nonblock()
617 {
618 int flags;
619 do
620 {
621 flags= fcntl(_pipe_fd[READ], F_GETFL, 0);
622 } while (flags == -1 and (errno == EINTR or errno == EAGAIN));
623
624 if (flags == -1)
625 {
626 Error << "fcntl(F_GETFL) " << strerror(errno);
627 throw strerror(errno);
628 }
629
630 int rval;
631 do
632 {
633 rval= fcntl(_pipe_fd[READ], F_SETFL, flags | O_NONBLOCK);
634 } while (rval == -1 and (errno == EINTR or errno == EAGAIN));
635
636 if (rval == -1)
637 {
638 Error << "fcntl(F_SETFL) " << strerror(errno);
639 throw strerror(errno);
640 }
641 }
642
643 void Application::Pipe::reset()
644 {
645 close(READ);
646 close(WRITE);
647
648 #ifdef HAVE_PIPE2
649 if (pipe2(_pipe_fd, O_NONBLOCK|O_CLOEXEC) == -1)
650 #endif
651 {
652 if (pipe(_pipe_fd) == -1)
653 {
654 FATAL(strerror(errno));
655 }
656
657 // Since either pipe2() was not found/called we set the pipe directly
658 nonblock();
659 cloexec();
660 }
661 _open[0]= true;
662 _open[1]= true;
663 }
664
665 void Application::Pipe::cloexec()
666 {
667 //if (SOCK_CLOEXEC == 0)
668 {
669 if (FD_CLOEXEC)
670 {
671 int flags;
672 do
673 {
674 flags= fcntl(_pipe_fd[WRITE], F_GETFD, 0);
675 } while (flags == -1 and (errno == EINTR or errno == EAGAIN));
676
677 if (flags == -1)
678 {
679 Error << "fcntl(F_GETFD) " << strerror(errno);
680 throw strerror(errno);
681 }
682
683 int rval;
684 do
685 {
686 rval= fcntl(_pipe_fd[WRITE], F_SETFD, flags | FD_CLOEXEC);
687 } while (rval == -1 && (errno == EINTR or errno == EAGAIN));
688
689 if (rval == -1)
690 {
691 Error << "fcntl(F_SETFD) " << strerror(errno);
692 throw strerror(errno);
693 }
694 }
695 }
696 }
697
698 Application::Pipe::~Pipe()
699 {
700 if (_pipe_fd[0] != -1)
701 {
702 ::close(_pipe_fd[0]);
703 }
704
705 if (_pipe_fd[1] != -1)
706 {
707 ::close(_pipe_fd[1]);
708 }
709 }
710
711 void Application::Pipe::dup_for_spawn(posix_spawn_file_actions_t& file_actions)
712 {
713 int type= STDIN_FILENO == _std_fd ? 0 : 1;
714
715 int ret;
716 if ((ret= posix_spawn_file_actions_adddup2(&file_actions, _pipe_fd[type], _std_fd )) < 0)
717 {
718 FATAL("posix_spawn_file_actions_adddup2(%s)", strerror(ret));
719 }
720
721 if ((ret= posix_spawn_file_actions_addclose(&file_actions, _pipe_fd[type])) < 0)
722 {
723 FATAL("posix_spawn_file_actions_addclose(%s)", strerror(ret));
724 }
725 }
726
727 void Application::Pipe::close(const close_t& arg)
728 {
729 int type= int(arg);
730
731 if (_open[type])
732 {
733 if (::close(_pipe_fd[type]) == -1)
734 {
735 Error << "close(" << strerror(errno) << ")";
736 }
737 _open[type]= false;
738 _pipe_fd[type]= -1;
739 }
740 }
741
742 void Application::create_argv(const char *args[])
743 {
744 delete_argv();
745 if (_use_libtool)
746 {
747 assert(libtool());
748 vchar::append(built_argv, libtool());
749 vchar::append(built_argv, "--mode=execute");
750 }
751
752 if (_use_valgrind)
753 {
754 /*
755 valgrind --error-exitcode=1 --leak-check=yes --track-fds=yes --malloc-fill=A5 --free-fill=DE
756 */
757 vchar::append(built_argv, "valgrind");
758 vchar::append(built_argv, "--error-exitcode=1");
759 vchar::append(built_argv, "--leak-check=yes");
760 #if 0
761 vchar::append(built_argv, "--show-reachable=yes"));
762 #endif
763 vchar::append(built_argv, "--track-fds=yes");
764 #if 0
765 built_argv[x++]= strdup("--track-origin=yes");
766 #endif
767 vchar::append(built_argv, "--malloc-fill=A5");
768 vchar::append(built_argv, "--free-fill=DE");
769
770 std::string log_file= create_tmpfile("valgrind");
771 libtest::vchar_t buffer;
772 buffer.resize(1024);
773 int length= snprintf(&buffer[0], buffer.size(), "--log-file=%s", log_file.c_str());
774 fatal_assert(length > 0 and size_t(length) < buffer.size());
775 vchar::append(built_argv, &buffer[0]);
776 }
777 else if (_use_ptrcheck)
778 {
779 /*
780 valgrind --error-exitcode=1 --tool=exp-ptrcheck --log-file=
781 */
782 vchar::append(built_argv, "valgrind");
783 vchar::append(built_argv, "--error-exitcode=1");
784 vchar::append(built_argv, "--tool=exp-ptrcheck");
785 std::string log_file= create_tmpfile("ptrcheck");
786 libtest::vchar_t buffer;
787 buffer.resize(1024);
788 int length= snprintf(&buffer[0], buffer.size(), "--log-file=%s", log_file.c_str());
789 fatal_assert(length > 0 and size_t(length) < buffer.size());
790 vchar::append(built_argv, &buffer[0]);
791 }
792 else if (_use_gdb)
793 {
794 vchar::append(built_argv, "gdb");
795 }
796
797 vchar::append(built_argv, _exectuble_with_path.c_str());
798
799 for (Options::const_iterator iter= _options.begin(); iter != _options.end(); ++iter)
800 {
801 vchar::append(built_argv, (*iter).first.c_str());
802 if ((*iter).second.empty() == false)
803 {
804 vchar::append(built_argv, (*iter).second.c_str());
805 }
806 }
807
808 if (args)
809 {
810 for (const char **ptr= args; *ptr; ++ptr)
811 {
812 vchar::append(built_argv, *ptr);
813 }
814 }
815 built_argv.push_back(nullptr);
816 }
817
818 std::string Application::print()
819 {
820 return print_argv(built_argv);
821 }
822
823 std::string Application::arguments()
824 {
825 std::stringstream arg_buffer;
826
827 // Skip printing out the libtool reference
828 for (size_t x= _use_libtool ? 2 : 0; x < _argc; ++x)
829 {
830 if (built_argv[x])
831 {
832 arg_buffer << built_argv[x] << " ";
833 }
834 }
835
836 return arg_buffer.str();
837 }
838
839 void Application::delete_argv()
840 {
841 std::for_each(built_argv.begin(), built_argv.end(), FreeFromVector());
842
843 built_argv.clear();
844 _argc= 0;
845 }
846
847
848 int exec_cmdline(const std::string& command, const char *args[], bool use_libtool)
849 {
850 Application app(command, use_libtool);
851
852 Application::error_t ret= app.run(args);
853
854 if (Application::SUCCESS == ret) {
855 ret = app.join();
856 }
857
858 if (ret != Application::SUCCESS)
859 {
860 auto out = app.output();
861 Error << command << " stdout: " << out.first;
862 Error << command << " stderr: " << out.second;
863 }
864
865 return int(ret);
866 }
867
868 } // namespace exec_cmdline