cmdline.cc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Data Differential YATL (i.e. libtest) library
  4. *
  5. * Copyright (C) 2012 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. #include <config.h>
  37. #include <libtest/common.h>
  38. using namespace libtest;
  39. #include <cstdlib>
  40. #include <cstring>
  41. #include <cerrno>
  42. #include <fcntl.h>
  43. #include <fstream>
  44. #include <memory>
  45. #include <poll.h>
  46. #include <spawn.h>
  47. #include <sstream>
  48. #include <string>
  49. #include <sys/stat.h>
  50. #include <sys/types.h>
  51. #include <unistd.h>
  52. #include <algorithm>
  53. #ifndef __USE_GNU
  54. static char **environ= NULL;
  55. #endif
  56. #ifndef FD_CLOEXEC
  57. # define FD_CLOEXEC 0
  58. #endif
  59. extern "C" {
  60. static int exited_successfully(int status)
  61. {
  62. if (status == 0)
  63. {
  64. return EXIT_SUCCESS;
  65. }
  66. if (WIFEXITED(status) == true)
  67. {
  68. return WEXITSTATUS(status);
  69. }
  70. else if (WIFSIGNALED(status) == true)
  71. {
  72. return WTERMSIG(status);
  73. }
  74. return EXIT_FAILURE;
  75. }
  76. }
  77. namespace {
  78. std::string print_argv(libtest::vchar_ptr_t& built_argv)
  79. {
  80. std::stringstream arg_buffer;
  81. for (vchar_ptr_t::iterator iter= built_argv.begin();
  82. iter == built_argv.end();
  83. iter++)
  84. {
  85. arg_buffer << *iter << " ";
  86. }
  87. return arg_buffer.str();
  88. }
  89. #if 0
  90. std::string print_argv(char** argv)
  91. {
  92. std::stringstream arg_buffer;
  93. for (char** ptr= argv; *ptr; ++ptr)
  94. {
  95. arg_buffer << *ptr << " ";
  96. }
  97. return arg_buffer.str();
  98. }
  99. #endif
  100. static Application::error_t int_to_error_t(int arg)
  101. {
  102. switch (arg)
  103. {
  104. case 127:
  105. return Application::INVALID;
  106. case 0:
  107. return Application::SUCCESS;
  108. default:
  109. case 1:
  110. return Application::FAILURE;
  111. }
  112. }
  113. }
  114. namespace libtest {
  115. Application::Application(const std::string& arg, const bool _use_libtool_arg) :
  116. _use_libtool(_use_libtool_arg),
  117. _use_valgrind(false),
  118. _use_gdb(false),
  119. _use_ptrcheck(false),
  120. _will_fail(false),
  121. _argc(0),
  122. _exectuble(arg),
  123. stdin_fd(STDIN_FILENO),
  124. stdout_fd(STDOUT_FILENO),
  125. stderr_fd(STDERR_FILENO),
  126. _pid(-1)
  127. {
  128. if (_use_libtool)
  129. {
  130. if (libtool() == NULL)
  131. {
  132. fatal_message("libtool requested, but know libtool was found");
  133. }
  134. }
  135. // Find just the name of the application with no path
  136. {
  137. size_t found= arg.find_last_of("/\\");
  138. if (found)
  139. {
  140. _exectuble_name= arg.substr(found +1);
  141. }
  142. else
  143. {
  144. _exectuble_name= arg;
  145. }
  146. }
  147. if (_use_libtool and getenv("PWD"))
  148. {
  149. _exectuble_with_path+= getenv("PWD");
  150. _exectuble_with_path+= "/";
  151. }
  152. _exectuble_with_path+= _exectuble;
  153. }
  154. Application::~Application()
  155. {
  156. murder();
  157. delete_argv();
  158. }
  159. Application::error_t Application::run(const char *args[])
  160. {
  161. stdin_fd.reset();
  162. stdout_fd.reset();
  163. stderr_fd.reset();
  164. _stdout_buffer.clear();
  165. _stderr_buffer.clear();
  166. posix_spawn_file_actions_t file_actions;
  167. posix_spawn_file_actions_init(&file_actions);
  168. stdin_fd.dup_for_spawn(file_actions);
  169. stdout_fd.dup_for_spawn(file_actions);
  170. stderr_fd.dup_for_spawn(file_actions);
  171. posix_spawnattr_t spawnattr;
  172. posix_spawnattr_init(&spawnattr);
  173. sigset_t set;
  174. sigemptyset(&set);
  175. fatal_assert(posix_spawnattr_setsigmask(&spawnattr, &set) == 0);
  176. create_argv(args);
  177. int spawn_ret;
  178. if (_use_gdb)
  179. {
  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);
  183. _gdb_filename= create_tmpfile(_exectuble_name);
  184. file_stream
  185. << "set logging redirect on" << std::endl
  186. << "set logging file " << _gdb_filename << std::endl
  187. << "set logging overwrite on" << std::endl
  188. << "set logging on" << std::endl
  189. << "set environment LIBTEST_IN_GDB=1" << std::endl
  190. << "run " << arguments() << std::endl
  191. << "thread apply all bt" << std::endl
  192. << "quit" << std::endl;
  193. fatal_assert(file_stream.good());
  194. file_stream.close();
  195. if (_use_libtool)
  196. {
  197. // libtool --mode=execute gdb -f -x binary
  198. char *argv[]= {
  199. const_cast<char *>(libtool()),
  200. const_cast<char *>("--mode=execute"),
  201. const_cast<char *>("gdb"),
  202. const_cast<char *>("-batch"),
  203. const_cast<char *>("-f"),
  204. const_cast<char *>("-x"),
  205. const_cast<char *>(gdb_run_file.c_str()),
  206. const_cast<char *>(_exectuble_with_path.c_str()),
  207. 0};
  208. spawn_ret= posix_spawnp(&_pid, libtool(), &file_actions, &spawnattr, argv, environ);
  209. }
  210. else
  211. {
  212. // gdb binary
  213. char *argv[]= {
  214. const_cast<char *>("gdb"),
  215. const_cast<char *>("-batch"),
  216. const_cast<char *>("-f"),
  217. const_cast<char *>("-x"),
  218. const_cast<char *>(gdb_run_file.c_str()),
  219. const_cast<char *>(_exectuble_with_path.c_str()),
  220. 0};
  221. spawn_ret= posix_spawnp(&_pid, "gdb", &file_actions, &spawnattr, argv, environ);
  222. }
  223. }
  224. else
  225. {
  226. if (_use_libtool)
  227. {
  228. spawn_ret= posix_spawn(&_pid, built_argv[0], &file_actions, &spawnattr, &built_argv[0], NULL);
  229. }
  230. else
  231. {
  232. spawn_ret= posix_spawnp(&_pid, built_argv[0], &file_actions, &spawnattr, &built_argv[0], NULL);
  233. }
  234. }
  235. posix_spawn_file_actions_destroy(&file_actions);
  236. posix_spawnattr_destroy(&spawnattr);
  237. stdin_fd.close(Application::Pipe::READ);
  238. stdout_fd.close(Application::Pipe::WRITE);
  239. stderr_fd.close(Application::Pipe::WRITE);
  240. if (spawn_ret != 0)
  241. {
  242. if (_will_fail == false)
  243. {
  244. Error << strerror(spawn_ret) << "(" << spawn_ret << ")";
  245. }
  246. _pid= -1;
  247. return Application::INVALID;
  248. }
  249. return Application::SUCCESS;
  250. }
  251. bool Application::check() const
  252. {
  253. if (_pid > 1 and kill(_pid, 0) == 0)
  254. {
  255. return true;
  256. }
  257. return false;
  258. }
  259. void Application::murder()
  260. {
  261. if (check())
  262. {
  263. int count= 5;
  264. while ((count--) > 0 and check())
  265. {
  266. int kill_ret= kill(_pid, SIGTERM);
  267. if (kill_ret == 0)
  268. {
  269. int status= 0;
  270. pid_t waitpid_ret;
  271. if ((waitpid_ret= waitpid(_pid, &status, WNOHANG)) == -1)
  272. {
  273. switch (errno)
  274. {
  275. case ECHILD:
  276. case EINTR:
  277. break;
  278. default:
  279. Error << "waitpid() failed after kill with error of " << strerror(errno);
  280. break;
  281. }
  282. }
  283. if (waitpid_ret == 0)
  284. {
  285. libtest::dream(1, 0);
  286. }
  287. }
  288. else
  289. {
  290. Error << "kill(pid, SIGTERM) failed after kill with error of " << strerror(errno);
  291. continue;
  292. }
  293. break;
  294. }
  295. // If for whatever reason it lives, kill it hard
  296. if (check())
  297. {
  298. (void)kill(_pid, SIGKILL);
  299. }
  300. }
  301. slurp();
  302. }
  303. // false means that no data was returned
  304. bool Application::slurp()
  305. {
  306. struct pollfd fds[2];
  307. fds[0].fd= stdout_fd.fd();
  308. fds[0].events= POLLRDNORM;
  309. fds[0].revents= 0;
  310. fds[1].fd= stderr_fd.fd();
  311. fds[1].events= POLLRDNORM;
  312. fds[1].revents= 0;
  313. int active_fd;
  314. if ((active_fd= poll(fds, 2, 0)) == -1)
  315. {
  316. int error;
  317. switch ((error= errno))
  318. {
  319. #ifdef TARGET_OS_LINUX
  320. case ERESTART:
  321. #endif
  322. case EINTR:
  323. break;
  324. case EFAULT:
  325. case ENOMEM:
  326. fatal_message(strerror(error));
  327. break;
  328. case EINVAL:
  329. fatal_message("RLIMIT_NOFILE exceeded, or if OSX the timeout value was invalid");
  330. break;
  331. default:
  332. fatal_message(strerror(error));
  333. break;
  334. }
  335. return false;
  336. }
  337. if (active_fd == 0)
  338. {
  339. return false;
  340. }
  341. bool data_was_read= false;
  342. if (fds[0].revents & POLLRDNORM)
  343. {
  344. if (stdout_fd.read(_stdout_buffer) == true)
  345. {
  346. data_was_read= true;
  347. }
  348. }
  349. if (fds[1].revents & POLLRDNORM)
  350. {
  351. if (stderr_fd.read(_stderr_buffer) == true)
  352. {
  353. data_was_read= true;
  354. }
  355. }
  356. return data_was_read;
  357. }
  358. Application::error_t Application::wait(bool nohang)
  359. {
  360. if (_pid == -1)
  361. {
  362. return Application::INVALID;
  363. }
  364. slurp();
  365. error_t exit_code= FAILURE;
  366. {
  367. int status= 0;
  368. pid_t waited_pid;
  369. if ((waited_pid= waitpid(_pid, &status, nohang ? WNOHANG : 0)) == -1)
  370. {
  371. switch (errno)
  372. {
  373. case ECHILD:
  374. exit_code= Application::SUCCESS;
  375. break;
  376. case EINTR:
  377. break;
  378. default:
  379. Error << "Error occured while waitpid(" << strerror(errno) << ") on pid " << int(_pid);
  380. break;
  381. }
  382. }
  383. else if (waited_pid == 0)
  384. {
  385. exit_code= Application::SUCCESS;
  386. }
  387. else
  388. {
  389. if (waited_pid != _pid)
  390. {
  391. throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "Pid mismatch, %d != %d", int(waited_pid), int(_pid));
  392. }
  393. exit_code= int_to_error_t(exited_successfully(status));
  394. }
  395. }
  396. slurp();
  397. #if 0
  398. if (exit_code == Application::INVALID)
  399. {
  400. Error << print_argv(built_argv, _argc);
  401. }
  402. #endif
  403. return exit_code;
  404. }
  405. Application::error_t Application::join()
  406. {
  407. if (_pid == -1)
  408. {
  409. return Application::INVALID;
  410. }
  411. slurp();
  412. error_t exit_code= FAILURE;
  413. {
  414. int status= 0;
  415. pid_t waited_pid;
  416. do {
  417. waited_pid= waitpid(_pid, &status, 0);
  418. } while (waited_pid == -1 and (errno == EINTR or errno == EAGAIN));
  419. if (waited_pid == -1)
  420. {
  421. switch (errno)
  422. {
  423. case ECHILD:
  424. exit_code= Application::SUCCESS;
  425. break;
  426. case EINTR:
  427. break;
  428. default:
  429. Error << "Error occured while waitpid(" << strerror(errno) << ") on pid " << int(_pid);
  430. break;
  431. }
  432. }
  433. else if (waited_pid == 0)
  434. {
  435. exit_code= Application::SUCCESS;
  436. }
  437. else
  438. {
  439. if (waited_pid != _pid)
  440. {
  441. throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "Pid mismatch, %d != %d", int(waited_pid), int(_pid));
  442. }
  443. exit_code= int_to_error_t(exited_successfully(status));
  444. }
  445. }
  446. slurp();
  447. #if 0
  448. if (exit_code == Application::INVALID)
  449. {
  450. Error << print_argv(built_argv, _argc);
  451. }
  452. #endif
  453. return exit_code;
  454. }
  455. void Application::add_long_option(const std::string& name, const std::string& option_value)
  456. {
  457. std::string arg(name);
  458. arg+= option_value;
  459. _options.push_back(std::make_pair(arg, std::string()));
  460. }
  461. void Application::add_option(const std::string& arg)
  462. {
  463. _options.push_back(std::make_pair(arg, std::string()));
  464. }
  465. void Application::add_option(const std::string& name, const std::string& value)
  466. {
  467. _options.push_back(std::make_pair(name, value));
  468. }
  469. Application::Pipe::Pipe(int arg) :
  470. _std_fd(arg)
  471. {
  472. _pipe_fd[READ]= -1;
  473. _pipe_fd[WRITE]= -1;
  474. _open[READ]= false;
  475. _open[WRITE]= false;
  476. }
  477. int Application::Pipe::Pipe::fd()
  478. {
  479. if (_std_fd == STDOUT_FILENO)
  480. {
  481. return _pipe_fd[READ];
  482. }
  483. else if (_std_fd == STDERR_FILENO)
  484. {
  485. return _pipe_fd[READ];
  486. }
  487. return _pipe_fd[WRITE]; // STDIN_FILENO
  488. }
  489. bool Application::Pipe::read(libtest::vchar_t& arg)
  490. {
  491. fatal_assert(_std_fd == STDOUT_FILENO or _std_fd == STDERR_FILENO);
  492. bool data_was_read= false;
  493. ssize_t read_length;
  494. char buffer[1024]= { 0 };
  495. while ((read_length= ::read(_pipe_fd[READ], buffer, sizeof(buffer))))
  496. {
  497. if (read_length == -1)
  498. {
  499. switch(errno)
  500. {
  501. case EAGAIN:
  502. break;
  503. default:
  504. Error << strerror(errno);
  505. break;
  506. }
  507. break;
  508. }
  509. data_was_read= true;
  510. arg.reserve(read_length +1);
  511. for (size_t x= 0; x < size_t(read_length); ++x)
  512. {
  513. arg.push_back(buffer[x]);
  514. }
  515. // @todo Suck up all errput code here
  516. }
  517. return data_was_read;
  518. }
  519. void Application::Pipe::nonblock()
  520. {
  521. int ret;
  522. if ((ret= fcntl(_pipe_fd[READ], F_GETFL, 0)) == -1)
  523. {
  524. Error << "fcntl(F_GETFL) " << strerror(errno);
  525. throw strerror(errno);
  526. }
  527. if ((ret= fcntl(_pipe_fd[READ], F_SETFL, ret | O_NONBLOCK)) == -1)
  528. {
  529. Error << "fcntl(F_SETFL) " << strerror(errno);
  530. throw strerror(errno);
  531. }
  532. }
  533. void Application::Pipe::reset()
  534. {
  535. close(READ);
  536. close(WRITE);
  537. #if HAVE_PIPE2
  538. if (pipe2(_pipe_fd, O_NONBLOCK) == -1)
  539. #else
  540. if (pipe(_pipe_fd) == -1)
  541. #endif
  542. {
  543. fatal_message(strerror(errno));
  544. }
  545. _open[0]= true;
  546. _open[1]= true;
  547. if (true)
  548. {
  549. nonblock();
  550. cloexec();
  551. }
  552. }
  553. void Application::Pipe::cloexec()
  554. {
  555. //if (SOCK_CLOEXEC == 0)
  556. {
  557. if (FD_CLOEXEC)
  558. {
  559. int flags;
  560. do
  561. {
  562. flags= fcntl(_pipe_fd[WRITE], F_GETFD, 0);
  563. } while (flags == -1 and (errno == EINTR or errno == EAGAIN));
  564. if (flags == -1)
  565. {
  566. Error << "fcntl(F_GETFD) " << strerror(errno);
  567. throw strerror(errno);
  568. }
  569. int rval;
  570. do
  571. {
  572. rval= fcntl(_pipe_fd[WRITE], F_SETFD, flags | FD_CLOEXEC);
  573. } while (rval == -1 && (errno == EINTR or errno == EAGAIN));
  574. if (rval == -1)
  575. {
  576. Error << "fcntl(F_SETFD) " << strerror(errno);
  577. throw strerror(errno);
  578. }
  579. }
  580. }
  581. }
  582. Application::Pipe::~Pipe()
  583. {
  584. if (_pipe_fd[0] != -1)
  585. {
  586. ::close(_pipe_fd[0]);
  587. }
  588. if (_pipe_fd[1] != -1)
  589. {
  590. ::close(_pipe_fd[1]);
  591. }
  592. }
  593. void Application::Pipe::dup_for_spawn(posix_spawn_file_actions_t& file_actions)
  594. {
  595. int type= STDIN_FILENO == _std_fd ? 0 : 1;
  596. int ret;
  597. if ((ret= posix_spawn_file_actions_adddup2(&file_actions, _pipe_fd[type], _std_fd )) < 0)
  598. {
  599. Error << "posix_spawn_file_actions_adddup2(" << strerror(ret) << ")";
  600. fatal_message(strerror(ret));
  601. }
  602. if ((ret= posix_spawn_file_actions_addclose(&file_actions, _pipe_fd[type])) < 0)
  603. {
  604. Error << "posix_spawn_file_actions_adddup2(" << strerror(ret) << ")";
  605. fatal_message(strerror(ret));
  606. }
  607. }
  608. void Application::Pipe::close(const close_t& arg)
  609. {
  610. int type= int(arg);
  611. if (_open[type])
  612. {
  613. if (::close(_pipe_fd[type]) == -1)
  614. {
  615. Error << "close(" << strerror(errno) << ")";
  616. }
  617. _open[type]= false;
  618. _pipe_fd[type]= -1;
  619. }
  620. }
  621. void Application::create_argv(const char *args[])
  622. {
  623. delete_argv();
  624. if (_use_libtool)
  625. {
  626. assert(libtool());
  627. built_argv.push_back(strdup(libtool()));
  628. built_argv.push_back(strdup("--mode=execute"));
  629. }
  630. if (_use_valgrind)
  631. {
  632. /*
  633. valgrind --error-exitcode=1 --leak-check=yes --show-reachable=yes --track-fds=yes --malloc-fill=A5 --free-fill=DE
  634. */
  635. built_argv.push_back(strdup("valgrind"));
  636. built_argv.push_back(strdup("--error-exitcode=1"));
  637. built_argv.push_back(strdup("--leak-check=yes"));
  638. built_argv.push_back(strdup("--show-reachable=yes"));
  639. built_argv.push_back(strdup("--track-fds=yes"));
  640. #if 0
  641. built_argv[x++]= strdup("--track-origin=yes");
  642. #endif
  643. built_argv.push_back(strdup("--malloc-fill=A5"));
  644. built_argv.push_back(strdup("--free-fill=DE"));
  645. std::string log_file= create_tmpfile("valgrind");
  646. char buffer[1024];
  647. int length= snprintf(buffer, sizeof(buffer), "--log-file=%s", log_file.c_str());
  648. fatal_assert(length > 0 and size_t(length) < sizeof(buffer));
  649. built_argv.push_back(strdup(buffer));
  650. }
  651. else if (_use_ptrcheck)
  652. {
  653. /*
  654. valgrind --error-exitcode=1 --tool=exp-ptrcheck --log-file=
  655. */
  656. built_argv.push_back(strdup("valgrind"));
  657. built_argv.push_back(strdup("--error-exitcode=1"));
  658. built_argv.push_back(strdup("--tool=exp-ptrcheck"));
  659. std::string log_file= create_tmpfile("ptrcheck");
  660. char buffer[1024];
  661. int length= snprintf(buffer, sizeof(buffer), "--log-file=%s", log_file.c_str());
  662. fatal_assert(length > 0 and size_t(length) < sizeof(buffer));
  663. built_argv.push_back(strdup(buffer));
  664. }
  665. else if (_use_gdb)
  666. {
  667. built_argv.push_back(strdup("gdb"));
  668. }
  669. built_argv.push_back(strdup(_exectuble_with_path.c_str()));
  670. for (Options::const_iterator iter= _options.begin(); iter != _options.end(); ++iter)
  671. {
  672. built_argv.push_back(strdup((*iter).first.c_str()));
  673. if ((*iter).second.empty() == false)
  674. {
  675. built_argv.push_back(strdup((*iter).second.c_str()));
  676. }
  677. }
  678. if (args)
  679. {
  680. for (const char **ptr= args; *ptr; ++ptr)
  681. {
  682. built_argv.push_back(strdup(*ptr));
  683. }
  684. }
  685. built_argv.push_back(NULL);
  686. }
  687. std::string Application::print()
  688. {
  689. return print_argv(built_argv);
  690. }
  691. std::string Application::arguments()
  692. {
  693. std::stringstream arg_buffer;
  694. for (size_t x= (1 +_use_libtool) ? 2 : 0;
  695. x < _argc and built_argv[x];
  696. ++x)
  697. {
  698. arg_buffer << built_argv[x] << " ";
  699. }
  700. return arg_buffer.str();
  701. }
  702. struct DeleteFromVector
  703. {
  704. template <class T>
  705. void operator() ( T* ptr) const
  706. {
  707. free(ptr);
  708. }
  709. };
  710. void Application::delete_argv()
  711. {
  712. std::for_each(built_argv.begin(), built_argv.end(), DeleteFromVector());
  713. built_argv.clear();
  714. _argc= 0;
  715. }
  716. int exec_cmdline(const std::string& command, const char *args[], bool use_libtool)
  717. {
  718. Application app(command, use_libtool);
  719. Application::error_t ret= app.run(args);
  720. if (ret != Application::SUCCESS)
  721. {
  722. return int(ret);
  723. }
  724. return int(app.join());
  725. }
  726. const char *gearmand_binary()
  727. {
  728. return GEARMAND_BINARY;
  729. }
  730. const char *drizzled_binary()
  731. {
  732. return DRIZZLED_BINARY;
  733. }
  734. } // namespace exec_cmdline