1 // Copyright (c) 2006, 2007 Julio M. Merino Vidal
2 // Copyright (c) 2008 Ilya Sokolov, Boris Schaeling
3 // Copyright (c) 2009 Boris Schaeling
4 // Copyright (c) 2010 Felipe Tanus, Boris Schaeling
5 // Copyright (c) 2011, 2012 Jeff Flinn, Boris Schaeling
6 // Copyright (c) 2016 Klemens D. Morgenstern
8 // Distributed under the Boost Software License, Version 1.0. (See accompanying
9 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
12 * \file boost/process/child.hpp
14 * Defines a child process class.
17 #ifndef BOOST_PROCESS_CHILD_HPP
18 #define BOOST_PROCESS_CHILD_HPP
20 #include <boost/process/detail/config.hpp>
21 #include <boost/process/detail/child_decl.hpp>
22 #include <boost/process/detail/execute_impl.hpp>
24 #if defined(BOOST_POSIX_API)
25 #include <boost/process/posix.hpp>
30 ///The main namespace of boost.process.
33 template<typename ...Args>
34 child::child(Args&&...args)
35 : child(::boost::process::detail::execute_impl(std::forward<Args>(args)...)) {}
38 ///Typedef for the type of an pid_t
39 typedef ::boost::process::detail::api::pid_t pid_t;
41 #if defined(BOOST_PROCESS_DOXYGEN)
42 /** The main class to hold a child process. It is simliar to [std::thread](http://en.cppreference.com/w/cpp/thread/thread),
43 * in that it has a join and detach function.
45 * @attention The destructor will call terminate on the process if not joined or detached without any warning.
51 /** Type definition for the native process handle. */
52 typedef platform_specific native_handle_t;
54 /** Construct the child from a pid.
56 * @attention There is no guarantee that this will work. The process need the right access rights, which are very platform specific.
58 explicit child(pid_t & pid) : _child_handle(pid) {};
60 /** Move-Constructor.*/
63 /** Construct a child from a property list and launch it
64 * The standard version is to create a subprocess, which will spawn the process.
66 template<typename ...Args>
67 explicit child(Args&&...args);
69 /** Construct an empty child. */
73 child& operator=(child && lhs);
75 /** Detach the child, i.e. let it run after this handle dies. */
77 /** Join the child. This just calls wait, but that way the naming is similar to std::thread */
79 /** Check if the child is joinable. */
83 * @attention Will call terminate (without warning) when the child was neither joined nor detached.
87 /** Get the native handle for the child process. */
88 native_handle_t native_handle() const;
90 /** Get the exit_code. The return value is without any meaning if the child wasn't waited for or if it was terminated. */
91 int exit_code() const;
92 /** Get the Process Identifier. */
95 /** Get the native, uninterpreted exit code. The return value is without any meaning if the child wasn't waited
96 * for or if it was terminated. */
97 int native_exit_code() const;
99 /** Check if the child process is running. */
101 /** \overload void running() */
102 bool running(std::error_code & ec) noexcept;
104 /** Wait for the child process to exit. */
106 /** \overload void wait() */
107 void wait(std::error_code & ec) noexcept;
109 /** Wait for the child process to exit for a period of time.
110 * \return True if child exited while waiting.
112 template< class Rep, class Period >
113 bool wait_for (const std::chrono::duration<Rep, Period>& rel_time);
114 /** \overload bool wait_for(const std::chrono::duration<Rep, Period>& rel_time) */
115 bool wait_for (const std::chrono::duration<Rep, Period>& rel_time, std::error_code & ec) noexcept;
117 /** Wait for the child process to exit until a point in time.
118 * \return True if child exited while waiting.*/
119 template< class Clock, class Duration >
120 bool wait_until(const std::chrono::time_point<Clock, Duration>& timeout_time );
121 /** \overload bool wait_until(const std::chrono::time_point<Clock, Duration>& timeout_time )*/
122 bool wait_until(const std::chrono::time_point<Clock, Duration>& timeout_time, std::error_code & ec) noexcept;
124 /** Check if this handle holds a child process.
125 * @note That does not mean, that the process is still running. It only means, that the handle does or did exist.
128 /** Same as valid, for convenience. */
129 explicit operator bool() const;
131 /** Check if the the chlid process is in any process group. */
132 bool in_group() const;
134 /** \overload bool in_group() const */
135 bool in_group(std::error_code & ec) const noexcept;
137 /** Terminate the child process.
139 * This function will cause the child process to unconditionally and immediately exit.
140 * It is implement with [SIGKILL](http://pubs.opengroup.org/onlinepubs/009695399/functions/kill.html) on posix
141 * and [TerminateProcess](https://technet.microsoft.com/en-us/library/ms686714.aspx) on windows.
146 /** \overload void terminate() */
147 void terminate(std::error_code & ec) noexcept;