executor.hpp
Go to the documentation of this file.
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 
10 #pragma once
11 
13 #include <boost/fusion/algorithm/iteration/for_each.hpp>
14 #include <cstdlib>
15 #include <sys/types.h>
16 #include <unistd.h>
17 
18 namespace boost::process::posix
19 {
20 
21  struct executor
22  {
23  executor() : exe(0), cmd_line(0), env(0) {}
24 
26  {
28 
30 
31  template <class Arg>
32  void operator()(const Arg& arg) const
33  {
34  arg.on_fork_setup(e_);
35  }
36  };
37 
39  {
41 
43 
44  template <class Arg>
45  void operator()(Arg& arg) const
46  {
47  arg.on_fork_error(e_);
48  }
49  };
50 
52  {
54 
56 
57  template <class Arg>
58  void operator()(Arg& arg) const
59  {
60  arg.on_fork_success(e_);
61  }
62  };
63 
65  {
67 
69 
70  template <class Arg>
71  void operator()(Arg& arg) const
72  {
73  arg.on_exec_setup(e_);
74  }
75  };
76 
78  {
80 
82 
83  template <class Arg>
84  void operator()(Arg& arg) const
85  {
86  arg.on_exec_error(e_);
87  }
88  };
89 
90  template <class InitializerSequence>
91  child operator()(const InitializerSequence& seq)
92  {
93  boost::fusion::for_each(seq, call_on_fork_setup(*this));
94 
95  pid_t pid = ::fork();
96  if (pid == -1)
97  {
98  boost::fusion::for_each(seq, call_on_fork_error(*this));
99  }
100  else if (pid == 0)
101  {
102  boost::fusion::for_each(seq, call_on_exec_setup(*this));
103  ::execve(exe, cmd_line, env);
104  boost::fusion::for_each(seq, call_on_exec_error(*this));
105  _exit(EXIT_FAILURE);
106  }
107 
108  boost::fusion::for_each(seq, call_on_fork_success(*this));
109 
110  return child(pid);
111  }
112 
113  const char* exe;
114  char** cmd_line;
115  char** env;
116  };
117 
118 }
boost::process::posix::executor::call_on_fork_setup::call_on_fork_setup
call_on_fork_setup(executor &e)
Definition: executor.hpp:29
boost::process::posix::child
Definition: child.hpp:16
boost::process::posix::executor::call_on_fork_setup
Definition: executor.hpp:25
boost::process::posix::executor::call_on_exec_error::operator()
void operator()(Arg &arg) const
Definition: executor.hpp:84
boost::process::posix::executor::call_on_fork_success
Definition: executor.hpp:51
boost::process::posix::executor::executor
executor()
Definition: executor.hpp:23
boost::process::posix::executor::call_on_fork_success::e_
executor & e_
Definition: executor.hpp:53
boost::process::posix::executor::call_on_fork_error::operator()
void operator()(Arg &arg) const
Definition: executor.hpp:45
boost::process::posix::executor::exe
const char * exe
Definition: executor.hpp:113
boost::process::posix::executor::operator()
child operator()(const InitializerSequence &seq)
Definition: executor.hpp:91
boost::process::posix::executor::call_on_fork_success::call_on_fork_success
call_on_fork_success(executor &e)
Definition: executor.hpp:55
boost::process::posix::executor::call_on_exec_error
Definition: executor.hpp:77
boost::process::posix::executor::cmd_line
char ** cmd_line
Definition: executor.hpp:114
boost::process::posix::executor::env
char ** env
Definition: executor.hpp:115
boost::process::posix::executor::call_on_fork_setup::e_
executor & e_
Definition: executor.hpp:27
boost::process::posix::executor
Definition: executor.hpp:21
boost::process::posix::executor::call_on_fork_error::call_on_fork_error
call_on_fork_error(executor &e)
Definition: executor.hpp:42
boost::process::posix::executor::call_on_exec_error::call_on_exec_error
call_on_exec_error(executor &e)
Definition: executor.hpp:81
boost::process::posix::executor::call_on_fork_setup::operator()
void operator()(const Arg &arg) const
Definition: executor.hpp:32
boost::process::posix::executor::call_on_exec_setup::operator()
void operator()(Arg &arg) const
Definition: executor.hpp:71
boost::process::posix::executor::call_on_fork_error
Definition: executor.hpp:38
boost::process::posix::executor::call_on_fork_error::e_
executor & e_
Definition: executor.hpp:40
boost::process::posix::executor::call_on_fork_success::operator()
void operator()(Arg &arg) const
Definition: executor.hpp:58
boost::process::posix::executor::call_on_exec_setup::call_on_exec_setup
call_on_exec_setup(executor &e)
Definition: executor.hpp:68
boost::process::posix::executor::call_on_exec_error::e_
executor & e_
Definition: executor.hpp:79
boost::process::posix
Definition: child.hpp:14
boost::process::posix::executor::call_on_exec_setup::e_
executor & e_
Definition: executor.hpp:66
child.hpp
boost::process::posix::executor::call_on_exec_setup
Definition: executor.hpp:64