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