Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:50:13

0001 //
0002 // process/exit_code.hpp
0003 // ~~~~~~~~~~~~~~
0004 //
0005 // Copyright (c) 2022 Klemens D. Morgenstern (klemens dot morgenstern at gmx dot net)
0006 //
0007 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0008 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0009 //
0010 
0011 #ifndef BOOST_PROCESS_V2_EXIT_CODE_HPP
0012 #define BOOST_PROCESS_V2_EXIT_CODE_HPP
0013 
0014 #include <boost/process/v2/detail/config.hpp>
0015 #include <boost/process/v2/error.hpp>
0016 
0017 #if defined(BOOST_PROCESS_V2_STANDALONE)
0018 #include <asio/associator.hpp>
0019 #include <asio/async_result.hpp>
0020 #else
0021 #include <boost/asio/associator.hpp>
0022 #include <boost/asio/async_result.hpp>
0023 #endif 
0024 
0025 #if defined(BOOST_PROCESS_V2_POSIX)
0026 #include <sys/wait.h>
0027 #endif
0028 
0029 BOOST_PROCESS_V2_BEGIN_NAMESPACE
0030 
0031 #if defined(GENERATING_DOCUMENTATION)
0032 
0033 /// The native exit-code type, usually an integral value
0034 /** The OS may have a value different from `int` to represent 
0035  * the exit codes of subprocesses. It might also 
0036  * contain additional information.
0037  */ 
0038 typedef implementation_defined native_exit_code_type;
0039 
0040 
0041 /// Check if the native exit code indicates the process is still running
0042 bool process_is_running(native_exit_code_type code);
0043 
0044 /// Obtain the portable part of the exit code, i.e. what the subprocess has returned from main.
0045 int evaluate_exit_code(native_exit_code_type code);
0046 
0047 
0048 #else
0049 
0050 #if defined(BOOST_PROCESS_V2_WINDOWS)
0051 
0052 typedef unsigned long native_exit_code_type;
0053 
0054 namespace detail
0055 {
0056 constexpr native_exit_code_type still_active = 259u;
0057 }
0058 
0059 inline bool process_is_running(native_exit_code_type code)
0060 {
0061   return code == detail::still_active;
0062 }
0063 
0064 inline int evaluate_exit_code(native_exit_code_type code)
0065 {
0066   return static_cast<int>(code);
0067 }
0068 
0069 #else
0070 
0071 typedef int native_exit_code_type;
0072 
0073 namespace detail
0074 {
0075 constexpr native_exit_code_type still_active = 0x7f;
0076 }
0077 
0078 inline bool process_is_running(int code)
0079 {
0080     return !WIFEXITED(code) && !WIFSIGNALED(code);
0081 }
0082 
0083 inline int evaluate_exit_code(int code)
0084 {
0085   if (WIFEXITED(code))
0086     return WEXITSTATUS(code);
0087   else if (WIFSIGNALED(code))
0088     return WTERMSIG(code);
0089   else
0090     return code;
0091 }
0092 
0093 #endif
0094 
0095 #endif
0096 
0097 /// @{
0098 /** Helper to subsume an exit-code into an error_code if there's no actual error isn't set.
0099  * @code {.cpp}
0100  * process proc{ctx, "exit", {"1"}};
0101  * 
0102  * proc.async_wait(
0103  *     asio::deferred(
0104  *      [&proc](error_code ec, int)
0105  *      {
0106  *        return asio::deferred.values(
0107  *                  check_exit_code(ec, proc.native_exit_code())
0108  *              );
0109  *
0110  *    [](error_code ec)
0111  *    {
0112  *      assert(ec.value() == 10);
0113  *      assert(ec.category() == error::get_exit_code_category());
0114  *    }));
0115  * 
0116  * @endcode
0117  */
0118 
0119 inline error_code check_exit_code(
0120     error_code &ec, native_exit_code_type native_code,
0121     const error_category & category = error::get_exit_code_category())
0122 {
0123   if (!ec)
0124     BOOST_PROCESS_V2_ASSIGN_EC(ec, native_code, category);
0125   return ec;
0126 }
0127 
0128 /// @}
0129 
0130 BOOST_PROCESS_V2_END_NAMESPACE
0131 
0132 #endif //BOOST_PROCESS_V2_EXIT_CODE_HPP