executor.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. // Copyright (c) 2006, 2007 Julio M. Merino Vidal
  2. // Copyright (c) 2008 Ilya Sokolov, Boris Schaeling
  3. // Copyright (c) 2009 Boris Schaeling
  4. // Copyright (c) 2010 Felipe Tanus, Boris Schaeling
  5. // Copyright (c) 2011, 2012 Jeff Flinn, Boris Schaeling
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. #ifndef BOOST_PROCESS_DETAIL_POSIX_EXECUTOR_HPP
  10. #define BOOST_PROCESS_DETAIL_POSIX_EXECUTOR_HPP
  11. #include <boost/process/v1/detail/child_decl.hpp>
  12. #include <boost/process/v1/error.hpp>
  13. #include <boost/process/v1/pipe.hpp>
  14. #include <boost/process/v1/detail/posix/basic_pipe.hpp>
  15. #include <boost/process/v1/detail/posix/use_vfork.hpp>
  16. #include <boost/fusion/algorithm/iteration/for_each.hpp>
  17. #include <cstdlib>
  18. #include <sys/types.h>
  19. #include <fcntl.h>
  20. #include <errno.h>
  21. #include <unistd.h>
  22. #include <boost/algorithm/string/predicate.hpp>
  23. #include <boost/algorithm/string/split.hpp>
  24. #include <boost/algorithm/string/classification.hpp>
  25. #include <boost/core/ignore_unused.hpp>
  26. namespace boost { namespace process { BOOST_PROCESS_V1_INLINE namespace v1 { namespace detail { namespace posix {
  27. template<typename Executor>
  28. struct on_setup_t
  29. {
  30. Executor & exec;
  31. on_setup_t(Executor & exec) : exec(exec) {};
  32. template<typename T>
  33. void operator()(T & t) const
  34. {
  35. if (!exec.error())
  36. t.on_setup(exec);
  37. }
  38. };
  39. template<typename Executor>
  40. struct on_error_t
  41. {
  42. Executor & exec;
  43. const std::error_code & error;
  44. on_error_t(Executor & exec, const std::error_code & error) : exec(exec), error(error) {};
  45. template<typename T>
  46. void operator()(T & t) const
  47. {
  48. t.on_error(exec, error);
  49. }
  50. };
  51. template<typename Executor>
  52. struct on_success_t
  53. {
  54. Executor & exec;
  55. on_success_t(Executor & exec) : exec(exec) {};
  56. template<typename T>
  57. void operator()(T & t) const {t.on_success(exec);}
  58. };
  59. template<typename Executor>
  60. struct on_fork_error_t
  61. {
  62. Executor & exec;
  63. const std::error_code & error;
  64. on_fork_error_t(Executor & exec, const std::error_code & error) : exec(exec), error(error) {};
  65. template<typename T>
  66. void operator()(T & t) const
  67. {
  68. t.on_fork_error(exec, error);
  69. }
  70. };
  71. template<typename Executor>
  72. struct on_exec_setup_t
  73. {
  74. Executor & exec;
  75. on_exec_setup_t(Executor & exec) : exec(exec) {};
  76. template<typename T>
  77. void operator()(T & t) const
  78. {
  79. t.on_exec_setup(exec);
  80. }
  81. };
  82. template<typename Executor>
  83. struct on_exec_error_t
  84. {
  85. Executor & exec;
  86. const std::error_code &ec;
  87. on_exec_error_t(Executor & exec, const std::error_code & error) : exec(exec), ec(error) {};
  88. template<typename T>
  89. void operator()(T & t) const
  90. {
  91. t.on_exec_error(exec, ec);
  92. }
  93. };
  94. template<typename Executor>
  95. struct on_fork_success_t
  96. {
  97. Executor & exec;
  98. on_fork_success_t(Executor & exec) : exec(exec) {};
  99. template<typename T>
  100. void operator()(T & t) const
  101. {
  102. t.on_fork_success(exec);
  103. }
  104. };
  105. template<typename Executor> on_setup_t <Executor> call_on_setup (Executor & exec) {return exec;}
  106. template<typename Executor> on_error_t <Executor> call_on_error (Executor & exec, const std::error_code & ec)
  107. {
  108. return on_error_t<Executor> (exec, ec);
  109. }
  110. template<typename Executor> on_success_t<Executor> call_on_success(Executor & exec) {return exec;}
  111. template<typename Executor> on_fork_error_t <Executor> call_on_fork_error (Executor & exec, const std::error_code & ec)
  112. {
  113. return on_fork_error_t<Executor> (exec, ec);
  114. }
  115. template<typename Executor> on_exec_setup_t <Executor> call_on_exec_setup (Executor & exec) {return exec;}
  116. template<typename Executor> on_exec_error_t <Executor> call_on_exec_error (Executor & exec, const std::error_code & ec)
  117. {
  118. return on_exec_error_t<Executor> (exec, ec);
  119. }
  120. template<typename Sequence>
  121. class executor
  122. {
  123. template<typename HasHandler, typename UseVFork>
  124. void internal_error_handle(const std::error_code&, const char*, HasHandler, boost::mpl::true_, UseVFork) {}
  125. int _pipe_sink = -1;
  126. void write_error(const std::error_code & ec, const char * msg)
  127. {
  128. //I am the child
  129. const auto len = static_cast<int>(std::strlen(msg));
  130. int data[2] = {ec.value(), len + 1};
  131. boost::ignore_unused(::write(_pipe_sink, &data[0], sizeof(int) * 2));
  132. boost::ignore_unused(::write(_pipe_sink, msg, len));
  133. }
  134. void internal_error_handle(const std::error_code &ec, const char* msg, boost::mpl::true_ , boost::mpl::false_, boost::mpl::false_)
  135. {
  136. if (this->pid == 0) //on the fork.
  137. write_error(ec, msg);
  138. else
  139. {
  140. this->_ec = ec;
  141. this->_msg = msg;
  142. }
  143. }
  144. void internal_error_handle(const std::error_code &ec, const char* msg, boost::mpl::false_, boost::mpl::false_, boost::mpl::false_)
  145. {
  146. if (this->pid == 0)
  147. write_error(ec, msg);
  148. else
  149. throw process_error(ec, msg);
  150. }
  151. void internal_error_handle(const std::error_code &ec, const char* msg, boost::mpl::true_ , boost::mpl::false_, boost::mpl::true_)
  152. {
  153. this->_ec = ec;
  154. this->_msg = msg;
  155. }
  156. void internal_error_handle(const std::error_code &ec, const char* msg, boost::mpl::false_, boost::mpl::false_, boost::mpl::true_)
  157. {
  158. if (this->pid == 0)
  159. {
  160. this->_ec = ec;
  161. this->_msg = msg;
  162. }
  163. else
  164. throw process_error(ec, msg);
  165. }
  166. void check_error(boost::mpl::true_) {};
  167. void check_error(boost::mpl::false_)
  168. {
  169. if (_ec)
  170. throw process_error(_ec, _msg);
  171. }
  172. typedef typename ::boost::process::v1::detail::has_error_handler<Sequence>::type has_error_handler;
  173. typedef typename ::boost::process::v1::detail::has_ignore_error <Sequence>::type has_ignore_error;
  174. typedef typename ::boost::process::v1::detail::posix::shall_use_vfork<Sequence>::type shall_use_vfork;
  175. inline child invoke(boost::mpl::true_ , boost::mpl::true_ );
  176. inline child invoke(boost::mpl::false_, boost::mpl::true_ );
  177. inline child invoke(boost::mpl::true_ , boost::mpl::false_ );
  178. inline child invoke(boost::mpl::false_, boost::mpl::false_ );
  179. void _write_error(int sink)
  180. {
  181. int data[2] = {_ec.value(),static_cast<int>(_msg.size())};
  182. while (::write(sink, &data[0], sizeof(int) *2) == -1)
  183. {
  184. auto err = errno;
  185. if (err == EBADF)
  186. return;
  187. else if ((err != EINTR) && (err != EAGAIN))
  188. break;
  189. }
  190. while (::write(sink, &_msg.front(), _msg.size()) == -1)
  191. {
  192. auto err = errno;
  193. if (err == EBADF)
  194. return;
  195. else if ((err != EINTR) && (err != EAGAIN))
  196. break;
  197. }
  198. }
  199. void _read_error(int source)
  200. {
  201. int data[2];
  202. _ec.clear();
  203. int count = 0;
  204. while ((count = ::read(source, &data[0], sizeof(int) *2 ) ) == -1)
  205. {
  206. //actually, this should block until it's read.
  207. auto err = errno;
  208. if ((err != EAGAIN ) && (err != EINTR))
  209. set_error(std::error_code(err, std::system_category()), "Error read pipe");
  210. }
  211. if (count == 0)
  212. return ;
  213. std::error_code ec(data[0], std::system_category());
  214. std::string msg(data[1], ' ');
  215. while (::read(source, &msg.front(), msg.size() ) == -1)
  216. {
  217. //actually, this should block until it's read.
  218. auto err = errno;
  219. if ((err == EBADF) || (err == EPERM))//that should occur on success, therefore return.
  220. return;
  221. //EAGAIN not yet forked, EINTR interrupted, i.e. try again
  222. else if ((err != EAGAIN ) && (err != EINTR))
  223. set_error(std::error_code(err, std::system_category()), "Error read pipe");
  224. }
  225. set_error(ec, std::move(msg));
  226. }
  227. std::string prepare_cmd_style_fn; //buffer
  228. inline void prepare_cmd_style() //this does what execvpe does - but we execute it in the father process, to avoid allocations.
  229. {
  230. //use my own implementation
  231. prepare_cmd_style_fn = exe;
  232. if ((prepare_cmd_style_fn.find('/') == std::string::npos) && ::access(prepare_cmd_style_fn.c_str(), X_OK))
  233. {
  234. const auto * e = ::environ;
  235. while ((e != nullptr) && (*e != nullptr) && !boost::starts_with(*e, "PATH="))
  236. e++;
  237. if ((e != nullptr) && (*e != nullptr))
  238. {
  239. std::vector<std::string> path;
  240. //the beginning of the string contains "PATH="
  241. boost::split(path, (*e) + 5, boost::is_any_of(":"));
  242. for (const std::string & pp : path)
  243. {
  244. auto p = pp + "/" + exe;
  245. if (!::access(p.c_str(), X_OK))
  246. {
  247. prepare_cmd_style_fn = p;
  248. break;
  249. }
  250. }
  251. }
  252. }
  253. exe = prepare_cmd_style_fn.c_str();
  254. }
  255. std::error_code _ec;
  256. std::string _msg;
  257. public:
  258. executor(Sequence & seq) : seq(seq)
  259. {
  260. }
  261. child operator()()
  262. {
  263. return invoke(has_ignore_error(), shall_use_vfork());
  264. }
  265. Sequence & seq;
  266. const char * exe = nullptr;
  267. char *const* cmd_line = nullptr;
  268. bool cmd_style = false;
  269. char **env = ::environ;
  270. pid_t pid = -1;
  271. std::shared_ptr<std::atomic<int>> exit_status = std::make_shared<std::atomic<int>>(still_active);
  272. const std::error_code & error() const {return _ec;}
  273. void set_error(const std::error_code &ec, const char* msg)
  274. {
  275. internal_error_handle(ec, msg, has_error_handler(), has_ignore_error(), shall_use_vfork());
  276. }
  277. void set_error(const std::error_code &ec, const std::string &msg) {set_error(ec, msg.c_str());};
  278. std::vector<int> get_used_handles() const
  279. {
  280. if (_pipe_sink == -1)
  281. return {};
  282. else
  283. return {_pipe_sink};
  284. };
  285. };
  286. template<typename Sequence>
  287. child executor<Sequence>::invoke(boost::mpl::true_, boost::mpl::false_) //ignore errors
  288. {
  289. boost::fusion::for_each(seq, call_on_setup(*this));
  290. if (_ec)
  291. return child();
  292. if (cmd_style)
  293. prepare_cmd_style();
  294. this->pid = ::fork();
  295. if (pid == -1)
  296. {
  297. auto ec = boost::process::v1::detail::get_last_error();
  298. boost::fusion::for_each(seq, call_on_fork_error(*this, ec));
  299. return child();
  300. }
  301. else if (pid == 0)
  302. {
  303. boost::fusion::for_each(seq, call_on_exec_setup(*this));
  304. ::execve(exe, cmd_line, env);
  305. auto ec = boost::process::v1::detail::get_last_error();
  306. boost::fusion::for_each(seq, call_on_exec_error(*this, ec));
  307. _exit(EXIT_FAILURE);
  308. }
  309. child c(child_handle(pid), exit_status);
  310. boost::fusion::for_each(seq, call_on_success(*this));
  311. return c;
  312. }
  313. template<typename Sequence>
  314. child executor<Sequence>::invoke(boost::mpl::false_, boost::mpl::false_)
  315. {
  316. {
  317. struct pipe_guard
  318. {
  319. int p[2];
  320. pipe_guard() : p{-1,-1} {}
  321. ~pipe_guard()
  322. {
  323. if (p[0] != -1)
  324. ::close(p[0]);
  325. if (p[1] != -1)
  326. ::close(p[1]);
  327. }
  328. } p{};
  329. if (::pipe(p.p) == -1)
  330. {
  331. set_error(::boost::process::v1::detail::get_last_error(), "pipe(2) failed");
  332. return child();
  333. }
  334. if (::fcntl(p.p[1], F_SETFD, FD_CLOEXEC) == -1)
  335. {
  336. auto err = ::boost::process::v1::detail::get_last_error();
  337. set_error(err, "fcntl(2) failed");//this might throw, so we need to be sure our pipe is safe.
  338. return child();
  339. }
  340. _pipe_sink = p.p[1];
  341. _ec.clear();
  342. boost::fusion::for_each(seq, call_on_setup(*this));
  343. if (_ec)
  344. {
  345. boost::fusion::for_each(seq, call_on_error(*this, _ec));
  346. _pipe_sink = -1;
  347. return child();
  348. }
  349. if (cmd_style)
  350. prepare_cmd_style();
  351. this->pid = ::fork();
  352. if (pid == -1)
  353. {
  354. _ec = boost::process::v1::detail::get_last_error();
  355. _msg = "fork() failed";
  356. boost::fusion::for_each(seq, call_on_fork_error(*this, _ec));
  357. boost::fusion::for_each(seq, call_on_error(*this, _ec));
  358. _pipe_sink = -1;
  359. return child();
  360. }
  361. else if (pid == 0)
  362. {
  363. ::close(p.p[0]);
  364. boost::fusion::for_each(seq, call_on_exec_setup(*this));
  365. ::execve(exe, cmd_line, env);
  366. _ec = boost::process::v1::detail::get_last_error();
  367. _msg = "execve failed";
  368. boost::fusion::for_each(seq, call_on_exec_error(*this, _ec));
  369. _write_error(_pipe_sink);
  370. ::close(p.p[1]);
  371. _exit(EXIT_FAILURE);
  372. return child();
  373. }
  374. ::close(p.p[1]);
  375. p.p[1] = -1;
  376. _pipe_sink = -1;
  377. _read_error(p.p[0]);
  378. }
  379. if (_ec)
  380. {
  381. //if an error occurred we need to reap the child process
  382. ::waitpid(this->pid, nullptr, WNOHANG);
  383. boost::fusion::for_each(seq, call_on_error(*this, _ec));
  384. return child();
  385. }
  386. child c(child_handle(pid), exit_status);
  387. boost::fusion::for_each(seq, call_on_success(*this));
  388. if (_ec)
  389. {
  390. boost::fusion::for_each(seq, call_on_error(*this, _ec));
  391. return child();
  392. }
  393. return c;
  394. }
  395. #if BOOST_POSIX_HAS_VFORK
  396. template<typename Sequence>
  397. child executor<Sequence>::invoke(boost::mpl::true_, boost::mpl::true_) //ignore errors
  398. {
  399. boost::fusion::for_each(seq, call_on_setup(*this));
  400. if (_ec)
  401. return child();
  402. this->pid = ::vfork();
  403. if (pid == -1)
  404. {
  405. auto ec = boost::process::v1::detail::get_last_error();
  406. boost::fusion::for_each(seq, call_on_fork_error(*this, ec));
  407. return child();
  408. }
  409. else if (pid == 0)
  410. {
  411. boost::fusion::for_each(seq, call_on_exec_setup(*this));
  412. ::execve(exe, cmd_line, env);
  413. auto ec = boost::process::v1::detail::get_last_error();
  414. boost::fusion::for_each(seq, call_on_exec_error(*this, ec));
  415. _exit(EXIT_FAILURE);
  416. }
  417. child c(child_handle(pid), exit_status);
  418. boost::fusion::for_each(seq, call_on_success(*this));
  419. return c;
  420. }
  421. template<typename Sequence>
  422. child executor<Sequence>::invoke(boost::mpl::false_, boost::mpl::true_)
  423. {
  424. boost::fusion::for_each(seq, call_on_setup(*this));
  425. if (_ec)
  426. {
  427. boost::fusion::for_each(seq, call_on_error(*this, _ec));
  428. return child();
  429. }
  430. _ec.clear();
  431. if (cmd_style)
  432. this->prepare_cmd_style();
  433. this->pid = ::vfork();
  434. if (pid == -1)
  435. {
  436. _ec = boost::process::v1::detail::get_last_error();
  437. _msg = "fork() failed";
  438. boost::fusion::for_each(seq, call_on_fork_error(*this, _ec));
  439. boost::fusion::for_each(seq, call_on_error(*this, _ec));
  440. return child();
  441. }
  442. else if (pid == 0)
  443. {
  444. boost::fusion::for_each(seq, call_on_exec_setup(*this));
  445. ::execve(exe, cmd_line, env);
  446. _ec = boost::process::v1::detail::get_last_error();
  447. _msg = "execve failed";
  448. boost::fusion::for_each(seq, call_on_exec_error(*this, _ec));
  449. _exit(EXIT_FAILURE);
  450. return child();
  451. }
  452. child c(child_handle(pid), exit_status);
  453. check_error(has_error_handler());
  454. if (_ec)
  455. {
  456. ::waitpid(this->pid, nullptr, WNOHANG);
  457. boost::fusion::for_each(seq, call_on_error(*this, _ec));
  458. return child();
  459. }
  460. else
  461. boost::fusion::for_each(seq, call_on_success(*this));
  462. if (_ec)
  463. {
  464. boost::fusion::for_each(seq, call_on_error(*this, _ec));
  465. return child();
  466. }
  467. return c;
  468. }
  469. #endif
  470. template<typename Char, typename Tup>
  471. inline executor<Tup> make_executor(Tup & tup)
  472. {
  473. return executor<Tup>(tup);
  474. }
  475. }}}}}
  476. #endif