executor.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  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. {
  256. ::close(source);
  257. set_error(std::error_code(err, std::system_category()), "Error read pipe");
  258. }
  259. }
  260. ::close(source);
  261. set_error(ec, std::move(msg));
  262. }
  263. std::error_code _ec;
  264. std::string _msg;
  265. public:
  266. executor(Sequence & seq) : seq(seq)
  267. {
  268. }
  269. child operator()()
  270. {
  271. return invoke(has_ignore_error(), shall_use_vfork());
  272. }
  273. Sequence & seq;
  274. const char * exe = nullptr;
  275. char *const* cmd_line = nullptr;
  276. bool cmd_style = false;
  277. char **env = ::environ;
  278. pid_t pid = -1;
  279. std::shared_ptr<std::atomic<int>> exit_status = std::make_shared<std::atomic<int>>(still_active);
  280. const std::error_code & error() const {return _ec;}
  281. void set_error(const std::error_code &ec, const char* msg)
  282. {
  283. internal_error_handle(ec, msg, has_error_handler(), has_ignore_error(), shall_use_vfork());
  284. }
  285. void set_error(const std::error_code &ec, const std::string &msg) {set_error(ec, msg.c_str());};
  286. };
  287. template<typename Sequence>
  288. child executor<Sequence>::invoke(boost::mpl::true_, boost::mpl::false_) //ignore errors
  289. {
  290. boost::fusion::for_each(seq, call_on_setup(*this));
  291. if (_ec)
  292. return child();
  293. this->pid = ::fork();
  294. if (pid == -1)
  295. {
  296. auto ec = boost::process::detail::get_last_error();
  297. boost::fusion::for_each(seq, call_on_fork_error(*this, ec));
  298. return child();
  299. }
  300. else if (pid == 0)
  301. {
  302. boost::fusion::for_each(seq, call_on_exec_setup(*this));
  303. if (cmd_style)
  304. ::boost::process::detail::posix::execvpe(exe, cmd_line, env);
  305. else
  306. ::execve(exe, cmd_line, env);
  307. auto ec = boost::process::detail::get_last_error();
  308. boost::fusion::for_each(seq, call_on_exec_error(*this, ec));
  309. _exit(EXIT_FAILURE);
  310. }
  311. child c(child_handle(pid), exit_status);
  312. boost::fusion::for_each(seq, call_on_success(*this));
  313. return c;
  314. }
  315. template<typename Sequence>
  316. child executor<Sequence>::invoke(boost::mpl::false_, boost::mpl::false_)
  317. {
  318. int p[2];
  319. if (::pipe(p) == -1)
  320. {
  321. set_error(::boost::process::detail::get_last_error(), "pipe(2) failed");
  322. return child();
  323. }
  324. if (::fcntl(p[1], F_SETFD, FD_CLOEXEC) == -1)
  325. {
  326. auto err = ::boost::process::detail::get_last_error();
  327. ::close(p[0]);
  328. ::close(p[1]);
  329. set_error(err, "fcntl(2) failed");
  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. this->pid = ::fork();
  340. if (pid == -1)
  341. {
  342. _ec = boost::process::detail::get_last_error();
  343. _msg = "fork() failed";
  344. boost::fusion::for_each(seq, call_on_fork_error(*this, _ec));
  345. boost::fusion::for_each(seq, call_on_error(*this, _ec));
  346. return child();
  347. }
  348. else if (pid == 0)
  349. {
  350. _pipe_sink = p[1];
  351. ::close(p[0]);
  352. boost::fusion::for_each(seq, call_on_exec_setup(*this));
  353. if (cmd_style)
  354. ::boost::process::detail::posix::execvpe(exe, cmd_line, env);
  355. else
  356. ::execve(exe, cmd_line, env);
  357. _ec = boost::process::detail::get_last_error();
  358. _msg = "execve failed";
  359. boost::fusion::for_each(seq, call_on_exec_error(*this, _ec));
  360. _write_error(p[1]);
  361. _exit(EXIT_FAILURE);
  362. return child();
  363. }
  364. child c(child_handle(pid), exit_status);
  365. ::close(p[1]);
  366. _read_error(p[0]);
  367. if (_ec)
  368. {
  369. boost::fusion::for_each(seq, call_on_error(*this, _ec));
  370. return child();
  371. }
  372. else
  373. boost::fusion::for_each(seq, call_on_success(*this));
  374. if (_ec)
  375. {
  376. boost::fusion::for_each(seq, call_on_error(*this, _ec));
  377. return child();
  378. }
  379. return c;
  380. }
  381. #if BOOST_POSIX_HAS_VFORK
  382. template<typename Sequence>
  383. child executor<Sequence>::invoke(boost::mpl::true_, boost::mpl::true_) //ignore errors
  384. {
  385. boost::fusion::for_each(seq, call_on_setup(*this));
  386. if (_ec)
  387. return child();
  388. this->pid = ::vfork();
  389. if (pid == -1)
  390. {
  391. auto ec = boost::process::detail::get_last_error();
  392. boost::fusion::for_each(seq, call_on_fork_error(*this, ec));
  393. return child();
  394. }
  395. else if (pid == 0)
  396. {
  397. boost::fusion::for_each(seq, call_on_exec_setup(*this));
  398. ::execve(exe, cmd_line, env);
  399. auto ec = boost::process::detail::get_last_error();
  400. boost::fusion::for_each(seq, call_on_exec_error(*this, ec));
  401. _exit(EXIT_FAILURE);
  402. }
  403. child c(child_handle(pid), exit_status);
  404. boost::fusion::for_each(seq, call_on_success(*this));
  405. return c;
  406. }
  407. template<typename Sequence>
  408. child executor<Sequence>::invoke(boost::mpl::false_, boost::mpl::true_)
  409. {
  410. boost::fusion::for_each(seq, call_on_setup(*this));
  411. if (_ec)
  412. {
  413. boost::fusion::for_each(seq, call_on_error(*this, _ec));
  414. return child();
  415. }
  416. _ec.clear();
  417. this->pid = ::vfork();
  418. if (pid == -1)
  419. {
  420. _ec = boost::process::detail::get_last_error();
  421. _msg = "fork() failed";
  422. boost::fusion::for_each(seq, call_on_fork_error(*this, _ec));
  423. boost::fusion::for_each(seq, call_on_error(*this, _ec));
  424. return child();
  425. }
  426. else if (pid == 0)
  427. {
  428. boost::fusion::for_each(seq, call_on_exec_setup(*this));
  429. if (cmd_style)
  430. ::boost::process::detail::posix::execvpe(exe, cmd_line, env);
  431. else
  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