1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
// Copyright 2015 The Kyua Authors.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of Google Inc. nor the names of its contributors
// may be used to endorse or promote products derived from this software
// without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#if !defined(UTILS_PROCESS_EXECUTOR_IPP)
#define UTILS_PROCESS_EXECUTOR_IPP
#include "utils/process/executor.hpp"
#include "utils/fs/path.hpp"
#include "utils/optional.ipp"
#include "utils/passwd.hpp"
#include "utils/process/child.ipp"
namespace utils {
namespace process {
namespace executor {
namespace detail {
/// Functor to execute a hook in a child process.
///
/// The hook is executed after the process has been isolated per the logic in
/// utils::process::isolation based on the input parameters at construction
/// time.
template< class Hook >
class run_child {
/// Function or functor to invoke in the child.
Hook _hook;
/// Directory where the hook may place control files.
const fs::path& _control_directory;
/// Directory to enter when running the subprocess.
///
/// This is a subdirectory of _control_directory but is separate so that
/// subprocess operations do not inadvertently affect our files.
const fs::path& _work_directory;
/// User to switch to when running the subprocess.
///
/// If not none, the subprocess will be executed as the provided user and
/// the control and work directories will be writable by this user.
const optional< passwd::user > _unprivileged_user;
public:
/// Constructor.
///
/// \param hook Function or functor to invoke in the child.
/// \param control_directory Directory where control files can be placed.
/// \param work_directory Directory to enter when running the subprocess.
/// \param unprivileged_user If set, user to switch to before execution.
run_child(Hook hook,
const fs::path& control_directory,
const fs::path& work_directory,
const optional< passwd::user > unprivileged_user) :
_hook(hook),
_control_directory(control_directory),
_work_directory(work_directory),
_unprivileged_user(unprivileged_user)
{
}
/// Body of the subprocess.
void
operator()(void)
{
executor::detail::setup_child(_unprivileged_user,
_control_directory, _work_directory);
_hook(_control_directory);
}
};
} // namespace detail
} // namespace executor
/// Forks and executes a subprocess asynchronously.
///
/// \tparam Hook Type of the hook.
/// \param hook Function or functor to run in the subprocess.
/// \param timeout Maximum amount of time the subprocess can run for.
/// \param unprivileged_user If not none, user to switch to before execution.
/// \param stdout_target If not none, file to which to write the stdout of the
/// test case.
/// \param stderr_target If not none, file to which to write the stderr of the
/// test case.
///
/// \return A handle for the background operation. Used to match the result of
/// the execution returned by wait_any() with this invocation.
template< class Hook >
executor::exec_handle
executor::executor_handle::spawn(
Hook hook,
const datetime::delta& timeout,
const optional< passwd::user > unprivileged_user,
const optional< fs::path > stdout_target,
const optional< fs::path > stderr_target)
{
const fs::path unique_work_directory = spawn_pre();
const fs::path stdout_path = stdout_target ?
stdout_target.get() : (unique_work_directory / detail::stdout_name);
const fs::path stderr_path = stderr_target ?
stderr_target.get() : (unique_work_directory / detail::stderr_name);
std::auto_ptr< process::child > child = process::child::fork_files(
detail::run_child< Hook >(hook,
unique_work_directory,
unique_work_directory / detail::work_subdir,
unprivileged_user),
stdout_path, stderr_path);
return spawn_post(unique_work_directory, stdout_path, stderr_path,
timeout, unprivileged_user, child);
}
/// Forks and executes a subprocess asynchronously in the context of another.
///
/// By context we understand the on-disk state of a previously-executed process,
/// thus the new subprocess spawned by this function will run with the same
/// control and work directories as another process.
///
/// \tparam Hook Type of the hook.
/// \param hook Function or functor to run in the subprocess.
/// \param base Context of the subprocess in which to run this one. The
/// exit_handle provided here must remain alive throughout the existence of
/// this other object because the original exit_handle is the one that owns
/// the on-disk state.
/// \param timeout Maximum amount of time the subprocess can run for.
///
/// \return A handle for the background operation. Used to match the result of
/// the execution returned by wait_any() with this invocation.
template< class Hook >
executor::exec_handle
executor::executor_handle::spawn_followup(Hook hook,
const exit_handle& base,
const datetime::delta& timeout)
{
spawn_followup_pre();
std::auto_ptr< process::child > child = process::child::fork_files(
detail::run_child< Hook >(hook,
base.control_directory(),
base.work_directory(),
base.unprivileged_user()),
base.stdout_file(), base.stderr_file());
return spawn_followup_post(base, timeout, child);
}
} // namespace process
} // namespace utils
#endif // !defined(UTILS_PROCESS_EXECUTOR_IPP)
|