cmdline.cc 18 KB

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