1b0d29bc4SBrooks Davis // Copyright 2015 The Kyua Authors.
2b0d29bc4SBrooks Davis // All rights reserved.
3b0d29bc4SBrooks Davis //
4b0d29bc4SBrooks Davis // Redistribution and use in source and binary forms, with or without
5b0d29bc4SBrooks Davis // modification, are permitted provided that the following conditions are
6b0d29bc4SBrooks Davis // met:
7b0d29bc4SBrooks Davis //
8b0d29bc4SBrooks Davis // * Redistributions of source code must retain the above copyright
9b0d29bc4SBrooks Davis // notice, this list of conditions and the following disclaimer.
10b0d29bc4SBrooks Davis // * Redistributions in binary form must reproduce the above copyright
11b0d29bc4SBrooks Davis // notice, this list of conditions and the following disclaimer in the
12b0d29bc4SBrooks Davis // documentation and/or other materials provided with the distribution.
13b0d29bc4SBrooks Davis // * Neither the name of Google Inc. nor the names of its contributors
14b0d29bc4SBrooks Davis // may be used to endorse or promote products derived from this software
15b0d29bc4SBrooks Davis // without specific prior written permission.
16b0d29bc4SBrooks Davis //
17b0d29bc4SBrooks Davis // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18b0d29bc4SBrooks Davis // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19b0d29bc4SBrooks Davis // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20b0d29bc4SBrooks Davis // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21b0d29bc4SBrooks Davis // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22b0d29bc4SBrooks Davis // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23b0d29bc4SBrooks Davis // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24b0d29bc4SBrooks Davis // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25b0d29bc4SBrooks Davis // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26b0d29bc4SBrooks Davis // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27b0d29bc4SBrooks Davis // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28b0d29bc4SBrooks Davis
29b0d29bc4SBrooks Davis #include "utils/process/executor.ipp"
30b0d29bc4SBrooks Davis
31b0d29bc4SBrooks Davis #if defined(HAVE_CONFIG_H)
32b0d29bc4SBrooks Davis #include "config.h"
33b0d29bc4SBrooks Davis #endif
34b0d29bc4SBrooks Davis
35b0d29bc4SBrooks Davis extern "C" {
36b0d29bc4SBrooks Davis #include <sys/types.h>
37b0d29bc4SBrooks Davis #include <sys/wait.h>
38b0d29bc4SBrooks Davis
39b0d29bc4SBrooks Davis #include <signal.h>
40b0d29bc4SBrooks Davis }
41b0d29bc4SBrooks Davis
42b0d29bc4SBrooks Davis #include <fstream>
43b0d29bc4SBrooks Davis #include <map>
44b0d29bc4SBrooks Davis #include <memory>
45b0d29bc4SBrooks Davis #include <stdexcept>
46b0d29bc4SBrooks Davis
47b0d29bc4SBrooks Davis #include "utils/datetime.hpp"
48b0d29bc4SBrooks Davis #include "utils/format/macros.hpp"
49b0d29bc4SBrooks Davis #include "utils/fs/auto_cleaners.hpp"
50b0d29bc4SBrooks Davis #include "utils/fs/exceptions.hpp"
51b0d29bc4SBrooks Davis #include "utils/fs/operations.hpp"
52b0d29bc4SBrooks Davis #include "utils/fs/path.hpp"
53b0d29bc4SBrooks Davis #include "utils/logging/macros.hpp"
54b0d29bc4SBrooks Davis #include "utils/logging/operations.hpp"
55b0d29bc4SBrooks Davis #include "utils/noncopyable.hpp"
56b0d29bc4SBrooks Davis #include "utils/optional.ipp"
57b0d29bc4SBrooks Davis #include "utils/passwd.hpp"
58b0d29bc4SBrooks Davis #include "utils/process/child.ipp"
59b0d29bc4SBrooks Davis #include "utils/process/deadline_killer.hpp"
60b0d29bc4SBrooks Davis #include "utils/process/isolation.hpp"
61b0d29bc4SBrooks Davis #include "utils/process/operations.hpp"
62b0d29bc4SBrooks Davis #include "utils/process/status.hpp"
63b0d29bc4SBrooks Davis #include "utils/sanity.hpp"
64b0d29bc4SBrooks Davis #include "utils/signals/interrupts.hpp"
65b0d29bc4SBrooks Davis #include "utils/signals/timer.hpp"
66b0d29bc4SBrooks Davis
67b0d29bc4SBrooks Davis namespace datetime = utils::datetime;
68b0d29bc4SBrooks Davis namespace executor = utils::process::executor;
69b0d29bc4SBrooks Davis namespace fs = utils::fs;
70b0d29bc4SBrooks Davis namespace logging = utils::logging;
71b0d29bc4SBrooks Davis namespace passwd = utils::passwd;
72b0d29bc4SBrooks Davis namespace process = utils::process;
73b0d29bc4SBrooks Davis namespace signals = utils::signals;
74b0d29bc4SBrooks Davis
75b0d29bc4SBrooks Davis using utils::none;
76b0d29bc4SBrooks Davis using utils::optional;
77b0d29bc4SBrooks Davis
78b0d29bc4SBrooks Davis
79b0d29bc4SBrooks Davis namespace {
80b0d29bc4SBrooks Davis
81b0d29bc4SBrooks Davis
82b0d29bc4SBrooks Davis /// Template for temporary directories created by the executor.
83b0d29bc4SBrooks Davis static const char* work_directory_template = PACKAGE_TARNAME ".XXXXXX";
84b0d29bc4SBrooks Davis
85b0d29bc4SBrooks Davis
86b0d29bc4SBrooks Davis /// Mapping of active subprocess PIDs to their execution data.
87b0d29bc4SBrooks Davis typedef std::map< int, executor::exec_handle > exec_handles_map;
88b0d29bc4SBrooks Davis
89b0d29bc4SBrooks Davis
90b0d29bc4SBrooks Davis } // anonymous namespace
91b0d29bc4SBrooks Davis
92b0d29bc4SBrooks Davis
93b0d29bc4SBrooks Davis /// Basename of the file containing the stdout of the subprocess.
94b0d29bc4SBrooks Davis const char* utils::process::executor::detail::stdout_name = "stdout.txt";
95b0d29bc4SBrooks Davis
96b0d29bc4SBrooks Davis
97b0d29bc4SBrooks Davis /// Basename of the file containing the stderr of the subprocess.
98b0d29bc4SBrooks Davis const char* utils::process::executor::detail::stderr_name = "stderr.txt";
99b0d29bc4SBrooks Davis
100b0d29bc4SBrooks Davis
101b0d29bc4SBrooks Davis /// Basename of the subdirectory in which the subprocess is actually executed.
102b0d29bc4SBrooks Davis ///
103b0d29bc4SBrooks Davis /// This is a subdirectory of the "unique work directory" generated for the
104b0d29bc4SBrooks Davis /// subprocess so that our code can create control files on disk and not
105b0d29bc4SBrooks Davis /// get them clobbered by the subprocess's activity.
106b0d29bc4SBrooks Davis const char* utils::process::executor::detail::work_subdir = "work";
107b0d29bc4SBrooks Davis
108b0d29bc4SBrooks Davis
109b0d29bc4SBrooks Davis /// Prepares a subprocess to run a user-provided hook in a controlled manner.
110b0d29bc4SBrooks Davis ///
111b0d29bc4SBrooks Davis /// \param unprivileged_user User to switch to if not none.
112b0d29bc4SBrooks Davis /// \param control_directory Path to the subprocess-specific control directory.
113b0d29bc4SBrooks Davis /// \param work_directory Path to the subprocess-specific work directory.
114b0d29bc4SBrooks Davis void
setup_child(const optional<passwd::user> unprivileged_user,const fs::path & control_directory,const fs::path & work_directory)115b0d29bc4SBrooks Davis utils::process::executor::detail::setup_child(
116b0d29bc4SBrooks Davis const optional< passwd::user > unprivileged_user,
117b0d29bc4SBrooks Davis const fs::path& control_directory,
118b0d29bc4SBrooks Davis const fs::path& work_directory)
119b0d29bc4SBrooks Davis {
120b0d29bc4SBrooks Davis logging::set_inmemory();
121b0d29bc4SBrooks Davis process::isolate_path(unprivileged_user, control_directory);
122b0d29bc4SBrooks Davis process::isolate_child(unprivileged_user, work_directory);
123b0d29bc4SBrooks Davis }
124b0d29bc4SBrooks Davis
125b0d29bc4SBrooks Davis
126b0d29bc4SBrooks Davis /// Internal implementation for the exit_handle class.
127b0d29bc4SBrooks Davis struct utils::process::executor::exec_handle::impl : utils::noncopyable {
128b0d29bc4SBrooks Davis /// PID of the process being run.
129b0d29bc4SBrooks Davis int pid;
130b0d29bc4SBrooks Davis
131b0d29bc4SBrooks Davis /// Path to the subprocess-specific work directory.
132b0d29bc4SBrooks Davis fs::path control_directory;
133b0d29bc4SBrooks Davis
134b0d29bc4SBrooks Davis /// Path to the subprocess's stdout file.
135b0d29bc4SBrooks Davis const fs::path stdout_file;
136b0d29bc4SBrooks Davis
137b0d29bc4SBrooks Davis /// Path to the subprocess's stderr file.
138b0d29bc4SBrooks Davis const fs::path stderr_file;
139b0d29bc4SBrooks Davis
140b0d29bc4SBrooks Davis /// Start time.
141b0d29bc4SBrooks Davis datetime::timestamp start_time;
142b0d29bc4SBrooks Davis
143b0d29bc4SBrooks Davis /// User the subprocess is running as if different than the current one.
144b0d29bc4SBrooks Davis const optional< passwd::user > unprivileged_user;
145b0d29bc4SBrooks Davis
146b0d29bc4SBrooks Davis /// Timer to kill the subprocess on activation.
147b0d29bc4SBrooks Davis process::deadline_killer timer;
148b0d29bc4SBrooks Davis
149b0d29bc4SBrooks Davis /// Number of owners of the on-disk state.
150b0d29bc4SBrooks Davis executor::detail::refcnt_t state_owners;
151b0d29bc4SBrooks Davis
152b0d29bc4SBrooks Davis /// Constructor.
153b0d29bc4SBrooks Davis ///
154b0d29bc4SBrooks Davis /// \param pid_ PID of the forked process.
155b0d29bc4SBrooks Davis /// \param control_directory_ Path to the subprocess-specific work
156b0d29bc4SBrooks Davis /// directory.
157b0d29bc4SBrooks Davis /// \param stdout_file_ Path to the subprocess's stdout file.
158b0d29bc4SBrooks Davis /// \param stderr_file_ Path to the subprocess's stderr file.
159b0d29bc4SBrooks Davis /// \param start_time_ Timestamp of when this object was constructed.
160b0d29bc4SBrooks Davis /// \param timeout Maximum amount of time the subprocess can run for.
161b0d29bc4SBrooks Davis /// \param unprivileged_user_ User the subprocess is running as if
162b0d29bc4SBrooks Davis /// different than the current one.
163b0d29bc4SBrooks Davis /// \param [in,out] state_owners_ Number of owners of the on-disk state.
164b0d29bc4SBrooks Davis /// For first-time processes, this should be a new counter set to 0;
165b0d29bc4SBrooks Davis /// for followup processes, this should point to the same counter used
166b0d29bc4SBrooks Davis /// by the preceding process.
implutils::process::executor::exec_handle::impl167b0d29bc4SBrooks Davis impl(const int pid_,
168b0d29bc4SBrooks Davis const fs::path& control_directory_,
169b0d29bc4SBrooks Davis const fs::path& stdout_file_,
170b0d29bc4SBrooks Davis const fs::path& stderr_file_,
171b0d29bc4SBrooks Davis const datetime::timestamp& start_time_,
172b0d29bc4SBrooks Davis const datetime::delta& timeout,
173b0d29bc4SBrooks Davis const optional< passwd::user > unprivileged_user_,
174b0d29bc4SBrooks Davis executor::detail::refcnt_t state_owners_) :
175b0d29bc4SBrooks Davis pid(pid_),
176b0d29bc4SBrooks Davis control_directory(control_directory_),
177b0d29bc4SBrooks Davis stdout_file(stdout_file_),
178b0d29bc4SBrooks Davis stderr_file(stderr_file_),
179b0d29bc4SBrooks Davis start_time(start_time_),
180b0d29bc4SBrooks Davis unprivileged_user(unprivileged_user_),
181b0d29bc4SBrooks Davis timer(timeout, pid_),
182b0d29bc4SBrooks Davis state_owners(state_owners_)
183b0d29bc4SBrooks Davis {
184b0d29bc4SBrooks Davis (*state_owners)++;
185b0d29bc4SBrooks Davis POST(*state_owners > 0);
186b0d29bc4SBrooks Davis }
187b0d29bc4SBrooks Davis };
188b0d29bc4SBrooks Davis
189b0d29bc4SBrooks Davis
190b0d29bc4SBrooks Davis /// Constructor.
191b0d29bc4SBrooks Davis ///
192b0d29bc4SBrooks Davis /// \param pimpl Constructed internal implementation.
exec_handle(std::shared_ptr<impl> pimpl)193b0d29bc4SBrooks Davis executor::exec_handle::exec_handle(std::shared_ptr< impl > pimpl) :
194b0d29bc4SBrooks Davis _pimpl(pimpl)
195b0d29bc4SBrooks Davis {
196b0d29bc4SBrooks Davis }
197b0d29bc4SBrooks Davis
198b0d29bc4SBrooks Davis
199b0d29bc4SBrooks Davis /// Destructor.
~exec_handle(void)200b0d29bc4SBrooks Davis executor::exec_handle::~exec_handle(void)
201b0d29bc4SBrooks Davis {
202b0d29bc4SBrooks Davis }
203b0d29bc4SBrooks Davis
204b0d29bc4SBrooks Davis
205b0d29bc4SBrooks Davis /// Returns the PID of the process being run.
206b0d29bc4SBrooks Davis ///
207b0d29bc4SBrooks Davis /// \return A PID.
208b0d29bc4SBrooks Davis int
pid(void) const209b0d29bc4SBrooks Davis executor::exec_handle::pid(void) const
210b0d29bc4SBrooks Davis {
211b0d29bc4SBrooks Davis return _pimpl->pid;
212b0d29bc4SBrooks Davis }
213b0d29bc4SBrooks Davis
214b0d29bc4SBrooks Davis
215b0d29bc4SBrooks Davis /// Returns the path to the subprocess-specific control directory.
216b0d29bc4SBrooks Davis ///
217b0d29bc4SBrooks Davis /// This is where the executor may store control files.
218b0d29bc4SBrooks Davis ///
219b0d29bc4SBrooks Davis /// \return The path to a directory that exists until cleanup() is called.
220b0d29bc4SBrooks Davis fs::path
control_directory(void) const221b0d29bc4SBrooks Davis executor::exec_handle::control_directory(void) const
222b0d29bc4SBrooks Davis {
223b0d29bc4SBrooks Davis return _pimpl->control_directory;
224b0d29bc4SBrooks Davis }
225b0d29bc4SBrooks Davis
226b0d29bc4SBrooks Davis
227b0d29bc4SBrooks Davis /// Returns the path to the subprocess-specific work directory.
228b0d29bc4SBrooks Davis ///
229b0d29bc4SBrooks Davis /// This is guaranteed to be clear of files created by the executor.
230b0d29bc4SBrooks Davis ///
231b0d29bc4SBrooks Davis /// \return The path to a directory that exists until cleanup() is called.
232b0d29bc4SBrooks Davis fs::path
work_directory(void) const233b0d29bc4SBrooks Davis executor::exec_handle::work_directory(void) const
234b0d29bc4SBrooks Davis {
235b0d29bc4SBrooks Davis return _pimpl->control_directory / detail::work_subdir;
236b0d29bc4SBrooks Davis }
237b0d29bc4SBrooks Davis
238b0d29bc4SBrooks Davis
239b0d29bc4SBrooks Davis /// Returns the path to the subprocess's stdout file.
240b0d29bc4SBrooks Davis ///
241b0d29bc4SBrooks Davis /// \return The path to a file that exists until cleanup() is called.
242b0d29bc4SBrooks Davis const fs::path&
stdout_file(void) const243b0d29bc4SBrooks Davis executor::exec_handle::stdout_file(void) const
244b0d29bc4SBrooks Davis {
245b0d29bc4SBrooks Davis return _pimpl->stdout_file;
246b0d29bc4SBrooks Davis }
247b0d29bc4SBrooks Davis
248b0d29bc4SBrooks Davis
249b0d29bc4SBrooks Davis /// Returns the path to the subprocess's stderr file.
250b0d29bc4SBrooks Davis ///
251b0d29bc4SBrooks Davis /// \return The path to a file that exists until cleanup() is called.
252b0d29bc4SBrooks Davis const fs::path&
stderr_file(void) const253b0d29bc4SBrooks Davis executor::exec_handle::stderr_file(void) const
254b0d29bc4SBrooks Davis {
255b0d29bc4SBrooks Davis return _pimpl->stderr_file;
256b0d29bc4SBrooks Davis }
257b0d29bc4SBrooks Davis
258b0d29bc4SBrooks Davis
259b0d29bc4SBrooks Davis /// Internal implementation for the exit_handle class.
260b0d29bc4SBrooks Davis struct utils::process::executor::exit_handle::impl : utils::noncopyable {
261b0d29bc4SBrooks Davis /// Original PID of the terminated subprocess.
262b0d29bc4SBrooks Davis ///
263b0d29bc4SBrooks Davis /// Note that this PID is no longer valid and cannot be used on system
264b0d29bc4SBrooks Davis /// tables!
265b0d29bc4SBrooks Davis const int original_pid;
266b0d29bc4SBrooks Davis
267b0d29bc4SBrooks Davis /// Termination status of the subprocess, or none if it timed out.
268b0d29bc4SBrooks Davis const optional< process::status > status;
269b0d29bc4SBrooks Davis
270b0d29bc4SBrooks Davis /// The user the process ran as, if different than the current one.
271b0d29bc4SBrooks Davis const optional< passwd::user > unprivileged_user;
272b0d29bc4SBrooks Davis
273b0d29bc4SBrooks Davis /// Timestamp of when the subprocess was spawned.
274b0d29bc4SBrooks Davis const datetime::timestamp start_time;
275b0d29bc4SBrooks Davis
276b0d29bc4SBrooks Davis /// Timestamp of when wait() or wait_any() returned this object.
277b0d29bc4SBrooks Davis const datetime::timestamp end_time;
278b0d29bc4SBrooks Davis
279b0d29bc4SBrooks Davis /// Path to the subprocess-specific work directory.
280b0d29bc4SBrooks Davis const fs::path control_directory;
281b0d29bc4SBrooks Davis
282b0d29bc4SBrooks Davis /// Path to the subprocess's stdout file.
283b0d29bc4SBrooks Davis const fs::path stdout_file;
284b0d29bc4SBrooks Davis
285b0d29bc4SBrooks Davis /// Path to the subprocess's stderr file.
286b0d29bc4SBrooks Davis const fs::path stderr_file;
287b0d29bc4SBrooks Davis
288b0d29bc4SBrooks Davis /// Number of owners of the on-disk state.
289b0d29bc4SBrooks Davis ///
290b0d29bc4SBrooks Davis /// This will be 1 if this exit_handle is the last holder of the on-disk
291b0d29bc4SBrooks Davis /// state, in which case cleanup() invocations will wipe the disk state.
292b0d29bc4SBrooks Davis /// For all other cases, this will hold a higher value.
293b0d29bc4SBrooks Davis detail::refcnt_t state_owners;
294b0d29bc4SBrooks Davis
295b0d29bc4SBrooks Davis /// Mutable pointer to the corresponding executor state.
296b0d29bc4SBrooks Davis ///
297b0d29bc4SBrooks Davis /// This object references a member of the executor_handle that yielded this
298b0d29bc4SBrooks Davis /// exit_handle instance. We need this direct access to clean up after
299b0d29bc4SBrooks Davis /// ourselves when the handle is destroyed.
300b0d29bc4SBrooks Davis exec_handles_map& all_exec_handles;
301b0d29bc4SBrooks Davis
302b0d29bc4SBrooks Davis /// Whether the subprocess state has been cleaned yet or not.
303b0d29bc4SBrooks Davis ///
304b0d29bc4SBrooks Davis /// Used to keep track of explicit calls to the public cleanup().
305b0d29bc4SBrooks Davis bool cleaned;
306b0d29bc4SBrooks Davis
307b0d29bc4SBrooks Davis /// Constructor.
308b0d29bc4SBrooks Davis ///
309b0d29bc4SBrooks Davis /// \param original_pid_ Original PID of the terminated subprocess.
310b0d29bc4SBrooks Davis /// \param status_ Termination status of the subprocess, or none if
311b0d29bc4SBrooks Davis /// timed out.
312b0d29bc4SBrooks Davis /// \param unprivileged_user_ The user the process ran as, if different than
313b0d29bc4SBrooks Davis /// the current one.
314b0d29bc4SBrooks Davis /// \param start_time_ Timestamp of when the subprocess was spawned.
315b0d29bc4SBrooks Davis /// \param end_time_ Timestamp of when wait() or wait_any() returned this
316b0d29bc4SBrooks Davis /// object.
317b0d29bc4SBrooks Davis /// \param control_directory_ Path to the subprocess-specific work
318b0d29bc4SBrooks Davis /// directory.
319b0d29bc4SBrooks Davis /// \param stdout_file_ Path to the subprocess's stdout file.
320b0d29bc4SBrooks Davis /// \param stderr_file_ Path to the subprocess's stderr file.
321b0d29bc4SBrooks Davis /// \param [in,out] state_owners_ Number of owners of the on-disk state.
322b0d29bc4SBrooks Davis /// \param [in,out] all_exec_handles_ Global object keeping track of all
323b0d29bc4SBrooks Davis /// active executions for an executor. This is a pointer to a member of
324b0d29bc4SBrooks Davis /// the executor_handle object.
implutils::process::executor::exit_handle::impl325b0d29bc4SBrooks Davis impl(const int original_pid_,
326b0d29bc4SBrooks Davis const optional< process::status > status_,
327b0d29bc4SBrooks Davis const optional< passwd::user > unprivileged_user_,
328b0d29bc4SBrooks Davis const datetime::timestamp& start_time_,
329b0d29bc4SBrooks Davis const datetime::timestamp& end_time_,
330b0d29bc4SBrooks Davis const fs::path& control_directory_,
331b0d29bc4SBrooks Davis const fs::path& stdout_file_,
332b0d29bc4SBrooks Davis const fs::path& stderr_file_,
333b0d29bc4SBrooks Davis detail::refcnt_t state_owners_,
334b0d29bc4SBrooks Davis exec_handles_map& all_exec_handles_) :
335b0d29bc4SBrooks Davis original_pid(original_pid_), status(status_),
336b0d29bc4SBrooks Davis unprivileged_user(unprivileged_user_),
337b0d29bc4SBrooks Davis start_time(start_time_), end_time(end_time_),
338b0d29bc4SBrooks Davis control_directory(control_directory_),
339b0d29bc4SBrooks Davis stdout_file(stdout_file_), stderr_file(stderr_file_),
340b0d29bc4SBrooks Davis state_owners(state_owners_),
341b0d29bc4SBrooks Davis all_exec_handles(all_exec_handles_), cleaned(false)
342b0d29bc4SBrooks Davis {
343b0d29bc4SBrooks Davis }
344b0d29bc4SBrooks Davis
345b0d29bc4SBrooks Davis /// Destructor.
~implutils::process::executor::exit_handle::impl346b0d29bc4SBrooks Davis ~impl(void)
347b0d29bc4SBrooks Davis {
348b0d29bc4SBrooks Davis if (!cleaned) {
349b0d29bc4SBrooks Davis LW(F("Implicitly cleaning up exit_handle for exec_handle %s; "
350b0d29bc4SBrooks Davis "ignoring errors!") % original_pid);
351b0d29bc4SBrooks Davis try {
352b0d29bc4SBrooks Davis cleanup();
353b0d29bc4SBrooks Davis } catch (const std::runtime_error& error) {
354b0d29bc4SBrooks Davis LE(F("Subprocess cleanup failed: %s") % error.what());
355b0d29bc4SBrooks Davis }
356b0d29bc4SBrooks Davis }
357b0d29bc4SBrooks Davis }
358b0d29bc4SBrooks Davis
359b0d29bc4SBrooks Davis /// Cleans up the subprocess on-disk state.
360b0d29bc4SBrooks Davis ///
361b0d29bc4SBrooks Davis /// \throw engine::error If the cleanup fails, especially due to the
362b0d29bc4SBrooks Davis /// inability to remove the work directory.
363b0d29bc4SBrooks Davis void
cleanuputils::process::executor::exit_handle::impl364b0d29bc4SBrooks Davis cleanup(void)
365b0d29bc4SBrooks Davis {
366b0d29bc4SBrooks Davis PRE(*state_owners > 0);
367b0d29bc4SBrooks Davis if (*state_owners == 1) {
368b0d29bc4SBrooks Davis LI(F("Cleaning up exit_handle for exec_handle %s") % original_pid);
369b0d29bc4SBrooks Davis fs::rm_r(control_directory);
370b0d29bc4SBrooks Davis } else {
371b0d29bc4SBrooks Davis LI(F("Not cleaning up exit_handle for exec_handle %s; "
372b0d29bc4SBrooks Davis "%s owners left") % original_pid % (*state_owners - 1));
373b0d29bc4SBrooks Davis }
374b0d29bc4SBrooks Davis // We must decrease our reference only after we have successfully
375b0d29bc4SBrooks Davis // cleaned up the control directory. Otherwise, the rm_r call would
376b0d29bc4SBrooks Davis // throw an exception, which would in turn invoke the implicit cleanup
377b0d29bc4SBrooks Davis // from the destructor, which would make us crash due to an invalid
378b0d29bc4SBrooks Davis // reference count.
379b0d29bc4SBrooks Davis (*state_owners)--;
380b0d29bc4SBrooks Davis // Marking this object as clean here, even if we did not do actually the
381b0d29bc4SBrooks Davis // cleaning above, is fine (albeit a bit confusing). Note that "another
382b0d29bc4SBrooks Davis // owner" refers to a handle for a different PID, so that handle will be
383b0d29bc4SBrooks Davis // the one issuing the cleanup.
384b0d29bc4SBrooks Davis all_exec_handles.erase(original_pid);
385b0d29bc4SBrooks Davis cleaned = true;
386b0d29bc4SBrooks Davis }
387b0d29bc4SBrooks Davis };
388b0d29bc4SBrooks Davis
389b0d29bc4SBrooks Davis
390b0d29bc4SBrooks Davis /// Constructor.
391b0d29bc4SBrooks Davis ///
392b0d29bc4SBrooks Davis /// \param pimpl Constructed internal implementation.
exit_handle(std::shared_ptr<impl> pimpl)393b0d29bc4SBrooks Davis executor::exit_handle::exit_handle(std::shared_ptr< impl > pimpl) :
394b0d29bc4SBrooks Davis _pimpl(pimpl)
395b0d29bc4SBrooks Davis {
396b0d29bc4SBrooks Davis }
397b0d29bc4SBrooks Davis
398b0d29bc4SBrooks Davis
399b0d29bc4SBrooks Davis /// Destructor.
~exit_handle(void)400b0d29bc4SBrooks Davis executor::exit_handle::~exit_handle(void)
401b0d29bc4SBrooks Davis {
402b0d29bc4SBrooks Davis }
403b0d29bc4SBrooks Davis
404b0d29bc4SBrooks Davis
405b0d29bc4SBrooks Davis /// Cleans up the subprocess status.
406b0d29bc4SBrooks Davis ///
407b0d29bc4SBrooks Davis /// This function should be called explicitly as it provides the means to
408b0d29bc4SBrooks Davis /// control any exceptions raised during cleanup. Do not rely on the destructor
409b0d29bc4SBrooks Davis /// to clean things up.
410b0d29bc4SBrooks Davis ///
411b0d29bc4SBrooks Davis /// \throw engine::error If the cleanup fails, especially due to the inability
412b0d29bc4SBrooks Davis /// to remove the work directory.
413b0d29bc4SBrooks Davis void
cleanup(void)414b0d29bc4SBrooks Davis executor::exit_handle::cleanup(void)
415b0d29bc4SBrooks Davis {
416b0d29bc4SBrooks Davis PRE(!_pimpl->cleaned);
417b0d29bc4SBrooks Davis _pimpl->cleanup();
418b0d29bc4SBrooks Davis POST(_pimpl->cleaned);
419b0d29bc4SBrooks Davis }
420b0d29bc4SBrooks Davis
421b0d29bc4SBrooks Davis
422b0d29bc4SBrooks Davis /// Gets the current number of owners of the on-disk data.
423b0d29bc4SBrooks Davis ///
424b0d29bc4SBrooks Davis /// \return A shared reference counter. Even though this function is marked as
425b0d29bc4SBrooks Davis /// const, the return value is intentionally mutable because we need to update
426b0d29bc4SBrooks Davis /// reference counts from different but related processes. This is why this
427b0d29bc4SBrooks Davis /// method is not public.
428b0d29bc4SBrooks Davis std::shared_ptr< std::size_t >
state_owners(void) const429b0d29bc4SBrooks Davis executor::exit_handle::state_owners(void) const
430b0d29bc4SBrooks Davis {
431b0d29bc4SBrooks Davis return _pimpl->state_owners;
432b0d29bc4SBrooks Davis }
433b0d29bc4SBrooks Davis
434b0d29bc4SBrooks Davis
435b0d29bc4SBrooks Davis /// Returns the original PID corresponding to the terminated subprocess.
436b0d29bc4SBrooks Davis ///
437b0d29bc4SBrooks Davis /// \return An exec_handle.
438b0d29bc4SBrooks Davis int
original_pid(void) const439b0d29bc4SBrooks Davis executor::exit_handle::original_pid(void) const
440b0d29bc4SBrooks Davis {
441b0d29bc4SBrooks Davis return _pimpl->original_pid;
442b0d29bc4SBrooks Davis }
443b0d29bc4SBrooks Davis
444b0d29bc4SBrooks Davis
445b0d29bc4SBrooks Davis /// Returns the process termination status of the subprocess.
446b0d29bc4SBrooks Davis ///
447b0d29bc4SBrooks Davis /// \return A process termination status, or none if the subprocess timed out.
448b0d29bc4SBrooks Davis const optional< process::status >&
status(void) const449b0d29bc4SBrooks Davis executor::exit_handle::status(void) const
450b0d29bc4SBrooks Davis {
451b0d29bc4SBrooks Davis return _pimpl->status;
452b0d29bc4SBrooks Davis }
453b0d29bc4SBrooks Davis
454b0d29bc4SBrooks Davis
455b0d29bc4SBrooks Davis /// Returns the user the process ran as if different than the current one.
456b0d29bc4SBrooks Davis ///
457b0d29bc4SBrooks Davis /// \return None if the credentials of the process were the same as the current
458b0d29bc4SBrooks Davis /// one, or else a user.
459b0d29bc4SBrooks Davis const optional< passwd::user >&
unprivileged_user(void) const460b0d29bc4SBrooks Davis executor::exit_handle::unprivileged_user(void) const
461b0d29bc4SBrooks Davis {
462b0d29bc4SBrooks Davis return _pimpl->unprivileged_user;
463b0d29bc4SBrooks Davis }
464b0d29bc4SBrooks Davis
465b0d29bc4SBrooks Davis
466b0d29bc4SBrooks Davis /// Returns the timestamp of when the subprocess was spawned.
467b0d29bc4SBrooks Davis ///
468b0d29bc4SBrooks Davis /// \return A timestamp.
469b0d29bc4SBrooks Davis const datetime::timestamp&
start_time(void) const470b0d29bc4SBrooks Davis executor::exit_handle::start_time(void) const
471b0d29bc4SBrooks Davis {
472b0d29bc4SBrooks Davis return _pimpl->start_time;
473b0d29bc4SBrooks Davis }
474b0d29bc4SBrooks Davis
475b0d29bc4SBrooks Davis
476b0d29bc4SBrooks Davis /// Returns the timestamp of when wait() or wait_any() returned this object.
477b0d29bc4SBrooks Davis ///
478b0d29bc4SBrooks Davis /// \return A timestamp.
479b0d29bc4SBrooks Davis const datetime::timestamp&
end_time(void) const480b0d29bc4SBrooks Davis executor::exit_handle::end_time(void) const
481b0d29bc4SBrooks Davis {
482b0d29bc4SBrooks Davis return _pimpl->end_time;
483b0d29bc4SBrooks Davis }
484b0d29bc4SBrooks Davis
485b0d29bc4SBrooks Davis
486b0d29bc4SBrooks Davis /// Returns the path to the subprocess-specific control directory.
487b0d29bc4SBrooks Davis ///
488b0d29bc4SBrooks Davis /// This is where the executor may store control files.
489b0d29bc4SBrooks Davis ///
490b0d29bc4SBrooks Davis /// \return The path to a directory that exists until cleanup() is called.
491b0d29bc4SBrooks Davis fs::path
control_directory(void) const492b0d29bc4SBrooks Davis executor::exit_handle::control_directory(void) const
493b0d29bc4SBrooks Davis {
494b0d29bc4SBrooks Davis return _pimpl->control_directory;
495b0d29bc4SBrooks Davis }
496b0d29bc4SBrooks Davis
497b0d29bc4SBrooks Davis
498b0d29bc4SBrooks Davis /// Returns the path to the subprocess-specific work directory.
499b0d29bc4SBrooks Davis ///
500b0d29bc4SBrooks Davis /// This is guaranteed to be clear of files created by the executor.
501b0d29bc4SBrooks Davis ///
502b0d29bc4SBrooks Davis /// \return The path to a directory that exists until cleanup() is called.
503b0d29bc4SBrooks Davis fs::path
work_directory(void) const504b0d29bc4SBrooks Davis executor::exit_handle::work_directory(void) const
505b0d29bc4SBrooks Davis {
506b0d29bc4SBrooks Davis return _pimpl->control_directory / detail::work_subdir;
507b0d29bc4SBrooks Davis }
508b0d29bc4SBrooks Davis
509b0d29bc4SBrooks Davis
510b0d29bc4SBrooks Davis /// Returns the path to the subprocess's stdout file.
511b0d29bc4SBrooks Davis ///
512b0d29bc4SBrooks Davis /// \return The path to a file that exists until cleanup() is called.
513b0d29bc4SBrooks Davis const fs::path&
stdout_file(void) const514b0d29bc4SBrooks Davis executor::exit_handle::stdout_file(void) const
515b0d29bc4SBrooks Davis {
516b0d29bc4SBrooks Davis return _pimpl->stdout_file;
517b0d29bc4SBrooks Davis }
518b0d29bc4SBrooks Davis
519b0d29bc4SBrooks Davis
520b0d29bc4SBrooks Davis /// Returns the path to the subprocess's stderr file.
521b0d29bc4SBrooks Davis ///
522b0d29bc4SBrooks Davis /// \return The path to a file that exists until cleanup() is called.
523b0d29bc4SBrooks Davis const fs::path&
stderr_file(void) const524b0d29bc4SBrooks Davis executor::exit_handle::stderr_file(void) const
525b0d29bc4SBrooks Davis {
526b0d29bc4SBrooks Davis return _pimpl->stderr_file;
527b0d29bc4SBrooks Davis }
528b0d29bc4SBrooks Davis
529b0d29bc4SBrooks Davis
530b0d29bc4SBrooks Davis /// Internal implementation for the executor_handle.
531b0d29bc4SBrooks Davis ///
532b0d29bc4SBrooks Davis /// Because the executor is a singleton, these essentially is a container for
533b0d29bc4SBrooks Davis /// global variables.
534b0d29bc4SBrooks Davis struct utils::process::executor::executor_handle::impl : utils::noncopyable {
535b0d29bc4SBrooks Davis /// Numeric counter of executed subprocesses.
536b0d29bc4SBrooks Davis ///
537b0d29bc4SBrooks Davis /// This is used to generate a unique identifier for each subprocess as an
538b0d29bc4SBrooks Davis /// easy mechanism to discern their unique work directories.
539b0d29bc4SBrooks Davis size_t last_subprocess;
540b0d29bc4SBrooks Davis
541b0d29bc4SBrooks Davis /// Interrupts handler.
542b0d29bc4SBrooks Davis std::auto_ptr< signals::interrupts_handler > interrupts_handler;
543b0d29bc4SBrooks Davis
544b0d29bc4SBrooks Davis /// Root work directory for all executed subprocesses.
545b0d29bc4SBrooks Davis std::auto_ptr< fs::auto_directory > root_work_directory;
546b0d29bc4SBrooks Davis
547b0d29bc4SBrooks Davis /// Mapping of PIDs to the data required at run time.
548b0d29bc4SBrooks Davis exec_handles_map all_exec_handles;
549b0d29bc4SBrooks Davis
550b0d29bc4SBrooks Davis /// Whether the executor state has been cleaned yet or not.
551b0d29bc4SBrooks Davis ///
552b0d29bc4SBrooks Davis /// Used to keep track of explicit calls to the public cleanup().
553b0d29bc4SBrooks Davis bool cleaned;
554b0d29bc4SBrooks Davis
555b0d29bc4SBrooks Davis /// Constructor.
implutils::process::executor::executor_handle::impl556b0d29bc4SBrooks Davis impl(void) :
557b0d29bc4SBrooks Davis last_subprocess(0),
558b0d29bc4SBrooks Davis interrupts_handler(new signals::interrupts_handler()),
559b0d29bc4SBrooks Davis root_work_directory(new fs::auto_directory(
560b0d29bc4SBrooks Davis fs::auto_directory::mkdtemp_public(work_directory_template))),
561b0d29bc4SBrooks Davis cleaned(false)
562b0d29bc4SBrooks Davis {
563b0d29bc4SBrooks Davis }
564b0d29bc4SBrooks Davis
565b0d29bc4SBrooks Davis /// Destructor.
~implutils::process::executor::executor_handle::impl566b0d29bc4SBrooks Davis ~impl(void)
567b0d29bc4SBrooks Davis {
568b0d29bc4SBrooks Davis if (!cleaned) {
569b0d29bc4SBrooks Davis LW("Implicitly cleaning up executor; ignoring errors!");
570b0d29bc4SBrooks Davis try {
571b0d29bc4SBrooks Davis cleanup();
572b0d29bc4SBrooks Davis cleaned = true;
573b0d29bc4SBrooks Davis } catch (const std::runtime_error& error) {
574b0d29bc4SBrooks Davis LE(F("Executor global cleanup failed: %s") % error.what());
575b0d29bc4SBrooks Davis }
576b0d29bc4SBrooks Davis }
577b0d29bc4SBrooks Davis }
578b0d29bc4SBrooks Davis
579b0d29bc4SBrooks Davis /// Cleans up the executor state.
580b0d29bc4SBrooks Davis void
cleanuputils::process::executor::executor_handle::impl581b0d29bc4SBrooks Davis cleanup(void)
582b0d29bc4SBrooks Davis {
583b0d29bc4SBrooks Davis PRE(!cleaned);
584b0d29bc4SBrooks Davis
585b0d29bc4SBrooks Davis for (exec_handles_map::const_iterator iter = all_exec_handles.begin();
586b0d29bc4SBrooks Davis iter != all_exec_handles.end(); ++iter) {
587b0d29bc4SBrooks Davis const int& pid = (*iter).first;
588b0d29bc4SBrooks Davis const exec_handle& data = (*iter).second;
589b0d29bc4SBrooks Davis
590b0d29bc4SBrooks Davis process::terminate_group(pid);
591b0d29bc4SBrooks Davis int status;
592b0d29bc4SBrooks Davis if (::waitpid(pid, &status, 0) == -1) {
593b0d29bc4SBrooks Davis // Should not happen.
594b0d29bc4SBrooks Davis LW(F("Failed to wait for PID %s") % pid);
595b0d29bc4SBrooks Davis }
596b0d29bc4SBrooks Davis
597b0d29bc4SBrooks Davis try {
598b0d29bc4SBrooks Davis fs::rm_r(data.control_directory());
599b0d29bc4SBrooks Davis } catch (const fs::error& e) {
600b0d29bc4SBrooks Davis LE(F("Failed to clean up subprocess work directory %s: %s") %
601b0d29bc4SBrooks Davis data.control_directory() % e.what());
602b0d29bc4SBrooks Davis }
603b0d29bc4SBrooks Davis }
604b0d29bc4SBrooks Davis all_exec_handles.clear();
605b0d29bc4SBrooks Davis
606b0d29bc4SBrooks Davis try {
607b0d29bc4SBrooks Davis // The following only causes the work directory to be deleted, not
608b0d29bc4SBrooks Davis // any of its contents, so we expect this to always succeed. This
609b0d29bc4SBrooks Davis // *should* be sufficient because, in the loop above, we have
610b0d29bc4SBrooks Davis // individually wiped the subdirectories of any still-unclean
611b0d29bc4SBrooks Davis // subprocesses.
612b0d29bc4SBrooks Davis root_work_directory->cleanup();
613b0d29bc4SBrooks Davis } catch (const fs::error& e) {
614b0d29bc4SBrooks Davis LE(F("Failed to clean up executor work directory %s: %s; this is "
615b0d29bc4SBrooks Davis "an internal error") % root_work_directory->directory()
616b0d29bc4SBrooks Davis % e.what());
617b0d29bc4SBrooks Davis }
618b0d29bc4SBrooks Davis root_work_directory.reset(NULL);
619b0d29bc4SBrooks Davis
620b0d29bc4SBrooks Davis interrupts_handler->unprogram();
621b0d29bc4SBrooks Davis interrupts_handler.reset(NULL);
622b0d29bc4SBrooks Davis }
623b0d29bc4SBrooks Davis
624b0d29bc4SBrooks Davis /// Common code to run after any of the wait calls.
625b0d29bc4SBrooks Davis ///
626b0d29bc4SBrooks Davis /// \param original_pid The PID of the terminated subprocess.
627b0d29bc4SBrooks Davis /// \param status The exit status of the terminated subprocess.
628b0d29bc4SBrooks Davis ///
629b0d29bc4SBrooks Davis /// \return A pointer to an object describing the waited-for subprocess.
630b0d29bc4SBrooks Davis executor::exit_handle
post_waitutils::process::executor::executor_handle::impl631b0d29bc4SBrooks Davis post_wait(const int original_pid, const process::status& status)
632b0d29bc4SBrooks Davis {
633b0d29bc4SBrooks Davis PRE(original_pid == status.dead_pid());
634b0d29bc4SBrooks Davis LI(F("Waited for subprocess with exec_handle %s") % original_pid);
635b0d29bc4SBrooks Davis
636b0d29bc4SBrooks Davis process::terminate_group(status.dead_pid());
637b0d29bc4SBrooks Davis
638b0d29bc4SBrooks Davis const exec_handles_map::iterator iter = all_exec_handles.find(
639b0d29bc4SBrooks Davis original_pid);
640b0d29bc4SBrooks Davis exec_handle& data = (*iter).second;
641b0d29bc4SBrooks Davis data._pimpl->timer.unprogram();
642b0d29bc4SBrooks Davis
643b0d29bc4SBrooks Davis // It is tempting to assert here (and old code did) that, if the timer
644b0d29bc4SBrooks Davis // has fired, the process has been forcibly killed by us. This is not
645b0d29bc4SBrooks Davis // always the case though: for short-lived processes and with very short
646b0d29bc4SBrooks Davis // timeouts (think 1ms), it is possible for scheduling decisions to
647b0d29bc4SBrooks Davis // allow the subprocess to finish while at the same time cause the timer
648b0d29bc4SBrooks Davis // to fire. So we do not assert this any longer and just rely on the
649b0d29bc4SBrooks Davis // timer expiration to check if the process timed out or not. If the
650b0d29bc4SBrooks Davis // process did finish but the timer expired... oh well, we do not detect
651b0d29bc4SBrooks Davis // this correctly but we don't care because this should not really
652b0d29bc4SBrooks Davis // happen.
653b0d29bc4SBrooks Davis
654b0d29bc4SBrooks Davis if (!fs::exists(data.stdout_file())) {
655b0d29bc4SBrooks Davis std::ofstream new_stdout(data.stdout_file().c_str());
656b0d29bc4SBrooks Davis }
657b0d29bc4SBrooks Davis if (!fs::exists(data.stderr_file())) {
658b0d29bc4SBrooks Davis std::ofstream new_stderr(data.stderr_file().c_str());
659b0d29bc4SBrooks Davis }
660b0d29bc4SBrooks Davis
661b0d29bc4SBrooks Davis return exit_handle(std::shared_ptr< exit_handle::impl >(
662b0d29bc4SBrooks Davis new exit_handle::impl(
663b0d29bc4SBrooks Davis data.pid(),
664b0d29bc4SBrooks Davis data._pimpl->timer.fired() ?
665b0d29bc4SBrooks Davis none : utils::make_optional(status),
666b0d29bc4SBrooks Davis data._pimpl->unprivileged_user,
667b0d29bc4SBrooks Davis data._pimpl->start_time, datetime::timestamp::now(),
668b0d29bc4SBrooks Davis data.control_directory(),
669b0d29bc4SBrooks Davis data.stdout_file(),
670b0d29bc4SBrooks Davis data.stderr_file(),
671b0d29bc4SBrooks Davis data._pimpl->state_owners,
672b0d29bc4SBrooks Davis all_exec_handles)));
673b0d29bc4SBrooks Davis }
674*26980dceSIgor Ostapenko
675*26980dceSIgor Ostapenko executor::exit_handle
reaputils::process::executor::executor_handle::impl676*26980dceSIgor Ostapenko reap(const pid_t original_pid)
677*26980dceSIgor Ostapenko {
678*26980dceSIgor Ostapenko const exec_handles_map::iterator iter = all_exec_handles.find(
679*26980dceSIgor Ostapenko original_pid);
680*26980dceSIgor Ostapenko exec_handle& data = (*iter).second;
681*26980dceSIgor Ostapenko data._pimpl->timer.unprogram();
682*26980dceSIgor Ostapenko
683*26980dceSIgor Ostapenko if (!fs::exists(data.stdout_file())) {
684*26980dceSIgor Ostapenko std::ofstream new_stdout(data.stdout_file().c_str());
685*26980dceSIgor Ostapenko }
686*26980dceSIgor Ostapenko if (!fs::exists(data.stderr_file())) {
687*26980dceSIgor Ostapenko std::ofstream new_stderr(data.stderr_file().c_str());
688*26980dceSIgor Ostapenko }
689*26980dceSIgor Ostapenko
690*26980dceSIgor Ostapenko return exit_handle(std::shared_ptr< exit_handle::impl >(
691*26980dceSIgor Ostapenko new exit_handle::impl(
692*26980dceSIgor Ostapenko data.pid(),
693*26980dceSIgor Ostapenko none,
694*26980dceSIgor Ostapenko data._pimpl->unprivileged_user,
695*26980dceSIgor Ostapenko data._pimpl->start_time, datetime::timestamp::now(),
696*26980dceSIgor Ostapenko data.control_directory(),
697*26980dceSIgor Ostapenko data.stdout_file(),
698*26980dceSIgor Ostapenko data.stderr_file(),
699*26980dceSIgor Ostapenko data._pimpl->state_owners,
700*26980dceSIgor Ostapenko all_exec_handles)));
701*26980dceSIgor Ostapenko }
702b0d29bc4SBrooks Davis };
703b0d29bc4SBrooks Davis
704b0d29bc4SBrooks Davis
705b0d29bc4SBrooks Davis /// Constructor.
executor_handle(void)706b0d29bc4SBrooks Davis executor::executor_handle::executor_handle(void) throw() : _pimpl(new impl())
707b0d29bc4SBrooks Davis {
708b0d29bc4SBrooks Davis }
709b0d29bc4SBrooks Davis
710b0d29bc4SBrooks Davis
711b0d29bc4SBrooks Davis /// Destructor.
~executor_handle(void)712b0d29bc4SBrooks Davis executor::executor_handle::~executor_handle(void)
713b0d29bc4SBrooks Davis {
714b0d29bc4SBrooks Davis }
715b0d29bc4SBrooks Davis
716b0d29bc4SBrooks Davis
717b0d29bc4SBrooks Davis /// Queries the path to the root of the work directory for all subprocesses.
718b0d29bc4SBrooks Davis ///
719b0d29bc4SBrooks Davis /// \return A path.
720b0d29bc4SBrooks Davis const fs::path&
root_work_directory(void) const721b0d29bc4SBrooks Davis executor::executor_handle::root_work_directory(void) const
722b0d29bc4SBrooks Davis {
723b0d29bc4SBrooks Davis return _pimpl->root_work_directory->directory();
724b0d29bc4SBrooks Davis }
725b0d29bc4SBrooks Davis
726b0d29bc4SBrooks Davis
727b0d29bc4SBrooks Davis /// Cleans up the executor state.
728b0d29bc4SBrooks Davis ///
729b0d29bc4SBrooks Davis /// This function should be called explicitly as it provides the means to
730b0d29bc4SBrooks Davis /// control any exceptions raised during cleanup. Do not rely on the destructor
731b0d29bc4SBrooks Davis /// to clean things up.
732b0d29bc4SBrooks Davis ///
733b0d29bc4SBrooks Davis /// \throw engine::error If there are problems cleaning up the executor.
734b0d29bc4SBrooks Davis void
cleanup(void)735b0d29bc4SBrooks Davis executor::executor_handle::cleanup(void)
736b0d29bc4SBrooks Davis {
737b0d29bc4SBrooks Davis PRE(!_pimpl->cleaned);
738b0d29bc4SBrooks Davis _pimpl->cleanup();
739b0d29bc4SBrooks Davis _pimpl->cleaned = true;
740b0d29bc4SBrooks Davis }
741b0d29bc4SBrooks Davis
742b0d29bc4SBrooks Davis
743b0d29bc4SBrooks Davis /// Initializes the executor.
744b0d29bc4SBrooks Davis ///
745b0d29bc4SBrooks Davis /// \pre This function can only be called if there is no other executor_handle
746b0d29bc4SBrooks Davis /// object alive.
747b0d29bc4SBrooks Davis ///
748b0d29bc4SBrooks Davis /// \return A handle to the operations of the executor.
749b0d29bc4SBrooks Davis executor::executor_handle
setup(void)750b0d29bc4SBrooks Davis executor::setup(void)
751b0d29bc4SBrooks Davis {
752b0d29bc4SBrooks Davis return executor_handle();
753b0d29bc4SBrooks Davis }
754b0d29bc4SBrooks Davis
755b0d29bc4SBrooks Davis
756b0d29bc4SBrooks Davis /// Pre-helper for the spawn() method.
757b0d29bc4SBrooks Davis ///
758b0d29bc4SBrooks Davis /// \return The created control directory for the subprocess.
759b0d29bc4SBrooks Davis fs::path
spawn_pre(void)760b0d29bc4SBrooks Davis executor::executor_handle::spawn_pre(void)
761b0d29bc4SBrooks Davis {
762b0d29bc4SBrooks Davis signals::check_interrupt();
763b0d29bc4SBrooks Davis
764b0d29bc4SBrooks Davis ++_pimpl->last_subprocess;
765b0d29bc4SBrooks Davis
766b0d29bc4SBrooks Davis const fs::path control_directory =
767b0d29bc4SBrooks Davis _pimpl->root_work_directory->directory() /
768b0d29bc4SBrooks Davis (F("%s") % _pimpl->last_subprocess);
769b0d29bc4SBrooks Davis fs::mkdir_p(control_directory / detail::work_subdir, 0755);
770b0d29bc4SBrooks Davis
771b0d29bc4SBrooks Davis return control_directory;
772b0d29bc4SBrooks Davis }
773b0d29bc4SBrooks Davis
774b0d29bc4SBrooks Davis
775b0d29bc4SBrooks Davis /// Post-helper for the spawn() method.
776b0d29bc4SBrooks Davis ///
777b0d29bc4SBrooks Davis /// \param control_directory Control directory as returned by spawn_pre().
778b0d29bc4SBrooks Davis /// \param stdout_file Path to the subprocess' stdout.
779b0d29bc4SBrooks Davis /// \param stderr_file Path to the subprocess' stderr.
780b0d29bc4SBrooks Davis /// \param timeout Maximum amount of time the subprocess can run for.
781b0d29bc4SBrooks Davis /// \param unprivileged_user If not none, user to switch to before execution.
782b0d29bc4SBrooks Davis /// \param child The process created by spawn().
783b0d29bc4SBrooks Davis ///
784b0d29bc4SBrooks Davis /// \return The execution handle of the started subprocess.
785b0d29bc4SBrooks Davis executor::exec_handle
spawn_post(const fs::path & control_directory,const fs::path & stdout_file,const fs::path & stderr_file,const datetime::delta & timeout,const optional<passwd::user> unprivileged_user,std::auto_ptr<process::child> child)786b0d29bc4SBrooks Davis executor::executor_handle::spawn_post(
787b0d29bc4SBrooks Davis const fs::path& control_directory,
788b0d29bc4SBrooks Davis const fs::path& stdout_file,
789b0d29bc4SBrooks Davis const fs::path& stderr_file,
790b0d29bc4SBrooks Davis const datetime::delta& timeout,
791b0d29bc4SBrooks Davis const optional< passwd::user > unprivileged_user,
792b0d29bc4SBrooks Davis std::auto_ptr< process::child > child)
793b0d29bc4SBrooks Davis {
794b0d29bc4SBrooks Davis const exec_handle handle(std::shared_ptr< exec_handle::impl >(
795b0d29bc4SBrooks Davis new exec_handle::impl(
796b0d29bc4SBrooks Davis child->pid(),
797b0d29bc4SBrooks Davis control_directory,
798b0d29bc4SBrooks Davis stdout_file,
799b0d29bc4SBrooks Davis stderr_file,
800b0d29bc4SBrooks Davis datetime::timestamp::now(),
801b0d29bc4SBrooks Davis timeout,
802b0d29bc4SBrooks Davis unprivileged_user,
803b0d29bc4SBrooks Davis detail::refcnt_t(new detail::refcnt_t::element_type(0)))));
804b0d29bc4SBrooks Davis INV_MSG(_pimpl->all_exec_handles.find(handle.pid()) ==
805b0d29bc4SBrooks Davis _pimpl->all_exec_handles.end(),
806b0d29bc4SBrooks Davis F("PID %s already in all_exec_handles; not properly cleaned "
807b0d29bc4SBrooks Davis "up or reused too fast") % handle.pid());;
808b0d29bc4SBrooks Davis _pimpl->all_exec_handles.insert(exec_handles_map::value_type(
809b0d29bc4SBrooks Davis handle.pid(), handle));
810b0d29bc4SBrooks Davis LI(F("Spawned subprocess with exec_handle %s") % handle.pid());
811b0d29bc4SBrooks Davis return handle;
812b0d29bc4SBrooks Davis }
813b0d29bc4SBrooks Davis
814b0d29bc4SBrooks Davis
815b0d29bc4SBrooks Davis /// Pre-helper for the spawn_followup() method.
816b0d29bc4SBrooks Davis void
spawn_followup_pre(void)817b0d29bc4SBrooks Davis executor::executor_handle::spawn_followup_pre(void)
818b0d29bc4SBrooks Davis {
819b0d29bc4SBrooks Davis signals::check_interrupt();
820b0d29bc4SBrooks Davis }
821b0d29bc4SBrooks Davis
822b0d29bc4SBrooks Davis
823b0d29bc4SBrooks Davis /// Post-helper for the spawn_followup() method.
824b0d29bc4SBrooks Davis ///
825b0d29bc4SBrooks Davis /// \param base Exit handle of the subprocess to use as context.
826b0d29bc4SBrooks Davis /// \param timeout Maximum amount of time the subprocess can run for.
827b0d29bc4SBrooks Davis /// \param child The process created by spawn_followup().
828b0d29bc4SBrooks Davis ///
829b0d29bc4SBrooks Davis /// \return The execution handle of the started subprocess.
830b0d29bc4SBrooks Davis executor::exec_handle
spawn_followup_post(const exit_handle & base,const datetime::delta & timeout,std::auto_ptr<process::child> child)831b0d29bc4SBrooks Davis executor::executor_handle::spawn_followup_post(
832b0d29bc4SBrooks Davis const exit_handle& base,
833b0d29bc4SBrooks Davis const datetime::delta& timeout,
834b0d29bc4SBrooks Davis std::auto_ptr< process::child > child)
835b0d29bc4SBrooks Davis {
836b0d29bc4SBrooks Davis INV(*base.state_owners() > 0);
837b0d29bc4SBrooks Davis const exec_handle handle(std::shared_ptr< exec_handle::impl >(
838b0d29bc4SBrooks Davis new exec_handle::impl(
839b0d29bc4SBrooks Davis child->pid(),
840b0d29bc4SBrooks Davis base.control_directory(),
841b0d29bc4SBrooks Davis base.stdout_file(),
842b0d29bc4SBrooks Davis base.stderr_file(),
843b0d29bc4SBrooks Davis datetime::timestamp::now(),
844b0d29bc4SBrooks Davis timeout,
845b0d29bc4SBrooks Davis base.unprivileged_user(),
846b0d29bc4SBrooks Davis base.state_owners())));
847b0d29bc4SBrooks Davis INV_MSG(_pimpl->all_exec_handles.find(handle.pid()) ==
848b0d29bc4SBrooks Davis _pimpl->all_exec_handles.end(),
849b0d29bc4SBrooks Davis F("PID %s already in all_exec_handles; not properly cleaned "
850b0d29bc4SBrooks Davis "up or reused too fast") % handle.pid());;
851b0d29bc4SBrooks Davis _pimpl->all_exec_handles.insert(exec_handles_map::value_type(
852b0d29bc4SBrooks Davis handle.pid(), handle));
853b0d29bc4SBrooks Davis LI(F("Spawned subprocess with exec_handle %s") % handle.pid());
854b0d29bc4SBrooks Davis return handle;
855b0d29bc4SBrooks Davis }
856b0d29bc4SBrooks Davis
857b0d29bc4SBrooks Davis
858b0d29bc4SBrooks Davis /// Waits for completion of any forked process.
859b0d29bc4SBrooks Davis ///
860b0d29bc4SBrooks Davis /// \param exec_handle The handle of the process to wait for.
861b0d29bc4SBrooks Davis ///
862b0d29bc4SBrooks Davis /// \return A pointer to an object describing the waited-for subprocess.
863b0d29bc4SBrooks Davis executor::exit_handle
wait(const exec_handle exec_handle)864b0d29bc4SBrooks Davis executor::executor_handle::wait(const exec_handle exec_handle)
865b0d29bc4SBrooks Davis {
866b0d29bc4SBrooks Davis signals::check_interrupt();
867b0d29bc4SBrooks Davis const process::status status = process::wait(exec_handle.pid());
868b0d29bc4SBrooks Davis return _pimpl->post_wait(exec_handle.pid(), status);
869b0d29bc4SBrooks Davis }
870b0d29bc4SBrooks Davis
871b0d29bc4SBrooks Davis
872b0d29bc4SBrooks Davis /// Waits for completion of any forked process.
873b0d29bc4SBrooks Davis ///
874b0d29bc4SBrooks Davis /// \return A pointer to an object describing the waited-for subprocess.
875b0d29bc4SBrooks Davis executor::exit_handle
wait_any(void)876b0d29bc4SBrooks Davis executor::executor_handle::wait_any(void)
877b0d29bc4SBrooks Davis {
878b0d29bc4SBrooks Davis signals::check_interrupt();
879b0d29bc4SBrooks Davis const process::status status = process::wait_any();
880b0d29bc4SBrooks Davis return _pimpl->post_wait(status.dead_pid(), status);
881b0d29bc4SBrooks Davis }
882b0d29bc4SBrooks Davis
883b0d29bc4SBrooks Davis
884*26980dceSIgor Ostapenko /// Forms exit_handle for the given PID subprocess.
885*26980dceSIgor Ostapenko ///
886*26980dceSIgor Ostapenko /// Can be used in the cases when we want to do cleanup(s) of a killed test
887*26980dceSIgor Ostapenko /// subprocess, but we do not have exit handle as we usually do after normal
888*26980dceSIgor Ostapenko /// wait mechanism.
889*26980dceSIgor Ostapenko ///
890*26980dceSIgor Ostapenko /// \return A pointer to an object describing the subprocess.
891*26980dceSIgor Ostapenko executor::exit_handle
reap(const int pid)892*26980dceSIgor Ostapenko executor::executor_handle::reap(const int pid)
893*26980dceSIgor Ostapenko {
894*26980dceSIgor Ostapenko return _pimpl->reap(pid);
895*26980dceSIgor Ostapenko }
896*26980dceSIgor Ostapenko
897*26980dceSIgor Ostapenko
898b0d29bc4SBrooks Davis /// Checks if an interrupt has fired.
899b0d29bc4SBrooks Davis ///
900b0d29bc4SBrooks Davis /// Calls to this function should be sprinkled in strategic places through the
901b0d29bc4SBrooks Davis /// code protected by an interrupts_handler object.
902b0d29bc4SBrooks Davis ///
903b0d29bc4SBrooks Davis /// This is just a wrapper over signals::check_interrupt() to avoid leaking this
904b0d29bc4SBrooks Davis /// dependency to the caller.
905b0d29bc4SBrooks Davis ///
906b0d29bc4SBrooks Davis /// \throw signals::interrupted_error If there has been an interrupt.
907b0d29bc4SBrooks Davis void
check_interrupt(void) const908b0d29bc4SBrooks Davis executor::executor_handle::check_interrupt(void) const
909b0d29bc4SBrooks Davis {
910b0d29bc4SBrooks Davis signals::check_interrupt();
911b0d29bc4SBrooks Davis }
912