handles.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright (c) 2019 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_HANDLES_HPP_
  6. #define BOOST_PROCESS_HANDLES_HPP_
  7. /**
  8. * \file boost/process/handles.hpp
  9. *
  10. * Defines functions to obtain handles of the current process and limit the amount for inherited ones.
  11. */
  12. #include <boost/process/v1/detail/config.hpp>
  13. #if defined(BOOST_POSIX_API)
  14. #include <boost/process/v1/detail/posix/handles.hpp>
  15. #elif defined(BOOST_WINDOWS_API)
  16. #include <boost/process/v1/detail/windows/handles.hpp>
  17. #endif
  18. #include <boost/process/v1/detail/used_handles.hpp>
  19. namespace boost
  20. {
  21. namespace this_process
  22. {
  23. ///The native type for handles
  24. using native_handle_type = ::boost::process::v1::detail::api::native_handle_type;
  25. /**
  26. * Get a snapshot of all handles of the process (i.e. file descriptors on posix and handles on windows) of the current process.
  27. *
  28. * \note This function might not work on certain posix systems.
  29. *
  30. * \note On Windows version older than windows 8 this function will iterate all the system handles, meaning it might be quite slow.
  31. *
  32. * \warning This functionality is utterly prone to race conditions, since other threads might open or close handles.
  33. *
  34. * \return The list of all open handles of the current process
  35. */
  36. inline std::vector <native_handle_type> get_handles()
  37. {
  38. return ::boost::process::v1::detail::api::get_handles();
  39. }
  40. /** \overload std::vector<native_handle_type> get_handles() */
  41. inline std::vector <native_handle_type> get_handles(std::error_code &ec)
  42. {
  43. return ::boost::process::v1::detail::api::get_handles(ec);
  44. }
  45. /** Determines if a given handle is a a stream-handle, i.e. any handle that can be used with read and write functions.
  46. * Stream handles include pipes, regular files and sockets.
  47. *
  48. * \return Indicates if it's a stream handle.
  49. */
  50. inline bool is_stream_handle(native_handle_type handle)
  51. {
  52. return ::boost::process::v1::detail::api::is_stream_handle(handle);
  53. }
  54. /** \overload bool is_stream_handle(native_handle_type handle) */
  55. inline bool is_stream_handle(native_handle_type handle, std::error_code &ec)
  56. {
  57. return ::boost::process::v1::detail::api::is_stream_handle(handle, ec);
  58. }
  59. }
  60. namespace process
  61. {
  62. BOOST_PROCESS_V1_INLINE
  63. namespace v1
  64. {
  65. namespace detail
  66. {
  67. using limit_handles_ = ::boost::process::v1::detail::api::limit_handles_;
  68. }
  69. /**
  70. * The limit_handles property sets all properties to be inherited only explicitly. It closes all unused file-descriptors on posix after the fork and
  71. * removes the inherit flags on windows.
  72. *
  73. * \note This is executed after the fork on posix.
  74. *
  75. * \code{.cpp}
  76. * system("gcc", limit_handles);
  77. * \endcode
  78. *
  79. * Since limit also closes the standard handles unless they are explicitly redirected they can be ignored by `limit_handles` in the following way.
  80. *
  81. * \code{.cpp}
  82. * system("gcc", limit_handles.allowStd())
  83. * \endcode
  84. *
  85. */
  86. const static ::boost::process::v1::detail::api::limit_handles_ limit_handles;
  87. }
  88. }
  89. }
  90. #endif //BOOST_PROCESS_HANDLES_HPP_