executor.hpp 15 KB

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