handles.hpp 3.0 KB

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