is_running.hpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright (c) 2106 Klemens D. Morgenstern
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_PROCESS_DETAIL_POSIX_IS_RUNNING_HPP
  6. #define BOOST_PROCESS_DETAIL_POSIX_IS_RUNNING_HPP
  7. #include <boost/process/detail/config.hpp>
  8. #include <boost/process/detail/posix/child_handle.hpp>
  9. #include <system_error>
  10. #include <sys/wait.h>
  11. namespace boost { namespace process { namespace detail { namespace posix {
  12. // Use the "stopped" state (WIFSTOPPED) to indicate "not terminated".
  13. // This bit arrangement of status codes is not guaranteed by POSIX, but (according to comments in
  14. // the glibc <bits/waitstatus.h> header) is the same across systems in practice.
  15. constexpr int still_active = 0x017f;
  16. static_assert(WIFSTOPPED(still_active), "Expected still_active to indicate WIFSTOPPED");
  17. static_assert(!WIFEXITED(still_active), "Expected still_active to not indicate WIFEXITED");
  18. static_assert(!WIFSIGNALED(still_active), "Expected still_active to not indicate WIFSIGNALED");
  19. static_assert(!WIFCONTINUED(still_active), "Expected still_active to not indicate WIFCONTINUED");
  20. inline bool is_running(int code)
  21. {
  22. return !WIFEXITED(code) && !WIFSIGNALED(code);
  23. }
  24. inline bool is_running(const child_handle &p, int & exit_code, std::error_code &ec) noexcept
  25. {
  26. int status;
  27. auto ret = ::waitpid(p.pid, &status, WNOHANG);
  28. if (ret == -1)
  29. {
  30. if (errno != ECHILD) //because it no child is running, than this one isn't either, obviously.
  31. ec = ::boost::process::detail::get_last_error();
  32. return false;
  33. }
  34. else if (ret == 0)
  35. return true;
  36. else
  37. {
  38. ec.clear();
  39. if (!is_running(status))
  40. exit_code = status;
  41. return false;
  42. }
  43. }
  44. inline bool is_running(const child_handle &p, int & exit_code)
  45. {
  46. std::error_code ec;
  47. bool b = is_running(p, exit_code, ec);
  48. boost::process::detail::throw_error(ec, "waitpid(2) failed in is_running");
  49. return b;
  50. }
  51. inline int eval_exit_status(int code)
  52. {
  53. if (WIFEXITED(code))
  54. {
  55. return WEXITSTATUS(code);
  56. }
  57. else if (WIFSIGNALED(code))
  58. {
  59. return WTERMSIG(code);
  60. }
  61. else
  62. {
  63. return code;
  64. }
  65. }
  66. }}}}
  67. #endif