Path: blob/main/contrib/atf/atf-c++/detail/process.hpp
39562 views
// Copyright (c) 2008 The NetBSD Foundation, Inc.1// All rights reserved.2//3// Redistribution and use in source and binary forms, with or without4// modification, are permitted provided that the following conditions5// are met:6// 1. Redistributions of source code must retain the above copyright7// notice, this list of conditions and the following disclaimer.8// 2. Redistributions in binary form must reproduce the above copyright9// notice, this list of conditions and the following disclaimer in the10// documentation and/or other materials provided with the distribution.11//12// THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND13// CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,14// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF15// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.16// IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY17// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE19// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS20// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER21// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR22// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN23// IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.2425#if !defined(ATF_CXX_DETAIL_PROCESS_HPP)26#define ATF_CXX_DETAIL_PROCESS_HPP2728extern "C" {29#include <sys/types.h>3031#include <atf-c/detail/process.h>32#include <atf-c/error.h>33}3435#include <string>36#include <vector>3738#include <atf-c++/detail/auto_array.hpp>39#include <atf-c++/detail/exceptions.hpp>40#include <atf-c++/detail/fs.hpp>4142namespace atf {43namespace process {4445class child;46class status;4748// ------------------------------------------------------------------------49// The "argv_array" type.50// ------------------------------------------------------------------------5152class argv_array {53typedef std::vector< std::string > args_vector;54args_vector m_args;5556// TODO: This is immutable, so we should be able to use57// std::tr1::shared_array instead when it becomes widely available.58// The reason would be to remove all copy constructors and assignment59// operators from this class.60auto_array< const char* > m_exec_argv;61void ctor_init_exec_argv(void);6263public:64typedef args_vector::const_iterator const_iterator;65typedef args_vector::size_type size_type;6667argv_array(void);68argv_array(const char*, ...);69explicit argv_array(const char* const*);70template< class C > explicit argv_array(const C&);71argv_array(const argv_array&);7273const char* const* exec_argv(void) const;74size_type size(void) const;75const char* operator[](int) const;7677const_iterator begin(void) const;78const_iterator end(void) const;7980argv_array& operator=(const argv_array&);81};8283template< class C >84argv_array::argv_array(const C& c)85{86for (typename C::const_iterator iter = c.begin(); iter != c.end();87iter++)88m_args.push_back(*iter);89ctor_init_exec_argv();90}9192// ------------------------------------------------------------------------93// The "stream" types.94// ------------------------------------------------------------------------9596class basic_stream {97protected:98atf_process_stream_t m_sb;99bool m_inited;100101const atf_process_stream_t* get_sb(void) const;102103public:104basic_stream(void);105~basic_stream(void);106};107108class stream_capture : basic_stream {109// Allow access to the getters.110template< class OutStream, class ErrStream > friend111child fork(void (*)(void*), const OutStream&, const ErrStream&, void*);112template< class OutStream, class ErrStream > friend113status exec(const atf::fs::path&, const argv_array&,114const OutStream&, const ErrStream&, void (*)(void));115116public:117stream_capture(void);118};119120class stream_connect : basic_stream {121// Allow access to the getters.122template< class OutStream, class ErrStream > friend123child fork(void (*)(void*), const OutStream&, const ErrStream&, void*);124template< class OutStream, class ErrStream > friend125status exec(const atf::fs::path&, const argv_array&,126const OutStream&, const ErrStream&, void (*)(void));127128public:129stream_connect(const int, const int);130};131132class stream_inherit : basic_stream {133// Allow access to the getters.134template< class OutStream, class ErrStream > friend135child fork(void (*)(void*), const OutStream&, const ErrStream&, void*);136template< class OutStream, class ErrStream > friend137status exec(const atf::fs::path&, const argv_array&,138const OutStream&, const ErrStream&, void (*)(void));139140public:141stream_inherit(void);142};143144class stream_redirect_fd : basic_stream {145// Allow access to the getters.146template< class OutStream, class ErrStream > friend147child fork(void (*)(void*), const OutStream&, const ErrStream&, void*);148template< class OutStream, class ErrStream > friend149status exec(const atf::fs::path&, const argv_array&,150const OutStream&, const ErrStream&, void (*)(void));151152public:153stream_redirect_fd(const int);154};155156class stream_redirect_path : basic_stream {157// Allow access to the getters.158template< class OutStream, class ErrStream > friend159child fork(void (*)(void*), const OutStream&, const ErrStream&, void*);160template< class OutStream, class ErrStream > friend161status exec(const atf::fs::path&, const argv_array&,162const OutStream&, const ErrStream&, void (*)(void));163164public:165stream_redirect_path(const fs::path&);166};167168// ------------------------------------------------------------------------169// The "status" type.170// ------------------------------------------------------------------------171172class status {173atf_process_status_t m_status;174175friend class child;176template< class OutStream, class ErrStream > friend177status exec(const atf::fs::path&, const argv_array&,178const OutStream&, const ErrStream&, void (*)(void));179180status(atf_process_status_t&);181182public:183~status(void);184185bool exited(void) const;186int exitstatus(void) const;187188bool signaled(void) const;189int termsig(void) const;190bool coredump(void) const;191};192193// ------------------------------------------------------------------------194// The "child" type.195// ------------------------------------------------------------------------196197class child {198atf_process_child_t m_child;199bool m_waited;200201template< class OutStream, class ErrStream > friend202child fork(void (*)(void*), const OutStream&, const ErrStream&, void*);203204child(atf_process_child_t& c);205206public:207~child(void);208209status wait(void);210211pid_t pid(void) const;212int stdout_fd(void);213int stderr_fd(void);214};215216// ------------------------------------------------------------------------217// Free functions.218// ------------------------------------------------------------------------219220namespace detail {221void flush_streams(void);222} // namespace detail223224// TODO: The void* cookie can probably be templatized, thus also allowing225// const data structures.226template< class OutStream, class ErrStream >227child228fork(void (*start)(void*), const OutStream& outsb,229const ErrStream& errsb, void* v)230{231atf_process_child_t c;232233detail::flush_streams();234atf_error_t err = atf_process_fork(&c, start, outsb.get_sb(),235errsb.get_sb(), v);236if (atf_is_error(err))237throw_atf_error(err);238239return child(c);240}241242template< class OutStream, class ErrStream >243status244exec(const atf::fs::path& prog, const argv_array& argv,245const OutStream& outsb, const ErrStream& errsb,246void (*prehook)(void))247{248atf_process_status_t s;249250detail::flush_streams();251atf_error_t err = atf_process_exec_array(&s, prog.c_path(),252argv.exec_argv(),253outsb.get_sb(),254errsb.get_sb(),255prehook);256if (atf_is_error(err))257throw_atf_error(err);258259return status(s);260}261262template< class OutStream, class ErrStream >263status264exec(const atf::fs::path& prog, const argv_array& argv,265const OutStream& outsb, const ErrStream& errsb)266{267return exec(prog, argv, outsb, errsb, NULL);268}269270} // namespace process271} // namespace atf272273#endif // !defined(ATF_CXX_DETAIL_PROCESS_HPP)274275276