child.hpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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
  7. //
  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)
  10. /**
  11. * \file boost/process/child.hpp
  12. *
  13. * Defines a child process class.
  14. */
  15. #ifndef BOOST_PROCESS_CHILD_HPP
  16. #define BOOST_PROCESS_CHILD_HPP
  17. #include <boost/process/detail/config.hpp>
  18. #include <boost/process/detail/child_decl.hpp>
  19. #include <boost/process/detail/execute_impl.hpp>
  20. #if defined(BOOST_POSIX_API)
  21. #include <boost/process/posix.hpp>
  22. #endif
  23. namespace boost {
  24. ///The main namespace of boost.process.
  25. namespace process {
  26. template<typename ...Args>
  27. child::child(Args&&...args)
  28. : child(::boost::process::detail::execute_impl(std::forward<Args>(args)...)) {}
  29. ///Typedef for the type of an pid_t
  30. typedef ::boost::process::detail::api::pid_t pid_t;
  31. #if defined(BOOST_PROCESS_DOXYGEN)
  32. /** The main class to hold a child process. It is simliar to [std::thread](http://en.cppreference.com/w/cpp/thread/thread),
  33. * in that it has a join and detach function.
  34. *
  35. * @attention The destructor will call terminate on the process if not joined or detached without any warning.
  36. *
  37. */
  38. class child
  39. {
  40. /** Type definition for the native process handle. */
  41. typedef platform_specific native_handle_t;
  42. /** Construct the child from a pid.
  43. *
  44. * @attention There is no guarantee that this will work. The process need the right access rights, which are very platform specific.
  45. */
  46. explicit child(pid_t & pid) : _child_handle(pid) {};
  47. /** Move-Constructor.*/
  48. child(child && lhs);
  49. /** Construct a child from a property list and launch it
  50. * The standard version is to create a subprocess, which will spawn the process.
  51. */
  52. template<typename ...Args>
  53. explicit child(Args&&...args);
  54. /** Construct an empty child. */
  55. child() = default;
  56. /** Move assign. */
  57. child& operator=(child && lhs);
  58. /** Detach the child, i.e. let it run after this handle dies. */
  59. void detach();
  60. /** Join the child. This just calls wait, but that way the naming is similar to std::thread */
  61. void join();
  62. /** Check if the child is joinable. */
  63. bool joinable();
  64. /** Destructor.
  65. * @attention Will call terminate (without warning) when the child was neither joined nor detached.
  66. */
  67. ~child();
  68. /** Get the native handle for the child process. */
  69. native_handle_t native_handle() const;
  70. /** Get the exit_code. The return value is without any meaning if the child wasn't waited for or if it was terminated. */
  71. int exit_code() const;
  72. /** Get the Process Identifier. */
  73. pid_t id() const;
  74. /** Get the native, uninterpreted exit code. The return value is without any meaning if the child wasn't waited
  75. * for or if it was terminated. */
  76. int native_exit_code() const;
  77. /** Check if the child process is running. */
  78. bool running();
  79. /** \overload void running() */
  80. bool running(std::error_code & ec) noexcept;
  81. /** Wait for the child process to exit. */
  82. void wait();
  83. /** \overload void wait() */
  84. void wait(std::error_code & ec) noexcept;
  85. /** Wait for the child process to exit for a period of time.
  86. * \return True if child exited while waiting.
  87. */
  88. template< class Rep, class Period >
  89. bool wait_for (const std::chrono::duration<Rep, Period>& rel_time);
  90. /** \overload bool wait_for(const std::chrono::duration<Rep, Period>& rel_time) */
  91. bool wait_for (const std::chrono::duration<Rep, Period>& rel_time, std::error_code & ec) noexcept;
  92. /** Wait for the child process to exit until a point in time.
  93. * \return True if child exited while waiting.*/
  94. template< class Clock, class Duration >
  95. bool wait_until(const std::chrono::time_point<Clock, Duration>& timeout_time );
  96. /** \overload bool wait_until(const std::chrono::time_point<Clock, Duration>& timeout_time )*/
  97. bool wait_until(const std::chrono::time_point<Clock, Duration>& timeout_time, std::error_code & ec) noexcept;
  98. /** Check if this handle holds a child process.
  99. * @note That does not mean, that the process is still running. It only means, that the handle does or did exist.
  100. */
  101. bool valid() const;
  102. /** Same as valid, for convenience. */
  103. explicit operator bool() const;
  104. /** Check if the the chlid process is in any process group. */
  105. bool in_group() const;
  106. /** \overload bool in_group() const */
  107. bool in_group(std::error_code & ec) const noexcept;
  108. /** Terminate the child process.
  109. *
  110. * This function will cause the child process to unconditionally and immediately exit.
  111. * It is implement with [SIGKILL](http://pubs.opengroup.org/onlinepubs/009695399/functions/kill.html) on posix
  112. * and [TerminateProcess](https://technet.microsoft.com/en-us/library/ms686714.aspx) on windows.
  113. *
  114. */
  115. void terminate();
  116. /** \overload void terminate() */
  117. void terminate(std::error_code & ec) noexcept;
  118. };
  119. #endif
  120. }}
  121. #endif