Path: blob/main/contrib/kyua/utils/process/executor.hpp
48180 views
// Copyright 2015 The Kyua Authors.1// All rights reserved.2//3// Redistribution and use in source and binary forms, with or without4// modification, are permitted provided that the following conditions are5// met:6//7// * Redistributions of source code must retain the above copyright8// notice, this list of conditions and the following disclaimer.9// * Redistributions in binary form must reproduce the above copyright10// notice, this list of conditions and the following disclaimer in the11// documentation and/or other materials provided with the distribution.12// * Neither the name of Google Inc. nor the names of its contributors13// may be used to endorse or promote products derived from this software14// without specific prior written permission.15//16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.2728/// \file utils/process/executor.hpp29/// Multiprogrammed process executor with isolation guarantees.30///31/// This module provides a mechanism to invoke more than one process32/// concurrently while at the same time ensuring that each process is run33/// in a clean container and in a "safe" work directory that gets cleaned34/// up automatically on termination.35///36/// The intended workflow for using this module is the following:37///38/// 1) Initialize the executor using setup(). Keep the returned object39/// around through the lifetime of the next operations. Only one40/// instance of the executor can be alive at once.41/// 2) Spawn one or more processes with spawn(). On the caller side, keep42/// track of any per-process data you may need using the returned43/// exec_handle, which is unique among the set of active processes.44/// 3) Call wait() or wait_any() to wait for completion of a process started45/// in the previous step. Repeat as desired.46/// 4) Use the returned exit_handle object by wait() or wait_any() to query47/// the status of the terminated process and/or to access any of its48/// data files.49/// 5) Invoke cleanup() on the exit_handle to wipe any stale data.50/// 6) Invoke cleanup() on the object returned by setup().51///52/// It is the responsibility of the caller to ensure that calls to53/// spawn() and spawn_followup() are balanced with wait() and wait_any() calls.54///55/// Processes executed in this manner have access to two different "unique"56/// directories: the first is the "work directory", which is an empty directory57/// that acts as the subprocess' work directory; the second is the "control58/// directory", which is the location where the in-process code may place files59/// that are not clobbered by activities in the work directory.6061#if !defined(UTILS_PROCESS_EXECUTOR_HPP)62#define UTILS_PROCESS_EXECUTOR_HPP6364#include "utils/process/executor_fwd.hpp"6566#include <cstddef>67#include <memory>6869#include "utils/datetime_fwd.hpp"70#include "utils/fs/path_fwd.hpp"71#include "utils/optional.hpp"72#include "utils/passwd_fwd.hpp"73#include "utils/process/child_fwd.hpp"74#include "utils/process/status_fwd.hpp"7576namespace utils {77namespace process {78namespace executor {798081namespace detail {828384extern const char* stdout_name;85extern const char* stderr_name;86extern const char* work_subdir;878889/// Shared reference counter.90typedef std::shared_ptr< std::size_t > refcnt_t;919293void setup_child(const utils::optional< utils::passwd::user >,94const utils::fs::path&, const utils::fs::path&);959697} // namespace detail9899100/// Maintenance data held while a subprocess is being executed.101///102/// This data structure exists from the moment a subprocess is executed via103/// executor::spawn() to when it is cleaned up with exit_handle::cleanup().104///105/// The caller NEED NOT maintain this object alive for the execution of the106/// subprocess. However, the PID contained in here can be used to match107/// exec_handle objects with corresponding exit_handle objects via their108/// original_pid() method.109///110/// Objects of this type can be copied around but their implementation is111/// shared. The implication of this is that only the last copy of a given exit112/// handle will execute the automatic cleanup() on destruction.113class exec_handle {114struct impl;115116/// Pointer to internal implementation.117std::shared_ptr< impl > _pimpl;118119friend class executor_handle;120exec_handle(std::shared_ptr< impl >);121122public:123~exec_handle(void);124125int pid(void) const;126utils::fs::path control_directory(void) const;127utils::fs::path work_directory(void) const;128const utils::fs::path& stdout_file(void) const;129const utils::fs::path& stderr_file(void) const;130};131132133/// Container for the data of a process termination.134///135/// This handle provides access to the details of the process that terminated136/// and serves as the owner of the remaining on-disk files. The caller is137/// expected to call cleanup() before destruction to remove the on-disk state.138///139/// Objects of this type can be copied around but their implementation is140/// shared. The implication of this is that only the last copy of a given exit141/// handle will execute the automatic cleanup() on destruction.142class exit_handle {143struct impl;144145/// Pointer to internal implementation.146std::shared_ptr< impl > _pimpl;147148friend class executor_handle;149exit_handle(std::shared_ptr< impl >);150151detail::refcnt_t state_owners(void) const;152153public:154~exit_handle(void);155156void cleanup(void);157158int original_pid(void) const;159const utils::optional< utils::process::status >& status(void) const;160const utils::optional< utils::passwd::user >& unprivileged_user(void) const;161const utils::datetime::timestamp& start_time() const;162const utils::datetime::timestamp& end_time() const;163utils::fs::path control_directory(void) const;164utils::fs::path work_directory(void) const;165const utils::fs::path& stdout_file(void) const;166const utils::fs::path& stderr_file(void) const;167};168169170/// Handler for the livelihood of the executor.171///172/// Objects of this type can be copied around (because we do not have move173/// semantics...) but their implementation is shared. Only one instance of the174/// executor can exist at any point in time.175class executor_handle {176struct impl;177/// Pointer to internal implementation.178std::shared_ptr< impl > _pimpl;179180friend executor_handle setup(void);181executor_handle(void) throw();182183utils::fs::path spawn_pre(void);184exec_handle spawn_post(const utils::fs::path&,185const utils::fs::path&,186const utils::fs::path&,187const utils::datetime::delta&,188const utils::optional< utils::passwd::user >,189std::unique_ptr< utils::process::child >);190191void spawn_followup_pre(void);192exec_handle spawn_followup_post(const exit_handle&,193const utils::datetime::delta&,194std::unique_ptr< utils::process::child >);195196public:197~executor_handle(void);198199const utils::fs::path& root_work_directory(void) const;200201void cleanup(void);202203template< class Hook >204exec_handle spawn(Hook,205const datetime::delta&,206const utils::optional< utils::passwd::user >,207const utils::optional< utils::fs::path > = utils::none,208const utils::optional< utils::fs::path > = utils::none);209210template< class Hook >211exec_handle spawn_followup(Hook,212const exit_handle&,213const datetime::delta&);214215exit_handle wait(const exec_handle);216exit_handle wait_any(void);217exit_handle reap(const pid_t);218219void check_interrupt(void) const;220};221222223executor_handle setup(void);224225226} // namespace executor227} // namespace process228} // namespace utils229230231#endif // !defined(UTILS_PROCESS_EXECUTOR_HPP)232233234