exit_code.hpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. //
  2. // process/exit_code.hpp
  3. // ~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2022 Klemens D. Morgenstern (klemens dot morgenstern at gmx dot net)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef BOOST_PROCESS_V2_EXIT_CODE_HPP
  11. #define BOOST_PROCESS_V2_EXIT_CODE_HPP
  12. #include <boost/process/v2/detail/config.hpp>
  13. #include <boost/process/v2/error.hpp>
  14. #if defined(BOOST_PROCESS_V2_STANDALONE)
  15. #include <asio/associator.hpp>
  16. #include <asio/async_result.hpp>
  17. #else
  18. #include <boost/asio/associator.hpp>
  19. #include <boost/asio/async_result.hpp>
  20. #endif
  21. #if defined(BOOST_PROCESS_V2_POSIX)
  22. #include <sys/wait.h>
  23. #endif
  24. BOOST_PROCESS_V2_BEGIN_NAMESPACE
  25. #if defined(GENERATING_DOCUMENTATION)
  26. /// The native exit-code type, usually an integral value
  27. /** The OS may have a value different from `int` to represent
  28. * the exit codes of subprocesses. It might also
  29. * contain additional information.
  30. */
  31. typedef implementation_defined native_exit_code_type;
  32. /// Check if the native exit code indicates the process is still running
  33. bool process_is_running(native_exit_code_type code);
  34. /// Obtain the portable part of the exit code, i.e. what the subprocess has returned from main.
  35. int evaluate_exit_code(native_exit_code_type code);
  36. #else
  37. #if defined(BOOST_PROCESS_V2_WINDOWS)
  38. typedef unsigned long native_exit_code_type;
  39. namespace detail
  40. {
  41. constexpr native_exit_code_type still_active = 259u;
  42. }
  43. inline bool process_is_running(native_exit_code_type code)
  44. {
  45. return code == detail::still_active;
  46. }
  47. inline int evaluate_exit_code(native_exit_code_type code)
  48. {
  49. return static_cast<int>(code);
  50. }
  51. #else
  52. typedef int native_exit_code_type;
  53. namespace detail
  54. {
  55. constexpr native_exit_code_type still_active = 0x7f;
  56. }
  57. inline bool process_is_running(int code)
  58. {
  59. return !WIFEXITED(code) && !WIFSIGNALED(code);
  60. }
  61. inline int evaluate_exit_code(int code)
  62. {
  63. if (WIFEXITED(code))
  64. return WEXITSTATUS(code);
  65. else if (WIFSIGNALED(code))
  66. return WTERMSIG(code);
  67. else
  68. return code;
  69. }
  70. #endif
  71. #endif
  72. /** Convert the exit-code in a completion into an error if the actual error isn't set.
  73. * @code {.cpp}
  74. * process proc{ctx, "exit", {"1"}};
  75. *
  76. * proc.async_wait(code_as_error(
  77. * [](error_code ec)
  78. * {
  79. * assert(ec.value() == 10);
  80. * assert(ec.category() == error::get_exit_code_category());
  81. * }));
  82. *
  83. * @endcode
  84. */
  85. template<typename CompletionToken>
  86. struct code_as_error_t
  87. {
  88. CompletionToken token_;
  89. const error_category & category;
  90. template<typename Token_>
  91. code_as_error_t(Token_ && token, const error_category & category)
  92. : token_(std::forward<Token_>(token)), category(category)
  93. {
  94. }
  95. };
  96. /// Deduction function for code_as_error_t.
  97. template<typename CompletionToken>
  98. code_as_error_t<CompletionToken> code_as_error(
  99. CompletionToken && token,
  100. const error_category & category = error::get_exit_code_category())
  101. {
  102. return code_as_error_t<typename std::decay<CompletionToken>::type>(
  103. std::forward<CompletionToken>(token), category);
  104. };
  105. namespace detail
  106. {
  107. template<typename Handler>
  108. struct code_as_error_handler
  109. {
  110. typedef void result_type;
  111. template<typename H>
  112. code_as_error_handler(H && h, const error_category & category)
  113. : handler_(std::forward<H>(h)), category(category)
  114. {
  115. }
  116. void operator()(error_code ec, native_exit_code_type code)
  117. {
  118. if (!ec)
  119. BOOST_PROCESS_V2_ASSIGN_EC(ec, code, category)
  120. std::move(handler_)(ec);
  121. }
  122. Handler handler_;
  123. const error_category & category;
  124. };
  125. }
  126. BOOST_PROCESS_V2_END_NAMESPACE
  127. #if !defined(BOOST_PROCESS_V2_STANDALONE)
  128. namespace boost
  129. {
  130. #endif
  131. namespace asio
  132. {
  133. template <typename CompletionToken>
  134. struct async_result<
  135. BOOST_PROCESS_V2_NAMESPACE::code_as_error_t<CompletionToken>,
  136. void(BOOST_PROCESS_V2_NAMESPACE::error_code,
  137. BOOST_PROCESS_V2_NAMESPACE::native_exit_code_type)>
  138. {
  139. using signature = void(BOOST_PROCESS_V2_NAMESPACE::error_code);
  140. using return_type = typename async_result<CompletionToken, void(BOOST_PROCESS_V2_NAMESPACE::error_code)>::return_type;
  141. template <typename Initiation>
  142. struct init_wrapper
  143. {
  144. init_wrapper(Initiation init)
  145. : initiation_(std::move(init))
  146. {
  147. }
  148. template <typename Handler, typename... Args>
  149. void operator()(
  150. Handler && handler,
  151. const BOOST_PROCESS_V2_NAMESPACE::error_category & cat,
  152. Args && ... args)
  153. {
  154. std::move(initiation_)(
  155. BOOST_PROCESS_V2_NAMESPACE::detail::code_as_error_handler<typename decay<Handler>::type>(
  156. std::forward<Handler>(handler), cat),
  157. std::forward<Args>(args)...);
  158. }
  159. Initiation initiation_;
  160. };
  161. template <typename Initiation, typename RawCompletionToken, typename... Args>
  162. static BOOST_PROCESS_V2_INITFN_DEDUCED_RESULT_TYPE(CompletionToken, signature,
  163. (async_initiate<CompletionToken, signature>(
  164. declval<init_wrapper<typename decay<Initiation>::type> >(),
  165. declval<CompletionToken&>(),
  166. declval<BOOST_ASIO_MOVE_ARG(Args)>()...)))
  167. initiate(
  168. Initiation && initiation,
  169. RawCompletionToken && token,
  170. Args &&... args)
  171. {
  172. return async_initiate<CompletionToken, signature>(
  173. init_wrapper<typename decay<Initiation>::type>(
  174. std::forward<Initiation>(initiation)),
  175. token.token_,
  176. token.category,
  177. std::forward<Args>(args)...);
  178. }
  179. };
  180. template<template <typename, typename> class Associator, typename Handler, typename DefaultCandidate>
  181. struct associator<Associator,
  182. BOOST_PROCESS_V2_NAMESPACE::detail::code_as_error_handler<Handler>, DefaultCandidate>
  183. : Associator<Handler, DefaultCandidate>
  184. {
  185. static typename Associator<Handler, DefaultCandidate>::type get(
  186. const BOOST_PROCESS_V2_NAMESPACE::detail::code_as_error_handler<Handler> & h,
  187. const DefaultCandidate& c = DefaultCandidate()) noexcept
  188. {
  189. return Associator<Handler, DefaultCandidate>::get(h.handler_, c);
  190. }
  191. };
  192. }
  193. #if !defined(BOOST_PROCESS_V2_STANDALONE)
  194. } // boost
  195. #endif
  196. #endif //BOOST_PROCESS_V2_EXIT_CODE_HPP