Path: blob/main/contrib/kyua/utils/process/child.hpp
48199 views
// Copyright 2010 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/child.hpp29/// Spawning and manipulation of children processes.30///31/// The child module provides a set of functions to spawn subprocesses with32/// different settings, and the corresponding set of classes to interact with33/// said subprocesses. The interfaces to fork subprocesses are very simplified34/// and only provide the minimum functionality required by the rest of the35/// project.36///37/// Be aware that the semantics of the fork and wait methods exposed by this38/// module are slightly different from that of the native calls. Any process39/// spawned by fork here will be isolated in its own session; once any of40/// such children processes is awaited for, its whole process group will be41/// terminated. This is the semantics we want in the above layers to ensure42/// that test programs (and, for that matter, external utilities) do not leak43/// subprocesses on the system.4445#if !defined(UTILS_PROCESS_CHILD_HPP)46#define UTILS_PROCESS_CHILD_HPP4748#include "utils/process/child_fwd.hpp"4950#include <istream>51#include <memory>52#include <stdexcept>5354#include "utils/defs.hpp"55#include "utils/fs/path_fwd.hpp"56#include "utils/noncopyable.hpp"57#include "utils/process/operations_fwd.hpp"58#include "utils/process/status_fwd.hpp"5960namespace utils {61namespace process {626364namespace detail {6566void report_error_and_abort(void) UTILS_NORETURN;67void report_error_and_abort(const std::runtime_error&) UTILS_NORETURN;686970} // namespace detail717273/// Child process spawner and controller.74class child : noncopyable {75struct impl;7677/// Pointer to the shared internal implementation.78std::unique_ptr< impl > _pimpl;7980static std::unique_ptr< child > fork_capture_aux(void);8182static std::unique_ptr< child > fork_files_aux(const fs::path&,83const fs::path&);8485explicit child(impl *);8687public:88~child(void);8990template< typename Hook >91static std::unique_ptr< child > fork_capture(Hook);92std::istream& output(void);9394template< typename Hook >95static std::unique_ptr< child > fork_files(Hook, const fs::path&,96const fs::path&);9798static std::unique_ptr< child > spawn_capture(99const fs::path&, const args_vector&);100static std::unique_ptr< child > spawn_files(101const fs::path&, const args_vector&, const fs::path&, const fs::path&);102103int pid(void) const;104105status wait(void);106};107108109} // namespace process110} // namespace utils111112#endif // !defined(UTILS_PROCESS_CHILD_HPP)113114115