executor.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  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/detail/child_decl.hpp>
  12. #include <boost/process/error.hpp>
  13. #include <boost/process/pipe.hpp>
  14. #include <boost/process/detail/posix/basic_pipe.hpp>
  15. #include <boost/process/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. namespace boost { namespace process { namespace detail { namespace posix {
  26. template<typename Executor>
  27. struct on_setup_t
  28. {
  29. Executor & exec;
  30. on_setup_t(Executor & exec) : exec(exec) {};
  31. template<typename T>
  32. void operator()(T & t) const
  33. {
  34. if (!exec.error())
  35. t.on_setup(exec);
  36. }
  37. };
  38. template<typename Executor>
  39. struct on_error_t
  40. {
  41. Executor & exec;
  42. const std::error_code & error;
  43. on_error_t(Executor & exec, const std::error_code & error) : exec(exec), error(error) {};
  44. template<typename T>
  45. void operator()(T & t) const
  46. {
  47. t.on_error(exec, error);
  48. }
  49. };
  50. template<typename Executor>
  51. struct on_success_t
  52. {
  53. Executor & exec;
  54. on_success_t(Executor & exec) : exec(exec) {};
  55. template<typename T>
  56. void operator()(T & t) const {t.on_success(exec);}
  57. };
  58. template<typename Executor>
  59. struct on_fork_error_t
  60. {
  61. Executor & exec;
  62. const std::error_code & error;
  63. on_fork_error_t(Executor & exec, const std::error_code & error) : exec(exec), error(error) {};
  64. template<typename T>
  65. void operator()(T & t) const
  66. {
  67. t.on_fork_error(exec, error);
  68. }
  69. };
  70. template<typename Executor>
  71. struct on_exec_setup_t
  72. {
  73. Executor & exec;
  74. on_exec_setup_t(Executor & exec) : exec(exec) {};
  75. template<typename T>
  76. void operator()(T & t) const
  77. {
  78. t.on_exec_setup(exec);
  79. }
  80. };
  81. template<typename Executor>
  82. struct on_exec_error_t
  83. {
  84. Executor & exec;
  85. const std::error_code &ec;
  86. on_exec_error_t(Executor & exec, const std::error_code & error) : exec(exec), ec(error) {};
  87. template<typename T>
  88. void operator()(T & t) const
  89. {
  90. t.on_exec_error(exec, ec);
  91. }
  92. };
  93. template<typename Executor>
  94. struct on_fork_success_t
  95. {
  96. Executor & exec;
  97. on_fork_success_t(Executor & exec) : exec(exec) {};
  98. template<typename T>
  99. void operator()(T & t) const
  100. {
  101. t.on_fork_success(exec);
  102. }
  103. };
  104. template<typename Executor> on_setup_t <Executor> call_on_setup (Executor & exec) {return exec;}
  105. template<typename Executor> on_error_t <Executor> call_on_error (Executor & exec, const std::error_code & ec)
  106. {
  107. return on_error_t<Executor> (exec, ec);
  108. }
  109. template<typename Executor> on_success_t<Executor> call_on_success(Executor & exec) {return exec;}
  110. template<typename Executor> on_fork_error_t <Executor> call_on_fork_error (Executor & exec, const std::error_code & ec)
  111. {
  112. return on_fork_error_t<Executor> (exec, ec);
  113. }
  114. template<typename Executor> on_exec_setup_t <Executor> call_on_exec_setup (Executor & exec) {return exec;}
  115. template<typename Executor> on_exec_error_t <Executor> call_on_exec_error (Executor & exec, const std::error_code & ec)
  116. {
  117. return on_exec_error_t<Executor> (exec, ec);
  118. }
  119. template<typename Sequence>
  120. class executor
  121. {
  122. template<typename HasHandler, typename UseVFork>
  123. void internal_error_handle(const std::error_code&, const char*, HasHandler, boost::mpl::true_, UseVFork) {}
  124. int _pipe_sink = -1;
  125. void write_error(const std::error_code & ec, const char * msg)
  126. {
  127. //I am the child
  128. int len = ec.value();
  129. ::write(_pipe_sink, &len, sizeof(int));
  130. len = std::strlen(msg) + 1;
  131. ::write(_pipe_sink, &len, sizeof(int));
  132. ::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::detail::has_error_handler<Sequence>::type has_error_handler;
  173. typedef typename ::boost::process::detail::has_ignore_error <Sequence>::type has_ignore_error;
  174. typedef typename ::boost::process::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. 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. boost::split(path, *e, boost::is_any_of(":"));
  241. for (const std::string & pp : path)
  242. {
  243. auto p = pp + "/" + exe;
  244. if (!::access(p.c_str(), X_OK))
  245. {
  246. prepare_cmd_style_fn = p;
  247. break;
  248. }
  249. }
  250. }
  251. }
  252. exe = prepare_cmd_style_fn.c_str();
  253. }
  254. std::error_code _ec;
  255. std::string _msg;
  256. public:
  257. executor(Sequence & seq) : seq(seq)
  258. {
  259. }
  260. child operator()()
  261. {
  262. return invoke(has_ignore_error(), shall_use_vfork());
  263. }
  264. Sequence & seq;
  265. const char * exe = nullptr;
  266. char *const* cmd_line = nullptr;
  267. bool cmd_style = false;
  268. char **env = ::environ;
  269. pid_t pid = -1;
  270. std::shared_ptr<std::atomic<int>> exit_status = std::make_shared<std::atomic<int>>(still_active);
  271. const std::error_code & error() const {return _ec;}
  272. void set_error(const std::error_code &ec, const char* msg)
  273. {
  274. internal_error_handle(ec, msg, has_error_handler(), has_ignore_error(), shall_use_vfork());
  275. }
  276. void set_error(const std::error_code &ec, const std::string &msg) {set_error(ec, msg.c_str());};
  277. };
  278. template<typename Sequence>
  279. child executor<Sequence>::invoke(boost::mpl::true_, boost::mpl::false_) //ignore errors
  280. {
  281. boost::fusion::for_each(seq, call_on_setup(*this));
  282. if (_ec)
  283. return child();
  284. if (cmd_style)
  285. prepare_cmd_style();
  286. this->pid = ::fork();
  287. if (pid == -1)
  288. {
  289. auto ec = boost::process::detail::get_last_error();
  290. boost::fusion::for_each(seq, call_on_fork_error(*this, ec));
  291. return child();
  292. }
  293. else if (pid == 0)
  294. {
  295. boost::fusion::for_each(seq, call_on_exec_setup(*this));
  296. ::execve(exe, cmd_line, env);
  297. auto ec = boost::process::detail::get_last_error();
  298. boost::fusion::for_each(seq, call_on_exec_error(*this, ec));
  299. _exit(EXIT_FAILURE);
  300. }
  301. child c(child_handle(pid), exit_status);
  302. boost::fusion::for_each(seq, call_on_success(*this));
  303. return c;
  304. }
  305. template<typename Sequence>
  306. child executor<Sequence>::invoke(boost::mpl::false_, boost::mpl::false_)
  307. {
  308. {
  309. struct pipe_guard
  310. {
  311. int p[2];
  312. pipe_guard() : p{-1,-1} {}
  313. ~pipe_guard()
  314. {
  315. if (p[0] != -1)
  316. ::close(p[0]);
  317. if (p[1] != -1)
  318. ::close(p[1]);
  319. }
  320. } p{};
  321. if (::pipe(p.p) == -1)
  322. {
  323. set_error(::boost::process::detail::get_last_error(), "pipe(2) failed");
  324. return child();
  325. }
  326. if (::fcntl(p.p[1], F_SETFD, FD_CLOEXEC) == -1)
  327. {
  328. auto err = ::boost::process::detail::get_last_error();
  329. set_error(err, "fcntl(2) failed");//this might throw, so we need to be sure our pipe is safe.
  330. return child();
  331. }
  332. _ec.clear();
  333. boost::fusion::for_each(seq, call_on_setup(*this));
  334. if (_ec)
  335. {
  336. boost::fusion::for_each(seq, call_on_error(*this, _ec));
  337. return child();
  338. }
  339. if (cmd_style)
  340. prepare_cmd_style();
  341. this->pid = ::fork();
  342. if (pid == -1)
  343. {
  344. _ec = boost::process::detail::get_last_error();
  345. _msg = "fork() failed";
  346. boost::fusion::for_each(seq, call_on_fork_error(*this, _ec));
  347. boost::fusion::for_each(seq, call_on_error(*this, _ec));
  348. return child();
  349. }
  350. else if (pid == 0)
  351. {
  352. _pipe_sink = p.p[1];
  353. ::close(p.p[0]);
  354. boost::fusion::for_each(seq, call_on_exec_setup(*this));
  355. ::execve(exe, cmd_line, env);
  356. _ec = boost::process::detail::get_last_error();
  357. _msg = "execve failed";
  358. boost::fusion::for_each(seq, call_on_exec_error(*this, _ec));
  359. _write_error(_pipe_sink);
  360. ::close(p.p[1]);
  361. _exit(EXIT_FAILURE);
  362. return child();
  363. }
  364. ::close(p.p[1]);
  365. p.p[1] = -1;
  366. _read_error(p.p[0]);
  367. }
  368. if (_ec)
  369. {
  370. boost::fusion::for_each(seq, call_on_error(*this, _ec));
  371. return child();
  372. }
  373. child c(child_handle(pid), exit_status);
  374. boost::fusion::for_each(seq, call_on_success(*this));
  375. if (_ec)
  376. {
  377. boost::fusion::for_each(seq, call_on_error(*this, _ec));
  378. return child();
  379. }
  380. return c;
  381. }
  382. #if BOOST_POSIX_HAS_VFORK
  383. template<typename Sequence>
  384. child executor<Sequence>::invoke(boost::mpl::true_, boost::mpl::true_) //ignore errors
  385. {
  386. boost::fusion::for_each(seq, call_on_setup(*this));
  387. if (_ec)
  388. return child();
  389. this->pid = ::vfork();
  390. if (pid == -1)
  391. {
  392. auto ec = boost::process::detail::get_last_error();
  393. boost::fusion::for_each(seq, call_on_fork_error(*this, ec));
  394. return child();
  395. }
  396. else if (pid == 0)
  397. {
  398. boost::fusion::for_each(seq, call_on_exec_setup(*this));
  399. ::execve(exe, cmd_line, env);
  400. auto ec = boost::process::detail::get_last_error();
  401. boost::fusion::for_each(seq, call_on_exec_error(*this, ec));
  402. _exit(EXIT_FAILURE);
  403. }
  404. child c(child_handle(pid), exit_status);
  405. boost::fusion::for_each(seq, call_on_success(*this));
  406. return c;
  407. }
  408. template<typename Sequence>
  409. child executor<Sequence>::invoke(boost::mpl::false_, boost::mpl::true_)
  410. {
  411. boost::fusion::for_each(seq, call_on_setup(*this));
  412. if (_ec)
  413. {
  414. boost::fusion::for_each(seq, call_on_error(*this, _ec));
  415. return child();
  416. }
  417. _ec.clear();
  418. if (cmd_style)
  419. this->prepare_cmd_style();
  420. this->pid = ::vfork();
  421. if (pid == -1)
  422. {
  423. _ec = boost::process::detail::get_last_error();
  424. _msg = "fork() failed";
  425. boost::fusion::for_each(seq, call_on_fork_error(*this, _ec));
  426. boost::fusion::for_each(seq, call_on_error(*this, _ec));
  427. return child();
  428. }
  429. else if (pid == 0)
  430. {
  431. boost::fusion::for_each(seq, call_on_exec_setup(*this));
  432. ::execve(exe, cmd_line, env);
  433. _ec = boost::process::detail::get_last_error();
  434. _msg = "execve failed";
  435. boost::fusion::for_each(seq, call_on_exec_error(*this, _ec));
  436. _exit(EXIT_FAILURE);
  437. return child();
  438. }
  439. child c(child_handle(pid), exit_status);
  440. check_error(has_error_handler());
  441. if (_ec)
  442. {
  443. boost::fusion::for_each(seq, call_on_error(*this, _ec));
  444. return child();
  445. }
  446. else
  447. boost::fusion::for_each(seq, call_on_success(*this));
  448. if (_ec)
  449. {
  450. boost::fusion::for_each(seq, call_on_error(*this, _ec));
  451. return child();
  452. }
  453. return c;
  454. }
  455. #endif
  456. template<typename Char, typename Tup>
  457. inline executor<Tup> make_executor(Tup & tup)
  458. {
  459. return executor<Tup>(tup);
  460. }
  461. }}}}
  462. #endif