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
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)
10 #ifndef BOOST_PROCESS_DETAIL_POSIX_EXECUTOR_HPP
11 #define BOOST_PROCESS_DETAIL_POSIX_EXECUTOR_HPP
13 #include <boost/process/detail/child_decl.hpp>
14 #include <boost/process/error.hpp>
15 #include <boost/process/pipe.hpp>
16 #include <boost/process/detail/posix/basic_pipe.hpp>
17 #include <boost/process/detail/posix/use_vfork.hpp>
18 #include <boost/fusion/algorithm/iteration/for_each.hpp>
20 #include <sys/types.h>
25 #if !defined(__GLIBC__)
26 #include <boost/algorithm/string/predicate.hpp>
27 #include <boost/algorithm/string/split.hpp>
28 #include <boost/algorithm/string/classification.hpp>
31 namespace boost { namespace process { namespace detail { namespace posix {
33 inline int execvpe(const char* filename, char * const arg_list[], char* env[])
35 #if defined(__GLIBC__)
36 return ::execvpe(filename, arg_list, env);
38 //use my own implementation
39 std::string fn = filename;
40 if ((fn.find('/') == std::string::npos) && ::access(fn.c_str(), X_OK))
43 while ((*e != nullptr) && !boost::starts_with(*e, "PATH="))
48 std::vector<std::string> path;
49 boost::split(path, *e, boost::is_any_of(":"));
51 for (const std::string & pp : path)
53 auto p = pp + "/" + filename;
54 if (!::access(p.c_str(), X_OK))
62 return ::execve(fn.c_str(), arg_list, env);
66 template<typename Executor>
70 on_setup_t(Executor & exec) : exec(exec) {};
72 void operator()(T & t) const
79 template<typename Executor>
83 const std::error_code & error;
84 on_error_t(Executor & exec, const std::error_code & error) : exec(exec), error(error) {};
86 void operator()(T & t) const
88 t.on_error(exec, error);
92 template<typename Executor>
96 on_success_t(Executor & exec) : exec(exec) {};
98 void operator()(T & t) const {t.on_success(exec);}
103 template<typename Executor>
104 struct on_fork_error_t
107 const std::error_code & error;
108 on_fork_error_t(Executor & exec, const std::error_code & error) : exec(exec), error(error) {};
111 void operator()(T & t) const
113 t.on_fork_error(exec, error);
118 template<typename Executor>
119 struct on_exec_setup_t
122 on_exec_setup_t(Executor & exec) : exec(exec) {};
125 void operator()(T & t) const
127 t.on_exec_setup(exec);
132 template<typename Executor>
133 struct on_exec_error_t
136 const std::error_code &ec;
137 on_exec_error_t(Executor & exec, const std::error_code & error) : exec(exec), ec(error) {};
140 void operator()(T & t) const
142 t.on_exec_error(exec, ec);
146 template<typename Executor>
147 struct on_fork_success_t
150 on_fork_success_t(Executor & exec) : exec(exec) {};
153 void operator()(T & t) const
155 t.on_fork_success(exec);
159 template<typename Executor> on_setup_t <Executor> call_on_setup (Executor & exec) {return exec;}
160 template<typename Executor> on_error_t <Executor> call_on_error (Executor & exec, const std::error_code & ec)
162 return on_error_t<Executor> (exec, ec);
164 template<typename Executor> on_success_t<Executor> call_on_success(Executor & exec) {return exec;}
166 template<typename Executor> on_fork_error_t <Executor> call_on_fork_error (Executor & exec, const std::error_code & ec)
168 return on_fork_error_t<Executor> (exec, ec);
172 template<typename Executor> on_exec_setup_t <Executor> call_on_exec_setup (Executor & exec) {return exec;}
173 template<typename Executor> on_exec_error_t <Executor> call_on_exec_error (Executor & exec, const std::error_code & ec)
175 return on_exec_error_t<Executor> (exec, ec);
179 template<typename Sequence>
182 template<typename HasHandler, typename UseVFork>
183 void internal_error_handle(const std::error_code&, const char*, HasHandler, boost::mpl::true_, UseVFork) {}
187 void write_error(const std::error_code & ec, const char * msg)
190 int len = ec.value();
191 ::write(_pipe_sink, &len, sizeof(int));
193 len = std::strlen(msg) + 1;
194 ::write(_pipe_sink, &len, sizeof(int));
195 ::write(_pipe_sink, msg, len);
198 void internal_error_handle(const std::error_code &ec, const char* msg, boost::mpl::true_ , boost::mpl::false_, boost::mpl::false_)
200 if (this->pid == 0) //on the fork.
201 write_error(ec, msg);
208 void internal_error_handle(const std::error_code &ec, const char* msg, boost::mpl::false_, boost::mpl::false_, boost::mpl::false_)
211 write_error(ec, msg);
213 throw process_error(ec, msg);
217 void internal_error_handle(const std::error_code &ec, const char* msg, boost::mpl::true_ , boost::mpl::false_, boost::mpl::true_)
222 void internal_error_handle(const std::error_code &ec, const char* msg, boost::mpl::false_, boost::mpl::false_, boost::mpl::true_)
230 throw process_error(ec, msg);
233 void check_error(boost::mpl::true_) {};
234 void check_error(boost::mpl::false_)
237 throw process_error(_ec, _msg);
240 typedef typename ::boost::process::detail::has_error_handler<Sequence>::type has_error_handler;
241 typedef typename ::boost::process::detail::has_ignore_error <Sequence>::type has_ignore_error;
242 typedef typename ::boost::process::detail::posix::shall_use_vfork<Sequence>::type shall_use_vfork;
244 inline child invoke(boost::mpl::true_ , boost::mpl::true_ );
245 inline child invoke(boost::mpl::false_, boost::mpl::true_ );
246 inline child invoke(boost::mpl::true_ , boost::mpl::false_ );
247 inline child invoke(boost::mpl::false_, boost::mpl::false_ );
248 void _write_error(int sink)
250 int data[2] = {_ec.value(),static_cast<int>(_msg.size())};
251 while (::write(sink, &data[0], sizeof(int) *2) == -1)
257 else if ((err != EINTR) && (err != EAGAIN))
260 while (::write(sink, &_msg.front(), _msg.size()) == -1)
266 else if ((err != EINTR) && (err != EAGAIN))
271 void _read_error(int source)
277 while ((count = ::read(source, &data[0], sizeof(int) *2 ) ) == -1)
279 //actually, this should block until it's read.
281 if ((err != EAGAIN ) && (err != EINTR))
282 set_error(std::error_code(err, std::system_category()), "Error read pipe");
287 std::error_code ec(data[0], std::system_category());
288 std::string msg(data[1], ' ');
290 while (::read(source, &msg.front(), msg.size() ) == -1)
292 //actually, this should block until it's read.
294 if ((err == EBADF) || (err == EPERM))//that should occur on success, therefore return.
296 //EAGAIN not yet forked, EINTR interrupted, i.e. try again
297 else if ((err != EAGAIN ) && (err != EINTR))
298 set_error(std::error_code(err, std::system_category()), "Error read pipe");
300 set_error(ec, std::move(msg));
307 executor(Sequence & seq) : seq(seq)
313 return invoke(has_ignore_error(), shall_use_vfork());
318 const char * exe = nullptr;
319 char *const* cmd_line = nullptr;
320 bool cmd_style = false;
321 char **env = ::environ;
323 std::shared_ptr<std::atomic<int>> exit_status = std::make_shared<std::atomic<int>>(still_active);
325 const std::error_code & error() const {return _ec;}
327 void set_error(const std::error_code &ec, const char* msg)
329 internal_error_handle(ec, msg, has_error_handler(), has_ignore_error(), shall_use_vfork());
331 void set_error(const std::error_code &ec, const std::string &msg) {set_error(ec, msg.c_str());};
335 template<typename Sequence>
336 child executor<Sequence>::invoke(boost::mpl::true_, boost::mpl::false_) //ignore errors
338 boost::fusion::for_each(seq, call_on_setup(*this));
342 this->pid = ::fork();
345 auto ec = boost::process::detail::get_last_error();
346 boost::fusion::for_each(seq, call_on_fork_error(*this, ec));
351 boost::fusion::for_each(seq, call_on_exec_setup(*this));
353 ::boost::process::detail::posix::execvpe(exe, cmd_line, env);
355 ::execve(exe, cmd_line, env);
356 auto ec = boost::process::detail::get_last_error();
357 boost::fusion::for_each(seq, call_on_exec_error(*this, ec));
361 child c(child_handle(pid), exit_status);
363 boost::fusion::for_each(seq, call_on_success(*this));
368 template<typename Sequence>
369 child executor<Sequence>::invoke(boost::mpl::false_, boost::mpl::false_)
374 set_error(::boost::process::detail::get_last_error(), "pipe(2) failed");
377 if (::fcntl(p[1], F_SETFD, FD_CLOEXEC) == -1)
379 set_error(::boost::process::detail::get_last_error(), "fcntl(2) failed");
383 boost::fusion::for_each(seq, call_on_setup(*this));
387 boost::fusion::for_each(seq, call_on_error(*this, _ec));
391 this->pid = ::fork();
394 _ec = boost::process::detail::get_last_error();
395 _msg = "fork() failed";
396 boost::fusion::for_each(seq, call_on_fork_error(*this, _ec));
397 boost::fusion::for_each(seq, call_on_error(*this, _ec));
406 boost::fusion::for_each(seq, call_on_exec_setup(*this));
408 ::boost::process::detail::posix::execvpe(exe, cmd_line, env);
410 ::execve(exe, cmd_line, env);
411 _ec = boost::process::detail::get_last_error();
412 _msg = "execve failed";
413 boost::fusion::for_each(seq, call_on_exec_error(*this, _ec));
421 child c(child_handle(pid), exit_status);
431 boost::fusion::for_each(seq, call_on_error(*this, _ec));
435 boost::fusion::for_each(seq, call_on_success(*this));
439 boost::fusion::for_each(seq, call_on_error(*this, _ec));
446 #if BOOST_POSIX_HAS_VFORK
449 template<typename Sequence>
450 child executor<Sequence>::invoke(boost::mpl::true_, boost::mpl::true_) //ignore errors
452 boost::fusion::for_each(seq, call_on_setup(*this));
455 this->pid = ::vfork();
458 auto ec = boost::process::detail::get_last_error();
459 boost::fusion::for_each(seq, call_on_fork_error(*this, ec));
464 boost::fusion::for_each(seq, call_on_exec_setup(*this));
465 ::execve(exe, cmd_line, env);
466 auto ec = boost::process::detail::get_last_error();
467 boost::fusion::for_each(seq, call_on_exec_error(*this, ec));
470 child c(child_handle(pid), exit_status);
472 boost::fusion::for_each(seq, call_on_success(*this));
477 template<typename Sequence>
478 child executor<Sequence>::invoke(boost::mpl::false_, boost::mpl::true_)
480 boost::fusion::for_each(seq, call_on_setup(*this));
484 boost::fusion::for_each(seq, call_on_error(*this, _ec));
489 this->pid = ::vfork();
492 _ec = boost::process::detail::get_last_error();
493 _msg = "fork() failed";
494 boost::fusion::for_each(seq, call_on_fork_error(*this, _ec));
495 boost::fusion::for_each(seq, call_on_error(*this, _ec));
501 boost::fusion::for_each(seq, call_on_exec_setup(*this));
504 ::boost::process::detail::posix::execvpe(exe, cmd_line, env);
506 ::execve(exe, cmd_line, env);
508 _ec = boost::process::detail::get_last_error();
509 _msg = "execve failed";
510 boost::fusion::for_each(seq, call_on_exec_error(*this, _ec));
515 child c(child_handle(pid), exit_status);
517 check_error(has_error_handler());
523 boost::fusion::for_each(seq, call_on_error(*this, _ec));
527 boost::fusion::for_each(seq, call_on_success(*this));
531 boost::fusion::for_each(seq, call_on_error(*this, _ec));
540 template<typename Char, typename Tup>
541 inline executor<Tup> make_executor(Tup & tup)
543 return executor<Tup>(tup);