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
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
child operator()(const InitializerSequence &seq)
Definition executor.hpp:111